Skip to content

Commit b761ab8

Browse files
Fixes + tests
1 parent 4cbd846 commit b761ab8

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/utils/variable_manipulations.jl

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ Arguments:
4242
"""
4343
@inline specific(ρχ, ρ) = ρχ / ρ
4444

45-
#! format: off
46-
@inline specific_name(ρχ_name::Symbol) =
47-
if ρχ_name == :ρe_tot; return :e_tot
48-
elseif ρχ_name == :ρq_tot; return :q_tot
49-
elseif ρχ_name == :ρq_liq; return :q_liq
50-
elseif ρχ_name == :ρq_ice; return :q_ice
51-
elseif ρχ_name == :ρq_rai; return :q_rai
52-
elseif ρχ_name == :ρn_liq; return :n_liq
53-
elseif ρχ_name == :ρn_rai; return :q_rai
54-
elseif ρχ_name == :ρq_sno; return :q_sno
55-
else; error("Uncaught name: $ρχ_name")
56-
end
57-
#! format: on
58-
5945
@inline function specific(ρaχ, ρa, ρχ, ρ, turbconv_model)
6046
# TODO: Replace turbconv_model struct by parameters, and include a_half in
6147
# parameters, not in config
@@ -173,24 +159,28 @@ Converts every variable of the form `ρχ` in the grid-scale state `gs` into the
173159
specific variable `χ` by dividing it by `ρ`. All other variables in `gs` are
174160
omitted from the result.
175161
"""
176-
@generated specific_gs(gs) =
177-
:(NamedTuple{$(specific_gs_names(gs))}(($(map(name -> :(gs.$name / gs.ρ), relevant_gs_names(gs))...),)))
162+
specific_gs(gs) =
163+
NamedTuple{specific_gs_names(gs)}(map(name -> gs.:($name) / gs.ρ, relevant_gs_names(gs)))
178164

179165
"""
180166
relevant_gs_names(gs)
181167
182168
Returns relevant grid-scale state `gs` names for determining specific variables.
183169
"""
184-
@generated relevant_gs_names(gs) =
185-
filter(name -> has_prefix(name, ) && name != , Base._nt_names(gs))
170+
@generated relevant_gs_names(::Type{GS}) where {GS} =
171+
filter(name -> has_prefix(name, ) && name != , Base._nt_names(GS))
172+
173+
@inline relevant_gs_names(gs) = relevant_gs_names(typeof(gs))
186174

187175
"""
188176
specific_gs_names(gs)
189177
190178
Returns relevant specific grid-scale state `gs` names.
191179
"""
192-
@generated specific_gs_names(gs) =
193-
map(name -> remove_prefix(name, ), relevant_gs_names(gs))
180+
@generated specific_gs_names(::Type{GS}) where {GS} =
181+
map(name -> remove_prefix(name, ), relevant_gs_names(GS))
182+
183+
@inline specific_gs_names(gs) = specific_gs_names(typeof(gs))
194184

195185
"""
196186
all_specific_gs(gs)
@@ -209,9 +199,9 @@ Arguments:
209199
Returns:
210200
- A new `NamedTuple` containing only the specific quantities (e.g., `:q_tot`, `:e_tot`).
211201
"""
212-
all_specific_gs(gs::Fields.Field) =
202+
all_specific_gs(gs) =
213203
NamedTuple{specific_gs_names(eltype(gs))}(
214-
UU.unrolled_map(relevant_gs_names(gs)) do name
204+
UU.unrolled_map(relevant_gs_names(eltype(gs))) do name
215205
lazy.(specific.(getproperty(gs, name), gs.ρ))
216206
end
217207
)

test/utilities.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Dates
55
using Random
66
Random.seed!(1234)
77
import ClimaAtmos as CA
8+
import LazyBroadcast: lazy
89
using NCDatasets
910

1011
include("test_helpers.jl")
@@ -33,6 +34,21 @@ end
3334
@test CA.isdivisible(Dates.Month(1), Dates.Hour(1))
3435
end
3536

37+
@testset "Variable manipulations" begin
38+
(; helem, cent_space, face_space) = get_cartesian_spaces()
39+
ccoords, fcoords = get_coords(cent_space, face_space)
40+
FT = eltype(ccoords.x)
41+
nt = (;ρ=FT(2),ρa=FT(4), ρe_tot=FT(4))
42+
@test CA.relevant_gs_names(typeof(nt)) == (:ρa, :ρe_tot)
43+
@test CA.specific_gs_names(nt) == (:a, :e_tot)
44+
@test CA.specific_gs(nt) == (a = 2.0, e_tot = 2.0)
45+
f = Fields.Field(typeof(nt), cent_space)
46+
@test CA.all_specific_gs(f) == (;
47+
a = lazy.(CA.specific.(f.ρa, f.ρ)),
48+
e_tot = lazy.(CA.specific.(f.ρe_tot, f.ρ)),
49+
)
50+
end
51+
3652
@testset "kinetic_energy (c.f. analytical function)" begin
3753
# Test kinetic energy function for staggered grids
3854
# given an analytical expression for the velocity profiles

0 commit comments

Comments
 (0)