Skip to content

Commit 6466d8a

Browse files
committed
Make alias_elimination more powerful
1 parent 86db7f6 commit 6466d8a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/systems/diffeqs/odesystem.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,5 @@ Base.:(==)(sys1::ODESystem, sys2::ODESystem) =
149149
function rename(sys::ODESystem,name)
150150
ODESystem(sys.eqs, sys.iv, sys.states, sys.ps, sys.pins, sys.observed, sys.tgrad, sys.jac, sys.Wfact, sys.Wfact_t, name, sys.systems)
151151
end
152+
153+
isdiffeq(eq) = eq.lhs isa Term && operation(eq.lhs) isa Differential

src/systems/reduction.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function flatten(sys::ODESystem)
66
else
77
return ODESystem(equations(sys),
88
independent_variable(sys),
9+
states(sys),
10+
parameters(sys),
911
observed=observed(sys))
1012
end
1113
end
@@ -116,7 +118,7 @@ function alias_elimination2(sys)
116118
# Case 2: One side is a differentiated var, the other is an algebraic var
117119
# substitute the algebraic var with the diff var
118120
diff_vars = filter(!isnothing, map(eqs) do eq
119-
if eq.lhs isa Term && eq.lhs.op isa Differential
121+
if isdiffeq(eq)
120122
eq.lhs.args[1]
121123
else
122124
nothing
@@ -148,14 +150,19 @@ function alias_elimination2(sys)
148150
# Case 3: Explicit substitutions
149151
del = Int[]
150152
for (i, eq) in enumerate(eqs)
153+
isdiffeq(eq) && continue
151154
res_left = get_α_x(eq.lhs)
152155
if !isnothing(res_left)
156+
# `α x = rhs` => `x = rhs / α`
153157
α, x = res_left
158+
push!(subs, x => _isone(α) ? eq.rhs : eq.rhs / α)
159+
push!(del, i)
160+
else
154161
res_right = get_α_x(eq.rhs)
155162
if !isnothing(res_right)
163+
# `lhs = β y` => `y = lhs / β`
156164
β, y = res_right
157-
multiple = β / α
158-
push!(subs, x => _isone(multiple) ? x : multiple * x)
165+
push!(subs, y => _isone(β) ? eq.lhs : β * eq.lhs)
159166
push!(del, i)
160167
end
161168
end

0 commit comments

Comments
 (0)