Skip to content

Commit c06dd92

Browse files
dennisYatunincharleskawczynski
authored andcommitted
Fix hard-coded ARS343, simplify code and add comments
1 parent 1f6e259 commit c06dd92

File tree

3 files changed

+48
-62
lines changed

3 files changed

+48
-62
lines changed

src/solvers/hard_coded_ars343.jl

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ function step_u!(integrator, cache::IMEXARKCache, ::ARS343)
1010
(; U, T_lim, T_exp, T_imp, temp, γ, newtons_method_cache) = cache
1111
T_lim! = !isnothing(f.T_lim!) ? f.T_lim! : (args...) -> nothing
1212
T_exp! = !isnothing(f.T_exp!) ? f.T_exp! : (args...) -> nothing
13+
dtγ = dt * γ
1314

1415
if !isnothing(newtons_method_cache)
1516
jacobian = newtons_method_cache.j
1617
if (!isnothing(jacobian)) && needs_update!(newtons_method.update_j, NewTimeStep(t))
17-
T_imp!.Wfact(jacobian, u, p, dt * γ, t)
18+
T_imp!.Wfact(jacobian, u, p, dtγ, t)
1819
end
1920
end
2021

@@ -36,29 +37,24 @@ function step_u!(integrator, cache::IMEXARKCache, ::ARS343)
3637
let i = i
3738
t_imp = t + dt * c_imp[i]
3839
cache_imp!(U, p, t_imp)
39-
implicit_equation_residual! = (residual, Ui) -> begin
40-
T_imp!(residual, Ui, p, t_imp)
41-
@. residual = temp + dt * a_imp[i, i] * residual - Ui
40+
implicit_equation_residual! = (residual, U′) -> begin
41+
T_imp!(residual, U′, p, t_imp)
42+
@. residual = temp + dtγ * residual - U′
4243
end
43-
implicit_equation_jacobian! = (jacobian, Ui) -> begin
44-
T_imp!.Wfact(jacobian, Ui, p, dt * a_imp[i, i], t_imp)
45-
end
46-
call_cache_imp! = Ui -> cache_imp!(Ui, p, t_imp)
4744
solve_newton!(
4845
newtons_method,
4946
newtons_method_cache,
5047
U,
5148
implicit_equation_residual!,
52-
implicit_equation_jacobian!,
53-
call_cache_imp!,
54-
nothing,
49+
(jacobian, U′) -> T_imp!.Wfact(jacobian, U′, p, dtγ, t_imp),
50+
U′ -> cache_imp!(U′, p, t_imp),
5551
)
56-
@. T_imp[i] = (U - temp) / (dt * a_imp[i, i])
5752
dss!(U, p, t_imp)
5853
cache!(U, p, t_imp)
5954
end
6055
T_lim!(T_lim[i], U, p, t_exp)
6156
T_exp!(T_exp[i], U, p, t_exp)
57+
@. T_imp[i] = (U - temp) / dtγ
6258

6359
i = 3
6460
t_exp = t + dt * c_exp[i]
@@ -70,29 +66,24 @@ function step_u!(integrator, cache::IMEXARKCache, ::ARS343)
7066
let i = i
7167
t_imp = t + dt * c_imp[i]
7268
cache_imp!(U, p, t_imp)
73-
implicit_equation_residual! = (residual, Ui) -> begin
74-
T_imp!(residual, Ui, p, t_imp)
75-
@. residual = temp + dt * a_imp[i, i] * residual - Ui
76-
end
77-
implicit_equation_jacobian! = (jacobian, Ui) -> begin
78-
T_imp!.Wfact(jacobian, Ui, p, dt * a_imp[i, i], t_imp)
69+
implicit_equation_residual! = (residual, U′) -> begin
70+
T_imp!(residual, U′, p, t_imp)
71+
@. residual = temp + dtγ * residual - U′
7972
end
80-
call_cache_imp! = Ui -> cache_imp!(Ui, p, t_imp)
8173
solve_newton!(
8274
newtons_method,
8375
newtons_method_cache,
8476
U,
8577
implicit_equation_residual!,
86-
implicit_equation_jacobian!,
87-
call_cache_imp!,
88-
nothing,
78+
(jacobian, U′) -> T_imp!.Wfact(jacobian, U′, p, dtγ, t_imp),
79+
U′ -> cache_imp!(U′, p, t_imp),
8980
)
90-
@. T_imp[i] = (U - temp) / (dt * a_imp[i, i])
9181
dss!(U, p, t_imp)
9282
cache!(U, p, t_imp)
9383
end
9484
T_lim!(T_lim[i], U, p, t_exp)
9585
T_exp!(T_exp[i], U, p, t_exp)
86+
@. T_imp[i] = (U - temp) / dtγ
9687

9788
i = 4
9889
t_exp = t + dt
@@ -109,29 +100,24 @@ function step_u!(integrator, cache::IMEXARKCache, ::ARS343)
109100
let i = i
110101
t_imp = t + dt * c_imp[i]
111102
cache_imp!(U, p, t_imp)
112-
implicit_equation_residual! = (residual, Ui) -> begin
113-
T_imp!(residual, Ui, p, t_imp)
114-
@. residual = temp + dt * a_imp[i, i] * residual - Ui
115-
end
116-
implicit_equation_jacobian! = (jacobian, Ui) -> begin
117-
T_imp!.Wfact(jacobian, Ui, p, dt * a_imp[i, i], t_imp)
103+
implicit_equation_residual! = (residual, U′) -> begin
104+
T_imp!(residual, U′, p, t_imp)
105+
@. residual = temp + dtγ * residual - U′
118106
end
119-
call_cache_imp! = Ui -> cache_imp!(Ui, p, t_imp)
120107
solve_newton!(
121108
newtons_method,
122109
newtons_method_cache,
123110
U,
124111
implicit_equation_residual!,
125-
implicit_equation_jacobian!,
126-
call_cache_imp!,
127-
nothing,
112+
(jacobian, U′) -> T_imp!.Wfact(jacobian, U′, p, dtγ, t_imp),
113+
U′ -> cache_imp!(U′, p, t_imp),
128114
)
129-
@. T_imp[i] = (U - temp) / (dt * a_imp[i, i])
130115
dss!(U, p, t_imp)
131116
cache!(U, p, t_imp)
132117
end
133118
T_lim!(T_lim[i], U, p, t_exp)
134119
T_exp!(T_exp[i], U, p, t_exp)
120+
@. T_imp[i] = (U - temp) / dtγ
135121

136122
t_final = t + dt
137123
@. temp = u + dt * b_exp[2] * T_lim[2] + dt * b_exp[3] * T_lim[3] + dt * b_exp[4] * T_lim[4]

src/solvers/imex_ark.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,28 @@ end
120120
has_T_exp(f) && fused_increment!(U, dt, a_exp, T_exp, Val(i))
121121
isnothing(T_imp!) || fused_increment!(U, dt, a_imp, T_imp, Val(i))
122122

123+
# Run the implicit solver, apply DSS, and update the cache. When γ == 0,
124+
# the implicit solver does not need to be run. On stage i == 1, we do not
125+
# need to apply DSS and update the cache because we did that at the end of
126+
# the previous timestep.
123127
i 1 && dss!(U, p, t_exp)
124-
125128
if isnothing(T_imp!) || iszero(a_imp[i, i])
126129
i 1 && cache!(U, p, t_exp)
127-
else # Implicit solve
130+
else
128131
@assert !isnothing(newtons_method)
129132
i 1 && cache_imp!(U, p, t_imp)
130133
@. temp = U
131-
implicit_equation_residual! = (residual, Ui) -> begin
132-
T_imp!(residual, Ui, p, t_imp)
133-
@. residual = temp + dtγ * residual - Ui
134-
end
135-
implicit_equation_jacobian! = (jacobian, Ui) -> begin
136-
T_imp!.Wfact(jacobian, Ui, p, dtγ, t_imp)
134+
implicit_equation_residual! = (residual, U′) -> begin
135+
T_imp!(residual, U′, p, t_imp)
136+
@. residual = temp + dtγ * residual - U′
137137
end
138-
implicit_equation_cache! = Ui -> cache_imp!(Ui, p, t_imp)
139138
solve_newton!(
140139
newtons_method,
141140
newtons_method_cache,
142141
U,
143142
implicit_equation_residual!,
144-
implicit_equation_jacobian!,
145-
implicit_equation_cache!,
143+
(jacobian, U′) -> T_imp!.Wfact(jacobian, U′, p, dtγ, t_imp),
144+
U′ -> cache_imp!(U′, p, t_imp),
146145
)
147146
dss!(U, p, t_imp)
148147
cache!(U, p, t_imp)
@@ -155,11 +154,12 @@ end
155154
end
156155
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
157156
if iszero(a_imp[i, i])
158-
# If its coefficient is 0, T_imp[i] is being treated explicitly.
157+
# When γ == 0, T_imp[i] is treated explicitly.
159158
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
160159
else
161-
# If T_imp[i] is being treated implicitly, ensure that it
162-
# exactly satisfies the implicit equation after applying DSS.
160+
# When γ != 0, T_imp[i] is treated implicitly, so it must satisfy
161+
# the implicit equation. To ensure that T_imp[i] only includes the
162+
# effect of applying DSS to T_imp!, U and temp must be DSSed.
163163
isnothing(T_imp!) || @. T_imp[i] = (U - temp) / dtγ
164164
end
165165
end

src/solvers/imex_ssprk.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,28 @@ function step_u!(integrator, cache::IMEXSSPRKCache)
103103
end
104104
end
105105

106+
# Run the implicit solver, apply DSS, and update the cache. When γ == 0,
107+
# the implicit solver does not need to be run. On stage i == 1, we do
108+
# not need to apply DSS and update the cache because we did that at the
109+
# end of the previous timestep.
106110
i 1 && dss!(U, p, t_exp)
107-
108111
if isnothing(T_imp!) || iszero(a_imp[i, i])
109112
i 1 && cache!(U, p, t_exp)
110-
else # Implicit solve
113+
else
111114
@assert !isnothing(newtons_method)
112115
i 1 && cache_imp!(U, p, t_imp)
113116
@. temp = U
114-
implicit_equation_residual! = (residual, Ui) -> begin
115-
T_imp!(residual, Ui, p, t_imp)
116-
@. residual = temp + dtγ * residual - Ui
117-
end
118-
implicit_equation_jacobian! = (jacobian, Ui) -> begin
119-
T_imp!.Wfact(jacobian, Ui, p, dtγ, t_imp)
117+
implicit_equation_residual! = (residual, U′) -> begin
118+
T_imp!(residual, U′, p, t_imp)
119+
@. residual = temp + dtγ * residual - U′
120120
end
121-
implicit_equation_cache! = Ui -> cache_imp!(Ui, p, t_imp)
122121
solve_newton!(
123122
newtons_method,
124123
newtons_method_cache,
125124
U,
126125
implicit_equation_residual!,
127-
implicit_equation_jacobian!,
128-
implicit_equation_cache!,
126+
(jacobian, U′) -> T_imp!.Wfact(jacobian, U′, p, dtγ, t_imp),
127+
U′ -> cache_imp!(U′, p, t_imp),
129128
)
130129
dss!(U, p, t_imp)
131130
cache!(U, p, t_imp)
@@ -138,11 +137,12 @@ function step_u!(integrator, cache::IMEXSSPRKCache)
138137
end
139138
if !all(iszero, a_imp[:, i]) || !iszero(b_imp[i])
140139
if iszero(a_imp[i, i])
141-
# If its coefficient is 0, T_imp[i] is being treated explicitly.
140+
# When γ == 0, T_imp[i] is treated explicitly.
142141
isnothing(T_imp!) || T_imp!(T_imp[i], U, p, t_imp)
143142
else
144-
# If T_imp[i] is being treated implicitly, ensure that it
145-
# exactly satisfies the implicit equation after applying DSS.
143+
# When γ != 0, T_imp[i] is treated implicitly, so it must satisfy
144+
# the implicit equation. To ensure that T_imp[i] only includes the
145+
# effect of applying DSS to T_imp!, U and temp must be DSSed.
146146
isnothing(T_imp!) || @. T_imp[i] = (U - temp) / dtγ
147147
end
148148
end

0 commit comments

Comments
 (0)