Skip to content

Fix flattening of array dependent variables - #101

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:agent/fix-array-variable-flatten
Jul 29, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:agent/fix-array-variable-flatten

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Fixes #100. Independent of #99 — different files, either order merges.

Problem

Discretizing a PDESystem written against @variables (u(..))[1:N] fails on master:

MethodError: no method matching operation(::Symbolics.Arr{Num, 1})
  [1] get_ops(depvars::Vector{Symbolics.Arr{Num, 1}})   src/symbolic_utils.jl:303
  [2] PDEBase.VariableMap(...)                          src/variable_map.jl:42
  [3] symbolic_discretize(...)                          src/symbolic_discretize.jl:23

Introduced by #98, which rewrote chain_flatten_array_variables from a per-index @rule list to a Dict. Two defects:

  1. The map was keyed on the array operation alone. fullname varies per index but the key did not, so each iteration overwrote the previous and every u[i] collapsed onto whichever index came last.
  2. Symbolics.variable(fullname; T = symtype(name)) does not produce a callable. It yields an array-valued variable, so the index was never resolved away and an Arr propagated through dvs = map(safe_ch, dvs) into get_dvs, where get_ops called operation on it.

Probing the function directly on master shows both:

distinct replacements for u[1..6]: 1 of 6
eltype: Symbolics.Arr{Num, 1}

Fix

Key the map on (name, idxs), and build the replacement as a callable symbolic so u(x)[i] flattens to u_[i](x).

The result is safe_unwrapped: the surrounding traversal composes terms out of unwrapped arguments, so returning a Num would leak a wrapper into maketerm. My first attempt missed this and the new test caught it.

I deliberately kept #98's manual traversal instead of restoring Prewalk(Chain(rs)). The old form depended on Chain and Prewalk, non-public SymbolicUtils names that were in the QA ignore list removed by #98 — reverting would undo that. @variables is public Symbolics API, so no ignore is reintroduced.

Tests

New test/array_variables.jl asserts the two properties that broke: N distinct replacements for u[1..N], and that each result is a non-Arr callable term in the independent variable. Both fail on unfixed master.

Validation

Julia 1.12.6.

  • Reduced reproduction from MethodOfLines.jl/test/MOL_Interface1/MOLtest1.jl:231 ("Array u"): MethodError → discretize OK.
  • GROUP=Core: alloc_tests 12, array_variables 11, discrete_initialization 18, public_api 46 — all pass.
  • GROUP=QA: 20/20 pass.
  • MethodOfLines MOL_Interface1 (the group that surfaced this) runs clean against this branch with MOL compat locally widened. Note the parent testset reports 0 because SafeTestsets does not aggregate pass counts — only failures propagate, which is how the original error surfaced.
  • Runic --check clean.

Ignore until reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code

https://claude.ai/code/session_0117aJvytDT3y2jMi5Ng6Q5m

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Correction to a claim in the PR description.

I wrote that Chain and Prewalk are "non-public SymbolicUtils names" and that restoring Prewalk(Chain(rs)) would reintroduce a QA ignore. That is wrong. They are already public — just in the Rewriters submodule, which is itself public:

SymbolicUtils.Chain                isdefined=true   ispublic=false
SymbolicUtils.Prewalk              isdefined=true   ispublic=false
SymbolicUtils.Rewriters.Chain      isdefined=true   ispublic=true
SymbolicUtils.Rewriters.Prewalk    isdefined=true   ispublic=true
SymbolicUtils.Rewriters ispublic in parent = true

SymbolicUtils/src/rewriters.jl:38 exports Empty, IfElse, If, Chain, RestartedChain, Fixpoint, Postwalk, Prewalk, PassThrough.

The old QA ignore existed only because PDEBase imported them from the parent SymbolicUtils rather than from SymbolicUtils.Rewriters, which owns them — the same "reach the owner, not the re-exporter" issue that #98 fixed correctly for maketerm and metadata by moving to TermInterface. It just did not apply the same treatment here.

That means using SymbolicUtils.Rewriters: Chain, Prewalk would have satisfied strict QA with no ignore and no rewrite of chain_flatten_array_variables — and it was the rewrite that introduced #100.

This does not change the fix in this PR: it is verified and introduces no ignore either way. But it does open a cheaper alternative worth considering — revert chain_flatten_array_variables to the pre-#98 rule-based implementation and import the combinators from SymbolicUtils.Rewriters. That restores code that shipped in 0.1.30 and which I have separately confirmed working (the 0.1.30 control discretizes the Array u case fine), and deletes _replace_indexed_array_variables rather than repairing it.

Trade-off, briefly:

  • This PR as it stands: smaller diff, keeps Enforce strict SciMLTesting 2.4 QA #98's traversal, already verified. But it is newer code — it shipped with two defects and I introduced a third (Num leaking into maketerm) while fixing it, caught only by the new test.
  • Restore the 0.1.30 implementation: larger diff, but returns to long-tested code and removes a function. Needs re-verification.

Happy to switch it to the restore if preferred — @ChrisRackauckas. Ignore until reviewed.

SciML#98 rewrote `chain_flatten_array_variables` to avoid a QA ignore for
`Chain` and `Prewalk`. The rewrite keyed its replacement map on the array
operation alone, so every `u[i]` overwrote the previous entry and all
indices collapsed onto the last one, and it built replacements with
`Symbolics.variable(...; T = symtype(name))`, which yields an array-valued
variable rather than a callable. The resulting `Arr` reached `get_ops`, so
discretizing any `PDESystem` written against `@variables (u(..))[1:N]`
failed with `MethodError: operation(::Symbolics.Arr{Num, 1})`.

The ignore was avoidable without touching this code: `Chain` and `Prewalk`
are public in `SymbolicUtils.Rewriters`, which owns them, and were only
non-public via the parent `SymbolicUtils`. Import them from the owner and
restore the previous implementation, which shipped in 0.1.30.

Fixes SciML#100

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0117aJvytDT3y2jMi5Ng6Q5m
@ChrisRackauckas-Claude
ChrisRackauckas-Claude force-pushed the agent/fix-array-variable-flatten branch from 3e94cef to 6cb4f31 Compare July 29, 2026 12:56
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Force-pushed 6cb4f31, replacing the previous approach. Two changes since the last review pass.

1. Rebased onto current master. The branch was based on 3ea8c85, i.e. before #99 merged. Merging it as it stood would have reverted the ProblemTypeCtx restoration and reintroduced the silent PDETimeSeriesSolution → ODESolution regression. It is now based on 97c9de5 and the diff is only:

 src/PDEBase.jl                |  4 +++-
 src/make_pdesys_compatible.jl | 25 ++++---------------------
 test/array_variables.jl       | 29 +++++++++++++++++++++++

2. Switched from repairing the rewrite to restoring the original implementation, per the correction above. Chain and Prewalk were never non-public — they are exported from SymbolicUtils.Rewriters, which owns them and is itself public. Only the parent-module spelling was non-public. So the QA ignore that motivated #98's rewrite was avoidable by fixing the import, and the rewrite is what introduced #100.

This PR now imports Chain/Prewalk from SymbolicUtils.Rewriters and @rule from SymbolicUtils, and restores the Prewalk(Chain(rs)) implementation that shipped in 0.1.30. _replace_indexed_array_variables is deleted rather than repaired.

I also opened JuliaSymbolics/SymbolicUtils.jl#1021 to declare the combinators public in the top-level SymbolicUtils too, since the parent spelling already resolves and Symbolics depends on it. That PR is not a prerequisite for this one — the Rewriters path is public today, so no compat bump is needed here.

Validation on Julia 1.12.6, all re-run after the rebase:

Check Result
Array u reduced reproduction discretize OK (was MethodError)
GROUP=Core 12 + 11 + 18 + 46 pass
GROUP=QA 20/20, zero ignores
Runic --check clean

The 11 are test/array_variables.jl, which asserts the two properties that broke — N distinct replacements for u[1..N], and each result a non-Arr callable term. Worth noting they were written against the other implementation and pass unchanged here, since they assert behavior rather than internals. They fail on unfixed master either way.

MethodOfLines loads cleanly against this branch, with symbolic_discretize resolving to SciMLBase and Interval to IntervalSets.

Ignore until reviewed by @ChrisRackauckas.

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 29, 2026 21:18
@ChrisRackauckas
ChrisRackauckas merged commit 01a7e79 into SciML:master Jul 29, 2026
23 of 24 checks passed
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.

Regression on master: array-variable PDESystems fail to discretize (MethodError: operation(::Symbolics.Arr))

2 participants