Skip to content

Commit d70442d

Browse files
committed
More optimization and simplification
1 parent 69c728b commit d70442d

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/structural_transformation/partial_state_selection.jl

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -157,32 +157,13 @@ function dummy_derivative_graph!(state::TransformationState, jac = nothing;
157157
dummy_derivative_graph!(state.structure, var_eq_matching, jac, state_priority)
158158
end
159159

160-
function compute_diff_level(diff_to_x)
161-
nxs = length(diff_to_x)
162-
xlevel = zeros(Int, nxs)
163-
maxlevel = 0
164-
for i in 1:nxs
165-
level = 0
166-
x = i
167-
while diff_to_x[x] !== nothing
168-
x = diff_to_x[x]
169-
level += 1
170-
end
171-
maxlevel = max(maxlevel, level)
172-
xlevel[i] = level
173-
end
174-
return xlevel, maxlevel
175-
end
176-
177160
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac,
178161
state_priority)
179162
@unpack eq_to_diff, var_to_diff, graph = structure
180163
diff_to_eq = invview(eq_to_diff)
181164
diff_to_var = invview(var_to_diff)
182165
invgraph = invview(graph)
183166

184-
eqlevel, _ = compute_diff_level(diff_to_eq)
185-
186167
var_sccs = find_var_sccs(graph, var_eq_matching)
187168
eqcolor = falses(nsrcs(graph))
188169
dummy_derivatives = Int[]
@@ -193,6 +174,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
193174
next_var_idxs = Int[]
194175
new_eqs = Int[]
195176
new_vars = Int[]
177+
eqs_set = BitSet()
196178
for vars in var_sccs
197179
empty!(eqs)
198180
for var in vars
@@ -202,8 +184,6 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
202184
push!(eqs, eq)
203185
end
204186
isempty(eqs) && continue
205-
maxlevel = maximum(Base.Fix1(getindex, eqlevel), eqs)
206-
iszero(maxlevel) && continue
207187

208188
rank_matching = Matching(nvars)
209189
isfirst = true
@@ -219,15 +199,13 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
219199
end
220200
J = is_all_small_int ? Int.(unwrap.(_J)) : nothing
221201
end
222-
for level in maxlevel:-1:1
202+
while true
223203
nrows = length(eqs)
224204
iszero(nrows) && break
225-
eqs_set = BitSet(eqs)
226205

227206
if state_priority !== nothing && isfirst
228207
sort!(vars, by = state_priority)
229208
end
230-
isfirst = false
231209
# TODO: making the algorithm more robust
232210
# 1. If the Jacobian is a integer matrix, use Bareiss to check
233211
# linear independence. (done)
@@ -237,7 +215,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
237215
#
238216
# 3. If the Jacobian is a polynomial matrix, use Gröbner basis (?)
239217
if J !== nothing
240-
if level < maxlevel
218+
if !isfirst
241219
J = J[next_eq_idxs, next_var_idxs]
242220
end
243221
N = ModelingToolkit.nullspace(J; col_order) # modifies col_order
@@ -246,6 +224,8 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
246224
push!(dummy_derivatives, vars[col_order[i]])
247225
end
248226
else
227+
empty!(eqs_set)
228+
union!(eqs_set, eqs)
249229
rank = 0
250230
for var in vars
251231
eqcolor .= false
@@ -261,7 +241,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
261241
fill!(rank_matching, unassigned)
262242
end
263243
if rank != nrows
264-
@warn "The DAE system is structurally singular!"
244+
@warn "The DAE system is singular!"
265245
end
266246

267247
# prepare the next iteration
@@ -293,6 +273,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
293273
end
294274
eqs, new_eqs = new_eqs, eqs
295275
vars, new_vars = new_vars, vars
276+
isfirst = false
296277
end
297278
end
298279

0 commit comments

Comments
 (0)