Skip to content

Commit 4607ec2

Browse files
Merge pull request #3845 from CliMA/ck/slim_cache
Reduce dependency on specific cache
2 parents 1f3398d + 6cb73be commit 4607ec2

File tree

10 files changed

+194
-82
lines changed

10 files changed

+194
-82
lines changed

src/cache/cloud_fraction.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ NVTX.@annotate function set_cloud_fraction!(
6464
else
6565
@. cloud_diagnostics_tuple = make_named_tuple(
6666
ifelse(
67-
p.precomputed.ᶜspecific.q_liq + p.precomputed.ᶜspecific.q_ice > 0,
67+
specific(Y.c.ρq_liq, Y.c.ρ) + specific(Y.c.ρq_ice, Y.c.ρ) > 0,
6868
1,
6969
0,
7070
),
71-
p.precomputed.ᶜspecific.q_liq,
72-
p.precomputed.ᶜspecific.q_ice,
71+
specific(Y.c.ρq_liq, Y.c.ρ),
72+
specific(Y.c.ρq_ice, Y.c.ρ),
7373
)
7474
end
7575
end
76+
7677
NVTX.@annotate function set_cloud_fraction!(
7778
Y,
7879
p,

src/cache/precipitation_precomputed_quantities.jl

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ function set_precipitation_velocities!(
105105
precip_model::Microphysics2Moment,
106106
)
107107
(; ᶜwₗ, ᶜwᵢ, ᶜwᵣ, ᶜwₛ, ᶜwnₗ, ᶜwnᵣ, ᶜwₜqₜ, ᶜwₕhₜ, ᶜts, ᶜu) = p.precomputed
108-
(; q_liq, q_ice, q_rai, q_sno) = p.precomputed.ᶜspecific
109108
(; ᶜΦ) = p.core
110109

111110
cm1c = CAP.microphysics_cloud_params(p.params)
@@ -116,23 +115,53 @@ function set_precipitation_velocities!(
116115
# compute the precipitation terminal velocity [m/s]
117116
# TODO sedimentation of snow is based on the 1M scheme
118117
@. ᶜwnᵣ = getindex(
119-
CM2.rain_terminal_velocity(cm2p.sb, cm2p.tv, q_rai, Y.c.ρ, Y.c.ρn_rai),
118+
CM2.rain_terminal_velocity(
119+
cm2p.sb,
120+
cm2p.tv,
121+
specific(Y.c.ρq_rai, Y.c.ρ),
122+
Y.c.ρ,
123+
Y.c.ρn_rai,
124+
),
120125
1,
121126
)
122127
@. ᶜwᵣ = getindex(
123-
CM2.rain_terminal_velocity(cm2p.sb, cm2p.tv, q_rai, Y.c.ρ, Y.c.ρn_rai),
128+
CM2.rain_terminal_velocity(
129+
cm2p.sb,
130+
cm2p.tv,
131+
specific(Y.c.ρq_rai, Y.c.ρ),
132+
Y.c.ρ,
133+
Y.c.ρn_rai,
134+
),
124135
2,
125136
)
126-
@. ᶜwₛ = CM1.terminal_velocity(cm1p.ps, cm1p.tv.snow, Y.c.ρ, q_sno)
137+
@. ᶜwₛ = CM1.terminal_velocity(
138+
cm1p.ps,
139+
cm1p.tv.snow,
140+
Y.c.ρ,
141+
specific(Y.c.ρq_sno, Y.c.ρ),
142+
)
127143
# compute sedimentation velocity for cloud condensate [m/s]
128144
# TODO sedimentation velocities of cloud condensates are based
129145
# on the 1M scheme. Sedimentation velocity of cloud number concentration
130146
# is equal to that of the mass.
131-
@. ᶜwnₗ =
132-
CMNe.terminal_velocity(cm1c.liquid, cm1c.Ch2022.rain, Y.c.ρ, q_liq)
133-
@. ᶜwₗ = CMNe.terminal_velocity(cm1c.liquid, cm1c.Ch2022.rain, Y.c.ρ, q_liq)
134-
@. ᶜwᵢ =
135-
CMNe.terminal_velocity(cm1c.ice, cm1c.Ch2022.small_ice, Y.c.ρ, q_ice)
147+
@. ᶜwnₗ = CMNe.terminal_velocity(
148+
cm1c.liquid,
149+
cm1c.Ch2022.rain,
150+
Y.c.ρ,
151+
specific(Y.c.ρq_liq, Y.c.ρ),
152+
)
153+
@. ᶜwₗ = CMNe.terminal_velocity(
154+
cm1c.liquid,
155+
cm1c.Ch2022.rain,
156+
Y.c.ρ,
157+
specific(Y.c.ρq_liq, Y.c.ρ),
158+
)
159+
@. ᶜwᵢ = CMNe.terminal_velocity(
160+
cm1c.ice,
161+
cm1c.Ch2022.small_ice,
162+
Y.c.ρ,
163+
specific(Y.c.ρq_ice, Y.c.ρ),
164+
)
136165

137166
# compute their contributions to energy and total water advection
138167
@. ᶜwₜqₜ =

src/cache/precomputed_quantities.jl

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,36 +398,34 @@ function thermo_state(
398398
return get_ts(ρ, p, θ, e_int, q_tot, q_pt)
399399
end
400400

401-
function thermo_vars(moisture_model, precip_model, specific, K, Φ)
402-
energy_var = (; e_int = specific.e_tot - K - Φ)
401+
function thermo_vars(moisture_model, precip_model, ᶜY, K, Φ)
402+
energy_var = (; e_int = specific(ᶜY.ρe_tot, ᶜY.ρ) - K - Φ)
403403
moisture_var = if moisture_model isa DryModel
404404
(;)
405405
elseif moisture_model isa EquilMoistModel
406-
(; specific.q_tot)
406+
(; q_tot = specific(ᶜY.ρq_tot, ᶜY.ρ))
407407
elseif moisture_model isa NonEquilMoistModel
408-
q_pt_args = (
409-
specific.q_tot,
410-
specific.q_liq + specific.q_rai,
411-
specific.q_ice + specific.q_sno,
408+
q_pt_args = (;
409+
q_tot = specific(ᶜY.ρq_tot, ᶜY.ρ),
410+
q_liq = specific(ᶜY.ρq_liq, ᶜY.ρ) + specific(ᶜY.ρq_rai, ᶜY.ρ),
411+
q_ice = specific(ᶜY.ρq_ice, ᶜY.ρ) + specific(ᶜY.ρq_sno, ᶜY.ρ),
412412
)
413413
(; q_pt = TD.PhasePartition(q_pt_args...))
414414
end
415415
return (; energy_var..., moisture_var...)
416416
end
417417

418-
ts_gs(thermo_params, moisture_model, precip_model, specific, K, Φ, ρ) =
419-
thermo_state(
420-
thermo_params;
421-
thermo_vars(moisture_model, precip_model, specific, K, Φ)...,
422-
ρ,
423-
)
418+
ts_gs(thermo_params, moisture_model, precip_model, ᶜY, K, Φ, ρ) = thermo_state(
419+
thermo_params;
420+
thermo_vars(moisture_model, precip_model, ᶜY, K, Φ)...,
421+
ρ,
422+
)
424423

425-
ts_sgs(thermo_params, moisture_model, precip_model, specific, K, Φ, p) =
426-
thermo_state(
427-
thermo_params;
428-
thermo_vars(moisture_model, precip_model, specific, K, Φ)...,
429-
p,
430-
)
424+
ts_sgs(thermo_params, moisture_model, precip_model, ᶜY, K, Φ, p) = thermo_state(
425+
thermo_params;
426+
thermo_vars(moisture_model, precip_model, ᶜY, K, Φ)...,
427+
p,
428+
)
431429

432430
function eddy_diffusivity_coefficient_H(D₀, H, z_sfc, z)
433431
return D₀ * exp(-(z - z_sfc) / H)
@@ -489,9 +487,13 @@ NVTX.@annotate function set_implicit_precomputed_quantities!(Y, p, t)
489487
# @. ᶜK += Y.c.sgs⁰.ρatke / Y.c.ρ
490488
# TODO: We should think more about these increments before we use them.
491489
end
492-
@. ᶜts = ts_gs(thermo_args..., ᶜspecific, ᶜK, ᶜΦ, Y.c.ρ)
490+
@. ᶜts = ts_gs(thermo_args..., Y.c, ᶜK, ᶜΦ, Y.c.ρ)
493491
@. ᶜp = TD.air_pressure(thermo_params, ᶜts)
494-
@. ᶜh_tot = TD.total_specific_enthalpy(thermo_params, ᶜts, ᶜspecific.e_tot)
492+
@. ᶜh_tot = TD.total_specific_enthalpy(
493+
thermo_params,
494+
ᶜts,
495+
specific(Y.c.ρe_tot, Y.c.ρ),
496+
)
495497

496498
if turbconv_model isa PrognosticEDMFX
497499
set_prognostic_edmf_precomputed_quantities_draft!(Y, p, ᶠuₕ³, t)

src/parameterized_tendencies/microphysics/cloud_condensate.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ function cloud_condensate_tendency!(
2929
)
3030
(; ᶜts) = p.precomputed
3131
(; params, dt) = p
32-
(; q_rai, q_sno) = p.precomputed.ᶜspecific
3332
FT = eltype(params)
3433
thp = CAP.thermodynamics_params(params)
3534
cmc = CAP.microphysics_cloud_params(params)
3635

37-
@. Yₜ.c.ρq_liq += Y.c.ρ * cloud_sources(cmc.liquid, thp, ᶜts, q_rai, dt)
38-
@. Yₜ.c.ρq_ice += Y.c.ρ * cloud_sources(cmc.ice, thp, ᶜts, q_sno, dt)
36+
@. Yₜ.c.ρq_liq +=
37+
Y.c.ρ *
38+
cloud_sources(cmc.liquid, thp, ᶜts, specific(Y.c.ρq_rai, Y.c.ρ), dt)
39+
@. Yₜ.c.ρq_ice +=
40+
Y.c.ρ *
41+
cloud_sources(cmc.ice, thp, ᶜts, specific(Y.c.ρq_sno, Y.c.ρ), dt)
3942
end
4043

4144
function cloud_condensate_tendency!(
@@ -47,21 +50,24 @@ function cloud_condensate_tendency!(
4750
)
4851
(; ᶜts) = p.precomputed
4952
(; params, dt) = p
50-
(; q_rai, q_sno, n_liq) = p.precomputed.ᶜspecific
5153
FT = eltype(params)
5254
thp = CAP.thermodynamics_params(params)
5355
cmc = CAP.microphysics_cloud_params(params)
5456

55-
@. Yₜ.c.ρq_liq += Y.c.ρ * cloud_sources(cmc.liquid, thp, ᶜts, q_rai, dt)
56-
@. Yₜ.c.ρq_ice += Y.c.ρ * cloud_sources(cmc.ice, thp, ᶜts, q_sno, dt)
57+
@. Yₜ.c.ρq_liq +=
58+
Y.c.ρ *
59+
cloud_sources(cmc.liquid, thp, ᶜts, specific(Y.c.ρq_rai, Y.c.ρ), dt)
60+
@. Yₜ.c.ρq_ice +=
61+
Y.c.ρ *
62+
cloud_sources(cmc.ice, thp, ᶜts, specific(Y.c.ρq_sno, Y.c.ρ), dt)
5763

5864
@. Yₜ.c.ρn_liq +=
5965
Y.c.ρ * aerosol_activation_sources(
6066
cmc.liquid,
6167
thp,
6268
ᶜts,
63-
q_rai,
64-
n_liq,
69+
specific(Y.c.ρq_rai, Y.c.ρ),
70+
specific(Y.c.ρn_liq, Y.c.ρ),
6571
cmc.N_cloud_liquid_droplets,
6672
dt,
6773
)

src/prognostic_equations/edmfx_sgs_flux.jl

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function edmfx_sgs_mass_flux_tendency!(
9090
for j in 1:n
9191
@. ᶠu³_diff = ᶠu³ʲs.:($$j) - ᶠu³
9292
@. ᶜa_scalar =
93-
(Y.c.sgsʲs.:($$j).q_tot - ᶜspecific.q_tot) *
93+
(Y.c.sgsʲs.:($$j).q_tot - specific(Y.c.ρq_tot, Y.c.ρ)) *
9494
draft_area(Y.c.sgsʲs.:($$j).ρa, ᶜρʲs.:($$j))
9595
vtt = vertical_transport(
9696
ᶜρʲs.:($j),
@@ -103,7 +103,8 @@ function edmfx_sgs_mass_flux_tendency!(
103103
end
104104
# Add the environment fluxes
105105
@. ᶠu³_diff = ᶠu³⁰ - ᶠu³
106-
@. ᶜa_scalar = (ᶜq_tot⁰ - ᶜspecific.q_tot) * draft_area(ᶜρa⁰, ᶜρ⁰)
106+
@. ᶜa_scalar =
107+
(ᶜq_tot⁰ - specific(Y.c.ρq_tot, Y.c.ρ)) * draft_area(ᶜρa⁰, ᶜρ⁰)
107108
vtt = vertical_transport(
108109
ᶜρ⁰,
109110
ᶠu³_diff,
@@ -123,7 +124,7 @@ function edmfx_sgs_mass_flux_tendency!(
123124
@. ᶠu³_diff = ᶠu³ʲs.:($$j) - ᶠu³
124125

125126
@. ᶜa_scalar =
126-
(Y.c.sgsʲs.:($$j).q_liq - ᶜspecific.q_liq) *
127+
(Y.c.sgsʲs.:($$j).q_liq - specific(Y.c.ρq_liq, Y.c.ρ)) *
127128
draft_area(Y.c.sgsʲs.:($$j).ρa, ᶜρʲs.:($$j))
128129
vtt = vertical_transport(
129130
ᶜρʲs.:($j),
@@ -135,7 +136,7 @@ function edmfx_sgs_mass_flux_tendency!(
135136
@. Yₜ.c.ρq_liq += vtt
136137

137138
@. ᶜa_scalar =
138-
(Y.c.sgsʲs.:($$j).q_ice - ᶜspecific.q_ice) *
139+
(Y.c.sgsʲs.:($$j).q_ice - specific(Y.c.ρq_ice, Y.c.ρ)) *
139140
draft_area(Y.c.sgsʲs.:($$j).ρa, ᶜρʲs.:($$j))
140141
vtt = vertical_transport(
141142
ᶜρʲs.:($j),
@@ -147,7 +148,7 @@ function edmfx_sgs_mass_flux_tendency!(
147148
@. Yₜ.c.ρq_ice += vtt
148149

149150
@. ᶜa_scalar =
150-
(Y.c.sgsʲs.:($$j).q_rai - ᶜspecific.q_rai) *
151+
(Y.c.sgsʲs.:($$j).q_rai - specific(Y.c.ρq_rai, Y.c.ρ)) *
151152
draft_area(Y.c.sgsʲs.:($$j).ρa, ᶜρʲs.:($$j))
152153
vtt = vertical_transport(
153154
ᶜρʲs.:($j),
@@ -159,7 +160,7 @@ function edmfx_sgs_mass_flux_tendency!(
159160
@. Yₜ.c.ρq_rai += vtt
160161

161162
@. ᶜa_scalar =
162-
(Y.c.sgsʲs.:($$j).q_sno - ᶜspecific.q_sno) *
163+
(Y.c.sgsʲs.:($$j).q_sno - specific(Y.c.ρq_sno, Y.c.ρ)) *
163164
draft_area(Y.c.sgsʲs.:($$j).ρa, ᶜρʲs.:($$j))
164165
vtt = vertical_transport(
165166
ᶜρʲs.:($j),
@@ -172,7 +173,8 @@ function edmfx_sgs_mass_flux_tendency!(
172173
end
173174
@. ᶠu³_diff = ᶠu³⁰ - ᶠu³
174175

175-
@. ᶜa_scalar = (ᶜq_liq⁰ - ᶜspecific.q_liq) * draft_area(ᶜρa⁰, ᶜρ⁰)
176+
@. ᶜa_scalar =
177+
(ᶜq_liq⁰ - specific(Y.c.ρq_liq, Y.c.ρ)) * draft_area(ᶜρa⁰, ᶜρ⁰)
176178
vtt = vertical_transport(
177179
ᶜρ⁰,
178180
ᶠu³_diff,
@@ -182,7 +184,8 @@ function edmfx_sgs_mass_flux_tendency!(
182184
)
183185
@. Yₜ.c.ρq_liq += vtt
184186

185-
@. ᶜa_scalar = (ᶜq_ice⁰ - ᶜspecific.q_ice) * draft_area(ᶜρa⁰, ᶜρ⁰)
187+
@. ᶜa_scalar =
188+
(ᶜq_ice⁰ - specific(Y.c.ρq_ice, Y.c.ρ)) * draft_area(ᶜρa⁰, ᶜρ⁰)
186189
vtt = vertical_transport(
187190
ᶜρ⁰,
188191
ᶠu³_diff,
@@ -192,7 +195,8 @@ function edmfx_sgs_mass_flux_tendency!(
192195
)
193196
@. Yₜ.c.ρq_ice += vtt
194197

195-
@. ᶜa_scalar = (ᶜq_rai⁰ - ᶜspecific.q_rai) * draft_area(ᶜρa⁰, ᶜρ⁰)
198+
@. ᶜa_scalar =
199+
(ᶜq_rai⁰ - specific(Y.c.ρq_rai, Y.c.ρ)) * draft_area(ᶜρa⁰, ᶜρ⁰)
196200
vtt = vertical_transport(
197201
ᶜρ⁰,
198202
ᶠu³_diff,
@@ -202,7 +206,8 @@ function edmfx_sgs_mass_flux_tendency!(
202206
)
203207
@. Yₜ.c.ρq_rai += vtt
204208

205-
@. ᶜa_scalar = (ᶜq_sno⁰ - ᶜspecific.q_sno) * draft_area(ᶜρa⁰, ᶜρ⁰)
209+
@. ᶜa_scalar =
210+
(ᶜq_sno⁰ - specific(Y.c.ρq_sno, Y.c.ρ)) * draft_area(ᶜρa⁰, ᶜρ⁰)
206211
vtt = vertical_transport(
207212
ᶜρ⁰,
208213
ᶠu³_diff,
@@ -271,11 +276,11 @@ function edmfx_sgs_mass_flux_tendency!(
271276
for j in 1:n
272277
@. ᶠu³_diff = ᶠu³ʲs.:($$j) - ᶠu³
273278
# @. ᶜa_scalar =
274-
# (ᶜq_totʲs.:($$j) - ᶜspecific.q_tot) *
279+
# (ᶜq_totʲs.:($$j) - specific(Y.c.ρq_tot, Y.c.ρ)) *
275280
# draft_area(ᶜρaʲs.:($$j), ᶜρʲs.:($$j))
276281
# TODO: remove this filter when mass flux is treated implicitly
277282
@. ᶜa_scalar =
278-
(ᶜq_totʲs.:($$j) - ᶜspecific.q_tot) * min(
283+
(ᶜq_totʲs.:($$j) - specific(Y.c.ρq_tot, Y.c.ρ)) * min(
279284
min(draft_area(ᶜρaʲs.:($$j), ᶜρʲs.:($$j)), a_max),
280285
FT(0.02) / max(
281286
Geometry.WVector(
@@ -491,7 +496,7 @@ function edmfx_sgs_diffusive_flux_tendency!(
491496
bottom = Operators.SetValue(C3(FT(0))),
492497
)
493498
@. ᶜρχₜ_diffusion =
494-
ᶜdivᵥ_ρq_tot(-(ᶠρaK_h * ᶠgradᵥ(ᶜspecific.q_tot)))
499+
ᶜdivᵥ_ρq_tot(-(ᶠρaK_h * ᶠgradᵥ(specific(Y.c.ρq_tot, Y.c.ρ))))
495500
@. Yₜ.c.ρq_tot -= ᶜρχₜ_diffusion
496501
@. Yₜ.c.ρ -= ᶜρχₜ_diffusion
497502
end

src/prognostic_equations/forcing/external_forcing.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ function external_forcing_tendency!(
363363
ᶜdqtdt_nudging = p.scratch.ᶜtemp_scalar_2
364364
@. ᶜdTdt_nudging =
365365
-(TD.air_temperature(thermo_params, ᶜts) - ᶜT_nudge) * ᶜinv_τ_scalar
366-
@. ᶜdqtdt_nudging = -(ᶜspecific.q_tot - ᶜqt_nudge) * ᶜinv_τ_scalar
366+
@. ᶜdqtdt_nudging =
367+
-(specific(Y.c.ρq_tot, Y.c.ρ) - ᶜqt_nudge) * ᶜinv_τ_scalar
367368

368369
ᶜdTdt_sum = p.scratch.ᶜtemp_scalar
369370
ᶜdqtdt_sum = p.scratch.ᶜtemp_scalar_2
@@ -572,7 +573,7 @@ function external_forcing_tendency!(Yₜ, Y, p, t, ::ISDACForcing)
572573
FT = Spaces.undertype(axes(Y.c))
573574
(; params) = p
574575
thermo_params = CAP.thermodynamics_params(params)
575-
(; ᶜspecific, ᶜts, ᶜh_tot, ᶜp) = p.precomputed
576+
(; ᶜts, ᶜh_tot, ᶜp) = p.precomputed
576577

577578
ᶜinv_τ_scalar = APL.ISDAC_inv_τ_scalar(FT) # s⁻¹
578579
ᶜinv_τ_wind = APL.ISDAC_inv_τ_wind(FT) # s⁻¹
@@ -601,7 +602,8 @@ function external_forcing_tendency!(Yₜ, Y, p, t, ::ISDACForcing)
601602
@. ᶜdTdt_nudging =
602603
-(TD.air_temperature(thermo_params, ᶜts) - ta_ISDAC(ᶜp, ᶜz)) *
603604
ᶜinv_τ_scalar(ᶜz)
604-
@. ᶜdqtdt_nudging = -(ᶜspecific.q_tot - q_tot(ᶜz)) * ᶜinv_τ_scalar(ᶜz)
605+
@. ᶜdqtdt_nudging =
606+
-(specific(Y.c.ρq_tot, Y.c.ρ) - q_tot(ᶜz)) * ᶜinv_τ_scalar(ᶜz)
605607

606608
T_0 = TD.Parameters.T_0(thermo_params)
607609
Lv_0 = TD.Parameters.LH_v0(thermo_params)

src/prognostic_equations/hyperdiffusion.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ NVTX.@annotate function prep_hyperdiffusion_tendency!(Yₜ, Y, p, t)
9696
C123(wgradₕ(divₕ(p.precomputed.ᶜu))) -
9797
C123(wcurlₕ(C123(curlₕ(p.precomputed.ᶜu))))
9898

99-
@. ᶜ∇²specific_energy = wdivₕ(gradₕ(ᶜspecific.e_tot + ᶜp / Y.c.ρ))
99+
@. ᶜ∇²specific_energy =
100+
wdivₕ(gradₕ(specific(Y.c.ρe_tot, Y.c.ρ) + ᶜp / Y.c.ρ))
100101

101102
if diffuse_tke
102103
(; ᶜtke⁰) = p.precomputed

0 commit comments

Comments
 (0)