Skip to content

Commit e0d089c

Browse files
committed
Make sure that irreducible variables don't get eliminated by accident
Fix #1722
1 parent b53817b commit e0d089c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/systems/alias_elimination.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ function simple_aliases!(ag, graph, var_to_diff, mm_orig, irreducibles = ())
570570
irreducibles)
571571

572572
# Step 2: Simplify the system using the Bareiss factorization
573-
for v in setdiff(solvable_variables, @view pivots[1:rank1])
573+
rk1vars = BitSet(@view pivots[1:rank1])
574+
for v in solvable_variables
575+
v in rk1vars && continue
574576
ag[v] = 0
575577
end
576578

@@ -699,6 +701,16 @@ function alias_eliminate_graph!(graph, var_to_diff, mm_orig::SparseMatrixCLIL)
699701
end
700702
for (i, v) in enumerate(level_to_var)
701703
_alias = get(ag, v, nothing)
704+
v_eqs = 𝑑neighbors(graph, v)
705+
# if an irreducible appears in only one equation, we need to make
706+
# sure that the other variables don't get eliminated
707+
if length(v_eqs) == 1
708+
eq = v_eqs[1]
709+
for av in 𝑠neighbors(graph, eq)
710+
push!(irreducibles, av)
711+
end
712+
ag[v] = nothing
713+
end
702714
push!(irreducibles, v)
703715
if _alias !== nothing && iszero(_alias[1]) && i < length(level_to_var)
704716
# we have `x = 0`
@@ -709,7 +721,8 @@ function alias_eliminate_graph!(graph, var_to_diff, mm_orig::SparseMatrixCLIL)
709721
end
710722
if nlevels < (new_nlevels = length(level_to_var))
711723
for i in (nlevels + 1):new_nlevels
712-
var_to_diff[level_to_var[i - 1]] = level_to_var[i]
724+
li = level_to_var[i]
725+
var_to_diff[level_to_var[i - 1]] = li
713726
push!(updated_diff_vars, level_to_var[i - 1])
714727
end
715728
end

0 commit comments

Comments
 (0)