-
Notifications
You must be signed in to change notification settings - Fork 24
/
prognostic_variables.jl
411 lines (317 loc) · 14.3 KB
/
prognostic_variables.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# how many time steps have to be stored for the time integration? Leapfrog = 2
const N_STEPS = 2
const LTM = LowerTriangularMatrix # just because it's shorter here
export PrognosticVariablesLayer
"""A layer of the prognostic variables in spectral space.
$(TYPEDFIELDS)"""
Base.@kwdef struct PrognosticVariablesLayer{NF<:AbstractFloat} <: AbstractVariables
"Spectral resolution as max degree of spherical harmonics"
trunc::Int
"Vorticity of horizontal wind field [1/s]"
vor ::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
"Divergence of horizontal wind field [1/s]"
div ::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
"Absolute temperature [K]"
temp ::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
"Specific humidity [kg/kg]"
humid::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
end
# generator function based on spectral grid
PrognosticVariablesLayer(SG::SpectralGrid) = PrognosticVariablesLayer{SG.NF}(trunc=SG.trunc)
function Base.show(io::IO, A::AbstractVariables)
println(io, "$(typeof(A))")
keys = propertynames(A)
print_fields(io, A, keys)
end
"""Collect the n time steps of PrognosticVariablesLayer
of an n-step time integration (leapfrog=2) into a single struct.
$(TYPEDFIELDS)."""
struct PrognosticLayerTimesteps{NF<:AbstractFloat} <: AbstractVariables
timesteps::Vector{PrognosticVariablesLayer{NF}} # N_STEPS-element vector for time steps
end
# generator function based on spectral grid
function PrognosticLayerTimesteps(SG::SpectralGrid)
return PrognosticLayerTimesteps([PrognosticVariablesLayer(SG) for _ in 1:N_STEPS])
end
"""The spectral and gridded prognostic variables at the surface.
$(TYPEDFIELDS)"""
Base.@kwdef struct PrognosticVariablesSurface{NF<:AbstractFloat} <: AbstractVariables
"Spectral resolution as max degree of spherical harmonics"
trunc::Int
"log of surface pressure [log(Pa)] for PrimitiveEquation, interface displacement [m] for ShallowWaterModel"
pres::LTM{Complex{NF}} = zeros(LTM{Complex{NF}}, trunc+2, trunc+1)
end
# generator function based on a SpectralGrid
PrognosticVariablesSurface(SG::SpectralGrid) = PrognosticVariablesSurface{SG.NF}(trunc=SG.trunc)
Base.@kwdef mutable struct PrognosticVariablesOcean{NF<:AbstractFloat, Grid<:AbstractGrid{NF}} <: AbstractVariables
"Resolution parameter of grid"
const nlat_half::Int
"Current time of the ocean variables"
time::DateTime = DEFAULT_DATE
# SEA
"Sea surface temperature [K]"
const sea_surface_temperature::Grid = zeros(Grid, nlat_half)
"Sea ice concentration [1]"
const sea_ice_concentration::Grid = zeros(Grid, nlat_half)
end
# generator function based on a SpectralGrid
function PrognosticVariablesOcean(SG::SpectralGrid)
(; nlat_half, Grid, NF) = SG
return PrognosticVariablesOcean{NF, Grid{NF}}(; nlat_half)
end
Base.@kwdef mutable struct PrognosticVariablesLand{NF<:AbstractFloat, Grid<:AbstractGrid{NF}} <: AbstractVariables
"Resolution parameter of grid"
const nlat_half::Int
"Current time of the land variables"
time::DateTime = DEFAULT_DATE
# LAND
"Land surface temperature [K]"
const land_surface_temperature::Grid = zeros(Grid, nlat_half)
"Snow depth [m]"
const snow_depth::Grid = zeros(Grid, nlat_half)
"Soil moisture layer 1, volume fraction [1]"
const soil_moisture_layer1::Grid = zeros(Grid, nlat_half)
"Soil moisture layer 2, volume fraction [1]"
const soil_moisture_layer2::Grid = zeros(Grid, nlat_half)
end
# generator function based on a SpectralGrid
function PrognosticVariablesLand(SG::SpectralGrid)
(; nlat_half, Grid, NF) = SG
return PrognosticVariablesLand{NF, Grid{NF}}(; nlat_half)
end
"""Collect the n time steps of PrognosticVariablesSurface
of an n-step time integration (leapfrog=2) into a single struct.
$(TYPEDFIELDS)."""
struct PrognosticSurfaceTimesteps{NF<:AbstractFloat} <: AbstractVariables
timesteps::Vector{PrognosticVariablesSurface{NF}} # N_STEPS-element vector for time steps
end
# generator function based on spectral grid
function PrognosticSurfaceTimesteps(SG::SpectralGrid)
return PrognosticSurfaceTimesteps([PrognosticVariablesSurface(SG) for _ in 1:N_STEPS])
end
export PrognosticVariables
struct PrognosticVariables{
NF<:AbstractFloat,
Grid<:AbstractGrid{NF},
M<:ModelSetup
} <: AbstractPrognosticVariables
# dimensions
trunc::Int # max degree of spherical harmonics
nlat_half::Int # resolution parameter of grids
nlev::Int # number of vertical levels
n_steps::Int # N_STEPS time steps that are stored
layers::Vector{PrognosticLayerTimesteps{NF}} # vector of vertical layers
surface::PrognosticSurfaceTimesteps{NF}
ocean::PrognosticVariablesOcean{NF, Grid}
land::PrognosticVariablesLand{NF, Grid}
particles::Vector{Particle{NF}}
# scaling
scale::Base.RefValue{NF}
clock::Clock
end
function PrognosticVariables(SG::SpectralGrid, model::ModelSetup)
(; trunc, nlat_half, nlev, Grid, NF) = SG
(; n_particles) = SG
# data structs
layers = [PrognosticLayerTimesteps(SG) for _ in 1:nlev] # vector of nlev layers
surface = PrognosticSurfaceTimesteps(SG)
ocean = PrognosticVariablesOcean(SG)
land = PrognosticVariablesLand(SG)
# particles advection
particles = zeros(Particle{NF}, n_particles)
scale = Ref(one(NF)) # initialize with scale=1, wrapped in RefValue for mutability
clock = Clock()
Model = model_class(model) # strip away the parameters
return PrognosticVariables{NF, Grid{NF}, Model}(trunc, nlat_half, nlev, N_STEPS,
layers, surface, ocean, land, particles,
scale, clock)
end
has(::PrognosticVariables{NF, Grid, M}, var_name::Symbol) where {NF, Grid, M} = has(M, var_name)
"""
copy!(progn_new::PrognosticVariables, progn_old::PrognosticVariables)
Copies entries of `progn_old` into `progn_new`. Only copies those variables that are present
in the model of both `progn_new` and `progn_old`.
"""
function Base.copy!(progn_new::PrognosticVariables, progn_old::PrognosticVariables)
var_names = propertynames(progn_old.layers[1].timesteps[1])
for var_name in var_names
if has(progn_new, var_name)
var = get_var(progn_old, var_name)
set_var!(progn_new, var_name, var)
end
end
pres = get_pressure(progn_old)
set_pressure!(progn_new, pres)
# synchronize the clock
progn_new.clock.time = progn_old.clock.time
return progn_new
end
# SET_VAR FUNCTIONS TO ASSIGN NEW VALUES TO PrognosticVariables
"""
set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:LowerTriangularMatrix};
lf::Integer=1) where NF
Sets the prognostic variable with the name `varname` in all layers at leapfrog index `lf`
with values given in `var` a vector with all information for all layers in spectral space.
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:LowerTriangularMatrix};
lf::Integer=1) where NF
@assert length(var) == length(progn.layers)
@assert has(progn, varname) "PrognosticVariables has no variable $varname"
for (progn_layer, var_layer) in zip(progn.layers, var)
_set_var_core!(getfield(progn_layer.timesteps[lf], varname), var_layer)
end
return progn
end
function _set_var_core!(var_old::LowerTriangularMatrix{T}, var_new::LowerTriangularMatrix{R}) where {T, R}
lmax, mmax = size(var_old) .- (1, 1)
var_new_trunc = spectral_truncation!(var_new, mmax+1, mmax)
copyto!(var_old, var_new_trunc)
end
"""
set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractGrid};
lf::Integer=1) where NF
Sets the prognostic variable with the name `varname` in all layers at leapfrog index `lf`
with values given in `var` a vector with all information for all layers in grid space.
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractGrid};
lf::Integer=1) where NF
@assert length(var) == length(progn.layers)
var_sph = [spectral(var_layer, one_more_degree=true) for var_layer in var]
return set_var!(progn, varname, var_sph; lf=lf)
end
"""
set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractGrid},
M::ModelSetup;
lf::Integer=1) where NF
Sets the prognostic variable with the name `varname` in all layers at leapfrog index `lf`
with values given in `var` a vector with all information for all layers in grid space.
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractGrid},
M::ModelSetup;
lf::Integer=1) where NF
@assert length(var) == length(progn.layers)
var_sph = [spectral(var_layer, M.spectral_transform) for var_layer in var]
return set_var!(progn, varname, var_sph; lf=lf)
end
"""
set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractMatrix},
Grid::Type{<:AbstractGrid}=FullGaussianGrid;
lf::Integer=1) where NF
Sets the prognostic variable with the name `varname` in all layers at leapfrog index `lf`
with values given in `var` a vector with all information for all layers in grid space.
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
var::Vector{<:AbstractMatrix},
Grid::Type{<:AbstractGrid}=FullGaussianGrid;
lf::Integer=1) where NF
@assert length(var) == length(progn.layers)
var_grid = [spectral(var_layer; Grid, one_more_degree=true) for var_layer in var]
return set_var!(progn, varname, var_grid; lf=lf)
end
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
s::Number;
lf::Integer=1) where NF
Sets all values of prognostic variable `varname` at leapfrog index `lf` to the scalar `s`.
"""
function set_var!(progn::PrognosticVariables{NF},
varname::Symbol,
s::Number;
lf::Integer=1) where NF
for progn_layer in progn.layers
fill!(getfield(progn_layer.timesteps[lf], varname), s)
end
return progn
end
"""
set_vorticity!(progn::PrognosticVariables, varargs...; kwargs...)
See [`set_var!`](@ref)
"""
set_vorticity!(progn::PrognosticVariables, varargs...; kwargs...) = set_var!(progn, :vor, varargs...; kwargs...)
"""
set_divergence!(progn::PrognosticVariables, varargs...; kwargs...)
See [`set_var!`](@ref)
"""
set_divergence!(progn::PrognosticVariables, varargs...; kwargs...) = set_var!(progn, :div, varargs...; kwargs...)
"""
set_temperature!(progn::PrognosticVariables, varargs...; kwargs...)
See [`set_var!`](@ref)
"""
set_temperature!(progn::PrognosticVariables, varargs...; kwargs...) = set_var!(progn, :temp, varargs...; kwargs...)
"""
set_humidity!(progn::PrognosticVariables, varargs...; kwargs...)
See [`set_var!`](@ref)
"""
set_humidity!(progn::PrognosticVariables, varargs...; kwargs...) = set_var!(progn, :humid, varargs...; kwargs...)
"""
set_pressure!(progn::PrognosticVariables{NF},
pressure::LowerTriangularMatrix;
lf::Integer=1) where NF
Sets the prognostic variable with the surface pressure in spectral space at leapfrog index `lf`.
"""
function set_pressure!(progn::PrognosticVariables,
pressure::LowerTriangularMatrix;
lf::Integer=1)
_set_var_core!(progn.surface.timesteps[lf].pres, pressure)
return progn
end
"""
set_pressure!(progn::PrognosticVariables{NF},
pressure::AbstractGrid,
M::ModelSetup;
lf::Integer=1) where NF
Sets the prognostic variable with the surface pressure in grid space at leapfrog index `lf`.
"""
set_pressure!(progn::PrognosticVariables, pressure::AbstractGrid, M::ModelSetup; lf::Integer=1) =
set_pressure!(progn, spectral(pressure, M.spectral_transform); lf)
"""
set_pressure!(progn::PrognosticVariables{NF},
pressure::AbstractGrid,
lf::Integer=1) where NF
Sets the prognostic variable with the surface pressure in grid space at leapfrog index `lf`.
"""
set_pressure!(progn::PrognosticVariables, pressure::AbstractGrid; lf::Integer=1) =
set_pressure!(progn, spectral(pressure, one_more_degree=true); lf)
"""
set_pressure!(progn::PrognosticVariables{NF},
pressure::AbstractMatrix,
Grid::Type{<:AbstractGrid},
lf::Integer=1) where NF
Sets the prognostic variable with the surface pressure in grid space at leapfrog index `lf`.
"""
set_pressure!(progn::PrognosticVariables, pressure::AbstractMatrix; lf::Integer=1,
Grid::Type{<:AbstractGrid}=FullGaussianGrid) = set_pressure!(progn, spectral(pressure; Grid, one_more_degree=true); lf)
"""
get_var(progn::PrognosticVariables, var_name::Symbol; lf::Integer=1)
Returns the prognostic variable `var_name` at leapfrog index `lf` as a `Vector{LowerTriangularMatrices}`.
"""
function get_var(progn::PrognosticVariables, var_name::Symbol; lf::Integer=1)
@assert has(progn, var_name) "PrognosticVariables has no variable $var_name"
return [getfield(layer.timesteps[lf], var_name) for layer in progn.layers]
end
get_vorticity(progn::PrognosticVariables; kwargs...) = get_var(progn, :vor; kwargs...)
get_divergence(progn::PrognosticVariables; kwargs...) = get_var(progn, :div; kwargs...)
get_temperature(progn::PrognosticVariables; kwargs...) = get_var(progn, :temp; kwargs...)
get_humidity(progn::PrognosticVariables; kwargs...) = get_var(progn, :humid; kwargs...)
get_pressure(progn::PrognosticVariables; lf::Integer=1) = progn.surface.timesteps[lf].pres
function Base.show(io::IO, P::PrognosticVariables)
ζ = P.layers[end].timesteps[1].vor # create a view on surface relative vorticity
ζ_grid = gridded(ζ) # to grid space
print(io, plot(ζ_grid, title="Surface relative vorticity"))
end