Timed Callbacks

The following callbacks are designed to help build callbacks with specific timing schemes for when the affect! is to be detonated.

DiffEqCallbacks.PresetTimeCallbackFunction
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::Union{Number, AbstractVector}: one time or a collection of callback times. Vector inputs 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 in place. By default, sort a copy and leave the supplied 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 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)
source
DiffEqCallbacks.PeriodicCallbackFunction
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.IterativeCallbackFunction
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