20. Automatic step-size control#
Some ODEs have local features, which require locally small step sizes. For other regions one can use larger step sizes, and save computations. A simple adaptive algorithm is as follows.
20.1. Estimating the local error#
To estimate the local error, we apply two different time stepping methods, and take the difference as error estimation. Method 1 has order \(p\), method 2 is more accurate. Then the difference estimates the local error of method 1, which behaves like \(C \tau^{p+1}\). The error in one time-step should be about \(\tau \times \text{tol}\).
while t < Tend
tau = min(tau, Tend-t);
y1 = y;
method1.doStep(tau, y1);
y2 = y;
method2.doStep(tau, y2);
double errest = norm(y2-y1);
if errest < tau*tol
t = t + tau;
y = y2; // accept step, continue with the more accurate solution
tau = tau * 1.5; // increase step size
else
tau = tau * 0.5; // decrease step size
A better way to increase the time-step is
which aims at \(\text{errest} \approx 0.5 \, \tau_{\text{new}} \, \text{tol}\) in the next step.
If Newton’s method did not converge, we also try with a smaller time-step:
try {
method1.doStep(tau,y1);
method2.doStep(tau,y2);
}
catch (std::exception & e) { // Newton did not converge
tau *= 0.5;
continue;
}
20.1.1. In practice#
Production codes use a few more ingredients:
The tolerance is often posed per step, i.e. accept if \(\text{errest} \leq \text{tol}\). Then \(\tau_{\text{new}} = \tau \, \big( \text{fac} \cdot \text{tol} / \text{errest} \big)^{1/(p+1)}\), with a safety factor like \(\text{fac} = 0.9\).
The change of the time-step is limited, for example \(0.2 \, \tau \leq \tau_{\text{new}} \leq 5 \, \tau\).
If the components of \(y\) have very different magnitudes (in the chemical example below from \(10^{-11}\) to \(10^{-1}\)), the Euclidean norm only sees the large components. One uses absolute and relative tolerances, and the scaled norm
\[ \text{errest} = \Big( \frac{1}{d} \sum_{k=1}^d \Big( \frac{y_{2,k} - y_{1,k}}{\text{atol} + \text{rtol} \, |y_k|} \Big)^2 \Big)^{1/2}, \]which is compared with \(\text{tol} = 1\).
20.2. Embedded Runge-Kutta methods#
Computing two independent methods is expensive. Embedded Runge-Kutta methods share all stages \(k^j\), i.e. the \(a\) and \(c\) coefficients, and use two different weight vectors \(b\) and \(\hat b\):
The weights are chosen such that \(y_{n+1}\) has order \(p+1\), and \(\hat y_{n+1}\) has order \(p\). The error estimate
comes for free, no additional evaluations of \(f\) are needed. The estimate is for the lower order solution \(\hat y_{n+1}\), so \(p\) is the exponent in the step-size formula, but we continue with the more accurate \(y_{n+1}\). The Butcher tableau is extended by one row for \(\hat b\).
The simplest example is the Heun-Euler pair of orders 2 and 1:
The Bogacki-Shampine pair has orders 3 and 2:
The last row of \(a\) coincides with \(b\). Thus the last stage is \(k^3 = f(y_{n+1})\), which is also the first stage of the next time-step. This trick is called first same as last (FSAL), and the method needs only three evaluations of \(f\) per step.
The most popular embedded method is the 7-stage Dormand-Prince pair of orders 5 and 4, also using FSAL.
It is the default method ode45 in Matlab, and RK45 in scipy.
20.3. Stiff problems#
Adaptive explicit methods do not fail for stiff problems, but they become very expensive: When \(\tau \lambda\) leaves the domain of stability, the numerical solution starts to grow, the error estimator detects this, and reduces the time-step. Thus the time-step is determined by stability, and not by accuracy, even when the solution is smooth.
For the Oregonator example below, an explicit adaptive 3(2) method needs about one million steps on the interval \([0, 50]\), while an implicit Radau IIA method needs about 300 steps (both computed with scipy, \(\text{rtol} = 10^{-6}\)).
For stiff problems we use a pair of implicit methods, preferably both \(L\)-stable. An example is the implicit Euler method (\(p = 1\)) for the error estimate, together with the two-stage Radau IIA method (order 3).
20.4. Interfaces for the implementation#
Step-size control is your own implementation, the following interfaces are a suggestion.
A time-stepper which provides an error estimate extends the TimeStepper interface:
class EmbeddedTimeStepper : public TimeStepper
{
public:
using TimeStepper::TimeStepper;
// advances y by tau, returns the estimated local error
virtual double doStepWithError (double tau, VectorView<double> y) = 0;
// order p of the method used for the error estimate
virtual int order() const = 0;
};
Two implementations are needed: an embedded Runge-Kutta method with weights \(b\) and \(\hat b\), and a pair of two
independent TimeStepper objects, such as the implicit Euler and the Radau IIA method.
The adaptive loop only uses the interface, and works for both:
void SolveODE_Adaptive (EmbeddedTimeStepper & stepper,
double tend, double tau, double tol,
VectorView<double> y,
std::function<void(double,VectorView<double>)> callback = nullptr);
It must
not step beyond
tend,reject a step if the error is too large, or if Newton’s method throws, and restore
y,limit the change of the time-step,
count accepted and rejected steps, which is the main measure of efficiency.
20.5. Examples#
20.5.1. Arenstorf-Orbit#
cf. Exercise 4.4 in P. Deuflhard, F. Bornemann, Numerische Mathematik II
The equations of motion of a satellite in the earth-moon system, in coordinates \(x =(x_1,x_2)\) of the co-rotating barycentric frame, are
with the abbreviations
and the data
Here, \(\mu\) is the ratio of the moon’s mass to the mass of the total system. The unit of length for the Euclidean norm \(|x|\) is the mean earth-moon distance (about \(384000 \, km\)), the unit of time is one month. The initial values
are chosen such that the cloverleaf-shaped Arenstorf orbit is obtained. The period of this orbit is
The solution is non-stiff, but it has phases of very fast motion close to the earth and the moon.
20.5.2. Chemical reaction equations#
Molecules of type A and type B react to form molecules of type C and type D:
The reaction rate is \(k c_A c_B\), where \(c_A, c_B, ...\) are concentrations of molecules. This leads to differential equations:
An interesting example is the Belousov-Zhabotinsky reaction. Its simplified model, the so called Oregonator, is
The concentrations \((c_1, c_2, c_3, c_4, c_5)\) give the densities of (\(\mathrm{BrO_3^-}\), \(\mathrm{Br^-}\), \(\mathrm{HBrO_2}\), \(\mathrm{HOBr}\), \(\mathrm{Ce^{4+}}\)). The cerium ions act as catalyst, the last reaction summarizes the oxidation of an organic substance (malonic acid), which releases bromide. Hydrogen ions and the organic substance are available as much as needed.
This results in the ODE system
With reaction constants
Initial conditions are
The concentrations oscillate with a period of roughly 60 time units. The eigenvalues of the Jacobi-matrix reach \(-4 \cdot 10^5\), the system is stiff.
20.6. Exercises#
Implement
SolveODE_Adaptive, and anEmbeddedTimeSteppercombining twoTimeStepperobjects, including the handling of non-converging Newton iterations.Extend your
ExplicitRungeKuttaclass to anEmbeddedTimeStepperwith weights \(b\) and \(\hat b\). Implement the Bogacki-Shampine pair, and verify the orders of \(b\) and \(\hat b\) with constant time-steps.Test the step-size control: the error at the end time should decrease proportional to
tol.Compute and plot the Arenstorf orbit \(x(t)\) with automatic step-size control. The desired accuracy of \(x(T)\) is 1 km. Plot the time-step \(\tau\) over time. How many steps does the classical \(RK_4\) method with constant time-step need for the same accuracy?
Solve the Oregonator equations on the interval \([0, 300]\) with automatic step-size control, using the scaled error norm. Plot the concentrations in logarithmic scale. Compare the number of time-steps of the explicit Bogacki-Shampine pair with the implicit pair of implicit Euler and Radau IIA.