# How to use initial fixed timestep, then decrease it according to dependent variable, while spatial stepsize is fixed

I am trying to solve an advection equation. I want to force constant spatial step size (x dimension) with the “MethodOfLines” option, whereas I want to use initially fixed time step size 0.01 then decrease the time step size to be (0.001, 0.0001) when the minimum value of u(t,x) is less than (-6, -7). I tried the following code, and adopt ImplicitRungeKutta method to integrate with respect to time. I even can not set time step size in the ImplicitRungeKutta method. I try this

WhenEvent[{u[t, x]<=-6}, stepsize -> 0.001];
WhenEvent[{u[t, x]<=-7}, stepsize -> 0.0001];


But how can I combine this event trigger in my time integration method? Anyone can help me?

mdfun = First[u /. NDSolve[{D[u[t, x], t] ==
0.5 D[u[t, x], x, x] + u[t, x] D[u[t, x], x],
u[t, -Pi] == u[t, Pi] == 0, u[0, x] == Sin[x]},
u, {t, 0, 10}, {x, -Pi, Pi},
Method -> {"MethodOfLines",
Method -> {"ImplicitRungeKutta", DifferenceOrder -> 2,
"ImplicitSolver" -> {"Newton", "IterationSafetyFactor" -> 1}},
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 100, "MaxPoints" -> 100,
"DifferenceOrder" -> 2}}]]


Then Plot it

Plot3D[Evaluate[mdfun[t, x]], {t, 0, 10}, {x, -Pi, Pi}, PlotPoints -> 100, PlotRange -> All]


This is how you can set the time step:

mdfun = NDSolveValue[{D[u[t, x], t] == 0.5 D[u[t, x], x, x] + u[t, x] D[u[t, x], x],
u[t, -Pi] == u[t, Pi] == 0, u[0, x] == Sin[x], WhenEvent[u[t, Pi/2] > -.1, "StopIntegration"]},
u, {t, 0, 10}, {x, -Pi, Pi},
Method -> \
{"MethodOfLines",(*"DiscretizedMonitorVariables"->True,*)
Method -> {"FixedStep", "StepSize" -> .5, Method -> "ImplicitRungeKutta"},
"SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> 100, "MaxPoints" -> 100}},
StepMonitor :> Print[t]];


Uncomment the DiscretizedMonitorVariables if you want to use something like Min[u[t, x]] in WhenEvent.

But it seems you can't change the method parameters with WhenEvent. At least I don't know how. The only thing I can suggest is to write the function, which will use new ndsolve after each whenevent happens and starting with time, when last ndsolve stopped. Ask if you need such function.

• Yes, it is helpful. It seems what I want. In fact, I am a MMA beginner. I have learned the use of NDSolveValue. :) You mean I can use the result given by NDSloveValue and only change the stepsize in the next run or runs. However, Sometimes I want to judge where the min value of u[t,x] exceed a certain value,say, min(u)<0.01. How can I achieve it. In addition, How can I know the exact value of min(u) when the integration just is stopped and the value of independent variable x where the min(u) is reached (although it is obvious in this equation). Thanks a lot! – Enter Sep 13 '14 at 17:30