Timed Callbacks
The following callbacks are designed to help build callbacks with specific timing schemes for when the affect! is to be detonated.
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::Union{Number, AbstractVector}: one time or a collection of callback times. Vector inputs 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 vectortstopsin place. By default, sort a copy and leave the supplied 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 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)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)