Skip to content

Give a warning for underconstrained variables forced to 0 #2328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions src/systems/alias_elimination.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using SymbolicUtils: Rewriters
using Graphs.Experimental.Traversals

function alias_eliminate_graph!(state::TransformationState; kwargs...)
function alias_eliminate_graph!(state::TransformationState;
Copy link
Member

@YingboMa YingboMa Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make warning reporting an option here, because sometimes MTK performs transformations that leave harmless dangling variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default here is not to warn. The warning is in the higher level function in alias_elimination! that has access to the variables. If you can tell which variables are harmless, you can suppress the warning there.

variable_underconstrained! = force_var_to_zero!, kwargs...)
mm = linear_subsys_adjmat!(state; kwargs...)
if size(mm, 1) == 0
return mm # No linear subsystems
end

@unpack graph, var_to_diff, solvable_graph = state.structure
mm = alias_eliminate_graph!(state, mm)
mm = alias_eliminate_graph!(state, mm; variable_underconstrained!)
s = state.structure
for g in (s.graph, s.solvable_graph)
g === nothing && continue
Expand Down Expand Up @@ -47,9 +48,17 @@ function alias_elimination!(state::TearingState; kwargs...)
sys = state.sys
complete!(state.structure)
graph_orig = copy(state.structure.graph)
mm = alias_eliminate_graph!(state; kwargs...)

fullvars = state.fullvars

function variable_underconstrained_mtk!(structure::SystemStructure,
ils::SparseMatrixCLIL,
v::Int)
@warn "The model is under-constrained. Variable $(fullvars[v]) was arbitrarily chosen to be set to 0. This may indicate a model bug!"
return force_var_to_zero!(structure, ils, v)
end
mm = alias_eliminate_graph!(state;
variable_underconstrained! = variable_underconstrained_mtk!, kwargs...)

@unpack var_to_diff, graph, solvable_graph = state.structure

subs = Dict()
Expand Down Expand Up @@ -347,7 +356,22 @@ function do_bareiss!(M, Mold, is_linear_variables, is_highest_diff)
(rank1, rank2, rank3, pivots)
end

function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL)
function force_var_to_zero!(structure::SystemStructure, ils::SparseMatrixCLIL, v::Int)
@unpack graph, solvable_graph, eq_to_diff = structure
@set! ils.nparentrows += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call needs to return ils because @set! creates a new object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I thought it was mutable, but of course it isn't.

push!(ils.nzrows, ils.nparentrows)
push!(ils.row_cols, [v])
push!(ils.row_vals, [convert(eltype(ils), 1)])
add_vertex!(graph, SRC)
add_vertex!(solvable_graph, SRC)
add_edge!(graph, ils.nparentrows, v)
add_edge!(solvable_graph, ils.nparentrows, v)
add_vertex!(eq_to_diff)
return ils
end

function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL;
variable_underconstrained! = force_var_to_zero!)
@unpack structure = state
@unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure
# Step 1: Perform Bareiss factorization on the adjacency matrix of the linear
Expand All @@ -359,15 +383,7 @@ function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLI
rk1vars = BitSet(@view pivots[1:rank1])
for v in solvable_variables
v in rk1vars && continue
@set! ils.nparentrows += 1
push!(ils.nzrows, ils.nparentrows)
push!(ils.row_cols, [v])
push!(ils.row_vals, [convert(eltype(ils), 1)])
add_vertex!(graph, SRC)
add_vertex!(solvable_graph, SRC)
add_edge!(graph, ils.nparentrows, v)
add_edge!(solvable_graph, ils.nparentrows, v)
add_vertex!(eq_to_diff)
ils = variable_underconstrained!(structure, ils, v)
end

return ils
Expand Down