Skip to content

Commit 14900d3

Browse files
Define and use all_specific_gs
1 parent 9ccf960 commit 14900d3

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/prognostic_equations/hyperdiffusion.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,10 @@ NVTX.@annotate function prep_tracer_hyperdiffusion_tendency!(Yₜ, Y, p, t)
242242

243243
(; ᶜ∇²specific_tracers) = p.hyperdiff
244244

245-
for ρχ_name in filter(is_tracer_var, propertynames(Y.c))
246-
χ_name = specific_name(ρχ_name)
247-
ᶜρχ = getproperty(Y.c, ρχ_name)
248-
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
249-
@. ᶜ∇²specific_tracers.:($$χ_name) = wdivₕ(gradₕ(ᶜχ))
245+
ᶜspecific = all_specific_gs(Y.c)
246+
247+
for χ_name in propertynames(ᶜ∇²specific_tracers)
248+
@. ᶜ∇²specific_tracers.:($$χ_name) = wdivₕ(gradₕ(ᶜspecific.:($$χ_name)))
250249
end
251250

252251
if turbconv_model isa PrognosticEDMFX

src/utils/variable_manipulations.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ omitted from the result.
182182
return :(NamedTuple{$specific_gs_names}(($(specific_gs_values...),)))
183183
end
184184

185+
"""
186+
all_specific_gs(gs)
187+
188+
Lazily computes all specific quantities (`χ`) from a grid-scale state `gs`.
189+
This `@generated` function introspects the field names of `gs` at compile time.
190+
It identifies all density-weighted fields (e.g., `:ρq_tot`, `:ρe_tot`), divides
191+
them by the grid-scale density `gs.ρ`, and returns them in a new `NamedTuple`.
192+
This provides a type-stable and performant way to convert all relevant state
193+
variables to their specific counterparts at once.
194+
195+
Arguments:
196+
- `gs`: The grid-scale state, which must contain a `:ρ` field and other fields
197+
with a `:ρ` prefix.
198+
199+
Returns:
200+
- A new `NamedTuple` containing only the specific quantities (e.g., `:q_tot`, `:e_tot`).
201+
"""
202+
@generated function all_specific_gs(gs)
203+
gs_names = Base._nt_names(gs)
204+
relevant_gs_names =
205+
filter(name -> has_prefix(name, ) && name != , gs_names)
206+
all_specific_gs_names = map(name -> remove_prefix(name, ), relevant_gs_names)
207+
all_specific_gs_values = map(name -> :(lazy.(specific.(gs.$name, gs.ρ))), relevant_gs_names)
208+
return :(NamedTuple{$all_specific_gs_names}(($(all_specific_gs_values...),)))
209+
end
210+
185211
"""
186212
specific_sgs(sgs, gs, turbconv_model)
187213

0 commit comments

Comments
 (0)