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 21 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Oceananigans"
uuid = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
authors = ["Climate Modeling Alliance and contributors"]
version = "0.95.2"
version = "0.95.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
36 changes: 30 additions & 6 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,19 @@ 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

# Align nearest_next_actuation_time with simulation stop_time
nearest_next_actuation_time = min(nearest_next_actuation_time, sim.stop_time)

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 @@ -102,11 +115,24 @@ end

const ModelCallsite = Union{TendencyCallsite, UpdateStateCallsite}


function time_step_or_skip!(sim)
model_callbacks = Tuple(cb for cb in values(sim.callbacks) if cb.callsite isa ModelCallsite)
Δt = aligned_time_step(sim, sim.Δt)
if Δt < sim.minimum_relative_step * sim.Δt
next_time = next_actuation_time(sim)
Comment on lines +121 to +123
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI, next_actuation_time(sim) and sim.model.clock.time + Δt are equivalent. I chose to use the former here to avoid errors related to the addition operation. But let me know if I should change it to just use the latter.

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't it complicate the code to define next_actuation_time in addition to aligned_time_step? Things go wrong if you change one but not the other. It's usually best to have one "source" of reality / truth

@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


""" Step `sim`ulation forward by one time step. """
function time_step!(sim::Simulation)

start_time_step = time_ns()
model_callbacks = Tuple(cb for cb in values(sim.callbacks) if cb.callsite isa ModelCallsite)

if !(sim.initialized) # execute initialization step
initialize!(sim)
Expand All @@ -118,8 +144,7 @@ function time_step!(sim::Simulation)
start_time = time_ns()
end

Δt = aligned_time_step(sim, sim.Δt)
time_step!(sim.model, Δt, callbacks=model_callbacks)
time_step_or_skip!(sim)

if sim.verbose
elapsed_initial_step_time = prettytime(1e-9 * (time_ns() - start_time))
Expand All @@ -130,8 +155,7 @@ function time_step!(sim::Simulation)
end

else # business as usual...
Δt = aligned_time_step(sim, sim.Δt)
time_step!(sim.model, Δt, callbacks=model_callbacks)
time_step_or_skip!(sim)
end

# Callbacks and callback-like things
Expand Down
35 changes: 21 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 All @@ -44,12 +45,16 @@ Keyword arguments

- `wall_time_limit`: Stop the simulation if it's been running for longer than this many
seconds of wall clock time.
- `minimum_relative_step`: time steps smaller than `Δt * minimum_relative_step` will be skipped.
This avoids extremely high values when writing the pressure to disk.
Default value is 0. See github.com/CliMA/Oceananigans.jl/issues/3593 for details.
"""
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 +93,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 +106,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: ", prettysummary(s.minimum_relative_step), "\n",
"├── 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
10 changes: 9 additions & 1 deletion test/test_simulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,17 @@ function run_basic_simulation_tests(arch)
simulation.callbacks[:tester] = Callback(capture_call_time, schedule, parameters=called_at)
run!(simulation)

@show called_at
@test all(called_at .≈ 0.0:schedule.interval:simulation.stop_time)


# Test that minimum_relative_step is running correctly
final_time = 1.0 + 1e-11
simulation = Simulation(model, Δt=1, stop_time=final_time, minimum_relative_step=1e-10)
run!(simulation)

@test simulation.model.clock.time == final_time
@test simulation.model.clock.iteration == 1

return nothing
end

Expand Down
Loading