Skip to content

Preserve u0 === nothing for solvers that handle it via allows_null_u0 - #3433

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:cr/allows-null-u0-trait
Apr 15, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:cr/allows-null-u0-trait

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Motivation

This repro previously crashed inside NonlinearSolveBase.construct_jacobian_cache trying to build a 0x1 Jacobian:

fn = ImplicitDiscreteFunction(; resid_prototype = [0.0]) do du, unext, u, p, t
    du[1] = 1
end
pr = ImplicitDiscreteProblem(fn, nothing, (0, 1))
init(pr, IDSolve())

IDSolve's cache.jl already has alg_cache(alg::IDSolve, ::Nothing, ...) that returns IDSolveCache(nothing, nothing, nothing, nothing, ...) with perform_step! as a no-op success. That dispatch is the right thing for a discrete problem with no state — there is nothing to nonlinear-solve. But it was unreachable: the blanket coercion in solve.jl introduced in d20c351 turned nothing into Float64[] before alg_cache ran, routing into the generic-vector branch which built a NonlinearLeastSquaresProblem over an empty u.

Rather than unwind the coercion (which would regress the MTK case — Tsit5/Rosenbrock23/FBDF caches can't directly ingest nothing), this introduces a trait so solvers that can handle the richer nothing signal opt in.

Design notes

  • allows_null_u0(alg) = false default in OrdinaryDiffEqCore/src/alg_utils.jl.
  • allows_null_u0(::IDSolve) = true override.
  • Gate: if u === nothing && !allows_null_u0(_alg); u = Float64[]; end.
  • @code_warntype confirms the branch is compile-time constant-folded in both directions:
    • Tsit5() + nothing → Body::Vector{Float64} (coercion branch taken as const).
    • IDSolve() + nothing → Body::Nothing (coercion branch is dead code).
    • Any alg + non-nothing u → the whole block is dead code.

So there is no runtime branch cost and no type instability leaking into the integrator struct.

Test plan

  • New testset Null u0 bypasses NonlinearSolve in lib/ImplicitDiscreteSolve/test/runtests.jl covering the failing repro — asserts integ.cache.u === nothing, integ.cache.nlcache === nothing, and successful_retcode(sol).
  • Local: ODEDIFFEQ_TEST_GROUP=Core Pkg.test("ImplicitDiscreteSolve") — all functional tests pass including the new one.
  • Local smoke: Tsit5 / Rosenbrock23 / FBDF on ODEProblem(f, nothing, tspan) + PeriodicCallback still succeed, callback triggers expected count (the Release DelayDiffEq 6.1.1 and OrdinaryDiffEqDefault 2.4.2 with correct OrdinaryDiffEqCore floors #4078 regression gate).
  • CI green across the full matrix.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Co-Authored-By: Chris Rackauckas [email protected]

ChrisRackauckas and others added 2 commits April 14, 2026 09:29
…_u0`

`__init` coerces `u0 === nothing` to `Float64[]` so generic caches
(`zero(u)`/`similar(u)`/broadcasts) keep working for MTK systems that
simplify to only callbacks. IDSolve has a purpose-built `::Nothing`
`alg_cache` dispatch that bypasses NonlinearSolve entirely — but it was
unreachable because the coercion ran first, and the fallback then tried
to build a 0x1 Jacobian in the NLLS branch and crashed.

Gate the coercion behind a new trait `allows_null_u0(alg) = false`.
IDSolve overrides it to `true`, so `nothing` reaches its cache
constructor unchanged and the no-op step path is used. All other
solvers keep the existing coercion behavior, so the MTK issue SciML#4078
case still works.

Repro that previously errored in `NonlinearSolveBase.construct_jacobian_cache`:

    fn = ImplicitDiscreteFunction(; resid_prototype = [0.0]) do du, unext, u, p, t
        du[1] = 1
    end
    pr = ImplicitDiscreteProblem(fn, nothing, (0, 1))
    init(pr, IDSolve())

The trait branch is compile-time constant-folded per `@code_warntype`,
so there is no runtime branch cost or type instability in the integrator
struct.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
Previous run hit infrastructure-level failures (missing julia binary on
several self-hosted runners, pkg.julialang.org HTTP/2 download errors,
missing gh CLI on benchmark runner) plus a known-flaky SplitODE resize
test that passes locally.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas merged commit 62b4d67 into SciML:master Apr 15, 2026
213 of 226 checks passed
ChrisRackauckas added a commit that referenced this pull request Apr 16, 2026
Source changes since 1.10.0 registration:
- allows_null_u0 trait to preserve u0 === nothing (#3433)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Apr 16, 2026
Source changes since 1.10.0 registration:
- allows_null_u0 trait to preserve u0 === nothing (#3433)

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Apr 16, 2026
ImplicitDiscreteSolve v1.11.0 imports `allows_null_u0` from
OrdinaryDiffEqCore, which was only introduced in v3.31.0 (PR SciML#3433).
The previous lower bound of 3.4 allowed resolving versions that don't
export this trait, causing an UndefVarError at load time.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Apr 16, 2026
)

ImplicitDiscreteSolve v1.11.0 imports `allows_null_u0` from
OrdinaryDiffEqCore, which was only introduced in v3.31.0 (PR #3433).
The previous lower bound of 3.4 allowed resolving versions that don't
export this trait, causing an UndefVarError at load time.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/General that referenced this pull request Apr 16, 2026
….11.0

ImplicitDiscreteSolve v1.11.0 imports `allows_null_u0` from
OrdinaryDiffEqCore, which was only introduced in v3.31.0
(SciML/OrdinaryDiffEq.jl#3433). The previous lower bound of 3.4.0
allowed resolving versions that don't export this trait, causing
UndefVarError at load time.

This tightens the OrdinaryDiffEqCore compat for v1.11.0 from
"3.4.0 - 3" to "3.31.0 - 3", while leaving prior versions unchanged.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
DilumAluthge pushed a commit to JuliaRegistries/General that referenced this pull request Apr 16, 2026
…#153130)

[ImplicitDiscreteSolve] Tighten OrdinaryDiffEqCore lower bound for v1.11.0

ImplicitDiscreteSolve v1.11.0 imports `allows_null_u0` from
OrdinaryDiffEqCore, which was only introduced in v3.31.0
(SciML/OrdinaryDiffEq.jl#3433). The previous lower bound of 3.4.0
allowed resolving versions that don't export this trait, causing
UndefVarError at load time.

This tightens the OrdinaryDiffEqCore compat for v1.11.0 from
"3.4.0 - 3" to "3.31.0 - 3", while leaving prior versions unchanged.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
singhharsh1708 pushed a commit to singhharsh1708/OrdinaryDiffEq.jl that referenced this pull request Apr 16, 2026
…SciML#3433)

* Preserve `u0 === nothing` for solvers that handle it via `allows_null_u0`

`__init` coerces `u0 === nothing` to `Float64[]` so generic caches
(`zero(u)`/`similar(u)`/broadcasts) keep working for MTK systems that
simplify to only callbacks. IDSolve has a purpose-built `::Nothing`
`alg_cache` dispatch that bypasses NonlinearSolve entirely — but it was
unreachable because the coercion ran first, and the fallback then tried
to build a 0x1 Jacobian in the NLLS branch and crashed.

Gate the coercion behind a new trait `allows_null_u0(alg) = false`.
IDSolve overrides it to `true`, so `nothing` reaches its cache
constructor unchanged and the no-op step path is used. All other
solvers keep the existing coercion behavior, so the MTK issue SciML#4078
case still works.

Repro that previously errored in `NonlinearSolveBase.construct_jacobian_cache`:

    fn = ImplicitDiscreteFunction(; resid_prototype = [0.0]) do du, unext, u, p, t
        du[1] = 1
    end
    pr = ImplicitDiscreteProblem(fn, nothing, (0, 1))
    init(pr, IDSolve())

The trait branch is compile-time constant-folded per `@code_warntype`,
so there is no runtime branch cost or type instability in the integrator
struct.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>

* Retrigger CI

Previous run hit infrastructure-level failures (missing julia binary on
several self-hosted runners, pkg.julialang.org HTTP/2 download errors,
missing gh CLI on benchmark runner) plus a known-flaky SplitODE resize
test that passes locally.

Co-Authored-By: Chris Rackauckas <[email protected]>

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
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