Skip to content

Commit 18e6bcf

Browse files
authored
Merge pull request #1279 from SciML/myb/simp
Add quick_cancel in alias_elimination
2 parents 240809d + 7ccfd17 commit 18e6bcf

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/systems/alias_elimination.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using SymbolicUtils: Rewriters
33
const KEEP = typemin(Int)
44

55
function alias_elimination(sys)
6-
sys = initialize_system_structure(sys)
6+
sys = initialize_system_structure(sys; quick_cancel=true)
77
s = structure(sys)
88
is_linear_equations, eadj, cadj = find_linear_equations(sys)
99

src/systems/systemstructure.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module SystemStructures
33
using DataStructures
44
using Symbolics: linear_expansion, unwrap
55
using SymbolicUtils: istree, operation, arguments, Symbolic
6+
using SymbolicUtils: quick_cancel, similarterm
67
using ..ModelingToolkit
78
import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten,
89
value, InvalidSystemException, isdifferential, _iszero, isparameter,
@@ -13,6 +14,14 @@ using UnPack
1314
using Setfield
1415
using SparseArrays
1516

17+
quick_cancel_expr(expr) = Rewriters.Postwalk(
18+
quick_cancel,
19+
similarterm=(x, f, args) -> similarterm(
20+
x, f, args, SymbolicUtils.symtype(x);
21+
metadata=SymbolicUtils.metadata(x)
22+
)
23+
)(expr)
24+
1625
#=
1726
When we don't do subsitution, variable information is split into two different
1827
places, i.e. `states` and the right-hand-side of `observed`.
@@ -86,7 +95,7 @@ algvars_range(s::SystemStructure) = Iterators.filter(Base.Fix1(isalgvar, s), eac
8695
isalgeq(s::SystemStructure, eq::Integer) = s.algeqs[eq]
8796
isdiffeq(s::SystemStructure, eq::Integer) = !isalgeq(s, eq)
8897

89-
function initialize_system_structure(sys)
98+
function initialize_system_structure(sys; quick_cancel=false)
9099
sys = flatten(sys)
91100
ivs = independent_variables(sys)
92101
eqs = copy(equations(sys))
@@ -109,9 +118,12 @@ function initialize_system_structure(sys)
109118
vars = OrderedSet()
110119
for (i, eq′) in enumerate(eqs)
111120
if _iszero(eq′.lhs)
121+
rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs
112122
eq = eq′
113123
else
114-
eq = 0 ~ eq′.rhs - eq′.lhs
124+
lhs = quick_cancel ? quick_cancel_expr(eq′.lhs) : eq′.lhs
125+
rhs = quick_cancel ? quick_cancel_expr(eq′.rhs) : eq′.rhs
126+
eq = 0 ~ rhs - lhs
115127
end
116128
vars!(vars, eq.rhs)
117129
isalgeq = true
@@ -141,6 +153,8 @@ function initialize_system_structure(sys)
141153
algeqs[i] = isalgeq
142154
if isalgeq
143155
eqs[i] = eq
156+
else
157+
eqs[i] = eqs[i].lhs ~ rhs
144158
end
145159
end
146160

test/odesystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,8 @@ eqs = [D(x) ~ foo(x, ms); D.(ms) .~ 1]
499499
@named outersys = compose(emptysys, sys)
500500
prob = ODEProblem(outersys, [sys.x=>1.0; collect(sys.ms).=>1:3], (0, 1.0))
501501
@test_nowarn solve(prob, Tsit5())
502+
503+
# x/x
504+
@variables t x(t)
505+
@named sys = ODESystem([D(x) ~ x/x], t)
506+
@test equations(alias_elimination(sys)) == [D(x) ~ 1]

0 commit comments

Comments
 (0)