Skip to content

Exception with VectorContinuousCallback and backward integration #1184

@dcourteville

Description

@dcourteville

Describe the bug 🐞

Event detection with a VectorContinuousCallback and backward integration throw an exception when the integrator steps exactly on the event date.
This is caused by

min_t = nextfloat(top_t)

With the backward integration, top_t < min_t < bottom_t which cause the event to be ignored when top_t correspond to the event date. We should take the previous float instead for backward integration so that min_t < top_t < bottom_t
I will open a PR with the fix.

Minimal Reproducible Example 👇

using OrdinaryDiffEq

cb = VectorContinuousCallback(
    (out, u, t, integrator) -> out .= (t - 1.0, 1.0),
    (integrator, event_index) -> nothing,
    2
)

f(u, p, t) = 1.0
u0 = 0.0
tspan = (2.0, 0.0)
prob = ODEProblem(f, u0, tspan)
sol = solve(prob, Tsit5(), tstops=[1.0], callback=cb)

Error & Stacktrace ⚠️

ERROR: Callback handling failed. Please file an issue with code to reproduce.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] find_callback_time(integrator::OrdinaryDiffEqCore.ODEIntegrator{…}, callback::VectorContinuousCallback{…}, counter::Int64)
    @ DiffEqBase ~/.julia/packages/DiffEqBase/7xkMr/src/callbacks.jl:547
  [3] macro expansion
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/callbacks.jl:130 [inlined]
  [4] find_first_continuous_callback
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/callbacks.jl:125 [inlined]
  [5] find_first_continuous_callback
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/callbacks.jl:123 [inlined]
  [6] handle_callbacks!
    @ ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/integrators/integrator_utils.jl:379 [inlined]
  [7] _loopfooter!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…})
    @ OrdinaryDiffEqCore ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/integrators/integrator_utils.jl:284
  [8] loopfooter!
    @ ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/integrators/integrator_utils.jl:248 [inlined]
  [9] solve!(integrator::OrdinaryDiffEqCore.ODEIntegrator{…})
    @ OrdinaryDiffEqCore ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/solve.jl:621
 [10] #__solve#62
    @ ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/solve.jl:7 [inlined]
 [11] __solve
    @ ~/.julia/packages/OrdinaryDiffEqCore/zs1s7/src/solve.jl:1 [inlined]
 [12] solve_call(_prob::ODEProblem{…}, args::Tsit5{…}; merge_callbacks::Bool, kwargshandle::Nothing, kwargs::@Kwargs{})
    @ DiffEqBase ~/.julia/packages/DiffEqBase/7xkMr/src/solve.jl:657
 [13] solve_call
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/solve.jl:614 [inlined]
 [14] #solve_up#45
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/solve.jl:1211 [inlined]
 [15] solve_up
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/solve.jl:1188 [inlined]
 [16] #solve#43
    @ ~/.julia/packages/DiffEqBase/7xkMr/src/solve.jl:1083 [inlined]
 [17] top-level scope
    @ ~/Documents/sandbox/error.jl:13

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