Skip to content

Commit 7437cef

Browse files
Merge pull request #3732 from CliMA/dy/zero_velocity
Fix Jacobian for zero_velocity_tendency
2 parents 9507811 + b8e6a95 commit 7437cef

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

src/prognostic_equations/implicit/implicit_solver.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@ NVTX.@annotate function Wfact!(A, Y, p, dtγ, t)
550550
dtγ′ = FT(float(dtγ))
551551

552552
A.dtγ_ref[] = dtγ′
553-
update_implicit_equation_jacobian!(A, Y, p′, dtγ′)
553+
update_implicit_equation_jacobian!(A, Y, p′, dtγ′, t)
554554
end
555555

556-
function update_implicit_equation_jacobian!(A, Y, p, dtγ)
556+
function update_implicit_equation_jacobian!(A, Y, p, dtγ, t)
557557
dtγ = float(dtγ)
558558
(;
559559
matrix,
@@ -1148,4 +1148,7 @@ function update_implicit_equation_jacobian!(A, Y, p, dtγ)
11481148
) - (I_u₃,)
11491149
end
11501150
end
1151+
1152+
# NOTE: All velocity tendency derivatives should be set BEFORE this call.
1153+
zero_velocity_jacobian!(matrix, Y, p, t)
11511154
end

src/prognostic_equations/implicit/implicit_tendency.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ NVTX.@annotate function implicit_tendency!(Yₜ, Y, p, t)
4646
# NOTE: All ρa tendencies should be applied before calling this function
4747
pressure_work_tendency!(Yₜ, Y, p, t, p.atmos.turbconv_model)
4848

49-
# NOTE: This will zero out all monmentum tendencies in the edmfx advection test
49+
# NOTE: This will zero out all momentum tendencies in the edmfx advection test
5050
# please DO NOT add additional velocity tendencies after this function
5151
zero_velocity_tendency!(Yₜ, Y, p, t)
5252

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1+
import ClimaCore: MatrixFields
2+
import LinearAlgebra: UniformScaling
3+
14
###
25
### edmfx advection test
36
###
47

8+
# Turn off all momentum tendencies in the advection test.
59
function zero_velocity_tendency!(Yₜ, Y, p, t)
6-
# turn off all monmentum tendencies in the advection test
7-
if p.atmos.advection_test
8-
FT = eltype(Y)
9-
n = n_mass_flux_subdomains(p.atmos.turbconv_model)
10+
p.atmos.advection_test || return nothing
11+
@. Yₜ.c.uₕ = C12(0, 0)
12+
@. Yₜ.f.u₃ = C3(0)
13+
if p.atmos.turbconv_model isa PrognosticEDMFX
14+
for j in 1:n_mass_flux_subdomains(p.atmos.turbconv_model)
15+
@. Yₜ.f.sgsʲs.:($$j).u₃ = C3(0)
16+
end
17+
end
18+
return nothing
19+
end
1020

11-
@. Yₜ.c.uₕ = C12(FT(0), FT(0))
12-
@. Yₜ.f.u₃ = Geometry.Covariant3Vector(FT(0))
21+
# Turn off all momentum tendency derivatives in the advection test.
22+
function zero_velocity_jacobian!(∂Yₜ_err_∂Y, Y, p, t)
23+
p.atmos.advection_test || return nothing
24+
for ((row_name, col_name), matrix_entry) in pairs(∂Yₜ_err_∂Y)
25+
matrix_entry isa Fields.Field || continue
26+
if row_name in (MatrixFields.@name(c.uₕ), MatrixFields.@name(f.u₃))
27+
set_identity_matrix_entry!(matrix_entry, row_name, col_name)
28+
end
1329
if p.atmos.turbconv_model isa PrognosticEDMFX
14-
for j in 1:n
15-
@. Yₜ.f.sgsʲs.:($$j).u₃ = Geometry.Covariant3Vector(FT(0))
30+
for j in 1:n_mass_flux_subdomains(p.atmos.turbconv_model)
31+
if row_name == MatrixFields.FieldName(:f, :sgsʲs, j, :u₃)
32+
set_identity_matrix_entry!(matrix_entry, row_name, col_name)
33+
end
1634
end
1735
end
1836
end
19-
return nothing
37+
end
38+
39+
function set_identity_matrix_entry!(matrix_entry, row_name, col_name)
40+
identity_matrix_entry_value = if row_name == col_name
41+
# TODO: Add a method for one(::Axis2Tensor) to simplify this.
42+
T = eltype(eltype(matrix_entry))
43+
tensor_data = UniformScaling(one(eltype(T)))
44+
-DiagonalMatrixRow(Geometry.AxisTensor(axes(T), tensor_data))
45+
else
46+
zero(eltype(matrix_entry))
47+
end
48+
matrix_entry .= (identity_matrix_entry_value,)
2049
end

0 commit comments

Comments
 (0)