Skip to content

mtkcompile: preserve_array_equations = :strict; prefer array elements as alias targets (stacked on #5102) - #5306

Draft
xtalax wants to merge 12 commits into
SciML:masterfrom
xtalax:array-groups-strict
Draft

xtalax wants to merge 12 commits into
SciML:masterfrom
xtalax:array-groups-strict

Conversation

@xtalax

@xtalax xtalax commented Oct 21, 2026

Copy link
Copy Markdown
Member

Stacked on #5102. The base branch array-groups-base (05003d5) is SciML master 9cde066 with #5102 (998d0df, 8768e37, 9094576, 7507240, 18af7bb) merged in. #5102 lives on ChrisRackauckas-Claude's fork, so it can't be this PR's base; review only the last commit (4b56525). It needs the ModelingToolkitTearing side, JuliaComputing/StateSelection.jl#174, which is stacked on JuliaComputing/StateSelection.jl#158. Without it the new three-argument dirty_array_group! call and MTKTearing.ArrayEquationNotPreservedError don't exist.

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 / T1 (docs and plumbing). The mtkcompile docstring and docs/src/internals/mtkcompile.md describe preserve_array_equations = :strict and the warning true now gives for array differential equations that can't be grouped. The SDE noise extraction passes a reason to dirty_array_group!. The behaviour itself is in StateSelection.jl#174.
  • T6, prefer the array element in state selection. Among the highest-priority candidates of an alias group, pick_alias_target prefers an element of an intact array differential equation. For x[k] ~ m with both differential, the old choice eliminated x[k], which left the row of D(x[k]) solved for D(m) and scalarized all N−2 rows. This changes default state selection too, with or without preserve_array_equations: it applies whenever an alias group contains an element of a still-intact array differential equation. It could be gated on the flag if a flag-independent choice isn't wanted.

Evidence

The fixture is PDEComponents.jl's generated HeatRod(N = 50) and lumped components, plus a free heater. They were loaded through a shim that skips DyadInterface, because the test machine only has the General registry. "Base" means array-groups-base with #158. "Branch" means this PR with StateSelection.jl#174. Solutions use DFBDF with abstol = reltol = 1e-10 and are compared on t ∈ 0.5:0.5:10.

Test Base Branch
T6: rod.T[25] ~ mass.T, true 48 scalar equations; heater.port.T and rod.T[2:49] \ rod.T[25] are the unknowns one array equation, D(rod.T[2:49]), and nothing else; rod.T[25] is the unknown and mass.T is observed; also compiles under :strict
T6: solution vs base scalar path — max |Δ| = 2.9e-9 K over the rod field and 1.3e-9 K for mass.T (the default false path: 5.4e-9 K)
T5: rod.T[25] - rod.T[26] ~ 0, :strict — throws ArrayEquationNotPreservedError with reason index reduction differentiated the row (Pantelides eq_derivative!) and element rod.T[25]
T1: MOL mol_broadcast residual form, true 49 scalar equations, silently one array equation; solution identical to the explicit form
71×71 HeatedPlate, :strict — 70 equations, one of them D(plate.T[2:70, 2:70]); 5.1 s

test/structural_transformation/array_equations.jl gains 18 tests covering these (:strict on the rod with u[k] - u[k+1] ~ 0, an intact group and the pendulum; the ungrouped-equation warning; u[k] ~ m aliasing, solved against the default path).

Tests

Julia 1.12.6, Pkg.test with ModelingToolkitTearing from the respective StateSelection branch.

Group Base Branch
InterfaceI (includes structural_transformation, state selection, reduction) 1634 pass, 3 broken 1652 pass, 3 broken (the 18 new tests)
Initialization 953 pass, 12 broken 953 pass, 12 broken
InterfaceII 1440 pass, 7 broken 1440 pass, 7 broken

Not verified

  • Other groups (SymbolicIndexingInterface, Downstream, Extensions, Optimization, FMI, QA) were not run.
  • T6 was only exercised on alias groups of two differential states. Groups with irreducible or positive-priority members follow the old rules first, by construction, but have no dedicated test.
  • The fixtures were not run through PDETransientAnalysis.

🤖 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 7 commits September 9, 2026 17:05
Propagate ModelingToolkitTearing's array equation groups through alias
elimination and brownian handling, and make map_variables_to_equations
understand preserved array equations.

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

- `mtkcompile(sys; preserve_array_equations = true)` emits intact array
  differential equations as single equations over scalar unknowns.
- `build_function_wrapper` rewrites `array_literal`s of consecutive buffer
  elements into `vcat`s of `view`s so generated code for preserved array
  equations does not grow with the array size.
- Alias elimination keeps array-equation rows out of the integer-linear
  elimination instead of restoring them afterwards.
- Document the design in the mtkcompile internals page; bump MTKTearing
  compat to 1.21.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
The status notes belong in the PR description, not in the built docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
# Conflicts:
#	Project.toml
#	lib/ModelingToolkitBase/Project.toml
…ts as alias targets

Documents `preserve_array_equations = :strict`, which throws an
`ArrayEquationNotPreservedError` naming the equation, the transformation and the
element instead of scalarizing an array differential equation, and the warning that
`true` now gives for array differential equations that cannot be grouped. The SDE
noise extraction passes its reason to `dirty_array_group!`. Needs the companion
StateSelection.jl change to ModelingToolkitTearing.

Alias elimination now prefers an element of an intact array differential equation as
the alias target among the highest-priority candidates. For `x[k] ~ m` with both
differential, eliminating `x[k]` left the row of `D(x[k])` solved for `D(m)` and
scalarized the whole array equation; keeping `x[k]` keeps it intact. This applies
whether or not `preserve_array_equations` is set.

Stacked on SciML#5102 (18af7bb) via array-groups-base.

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>
Runic wants the closing parenthesis of multi-line mtkcompile calls on its own line; this fixes the failing Runic Format Check on PR SciML#5306.

Agent-Harness: Claude Code 2.1.308
Agent-Model: claude-sonnet-5-5
Agent-Session: fbf12fae-28a4-496f-b740-3339fe3ac7a1
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
xtalax and others added 2 commits November 2, 2026 17:49
Conflicts: lib/ModelingToolkitBase/src/systems/codegen_utils.jl (this branch's array-literal compression helpers and master's find_arrvars_recurse were both added after find_arrvars_is_atomic; kept both), lib/ModelingToolkitBase/src/systems/systems.jl (mtkcompile docstring: kept both the preserve_array_equations entry and master's verbose / print_underconstrained_variables entries), src/systems/alias_elimination.jl (pick_alias_target gained both master's verbose::MTKVerbosity argument and this branch's array_elems argument, now (state, group_vars, irreducibles, verbose, array_elems = Set()); call site passes both).

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Conflict in codegen_utils.jl: kept both the array_literal compression helpers and the
RHS chunking constants.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor

Pushed f02cd6f6f5, a merge of current master into this branch. The only conflict was in codegen_utils.jl, where both sides had added a block at the same spot: the array_literal helpers from this PR and the chunking constants from the merged RHS chunking PR are both kept (compress_array_literals runs first in build_function_wrapper, then the chunking branch). The :strict tests in test/structural_transformation/array_equations.jl could not be run here because they need ModelingToolkitTearing 1.21, which is not released yet. The chunking tests pass against the merged codegen_utils.jl and Runic is clean on the touched files.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor

Pushed 78f52b2b0a, a merge of current master into this branch. The only conflict was the ModelingToolkitTearing compat line in Project.toml; it stays at 1.21, which this PR needs, so the branch still cannot resolve until that release exists. Chunking tests pass (56/56) and Runic is clean on the touched files; the :strict tests in test/structural_transformation/array_equations.jl could not be run for the same reason.


🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

Agent-Harness: Claude Code 2.1.319
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor

Pushed 7ee1ed046f, a merge of current master after #5352 landed. The only conflict was test/structural_transformation/runtests.jl, where both sides had appended a testset at the end; both are kept. No source files changed, and the merged tree was not run locally. The ModelingToolkitTearing 1.21 compat bump is unchanged and still waits on that release.


🤖 Posted by an AI agent · harness: Claude Code 2.1.319 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

This branch has not been deployed

No deployments
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