Skip to content

Commit 55ffc8c

Browse files
Fix broadcasting
1 parent b0c1d2d commit 55ffc8c

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/cache/precipitation_precomputed_quantities.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,31 @@ function compute_precip_velocities(ᶜY, ᶠY, p, t)
3434
cmp = CAP.microphysics_1m_params(p.params)
3535

3636
# compute the precipitation terminal velocity [m/s]
37-
ᶜwᵣ = CM1.terminal_velocity(
38-
cmp.pr,
39-
cmp.tv.rain,
40-
ᶜY.ρ,
41-
max(zero(ᶜY.ρ), ᶜY.ρq_rai / ᶜY.ρ),
42-
)
43-
ᶜwₛ = CM1.terminal_velocity(
37+
ᶜwᵣ = @. lazy(CM1.terminal_velocity(
38+
cmp.pr,
39+
cmp.tv.rain,
40+
ᶜY.ρ,
41+
max(zero(ᶜY.ρ), ᶜY.ρq_rai / ᶜY.ρ),
42+
))
43+
ᶜwₛ = @. lazy(CM1.terminal_velocity(
4444
cmp.ps,
4545
cmp.tv.snow,
4646
ᶜY.ρ,
4747
max(zero(ᶜY.ρ), ᶜY.ρq_sno / ᶜY.ρ),
48-
)
48+
))
4949
# compute sedimentation velocity for cloud condensate [m/s]
50-
ᶜwₗ = CMNe.terminal_velocity(
50+
ᶜwₗ = @. lazy(CMNe.terminal_velocity(
5151
cmc.liquid,
5252
cmc.Ch2022.rain,
5353
ᶜY.ρ,
5454
max(zero(ᶜY.ρ), ᶜY.ρq_liq / ᶜY.ρ),
55-
)
56-
ᶜwᵢ = CMNe.terminal_velocity(
55+
))
56+
ᶜwᵢ = @. lazy(CMNe.terminal_velocity(
5757
cmc.ice,
5858
cmc.Ch2022.small_ice,
5959
ᶜY.ρ,
6060
max(zero(ᶜY.ρ), ᶜY.ρq_ice / ᶜY.ρ),
61-
)
61+
))
6262
return (ᶜwᵣ, ᶜwₛ, ᶜwₗ, ᶜwᵢ)
6363
end
6464

@@ -73,12 +73,12 @@ function compute_ᶜwₜqₜ(ᶜY, ᶠY, p, t,
7373
)
7474
(ᶜwᵣ, ᶜwₛ, ᶜwₗ, ᶜwᵢ) = compute_precip_velocities(ᶜY, ᶠY, p, t)
7575
# compute their contributions to energy and total water advection
76-
return Geometry.WVector(
76+
return @. lazy(Geometry.WVector(
7777
ᶜwₗ * ᶜY.ρq_liq +
7878
ᶜwᵢ * ᶜY.ρq_ice +
7979
ᶜwᵣ * ᶜY.ρq_rai +
8080
ᶜwₛ * ᶜY.ρq_sno,
81-
) / ᶜY.ρ
81+
) / ᶜY.ρ)
8282
end
8383

8484
compute_ᶜwₕhₜ(ᶜY, ᶠY, p, t) =

src/prognostic_equations/remaining_tendency.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ NVTX.@annotate function hyperdiffusion_tendency!(Yₜ, Yₜ_lim, Y, p, t)
1010
apply_hyperdiffusion_tendency!(Yₜ, Y, p, t)
1111
end
1212

13-
function prognostic_nt(::Val{names}; tends...) where {names}
14-
nt_ordered = NamedTuple{names}(rzero(values(tends)))
15-
nt_values = NamedTuple{keys(tends)}(values(tends))
16-
return merge(nt_ordered, nt_values)
13+
@generated function sorted_nt(::Val{snames}, ::Val{unames}, vals...) where {snames,unames}
14+
svals_exprs = []
15+
for sn in snames
16+
i = findfirst(un -> un == sn, unames)::Int
17+
push!(svals_exprs, :(getfield(vals, $i)))
18+
end
19+
return quote
20+
NamedTuple{snames}(($(svals_exprs...),))
21+
end
1722
end
1823

24+
prognostic_nt(::Val{names}, ::Val{K}, vals...) where {names, K} =
25+
sorted_nt(Val(names), Val(K), vals...)
26+
1927
function ᶜremaining_tendency(ᶜY, ᶠY, p, t)
2028
names = propertynames(ᶜY)
2129
tends = (;
@@ -30,22 +38,23 @@ function ᶜremaining_tendency(ᶜY, ᶠY, p, t)
3038
ᶜremaining_tendency_sgs⁰(ᶜY, ᶠY, p, t)...,
3139
ᶜremaining_tendency_sgsʲs(ᶜY, ᶠY, p, t)...,
3240
)
33-
return lazy.(prognostic_nt.(Val(names); tends...))
41+
return lazy.(prognostic_nt.(Val(names), Val(keys(tends)), values(tends)...))
3442
end
3543
function ᶠremaining_tendency(ᶜY, ᶠY, p, t)
3644
names = propertynames(ᶠY)
3745
tends = (;
3846
ᶠremaining_tendency_u₃(ᶜY, ᶠY, p, t)...,
3947
ᶠremaining_tendency_sgsʲs(ᶜY, ᶠY, p, t)...,
4048
)
41-
return lazy.(prognostic_nt.(Val(names); tends...))
49+
return lazy.(prognostic_nt.(Val(names), Val(keys(tends)), values(tends)...))
4250
end
4351
using ClimaCore.RecursiveApply: rzero
4452
function ᶜremaining_tendency_ρ(ᶜY, ᶠY, p, t)
4553
in propertynames(ᶜY) || return ()
4654
∑tendencies = zero(eltype(ᶜY.ρ))
4755
ᶜJ = Fields.local_geometry_field(ᶜY).J
4856
ᶠJ = Fields.local_geometry_field(ᶠY).J
57+
ᶜρ = ᶜY.ρ
4958

5059
if !(p.atmos.moisture_model isa DryModel)
5160
ᶜwₜqₜ = compute_ᶜwₜqₜ(ᶜY, ᶠY, p, t)
@@ -178,7 +187,8 @@ function ᶠremaining_tendency_sgsʲs(ᶜY, ᶠY, p, t)
178187
end
179188

180189

181-
water_adv(ᶜρ, ᶜJ, ᶠJ, ᶜχ) =
190+
water_adv(ᶜρ, ᶜJ, ᶠJ, ᶜχ::Real) = zero(eltype(ᶜρ))
191+
water_adv(ᶜρ, ᶜJ, ᶠJ, ᶜχ) = # only valid when ᶜχ is a field
182192
@. lazy(ᶜprecipdivᵥ(ᶠinterp(ᶜρ * ᶜJ) / ᶠJ * ᶠright_bias(-(ᶜχ))))
183193

184194
function surface_velocity_full(ᶠu₃, ᶠuₕ³)

0 commit comments

Comments
 (0)