# Switching methods

Many systems are stiff in some regions and non-stiff in others. For these,
`modax` offers `Auto(Tsit5(), Rodas5P())`, which steps a system explicitly
with Tsit5 while it is not stiff and implicitly with Rodas5P while it is. The
design draws on the `AutoSwitch` algorithm of OrdinaryDiffEq.jl
([Rackauckas & Nie 2017](https://doi.org/10.5334/jors.151);
[Rackauckas 2018](https://sciml.ai/news/2018/04/09/AutoSwitch);
[SciML contributors 2018](https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/lib/OrdinaryDiffEqCore/src/composite_algs.jl)),
a descendant of LSODA ([Petzold 1983](https://doi.org/10.1137/0904010)).

After every attempted step, the system estimates the magnitude $\rho$ of its
Jacobian's largest eigenvalue. It obtains this either from Tsit5's last two
stages, $\mathbf{k}_6 = f(t_{n+1}, \mathbf{g}_6)$ and
$\mathbf{k}_7 = f(t_{n+1}, \mathbf{y}_{n+1})$ using the formula of
[Hairer & Wanner (1991, Section IV.2)](https://doi.org/10.1007/978-3-662-09947-6),
or from Rodas5P's Jacobian itself using Gershgorin's circle theorem
([Gerschgorin 1931](https://www.mathnet.ru/eng/im5235)). A step is stiff when
$\rho$ is large enough that the next step size $h$ would exceed the stability
region of Tsit5. After more than ten such stiff steps in a row the solver
switches to Rodas5P and doubles the step size. More than three non-stiff
steps in a row it switches back to Tsit5 and halves the step. On the GPU, all
32 trajectories of a warp step with Rodas5P if any one of them requires it,
which keeps the warp in lockstep (see
[SIMT parallelism and thread divergence](../design.md#simt-parallelism-and-thread-divergence)).

Both estimates of $\rho$ reuse quantities the step has already computed, so
switching adds almost no runtime cost. However, the switching is sensitive to
the accuracy of the estimates of $\rho$. Large couplings can lead to
overestimates of the stiffness of the system, and prevent the solver from
ever switching to an explicit method. In addition, a GPU-compiled Rodas5P
step with sparsity optimisations enabled is not significantly slower than a
Tsit5 step. For these reasons, automatic switching only pays off for very
large, densely coupled systems for which the Rodas5P step is significantly
slower, and that are only very transiently stiff.
