This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
atmos_gcm_default.jl
364 lines (320 loc) · 10.7 KB
/
atmos_gcm_default.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
# # Dry Atmosphere GCM diagnostics
#
# This file computes selected diagnostics for the GCM and outputs them
# on the spherical interpolated diagnostic grid.
#
# Use it by calling `Diagnostics.setup_atmos_default_diagnostics()`.
#
# TODO:
# - enable zonal means and calculation of covariances using those means
# - ds.T_zm = mean(.*1., ds.T; dims = 3)
# - ds.u_zm = mean((ds.u); dims = 3 )
# - v_zm = mean(ds.v; dims = 3)
# - w_zm = mean(ds.w; dims = 3)
# - ds.uvcovariance = (ds.u .- ds.u_zm) * (ds.v .- v_zm)
# - ds.vTcovariance = (ds.v .- v_zm) * (ds.T .- ds.T_zm)
# - add more variables, including horiz streamfunction from laplacial of vorticity (LN)
# - density weighting
# - maybe change thermo/dyn separation to local/nonlocal vars?
import CUDA
using LinearAlgebra
using Printf
using Statistics
using ..Atmos
using ..Atmos: recover_thermo_state
using ..TurbulenceClosures: turbulence_tensors
"""
setup_atmos_default_diagnostics(
::AtmosGCMConfigType,
interval::String,
out_prefix::String;
writer::AbstractWriter,
interpol = nothing,
)
Create and return a `DiagnosticsGroup` containing the "AtmosDefault"
diagnostics for GCM configurations. All the diagnostics in the group will run
at the specified `interval`, be interpolated to the specified boundaries and
resolution, and will be written to files prefixed by `out_prefix` using
`writer`.
"""
function setup_atmos_default_diagnostics(
::AtmosGCMConfigType,
interval::String,
out_prefix::String;
writer = NetCDFWriter(),
interpol = nothing,
)
# TODO: remove this
@assert !isnothing(interpol)
return DiagnosticsGroup(
"AtmosGCMDefault",
Diagnostics.atmos_gcm_default_init,
Diagnostics.atmos_gcm_default_fini,
Diagnostics.atmos_gcm_default_collect,
interval,
out_prefix,
writer,
interpol,
)
end
include("diagnostic_fields.jl")
# 3D variables
function vars_atmos_gcm_default_simple_3d(atmos::AtmosModel, FT)
@vars begin
u::FT
v::FT
w::FT
rho::FT
temp::FT
pres::FT
thd::FT # θ_dry
et::FT # e_tot
ei::FT # e_int
ht::FT
hi::FT
vort::FT # Ω₃
moisture::vars_atmos_gcm_default_simple_3d(atmos.moisture, FT)
end
end
vars_atmos_gcm_default_simple_3d(::MoistureModel, FT) = @vars()
function vars_atmos_gcm_default_simple_3d(m::EquilMoist, FT)
@vars begin
qt::FT # q_tot
ql::FT # q_liq
qv::FT # q_vap
qi::FT # q_ice
thv::FT # θ_vir
thl::FT # θ_liq
end
end
num_atmos_gcm_default_simple_3d_vars(m, FT) =
varsize(vars_atmos_gcm_default_simple_3d(m, FT))
atmos_gcm_default_simple_3d_vars(m, array) =
Vars{vars_atmos_gcm_default_simple_3d(m, eltype(array))}(array)
function atmos_gcm_default_simple_3d_vars!(
atmos::AtmosModel,
state_prognostic,
thermo,
dyni,
vars,
)
vars.u = state_prognostic.ρu[1] / state_prognostic.ρ
vars.v = state_prognostic.ρu[2] / state_prognostic.ρ
vars.w = state_prognostic.ρu[3] / state_prognostic.ρ
vars.rho = state_prognostic.ρ
vars.temp = thermo.temp
vars.pres = thermo.pres
vars.thd = thermo.θ_dry
vars.et = state_prognostic.ρe / state_prognostic.ρ
vars.ei = thermo.e_int
vars.ht = thermo.h_tot
vars.hi = thermo.h_int
vars.vort = dyni.Ω₃
atmos_gcm_default_simple_3d_vars!(
atmos.moisture,
state_prognostic,
thermo,
vars,
)
return nothing
end
function atmos_gcm_default_simple_3d_vars!(
::MoistureModel,
state_prognostic,
thermo,
vars,
)
return nothing
end
function atmos_gcm_default_simple_3d_vars!(
moist::EquilMoist,
state_prognostic,
thermo,
vars,
)
vars.moisture.qt = state_prognostic.moisture.ρq_tot / state_prognostic.ρ
vars.moisture.ql = thermo.moisture.q_liq
vars.moisture.qv = thermo.moisture.q_vap
vars.moisture.qi = thermo.moisture.q_ice
vars.moisture.thv = thermo.moisture.θ_vir
vars.moisture.thl = thermo.moisture.θ_liq_ice
return nothing
end
# Dynamic variables
function vars_dyn(FT)
@vars begin
Ω₁::FT
Ω₂::FT
Ω₃::FT
end
end
dyn_vars(array) = Vars{vars_dyn(eltype(array))}(array)
"""
atmos_gcm_default_init(dgngrp, currtime)
Initialize the GCM default diagnostics group, establishing the output file's
dimensions and variables.
"""
function atmos_gcm_default_init(dgngrp::DiagnosticsGroup, currtime)
atmos = Settings.dg.balance_law
FT = eltype(Settings.Q)
mpicomm = Settings.mpicomm
mpirank = MPI.Comm_rank(mpicomm)
if !(dgngrp.interpol isa InterpolationCubedSphere)
@warn """
Diagnostics ($dgngrp.name): currently requires `InterpolationCubedSphere`!
"""
return nothing
end
if mpirank == 0
# get dimensions for the interpolated grid
dims = dimensions(dgngrp.interpol)
# adjust the level dimension for `planet_radius`
level_val = dims["level"]
dims["level"] = (
level_val[1] .- FT(planet_radius(Settings.param_set)),
level_val[2],
)
# set up the variables we're going to be writing
vars = OrderedDict()
varnames = map(
s -> startswith(s, "moisture.") ? s[10:end] : s,
flattenednames(vars_atmos_gcm_default_simple_3d(atmos, FT)),
)
for varname in varnames
var = Variables[varname]
vars[varname] = (tuple(collect(keys(dims))...), FT, var.attrib)
end
# create the output file
dprefix = @sprintf(
"%s_%s_%s",
dgngrp.out_prefix,
dgngrp.name,
Settings.starttime,
)
dfilename = joinpath(Settings.output_dir, dprefix)
init_data(dgngrp.writer, dfilename, dims, vars)
end
return nothing
end
"""
atmos_gcm_default_collect(bl, currtime)
Master function that performs a global grid traversal to compute various
diagnostics using the above functions.
"""
function atmos_gcm_default_collect(dgngrp::DiagnosticsGroup, currtime)
interpol = dgngrp.interpol
if !(interpol isa InterpolationCubedSphere)
@warn """
Diagnostics ($dgngrp.name): currently requires `InterpolationCubedSphere`!
"""
return nothing
end
dg = Settings.dg
atmos = dg.balance_law
Q = Settings.Q
mpicomm = Settings.mpicomm
mpirank = MPI.Comm_rank(mpicomm)
grid = dg.grid
topology = grid.topology
N = polynomialorder(grid)
Nq = N + 1
Nqk = dimensionality(grid) == 2 ? 1 : Nq
npoints = Nq * Nq * Nqk
nrealelem = length(topology.realelems)
nvertelem = topology.stacksize
nhorzelem = div(nrealelem, nvertelem)
# get needed arrays onto the CPU
device = array_device(Q)
if device isa CPU
ArrayType = Array
state_data = Q.realdata
aux_data = dg.state_auxiliary.realdata
else
ArrayType = CUDA.CuArray
state_data = Array(Q.realdata)
aux_data = Array(dg.state_auxiliary.realdata)
end
FT = eltype(state_data)
# TODO: can this be done in one pass?
#
# Non-local vars, e.g. relative vorticity
vgrad = VectorGradients(dg, Q)
vort = Vorticity(dg, vgrad)
# Compute thermo variables
thermo_array = Array{FT}(undef, npoints, num_thermo(atmos, FT), nrealelem)
@visitQ nhorzelem nvertelem Nqk Nq begin
state = extract_state(dg, state_data, ijk, e, Prognostic())
aux = extract_state(dg, aux_data, ijk, e, Auxiliary())
thermo = thermo_vars(atmos, view(thermo_array, ijk, :, e))
compute_thermo!(atmos, state, aux, thermo)
end
# Interpolate the state, thermo and dyn vars to sphere (u and vorticity
# need projection to zonal, merid). All this may happen on the GPU.
istate =
ArrayType{FT}(undef, interpol.Npl, number_states(atmos, Prognostic()))
interpolate_local!(interpol, Q.realdata, istate)
ithermo = ArrayType{FT}(undef, interpol.Npl, num_thermo(atmos, FT))
interpolate_local!(interpol, ArrayType(thermo_array), ithermo)
idyn = ArrayType{FT}(undef, interpol.Npl, size(vort.data, 2))
interpolate_local!(interpol, vort.data, idyn)
# TODO: get indices here without hard-coding them
_ρu, _ρv, _ρw = 2, 3, 4
project_cubed_sphere!(interpol, istate, (_ρu, _ρv, _ρw))
_Ω₁, _Ω₂, _Ω₃ = 1, 2, 3
project_cubed_sphere!(interpol, idyn, (_Ω₁, _Ω₂, _Ω₃))
# FIXME: accumulating to rank 0 is not scalable
all_state_data = accumulate_interpolated_data(mpicomm, interpol, istate)
all_thermo_data = accumulate_interpolated_data(mpicomm, interpol, ithermo)
all_dyn_data = accumulate_interpolated_data(mpicomm, interpol, idyn)
if mpirank == 0
# get dimensions for the interpolated grid
dims = dimensions(dgngrp.interpol)
# set up the array for the diagnostic variables based on the interpolated grid
nlong = length(dims["long"][1])
nlat = length(dims["lat"][1])
nlevel = length(dims["level"][1])
simple_3d_vars_array = Array{FT}(
undef,
nlong,
nlat,
nlevel,
num_atmos_gcm_default_simple_3d_vars(atmos, FT),
)
@visitI nlong nlat nlevel begin
statei = Vars{vars_state(atmos, Prognostic(), FT)}(view(
all_state_data,
lo,
la,
le,
:,
))
thermoi = thermo_vars(atmos, view(all_thermo_data, lo, la, le, :))
dyni = dyn_vars(view(all_dyn_data, lo, la, le, :))
simple_3d_vars = atmos_gcm_default_simple_3d_vars(
atmos,
view(simple_3d_vars_array, lo, la, le, :),
)
atmos_gcm_default_simple_3d_vars!(
atmos,
statei,
thermoi,
dyni,
simple_3d_vars,
)
end
# assemble the diagnostics for writing
varvals = OrderedDict()
varnames = map(
s -> startswith(s, "moisture.") ? s[10:end] : s,
flattenednames(vars_atmos_gcm_default_simple_3d(atmos, FT)),
)
for (vari, varname) in enumerate(varnames)
varvals[varname] = simple_3d_vars_array[:, :, :, vari]
end
# write output
append_data(dgngrp.writer, varvals, currtime)
end
MPI.Barrier(mpicomm)
return nothing
end # function collect
function atmos_gcm_default_fini(dgngrp::DiagnosticsGroup, currtime) end