Skip to content

Fix recursivecopy! for mixed nested VectorOfArray#542

Merged
ChrisRackauckas merged 2 commits intoSciML:masterfrom
JoshuaLampert:fix-recursivecopy
Mar 6, 2026
Merged

Fix recursivecopy! for mixed nested VectorOfArray#542
ChrisRackauckas merged 2 commits intoSciML:masterfrom
JoshuaLampert:fix-recursivecopy

Conversation

@JoshuaLampert
Copy link
Contributor

@JoshuaLampert JoshuaLampert commented Mar 5, 2026

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

For VectorOfArrays there was an aliasing issue for mixed nested VectorOfArray. This MWE

using RecursiveArrayTools
using OrdinaryDiffEqSSPRK

function rhs!(du, u, p, t)
    du .= u
    return nothing
end

n = 2
left = ones(n)
right = left

u_flat = VectorOfArray([copy(left), copy(right)])
u_nested = VectorOfArray([copy(left),
                          VectorOfArray([right[e] for e in 1:n])])

tspan = (0.0, 1.0)
prob_flat = ODEProblem(rhs!, u_flat, tspan)
prob_nested = ODEProblem(rhs!, u_nested, tspan)

solver = SSPRK53()
kwargs = (; dt = 0.1, adaptive = false, save_everystep = false)

sol_flat = solve(prob_flat, solver; kwargs...)
sol_nested = solve(prob_nested, solver; kwargs...)

previously returned

julia> sol_nested.u[end]
VectorOfArray{Float64,2}:
2-element Vector{Union{VectorOfArray{Float64, 1, Vector{Float64}}, Vector{Float64}}}:
 [3.6438404308186327, 3.6438404308186327]
 VectorOfArray{Float64, 1, Vector{Float64}}([2.7182551719053256, 2.7182551719053256])

while with this fix this is correct:

julia> sol_nested.u[end]
VectorOfArray{Float64,2}:
2-element Vector{Union{VectorOfArray{Float64, 1, Vector{Float64}}, Vector{Float64}}}:
 [2.7182551719053256, 2.7182551719053256]
 VectorOfArray{Float64, 1, Vector{Float64}}([2.7182551719053256, 2.7182551719053256])

For transparency, I used AI tools to help me come up with this solution.

@ChrisRackauckas ChrisRackauckas merged commit 428b1be into SciML:master Mar 6, 2026
21 of 27 checks passed
@JoshuaLampert JoshuaLampert deleted the fix-recursivecopy branch March 6, 2026 12:03
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