Skip to content
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

Give a warning for underconstrained variables forced to 0 #2328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Keno
Copy link
Contributor

@Keno Keno commented Oct 25, 2023

@oxinabox noticed that the following MSL test
https://github.com/SciML/ModelingToolkitStandardLibrary.jl/blob/0f0fae138fe202140a4862eccaaac734051dc44f/test/Mechanical/translational.jl

has a test that depends on an underconstrained variable (either free.f or acc.f may be set to an arbitrary value without affecting the dynamics of the system). Our structural singularity removal logic detects these situations and chooses one variable arbitrarily to set to 0. However, it does so silently, so users are unaware that they likely have a modeling bug. This PR adds a warning when this happens. For example, the above mentioned test now gives:

┌ Warning: The model is under-constrained. Variable acc₊flange₊f(t) was arbitrarily chosen to be set to 0. This may indicate a model bug!
└ @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/alias_elimination.jl:54

This is styled as an overridable callback, so higher level modeling frameworks that use MTK as a library can hook into this to give more domain-specific errors if desired.

@ChrisRackauckas
Copy link
Member

I'm not sure this makes sense here? I would think the check should be done on the complete call

@Keno
Copy link
Contributor Author

Keno commented Oct 25, 2023

How would that call know? This information is discovered during bareiss.

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.

@@ -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.

@oxinabox noticed that the following MSL test
https://github.com/SciML/ModelingToolkitStandardLibrary.jl/blob/0f0fae138fe202140a4862eccaaac734051dc44f/test/Mechanical/translational.jl

has a test that depends on an underconstrained variable (either
`free.f` or `acc.f` may be set to an arbitrary value without
affecting the dynamics of the system). Our structural singularity
removal logic detects these situations and chooses one variable
arbitrarily to set to 0. However, it does so silently, so users
are unaware that they likely have a modeling bug. This PR adds
a warning when this happens. For example, the above mentioned
test now gives:

```
┌ Warning: The model is under-constrained. Variable acc₊flange₊f(t) was arbitrarily chosen to be set to 0. This may indicate a model bug!
└ @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/alias_elimination.jl:54
```

This is styled as an overridable callback, so higher level modeling
frameworks that use MTK as a library can hook into this to give
more domain-specific errors if desired.
@Keno Keno force-pushed the kf/unconstrainedwarning branch from a193152 to e34f3de Compare October 26, 2023 09:48
@Keno
Copy link
Contributor Author

Keno commented Mar 26, 2024

Bump on this? Came up again.

@YingboMa
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants