Skip to content

feat: accept array equations on the NonlinearProblem path - #5049

Closed
utkuyilmaz1903 wants to merge 6 commits into
SciML:masterfrom
utkuyilmaz1903:feature/array-nonlinear-systems
Closed

utkuyilmaz1903 wants to merge 6 commits into
SciML:masterfrom
utkuyilmaz1903:feature/array-nonlinear-systems

Conversation

@utkuyilmaz1903

@utkuyilmaz1903 utkuyilmaz1903 commented Aug 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Accept unscalarized array equations in NonlinearProblem and NonlinearLeastSquaresProblem after complete.
  • Assemble mixed scalar and array equations into one residual, with one row per array element.
  • Preserve out-of-place ForwardDiff.Dual values from unknowns and tunable parameters.
  • Zero the derivatives of array unknowns and of slices in the steady-state conversion.
  • Preserve FunctionWrapperSpecialize and non-square residual prototypes.

Constraints

  • Array residuals require mtkcompile before using jac = true or sparse = true.
  • The out-of-place allocator is restricted to nonlinear codegen. DAEProblem{false} Dual promotion is outside this PR.

Tests

Compat

  • New tests import unwrap from Symbolics so they run on older Symbolics.
  • DiffEqBase floor is 7.18.1 so downgrade CI accepts already-unwrapped parameters in nested AutoDespecialize.

Related: SciML/MethodOfLines.jl#623.

@utkuyilmaz1903

Copy link
Copy Markdown
Member Author

Comment thread docs/src/API/model_building.md Resolved
@utkuyilmaz1903
utkuyilmaz1903 force-pushed the feature/array-nonlinear-systems branch from 1e0635b to f16f220 Compare September 13, 2026 15:00
@utkuyilmaz1903
utkuyilmaz1903 force-pushed the feature/array-nonlinear-systems branch from 85f51d1 to 1841f4e Compare September 13, 2026 20:54
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/ModelingToolkit.jl that referenced this pull request Sep 13, 2026
Stacked on SciML#5049: array *equations* are accepted by the implicit-DAE and
nonlinear paths, but the opt-in is a hardcoded
`!implicit_dae && !(constructor <: NonlinearFunction)` test in
`__process_SciMLProblem` — a codegen flag and a constructor-type check
doing capability duty. This treats accepting array equations as a named,
per-constructor capability.

- `accepts_array_equations(constructor)` gates `__process_SciMLProblem`.
  `DAEFunction` and `NonlinearFunction` opt in, matching SciML#5049's behavior
  exactly (`HomotopyProblem` inherits nonlinear support through the same
  constructor). Every other problem type still needs `mtkcompile`, and
  opting a new constructor in is a one-line method.
- `has_array_equations(eqs)` gets a docstring; it is the shared,
  shape-based detection predicate behind `check_array_equations` and the
  Jacobian guards.
- `DAEFunction` throws the same `ArgumentError` for `jac = true` /
  `sparse = true` on array residuals that `NonlinearFunction` gets: today
  `jac = true` dies inside `executediff` and `sparse = true` silently
  builds a wrong-shaped `jac_prototype` (one row per equation instead of
  one per residual row).

Related: SciML#4983, SciML#5049, SciML#5131, SciML#5136.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: local Devin CLI session, no shareable URL (workspace /home/crackauc/sandbox/tmp_20260913_101544_55869)
@ChrisRackauckas

Copy link
Copy Markdown
Member

I'm incorporating this into another PR to make sure it's done uniformly with the other problem types.

@utkuyilmaz1903
utkuyilmaz1903 deleted the feature/array-nonlinear-systems branch September 13, 2026 21:40
ChrisRackauckas added a commit that referenced this pull request Sep 14, 2026
…ODE support and MultiObjectiveOptimizationFunction (#5148)

* feat: accept array equations on the NonlinearProblem path

* fix: keep array-equation downgrade CI on DiffEqBase 7.18.1

Import unwrap explicitly in the new NL tests and raise the DiffEqBase floor so nested AutoDespecialize accepts already-unwrapped parameters.

* ci: retrigger checks

* Apply suggestion from @ChrisRackauckas

* Zero the derivatives of array unknowns before expanding slices

* Test that array unknowns carry an array equation

* Gate array equations on a per-constructor capability trait

Stacked on #5049: array *equations* are accepted by the implicit-DAE and
nonlinear paths, but the opt-in is a hardcoded
`!implicit_dae && !(constructor <: NonlinearFunction)` test in
`__process_SciMLProblem` — a codegen flag and a constructor-type check
doing capability duty. This treats accepting array equations as a named,
per-constructor capability.

- `accepts_array_equations(constructor)` gates `__process_SciMLProblem`.
  `DAEFunction` and `NonlinearFunction` opt in, matching #5049's behavior
  exactly (`HomotopyProblem` inherits nonlinear support through the same
  constructor). Every other problem type still needs `mtkcompile`, and
  opting a new constructor in is a one-line method.
- `has_array_equations(eqs)` gets a docstring; it is the shared,
  shape-based detection predicate behind `check_array_equations` and the
  Jacobian guards.
- `DAEFunction` throws the same `ArgumentError` for `jac = true` /
  `sparse = true` on array residuals that `NonlinearFunction` gets: today
  `jac = true` dies inside `executediff` and `sparse = true` silently
  builds a wrong-shaped `jac_prototype` (one row per equation instead of
  one per residual row).

Related: #4983, #5049, #5131, #5136.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: local Devin CLI session, no shareable URL (workspace /home/crackauc/sandbox/tmp_20260913_101544_55869)

* Build `ODEProblem` from array differential equations without `mtkcompile`

`complete(sys)` followed by `ODEProblem` rejected array equations such as
`D(u[2:n-1]) ~ lap(u)`, forcing the finite-difference use case through the
scalarizing `mtkcompile`, whose output and compile time grow with the grid.
`DAEProblem` already accepts them, so give the explicit ODE path the same
treatment: the right-hand side of an array equation is written to a
contiguous block of `du` through `array_residual_maker`, the mass matrix
gets one row per element, and the residual form `D(x) .- f ~ 0` emitted by
MethodOfLines is rewritten to `D(x) ~ f` first. The generated code is
independent of the array length.

`full_equations` expands array equations into scalar rows so that the
symbolic jacobian, sparsity and initialization paths keep one equation per
row, and `ODEFunction` only computes the W sparsity pattern when `sparse`
or `sparsity` asks for it.

Co-authored-by: Cursor <[email protected]>

* Update lib/ModelingToolkitBase/src/utils.jl

Co-authored-by: Aayush Sabharwal <[email protected]>

* Expect ODEProblem to accept array equations over scalar unknowns in the #2597 test

Co-authored-by: Cursor <[email protected]>

* Assert the operator type when re-applying a slice derivative to its elements

Co-authored-by: Cursor <[email protected]>

* Solve the array-equation DAE tests from a consistent du0 instead of BrownFullBasicInit

The downgrade CI environment (DiffEqBase 7.15) trips a despecialization
wrapper assertion inside BrownFullBasicInit for DAEFunctions, which aborted
the InterfaceII group before the new ODEProblem tests could run. The tests
only need a solve to compare against the analytic solution, so hand the
solver a consistent du0 and skip DAE initialization.

Co-authored-by: Cursor <[email protected]>

* Share the array-equation predicate and accept them on the ODE path

`is_array_equation` (from the ODE array-equation support) and
`has_array_equations` were two spellings of the same shape check; keep the
per-equation predicate in `utils.jl` and make `has_array_equations` delegate
to it. `ODEFunction` opts into `accepts_array_equations`, matching what its
`generate_rhs` branch already assembles through `array_residual_maker`.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: local Devin CLI session, no shareable URL (workspace /home/crackauc/sandbox/tmp_20260913_101544_55869)

* Expect array unknowns to flatten in the ODE array-equation tests

Post-#5131 `flat_unknowns` handles the array unknown unconditionally, so an
array equation over `u(t)[1:4]` declared as `[u]` constructs and evaluates
rather than throwing "array unknowns".

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: local Devin CLI session, no shareable URL (workspace /home/crackauc/sandbox/tmp_20260913_101544_55869)

* Update rejection tests for the ODE array-equation capability

`ODEProblem` accepts array equations now, so it no longer belongs in the
"still require scalarized equations" testset (`SDEProblem` rejection remains
covered in the same file), and a `D(u) ~ -u` system over the array unknown
`u` constructs because `flat_unknowns` handles it.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.21
Agent-Model: SWE-2 Max
Agent-Session: local Devin CLI session, no shareable URL (workspace /home/crackauc/sandbox/tmp_20260913_101544_55869)

* Reject residual-form array differential equations on the ODE path

The scalar path only accepts `D(x) ~ f` from `complete`; `D(x) - f ~ 0` is a DAE
and needs `mtkcompile` or `DAEProblem`. Treat `D(u[2:4]) .- f ~ 0` the same way
instead of pattern-matching it back into explicit form in every consumer.

Also drop the duplicate InterfaceII include of `array_equation_dae.jl` (InterfaceI
already runs it) and scope the docs note about `jac`/`sparse` to array equations.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Agent-Harness: Claude Code
Agent-Model: claude-fable-5-1
Agent-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga
Claude-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga

* Construct MultiObjectiveOptimizationFunction from a system's cost vector

`OptimizationProblem` accepts `f::MultiObjectiveOptimizationFunction` in
SciMLBase, but MTK could never produce one: `generate_cost` always folds
`get_costs(sys)` through `consolidate` into a scalar.

- `costs(sys)` returns the unconsolidated objective vector: the system's own
  costs plus the consolidated cost of each subsystem, namespaced.
- `MultiObjectiveOptimizationFunction(sys)` generates a vector-valued objective
  over `costs(sys)`. `jac` builds the objective jacobian; `hess` is a vector
  with one generated hessian function per objective, the shape
  `OptimizationBase.instantiate_function` iterates.
- The constraint fields are assembled by `generate_constraint_fields`, shared
  with `OptimizationFunction` instead of duplicated.
- `OptimizationProblem(sys, op; multiobjective = true)` routes through it.
  `weights` cannot combine with it.

Also drop the `jac`/`sparse` guards on `NonlinearFunction` and `DAEFunction`
for array equations: since `full_equations` scalarizes array equations, the
symbolic jacobian and sparsity pattern already come out with one row per
residual, so both now work and are tested against ForwardDiff and the analytic
stencil. The array-residual DAE tests go back to `BrownFullBasicInit`, which
runs on the DiffEqBase 7.18.1 floor.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Agent-Harness: Claude Code
Agent-Model: claude-fable-5-1
Agent-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga
Claude-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga

* Approve the multi-objective names as ModelingToolkit reexports

`costs` and the `generate_multiobjective_*` / `calculate_multiobjective_*`
functions are exported from ModelingToolkitBase and therefore reexported by the
ModelingToolkit facade; list them in the root QA allow-list.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Agent-Harness: Claude Code
Agent-Model: claude-fable-5-1
Agent-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga
Claude-Session: https://claude.ai/code/session_01VorhgJYhDasyVLpGNrviga

---------

Co-authored-by: utkuyilmaz1903 <[email protected]>
Co-authored-by: Christopher Rackauckas <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Aayush Sabharwal <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
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.

2 participants