Skip to content

Commit 492e90b

Browse files
Use more foreach_scalar
1 parent 755d643 commit 492e90b

File tree

5 files changed

+49
-67
lines changed

5 files changed

+49
-67
lines changed

src/parameterized_tendencies/les_sgs_models/smagorinsky_lilly.jl

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,19 @@ function vertical_smagorinsky_lilly_tendency!(Yₜ, Y, p, t, ::SmagorinskyLilly)
156156
@. Yₜ.c.ρe_tot -= ᶜdivᵥ_ρe_tot(-(ᶠρ * ᶠD_smag * ᶠgradᵥ(ᶜh_tot)))
157157

158158
## Tracer diffusion and associated mass changes
159-
for ρχ_name in propertynames(Yₜ.c)
160-
if is_tracer_var(ρχ_name) && ρχ_name != :ρe_tot
161-
ᶜρχ = getproperty(Y.c, ρχ_name)
162-
ᶜρχₜ = getproperty(Yₜ.c, ρχ_name)
163-
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
164-
165-
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(;
166-
top = Operators.SetValue(C3(FT(0))),
167-
bottom = Operators.SetValue(C3(FT(0))),
168-
)
169-
170-
ᶜ∇ᵥρD∇χₜ = @. lazy(ᶜdivᵥ_ρχ(-(ᶠρ * ᶠD_smag * ᶠgradᵥ(ᶜχ))))
171-
@. ᶜρχₜ -= ᶜ∇ᵥρD∇χₜ
172-
# Rain and snow does not affect the mass
173-
if ρχ_name == :ρq_tot
174-
@. Yₜ.c.ρ -= ᶜ∇ᵥρD∇χₜ
175-
end
159+
foreach_scalar(Yₜ, Y) do ᶜρχₜ, ᶜρχ, is_ρq_tot
160+
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
161+
162+
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(;
163+
top = Operators.SetValue(C3(FT(0))),
164+
bottom = Operators.SetValue(C3(FT(0))),
165+
)
166+
167+
ᶜ∇ᵥρD∇χₜ = @. lazy(ᶜdivᵥ_ρχ(-(ᶠρ * ᶠD_smag * ᶠgradᵥ(ᶜχ))))
168+
@. ᶜρχₜ -= ᶜ∇ᵥρD∇χₜ
169+
# Rain and snow does not affect the mass
170+
if is_ρq_tot
171+
@. Yₜ.c.ρ -= ᶜ∇ᵥρD∇χₜ
176172
end
177173
end
178174
end

src/prognostic_equations/advection.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,8 @@ NVTX.@annotate function explicit_vertical_advection_tendency!(Yₜ, Y, p, t)
213213
ᶜρ = Y.c.ρ
214214

215215
# Full vertical advection of passive tracers (like liq, rai, etc) ...
216-
for ρχ_name in propertynames(Yₜ.c)
217-
if is_tracer_var(ρχ_name) && !(ρχ_name in (:ρe_tot, :ρq_tot))
218-
ᶜρχ = getproperty(Y.c, ρχ_name)
219-
ᶜρχₜ = getproperty(Yₜ.c, ρχ_name)
216+
foreach_scalar(Yₜ, Y) do ᶜρχₜ, ᶜρχ, is_ρq_tot
217+
if !is_ρq_tot
220218
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
221219
vtt = vertical_transport(ᶜρ, ᶠu³, ᶜχ, float(dt), tracer_upwinding)
222220
@. ᶜρχₜ += vtt

src/prognostic_equations/remaining_tendency.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,11 @@ NVTX.@annotate function additional_tendency!(Yₜ, Y, p, t)
166166
@. Yₜ.c.ρe_tot += vst_ρe_tot
167167

168168
# TODO: can we write this out explicitly?
169-
for ρχ_name in propertynames(Yₜ.c)
170-
if is_tracer_var(ρχ_name) && ρχ_name != :ρe_tot
171-
ᶜρχ = getproperty(Y.c, ρχ_name)
172-
ᶜρχₜ = getproperty(Yₜ.c, ρχ_name)
173-
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
174-
vst_tracer = viscous_sponge_tendency_tracer(ᶜρ, ᶜχ, viscous_sponge)
175-
@. ᶜρχₜ += vst_tracer
176-
@. Yₜ.c.ρ += vst_tracer # TODO: This doesn't look right for all tracers here. Remove?
177-
end
169+
foreach_scalar(Yₜ, Y) do ᶜρχₜ, ᶜρχ, is_ρq_tot
170+
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
171+
vst_tracer = viscous_sponge_tendency_tracer(ᶜρ, ᶜχ, viscous_sponge)
172+
@. ᶜρχₜ += vst_tracer
173+
@. Yₜ.c.ρ += vst_tracer # TODO: This doesn't look right for all tracers here. Remove?
178174
end
179175

180176
# Held Suarez tendencies

src/prognostic_equations/surface_flux.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,17 @@ function surface_flux_tendency!(Yₜ, Y, p, t)
115115
btt = boundary_tendency_scalar(ᶜh_tot, sfc_conditions.ρ_flux_h_tot)
116116
@. Yₜ.c.ρe_tot -= btt
117117
ρ_flux_χ = p.scratch.sfc_temp_C3
118-
for ρχ_name in propertynames(Yₜ.c)
119-
if is_tracer_var(ρχ_name) && ρχ_name != :ρe_tot
120-
ᶜρχ = getproperty(Y.c, ρχ_name)
121-
ᶜρχₜ = getproperty(Yₜ.c, ρχ_name)
122-
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
123-
if ρχ_name == :ρq_tot
124-
@. ρ_flux_χ = sfc_conditions.ρ_flux_q_tot
125-
else
126-
@. ρ_flux_χ = C3(FT(0))
127-
end
128-
btt = boundary_tendency_scalar(ᶜχ, ρ_flux_χ)
129-
@. ᶜρχₜ -= btt
130-
if ρχ_name == :ρq_tot
131-
@. Yₜ.c.ρ -= btt
132-
end
118+
foreach_scalar(Yₜ, Y) do ᶜρχₜ, ᶜρχ, is_ρq_tot
119+
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
120+
if is_ρq_tot
121+
@. ρ_flux_χ = sfc_conditions.ρ_flux_q_tot
122+
else
123+
@. ρ_flux_χ = C3(FT(0))
124+
end
125+
btt = boundary_tendency_scalar(ᶜχ, ρ_flux_χ)
126+
@. ᶜρχₜ -= btt
127+
if is_ρq_tot
128+
@. Yₜ.c.ρ -= btt
133129
end
134130
end
135131
end

src/prognostic_equations/vertical_diffusion_boundary_layer.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,24 @@ function vertical_diffusion_boundary_layer_tendency!(
9696

9797
ᶜρχₜ_diffusion = p.scratch.ᶜtemp_scalar
9898
ᶜK_h_scaled = p.scratch.ᶜtemp_scalar_2
99-
for ρχ_name in propertynames(Yₜ.c)
100-
if is_tracer_var(ρχ_name) && ρχ_name != :ρe_tot
101-
ᶜρχ = getproperty(Y.c, ρχ_name)
102-
ᶜρχₜ = getproperty(Yₜ.c, ρχ_name)
103-
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
104-
if ρχ_name in (:ρq_rai, :ρq_sno, :ρn_rai)
105-
@. ᶜK_h_scaled = α_vert_diff_tracer * ᶜK_h
106-
else
107-
@. ᶜK_h_scaled = ᶜK_h
108-
end
109-
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(
110-
top = Operators.SetValue(C3(0)),
111-
bottom = Operators.SetValue(C3(0)),
112-
)
113-
@. ᶜρχₜ_diffusion =
114-
ᶜdivᵥ_ρχ(-(ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h_scaled) * ᶠgradᵥ(ᶜχ)))
115-
@. ᶜρχₜ -= ᶜρχₜ_diffusion
116-
# Only add contribution from total water diffusion to mass tendency
117-
# (exclude contributions from diffusion of condensate, precipitation)
118-
if ρχ_name == :ρq_tot
119-
@. Yₜ.c.ρ -= ᶜρχₜ_diffusion
120-
end
99+
foreach_scalar(Yₜ, Y) do ᶜρχₜ, ᶜρχ, is_ρq_tot
100+
ᶜχ = @. lazy(specific(ᶜρχ, Y.c.ρ))
101+
if ρχ_name in (:ρq_rai, :ρq_sno, :ρn_rai)
102+
@. ᶜK_h_scaled = α_vert_diff_tracer * ᶜK_h
103+
else
104+
@. ᶜK_h_scaled = ᶜK_h
105+
end
106+
ᶜdivᵥ_ρχ = Operators.DivergenceF2C(
107+
top = Operators.SetValue(C3(0)),
108+
bottom = Operators.SetValue(C3(0)),
109+
)
110+
@. ᶜρχₜ_diffusion =
111+
ᶜdivᵥ_ρχ(-(ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_h_scaled) * ᶠgradᵥ(ᶜχ)))
112+
@. ᶜρχₜ -= ᶜρχₜ_diffusion
113+
# Only add contribution from total water diffusion to mass tendency
114+
# (exclude contributions from diffusion of condensate, precipitation)
115+
if is_ρq_tot
116+
@. Yₜ.c.ρ -= ᶜρχₜ_diffusion
121117
end
122118
end
123119
end

0 commit comments

Comments
 (0)