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

(0.95.3) Reset model clock and skip time_step!ing if next actuation time is tiny #3606

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Simulations/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Oceananigans: AbstractModel, run_diagnostic!, write_output!
import Oceananigans: initialize!
import Oceananigans.OutputWriters: checkpoint_path, set!
import Oceananigans.TimeSteppers: time_step!
import Oceananigans.Utils: aligned_time_step
import Oceananigans.Utils: aligned_time_step, next_actuation_time

# Simulations are for running

Expand All @@ -21,6 +21,15 @@ function collect_scheduled_activities(sim)
return tuple(writers..., callbacks...)
end

function next_actuation_time(sim::Simulation)
activities = collect_scheduled_activities(sim)
nearest_next_actuation_time = Inf
for activity in activities
nearest_next_actuation_time = min(nearest_next_actuation_time, next_actuation_time(activity.schedule))
end
return nearest_next_actuation_time
end

function schedule_aligned_Δt(sim, aligned_Δt)
clock = sim.model.clock
activities = collect_scheduled_activities(sim)
Expand Down Expand Up @@ -131,7 +140,12 @@ function time_step!(sim::Simulation)

else # business as usual...
Δt = aligned_time_step(sim, sim.Δt)
time_step!(sim.model, Δt, callbacks=model_callbacks)
if Δt > (sim.Δt / 1e10)
tomchor marked this conversation as resolved.
Show resolved Hide resolved
time_step!(sim.model, Δt, callbacks=model_callbacks)
else
println("Skipping aligned time step, which is of ", Δt)
sim.model.clock.time = next_actuation_time(sim)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also be

Suggested change
sim.model.clock.time = next_actuation_time(sim)
sim.model.clock.time += Δt

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works when we're almost at next_actuation_time since it would skip that actuation time, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is

sim.model.clock.time + Δt

vs

next_actuation_time(sim)

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomchor still have this question

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry I missed this. It's been so long that I forgot the details, but looking at the code for both, it's possible they end up calculating the same thing since Δt is calculated with aligned_time_step().

end
end

# Callbacks and callback-like things
Expand Down
7 changes: 5 additions & 2 deletions src/Utils/schedules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ function (schedule::TimeInterval)(model)
end
end

next_actuation_time(schedule::TimeInterval) = schedule.previous_actuation_time + schedule.interval

function aligned_time_step(schedule::TimeInterval, clock, Δt)
next_actuation_time = schedule.previous_actuation_time + schedule.interval
return min(Δt, next_actuation_time - clock.time)
return min(Δt, next_actuation_time(schedule::TimeInterval) - clock.time)
end

#####
Expand All @@ -90,6 +91,8 @@ IterationInterval(interval; offset=0) = IterationInterval(interval, offset)

(schedule::IterationInterval)(model) = (model.clock.iteration - schedule.offset) % schedule.interval == 0

next_actuation_time(schedule::IterationInterval) = Inf

#####
##### WallTimeInterval
#####
Expand Down