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...
    ) -> DiscreteCallback

Construct a callback that schedules user_affect! at the supplied integration-time stops.

Arguments

  • tstops: one time, a collection of callback times, or a callable tstops(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 example integrator -> integrator.p when the parameters are the event times. The same callback can then be reused after remake(prob; p = ...) without rebuilding it. Vector inputs and vector results from the callable are sorted before use.
  • user_affect!: a function user_affect!(integrator) applied at each scheduled stop.

Keywords

  • initialize = INITIALIZE_DEFAULT: callback initialization function called as initialize(callback, u, t, integrator) before the stops are scheduled.
  • filter_tstops::Bool = true: schedule only stops strictly inside the integration interval. Set this to false to schedule all supplied stops, including values outside that interval.
  • sort_inplace::Bool = false: sort a vector tstops, or a vector returned by a callable tstops, in place. By default, sort a copy and leave the supplied or returned vector unchanged.
  • kwargs...: keyword arguments forwarded to DiscreteCallback.

Returns

  • DiscreteCallback: a callback that schedules the requested stops and calls user_affect!.

Throws

  • ArgumentError: if tstops is neither a number, a vector, nor a callable, or if a callable tstops returns 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)
source
DiffEqCallbacks.PeriodicCallback — Function
PeriodicCallback(f, Δt::Number; phase = 0, initial_affect = false,
    final_affect = false, initialize = ..., kwargs...) -> DiscreteCallback

Construct 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 function f(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 an ArgumentError.
  • initial_affect::Bool = false: apply f during callback initialization at the initial integration time.
  • final_affect::Bool = false: apply f when the integrator finishes, even when the final time is not a periodic stop.
  • initialize = ...: callback initialization function called as initialize(callback, u, t, integrator) before periodic stops are scheduled. By default, it marks a derivative discontinuity according to initial_affect.
  • kwargs...: keyword arguments forwarded to DiscreteCallback.

Returns

  • DiscreteCallback: a callback that schedules f at periodic integration-time stops.

Throws

  • ArgumentError: if phase < 0.
  • AssertionError: during callback initialization if the sign of Δt does 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)
source
DiffEqCallbacks.IterativeCallback — Function
IterativeCallback(time_choice, user_affect!, tType = Float64;
    initial_affect = false, initialize = ..., kwargs...) -> DiscreteCallback

Construct a callback that applies user_affect! at the sequence of integration times returned by time_choice.

Arguments

  • time_choice: a function time_choice(integrator) that returns the next callback time or nothing to stop scheduling further affects.
  • user_affect!: a function user_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 not Float64.

Keywords

  • initial_affect::Bool = false: apply user_affect! during callback initialization at the initial integration time before asking time_choice for the next time.
  • initialize = ...: callback initialization function called as initialize(callback, u, t, integrator) before the initial affect or first scheduled stop. By default, it marks a derivative discontinuity according to initial_affect.
  • kwargs...: keyword arguments forwarded to DiscreteCallback.

Returns

  • DiscreteCallback: a callback that schedules each time returned by time_choice until it returns nothing.

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)
source