Skip to content

Add analytical parameter jacobian code generation - #5083

Merged
ChrisRackauckas merged 3 commits into
SciML:masterfrom
AJ0070:paramjac-codegen
Sep 7, 2026
Merged

ChrisRackauckas merged 3 commits into
SciML:masterfrom
AJ0070:paramjac-codegen

Conversation

@AJ0070

@AJ0070 AJ0070 commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

MTK can already generate analytical jacobians with respect to unknowns (jac = true) and inputs (calculate_control_jacobian), but nothing with respect to parameters. SciMLBase already defines a paramjac field on ODEFunction, so this just populates it.

Adds calculate_paramjac and generate_paramjac, modelled on the existing control jacobian, plus a paramjac = false keyword on ODEFunction.

Column j of pJ is the derivative with respect to entry j of SciMLStructures.canonicalize(SciMLStructures.Tunable(), p)[1], which is what SciMLSensitivity consumes. tunable_parameters is not usable as the column basis despite its docstring: it is a metadata filter, so it also returns Initial(...), ::Int, FnType and discrete parameters. The columns come from reorder_parameters(sys, ps; flatten = false)[1], the same call complete uses to lay out sys.ps.

Notes:

  • paramjac defaults to false, so nothing changes unless you opt in.
  • sparse works on calculate_paramjac/generate_paramjac but is not forwarded from ODEFunction, since SciMLBase has no paramjac_prototype.
  • A system with no tunable parameters throws instead of returning an empty function, because dense out of place codegen of an n x 0 array fails inside SymbolicUtils.

Tests cover the column ordering, agreement with central differences, sparse and dense, in place and out of place, time independent and non split systems, and the ODEFunction wiring. Adjoint gradients through SciMLSensitivity match ForwardDiff to 3e-11.

AI Disclosure: Used Opus 5

if sparse
return sparsejacobian(rhs, ps; simplify)
else
return jacobian(rhs, ps; simplify)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really all there is to it?

@AJ0070 AJ0070 Sep 7, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly yes: full_equations already inlines observed and torn equations, and check_lhs guarantees every lhs is 0 or D(x), so eq.rhs is the f the solver integrates. Mass matrices are constant, so there is no dM/dp term.

Two things it did miss, both now fixed: a delay parameter got a silently zero column, since Symbolics.jacobian treats x(t - tau) as opaque, so calculate_paramjac throws for DDEs; and the sparse in-place function filled a mismatched buffer in the wrong cells, so it now asserts the pattern like generate_jacobian does.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup the symbolic machinery takes care of most of it.

@ChrisRackauckas
ChrisRackauckas merged commit 054f62f into SciML:master Sep 7, 2026
107 of 109 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
- Add analytical parameter jacobian code generation (#5083)
- Document observed access and plotting from a live integrator (#5081)
- [bot] Solve implicit affects at the ODE solver's tolerance (#5080)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (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.

3 participants