-
Notifications
You must be signed in to change notification settings - Fork 186
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
Callback with SpecifiedTimes
also gets called in iteration 0
#2719
Comments
All of the callbacks are executed on iteration 0 currently: Oceananigans.jl/src/Simulations/run.jl Lines 157 to 205 in af21542
|
I'm not sure what's best. We're assuming that iteration 0 is scheduled. I guess under ordinary circumstances, iteration 0 is scheduled for Perhaps schedules themselves should somehow explicitly specify whether they should be actuated at iteration 0 or not. A downside of avoiding iteration 0 is that issues / bugs with a callback are not caught until first actuation. So it may be a sensible default to actuate at iteration 0. |
I would argue that If no one opposes this, I can open a PR to make this change. |
This is about more than just defining inuitive behavior, though. Executing a callback at iteration 0 might be considered a feature. However, I think that sometimes it's not desired. In reality, what we are missing is the concept of callback "initialization" (we are also missing the concept of callback "finalization"). Right now, we use the hack that "calling at iteration 0" is tantamount to initialization. I think we should discuss how to generalize our design to something more sustainable... |
I think we should call them at iteration 0. Ideally we want an |
What if we add this feature to struct Callback{P, F, S, I}
func :: F
schedule :: S
parameters :: P
initialize :: I
end Then by default we set Callback(; ..., initialize=call_at_iteration_0) where call_at_iteration_0(callback, simulation) = iteration(simulation) == 0 && callback(simulation) so the default "initialization" is simply to "call" the callback at iteration 0 (as we currently do). Users can cancel this by setting Finally, rather than calling all the callbacks at iteration 0, we instead call Oceananigans.jl/src/Simulations/run.jl Line 166 in af21542
For "finalization" we need a bit more work, since I think we want to add the concept of finalizing a simulation as well, so we might need |
Yes I like that. And I like the idea of Note that a feature like that comes in needed in, e.g., #2657. |
I can work on this. We can build function finalize!(sim::Simulation)
# Finalize callbacks
[cb.finalize(cb, sim) for cb in sim.callbacks]
# Finalize model
finalize!(sim.model)
return nothing
end Models can then define appropriate finalizations. One layer down, we can have function finalize!(model::HydrostaticFreeSurfaceModel)
finalize!(model.free_surface)
return nothing
end |
Let's do this! :) |
Right now
SpecifiedTimes
is acting in a way different from its documentation. In addition to triggering a callback in the specified times, according to the docs, it's also triggering the callback in initialization.For example, the following MWE
produces the following output
Is this by design? If so the docs for
SpecifiedTimes
must be changed to account for that since they currently read:The text was updated successfully, but these errors were encountered: