-
Notifications
You must be signed in to change notification settings - Fork 98
/
RungeKuttaIMEX.jl
417 lines (362 loc) · 13 KB
/
RungeKuttaIMEX.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
"""
struct IMEXRungeKutta <: ODESolver
Implicit-Explicit Runge-Kutta ODE solver.
```
mass(tx, ux) vx + im_res(tx, ux) = 0,
tx = t_n + c[i] * dt
ux = u_n + ∑_{1 ≤ j < i} im_A[i, j] * dt * im_slopes[j] + im_A[i, i] * dt * x
+ ∑_{1 ≤ j < i} ex_A[i, j] * dt * ex_slopes[j]
vx = x
im_slopes[i] = x,
mass(tx, ux) vx + ex_res(tx, ux) = 0,
tx = t_n + c[i] * dt
ux = u_n + ∑_{1 ≤ j ≤ i} im_A[i, j] * dt * im_slopes[j]
+ ∑_{1 ≤ j < i} ex_A[i, j] * dt * ex_slopes[j]
vx = x
ex_slopes[i] = x,
u_(n+1) = u_n + ∑_{1 ≤ i ≤ s} im_b[i] * dt * im_slopes[i]
+ ∑_{1 ≤ i ≤ s} ex_b[i] * dt * ex_slopes[i].
```
"""
struct IMEXRungeKutta <: ODESolver
sysslvr_nl::NonlinearSolver
sysslvr_l::NonlinearSolver
dt::Real
tableau::AbstractTableau{ImplicitExplicitTableau}
end
#######################
# Notimplemented case #
#######################
const imex_rk_not_implemented_msg = """
IMEX Runge-Kutta is only implemented for IMEX ODE operators whose implicit
residual is quasilinear.
"""
function allocate_odecache(
odeslvr::IMEXRungeKutta, odeop::IMEXODEOperator,
t0::Real, us0::NTuple{1,AbstractVector}
)
@unreachable imex_rk_not_implemented_msg
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::IMEXRungeKutta, odeop::IMEXODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
@unreachable imex_rk_not_implemented_msg
end
# Dispatch on the IMEX decomposition
function allocate_odecache(
odeslvr::IMEXRungeKutta, odeop::IMEXODEOperator{<:AbstractQuasilinearODE},
t0::Real, us0::NTuple{1,AbstractVector}
)
im_odeop, ex_odeop = get_imex_operators(odeop)
allocate_odecache(odeslvr, odeop, im_odeop, ex_odeop, t0, us0)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::IMEXRungeKutta, odeop::IMEXODEOperator{<:AbstractQuasilinearODE},
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
im_odeop, ex_odeop = get_imex_operators(odeop)
ode_march!(stateF, odeslvr, odeop, im_odeop, ex_odeop, t0, state0, odecache)
end
##################
# Nonlinear case #
##################
# This is very similar to `DIMRungeKutta` applied to a nonlinear `ODEOperator`
function allocate_odecache(
odeslvr::IMEXRungeKutta, odeop,
im_odeop::ODEOperator{<:AbstractQuasilinearODE},
ex_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)
im_tableau, ex_tableau = get_imex_tableaus(odeslvr.tableau)
im_num_stages = length(get_nodes(im_tableau))
im_slopes = [zero(u0) for _ in 1:im_num_stages]
ex_num_stages = length(get_nodes(ex_tableau))
ex_slopes = [zero(u0) for _ in 1:ex_num_stages]
is_semilinear = ODEOperatorType(im_odeop) <: AbstractSemilinearODE
mass_constant = is_form_constant(odeop, 1)
reuse = (is_semilinear && mass_constant)
# The explicit stage is always going to be linear so we always need to
# allocate a jacobian and residual.
J = allocate_jacobian(odeop, t0, us0N, odeopcache)
r = allocate_residual(odeop, t0, us0N, odeopcache)
# We can share the numerical setup across the implicit and explicit parts
# because the linear solver will only be called when the implicit part goes
# through an explicit stage (aii = 0) and on the explicit part. In both cases
# the matrix of the linear stage operator is the mass matrix.
sysslvrcaches = (nothing, nothing)
odeslvrcache = (reuse, ui_pre, ui, im_slopes, ex_slopes, J, r, sysslvrcaches)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::IMEXRungeKutta, odeop,
im_odeop::ODEOperator{<:AbstractQuasilinearODE},
ex_odeop::ODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
im_odeopcache, ex_odeopcache = odeopcache
reuse, ui_pre, ui, im_slopes, ex_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
im_tableau, ex_tableau = get_imex_tableaus(tableau)
im_A, im_b = get_matrix(im_tableau), get_weights(im_tableau)
ex_A, ex_b = get_matrix(ex_tableau), get_weights(ex_tableau)
c = get_nodes(im_tableau)
for i in eachindex(c)
# Define scheme
x = im_slopes[i]
tx = t0 + c[i] * dt
copy!(ui_pre, u0)
for j in 1:i-1
axpy!(im_A[i, j] * dt, im_slopes[j], ui_pre)
axpy!(ex_A[i, j] * dt, ex_slopes[j], ui_pre)
end
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# 1. Implicit part
aii = im_A[i, i]
# If the implicit tableau is padded, we can skip the first implicit solve
# and set im_slopes[1] to zero
if i == 1 && is_padded(tableau)
fill!(x, zero(eltype(x)))
else
# The stage becomes explicit when aii = 0 because the implicit part is
# quasilinear
explicit_stage = iszero(aii)
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
im_stageop = LinearStageOperator(
im_odeop, im_odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache_l
)
sysslvrcache_l = solve!(x, sysslvr_l, im_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
im_stageop = NonlinearStageOperator(
im_odeop, im_odeopcache,
tx, usx, ws
)
sysslvrcache_nl = solve!(x, sysslvr_nl, im_stageop, sysslvrcache_nl)
end
end
# 2. Explicit part
# vx does not matter
# Compute ui from ui_pre and im_slopes[i]
copy!(ui, ui_pre)
axpy!(aii * dt, im_slopes[i], ui)
usx = (ui, u0)
# This stage operator is a little more complicated than usual so we build
# the jacobian and residual here:
# [m(ti, ui)] x + [ex_res(ti, ui)] = 0
# * The explicit part does not contain the mass, we take the full residual
# * The jacobian is the mass matrix, which is stored in the implicit part
residual!(r, ex_odeop, tx, usx, ex_odeopcache)
if isnothing(sysslvrcache_l) || !reuse
ws = (0, 1)
jacobian!(J, im_odeop, tx, usx, ws, im_odeopcache)
end
ex_stageop = LinearStageOperator(J, r, reuse)
x = ex_slopes[i]
sysslvrcache_l = solve!(x, sysslvr_l, ex_stageop, sysslvrcache_l)
end
# Update state
tF = t0 + dt
stateF = _update_imexrk!(stateF, state0, dt, im_slopes, im_b, ex_slopes, ex_b)
# Pack outputs
sysslvrcaches = (sysslvrcache_nl, sysslvrcache_l)
odeslvrcache = (reuse, ui_pre, ui, im_slopes, ex_slopes, J, r, sysslvrcaches)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
###############
# Linear case #
###############
# This is very similar to `DIMRungeKutta` applied to a linear `ODEOperator`
function allocate_odecache(
odeslvr::IMEXRungeKutta, odeop,
im_odeop::ODEOperator{<:AbstractLinearODE},
ex_odeop::ODEOperator,
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
ui_pre = zero(u0)
im_tableau, ex_tableau = get_imex_tableaus(odeslvr.tableau)
im_num_stages = length(get_nodes(im_tableau))
im_slopes = [zero(u0) for _ in 1:im_num_stages]
ex_num_stages = length(get_nodes(ex_tableau))
ex_slopes = [zero(u0) for _ in 1:ex_num_stages]
stiffness_constant = is_form_constant(im_odeop, 0)
mass_constant = is_form_constant(im_odeop, 1)
reuse = (stiffness_constant && mass_constant)
# The explict part will always bring about a linear stage operator, and its
# matrix is always the mass matrix. From the constraint on the nodes of the
# implicit and explicit tableaus, we know that the first stage of the
# implicit part is in fact explicit so the corresponding matrix is also the
# mass matrix. This means that the numerical setup of the explict part can
# always be chosen as the same as numerical setup of the first stage of the
# implicit part.
# For the same reason, the jacobian and residual can be shared across the
# implicit and explicit parts.
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), im_num_stages)
else
im_A = get_matrix(im_tableau)
d = Dict{eltype(im_A),Int8}()
n = 0
ptrs = zeros(Int8, im_num_stages)
for i in 1:im_num_stages
aii = im_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, im_slopes, ex_slopes, J, r, sysslvrcaches)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::IMEXRungeKutta, odeop,
im_odeop::ODEOperator{<:AbstractLinearODE},
ex_odeop::ODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
im_odeopcache, ex_odeopcache = odeopcache
reuse, ui_pre, im_slopes, ex_slopes, J, r, sysslvrcaches = odeslvrcache
# Unpack solver
sysslvr = odeslvr.sysslvr_l
dt, tableau = odeslvr.dt, odeslvr.tableau
im_tableau, ex_tableau = get_imex_tableaus(tableau)
im_A, im_b = get_matrix(im_tableau), get_weights(im_tableau)
ex_A, ex_b = get_matrix(ex_tableau), get_weights(ex_tableau)
c = get_nodes(im_tableau)
for i in eachindex(c)
# Define scheme
# Set x to zero to split jacobian and residual
x = im_slopes[i]
fill!(x, zero(eltype(x)))
tx = t0 + c[i] * dt
copy!(ui_pre, u0)
for j in 1:i-1
axpy!(im_A[i, j] * dt, im_slopes[j], ui_pre)
axpy!(ex_A[i, j] * dt, ex_slopes[j], ui_pre)
end
usx = (ui_pre, x)
ws = (im_A[i, i] * dt, 1)
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Create and solve stage operator
# 1. Implicit stage
# If the implicit tableau is padded, we can skip the first implicit solve
# and set im_slopes[1] to zero
if i == 1 && is_padded(tableau)
fill!(x, zero(eltype(x)))
else
# sysslvrcaches[i] will be unassigned at the first iteration
sysslvrcache = isassigned(sysslvrcaches, i) ? sysslvrcaches[i] : nothing
im_stageop = LinearStageOperator(
im_odeop, im_odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache
)
sysslvrcache = solve!(x, sysslvr, im_stageop, sysslvrcache)
sysslvrcaches = _setindex_all!(sysslvrcaches, sysslvrcache, i)
end
# 2. Explicit part
# vx does not matter
# Compute ui from ui_pre and im_slopes[i]
ui = axpy!(im_A[i, i] * dt, im_slopes[i], ui_pre)
usx = (ui, u0)
# This stage operator is a little more complicated than usual so we build
# the jacobian and residual here:
# [m(ti, ui)] x + [ex_res(ti, ui)] = 0
# * The explicit part does not contain the mass, we take the full residual
# * The jacobian is the mass matrix, which is stored in the implicit part
residual!(r, ex_odeop, tx, usx, ex_odeopcache)
sysslvrcache = isassigned(sysslvrcaches, 1) ? sysslvrcaches[1] : nothing
if isnothing(sysslvrcache) || !reuse
ws = (0, 1)
jacobian!(J, im_odeop, tx, usx, ws, im_odeopcache)
end
ex_stageop = LinearStageOperator(J, r, reuse)
x = ex_slopes[i]
sysslvrcache = solve!(x, sysslvr, ex_stageop, sysslvrcache)
sysslvrcaches = _setindex_all!(sysslvrcaches, sysslvrcache, i)
end
# Update state
tF = t0 + dt
stateF = _update_imexrk!(stateF, state0, dt, im_slopes, im_b, ex_slopes, ex_b)
# Pack outputs
odeslvrcache = (reuse, ui_pre, im_slopes, ex_slopes, J, r, sysslvrcaches)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
#########
# Utils #
#########
function _update_imexrk!(
stateF::NTuple{1,AbstractVector}, state0::NTuple{1,AbstractVector},
dt::Real, im_slopes::AbstractVector{<:AbstractVector}, im_b::AbstractVector,
ex_slopes::AbstractVector{<:AbstractVector}, ex_b::AbstractVector
)
# uF = u0 + ∑_{1 ≤ i ≤ s} im_b[i] * dt * im_slopes[i]
# + ∑_{1 ≤ i ≤ s} ex_b[i] * dt * ex_slopes[i]
u0 = state0[1]
uF = stateF[1]
copy!(uF, u0)
for (bi, slopei) in zip(im_b, im_slopes)
axpy!(bi * dt, slopei, uF)
end
for (bi, slopei) in zip(ex_b, ex_slopes)
axpy!(bi * dt, slopei, uF)
end
(uF,)
end