Skip to content

Commit 6a304a4

Browse files
Handle empty u in construct_jacobian_cache
When u has length 0 (e.g. from u0=nothing converted to Float64[] by OrdinaryDiffEqCore), return an empty Jacobian immediately instead of passing the empty vector to ForwardDiff/DifferentiationInterface which errors with "chunk size cannot be greater than structural_length(x)". This fixes ImplicitDiscreteSolve's u0=nothing support for MTK systems with only callbacks and no state variables. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b0902e3 commit 6a304a4

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

lib/NonlinearSolveBase/src/jacobian.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ function construct_jacobian_cache(
3535
autodiff = nothing, vjp_autodiff = nothing, jvp_autodiff = nothing,
3636
linsolve = missing
3737
)
38+
# Empty state vector (e.g. u0=nothing converted to Float64[]) — nothing to differentiate
39+
if length(u) == 0
40+
J = Utils.safe_similar(fu, promote_type(eltype(fu), eltype(u)), length(fu), 0)
41+
return JacobianCache(J, f, fu, p, stats, autodiff, nothing)
42+
end
43+
3844
has_analytic_jac = SciMLBase.has_jac(f)
3945
linsolve_needs_jac = !concrete_jac(alg) && (
4046
linsolve === missing ||

0 commit comments

Comments
 (0)