Skip to content

Commit d2c169c

Browse files
committed
Remove dead code
1 parent 36143c6 commit d2c169c

File tree

10 files changed

+28
-929
lines changed

10 files changed

+28
-929
lines changed

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di
2222
get_postprocess_fbody, vars!,
2323
IncrementalCycleTracker, add_edge_checked!, topological_sort,
2424
invalidate_cache!, Substitutions, get_or_construct_tearing_state,
25-
AliasGraph, filter_kwargs, lower_varname, setio, SparseMatrixCLIL,
25+
filter_kwargs, lower_varname, setio, SparseMatrixCLIL,
2626
fast_substitute, get_fullvars, has_equations
2727

2828
using ModelingToolkit.BipartiteGraphs

src/structural_transformation/pantelides.jl

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,20 @@ function pantelides_reassemble(state::TearingState, var_eq_matching)
7070
end
7171

7272
"""
73-
computed_highest_diff_variables(var_to_diff, ag)
73+
computed_highest_diff_variables(var_to_diff)
7474
7575
Computes which variables are the "highest-differentiated" for purposes of
7676
pantelides. Ordinarily this is relatively straightforward. However, in our
77-
case, there are two complicating conditions:
77+
case, there is one complicating condition:
7878
79-
1. We allow variables in the structure graph that don't appear in the
79+
We allow variables in the structure graph that don't appear in the
8080
system at all. What we are interested in is the highest-differentiated
8181
variable that actually appears in the system.
8282
83-
2. We have an alias graph. The alias graph implicitly contributes an
84-
alias equation, so it doesn't actually whitelist any additional variables,
85-
but it may change which variable is considered the highest differentiated one.
86-
Consider the following situation:
87-
88-
Vars: x, y
89-
Eqs: 0 = f(x)
90-
Alias: ẋ = ẏ
91-
92-
In the absence of the alias, we would consider `x` to be the highest
93-
differentiated variable. However, because of the alias (and because there
94-
is no alias for `x=y`), we actually need to take `ẋ` as the highest
95-
differentiated variable.
96-
9783
This function takes care of these complications are returns a boolean array
9884
for every variable, indicating whether it is considered "highest-differentiated".
9985
"""
100-
function computed_highest_diff_variables(structure, ag::Union{AliasGraph, Nothing})
86+
function computed_highest_diff_variables(structure)
10187
@unpack graph, var_to_diff = structure
10288

10389
nvars = length(var_to_diff)
@@ -107,56 +93,12 @@ function computed_highest_diff_variables(structure, ag::Union{AliasGraph, Nothin
10793
# This variable is structurally highest-differentiated, but may not actually appear in the
10894
# system (complication 1 above). Ascend the differentiation graph to find the highest
10995
# differentiated variable that does appear in the system or the alias graph).
110-
while isempty(𝑑neighbors(graph, var)) && (ag === nothing || !haskey(ag, var))
96+
while isempty(𝑑neighbors(graph, var))
11197
var′ = invview(var_to_diff)[var]
11298
var′ === nothing && break
11399
var = var′
114100
end
115-
# If we don't have an alias graph, we are done. If we do have an alias graph, we may
116-
# have to keep going along the stem, for as long as our differentiation path
117-
# matches that of the stem (see complication 2 above). Note that we may end up
118-
# whitelisting multiple differentiation levels of the stem here from different
119-
# starting points that all map to the same stem. We clean that up in a post-processing
120-
# pass below.
121-
if ag !== nothing && haskey(ag, var)
122-
(_, stem) = ag[var]
123-
stem == 0 && continue
124-
# If we have a self-loop in the stem, we could have the
125-
# var′ also alias to the original stem. In that case, the
126-
# derivative of the stem is highest differentiated, because of the loop
127-
loop_found = false
128-
var′ = invview(var_to_diff)[var]
129-
while var′ !== nothing
130-
if var′ == stem || (haskey(ag, var′) && ag[var′][2] == stem)
131-
dstem = var_to_diff[stem]
132-
@assert dstem !== nothing
133-
varwhitelist[dstem] = true
134-
loop_found = true
135-
break
136-
end
137-
var′ = invview(var_to_diff)[var′]
138-
end
139-
loop_found && continue
140-
# Ascend the stem
141-
while isempty(𝑑neighbors(graph, var))
142-
var′ = invview(var_to_diff)[var]
143-
var′ === nothing && break
144-
loop_found = false
145-
cvar = var′
146-
# Invariant from alias elimination: Stem is chosen to have
147-
# the highest differentiation order.
148-
stem′ = invview(var_to_diff)[stem]
149-
@assert stem′ !== nothing
150-
if !haskey(ag, var′) || (ag[var′][2] != stem′)
151-
varwhitelist[stem] = true
152-
break
153-
end
154-
stem = stem′
155-
var = var′
156-
end
157-
else
158-
varwhitelist[var] = true
159-
end
101+
varwhitelist[var] = true
160102
end
161103
end
162104

@@ -181,8 +123,7 @@ end
181123
182124
Perform Pantelides algorithm.
183125
"""
184-
function pantelides!(state::TransformationState, ag::Union{AliasGraph, Nothing} = nothing;
185-
finalize = true, maxiters = 8000)
126+
function pantelides!(state::TransformationState; finalize = true, maxiters = 8000)
186127
@unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure
187128
neqs = nsrcs(graph)
188129
nvars = nv(var_to_diff)
@@ -193,7 +134,7 @@ function pantelides!(state::TransformationState, ag::Union{AliasGraph, Nothing}
193134
nnonemptyeqs = count(eq -> !isempty(𝑠neighbors(graph, eq)) && eq_to_diff[eq] === nothing,
194135
1:neqs′)
195136

196-
varwhitelist = computed_highest_diff_variables(state.structure, ag)
137+
varwhitelist = computed_highest_diff_variables(state.structure)
197138

198139
if nnonemptyeqs > count(varwhitelist)
199140
throw(InvalidSystemException("System is structurally singular"))

src/structural_transformation/partial_state_selection.jl

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
function partial_state_selection_graph!(state::TransformationState;
2-
ag::Union{AliasGraph, Nothing} = nothing)
1+
function partial_state_selection_graph!(state::TransformationState)
32
find_solvables!(state; allow_symbolic = true)
4-
var_eq_matching = complete(pantelides!(state, ag))
3+
var_eq_matching = complete(pantelides!(state))
54
complete!(state.structure)
65
partial_state_selection_graph!(state.structure, var_eq_matching)
76
end
@@ -150,14 +149,12 @@ function partial_state_selection_graph!(structure::SystemStructure, var_eq_match
150149
var_eq_matching
151150
end
152151

153-
function dummy_derivative_graph!(state::TransformationState, jac = nothing,
154-
(ag, diff_va) = (nothing, nothing);
152+
function dummy_derivative_graph!(state::TransformationState, jac = nothing;
155153
state_priority = nothing, kwargs...)
156154
state.structure.solvable_graph === nothing && find_solvables!(state; kwargs...)
157155
complete!(state.structure)
158-
var_eq_matching = complete(pantelides!(state, ag))
159-
dummy_derivative_graph!(state.structure, var_eq_matching, jac, (ag, diff_va),
160-
state_priority)
156+
var_eq_matching = complete(pantelides!(state))
157+
dummy_derivative_graph!(state.structure, var_eq_matching, jac, state_priority)
161158
end
162159

163160
function compute_diff_level(diff_to_x)
@@ -178,7 +175,7 @@ function compute_diff_level(diff_to_x)
178175
end
179176

180177
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac,
181-
(ag, diff_va), state_priority)
178+
state_priority)
182179
@unpack eq_to_diff, var_to_diff, graph = structure
183180
diff_to_eq = invview(eq_to_diff)
184181
diff_to_var = invview(var_to_diff)
@@ -249,61 +246,14 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
249246
end
250247
end
251248

252-
if diff_va !== nothing
253-
# differentiated alias
254-
n_dummys = length(dummy_derivatives)
255-
needed = count(x -> x isa Int, diff_to_eq) - n_dummys
256-
n = 0
257-
for v in diff_va
258-
c, a = ag[v]
259-
n += 1
260-
push!(dummy_derivatives, iszero(c) ? v : a)
261-
needed == n && break
262-
continue
263-
end
264-
end
265-
266249
if (n_diff_eqs = count(!isnothing, diff_to_eq)) !=
267250
(n_dummys = length(dummy_derivatives))
268251
@warn "The number of dummy derivatives ($n_dummys) does not match the number of differentiated equations ($n_diff_eqs)."
269252
end
270253
dummy_derivatives_set = BitSet(dummy_derivatives)
271254

272-
irreducible_set = BitSet()
273-
if ag !== nothing
274-
function isreducible(x)
275-
# `k` is reducible if all lower differentiated variables are.
276-
isred = true
277-
while isred
278-
if x in dummy_derivatives_set
279-
break
280-
end
281-
x = diff_to_var[x]
282-
x === nothing && break
283-
# We deliberately do not check `isempty(𝑑neighbors(graph, x))`
284-
# because when `D(x)` appears in the alias graph, and `x`
285-
# doesn't appear in any equations nor in the alias graph, `D(x)`
286-
# is not reducible. Consider the system `D(x) ~ 0`.
287-
if !haskey(ag, x)
288-
isred = false
289-
end
290-
end
291-
isred
292-
end
293-
for (k, (c, v)) in ag
294-
isreducible(k) || push!(irreducible_set, k)
295-
iszero(c) && continue
296-
isempty(𝑑neighbors(graph, v)) || push!(irreducible_set, v)
297-
end
298-
end
299-
300-
is_not_present_non_rec = let graph = graph, irreducible_set = irreducible_set
301-
v -> begin
302-
not_in_eqs = isempty(𝑑neighbors(graph, v))
303-
ag === nothing && return not_in_eqs
304-
isirreducible = v in irreducible_set
305-
return not_in_eqs && !isirreducible
306-
end
255+
is_not_present_non_rec = let graph = graph
256+
v -> isempty(𝑑neighbors(graph, v))
307257
end
308258

309259
is_not_present = let var_to_diff = var_to_diff
@@ -330,11 +280,8 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
330280
# We can eliminate variables that are not a selected state (differential
331281
# variables). Selected states are differentiated variables that are not
332282
# dummy derivatives.
333-
can_eliminate = let var_to_diff = var_to_diff, ag = ag
283+
can_eliminate = let var_to_diff = var_to_diff
334284
v -> begin
335-
if ag !== nothing
336-
haskey(ag, v) && return false
337-
end
338285
dv = var_to_diff[v]
339286
dv === nothing && return true
340287
is_some_diff(dv) || return true

src/structural_transformation/symbolics_tearing.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function check_diff_graph(var_to_diff, fullvars)
217217
end
218218
=#
219219

220-
function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
220+
function tearing_reassemble(state::TearingState, var_eq_matching;
221221
simplify = false, mm = nothing)
222222
@unpack fullvars, sys, structure = state
223223
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
@@ -494,7 +494,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
494494
error("Tearing internal error: lowering DAE into semi-implicit ODE failed!")
495495
end
496496
solved_variables_set = BitSet(solved_variables)
497-
ag === nothing || union!(solved_variables_set, keys(ag))
498497
invvarsperm = [diff_vars;
499498
setdiff!(setdiff(1:ndsts(graph), diff_vars_set),
500499
solved_variables_set)]
@@ -608,7 +607,7 @@ end
608607
Perform index reduction and use the dummy derivative technique to ensure that
609608
the system is balanced.
610609
"""
611-
function dummy_derivative(sys, state = TearingState(sys), ag = nothing; simplify = false,
610+
function dummy_derivative(sys, state = TearingState(sys); simplify = false,
612611
mm = nothing, kwargs...)
613612
jac = let state = state
614613
(eqs, vars) -> begin
@@ -631,7 +630,7 @@ function dummy_derivative(sys, state = TearingState(sys), ag = nothing; simplify
631630
p
632631
end
633632
end
634-
var_eq_matching = dummy_derivative_graph!(state, jac, (ag, nothing); state_priority,
633+
var_eq_matching = dummy_derivative_graph!(state, jac; state_priority,
635634
kwargs...)
636-
tearing_reassemble(state, var_eq_matching, ag; simplify, mm)
635+
tearing_reassemble(state, var_eq_matching; simplify, mm)
637636
end

src/structural_transformation/utils.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ end
5858
###
5959
### Structural check
6060
###
61-
function check_consistency(state::TransformationState, ag, orig_inputs)
61+
function check_consistency(state::TransformationState, orig_inputs)
6262
fullvars = get_fullvars(state)
6363
neqs = n_concrete_eqs(state)
6464
@unpack graph, var_to_diff = state.structure
65-
highest_vars = computed_highest_diff_variables(complete!(state.structure), ag)
65+
highest_vars = computed_highest_diff_variables(complete!(state.structure))
6666
n_highest_vars = 0
6767
for (v, h) in enumerate(highest_vars)
6868
h || continue
@@ -89,13 +89,11 @@ function check_consistency(state::TransformationState, ag, orig_inputs)
8989
# details, check the equation (15) of the original paper.
9090
extended_graph = (@set graph.fadjlist = Vector{Int}[graph.fadjlist;
9191
map(collect, edges(var_to_diff))])
92-
extended_var_eq_matching = maximal_matching(extended_graph, eq -> true,
93-
v -> ag === nothing || !haskey(ag, v))
92+
extended_var_eq_matching = maximal_matching(extended_graph)
9493

9594
unassigned_var = []
9695
for (vj, eq) in enumerate(extended_var_eq_matching)
97-
if eq === unassigned && (ag === nothing || !haskey(ag, vj)) &&
98-
!isempty(𝑑neighbors(graph, vj))
96+
if eq === unassigned && !isempty(𝑑neighbors(graph, vj))
9997
push!(unassigned_var, fullvars[vj])
10098
end
10199
end

0 commit comments

Comments
 (0)