-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbdf_caches.jl
More file actions
955 lines (862 loc) · 27.4 KB
/
bdf_caches.jl
File metadata and controls
955 lines (862 loc) · 27.4 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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
abstract type BDFMutableCache <: OrdinaryDiffEqMutableCache end
function get_fsalfirstlast(cache::BDFMutableCache, u)
return (cache.fsalfirst, du_alias_or_new(cache.nlsolver, cache.fsalfirst))
end
@cache mutable struct ABDF2ConstantCache{N, dtType, rate_prototype} <:
OrdinaryDiffEqConstantCache
nlsolver::N
eulercache::ImplicitEulerConstantCache
dtₙ₋₁::dtType
fsalfirstprev::rate_prototype
end
function alg_cache(
alg::ABDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(2) // 3, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
eulercache = ImplicitEulerConstantCache(nlsolver)
dtₙ₋₁ = one(dt)
fsalfirstprev = rate_prototype
return ABDF2ConstantCache(nlsolver, eulercache, dtₙ₋₁, fsalfirstprev)
end
@cache mutable struct ABDF2Cache{uType, rateType, uNoUnitsType, N, dtType, StepLimiter} <:
BDFMutableCache
uₙ::uType
uₙ₋₁::uType
uₙ₋₂::uType
fsalfirst::rateType
fsalfirstprev::rateType
zₙ₋₁::uType
atmp::uNoUnitsType
nlsolver::N
eulercache::ImplicitEulerCache
dtₙ₋₁::dtType
step_limiter!::StepLimiter
end
function alg_cache(
alg::ABDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(2) // 3, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
fsalfirstprev = zero(rate_prototype)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
algebraic_vars = f.mass_matrix === I ? nothing :
[all(iszero, x) for x in eachcol(f.mass_matrix)]
eulercache = ImplicitEulerCache(
u, uprev, uprev2, fsalfirst, atmp, nlsolver, algebraic_vars, alg.step_limiter!
)
dtₙ₋₁ = one(dt)
zₙ₋₁ = zero(u)
return ABDF2Cache(
u, uprev, uprev2, fsalfirst, fsalfirstprev, zₙ₋₁, atmp,
nlsolver, eulercache, dtₙ₋₁, alg.step_limiter!
)
end
# SBDF
@cache mutable struct SBDFConstantCache{rateType, N, uType} <: OrdinaryDiffEqConstantCache
cnt::Int
ark::Bool
k2::rateType
nlsolver::N
uprev2::uType
uprev4::uType
uprev3::uType
k₁::rateType
k₂::rateType
k₃::rateType
du₁::rateType
du₂::rateType
end
@cache mutable struct SBDFCache{uType, rateType, N} <: BDFMutableCache
cnt::Int
ark::Bool
u::uType
uprev::uType
fsalfirst::rateType
nlsolver::N
uprev2::uType
uprev3::uType
uprev4::uType
k₁::rateType
k₂::rateType
k₃::rateType
du₁::rateType
du₂::rateType
end
function alg_cache(
alg::SBDF, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(1) // 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
k2 = rate_prototype
k₁ = rate_prototype
k₂ = rate_prototype
k₃ = rate_prototype
du₁ = rate_prototype
du₂ = rate_prototype
uprev2 = u
uprev3 = u
uprev4 = u
return SBDFConstantCache(
1, alg.ark, k2, nlsolver, uprev2, uprev3, uprev4, k₁, k₂, k₃, du₁,
du₂
)
end
function alg_cache(
alg::SBDF, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(1) // 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
order = alg.order
k₁ = zero(rate_prototype)
k₂ = order >= 3 ? zero(rate_prototype) : k₁
k₃ = order == 4 ? zero(rate_prototype) : k₁
du₁ = zero(rate_prototype)
du₂ = zero(rate_prototype)
uprev2 = zero(u)
uprev3 = order >= 3 ? zero(u) : uprev2
uprev4 = order == 4 ? zero(u) : uprev2
return SBDFCache(
1, alg.ark, u, uprev, fsalfirst, nlsolver, uprev2, uprev3, uprev4, k₁, k₂, k₃,
du₁, du₂
)
end
# QNDF1
@cache mutable struct QNDF1ConstantCache{
N,
coefType,
coefType1,
coefType2,
dtType,
uType,
} <: OrdinaryDiffEqConstantCache
nlsolver::N
D::coefType1
D2::coefType2
R::coefType
U::coefType
uprev2::uType
dtₙ₋₁::dtType
end
@cache mutable struct QNDF1Cache{
uType, rateType, coefType, coefType1, coefType2,
uNoUnitsType, N, dtType, StepLimiter,
} <: BDFMutableCache
uprev2::uType
fsalfirst::rateType
D::coefType1
D2::coefType2
R::coefType
U::coefType
atmp::uNoUnitsType
utilde::uType
nlsolver::N
dtₙ₋₁::dtType
step_limiter!::StepLimiter
end
function alg_cache(
alg::QNDF1, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = zero(inv((1 - alg.kappa))), 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
uprev2 = u
dtₙ₋₁ = zero(t)
D = fill(zero(u), 1, 1)
D2 = fill(zero(u), 1, 2)
R = fill(zero(t), 1, 1)
U = fill(zero(t), 1, 1)
U!(1, U)
return QNDF1ConstantCache(nlsolver, D, D2, R, U, uprev2, dtₙ₋₁)
end
function alg_cache(
alg::QNDF1, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = zero(inv((1 - alg.kappa))), 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
D = Array{typeof(u)}(undef, 1, 1)
D2 = Array{typeof(u)}(undef, 1, 2)
R = fill(zero(t), 1, 1)
U = fill(zero(t), 1, 1)
D[1] = zero(u)
D2[1] = zero(u)
D2[2] = zero(u)
U!(1, U)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
utilde = zero(u)
uprev2 = zero(u)
dtₙ₋₁ = zero(dt)
return QNDF1Cache(
uprev2, fsalfirst, D, D2, R, U, atmp, utilde, nlsolver, dtₙ₋₁, alg.step_limiter!
)
end
# QNDF2
@cache mutable struct QNDF2ConstantCache{
N,
coefType,
coefType1,
coefType2,
uType,
dtType,
} <: OrdinaryDiffEqConstantCache
nlsolver::N
D::coefType1
D2::coefType2
R::coefType
U::coefType
uprev2::uType
uprev3::uType
dtₙ₋₁::dtType
dtₙ₋₂::dtType
end
@cache mutable struct QNDF2Cache{
uType, rateType, coefType, coefType1, coefType2,
uNoUnitsType, N, dtType, StepLimiter,
} <: BDFMutableCache
uprev2::uType
uprev3::uType
fsalfirst::rateType
D::coefType1
D2::coefType2
R::coefType
U::coefType
atmp::uNoUnitsType
utilde::uType
nlsolver::N
dtₙ₋₁::dtType
dtₙ₋₂::dtType
step_limiter!::StepLimiter
end
function alg_cache(
alg::QNDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = zero(inv((1 - alg.kappa))), 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
uprev2 = u
uprev3 = u
dtₙ₋₁ = zero(t)
dtₙ₋₂ = zero(t)
D = fill(zero(u), 1, 2)
D2 = fill(zero(u), 1, 3)
R = fill(zero(t), 2, 2)
U = fill(zero(t), 2, 2)
U!(2, U)
return QNDF2ConstantCache(nlsolver, D, D2, R, U, uprev2, uprev3, dtₙ₋₁, dtₙ₋₂)
end
function alg_cache(
alg::QNDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = zero(inv((1 - alg.kappa))), 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
D = Array{typeof(u)}(undef, 1, 2)
D2 = Array{typeof(u)}(undef, 1, 3)
R = fill(zero(t), 2, 2)
U = fill(zero(t), 2, 2)
D[1] = zero(u)
D[2] = zero(u)
D2[1] = zero(u)
D2[2] = zero(u)
D2[3] = zero(u)
U!(2, U)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
utilde = zero(u)
uprev2 = zero(u)
uprev3 = zero(u)
dtₙ₋₁ = zero(dt)
dtₙ₋₂ = zero(dt)
return QNDF2Cache(
uprev2, uprev3, fsalfirst, D, D2, R, U, atmp,
utilde, nlsolver, dtₙ₋₁, dtₙ₋₂, alg.step_limiter!
)
end
@cache mutable struct QNDFConstantCache{
MO,
N,
coefType,
UType,
dtType,
EEstType,
gammaType,
} <: OrdinaryDiffEqConstantCache
nlsolver::N
U::UType
D::coefType
prevD::coefType
prevorder::Int
order::Int
max_order::Val{MO}
dtprev::dtType
nconsteps::Int ##Successful Consecutive Step with the same step size
consfailcnt::Int #Consecutive failed steps count
EEst1::EEstType #Error Estimator for k-1 order
EEst2::EEstType #Error Estimator for k+1 order
γₖ::gammaType
end
function alg_cache(
alg::QNDF{MO}, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {
uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits,
} where {MO}
max_order = MO
γ, c = Int64(1) // 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
dtprev = one(dt)
if u isa Number
D = zeros(uEltypeNoUnits, max_order + 2)
prevD = zeros(uEltypeNoUnits, max_order + 2)
else
D = [zero(u) .* zero(uEltypeNoUnits) for _ in 1:(max_order + 2)]
prevD = [zero(u) .* zero(uEltypeNoUnits) for _ in 1:(max_order + 2)]
end
EEst1 = tTypeNoUnits(1)
EEst2 = tTypeNoUnits(1)
U = zero(MMatrix{max_order, max_order, tTypeNoUnits})
for r in 1:max_order
U[1, r] = -r
for j in 2:max_order
U[j, r] = U[j - 1, r] * ((j - 1) - r) / j
end
end
U = SArray(U)
γₖ = SVector(ntuple(k -> sum(tTypeNoUnits(Int64(1) // j) for j in 1:k), Val(max_order)))
return QNDFConstantCache(
nlsolver, U, D, prevD, 1, 1, Val(max_order), dtprev, 0, 0, EEst1,
EEst2, γₖ
)
end
@cache mutable struct QNDFCache{
MO, UType, RUType, rateType, N, coefType, dtType, EEstType,
gammaType, uType, uNoUnitsType, StepLimiter,
} <:
BDFMutableCache
fsalfirst::rateType
dd::uType
utilde::uType
utildem1::uType
utildep1::uType
ϕ::uType
u₀::uType
nlsolver::N
U::UType
RU::RUType
D::coefType
Dtmp::coefType
tmp2::uType
prevD::coefType
order::Int
prevorder::Int
max_order::Val{MO}
dtprev::dtType
nconsteps::Int ##Successful consecutive step with the same step size
consfailcnt::Int #Consecutive failed steps count
EEst1::EEstType #Error Estimator for k-1 order
EEst2::EEstType #Error Estimator for k+1 order
γₖ::gammaType
atmp::uNoUnitsType
atmpm1::uNoUnitsType
atmpp1::uNoUnitsType
dense::Vector{uType}
step_limiter!::StepLimiter
end
@truncate_stacktrace QNDFCache 1
function alg_cache(
alg::QNDF{MO}, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {
uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits,
} where {MO}
max_order = MO
γ, c = Int64(1) // 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
dd = zero(u)
utilde = zero(u)
utildem1 = zero(u)
utildep1 = zero(u)
ϕ = zero(u)
u₀ = zero(u)
dtprev = one(dt)
D = [zero(similar(u, uEltypeNoUnits)) for _ in 1:(max_order + 2)]
Dtmp = [zero(similar(u, uEltypeNoUnits)) for _ in 1:(max_order + 2)]
prevD = [zero(similar(u, uEltypeNoUnits)) for _ in 1:(max_order + 2)]
atmp = zero(similar(u, uEltypeNoUnits))
atmpm1 = zero(similar(u, uEltypeNoUnits))
atmpp1 = zero(similar(u, uEltypeNoUnits))
tmp2 = zero(u)
EEst1 = tTypeNoUnits(1)
EEst2 = tTypeNoUnits(1)
U = zero(MMatrix{max_order, max_order, tTypeNoUnits})
for r in 1:max_order
U[1, r] = -r
for j in 2:max_order
U[j, r] = U[j - 1, r] * ((j - 1) - r) / j
end
end
U = SArray(U)
RU = Matrix(U)
γₖ = SVector(ntuple(k -> sum(tTypeNoUnits(Int64(1) // j) for j in 1:k), Val(max_order)))
dense = [zero(u) for _ in 1:max_order]
return QNDFCache(
fsalfirst, dd, utilde, utildem1, utildep1, ϕ, u₀, nlsolver, U, RU, D, Dtmp,
tmp2, prevD, 1, 1, Val(max_order), dtprev, 0, 0, EEst1, EEst2, γₖ, atmp,
atmpm1, atmpp1, dense, alg.step_limiter!
)
end
@cache mutable struct MEBDF2Cache{uType, rateType, uNoUnitsType, N} <:
BDFMutableCache
u::uType
uprev::uType
uprev2::uType
fsalfirst::rateType
z₁::uType
z₂::uType
tmp2::uType
atmp::uNoUnitsType
nlsolver::N
end
function alg_cache(
alg::MEBDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
fsalfirst = zero(rate_prototype)
z₁ = zero(u)
z₂ = zero(u)
z₃ = zero(u)
tmp2 = zero(u)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, false)
return MEBDF2Cache(u, uprev, uprev2, fsalfirst, z₁, z₂, tmp2, atmp, nlsolver)
end
mutable struct MEBDF2ConstantCache{N} <: OrdinaryDiffEqConstantCache
nlsolver::N
end
function alg_cache(
alg::MEBDF2, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits},
::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
return MEBDF2ConstantCache(nlsolver)
end
@cache mutable struct FBDFConstantCache{
MO, N, tsType, tType, uType, uuType, coeffType,
EEstType, rType, wType, staldType,
} <:
OrdinaryDiffEqConstantCache
nlsolver::N
ts::tsType
ts_tmp::tsType
t_old::tType
u_history::uuType
order::Int
prev_order::Int
u_corrector::uType
bdf_coeffs::coeffType
max_order::Val{MO}
nconsteps::Int # consecutive success steps
consfailcnt::Int #consecutive failed step counts
qwait::Int # countdown to next order change consideration (CVODE-style)
terkm2::EEstType
terkm1::EEstType
terk::EEstType
terkp1::EEstType
r::rType
weights::wType
iters_from_event::Int
stald::staldType
end
function alg_cache(
alg::FBDF{MO}, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {
uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits,
} where {MO}
γ, c = Int64(1) // 1, 1
max_order = MO
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
bdf_coeffs = SA[
1 -1 0 0 0 0;
Int64(3) // 2 -2 Int64(1) // 2 0 0 0;
Int64(11) // 6 -3 Int64(3) // 2 -Int64(1) // 3 0 0;
Int64(25) // 12 -4 3 -Int64(4) // 3 Int64(1) // 4 0;
Int64(137) // 60 -5 5 -Int64(10) // 3 Int64(5) // 4 -Int64(1) // 5
]
ts = zero(Vector{typeof(t)}(undef, max_order + 2)) #ts is the successful past points, it will be updated after successful step
ts_tmp = similar(ts)
if u isa Number
u_history = zeros(eltype(u), max_order + 2)
u_corrector = zeros(eltype(u), max_order + 2)
else
u_history = [zero(u) for _ in 1:(max_order + 2)]
u_corrector = [zero(u) for _ in 1:(max_order + 2)]
end
order = 1
prev_order = 1
terkm2 = tTypeNoUnits(1)
terkm1 = tTypeNoUnits(1)
terk = tTypeNoUnits(1)
terkp1 = tTypeNoUnits(1)
r = zero(Vector{typeof(t)}(undef, max_order + 2))
weights = zero(Vector{typeof(t)}(undef, max_order + 2))
weights[1] = 1
nconsteps = 0
consfailcnt = 0
qwait = 3 # order + 2, matching nconsteps >= order + 2 for failure-free runs
t_old = zero(t)
iters_from_event = 0
T_stald = real(uBottomEltypeNoUnits)
stald = StabilityLimitDetectionState(
T_stald;
enabled = alg.stald,
rrcut = alg.stald_rrcut,
vrrtol = alg.stald_vrrtol,
vrrt2 = alg.stald_vrrt2,
sqtol = alg.stald_sqtol,
rrtol = alg.stald_rrtol,
tiny = alg.stald_tiny,
)
return FBDFConstantCache(
nlsolver, ts, ts_tmp, t_old, u_history, order, prev_order,
u_corrector, bdf_coeffs, Val(MO), nconsteps, consfailcnt, qwait, terkm2,
terkm1, terk, terkp1, r, weights, iters_from_event, stald
)
end
@cache mutable struct FBDFCache{
MO, N, rateType, uNoUnitsType, tsType, tType, uType, uuType,
coeffType, EEstType, rType, wType, StepLimiter, fdWeightsType, staldType,
} <:
BDFMutableCache
fsalfirst::rateType
nlsolver::N
ts::tsType
ts_tmp::tsType
t_old::tType
u_history::uuType
order::Int
prev_order::Int
u_corrector::uuType
u₀::uType
bdf_coeffs::coeffType
max_order::Val{MO}
nconsteps::Int # consecutive success steps
consfailcnt::Int #consecutive failed step counts
qwait::Int # countdown to next order change consideration (CVODE-style)
tmp::uType
atmp::uNoUnitsType
terkm2::EEstType
terkm1::EEstType
terk::EEstType #terk corresponds to hᵏyᵏ(tₙ₊₁)
terkp1::EEstType
terk_tmp::uType
terkp1_tmp::uType
r::rType
weights::wType #weights of Lagrangian formula
equi_ts::tsType
iters_from_event::Int
dense::Vector{uType}
step_limiter!::StepLimiter
fd_weights::fdWeightsType
stald::staldType
end
@truncate_stacktrace FBDFCache 1
function alg_cache(
alg::FBDF{MO}, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {
MO, uEltypeNoUnits, uBottomEltypeNoUnits,
tTypeNoUnits,
}
γ, c = Int64(1) // 1, 1
fsalfirst = zero(rate_prototype)
max_order = MO
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
bdf_coeffs = SA[
1 -1 0 0 0 0;
Int64(3) // 2 -2 Int64(1) // 2 0 0 0;
Int64(11) // 6 -3 Int64(3) // 2 -Int64(1) // 3 0 0;
Int64(25) // 12 -4 3 -Int64(4) // 3 Int64(1) // 4 0;
Int64(137) // 60 -5 5 -Int64(10) // 3 Int64(5) // 4 -Int64(1) // 5
]
ts = Vector{typeof(t)}(undef, max_order + 2) #ts is the successful past points, it will be updated after successful step
u_history = [zero(u) for _ in 1:(max_order + 2)]
order = 1
prev_order = 1
u_corrector = [zero(u) for _ in 1:(max_order + 2)]
recursivefill!(ts, zero(t))
terkm2 = tTypeNoUnits(1)
terkm1 = tTypeNoUnits(1)
terk = tTypeNoUnits(1)
terkp1 = tTypeNoUnits(1)
terk_tmp = similar(u)
terkp1_tmp = similar(u)
r = Vector{typeof(t)}(undef, max_order + 2)
weights = Vector{typeof(t)}(undef, max_order + 2)
recursivefill!(r, zero(t))
recursivefill!(weights, zero(t))
weights[1] = 1
nconsteps = 0
consfailcnt = 0
qwait = 3 # order + 2, matching nconsteps >= order + 2 for failure-free runs
t_old = zero(t)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, zero(uEltypeNoUnits))
u₀ = similar(u)
equi_ts = similar(ts)
tmp = similar(u)
ts_tmp = similar(ts)
iters_from_event = 0
dense = [zero(u) for _ in 1:(2 * (max_order + 1))] # first half for integrator.k, second half as scratch
fd_weights = zeros(typeof(t), max_order + 1, max_order + 1)
T_stald = real(uBottomEltypeNoUnits)
stald = StabilityLimitDetectionState(
T_stald;
enabled = alg.stald,
rrcut = alg.stald_rrcut,
vrrtol = alg.stald_vrrtol,
vrrt2 = alg.stald_vrrt2,
sqtol = alg.stald_sqtol,
rrtol = alg.stald_rrtol,
tiny = alg.stald_tiny,
)
return FBDFCache(
fsalfirst, nlsolver, ts, ts_tmp, t_old, u_history, order, prev_order,
u_corrector, u₀, bdf_coeffs, Val(MO), nconsteps, consfailcnt, qwait, tmp, atmp,
terkm2, terkm1, terk, terkp1, terk_tmp, terkp1_tmp, r, weights, equi_ts,
iters_from_event, dense, alg.step_limiter!, fd_weights, stald
)
end
# MOOSE234 — Variable-stepsize, variable-order (2/3/4) embedded method
# DeCaria et al., arXiv:1810.06670v1
@cache mutable struct MOOSE234ConstantCache{
N, tsType, tType, uType, uuType,
EEstType, rType, wType,
} <:
OrdinaryDiffEqConstantCache
nlsolver::N
ts::tsType
ts_tmp::tsType
t_old::tType
u_history::uuType
order::Int
prev_order::Int
u_corrector::uType
nconsteps::Int
consfailcnt::Int
qwait::Int
terkm1::EEstType
terk::EEstType
terkp1::EEstType
r::rType
weights::wType
iters_from_event::Int
end
function alg_cache(
alg::MOOSE234, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{false}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(1) // 1, 1
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(false), verbose
)
hist_size = 6 # max_order(4) + 2
ts = zero(Vector{typeof(t)}(undef, hist_size))
ts_tmp = similar(ts)
if u isa Number
u_history = zeros(eltype(u), hist_size)
u_corrector = zeros(eltype(u), hist_size)
else
u_history = [zero(u) for _ in 1:hist_size]
u_corrector = [zero(u) for _ in 1:hist_size]
end
order = 2
prev_order = 2
terkm1 = tTypeNoUnits(1)
terk = tTypeNoUnits(1)
terkp1 = tTypeNoUnits(1)
r = zero(Vector{typeof(t)}(undef, hist_size))
weights = zero(Vector{typeof(t)}(undef, hist_size))
weights[1] = 1
nconsteps = 0
consfailcnt = 0
qwait = 4 # order + 2
t_old = zero(t)
iters_from_event = 0
return MOOSE234ConstantCache(
nlsolver, ts, ts_tmp, t_old, u_history, order, prev_order,
u_corrector, nconsteps, consfailcnt, qwait, terkm1,
terk, terkp1, r, weights, iters_from_event
)
end
@cache mutable struct MOOSE234Cache{
N, rateType, uNoUnitsType, tsType, tType, uType, uuType,
EEstType, rType, wType, StepLimiter, fdWeightsType,
} <:
BDFMutableCache
fsalfirst::rateType
nlsolver::N
ts::tsType
ts_tmp::tsType
t_old::tType
u_history::uuType
order::Int
prev_order::Int
u_corrector::uuType
u₀::uType
nconsteps::Int
consfailcnt::Int
qwait::Int
tmp::uType
atmp::uNoUnitsType
terkm1::EEstType
terk::EEstType
terkp1::EEstType
terk_tmp::uType
terkp1_tmp::uType
r::rType
weights::wType
equi_ts::tsType
iters_from_event::Int
dense::Vector{uType}
step_limiter!::StepLimiter
fd_weights::fdWeightsType
end
@truncate_stacktrace MOOSE234Cache 1
function alg_cache(
alg::MOOSE234, u, rate_prototype, ::Type{uEltypeNoUnits},
::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t,
dt, reltol, p, calck,
::Val{true}, verbose
) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits}
γ, c = Int64(1) // 1, 1
fsalfirst = zero(rate_prototype)
nlsolver = build_nlsolver(
alg, u, uprev, p, t, dt, f, rate_prototype, uEltypeNoUnits,
uBottomEltypeNoUnits, tTypeNoUnits, γ, c, Val(true), verbose
)
hist_size = 6
ts = Vector{typeof(t)}(undef, hist_size)
u_history = [zero(u) for _ in 1:hist_size]
order = 2
prev_order = 2
u_corrector = [zero(u) for _ in 1:hist_size]
recursivefill!(ts, zero(t))
terkm1 = tTypeNoUnits(1)
terk = tTypeNoUnits(1)
terkp1 = tTypeNoUnits(1)
terk_tmp = similar(u)
terkp1_tmp = similar(u)
r = Vector{typeof(t)}(undef, hist_size)
weights = Vector{typeof(t)}(undef, hist_size)
recursivefill!(r, zero(t))
recursivefill!(weights, zero(t))
weights[1] = 1
nconsteps = 0
consfailcnt = 0
qwait = 4 # order + 2
t_old = zero(t)
atmp = similar(u, uEltypeNoUnits)
recursivefill!(atmp, zero(uEltypeNoUnits))
u₀ = similar(u)
equi_ts = similar(ts)
tmp = similar(u)
ts_tmp = similar(ts)
iters_from_event = 0
dense = [zero(u) for _ in 1:10] # 2*(max_order+1) = 2*5 = 10
fd_weights = zeros(typeof(t), 5, 5) # (max_order+1) × (max_order+1)
return MOOSE234Cache(
fsalfirst, nlsolver, ts, ts_tmp, t_old, u_history, order, prev_order,
u_corrector, u₀, nconsteps, consfailcnt, qwait, tmp, atmp,
terkm1, terk, terkp1, terk_tmp, terkp1_tmp, r, weights, equi_ts,
iters_from_event, dense, alg.step_limiter!, fd_weights
)
end