Skip to content

Commit dc3c9cb

Browse files
committed
add check that lhs is a variable
1 parent b696d4b commit dc3c9cb

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/systems/callbacks.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ affect. The generated function has the signature `affect!(integrator)`.
9898
Notes
9999
- `expression = Val{true}`, causes the generated function to be returned as an expression.
100100
If set to `Val{false}` a `RuntimeGeneratedFunction` will be returned.
101-
- `outputidxs`, a vector of indices of the output variables.
101+
- `outputidxs`, a vector of indices of the output variables which should correspond to
102+
`states(sys)`. If provided checks that the LHS of affect equations are variables are
103+
dropped, i.e. it is assumed these indices are correct and affect equations are
104+
well-formed.
102105
- `kwargs` are passed through to `Symbolics.build_function`.
103106
"""
104107
function compile_affect(eqs::Vector{Equation}, sys, dvs, ps; outputidxs = nothing,
105-
expression = Val{true},
106-
kwargs...)
108+
expression = Val{true}, kwargs...)
107109
if isempty(eqs)
108110
if expression == Val{true}
109111
return :((args...) -> ())
@@ -115,6 +117,8 @@ function compile_affect(eqs::Vector{Equation}, sys, dvs, ps; outputidxs = nothin
115117

116118
if outputidxs === nothing
117119
lhss = map(x -> x.lhs, eqs)
120+
all(isvariable, lhss) ||
121+
error("Non-variable symbolic expression found on the left hand side of an affect equation. Such equations must be of the form variable ~ symbolic expression for the new value of the variable.")
118122
update_vars = collect(Iterators.flatten(map(ModelingToolkit.vars, lhss))) # these are the ones we're chaning
119123
length(update_vars) == length(unique(update_vars)) == length(eqs) ||
120124
error("affected variables not unique, each state can only be affected by one equation for a single `root_eqs => affects` pair.")

0 commit comments

Comments
 (0)