Skip to contents

This article covers the numerical half of the workflow: how a model becomes a problem, how solve() turns a problem into a solution, what the solution object holds, how to drive an integrator step by step, how to run ensembles of simulations, and which of the ported OrdinaryDiffEq.jl methods to pick for a given problem. The names follow SciMLBase.jl and OrdinaryDiffEq.jl, so Julia code translates almost line by line.

Problems from systems

A problem bundles everything a solver needs: the right hand side function, the initial state u0, the parameter vector p and the time span. With ModelingToolkit the function is generated from a compiled System, and the values are given as a list of equations mapping variables to numbers, the op argument (the operating point).

t <- t_nounits
D <- D_nounits
variables("x(t)", "y(t)")
parameters("a = 1.5", "b = 1", "g = 3", "d = 1")
lv <- mtkcompile(System(list(D(x) ~ a * x - b * x * y,
                             D(y) ~ -g * y + d * x * y), t, name = "lv"))
prob <- ODEProblem(lv, list(x ~ 1, y ~ 1), c(0, 10))
prob
#> ODEProblem of system lv
#> Time span: [0, 10]
#> u0:
#> y(t) x(t) 
#>    1    1 
#> p:
#>   a   b   d   g 
#> 1.5 1.0 1.0 3.0

Parameters with a default (a = 1.5 above) need not appear in op. The numeric fields are plain R vectors. Note that the order of the unknowns is chosen by structural simplification and need not be the order of declaration, so code should index by symbol rather than by position.

prob$u0
#> y(t) x(t) 
#>    1    1
prob$p
#>   a   b   d   g 
#> 1.5 1.0 1.0 3.0
prob$tspan
#> [1]  0 10

Symbolic indexing of problems

Indexing a problem with a variable returns its initial value, and prob$ps[...] reads a parameter, as prob[x] and prob.ps[a] in ModelingToolkit. Any expression of the model can be evaluated at the initial point too.

prob[x]
#> [1] 1
prob$ps[a]
#> [1] 1.5
prob[x * y + a]
#> [1] 2.5

Assigning through prob$ps changes a parameter and returns the updated problem. modelcompR does not export the SymbolicIndexingInterface getters and setters (getu, setu, getp, setp); the indexing operators above and remake() cover the same ground.

prob$ps[a] <- 2
prob$ps[a]
#> [1] 2

remake

remake() returns a modified copy and leaves the original alone. New values may be partial maps (only the given entries change) or full numeric vectors; the time span and solver keywords stored in the problem can be replaced as well.

prob2 <- remake(prob, u0 = list(x ~ 2), p = list(g ~ 2.5), tspan = c(0, 5))
c(prob2[x], prob2[y], prob2$ps[g])
#> [1] 2.0 1.0 2.5
prob2$tspan
#> [1] 0 5
c(prob[x], prob$ps[g])
#> [1] 1 3

Problems from plain R functions

Without a symbolic model, a problem is built from a function f(u, p, t) that returns the derivative, exactly as ODEProblem(f, u0, tspan, p) in SciMLBase. An unnamed fourth argument is the parameter vector.

lotka <- function(u, p, t) c(p[1] * u[1] - p[2] * u[1] * u[2],
                             -p[3] * u[2] + p[4] * u[1] * u[2])
fprob <- ODEProblem(lotka, c(1, 1), c(0, 10), c(1.5, 1, 3, 1))
fsol <- solve(fprob, Tsit5())
fsol$u[[length(fsol$t)]]
#> [1] 1.0337581 0.9063704

Symbolic indexing needs a system, so for such problems the states are indexed by position: fsol[1, ] is the first state at every saved time.

head(fsol[1, ])
#> [1] 1.000000 1.045494 1.175872 1.419681 1.876719 2.588250

A mass matrix M u' = f(u, p, t) is given with mass_matrix; a zero row makes the corresponding equation algebraic.

mprob <- ODEProblem(function(u, p, t) c(-u[1] + u[2], u[1] + u[2] - 1),
                    c(1, 0), c(0, 1), mass_matrix = diag(c(1, 0)))
msol <- solve(mprob, Rodas5P())
msol$u[[length(msol$t)]]
#> [1] 0.5676676 0.4323324

A fully implicit DAE F(du, u, p, t) = 0 is a DAEProblem, which takes the residual function, initial derivatives du0, initial states u0 and the time span, and optionally which components are differential.

resid <- function(du, u, p, t) c(du[1] - u[2], u[2]^3 + u[2] + u[1]^3 + u[1])
dprob <- DAEProblem(resid, c(-1, -1), c(1, -1), c(0, 2),
                    differential_vars = c(TRUE, FALSE))
dsol <- solve(dprob, DFBDF())
dsol$u[[length(dsol$t)]]
#> [1]  0.1353448 -0.1353448

SteadyStateProblem() asks for the state where the right hand side vanishes. It takes an ODE problem, a system or a function. Solving it without an algorithm uses a Newton type root finder, and DynamicSS() integrates to steady state instead.

ss <- SteadyStateProblem(function(u, p, t) c(1 - u[1], u[1] - 2 * u[2]), u0 = c(0, 0))
solve(ss)$u
#> [1] 1.0 0.5
solve(ss, DynamicSS())$u
#> [1] 1.0000000 0.4999999

solve() and its options

solve(prob, alg, ...) integrates the problem with algorithm alg; when the algorithm is missing, DefaultODEAlgorithm() is used (see below). The common keywords are those of the DifferentialEquations.jl common solver options.

  • saveat: a step (save at every multiple) or a vector of times. Values are then interpolated at those times and the solution is no longer dense.
  • save_everystep = FALSE: save only the start and the end.
  • tstops: times the integrator must step to exactly, such as known discontinuities.
  • dense: keep the full interpolant (the default when saving every step).
  • reltol, abstol: the relative and absolute error tolerances of the adaptive step size controller (defaults 1e-3 and 1e-6).
  • dt: the initial step, or the fixed step when adaptive = FALSE or the method is not adaptive.
  • maxiters: the maximum number of steps.
  • callback: event handling (see the events article), plus dtmin, dtmax, isoutofdomain, unstable_check and the controller settings documented in ?solve.ODEProblem.
sol <- solve(prob, Tsit5(), saveat = 1)
sol$t
#>  [1]  0  1  2  3  4  5  6  7  8  9 10
sol <- solve(prob, Tsit5(), saveat = c(0, 2.5, 10))
sol$t
#> [1]  0.0  2.5 10.0
sol$dense
#> [1] FALSE
length(solve(prob, Tsit5(), save_everystep = FALSE)$t)
#> [1] 2
2.5 %in% solve(prob, Tsit5(), tstops = 2.5)$t
#> [1] TRUE
length(solve(prob, RK4(), dt = 0.1, adaptive = FALSE)$t)
#> [1] 101

Tightening the tolerances costs more steps:

sapply(c(1e-3, 1e-6, 1e-9), function(tol)
  length(solve(prob, Tsit5(), reltol = tol, abstol = tol)$t))
#> [1]  35 115 450

When the step budget runs out, the solve stops early with the return code MaxIters and a warning, which we expect here:

short <- suppressWarnings(solve(prob, Tsit5(), maxiters = 5))
short$retcode
#> [1] "MaxIters"

Unknown keywords are an error of class CommonKwargError rather than being silently ignored, which catches typos such as relto. The message lists every accepted keyword and ends with the offending one; kwargshandle = "warn" turns the error into a warning.

msg <- tryCatch(solve(prob, Tsit5(), relto = 1e-8), CommonKwargError = function(e) conditionMessage(e))
tail(strsplit(msg, "\n")[[1]], 1)
#> [1] "Unrecognized keyword arguments: relto"

A callback is passed with callback. Here a PresetTimeCallback doubles the prey at t = 5; the events article treats callbacks in detail.

xi <- match("x(t)", names(prob$u0))
cb <- PresetTimeCallback(5, function(integ) integ$u[xi] <- 2 * integ$u[xi])
csol <- solve(prob, Tsit5(), callback = cb)
csol(c(4.999, 5.001), idxs = x)
#> [1] 1.372174 2.729944

The solution object

A solution stores the saved times sol$t, the saved states sol$u (a list with one state vector per time, as in Julia), the return code, the problem, the algorithm and the solver statistics.

sol <- solve(prob, Tsit5())
sol
#> ODESolution with retcode Success (Tsit5)
#> Saved time points: 38 in [0, 10]
#> Final state:
#>     y(t)     x(t) 
#> 1.289839 7.072484
sol$retcode
#> [1] "Success"
successful_retcode(sol$retcode)
#> [1] TRUE
length(sol$t)
#> [1] 38
sol$u[[2]]
#>      y(t)      x(t) 
#> 0.8627314 1.0840348

sol$stats counts the work done: right hand side evaluations nf, Jacobians njacs, linear solves nsolve, factorizations nw, and accepted and rejected steps.

unlist(sol$stats)
#>      nf     nf2   njacs  nsolve      nw naccept nreject 
#>     243       0       0       0       0      37       3

Indexing

sol[x] is the time series of a variable, and any symbolic expression of the model, including observed variables and parameters, can be indexed. A second index selects saved time points; a list of variables gives a matrix.

head(sol[x])
#> [1] 1.000000 1.084035 1.311291 1.718899 2.420837 3.699656
sol[x, 1:3]
#> [1] 1.000000 1.084035 1.311291
head(sol[a * x - b * x * y])
#> [1] 1.000000 1.232839 1.764009 2.589628 3.872687 5.912496
head(sol[list(x, y)], 3)
#>          x(t)      y(t)
#> [1,] 1.000000 1.0000000
#> [2,] 1.084035 0.8627314
#> [3,] 1.311291 0.6547538

Interpolation

Calling the solution as a function evaluates the dense interpolant, the equivalent of sol(t) in Julia. idxs selects variables or expressions, and deriv gives derivatives of the interpolant.

sol(2.5)
#>      y(t)      x(t) 
#> 2.2013434 0.8354841
sol(c(1, 2, 3), idxs = x)
#> [1] 4.335310 2.026140 1.153637
sol(2.5, idxs = x, deriv = 1)
#> [1] -0.1684129

Conversion and plotting

as.data.frame() and as.matrix() give the saved values as a table, with one column per state (and per observed variable for system problems). plot() draws the dense solution; idxs selects variables, and a pair of variables gives a phase plot.

head(as.data.frame(sol), 3)
#>            t      y(t)     x(t)
#> 1 0.00000000 1.0000000 1.000000
#> 2 0.07535398 0.8627314 1.084035
#> 3 0.22783170 0.6547538 1.311291
dim(as.matrix(sol))
#> [1] 38  2
plot(sol)

plot(sol, idxs = list(x, y))

The integrator interface

init() builds an integrator, the object solve() uses internally, and returns it without integrating; the integrator can then be advanced one step at a time and modified between steps. These are init, step!, solve!, reinit!, set_u! and friends in SciMLBase. In R the functions that modify the integrator are named without the bang: step_integrator(), solve_integrator(), set_u(), set_t(), add_tstop(), add_saveat(), set_proposed_dt(), reinit() and terminate().

decay <- ODEProblem(function(u, p, t) -p[1] * u, 1, c(0, 1), 2)
integ <- init(decay, Tsit5())
integ
#> Integrator at t = 0 (Tsit5)
#> [1] 1
step_integrator(integ)
c(integ$t, integ$u, integ$dt)
#> [1] 0.07580098 0.85933026 0.07580098

step_integrator(integ, dt, stop_at_tdt = TRUE) advances by exactly dt. Between steps the state can be changed with set_u(), a stop time added with add_tstop(), and integ$interp(t) evaluates the interpolant of the last step.

step_integrator(integ, 0.25, stop_at_tdt = TRUE)
integ$t
#> [1] 0.325801
integ$interp(integ$t - 0.01)
#> [1] 0.5317393
set_u(integ, 2 * integ$u)
add_tstop(integ, 0.8)
rest <- solve_integrator(integ)
rest$t
#> [1] 0.00000000 0.07580098 0.21255898 0.32580098 0.50578157 0.68120353 0.80000000
#> [8] 1.00000000

reinit() restarts from new initial values, and a loop over step_integrator() gives full control of the integration. get_du() is the current derivative and terminate() ends the solve.

reinit(integ, u0 = 3)
while (integ$t < 0.5) step_integrator(integ)
c(integ$t, integ$u, get_du(integ))
#> [1]  0.5742273  0.9513804 -1.9027608
terminate(integ)
solve_integrator(integ)$retcode
#> [1] "Terminated"

Ensembles

An EnsembleProblem solves the same problem many times with variations, as in DiffEqBase. prob_func(prob, i, repeat_) builds trajectory i, typically with remake(); output_func(sol, i) transforms each solution and returns a list of the output and a rerun flag; reduction combines batches. The trajectories run serially by default, and EnsembleThreads() or EnsembleDistributed() run them on parallel workers.

ens <- EnsembleProblem(decay, prob_func = function(prob, i, repeat_) remake(prob, p = i / 2))
sim <- solve(ens, Tsit5(), trajectories = 10, saveat = 0.1)
length(sim)
#> [1] 10
sim[[3]](1)
#> [1] 0.2231305

The ensemble analysis functions summarize the trajectories at a saved step (timestep_*) or at an arbitrary time through the interpolants (timepoint_*).

timestep_mean(sim, 11)
#> [1] 0.1531113
timepoint_meanvar(sim, 0.5)
#> $mean
#> [1] 0.3231803
#> 
#> $var
#> [1] 0.0540723
timepoint_quantile(sim, 0.5, 0.9)
#> [1] 0.08630302

EnsembleSummary() collects the mean, the variance and two quantiles over time, and has a plot method that draws the mean with the quantile band dashed. The axis range follows the mean, so ylim is set to show the whole band.

summ <- EnsembleSummary(sim, quantiles = c(0.1, 0.9))
names(summ)
#> [1] "t"         "u"         "v"         "qlow"      "qhigh"     "num_monte"
plot(summ, ylim = range(summ$qlow, summ$qhigh))

An output_func keeps only what is needed; here the final value of each trajectory, with random initial values drawn from a seeded stream.

ens2 <- EnsembleProblem(decay,
  prob_func = function(prob, i, repeat_) remake(prob, u0 = stats::runif(1)),
  output_func = function(sol, i) list(sol$u[[length(sol$t)]], FALSE))
unlist(solve(ens2, Tsit5(), trajectories = 5, seed = 1)$u)
#> [1] 0.03593281 0.05036166 0.07752754 0.12291298 0.02729477

With rng_streams = "trajectory" trajectory i draws from the ith L’Ecuyer-CMRG stream of the seed, so the result is the same in batches, on several cores or serially. A trajectory that throws an error stops the ensemble, unless retries runs it again or on_error = "collect" keeps the error in its place; retcodes and failures report each trajectory, and checkpoint saves each batch so that an interrupted ensemble resumes.

e1 <- solve(ens2, Tsit5(), trajectories = 6, seed = 1, rng_streams = "trajectory")
e2 <- solve(ens2, Tsit5(), trajectories = 6, seed = 1, rng_streams = "trajectory", batch_size = 2)
identical(unlist(e1$u), unlist(e2$u))
#> [1] TRUE
c(e1$failures, e1$backend)
#> [1] "0"      "serial"

The solver catalog

The algorithms are grouped as in OrdinaryDiffEq.jl. Each constructor returns an algorithm object whose options are its arguments. For the examples we use two test problems: a harmonic oscillator, nonstiff, and a semidiscretized heat equation ut=uxxu_t = u_{xx} on 20 interior grid points, whose Laplacian has eigenvalues down to about −4/Δx2-4/\Delta x^2, which makes it stiff. Its exact solution is a decaying sine with the smallest discrete eigenvalue as its rate.

osc <- ODEProblem(function(u, p, t) c(u[2], -u[1]), c(1, 0), c(0, 2 * pi))
err <- function(s) max(abs(s$u[[length(s$t)]] - c(1, 0)))

N <- 20
dx <- 1 / (N + 1)
lap <- diag(-2, N)
lap[cbind(1:(N - 1), 2:N)] <- 1
lap[cbind(2:N, 1:(N - 1))] <- 1
lap <- lap / dx^2
xs <- seq_len(N) * dx
lambda1 <- -4 / dx^2 * sin(pi * dx / 2)^2
heat <- ODEProblem(function(u, p, t) as.vector(lap %*% u), sin(pi * xs), c(0, 0.5))
heat_exact <- exp(lambda1 * 0.5) * sin(pi * xs)
heat_err <- function(s) max(abs(s$u[[length(s$t)]] - heat_exact))

compare <- function(prob, algs, errfun, ...) {
  do.call(rbind, lapply(algs, function(alg) {
    s <- solve(prob, alg, ...)
    data.frame(alg = alg$name, retcode = s$retcode, steps = s$stats$naccept,
               nf = s$stats$nf, error = signif(errfun(s), 3))
  }))
}

Explicit Runge Kutta methods

The explicit methods are the right choice for nonstiff problems. Tsit5() is the recommended default; DP5() is the classic Dormand Prince pair; BS3() is cheap at loose tolerances; Vern7() and Vern9() are efficient at tight tolerances. The low order methods Euler(), Midpoint(), Heun(), Ralston() and RK4() are mostly of didactic interest. Euler() always takes the fixed step dt; the others carry an error estimate and adapt the step unless adaptive = FALSE, which is used here to compare them at the same step.

compare(osc, list(BS3(), Tsit5(), DP5(), Vern7(), Vern9()), err,
        reltol = 1e-8, abstol = 1e-10)
#>     alg retcode steps   nf    error
#> 1   BS3 Success  1570 4713 2.53e-08
#> 2 Tsit5 Success    94  567 5.69e-10
#> 3   DP5 Success    85  525 5.74e-09
#> 4 Vern7 Success    34  342 5.52e-11
#> 5 Vern9 Success    19  306 2.38e-11
compare(osc, list(Euler(), Midpoint(), Heun(), Ralston(), RK4()), err,
        dt = 0.01, adaptive = FALSE)
#>        alg retcode steps   nf    error
#> 1    Euler Success   629  630 3.19e-02
#> 2 Midpoint Success   629 1259 1.05e-04
#> 3     Heun Success   629 1259 1.05e-04
#> 4  Ralston Success   629 1259 1.05e-04
#> 5      RK4 Success   629 2517 5.23e-10

The two stage methods Midpoint(), Heun() and Ralston() give the same error: on a linear problem every explicit two stage method of order two has the same stability polynomial, so they produce identical steps.

Strong stability preserving methods (SSPRK22(), SSPRK33(), SSPRK432() and others) keep the monotonicity properties of forward Euler under a step size restriction, which matters for hyperbolic PDEs. The low storage methods (CarpenterKennedy2N54(), ORK256(), the CKLLSRK*, DGLDDRK* and NDBLSRK* families) need only two or three registers per state.

compare(osc, list(SSPRK22(), SSPRK33(), SSPRK432(), CarpenterKennedy2N54(), ORK256()), err,
        dt = 0.01, adaptive = FALSE)
#>                    alg retcode steps   nf    error
#> 1              SSPRK22 Success   629 1259 1.05e-04
#> 2              SSPRK33 Success   629 1888 2.62e-07
#> 3             SSPRK432 Success   629 2517 1.31e-07
#> 4 CarpenterKennedy2N54 Success   629 3146 2.09e-10
#> 5               ORK256 Success   629 3146 4.44e-08

Stiff solvers

A problem is stiff when stability rather than accuracy limits the step of an explicit method. On the heat equation Tsit5() is held to small steps by stability long after the solution has become smooth, while the implicit methods take steps limited only by accuracy:

compare(heat, list(Tsit5(), Rodas5P(), KenCarp4(), FBDF()), heat_err,
        reltol = 1e-6, abstol = 1e-8)
#>        alg retcode steps   nf    error
#> 1    Tsit5 Success   243 1473 1.80e-09
#> 2  Rodas5P Success    22 1960 6.91e-11
#> 3 KenCarp4 Success    13  666 1.66e-06
#> 4     FBDF Success    84  861 5.43e-09

The main families of implicit methods are:

compare(heat, list(Rosenbrock23(), TRBDF2(), QNDF(), RadauIIA5(),
                   ImplicitEulerExtrapolation()), heat_err, reltol = 1e-6, abstol = 1e-8)
#>                          alg retcode steps    nf    error
#> 1               Rosenbrock23 Success   199 16520 9.58e-07
#> 2                     TRBDF2 Success    38  2239 3.38e-05
#> 3                       QNDF Success    63   870 1.18e-07
#> 4                  RadauIIA5 Success    24  1071 2.25e-09
#> 5 ImplicitEulerExtrapolation Success     7   452 6.50e-09

Exponential integrators

Exponential integrators treat the linear part of the problem exactly with matrix exponentials (computed by Krylov projection for the adaptive Exprb32() and Exprb43(), and in the fixed step Exp4() and EXPRB53s3()). These exponential Rosenbrock methods work with the full Jacobian, so a plain ODEProblem is enough. On a linear problem they are nearly exact.

compare(heat, list(Exprb32(), Exprb43()), heat_err, reltol = 1e-6, abstol = 1e-8)
#>       alg retcode steps  nf    error
#> 1 Exprb32 Success     5 213 1.98e-12
#> 2 Exprb43 Success     5 218 8.56e-12
compare(heat, list(Exp4(), EXPRB53s3()), heat_err, dt = 0.01)
#>         alg retcode steps   nf    error
#> 1      Exp4 Success    50 2151 5.01e-10
#> 2 EXPRB53s3 Success    50 2151 2.26e-16

The fixed step exponential Runge Kutta methods (LawsonEuler(), NorsettEuler(), ETDRK4(), HochOst4()) take a semilinear problem u′=Au+f(u,p,t)u' = A u + f(u, p, t), given as a SplitODEProblem whose first part is a MatrixOperator. Here a constant source term is added to the heat equation, and the errors are measured against a tight Rodas5P() solution.

src <- function(u, p, t) 0 * u + 1
forced <- ODEProblem(function(u, p, t) as.vector(lap %*% u) + 1, sin(pi * xs), c(0, 0.5))
ref <- solve(forced, Rodas5P(), reltol = 1e-10, abstol = 1e-12)
ref <- ref$u[[length(ref$t)]]
ref_err <- function(s) max(abs(s$u[[length(s$t)]] - ref))
semi <- SplitODEProblem(MatrixOperator(lap), src, sin(pi * xs), c(0, 0.5))
compare(semi, list(LawsonEuler(), NorsettEuler(), ETDRK4(), HochOst4()), ref_err, dt = 0.01)
#>            alg retcode steps nf    error
#> 1  LawsonEuler Success    50 51 5.00e-03
#> 2 NorsettEuler Success    50 51 1.13e-14
#> 3       ETDRK4 Success    50 51 1.13e-14
#> 4     HochOst4 Success    50 51 1.14e-14

The exponential time differencing methods (NorsettEuler(), ETDRK4(), HochOst4()) integrate a constant source exactly, so their error is at rounding level; the Lawson method, which exponentiates only the linear part, is first order here.

IMEX and multirate methods

A SplitODEProblem(f1, f2, u0, tspan) represents u′=f1(u)+f2(u)u' = f_1(u) + f_2(u) where f1f_1 is stiff and f2f_2 is not. IMEX methods treat f1f_1 implicitly and f2f_2 explicitly: the additive KenCarp* methods, the multistep CNAB2(), CNLF2() and SBDF2() to SBDF4(), and IMEXEuler(). The multistep methods need a fixed dt.

split <- SplitODEProblem(function(u, p, t) as.vector(lap %*% u), src,
                         sin(pi * xs), c(0, 0.5))
compare(split, list(KenCarp4(), CNAB2(), SBDF2(), IMEXEuler()), ref_err, dt = 0.01)
#>         alg retcode steps   nf    error
#> 1  KenCarp4 Success     7  449 5.99e-04
#> 2     CNAB2 Success    50 2201 2.51e-05
#> 3     SBDF2 Success    50 2151 5.93e-05
#> 4 IMEXEuler Success    50 2151 1.61e-03

KenCarp4() is adaptive (dt is only its first step); the others take the fixed step.

Multirate methods such as MREEF() take several small steps of the fast part f1f_1 for each step of the slow part f2f_2; nf counts the evaluations of f1f_1 and nf2 those of f2f_2.

fast_slow <- SplitODEProblem(function(u, p, t) c(-10 * u[1] + u[2], -u[2]),
                             function(u, p, t) c(0.1 * sin(t), 0.1 * u[1]),
                             c(1, 1), c(0, 1))
mr <- solve(fast_slow, MREEF(), dt = 0.1)
unlist(mr$stats[c("nf", "nf2")])
#>  nf nf2 
#> 411 111

Stabilized explicit methods

Runge Kutta Chebyshev methods, RKC(), ROCK2(), ROCK4(), SERK2() and ESERK4(), are explicit methods with stability regions stretched along the negative real axis. They suit large, moderately stiff problems with real eigenvalues, like diffusion, where no linear algebra is wanted; the number of stages adapts to a running estimate of the spectral radius.

compare(heat, list(RKC(), ROCK4()), heat_err, reltol = 1e-6, abstol = 1e-8)
#>     alg retcode steps  nf    error
#> 1   RKC Success   302 888 1.10e-06
#> 2 ROCK4 Success    75 530 8.11e-09

Automatic stiffness detection and the default algorithm

AutoSwitch(nonstiff, stiff) monitors a stiffness estimate and switches between two methods; AutoTsit5(alg) and AutoVern7(alg) (also AutoVern6, AutoVern8, AutoVern9) pair the named explicit method with a stiff one, as in OrdinaryDiffEq.jl.

When no algorithm is given, solve() uses DefaultODEAlgorithm(), the OrdinaryDiffEqDefault choice: it starts with Tsit5() (Vern7() when reltol < 1e-6), and on stiffness switches to Rosenbrock23() (Rodas5P() when reltol < 1e-6) for up to 50 states and FBDF() above. With a non identity mass matrix it uses Rodas5P() or FBDF() throughout. sol$alg_choice records the method of each step (1 Tsit5, 2 Vern7, 3 Rosenbrock23, 4 Rodas5P, 5 FBDF). For an AutoSwitch() algorithm it is 1 for the nonstiff and 2 for the stiff method.

table(solve(osc)$alg_choice)
#> 
#>  1 
#> 15
table(solve(heat)$alg_choice)
#> 
#>  1  3 
#>  8 24
table(solve(heat, AutoTsit5(Rodas5P()))$alg_choice)
#> 
#>  1  2 
#>  8 11

The oscillator never leaves Tsit5(); on the heat equation the first steps are taken with Tsit5() while the transient is resolved, and the stiff method takes over once stability limits the step.

DAE solvers

Differential algebraic equations arise naturally from acausal models. There are two ways to solve them. When mtkcompile() leaves algebraic equations, ODEProblem() builds a mass matrix problem, solved by the Rosenbrock methods, RadauIIA5(), the BDF methods and the default algorithm.

dae <- mtkcompile(System(list(D(x) ~ y, 0 ~ y^3 + y + x^3 + x), t, name = "dae"))
equations(dae)
#> [[1]]
#> Derivative(x(t), t) ~ y(t) 
#> 
#> [[2]]
#> 0 ~ x(t)^3 + x(t) + y(t)^3 + y(t)
mm_prob <- ODEProblem(dae, list(x ~ 1, y ~ -1), c(0, 2))
mm_prob$f$mass_matrix
#>      [,1] [,2]
#> [1,]    1    0
#> [2,]    0    0
mm_sol <- solve(mm_prob, Rodas5P())
c(tail(mm_sol[x], 1), exp(-2))
#> [1] 0.1353353 0.1353353

DAEProblem() builds the fully implicit form instead, solved by DFBDF() (variable order BDF, the analogue of SUNDIALS IDA) and DImplicitEuler(); initial derivatives are given as D(x) ~ value. The default algorithm for a DAEProblem is DFBDF().

imp_prob <- DAEProblem(dae, list(x ~ 1, y ~ -1, D(x) ~ -1), c(0, 2))
sapply(list(DFBDF(), DImplicitEuler()), function(alg) {
  s <- solve(imp_prob, alg, reltol = 1e-6, abstol = 1e-8)
  c(steps = s$stats$naccept, x_end = tail(s[x], 1))
})
#>             [,1]         [,2]
#> steps 54.0000000 2211.0000000
#> x_end  0.1353353    0.1354625
solve(imp_prob)$alg$name
#> [1] "DFBDF"

Solver options

Automatic differentiation

Implicit methods need the Jacobian of the right hand side. By default it is computed by forward mode automatic differentiation (AutoForwardDiff()), which is exact. AutoFiniteDiff() uses finite differences (forward by default, AutoFiniteDiff("central") for central ones), which costs extra f evaluations. AutoSparse(AutoForwardDiff()) detects the sparsity pattern and colors the columns so that fewer directional derivatives are needed; on this two state problem there is nothing to save, so its counts match forward mode, and it pays off for large banded or sparse Jacobians. System problems can also use the symbolic Jacobian with ODEProblem(sys, ..., jac = TRUE).

vdp <- ODEProblem(function(u, p, t) c(u[2], 10 * (1 - u[1]^2) * u[2] - u[1]), c(2, 0), c(0, 6))
sapply(list(forward = AutoForwardDiff(), finite = AutoFiniteDiff(),
            sparse = AutoSparse(AutoForwardDiff())), function(ad) {
  s <- solve(vdp, Rodas5P(autodiff = ad), reltol = 1e-6, abstol = 1e-8)
  c(nf = s$stats$nf, njacs = s$stats$njacs, u1 = s$u[[length(s$t)]][1])
})
#>          forward     finite     sparse
#> nf    728.000000 992.000000 728.000000
#> njacs 132.000000 132.000000 132.000000
#> u1      1.492518   1.492518   1.492518

Nonlinear solvers

The SDIRK methods solve their stage equations with the solver given by nlsolve: NLNewton() (the default; kappa is the convergence tolerance relative to the error tolerance and max_iter the iteration limit), the fixed point iteration NLFunctional() and its Anderson accelerated form NLAnderson(). The fixed point iterations need no Jacobian, and so only converge on mildly stiff problems; Anderson acceleration combines the previous iterates and here needs fewer function evaluations than the plain iteration. NonlinearSolveAlg(alg) hands each stage system to a nonlinear solver of the package (with the integrator’s W as its Jacobian; postcondition corrects the stage state, for instance to keep it positive), and HomotopyNonlinearSolveAlg() solves it by continuation in the step size, which converges where Newton’s method fails from a poor guess. ImplicitEuler, Trapezoid and the implicit SDE methods take the same choices.

mild <- ODEProblem(function(u, p, t) c(-20 * u[1] + u[2]^2, -u[2] + u[1]), c(1, 1), c(0, 1))
nl <- list(newton = NLNewton(), tight = NLNewton(kappa = 1e-6),
           functional = NLFunctional(), anderson = NLAnderson(),
           inner_trust = NonlinearSolveAlg(TrustRegion()),
           positive = NonlinearSolveAlg(postcondition = function(u, uprev, p, cache) pmax(u, 0)),
           homotopy = HomotopyNonlinearSolveAlg())
sapply(nl, function(n) {
  s <- solve(mild, TRBDF2(nlsolve = n))
  unlist(s$stats[c("nf", "njacs", "nsolve", "naccept")])
})
#>         newton tight functional anderson inner_trust positive homotopy
#> nf          64    85        317      205         158      156     2283
#> njacs       13    13          0        0          13       13        0
#> nsolve      60    81          0       67         113      111      340
#> naccept     10    10         37       34          10       10       34

Predictors

The initial guess of each stage solve is set by the predictor argument, one of the values of Predictor (for instance Predictor$Trivial, Predictor$Linear, the default of most SDIRK methods, or Predictor$MaxOrder, which extrapolates the last step’s interpolant). A function predictor(uprev, p, t, dt) given to NLNewton() replaces the guess at every stage.

sapply(list(trivial = Predictor$Trivial, linear = Predictor$Linear,
            maxorder = Predictor$MaxOrder), function(pr) {
  s <- solve(mild, KenCarp4(predictor = pr))
  s$stats$nf
})
#>  trivial   linear maxorder 
#>      123       89       99

Step limiters

A step limiter is a function limiter(u, integ, p, t) that returns the state to keep after each accepted step, for instance a projection onto an invariant. It is a solve() keyword, and also an argument of the implicit and SSP constructors (stage_limiter for the SSP and Rosenbrock methods acts after every stage). Here the oscillator is projected onto the unit circle.

proj <- function(u, integ, p, t) u / sqrt(sum(u^2))
free <- solve(osc, Tsit5(), reltol = 1e-3, abstol = 1e-6)
kept <- solve(osc, Tsit5(), reltol = 1e-3, abstol = 1e-6, step_limiter = proj)
c(free = max(abs(rowSums(free[]^2) - 1)), limited = max(abs(rowSums(kept[]^2) - 1)))
#>         free      limited 
#> 5.228158e-05 4.440892e-16

The StepsizeLimiter(dtFE) callback is different: it caps the step size by a function dtFE(u, p, t), such as a CFL condition, times a safety factor of 9/10.

capped <- solve(osc, Tsit5(), callback = StepsizeLimiter(function(u, p, t) 0.05))
max(diff(capped$t))
#> [1] 0.045

Complex states

States may be complex, as for the Schrödinger equation. The explicit methods work as they are. Implicit methods should be given AutoFiniteDiff(): the forward mode duals are real, so with the default AutoForwardDiff() the imaginary part of the Jacobian is discarded (with a warning “imaginary parts discarded in coercion”).

H <- function(t) -1i * matrix(c(t, 1, 1, -t), 2)
lz <- ODEProblem(function(u, p, t) as.vector(H(t) %*% u), c(1 + 0i, 0), c(-1, 1))
zsol <- solve(lz, Tsit5(), reltol = 1e-8, abstol = 1e-10)
zsol(1)
#> [1] -0.2890776+9.815729e-11i  0.7634705-5.775352e-01i
sum(Mod(zsol(1))^2)
#> [1] 1
zimp <- solve(lz, Rosenbrock23(autodiff = AutoFiniteDiff()))
sum(Mod(zimp(1))^2)
#> [1] 1.000105

A stiff benchmark: the Robertson problem

The Robertson chemical kinetics problem is the classic stiff test: its rate constants span nine orders of magnitude and the solution evolves over 10510^5 time units. Comparing the statistics of several stiff solvers at the same tolerances shows the trade offs: Rosenbrock methods compute a new Jacobian on every step; BDF methods reuse it over many steps at the cost of more, smaller steps; RadauIIA5() takes few large steps.

rober <- function(u, p, t) {
  k1 <- p[1]; k2 <- p[2]; k3 <- p[3]
  c(-k1 * u[1] + k3 * u[2] * u[3],
    k1 * u[1] - k2 * u[2]^2 - k3 * u[2] * u[3],
    k2 * u[2]^2)
}
rprob <- ODEProblem(rober, c(1, 0, 0), c(0, 1e5), c(0.04, 3e7, 1e4))
rober_algs <- list(Rosenbrock23(), Rodas5P(), KenCarp4(), FBDF(), QNDF(), RadauIIA5())
rober_tab <- do.call(rbind, lapply(rober_algs, function(alg) {
  s <- solve(rprob, alg, reltol = 1e-6, abstol = 1e-8)
  u <- s$u[[length(s$t)]]
  data.frame(alg = alg$name, retcode = s$retcode, naccept = s$stats$naccept,
             nreject = s$stats$nreject, nf = s$stats$nf, njacs = s$stats$njacs,
             y3_end = signif(u[3], 6), mass = signif(sum(u), 10))
}))
rober_tab
#>            alg retcode naccept nreject   nf njacs   y3_end mass
#> 1 Rosenbrock23 Success     265       0 1328   530 0.982135    1
#> 2      Rodas5P Success     128       2 1432   260 0.982134    1
#> 3     KenCarp4 Success     145       0 2405    48 0.982134    1
#> 4         FBDF Success     323       5  843    77 0.982134    1
#> 5         QNDF Success     284      21  801    68 0.982134    1
#> 6    RadauIIA5 Success      89       0  757    92 0.982134    1

All methods agree on the final state and conserve the total mass. The default algorithm detects the stiffness after a few Tsit5() steps and hands over to Rosenbrock23():

rdef <- solve(rprob)
table(rdef$alg_choice)
#> 
#>  1  3 
#> 22 51
rs <- solve(rprob, Rodas5P(), reltol = 1e-6, abstol = 1e-8)
ts <- 10^seq(-5, 5, length.out = 200)
Y <- rs(ts)
matplot(ts, Y * rep(c(1, 1e4, 1), each = length(ts)), type = "l", log = "x", lty = 1,
        xlab = "t", ylab = "concentration (y2 scaled by 1e4)")
legend("right", legend = c("y1", "1e4 y2", "y3"), col = 1:3, lty = 1, bty = "n")

Large problems: sparse Jacobians, reduced storage, native arithmetic

A Jacobian returned as a sparse matrix of the Matrix package keeps the Newton matrices of the implicit and Rosenbrock methods sparse; they are factorized by a sparse LU. Without an analytic Jacobian, AutoSparse() with the sparsity pattern of the ODEFunction differences the columns of each color of a column coloring together.

n <- 300
L <- Matrix::bandSparse(n, k = -1:1, diagonals = list(rep(1, n - 1), rep(-2, n), rep(1, n - 1))) * 100
f <- function(u, p, t) as.numeric(L %*% u) - u^3
u0 <- sin(pi * seq_len(n) / (n + 1))
s1 <- solve(ODEProblem(ODEFunction(f, jac = function(u, p, t) L - Matrix::Diagonal(n, 3 * u^2)), u0, c(0, 0.2)),
            Rodas5P())
s2 <- solve(ODEProblem(ODEFunction(f, sparsity = L), u0, c(0, 0.2)), Rodas5P(autodiff = AutoSparse(AutoFiniteDiff())))
max(abs(tail(s1$u, 1)[[1]] - tail(s2$u, 1)[[1]]))
#> [1] 1.9309e-12

save_idxs selects the saved components; the solution still stores every state, so symbolic indexing of any variable works. With save_idxs_storage = "reduced" only the selected components are stored, as save_idxs does in Julia, and the solution interpolates linearly between them.

big <- ODEProblem(function(u, p, t) -u, as.numeric(1:200), c(0, 1))
full <- solve(big, Tsit5(), saveat = 0.01, save_idxs = 1)
small <- solve(big, Tsit5(), saveat = 0.01, save_idxs = 1, save_idxs_storage = "reduced")
# the full store answers for any state, the reduced one only for the saved
full[2][1:3]
#> [1] 2.000000 1.980100 1.960397
tryCatch(small[2], error = conditionMessage)
#> [1] "state 2 was not saved (save_idxs_storage = \"reduced\")"

By default the solvers replay the floating point operations of the Julia packages, so the results match Julia bit for bit. arithmetic_mode("native") uses R’s arithmetic and LAPACK for the same algorithms and tolerances: the results differ by rounding, and the solves are faster.

osc <- ODEProblem(function(u, p, t) c(u[2], -u[1]), c(1, 0), c(0, 30))
old <- arithmetic_mode("native")
sn <- solve(osc, Tsit5(), reltol = 1e-8, abstol = 1e-10)
arithmetic_mode(old)
sr <- solve(osc, Tsit5(), reltol = 1e-8, abstol = 1e-10)
c(native = tail(sn$u, 1)[[1]][1] - cos(30), reference = tail(sr$u, 1)[[1]][1] - cos(30))
#>       native    reference 
#> 7.792718e-10 7.792969e-10

Julia equivalents

Julia (SciML) modelcompR
ODEProblem(sys, op, tspan) ODEProblem(sys, op, tspan)
prob[x], prob.ps[a] prob[x], prob$ps[a]
prob.ps[a] = 2 prob$ps[a] <- 2
remake(prob; u0, p, tspan) remake(prob, u0 = , p = , tspan = )
sol[x], sol(t; idxs = x) sol[x], sol(t, idxs = x)
sol.t, sol.u, sol.stats, sol.retcode sol$t, sol$u, sol$stats, sol$retcode
init, step!, solve! init(), step_integrator(), solve_integrator()
reinit!, set_u!, add_tstop!, terminate! reinit(), set_u(), add_tstop(), terminate()
EnsembleProblem, EnsembleSummary EnsembleProblem(), EnsembleSummary()
Rodas5P(autodiff = AutoFiniteDiff()) Rodas5P(autodiff = AutoFiniteDiff())
KenCarp4(nlsolve = NLAnderson()) KenCarp4(nlsolve = NLAnderson())