MethodOfLines.jl icon indicating copy to clipboard operation
MethodOfLines.jl copied to clipboard

Missing defaults from variable map when discretizing PDE

Open anandijain opened this issue 3 years ago • 5 comments

This code used to give ExtraEquations error, but now it's missing defaults. It's quite possible I'm missing something obvious. Given that its uniformly spacing the discretization, I imagine discretize could just figure it out the defaults, but I can't seem to pass defaults in a way that gets this to work.

using ModelingToolkit, DifferentialEquations, MethodOfLines, DomainSets# , NonlinearSolve

@parameters t S = 10 r = 0.05 σ = 0.3 K = 10
@variables C(..)

dt = Differential(t)
dS = Differential(S)

S_min = 0.
S_max = 20.0
T = 1.

domains = [S ∈ Interval(S_min, S_max), 
    t ∈ Interval(0, T)]

bcs = [
    C(S, T) ~ max(S - K, 0),
    C(S_max, t) ~ S_max-K,
    C(0, t) ~ 0,
]

eqs = [dt(C(S, t)) + 1 / 2 * σ^2 * S^2 * (dS^2)(C(S, t)) + r * S * dS(C(S, t)) - r * C(S, t) ~ 0]

pde = PDESystem(
    eqs,
    bcs,
    domains,
    [t, S],
    [C(S, t)]
    ;name=:pde)

Nt = 10
ds = (S_max - S_min)/5

discretization = MOLFiniteDifference([S => ds], t)
prob = discretize(pde, discretization) # ArgumentError: Term{Real, Base.ImmutableDict{DataType, Any}}[C[2](t), C[3](t), C[4](t), C[5](t)] are missing from the variable map.
  [0c46a032] DifferentialEquations v7.1.0
  [5b8099bc] DomainSets v0.5.9
  [94925ecb] MethodOfLines v0.3.0
  [961ee093] ModelingToolkit v8.12.0
  [8913a72c] NonlinearSolve v0.3.18

Thanks

anandijain avatar May 25 '22 22:05 anandijain

this is the almost equivalent mathematica code that solves

S0 = 10
T = 1
sigma = 0.3
r = 0.05
K = 10

sol = NDSolve[
    {D[V[S, t], t] + r*S*D[V[S, t], S]+1/2*sigma^2*S^2*D[V[S, t], {S, 2}] -r*V[S, t]==0, 
    V[S, T] == Max[S-K, 0], 
    V[1000,t]==1000-K, 
    V[0, t] == 0},V, {S, 0.1, 1000}, {t, 0, T} ]
Plot3D[V[S, t] /. sol, {S, 0, 100}, {t, 0, 1},AxesLabel -> Automatic]

anandijain avatar May 25 '22 23:05 anandijain

Supplying time boundary conditions at the end of time is not supported, please use a change of variables so that you can supply this condition at the initial time.

xtalax avatar May 26 '22 12:05 xtalax

Another thing, in the current version you will encounter instability when you have an advection term on the lhs, please move this term to the rhs.

This is fixed in an upcoming PR

xtalax avatar May 26 '22 12:05 xtalax

Thanks Alex. I don't really know PDEs, I just wanted to try out the package. What do you mean do a change of variables? I can't find any docs on it

Is it planned to eventually do the change automatically so that I can continue to live in ignorance?

anandijain avatar May 26 '22 15:05 anandijain

You're welcome, you can message me on the slack anytime and I'll try to get back to you.

Try substituting in t = -τ to all your equations/bcs and adjust your domain accordingly, that way your time boundary condition will be at -T which will be the initial time, so it should work.

And yes, there is a solution interface in the works, so we're going to feed information about the PDE through to the solution object, so I could automatically perform this substitution and feed through the fact that this has happened to automatically undo it. Ignorance will hopefully be bliss!

xtalax avatar May 26 '22 15:05 xtalax