Skip to content

Commit a45c863

Browse files
Merge pull request #78 from JuliaComputing/as/fix-unhack-sys
fix: fix duplicate variable handling in `unhack_system`
2 parents aca60ab + 52b44ce commit a45c863

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

lib/ModelingToolkitTearing/src/ModelingToolkitTearing.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function MTKBase.unhack_system(sys::System)
107107
resize!(obs_mask, length(obseqs))
108108
fill!(obs_mask, true)
109109
additional_eqs = Equation[]
110-
additional_vars = SymbolicT[]
110+
additional_vars = Set{SymbolicT}()
111111
additional_subs = Dict{SymbolicT, SymbolicT}()
112112

113113
# Also need to update schedule
@@ -142,21 +142,15 @@ function MTKBase.unhack_system(sys::System)
142142

143143
resid = A * x - b
144144
for res in resid
145+
SU._iszero(res) && continue
146+
# If a linear SCC contains both `D(w)` and `w_t`, it'll contain the equation `D(w) ~ w_t`.
147+
# When unhacking it, `D(w)` will be totermed into `w_t`. Avoid adding the `0 ~ 0` equations.
148+
# The duplicate variables are automatically removed by the `Set`.
149+
# See https://github.com/SciML/ModelingToolkit.jl/issues/4196 for further details.
145150
push!(additional_eqs, Symbolics.COMMON_ZERO ~ res)
146151
end
147152
end
148153
@assert length(additional_eqs) == length(additional_vars)
149-
# If a linear SCC contains both `D(w)` and `w_t`, it'll contain the equation `D(w) ~ w_t`.
150-
# When unhacking it, `D(w)` will be totermed into `w_t`. This, `additional_vars` contains
151-
# two `w_t` and an equation that is `0 ~ 0`. Find the `0 ~ 0` equations, and remove them
152-
# along with the duplicate variables.
153-
# See https://github.com/SciML/ModelingToolkit.jl/issues/4196 for further details.
154-
additional_eqs_mask = trues(length(additional_eqs))
155-
for (i, eq) in enumerate(additional_eqs)
156-
additional_eqs_mask[i] = !SU._iszero(eq.rhs)
157-
end
158-
additional_eqs = additional_eqs[additional_eqs_mask]
159-
additional_vars = additional_vars[additional_eqs_mask]
160154
subst = SU.Substituter{false}(additional_subs, SU.default_substitute_filter)
161155
obseqs = obseqs[obs_mask]
162156
map!(subst, obseqs, obseqs)
@@ -167,7 +161,7 @@ function MTKBase.unhack_system(sys::System)
167161
map!(subst, values(sched.dummy_sub))
168162
end
169163

170-
dvs = [unknowns(sys); additional_vars]
164+
dvs = [unknowns(sys); collect(additional_vars)]
171165

172166
newsys = @set sys.observed = obseqs
173167
@set! newsys.eqs = eqs

0 commit comments

Comments
 (0)