Skip to content

Commit b726810

Browse files
committed
fix: fix implicit_affect
1 parent 29cdece commit b726810

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/systems/callbacks.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ Compile an affect defined by a set of equations. Systems with algebraic equation
887887
function compile_equational_affect(
888888
aff::Union{AffectSystem, Vector{Equation}}, sys; reset_jumps = false, kwargs...)
889889
if aff isa AbstractVector
890-
aff = make_affect(aff; iv = get_iv(sys), warn_no_algebraic = false)
890+
aff = make_affect(aff; iv = get_iv(sys), alg_eqs = alg_equations(sys), warn_no_algebraic = false)
891891
end
892892
affsys = system(aff)
893893
ps_to_update = discretes(aff)
@@ -925,7 +925,7 @@ function compile_equational_affect(
925925
wrap_code = add_integrator_header(sys, integ, :p),
926926
expression = Val{false}, outputidxs = p_idxs, wrap_mtkparameters, cse = false)
927927

928-
return let dvs_to_update = dvs_to_update, ps_to_update = ps_to_update, reset_jump = reset_jump, u_up! = u_up!, p_up! = p_up!
928+
return let dvs_to_update = dvs_to_update, ps_to_update = ps_to_update, reset_jumps = reset_jumps, u_up! = u_up!, p_up! = p_up!
929929
function explicit_affect!(integ)
930930
isempty(dvs_to_update) || u_up!(integ)
931931
isempty(ps_to_update) || p_up!(integ)
@@ -936,28 +936,31 @@ function compile_equational_affect(
936936
return let dvs_to_update = dvs_to_update, aff_map = aff_map, sys_map = sys_map,
937937
affsys = affsys, ps_to_update = ps_to_update, aff = aff, sys = sys
938938

939-
affu_getters = [getsym(affsys, u) for u in unknowns(affsys)]
940-
affp_getters = [getsym(affsys, unPre(p)) for p in parameters(affsys)]
939+
dvs_to_access = unknowns(affsys)
940+
ps_to_access = parameters(affsys)
941+
942+
u_getters = [getsym(sys, aff_map[u]) for u in dvs_to_access]
943+
p_getters = [getsym(sys, unPre(p)) for p in ps_to_access]
941944
u_setters = [setsym(sys, u) for u in dvs_to_update]
942945
p_setters = [setsym(sys, p) for p in ps_to_update]
943-
solu_getters = [getsym(affsys, sys_map[u]) for u in dvs_to_update]
944-
solp_getters = [getsym(affsys, sys_map[p]) for p in ps_to_update]
946+
affu_getters = [getsym(affsys, sys_map[u]) for u in dvs_to_update]
947+
affp_getters = [getsym(affsys, sys_map[p]) for p in ps_to_update]
945948

946-
affprob = ImplicitDiscreteProblem(affsys, u0map, (integ.t, integ.t), pmap;
949+
affprob = ImplicitDiscreteProblem(affsys, [dv => 0 for dv in dvs_to_access], (0, 0), [p => 0 for p in ps_to_access];
947950
build_initializeprob = false, check_length = false)
948951

949952
function implicit_affect!(integ)
950-
pmap = [p => getp(integ) for (p, getp) in zip(parameters(affsys), p_getters)]
951-
u0map = [u => getu(integ) for (u, getu) in zip(unknowns(affsys), u_getters)]
952-
affprob = remake(affprob, u0 = u0map, p = pmap)
953+
pmap = Pair[p => getp(integ) for (p, getp) in zip(ps_to_access, p_getters)]
954+
u0map = Pair[u => getu(integ) for (u, getu) in zip(dvs_to_access, u_getters)]
955+
affprob = remake(affprob, u0 = u0map, p = pmap, tspan = (integ.t, integ.t))
953956
affsol = init(affprob, IDSolve())
954957
(check_error(affsol) === ReturnCode.InitialFailure) &&
955958
throw(UnsolvableCallbackError(all_equations(aff)))
956959

957-
for (setu!, getu) in zip(u_setters, solu_getters)
960+
for (setu!, getu) in zip(u_setters, affu_getters)
958961
setu!(integ, getu(affsol))
959962
end
960-
for (setp!, getp) in zip(p_setters, solp_getters)
963+
for (setp!, getp) in zip(p_setters, affp_getters)
961964
setp!(integ, getp(affsol))
962965
end
963966
end

test/symbolic_events.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ end
789789
@test sign.(cos.(required_crossings_c1 .- 1e-6)) == sign.(last.(cr1))
790790
@test sign.(cos.(3 * (required_crossings_c2 .+ 1e-6))) == sign.(last.(cr2))
791791
end
792-
#
792+
793793
@testset "Discrete event reinitialization (#3142)" begin
794794
@connector LiquidPort begin
795795
p(t)::Float64, [description = "Set pressure in bar",

0 commit comments

Comments
 (0)