Skip to content

Commit e28b5dc

Browse files
committed
Better cycle detection
1 parent 572c24d commit e28b5dc

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/systems/reduction.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ julia> ModelingToolkit.topsort_observed(eqs, [x, y, z, k])
118118
```
119119
"""
120120
function topsort_observed(eqs, states)
121-
graph, assigns, v2j = observed2graph(eqs, states)
121+
graph, assigns = observed2graph(eqs, states)
122122
neqs = length(eqs)
123123
degrees = zeros(Int, neqs)
124124

@@ -137,18 +137,16 @@ function topsort_observed(eqs, states)
137137
idx = 0
138138
order = zeros(Int, neqs)
139139
while !isempty(q)
140-
j = dequeue!(q)
141-
order[idx+=1] = j
142-
for 𝑠eq in 1:length(eqs); var = assigns[𝑠eq]
143-
for 𝑑eq in 𝑑neighbors(graph, var)
144-
# 𝑠eq => 𝑑eq
145-
degree = degrees[𝑑eq] = degrees[𝑑eq] - 1
146-
degree == 0 && enqueue!(q, 𝑑eq)
147-
end
140+
𝑠eq = dequeue!(q)
141+
order[idx+=1] = 𝑠eq
142+
var = assigns[𝑠eq]
143+
for 𝑑eq in 𝑑neighbors(graph, var)
144+
degree = degrees[𝑑eq] = degrees[𝑑eq] - 1
145+
degree == 0 && enqueue!(q, 𝑑eq)
148146
end
149147
end
150148

151-
idx == neqs || throw(ArgumentError("There's a cycle in obversed equations."))
149+
idx == neqs || throw(ArgumentError("The obversed equations have at least one cycle."))
152150

153151
return eqs[order]
154152
end
@@ -171,5 +169,5 @@ function observed2graph(eqs, states)
171169
end
172170
end
173171

174-
return graph, assigns, v2j
172+
return graph, assigns
175173
end

0 commit comments

Comments
 (0)