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 14 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
19 changes: 17 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: schedule_aligned_time_step
import Oceananigans.Utils: schedule_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_time_step(sim, aligned_Δt)
clock = sim.model.clock
activities = collect_scheduled_activities(sim)
Expand Down Expand Up @@ -131,7 +140,13 @@ 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.minimum_relative_step * sim.Δt
next_time = next_actuation_time(sim)
@warn "Reseting clock to $next_time and skipping aligned time step Δt = $Δt"
sim.model.clock.time = next_time
else
time_step!(sim.model, Δt, callbacks=model_callbacks)
end
end

# Callbacks and callback-like things
Expand Down
32 changes: 18 additions & 14 deletions src/Simulations/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import Oceananigans.TimeSteppers: reset!
default_progress(simulation) = nothing

mutable struct Simulation{ML, DT, ST, DI, OW, CB}
model :: ML
Δt :: DT
stop_iteration :: Float64
stop_time :: ST
wall_time_limit :: Float64
diagnostics :: DI
output_writers :: OW
callbacks :: CB
run_wall_time :: Float64
running :: Bool
initialized :: Bool
verbose :: Bool
model :: ML
Δt :: DT
stop_iteration :: Float64
stop_time :: ST
wall_time_limit :: Float64
diagnostics :: DI
output_writers :: OW
callbacks :: CB
run_wall_time :: Float64
running :: Bool
initialized :: Bool
verbose :: Bool
minimum_relative_step :: Float64
end

"""
Expand Down Expand Up @@ -49,7 +50,8 @@ function Simulation(model; Δt,
verbose = true,
stop_iteration = Inf,
stop_time = Inf,
wall_time_limit = Inf)
wall_time_limit = Inf,
minimum_relative_step = 0)
tomchor marked this conversation as resolved.
Show resolved Hide resolved

if stop_iteration == Inf && stop_time == Inf && wall_time_limit == Inf
@warn "This simulation will run forever as stop iteration = stop time " *
Expand Down Expand Up @@ -88,7 +90,8 @@ function Simulation(model; Δt,
0.0,
false,
false,
verbose)
verbose,
Float64(minimum_relative_step))
Copy link
Collaborator

Choose a reason for hiding this comment

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

why Float64?

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 was following the other floats in the Simulation constructor, which are also converted to Float64. I can't remember the PR where this was decided, but it minimizes errors in time-step alignment. The error that this PR solves is an example of this type of error .

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably we should make it optional.
This will not work if we want to support Metal architectures that do not want Float64.

end

function Base.show(io::IO, s::Simulation)
Expand All @@ -100,6 +103,7 @@ function Base.show(io::IO, s::Simulation)
"├── Stop time: $(prettytime(s.stop_time))", "\n",
"├── Stop iteration: $(s.stop_iteration)", "\n",
"├── Wall time limit: $(s.wall_time_limit)", "\n",
"├── Minimum relative step: $(s.minimum_relative_step)", "\n",
tomchor marked this conversation as resolved.
Show resolved Hide resolved
"├── Callbacks: $(ordered_dict_show(s.callbacks, "│"))", "\n",
"├── Output writers: $(ordered_dict_show(s.output_writers, "│"))", "\n",
"└── Diagnostics: $(ordered_dict_show(s.diagnostics, "│"))")
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/schedules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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
Loading