Skip to content

Add Symbolic Analysis of MTK System on Exiting Integration - #4720

Open
Shreyas-Ekanathan wants to merge 13 commits into
SciML:masterfrom
Shreyas-Ekanathan:master
Open

Add Symbolic Analysis of MTK System on Exiting Integration#4720
Shreyas-Ekanathan wants to merge 13 commits into
SciML:masterfrom
Shreyas-Ekanathan:master

Conversation

@Shreyas-Ekanathan

Copy link
Copy Markdown

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Uses interface created in SciML/SciMLBase.jl#1434 and works with SciML/OrdinaryDiffEq.jl#3731 to analyze causes of instability

@AayushSabharwal AayushSabharwal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apart from the piracy, it might be helpful to have a way to disable this as a solve kwarg or ODEProblem flag. Places that run solve in a hot loop (e.g. parameter optimization) typically want to avoid any sort of symbolic manipulation inside the loop. Alternatively, do we really need to identify the exact failing boolean expression? The user already has provided assertions in a way that makes sense for them so why does MTK need to do the extra effort?

return term
end

function SciMLBase.diagnose_symbolic_instability(integrator::SciMLBase.DEIntegrator)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is type piracy. MTK owns neither the function diagnose_symbolic_instability or the type DEIntegrator. Can this use something like get_root_indp in SciMLBase to obtain the system (if present) and dispatch on that?

@AayushSabharwal AayushSabharwal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just some performance/scaling comments.

Comment thread lib/ModelingToolkitBase/src/debugging.jl Outdated
Comment thread lib/ModelingToolkitBase/src/debugging.jl Outdated
return isempty(diagnosis) ? "" : join(diagnosis, "\n")
end

function find_singular_subterms(eq, expr, sub_map, diagnosis)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we need to substitute with the same sub_map over and over, it's a lot faster to build subber = SymbolicUtils.IRSubstituter{true}(get_irstructure(sys), sub_map) and call subber(expr) instead of substitute.


#find singularity causes in equations
singularities = String[]
for eq in equations(sys)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We might want to run this on full_equations? equations are often trivial and we actually codegen full_equations. This would require using IRSubstituter to scale to the massive expressions Multibody has.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we do want to scale to full_equations, find_singular_subterms should also maintain an IdSet of expressions it has recursed into, so that it avoids recursing into the same subexpressions repeatedly. An example would be the same observed quantity substituted into multiple equations - it only needs to be reported and processed once.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should be addressed now, if you could take a look and lmk if anything else stands out that would be great!

Shreyas-Ekanathan and others added 3 commits July 13, 2026 11:17
Co-authored-by: Aayush Sabharwal <aayush.sabharwal@gmail.com>
Co-authored-by: Aayush Sabharwal <aayush.sabharwal@gmail.com>

@AayushSabharwal AayushSabharwal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just minor changes, looks good otherwise!

Comment thread lib/ModelingToolkitBase/src/debugging.jl Outdated
Comment thread lib/ModelingToolkitBase/src/debugging.jl Outdated
AayushSabharwal and others added 3 commits July 15, 2026 10:39
Co-authored-by: Aayush Sabharwal <aayush.sabharwal@gmail.com>
@Shreyas-Ekanathan

Copy link
Copy Markdown
Author

@AayushSabharwal can you rerun tests? the SciMLBase PR is merged now

@AayushSabharwal

Copy link
Copy Markdown
Member

This then needs a compat bump on SciMLBase

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.

2 participants