Skip to content

Commit 080c40e

Browse files
committed
Add maxiter and simplify control flow
1 parent 95fbfb1 commit 080c40e

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

src/systems/diffeqs/index_reduction.jl

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function pantelides(sys::ODESystem)
107107
return pantelides!(edges, fullvars, vars_asso)
108108
end
109109

110-
function pantelides!(edges, vars, vars_asso)
110+
function pantelides!(edges, vars, vars_asso; maxiter = 100_000_000)
111111
neqs = length(edges)
112112
nvars = length(vars)
113113
assign = zeros(Int, nvars)
@@ -116,47 +116,44 @@ function pantelides!(edges, vars, vars_asso)
116116
for k in 1:neqs′
117117
i = k
118118
pathfound = false
119-
iter = 0
120-
while !pathfound && iter < 3
121-
iter += 1
119+
for _ in 1:maxiter
122120
# run matching on (dx, y) variables
123121
active = vars_asso .== 0
124122
vcolor = falses(nvars)
125123
ecolor = falses(neqs)
126124
pathfound = match_equation!(edges, i, assign, active, vcolor, ecolor)
127-
if !pathfound
128-
# for every colored V-node j
129-
for j in eachindex(vcolor); vcolor[j] || continue
130-
# introduce a new variable
131-
nvars += 1
132-
push!(vars_asso, 0)
133-
vars_asso[j] = nvars
134-
push!(assign, 0)
135-
end
125+
pathfound && break
126+
# for every colored V-node j
127+
for j in eachindex(vcolor); vcolor[j] || continue
128+
# introduce a new variable
129+
nvars += 1
130+
push!(vars_asso, 0)
131+
vars_asso[j] = nvars
132+
push!(assign, 0)
133+
end
136134

137-
# for every colored E-node l
138-
for l in eachindex(ecolor); ecolor[l] || continue
139-
neqs += 1
140-
# create new E-node
141-
push!(edges, copy(edges[l]))
142-
# create edges from E-node `neqs` to all V-nodes `j` and
143-
# `vars_asso[j]` s.t. edge `(l-j)` exists
144-
for j in edges[l]
145-
if !(vars_asso[j] in edges[neqs])
146-
push!(edges[neqs], vars_asso[j])
147-
end
135+
# for every colored E-node l
136+
for l in eachindex(ecolor); ecolor[l] || continue
137+
neqs += 1
138+
# create new E-node
139+
push!(edges, copy(edges[l]))
140+
# create edges from E-node `neqs` to all V-nodes `j` and
141+
# `vars_asso[j]` s.t. edge `(l-j)` exists
142+
for j in edges[l]
143+
if !(vars_asso[j] in edges[neqs])
144+
push!(edges[neqs], vars_asso[j])
148145
end
149-
push!(eqs_asso, 0)
150-
eqs_asso[l] = neqs
151146
end
147+
push!(eqs_asso, 0)
148+
eqs_asso[l] = neqs
149+
end
152150

153-
# for every colored V-node j
154-
for j in eachindex(vcolor); vcolor[j] || continue
155-
assign[vars_asso[j]] = eqs_asso[assign[j]]
156-
end
157-
i = eqs_asso[i]
158-
end # if !pathfound
159-
end # while !pathfound
151+
# for every colored V-node j
152+
for j in eachindex(vcolor); vcolor[j] || continue
153+
assign[vars_asso[j]] = eqs_asso[assign[j]]
154+
end
155+
i = eqs_asso[i]
156+
end # for _ in 1:maxiter
160157
end # for k in 1:neqs′
161158
return edges, assign, vars_asso, eqs_asso
162159
end

0 commit comments

Comments
 (0)