Skip to content

Commit f9fed87

Browse files
fix: fix 0 ~ 0 equations from unhack_system
1 parent 0d1ca3e commit f9fed87

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ function MTKBase.unhack_system(sys::System)
123123
push!(additional_eqs, Symbolics.COMMON_ZERO ~ res)
124124
end
125125
end
126+
@assert length(additional_eqs) == length(additional_vars)
127+
# If a linear SCC contains both `D(w)` and `w_t`, it'll contain the equation `D(w) ~ w_t`.
128+
# When unhacking it, `D(w)` will be totermed into `w_t`. This, `additional_vars` contains
129+
# two `w_t` and an equation that is `0 ~ 0`. Find the `0 ~ 0` equations, and remove them
130+
# along with the duplicate variables.
131+
# See https://github.com/SciML/ModelingToolkit.jl/issues/4196 for further details.
132+
additional_eqs_mask = trues(length(additional_eqs))
133+
for (i, eq) in enumerate(additional_eqs)
134+
additional_eqs_mask[i] = !SU._iszero(eq.rhs)
135+
end
136+
additional_eqs = additional_eqs[additional_eqs_mask]
137+
additional_vars = additional_vars[additional_eqs_mask]
126138
subst = SU.Substituter{false}(additional_subs, SU.default_substitute_filter)
127139
obseqs = obseqs[obs_mask]
128140
map!(subst, obseqs, obseqs)

0 commit comments

Comments
 (0)