-
Notifications
You must be signed in to change notification settings - Fork 98
/
RungeKuttaDIM.jl
269 lines (229 loc) · 7.09 KB
/
RungeKuttaDIM.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#################
# DIMRungeKutta #
#################
"""
struct DIMRungeKutta <: ODESolver end
Diagonally-implicit Runge-Kutta ODE solver.
```
residual(tx, ux, vx) = 0,
tx = t_n + c[i] * dt
ux = u_n + dt * ∑_{1 ≤ j < i} A[i, j] * slopes[j] + dt * A[i, i] * x
vx = x,
u_(n+1) = u_n + dt * ∑_{1 ≤ i ≤ s} b[i] * slopes[i].
```
"""
struct DIMRungeKutta <: ODESolver
sysslvr_nl::NonlinearSolver
sysslvr_l::NonlinearSolver
dt::Real
tableau::AbstractTableau{DiagonallyImplicitTableau}
end
##################
# Nonlinear case #
##################
function allocate_odecache(
odeslvr::DIMRungeKutta, odeop::ODEOperator,
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
ui_pre, ui = zero(u0), zero(u0)
num_stages = length(get_nodes(odeslvr.tableau))
slopes = [zero(u0) for _ in 1:num_stages]
odeoptype = ODEOperatorType(odeop)
has_explicit = odeoptype <: AbstractQuasilinearODE
is_semilinear = odeoptype <: AbstractSemilinearODE
mass_constant = is_form_constant(odeop, 1)
reuse = (is_semilinear && mass_constant)
J, r = nothing, nothing
if has_explicit
# Allocate J, r if there are explicit stages
A = get_matrix(odeslvr.tableau)
if any(i -> iszero(A[i, i]), axes(A, 2))
J = allocate_jacobian(odeop, t0, us0N, odeopcache)
r = allocate_residual(odeop, t0, us0N, odeopcache)
end
end
sysslvrcaches = (nothing, nothing)
odeslvrcache = (reuse, has_explicit, ui_pre, ui, slopes, J, r, sysslvrcaches)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::DIMRungeKutta, odeop::ODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
reuse, has_explicit, ui_pre, ui, slopes, J, r, sysslvrcaches = odeslvrcache
sysslvrcache_nl, sysslvrcache_l = sysslvrcaches
# Unpack solver
sysslvr_nl, sysslvr_l = odeslvr.sysslvr_nl, odeslvr.sysslvr_l
dt, tableau = odeslvr.dt, odeslvr.tableau
A, b, c = get_matrix(tableau), get_weights(tableau), get_nodes(tableau)
for i in eachindex(c)
# Define scheme
x = slopes[i]
tx = t0 + c[i] * dt
copy!(ui_pre, u0)
for j in 1:i-1
axpy!(A[i, j] * dt, slopes[j], ui_pre)
end
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Decide whether the stage is explicit or implicit
# The stage becomes explicit when aii = 0 and the operator is quasilinear,
# which is precomputed in has_explicit
aii = A[i, i]
explicit_stage = iszero(aii) && has_explicit
if explicit_stage
# Define scheme
# Set x to zero to split jacobian and residual
fill!(x, zero(eltype(x)))
usx = (ui_pre, x)
ws = (0, 1)
# Create and solve stage operator
stageop = LinearStageOperator(
odeop, odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache_l
)
sysslvrcache_l = solve!(x, sysslvr_l, stageop, sysslvrcache_l)
else
# Define scheme
function usx(x)
copy!(ui, ui_pre)
axpy!(aii * dt, x, ui)
(ui, x)
end
ws = (aii * dt, 1)
# Create and solve stage operator
stageop = NonlinearStageOperator(
odeop, odeopcache,
tx, usx, ws
)
sysslvrcache_nl = solve!(x, sysslvr_nl, stageop, sysslvrcache_nl)
end
end
# Update state
tF = t0 + dt
stateF = _update_dimrk!(stateF, state0, dt, slopes, b)
# Pack outputs
sysslvrcaches = (sysslvrcache_nl, sysslvrcache_l)
odeslvrcache = (reuse, has_explicit, ui_pre, ui, slopes, J, r, sysslvrcaches)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
###############
# Linear case #
###############
function allocate_odecache(
odeslvr::DIMRungeKutta, odeop::ODEOperator{<:AbstractLinearODE},
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
ui_pre = zero(u0)
num_stages = length(get_nodes(odeslvr.tableau))
slopes = [zero(u0) for _ in 1:num_stages]
stiffness_constant = is_form_constant(odeop, 0)
mass_constant = is_form_constant(odeop, 1)
reuse = (stiffness_constant && mass_constant)
J = allocate_jacobian(odeop, t0, us0N, odeopcache)
r = allocate_residual(odeop, t0, us0N, odeopcache)
# Numerical setups for the linear solver
# * If the mass and stiffness matrices are constant, we can reuse numerical
# setups and we allocate one for each distinct aii.
# * Otherwise, there will be no reuse so we only need one numerical setup
# that will be updated.
# To be general, we build a map sysslvrcaches: step -> NumericalSetup.
# We will probably never need more than 256 stages so we can use Int8.
if !reuse
n = 1
ptrs = fill(Int8(1), num_stages)
else
A = get_matrix(odeslvr.tableau)
d = Dict{eltype(A),Int8}()
n = 0
ptrs = zeros(Int8, num_stages)
for i in 1:num_stages
aii = A[i, i]
if !haskey(d, aii)
n += 1
d[aii] = n
end
ptrs[i] = d[aii]
end
end
values = Vector{NumericalSetup}(undef, n)
sysslvrcaches = CompressedArray(values, ptrs)
odeslvrcache = (reuse, ui_pre, slopes, J, r, sysslvrcaches)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::DIMRungeKutta, odeop::ODEOperator{<:AbstractLinearODE},
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
reuse, ui_pre, slopes, J, r, sysslvrcaches = odeslvrcache
# Unpack solver
sysslvr = odeslvr.sysslvr_l
dt, tableau = odeslvr.dt, odeslvr.tableau
A, b, c = get_matrix(tableau), get_weights(tableau), get_nodes(tableau)
for i in eachindex(c)
# Define scheme
# Set x to zero to split jacobian and residual
x = slopes[i]
fill!(x, zero(eltype(x)))
tx = t0 + c[i] * dt
copy!(ui_pre, u0)
for j in 1:i-1
axpy!(A[i, j] * dt, slopes[j], ui_pre)
end
usx = (ui_pre, x)
ws = (A[i, i] * dt, 1)
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Create and solve stage operator
# sysslvrcaches[i] will be unassigned at the first iteration
sysslvrcache = isassigned(sysslvrcaches, i) ? sysslvrcaches[i] : nothing
stageop = LinearStageOperator(
odeop, odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache
)
sysslvrcache = solve!(x, sysslvr, stageop, sysslvrcache)
sysslvrcaches = _setindex_all!(sysslvrcaches, sysslvrcache, i)
end
# Update state
tF = t0 + dt
stateF = _update_dimrk!(stateF, state0, dt, slopes, b)
# Pack outputs
odeslvrcache = (reuse, ui_pre, slopes, J, r, sysslvrcaches)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
#########
# Utils #
#########
function _update_dimrk!(
stateF::NTuple{1,AbstractVector}, state0::NTuple{1,AbstractVector},
dt::Real, slopes::AbstractVector, b::AbstractVector
)
# uF = u0 + ∑_{1 ≤ i ≤ s} b[i] * dt * slopes[i]
u0 = state0[1]
uF = stateF[1]
copy!(uF, u0)
for (bi, slopei) in zip(b, slopes)
axpy!(bi * dt, slopei, uF)
end
(uF,)
end