Timed Callbacks
The following callbacks are designed to help build callbacks with specific timing schemes for when the affect! is to be detonated.
PresetTimeCallback accepts its tstops as a number, a vector of times, or a callable tstops(integrator) returning a number or a vector of times. The callable is evaluated when the callback is initialized at the start of each solve, so event times that depend on the model parameters can be read from the integrator, for example PresetTimeCallback(integrator -> integrator.p, affect!) when the parameters are the event times. The same callback then works unchanged for a problem updated with remake(prob; p = ...), without rebuilding the callback for each parameter vector.
DiffEqCallbacks.PresetTimeCallback — Function
PresetTimeCallback(
tstops, user_affect!; initialize = INITIALIZE_DEFAULT,
filter_tstops = true, sort_inplace = false, kwargs...
) -> DiscreteCallbackConstruct a callback that schedules user_affect! at the supplied integration-time stops.
Arguments
tstops: one time, a collection of callback times, or a callabletstops(integrator)returning a number or a collection of callback times. The callable is evaluated when the callback is initialized, so the stops can depend on the problem as it is being solved, for exampleintegrator -> integrator.pwhen the parameters are the event times. The same callback can then be reused afterremake(prob; p = ...)without rebuilding it. Vector inputs and vector results from the callable are sorted before use.user_affect!: a functionuser_affect!(integrator)applied at each scheduled stop.
Keywords
initialize = INITIALIZE_DEFAULT: callback initialization function called asinitialize(callback, u, t, integrator)before the stops are scheduled.filter_tstops::Bool = true: schedule only stops strictly inside the integration interval. Set this tofalseto schedule all supplied stops, including values outside that interval.sort_inplace::Bool = false: sort a vectortstops, or a vector returned by a callabletstops, in place. By default, sort a copy and leave the supplied or returned vector unchanged.kwargs...: keyword arguments forwarded toDiscreteCallback.
Returns
DiscreteCallback: a callback that schedules the requested stops and callsuser_affect!.
Throws
ArgumentError: iftstopsis neither a number, a vector, nor a callable, or if a callabletstopsreturns neither a number nor a vector.
Examples
using DiffEqCallbacks, OrdinaryDiffEq
hits = Float64[]
affect! = integrator -> push!(hits, integrator.t)
cb = PresetTimeCallback([0.25, 0.5, 0.75], affect!)
prob = ODEProblem((u, p, t) -> -u, 1.0, (0.0, 1.0))
sol = solve(prob, Tsit5(); callback = cb)With parameter-dependent stops, pass a callable instead; here the parameters themselves are the event times:
cb = PresetTimeCallback(integrator -> integrator.p, affect!)
prob = ODEProblem((u, p, t) -> -u, 1.0, (0.0, 1.0), [0.25, 0.5])
sol = solve(prob, Tsit5(); callback = cb)
sol = solve(remake(prob; p = [0.75]), Tsit5(); callback = cb)DiffEqCallbacks.PeriodicCallback — Function
PeriodicCallback(f, Δt::Number; phase = 0, initial_affect = false,
final_affect = false, initialize = ..., kwargs...) -> DiscreteCallbackConstruct a callback that applies f at regular intervals of integration time. Scheduled stops are separated by Δt and are offset from the initial time by phase. When initial_affect = true, f is also applied during callback initialization.
Arguments
f: a functionf(integrator)applied at each periodic stop.Δt::Number: signed integration-time period. Its sign must match the integration direction.
Keywords
phase = 0: nonnegative offset of scheduled periodic stops from the initial integration time. A negative phase throws anArgumentError.initial_affect::Bool = false: applyfduring callback initialization at the initial integration time.final_affect::Bool = false: applyfwhen the integrator finishes, even when the final time is not a periodic stop.initialize = ...: callback initialization function called asinitialize(callback, u, t, integrator)before periodic stops are scheduled. By default, it marks a derivative discontinuity according toinitial_affect.kwargs...: keyword arguments forwarded toDiscreteCallback.
Returns
DiscreteCallback: a callback that schedulesfat periodic integration-time stops.
Throws
ArgumentError: ifphase < 0.AssertionError: during callback initialization if the sign ofΔtdoes not match the integration direction.
Examples
using DiffEqCallbacks, OrdinaryDiffEq
samples = Float64[]
affect! = integrator -> push!(samples, integrator.u)
cb = PeriodicCallback(affect!, 0.1; initial_affect = true)
prob = ODEProblem((u, p, t) -> -u, 1.0, (0.0, 1.0))
sol = solve(prob, Tsit5(); callback = cb)DiffEqCallbacks.IterativeCallback — Function
IterativeCallback(time_choice, user_affect!, tType = Float64;
initial_affect = false, initialize = ..., kwargs...) -> DiscreteCallbackConstruct a callback that applies user_affect! at the sequence of integration times returned by time_choice.
Arguments
time_choice: a functiontime_choice(integrator)that returns the next callback time ornothingto stop scheduling further affects.user_affect!: a functionuser_affect!(integrator)applied at each scheduled time.tType::Type = Float64: type used to store the next callback time. Set this to the problem time type when it is notFloat64.
Keywords
initial_affect::Bool = false: applyuser_affect!during callback initialization at the initial integration time before askingtime_choicefor the next time.initialize = ...: callback initialization function called asinitialize(callback, u, t, integrator)before the initial affect or first scheduled stop. By default, it marks a derivative discontinuity according toinitial_affect.kwargs...: keyword arguments forwarded toDiscreteCallback.
Returns
DiscreteCallback: a callback that schedules each time returned bytime_choiceuntil it returnsnothing.
Examples
using DiffEqCallbacks, OrdinaryDiffEq
count = Ref(0)
hits = Float64[]
time_choice = integrator -> (count[] += 1; count[] <= 3 ? integrator.t + 0.1 : nothing)
affect! = integrator -> push!(hits, integrator.t)
cb = IterativeCallback(time_choice, affect!)
prob = ODEProblem((u, p, t) -> -u, 1.0, (0.0, 1.0))
sol = solve(prob, Tsit5(); callback = cb)