Skip to content

Commit a85ba64

Browse files
authored
Merge pull request #4139 from CliMA/zs/dpdqt
fix jacobian terms related to dpdqt
2 parents 4fbc052 + f1149d0 commit a85ba64

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

reproducibility_tests/ref_counter.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
282
1+
283
22

33
# **README**
44
#
@@ -20,6 +20,9 @@
2020

2121

2222
#=
23+
283
24+
- Change the Jacobian terms related to dp_drhoq_tot
25+
2326
282
2427
- Use ClimaCore.CommonSpaces constructors for Atmos spaces
2528

src/prognostic_equations/implicit/manual_sparse_jacobian.jl

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,19 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
412412
Δcv_v = FT(CAP.cv_v(params)) - cv_d
413413
T_0 = FT(CAP.T_0(params))
414414
R_d = FT(CAP.R_d(params))
415-
ΔR_v = FT(CAP.R_v(params)) - R_d
415+
R_v = FT(CAP.R_v(params))
416+
ΔR_v = R_v - R_d
416417
cp_d = FT(CAP.cp_d(params))
417418
Δcp_v = FT(CAP.cp_v(params)) - cp_d
419+
e_int_v0 = FT(CAP.e_int_v0(params))
418420
LH_v0 = FT(CAP.LH_v0(params))
419421
LH_s0 = FT(CAP.LH_s0(params))
420-
R_v = FT(CAP.R_v(params))
421422
Δcp_l = FT(CAP.cp_l(params) - CAP.cp_v(params))
422423
Δcp_i = FT(CAP.cp_i(params) - CAP.cp_v(params))
423424
# This term appears a few times in the Jacobian, and is technically
424425
# minus ∂e_int_∂q_tot
425426
thermo_params = CAP.thermodynamics_params(params)
426-
∂e_int_∂q_tot = T_0 * (Δcv_v - R_d) - FT(CAP.e_int_v0(params))
427+
∂e_int_∂q_tot = T_0 * (Δcv_v - R_d) - e_int_v0
427428
ᶜh_tot = @. lazy(
428429
TD.total_specific_enthalpy(
429430
thermo_params,
@@ -446,13 +447,9 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
446447
@. ᶜkappa_m =
447448
TD.gas_constant_air(thermo_params, ᶜts) / TD.cv_m(thermo_params, ᶜts)
448449

449-
ᶜ∂kappa_m∂q_tot = p.scratch.ᶜtemp_scalar_2
450-
# Using abs2 because ^2 results in allocation
451-
@. ᶜ∂kappa_m∂q_tot =
452-
(
453-
ΔR_v * TD.cv_m(thermo_params, ᶜts) -
454-
Δcv_v * TD.gas_constant_air(thermo_params, ᶜts)
455-
) / abs2(TD.cv_m(thermo_params, ᶜts))
450+
T = @. lazy(TD.air_temperature(thermo_params, ᶜts))
451+
ᶜ∂p∂ρq_tot = p.scratch.ᶜtemp_scalar_2
452+
@. ᶜ∂p∂ρq_tot = ᶜkappa_m * (-e_int_v0 - R_d * T_0 - Δcv_v * (T - T_0)) + ΔR_v * T
456453

457454
if use_derivative(topography_flag)
458455
@. ∂ᶜK_∂ᶜuₕ = DiagonalMatrixRow(
@@ -527,15 +524,12 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
527524
)
528525
@. ∂ᶠu₃_err_∂ᶜρe_tot = dtγ * ᶠp_grad_matrix DiagonalMatrixRow(ᶜkappa_m)
529526
ᶜe_tot = @. lazy(specific(Y.c.ρe_tot, Y.c.ρ))
527+
530528
if MatrixFields.has_field(Y, @name(c.ρq_tot))
531529
ᶜq_tot = @. lazy(specific(Y.c.ρq_tot, Y.c.ρ))
532530
∂ᶠu₃_err_∂ᶜρq_tot = matrix[@name(f.u₃), @name(c.ρq_tot)]
533531
@. ∂ᶠu₃_err_∂ᶜρq_tot =
534-
dtγ * ᶠp_grad_matrix DiagonalMatrixRow((
535-
ᶜkappa_m * ∂e_int_∂q_tot +
536-
ᶜ∂kappa_m∂q_tot *
537-
(cp_d * T_0 + ᶜe_tot - ᶜK - ᶜΦ + ∂e_int_∂q_tot * ᶜq_tot)
538-
))
532+
dtγ * ᶠp_grad_matrix DiagonalMatrixRow(ᶜ∂p∂ρq_tot)
539533
end
540534

541535
∂ᶠu₃_err_∂ᶜuₕ = matrix[@name(f.u₃), @name(c.uₕ)]
@@ -690,11 +684,7 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
690684
∂ᶜρe_tot_err_∂ᶜρq_tot = matrix[@name(c.ρe_tot), @name(c.ρq_tot)]
691685
∂ᶜρq_tot_err_∂ᶜρ = matrix[@name(c.ρq_tot), @name(c.ρ)]
692686
@. ∂ᶜρe_tot_err_∂ᶜρq_tot +=
693-
dtγ * ᶜdiffusion_h_matrix DiagonalMatrixRow((
694-
ᶜkappa_m * ∂e_int_∂q_tot / ᶜρ +
695-
ᶜ∂kappa_m∂q_tot *
696-
(cp_d * T_0 + ᶜe_tot - ᶜK - ᶜΦ + ∂e_int_∂q_tot * ᶜq_tot)
697-
))
687+
dtγ * ᶜdiffusion_h_matrix DiagonalMatrixRow(ᶜ∂p∂ρq_tot / ᶜρ)
698688
@. ∂ᶜρq_tot_err_∂ᶜρ = zero(typeof(∂ᶜρq_tot_err_∂ᶜρ))
699689
@. ∂ᶜρq_tot_err_∂ᶜρq_tot +=
700690
dtγ * ᶜdiffusion_h_matrix DiagonalMatrixRow(1 / ᶜρ)
@@ -907,9 +897,8 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
907897

908898
turbconv_params = CAP.turbconv_params(params)
909899
α_b = CAP.pressure_normalmode_buoy_coeff1(turbconv_params)
910-
ᶜTʲ = p.scratch.ᶜtemp_scalar_2
911-
@. ᶜTʲ = TD.air_temperature(thermo_params, ᶜtsʲs.:(1))
912-
ᶜ∂RmT∂qʲ = p.scratch.ᶜtemp_scalar_3
900+
ᶜTʲ = @. lazy(TD.air_temperature(thermo_params, ᶜtsʲs.:(1)))
901+
ᶜ∂RmT∂qʲ = p.scratch.ᶜtemp_scalar_2
913902
sgs_microphysics_tracers =
914903
p.atmos.moisture_model isa NonEquilMoistModel && (
915904
p.atmos.microphysics_model isa Microphysics1Moment ||
@@ -1210,12 +1199,10 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
12101199
TD.gas_constant_air(thermo_params, ᶜts) /
12111200
TD.cv_m(thermo_params, ᶜts)
12121201

1213-
ᶜ∂kappa_m∂q_tot = p.scratch.ᶜtemp_scalar_2
1214-
@. ᶜ∂kappa_m∂q_tot =
1215-
(
1216-
ΔR_v * TD.cv_m(thermo_params, ᶜts) -
1217-
Δcv_v * TD.gas_constant_air(thermo_params, ᶜts)
1218-
) / abs2(TD.cv_m(thermo_params, ᶜts))
1202+
T = @. lazy(TD.air_temperature(thermo_params, ᶜts))
1203+
ᶜ∂p∂ρq_tot = p.scratch.ᶜtemp_scalar_2
1204+
@. ᶜ∂p∂ρq_tot =
1205+
ᶜkappa_m * (-e_int_v0 - R_d * T_0 - Δcv_v * (T - T_0)) + ΔR_v * T
12191206

12201207
ᶜq_tot = @. lazy(specific(Y.c.ρq_tot, Y.c.ρ))
12211208
@. ∂ᶜρe_tot_err_∂ᶜρ +=
@@ -1229,13 +1216,7 @@ function update_jacobian!(alg::ManualSparseJacobian, cache, Y, p, dtγ, t)
12291216

12301217
@. ∂ᶜρe_tot_err_∂ᶜρq_tot +=
12311218
p.scratch.ᶜtridiagonal_matrix_scalar
1232-
DiagonalMatrixRow((
1233-
ᶜkappa_m * ∂e_int_∂q_tot / ᶜρ +
1234-
ᶜ∂kappa_m∂q_tot * (
1235-
cp_d * T_0 + ᶜe_tot - ᶜK - ᶜΦ +
1236-
∂e_int_∂q_tot * ᶜq_tot
1237-
)
1238-
))
1219+
DiagonalMatrixRow(ᶜ∂p∂ρq_tot / ᶜρ)
12391220

12401221
@. ∂ᶜρe_tot_err_∂ᶜρe_tot +=
12411222
p.scratch.ᶜtridiagonal_matrix_scalar

0 commit comments

Comments
 (0)