Skip to content

Require joint linearity before inlining an SCC as a linear system - #200

Open
ambujone wants to merge 1 commit into
JuliaComputing:mainfrom
ambujone:fix/inline-linear-scc-joint-linearity
Open

ambujone wants to merge 1 commit into
JuliaComputing:mainfrom
ambujone:fix/inline-linear-scc-joint-linearity

Conversation

@ambujone

@ambujone ambujone commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #165

get_linear_scc_linsol checks linearity one unknown at a time, so a product of two SCC unknowns such as z * y passes, and one unknown ends up in the other's coefficient. In the issue's model that leaves Tw inside A when solving for Tw, and topsort_equations reports a cycle.

This treats the SCC as nonlinear when a coefficient contains an unknown of the linear system, so it is torn as it is without inline_linear_sccs. ModelingToolkitBase's calculate_A_b uses the same check. Coefficients that depend on states or earlier SCCs are still inlined. The affected systems never compiled before, so output for systems that did compile is unchanged.

Test: D(x) ~ y - x, z ~ 2 - y, 0 ~ z * y + y - x with inline_linear_sccs = true now compiles to the same equations as the default mtkcompile; before, it errored with "The equations have at least one cycle". The issue's model also compiles, and T(20) matches the default at alpha = 0, 0.01, 0.05. The ModelingToolkitTearing tests and ModelingToolkit v11.47.0 test/structural_transformation pass locally on Julia 1.13.4.

Prepared with Claude Code.

`get_linear_scc_linsol` checks each residual for linearity in one
unknown of the linear system at a time. A product of two of them, such
as `z * y`, is linear in each separately, so both checks pass and the
coefficient of one in `A` contains the other. After
`__reduce_linear_system!` eliminates the matched unknowns, the solution
for an unknown depends on itself, and `mtkcompile` fails with "The
equations have at least one cycle."

Treat the SCC as nonlinear when a coefficient contains an unknown of
the linear system, as `calculate_A_b` in ModelingToolkitBase does, so
it is torn and solved as it is without `inline_linear_sccs`.
Coefficients may still contain unknowns outside the linear system.

License: Apache-2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

inline_linear_sccs = true inlines a loop with a product of two loop unknowns → "The equations have at least one cycle"

2 participants