Skip to content

Commit 0bd4b89

Browse files
Fuse some broadcast expressions
1 parent 0ff2ea7 commit 0bd4b89

8 files changed

+321
-256
lines changed

src/ClimaAtmos.jl

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ClimaAtmos
22

33
using NVTX, Colors
44
import Thermodynamics as TD
5+
import ClimaCore.Fields: @fused_direct
56

67
include(joinpath("parameters", "Parameters.jl"))
78
import .Parameters as CAP

src/cache/diagnostic_edmf_precomputed_quantities.jl

+11-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ NVTX.@annotate function set_diagnostic_edmfx_draft_quantities_level!(
3939
Φ_level,
4040
)
4141
FT = eltype(thermo_params)
42-
@. ts_level = TD.PhaseEquil_phq(
43-
thermo_params,
44-
p_level,
45-
mse_level - Φ_level,
46-
q_tot_level,
47-
8,
48-
FT(0.0003),
49-
)
50-
@. ρ_level = TD.air_density(thermo_params, ts_level)
42+
@fused_direct begin
43+
@. ts_level = TD.PhaseEquil_phq(
44+
thermo_params,
45+
p_level,
46+
mse_level - Φ_level,
47+
q_tot_level,
48+
8,
49+
FT(0.0003),
50+
)
51+
@. ρ_level = TD.air_density(thermo_params, ts_level)
52+
end
5153
return nothing
5254
end
5355

src/cache/precipitation_precomputed_quantities.jl

+17-15
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ function set_precipitation_precomputed_quantities!(Y, p, t)
2222
cmp = CAP.microphysics_precipitation_params(p.params)
2323

2424
# compute the precipitation specific humidities
25-
@. ᶜqᵣ = qₚ(Y.c.ρq_rai, Y.c.ρ)
26-
@. ᶜqₛ = qₚ(Y.c.ρq_sno, Y.c.ρ)
25+
@fused_direct begin
26+
@. ᶜqᵣ = qₚ(Y.c.ρq_rai, Y.c.ρ)
27+
@. ᶜqₛ = qₚ(Y.c.ρq_sno, Y.c.ρ)
2728

28-
# compute the precipitation terminal velocity [m/s]
29-
@. ᶜwᵣ = CM1.terminal_velocity(
30-
cmp.pr,
31-
cmp.tv.rain,
32-
Y.c.ρ,
33-
abs(Y.c.ρq_rai / Y.c.ρ),
34-
)
35-
@. ᶜwₛ = CM1.terminal_velocity(
36-
cmp.ps,
37-
cmp.tv.snow,
38-
Y.c.ρ,
39-
abs(Y.c.ρq_sno / Y.c.ρ),
40-
)
29+
# compute the precipitation terminal velocity [m/s]
30+
@. ᶜwᵣ = CM1.terminal_velocity(
31+
cmp.pr,
32+
cmp.tv.rain,
33+
Y.c.ρ,
34+
abs(Y.c.ρq_rai / Y.c.ρ),
35+
)
36+
@. ᶜwₛ = CM1.terminal_velocity(
37+
cmp.ps,
38+
cmp.tv.snow,
39+
Y.c.ρ,
40+
abs(Y.c.ρq_sno / Y.c.ρ),
41+
)
42+
end
4143
return nothing
4244
end

src/cache/precomputed_quantities.jl

+7-5
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,13 @@ NVTX.@annotate function set_precomputed_quantities!(Y, p, t)
479479
# @. ᶜK += Y.c.sgs⁰.ρatke / Y.c.ρ
480480
# TODO: We should think more about these increments before we use them.
481481
end
482-
@. ᶜts = ts_gs(thermo_args..., ᶜspecific, ᶜK, ᶜΦ, Y.c.ρ)
483-
@. ᶜp = TD.air_pressure(thermo_params, ᶜts)
482+
(; ᶜh_tot) = p.precomputed
483+
@fused_direct begin
484+
@. ᶜts = ts_gs(thermo_args..., ᶜspecific, ᶜK, ᶜΦ, Y.c.ρ)
485+
@. ᶜp = TD.air_pressure(thermo_params, ᶜts)
486+
@. ᶜh_tot =
487+
TD.total_specific_enthalpy(thermo_params, ᶜts, ᶜspecific.e_tot)
488+
end
484489

485490
if turbconv_model isa AbstractEDMF
486491
@. p.precomputed.ᶜgradᵥ_θ_virt =
@@ -491,9 +496,6 @@ NVTX.@annotate function set_precomputed_quantities!(Y, p, t)
491496
ᶜgradᵥ(ᶠinterp(TD.liquid_ice_pottemp(thermo_params, ᶜts)))
492497
end
493498

494-
(; ᶜh_tot) = p.precomputed
495-
@. ᶜh_tot = TD.total_specific_enthalpy(thermo_params, ᶜts, ᶜspecific.e_tot)
496-
497499
if !isnothing(p.sfc_setup)
498500
SurfaceConditions.update_surface_conditions!(Y, p, t)
499501
end

src/cache/prognostic_edmf_precomputed_quantities.jl

+31-24
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,31 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_environment!(
2525
(; ᶜtke⁰, ᶜρa⁰, ᶠu₃⁰, ᶜu⁰, ᶠu³⁰, ᶜK⁰, ᶜts⁰, ᶜρ⁰, ᶜmse⁰, ᶜq_tot⁰) =
2626
p.precomputed
2727

28-
@. ᶜρa⁰ = ρa⁰(Y.c)
29-
@. ᶜtke⁰ = divide_by_ρa(Y.c.sgs⁰.ρatke, ᶜρa⁰, 0, Y.c.ρ, turbconv_model)
30-
@. ᶜmse⁰ = divide_by_ρa(
31-
Y.c.ρ * (ᶜh_tot - ᶜK) - ρamse⁺(Y.c.sgsʲs),
32-
ᶜρa⁰,
33-
Y.c.ρ * (ᶜh_tot - ᶜK),
34-
Y.c.ρ,
35-
turbconv_model,
36-
)
37-
@. ᶜq_tot⁰ = divide_by_ρa(
38-
Y.c.ρq_tot - ρaq_tot⁺(Y.c.sgsʲs),
39-
ᶜρa⁰,
40-
Y.c.ρq_tot,
41-
Y.c.ρ,
42-
turbconv_model,
43-
)
28+
@fused_direct begin
29+
@. ᶜρa⁰ = ρa⁰(Y.c)
30+
@. ᶜtke⁰ = divide_by_ρa(Y.c.sgs⁰.ρatke, ᶜρa⁰, 0, Y.c.ρ, turbconv_model)
31+
@. ᶜmse⁰ = divide_by_ρa(
32+
Y.c.ρ * (ᶜh_tot - ᶜK) - ρamse⁺(Y.c.sgsʲs),
33+
ᶜρa⁰,
34+
Y.c.ρ * (ᶜh_tot - ᶜK),
35+
Y.c.ρ,
36+
turbconv_model,
37+
)
38+
@. ᶜq_tot⁰ = divide_by_ρa(
39+
Y.c.ρq_tot - ρaq_tot⁺(Y.c.sgsʲs),
40+
ᶜρa⁰,
41+
Y.c.ρq_tot,
42+
Y.c.ρ,
43+
turbconv_model,
44+
)
45+
end
4446
set_sgs_ᶠu₃!(u₃⁰, ᶠu₃⁰, Y, turbconv_model)
4547
set_velocity_quantities!(ᶜu⁰, ᶠu³⁰, ᶜK⁰, ᶠu₃⁰, Y.c.uₕ, ᶠuₕ³)
4648
# @. ᶜK⁰ += ᶜtke⁰
47-
@. ᶜts⁰ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmse⁰ - ᶜΦ, ᶜq_tot⁰)
48-
@. ᶜρ⁰ = TD.air_density(thermo_params, ᶜts⁰)
49+
@fused_direct begin
50+
@. ᶜts⁰ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmse⁰ - ᶜΦ, ᶜq_tot⁰)
51+
@. ᶜρ⁰ = TD.air_density(thermo_params, ᶜts⁰)
52+
end
4953
return nothing
5054
end
5155

@@ -90,8 +94,10 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_draft_and_bc!
9094

9195
set_velocity_quantities!(ᶜuʲ, ᶠu³ʲ, ᶜKʲ, ᶠu₃ʲ, Y.c.uₕ, ᶠuₕ³)
9296
@. ᶠKᵥʲ = (adjoint(CT3(ᶠu₃ʲ)) * ᶠu₃ʲ) / 2
93-
@. ᶜtsʲ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmseʲ - ᶜΦ, ᶜq_totʲ)
94-
@. ᶜρʲ = TD.air_density(thermo_params, ᶜtsʲ)
97+
@fused_direct begin
98+
@. ᶜtsʲ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmseʲ - ᶜΦ, ᶜq_totʲ)
99+
@. ᶜρʲ = TD.air_density(thermo_params, ᶜtsʲ)
100+
end
95101

96102
# EDMFX boundary condition:
97103

@@ -324,12 +330,13 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_closures!(
324330
ᶜtke_exch,
325331
)
326332

327-
@. ᶜmixing_length = ᶜmixing_length_tuple.master
328-
329333
turbconv_params = CAP.turbconv_params(params)
330334
c_m = CAP.tke_ed_coeff(turbconv_params)
331-
@. ᶜK_u = c_m * ᶜmixing_length * sqrt(max(ᶜtke⁰, 0))
332-
@. ᶜK_h = ᶜK_u / ᶜprandtl_nvec
335+
@fused_direct begin
336+
@. ᶜmixing_length = ᶜmixing_length_tuple.master
337+
@. ᶜK_u = c_m * ᶜmixing_length * sqrt(max(ᶜtke⁰, 0))
338+
@. ᶜK_h = ᶜK_u / ᶜprandtl_nvec
339+
end
333340

334341
ρatke_flux_values = Fields.field_values(ρatke_flux)
335342
ρ_int_values = Fields.field_values(Fields.level(ᶜρa⁰, 1))

src/parameterized_tendencies/microphysics/cloud_condensate.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function cloud_condensate_tendency!(Yₜ, p, colidx, ::NonEquilMoistModel)
1515
thp = CAP.thermodynamics_params(params)
1616
cmc = CAP.microphysics_cloud_params(params)
1717

18-
@. Yₜ.c.ρq_liq[colidx] += cloud_sources(cmc.liquid, thp, ᶜts[colidx], dt)
19-
@. Yₜ.c.ρq_ice[colidx] += cloud_sources(cmc.ice, thp, ᶜts[colidx], dt)
18+
@fused_direct begin
19+
@. Yₜ.c.ρq_liq[colidx] +=
20+
cloud_sources(cmc.liquid, thp, ᶜts[colidx], dt)
21+
@. Yₜ.c.ρq_ice[colidx] += cloud_sources(cmc.ice, thp, ᶜts[colidx], dt)
22+
end
2023
end

0 commit comments

Comments
 (0)