Skip to content

Fix symbolic save_idxs skipping translation when selecting all state variables - #1460

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-1454-symbolic-save-idxs-all-states
Jul 23, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-1454-symbolic-save-idxs-all-states

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

This PR should be ignored until reviewed by @ChrisRackauckas.

Fixes #1454.

Problem

Symbolic save_idxs silently skipped index translation when the selection named all state variables, leaking raw Symbol/Num into the integer indexing path.

With a 3-variable MTK Lotka-Volterra system:

save_idxs before
[:x, :z] (subset) OK → [1, 3]
[:x, :y, :z] (ALL) FAIL ArgumentError: unable to check bounds for indices of type Symbol
[:z, :y, :x] (ALL, permuted) FAIL (same)

Reproduced with Vector{Symbol} and Vector{Num} (the latter: TypeError: non-boolean (Num) used in boolean context).

Root cause

SavedSubsystem deliberately returns nothing for a save-everything request (its docstring: "returns nothing when no metadata is needed, including ... requests that save all state variables"). The get_save_idxs_and_saved_subsystem generic method's if saved_subsystem !== nothing guard misread that nothing as "no translation needed" and returned the raw symbolic save_idxs unchanged.

Fix

In the saved_subsystem === nothing branch, translate symbolic save_idxs to integer state indexes via variable_index, preserving the requested order (so permutations like [:z, :y, :x] reorder correctly rather than collapsing to a sorted/identity shortcut) and scalarizing symbolic arrays. Non-symbolic save_idxs (integers, Colon) pass through untouched; observed / non-state expressions are still rejected by SavedSubsystem as before.

Verification (run locally, Julia 1.11)

save_idxs = [:x, :y, :z] and [:z, :y, :x] (and their Num equivalents) now succeed, and the saved solution matches a full solve in the requested order:

[:x,:y,:z] order/value matches full [x,y,z]: true
[:z,:y,:x] order/value matches full [z,y,x]: true
[:x,:z]    order/value matches full [x,z]:   true
:x scalar  matches full x:                   true

Subset, single-symbol, integer, permuted-integer, and nothing cases all still work; [x + y] (non-state expression) still errors.

Tests

Added a regression test to test/downstream/solution_interface.jl in the "Saved subsystem" testset covering: symbolic all-vars (natural + permuted), Num all-vars, the get_save_idxs_and_saved_subsystem translation output, subset/single/integer regressions, and non-state rejection.

Full solution_interface.jl (Solution Indexing) suite passes locally: 260 passed, 0 failed, 0 errored.

…variables

`SavedSubsystem` returns `nothing` for a save-everything request, and the
`get_save_idxs_and_saved_subsystem` generic method misread that `nothing` as
"no translation needed", returning the raw symbolic `save_idxs` unchanged.
Raw `Symbol`/`Num` then leaked into the integer indexing path, failing with
`ArgumentError: unable to check bounds for indices of type Symbol` (or a
`TypeError` for `Num`) whenever the selection named all state variables,
including permutations like `[:z, :y, :x]`.

Translate symbolic `save_idxs` to integer state indexes in the `nothing`
branch, preserving the requested order (so permutations reorder correctly)
and scalarizing symbolic arrays. Non-symbolic `save_idxs` pass through
untouched, and observed/non-state expressions are still rejected.

Fixes SciML#1454

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 23, 2026 18:26
@ChrisRackauckas
ChrisRackauckas merged commit a8de0b1 into SciML:master Jul 23, 2026
47 of 63 checks passed
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Post-merge verification (master CI triage, 2026-07-23)

Status: MERGED (a8de0b1). Issue #1454 CLOSED.

Code review of the fix

Root cause confirmed: SavedSubsystem returns nothing for save-everything (docstring contract at ~L109–111). The generic get_save_idxs_and_saved_subsystem used to treat that as “no translation,” leaking raw Symbol/Num into integer indexing.

The fix introduces translate_symbolic_save_idxs / _append_state_indices! in the saved_subsystem === nothing branch and preserves requested order (so [:z,:y,:x] reorders rather than identity-collapsing). Regression tests in test/downstream/solution_interface.jl cover:

  • symbolic all-vars natural + permuted
  • Num all-vars
  • subset / single / integer regressions
  • non-state rejection

Adjacent all-vars paths checked

Path Status
Pure Symbol/Num all states Fixed + tested
All states, permuted Fixed + tested
Integer all states Still uses Int overload → SavedSubsystem non-nothing path; OK
Mixed Symbol+Int all states Translates via _append_state_indices! (NotSymbolic → push as-is); not covered by #1460 tests — follow-up regression added in #1462
Observed variables in save_idxs Still rejected by design (Can only save variables and timeseries parameters) — not an all-vars translation bug
All states + subset of ts-params SavedSubsystem returns non-nothing (save_all_states && !save_all_tsparams) → existing integer translation path

No remaining silent skip of symbolic→int translation when SavedSubsystem returns nothing for full state selection.

CI note

PR CI had some Downstream/Integration reds that match unrelated master failures (ODE InterfaceIII resize verbosity, Catalyst parent_sys, SciMLBase QA reexports) — not regressions from this change. Core + Downstream solution_interface path for the regression is the relevant gate.

Beforerr added a commit to Beforerr/SciMLBase.jl that referenced this pull request Nov 26, 2026
Since SciML#1460, `translate_symbolic_save_idxs` copies every non-symbolic index
array into a `Vector{Int}`, so static indices like `SOneTo(4)` lose their
type and an out-of-place `SVector` solve saves each point as a heap-allocated
`Vector`.
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.

Symbolic save_idxs skips index translation when selecting all state variables, leaking raw Symbol/Num into integer indexing

2 participants