Output and Saving Controls

These callbacks extend the output and saving controls available during time stepping.

DiffEqCallbacks.SavingCallbackFunction
SavingCallback(save_func, saved_values::SavedValues;
               saveat=Vector{eltype(saved_values.t)}(),
               save_everystep=isempty(saveat),
               save_start = true,
               tdir=1)

The saving callback lets you define a function save_func(u, t, integrator) which returns quantities of interest that shall be saved.

Arguments

  • save_func(u, t, integrator) returns the quantities which shall be saved. Note that this should allocate the output (not as a view to u).
  • saved_values::SavedValues is the types that save_func will return, i.e. save_func(t, u, integrator)::savevalType. It's specified via SavedValues(typeof(t),savevalType), i.e. give the type for time and the type that save_func will output (or higher compatible type).

Keyword Arguments

  • saveat mimics saveat in solve from solve.
  • save_everystep mimics save_everystep from solve.
  • save_start mimics save_start from solve.
  • tdir should be sign(tspan[end]-tspan[1]). It defaults to 1 and should be adapted if tspan[1] > tspan[end].

The outputted values are saved into saved_values. Time points are found via saved_values.t and the values are saved_values.saveval.

DiffEqCallbacks.FunctionCallingCallbackFunction
FunctionCallingCallback(func;
               funcat=Vector{Float64}(),
               func_everystep=isempty(funcat),
               func_start = true
               tdir=1)

The function calling callback lets you define a function func(u,t,integrator) which gets calls at the time points of interest. The constructor is:

  • func(t, u, integrator) is the function to be called.
  • funcat values that the function is sure to be evaluated at.
  • func_everystep whether to call the function after each integrator step.
  • func_start whether the function is called the initial condition.
  • tdir should be sign(tspan[end]-tspan[1]). It defaults to 1 and should be adapted if tspan[1] > tspan[end].

Saving Example

In this example we will solve a matrix equation and at each step save a tuple of values which contains the current trace and the norm of the matrix. We build the SavedValues cache to use Float64 for time and Tuple{Float64,Float64} for the saved values, and then call the solver with the callback.

using DiffEqCallbacks, OrdinaryDiffEq, LinearAlgebra
prob = ODEProblem((du,u,p,t) -> du .= u, rand(4,4), (0.0,1.0))
saved_values = SavedValues(Float64, Tuple{Float64,Float64})
cb = SavingCallback((u,t,integrator)->(tr(u),norm(u)), saved_values)
sol = solve(prob, Tsit5(), callback=cb)

print(saved_values.saveval)
[(1.5707418440899112, 2.1913556749565113), (1.7369526808031068, 2.423237865936), (2.2288950191293253, 3.1095509217056074), (3.1213516866732727, 4.35462501865776), (4.269718825384581, 5.95672205059024)]

Note that the values are retrieved from the cache as .saveval, and the time points are found as .t. If we want to control the saved times, we use saveat in the callback. The save controls like saveat act analogously to how they act in the solve function.

saved_values = SavedValues(Float64, Tuple{Float64,Float64})
cb = SavingCallback((u,t,integrator)->(tr(u),norm(u)), saved_values, saveat=0.0:0.1:1.0)
sol = solve(prob, Tsit5(), callback=cb)
print(saved_values.saveval)
print(saved_values.t)
[(1.5707418440899112, 2.1913556749565113), (1.735938205928253, 2.421822563171592), (1.9185084931515086, 2.6765279665390858), (2.1202795785024193, 2.9580205711892376), (2.343271894565863, 3.2691191002797715), (2.5897153874617302, 3.61293457113225), (2.8620770880266266, 3.992908760028645), (3.163085608940009, 4.412848378366111), (3.4957505521043317, 4.876952148062653), (3.8634003429544883, 5.38986358453289), (4.269718825384581, 5.95672205059024)][0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]