Skip to content

Commit 8ccab2a

Browse files
Use columnwise for setting tendencies to zero
1 parent 1f3398d commit 8ccab2a

File tree

2 files changed

+180
-1
lines changed

2 files changed

+180
-1
lines changed

src/prognostic_equations/remaining_tendency.jl

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,99 @@ NVTX.@annotate function hyperdiffusion_tendency!(Yₜ, Yₜ_lim, Y, p, t)
3737
apply_hyperdiffusion_tendency!(Yₜ, Y, p, t)
3838
end
3939

40+
using ClimaCore.RecursiveApply: rzero
41+
42+
#####
43+
##### Cell center tendencies
44+
#####
45+
function ᶜremaining_tendency(ᶜY, ᶠY, p, t)
46+
names = propertynames(ᶜY)
47+
tends = construct_tendencies(Val(names), ᶜremaining_tendency, ᶜY, ᶠY, p, t)
48+
# We cannot broadcast over a NamedTuple, so we need to check that edge case
49+
# first.
50+
if all(t -> !(t isa Base.Broadcast.Broadcasted), tends)
51+
return make_named_tuple(Val(names), tends...)
52+
else
53+
return lazy.(make_named_tuple.(Val(names), tends...))
54+
end
55+
end
56+
57+
#####
58+
##### Cell face tendencies
59+
#####
60+
function ᶠremaining_tendency(ᶜY, ᶠY, p, t)
61+
names = propertynames(ᶠY)
62+
tends = construct_tendencies(Val(names), ᶠremaining_tendency, ᶜY, ᶠY, p, t)
63+
# We cannot broadcast over a NamedTuple, so we need to check that edge case
64+
# first.
65+
if all(t -> !(t isa Base.Broadcast.Broadcasted), tends)
66+
return make_named_tuple(Val(names), tends...)
67+
else
68+
return lazy.(make_named_tuple.(Val(names), tends...))
69+
end
70+
end
71+
72+
#####
73+
##### Individual tendencies
74+
#####
75+
76+
function ᶜremaining_tendency(::Val{:ρ}, ᶜY, ᶠY, p, t)
77+
∑tendencies = zero(eltype(ᶜY.ρ))
78+
return ∑tendencies
79+
end
80+
function ᶜremaining_tendency(::Val{:uₕ}, ᶜY, ᶠY, p, t)
81+
∑tendencies = zero(eltype(ᶜY.uₕ))
82+
return ∑tendencies
83+
end
84+
function ᶜremaining_tendency(::Val{:ρe_tot}, ᶜY, ᶠY, p, t)
85+
∑tendencies = zero(eltype(ᶜY.ρe_tot))
86+
return ∑tendencies
87+
end
88+
function ᶜremaining_tendency(::Val{:ρq_tot}, ᶜY, ᶠY, p, t)
89+
∑tendencies = zero(eltype(ᶜY.ρq_tot))
90+
return ∑tendencies
91+
end
92+
function ᶜremaining_tendency(::Val{:ρq_liq}, ᶜY, ᶠY, p, t)
93+
∑tendencies = zero(eltype(ᶜY.ρq_liq))
94+
return ∑tendencies
95+
end
96+
function ᶜremaining_tendency(::Val{:ρq_ice}, ᶜY, ᶠY, p, t)
97+
∑tendencies = zero(eltype(ᶜY.ρq_ice))
98+
return ∑tendencies
99+
end
100+
function ᶜremaining_tendency(::Val{:ρn_liq}, ᶜY, ᶠY, p, t)
101+
∑tendencies = zero(eltype(ᶜY.ρn_liq))
102+
return ∑tendencies
103+
end
104+
function ᶜremaining_tendency(::Val{:ρn_rai}, ᶜY, ᶠY, p, t)
105+
∑tendencies = zero(eltype(ᶜY.ρn_rai))
106+
return ∑tendencies
107+
end
108+
function ᶜremaining_tendency(::Val{:ρq_rai}, ᶜY, ᶠY, p, t)
109+
∑tendencies = zero(eltype(ᶜY.ρq_rai))
110+
return ∑tendencies
111+
end
112+
function ᶜremaining_tendency(::Val{:ρq_sno}, ᶜY, ᶠY, p, t)
113+
∑tendencies = zero(eltype(ᶜY.ρq_sno))
114+
return ∑tendencies
115+
end
116+
function ᶜremaining_tendency(::Val{:sgsʲs}, ᶜY, ᶠY, p, t)
117+
∑tendencies = rzero(eltype(ᶜY.sgsʲs))
118+
return ∑tendencies
119+
end
120+
function ᶜremaining_tendency(::Val{:sgs⁰}, ᶜY, ᶠY, p, t)
121+
∑tendencies = rzero(eltype(ᶜY.sgs⁰))
122+
return ∑tendencies
123+
end
124+
function ᶠremaining_tendency(::Val{:u₃}, ᶜY, ᶠY, p, t)
125+
∑tendencies = zero(eltype(ᶠY.u₃))
126+
return ∑tendencies
127+
end
128+
function ᶠremaining_tendency(::Val{:sgsʲs}, ᶜY, ᶠY, p, t)
129+
∑tendencies = rzero(eltype(ᶠY.sgsʲs))
130+
return ∑tendencies
131+
end
132+
40133
"""
41134
remaining_tendency!(Yₜ, Yₜ_lim, Y, p, t)
42135
@@ -64,7 +157,27 @@ Returns:
64157
"""
65158
NVTX.@annotate function remaining_tendency!(Yₜ, Yₜ_lim, Y, p, t)
66159
Yₜ_lim .= zero(eltype(Yₜ_lim))
67-
Yₜ .= zero(eltype(Yₜ))
160+
device = ClimaComms.device(axes(Y.c))
161+
p_kernel = (;
162+
zmax = Spaces.z_max(axes(Y.f)),
163+
atmos = p.atmos,
164+
params = p.params,
165+
dt = p.dt,
166+
)
167+
if :sfc in propertynames(Y) # columnwise! does not yet handle .sfc
168+
parent(Yₜ.sfc) .= zero(Spaces.undertype(axes(Y.c)))
169+
end
170+
Operators.columnwise!(
171+
device,
172+
ᶜremaining_tendency,
173+
ᶠremaining_tendency,
174+
Yₜ.c,
175+
Yₜ.f,
176+
Y.c,
177+
Y.f,
178+
p_kernel,
179+
t,
180+
)
68181
horizontal_tracer_advection_tendency!(Yₜ_lim, Y, p, t)
69182
fill_with_nans!(p) # TODO: would be better to limit this to debug mode (e.g., if p.debug_mode...)
70183
horizontal_dynamics_tendency!(Yₜ, Y, p, t)

src/utils/utilities.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,69 @@ function issphere(space)
496496
return Meshes.domain(Spaces.topology(Spaces.horizontal_space(space))) isa
497497
Domains.SphereDomain
498498
end
499+
500+
501+
"""
502+
construct_tendencies(::Val{names}, f, ᶜY, ᶠY, p, t)
503+
504+
Return a tuple of calls to
505+
506+
```
507+
f(Val(name), ᶜY, ᶠY, p, t)
508+
```
509+
for all names in `names`.
510+
511+
For example, `f(Val((:a, :b)), ᶜY, ᶠY, p, t)` will return:
512+
513+
```
514+
(
515+
f(Val(:a), ᶜY, ᶠY, p, t),
516+
f(Val(:b), ᶜY, ᶠY, p, t),
517+
)
518+
```
519+
"""
520+
@generated function construct_tendencies(
521+
::Val{names},
522+
f,
523+
ᶜY,
524+
ᶠY,
525+
p,
526+
t,
527+
) where {names}
528+
calls = []
529+
for name in names
530+
push!(calls, :(f(Val($(QuoteNode(name))), ᶜY, ᶠY, p, t)))
531+
end
532+
return quote
533+
($(calls...),)
534+
end
535+
end
536+
537+
"""
538+
make_named_tuple(::Val{names}, vals...) where {names}
539+
540+
Construct a NamedTuple given the names `names` and values `vals`.
541+
"""
542+
make_named_tuple(::Val{names}, vals...) where {names} = NamedTuple{names}(vals)
543+
544+
"""
545+
add_tend(∑tendencies, tendency)
546+
547+
A helper function which returns `∑tendencies` when `tendency` is a
548+
`NullBroadcasted` and `lazy.(∑tendencies + tendency)` when `tendency` is not a
549+
`NullBroadcasted`.
550+
"""
551+
function add_tend end
552+
add_tend(∑tends, t) = lazy.(∑tends .+ t)
553+
add_tend(∑tends, ::NullBroadcasted) = ∑tends
554+
555+
"""
556+
sub_tend(∑tendencies, tendency)
557+
558+
A helper function which returns `∑tendencies` when `tendency` is a
559+
`NullBroadcasted` and `lazy.(∑tendencies - tendency)` when `tendency` is not a
560+
`NullBroadcasted`.
561+
"""
562+
function sub_tend end
563+
sub_tend(∑tends, ::NullBroadcasted) = ∑tends
564+
sub_tend(∑tends, t) = lazy.(∑tends .- t)

0 commit comments

Comments
 (0)