Skip to contents

The callbacks of DiffEqCallbacks.jl, for solve() and init() of ODE, DAE and SDE problems (pass them as callback, alone or in a CallbackSet()).

Usage

SavedValues()

SavingCallback(
  save_func,
  saved_values,
  saveat = numeric(),
  save_everystep = !length(saveat),
  save_start = save_everystep || !length(saveat) || length(saveat) == 1L,
  save_end = save_everystep || !length(saveat) || length(saveat) == 1L,
  tdir = 1
)

FunctionCallingCallback(
  func,
  funcat = numeric(),
  func_everystep = !length(funcat),
  func_start = TRUE,
  tdir = 1
)

IndependentlyLinearizedSolution(prob, num_derivatives = 0)

LinearizingSavingCallback(
  ils,
  interpolate_mask = NULL,
  abstol = NULL,
  reltol = NULL
)

PositiveDomain(u = NULL, save = TRUE, abstol = NULL, scalefactor = NULL)

GeneralDomain(
  g,
  u = NULL,
  save = TRUE,
  abstol = NULL,
  scalefactor = NULL,
  autonomous = NULL,
  domain_jacobian = NULL,
  ...
)

ManifoldProjection(
  g,
  nlsolve = NULL,
  save = TRUE,
  autonomous = NULL,
  manifold_jacobian = NULL,
  ...
)

AutoAbstol(save = TRUE, init_curmax = 0)

StepsizeLimiter(
  dtFE,
  safety_factor = 9/10,
  max_step = FALSE,
  cached_dtcache = 0
)

TerminateSteadyState(
  abstol = 1e-08,
  reltol = 1e-06,
  test = .all_deriv_pass,
  min_t = NULL,
  wrap_test = TRUE
)

ProbIntsUncertainty(sigma, order, save = TRUE)

AdaptiveProbIntsUncertainty(order, save = TRUE)

IterativeCallback(
  time_choice,
  affect,
  initial_affect = FALSE,
  save_positions = c(TRUE, TRUE)
)

IntegrandValues()

IntegrandValuesSum()

IntegratingCallback(
  integrand_func,
  integrand_values,
  integrand_prototype = NULL
)

IntegratingSumCallback(
  integrand_func,
  integrand_values,
  integrand_prototype = NULL
)

IntegratingGKCallback(
  integrand_func,
  integrand_values,
  integrand_prototype = NULL,
  abstol = 1e-06,
  reltol = 0.001
)

IntegratingGKSumCallback(
  integrand_func,
  integrand_values,
  integrand_prototype = NULL,
  abstol = 1e-06,
  reltol = 0.001
)

Arguments

save_func, func

Functions (u, t, integ).

saved_values

A SavedValues() object.

saveat, funcat

Times (or, as one number, an interval).

save_everystep, func_everystep

Call after every step.

save_start, save_end, func_start

Call at the start or the end.

tdir

Direction of time.

prob

An ODE problem.

num_derivatives

Number of derivatives to store with each point (0 or 1).

ils

An IndependentlyLinearizedSolution().

interpolate_mask

Logical vector: the unknowns whose interpolant is refined (others are stored at the step ends).

abstol, reltol

Tolerances (default: those of the integrator).

u

Unused cache argument of the Julia signature.

save

Save after the callback acts.

scalefactor

Factor by which a step is shortened (default 1/2).

g

A residual function (u, p, t) or (u, p).

autonomous

Whether g takes no time argument (default: from the number of arguments).

nlsolve, ...

Unused options of the Julia signature.

manifold_jacobian, domain_jacobian

Optional Jacobian of g.

init_curmax

Initial largest absolute values for AutoAbstol().

dtFE

Function (u, p, t) giving the largest stable step.

safety_factor

Fraction of dtFE allowed.

max_step

Always propose the largest allowed step.

cached_dtcache

Unused option of the Julia signature.

test

Function (integ, abstol, reltol, min_t).

min_t

Earliest time to stop.

wrap_test

Pass test as the condition (u, t, integ) itself when FALSE.

sigma, order

Noise scale and order of the method.

time_choice

Function (integ) returning the next time or NULL.

affect

Function (integ).

initial_affect

Apply affect at the start.

save_positions

Save the state before and after the affect.

integrand_func

Function (u, t, integ).

integrand_values

An IntegrandValues() (or IntegrandValuesSum()) object.

integrand_prototype

Unused (the integrand's shape is taken from its first value).

Value

A callback, or a storage object.

Details

  • SavingCallback(save_func, saved_values) calls save_func(u, t, integ) after every step (or at saveat) and stores the results in saved_values, a SavedValues() object with elements t and saveval. FunctionCallingCallback(func) calls func(u, t, integ) the same way without storing anything.

  • LinearizingSavingCallback(ils) stores, in an IndependentlyLinearizedSolution(prob), for each unknown separately the fewest time points such that linear interpolation is within abstol and reltol of the solver's interpolant. ils(t) interpolates, ils$ts and ils$us hold the points of each unknown.

  • PositiveDomain() keeps the solution non negative: the next step is shortened until the extrapolated state is above -abstol, and negative values are set to zero. GeneralDomain(g) keeps g(u, p, t) (or g(u, p)) below abstol the same way and projects onto g = 0.

  • ManifoldProjection(g) projects the state onto the manifold g(u, p, t) = 0 (or g(u, p)) after every step, the closest point in the Euclidean norm (Newton on the Lagrange conditions).

  • AutoAbstol() sets the absolute tolerance to the relative tolerance times the largest absolute value of each unknown so far.

  • StepsizeLimiter(dtFE) limits each step to safety_factor * dtFE(u, p, t).

  • TerminateSteadyState(abstol, reltol) stops when every derivative is below max(abstol, reltol |u|) (after min_t), or when test(integ, abstol, reltol, min_t) is TRUE.

  • ProbIntsUncertainty(sigma, order) adds Gaussian noise of standard deviation sigma dt^order after each step (the ProbInts method of Conrad et al.); AdaptiveProbIntsUncertainty(order) uses the error estimate of the step for sigma.

  • IterativeCallback(time_choice, affect) applies affect(integ) at the times time_choice(integ) returns one after the other (NULL ends it).

  • IntegratingCallback(integrand_func, integrand_values) integrates integrand_func(u, t, integ) over each step by Gauss-Legendre quadrature on the interpolant and stores one integral per step in IntegrandValues(); IntegratingSumCallback() accumulates the total in IntegrandValuesSum(). The GK versions use adaptive Gauss-Kronrod quadrature with abstol and reltol.

Examples

prob <- ODEProblem(function(u, p, t) -u, c(1, 2), c(0, 1))
sv <- SavedValues()
sol <- solve(prob, Tsit5(), callback = SavingCallback(function(u, t, integ) sum(u), sv,
                                                      saveat = 0.25))
unlist(sv$saveval)
#> [1] 3.000000 2.336402 1.819591 1.417098 1.103639
iv <- IntegrandValuesSum()
sol <- solve(prob, Tsit5(), callback = IntegratingSumCallback(function(u, t, integ) u, iv))
iv$integrand # 1 - exp(-1) times c(1, 2)
#> [1] 0.6321204 1.2642408