@@ -565,8 +565,22 @@ function generate_single_rootfinding_callback(
565
565
rf_oop (u, parameter_values (integ), t)
566
566
end
567
567
end
568
+
569
+ if has_index_cache (sys) && (ic = get_index_cache (sys)) != = nothing &&
570
+ (save_idxs = get (ic. callback_to_clocks, cb, nothing )) != = nothing
571
+ initfn = let save_idxs = save_idxs
572
+ function (cb, u, t, integrator)
573
+ for idx in save_idxs
574
+ SciMLBase. save_discretes! (integrator, idx)
575
+ end
576
+ end
577
+ end
578
+ else
579
+ initfn = SciMLBase. INITIALIZE_DEFAULT
580
+ end
568
581
return ContinuousCallback (
569
- cond, affect_function. affect, affect_function. affect_neg, rootfind = cb. rootfind)
582
+ cond, affect_function. affect, affect_function. affect_neg,
583
+ rootfind = cb. rootfind, initialize = initfn)
570
584
end
571
585
572
586
function generate_vector_rootfinding_callback (
@@ -618,8 +632,25 @@ function generate_vector_rootfinding_callback(
618
632
affect_neg (integ)
619
633
end
620
634
end
635
+ if has_index_cache (sys) && (ic = get_index_cache (sys)) != = nothing
636
+ save_idxs = mapreduce (
637
+ cb -> get (ic. callback_to_clocks, cb, Int[]), vcat, cbs; init = Int[])
638
+ initfn = if isempty (save_idxs)
639
+ SciMLBase. INITIALIZE_DEFAULT
640
+ else
641
+ let save_idxs = save_idxs
642
+ function (cb, u, t, integrator)
643
+ for idx in save_idxs
644
+ SciMLBase. save_discretes! (integrator, idx)
645
+ end
646
+ end
647
+ end
648
+ end
649
+ else
650
+ initfn = SciMLBase. INITIALIZE_DEFAULT
651
+ end
621
652
return VectorContinuousCallback (
622
- cond, affect, affect_neg, length (eqs), rootfind = rootfind)
653
+ cond, affect, affect_neg, length (eqs), rootfind = rootfind, initialize = initfn )
623
654
end
624
655
625
656
"""
@@ -727,12 +758,24 @@ function generate_timed_callback(cb, sys, dvs, ps; postprocess_affect_expr! = no
727
758
cond = condition (cb)
728
759
as = compile_affect (affects (cb), cb, sys, dvs, ps; expression = Val{false },
729
760
postprocess_affect_expr!, kwargs... )
761
+ if has_index_cache (sys) && (ic = get_index_cache (sys)) != = nothing &&
762
+ (save_idxs = get (ic. callback_to_clocks, cb, nothing )) != = nothing
763
+ initfn = let save_idxs = save_idxs
764
+ function (cb, u, t, integrator)
765
+ for idx in save_idxs
766
+ SciMLBase. save_discretes! (integrator, idx)
767
+ end
768
+ end
769
+ end
770
+ else
771
+ initfn = SciMLBase. INITIALIZE_DEFAULT
772
+ end
730
773
if cond isa AbstractVector
731
774
# Preset Time
732
- return PresetTimeCallback (cond, as)
775
+ return PresetTimeCallback (cond, as; initialize = initfn )
733
776
else
734
777
# Periodic
735
- return PeriodicCallback (as, cond)
778
+ return PeriodicCallback (as, cond; initialize = initfn )
736
779
end
737
780
end
738
781
@@ -745,7 +788,19 @@ function generate_discrete_callback(cb, sys, dvs, ps; postprocess_affect_expr! =
745
788
c = compile_condition (cb, sys, dvs, ps; expression = Val{false }, kwargs... )
746
789
as = compile_affect (affects (cb), cb, sys, dvs, ps; expression = Val{false },
747
790
postprocess_affect_expr!, kwargs... )
748
- return DiscreteCallback (c, as)
791
+ if has_index_cache (sys) && (ic = get_index_cache (sys)) != = nothing &&
792
+ (save_idxs = get (ic. callback_to_clocks, cb, nothing )) != = nothing
793
+ initfn = let save_idxs = save_idxs
794
+ function (cb, u, t, integrator)
795
+ for idx in save_idxs
796
+ SciMLBase. save_discretes! (integrator, idx)
797
+ end
798
+ end
799
+ end
800
+ else
801
+ initfn = SciMLBase. INITIALIZE_DEFAULT
802
+ end
803
+ return DiscreteCallback (c, as; initialize = initfn)
749
804
end
750
805
end
751
806
0 commit comments