Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent callback behavior #955

Closed
Vilin97 opened this issue Apr 26, 2023 · 1 comment
Closed

Inconsistent callback behavior #955

Vilin97 opened this issue Apr 26, 2023 · 1 comment

Comments

@Vilin97
Copy link

Vilin97 commented Apr 26, 2023

I encounter two weird bahaviors when solving u' = u.

  1. The solution is different by about 10^-6 depending on whether I use a callback that does nothing or not
  2. The number of calls of f! is different (11, 21, 12) based on whether I use a callback and whether I pass in tstops or dt kwarg.
using DifferentialEquations, TimerOutputs
ts = 0.0:0.1:1.0
affect!(integrator) = (DifferentialEquations.u_modified!(integrator, false)); nothing
cb = PresetTimeCallback(ts .+ 1e-6, affect!, save_positions=(false,false))
function f!(du,u,p,t) 
    @timeit "f!" (du .= u)
end
prob = ODEProblem(f!, [1.0], (0.0,1.0))

reset_timer!()
solution1 = solve(prob, alg = Euler(), saveat=ts, tstops = ts)
print_timer() # f! ncalls = 11
reset_timer!()
solution2 = solve(prob, alg = Euler(), saveat=ts, tstops = ts, callback = cb)
print_timer() # f! ncalls = 21
reset_timer!()
solution3 = solve(prob, alg = Euler(), saveat=ts, dt = 0.1, callback = cb)
print_timer() # f! ncalls = 12

maximum(abs, vcat((solution1.u .- solution3.u)...)) # 2.36e-7
maximum(abs, vcat((solution1.u .- solution2.u)...)) # 2.36e-6
maximum(abs, vcat((solution2.u .- solution3.u)...)) # 2.12e-6
@ChrisRackauckas
Copy link
Member

This looks correct. Explanation:

  1. It has to stop at the callbacks. Otherwise it would in theory be incorrect. Yes in your case here the callbacks don't do anything, but there's really no way to know in general if someone is doing something adverse inside of a callback. So if you set a callback to fire at a time t, it has to step to the time t.
  2. In the first case, dt is set so that way it hits each tstops value.
  3. In the second case, between each ts value there is an event that occurs, and so dt needs to be shrunk to 1e-6 and then the next step has dt = ts[i-1] - ts[i] - 1e-6. This is because the PresetTimeCallback is simply appending ts .+ 1e-6 to the ts array for the total tstops to have 21 points that it steps to.
  4. In the 4th case, the same thing occurs but now with a maximal dt = 0.1. So first it steps to 0.1, then 0.1 + 1e-6 for the vent, then 0.2 + 1e-6 for the next event, etc. to 1.0 for 12 steps.

And if you check solutioni.t, the time points are exactly as you have specified from the time stops. So then the last thing,

The solution is different by about 10^-6 depending on whether I use a callback that does nothing or not

Yes, because you changed dt, and the Euler method is only an approximation, so as dt changes you'll get a better or worse approximation of the ODE solution depending on whether the dt shrinks or grows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants