Skip to content

Commit 419f7fd

Browse files
authored
Merge pull request #2463 from CliMA/ts/thermo-update
Update Thermodynamics API usage
2 parents e10ed3c + b10ccbe commit 419f7fd

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

benchmarks/scripts/thermo_bench.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ using JET
3838
import ClimaCore: Spaces, Fields
3939
import ClimaCore.Domains: Geometry
4040

41+
@inline function make_thermo_state(thermo_params, ρ, e_int, q_tot)
42+
sa = TD.saturation_adjustment(thermo_params, TD.ρe(), ρ, e_int, q_tot; maxiter = 3)
43+
p = TD.air_pressure(thermo_params, sa.T, ρ, q_tot, sa.q_liq, sa.q_ice)
44+
return (; ρ = ρ, p = p, T = sa.T, e_int = e_int, q_tot = q_tot)
45+
end
4146
@inline ts_gs(thermo_params, e_tot, q_tot, K, Φ, ρ) =
42-
thermo_state(thermo_params, e_tot - K - Φ, q_tot, ρ)
43-
@inline thermo_state(thermo_params, ρ, e_int, q_tot) =
44-
TD.PhaseEquil_ρeq(thermo_params,ρ,e_int,q_tot, 3, eltype(thermo_params)(0.003))
47+
make_thermo_state(thermo_params, ρ, e_tot - K - Φ, q_tot)
4548

4649
import Thermodynamics as TD
4750

@@ -115,7 +118,7 @@ using ClimaCore
115118
import ClimaCore: Spaces, Fields
116119
import ClimaCore.Domains: Geometry
117120

118-
ENV["CLIMACOMMS_DEVICE"] = "CUDA";
121+
ENV["CLIMACOMMS_DEVICE"] = get(ENV, "CLIMACOMMS_DEVICE", "CPU");
119122
ClimaComms.@import_required_backends
120123
using BenchmarkTools
121124
@isdefined(TU) || include(
@@ -149,7 +152,7 @@ using Test
149152
q_tot = FT(0),
150153
T = FT(0),
151154
)
152-
x = fill((; ts = zero(TD.PhaseEquil{FT}), nt_core...), cspace)
155+
x = fill((; ts = nt_ts, nt_core...), cspace)
153156
xv = fill((; ts = nt_ts, nt_core...), cspace)
154157
(_, Nij, _, Nv, Nh) = size(Fields.field_values(x.ts))
155158
us = TB.UniversalSizesStatic(Nv, Nij, Nh)

test/Operators/finitedifference/unit_columnwise.jl

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ function implicit_tendency_bc!(Yₜ, Y, p, t)
139139
return nothing
140140
end
141141

142-
function thermo_state(thermo_params, ᶜρ, ᶜρe_tot, ᶜK, grav, ᶜz)
143-
return @. lazy(
144-
TD.PhaseDry_ρe(thermo_params, ᶜρ, ᶜρe_tot / ᶜρ - ᶜK - Φ(grav, ᶜz)),
145-
)
146-
end
147142

148143
function ᶜimplicit_tendency_bc(ᶜY, ᶠY, p, t)
149144
(; rayleigh_sponge, dt, zmax, thermo_params) = p
@@ -158,10 +153,11 @@ function ᶜimplicit_tendency_bc(ᶜY, ᶠY, p, t)
158153
ᶠu₃ = ᶠY.u₃
159154

160155
ᶜK = compute_kinetic(ᶜuₕ, ᶠu₃)
161-
ᶜts = thermo_state(thermo_params, ᶜρ, ᶜρe_tot, ᶜK, grav, ᶜz)
162-
ᶜp = @. lazy(TD.air_pressure(thermo_params, ᶜts))
156+
ᶜe_int = @. lazy(ᶜρe_tot / ᶜρ - ᶜK - Φ(grav, ᶜz))
157+
ᶜT = @. lazy(TD.air_temperature(thermo_params, ᶜe_int))
158+
ᶜp = @. lazy(TD.air_pressure(thermo_params, ᶜT, ᶜρ))
163159
ᶜh_tot =
164-
@. lazy(TD.total_specific_enthalpy(thermo_params, ᶜts, ᶜρe_tot / ᶜρ))
160+
@. lazy(TD.total_enthalpy(thermo_params, ᶜρe_tot / ᶜρ, ᶜT))
165161
# Central advection of active tracers (e_tot and q_tot)
166162
ᶠuₕ³ = @. lazy(ᶠwinterp(ᶜρ * ᶜJ, CT3(ᶜuₕ)))
167163
ᶠu³ = @. lazy(ᶠuₕ³ + CT3(ᶠu₃))
@@ -183,8 +179,9 @@ function ᶠimplicit_tendency_bc(ᶜY, ᶠY, p, t)
183179
ᶜuₕ = ᶜY.uₕ
184180
ᶠu₃ = ᶠY.u₃
185181
ᶜK = compute_kinetic(ᶜuₕ, ᶠu₃)
186-
ᶜts = thermo_state(thermo_params, ᶜρ, ᶜρe_tot, ᶜK, grav, ᶜz)
187-
ᶜp = @. lazy(TD.air_pressure(thermo_params, ᶜts))
182+
ᶜe_int = @. lazy(ᶜρe_tot / ᶜρ - ᶜK - Φ(grav, ᶜz))
183+
ᶜT = @. lazy(TD.air_temperature(thermo_params, ᶜe_int))
184+
ᶜp = @. lazy(TD.air_pressure(thermo_params, ᶜT, ᶜρ))
188185
bc1 = @. lazy(-(ᶠgradᵥ(ᶜp) / ᶠinterp(ᶜρ) + ᶠgradᵥ(Φ(grav, ᶜz))))
189186
bc2 = @. lazy(-β_rayleigh_w(rayleigh_sponge, ᶠz, zmax) * ᶠu₃)
190187
return @. lazy(ᶠtendencies(bc1 + bc2))
@@ -199,7 +196,7 @@ end
199196

200197
function set_precomputed_quantities!(Y, p, t)
201198
(; thermo_params) = p
202-
(; ᶜu, ᶠu³, ᶠu, ᶜK, ᶜts, ᶜp) = p.precomputed
199+
(; ᶜu, ᶠu³, ᶠu, ᶜK, ᶜT, ᶜp) = p.precomputed
203200

204201
ᶜρ = Y.c.ρ
205202
ᶜuₕ = Y.c.uₕ
@@ -211,16 +208,13 @@ function set_precomputed_quantities!(Y, p, t)
211208
ᶠu³ .= compute_ᶠuₕ³(ᶜuₕ, ᶜρ) .+ CT3.(ᶠu₃)
212209
ᶜK .= compute_kinetic(ᶜuₕ, ᶠu₃)
213210

214-
@. ᶜts = TD.PhaseDry_ρe(
215-
thermo_params,
216-
Y.c.ρ,
217-
Y.c.ρe_tot / Y.c.ρ - ᶜK - Φ(grav, ᶜz),
218-
)
219-
@. ᶜp = TD.air_pressure(thermo_params, ᶜts)
211+
ᶜe_int = @. Y.c.ρe_tot / Y.c.ρ - ᶜK - Φ(grav, ᶜz)
212+
@. ᶜT = TD.air_temperature(thermo_params, ᶜe_int)
213+
@. ᶜp = TD.air_pressure(thermo_params, ᶜT, Y.c.ρ)
220214

221215
(; ᶜh_tot) = p.precomputed
222216
@. ᶜh_tot =
223-
TD.total_specific_enthalpy(thermo_params, ᶜts, Y.c.ρe_tot / Y.c.ρ)
217+
TD.total_enthalpy(thermo_params, Y.c.ρe_tot / Y.c.ρ, ᶜT)
224218
return nothing
225219
end
226220

@@ -300,7 +294,7 @@ precomputed = (;
300294
ᶠu³ = Fields.Field(CT3{FT}, ᶠspace),
301295
ᶜp = Fields.Field(FT, ᶜspace),
302296
ᶜK = Fields.Field(FT, ᶜspace),
303-
ᶜts = Fields.Field(TD.PhaseDry{FT}, ᶜspace),
297+
ᶜT = Fields.Field(FT, ᶜspace),
304298
ᶠu = Fields.Field(C123{FT}, ᶠspace),
305299
ᶜu = Fields.Field(C123{FT}, ᶜspace),
306300
)

0 commit comments

Comments
 (0)