Skip to content

Backward integration does not step to the first occuring callbackΒ #1231

@dcourteville

Description

@dcourteville

Describe the bug 🐞

When multiple distinct callbacks occur in an single time step during backward integration, the integrator will step to the last occurring callback (in the integration direction) instead of the first.

This is caused by this date comparison that doesn't account for integration direction :

https://github.com/SciML/DiffEqBase.jl/blob/2ff29cc394503da446f50127c31cc2b889b12b8f/src/callbacks.jl#L143C1-L143C68

I will open a pull request with the fix.

Minimal Reproducible Example πŸ‘‡

using OrdinaryDiffEq, Test

cb = ContinuousCallback(
    (u, t, integrator) -> t - 0e-8,
    (integrator) -> push!(record, 0)
)

vcb = VectorContinuousCallback(
    (out, u, t, integrator) -> out .= (t - 1e-8, t - 2e-8),
    (integrator, event_index) -> push!(record, event_index),
    2
)

f(u, p, t) = 1.0
u0 = 0.0

# Forward propagation with successive events
record = []
tspan = (-1.0, 1.0)
prob = ODEProblem(f, u0, tspan)
sol = solve(prob, Tsit5(), dt=2.0, callback=CallbackSet(cb, vcb))
@test record == [0,1,2]

# Backward propagation with successive events
record = []
tspan = (1.0, -1.0)
prob = ODEProblem(f, u0, tspan)
sol = solve(prob, Tsit5(), dt=2.0, callback=CallbackSet(cb, vcb))
@test record == [2,1,0] # Fail

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
[1dea7af3] OrdinaryDiffEq v6.104.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions