Skip to content

Commit

Permalink
Adds last_Δt to Clock (#3508)
Browse files Browse the repository at this point in the history
* added `Δt` to `Clock`

* fixed clock to work for DateTime

* changed name to `last_Δt`

* fixed Clock adapt

* updated docstring

* Update src/TimeSteppers/clock.jl

Co-authored-by: Gregory L. Wagner <[email protected]>

* fixed constructor

* updated `reset!`

* fixed formatting

* Apply suggestions from code review

Co-authored-by: Gregory L. Wagner <[email protected]>

* Update src/TimeSteppers/clock.jl

Co-authored-by: Navid C. Constantinou <[email protected]>

* increase size_threshold=1MiB

* fix doctest

* fix doctest

* Update pipeline.yml

* fix size_threshold

---------

Co-authored-by: Gregory L. Wagner <[email protected]>
Co-authored-by: Navid C. Constantinou <[email protected]>
  • Loading branch information
3 people committed Mar 21, 2024
1 parent f98c952 commit e243e5b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .buildkite/distributed/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
agents:
queue: new-central
slurm_mem: 8G
modules: climacommon/2024_02_27
modules: climacommon/2024_03_18


env:
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ format = Documenter.HTML(collapselevel = 1,
prettyurls = get(ENV, "CI", nothing) == "true",
canonical = "https://clima.github.io/OceananigansDocumentation/stable/",
mathengine = MathJax3(),
size_threshold = 819200,
size_threshold = 2^20,
assets = String["assets/citations.css"])

makedocs(sitename = "Oceananigans.jl",
Expand Down
4 changes: 2 additions & 2 deletions docs/src/model_setup/background_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ model.background_fields.velocities.u
FunctionField located at (Face, Center, Center)
├── func: U (generic function with 1 method)
├── grid: 1×1×1 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── clock: Clock(time=0 seconds, iteration=0)
├── clock: Clock(time=0 seconds, iteration=0, last_Δt=Inf days)
└── parameters: nothing
```

Expand Down Expand Up @@ -99,6 +99,6 @@ model.background_fields.tracers.b
FunctionField located at (Center, Center, Center)
├── func: B (generic function with 1 method)
├── grid: 1×1×1 RectilinearGrid{Float64, Periodic, Periodic, Bounded} on CPU with 3×3×3 halo
├── clock: Clock(time=0 seconds, iteration=0)
├── clock: Clock(time=0 seconds, iteration=0, last_Δt=Inf days)
└── parameters: (α = 3.14, N = 1.0, f = 0.1)
```
6 changes: 3 additions & 3 deletions docs/src/model_setup/clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ end

```jldoctest
julia> clock = Clock(time=0.0)
Clock{Float64}: time = 0 seconds, iteration = 0, stage = 1
Clock{Float64, Float64}: time = 0 seconds, last_Δt = Inf days, iteration = 0, stage = 1
```

but can be modified to start the model clock at some other time.
For example, passing

```jldoctest
julia> clock = Clock(time=3600.0)
Clock{Float64}: time = 1 hour, iteration = 0, stage = 1
Clock{Float64, Float64}: time = 1 hour, last_Δt = Inf days, iteration = 0, stage = 1
```

to the constructor for `NonhydrostaticModel` causes the simulation
Expand All @@ -37,7 +37,7 @@ for example, pass
julia> using TimesDates
julia> clock = Clock(time=TimeDate(2020))
Clock{TimesDates.TimeDate}: time = 2020-01-01T00:00:00, iteration = 0, stage = 1
Clock{TimesDates.TimeDate, Float64}: time = 2020-01-01T00:00:00, last_Δt = Inf days, iteration = 0, stage = 1
```

to `NonhydrostaticModel`. `TimesDates.TimeDate` supports nanosecond resolution and is thus recommended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ default_free_surface(grid; gravitational_acceleration=g_Earth) =

"""
HydrostaticFreeSurfaceModel(; grid,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid)}(time = 0),
momentum_advection = CenteredSecondOrder(),
tracer_advection = CenteredSecondOrder(),
buoyancy = SeawaterBuoyancy(eltype(grid)),
Expand Down Expand Up @@ -103,7 +103,7 @@ Keyword arguments
- `auxiliary_fields`: `NamedTuple` of auxiliary fields. Default: `nothing`.
"""
function HydrostaticFreeSurfaceModel(; grid,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid)}(time = 0),
momentum_advection = CenteredSecondOrder(),
tracer_advection = CenteredSecondOrder(),
buoyancy = SeawaterBuoyancy(eltype(grid)),
Expand Down
4 changes: 2 additions & 2 deletions src/Models/NonhydrostaticModels/nonhydrostatic_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end

"""
NonhydrostaticModel(; grid,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid)}(time = 0),
advection = CenteredSecondOrder(),
buoyancy = nothing,
coriolis = nothing,
Expand Down Expand Up @@ -105,7 +105,7 @@ Keyword arguments
- `auxiliary_fields`: `NamedTuple` of auxiliary fields. Default: `nothing`
"""
function NonhydrostaticModel(; grid,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid)}(time = 0),
advection = CenteredSecondOrder(),
buoyancy = nothing,
coriolis = nothing,
Expand Down
4 changes: 2 additions & 2 deletions src/Models/ShallowWaterModels/shallow_water_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct VectorInvariantFormulation end
"""
ShallowWaterModel(; grid,
gravitational_acceleration,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid)}(time = 0),
momentum_advection = UpwindBiasedFifthOrder(),
tracer_advection = WENO(),
mass_advection = WENO(),
Expand Down Expand Up @@ -112,7 +112,7 @@ Keyword arguments
function ShallowWaterModel(;
grid,
gravitational_acceleration,
clock = Clock{eltype(grid)}(0, 0, 1),
clock = Clock{eltype(grid), eltype(grid)}(0, Inf, 0, 1),
momentum_advection = UpwindBiasedFifthOrder(),
tracer_advection = WENO(),
mass_advection = WENO(),
Expand Down
3 changes: 2 additions & 1 deletion src/Simulations/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ run_wall_time(sim::Simulation) = prettytime(sim.run_wall_time)
Reset `sim`ulation, `model.clock`, and `model.timestepper` to their initial state.
"""
function reset!(sim::Simulation)
sim.model.clock.time = 0.0
sim.model.clock.time = 0
sim.model.clock.last_Δt = Inf
sim.model.clock.iteration = 0
sim.model.clock.stage = 1
sim.stop_iteration = Inf
Expand Down
35 changes: 24 additions & 11 deletions src/TimeSteppers/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,39 @@ import Base: show
import Oceananigans.Units: Time

"""
mutable struct Clock{T<:Number}
mutable struct Clock{T, FT}
Keeps track of the current `time`, `iteration` number, and time-stepping `stage`.
Keeps track of the current `time`, `last_Δt`, `iteration` number, and time-stepping `stage`.
The `stage` is updated only for multi-stage time-stepping methods. The `time::T` is
either a number or a `DateTime` object.
"""
mutable struct Clock{T}
time :: T
mutable struct Clock{TT, DT}
time :: TT
last_Δt :: DT
iteration :: Int
stage :: Int
end

"""
Clock(; time, iteration=0, stage=1)
Clock(; time, last_Δt = Inf, iteration=0, stage=1)
Returns a `Clock` object. By default, `Clock` is initialized to the zeroth `iteration`
and first time step `stage`.
and first time step `stage` with `last_Δt`.
"""
Clock(; time::T, iteration=0, stage=1) where T = Clock{T}(time, iteration, stage)
Clock(; time::TT, last_Δt::DT=Inf, iteration=0, stage=1) where {TT, DT} = Clock{TT, DT}(time, last_Δt, iteration, stage)
# TODO: when supporting DateTime, this function will have to be extended
time_step_type(TT) = TT

function Clock{TT}(; time, last_Δt=Inf, iteration=0, stage=1) where TT
DT = time_step_type(TT)
last_Δt = convert(DT, last_Δt)
return Clock{TT, DT}(time, last_Δt, iteration, stage)
end

Base.summary(clock::Clock) = string("Clock(time=$(prettytime(clock.time)), iteration=$(clock.iteration))")
Base.summary(clock::Clock) = string("Clock(time=$(prettytime(clock.time)), iteration=$(clock.iteration), last_Δt=$(prettytime(clock.last_Δt)))")

Base.show(io::IO, c::Clock{T}) where T =
println(io, "Clock{$T}: time = $(prettytime(c.time)), iteration = $(c.iteration), stage = $(c.stage)")
Base.show(io::IO, c::Clock{TT, DT}) where {TT, DT} =
println(io, "Clock{$TT, $DT}: time = $(prettytime(c.time)), last_Δt = $(prettytime(c.last_Δt)), iteration = $(c.iteration), stage = $(c.stage)")

next_time(clock, Δt) = clock.time + Δt
next_time(clock::Clock{<:AbstractTime}, Δt) = clock.time + Nanosecond(round(Int, 1e9 * Δt))
Expand All @@ -52,6 +61,8 @@ function tick!(clock, Δt; stage=false)

tick_time!(clock, Δt)

clock.last_Δt = Δt

if stage # tick a stage update
clock.stage += 1
else # tick an iteration and reset stage
Expand All @@ -63,4 +74,6 @@ function tick!(clock, Δt; stage=false)
end

"Adapt `Clock` to work on the GPU via CUDAnative and CUDAdrv."
Adapt.adapt_structure(to, clock::Clock) = (time=clock.time, iteration=clock.iteration, stage=clock.stage)
Adapt.adapt_structure(to, clock::Clock) =
(time=clock.time, last_Δt=clock.last_Δt, iteration=clock.iteration, stage=clock.stage)

0 comments on commit e243e5b

Please sign in to comment.