ODENLStepData: forward outer ODEFunction's flag to inner nlprob#4609
Open
singhharsh1708 wants to merge 1 commit into
Open
ODENLStepData: forward outer ODEFunction's flag to inner nlprob#4609singhharsh1708 wants to merge 1 commit into
singhharsh1708 wants to merge 1 commit into
Conversation
Author
|
@ChrisRackauckas — small MTK side of the W-reuse story. Threads the existing jac kwarg through generate_ODENLStepData so the inner teared NonlinearProblem gets MTK's symbolic analytic Jacobian when the user writes ODEProblem(...; jac = true, nlstep = true). Prereq for SciML/OrdinaryDiffEq.jl#3696 closure. |
ChrisRackauckas
approved these changes
Jun 10, 2026
Author
|
@ChrisRackauckas — friendly bump on this one. Re-verified: CI failures match master's pre-existing red state (same Runic failures on the same files — |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4570.
When a user writes ODEProblem(sys, ...; jac = true, nlstep = true), the outer ODEFunction gets an analytic Jacobian but the inner teared NonlinearProblem built by generate_ODENLStepData did not — its f.jac was nothing.
This threads the jac kwarg through generate_ODENLStepData and into the inner NonlinearProblem / SCCNonlinearProblem constructor. With jac = true, MTK now symbolically differentiates the teared inner system once at build time and attaches the result to nlprob.f.jac.
Repro
using ModelingToolkit, SciMLBase
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters k₁ k₂ k₃
@variables y₁(t) y₂(t) y₃(t)
@mtkcompile rober = System([
D(y₁) ~ -k₁y₁ + k₃y₂y₃,
D(y₂) ~ k₁y₁ - k₂y₂^2 - k₃y₂y₃,
D(y₃) ~ k₂y₂^2,
], t)
prob = ODEProblem(rober,
[[y₁,y₂,y₃] .=> [1.0,0.0,0.0]; [k₁,k₂,k₃] .=> (0.04, 3e7, 1e4)],
(0.0, 1e5); jac = true, nlstep = true)
SciMLBase.has_jac(prob.f.nlstep_data.nlprob.f) # false on master, true with this PR
Verified locally
nlprob.f.jac populated when jac = true, untouched (nothing) when jac = false
The attached function is a real MTK GeneratedFunctionWrapper
Solutions still converge with the same retcode and within tolerance of the master result
Motivation
This is a prerequisite for OrdinaryDiffEq.jl-side W-matrix reuse on the nlstep path (SciML/OrdinaryDiffEq.jl#3696). Standalone performance impact is small/mixed (the inner teared problem for benchmark-scale Robertson is 1D, so FD jac is already nearly free); the real wins are downstream when paired with #3696 and on larger MTK systems where the teared inner Jacobian is non-trivial.
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Add any other context about the problem here.