Skip to content

Commit c224a68

Browse files
authored
Merge pull request #11 from SpeedyWeather/mk/speedyweather0.9
Adapted to upcoming SpeedyWeather v0.9
2 parents 57449ad + c5432b3 commit c224a68

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name = "StochasticStir"
22
uuid = "c3541e7a-ece5-4a49-9931-82ce8f5cb0be"
33
authors = ["Milan Klöwer <[email protected]> and contributors"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
88
SpeedyWeather = "9e226e20-d153-4fed-8a5b-493def4f21a9"
99

1010
[compat]
1111
DocStringExtensions = "0.9"
12-
SpeedyWeather = "0.8"
12+
SpeedyWeather = "0.9"
1313
julia = "1.8"
1414

1515
[extras]

src/jet_drag.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Defines the JetDrag drag term for SpeedyWeather that applies
33
a zonal drag to the BarotropicModel or ShallowWaterModel following
44
a jet at specified latitude, strength and width with time scale time_scale.
55
$(TYPEDFIELDS)"""
6-
Base.@kwdef struct JetDrag{NF} <: SpeedyWeather.AbstractDrag{NF}
6+
Base.@kwdef struct JetDrag{NF} <: SpeedyWeather.AbstractDrag
77

88
# DIMENSIONS from SpectralGrid
99
"Spectral resolution as max degree of spherical harmonics"
@@ -14,25 +14,25 @@ Base.@kwdef struct JetDrag{NF} <: SpeedyWeather.AbstractDrag{NF}
1414
time_scale::Second = Day(6)
1515

1616
"Jet strength [m/s]"
17-
u₀::Float64 = 20
17+
u₀::NF = 20
1818

1919
"latitude of Gaussian jet [˚N]"
20-
latitude::Float64 = 30
20+
latitude::NF = 30
2121

2222
"Width of Gaussian jet [˚]"
23-
width::Float64 = 6
23+
width::NF = 6
2424

2525
# TO BE INITIALISED
2626
"Relaxation back to reference vorticity"
27-
ζ₀::LowerTriangularMatrix{Complex{NF}} = zeros(LowerTriangularMatrix{Complex{NF}},trunc+2,trunc+1)
27+
ζ₀::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
2828
end
2929

3030
"""
3131
$(TYPEDSIGNATURES)
3232
Generator function of a JetDrag struct taking resolution and number format from
3333
a SpectralGrid. Other options are provided as keyword arguments."""
34-
function JetDrag(SG::SpectralGrid;kwargs...)
35-
return JetDrag{SG.NF}(;SG.trunc,kwargs...)
34+
function JetDrag(SG::SpectralGrid; kwargs...)
35+
return JetDrag{SG.NF}(; SG.trunc, kwargs...)
3636
end
3737

3838
"""
@@ -45,21 +45,21 @@ function SpeedyWeather.initialize!( drag::JetDrag,
4545
model::ModelSetup)
4646

4747
(;spectral_grid, geometry) = model
48-
(;Grid,NF,nlat_half) = spectral_grid
48+
(;Grid, NF, nlat_half) = spectral_grid
4949
lat = geometry.latds # latitude in ˚N for every grid point
5050

5151
# zonal velocity of the jet, allocate following grid and number format NF of model
52-
u = zeros(Grid{NF},nlat_half)
52+
u = zeros(Grid{NF}, nlat_half)
5353

5454
# Gaussian in latitude, calculated for every grid point ij
5555
for ij in eachindex(u)
5656
u[ij] = drag.u₀ * exp(-(lat[ij]-drag.latitude)^2/(2*drag.width^2))
5757
end
5858

5959
# to spectral space of size lmax+1 × mmax as required by the curl
60-
û = SpeedyTransforms.spectral(u,one_more_degree=true)
60+
û = SpeedyTransforms.spectral(u, one_more_degree=true)
6161
= zero(û)
62-
SpeedyTransforms.curl!(drag.ζ₀,û,v̂,model.spectral_transform)
62+
SpeedyTransforms.curl!(drag.ζ₀, û, v̂, model.spectral_transform)
6363
return nothing
6464
end
6565

@@ -71,7 +71,7 @@ function SpeedyWeather.drag!(
7171
time::DateTime,
7272
model::ModelSetup,
7373
)
74-
SpeedyWeather.drag!(diagn,progn,drag,model.geometry)
74+
SpeedyWeather.drag!(diagn, progn, drag, model.geometry)
7575
end
7676

7777
"""
@@ -85,16 +85,16 @@ function SpeedyWeather.drag!(
8585
drag::JetDrag,
8686
geometry::Geometry,
8787
)
88-
(;vor) = progn
89-
(;vor_tend) = diagn.tendencies
90-
(;ζ₀) = drag
88+
(; vor) = progn
89+
(; vor_tend) = diagn.tendencies
90+
(; ζ₀) = drag
9191

9292
# 1/time_scale [1/s] but scaled with radius as is the vorticity equation
93-
(;radius) = geometry
93+
(; radius) = geometry
9494
r = radius/drag.time_scale.value
9595

9696
# add the -r*(ζ-ζ₀) term to the vorticity tendency that already includes the forcing term
97-
@inbounds for lm in eachindex(vor,vor_tend,ζ₀)
97+
@inbounds for lm in eachindex(vor, vor_tend, ζ₀)
9898
vor_tend[lm] -= r*(vor[lm] - ζ₀[lm])
9999
end
100100
end

src/stochastic_stirring.jl

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const LTM = LowerTriangularMatrix # for convenience
2+
13
"""A SpeedyWeather forcing term that stochastically stirrs relative vorticity in
24
the BarotropicModel or the ShallowWaterModel.
35
$(TYPEDFIELDS)"""
4-
Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing{NF}
6+
Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing
57

68
# DIMENSIONS from SpectralGrid
79
"Spectral resolution as max degree of spherical harmonics"
@@ -16,13 +18,13 @@ Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing{NF}
1618
decorrelation_time::Second = Day(2)
1719

1820
"Stirring strength A [1/s²]"
19-
strength::Float64 = 7e-11
21+
strength::NF = 7e-11
2022

2123
"Stirring latitude [˚N]"
22-
latitude::Float64 = 45
24+
latitude::NF = 45
2325

2426
"Stirring width [˚]"
25-
width::Float64 = 24
27+
width::NF = 24
2628

2729
"Minimum degree of spherical harmonics to force"
2830
lmin::Int = 8
@@ -38,7 +40,7 @@ Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing{NF}
3840

3941
# TO BE INITIALISED
4042
"Stochastic stirring term S"
41-
S::LowerTriangularMatrix{Complex{NF}} = zeros(LowerTriangularMatrix{Complex{NF}},trunc+2,trunc+1)
43+
S::LTM{Complex{NF}} = zeros(LTM{Complex{NF}},trunc+2,trunc+1)
4244

4345
"a = A*sqrt(1 - exp(-2dt/τ)), the noise factor times the stirring strength [1/s²]"
4446
a::Base.RefValue{NF} = Ref(zero(NF))
@@ -54,10 +56,10 @@ end
5456
$(TYPEDSIGNATURES)
5557
Generator function for StochasticStirring using resolution and number format
5658
from a SpectralGrid. Further options should be provided as keyword arguments."""
57-
function StochasticStirring(SG::SpectralGrid;kwargs...)
58-
(;trunc,Grid,nlat_half) = SG
59-
nlat = RingGrids.get_nlat(Grid,nlat_half)
60-
return StochasticStirring{SG.NF}(;trunc,nlat,kwargs...)
59+
function StochasticStirring(SG::SpectralGrid; kwargs...)
60+
(;trunc, Grid, nlat_half) = SG
61+
nlat = RingGrids.get_nlat(Grid, nlat_half)
62+
return StochasticStirring{SG.NF}(; trunc, nlat, kwargs...)
6163
end
6264

6365
"""
@@ -70,7 +72,7 @@ function SpeedyWeather.initialize!( forcing::StochasticStirring,
7072
model::ModelSetup)
7173

7274
# precompute forcing strength, scale with radius^2 as is the vorticity equation
73-
(;radius) = model.spectral_grid
75+
(; radius) = model.spectral_grid
7476
A = radius^2 * forcing.strength
7577

7678
# precompute noise and auto-regressive factor, packed in RefValue for mutability
@@ -91,12 +93,14 @@ function SpeedyWeather.initialize!( forcing::StochasticStirring,
9193
end
9294

9395
# function barrier to unpack from model what's needed (spectral transform only here)
94-
function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
95-
progn::PrognosticVariablesLayer,
96-
forcing::StochasticStirring,
97-
time::DateTime,
98-
model::ModelSetup)
99-
SpeedyWeather.forcing!(diagn,forcing,model.spectral_transform)
96+
function SpeedyWeather.forcing!(
97+
diagn::DiagnosticVariablesLayer,
98+
progn::PrognosticVariablesLayer,
99+
forcing::StochasticStirring,
100+
time::DateTime,
101+
model::ModelSetup,
102+
)
103+
SpeedyWeather.forcing!(diagn, forcing, model.spectral_transform)
100104
end
101105

102106
"""
@@ -106,9 +110,11 @@ coefficients S of the forcing following an AR1 process in time, transforms
106110
to grid-point space to apply a mask to only force specified latitudes then
107111
transforms back to force in spectral space where also the time stepping is
108112
applied."""
109-
function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
110-
forcing::StochasticStirring{NF},
111-
spectral_transform::SpectralTransform) where NF
113+
function SpeedyWeather.forcing!(
114+
diagn::DiagnosticVariablesLayer,
115+
forcing::StochasticStirring{NF},
116+
spectral_transform::SpectralTransform
117+
) where NF
112118

113119
# noise and auto-regressive factors
114120
a = forcing.a[] # = sqrt(1 - exp(-2dt/τ))
@@ -130,14 +136,14 @@ function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
130136

131137
# to grid-point space
132138
S_grid = diagn.dynamics_variables.a_grid # reuse general work array
133-
SpeedyTransforms.gridded!(S_grid,S,spectral_transform)
139+
SpeedyTransforms.gridded!(S_grid, S, spectral_transform)
134140

135141
# mask everything but mid-latitudes
136-
RingGrids._scale_lat!(S_grid,forcing.lat_mask)
142+
RingGrids._scale_lat!(S_grid, forcing.lat_mask)
137143

138144
# back to spectral space, write directly into vorticity tendency
139-
(;vor_tend) = diagn.tendencies
140-
SpeedyTransforms.spectral!(vor_tend,S_grid,spectral_transform)
145+
(; vor_tend) = diagn.tendencies
146+
SpeedyTransforms.spectral!(vor_tend, S_grid, spectral_transform)
141147

142148
return nothing
143149
end

test/runtests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ using StochasticStir
33
using Test
44

55
@testset "StochasticStir.jl" begin
6-
spectral_grid = SpectralGrid(trunc=31,nlev=1)
6+
spectral_grid = SpectralGrid(trunc=31, nlev=1)
77

8-
drag = JetDrag(spectral_grid,time_scale=Day(6))
8+
drag = JetDrag(spectral_grid, time_scale=Day(6))
99
forcing = StochasticStirring(spectral_grid)
1010
initial_conditions = StartFromRest()
1111

1212
# with barotropic model
13-
model = BarotropicModel(;spectral_grid,initial_conditions,forcing,drag)
13+
model = BarotropicModel(;spectral_grid, initial_conditions, forcing, drag)
1414
simulation = initialize!(model)
1515

16-
run!(simulation,period=Day(5))
16+
run!(simulation, period=Day(5))
1717
@test simulation.model.feedback.nars_detected == false
1818

1919
# with shallow water model
20-
model = ShallowWaterModel(;spectral_grid,initial_conditions,forcing,drag)
20+
model = ShallowWaterModel(;spectral_grid, initial_conditions, forcing, drag)
2121
simulation = initialize!(model)
2222

23-
run!(simulation,period=Day(5))
23+
run!(simulation, period=Day(5))
2424
@test simulation.model.feedback.nars_detected == false
2525
end

0 commit comments

Comments
 (0)