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

Order of PresetTimeCallback and an ODEFunction is non-intuitive #172

Closed
Vilin97 opened this issue Oct 10, 2023 · 1 comment
Closed

Order of PresetTimeCallback and an ODEFunction is non-intuitive #172

Vilin97 opened this issue Oct 10, 2023 · 1 comment

Comments

@Vilin97
Copy link

Vilin97 commented Oct 10, 2023

I am trying to understand the order of evaluation of a callback and an ode function. It seems somewhat cryptic. The callback seems to go before the ode function at first but then switches to going after, in effect skipping one step in-between.

using OrdinaryDiffEq, DiffEqCallbacks
tspan = (0.0, 2.0)
dt = 1.
ts = collect(tspan[1]:dt:tspan[2])
g!(integrator) = (u_modified!(integrator, false); println("$(integrator.t) g "))
f!(du,u,p,t) = (du .= u; println("$t f "))
cb = PresetTimeCallback(ts, g!, save_positions=(false, false))
solve(ODEProblem(f!, [1.0], tspan), Euler(), dt=dt, callback=cb)

Output:

0.0 g 
0.0 f 
1.0 f
1.0 g
2.0 f
2.0 g
@ChrisRackauckas
Copy link
Member

Described on Slack, this is because tspan[1] is in the time choice and thus initialize! is setup so that it calls the affect! at time 0. This is a special behavior to ensure correctness, given that the user in this instance told us of the special case that there should be an event applied before the start of the simulation.

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