Skip to content

MTKTearing: preserve_array_equations = :strict, broadcast-like residual forms (stacked on #158) - #174

Draft
xtalax wants to merge 3 commits into
mainfrom
array-groups-strict
Draft

xtalax wants to merge 3 commits into
mainfrom
array-groups-strict

Conversation

@xtalax

@xtalax xtalax commented Oct 21, 2026 •

Copy link
Copy Markdown

Stacked on #158. This branch contains #158's two commits (35361ef, 1c28107) plus one commit of its own. #158's branch lives on ChrisRackauckas-Claude's fork, so it can't be this PR's base; review only the last commit. The MTK side is SciML/ModelingToolkit.jl#5306, which is stacked on SciML/ModelingToolkit.jl#5102.

These are gap fixes for the acceptance tests in the PDEComponents P5 experiment (§4 of its report), which measured #5102/#158 against a pre-discretized heat rod (RodSystem: a thermal mass, HeatRod(N = 50) and a fixed-temperature sink).

Changes

  • T5, no silent dirty fallback. preserve_array_equations = :strict throws ArrayEquationNotPreservedError when an array differential equation would be scalarized. The error names the equation, the transformation that first broke its group, and the element. dirty_array_group! takes the reason (with a default, so existing two-argument calls still work), and ArrayEquationGroup stores the first reason and element. true keeps the documented lenient behaviour.
  • T1, eligibility or a loud refusal.
    • Residual forms are recognized when the residual is a call that scalarizes like broadcast (MethodOfLines' mol_broadcast, via SU.scalarization_function), and when the other side is any derivative-free expression rather than a literal zero. MOL emits mol_broadcast(+, -R, D(T[s])) ~ mol_broadcast(+, 0, mol_broadcast(*, 0, T[s])).
    • An array equation that contains D(x[slice]) but can't be grouped is recorded in TearingState.ungrouped_array_eqs. With true it is scalarized with a warning that names it; with :strict it throws.

Evidence

The fixture is PDEComponents.jl's generated HeatRod(N = 50) and lumped components. They were loaded through a shim that skips DyadInterface, because the test machine only has the General registry. "Base" means #158 at 1c28107 with MTK #5102. "Branch" means this PR with the MTK PR.

Test Base Branch
T5: rod + heater + rod.T[25] - rod.T[26] ~ 0, :strict (no :strict); true silently gives 24 scalar equations throws: the array equation D(rod.T[2:49]) ~ … was scalarized because index reduction differentiated the row (Pantelides eq_derivative!) (element rod.T[25]); true unchanged, 24 scalar equations, no warning
T1: MOL residual form, true 49 scalar equations, no warning 2 equations, one of them D(rod.T[2:49]); also under :strict
T1: residual vs explicit solution (DFBDF, abstol = reltol = 1e-10, t ∈ 0.5:0.5:10) — identical (max |Δ| = 0); 2.9e-9 K from the scalarized base
71×71 HeatedPlate, :strict — compiles in 5.1 s to 70 equations, one of them D(plate.T[2:70, 2:70])

T6 (alias targets) is on the MTK side.

Tests

  • ModelingToolkitTearing test/runtests.jl, run with MTK at #5102 (base) or the MTK PR (branch), Julia 1.12.6. Base: 28 top-level testsets, 160 passing. Branch: 29 testsets, 181 passing (the 21 new tests). No failures or errors on either.
  • MTK InterfaceI, Initialization and InterfaceII with this branch dev'd: see the MTK PR.

Not verified

  • Only ModelingToolkitTearing's own suite was run. The root StateSelection package and the other lib/ packages are unchanged, and their tests were not run.
  • The fixtures come from PDEComponents' generated code, loaded without DyadInterface. They were not run through PDETransientAnalysis.
  • The canonical right-hand side of the MOL form keeps the zero terms (0 + 0*T[s] + R), because mol_broadcast terms aren't simplified. Its effect on generated-code size and run time wasn't measured.

🤖 Posted by an AI agent · harness: Claude Code 2.1.306 · model: claude-opus-5-5
Review: unreviewed
Session: aa65e380-52df-443a-9f57-8c20c56babb9

ChrisRackauckas and others added 3 commits September 9, 2026 18:24
…structural simplification

Array equations `D(x[slice]) ~ rhs` are still scalarized into rows of the
bipartite graph, so matching, Pantelides, dummy derivatives, tearing and
alias elimination see exact per-element incidence. The rows now remember
the array equation they came from (`ArrayEquationGroup`, `row_group`,
`row_elem`), and every pass that rewrites a row in a way the array
equation cannot represent (differentiation, removal, dummy derivative
substitution, solving for another variable, inline linear SCCs, clock
partition splits) marks the group dirty. Rows of the integer-linear
subsystem that belong to a group are kept out of Gaussian elimination so
they are neither reduced nor used as pivots.

With `preserve_array_equations = true` on `DefaultReassembleAlgorithm`
(or as a `mtkcompile` keyword) intact groups are emitted as a single
array equation over scalar unknowns; dirty groups are emitted scalarized
as before. The default output is unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>
…collection

Fold the split/merge helpers into linear_subsys_adjmat! via
is_intact_array_group_row, and cover the mixed array-DE + linear algebraic case.

Co-authored-by: Cursor <cursoragent@cursor.com>
…al forms

`preserve_array_equations = :strict` throws `ArrayEquationNotPreservedError` instead
of scalarizing an array differential equation. The error names the equation, the
transformation that first broke its group and the element at which it broke;
`dirty_array_group!` now records both. With `true`, broken groups are still
scalarized silently, and array differential equations that cannot be tracked as a
group at all are scalarized with a warning that names them.

Residual forms are also recognized when the residual is a call that scalarizes like
`broadcast` (such as MethodOfLines' `mol_broadcast`, via `scalarization_function`)
and the other side is any derivative-free expression rather than a literal zero.

Stacked on #158 (35361ef, 1c28107).

Agent-Harness: Claude Code 2.1.306
Agent-Model: claude-opus-5-5
Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.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.

2 participants