Fix iterate(::AbstractTimeseriesSolution) to respect AbstractArray contract#1322
Merged
ChrisRackauckas merged 3 commits intoSciML:masterfrom Apr 23, 2026
Conversation
…contract
The previous implementation returned `solution_new_tslocation(sol, state)` —
a fresh solution of the same concrete type, with successively larger
`tslocation`. That violates the AbstractArray contract (element type must not
equal container type) and causes `LinearAlgebra.norm(sol)` on Julia 1.12 to
trip `norm_recursive_check`:
ArgumentError: cannot evaluate norm recursively if the type of the
initial element is identical to that of the container
This surfaced as failures in OrdinaryDiffEq's `VectorOfArray/StructArray
compatibility` testsets (LowStorageRK, SSPRK) where `sol_SA ≈ sol_SV`
compares two `ODESolution`s whose `u` is `Vector{VectorOfArray{Float64, 2,
StructVector{SVector{1, Float64}}}}`. The generic `Base.isapprox` for
`AbstractArray` falls through to `norm`, which hit the check.
Replace the custom `iterate` with the natural linear-indexing form
(`sol[state]`), which matches the AbstractArray iteration contract and
yields scalars for `VectorOfArray`-backed solutions. `solution_new_tslocation`
remains exported for callers (plot recipe) that want the `tslocation`-
stepping semantics directly; it was only ever load-bearing inside this
iterate method.
Behaviorally, this is a minor breaking change for any downstream code that
did `for s in sol` expecting `s` to be a full solution object (previously the
same container with a bumped `tslocation`). Such callers should either
iterate `sol.u` / `sol.t` explicitly, or index with `solution_new_tslocation`.
Bump to 3.4.0 to reflect the iteration semantics change.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
4 tasks
Per review: the cleanest fix is to not define `iterate` on `AbstractTimeseriesSolution` at all. The AbstractArray fallback (iterate over `eachindex(sol)`) is the only iteration that is type-consistent with `eltype(sol)` and `size(sol)` the solution already declares. Any custom iterate just risks re-introducing the same class of bug. No behavior change from the previous commit in practice (both forms delegated to `sol[state]`); this just removes the now-redundant wrapper. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Contributor
Author
|
Updated per review: deleted the custom |
Co-authored-by: Christopher Rackauckas <accounts@chrisrackauckas.com>
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.
Summary
The existing
Base.iterate(sol::AbstractTimeseriesSolution, state = 0)returnssolution_new_tslocation(sol, state)— a fresh solution of the same concrete type with a bumpedtslocation. That violates theAbstractArraycontract (an array's first element must not have type identical to the container) and trips Julia 1.12'sLinearAlgebra.norm_recursive_check:This surfaced in OrdinaryDiffEq's
VectorOfArray/StructArray compatibilitytestsets (OrdinaryDiffEqLowStorageRK,OrdinaryDiffEqSSPRK) wheresol_SA ≈ sol_SVcompares twoODESolutions whoseuisVector{VectorOfArray{Float64, 2, StructVector{SVector{1, Float64}}}}. The genericBase.isapproxforAbstractArrayfalls through toLinearAlgebra.norm, which on 1.12 hits the check.Change
Replace the custom iterate with the natural linear-indexing form:
For
VectorOfArray-backed solutions this yields scalars (matchingeltype(sol)), which is whatLinearAlgebra.normandisapproxexpect.solution_new_tslocationis still exported for the plot recipe to use directly — it was only ever load-bearing inside this method.Breaking behavior note
Minor behavior change: downstream code that did
for s in solexpectingsto be a full solution-like object (with.retcode,.prob, etc.) will need to change. In practice such callers should iteratesol.u/sol.texplicitly, or pairsolution_new_tslocationwitheachindex(sol.u). One such site lives inOrdinaryDiffEq.jl/test/ad/ad_tests.jl:388(for s in solwiths.retcode); that's a test-only call and can be fixed in-place to readsol.retcodeonce.Bumped to 3.4.0 to flag the iteration semantics change.
Test plan
@testset "iterate does not yield the container"intest/solution_interface.jlwith: type check, non-equality check,isfinite(norm(sol)),sol ≈ sol. All pass on Julia 1.12.VectorOfArray/StructArray compatibilitytestsets inOrdinaryDiffEqLowStorageRKandOrdinaryDiffEqSSPRK(verified locally against monorepo-dev'd sublibs).Closes-replaces-workaround-for: SciML/OrdinaryDiffEq.jl#3513
🤖 Generated with Claude Code