Skip to content

Commit 4f928ae

Browse files
committed
Support time-indexed parameters
1 parent 95956f3 commit 4f928ae

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/systems/callbacks.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ function unassignable_variables(sys, expr)
963963
x -> !any(isequal(x), assignable_syms), reduce(vcat, vars(expr); init = []))
964964
end
965965

966-
function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwargs...)
966+
function compile_user_affect(affect::MutatingFunctionalAffect, cb, sys, dvs, ps; kwargs...)
967967
#=
968968
Implementation sketch:
969969
generate observed function (oop), should save to a component array under obs_syms
@@ -1049,6 +1049,13 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa
10491049
collect(mkzero(size(e)) for e in first.(mod_unk_pairs))]))
10501050
upd_params_view = view(upd_component_array, last.(mod_param_pairs))
10511051
upd_unks_view = view(upd_component_array, last.(mod_unk_pairs))
1052+
1053+
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
1054+
save_idxs = get(ic.callback_to_clocks, cb, Int[])
1055+
else
1056+
save_idxs = Int[]
1057+
end
1058+
10521059
let user_affect = func(affect), ctx = context(affect)
10531060
function (integ)
10541061
# update the to-be-mutated values; this ensures that if you do a no-op then nothing happens
@@ -1074,11 +1081,16 @@ function compile_user_affect(affect::MutatingFunctionalAffect, sys, dvs, ps; kwa
10741081
# write the new values back to the integrator
10751082
upd_params_fun(integ, upd_params_view)
10761083
upd_unk_fun(integ, upd_unks_view)
1084+
1085+
1086+
for idx in save_idxs
1087+
SciMLBase.save_discretes!(integ, idx)
1088+
end
10771089
end
10781090
end
10791091
end
10801092

1081-
function compile_affect(affect::FunctionalAffect, cb, sys, dvs, ps; kwargs...)
1093+
function compile_affect(affect::Union{FunctionalAffect, MutatingFunctionalAffect}, cb, sys, dvs, ps; kwargs...)
10821094
compile_user_affect(affect, cb, sys, dvs, ps; kwargs...)
10831095
end
10841096

src/systems/index_cache.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function IndexCache(sys::AbstractSystem)
126126
for affect in affs
127127
if affect isa Equation
128128
is_parameter(sys, affect.lhs) && push!(discs, affect.lhs)
129-
elseif affect isa FunctionalAffect
129+
elseif affect isa FunctionalAffect || affect isa MutatingFunctionalAffect
130130
union!(discs, unwrap.(discretes(affect)))
131131
else
132132
error("Unhandled affect type $(typeof(affect))")

0 commit comments

Comments
 (0)