Skip to content

Commit b799532

Browse files
committed
Add check kwarg
1 parent 2b74cd2 commit b799532

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/systems/reduction.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ julia> ModelingToolkit.topsort_equations(eqs, [x, y, z, k])
117117
Equation(x(t), y(t) + z(t))
118118
```
119119
"""
120-
function topsort_equations(eqs, states)
120+
function topsort_equations(eqs, states; check=true)
121121
graph, assigns = observed2graph(eqs, states)
122122
neqs = length(eqs)
123123
degrees = zeros(Int, neqs)
@@ -135,18 +135,19 @@ function topsort_equations(eqs, states)
135135
end
136136

137137
idx = 0
138-
ordered_eqs = similar(eqs)
138+
ordered_eqs = similar(eqs, 0); sizehint!(ordered_eqs, neqs)
139139
while !isempty(q)
140140
𝑠eq = dequeue!(q)
141-
ordered_eqs[idx+=1] = eqs[𝑠eq]
141+
idx+=1
142+
push!(ordered_eqs, eqs[𝑠eq])
142143
var = assigns[𝑠eq]
143144
for 𝑑eq in 𝑑neighbors(graph, var)
144145
degree = degrees[𝑑eq] = degrees[𝑑eq] - 1
145146
degree == 0 && enqueue!(q, 𝑑eq)
146147
end
147148
end
148149

149-
idx == neqs || throw(ArgumentError("The equations have at least one cycle."))
150+
(check && idx != neqs) && throw(ArgumentError("The equations have at least one cycle."))
150151

151152
return ordered_eqs
152153
end
@@ -160,7 +161,7 @@ function observed2graph(eqs, states)
160161

161162
for (i, eq) in enumerate(eqs)
162163
lhs_j = get(v2j, eq.lhs, nothing)
163-
lhs_j === nothing && throw(ArgumentError("The lhs $lhs of $eq, doesn't appear in states."))
164+
lhs_j === nothing && throw(ArgumentError("The lhs $(eq.lhs) of $eq, doesn't appear in states."))
164165
assigns[i] = lhs_j
165166
vs = vars(eq.rhs)
166167
for v in vs

0 commit comments

Comments
 (0)