-
-
Notifications
You must be signed in to change notification settings - Fork 224
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
Keno
wants to merge
4
commits into
SciML:master
Choose a base branch
from
Keno:kf/unconstrainedwarning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+30
−14
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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 | ||
|
@@ -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() | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call needs to return There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.