Repository navigation
Add ArrayDiscretization: emit interior PDE equations as slice-form array equations - #604
Merged
ChrisRackauckas merged 12 commits intoAug 7, 2026
Merged
Conversation
…tions Implements the array-equation representation from SciML#428: the interior of each PDE is discretized as a single symbolic array equation over slices of the underlying array variables, with pointwise scalar fallback for unsupported patterns and for near-boundary frame points. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
…rray equations - Probe special-scheme rules with subsmatch against the equation instead of isempty (mixed/euler-integration rules are generated unconditionally, and boundary machinery always emits rules keyed on the canonical depvar) - Detect boundary values in interior equations as depvar terms with a number argument - Emit array equations as lhs ~ zeros(size) since ~ cannot equate an array with scalar zero - Use unwrap_const for zero literals and symtype for array-valuedness - Guard arrayify against unhandled operators (integrals) - Bump version to 0.11.18, require PDEBase 0.1.30 Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
… stationary test - arrayify guarded on `Symbolics.Operator`, which is non-public in both Symbolics and SymbolicUtils (flagged by ExplicitImports QA) and, since `Integral <: Operator` is false, never actually caught the integrals its comment claimed. Guard on `!(op isa Function)` instead: public-API only, and correct for Integral and symbolic callables alike. - The stationary test asserted `retcode == Success`. `mtkcompile` tears that linear system down to a single unknown, for which the solver's progress-based stall criterion can trip while the solution is fully converged — locally the *scalar* path reports Stalled under TrustRegion with a solution agreeing to 1.1e-14. Pin NewtonRaphson for determinism and assert the solution against the scalar path and the analytic solution, which is strictly stronger than the retcode check. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
ChrisRackauckas-Claude
force-pushed
the
array-equations-428
branch
from
July 22, 2026 09:02
8818e0a to
c875f64
Compare
The tests reference SciMLBase.ReturnCode.Success but relied on a transitively loaded package re-exporting the SciMLBase binding. That holds at current dependency versions but not at the downgrade-CI floors, where the name is undefined. Import SciMLBase directly, as the other test files do. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
…ar path handles An A/B run of the full test suite with ArrayDiscretization as the default (identical package versions, differing only in the default strategy) showed MOL_Interface2 failing under the array path but passing under the scalar path. Array-valued dependent variables (`@variables u(..)[1:n]`) discretize to a nested getindex, so array_variable extracted a component rather than the grid-shaped array and slicing it threw. That error was not an ArrayDiscretizationFallback, so it escaped the catch and failed discretize outright instead of degrading to the pointwise path. - array_variable now checks the extracted parent is a grid-shaped array of the expected rank, falling back when it is not - any failure while building the array form now degrades to the pointwise path (rethrowing InterruptException); errors in the equation itself still surface from the scalar path, which raises them directly - regression test covering array-valued dependent variables Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
The scalar path emits ifelse(coef > 0, coef*pos, coef*neg). The array path used max(coef,0)*pos + min(coef,0)*neg because ifelse cannot broadcast over a symbolic array condition. The two agree on finite values but differ when the unselected stencil is Inf/NaN: the surrogate multiplies it by zero and yields NaN, where ifelse returns the finite branch. That is reachable when a downwind stencil overflows during a Newton iterate while the upwind branch stays finite. When the coefficient does not vary over the grid -- a literal, a parameter, or any expression of time alone -- the wind direction is a single scalar condition for the whole slice, and ifelse broadcasts fine. Use it there, reproducing the scalar path exactly. Grid-varying coefficients still need a per-point condition and keep the surrogate. Verified against the scalar path: literal 4.9e-15, parametric 0.0, time-dependent 0.0, grid-varying 0.0, solution-dependent 4.0e-15. Tests assert the emitted form per class. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
This strategy is opt-in, so existing code must keep getting the scalarized discretization; nothing asserted that until now. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
…back ArrayDiscretization silently discretizes pointwise when an equation contains a pattern with no slice representation. That is correct but easy to miss, so a user can opt in and not realize they got the scalar path. StrictArrayDiscretization raises ArrayDiscretizationError instead, naming the equation and the reason. The two strategies produce identical discretizations and differ only in that error handling, so the machinery dispatches on a Union alias rather than duplicating methods. Strict mode covers whole-equation fallback only. Boundary, corner and extrapolation equations, and interior points whose stencils differ near a boundary, are pointwise under either strategy because they are irregular by construction; erroring on those would make the mode unusable at approx_order = 4 on even the plainest equation. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
…n O(1) The interior collapsed to one equation, but boundary conditions stayed pointwise and scaled with the surface, so they dominated in higher dimensions: at an 11-point 3D grid, 602 of 603 equations were boundary equations. Boundaries on a face admit the same treatment one dimension down. The index along the boundary's own direction is fixed across the face, so every point on it selects the same stencil weights and tap offsets. Taking the weights the scalar path computes at a representative face point and expressing its taps as shifted slices therefore reproduces that path exactly, which is why the solutions are bitwise identical. 1D: 3 -> 3 equations (unchanged; a 1D face is a point, nothing to collapse) 2D: 117 -> 9 equations, constant in resolution (was O(n)), 10.6x -> 92x faster 3D: 603 -> 123 equations (was O(n^2), now O(n)), 4.5x -> 58x faster 3D is not yet O(1): a cube's 12 edges are emitted by the corner-equation path as a set difference rather than as faces, so they remain pointwise (12n - 10). Each edge is a contiguous line and could be sliced the same way. Also distinguishes benign fallbacks from unrepresentable ones. Declining to build an array form because nothing would be collapsed (a single-point 1D boundary) is a deliberate choice, not an unsupported pattern, so StrictArrayDiscretization must tolerate it as it already tolerates frame points. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
… every dimension Once the faces were sliced, the 3D edges were all that kept the boundary growing with resolution, at 12n - 10 equations. They stayed pointwise only because generate_corner_eqs! builds that region with setdiff, which returns a bag of indices and discards the structure. The region has structure: along each dimension a point lies in the band below the interior, the interior band, or the band above, and this region is exactly the combinations with at least two non-interior bands. Enumerating those recovers the boxes and gives 3^N - 2N - 1 of them, independent of resolution: none in 1D, the 4 corners in 2D, the 12 edges and 8 corners in 3D. Total equations are now 3^N at any grid size: 1D: 3 equations (was 3) 2D: 9 equations (was 9) 3D: 27 equations (was 12n - 10, i.e. 123 at n = 11) Discretization time is flat in resolution, against 47.9 s / 7.5 s / 7.8 s for the scalarized path at the largest 1D / 2D / 3D grids measured. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
ChrisRackauckas
marked this pull request as ready for review
August 7, 2026 00:05
This was referenced Aug 7, 2026
This was referenced Aug 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Draft — please ignore until reviewed by @ChrisRackauckas.
Depends on SciML/PDEBase.jl#97 (needs PDEBase 0.1.30 released before CI can pass).
Closes #428.
Summary
Adds an
ArrayDiscretizationdiscretization strategy that represents the interior of each PDE as a single unscalarized symbolic array equation over slices of the underlying array variables, instead of one scalar equation per grid point. For the 1D heat equation at second order, the entire discretized system is now 3 equations:i.e. the
D(u[2:end-1]) ~ u[1:end-2] .- 2u[2:end-1] .+ u[3:end]form sketched in #428. The number of symbolic equations is independent of grid resolution, and the array structure is what array-aware compilers need to generate looped code. Since MOL's discrete variables were already elements of array symbolics, unknowns, ICs, and the solution interface are unchanged.Compile-time payoff
mtkcompilescalarizes array equations, so theODEProblempath is numerically andperformance-wise unchanged today. But feeding the discretized equations straight to
Symbolics.build_function— i.e. any codegen path that does not scalarize — shows whatthe representation buys, on the 1D heat equation:
Expression size and compile time are constant in grid resolution for the array form and
linear for the scalarized form. The two generated functions agree to ≤3e-12 relative,
so this is not a cheaper computation — it is the same computation expressed once.
The generated code shows the mechanism directly. Array form, one broadcast over slices:
versus the scalarized form, which unrolls every grid point:
So the payoff is available now to consumers that keep the array form, and
mtkcompileiswhat currently erases it.
Scaling with grid resolution and dimension
Interior, boundary faces, and the corner/edge region are all emitted in slice form, so
the discretized system has the same number of equations at any grid resolution —
3^N,one per combination of {below the interior, inside it, above it} along each axis.
Discretization time for the heat equation:
Array equation counts are constant across every resolution tested (1D: 50→400 pts,
2D: 10²→30², 3D: 5³→11³), and the interior expression size is likewise constant, growing
only with dimension (34 / 67 / 106 nodes) as each axis adds one stencil.
The three regions collapse for the same reason. The interior stencil is translation
invariant. On a boundary face the index along that boundary's own direction is fixed, so
every point on the face selects identical stencil weights and tap offsets — the same
invariance one dimension down. The corner/edge region is a union of boxes once its band
structure is recovered rather than computed as a
setdiffover indices.Numerics are unchanged throughout: solutions match
ScalarizedDiscretizationbitwise inthese cases, because each path reuses the scalar path's own weight computation.
Benchmark source (self-contained, reproduces the table above)
Usage:
Design
ifelse(coef > 0, coef*pos, coef*neg)exactly whenever the coefficient does not vary over the grid (a literal, a parameter, or any expression of time alone), since the wind direction is then a single scalar condition per slice. Grid-varying coefficients need a per-point condition, whichifelsecannot express (broadcasting it over a symbolic array condition fails: the elementwise comparison carries symtypeAny, notBool), so those usemax(coef,0)*pos + min(coef,0)*neg— equal on finite values, differing only when the unselected stencil isInf/NaN.approx_order = 4) get pointwise scalar equations, mirroring the scalar path's stencil selection exactly.ScalarizedDiscretization: WENO/functional advection, nonlinear and spherical Laplacians, integrals, mixed derivatives, interface/periodic BCs, callbacks, boundary values inside interior equations, staggered grids, and mixed-dimension variable systems.Tests
New
Array_Discretizationtest group (wired intotest_groups.toml/runtests.jl): 1D/2D diffusion, Neumann/Robin BCs, constant andu-dependent advection (winding), space/time-dependent coefficients, 4th-order (frame points), nonuniform grid, coupled systems, stationaryNonlinearProblem, and fallback cases (periodic, nonlinear Laplacian, WENO). Every case asserts agreement withScalarizedDiscretizationand, where applicable, that the interior really is one array equation.Ran locally (Julia 1.12.4, dev'd PDEBase#97): Array_Discretization 41/41 pass — solutions match the scalar path to machine precision (bitwise-identical in most cases). Regression groups run locally: Stationary + Burgers 44/44 pass, Diffusion 288/289 (the single failure,
Test 03 order-8 Neumannconservation atol, and a separate WENO single-BC initialization error were confirmed to reproduce on clean master independently of this PR — filed as #605 and #606, both traced to the #594 / PDEBase 0.1.29 initialization wiring.CI status (rebased onto master 2026-07-22): the branch now sits on current master, inheriting #601's
ModelingToolkitfloor bump11.26.8 → 11.31.2and its Diffusion conservation-test stabilization. Version bumped to0.11.19(0.11.18 is already released). This clears two of the three previously-red checks:Downgrade— fixed by inheriting Fix MOL_Interface2 downgrade overflow #601 (closes Downgrade CI: OverflowError in StateSelection Bareiss during MTK initialization at the ModelingToolkit 11.26.8 lower bound #607). Verified locally: with MTK pinned to the new11.31.2floor,MOL_Interface2/MOLtest2.jlpasses with noOverflowError.Diffusionconservation flakiness (Edge-aligned grid numerics changed since #594: Diffusion Test 03 conservation assertion now flaky at its 1e-9 atol #605) — addressed by Fix MOL_Interface2 downgrade overflow #601's test stabilization, now inherited.DocumentationandQAremain red, but both are the self-hosted-runner-loss infrastructure failure tracked in Documentation workflow permanently red on master: flaky linkcheck 429s plus self-hosted runner loss #608 (they fail identically on master); the QA suite itself passes clean locally on this branch with only the repo's trackedBrokenentries, and the docs build completes withBUILD OKwhen linkcheck is disabled. Neither is a code issue in this PR.Every functional test group, including the new
Array_Discretizationgroup, passes across Julia 1/lts/pre.).Notes
ArrayDiscretizationstub struct and thegenerate_interiortuple method for it were replaced;generate_interioris now a single method since the array path derives slice bounds from the sameCartesianIndicesinterior the BC machinery uses.generate_ic_defaultsdispatch was widened fromScalarizedDiscretizationto all strategies (same body).PDEBase = "0.1.30".🤖 Generated with Claude Code
https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9