Skip to content

Commit e0011ed

Browse files
authored
Merge pull request #2928 from CliMA/gb/kinetic_energy
Add spectrum of total kinetic energy in baroclinc wave
2 parents 5d1d9fe + 454522a commit e0011ed

File tree

5 files changed

+103
-19
lines changed

5 files changed

+103
-19
lines changed

config/longrun_configs/longrun_bw_rhoe_equil_highres.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ precip_model: "0M"
99
job_id: "longrun_bw_rhoe_equil_highres"
1010
moist: "equil"
1111
diagnostics:
12-
- short_name: [pfull, wa, va, rv, hus]
12+
- short_name: [pfull, wa, va, rv, hus, ke]
1313
period: 1days

config/longrun_configs/longrun_bw_rhoe_highres.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ z_elem: 45
77
dt: "400secs"
88
job_id: "longrun_bw_rhoe_highres"
99
diagnostics:
10-
- short_name: [pfull, wa, va, rv]
10+
- short_name: [pfull, wa, va, rv, ke]
1111
period: 1days

config/model_configs/sphere_baroclinic_wave_rhoe.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dt: "400secs"
44
t_end: "10days"
55
job_id: "sphere_baroclinic_wave_rhoe"
66
diagnostics:
7-
- short_name: [pfull, wa, va, rv]
7+
- short_name: [pfull, wa, va, rv, ke]
88
period: 1days
99
- short_name: [pfull, wa, va, rv]
1010
period: 1days

post_processing/ci_plots.jl

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,57 @@ function make_plots_generic(
280280
end
281281

282282
"""
283-
make_spectra_generic
283+
compute_spectrum(var)
284284
285-
Use ClimaCoreSpectra to compute and plot spectra for the given `vars`.
285+
Compute the spectrum associated to the given variable. Returns a ClimaAnalysis.OutputVar.
286+
"""
287+
function compute_spectrum(var; mass_weight = nothing)
288+
# power_spectrum_2d seems to work only when the two dimensions have precisely one
289+
# twice as many points as the other
290+
dim1, dim2, dim3 = var.index2dim[1:3]
286291

287-
Extra arguments are passed to `ClimaAnalysis.slice`
292+
len1 = length(var.dims[dim1])
293+
len2 = length(var.dims[dim2])
294+
295+
len1 == 2len2 || error("Cannot take this spectrum ($len1 != 2 $len2)")
296+
297+
(dim1 == "lon" || dim1 == "long") ||
298+
error("First dimension has to be longitude (found $dim1)")
299+
dim2 == "lat" || error("Second dimension has to be latitude (found $dim2)")
300+
dim3 == "z" || error("Third dimension has to be altitude (found $dim3)")
301+
302+
FT = eltype(var.data)
303+
mass_weight =
304+
isnothing(mass_weight) ? ones(FT, length(var.dims[dim3])) : mass_weight
305+
spectrum_data, wave_numbers, _spherical, mesh_info =
306+
power_spectrum_2d(FT, var.data, mass_weight)
307+
308+
power_spectrum =
309+
dropdims(sum(spectrum_data, dims = 1), dims = 1)[begin:(end - 1), :]
310+
311+
w_numbers = collect(0:1:(mesh_info.num_spherical - 1))
312+
313+
dims = Dict("Spherical Wavenumber" => w_numbers, dim3 => var.dims[dim3])
314+
315+
dim_attributes = Dict(
316+
"Spherical Wavenumber" => Dict("units" => ""),
317+
dim3 => var.dim_attributes[dim3],
318+
)
319+
320+
attributes = Dict(
321+
"short_name" => "log_spectrum_" * var.attributes["short_name"],
322+
"long_name" => "Spectrum of " * var.attributes["long_name"],
323+
"units" => "",
324+
)
325+
326+
return ClimaAnalysis.OutputVar(
327+
attributes,
328+
dims,
329+
dim_attributes,
330+
log.(power_spectrum),
331+
)
332+
end
288333

289-
"""
290334
function make_spectra_generic(
291335
output_path,
292336
vars,
@@ -307,38 +351,35 @@ function make_spectra_generic(
307351
dim1, dim2 = var.index2dim[1:2]
308352

309353
length(var.dims[dim1]) == 2 * length(var.dims[dim2]) ||
310-
error("Cannot take a this spectrum")
354+
error("Cannot take this spectrum")
311355

312356
FT = eltype(var.data)
313357
mass_weight = ones(FT, 1)
314358
spectrum_data, wave_numbers, spherical, mesh_info =
315359
power_spectrum_2d(FT, var.data, mass_weight)
316360

317361
# From ClimaCoreSpectra/examples
318-
X = collect(0:1:(mesh_info.num_fourier))
319362
Y = collect(0:1:(mesh_info.num_spherical))
320363
Z = spectrum_data[:, :, 1]
321364

322-
dims = Dict("num_fourier" => X, "num_spherical" => Y)
323-
dim_attributes = Dict(
324-
"num_fourier" => Dict("units" => ""),
325-
"num_spherical" => Dict("units" => ""),
326-
)
365+
dims = Dict("Spherical Wavenumber" => Y)
366+
dim_attributes =
367+
Dict("Spherical Wavenumber" => Dict("units" => ""))
327368

328369
attributes = Dict(
329-
"short_name" => "log fft_" * var.attributes["short_name"],
370+
"short_name" =>
371+
"log_spectrum_" * var.attributes["short_name"],
330372
"long_name" => "Spectrum of " * var.attributes["long_name"],
331373
"units" => "",
332374
)
333-
path = nothing
334375

335376
return ClimaAnalysis.OutputVar(
336377
attributes,
337378
dims,
338379
dim_attributes,
339380
log.(Z),
340-
path,
341381
)
382+
342383
end |> collect
343384

344385
make_plots_generic(output_path, spectra, args...; output_name, kwargs...)
@@ -574,10 +615,19 @@ DryBaroWavePlots = Union{Val{:sphere_baroclinic_wave_rhoe}}
574615
function make_plots(::DryBaroWavePlots, output_paths::Vector{<:AbstractString})
575616
simdirs = SimDir.(output_paths)
576617
short_names, reduction = ["pfull", "va", "wa", "rv"], "inst"
618+
short_names_spectra = ["ke"]
577619
vars = map_comparison(simdirs, short_names) do simdir, short_name
578-
return get(simdir; short_name, reduction)
620+
return slice(get(simdir; short_name, reduction), time = LAST_SNAP)
579621
end
580-
make_plots_generic(output_paths, vars, z = 1500, time = LAST_SNAP)
622+
vars_spectra =
623+
map_comparison(simdirs, short_names_spectra) do simdir, short_name
624+
compute_spectrum(
625+
slice(get(simdir; short_name, reduction), time = LAST_SNAP),
626+
)
627+
end
628+
vars = vcat(vars..., vars_spectra...)
629+
630+
make_plots_generic(output_paths, vars, z = 1500)
581631
end
582632

583633
function make_plots(
@@ -601,6 +651,14 @@ function make_plots(
601651
vars = map_comparison(simdirs, short_names) do simdir, short_name
602652
return get(simdir; short_name, reduction)
603653
end
654+
vars_spectra =
655+
map_comparison(simdirs, short_names_spectra) do simdir, short_name
656+
compute_spectrum(
657+
slice(get(simdir; short_name, reduction), time = 10days),
658+
)
659+
end
660+
vars = vcat(vars..., vars_spectra...)
661+
604662
make_plots_generic(output_paths, vars, z = 1500, time = 10days)
605663
end
606664

@@ -649,6 +707,13 @@ function make_plots(
649707
vars = map_comparison(simdirs, short_names) do simdir, short_name
650708
return get(simdir; short_name, reduction)
651709
end
710+
vars_spectra =
711+
map_comparison(simdirs, short_names_spectra) do simdir, short_name
712+
compute_spectrum(
713+
slice(get(simdir; short_name, reduction), time = 10days),
714+
)
715+
end
716+
vars = vcat(vars..., vars_spectra...)
652717
make_plots_generic(output_paths, vars, z = 1500, time = 10days)
653718
end
654719

@@ -1126,6 +1191,7 @@ function make_plots(::EDMFSpherePlots, output_paths::Vector{<:AbstractString})
11261191

11271192
short_names =
11281193
["ua", "wa", "waup", "thetaa", "ta", "taup", "haup", "tke", "arup"]
1194+
11291195
reduction = "average"
11301196
period = "1h"
11311197
latitudes = [0.0, 30.0, 60.0, 90.0]

src/diagnostics/core_diagnostics.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ add_diagnostic_variable!(
226226
end,
227227
)
228228

229+
###
230+
# Total kinetic energy
231+
###
232+
add_diagnostic_variable!(
233+
short_name = "ke",
234+
long_name = "Total Kinetic Energy",
235+
standard_name = "total_kinetic_energy",
236+
units = "m^2 s^-2",
237+
comments = "The kinetic energy on cell centers",
238+
compute! = (out, state, cache, time) -> begin
239+
if isnothing(out)
240+
return copy(cache.precomputed.ᶜK)
241+
else
242+
out .= cache.precomputed.ᶜK
243+
end
244+
end,
245+
)
246+
229247
###
230248
# Mixing length (3d)
231249
###

0 commit comments

Comments
 (0)