Skip to content

Commit a63dcd2

Browse files
Use LazyBroadcast in set_velocity_quantities
1 parent 8445cd1 commit a63dcd2

9 files changed

+43
-50
lines changed

src/cache/diagnostic_edmf_precomputed_quantities.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,7 @@ NVTX.@annotate function set_diagnostic_edmf_precomputed_quantities_env_closures!
944944
ᶠu⁰ = p.scratch.ᶠtemp_C123
945945
@. ᶠu⁰ = C123(ᶠinterp(Y.c.uₕ)) + C123(ᶠu³⁰)
946946
ᶜstrain_rate = p.scratch.ᶜtemp_UVWxUVW
947-
bc_strain_rate = compute_strain_rate_center(ᶠu⁰)
948-
@. ᶜstrain_rate = bc_strain_rate
947+
ᶜstrain_rate .= compute_strain_rate_center(ᶠu⁰)
949948
@. ᶜstrain_rate_norm = norm_sqr(ᶜstrain_rate)
950949

951950
ᶜprandtl_nvec = p.scratch.ᶜtemp_scalar

src/cache/precomputed_quantities.jl

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ the `turbconv_model` is EDMFX, the `Y.f.sgsʲs` are also modified so that each
197197
"""
198198
function set_velocity_at_surface!(Y, ᶠuₕ³, turbconv_model)
199199
sfc_u₃ = Fields.level(Y.f.u₃.components.data.:1, half)
200-
bc_sfc_u₃ = surface_velocity(Y.f.u₃, ᶠuₕ³)
201-
@. sfc_u₃ = bc_sfc_u₃
200+
sfc_u₃ .= surface_velocity(Y.f.u₃, ᶠuₕ³)
202201
if turbconv_model isa PrognosticEDMFX
203202
for j in 1:n_mass_flux_subdomains(turbconv_model)
204203
sfc_u₃ʲ = Fields.level(Y.f.sgsʲs.:($j).u₃.components.data.:1, half)
@@ -240,12 +239,10 @@ end
240239

241240
# This is used to set the grid-scale velocity quantities ᶜu, ᶠu³, ᶜK based on
242241
# ᶠu₃, and it is also used to set the SGS quantities based on ᶠu₃⁰ and ᶠu₃ʲ.
243-
function set_velocity_quantities!(ᶜu, ᶠu³, ᶜK, ᶠu₃, ᶜuₕ, ᶠuₕ³)
244-
@. ᶜu = C123(ᶜuₕ) + ᶜinterp(C123(ᶠu₃))
245-
@. ᶠu³ = ᶠuₕ³ + CT3(ᶠu₃)
246-
bc_kinetic = compute_kinetic(ᶜuₕ, ᶠu₃)
247-
@. ᶜK = bc_kinetic
248-
return nothing
242+
compute_ᶜu(ᶜuₕ, ᶠu₃) = @lazy @. C123(ᶜuₕ) + ᶜinterp(C123(ᶠu₃))
243+
function compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃)
244+
ᶠuₕ³ = compute_ᶠuₕ³(ᶜuₕ, ᶜρ)
245+
return @lazy @. ᶠuₕ³ + CT3(ᶠu₃)
249246
end
250247

251248
function set_sgs_ᶠu₃!(w_function, ᶠu₃, Y, turbconv_model)
@@ -385,15 +382,18 @@ NVTX.@annotate function set_precomputed_quantities!(Y, p, t)
385382
@. ᶜspecific = specific_gs(Y.c)
386383
ᶜρ = Y.c.ρ
387384
ᶜuₕ = Y.c.uₕ
388-
bc_ᶠuₕ³ = compute_ᶠuₕ³(ᶜuₕ, ᶜρ)
389-
@. ᶠuₕ³ = bc_ᶠuₕ³
385+
ᶠu₃ = Y.f.u₃
386+
ᶠuₕ³ .= compute_ᶠuₕ³(ᶜuₕ, ᶜρ)
390387

391388
# TODO: We might want to move this to dss! (and rename dss! to something
392389
# like enforce_constraints!).
393390
set_velocity_at_surface!(Y, ᶠuₕ³, turbconv_model)
394391
set_velocity_at_top!(Y, turbconv_model)
395392

396-
set_velocity_quantities!(ᶜu, ᶠu³, ᶜK, Y.f.u₃, Y.c.uₕ, ᶠuₕ³)
393+
ᶠu³ .= compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃)
394+
ᶜu .= compute_ᶜu(ᶜuₕ, ᶠu₃)
395+
ᶜK .= compute_kinetic(ᶜuₕ, ᶠu₃)
396+
397397
if n > 0
398398
# TODO: In the following increments to ᶜK, we actually need to add
399399
# quantities of the form ᶜρaχ⁰ / ᶜρ⁰ and ᶜρaχʲ / ᶜρʲ to ᶜK, rather than
@@ -496,8 +496,8 @@ NVTX.@annotate function set_precomputed_quantities!(Y, p, t)
496496
end
497497

498498
if turbconv_model isa PrognosticEDMFX
499-
set_prognostic_edmf_precomputed_quantities_draft_and_bc!(Y, p, ᶠuₕ³, t)
500-
set_prognostic_edmf_precomputed_quantities_environment!(Y, p, ᶠuₕ³, t)
499+
set_prognostic_edmf_precomputed_quantities_draft_and_bc!(Y, p, t)
500+
set_prognostic_edmf_precomputed_quantities_environment!(Y, p, t)
501501
set_prognostic_edmf_precomputed_quantities_closures!(Y, p, t)
502502
set_prognostic_edmf_precomputed_quantities_precipitation!(
503503
Y,
@@ -521,12 +521,10 @@ NVTX.@annotate function set_precomputed_quantities!(Y, p, t)
521521

522522
if vert_diff isa DecayWithHeightDiffusion
523523
(; ᶜK_h) = p.precomputed
524-
bc_K_h = compute_eddy_diffusivity_coefficient(ᶜρ, vert_diff)
525-
@. ᶜK_h = bc_K_h
524+
ᶜK_h .= compute_eddy_diffusivity_coefficient(ᶜρ, vert_diff)
526525
elseif vert_diff isa VerticalDiffusion
527526
(; ᶜK_h) = p.precomputed
528-
bc_K_h = compute_eddy_diffusivity_coefficient(Y.c.uₕ, ᶜp, vert_diff)
529-
@. ᶜK_h = bc_K_h
527+
ᶜK_h .= compute_eddy_diffusivity_coefficient(Y.c.uₕ, ᶜp, vert_diff)
530528
end
531529

532530
# TODO

src/cache/prognostic_edmf_precomputed_quantities.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ Updates the edmf environment precomputed quantities stored in `p` for edmfx.
1313
NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_environment!(
1414
Y,
1515
p,
16-
ᶠuₕ³,
1716
t,
1817
)
1918
@assert !(p.atmos.moisture_model isa DryModel)
2019

20+
ᶜuₕ = Y.c.uₕ
21+
ᶠu₃ = Y.f.u₃
22+
ᶜρ = Y.c.ρ
2123
thermo_params = CAP.thermodynamics_params(p.params)
2224
(; turbconv_model) = p.atmos
2325
(; ᶜΦ,) = p.core
@@ -42,29 +44,34 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_environment!(
4244
turbconv_model,
4345
)
4446
set_sgs_ᶠu₃!(u₃⁰, ᶠu₃⁰, Y, turbconv_model)
45-
set_velocity_quantities!(ᶜu⁰, ᶠu³⁰, ᶜK⁰, ᶠu₃⁰, Y.c.uₕ, ᶠuₕ³)
47+
ᶠu³⁰ .= compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃)
48+
ᶜu⁰ .= compute_ᶜu(ᶜuₕ, ᶠu₃⁰)
49+
ᶜK⁰ .= compute_kinetic(ᶜuₕ, ᶠu₃⁰)
50+
4651
# @. ᶜK⁰ += ᶜtke⁰
4752
@. ᶜts⁰ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmse⁰ - ᶜΦ, ᶜq_tot⁰)
4853
@. ᶜρ⁰ = TD.air_density(thermo_params, ᶜts⁰)
4954
return nothing
5055
end
5156

5257
"""
53-
set_prognostic_edmf_precomputed_quantities_draft_and_bc!(Y, p, ᶠuₕ³, t)
58+
set_prognostic_edmf_precomputed_quantities_draft_and_bc!(Y, p, t)
5459
5560
Updates the draft thermo state and boundary conditions
5661
precomputed quantities stored in `p` for edmfx.
5762
"""
5863
NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_draft_and_bc!(
5964
Y,
6065
p,
61-
ᶠuₕ³,
6266
t,
6367
)
6468
(; moisture_model, turbconv_model) = p.atmos
6569
#EDMFX BCs only support total energy as state variable
6670
@assert !(moisture_model isa DryModel)
6771

72+
ᶜuₕ = Y.c.uₕ
73+
ᶠu₃ = Y.f.u₃
74+
ᶜρ = Y.c.ρ
6875
FT = Spaces.undertype(axes(Y.c))
6976
n = n_mass_flux_subdomains(turbconv_model)
7077

@@ -88,7 +95,10 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_draft_and_bc!
8895
ᶜmseʲ = Y.c.sgsʲs.:($j).mse
8996
ᶜq_totʲ = Y.c.sgsʲs.:($j).q_tot
9097

91-
set_velocity_quantities!(ᶜuʲ, ᶠu³ʲ, ᶜKʲ, ᶠu₃ʲ, Y.c.uₕ, ᶠuₕ³)
98+
ᶠu³ʲ .= compute_ᶠu³(ᶜuₕ, ᶜρ, ᶠu₃)
99+
ᶜuʲ .= compute_ᶜu(ᶜuₕ, ᶠu₃ʲ)
100+
ᶜKʲ .= compute_kinetic(ᶜuₕ, ᶠu₃ʲ)
101+
92102
@. ᶠKᵥʲ = (adjoint(CT3(ᶠu₃ʲ)) * ᶠu₃ʲ) / 2
93103
@. ᶜtsʲ = TD.PhaseEquil_phq(thermo_params, ᶜp, ᶜmseʲ - ᶜΦ, ᶜq_totʲ)
94104
@. ᶜρʲ = TD.air_density(thermo_params, ᶜtsʲ)
@@ -306,8 +316,7 @@ NVTX.@annotate function set_prognostic_edmf_precomputed_quantities_closures!(
306316
ᶠu⁰ = p.scratch.ᶠtemp_C123
307317
@. ᶠu⁰ = C123(ᶠinterp(Y.c.uₕ)) + C123(ᶠu³⁰)
308318
ᶜstrain_rate = p.scratch.ᶜtemp_UVWxUVW
309-
bc_strain_rate = compute_strain_rate_center(ᶠu⁰)
310-
@. ᶜstrain_rate = bc_strain_rate
319+
ᶜstrain_rate .= compute_strain_rate_center(ᶠu⁰)
311320
@. ᶜstrain_rate_norm = norm_sqr(ᶜstrain_rate)
312321

313322
ᶜprandtl_nvec = p.scratch.ᶜtemp_scalar

src/initial_conditions/initial_conditions.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ function overwrite_initial_conditions!(
459459
Y.c.uₕ .= C12.(Geometry.UVVector.(vel))
460460
Y.f.u₃ .= ᶠinterp.(C3.(Geometry.WVector.(vel)))
461461
e_kin = similar(ᶜT)
462-
bc_kinetic = compute_kinetic(Y.c.uₕ, Y.f.u₃)
463-
@. e_kin = bc_kinetic
462+
e_kin .= compute_kinetic(Y.c.uₕ, Y.f.u₃)
464463
e_pot = Fields.coordinate_field(Y.c).z .* thermo_params.grav
465464
Y.c.ρe_tot .= TD.total_energy.(thermo_params, ᶜts, e_kin, e_pot) .* Y.c.ρ
466465
if hasproperty(Y.c, :ρq_tot)

src/prognostic_equations/edmfx_sgs_flux.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ function edmfx_sgs_diffusive_flux_tendency!(
225225

226226
# momentum
227227
ᶠstrain_rate = p.scratch.ᶠtemp_UVWxUVW
228-
bc_strain_rate = compute_strain_rate_face(ᶜu⁰)
229-
@. ᶠstrain_rate = bc_strain_rate
228+
ᶠstrain_rate .= compute_strain_rate_face(ᶜu⁰)
230229
@. Yₜ.c.uₕ -= C12(ᶜdivᵥ(-(2 * ᶠρaK_u * ᶠstrain_rate)) / Y.c.ρ)
231230
# apply boundary condition for momentum flux
232231
ᶜdivᵥ_uₕ = Operators.DivergenceF2C(
@@ -298,8 +297,7 @@ function edmfx_sgs_diffusive_flux_tendency!(
298297

299298
# momentum
300299
ᶠstrain_rate = p.scratch.ᶠtemp_UVWxUVW
301-
bc_strain_rate = compute_strain_rate_face(ᶜu)
302-
@. ᶠstrain_rate = bc_strain_rate
300+
ᶠstrain_rate .= compute_strain_rate_face(ᶜu)
303301
@. Yₜ.c.uₕ -= C12(ᶜdivᵥ(-(2 * ᶠρaK_u * ᶠstrain_rate)) / Y.c.ρ)
304302
# apply boundary condition for momentum flux
305303
ᶜdivᵥ_uₕ = Operators.DivergenceF2C(

src/prognostic_equations/gm_sgs_closures.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ NVTX.@annotate function compute_gm_mixing_length!(ᶜmixing_length, Y, p)
4747
ᶠu = p.scratch.ᶠtemp_C123
4848
@. ᶠu = C123(ᶠinterp(Y.c.uₕ)) + C123(ᶠu³)
4949
ᶜstrain_rate = p.scratch.ᶜtemp_UVWxUVW
50-
bc_strain_rate = compute_strain_rate_center(ᶠu)
51-
@. ᶜstrain_rate = bc_strain_rate
50+
ᶜstrain_rate .= compute_strain_rate_center(ᶠu)
5251
@. ᶜstrain_rate_norm = norm_sqr(ᶜstrain_rate)
5352

5453
ᶜprandtl_nvec = p.scratch.ᶜtemp_scalar_2

src/prognostic_equations/vertical_diffusion_boundary_layer.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ function vertical_diffusion_boundary_layer_tendency!(
3131

3232
if diffuse_momentum(p.atmos.vert_diff)
3333
ᶠstrain_rate = p.scratch.ᶠtemp_UVWxUVW
34-
bc_strain_rate = compute_strain_rate_face(ᶜu)
35-
@. ᶠstrain_rate = bc_strain_rate
34+
ᶠstrain_rate .= compute_strain_rate_face(ᶜu)
3635
@. Yₜ.c.uₕ -= C12(
3736
ᶜdivᵥ(-2 * ᶠinterp(Y.c.ρ) * ᶠinterp(ᶜK_u) * ᶠstrain_rate) / Y.c.ρ,
3837
)

src/utils/utilities.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ sort_files_by_time(files) =
4848
permute!(files, sortperm(time_from_filename.(files)))
4949

5050
"""
51-
bc_kinetic = compute_kinetic(uₕ::Field, uᵥ::Field)
52-
@. κ = bc_kinetic
51+
κ .= compute_kinetic(uₕ::Field, uᵥ::Field)
5352
5453
Compute the specific kinetic energy at cell centers, resulting in `κ` from
5554
individual velocity components:
@@ -78,8 +77,7 @@ state.
7877
compute_kinetic(Y::Fields.FieldVector) = compute_kinetic(Y.c.uₕ, Y.f.u₃)
7978

8079
"""
81-
bc_ϵ = compute_strain_rate_center(u::Field)
82-
@. ϵ = bc_ϵ
80+
ϵ .= compute_strain_rate_center(u::Field)
8381
8482
Compute the strain_rate at cell centers from velocity at cell faces.
8583
"""
@@ -93,8 +91,7 @@ function compute_strain_rate_center(u::Fields.Field)
9391
end
9492

9593
"""
96-
bc_ϵ = compute_strain_rate_face(u::Field)
97-
@. ϵ = bc_ϵ
94+
ϵ .= compute_strain_rate_face(u::Field)
9895
9996
Compute the strain_rate at cell faces from velocity at cell centers.
10097
"""

test/utilities.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ end
4747
CT123 = Geometry.Contravariant123Vector
4848
# Exercise function
4949
κ = zeros(cent_space)
50-
bc_kinetic = CA.compute_kinetic(uₕ, uᵥ)
51-
@. κ = bc_kinetic
50+
κ .= CA.compute_kinetic(uₕ, uᵥ)
5251
ᶜκ_exact = @. 1 // 2 *
5352
cos(z)^2 *
5453
((sin(x)^2) * (cos(y)^2) + (cos(x)^2) * (sin(y)^2))
@@ -86,12 +85,8 @@ end
8685
ᶠu = @. UVW(Geometry.UVector(ᶠu)) +
8786
UVW(Geometry.VVector(ᶠv)) +
8887
UVW(Geometry.WVector(ᶠw))
89-
bc_strain_rate =
90-
CA.compute_strain_rate_center(Geometry.Covariant123Vector.(ᶠu))
91-
@. ᶜϵ = bc_strain_rate
92-
bc_strain_rate =
93-
CA.compute_strain_rate_face(Geometry.Covariant123Vector.(ᶜu))
94-
@. ᶠϵ = bc_strain_rate
88+
ᶜϵ .= CA.compute_strain_rate_center(Geometry.Covariant123Vector.(ᶠu))
89+
ᶠϵ .= CA.compute_strain_rate_face(Geometry.Covariant123Vector.(ᶜu))
9590

9691
# Center valued strain rate
9792
@test ᶜϵ.components.data.:1 == ᶜϵ.components.data.:1 .* FT(0)

0 commit comments

Comments
 (0)