fix: record array elements in collect_operator_variables - #4982
Draft
ChrisRackauckas-Claude wants to merge 1 commit into
Draft
fix: record array elements in collect_operator_variables#4982ChrisRackauckas-Claude wants to merge 1 commit into
ChrisRackauckas-Claude wants to merge 1 commit into
Conversation
An operator applied to an array variable or a slice, such as `D(u[2:4])`, names the array
rather than its elements. Every caller tests membership of the scalar unknowns, so nothing
matched and the variables were silently misclassified.
The visible consequence is in DAEProblem: `differential_vars` is built as
`map(Base.Fix2(in, collect_differential_variables(sys)), unknowns(sys))`, so a system whose
equations differentiate an array slice reports *every* variable as algebraic.
Before: collect_differential_variables(D(w[2:3]) ~ w[1:2]) == Set([w[2:3]])
membership map over unknowns == [0, 0, 0, 0]
After: Set([w[2], w[3]])
membership map over unknowns == [0, 1, 1, 0]
Scalar operators take the identical path as before.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9
AayushSabharwal
requested changes
Aug 17, 2026
AayushSabharwal
left a comment
Member
There was a problem hiding this comment.
The comments in #4982 on this part of the code apply here.
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.
The bug
collect_operator_variablesrecordsarguments(v)[1]for each operator application. Whenthe operator is applied to an array variable or a slice —
D(u[2:4])— that argument isthe array, not its elements. Every caller tests membership of the scalar unknowns, so
nothing ever matches.
The visible consequence is in
DAEProblem, which buildsso a system whose equations differentiate an array slice reports every variable as
algebraic, and DAE initialization then has nothing to work from.
Failing before, passing after
unknownsSet([w[2:3]])[0, 0, 0, 0]Set([w[2], w[3]])[0, 1, 1, 0]Measured downstream on a real system (a MethodOfLines finite-difference discretization
that emits its interior as one array equation),
DAEProblem'sdifferential_varsgoesfrom 0/21 to 19/21 at one grid size and 0/81 to 79/81 at another — 19 and 79 being
the interior points, with the two boundary points genuinely algebraic.
The change
One function. When the operator's argument is array-valued, scalarize it and record the
elements; otherwise behave exactly as before, so scalar operators take an identical path.
Tests
lib/ModelingToolkitBase/test/variable_utils.jlgains a testset covering a whole arrayvariable, a slice, an unaffected scalar derivative, and the
differential_vars-stylemembership test that motivated the fix. It fails on unpatched code with the
[0, 0, 0, 0]map shown above.(145 = 141 pre-existing + 4 added.)
Not verified
I ran the
variable_utilsfile, not the full ModelingToolkitBase suite — the change isadditive and confined to one branch of one function, but a full run has not been done
locally. Other callers of
collect_operator_variables(collect_applied_operators,Shift-based discrete paths) are unaffected for scalar arguments and now see elementsrather than arrays for array ones, which is the intended correction but is worth a
reviewer's eye.
Context
Found while making finite-difference PDE discretizations emit array (slice-form) equations
instead of one scalar equation per grid point — see SciML/MethodOfLines.jl#428. This is one
of several places where array equations lose their shape; the others (equation counting,
implicit-DAE codegen row indexing) are more invasive and will be proposed separately.
🤖 Generated with Claude Code
https://claude.ai/code/session_01S1dxUEPGysx27SSSpxtiQ9