Skip to content

Commit d20c351

Browse files
Support null u0 with callbacks for all solvers
When `u0 === nothing`, convert u to an empty Float64 array to allow initialization to proceed. This enables problems with no state variables but with callbacks to be solved with any solver. Changes: 1. OrdinaryDiffEqCore/src/solve.jl: Convert null u0 to Float64[] 2. OrdinaryDiffEqDifferentiation/src/derivative_wrappers.jl: Skip Jacobian computation for empty state vectors This fixes the issue where MTK systems that simplify to having no differential equations (resulting in `u0 === nothing`) but have `SymbolicDiscreteCallback` could not trigger callbacks. Tested with explicit (FunctionMap, Tsit5, Euler, RK4) and implicit (Rosenbrock23, Rodas5P, TRBDF2, KenCarp4, ImplicitEuler, QNDF, FBDF) solvers. References: SciML/ModelingToolkit.jl#4078 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 7ce26b5 commit d20c351

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/OrdinaryDiffEqCore/src/solve.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ function SciMLBase.__init(
212212
u = recursivecopy(prob.u0)
213213
end
214214

215+
# Handle null u0 (e.g., MTK systems with only callbacks and no state variables)
216+
# Convert to empty Float64 array to allow initialization to proceed
217+
if u === nothing
218+
u = Float64[]
219+
end
220+
215221
if _alg isa DAEAlgorithm
216222
if !isnothing(aliases.alias_du0) && aliases.alias_du0
217223
du = prob.du0

lib/OrdinaryDiffEqDifferentiation/src/derivative_wrappers.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ end
186186
function jacobian!(J::AbstractMatrix{<:Number}, f::F, x::AbstractArray{<:Number},
187187
fx::AbstractArray{<:Number}, integrator::SciMLBase.DEIntegrator,
188188
jac_config) where F
189+
# Handle empty state vector - nothing to compute
190+
if isempty(x)
191+
return nothing
192+
end
193+
189194
alg = unwrap_alg(integrator, true)
190195

191196
dense = ADTypes.dense_ad(alg_autodiff(alg))

0 commit comments

Comments
 (0)