-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathfirk_caches.jl
More file actions
864 lines (779 loc) · 24.8 KB
/
firk_caches.jl
File metadata and controls
864 lines (779 loc) · 24.8 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
abstract type FIRKMutableCache <: OrdinaryDiffEqMutableCache end
get_fsalfirstlast(cache::FIRKMutableCache, u) = (cache.fsalfirst, cache.k)
mutable struct RadauIIA3ConstantCache{F, Tab, Tol, Dt, U, JType} <:
OrdinaryDiffEqConstantCache
uf::F
tab::Tab
κ::Tol
ηold::Tol
iter::Int
cont1::U
cont2::U
dtprev::Dt
W_γdt::Dt
status::NLStatus
J::JType
end
function alg_cache(
alg::RadauIIA3, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UDerivativeWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA3Tableau(uToltype, constvalue(tTypeNoUnits))
κ = convert(uToltype, 1 // 100)
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
return RadauIIA3ConstantCache(
uf, tab, κ, one(uToltype), 10000, u, u, dt, dt,
Convergence, J
)
end
mutable struct RadauIIA3Cache{
uType, cuType, uNoUnitsType, rateType, JType, W1Type, UF, JC,
F1, Tab, Tol, Dt, rTol, aTol, StepLimiter,
} <: FIRKMutableCache
u::uType
uprev::uType
z1::uType
z2::uType
w1::uType
w2::uType
dw12::cuType
cubuff::cuType
du1::rateType
fsalfirst::rateType
k::rateType
k2::rateType
fw1::rateType
fw2::rateType
J::JType
W1::W1Type
uf::UF
tab::Tab
κ::Tol
ηold::Tol
iter::Int
tmp::uType
atmp::uNoUnitsType
jac_config::JC
linsolve::F1
rtol::rTol
atol::aTol
dtprev::Dt
W_γdt::Dt
status::NLStatus
step_limiter!::StepLimiter
end
function alg_cache(
alg::RadauIIA3, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UJacobianWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA3Tableau(uToltype, constvalue(tTypeNoUnits))
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
z1 = zero(u)
z2 = zero(u)
w1 = zero(u)
w2 = zero(u)
dw12 = similar(u, Complex{eltype(u)})
recursivefill!(dw12, false)
cubuff = similar(u, Complex{eltype(u)})
recursivefill!(cubuff, false)
fsalfirst = zero(rate_prototype)
k = zero(rate_prototype)
k2 = zero(rate_prototype)
fw1 = zero(rate_prototype)
fw2 = zero(rate_prototype)
du1 = zero(rate_prototype)
tmp = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, dw12)
J, W1 = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
W1 = similar(J, Complex{eltype(W1)})
recursivefill!(W1, false)
linprob = LinearProblem(W1, _vec(cubuff), (nothing, u, p, t); u0 = _vec(dw12))
linsolve = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
abstol = reltol, reltol = reltol,
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
rtol = reltol isa Number ? reltol : zero(reltol)
atol = reltol isa Number ? reltol : zero(reltol)
return RadauIIA3Cache(
u, uprev,
z1, z2, w1, w2,
dw12, cubuff,
du1, fsalfirst, k, k2, fw1, fw2,
J, W1,
uf, tab, κ, one(uToltype), 10000,
tmp, atmp, jac_config, linsolve, rtol, atol, dt, dt,
Convergence, alg.step_limiter!
)
end
mutable struct RadauIIA5ConstantCache{F, Tab, Tol, Dt, U, JType} <:
OrdinaryDiffEqConstantCache
uf::F
tab::Tab
κ::Tol
ηold::Tol
iter::Int
cont1::U
cont2::U
cont3::U
dtprev::Dt
W_γdt::Dt
status::NLStatus
J::JType
end
function alg_cache(
alg::RadauIIA5, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UDerivativeWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA5Tableau(uToltype, constvalue(tTypeNoUnits))
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
return RadauIIA5ConstantCache(
uf, tab, κ, one(uToltype), 10000, u, u, u, dt, dt,
Convergence, J
)
end
mutable struct RadauIIA5Cache{
uType, cuType, uNoUnitsType, rateType, JType, W1Type, W2Type,
UF, JC, F1, F2, Tab, Tol, Dt, rTol, aTol, StepLimiter,
} <:
FIRKMutableCache
u::uType
uprev::uType
z1::uType
z2::uType
z3::uType
w1::uType
w2::uType
w3::uType
dw1::uType
ubuff::uType
dw23::cuType
cubuff::cuType
du1::rateType
fsalfirst::rateType
k::rateType
k1::rateType
k2::rateType
k3::rateType
fw1::rateType
fw2::rateType
fw3::rateType
J::JType
W1::W1Type
W2::W2Type # complex
uf::UF
tab::Tab
κ::Tol
ηold::Tol
iter::Int
tmp::uType
atmp::uNoUnitsType
jac_config::JC
linsolve1::F1
linsolve2::F2
rtol::rTol
atol::aTol
dtprev::Dt
W_γdt::Dt
status::NLStatus
step_limiter!::StepLimiter
end
function alg_cache(
alg::RadauIIA5, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UJacobianWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA5Tableau(uToltype, constvalue(tTypeNoUnits))
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
z1 = zero(u)
z2 = zero(u)
z3 = zero(u)
w1 = zero(u)
w2 = zero(u)
w3 = zero(u)
dw1 = zero(u)
ubuff = zero(u)
dw23 = similar(u, Complex{eltype(u)})
recursivefill!(dw23, false)
cubuff = similar(u, Complex{eltype(u)})
recursivefill!(cubuff, false)
fsalfirst = zero(rate_prototype)
k = zero(rate_prototype)
k1 = zero(rate_prototype)
k2 = zero(rate_prototype)
k3 = zero(rate_prototype)
fw1 = zero(rate_prototype)
fw2 = zero(rate_prototype)
fw3 = zero(rate_prototype)
du1 = zero(rate_prototype)
tmp = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, dw1)
J, W1 = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
if J isa AbstractSciMLOperator
error("Non-concrete Jacobian not yet supported by RadauIIA5.")
end
W2 = similar(J, Complex{eltype(W1)})
recursivefill!(W2, false)
linprob = LinearProblem(W1, _vec(ubuff), (nothing, u, p, t); u0 = _vec(dw1))
linsolve1 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
linprob = LinearProblem(W2, _vec(cubuff), (nothing, u, p, t); u0 = _vec(dw23))
linsolve2 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
rtol = reltol isa Number ? reltol : zero(reltol)
atol = reltol isa Number ? reltol : zero(reltol)
return RadauIIA5Cache(
u, uprev,
z1, z2, z3, w1, w2, w3,
dw1, ubuff, dw23, cubuff,
du1, fsalfirst, k, k1, k2, k3, fw1, fw2, fw3,
J, W1, W2,
uf, tab, κ, one(uToltype), 10000,
tmp, atmp, jac_config, linsolve1, linsolve2, rtol, atol, dt, dt,
Convergence, alg.step_limiter!
)
end
mutable struct RadauIIA9ConstantCache{F, Tab, Tol, Dt, U, JType} <:
OrdinaryDiffEqConstantCache
uf::F
tab::Tab
κ::Tol
ηold::Tol
iter::Int
cont1::U
cont2::U
cont3::U
cont4::U
cont5::U
dtprev::Dt
W_γdt::Dt
status::NLStatus
J::JType
end
function alg_cache(
alg::RadauIIA9, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UDerivativeWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA9Tableau(uToltype, constvalue(tTypeNoUnits))
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
return RadauIIA9ConstantCache(
uf, tab, κ, one(uToltype), 10000, u, u, u, u, u, dt, dt,
Convergence, J
)
end
mutable struct RadauIIA9Cache{
uType, cuType, uNoUnitsType, rateType, JType, W1Type, W2Type,
UF, JC, F1, F2, Tab, Tol, Dt, rTol, aTol, StepLimiter,
} <:
FIRKMutableCache
u::uType
uprev::uType
z1::uType
z2::uType
z3::uType
z4::uType
z5::uType
w1::uType
w2::uType
w3::uType
w4::uType
w5::uType
dw1::uType
ubuff::uType
dw23::cuType
dw45::cuType
cubuff1::cuType
cubuff2::cuType
du1::rateType
fsalfirst::rateType
k::rateType
k1::rateType
k2::rateType
k3::rateType
k4::rateType
k5::rateType
fw1::rateType
fw2::rateType
fw3::rateType
fw4::rateType
fw5::rateType
J::JType
W1::W1Type
W2::W2Type # complex
W3::W2Type
uf::UF
tab::Tab
κ::Tol
ηold::Tol
iter::Int
tmp::uType
tmp2::uType
tmp3::uType
tmp4::uType
tmp5::uType
tmp6::uType
tmp7::uType
tmp8::uType
tmp9::uType
tmp10::uType
atmp::uNoUnitsType
jac_config::JC
linsolve1::F1
linsolve2::F2
linsolve3::F2
rtol::rTol
atol::aTol
dtprev::Dt
W_γdt::Dt
status::NLStatus
step_limiter!::StepLimiter
end
function alg_cache(
alg::RadauIIA9, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UJacobianWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
tab = RadauIIA9Tableau(uToltype, constvalue(tTypeNoUnits))
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
z1 = zero(u)
z2 = zero(u)
z3 = zero(u)
z4 = zero(u)
z5 = zero(u)
w1 = zero(u)
w2 = zero(u)
w3 = zero(u)
w4 = zero(u)
w5 = zero(u)
dw1 = zero(u)
ubuff = zero(u)
dw23 = similar(u, Complex{eltype(u)})
dw45 = similar(u, Complex{eltype(u)})
recursivefill!(dw23, false)
recursivefill!(dw45, false)
cubuff1 = similar(u, Complex{eltype(u)})
cubuff2 = similar(u, Complex{eltype(u)})
recursivefill!(cubuff1, false)
recursivefill!(cubuff2, false)
fsalfirst = zero(rate_prototype)
k = zero(rate_prototype)
k1 = zero(rate_prototype)
k2 = zero(rate_prototype)
k3 = zero(rate_prototype)
k4 = zero(rate_prototype)
k5 = zero(rate_prototype)
fw1 = zero(rate_prototype)
fw2 = zero(rate_prototype)
fw3 = zero(rate_prototype)
fw4 = zero(rate_prototype)
fw5 = zero(rate_prototype)
du1 = zero(rate_prototype)
tmp = zero(u)
tmp2 = zero(u)
tmp3 = zero(u)
tmp4 = zero(u)
tmp5 = zero(u)
tmp6 = zero(u)
tmp7 = zero(u)
tmp8 = zero(u)
tmp9 = zero(u)
tmp10 = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, dw1)
J, W1 = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
if J isa AbstractSciMLOperator
error("Non-concrete Jacobian not yet supported by RadauIIA5.")
end
W2 = similar(J, Complex{eltype(W1)})
W3 = similar(J, Complex{eltype(W1)})
recursivefill!(W2, false)
recursivefill!(W3, false)
linprob = LinearProblem(W1, _vec(ubuff), (nothing, u, p, t); u0 = _vec(dw1))
linsolve1 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
linprob = LinearProblem(W2, _vec(cubuff1), (nothing, u, p, t); u0 = _vec(dw23))
linsolve2 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
linprob = LinearProblem(W3, _vec(cubuff2), (nothing, u, p, t); u0 = _vec(dw45))
linsolve3 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
#Pl = LinearSolve.InvPreconditioner(Diagonal(_vec(weight))),
#Pr = Diagonal(_vec(weight)))
rtol = reltol isa Number ? reltol : zero(reltol)
atol = reltol isa Number ? reltol : zero(reltol)
return RadauIIA9Cache(
u, uprev,
z1, z2, z3, z4, z5, w1, w2, w3, w4, w5,
dw1, ubuff, dw23, dw45, cubuff1, cubuff2,
du1, fsalfirst, k, k1, k2, k3, k4, k5, fw1, fw2, fw3, fw4, fw5,
J, W1, W2, W3,
uf, tab, κ, one(uToltype), 10000,
tmp, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, atmp, jac_config,
linsolve1, linsolve2, linsolve3, rtol, atol, dt, dt,
Convergence, alg.step_limiter!
)
end
mutable struct AdaptiveRadauConstantCache{F, Tab, Tol, Dt, U, JType} <:
OrdinaryDiffEqConstantCache
uf::F
tabs::Vector{Tab}
κ::Tol
ηold::Tol
iter::Int
cont::Vector{U}
dtprev::Dt
W_γdt::Dt
status::NLStatus
J::JType
num_stages::Int
step::Int
hist_iter::Float64
index::Int
end
function alg_cache(
alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UDerivativeWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
max_order = alg.max_order
min_order = alg.min_order
max_stages = (max_order - 1) ÷ 4 * 2 + 1
min_stages = (min_order - 1) ÷ 4 * 2 + 1
if (alg.min_order < 5)
error("min_order choice $min_order below 5 is not compatible with the algorithm")
elseif (max_stages < min_stages)
error("max_order $max_order is below min_order $min_order")
end
num_stages = min_stages
tabs = [
RadauIIATableau(uToltype, constvalue(tTypeNoUnits), i)
for i in min_stages:2:max_stages
]
cont = Vector{typeof(u)}(undef, max_stages)
for i in 1:max_stages
cont[i] = zero(u)
end
index = 1
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
return AdaptiveRadauConstantCache(
uf, tabs, κ, one(uToltype), 10000, cont, dt, dt,
Convergence, J, num_stages, 1, 0.0, index
)
end
mutable struct AdaptiveRadauCache{
uType, cuType, tType, uNoUnitsType, rateType, JType, W1Type, W2Type,
UF, JC, F1, F2, Tab, Tol, Dt, rTol, aTol, StepLimiter,
} <:
FIRKMutableCache
u::uType
uprev::uType
z::Vector{uType}
w::Vector{uType}
c_prime::Vector{tType}
αdt::Vector{tType}
βdt::Vector{tType}
dw1::uType
ubuff::uType
dw2::Vector{cuType}
cubuff::Vector{cuType}
dw::Vector{uType}
derivatives::Matrix{uType}
du1::rateType
fsalfirst::rateType
ks::Vector{rateType}
k::rateType
fw::Vector{rateType}
J::JType
W1::W1Type #real
W2::Vector{W2Type} #complex
uf::UF
tabs::Vector{Tab}
κ::Tol
ηold::Tol
iter::Int
tmp::uType
atmp::uNoUnitsType
jac_config::JC
linsolve1::F1 #real
linsolve2::Vector{F2} #complex
rtol::rTol
atol::aTol
dtprev::Dt
W_γdt::Dt
status::NLStatus
step_limiter!::StepLimiter
num_stages::Int
step::Int
hist_iter::Float64
index::Int
end
function alg_cache(
alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UJacobianWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
max_order = alg.max_order
min_order = alg.min_order
max_stages = (max_order - 1) ÷ 4 * 2 + 1
min_stages = (min_order - 1) ÷ 4 * 2 + 1
if (alg.min_order < 5)
error("min_order choice $min_order below 5 is not compatible with the algorithm")
elseif (max_stages < min_stages)
error("max_order $max_order is below min_order $min_order")
end
num_stages = min_stages
tabs = [
RadauIIATableau(uToltype, constvalue(tTypeNoUnits), i)
for i in min_stages:2:max_stages
]
index = 1
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
z = Vector{typeof(u)}(undef, max_stages)
w = Vector{typeof(u)}(undef, max_stages)
for i in 1:max_stages
z[i] = zero(u)
w[i] = zero(u)
end
αdt = [zero(t) for i in 1:max_stages]
βdt = [zero(t) for i in 1:max_stages]
c_prime = Vector{typeof(t)}(undef, max_stages) #time stepping
for i in 1:max_stages
c_prime[i] = zero(t)
end
dw1 = zero(u)
ubuff = zero(u)
dw2 = [similar(u, Complex{eltype(u)}) for _ in 1:((max_stages - 1) ÷ 2)]
recursivefill!.(dw2, false)
cubuff = [similar(u, Complex{eltype(u)}) for _ in 1:((max_stages - 1) ÷ 2)]
recursivefill!.(cubuff, false)
dw = [zero(u) for i in 1:max_stages]
derivatives = Matrix{typeof(u)}(undef, max_stages, max_stages)
for i in 1:max_stages, j in 1:max_stages
derivatives[i, j] = zero(u)
end
fsalfirst = zero(rate_prototype)
fw = [zero(rate_prototype) for i in 1:max_stages]
ks = [zero(rate_prototype) for i in 1:max_stages]
k = ks[1]
du1 = zero(rate_prototype)
tmp = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, zero(u), dw1)
J, W1 = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
if J isa AbstractSciMLOperator
error("Non-concrete Jacobian not yet supported by AdaptiveRadau.")
end
W2 = [similar(J, Complex{eltype(W1)}) for _ in 1:((max_stages - 1) ÷ 2)]
recursivefill!.(W2, false)
linprob = LinearProblem(W1, _vec(ubuff), (nothing, u, p, t); u0 = _vec(dw1))
linsolve1 = init(
linprob, alg.linsolve, alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
linsolve2 = [
init(
LinearProblem(W2[i], _vec(cubuff[i]), (nothing, u, p, t); u0 = _vec(dw2[i])),
alg.linsolve, alias = LinearAliasSpecifier(
alias_A = true, alias_b = true
),
assumptions = LinearSolve.OperatorAssumptions(true), verbose = verbose.linear_verbosity
)
for i in 1:((max_stages - 1) ÷ 2)
]
rtol = reltol isa Number ? reltol : zero(reltol)
atol = reltol isa Number ? reltol : zero(reltol)
return AdaptiveRadauCache(
u, uprev,
z, w, c_prime, αdt, βdt, dw1, ubuff, dw2, cubuff, dw, derivatives,
du1, fsalfirst, ks, k, fw,
J, W1, W2,
uf, tabs, κ, one(uToltype), 10000, tmp,
atmp, jac_config,
linsolve1, linsolve2, rtol, atol, dt, dt,
Convergence, alg.step_limiter!, num_stages, 1, 0.0, index
)
end
mutable struct GaussLegendreConstantCache{F, Tab, Tol, Dt, U, JType} <:
OrdinaryDiffEqConstantCache
uf::F
tab::Tab
κ::Tol
ηold::Tol
iter::Int
cont::Vector{U}
dtprev::Dt
W_γdt::Dt
status::NLStatus
J::JType
num_stages::Int
end
function alg_cache(
alg::GaussLegendre, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UDerivativeWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
num_stages = alg.num_stages
tab = GaussLegendreTableau(uToltype, constvalue(tTypeNoUnits), num_stages)
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
cont = Vector{typeof(u)}(undef, num_stages)
for i in 1:num_stages
cont[i] = zero(u)
end
return GaussLegendreConstantCache(
uf, tab, κ, one(uToltype), 10000, cont, dt, dt,
Convergence, J, num_stages
)
end
mutable struct GaussLegendreCache{
uType, uNoUnitsType, rateType, JType, WType, Buff,
UF, JC, F1, Tab, Tol, Dt, rTol, aTol, StepLimiter,
} <: FIRKMutableCache
u::uType
uprev::uType
z::Vector{uType}
z_last::Vector{uType}
w::Vector{uType}
dw::Vector{uType}
ubuff::Buff
u_full::uType
u_half::uType
du1::rateType
fsalfirst::rateType
k::rateType
ks::Vector{rateType}
fw::Vector{rateType}
J::JType
W::WType
uf::UF
tab::Tab
κ::Tol
ηold::Tol
iter::Int
tmp::uType
atmp::uNoUnitsType
jac_config::JC
linsolve::F1
rtol::rTol
atol::aTol
dtprev::Dt
W_γdt::Dt
status::NLStatus
step_limiter!::StepLimiter
num_stages::Int
end
function alg_cache(
alg::GaussLegendre, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
uf = UJacobianWrapper(f, t, p)
uToltype = constvalue(uBottomEltypeNoUnits)
num_stages = alg.num_stages
tab = GaussLegendreTableau(uToltype, constvalue(tTypeNoUnits), num_stages)
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
z = [zero(u) for _ in 1:num_stages]
z_last = [zero(u) for _ in 1:num_stages]
w = [zero(u) for _ in 1:num_stages]
dw = [zero(u) for _ in 1:num_stages]
n = length(_vec(u))
ubuff = similar(_vec(u), num_stages * n)
recursivefill!(ubuff, false)
u_full = zero(u)
u_half = zero(u)
fsalfirst = zero(rate_prototype)
k = zero(rate_prototype)
ks = [zero(rate_prototype) for _ in 1:num_stages]
fw = [zero(rate_prototype) for _ in 1:num_stages]
du1 = zero(rate_prototype)
tmp = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, dw[1])
J, _ = build_J_W(alg, u, uprev, p, t, dt, f, jac_config, uEltypeNoUnits, Val(true))
if J isa AbstractSciMLOperator
error("Non-concrete Jacobian not yet supported by GaussLegendre.")
end
W = similar(J, num_stages * n, num_stages * n)
recursivefill!(W, false)
linu0 = similar(_vec(ubuff))
recursivefill!(linu0, false)
linprob = LinearProblem(W, _vec(ubuff); u0 = linu0)
linsolve = init(
linprob, alg.linsolve,
alias = LinearAliasSpecifier(alias_A = true, alias_b = true),
assumptions = LinearSolve.OperatorAssumptions(true),
verbose = verbose.linear_verbosity
)
rtol = reltol isa Number ? reltol : zero(reltol)
atol = reltol isa Number ? reltol : zero(reltol)
return GaussLegendreCache(
u, uprev, z, z_last, w, dw, ubuff, u_full, u_half,
du1, fsalfirst, k, ks, fw,
J, W,
uf, tab, κ, one(uToltype), 10000,
tmp, atmp, jac_config, linsolve, rtol, atol, dt, dt,
Convergence, alg.step_limiter!, num_stages
)
end