Skip to content

Commit 115b8db

Browse files
committed
fix: fix SII issue in the implicit affect
1 parent 15dc5e2 commit 115b8db

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/systems/callbacks.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function make_affect(affect::Vector{Equation}; discrete_parameters = Any[],
283283
for eq in affect
284284
if !haspre(eq) && !(symbolic_type(eq.rhs) === NotSymbolic() ||
285285
symbolic_type(eq.lhs) === NotSymbolic())
286-
@warn "Affect equation $eq has no `Pre` operator. As such it will be interpreted as an algebraic equation to be satisfied after the callback. If you intended to use the value of a variable x before the affect, use Pre(x). Errors may be thrown if there is no `Pre` and algebraic equations are unsatisfiable, such as X ~ X + 1."
286+
@warn "Affect equation $eq has no `Pre` operator. As such it will be interpreted as an algebraic equation to be satisfied after the callback. If you intended to use the value of a variable x before the affect, use Pre(x). Errors may be thrown if there is no `Pre` and the algebraic equation is unsatisfiable, such as X ~ X + 1."
287287
end
288288
collect_vars!(dvs, params, eq, iv; op = Pre)
289289
diffvs = collect_applied_operators(eq, Differential)
@@ -939,31 +939,36 @@ function compile_equational_affect(
939939
return let dvs_to_update = dvs_to_update, aff_map = aff_map, sys_map = sys_map,
940940
affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys
941941

942-
dvs_to_access = unknowns(affsys)
943-
ps_to_access = parameters(affsys)
942+
dvs_to_access = [aff_map[u] for u in unknowns(affsys)]
943+
ps_to_access = [unPre(p) for p in parameters(affsys)]
944944

945-
u_getter = getsym(sys, [aff_map[u] for u in dvs_to_access])
946-
p_getter = getsym(sys, [unPre(p) for p in ps_to_access])
945+
affu_getter = getsym(sys, dvs_to_access)
946+
affp_getter = getsym(sys, ps_to_access)
947+
affu_setter! = setsym(sys, unknowns(affsys))
948+
affp_setter! = setsym(sys, parameters(affsys))
947949
u_setter! = setsym(sys, dvs_to_update)
948950
p_setter! = setsym(sys, ps_to_update)
949-
affu_getter = getsym(affsys, [sys_map[u] for u in dvs_to_update])
950-
affp_getter = getsym(affsys, [sys_map[p] for p in ps_to_update])
951+
u_getter = getsym(affsys, [sys_map[u] for u in dvs_to_update])
952+
p_getter = getsym(affsys, [sys_map[p] for p in ps_to_update])
951953

952-
affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in dvs_to_access],
953-
(0, 0), [p => 0 for p in ps_to_access];
954+
affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in unknowns(affsys)],
955+
(0, 0), [p => 0. for p in parameters(affsys)];
954956
build_initializeprob = false, check_length = false)
955957

956958
function implicit_affect!(integ)
957-
new_us = u_getter(integ)
958-
new_ps = p_getter(integ)
959+
new_u0 = affu_getter(integ)
960+
affu_setter!(affporb, new_u0)
961+
new_ps = affp_getter(integ)
962+
affp_setter!(affprob, new_ps)
963+
959964
affprob = remake(
960-
affprob, u0 = new_us, p = new_ps, tspan = (integ.t, integ.t))
965+
affprob, tspan = (integ.t, integ.t))
961966
affsol = init(affprob, IDSolve())
962967
(check_error(affsol) === ReturnCode.InitialFailure) &&
963968
throw(UnsolvableCallbackError(all_equations(aff)))
964969

965-
u_setter!(integ, affu_getter(affsol))
966-
p_setter!(integ, affp_getter(affsol))
970+
u_setter!(integ, u_getter(affsol))
971+
p_setter!(integ, p_getter(affsol))
967972
end
968973
end
969974
end

src/systems/discrete_system/implicit_discrete_system.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}(
384384
else
385385
resid_prototype = calculate_resid_prototype(length(equations(sys)), u0, p)
386386
end
387+
out = zeros(length(dvs))
388+
u = zeros(length(dvs))
387389

388390
if specialize === SciMLBase.FunctionWrapperSpecialize && iip
389391
if u0 === nothing || p === nothing || t === nothing

0 commit comments

Comments
 (0)