Skip to content

Commit 8ddb7ee

Browse files
committed
Add alias_elimination draft
1 parent 15a04a4 commit 8ddb7ee

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/systems/alias_elimination.jl

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,72 @@ using SymbolicUtils: Rewriters
22

33
const KEEP = typemin(Int)
44

5-
function alias_eliminate_graph(sys)
5+
function alias_eliminate(sys)
66
sys = flatten(sys)
77
s = get_structure(sys)
88
if !(s isa SystemStructure)
99
sys = initialize_system_structure(sys)
1010
s = structure(sys)
1111
end
12+
is_linear_equations, eadj, cadj = find_linear_equations(sys)
13+
14+
sys, v_eliminated, v_types, n_null_vars, degenerate_equations, linear_equations = alias_eliminate_graph(
15+
s, is_linear_equations, eadj, cadj
16+
)
17+
18+
s = structure(sys)
19+
@unpack fullvars = s
20+
21+
subs = Dict()
22+
if length(v_eliminated) - n_null_vars > 0
23+
for v in v_eliminated[n_null_vars+1:end]
24+
subs[fullvars[v]] = iszeroterm(v_types, v) ? 0.0 :
25+
isalias(v_types, v) ? fullvars[alias(v_types, v)] :
26+
-fullvars[negalias(v_types, v)]
27+
end
28+
end
29+
30+
dels = Int[]
31+
eqs = copy(equations(sys))
32+
for (ei, e) in enumerate(linear_equations)
33+
vs = 𝑠vertices(graph, e)
34+
if isempty(vs)
35+
push!(dels, e)
36+
else
37+
rhs = 0
38+
for vj in eachindex(vs)
39+
var = fullvars[vs[vj]]
40+
rhs += cadj[ei][vj] * var
41+
end
42+
eqs[e] = 0 ~ rhs
43+
end
44+
end
45+
deleteat!(eqs, dels)
46+
47+
for (ieq, eq) in enumerate(eqs)
48+
if !isdiffeq(eq) && !_iszero(eq.lhs)
49+
eq = 0 ~ eq.rhs - eq.lhs
50+
end
51+
eqs[ieq] = eq.lhs ~ fixpoint_sub(eq.rhs, dict)
52+
end
1253

54+
newstates = []
55+
sts = states(sys)
56+
for j in eachindex(sts)
57+
if isirreducible(v_types, j)
58+
isdervar(s, j) || push!(newstates, fullvars[j])
59+
end
60+
end
61+
62+
@set sys.structure = nothing
63+
@set sys.states = newstates
64+
return sys
65+
end
66+
67+
function alias_eliminate_graph(s::SystemStructure, is_linear_variables, eadj, cadj)
1368
@unpack graph, varassoc = s
1469
invvarassoc = inverse_mapping(varassoc)
1570

16-
is_linear_equations, eadj, cadj = find_linear_equations(sys)
1771
old_cadj = map(copy, cadj)
1872

1973
is_not_potential_state = iszero.(varassoc)
@@ -94,7 +148,7 @@ function alias_eliminate_graph(sys)
94148
end
95149

96150
degenerate_equations = rank3 < length(linear_equations) ? linear_equations[rank3+1:end] : Int[]
97-
return v_eliminated, v_types, n_null_vars, degenerate_equations
151+
return v_eliminated, v_types, n_null_vars, degenerate_equations, linear_equations
98152
end
99153

100154
iszeroterm(v_types, v) = v_types[v] == 0

0 commit comments

Comments
 (0)