-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathalgorithms.jl
486 lines (453 loc) · 21.9 KB
/
algorithms.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
abstract type OrdinaryDiffEqExtrapolationVarOrderVarStepAlgorithm <:
OrdinaryDiffEqAdaptiveAlgorithm end
abstract type OrdinaryDiffEqImplicitExtrapolationAlgorithm{CS, AD, FDT, ST, CJ} <:
OrdinaryDiffEqAdaptiveImplicitAlgorithm{CS, AD, FDT, ST, CJ} end
reference = """@inproceedings{elrod2022parallelizing,
title={Parallelizing explicit and implicit extrapolation methods for ordinary differential equations},
author={Elrod, Chris and Ma, Yingbo and Althaus, Konstantin and Rackauckas, Christopher and others},
booktitle={2022 IEEE High Performance Extreme Computing Conference (HPEC)},
pages={1--9},
year={2022},
organization={IEEE}}
"""
@doc generic_solver_docstring(
"Euler extrapolation using Aitken-Neville with the Romberg Sequence.",
"AitkenNeville",
"Parallelized Explicit Extrapolation Method.",
reference,
"""
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
""",
"""
max_order::Int = 10,
min_order::Int = 1,
init_order = 3,
thread = OrdinaryDiffEq.False(),
""")
Base.@kwdef struct AitkenNeville{TO} <: OrdinaryDiffEqExtrapolationVarOrderVarStepAlgorithm
max_order::Int = 10
min_order::Int = 1
init_order::Int = 5
threading::TO = false
end
@doc differentiation_rk_docstring(
"Extrapolation of implicit Euler method with Romberg sequence.
Similar to Hairer's SEULEX.",
"ImplicitEulerExtrapolation",
"Parallelized Explicit Extrapolation Method.",
references = reference,
extra_keyword_description = """
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
""",
extra_keyword_default = """
max_order = 12,
min_order = 3,
init_order = 5,
thread = OrdinaryDiffEq.False(),
sequence = :harmonic
""")
struct ImplicitEulerExtrapolation{CS, AD, F, P, FDT, ST, CJ, TO} <:
OrdinaryDiffEqImplicitExtrapolationAlgorithm{CS, AD, FDT, ST, CJ}
linsolve::F
precs::P
max_order::Int
min_order::Int
init_order::Int
threading::TO
sequence::Symbol # Name of the subdividing sequence
end
function ImplicitEulerExtrapolation(; chunk_size = Val{0}(), autodiff = true,
standardtag = Val{true}(), concrete_jac = nothing,
diff_type = Val{:forward}, linsolve = nothing,
precs = DEFAULT_PRECS,
max_order = 12, min_order = 3, init_order = 5,
threading = false, sequence = :harmonic)
linsolve = (linsolve === nothing &&
(threading == true || threading isa PolyesterThreads)) ?
RFLUFactorization(; thread = Val(false)) : linsolve
min_order = max(3, min_order)
init_order = max(min_order + 1, init_order)
max_order = max(init_order + 1, max_order)
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ImplicitEulerExtrapolation` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ImplicitEulerExtrapolation` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
ImplicitEulerExtrapolation{_unwrap_val(chunk_size), _unwrap_val(autodiff),
typeof(linsolve), typeof(precs), diff_type,
_unwrap_val(standardtag), _unwrap_val(concrete_jac),
typeof(threading)}(linsolve, precs, max_order, min_order,
init_order,
threading, sequence)
end
@doc generic_solver_docstring("Midpoint extrapolation using Barycentric coordinates.",
"ExtrapolationMidpointDeuflhard",
"Parallelized Explicit Extrapolation Method.",
reference,
"""
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
- `sequence_factor`: denotes which even multiple of sequence to take while evaluating internal discretizations.
""",
"""
max_order = 10,
min_order = 1,
init_order = 5,
thread = OrdinaryDiffEq.True(),
sequence = :harmonic,
sequence_factor = 2,
""")
struct ExtrapolationMidpointDeuflhard{TO} <:
OrdinaryDiffEqExtrapolationVarOrderVarStepAlgorithm
min_order::Int # Minimal extrapolation order
init_order::Int # Initial extrapolation order
max_order::Int # Maximal extrapolation order
sequence::Symbol # Name of the subdividing sequence
threading::TO
sequence_factor::Int # An even factor by which sequence is scaled for midpoint extrapolation
end
function ExtrapolationMidpointDeuflhard(; min_order = 1, init_order = 5, max_order = 10,
sequence = :harmonic, threading = true,
sequence_factor = 2)
# Enforce 1 <= min_order <= init_order <= max_order:
min_order = max(1, min_order)
init_order = max(min_order, init_order)
max_order = max(init_order, max_order)
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ExtrapolationMidpointDeuflhard` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
end
# Warn user if sequence_factor is not even
if sequence_factor % 2 != 0
@warn "A non-even number cannot be used as sequence factor.
Thus is has been changed
$(sequence_factor) --> 2"
sequence_factor = 2
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ExtrapolationMidpointDeuflhard` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
# Initialize algorithm
ExtrapolationMidpointDeuflhard(min_order, init_order, max_order, sequence, threading,
sequence_factor)
end
@doc differentiation_rk_docstring("Midpoint extrapolation using Barycentric coordinates.",
"ImplicitDeuflhardExtrapolation",
"Parallelized Explicit Extrapolation Method.",
references = reference,
extra_keyword_description = """
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
""",
extra_keyword_default = """
max_order = 10,
min_order = 1,
init_order = 5,
thread = OrdinaryDiffEq.False(),
sequence = :harmonic,
""")
struct ImplicitDeuflhardExtrapolation{CS, AD, F, P, FDT, ST, CJ, TO} <:
OrdinaryDiffEqImplicitExtrapolationAlgorithm{CS, AD, FDT, ST, CJ}
linsolve::F
precs::P
min_order::Int # Minimal extrapolation order
init_order::Int # Initial extrapolation order
max_order::Int # Maximal extrapolation order
sequence::Symbol # Name of the subdividing sequence
threading::TO
end
function ImplicitDeuflhardExtrapolation(; chunk_size = Val{0}(), autodiff = Val{true}(),
standardtag = Val{true}(), concrete_jac = nothing,
linsolve = nothing, precs = DEFAULT_PRECS,
diff_type = Val{:forward},
min_order = 1, init_order = 5, max_order = 10,
sequence = :harmonic, threading = false)
# Enforce 1 <= min_order <= init_order <= max_order:
min_order = max(1, min_order)
init_order = max(min_order, init_order)
max_order = max(init_order, max_order)
linsolve = (linsolve === nothing &&
(threading == true || threading isa PolyesterThreads)) ?
RFLUFactorization(; thread = Val(false)) : linsolve
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ImplicitDeuflhardExtrapolation` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
chunk_size
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ImplicitDeuflhardExtrapolation` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
# Initialize algorithm
ImplicitDeuflhardExtrapolation{_unwrap_val(chunk_size), _unwrap_val(autodiff),
typeof(linsolve), typeof(precs), diff_type,
_unwrap_val(standardtag), _unwrap_val(concrete_jac),
typeof(threading)}(linsolve, precs, min_order,
init_order, max_order,
sequence, threading)
end
@doc generic_solver_docstring("Midpoint extrapolation using Barycentric coordinates,
following Hairer's ODEX in the adaptivity behavior.",
"ExtrapolationMidpointHairerWanner",
"Parallelized Explicit Extrapolation Method.",
reference,
"""
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
- `sequence_factor`: denotes which even multiple of sequence to take while evaluating internal discretizations.
""",
"""
max_order = 10,
min_order = 2,
init_order = 5,
thread = OrdinaryDiffEq.True(),
sequence = :harmonic,
sequence_factor = 2,
""")
struct ExtrapolationMidpointHairerWanner{TO} <:
OrdinaryDiffEqExtrapolationVarOrderVarStepAlgorithm
min_order::Int # Minimal extrapolation order
init_order::Int # Initial extrapolation order
max_order::Int # Maximal extrapolation order
sequence::Symbol # Name of the subdividing sequence
threading::TO
sequence_factor::Int # An even factor by which sequence is scaled for midpoint extrapolation
end
function ExtrapolationMidpointHairerWanner(; min_order = 2, init_order = 5, max_order = 10,
sequence = :harmonic, threading = true,
sequence_factor = 2)
# Enforce 2 <= min_order
# and min_order + 1 <= init_order <= max_order - 1:
min_order = max(2, min_order)
init_order = max(min_order + 1, init_order)
max_order = max(init_order + 1, max_order)
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ExtrapolationMidpointHairerWanner` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
end
# Warn user if sequence_factor is not even
if sequence_factor % 2 != 0
@warn "A non-even number cannot be used as sequence factor.
Thus is has been changed
$(sequence_factor) --> 2"
sequence_factor = 2
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ExtrapolationMidpointHairerWanner` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
# Initialize algorithm
ExtrapolationMidpointHairerWanner(
min_order, init_order, max_order, sequence, threading,
sequence_factor)
end
@doc differentiation_rk_docstring("Midpoint extrapolation using Barycentric coordinates,
following Hairer's SODEX in the adaptivity behavior.",
"ImplicitHairerWannerExtrapolation",
"Parallelized Explicit Extrapolation Method.",
references = reference,
extra_keyword_description = """
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
""",
extra_keyword_default = """
max_order = 10,
min_order = 2,
init_order = 5,
thread = OrdinaryDiffEq.False(),
sequence = :harmonic,
""")
struct ImplicitHairerWannerExtrapolation{CS, AD, F, P, FDT, ST, CJ, TO} <:
OrdinaryDiffEqImplicitExtrapolationAlgorithm{CS, AD, FDT, ST, CJ}
linsolve::F
precs::P
min_order::Int # Minimal extrapolation order
init_order::Int # Initial extrapolation order
max_order::Int # Maximal extrapolation order
sequence::Symbol # Name of the subdividing sequence
threading::TO
end
function ImplicitHairerWannerExtrapolation(; chunk_size = Val{0}(), autodiff = Val{true}(),
standardtag = Val{true}(),
concrete_jac = nothing,
linsolve = nothing, precs = DEFAULT_PRECS,
diff_type = Val{:forward},
min_order = 2, init_order = 5, max_order = 10,
sequence = :harmonic, threading = false)
# Enforce 2 <= min_order
# and min_order + 1 <= init_order <= max_order - 1:
min_order = max(2, min_order)
init_order = max(min_order + 1, init_order)
max_order = max(init_order + 1, max_order)
linsolve = (linsolve === nothing &&
(threading == true || threading isa PolyesterThreads)) ?
RFLUFactorization(; thread = Val(false)) : linsolve
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ImplicitHairerWannerExtrapolation` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ImplicitHairerWannerExtrapolation` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
# Initialize algorithm
ImplicitHairerWannerExtrapolation{_unwrap_val(chunk_size), _unwrap_val(autodiff),
typeof(linsolve), typeof(precs), diff_type,
_unwrap_val(standardtag), _unwrap_val(concrete_jac),
typeof(threading)}(linsolve, precs, min_order,
init_order,
max_order, sequence, threading)
end
@doc differentiation_rk_docstring("Euler extrapolation using Barycentric coordinates,
following Hairer's SODEX in the adaptivity behavior.",
"ImplicitEulerBarycentricExtrapolation",
"Parallelized Explicit Extrapolation Method.",
references = reference,
extra_keyword_description = """
- `max_order`: maximum order of the adaptive order algorithm.
- `min_order`: minimum order of the adaptive order algorithm.
- `init_order`: initial order of the adaptive order algorithm.
- `thread`: determines whether internal broadcasting on appropriate CPU arrays should be serial (`thread = OrdinaryDiffEq.False()`) or use multiple threads (`thread = OrdinaryDiffEq.True()`) when Julia is started with multiple threads.
- `sequence`: the step-number sequences, also called the subdividing sequence. Possible values are `:harmonic`, `:romberg` or `:bulirsch`.
- `sequence_factor`: denotes which even multiple of sequence to take while evaluating internal discretizations.
""",
extra_keyword_default = """
max_order = 10,
min_order = 3,
init_order = 5,
thread = OrdinaryDiffEq.False(),
sequence = :harmonic,
sequence_factor = 2,
""")
struct ImplicitEulerBarycentricExtrapolation{CS, AD, F, P, FDT, ST, CJ, TO} <:
OrdinaryDiffEqImplicitExtrapolationAlgorithm{CS, AD, FDT, ST, CJ}
linsolve::F
precs::P
min_order::Int # Minimal extrapolation order
init_order::Int # Initial extrapolation order
max_order::Int # Maximal extrapolation order
sequence::Symbol # Name of the subdividing sequence
threading::TO
sequence_factor::Int
end
function ImplicitEulerBarycentricExtrapolation(; chunk_size = Val{0}(),
autodiff = Val{true}(),
standardtag = Val{true}(),
concrete_jac = nothing,
linsolve = nothing, precs = DEFAULT_PRECS,
diff_type = Val{:forward},
min_order = 3, init_order = 5,
max_order = 12, sequence = :harmonic,
threading = false, sequence_factor = 2)
# Enforce 2 <= min_order
# and min_order + 1 <= init_order <= max_order - 1:
min_order = max(3, min_order)
init_order = max(min_order + 1, init_order)
max_order = max(init_order + 1, max_order)
linsolve = (linsolve === nothing &&
(threading == true || threading isa PolyesterThreads)) ?
RFLUFactorization(; thread = Val(false)) : linsolve
# Warn user if orders have been changed
if (min_order, init_order, max_order) != (min_order, init_order, max_order)
@warn "The range of extrapolation orders and/or the initial order given to the
`ImplicitEulerBarycentricExtrapolation` algorithm are not valid and have been changed:
Minimal order: " * lpad(min_order, 2, " ") * " --> " * lpad(min_order, 2, " ") *
"
Maximal order: " * lpad(max_order, 2, " ") * " --> " * lpad(max_order, 2, " ") *
"
Initial order: " * lpad(init_order, 2, " ") * " --> " * lpad(init_order, 2, " ")
end
# Warn user if sequence has been changed:
if sequence != :harmonic && sequence != :romberg && sequence != :bulirsch
@warn "The `sequence` given to the `ImplicitEulerBarycentricExtrapolation` algorithm
is not valid: it must match `:harmonic`, `:romberg` or `:bulirsch`.
Thus it has been changed
:$(sequence) --> :harmonic"
sequence = :harmonic
end
# Initialize algorithm
ImplicitEulerBarycentricExtrapolation{_unwrap_val(chunk_size), _unwrap_val(autodiff),
typeof(linsolve), typeof(precs), diff_type,
_unwrap_val(standardtag),
_unwrap_val(concrete_jac), typeof(threading)}(linsolve,
precs,
min_order,
init_order,
max_order,
sequence,
threading,
sequence_factor)
end