-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathintegrator.jl
More file actions
826 lines (747 loc) · 27.4 KB
/
integrator.jl
File metadata and controls
826 lines (747 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
mutable struct IntegratorStats
naccept::Int64
nreject::Int64
# TODO inner solver stats
end
IntegratorStats() = IntegratorStats(0, 0)
Base.@kwdef mutable struct IntegratorOptions{tType, fType, F3}
adaptive::Bool
dtmin::tType = eps(Float64)
dtmax::tType = Inf
failfactor::fType = 4.0
verbose::Bool = false
isoutofdomain::F3 = DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN
end
"""
OperatorSplittingIntegrator <: AbstractODEIntegrator
A variant of [`ODEIntegrator`](https://github.com/SciML/OrdinaryDiffEq.jl/blob/6ec5a55bda26efae596bf99bea1a1d729636f412/src/integrators/type.jl#L77-L123) to perform opeartor splitting.
Derived from https://github.com/CliMA/ClimaTimeSteppers.jl/blob/ef3023747606d2750e674d321413f80638136632/src/integrators.jl.
"""
mutable struct OperatorSplittingIntegrator{
fType,
algType,
uType,
tType,
pType,
heapType,
tstopsType,
saveatType,
callbackType,
cacheType,
solType,
subintTreeType,
solidxTreeType,
syncTreeType,
controllerType,
optionsType,
} <: SciMLBase.AbstractODEIntegrator{algType, true, uType, tType}
const f::fType
const alg::algType
u::uType # Master Solution
uprev::uType # Master Solution
tmp::uType # Interpolation buffer
p::pType
t::tType # Current time
tprev::tType
dt::tType # This is the time step length which which we use during time marching
dtcache::tType # This is the proposed time step length
const dtchangeable::Bool # Indicator whether dtcache can be changed
tstops::heapType
_tstops::tstopsType # argument to __init used as default argument to reinit!
saveat::heapType
_saveat::saveatType # argument to __init used as default argument to reinit!
callback::callbackType
advance_to_tstop::Bool
# TODO group these into some internal flag struct
last_step_failed::Bool
force_stepfail::Bool
isout::Bool
u_modified::Bool
# DiffEqBase.initialize! and DiffEqBase.finalize!
cache::cacheType
sol::solType
subintegrator_tree::subintTreeType
solution_index_tree::solidxTreeType
synchronizer_tree::syncTreeType
iter::Int
controller::controllerType
opts::optionsType
stats::IntegratorStats
tdir::tType
end
# called by DiffEqBase.init and DiffEqBase.solve
function SciMLBase.__init(
prob::OperatorSplittingProblem,
alg::AbstractOperatorSplittingAlgorithm,
args...;
dt,
tstops = (),
saveat = (),
d_discontinuities = (),
save_everystep = false,
callback = nothing,
advance_to_tstop = false,
adaptive = isadaptive(alg),
controller = nothing,
alias_u0 = false,
verbose = true,
kwargs...
)
(; u0, p) = prob
t0, tf = prob.tspan
dt > zero(dt) || error("dt must be positive")
dtcache = dt
dt = tf > t0 ? dt : -dt
tType = typeof(dt)
# Warn if the algorithm is non-adaptive but the user tries to make it adaptive.
(!isadaptive(alg) && adaptive && verbose) && warn("The algorithm $alg is not adaptive.")
dtchangeable = true # isadaptive(alg)
if tstops isa AbstractArray || tstops isa Tuple || tstops isa Number
_tstops = nothing
else
_tstops = tstops
tstops = ()
end
# Setup tstop logic
tstops_internal = OrdinaryDiffEqCore.initialize_tstops(
tType, tstops, d_discontinuities, prob.tspan
)
saveat_internal = OrdinaryDiffEqCore.initialize_saveat(tType, saveat, prob.tspan)
d_discontinuities_internal = OrdinaryDiffEqCore.initialize_d_discontinuities(
tType, d_discontinuities, prob.tspan
)
u = setup_u(prob, alg, alias_u0)
uprev = setup_u(prob, alg, false)
tmp = setup_u(prob, alg, false)
uType = typeof(u)
sol = SciMLBase.build_solution(prob, alg, tType[], uType[])
callback = DiffEqBase.CallbackSet(callback)
subintegrator_tree,
cache = build_subintegrator_tree_with_cache(
prob, alg,
uprev, u,
1:length(u),
t0, dt, tf,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose
)
integrator = OperatorSplittingIntegrator(
prob.f,
alg,
u,
uprev,
tmp,
p,
t0,
copy(t0),
dt,
dtcache,
dtchangeable,
tstops_internal,
tstops,
saveat_internal,
saveat,
callback,
advance_to_tstop,
false,
false,
false,
false,
cache,
sol,
subintegrator_tree,
build_solution_index_tree(prob.f),
build_synchronizer_tree(prob.f),
0,
controller,
IntegratorOptions(; verbose, adaptive),
IntegratorStats(),
tType(tstops_internal.ordering isa DataStructures.FasterForward ? 1 : -1)
)
DiffEqBase.initialize!(callback, u0, t0, integrator) # Do I need this?
return integrator
end
SciMLBase.has_reinit(integrator::OperatorSplittingIntegrator) = true
function DiffEqBase.reinit!(
integrator::OperatorSplittingIntegrator,
u0 = integrator.sol.prob.u0;
t0 = integrator.sol.prob.tspan[1],
tf = integrator.sol.prob.tspan[2],
dt = isadaptive(integrator) ? nothing : integrator.dtcache,
erase_sol = false,
tstops = integrator._tstops,
saveat = integrator._saveat,
reinit_callbacks = true,
reinit_retcode = true
)
integrator.u .= u0
integrator.uprev .= u0
integrator.t = t0
integrator.tprev = t0
if dt !== nothing
integrator.dt = dt
end
integrator.tstops, integrator.saveat = tstops_and_saveat_heaps(t0, tf, tstops, saveat)
integrator.iter = 0
if erase_sol
resize!(integrator.sol.t, 0)
resize!(integrator.sol.u, 0)
end
if reinit_callbacks
DiffEqBase.initialize!(integrator.callback, u0, t0, integrator)
else # always reinit the saving callback so that t0 can be saved if needed
saving_callback = integrator.callback.discrete_callbacks[end]
DiffEqBase.initialize!(saving_callback, u0, t0, integrator)
end
if reinit_retcode
integrator.sol = SciMLBase.solution_new_retcode(
integrator.sol, ReturnCode.Default
)
end
return subreinit!(
integrator.f,
u0,
1:length(u0),
integrator.subintegrator_tree;
t0, tf, dt,
erase_sol,
tstops,
saveat,
reinit_callbacks,
reinit_retcode
)
end
function subreinit!(
f,
u0,
solution_indices,
subintegrator::DEIntegrator;
dt,
kwargs...
)
# dt is not reset as expected in reinit!
if dt !== nothing
subintegrator.dt = dt
end
return DiffEqBase.reinit!(subintegrator, u0[solution_indices]; kwargs...)
end
@unroll function subreinit!(
f,
u0,
solution_indices,
subintegrators::Tuple;
kwargs...
)
i = 1
@unroll for subintegrator in subintegrators
subreinit!(get_operator(f, i), u0, f.solution_indices[i], subintegrator; kwargs...)
i += 1
end
end
function OrdinaryDiffEqCore.handle_tstop!(integrator::OperatorSplittingIntegrator)
if SciMLBase.has_tstop(integrator)
tdir_t = tdir(integrator) * integrator.t
tdir_tstop = SciMLBase.first_tstop(integrator)
if tdir_t == tdir_tstop
while tdir_t == tdir_tstop #remove all redundant copies
res = SciMLBase.pop_tstop!(integrator)
SciMLBase.has_tstop(integrator) ?
(tdir_tstop = SciMLBase.first_tstop(integrator)) : break
end
notify_integrator_hit_tstop!(integrator)
elseif tdir_t > tdir_tstop
if !integrator.dtchangeable
SciMLBase.change_t_via_interpolation!(
integrator,
tdir(integrator) *
SciMLBase.pop_tstop!(integrator), Val{true}
)
notify_integrator_hit_tstop!(integrator)
else
error("Something went wrong. Integrator stepped past tstops but the algorithm was dtchangeable. Please report this error.")
end
end
end
return nothing
end
notify_integrator_hit_tstop!(integrator::OperatorSplittingIntegrator) = nothing
is_first_iteration(integrator::OperatorSplittingIntegrator) = integrator.iter == 0
increment_iteration(integrator::OperatorSplittingIntegrator) = integrator.iter += 1
# Controller interface
function reject_step!(integrator::OperatorSplittingIntegrator)
OrdinaryDiffEqCore.increment_reject!(integrator.stats)
return reject_step!(integrator, integrator.cache, integrator.controller)
end
function reject_step!(integrator::OperatorSplittingIntegrator, cache, controller)
return integrator.u .= integrator.uprev
# TODO what do we need to do with the subintegrators?
end
function reject_step!(integrator::OperatorSplittingIntegrator, cache, ::Nothing)
return if length(integrator.uprev) == 0
error("Cannot roll back integrator. Aborting time integration step at $(integrator.t).")
end
end
# Solution looping interface
function should_accept_step(integrator::OperatorSplittingIntegrator)
if integrator.force_stepfail || integrator.isout
return false
end
return should_accept_step(integrator, integrator.cache, integrator.controller)
end
function should_accept_step(integrator::OperatorSplittingIntegrator, cache, ::Nothing)
return !(integrator.force_stepfail)
end
function accept_step!(integrator::OperatorSplittingIntegrator)
OrdinaryDiffEqCore.increment_accept!(integrator.stats)
return accept_step!(integrator, integrator.cache, integrator.controller)
end
function accept_step!(integrator::OperatorSplittingIntegrator, cache, controller)
return store_previous_info!(integrator)
end
function store_previous_info!(integrator::OperatorSplittingIntegrator)
return if length(integrator.uprev) > 0 # Integrator can rollback
update_uprev!(integrator)
end
end
function update_uprev!(integrator::OperatorSplittingIntegrator)
RecursiveArrayTools.recursivecopy!(integrator.uprev, integrator.u)
return nothing
end
function step_header!(integrator::OperatorSplittingIntegrator)
# Accept or reject the step
if !is_first_iteration(integrator)
if should_accept_step(integrator)
accept_step!(integrator)
else # Step should be rejected and hence repeated
reject_step!(integrator)
end
elseif integrator.u_modified # && integrator.iter == 0
update_uprev!(integrator)
end
# Before stepping we might need to adjust the dt
increment_iteration(integrator)
# OrdinaryDiffEqCore.choose_algorithm!(integrator, integrator.cache)
OrdinaryDiffEqCore.fix_dt_at_bounds!(integrator)
OrdinaryDiffEqCore.modify_dt_for_tstops!(integrator)
return integrator.force_stepfail = false
end
function footer_reset_flags!(integrator)
return integrator.u_modified = false
end
function setup_validity_flags!(integrator, t_next)
return integrator.isout = false #integrator.opts.isoutofdomain(integrator.u, integrator.p, t_next)
end
function fix_solution_buffer_sizes!(integrator, sol)
resize!(integrator.sol.t, integrator.saveiter)
resize!(integrator.sol.u, integrator.saveiter)
return if !(integrator.sol isa SciMLBase.DAESolution)
resize!(integrator.sol.k, integrator.saveiter_dense)
end
end
function step_footer!(integrator::OperatorSplittingIntegrator)
ttmp = integrator.t + tdir(integrator) * integrator.dt
footer_reset_flags!(integrator)
setup_validity_flags!(integrator, ttmp)
if should_accept_step(integrator)
integrator.last_step_failed = false
integrator.tprev = integrator.t
integrator.t = ttmp #OrdinaryDiffEqCore.fixed_t_for_floatingpoint_error!(integrator, ttmp)
# OrdinaryDiffEqCore.handle_callbacks!(integrator)
step_accept_controller!(integrator) # Noop for non-adaptive algorithms
elseif integrator.force_stepfail
if isadaptive(integrator)
step_reject_controller!(integrator)
OrdinaryDiffEqCore.post_newton_controller!(integrator, integrator.alg)
elseif integrator.dtchangeable # Non-adaptive but can change dt
integrator.dt /= integrator.opts.failfactor
elseif integrator.last_step_failed
return
end
integrator.last_step_failed = true
end
# integration_monitor_step(integrator)
return nothing
end
# called by DiffEqBase.solve
function SciMLBase.__solve(
prob::OperatorSplittingProblem,
alg::AbstractOperatorSplittingAlgorithm, args...; kwargs...
)
integrator = SciMLBase.__init(prob, alg, args...; kwargs...)
return DiffEqBase.solve!(integrator)
end
# either called directly (after init), or by DiffEqBase.solve (via __solve)
function DiffEqBase.solve!(integrator::OperatorSplittingIntegrator)
while !isempty(integrator.tstops)
while tdir(integrator) * integrator.t < SciMLBase.first_tstop(integrator)
step_header!(integrator)
@timeit_debug "check_error" SciMLBase.check_error!(integrator) ∉ (
ReturnCode.Success, ReturnCode.Default,
) && return
__step!(integrator)
step_footer!(integrator)
if !SciMLBase.has_tstop(integrator)
break
end
end
OrdinaryDiffEqCore.handle_tstop!(integrator)
end
SciMLBase.postamble!(integrator)
if integrator.sol.retcode != ReturnCode.Default
return integrator.sol
end
return integrator.sol = SciMLBase.solution_new_retcode(
integrator.sol, ReturnCode.Success
)
end
function DiffEqBase.step!(integrator::OperatorSplittingIntegrator)
@timeit_debug "step!" if integrator.advance_to_tstop
tstop = first_tstop(integrator)
while !reached_tstop(integrator, tstop)
step_header!(integrator)
@timeit_debug "check_error" SciMLBase.check_error!(integrator) ∉ (
ReturnCode.Success, ReturnCode.Default,
) && return
__step!(integrator)
step_footer!(integrator)
if !SciMLBase.has_tstop(integrator)
break
end
end
else
step_header!(integrator)
@timeit_debug "check_error" SciMLBase.check_error!(integrator) ∉ (
ReturnCode.Success, ReturnCode.Default,
) && return
__step!(integrator)
step_footer!(integrator)
while !should_accept_step(integrator)
step_header!(integrator)
@timeit_debug "check_error" SciMLBase.check_error!(integrator) ∉ (
ReturnCode.Success, ReturnCode.Default,
) && return
__step!(integrator)
step_footer!(integrator)
end
end
return OrdinaryDiffEqCore.handle_tstop!(integrator)
end
function SciMLBase.check_error(integrator::OperatorSplittingIntegrator)
if !SciMLBase.successful_retcode(integrator.sol) &&
integrator.sol.retcode != ReturnCode.Default
return integrator.sol.retcode
end
verbose = true # integrator.opts.verbose
if DiffEqBase.NAN_CHECK(integrator.dtcache) || DiffEqBase.NAN_CHECK(integrator.dt) # replace with https://github.com/SciML/OrdinaryDiffEq.jl/blob/373a8eec8024ef1acc6c5f0c87f479aa0cf128c3/lib/OrdinaryDiffEqCore/src/iterator_interface.jl#L5-L6 after moving to sciml integrators
if verbose
@warn("NaN dt detected. Likely a NaN value in the state, parameters, or derivative value caused this outcome.")
end
return ReturnCode.DtNaN
end
return check_error_subintegrators(integrator, integrator.subintegrator_tree)
end
function check_error_subintegrators(integrator, subintegrator_tree::Tuple)
for subintegrator in subintegrator_tree
retcode = check_error_subintegrators(integrator, subintegrator)
if !SciMLBase.successful_retcode(retcode) && retcode != ReturnCode.Default
return retcode
end
end
return integrator.sol.retcode
end
function check_error_subintegrators(integrator, subintegrator::DEIntegrator)
return SciMLBase.check_error(subintegrator)
end
function DiffEqBase.step!(integrator::OperatorSplittingIntegrator, dt, stop_at_tdt = false)
return @timeit_debug "step!" begin
# OridinaryDiffEq lets dt be negative if tdir is -1, but that's inconsistent
dt <= zero(dt) && error("dt must be positive")
stop_at_tdt && !integrator.dtchangeable &&
error("Cannot stop at t + dt if dtchangeable is false")
tnext = integrator.t + tdir(integrator) * dt
stop_at_tdt && DiffEqBase.add_tstop!(integrator, tnext)
while !reached_tstop(integrator, tnext, stop_at_tdt)
step_header!(integrator)
@timeit_debug "check_error" SciMLBase.check_error!(integrator) ∉ (
ReturnCode.Success, ReturnCode.Default,
) && return
__step!(integrator)
step_footer!(integrator)
end
end
end
function setup_u(prob::OperatorSplittingProblem, solver, alias_u0)
if alias_u0
return prob.u0
else
return RecursiveArrayTools.recursivecopy(prob.u0)
end
end
# TimeChoiceIterator API
@inline function DiffEqBase.get_tmp_cache(integrator::OperatorSplittingIntegrator)
# DiffEqBase.get_tmp_cache(integrator, integrator.alg, integrator.cache)
return (integrator.tmp,)
end
# @inline function DiffEqBase.get_tmp_cache(integrator::OperatorSplittingIntegrator, ::AbstractOperatorSplittingAlgorithm, cache)
# return (cache.tmp,)
# end
# Interpolation
# TODO via https://github.com/SciML/SciMLBase.jl/blob/master/src/interpolation.jl
function linear_interpolation!(y, t, y1, y2, t1, t2)
return y .= y1 + (t - t1) * (y2 - y1) / (t2 - t1)
end
function (integrator::OperatorSplittingIntegrator)(tmp, t)
return linear_interpolation!(
tmp, t, integrator.uprev, integrator.u, integrator.tprev, integrator.t
)
end
"""
stepsize_controller!(::OperatorSplittingIntegrator)
Updates the controller using the current state of the integrator if the operator splitting algorithm is adaptive.
"""
@inline function stepsize_controller!(integrator::OperatorSplittingIntegrator)
algorithm = integrator.alg
isadaptive(algorithm) || return nothing
return stepsize_controller!(integrator, algorithm)
end
"""
step_accept_controller!(::OperatorSplittingIntegrator)
Updates `dtcache` of the integrator if the step is accepted and the operator splitting algorithm is adaptive.
"""
@inline function step_accept_controller!(integrator::OperatorSplittingIntegrator)
algorithm = integrator.alg
isadaptive(algorithm) || return nothing
return step_accept_controller!(integrator, algorithm, nothing)
end
"""
step_reject_controller!(::OperatorSplittingIntegrator)
Updates `dtcache` of the integrator if the step is rejected and the the operator splitting algorithm is adaptive.
"""
@inline function step_reject_controller!(integrator::OperatorSplittingIntegrator)
algorithm = integrator.alg
isadaptive(algorithm) || return nothing
return step_reject_controller!(integrator, algorithm, nothing)
end
# helper functions for dealing with time-reversed integrators in the same way
# that OrdinaryDiffEq.jl does
tdir(integrator) = integrator.tstops.ordering isa DataStructures.FasterForward ? 1 : -1
is_past_t(integrator, t) = tdir(integrator) * (t - integrator.t) ≤ zero(integrator.t)
function reached_tstop(integrator, tstop, stop_at_tstop = integrator.dtchangeable)
if stop_at_tstop
integrator.t > tstop &&
error("Integrator missed stop at $tstop (current time=$(integrator.t)). Aborting.")
return integrator.t == tstop # Check for exact hit
else #!stop_at_tstop
return is_past_t(integrator, tstop)
end
end
# Dunno stuff
function SciMLBase.done(integrator::OperatorSplittingIntegrator)
if !(
integrator.sol.retcode in (
ReturnCode.Default, ReturnCode.Success,
)
)
return true
elseif isempty(integrator.tstops)
SciMLBase.postamble!(integrator)
return true
end
return false
end
function SciMLBase.postamble!(integrator::OperatorSplittingIntegrator)
return DiffEqBase.finalize!(integrator.callback, integrator.u, integrator.t, integrator)
end
function __step!(integrator)
tnext = integrator.t + integrator.dt
synchronize_subintegrator_tree!(integrator)
advance_solution_to!(integrator, tnext)
return stepsize_controller!(integrator)
end
# solvers need to define this interface
function advance_solution_to!(integrator::OperatorSplittingIntegrator, tnext)
return advance_solution_to!(integrator, integrator.cache, tnext)
end
function advance_solution_to!(
outer_integrator::OperatorSplittingIntegrator,
integrator::DEIntegrator, solution_indices, sync, cache, tend
)
dt = tend - integrator.t
return SciMLBase.step!(integrator, dt, true)
end
# ----------------------------------- SciMLBase.jl Integrator Interface ------------------------------------
SciMLBase.has_stats(::OperatorSplittingIntegrator) = true
SciMLBase.has_tstop(integrator::OperatorSplittingIntegrator) = !isempty(integrator.tstops)
SciMLBase.first_tstop(integrator::OperatorSplittingIntegrator) = first(integrator.tstops)
SciMLBase.pop_tstop!(integrator::OperatorSplittingIntegrator) = pop!(integrator.tstops)
DiffEqBase.get_dt(integrator::OperatorSplittingIntegrator) = integrator.dt
function set_dt!(integrator::OperatorSplittingIntegrator, dt)
# TODO: figure out interface for recomputing other objects (linear operators, etc)
dt <= zero(dt) && error("dt must be positive")
return integrator.dt = dt
end
function DiffEqBase.add_tstop!(integrator::OperatorSplittingIntegrator, t)
is_past_t(integrator, t) &&
error("Cannot add a tstop at $t because that is behind the current \
integrator time $(integrator.t)")
return push!(integrator.tstops, t)
end
function DiffEqBase.add_saveat!(integrator::OperatorSplittingIntegrator, t)
is_past_t(integrator, t) &&
error("Cannot add a saveat point at $t because that is behind the \
current integrator time $(integrator.t)")
return push!(integrator.saveat, t)
end
# not sure what this should do?
# defined as default initialize: https://github.com/SciML/DiffEqBase.jl/blob/master/src/callbacks.jl#L3
DiffEqBase.u_modified!(i::OperatorSplittingIntegrator, bool) = nothing
function synchronize_subintegrator_tree!(integrator::OperatorSplittingIntegrator)
return synchronize_subintegrator!(integrator.subintegrator_tree, integrator)
end
@unroll function synchronize_subintegrator!(
subintegrator_tree::Tuple, integrator::OperatorSplittingIntegrator
)
@unroll for subintegrator in subintegrator_tree
synchronize_subintegrator!(subintegrator, integrator)
end
end
function synchronize_subintegrator!(
subintegrator::DEIntegrator, integrator::OperatorSplittingIntegrator
)
(; t, dt) = integrator
@assert subintegrator.t == t
return if !isadaptive(subintegrator)
SciMLBase.set_proposed_dt!(subintegrator, dt)
end
end
function advance_solution_to!(
integrator::OperatorSplittingIntegrator,
cache::AbstractOperatorSplittingCache, tnext::Number
)
return advance_solution_to!(
integrator, integrator.subintegrator_tree, integrator.solution_index_tree,
integrator.synchronizer_tree, cache, tnext
)
end
# Dispatch for tree node construction
function build_subintegrator_tree_with_cache(
prob::OperatorSplittingProblem, alg::AbstractOperatorSplittingAlgorithm,
uprevouter::AbstractVector, uouter::AbstractVector,
solution_indices,
t0, dt, tf,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose
)
(; f, p) = prob
subintegrator_tree_with_caches = ntuple(
i -> build_subintegrator_tree_with_cache(
prob,
alg.inner_algs[i],
get_operator(f, i),
p[i],
uprevouter, uouter,
f.solution_indices[i],
t0, dt, tf,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose
),
length(f.functions)
)
subintegrator_tree = ntuple(
i -> subintegrator_tree_with_caches[i][1], length(f.functions)
)
caches = ntuple(i -> subintegrator_tree_with_caches[i][2], length(f.functions))
# TODO fix mixed device type problems we have to be smarter
return subintegrator_tree,
init_cache(
f, alg;
uprev = uprevouter, u = uouter, alias_u = true,
inner_caches = caches
)
end
function build_subintegrator_tree_with_cache(
prob::OperatorSplittingProblem, alg::AbstractOperatorSplittingAlgorithm,
f::GenericSplitFunction, p::Tuple,
uprevouter::AbstractVector, uouter::AbstractVector,
solution_indices,
t0, dt, tf,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose,
save_end = false,
controller = nothing
)
subintegrator_tree_with_caches = ntuple(
i -> build_subintegrator_tree_with_cache(
prob,
alg.inner_algs[i],
get_operator(f, i),
p[i],
uprevouter, uouter,
f.solution_indices[i],
t0, dt, tf,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose
),
length(f.functions)
)
subintegrator_tree = first.(subintegrator_tree_with_caches)
inner_caches = last.(subintegrator_tree_with_caches)
# TODO fix mixed device type problems we have to be smarter
uprev = @view uprevouter[solution_indices]
u = @view uouter[solution_indices]
return subintegrator_tree,
init_cache(
f, alg;
uprev = uprev, u = u,
inner_caches = inner_caches
)
end
function build_subintegrator_tree_with_cache(
prob::OperatorSplittingProblem,
alg::SciMLBase.AbstractODEAlgorithm,
f::F, p::P,
uprevouter::S, uouter::S,
solution_indices,
t0::T, dt::T, tf::T,
tstops, saveat, d_discontinuities, callback,
adaptive, verbose,
save_end = false,
controller = nothing
) where {S, T, P, F}
uprev = @view uprevouter[solution_indices]
u = @view uouter[solution_indices]
# When working with MTK, we want to pass f as a system down here.
# In that case ODEProblem constructs the correct parameter struct.
# If the system does not have parameters in first place, then
# The NullParameters object will be constructed automatically.
prob2 = if p isa NullParameters
SciMLBase.ODEProblem(f, u, (t0, min(t0 + dt, tf)))
else
SciMLBase.ODEProblem(f, u, (t0, min(t0 + dt, tf)), p)
end
integrator = SciMLBase.__init(
prob2,
alg;
dt,
saveat = (),
d_discontinuities,
save_everystep = false,
advance_to_tstop = false,
adaptive,
controller,
verbose
)
return integrator, integrator.cache
end
function forward_sync_subintegrator!(
outer_integrator::OperatorSplittingIntegrator, subintegrator_tree::Tuple,
solution_indices::Tuple, synchronizers::Tuple
)
return nothing
end
function backward_sync_subintegrator!(
outer_integrator::OperatorSplittingIntegrator,
subintegrator_tree::Tuple, solution_indices::Tuple, synchronizer::Tuple
)
return nothing
end