From 2b1b9642dc4fc627ce73a40abede4c9577fade17 Mon Sep 17 00:00:00 2001 From: milankl Date: Mon, 6 Jan 2025 23:56:35 -0100 Subject: [PATCH 1/3] set! for time step --- src/dynamics/time_integration.jl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/dynamics/time_integration.jl b/src/dynamics/time_integration.jl index a30c3aa02..acd90f49a 100644 --- a/src/dynamics/time_integration.jl +++ b/src/dynamics/time_integration.jl @@ -114,7 +114,7 @@ function initialize!(L::Leapfrog, model::AbstractModel) L.Δt = L.Δt_sec/L.radius end - # check how time stepping time step and output time step align + # check how time steps from time integration and output align n = round(Int, Millisecond(output_dt).value/L.Δt_millisec.value) nΔt = n*L.Δt_millisec if nΔt != output_dt @@ -122,6 +122,21 @@ function initialize!(L::Leapfrog, model::AbstractModel) end end +"""$(TYPEDSIGNATURES) +Change time step of timestepper `L` to `Δt` +and disables adjustment to output frequency.""" +function set!(L::AbstractTimeStepper, Δt::Period) + L.Δt_millisec = Millisecond(Δt) + L.Δt_sec = L.Δt_millisec.value/1000 + L.Δt = L.Δt_sec/L.radius + + resolution_factor = (L.trunc+1)/(DEFAULT_TRUNC+1) + L.Δt_at_T31 = Second(round(Int, L.Δt_sec*resolution_factor)) + + L.adjust_with_output = false + return L +end + """ $(TYPEDSIGNATURES) Performs one leapfrog time step with (`lf=2`) or without (`lf=1`) Robert+Williams filter From 9ad7b07494cb793afadeeba52dfef2d9327f441e Mon Sep 17 00:00:00 2001 From: milankl Date: Tue, 7 Jan 2025 00:00:09 -0100 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74bc44421..bc28382da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- set! for time step [#650](https://github.com/SpeedyWeather/SpeedyWeather.jl/pull/650) - Spectral Gradients are now differentiable, with correctness check in extended CI [#638](https://github.com/SpeedyWeather/SpeedyWeather.jl/pull/638) - Tracer advection [#579](https://github.com/SpeedyWeather/SpeedyWeather.jl/pull/579) - Buildkite CI with dummy pipeline [#646](https://github.com/SpeedyWeather/SpeedyWeather.jl/pull/646) From cf8cb7bbac04a88bff9eac432f087f9820f7b144 Mon Sep 17 00:00:00 2001 From: milankl Date: Tue, 7 Jan 2025 09:44:33 -0100 Subject: [PATCH 3/3] Add keyword method, comments, tests --- src/dynamics/time_integration.jl | 14 +++++++++++--- test/time_stepping.jl | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/dynamics/time_integration.jl b/src/dynamics/time_integration.jl index acd90f49a..0f583b0a7 100644 --- a/src/dynamics/time_integration.jl +++ b/src/dynamics/time_integration.jl @@ -123,20 +123,28 @@ function initialize!(L::Leapfrog, model::AbstractModel) end """$(TYPEDSIGNATURES) -Change time step of timestepper `L` to `Δt` +Change time step of timestepper `L` to `Δt` (unscaled) and disables adjustment to output frequency.""" -function set!(L::AbstractTimeStepper, Δt::Period) - L.Δt_millisec = Millisecond(Δt) +function set!( + L::AbstractTimeStepper, + Δt::Period, # unscaled time step in Second, Minute, ... +) + L.Δt_millisec = Millisecond(Δt) # recalculate all Δt fields L.Δt_sec = L.Δt_millisec.value/1000 L.Δt = L.Δt_sec/L.radius + # recalculate the default time step at resolution T31 to be consistent resolution_factor = (L.trunc+1)/(DEFAULT_TRUNC+1) L.Δt_at_T31 = Second(round(Int, L.Δt_sec*resolution_factor)) + # given Δt was manually set disallow adjustment to output frequency L.adjust_with_output = false return L end +# also allow for keyword arguments +set!(L::AbstractTimeStepper; Δt::Period) = set!(L, Δt) + """ $(TYPEDSIGNATURES) Performs one leapfrog time step with (`lf=2`) or without (`lf=1`) Robert+Williams filter diff --git a/test/time_stepping.jl b/test/time_stepping.jl index 63a924636..1f5c4324a 100644 --- a/test/time_stepping.jl +++ b/test/time_stepping.jl @@ -114,4 +114,16 @@ end @test M_Ronly < 1 @test M_Ronly <= M_RAW end +end + +@testset "Set timestep manually" begin + @testset for trunc in (31, 63, 127) + spectral_grid = SpectralGrid(; trunc) + time_stepping = Leapfrog(spectral_grid) + set!(time_stepping, Minute(10)) + @test time_stepping.Δt_sec == 10*60 + + set!(time_stepping, Δt=Minute(20)) + @test time_stepping.Δt_sec == 20*60 + end end \ No newline at end of file