I found something this tutorial for method of line doesn't tell us.
Consider the following toy example:
eqn = With[{u = u[x, t]},
D[u, t] == D[u, x] + D[u, {x, 2}] + D[u, {x, 3}] - D[u, {x, 4}]];
ic = u[x, 0] == 0;
bc = {u[0, t] == 0, u[1, t] == 0, D[u[x, t], x] == 0 /. {{x -> 0}, {x -> 1}}};
NDSolve[{eqn, ic, bc},
u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}]
Guess what difference order is chosen when those spatial derivatives (in this case $\frac{\partial u}{\partial x}$, $\frac{\partial ^2u}{\partial x^2}$, $\frac{\partial ^3u}{\partial x^3}$, $\frac{\partial ^4u}{\partial x^4}$) are discretized?
"What a needless question! The order is 4
, as we set with "DifferenceOrder" -> 4
! " About an hour ago, I thought so, too. But it's not true. Let's check the difference formula generated by NDSolve
:
state = First@NDSolve`ProcessEquations[{eqn, ic, bc},
u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}];
funcexpr = state["NumericalFunction"]["FunctionExpression"]
Introduction for
NDSolve`ProcessEquations
can be found intutorial/NDSolveStateData
andtutorial/NDSolveDAE
.
Then check the "DifferenceOrder"
of these NDSolve`FiniteDifferenceDerivativeFunction
:
Head[#]@"DifferenceOrder" & /@ funcexpr[[2, 1]]
(* {{7}, {6}, {5}, {4}} *)
The order is not 4
! Similarly, we can verify that it's the same case for PDE system:
eqn =
With[{u = u[x, t], v = v[x, t]},
{D[u, t] == D[u, x] + D[u, {x, 2}] + D[u, {x, 3}] - D[u, {x, 4}],
D[v, t] == D[v, x] + D[v, {x, 2}]}];
ic = {u[x, 0] == 0, v[x, 0] == 0};
bc = {{u[0, t] == 0, u[1, t] == 0,
D[u[x, t], x] == 0 /. {{x -> 0}, {x -> 1}}},
{v[0, t] == 0, v[1, t] == 0}};
state =
First@NDSolve`ProcessEquations[{eqn, ic, bc}, u, {x, 0, 1}, {t, 0, 2},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "DifferenceOrder" -> 4}}];
funcexpr = state["NumericalFunction"]["FunctionExpression"]
Head[#]@"DifferenceOrder" & /@ funcexpr[[2, 1]]
(* {{7}, {7}, {6}, {6}, {5}, {4}} *)
So, for a PDE or PDE system whose maximum spatial differential order is omax
, when "DifferenceOrder" -> n
is set for "TensorProductGrid"
, the actual difference order for m
-order spatial derivative is omax + n - m
.
In certain cases, this design seems to cause trouble, here's an example.
To make this post a question, I'd like to ask:
Why
NDSolve
chooses this design?If the 1st question is too hard, is there a easy way (e.g. a hidden option) to make
NDSolve
use the same difference order for every spatial derivative?