Skip to content

Commit 07922c6

Browse files
authored
Merge pull request #3041 from SciML/myb/uneven
Tolerate empty equations and uneven matching
2 parents c0b6d8c + c7491eb commit 07922c6

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/bipartite_graph.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,29 @@ function Base.setindex!(m::Matching{U}, v::Union{Integer, U}, i::Integer) where
6969

7070
# To maintain the invariant that `m.inv_match[m.match[i]] == i`, we need
7171
# to unassign the matching at `m.inv_match[v]` if it exists.
72-
if v isa Int && (iv = m.inv_match[v]) isa Int
72+
if v isa Int && 1 <= v <= length(m.inv_match) && (iv = m.inv_match[v]) isa Int
7373
m.match[iv] = unassigned
7474
end
7575
if isa(oldv, Int)
7676
@assert m.inv_match[oldv] == i
7777
m.inv_match[oldv] = unassigned
7878
end
79-
isa(v, Int) && (m.inv_match[v] = i)
79+
if isa(v, Int)
80+
for vv in (length(m.inv_match) + 1):v
81+
push!(m.inv_match, unassigned)
82+
end
83+
m.inv_match[v] = i
84+
end
8085
end
8186
return m.match[i] = v
8287
end
8388

8489
function Base.push!(m::Matching, v)
8590
push!(m.match, v)
8691
if v isa Integer && m.inv_match !== nothing
92+
for vv in (length(m.inv_match) + 1):v
93+
push!(m.inv_match, unassigned)
94+
end
8795
m.inv_match[v] = length(m.match)
8896
end
8997
end

src/structural_transformation/partial_state_selection.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ function dummy_derivative_graph!(
214214
eqcolor = falses(nsrcs(graph))
215215
dummy_derivatives = Int[]
216216
col_order = Int[]
217+
neqs = nsrcs(graph)
217218
nvars = ndsts(graph)
218219
eqs = Int[]
219220
vars = Int[]
@@ -236,7 +237,7 @@ function dummy_derivative_graph!(
236237
end
237238
isempty(eqs) && continue
238239

239-
rank_matching = Matching(nvars)
240+
rank_matching = Matching(max(nvars, neqs))
240241
isfirst = true
241242
if jac === nothing
242243
J = nothing
@@ -389,11 +390,13 @@ function tearing_with_dummy_derivatives(structure, dummy_derivatives)
389390
Base.Fix1(isdiffed, (structure, dummy_derivatives)),
390391
Union{Unassigned, SelectedState};
391392
varfilter = Base.Fix1(getindex, can_eliminate))
393+
392394
for v in 𝑑vertices(structure.graph)
393395
is_present(structure, v) || continue
394396
dv = var_to_diff[v]
395397
(dv === nothing || !is_some_diff(structure, dummy_derivatives, dv)) && continue
396398
var_eq_matching[v] = SelectedState()
397399
end
400+
398401
return var_eq_matching, full_var_eq_matching, var_sccs, can_eliminate
399402
end

src/structural_transformation/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ function check_consistency(state::TransformationState, orig_inputs)
9191
map(collect, edges(var_to_diff))])
9292
extended_var_eq_matching = maximal_matching(extended_graph)
9393

94+
nvars = ndsts(graph)
9495
unassigned_var = []
9596
for (vj, eq) in enumerate(extended_var_eq_matching)
97+
vj > nvars && break
9698
if eq === unassigned && !isempty(𝑑neighbors(graph, vj))
9799
push!(unassigned_var, fullvars[vj])
98100
end

0 commit comments

Comments
 (0)