Skip to content

Commit 0608038

Browse files
Merge pull request #74 from JuliaComputing/as/trivial-tearing-state-priorities
fix: do not tear variables with positive priority in `trivial_tearing!`
2 parents a462d2e + edca708 commit 0608038

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/ModelingToolkitTearing/test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,21 @@ end
190190
@test sub.state_priorities == [2, 5, 2, 5]
191191
end
192192

193+
@testset "`trivial_tearing!` does not eliminate variables with positive state priority" begin
194+
# y only appears in `y ~ 2x`, making it a candidate for trivial tearing
195+
@variables x(t) y(t)
196+
@named sys = System([D(x) ~ x, y ~ 2x], t)
197+
ts = TearingState(sys)
198+
StateSelection.trivial_tearing!(ts)
199+
# Without state_priority, y is trivially torn into additional_observed
200+
@test findfirst(isequal(y), ts.fullvars) === nothing
201+
@test any(eq -> isequal(eq.lhs, y), ts.additional_observed)
202+
203+
# With positive state_priority, y should not be trivially torn
204+
@variables x(t) y(t) [state_priority = 1]
205+
@named sys = System([D(x) ~ x, y ~ 2x], t)
206+
ts = TearingState(sys)
207+
StateSelection.trivial_tearing!(ts)
208+
@test findfirst(isequal(y), ts.fullvars) !== nothing
209+
@test !any(eq -> isequal(eq.lhs, y), ts.additional_observed)
210+
end

src/tearing.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ function trivial_tearing!(ts::TransformationState, mm::Union{SparseMatrixCLIL, N
6262
for (i, vari) in candidates
6363
# don't check already torn equations
6464
i in trivial_idxs && continue
65+
if has_state_priorities(ts.structure)
66+
get_state_priorities(ts.structure)[vari] <= 0 || continue
67+
end
6568

6669
# if a variable was the LHS of two trivial observed equations, we wouldn't have
6770
# included it in the list. Error if somehow it made it through.

0 commit comments

Comments
 (0)