6
$\begingroup$

I always wondered if I could start NDSolve from an intermediate time step.

What I mean is, in the code sample below, if I were to run my solution from tmin=0 to tmax=2 and then I realize that my solution hasn't converged yet, could I just change my tmin to 2 and then proceed with a larger tmax (in this case the solution coverges [attains steady state in this example] at tmax=5).

This is because very often I solve 4th order Nonlinear PDEs with NDSolve, some of which take over 70-80 minutes to complete (as I run them for long times of a million time steps or so) and then I realize that the solution hasn't converged yet. So can I just, as in my presumed working example, change my tmin to my tmax from the previous run and change my tmax to a larger run?

tmin = 0;
tmax = 5;
sol = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, 
    u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, tmin, tmax}, {x, 0, 5}];
Plot3D[Evaluate[u[t, x] /. sol], {t, tmin, tmax}, {x, 0, 5}, 
 PlotRange -> All]
$\endgroup$
  • $\begingroup$ By "converged" you mean "reached a steady state"? Regardless, you might use the old solution to give initial and boundary values for starting anew. $\endgroup$ – Daniel Lichtblau May 14 '12 at 23:31
  • $\begingroup$ @DanielLichtblau Yes, steady state. $\endgroup$ – dearN May 15 '12 at 0:25
8
$\begingroup$

You could integrate up to some intermediate time, tintermediate, and then feed the result as initial conditions to the solver to propagate from tintermediate to tmax, like so:

tmin = 0;
tintermediate = 2;
tmax = 5;
sol = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, 
    u[t, 0] == Sin[t], u[t, 5] == 0}, 
   u, {t, tmin, tintermediate}, {x, 0, 5}];
sol2 = NDSolve[{D[u[t, x], t] == D[u[t, x], x, x],
    u[tintermediate, x] \[Equal] First@(u[tintermediate, x] /. sol), 
    u[t, 0] == Sin[t], u[t, 5] == 0}, 
   u, {t, tintermediate, tmax}, {x, 0, 5}];
$\endgroup$
  • $\begingroup$ Thanks! Just so that I understand this right sol2 is the solution from t=2 to t=5...? $\endgroup$ – dearN May 15 '12 at 0:31
  • $\begingroup$ Yes, that is what it is $\endgroup$ – acl May 15 '12 at 0:32
  • $\begingroup$ Ok. Thanks! I actually just learned the use of @! $\endgroup$ – dearN May 15 '12 at 0:36
  • $\begingroup$ ah yes. glad to be of help! $\endgroup$ – acl May 15 '12 at 0:45
17
$\begingroup$

As it turns out, the designers of NDSolve[] have precisely anticipated this sort of use; this is where you can use the NDSolve`StateData framework.

To use acl's example:

(* prepare PDE *)
state = First[NDSolve`ProcessEquations[{D[u[t, x], t] == D[u[t, x], x, x], 
 u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, t, {x, 0, 5}]];


(* go up to t = 2 *)
NDSolve`Iterate[state, {0, 2}]
NDSolve`ProcessSolutions[state, "Forward"]

(* go up to t = 5 *)
NDSolve`Iterate[state, 5];
NDSolve`ProcessSolutions[state, "Forward"]

See the docs for more details.

$\endgroup$
  • $\begingroup$ good point, +1! $\endgroup$ – acl May 15 '12 at 0:46
  • $\begingroup$ Awesome! I'll look into the documentation. This is quite nifty! (Not taking anything away from the previous answerer :)) $\endgroup$ – dearN May 15 '12 at 12:16
  • $\begingroup$ Why am I not able to plot the solution u[t,x] with Plot3D[ Evaluate[u[t, x]], {t, 2, 5}, {x, 0, 5} ] OR Plot3D[ u[t, x]/.sol, {t, 2, 5}, {x, 0, 5} ] where sol= NDSolve`ProcessSolutions[state, "Forward"] $\endgroup$ – dearN May 22 '12 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.