Skip to contents

GlobalAdjoint(alg) wraps an ODE algorithm with adjoint based global error estimation and control (Cao and Petzold 2004), as GlobalDiffEq.jl. The problem is solved with local tolerances, the 2-norm of the endpoint error is estimated from samples orthogonal random projections, and the local tolerances (which default to gtol) are tightened until the estimate is at most gtol. With more than one state the estimate is probabilistic; two samples are accurate within a factor of ten with probability above 99%. With as many samples as states it is the norm of the full error estimate.

Usage

GlobalAdjoint(
  alg,
  adjoint_alg = alg,
  sensealg = NULL,
  samples = 2,
  rng = stats::rnorm,
  scope = EndpointError,
  control = ToleranceRefinement,
  gtol = NULL,
  maxiters = 6,
  safety = 0.8,
  adjoint_abstol = if (is.null(gtol)) 1e-10 else min(gtol/100, 1e-10),
  adjoint_reltol = if (is.null(gtol)) 1e-08 else min(gtol/100, 1e-08)
)

adjoint_error_estimate(
  prob,
  alg,
  ...,
  abstol = 1e-06,
  reltol = 0.001,
  adjoint_abstol = alg$options$adjoint_abstol,
  adjoint_reltol = alg$options$adjoint_reltol
)

Arguments

alg

The ODE algorithm.

adjoint_alg

The algorithm of the adjoint solves.

sensealg

An adjoint method for adjoint_sensitivities(), or NULL for its default.

samples

The number of random projections (at most the number of states are used).

rng

A function of n returning n standard normal draws, which give the random directions (Julia takes a random number generator).

scope

EndpointError or TrajectoryError.

control

ToleranceRefinement or StepGridRefinement.

gtol

The requested absolute endpoint error, needed by solve().

maxiters

The maximum number of tolerance refinements.

safety

The safety factor of each tolerance reduction.

adjoint_abstol, adjoint_reltol

Tolerances of the adjoint solves.

prob

An ODEProblem.

...

Options of the forward solve.

abstol, reltol

Tolerances of the forward solve.

Value

An algorithm object.

Details

For a projection direction v the error component <v, y(T) - P(T)> is the dual weighted residual, the integral of lambda(t)' (f(P) - P') with the adjoint lambda(T) = v, P the dense numerical solution and f(P) - P' its defect. It is computed by adjoint_sensitivities() as the sensitivity to a scalar forcing that adds the defect to the dynamics; sensealg (by default InterpolatingAdjoint()) and adjoint_alg select the adjoint method and its solver.

solve(prob, GlobalAdjoint(alg, gtol = ...)) returns a solution with sol$global_error (and has_global_error() is TRUE): with EndpointError the estimate in its last element (the others are zero), with TrajectoryError the estimate at every saved time. With ToleranceRefinement the solution is an adaptive solve at the accepted tolerances; with StepGridRefinement a fixed step solve on the accepted step grid. adjoint_error_estimate(prob, alg) solves prob and returns the estimate. The solver must have a dense output with a first derivative (see GlobalErrorEstimation()); forward time ODEs with the standard mass matrix and real vector states are supported.

References

Y. Cao and L. Petzold, A posteriori error estimation and global error control for ordinary differential equations by the adjoint method, SIAM Journal on Scientific Computing 26, 2004.

Examples

prob <- ODEProblem(function(u, p, t) p * u, 1, c(0, 1), 1)
alg <- GlobalAdjoint(Tsit5(), samples = 1)
adjoint_error_estimate(prob, alg, abstol = 1e-4, reltol = 1e-4)
#> [1] 1.420859e-08
sol <- solve(prob, Tsit5(), abstol = 1e-4, reltol = 1e-4)
abs(sol$u[[length(sol$t)]] - exp(1))
#> [1] 1.715204e-08