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.91.2) Remove instances of previous_Δt and fix a bug setting last_Δt in RK3 #3595

Merged
merged 21 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ const OnlyParticleTrackingModel = HydrostaticFreeSurfaceModel{TS, E, A, S, G, T,
{TS, E, A, S, G, T, V, B, R, F, P<:AbstractLagrangianParticles, U<:PrescribedVelocityFields, C<:NamedTuple{(), Tuple{}}}

function time_step!(model::OnlyParticleTrackingModel, Δt; callbacks = [], kwargs...)
model.timestepper.previous_Δt = Δt
tick!(model.clock, Δt)
model.clock.last_Δt = Δt
step_lagrangian_particles!(model, Δt)
update_state!(model, callbacks)
end
Expand Down
7 changes: 3 additions & 4 deletions src/OutputWriters/checkpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function set!(model, filepath::AbstractString)
# Update model clock
model.clock.iteration = checkpointed_clock.iteration
model.clock.time = checkpointed_clock.time
model.clock.last_Δt = checkpointed_clock.last_Δt
end

return nothing
Expand Down Expand Up @@ -260,8 +261,6 @@ end
set_time_stepper!(timestepper::RungeKutta3TimeStepper, file, model_fields) =
set_time_stepper_tendencies!(timestepper, file, model_fields)

function set_time_stepper!(timestepper::QuasiAdamsBashforth2TimeStepper, file, model_fields)
set_time_stepper!(timestepper::QuasiAdamsBashforth2TimeStepper, file, model_fields) =
set_time_stepper_tendencies!(timestepper, file, model_fields)
timestepper.previous_Δt = file["timestepper/previous_Δt"]
return nothing
end

1 change: 0 additions & 1 deletion src/OutputWriters/output_writer_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ end
function serializeproperty!(file, address, ts::QuasiAdamsBashforth2TimeStepper)
serializeproperty!(file, address * "/Gⁿ", ts.Gⁿ)
serializeproperty!(file, address * "/G⁻", ts.G⁻)
serializeproperty!(file, address * "/previous_Δt", ts.previous_Δt)
return nothing
end

Expand Down
2 changes: 1 addition & 1 deletion src/Simulations/time_step_wizard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ for advective and diffusive Courant-Friedrichs-Lewy (CFL) numbers (`cfl` and `di
subject to the limits

```julia
max(min_Δt, min_change * previous_Δt) ≤ new_Δt ≤ min(max_Δt, max_change * previous_Δt)
max(min_Δt, min_change * last_Δt) ≤ new_Δt ≤ min(max_Δt, max_change * last_Δt)
```

where `new_Δt` is the new time step calculated by the `TimeStepWizard`.
Expand Down
8 changes: 4 additions & 4 deletions src/Solvers/heptadiagonal_iterative_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mutable struct HeptadiagonalIterativeSolver{G, R, L, D, M, P, PM, PS, I, ST, T,
iterative_solver :: I
state_vars :: ST
tolerance :: T
previous_Δt :: F
last_Δt :: F
maximum_iterations :: Int
verbose :: Bool
end
Expand Down Expand Up @@ -70,7 +70,7 @@ The matrix constructors are calculated based on the pentadiagonal coeffients pas
to `matrix_from_coefficients` function.

To allow for variable time step, the diagonal term `- Az / (g * Δt²)` is only added later on
and it is updated only when the previous time step changes (`previous_Δt != Δt`).
and it is updated only when the previous time step changes (`last_Δt != Δt`).

Preconditioning is done through the various methods implemented in `Solvers/sparse_preconditioners.jl`.

Expand Down Expand Up @@ -296,7 +296,7 @@ function solve!(x, solver::HeptadiagonalIterativeSolver, b, Δt)
arch = architecture(solver.matrix)

# update matrix and preconditioner if time step changes
if Δt != solver.previous_Δt
if Δt != solver.last_Δt
constructors = deepcopy(solver.matrix_constructors)
M = prod(solver.problem_size)
update_diag!(constructors, arch, M, M, solver.diagonal, Δt, 0)
Expand All @@ -308,7 +308,7 @@ function solve!(x, solver::HeptadiagonalIterativeSolver, b, Δt)
solver.matrix,
solver.preconditioner_settings)

solver.previous_Δt = Δt
solver.last_Δt = Δt
end

solver.iterative_solver(x, solver.matrix, b,
Expand Down
52 changes: 38 additions & 14 deletions src/TimeSteppers/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,31 @@ The `stage` is updated only for multi-stage time-stepping methods. The `time::T`
either a number or a `DateTime` object.
"""
mutable struct Clock{TT, DT}
time :: TT
last_Δt :: DT
time :: TT
last_Δt :: DT
last_stage_Δt :: DT
iteration :: Int
stage :: Int
stage :: Int
end

"""
Clock(; time, last_Δt = Inf, iteration=0, stage=1)
Clock(; time, last_Δt=Inf, last_stage_Δt=Inf, iteration=0, stage=1)

Returns a `Clock` object. By default, `Clock` is initialized to the zeroth `iteration`
and first time step `stage` with `last_Δt`.
and first time step `stage` with `last_Δt=last_stage_Δt=Inf`.
"""
Clock(; time::TT, last_Δt::DT=Inf, iteration=0, stage=1) where {TT, DT} = Clock{TT, DT}(time, last_Δt, iteration, stage)
function Clock(; time,
last_Δt = Inf,
last_stage_Δt = Inf,
iteration = 0,
stage = 1)

TT = typeof(time)
DT = typeof(last_Δt)
last_stage_Δt = convert(DT, last_Δt)
return Clock{TT, DT}(time, last_Δt, last_stage_Δt, iteration, stage)
end

# TODO: when supporting DateTime, this function will have to be extended
time_step_type(TT) = TT

Expand All @@ -35,10 +47,20 @@ function Clock{TT}(; time, last_Δt=Inf, iteration=0, stage=1) where TT
return Clock{TT, DT}(time, last_Δt, iteration, stage)
end

Base.summary(clock::Clock) = string("Clock(time=$(prettytime(clock.time)), iteration=$(clock.iteration), last_Δt=$(prettytime(clock.last_Δt)))")
function Base.summary(clock::Clock)
TT = typeof(clock.time)
DT = typeof(clock.last_Δt)
return string("Clock{", TT, ", ", DT, "}",
"(time=", prettytime(clock.time),
" iteration=", clock.iteration,
" last_Δt=", prettytime(clock.last_Δt), ")")
end

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)")
function Base.show(io::IO, clock::Clock)
return print(io, summary(clock), '\n',
"├── stage: ", clock.stage, '\n',
"└── last_stage_Δt: ", prettytime(clock.last_stage_Δt))
end

next_time(clock, Δt) = clock.time + Δt
next_time(clock::Clock{<:AbstractTime}, Δt) = clock.time + Nanosecond(round(Int, 1e9 * Δt))
Expand All @@ -61,8 +83,6 @@ 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 @@ -73,7 +93,11 @@ function tick!(clock, Δt; stage=false)
return nothing
end

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


11 changes: 3 additions & 8 deletions src/TimeSteppers/quasi_adams_bashforth_2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using Oceananigans.ImmersedBoundaries: ActiveCellsIBG, active_linear_index_to_tu

mutable struct QuasiAdamsBashforth2TimeStepper{FT, GT, IT} <: AbstractTimeStepper
χ :: FT
previous_Δt :: FT
Gⁿ :: GT
G⁻ :: GT
implicit_solver :: IT
Expand Down Expand Up @@ -53,10 +52,7 @@ function QuasiAdamsBashforth2TimeStepper(grid, tracers,
return QuasiAdamsBashforth2TimeStepper{FT, GT, IT}(χ, Inf, Gⁿ, G⁻, implicit_solver)
end

function reset!(timestepper::QuasiAdamsBashforth2TimeStepper)
timestepper.previous_Δt = Inf
return nothing
end
reset!(timestepper::QuasiAdamsBashforth2TimeStepper) = nothing

#####
##### Time steppping
Expand All @@ -73,7 +69,7 @@ function time_step!(model::AbstractModel{<:QuasiAdamsBashforth2TimeStepper}, Δt
Δt == 0 && @warn "Δt == 0 may cause model blowup!"

# Shenanigans for properly starting the AB2 loop with an Euler step
euler = euler || (Δt != model.timestepper.previous_Δt)
euler = euler || (Δt != model.clock.last_Δt)

χ = ifelse(euler, convert(eltype(model.grid), -0.5), model.timestepper.χ)

Expand All @@ -86,8 +82,6 @@ function time_step!(model::AbstractModel{<:QuasiAdamsBashforth2TimeStepper}, Δt
end
end

model.timestepper.previous_Δt = Δt

# Be paranoid and update state at iteration 0
model.clock.iteration == 0 && update_state!(model, callbacks)

Expand All @@ -97,6 +91,7 @@ function time_step!(model::AbstractModel{<:QuasiAdamsBashforth2TimeStepper}, Δt
@apply_regionally correct_velocities_and_store_tendecies!(model, Δt)

tick!(model.clock, Δt)
clock.last_Δt = Δt
glwagner marked this conversation as resolved.
Show resolved Hide resolved
update_state!(model, callbacks; compute_tendencies)
step_lagrangian_particles!(model, Δt)

Expand Down
4 changes: 4 additions & 0 deletions src/TimeSteppers/runge_kutta_3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function time_step!(model::AbstractModel{<:RungeKutta3TimeStepper}, Δt; callbac
pressure_correct_velocities!(model, first_stage_Δt)

tick!(model.clock, first_stage_Δt; stage=true)
clock.last_stage_Δt = first_stage_Δt
store_tendencies!(model)
update_state!(model, callbacks)
step_lagrangian_particles!(model, first_stage_Δt)
Expand All @@ -119,6 +120,7 @@ function time_step!(model::AbstractModel{<:RungeKutta3TimeStepper}, Δt; callbac
pressure_correct_velocities!(model, second_stage_Δt)

tick!(model.clock, second_stage_Δt; stage=true)
clock.last_stage_Δt = second_stage_Δt
store_tendencies!(model)
update_state!(model, callbacks)
step_lagrangian_particles!(model, second_stage_Δt)
Expand All @@ -133,6 +135,8 @@ function time_step!(model::AbstractModel{<:RungeKutta3TimeStepper}, Δt; callbac
pressure_correct_velocities!(model, third_stage_Δt)

tick!(model.clock, third_stage_Δt)
clock.last_stage_Δt = third_stage_Δt
clock.last_Δt = Δt
update_state!(model, callbacks; compute_tendencies)
step_lagrangian_particles!(model, third_stage_Δt)

Expand Down