Skip to content

Commit a6ba3cd

Browse files
fix: make topsorting equations type-stable
1 parent cef5f24 commit a6ba3cd

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/systems/alias_elimination.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ julia> ModelingToolkit.topsort_equations(eqs, [x, y, z, k])
411411
Equation(x(t), y(t) + z(t))
412412
```
413413
"""
414-
function topsort_equations(eqs, unknowns; check = true)
414+
function topsort_equations(eqs::Vector{Equation}, unknowns::Vector{SymbolicT}; check = true)
415415
graph, assigns = observed2graph(eqs, unknowns)
416416
neqs = length(eqs)
417417
degrees = zeros(Int, neqs)
@@ -448,22 +448,25 @@ function topsort_equations(eqs, unknowns; check = true)
448448
return ordered_eqs
449449
end
450450

451-
function observed2graph(eqs, unknowns)
451+
function observed2graph(eqs::Vector{Equation}, unknowns::Vector{SymbolicT})::Tuple{BipartiteGraph{Int, Nothing}, Vector{Int}}
452452
graph = BipartiteGraph(length(eqs), length(unknowns))
453-
v2j = Dict(unknowns .=> 1:length(unknowns))
453+
v2j = Dict{SymbolicT, Int}(unknowns .=> 1:length(unknowns))
454454

455455
# `assigns: eq -> var`, `eq` defines `var`
456456
assigns = similar(eqs, Int)
457-
457+
vars = Set{SymbolicT}()
458458
for (i, eq) in enumerate(eqs)
459459
lhs_j = get(v2j, eq.lhs, nothing)
460460
lhs_j === nothing &&
461461
throw(ArgumentError("The lhs $(eq.lhs) of $eq, doesn't appear in unknowns."))
462462
assigns[i] = lhs_j
463-
vs = vars(eq.rhs; op = Symbolics.Operator)
464-
for v in vs
463+
empty!(vars)
464+
SU.search_variables!(vars, eq.rhs; is_atomic = OperatorIsAtomic{SU.Operator}())
465+
for v in vars
465466
j = get(v2j, v, nothing)
466-
j !== nothing && add_edge!(graph, i, j)
467+
if j isa Int
468+
add_edge!(graph, i, j)
469+
end
467470
end
468471
end
469472

0 commit comments

Comments
 (0)