Skip to content

Commit 31f611b

Browse files
committed
Faster alias_elimination
1 parent ac1eda7 commit 31f611b

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

src/bipartite_graph.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ function BipartiteGraph(nsrcs::T, ndsts::T, backedge::Val{B} = Val(true);
314314
end
315315

316316
function Base.copy(bg::BipartiteGraph)
317-
BipartiteGraph(bg.ne, copy(bg.fadjlist), copy(bg.badjlist), deepcopy(bg.metadata))
317+
BipartiteGraph(bg.ne, map(copy, bg.fadjlist), map(copy, bg.badjlist),
318+
deepcopy(bg.metadata))
318319
end
319320
Base.eltype(::Type{<:BipartiteGraph{I}}) where {I} = I
320321
function Base.empty!(g::BipartiteGraph)

src/systems/alias_elimination.jl

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ function extreme_var(var_to_diff, v, level = nothing, ::Val{descend} = Val(true)
4747
level === nothing ? v : (v => level)
4848
end
4949

50-
alias_elimination(sys) = alias_elimination!(TearingState(sys; quick_cancel = true))
50+
alias_elimination(sys) = alias_elimination!(TearingState(sys))
5151
function alias_elimination!(state::TearingState)
5252
sys = state.sys
5353
complete!(state.structure)
54+
graph_orig = copy(state.structure.graph)
5455
ag, mm, complete_ag, complete_mm, updated_diff_vars = alias_eliminate_graph!(state)
5556
isempty(ag) && return sys
5657

@@ -118,10 +119,23 @@ function alias_elimination!(state::TearingState)
118119
old_to_new[i] = idx
119120
end
120121

121-
lineqs = BitSet(old_to_new[e] for e in mm.nzrows)
122-
for (ieq, eq) in enumerate(eqs)
123-
ieq in lineqs && continue
124-
eqs[ieq] = substitute(eq, subs)
122+
lineqs = BitSet(mm.nzrows)
123+
eqs_to_update = BitSet()
124+
for k in keys(ag)
125+
# We need to update `D(D(x))` when we subsitute `D(x)` as well.
126+
while true
127+
for ieq in 𝑑neighbors(graph_orig, k)
128+
ieq in lineqs && continue
129+
new_eq = old_to_new[ieq]
130+
new_eq < 1 && continue
131+
push!(eqs_to_update, new_eq)
132+
end
133+
k = var_to_diff[k]
134+
k === nothing && break
135+
end
136+
end
137+
for ieq in eqs_to_update
138+
eqs[ieq] = substitute(eqs[ieq], subs)
125139
end
126140

127141
for old_ieq in to_expand
@@ -817,9 +831,9 @@ end
817831
function update_graph_neighbors!(graph, ag)
818832
for eq in 1:nsrcs(graph)
819833
set_neighbors!(graph, eq,
820-
[get(ag, n, (1, n))[2]
821-
for n in 𝑠neighbors(graph, eq)
822-
if !haskey(ag, n) || ag[n][2] != 0])
834+
Int[get(ag, n, (1, n))[2]
835+
for n in 𝑠neighbors(graph, eq)
836+
if !haskey(ag, n) || ag[n][2] != 0])
823837
end
824838
return graph
825839
end

src/systems/systemstructure.jl

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,6 @@ function quick_cancel_expr(expr)
2525
kws...))(expr)
2626
end
2727

28-
#=
29-
When we don't do subsitution, variable information is split into two different
30-
places, i.e. `states` and the right-hand-side of `observed`.
31-
32-
eqs = [0 ~ z + x; 0 ~ y + z^2]
33-
states = [y, z]
34-
observed = [x ~ sin(y) + z]
35-
struct Reduced
36-
var
37-
expr
38-
idxs
39-
end
40-
fullvars = [Reduced(x, sin(y) + z, [2, 3]), y, z]
41-
active_𝑣vertices = [false, true, true]
42-
x y z
43-
eq1: 1 1
44-
eq2: 1 1
45-
46-
x y z
47-
eq1: 1 1
48-
eq2: 1 1
49-
50-
for v in 𝑣vertices(graph); active_𝑣vertices[v] || continue
51-
52-
end
53-
=#
54-
5528
export SystemStructure, TransformationState, TearingState
5629
export initialize_system_structure, find_linear_equations
5730
export isdiffvar, isdervar, isalgvar, isdiffeq, isalgeq, algeqs

0 commit comments

Comments
 (0)