Skip to content

Commit b892604

Browse files
Merge pull request #3759 from AayushSabharwal/as/alias-elim
fix: do not add redundant `var ~ 0` equations in underdetermined systems
2 parents 3fd9073 + d0bffc1 commit b892604

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/systems/alias_elimination.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function alias_eliminate_graph!(state::TransformationState; kwargs...)
88
end
99

1010
@unpack graph, var_to_diff, solvable_graph = state.structure
11-
mm = alias_eliminate_graph!(state, mm)
11+
mm = alias_eliminate_graph!(state, mm; kwargs...)
1212
s = state.structure
1313
for g in (s.graph, s.solvable_graph)
1414
g === nothing && continue
@@ -347,27 +347,29 @@ function do_bareiss!(M, Mold, is_linear_variables, is_highest_diff)
347347
(rank1, rank2, rank3, pivots)
348348
end
349349

350-
function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL)
350+
function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL; fully_determined = true, kwargs...)
351351
@unpack structure = state
352352
@unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure
353353
# Step 1: Perform Bareiss factorization on the adjacency matrix of the linear
354354
# subsystem of the system we're interested in.
355355
#
356356
ils, solvable_variables, (rank1, rank2, rank3, pivots) = aag_bareiss!(structure, ils)
357357

358-
## Step 2: Simplify the system using the Bareiss factorization
359-
rk1vars = BitSet(@view pivots[1:rank1])
360-
for v in solvable_variables
361-
v in rk1vars && continue
362-
@set! ils.nparentrows += 1
363-
push!(ils.nzrows, ils.nparentrows)
364-
push!(ils.row_cols, [v])
365-
push!(ils.row_vals, [convert(eltype(ils), 1)])
366-
add_vertex!(graph, SRC)
367-
add_vertex!(solvable_graph, SRC)
368-
add_edge!(graph, ils.nparentrows, v)
369-
add_edge!(solvable_graph, ils.nparentrows, v)
370-
add_vertex!(eq_to_diff)
358+
if fully_determined == true
359+
## Step 2: Simplify the system using the Bareiss factorization
360+
rk1vars = BitSet(@view pivots[1:rank1])
361+
for v in solvable_variables
362+
v in rk1vars && continue
363+
@set! ils.nparentrows += 1
364+
push!(ils.nzrows, ils.nparentrows)
365+
push!(ils.row_cols, [v])
366+
push!(ils.row_vals, [convert(eltype(ils), 1)])
367+
add_vertex!(graph, SRC)
368+
add_vertex!(solvable_graph, SRC)
369+
add_edge!(graph, ils.nparentrows, v)
370+
add_edge!(solvable_graph, ils.nparentrows, v)
371+
add_vertex!(eq_to_diff)
372+
end
371373
end
372374

373375
return ils

src/systems/systemstructure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ function _mtkcompile!(state::TearingState; simplify = false,
753753
ModelingToolkit.markio!(state, orig_inputs, inputs, outputs, disturbance_inputs)
754754
state = ModelingToolkit.inputs_to_parameters!(state, [inputs; disturbance_inputs])
755755
end
756-
sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...)
756+
sys, mm = ModelingToolkit.alias_elimination!(state; fully_determined, kwargs...)
757757
if check_consistency
758758
fully_determined = ModelingToolkit.check_consistency(
759759
state, orig_inputs; nothrow = fully_determined === nothing)
@@ -765,7 +765,7 @@ function _mtkcompile!(state::TearingState; simplify = false,
765765
var_eq_matching = pantelides!(state; finalize = false, kwargs...)
766766
sys = pantelides_reassemble(state, var_eq_matching)
767767
state = TearingState(sys)
768-
sys, mm = ModelingToolkit.alias_elimination!(state; kwargs...)
768+
sys, mm = ModelingToolkit.alias_elimination!(state; fully_determined, kwargs...)
769769
sys = ModelingToolkit.dummy_derivative(
770770
sys, state; simplify, mm, check_consistency, fully_determined, kwargs...)
771771
else

test/initializationsystem.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,13 @@ sol = solve(prob, Tsit5())
455455
# Initialize with an observed variable
456456
prob = ODEProblem(simpsys, [z => 0.0], tspan, guesses = [x => 2.0, y => 4.0])
457457
sol = solve(prob, Tsit5())
458-
@test sol.u[1] == [0.0, 0.0]
458+
@test sol[z, 1] == 0.0
459459

460460
prob = ODEProblem(simpsys, [z => 1.0, y => 1.0], tspan, guesses = [x => 2.0])
461461
sol = solve(prob, Tsit5())
462462
@test sol[[x, y], 1] == [0.0, 1.0]
463463

464-
# This should warn, but logging tests can't be marked as broken
465-
@test_logs prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0])
464+
@test_warn "underdetermined" prob = ODEProblem(simpsys, [], tspan, guesses = [x => 2.0, y => 1.0])
466465

467466
# Late Binding initialization_eqs
468467
# https://github.com/SciML/ModelingToolkit.jl/issues/2787

test/symbolic_events.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ end
13681368
D(x) ~ p2,
13691369
x2 ~ p_1(x)
13701370
]
1371-
@mtkcompile sys = ODESystem(eq, t, [x, x2], [p_1, p2], discrete_events = [event])
1371+
@mtkcompile sys = System(eq, t, [x, x2], [p_1, p2], discrete_events = [event])
13721372

13731373
prob = ODEProblem(sys, [], (0.0, 1.0))
13741374
sol = solve(prob)

0 commit comments

Comments
 (0)