Skip to content

Commit e30cd24

Browse files
Use LazyBroadcast for subsidence tendency
1 parent 0605cb9 commit e30cd24

File tree

3 files changed

+52
-62
lines changed

3 files changed

+52
-62
lines changed

src/prognostic_equations/forcing/subsidence.jl

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,35 @@ import ClimaCore.Spaces as Spaces
77
import ClimaCore.Fields as Fields
88
import ClimaCore.Operators as Operators
99

10-
#####
11-
##### No subsidence
12-
#####
13-
14-
subsidence_tendency!(Yₜ, Y, p, t, ::Nothing) = nothing
15-
1610
#####
1711
##### Subsidence
1812
#####
1913

20-
subsidence!(ᶜρχₜ, ᶜρ, ᶠu³, ᶜχ, ::Val{:none}) =
21-
@. ᶜρχₜ -= ᶜρ * (ᶜsubdivᵥ(ᶠu³ * ᶠinterp(ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))
22-
subsidence!(ᶜρχₜ, ᶜρ, ᶠu³, ᶜχ, ::Val{:first_order}) =
23-
@. ᶜρχₜ -= ᶜρ * (ᶜsubdivᵥ(ᶠupwind1(ᶠu³, ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))
24-
subsidence!(ᶜρχₜ, ᶜρ, ᶠu³, ᶜχ, ::Val{:third_order}) =
25-
@. ᶜρχₜ -= ᶜρ * (ᶜsubdivᵥ(ᶠupwind3(ᶠu³, ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))
26-
27-
function subsidence_tendency!(Yₜ, Y, p, t, ::Subsidence)
28-
(; moisture_model) = p.atmos
29-
subsidence_profile = p.atmos.subsidence.prof
30-
(; ᶜh_tot, ᶜspecific) = p.precomputed
31-
32-
ᶠz = Fields.coordinate_field(axes(Y.f)).z
33-
ᶠlg = Fields.local_geometry_field(Y.f)
34-
ᶠsubsidence³ = p.scratch.ᶠtemp_CT3
35-
@. ᶠsubsidence³ =
36-
subsidence_profile(ᶠz) * CT3(unit_basis_vector_data(CT3, ᶠlg))
37-
38-
# LS Subsidence
39-
subsidence!(Yₜ.c.ρe_tot, Y.c.ρ, ᶠsubsidence³, ᶜh_tot, Val{:first_order}())
40-
subsidence!(
41-
Yₜ.c.ρq_tot,
42-
Y.c.ρ,
43-
ᶠsubsidence³,
44-
ᶜspecific.q_tot,
45-
Val{:first_order}(),
46-
)
47-
if moisture_model isa NonEquilMoistModel
48-
subsidence!(
49-
Yₜ.c.ρq_liq,
50-
Y.c.ρ,
51-
ᶠsubsidence³,
52-
ᶜspecific.q_liq,
53-
Val{:first_order}(),
54-
)
55-
subsidence!(
56-
Yₜ.c.ρq_ice,
57-
Y.c.ρ,
58-
ᶠsubsidence³,
59-
ᶜspecific.q_ice,
60-
Val{:first_order}(),
61-
)
62-
end
14+
subsidence_tracer(::Val{:h_tot}, thermo_params, ts, ρ, ρe_tot) =
15+
TD.total_specific_enthalpy(thermo_params, ts, ρe_tot / ρ)
16+
subsidence_tracer(::Val{:q_tot}, thermo_params, ts, ρ, ρe_tot) =
17+
TD.total_specific_humidity(thermo_params, ts)
18+
subsidence_tracer(::Val{:q_liq}, thermo_params, ts, ρ, ρe_tot) =
19+
TD.liquid_specific_humidity(thermo_params, ts)
20+
subsidence_tracer(::Val{:q_ice}, thermo_params, ts, ρ, ρe_tot) =
21+
TD.ice_specific_humidity(thermo_params, ts)
22+
23+
function subsidence_tendency(χname, subsidence, thermo_params, ᶜts, ᶜρ, ᶜρe_tot)
24+
subsidence isa Nothing && return NullBroadcasted()
25+
ᶠu³ = ᶠsubsidence³(axes(ᶜρ), subsidence)
26+
ᶜχ = @lazy @. subsidence_tracer(χname, thermo_params, ᶜts, ᶜρ, ᶜρe_tot)
27+
return subsidence_tendency(ᶜρ, ᶜχ, ᶠu³, Val{:first_order}())
28+
end
6329

64-
return nothing
30+
function ᶠsubsidence³(ᶠspace, subsidence::Subsidence)
31+
ᶠz = Fields.coordinate_field(ᶠspace).z
32+
ᶠlg = Fields.local_geometry_field(ᶠspace)
33+
(; prof) = subsidence
34+
return @lazy @. prof(ᶠz) * CT3(unit_basis_vector_data(CT3, ᶠlg))
6535
end
36+
subsidence_tendency(ᶜρ, ᶜχ, ᶠu³, ::Val{:none}) =
37+
@lazy @. - ᶜρ * (ᶜsubdivᵥ(ᶠu³ * ᶠinterp(ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))
38+
subsidence_tendency(ᶜρ, ᶜχ, ᶠu³, ::Val{:first_order}) =
39+
@lazy @. - ᶜρ * (ᶜsubdivᵥ(ᶠupwind1(ᶠu³, ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))
40+
subsidence_tendency(ᶜρ, ᶜχ, ᶠu³, ::Val{:third_order}) =
41+
@lazy @. - ᶜρ * (ᶜsubdivᵥ(ᶠupwind3(ᶠu³, ᶜχ)) - ᶜχ * ᶜsubdivᵥ(ᶠu³))

src/prognostic_equations/remaining_tendency.jl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ NVTX.@annotate function additional_tendency!(Yₜ, Y, p, t)
4040
ᶜuₕ = Y.c.uₕ
4141
ᶠu₃ = Yₜ.f.u₃
4242
ᶜρ = Y.c.ρ
43+
ᶜρe_tot = Y.c.ρe_tot
4344
(; forcing_type, moisture_model, rayleigh_sponge, viscous_sponge) = p.atmos
44-
(; edmf_coriolis) = p.atmos
45+
(; subsidence, edmf_coriolis) = p.atmos
4546
(; params) = p
46-
(; ᶜp, sfc_conditions) = p.precomputed
47+
thermo_params = CAP.thermodynamics_params(params)
48+
(; ᶜp, sfc_conditions, ᶜts) = p.precomputed
49+
ᶠspace = axes(Y.f)
50+
ᶠz = Fields.coordinate_field(ᶠspace).z
51+
ᶠlg = Fields.local_geometry_field(ᶠspace)
4752

4853
vst_uₕ = viscous_sponge_tendency_uₕ(ᶜuₕ, viscous_sponge)
4954
vst_u₃ = viscous_sponge_tendency_u₃(ᶠu₃, viscous_sponge)
@@ -52,6 +57,7 @@ NVTX.@annotate function additional_tendency!(Yₜ, Y, p, t)
5257
hs_args = (ᶜuₕ, ᶜp, params, sfc_conditions.ts, moisture_model, forcing_type)
5358
hs_tendency_uₕ = held_suarez_forcing_tendency_uₕ(hs_args...)
5459
hs_tendency_ρe_tot = held_suarez_forcing_tendency_ρe_tot(ᶜρ, hs_args...)
60+
subs_args = (subsidence, thermo_params, ᶜts, ᶜρ, ᶜρe_tot)
5561
edmf_cor_tend_uₕ = edmf_coriolis_tendency_uₕ(ᶜuₕ, edmf_coriolis)
5662

5763
# TODO: fuse, once we fix
@@ -62,20 +68,27 @@ NVTX.@annotate function additional_tendency!(Yₜ, Y, p, t)
6268
@. Yₜ.c.ρe_tot += vst_ρe_tot
6369

6470
# TODO: can we write this out explicitly?
65-
if viscous_sponge isa ViscousSponge
66-
for (ᶜρχₜ, ᶜχ, χ_name) in matching_subfields(Yₜ.c, ᶜspecific)
67-
χ_name == :e_tot && continue
68-
vst_tracer = viscous_sponge_tendency_tracer(ᶜρ, ᶜχ, viscous_sponge)
69-
@. ᶜρχₜ += vst_tracer
70-
@. Yₜ.c.ρ += vst_tracer
71-
end
71+
for (ᶜρχₜ, ᶜχ, χ_name) in matching_subfields(Yₜ.c, ᶜspecific)
72+
χ_name == :e_tot && continue
73+
vst_tracer = viscous_sponge_tendency_tracer(ᶜρ, ᶜχ, viscous_sponge)
74+
@. ᶜρχₜ += vst_tracer
75+
@. Yₜ.c.ρ += vst_tracer
7276
end
7377

7478
# Held Suarez tendencies
7579
@. Yₜ.c.uₕ += hs_tendency_uₕ
7680
@. Yₜ.c.ρe_tot += hs_tendency_ρe_tot
7781

78-
subsidence_tendency!(Yₜ, Y, p, t, p.atmos.subsidence)
82+
if moisture_model isa AbstractMoistModel
83+
bc_subsidence_ρq_tot = subsidence_tendency(Val(:q_tot), subs_args...)
84+
@. Yₜ.c.ρq_tot += bc_subsidence_ρq_tot
85+
end
86+
if moisture_model isa NonEquilMoistModel
87+
bc_subsidence_ρq_liq = subsidence_tendency(Val(:q_liq), subs_args...)
88+
bc_subsidence_ρq_ice = subsidence_tendency(Val(:q_ice), subs_args...)
89+
@. Yₜ.c.ρq_liq += bc_subsidence_ρq_liq
90+
@. Yₜ.c.ρq_ice += bc_subsidence_ρq_ice
91+
end
7992

8093
@. Yₜ.c.uₕ += edmf_cor_tend_uₕ
8194

src/solver/types.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import ClimaUtilities.ClimaArtifacts: @clima_artifact
77
import LazyArtifacts
88

99
abstract type AbstractMoistureModel end
10+
abstract type AbstractMoistModel <: AbstractMoistureModel end
1011
struct DryModel <: AbstractMoistureModel end
11-
struct EquilMoistModel <: AbstractMoistureModel end
12-
struct NonEquilMoistModel <: AbstractMoistureModel end
12+
struct EquilMoistModel <: AbstractMoistModel end
13+
struct NonEquilMoistModel <: AbstractMoistModel end
1314

1415
abstract type AbstractPrecipitationModel end
1516
struct NoPrecipitation <: AbstractPrecipitationModel end

0 commit comments

Comments
 (0)