Skip to content

Commit 055fbb1

Browse files
committed
Revert "Merge branch 'myb/pss' into myb/differential_alias"
This reverts commit 0877683, reversing changes made to ef61def.
1 parent 2140705 commit 055fbb1

File tree

12 files changed

+134
-550
lines changed

12 files changed

+134
-550
lines changed

src/bipartite_graph.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ end
4949
function Matching(m::Int)
5050
Matching{Unassigned}(Union{Int, Unassigned}[unassigned for _ in 1:m], nothing)
5151
end
52-
function Matching{U}(m::Int) where {U}
53-
Matching{Union{Unassigned, U}}(Union{Int, Unassigned, U}[unassigned for _ in 1:m],
54-
nothing)
55-
end
5652

5753
Base.size(m::Matching) = Base.size(m.match)
5854
Base.getindex(m::Matching, i::Integer) = m.match[i]
@@ -69,9 +65,9 @@ function Base.setindex!(m::Matching{U}, v::Union{Integer, U}, i::Integer) where
6965
return m.match[i] = v
7066
end
7167

72-
function Base.push!(m::Matching, v)
68+
function Base.push!(m::Matching{U}, v::Union{Integer, U}) where {U}
7369
push!(m.match, v)
74-
if v isa Integer && m.inv_match !== nothing
70+
if v !== unassigned && m.inv_match !== nothing
7571
m.inv_match[v] = length(m.match)
7672
end
7773
end
@@ -350,8 +346,8 @@ vertices, subject to the constraint that vertices for which `srcfilter` or `dstf
350346
return `false` may not be matched.
351347
"""
352348
function maximal_matching(g::BipartiteGraph, srcfilter = vsrc -> true,
353-
dstfilter = vdst -> true, ::Type{U} = Unassigned) where {U}
354-
matching = Matching{U}(ndsts(g))
349+
dstfilter = vdst -> true)
350+
matching = Matching(ndsts(g))
355351
foreach(Iterators.filter(srcfilter, 𝑠vertices(g))) do vsrc
356352
construct_augmenting_path!(matching, g, vsrc, dstfilter)
357353
end

src/structural_transformation/bipartite_tearing/modia_tearing.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function tear_graph_block_modia!(var_eq_matching, graph, solvable_graph, eqs, va
3535
return nothing
3636
end
3737

38-
function tear_graph_modia(structure::SystemStructure, ::Type{U} = Unassigned;
39-
varfilter = v -> true, eqfilter = eq -> true) where {U}
38+
function tear_graph_modia(structure::SystemStructure; varfilter = v -> true,
39+
eqfilter = eq -> true)
4040
# It would be possible here to simply iterate over all variables and attempt to
4141
# use tearEquations! to produce a matching that greedily selects the minimal
4242
# number of torn variables. However, we can do this process faster if we first
@@ -49,7 +49,7 @@ function tear_graph_modia(structure::SystemStructure, ::Type{U} = Unassigned;
4949
# find them here [TODO: It would be good to have an explicit example of this.]
5050

5151
@unpack graph, solvable_graph = structure
52-
var_eq_matching = complete(maximal_matching(graph, eqfilter, varfilter, U))
52+
var_eq_matching = complete(maximal_matching(graph, eqfilter, varfilter))
5353
var_sccs::Vector{Union{Vector{Int}, Int}} = find_var_sccs(graph, var_eq_matching)
5454

5555
for vars in var_sccs

src/structural_transformation/partial_state_selection.jl

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -146,48 +146,53 @@ function partial_state_selection_graph!(structure::SystemStructure, var_eq_match
146146
var_eq_matching
147147
end
148148

149-
function dummy_derivative_graph!(state::TransformationState, jac = nothing; kwargs...)
150-
state.structure.solvable_graph === nothing && find_solvables!(state; kwargs...)
149+
function dummy_derivative_graph!(state::TransformationState, jac = nothing)
151150
var_eq_matching = complete(pantelides!(state))
152151
complete!(state.structure)
153152
dummy_derivative_graph!(state.structure, var_eq_matching, jac)
154153
end
155154

156-
function compute_diff_level(diff_to_x)
157-
nxs = length(diff_to_x)
158-
xlevel = zeros(Int, nxs)
155+
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac)
156+
@unpack eq_to_diff, var_to_diff, graph = structure
157+
diff_to_eq = invview(eq_to_diff)
158+
diff_to_var = invview(var_to_diff)
159+
invgraph = invview(graph)
160+
161+
neqs = nsrcs(graph)
162+
eqlevel = zeros(Int, neqs)
159163
maxlevel = 0
160-
for i in 1:nxs
164+
for i in 1:neqs
161165
level = 0
162-
x = i
163-
while diff_to_x[x] !== nothing
164-
x = diff_to_x[x]
166+
eq = i
167+
while diff_to_eq[eq] !== nothing
168+
eq = diff_to_eq[eq]
165169
level += 1
166170
end
167171
maxlevel = max(maxlevel, level)
168-
xlevel[i] = level
172+
eqlevel[i] = level
169173
end
170-
return xlevel, maxlevel
171-
end
172174

173-
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac)
174-
@unpack eq_to_diff, var_to_diff, graph = structure
175-
diff_to_eq = invview(eq_to_diff)
176-
diff_to_var = invview(var_to_diff)
177-
invgraph = invview(graph)
178-
179-
eqlevel, _ = compute_diff_level(diff_to_eq)
180-
varlevel, _ = compute_diff_level(diff_to_var)
175+
nvars = ndsts(graph)
176+
varlevel = zeros(Int, nvars)
177+
for i in 1:nvars
178+
level = 0
179+
var = i
180+
while diff_to_var[var] !== nothing
181+
var = diff_to_var[var]
182+
level += 1
183+
end
184+
maxlevel = max(maxlevel, level)
185+
varlevel[i] = level
186+
end
181187

182188
var_sccs = find_var_sccs(graph, var_eq_matching)
183-
eqcolor = falses(nsrcs(graph))
189+
eqcolor = falses(neqs)
184190
dummy_derivatives = Int[]
185191
col_order = Int[]
186-
nvars = ndsts(graph)
187192
for vars in var_sccs
188193
eqs = [var_eq_matching[var] for var in vars if var_eq_matching[var] !== unassigned]
189194
isempty(eqs) && continue
190-
maxlevel = maximum(Base.Fix1(getindex, eqlevel), eqs)
195+
maxlevel = maximum(map(x -> eqlevel[x], eqs))
191196
iszero(maxlevel) && continue
192197

193198
rank_matching = Matching(nvars)
@@ -215,10 +220,8 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
215220
else
216221
rank = 0
217222
for var in vars
218-
# We need `invgraph` here because we are matching from
219-
# variables to equations.
220223
pathfound = construct_augmenting_path!(rank_matching, invgraph, var,
221-
Base.Fix2(in, eqs_set), eqcolor)
224+
eq -> eq in eqs_set, eqcolor)
222225
pathfound || continue
223226
push!(dummy_derivatives, var)
224227
rank += 1
@@ -236,35 +239,5 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
236239
end
237240
end
238241

239-
dummy_derivatives_set = BitSet(dummy_derivatives)
240-
# We can eliminate variables that are not a selected state (differential
241-
# variables). Selected states are differentiated variables that are not
242-
# dummy derivatives.
243-
can_eliminate = let var_to_diff = var_to_diff,
244-
dummy_derivatives_set = dummy_derivatives_set
245-
246-
v -> begin
247-
dv = var_to_diff[v]
248-
dv === nothing || dv in dummy_derivatives_set
249-
end
250-
end
251-
252-
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
253-
# actually differentiated variables.
254-
isdiffed = let diff_to_var = diff_to_var, dummy_derivatives_set = dummy_derivatives_set
255-
v -> diff_to_var[v] !== nothing && !(v in dummy_derivatives_set)
256-
end
257-
should_consider = let graph = graph, isdiffed = isdiffed
258-
eq -> !any(isdiffed, 𝑠neighbors(graph, eq))
259-
end
260-
261-
var_eq_matching = tear_graph_modia(structure, Union{Unassigned, SelectedState};
262-
varfilter = can_eliminate,
263-
eqfilter = should_consider)
264-
for v in eachindex(var_eq_matching)
265-
can_eliminate(v) && continue
266-
var_eq_matching[v] = SelectedState()
267-
end
268-
269-
return var_eq_matching
242+
dummy_derivatives
270243
end

0 commit comments

Comments
 (0)