Skip to content

Commit 73d15e1

Browse files
Merge pull request #356 from CliMA/dy/move_T_imp
Swap DSS and T_imp approximation
2 parents 7b367ab + 6e84816 commit 73d15e1

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ClimaTimeSteppers"
22
uuid = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
33
authors = ["Climate Modeling Alliance"]
4-
version = "0.8.0"
4+
version = "0.8.1"
55

66
[deps]
77
ClimaComms = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d"

src/solvers/imex_ark.jl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ end
102102
(; T_exp_T_lim!, T_lim!, T_exp!, T_imp!, lim!, dss!) = f
103103
(; tableau, newtons_method) = alg
104104
(; a_exp, b_exp, a_imp, b_imp, c_exp, c_imp) = tableau
105-
(; U, T_lim, T_exp, T_imp, temp, γ, newtons_method_cache) = cache
105+
(; U, T_lim, T_exp, T_imp, temp, newtons_method_cache) = cache
106106
s = length(b_exp)
107107

108108
t_exp = t + dt * c_exp[i]
109109
t_imp = t + dt * c_imp[i]
110+
dtγ = dt * a_imp[i, i]
110111

111112
if has_T_lim(f) # Update based on limited tendencies from previous stages
112113
assign_fused_increment!(U, u, dt, a_exp, T_lim, Val(i))
@@ -123,20 +124,16 @@ end
123124

124125
if isnothing(T_imp!) || iszero(a_imp[i, i])
125126
i 1 && cache!(U, p, t_exp)
126-
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
127-
# If its coefficient is 0, T_imp[i] is being treated explicitly.
128-
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
129-
end
130127
else # Implicit solve
131128
@assert !isnothing(newtons_method)
132129
i 1 && cache_imp!(U, p, t_imp)
133130
@. temp = U
134131
implicit_equation_residual! = (residual, Ui) -> begin
135132
T_imp!(residual, Ui, p, t_imp)
136-
@. residual = temp + dt * a_imp[i, i] * residual - Ui
133+
@. residual = temp + dtγ * residual - Ui
137134
end
138135
implicit_equation_jacobian! = (jacobian, Ui) -> begin
139-
T_imp!.Wfact(jacobian, Ui, p, dt * a_imp[i, i], t_imp)
136+
T_imp!.Wfact(jacobian, Ui, p, dtγ, t_imp)
140137
end
141138
implicit_equation_cache! = Ui -> cache_imp!(Ui, p, t_imp)
142139
solve_newton!(
@@ -147,21 +144,23 @@ end
147144
implicit_equation_jacobian!,
148145
implicit_equation_cache!,
149146
)
150-
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
151-
# If T_imp[i] is being treated implicitly, ensure that it
152-
# exactly satisfies the implicit equation before applying DSS.
153-
@. T_imp[i] = (U - temp) / (dt * a_imp[i, i])
154-
end
155147
dss!(U, p, t_imp)
156148
cache!(U, p, t_imp)
157149
end
158150

159151
if !all(iszero, a_exp[:, i]) || !iszero(b_exp[i])
160-
if !isnothing(T_exp_T_lim!)
161-
T_exp_T_lim!(T_exp[i], T_lim[i], U, p, t_exp)
152+
isnothing(T_exp_T_lim!) || T_exp_T_lim!(T_exp[i], T_lim[i], U, p, t_exp)
153+
isnothing(T_lim!) || T_lim!(T_lim[i], U, p, t_exp)
154+
isnothing(T_exp!) || T_exp!(T_exp[i], U, p, t_exp)
155+
end
156+
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
157+
if iszero(a_imp[i, i])
158+
# If its coefficient is 0, T_imp[i] is being treated explicitly.
159+
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
162160
else
163-
isnothing(T_lim!) || T_lim!(T_lim[i], U, p, t_exp)
164-
isnothing(T_exp!) || T_exp!(T_exp[i], U, p, t_exp)
161+
# If T_imp[i] is being treated implicitly, ensure that it
162+
# exactly satisfies the implicit equation after applying DSS.
163+
isnothing(T_imp!) || @. T_imp[i] = (U - temp) / dtγ
165164
end
166165
end
167166

src/solvers/imex_ssprk.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function step_u!(integrator, cache::IMEXSSPRKCache)
7979
for i in 1:s
8080
t_exp = t + dt * c_exp[i]
8181
t_imp = t + dt * c_imp[i]
82+
dtγ = dt * a_imp[i, i]
8283

8384
if i == 1
8485
@. U_exp = u
@@ -106,20 +107,16 @@ function step_u!(integrator, cache::IMEXSSPRKCache)
106107

107108
if isnothing(T_imp!) || iszero(a_imp[i, i])
108109
i 1 && cache!(U, p, t_exp)
109-
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
110-
# If its coefficient is 0, T_imp[i] is being treated explicitly.
111-
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
112-
end
113110
else # Implicit solve
114111
@assert !isnothing(newtons_method)
115112
i 1 && cache_imp!(U, p, t_imp)
116113
@. temp = U
117114
implicit_equation_residual! = (residual, Ui) -> begin
118115
T_imp!(residual, Ui, p, t_imp)
119-
@. residual = temp + dt * a_imp[i, i] * residual - Ui
116+
@. residual = temp + dtγ * residual - Ui
120117
end
121118
implicit_equation_jacobian! = (jacobian, Ui) -> begin
122-
T_imp!.Wfact(jacobian, Ui, p, dt * a_imp[i, i], t_imp)
119+
T_imp!.Wfact(jacobian, Ui, p, dtγ, t_imp)
123120
end
124121
implicit_equation_cache! = Ui -> cache_imp!(Ui, p, t_imp)
125122
solve_newton!(
@@ -130,21 +127,23 @@ function step_u!(integrator, cache::IMEXSSPRKCache)
130127
implicit_equation_jacobian!,
131128
implicit_equation_cache!,
132129
)
133-
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
134-
# If T_imp[i] is being treated implicitly, ensure that it
135-
# exactly satisfies the implicit equation before applying DSS.
136-
@. T_imp[i] = (U - temp) / (dt * a_imp[i, i])
137-
end
138130
dss!(U, p, t_imp)
139131
cache!(U, p, t_imp)
140132
end
141133

142134
if !iszero(β[i])
143-
if !isnothing(T_exp_T_lim!)
144-
T_exp_T_lim!(T_lim, T_exp, U, p, t_exp)
135+
isnothing(T_exp_T_lim!) || T_exp_T_lim!(T_lim, T_exp, U, p, t_exp)
136+
isnothing(T_lim!) || T_lim!(T_lim, U, p, t_exp)
137+
isnothing(T_exp!) || T_exp!(T_exp, U, p, t_exp)
138+
end
139+
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
140+
if iszero(a_imp[i, i])
141+
# If its coefficient is 0, T_imp[i] is being treated explicitly.
142+
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
145143
else
146-
isnothing(T_lim!) || T_lim!(T_lim, U, p, t_exp)
147-
isnothing(T_exp!) || T_exp!(T_exp, U, p, t_exp)
144+
# If T_imp[i] is being treated implicitly, ensure that it
145+
# exactly satisfies the implicit equation after applying DSS.
146+
isnothing(T_imp!) || @. T_imp[i] = (U - temp) / dtγ
148147
end
149148
end
150149
end

0 commit comments

Comments
 (0)