From 18f6039b84e636300f97c612455402bdc5c792f1 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sat, 3 Oct 2026 04:31:15 -0400 Subject: [PATCH 1/3] Make AN5 and JVODE caches resizable and restart Nordsieck history at order 1. AN5Cache and JVODECache had no full_cache, so resize!(integrator, n) threw MethodError. Nested z/Tsit5 arrays also need resizing, and the history is invalid after a size change. Co-Authored-By: Chris Rackauckas Co-Authored-By: Cursor Agent Agent-Harness: Cursor Agent 2026.10.01-14929f9 Agent-Model: unknown (Cursor auto) Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/pr-nordsieck-resize/log.txt on amdci2 --- .../src/OrdinaryDiffEqNordsieck.jl | 5 +- .../src/nordsieck_caches.jl | 50 +++++++++++++++--- .../src/nordsieck_perform_step.jl | 6 +++ .../test/nordsieck_tests.jl | 51 +++++++++++++++++++ 4 files changed, 102 insertions(+), 10 deletions(-) diff --git a/lib/OrdinaryDiffEqNordsieck/src/OrdinaryDiffEqNordsieck.jl b/lib/OrdinaryDiffEqNordsieck/src/OrdinaryDiffEqNordsieck.jl index ce618452407..70ed466b9b9 100644 --- a/lib/OrdinaryDiffEqNordsieck/src/OrdinaryDiffEqNordsieck.jl +++ b/lib/OrdinaryDiffEqNordsieck/src/OrdinaryDiffEqNordsieck.jl @@ -1,7 +1,7 @@ module OrdinaryDiffEqNordsieck # `alg_order` is owned (and public) by SciMLBase; import it from its owner. -import SciMLBase: alg_order +import SciMLBase: alg_order, resize_non_user_cache! # `initialize!`, `calculate_residuals`, `calculate_residuals!` are owned by # DiffEqBase (re-provided through OrdinaryDiffEqCore); import them from the owner # so the ExplicitImports owner check passes. `initialize!` is public in @@ -22,7 +22,8 @@ import OrdinaryDiffEqCore: alg_adaptive_order, CommonControllerOptions, resolve_basic, _resolved_QT, get_current_adaptive_order, get_fsalfirstlast, ode_interpolant, ode_interpolant!, trivial_limiter!, - generic_solver_docstring, default_controller + generic_solver_docstring, default_controller, + @cache, ODEIntegrator, reinit_controller! using MuladdMacro: @muladd using FastBroadcast: @.., Serial using RecursiveArrayTools: recursivefill! diff --git a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl index 4810d384211..c01cca01b9f 100644 --- a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl +++ b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl @@ -36,14 +36,15 @@ function alg_cache( return AN5ConstantCache(z, l, m, c_LTE, c_conv, dts, Δ, tsit5tab, 1) end -mutable struct AN5Cache{uType, dType, rateType, zType, lType, dtsType, tsit5Type} <: - OrdinaryDiffEqMutableCache +@cache mutable struct AN5Cache{ + uType, uNoUnitsType, rateType, zType, lType, dtsType, tsit5Type, + } <: OrdinaryDiffEqMutableCache u::uType uprev::uType tmp::uType - Δ::dType + Δ::uNoUnitsType # Error estimation - atmp::dType + atmp::uNoUnitsType fsalfirst::rateType ratetmp::rateType # `z` is the Nordsieck vector @@ -170,13 +171,13 @@ function alg_cache( ) end -mutable struct JVODECache{ +@cache mutable struct JVODECache{ uType, rateType, zType, lType, dtsType, - dType, + uNoUnitsType, etaType, tsit5Type, } <: OrdinaryDiffEqMutableCache @@ -205,9 +206,9 @@ mutable struct JVODECache{ # `dts` stores `dt`s dts::dtsType # `Δ` is the difference between the predictor `uₙ₀` and `uₙ` - Δ::dType + Δ::uNoUnitsType # Error estimation - atmp::dType + atmp::uNoUnitsType # `Tsit5` for the first step tsit5cache::tsit5Type L::Int @@ -288,3 +289,36 @@ end function get_fsalfirstlast(cache::Union{JVODECache, AN5Cache}, u) return get_fsalfirstlast(cache.tsit5cache, u) end + +function _resize_nordsieck_arrays!(integrator, cache, i) + for zi in cache.z + resize!(zi, i) + recursivefill!(zi, false) + end + tsit5 = cache.tsit5cache + for buf in ( + tsit5.k1, tsit5.k2, tsit5.k3, tsit5.k4, tsit5.k5, tsit5.k6, tsit5.k7, + tsit5.utilde, tsit5.tmp, tsit5.atmp, cache.Δ, cache.ratetmp, cache.fsalfirst, + ) + resize!(buf, i) + end + fill!(cache.l, 0) + fill!(cache.m, 0) + cache.order = 1 + integrator.derivative_discontinuity = true + integrator.reeval_fsal = true + reinit_controller!(integrator, integrator.controller_cache) + return nothing +end + +function resize_non_user_cache!(integrator::ODEIntegrator, cache::AN5Cache, i) + return _resize_nordsieck_arrays!(integrator, cache, i) +end + +function resize_non_user_cache!(integrator::ODEIntegrator, cache::JVODECache, i) + _resize_nordsieck_arrays!(integrator, cache, i) + cache.nextorder = 1 + cache.L = 2 + cache.n_wait = 2 + return nothing +end diff --git a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl index eec38241aad..f15092b514a 100644 --- a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl +++ b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl @@ -23,6 +23,8 @@ end # Nordsieck form needs to build the history vector if cache.order == 1 # Start the Nordsieck vector in one shot! + fill!(l, 0) + fill!(m, 0) perform_step!(integrator, tsit5tab, repeat_step) cache.order = 4 z[1] = integrator.uprev @@ -120,6 +122,10 @@ end # Nordsieck form needs to build the history vector if cache.order == 1 ## Start the Nordsieck vector in two shots! + fill!(l, 0) + fill!(m, 0) + f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) + increment_nf!(integrator.stats, 1) perform_step!(integrator, tsit5cache, repeat_step) copyto!(tmp, integrator.u) cache.order = 4 diff --git a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl index 470f0f901c2..aaa0b046a11 100644 --- a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl +++ b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl @@ -43,3 +43,54 @@ end @test norm(exact - sol.u[end], Inf) < 3.0e-3 end end + +# Coupled linear resize: AN5Cache had no `full_cache`, so `resize!(integrator, n)` +# threw MethodError. History after a resize is invalid, so the method restarts at +# order 1. JVODE is omitted: its `post_newton_controller!` 2-arg/3-arg recursion +# (StackOverflowError) is a separate bug, not this cache-resize path. +const A_resize = [-1.0 0.35 0.1; 0.2 -1.4 0.25; 0.15 0.3 -1.2] +const U0_resize = [1.0, -0.5] +const UNEW_resize = 0.3 +const TG_resize = 0.5 +const TS_resize = 0.65 +const TF_resize = 0.8 + +f_resize!(du, u, p, t) = (mul!(du, view(A_resize, 1:length(u), 1:length(u)), u); nothing) + +function exact_resize(t) + u = exp(A_resize[1:2, 1:2] * min(t, TG_resize)) * U0_resize + t <= TG_resize && return u + u3 = exp(A_resize * (min(t, TS_resize) - TG_resize)) * vcat(u, UNEW_resize) + t <= TS_resize && return u3 + return exp(A_resize[1:2, 1:2] * (t - TS_resize)) * u3[1:2] +end + +@testset "AN5 state resizing" begin + grew = Ref(false) + shrank = Ref(false) + restart_orders = Int[] + function affect!(integrator) + n = grew[] ? 2 : 3 + resize!(integrator, n) + n == 3 && (integrator.u[3] = UNEW_resize) + # New `uprev` slots are uninitialized (#4722); a Nordsieck restart + # reads `uprev` as the step start, so it must match the resized `u`. + copyto!(integrator.uprev, integrator.u) + push!(restart_orders, integrator.cache.order) + grew[] ? (shrank[] = true) : (grew[] = true) + return nothing + end + cb = DiscreteCallback( + (u, t, integrator) -> (!grew[] && t >= TG_resize) || (!shrank[] && t >= TS_resize), + affect!; + save_positions = (false, false) + ) + sol = solve( + ODEProblem(f_resize!, copy(U0_resize), (0.0, TF_resize)), AN5(); + callback = cb, tstops = [TG_resize, TS_resize], reltol = 1.0e-10, abstol = 1.0e-12 + ) + @test SciMLBase.successful_retcode(sol) + @test sol(0.58) ≈ exact_resize(0.58) rtol = 1.0e-7 atol = 1.0e-10 + @test sol.u[end] ≈ exact_resize(TF_resize) rtol = 1.0e-7 atol = 1.0e-10 + @test restart_orders == [1, 1] +end From 139d471a7816cd882c7d83bf3ce5d777f89a501d Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sat, 3 Oct 2026 05:36:22 -0400 Subject: [PATCH 2/3] Sync resized Nordsieck uprev after the caller fills u, not in resize!. Setting reeval_fsal in the resize hook skipped Core's discontinuity sync at iter==0, so startup consumed uninitialized history. Restrict the extra FSAL eval to a resize restart so same-size callbacks are unchanged. Co-Authored-By: Chris Rackauckas Co-Authored-By: Cursor Agent Agent-Harness: Cursor Agent 2026.10.01-14929f9 Agent-Model: unknown (Cursor auto) Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/pr-nordsieck-resize/log.txt on amdci2 --- .../src/nordsieck_caches.jl | 8 ++-- .../src/nordsieck_perform_step.jl | 18 +++++---- .../test/nordsieck_tests.jl | 37 +++++++++++++------ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl index c01cca01b9f..82857a62769 100644 --- a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl +++ b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl @@ -61,6 +61,7 @@ end # `Tsit5` for the first step tsit5cache::tsit5Type order::Int + resize_restart::Bool end function alg_cache( @@ -105,7 +106,7 @@ function alg_cache( return AN5Cache( u, uprev, tmp, Δ, atmp, fsalfirst, ratetmp, z, l, m, c_LTE, c_conv, dts, - tsit5cache, 1 + tsit5cache, 1, false ) end @@ -224,6 +225,7 @@ end η₊₁::etaType η₋₁::etaType maxη::etaType + resize_restart::Bool end function alg_cache( @@ -282,7 +284,7 @@ function alg_cache( u, uprev, tmp, fsalfirst, ratetmp, z, l, m, c_LTE₊₁, c_LTE, c_LTE₋₁, c_conv, c_𝒟, prev_𝒟, - dts, Δ, atmp, tsit5cache, 2, 1, 1, 2, η, η, η, η, η, η + dts, Δ, atmp, tsit5cache, 2, 1, 1, 2, η, η, η, η, η, η, false ) end @@ -305,8 +307,8 @@ function _resize_nordsieck_arrays!(integrator, cache, i) fill!(cache.l, 0) fill!(cache.m, 0) cache.order = 1 + cache.resize_restart = true integrator.derivative_discontinuity = true - integrator.reeval_fsal = true reinit_controller!(integrator, integrator.controller_cache) return nothing end diff --git a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl index f15092b514a..30df600e528 100644 --- a/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl +++ b/lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl @@ -23,8 +23,6 @@ end # Nordsieck form needs to build the history vector if cache.order == 1 # Start the Nordsieck vector in one shot! - fill!(l, 0) - fill!(m, 0) perform_step!(integrator, tsit5tab, repeat_step) cache.order = 4 z[1] = integrator.uprev @@ -122,10 +120,12 @@ end # Nordsieck form needs to build the history vector if cache.order == 1 ## Start the Nordsieck vector in two shots! - fill!(l, 0) - fill!(m, 0) - f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) - increment_nf!(integrator.stats, 1) + if cache.resize_restart + copyto!(integrator.uprev, integrator.u) + f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) + increment_nf!(integrator.stats, 1) + cache.resize_restart = false + end perform_step!(integrator, tsit5cache, repeat_step) copyto!(tmp, integrator.u) cache.order = 4 @@ -288,7 +288,11 @@ end (; t, dt, uprev, u, f, p, uprev2, differential_vars) = integrator (; z, l, m, c_LTE, dts, tmp, ratetmp, atmp, tsit5cache) = cache # handle callbacks, rewind back to order one. - if integrator.derivative_discontinuity || integrator.iter == 1 + if integrator.derivative_discontinuity || integrator.iter == 1 || cache.resize_restart + if cache.resize_restart + copyto!(integrator.uprev, integrator.u) + cache.resize_restart = false + end cache.order = 1 @.. broadcast = false z[1] = integrator.uprev f(z[2], uprev, p, t) diff --git a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl index aaa0b046a11..8f5deae1863 100644 --- a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl +++ b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl @@ -44,10 +44,10 @@ end end end -# Coupled linear resize: AN5Cache had no `full_cache`, so `resize!(integrator, n)` -# threw MethodError. History after a resize is invalid, so the method restarts at -# order 1. JVODE is omitted: its `post_newton_controller!` 2-arg/3-arg recursion -# (StackOverflowError) is a separate bug, not this cache-resize path. +# Coupled linear resize: grow 2→3 and shrink back, restarting Nordsieck +# history at order 1. New `uprev` slots must not be consumed uninitialized +# (#4722); Core copies after an accepted step, and a resize before the first +# step is synchronized on the next restart. const A_resize = [-1.0 0.35 0.1; 0.2 -1.4 0.25; 0.15 0.3 -1.2] const U0_resize = [1.0, -0.5] const UNEW_resize = 0.3 @@ -65,7 +65,11 @@ function exact_resize(t) return exp(A_resize[1:2, 1:2] * (t - TS_resize)) * u3[1:2] end -@testset "AN5 state resizing" begin +resize_cases = ( + (AN5(), 1.0e-10, 1.0e-12, 1.0e-7), + (JVODE_Adams(), 1.0e-4, 1.0e-7, 5.0e-3), +) +@testset "Nordsieck state resizing ($alg)" for (alg, reltol, abstol, check_rtol) in resize_cases grew = Ref(false) shrank = Ref(false) restart_orders = Int[] @@ -73,9 +77,6 @@ end n = grew[] ? 2 : 3 resize!(integrator, n) n == 3 && (integrator.u[3] = UNEW_resize) - # New `uprev` slots are uninitialized (#4722); a Nordsieck restart - # reads `uprev` as the step start, so it must match the resized `u`. - copyto!(integrator.uprev, integrator.u) push!(restart_orders, integrator.cache.order) grew[] ? (shrank[] = true) : (grew[] = true) return nothing @@ -86,11 +87,23 @@ end save_positions = (false, false) ) sol = solve( - ODEProblem(f_resize!, copy(U0_resize), (0.0, TF_resize)), AN5(); - callback = cb, tstops = [TG_resize, TS_resize], reltol = 1.0e-10, abstol = 1.0e-12 + ODEProblem(f_resize!, copy(U0_resize), (0.0, TF_resize)), alg; + callback = cb, tstops = [TG_resize, TS_resize], reltol = reltol, abstol = abstol ) @test SciMLBase.successful_retcode(sol) - @test sol(0.58) ≈ exact_resize(0.58) rtol = 1.0e-7 atol = 1.0e-10 - @test sol.u[end] ≈ exact_resize(TF_resize) rtol = 1.0e-7 atol = 1.0e-10 + @test sol(0.58) ≈ exact_resize(0.58) rtol = check_rtol atol = 1.0e-10 + @test sol.u[end] ≈ exact_resize(TF_resize) rtol = check_rtol atol = 1.0e-10 @test restart_orders == [1, 1] end + +@testset "resize before first step initializes restart state" begin + for alg in (AN5(), JVODE_Adams()) + it = init(ODEProblem(f_resize!, [1.0, -0.5], (0.0, 0.1)), alg; adaptive = false, dt = 1.0e-4) + resize!(it, 3) + it.u[3] = 0.3 + it.uprev[3] = NaN + u_modified!(it, true) + step!(it) + @test all(isfinite, it.u) + end +end From eb80c56f105b8bdf9b5d4a4a6787db8ef16ddc1d Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sun, 18 Oct 2026 04:20:44 -0400 Subject: [PATCH 3/3] Require Core 4.7 and exercise Nordsieck resize restart paths Cover both modification flags and JVODE_BDF in the analytic resize regression, and tighten JVODE solve and reference tolerances. Co-Authored-By: Chris Rackauckas Co-Authored-By: Codex Agent-Harness: Codex CLI 0.163.0 Agent-Model: gpt-6.1-sol Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/pr-nordsieck-resize/log-codex.txt on amdci2 --- lib/OrdinaryDiffEqNordsieck/Project.toml | 2 +- lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/OrdinaryDiffEqNordsieck/Project.toml b/lib/OrdinaryDiffEqNordsieck/Project.toml index 5f504d381ae..e5e6c0656a2 100644 --- a/lib/OrdinaryDiffEqNordsieck/Project.toml +++ b/lib/OrdinaryDiffEqNordsieck/Project.toml @@ -38,7 +38,7 @@ DiffEqDevTools = "3" MuladdMacro = "0.2.4" LinearAlgebra = "1.10" SciMLBase = "3.39" -OrdinaryDiffEqCore = "4.4" +OrdinaryDiffEqCore = "4.7" julia = "1.10" RecursiveArrayTools = "4.2.0" ODEProblemLibrary = "1" diff --git a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl index 484f124aa39..078c84d29ad 100644 --- a/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl +++ b/lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl @@ -112,10 +112,6 @@ end end end -# Coupled linear resize: grow 2→3 and shrink back, restarting Nordsieck -# history at order 1. New `uprev` slots must not be consumed uninitialized -# (#4722); Core copies after an accepted step, and a resize before the first -# step is synchronized on the next restart. const A_resize = [-1.0 0.35 0.1; 0.2 -1.4 0.25; 0.15 0.3 -1.2] const U0_resize = [1.0, -0.5] const UNEW_resize = 0.3 @@ -135,9 +131,11 @@ end resize_cases = ( (AN5(), 1.0e-10, 1.0e-12, 1.0e-7), - (JVODE_Adams(), 1.0e-4, 1.0e-7, 5.0e-3), + (JVODE_Adams(), 1.0e-8, 1.0e-10, 1.0e-6), + (JVODE_BDF(), 1.0e-8, 1.0e-10, 1.0e-6), ) -@testset "Nordsieck state resizing ($alg)" for (alg, reltol, abstol, check_rtol) in resize_cases +@testset "Nordsieck state resizing ($alg, u_modified=$modified)" for + (alg, reltol, abstol, check_rtol) in resize_cases, modified in (true, false) grew = Ref(false) shrank = Ref(false) restart_orders = Int[] @@ -145,6 +143,7 @@ resize_cases = ( n = grew[] ? 2 : 3 resize!(integrator, n) n == 3 && (integrator.u[3] = UNEW_resize) + u_modified!(integrator, modified) push!(restart_orders, integrator.cache.order) grew[] ? (shrank[] = true) : (grew[] = true) return nothing @@ -165,7 +164,7 @@ resize_cases = ( end @testset "resize before first step initializes restart state" begin - for alg in (AN5(), JVODE_Adams()) + for alg in (AN5(), JVODE_Adams(), JVODE_BDF()) it = init(ODEProblem(f_resize!, [1.0, -0.5], (0.0, 0.1)), alg; adaptive = false, dt = 1.0e-4) resize!(it, 3) it.u[3] = 0.3