-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathrosenbrock_caches.jl
More file actions
659 lines (602 loc) · 20.9 KB
/
rosenbrock_caches.jl
File metadata and controls
659 lines (602 loc) · 20.9 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
abstract type RosenbrockMutableCache <: OrdinaryDiffEqMutableCache end
abstract type RosenbrockConstantCache <: OrdinaryDiffEqConstantCache end
# Fake values since non-FSAL
get_fsalfirstlast(cache::RosenbrockMutableCache, u) = (nothing, nothing)
################################################################################
# Shampine's Low-order Rosenbrocks
mutable struct RosenbrockCache{
uType, rateType, tabType, uNoUnitsType, JType, WType, TabType,
TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter,
} <:
RosenbrockMutableCache
u::uType
uprev::uType
dense::Vector{rateType}
du::rateType
du1::rateType
du2::rateType
dtC::Matrix{tabType}
dtd::Vector{tabType}
ks::Vector{rateType}
fsalfirst::rateType
fsallast::rateType
dT::rateType
J::JType
W::WType
tmp::rateType
atmp::uNoUnitsType
weight::uNoUnitsType
tab::TabType
tf::TFType
uf::UFType
linsolve_tmp::rateType
linsolve::F
jac_config::JCType
grad_config::GCType
reltol::RTolType
alg::A
step_limiter!::StepLimiter
stage_limiter!::StageLimiter
interp_order::Int
end
function full_cache(c::RosenbrockCache)
return [
c.u, c.uprev, c.dense..., c.du, c.du1, c.du2,
c.ks..., c.fsalfirst, c.fsallast, c.dT, c.tmp, c.atmp, c.weight, c.linsolve_tmp,
]
end
struct RosenbrockCombinedConstantCache{TF, UF, Tab, JType, WType, F, AD} <:
RosenbrockConstantCache
tf::TF
uf::UF
tab::Tab
J::JType
W::WType
linsolve::F
autodiff::AD
interp_order::Int
end
@cache mutable struct Rosenbrock23Cache{
uType, rateType, uNoUnitsType, JType, WType,
TabType, TFType, UFType, F, JCType, GCType,
RTolType, A, AV, StepLimiter, StageLimiter,
} <: RosenbrockMutableCache
u::uType
uprev::uType
k₁::rateType
k₂::rateType
k₃::rateType
du1::rateType
du2::rateType
f₁::rateType
fsalfirst::rateType
fsallast::rateType
dT::rateType
J::JType
W::WType
tmp::rateType
atmp::uNoUnitsType
weight::uNoUnitsType
tab::TabType
tf::TFType
uf::UFType
linsolve_tmp::rateType
linsolve::F
jac_config::JCType
grad_config::GCType
reltol::RTolType
alg::A
algebraic_vars::AV
step_limiter!::StepLimiter
stage_limiter!::StageLimiter
end
@cache mutable struct Rosenbrock32Cache{
uType, rateType, uNoUnitsType, JType, WType,
TabType, TFType, UFType, F, JCType, GCType,
RTolType, A, AV, StepLimiter, StageLimiter,
} <: RosenbrockMutableCache
u::uType
uprev::uType
k₁::rateType
k₂::rateType
k₃::rateType
du1::rateType
du2::rateType
f₁::rateType
fsalfirst::rateType
fsallast::rateType
dT::rateType
J::JType
W::WType
tmp::rateType
atmp::uNoUnitsType
weight::uNoUnitsType
tab::TabType
tf::TFType
uf::UFType
linsolve_tmp::rateType
linsolve::F
jac_config::JCType
grad_config::GCType
reltol::RTolType
alg::A
algebraic_vars::AV
step_limiter!::StepLimiter
stage_limiter!::StageLimiter
end
function alg_cache(
alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
k₁ = zero(rate_prototype)
k₂ = zero(rate_prototype)
k₃ = zero(rate_prototype)
du1 = zero(rate_prototype)
du2 = zero(rate_prototype)
# f₀ = zero(u) fsalfirst
f₁ = zero(rate_prototype)
fsalfirst = zero(rate_prototype)
fsallast = zero(rate_prototype)
dT = zero(rate_prototype)
tmp = zero(rate_prototype)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
weight = similar(u, uEltypeNoUnits)
recursivefill!(weight, false)
tab = Rosenbrock23Tableau(constvalue(uBottomEltypeNoUnits))
tf = TimeGradientWrapper(f, uprev, p)
uf = UJacobianWrapper(f, t, p)
linsolve_tmp = zero(rate_prototype)
grad_config = build_grad_config(alg, f, tf, du1, t)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
linprob = LinearProblem(W, _vec(linsolve_tmp); u0 = _vec(tmp))
Pl,
Pr = wrapprecs(
alg.precs(
W, nothing, u, p, t, nothing, nothing, nothing,
nothing
)..., weight, tmp
)
linsolve = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
Pl = Pl, Pr = Pr,
assumptions = LinearSolve.OperatorAssumptions(true),
verbose = verbose.linear_verbosity
)
algebraic_vars = f.mass_matrix === I ? nothing :
find_algebraic_vars_eqs(f.mass_matrix)[1]
return Rosenbrock23Cache(
u, uprev, k₁, k₂, k₃, du1, du2, f₁,
fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf,
linsolve_tmp,
linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!,
alg.stage_limiter!
)
end
function alg_cache(
alg::Rosenbrock32, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
k₁ = zero(rate_prototype)
k₂ = zero(rate_prototype)
k₃ = zero(rate_prototype)
du1 = zero(rate_prototype)
du2 = zero(rate_prototype)
# f₀ = zero(u) fsalfirst
f₁ = zero(rate_prototype)
fsalfirst = zero(rate_prototype)
fsallast = zero(rate_prototype)
dT = zero(rate_prototype)
tmp = zero(rate_prototype)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
weight = similar(u, uEltypeNoUnits)
recursivefill!(weight, false)
tab = Rosenbrock32Tableau(constvalue(uBottomEltypeNoUnits))
tf = TimeGradientWrapper(f, uprev, p)
uf = UJacobianWrapper(f, t, p)
linsolve_tmp = zero(rate_prototype)
grad_config = build_grad_config(alg, f, tf, du1, t)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
linprob = LinearProblem(W, _vec(linsolve_tmp); u0 = _vec(tmp))
Pl,
Pr = wrapprecs(
alg.precs(
W, nothing, u, p, t, nothing, nothing, nothing,
nothing
)..., weight, tmp
)
linsolve = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
Pl = Pl, Pr = Pr,
assumptions = LinearSolve.OperatorAssumptions(true),
verbose = verbose.linear_verbosity
)
algebraic_vars = f.mass_matrix === I ? nothing :
find_algebraic_vars_eqs(f.mass_matrix)[1]
return Rosenbrock32Cache(
u, uprev, k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W,
tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config,
grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!
)
end
struct Rosenbrock23ConstantCache{T, TF, UF, JType, WType, F, AD} <:
RosenbrockConstantCache
c₃₂::T
d::T
tf::TF
uf::UF
J::JType
W::WType
linsolve::F
autodiff::AD
end
function Rosenbrock23ConstantCache(
::Type{T}, tf, uf, J, W, linsolve, autodiff
) where {T}
tab = Rosenbrock23Tableau(T)
return Rosenbrock23ConstantCache(
tab.c₃₂, tab.d, tf, uf, J, W, linsolve, autodiff
)
end
function alg_cache(
alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tf = TimeDerivativeWrapper(f, u, p)
uf = UDerivativeWrapper(f, t, p)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, nothing, uEltypeNoUnits, Val(false))
linprob = nothing #LinearProblem(W,copy(u); u0=copy(u))
linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true)
return Rosenbrock23ConstantCache(
constvalue(uBottomEltypeNoUnits), tf, uf, J, W, linsolve,
alg_autodiff(alg)
)
end
struct Rosenbrock32ConstantCache{T, TF, UF, JType, WType, F, AD} <:
RosenbrockConstantCache
c₃₂::T
d::T
tf::TF
uf::UF
J::JType
W::WType
linsolve::F
autodiff::AD
end
function Rosenbrock32ConstantCache(
::Type{T}, tf, uf, J, W, linsolve, autodiff
) where {T}
tab = Rosenbrock32Tableau(T)
return Rosenbrock32ConstantCache(
tab.c₃₂, tab.d, tf, uf, J, W, linsolve, autodiff
)
end
function alg_cache(
alg::Rosenbrock32, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tf = TimeDerivativeWrapper(f, u, p)
uf = UDerivativeWrapper(f, t, p)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, nothing, uEltypeNoUnits, Val(false))
linprob = nothing #LinearProblem(W,copy(u); u0=copy(u))
linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true)
return Rosenbrock32ConstantCache(
constvalue(uBottomEltypeNoUnits), tf, uf, J, W, linsolve,
alg_autodiff(alg)
)
end
### Rodas4+ methods and consolidated Rosenbrock methods (using RodasTableau)
# Helper accessors for step_limiter!/stage_limiter! — algorithms that have these fields
# return them directly; algorithms without return trivial_limiter!.
_get_step_limiter(alg) = trivial_limiter!
_get_stage_limiter(alg) = trivial_limiter!
for Alg in (
:Rosenbrock23, :Rosenbrock32, :ROS3P, :Rodas3, :Rodas23W, :Rodas3P,
:Rodas4, :Rodas42, :Rodas4P, :Rodas4P2, :Rodas5, :Rodas5P,
:Rodas5Pe, :Rodas5Pr, :Rodas6P,
)
@eval _get_step_limiter(alg::$Alg) = alg.step_limiter!
@eval _get_stage_limiter(alg::$Alg) = alg.stage_limiter!
end
# Tableau type dispatch
tabtype(::Rodas4) = Rodas4Tableau
tabtype(::Rodas42) = Rodas42Tableau
tabtype(::Rodas4P) = Rodas4PTableau
tabtype(::Rodas4P2) = Rodas4P2Tableau
tabtype(::Rodas5) = Rodas5Tableau
tabtype(::Rodas5P) = Rodas5PTableau
tabtype(::Rodas5Pr) = Rodas5PTableau
tabtype(::Rodas5Pe) = Rodas5PeTableau
tabtype(::Rodas6P) = Rodas6PTableau
# Consolidated methods: tableau type dispatch
tabtype(::ROS3P) = ROS3PRodasTableau
tabtype(::Rodas3) = Rodas3RodasTableau
tabtype(::Rodas3P) = Rodas3PRodasTableau
tabtype(::Rodas23W) = Rodas23WRodasTableau
tabtype(::ROS2) = ROS2RodasTableau
tabtype(::ROS2PR) = ROS2PRRodasTableau
tabtype(::ROS2S) = ROS2SRodasTableau
tabtype(::ROS3) = ROS3RodasTableau
tabtype(::ROS3PR) = ROS3PRRodasTableau
tabtype(::Scholz4_7) = Scholz4_7RodasTableau
tabtype(::ROS34PW1a) = ROS34PW1aRodasTableau
tabtype(::ROS34PW1b) = ROS34PW1bRodasTableau
tabtype(::ROS34PW2) = ROS34PW2RodasTableau
tabtype(::ROS34PW3) = ROS34PW3RodasTableau
tabtype(::ROS34PRw) = ROS34PRwRodasTableau
tabtype(::ROS3PRL) = ROS3PRLRodasTableau
tabtype(::ROS3PRL2) = ROS3PRL2RodasTableau
tabtype(::ROK4a) = ROK4aRodasTableau
tabtype(::RosShamp4) = RosShamp4RodasTableau
tabtype(::Veldd4) = Veldd4RodasTableau
tabtype(::Velds4) = Velds4RodasTableau
tabtype(::GRK4T) = GRK4TRodasTableau
tabtype(::GRK4A) = GRK4ARodasTableau
tabtype(::Ros4LStab) = Ros4LStabRodasTableau
tabtype(::RosenbrockW6S4OS) = RosenbrockW6S4OSRodasTableau
# Union of all algorithms using RodasTableau-based RosenbrockCache
const RodasTableauAlgorithms = Union{
Rodas4, Rodas42, Rodas4P, Rodas4P2, Rodas5,
Rodas5P, Rodas5Pe, Rodas5Pr, Rodas6P,
ROS3P, Rodas3, Rodas3P, Rodas23W,
ROS2, ROS2PR, ROS2S, ROS3, ROS3PR, Scholz4_7,
ROS34PW1a, ROS34PW1b, ROS34PW2, ROS34PW3,
ROS34PRw, ROS3PRL, ROS3PRL2, ROK4a,
RosShamp4, Veldd4, Velds4, GRK4T, GRK4A, Ros4LStab,
RosenbrockW6S4OS,
}
function alg_cache(
alg::RodasTableauAlgorithms,
u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tf = TimeDerivativeWrapper(f, u, p)
uf = UDerivativeWrapper(f, t, p)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, nothing, uEltypeNoUnits, Val(false))
linprob = nothing #LinearProblem(W,copy(u); u0=copy(u))
linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true)
tab = tabtype(alg)(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits))
H_rows = size(tab.H, 1)
# Rodas3P/Rodas23W: H has 3 rows but only 2 are for interpolation;
# the 3rd row is for interpoldiff error estimation
if alg isa Union{Rodas3P, Rodas23W}
interp_order = 2
else
interp_order = H_rows > 0 ? H_rows : 2
end
return RosenbrockCombinedConstantCache(
tf, uf,
tab, J, W, linsolve,
alg_autodiff(alg), interp_order
)
end
function alg_cache(
alg::RodasTableauAlgorithms,
u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tab = tabtype(alg)(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits))
# Initialize vectors: kshortsize depends on whether H has rows
H_rows = size(tab.H, 1)
kshortsize = H_rows > 0 ? H_rows : 2
# Rodas3P/Rodas23W: H has 3 rows but only 2 are for interpolation;
# the 3rd row is for interpoldiff error estimation
if alg isa Union{Rodas3P, Rodas23W}
interp_order = 2
else
interp_order = kshortsize
end
dense = [zero(rate_prototype) for _ in 1:kshortsize]
ks = [zero(rate_prototype) for _ in 1:size(tab.A, 1)]
du = zero(rate_prototype)
du1 = zero(rate_prototype)
du2 = zero(rate_prototype)
# Promote t-type for AD
dtC = zero(tab.C) .* dt
dtd = zero(tab.d) .* dt
# Initialize other variables
fsalfirst = zero(rate_prototype)
fsallast = zero(rate_prototype)
dT = zero(rate_prototype)
# Temporary and helper variables
tmp = zero(rate_prototype)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
weight = similar(u, uEltypeNoUnits)
recursivefill!(weight, false)
tf = TimeGradientWrapper(f, uprev, p)
uf = UJacobianWrapper(f, t, p)
grad_config = build_grad_config(alg, f, tf, du1, t)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
Pl,
Pr = wrapprecs(
alg.precs(
W, nothing, u, p, t, nothing, nothing, nothing,
nothing
)..., weight, tmp
)
linsolve_tmp = zero(rate_prototype)
linprob = LinearProblem(W, _vec(linsolve_tmp); u0 = _vec(tmp))
linsolve = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
Pl = Pl, Pr = Pr,
assumptions = LinearSolve.OperatorAssumptions(true),
verbose = verbose.linear_verbosity
)
# Return the cache struct with vectors
return RosenbrockCache(
u, uprev, dense, du, du1, du2, dtC, dtd, ks, fsalfirst, fsallast,
dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp,
linsolve, jac_config, grad_config, reltol, alg,
_get_step_limiter(alg), _get_stage_limiter(alg), interp_order
)
end
function get_fsalfirstlast(
cache::Union{
Rosenbrock23Cache, Rosenbrock32Cache,
RosenbrockCache,
},
u
)
return (cache.fsalfirst, cache.fsallast)
end
################################################################################
### Tsit5DA - hybrid explicit/linear-implicit method for DAEs
struct HybridExplicitImplicitConstantCache{TF, UF, Tab, JType, WType, F, AD} <: RosenbrockConstantCache
tf::TF
uf::UF
tab::Tab
J::JType
W::WType
linsolve::F
autodiff::AD
interp_order::Int
end
mutable struct HybridExplicitImplicitCache{
uType, rateType, uNoUnitsType, JType, WType, TabType,
TFType, UFType, F, JCType, GCType, RTolType, A,
StepLimiter, StageLimiter, DVType, AVType,
GZType, GYType, WZType, FZ,
} <: RosenbrockMutableCache
u::uType
uprev::uType
dense::Vector{rateType}
du::rateType
du1::rateType
du2::rateType
ks::Vector{rateType}
fsalfirst::rateType
fsallast::rateType
dT::rateType
J::JType
W::WType
tmp::rateType
atmp::uNoUnitsType
weight::uNoUnitsType
tab::TabType
tf::TFType
uf::UFType
linsolve_tmp::rateType
linsolve::F
jac_config::JCType
grad_config::GCType
reltol::RTolType
alg::A
step_limiter!::StepLimiter
stage_limiter!::StageLimiter
interp_order::Int
# DAE-specific fields
diff_vars::DVType
alg_vars::AVType
g_z::GZType # n_g x n_g algebraic Jacobian block
g_y::GYType # n_g x n_f coupling block
W_z::WZType # -gamma * g_z (used for linear solve)
linsolve_tmp_z::FZ # n_g-sized RHS for algebraic solve
end
function full_cache(c::HybridExplicitImplicitCache)
return [
c.u, c.uprev, c.dense..., c.du, c.du1, c.du2,
c.ks..., c.fsalfirst, c.fsallast, c.dT, c.tmp, c.atmp, c.weight, c.linsolve_tmp,
]
end
function alg_cache(
alg::HybridExplicitImplicitRK, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tf = TimeDerivativeWrapper(f, u, p)
uf = UDerivativeWrapper(f, t, p)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, nothing, uEltypeNoUnits, Val(false))
tab = alg.tab(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits))
return HybridExplicitImplicitConstantCache(
tf, uf, tab, J, W, nothing, alg_autodiff(alg), size(tab.H, 1)
)
end
function alg_cache(
alg::HybridExplicitImplicitRK, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
tab = alg.tab(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits))
num_stages = size(tab.A, 1)
interp_order = size(tab.H, 1)
# Initialize vectors
dense = [zero(rate_prototype) for _ in 1:interp_order]
ks = [zero(rate_prototype) for _ in 1:num_stages]
du = zero(rate_prototype)
du1 = zero(rate_prototype)
du2 = zero(rate_prototype)
fsalfirst = zero(rate_prototype)
fsallast = zero(rate_prototype)
dT = zero(rate_prototype)
tmp = zero(rate_prototype)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
weight = similar(u, uEltypeNoUnits)
recursivefill!(weight, false)
linsolve_tmp = zero(rate_prototype)
tf = TimeGradientWrapper(f, uprev, p)
uf = UJacobianWrapper(f, t, p)
grad_config = build_grad_config(alg, f, tf, du1, t)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2)
J, W = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
linprob = LinearProblem(W, _vec(linsolve_tmp); u0 = _vec(tmp))
Pl, Pr = wrapprecs(
alg.precs(
W, nothing, u, p, t, nothing, nothing, nothing,
nothing
)..., weight, tmp
)
linsolve = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
Pl = Pl, Pr = Pr,
assumptions = LinearSolve.OperatorAssumptions(true),
verbose = verbose.linear_verbosity
)
# Detect algebraic variables from mass matrix
mass_matrix = f.mass_matrix
if mass_matrix === I
diff_vars = collect(1:length(u))
alg_vars = Int[]
g_z = zeros(eltype(u), 0, 0)
g_y = zeros(eltype(u), 0, 0)
W_z = zeros(eltype(u), 0, 0)
linsolve_tmp_z = zeros(eltype(u), 0)
linsolve_z = nothing
else
n = length(u)
diff_vars = findall(i -> mass_matrix[i, i] != 0, 1:n)
alg_vars = findall(i -> mass_matrix[i, i] == 0, 1:n)
n_g = length(alg_vars)
n_f = length(diff_vars)
g_z = zeros(eltype(u), n_g, n_g)
g_y = zeros(eltype(u), n_g, n_f)
W_z = zeros(eltype(u), n_g, n_g)
linsolve_tmp_z = zeros(eltype(u), n_g)
end
return HybridExplicitImplicitCache(
u, uprev, dense, du, du1, du2, ks,
fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf,
linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg,
alg.step_limiter!, alg.stage_limiter!, interp_order,
diff_vars, alg_vars, g_z, g_y, W_z, linsolve_tmp_z
)
end