SciML / DifferentialEquations.jl

Multi-language suite for high-performance solvers of differential equations and scientific machine learning (SciML) components. Ordinary differential equations (ODEs), stochastic differential equations (SDEs), delay differential equations (DDEs), differential-algebraic equations (DAEs), and more in Julia.
https://docs.sciml.ai/DiffEqDocs/stable/
Other
2.8k stars 222 forks source link

Unexpected behavior with `PresetTimeCallback` #1022

Closed aelmokadem closed 3 months ago

aelmokadem commented 3 months ago

When defining callback events with PresetTimeCallback, I see strange behaviors that is different whether I include the 0 timepoint in the event times or define the callback event in ODEProblem or solve.

When 0 is included in the event times and the callback is defined within ODEProblem, the middle event is missed. When 0 is included in the event times and the callback is defined within solve, all events are missed.


function mod(du, u, p, t)
    du[1] = -p[1]*u[1]
end

p = [1.0]
u0 = [10.0]
tspan = (0.0,72.0)

#dosetimes = [0.0:24.0:tspan[2];]
times1 = 0.0:24.0:tspan[2]
times2 = 24.0:24.0:tspan[2]
affect!(integrator) = integrator.u[1] += 10.0
cb1 = PresetTimeCallback(times1, affect!)
cb2 = PresetTimeCallback(times2, affect!)

# solve

prob1 = ODEProblem(mod, u0, tspan, p, callback=cb1)
prob2 = ODEProblem(mod, u0, tspan, p)

sol1 = solve(prob1)
sol2 = solve(prob2, callback=cb1)

plot(sol1, label="0 included in discrete times and callback defined in ODEProblem")
plot!(sol2, label="0 included in discrete times and callback defined in solve")
Screenshot 2024-03-07 at 10 31 09 AM

When 0 is removed from the event times and the callback is defined within ODEProblem, things look as expected. When 0 is removed from the event times and the callback is defined within solve, all events are missed.

## remove 0 timepoint from callback times

prob1 = ODEProblem(mod, u0, tspan, p, callback=cb2)
prob2 = ODEProblem(mod, u0, tspan, p)

sol1 = solve(prob1)
sol2 = solve(prob2, callback=cb2)

plot(sol1, label="0 not included in discrete times and callback defined in ODEProblem")
plot!(sol2, label="0 not included in discrete times and callback defined in solve")
Screenshot 2024-03-07 at 10 32 35 AM
Pkg.status()
  [0c46a032] DifferentialEquations v7.13.0
  [91a5bcdd] Plots v1.40.1