Skip to content

Commit f4e3c29

Browse files
authored
Merge pull request #5 from SpeedyWeather/mk/docstrings
add docstrings and DocStringExtensions
2 parents 4e76599 + 39ba13d commit f4e3c29

File tree

4 files changed

+77
-17
lines changed

4 files changed

+77
-17
lines changed

Project.toml

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

66
[deps]
7+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
78
SpeedyWeather = "9e226e20-d153-4fed-8a5b-493def4f21a9"
89

910
[compat]
10-
julia = "1.8"
11+
DocStringExtensions = "0.9"
1112
SpeedyWeather = "0.8"
13+
julia = "1.8"
1214

1315
[extras]
1416
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/StochasticStir.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module StochasticStir
22

33
using SpeedyWeather
4+
using DocStringExtensions
45

56
export StochasticStirring, JetDrag
67

src/jet_drag.jl

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Defines the JetDrag drag term for SpeedyWeather that applies
3+
a zonal drag to the BarotropicModel or ShallowWaterModel following
4+
a jet at specified latitude, strength and width with time scale time_scale.
5+
$(TYPEDFIELDS)"""
16
Base.@kwdef struct JetDrag{NF} <: SpeedyWeather.AbstractDrag{NF}
27

38
# DIMENSIONS from SpectralGrid
@@ -22,42 +27,74 @@ Base.@kwdef struct JetDrag{NF} <: SpeedyWeather.AbstractDrag{NF}
2227
ζ₀::LowerTriangularMatrix{Complex{NF}} = zeros(LowerTriangularMatrix{Complex{NF}},trunc+2,trunc+1)
2328
end
2429

30+
"""
31+
$(TYPEDSIGNATURES)
32+
Generator function of a JetDrag struct taking resolution and number format from
33+
a SpectralGrid. Other options are provided as keyword arguments."""
2534
function JetDrag(SG::SpectralGrid;kwargs...)
2635
return JetDrag{SG.NF}(;SG.trunc,kwargs...)
2736
end
2837

38+
"""
39+
$(TYPEDSIGNATURES)
40+
Extends the initialize! function for a JetDrag. Precomputes the zonal velocity
41+
of the jet, transforms to spectral and takes the curl to get the corresponding
42+
relative vorticity of the jet, which is used to apply the drag to the vorticity
43+
equation."""
2944
function SpeedyWeather.initialize!( drag::JetDrag,
3045
model::ModelSetup)
3146

3247
(;spectral_grid, geometry) = model
3348
(;Grid,NF,nlat_half) = spectral_grid
34-
u = zeros(Grid{NF},nlat_half)
49+
lat = geometry.latds # latitude in ˚N for every grid point
3550

36-
lat = geometry.latds
51+
# zonal velocity of the jet, allocate following grid and number format NF of model
52+
u = zeros(Grid{NF},nlat_half)
3753

54+
# Gaussian in latitude, calculated for every grid point ij
3855
for ij in eachindex(u)
3956
u[ij] = drag.u₀ * exp(-(lat[ij]-drag.latitude)^2/(2*drag.width^2))
4057
end
4158

59+
# to spectral space of size lmax+1 × mmax as required by the curl
4260
û = SpeedyTransforms.spectral(u,one_more_degree=true)
4361
= zero(û)
4462
SpeedyTransforms.curl!(drag.ζ₀,û,v̂,model.spectral_transform)
4563
return nothing
4664
end
4765

48-
function SpeedyWeather.drag!( diagn::DiagnosticVariablesLayer,
49-
progn::PrognosticVariablesLayer,
50-
drag::JetDrag,
51-
time::DateTime,
52-
model::ModelSetup)
66+
# function barrier to unpack only what's needed
67+
function SpeedyWeather.drag!(
68+
diagn::DiagnosticVariablesLayer,
69+
progn::PrognosticVariablesLayer,
70+
drag::JetDrag,
71+
time::DateTime,
72+
model::ModelSetup,
73+
)
74+
SpeedyWeather.drag!(diagn,progn,drag,model.geometry)
75+
end
5376

77+
"""
78+
$(TYPEDSIGNATURES)
79+
Extends the drag! function for JetDrag. Adds the -r*(ζ-ζ₀) term
80+
to the vorticity tendency.
81+
"""
82+
function SpeedyWeather.drag!(
83+
diagn::DiagnosticVariablesLayer,
84+
progn::PrognosticVariablesLayer,
85+
drag::JetDrag,
86+
geometry::Geometry,
87+
)
5488
(;vor) = progn
5589
(;vor_tend) = diagn.tendencies
5690
(;ζ₀) = drag
5791

58-
(;radius) = model.spectral_grid
92+
# 1/time_scale [1/s] but scaled with radius as is the vorticity equation
93+
(;radius) = geometry
5994
r = radius/drag.time_scale.value
60-
for lm in eachindex(vor,vor_tend,ζ₀)
95+
96+
# add the -r*(ζ-ζ₀) term to the vorticity tendency that already includes the forcing term
97+
@inbounds for lm in eachindex(vor,vor_tend,ζ₀)
6198
vor_tend[lm] -= r*(vor[lm] - ζ₀[lm])
6299
end
63100
end

src/stochastic_stirring.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""A SpeedyWeather forcing term that stochastically stirrs relative vorticity in
2+
the BarotropicModel or the ShallowWaterModel.
3+
$(TYPEDFIELDS)"""
14
Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing{NF}
25

36
# DIMENSIONS from SpectralGrid
@@ -47,12 +50,22 @@ Base.@kwdef struct StochasticStirring{NF} <: SpeedyWeather.AbstractForcing{NF}
4750
lat_mask::Vector{NF} = zeros(NF,nlat)
4851
end
4952

53+
"""
54+
$(TYPEDSIGNATURES)
55+
Generator function for StochasticStirring using resolution and number format
56+
from a SpectralGrid. Further options should be provided as keyword arguments."""
5057
function StochasticStirring(SG::SpectralGrid;kwargs...)
5158
(;trunc,Grid,nlat_half) = SG
5259
nlat = RingGrids.get_nlat(Grid,nlat_half)
5360
return StochasticStirring{SG.NF}(;trunc,nlat,kwargs...)
5461
end
5562

63+
"""
64+
$(TYPEDSIGNATURES)
65+
Extends the initialize! function for StochasticStirring to precompute
66+
the AR1 coefficients for the stochastic processes per spherical harmonic.
67+
Also precomputes a Gaussian latitudinal mask to only force at given latitude
68+
with width as specified in StochasticStirring."""
5669
function SpeedyWeather.initialize!( forcing::StochasticStirring,
5770
model::ModelSetup)
5871

@@ -61,14 +74,14 @@ function SpeedyWeather.initialize!( forcing::StochasticStirring,
6174
A = radius^2 * forcing.strength
6275

6376
# precompute noise and auto-regressive factor, packed in RefValue for mutability
64-
dt = model.time_stepping.Δt_sec
77+
dt = model.time_stepping.Δt_sec # in seconds
6578
τ = forcing.decorrelation_time.value # in seconds
6679
forcing.a[] = A*sqrt(1 - exp(-2dt/τ))
6780
forcing.b[] = exp(-dt/τ)
6881

69-
# precompute the latitudinal mask
82+
# precompute the Gaussian latitudinal mask
7083
(;Grid,nlat_half) = model.spectral_grid
71-
latd = RingGrids.get_latd(Grid,nlat_half)
84+
latd = RingGrids.get_latd(Grid,nlat_half) # in ˚N
7285

7386
for j in eachindex(forcing.lat_mask)
7487
# Gaussian centred at forcing.latitude of width forcing.width
@@ -78,6 +91,7 @@ function SpeedyWeather.initialize!( forcing::StochasticStirring,
7891
return nothing
7992
end
8093

94+
# function barrier to unpack from model what's needed (spectral transform only here)
8195
function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
8296
progn::PrognosticVariablesLayer,
8397
forcing::StochasticStirring,
@@ -86,6 +100,13 @@ function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
86100
SpeedyWeather.forcing!(diagn,forcing,model.spectral_transform)
87101
end
88102

103+
"""
104+
$(TYPEDSIGNATURES)
105+
Extends the forcing! function for StochasticStirring. Evolves the stochastic
106+
coefficients S of the forcing following an AR1 process in time, transforms
107+
to grid-point space to apply a mask to only force specified latitudes then
108+
transforms back to force in spectral space where also the time stepping is
109+
applied."""
89110
function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
90111
forcing::StochasticStirring{NF},
91112
spectral_transform::SpectralTransform) where NF
@@ -108,16 +129,15 @@ function SpeedyWeather.forcing!(diagn::DiagnosticVariablesLayer,
108129
end
109130

110131
# to grid-point space
111-
S_grid = diagn.dynamics_variables.a_grid
132+
S_grid = diagn.dynamics_variables.a_grid # reuse general work array
112133
SpeedyTransforms.gridded!(S_grid,S,spectral_transform)
113134

114135
# mask everything but mid-latitudes
115136
RingGrids._scale_lat!(S_grid,forcing.lat_mask)
116137

117-
# back to spectral space
138+
# back to spectral space, write directly into vorticity tendency
118139
(;vor_tend) = diagn.tendencies
119140
SpeedyTransforms.spectral!(vor_tend,S_grid,spectral_transform)
120-
SpeedyTransforms.spectral_truncation!(vor_tend) # set lmax+1 to zero
121141

122142
return nothing
123143
end

0 commit comments

Comments
 (0)