Skip to main content

performance tuning - How to tell mathematica not to resolve stiffness issues


Very often I solve partial differential equations that are nonlinear and could be up to 4th order. In these cases, it is usual for the solution determined by NDSolve to be stiff during a later stage. What I suspect NDSolve does in this case is to resolve the stiffness until the error/local accuracy is very poor. That is when it quits the problem and gives you an Interpolating function polynomial.


Whilst using the BDF method to MaxOrder of 1 for instance, is there someway to tell Mathematica to quit as soon as stiffness is encountered in the solution so that I save time? I don't want to resolve the stiff portion and just stop my solution just as it gets stiff.


The below example looks like a mess in plain text but it copies fine. It gets stiff at t=4806. However, is lots of problems, NDSolve lingers at the time at which stiffness is achieved to try and resolve the features that I would like to circumvent completely.


I will obv. look into the stiffness switching stuff again.


Example


{xMin,xMax}={-4\[Pi]/0.0677,4\[Pi]/0.0677};

k=0.0677/4;


TMax=5000;

uSolpbc[t_,x_]=u[t,x]/.NDSolve[{\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(u[t, x]\)\)==-100\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\(u[t, x]\), \(3\)]\
\*SubscriptBox[\(\[PartialD]\), \(x, x, x\)]u[t, x])\)\)+1/3 \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\(u[t, x]\), \(3\)]\

\*SubscriptBox[\(\[PartialD]\), \(x\)]u[t, x])\)\)-5 \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\((
\*FractionBox[\(u[t, x]\), \(1 + u[t, x]\)])\), \(2\)]\
\*SubscriptBox[\(\[PartialD]\), \(x\)]u[t, x])\)\),u[0,x]==1-0.1 Cos[k*x],
u[t,xMin]== u[t,xMax],
Derivative[0,1]u[t,xMin]==Derivative[0,1]u[t,xMax],
Derivative[0,2]u[t,xMin]==Derivative[0,2]u[t,xMax],
Derivative[0,3]u[t,xMin]==Derivative[0,3]u[t,xMax]},
u,

{t,0,TMax},
{x,xMin,xMax},
MaxStepFraction->1/150][[1]]

Answer



Having played around with your example I don't think that there is any method switching involved at all, as that only seems to be the case when Method is explicitly set to "StiffnessSwitching", which you didn't do (you also haven't specified "BDF" and I'm not sure what NDSolve actually chose...). What you see is that NDSolve just makes the step size smaller and smaller because the errors get worse and worse. As you vary MaxStepFraction you will find that the point where it complains about an effectively zero step size will change. This I think you already have found yourself and thus I agree now that my comment about stiffness switching wasn't very useful.


You could stop the integration when the step size gets smaller than a reasonable amount, e.g. with something like this (there might be better ways to achieve the same thing):


pde = {\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(u[t, x]\)\) == -100 \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\(u[t, x]\), \(3\)]

\*SubscriptBox[\(\[PartialD]\), \(x, x, x\)]u[t, x])\)\) + 1/3 \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\(u[t, x]\), \(3\)]
\*SubscriptBox[\(\[PartialD]\), \(x\)]u[t, x])\)\) - 5 \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((
\*SuperscriptBox[\((
\*FractionBox[\(u[t, x]\), \(1 + u[t, x]\)])\), \(2\)]
\*SubscriptBox[\(\[PartialD]\), \(x\)]u[t, x])\)\),
u[0, x] == 1 - 0.1 Cos[k*x], u[t, xMin] == u[t, xMax],
Derivative[0, 1][u][t, xMin] == Derivative[0, 1][u][t, xMax],

Derivative[0, 2][u][t, xMin] == Derivative[0, 2][u][t, xMax],
Derivative[0, 3][u][t, xMin] == Derivative[0, 3][u][t, xMax]};

{xMin, xMax} = {-4 \[Pi]/0.0677, 4 \[Pi]/0.0677};
k = 0.0677/4;
TMax = 5000;
thisstep = 0;
laststep = 0;
Timing[
uSolpbc = u /. NDSolve[pde, u, {t, 0, TMax}, {x, xMin, xMax},

MaxStepFraction -> 1/150,
StepMonitor :> (
laststep = thisstep; thisstep = t;
stepsize = thisstep - laststep;
),
Method -> {"MethodOfLines",
Method -> {"EventLocator",
"Event" :> (If[stepsize < 10^-4, 0, 1])}}
][[1]]
]


unfortunately for the given problem this will be even slower than letting NDSolve run into "effectively zero stepsize". It might be different for other problems, but using event locators is in general rather slow, so I wouldn't have much hope to achieve any speedup this way except when the time for a single step is much larger than in this example. You could use something like If[t > 4500, Print[t -> stepsize]] within the StepMonitor to see what happens and check that it does what it is supposed to do.


A much easier approach is to just limit the maximal numbers of steps to be used which will also limit the maximal runtime. It is of course depending on the system you solve how far that number of steps will take you. So it probably needs some estimation about what a good value would be, for your example a value of 200 already seems to show the characteristics of the solution and is about twice as fast:


{xMin, xMax} = {-4 \[Pi]/0.0677, 4 \[Pi]/0.0677};
k = 0.0677/4;
TMax = 5000;
thisstep = 0;
laststep = 0;
Timing[Quiet[
uSolpbc = u /. NDSolve[pde, u, {t, 0, TMax}, {x, xMin, xMax},

MaxStepFraction -> 1/150,
MaxSteps -> 200
][[1]], NDSolve::mxst]]

Plot3D[uSolpbc[t, x], {t,
0, (uSolpbc@"Domain")[[1, 2]]}, {x, -185.618, 185.618},
PlotRange -> All]

Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...