Skip to content

Keep discretized dependent variables as array-valued unknowns - #701

Merged
ChrisRackauckas merged 6 commits into
SciML:masterfrom
ChrisRackauckas-Claude:agent/695-array-unknowns
Oct 13, 2026
Merged

ChrisRackauckas merged 6 commits into
SciML:masterfrom
ChrisRackauckas-Claude:agent/695-array-unknowns

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Note: Please ignore this draft until reviewed by @ChrisRackauckas.

What changed and why

The discretized System now has one unknown per dependent variable, the array variable u(t)[1:n, 1:m] that discretize_dep_vars already builds, instead of one scalar unknown per grid point. MethodOfLines implements PDEBase's new get_system_unknowns hook (SciML/PDEBase.jl#124) to register the array variable (or the scalar u(t) for a variable of t alone); discvars keeps the scalarized elements for stencil generation and the boundary/initial-condition maps, exactly as before.

The two problem-construction paths are affected differently:

  • Default DAE path. discretize(pdesys, disc) for a time-dependent system builds a DAEProblem from the residual equations without mtkcompile (_dae_problem; DAEProblem(pdesys, disc) is the same path), falling back to the compiled ODE path only for systems it cannot pose as a first-order DAE. This path now consumes the array unknowns directly: _dae_operating_point and brown_init_offenders enumerate the scalar elements of each unknown, and ModelingToolkitBase ≥ 1.72 lays u0 out as the flat concatenation of the unknowns, so the state vector is unchanged.
  • Explicit compiled ODE path. ODEProblem(mtkcompile(sys), nothing, tspan) on the result of symbolic_discretize (and the analytic = and fallback branches of discretize) is unchanged in behaviour because mtkcompile still scalarizes array unknowns; its cost only drops once mtkcompile keeps them, which is a separate ModelingToolkit question.

The solution wrappers locate a grid value with SymbolicIndexingInterface.variable_index (O(1) through the index cache, and correct for elements of array unknowns) instead of a linear findfirst over unknowns(sys).

This is a breaking change: version 1.5.1 → 2.0.0. unknowns(sys) of the system returned by symbolic_discretize changes shape and cardinality (one array per variable instead of one scalar per grid point), so code that enumerates it, builds an operating point from it, or filters it by name must scalarize first or work on mtkcompile(sys). discretize, DAEProblem(pdesys, disc), get_discrete, sol[u(t, x)], sol(t, x) and the problems' u0 are unchanged. NEWS.md has the breaking-change entry. Compat: PDEBase = "0.1.37" (the hook) and ModelingToolkitBase = "1.72" (problem construction from a completed system with array unknowns, SciML/ModelingToolkit.jl#5131); docs/Project.toml accepts the new major. CI cannot resolve until PDEBase 0.1.37 is registered; this PR is stacked on SciML/PDEBase.jl#124.

Tests: the pseudospectral tests counted scalar unknowns to check the number of grid degrees of freedom and now check the shape of the single array unknown; the 1D heat problem-construction test asserts one unknown whose elements are the get_discrete grid values.

Before/after on the 2D heat equation (Dt(u) ~ Dxx(u) + Dyy(u), Dirichlet BCs, x => n, y => n)

Julia 1.11.9, ModelingToolkitBase 1.76.0, SciMLBase 3.55.0, Symbolics 7.40.1; "before" is master (3f312ba) with registered PDEBase 0.1.36, "after" is this branch with the PDEBase PR developed. Second call of each (first call is compilation, ~65 s for symbolic_discretize in both). The explicit compiled path was not run at n = 100 (over two minutes at n = 50). Script: scratch/bench.jl in the transcript directory.

n length(unknowns(sys)) before → after length(u0) symbolic_discretize before → after default DAE path DAEProblem(pdesys, disc) before → after explicit compiled path ODEProblem(mtkcompile(sys)) before → after
5 25 → 1 25 0.02 s → 0.01 s 0.02 s → 0.02 s 0.02 s → 0.02 s
50 2,500 → 1 2,500 0.17 s → 0.18 s 2.81 s → 4.22 s (3.62 s → 2.96 s on the first pair) 127.7 s → 134.6 s
100 10,000 → 1 10,000 1.11 s → 0.60 s 50.3 s → 42.9 s (44.6 s → 43.2 s on the first pair) not run

So the unknown count is now O(1) in the grid, but problem-construction time is essentially unchanged. A profile of the n = 100 DAEProblem call (scratch/profile_dae2.jl) puts 96% of the 40 s inside ModelingToolkitBase.evaluate_varmap! (problem_utils.jl:541) called from varmap_to_vars in __process_SciMLProblem: for every one of the 20,000 flat unknowns and derivatives it does a get on the operating point that, for an element of an array unknown, scans the array (_any(_is_...) over getindex), which is quadratic in the grid size. Passing two array-valued entries (u => Matrix, D(u) => Matrix) instead of 20,000 element entries produces identical u0/du0 and the same 37–40 s (scratch/probe_op.jl), so the cost is not in how MethodOfLines writes the operating point; it needs an upstream change and is reported in REPORT.md rather than worked around here. The symbolic_discretize improvement at n = 100 comes from generate_system no longer flattening discvars.

Verification

Environment: a scratch project developing this branch and the PDEBase PR branch (Pkg.develop), then GROUP=<name> julia +1.11 --project=<scratch> -e 'using Pkg; Pkg.test("MethodOfLines")' for every group in test/runtests.jl, six at a time. All 24 functional groups exit 0 (the Broken column is pre-existing @test_broken):

Discretization    Equation discretization 3618/3618 (50m38s); Problem construction 56/56 (1m17s)
Components        MOLFiniteDifference Utils 38 pass 4 broken; Discretization of space and grid types 8/8; Nonlinear laplacian IV interpolation order 15/15; Variable PDE mapping and interior construction 9/9; Fornberg 9/9; WENO dispatch 9/9; WENO Non-Uniform Core 48/48; WENO Non-Uniform Boundary 205/205; WENO Boundary Integration 24/24; WENO Interface Coordinates 57/57; ODEFunction 0 (no assertions); Staggered constructors 3/3; MTK Metadata 17/17; Missing dependent variable 1/1; Discrete Callbacks 1/1
Sol_Interface     18/18 (5m02s)
MOL_Interface1    0 assertions, exit 0 (11m32s)
MOL_Interface2    8 pass 5 broken (7m01s)
Diffusion         269/269 (7m17s)
Diffusion_NU      150/150 (5m34s)
2D_Diffusion      2/2 (4m10s)
Brusselator       1154/1154 (28m03s)
Burgers           128/128 (3m21s)
Convection        8 pass 1 broken (3m48s)
Convection_NU     43/43; 109/109
Convection_WENO   309/309; 20/20; 31/31
Higher_Order      1 pass 2 broken (4m05s)
Stationary        8/8 (3m20s)
Mixed_Derivatives 2 pass 3 broken (7m00s)
DAE               22/22 (2m43s)
Integrals         2 pass 2 broken (2m27s)
Nonlinear_Diffusion    10 pass 2 broken (3m42s)
Nonlinear_Diffusion_NU 13 pass 2 broken (3m35s)
Nonlinlap_ADV     1/1 (2m44s)
Wave_Eq_Staggered 1 pass 5 broken (2m58s)
Pseudospectral    70/70; 174/174
Complex           102/102 (2m27s)

The Complex group passing is not evidence of complex-valued correctness: its assertions compare normalized squared magnitudes, and a phase-sensitive probe (im*Dt(ψ) ~ Dxx(ψ), initial (1+2im)*sinpi(x)) fails identically on master and on this branch because PDEBase's real/imaginary split loses the coupling. That is a pre-existing PDEBase defect outside this diff; the orchestrator is filing it separately.

Tail of the group this PR touches most:

Test Summary:           | Pass  Total      Time
Equation discretization | 3618   3618  50m38.6s
Test Summary:        | Pass  Total     Time
Problem construction |   56     56  1m17.2s
     Testing MethodOfLines tests passed

Failing-before evidence for the new assertions: on master symbolic_discretize of the 1D heat test gives 21 scalar unknowns, so @test length(unknowns(sys)) == 1 fails there and passes here (the benchmark table above shows the same 25 → 1 / 10,000 → 1 counts on master vs this branch).

QA: GROUP=QA through Pkg.test cannot resolve (PDEBase restricted to 0.1.37 by MethodOfLines, no registered version), so test/qa/qa.jl was run in a scratch copy of test/qa/Project.toml with both packages developed:

Test Summary:                                | Pass  Error  Total     Time
Quality Assurance                            |   20      1     21  4m30.9s
  Method ambiguity                           |    1             1    43.2s
  Unbound type parameters                    |    1             1     0.2s
  Undefined exports                          |    1             1     0.0s
  Compare Project.toml and test/Project.toml |    1             1     0.0s
  Stale dependencies                         |    1             1    19.7s
  Compat bounds                              |    4             4     0.2s
  Piracy                                     |    1             1     0.2s
  Persistent tasks                           |           1      1     3.3s
  ExplicitImports                            |    6             6    15.3s
  Public API documentation                   |    2             2     0.5s
  No unapproved public reexports             |    1             1     0.0s

The one error is Aqua's persistent-tasks manifest walk: Unable to locate CUDA_Runtime_jll, a dependency of CUDA (CUDA appears only as a weak dependency of ArrayInterface/DiffEqBase/LinearSolve/RecursiveArrayTools in that manifest). The identical manual run against master (3f312ba, registered PDEBase 0.1.36) gives the same result, 20 pass / 1 error with the same CUDA_Runtime_jll message, so it is pre-existing and environmental, not introduced here.

Unaddressed limitation (pre-existing, recorded, not worked around): DAEProblem(pdesys, disc; build_initializeprob = true) fails on master and on this branch with

Encountered operator `Differential(t, 1)` which has different independent variable than the one used in the system `nothing`.

(1D heat, x => 21; scratch/probe_initprob.jl, logs scratch/probe_initprob_{before,after}.log). The failure is in ModelingToolkit's initialization system for residual-form equations over slices (D(u[2:n-1]) - f ~ 0), independent of whether the unknowns are scalar or array-valued. The DAE constructor's default build_initializeprob = false is therefore a limitation this PR inherits and does not resolve, not a recommended usage; the orchestrator is filing the upstream issue.

Docs: docs/make.jl with this branch (at 1.6.0, before the 2.0.0 bump; the bump only changes the version, NEWS and the docs compat, and the docs project re-resolves with 2.0.0) and the PDEBase branch developed, linkcheck = false (the link check hit GitHub HTTP 429 rate limits on this machine, unrelated to the content), builds to completion: doctests run, all @example blocks execute, HTML rendered; the only warnings are the pre-existing api/discretization.md size-threshold warning and deployment auto-detection. Runic (--check) and typos are clean on every changed file.

Not verified

  • CI legs on Julia LTS, pre, and 32-bit.
  • The NeuralNets group (own environment, not in "All"); the extension does not touch unknowns.
  • The docs link check (rate limited locally).
  • GROUP=QA through the real Pkg.test path (blocked on the PDEBase release).

For the reviewer

  • Version bump. 2.0.0, because the representation of unknowns(sys) from symbolic_discretize changes for users (review round 1 finding). The problem-level API is unchanged.
  • get_system_unknowns(s::DiscreteSpace) derives the array from the first element of s.discvars[u] rather than storing it in a new DiscreteSpace field, to leave the struct and its three constructors untouched.
  • The pseudospectral tests' length(get_unknowns(sys)) == n assertions were rewritten as shape checks on the single array unknown (same degrees of freedom, new representation).
  • The construction-time finding above: the quadratic evaluate_varmap! behaviour in MTKBase is now the dominant cost at 100×100 and is outside this PR.

Closes #695. Stacked on SciML/PDEBase.jl#124.

🤖 Generated with Claude Code 2.1.280 (model: claude-fable-5-1); local session, transcript at /home/crackauc/sandbox/agent-jobs/MethodOfLines.jl/jobs/695-claude/log.txt on amdci2.julia.csail.mit.edu

CI triage (2026-09-25)

Triage of failures on head 7eeb213f (before this pass), compared to master tip b5ce4116 (all green). Fixes pushed: merge 2dd10848, PDEBase floor 38aea25f.

check classification evidence
tests / Complex (julia lts) master-red (fixed by merge of #703) PR fail: job Schroedinger atol=0.01 phase regression; master tip Complex lts success after #703
tests / Complex (julia 1) master-red (fixed by merge of #703) Same Schrodinger failure on PR; master tip Complex (Julia 1) success
Downgrade / Downgrade Tests PR-caused (fixed) job: UndefVarError: get_system_unknowns not defined with PDEBase v0.1.37; hook exists only in PDEBase 0.1.38. Bumped compat. Master Downgrade success (no hook import).
PDESystemLibrary.jl/Core / Downstream PR-caused (blocked) job: empty intersection between [email protected] and project compatibility 1. PDESystemLibrary pins MethodOfLines = "1". Needs a coordinated PDESystemLibrary compat bump; not fixable in this repo alone. Master Downstream success on MoL 1.x.
Documentation pending / not failed on prior head Old run still in progress at triage time; master tip Documentation success. Re-check after push.

Risk assessment

  • Risk: Low for the PDEBase floor bump (required API). Medium for the intentional 2.0.0 unknowns-shape break (pre-existing PR scope). Downstream will stay red until PDESystemLibrary allows MethodOfLines 2.
  • Blast radius: Package load / Downgrade CI under minimum PDEBase; any consumer enumerating scalar unknowns(sys) (documented break).
  • Evidence: Local load with PDEBase 0.1.38 succeeds (isdefined(PDEBase, :get_system_unknowns)); existing Discretization tests cover length(unknowns(sys)) == 1.
  • Independent review: none yet
  • Merge: needs review

🤖 Posted by an AI agent — harness: Cursor CLI autopilot · model: Composer
Conversation: /home/crackauc/.cursor/projects/home-crackauc-sandbox-fleet-master-jobs-nw-methodoflines-701/agent-transcripts/c1d3babe-56b1-4ef0-8d67-be79a9031b5f/c1d3babe-56b1-4ef0-8d67-be79a9031b5f.jsonl

  • Independent review (latest): Codex (gpt-6-astra), round 3 at 38aea25: MERGE. The post-round-2 commits (a master merge and the PDEBase 0.1.38 compat floor) are correct; registered PDEBase 0.1.38 has get_system_unknowns. The 2.0.0 major bump is correct. The PDESystemLibrary downstream job stays red until PDESystemLibrary's compat allows MOL 2.

ChrisRackauckas and others added 2 commits September 22, 2026 21:36
`discretize_dep_vars` builds the array variable `u(t)[1:n, ...]` and
scalarizes it, and PDEBase's `generate_system` then registered every
element as an unknown of the system, so `unknowns(sys)` grew with the
grid. Implement PDEBase's new `get_system_unknowns` hook to register the
array variable itself (or the scalar `u(t)` for a variable of `t`
alone); the elements in `discvars` still drive stencil generation and
the boundary and initial-condition maps.

The DAE path is adjusted for the new representation: the operating
point and the BrownFullBasicInit safety check enumerate the scalar
elements of each unknown, and the solution wrapper locates a grid
value through `variable_index`, which resolves both scalar unknowns
and elements of array unknowns and replaces the linear `findfirst`
over `unknowns(sys)`.

Requires PDEBase 0.1.37 for the hook and ModelingToolkitBase 1.72 for
problem construction from a `complete`d system with array unknowns.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Agent-Harness: Claude Code 2.1.280
Agent-Model: claude-fable-5-1
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/MethodOfLines.jl/jobs/695-claude/log.txt on amdci2.julia.csail.mit.edu
The pseudospectral tests counted scalar unknowns to check the number
of grid degrees of freedom; they now check the shape of the single
array unknown instead. The 1D heat problem-construction test asserts
that the system has one unknown whose elements are the discrete grid
values returned by `get_discrete`.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Agent-Harness: Claude Code 2.1.280
Agent-Model: claude-fable-5-1
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/MethodOfLines.jl/jobs/695-claude/log.txt on amdci2.julia.csail.mit.edu
`unknowns(sys)` of the system returned by `symbolic_discretize` changes
shape and cardinality, which is a breaking change for callers that
enumerate it, so the version is 2.0.0 rather than 1.6.0 and NEWS marks
the change as breaking. The docs project accepts the new major version.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Agent-Harness: Claude Code 2.1.280
Agent-Model: claude-fable-5-1
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/MethodOfLines.jl/jobs/695-claude/log.txt on amdci2.julia.csail.mit.edu
ChrisRackauckas and others added 2 commits September 24, 2026 23:40
Bring in the Schroedinger phase regression fix from SciML#703 so Complex CI can be re-evaluated against current master.
The array-unknown hook landed in PDEBase 0.1.38, not 0.1.37. Downgrade
CI pinned 0.1.37 and failed to import the hook at load time.

Co-Authored-By: Chris Rackauckas <[email protected]>
Agent-Harness: Cursor CLI autopilot
Agent-Model: Composer
Agent-Session: /home/crackauc/.cursor/projects/home-crackauc-sandbox-fleet-master-jobs-nw-methodoflines-701/agent-transcripts/c1d3babe-56b1-4ef0-8d67-be79a9031b5f/c1d3babe-56b1-4ef0-8d67-be79a9031b5f.jsonl
Co-authored-by: Cursor <[email protected]>
Resolve the Project.toml version conflict by keeping 2.0.0 (master is 1.5.2).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Agent-Harness: Claude Code
Agent-Model: claude-opus-5-5[1m]
Agent-Session: https://claude.ai/code/session_01WYPUXLxE9jVu2jXLFRd8ax
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.

Keep discretized dependent variables as array-valued unknowns instead of scalarizing per grid point

2 participants