@@ -117,7 +117,7 @@ julia> ModelingToolkit.topsort_equations(eqs, [x, y, z, k])
117
117
Equation(x(t), y(t) + z(t))
118
118
```
119
119
"""
120
- function topsort_equations (eqs, states)
120
+ function topsort_equations (eqs, states; check = true )
121
121
graph, assigns = observed2graph (eqs, states)
122
122
neqs = length (eqs)
123
123
degrees = zeros (Int, neqs)
@@ -135,18 +135,19 @@ function topsort_equations(eqs, states)
135
135
end
136
136
137
137
idx = 0
138
- ordered_eqs = similar (eqs)
138
+ ordered_eqs = similar (eqs, 0 ); sizehint! (ordered_eqs, neqs )
139
139
while ! isempty (q)
140
140
𝑠eq = dequeue! (q)
141
- ordered_eqs[idx+= 1 ] = eqs[𝑠eq]
141
+ idx+= 1
142
+ push! (ordered_eqs, eqs[𝑠eq])
142
143
var = assigns[𝑠eq]
143
144
for 𝑑eq in 𝑑neighbors (graph, var)
144
145
degree = degrees[𝑑eq] = degrees[𝑑eq] - 1
145
146
degree == 0 && enqueue! (q, 𝑑eq)
146
147
end
147
148
end
148
149
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." ))
150
151
151
152
return ordered_eqs
152
153
end
@@ -160,7 +161,7 @@ function observed2graph(eqs, states)
160
161
161
162
for (i, eq) in enumerate (eqs)
162
163
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." ))
164
165
assigns[i] = lhs_j
165
166
vs = vars (eq. rhs)
166
167
for v in vs
0 commit comments