Skip to content

Fix SplitFunction _func_cache aliasing u0 - #1520

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:splitfunction-cache-aliasing
Aug 26, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:splitfunction-cache-aliasing

Conversation

@singhharsh1708

Copy link
Copy Markdown
Member

Fixes #1519.

SplitODEProblem and the bare ODEProblem(::SplitFunction, ...) path build the in-place scratch cache as DiffCache(u0), and DiffCache keeps the passed array as its primary buffer, so any direct combined call f(du, u, p, t) evaluates f1 into prob.u0's memory. With u === u0 the result itself is wrong (f2 reads the state f1 just overwrote); with any other u the result is right but the stored initial condition is silently replaced. Details and history in #1519.

The change is DiffCache(copy(u0)) at both construction sites, plus copy(u0) in the non-number-array fallback, which was handing out u0 itself as the buffer. Only the buffer's identity changes, so the dual-cache path that #1106 added for OrdinaryDiffEq#2719 is untouched.

Test added to problem_building_test.jl: the cache must not alias u0, the combined call at u0 returns f1 + f2 correctly, and u0 is bit-identical after combined calls at u0 and at a different state, for both construction paths. The new test fails on master (du == [4.0, 8.0] instead of [3.0, 6.0], u0 mutated) and passes with the fix. Also re-verified the #2719 downstream case (Rodas5P on a split problem, ForwardDiff Jacobian through the cache) and solve-path equivalence against the joined problem.

AI Disclosure

Claude assisted with this work.

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.

Calling a SplitFunction directly corrupts prob.u0 (and its own result) through the aliased _func_cache

2 participants