Repository navigation
[bot] Solve implicit affects at the ODE solver's tolerance - #5080
Conversation
`(ia::ImplicitAffect)(integ)` solved the affect with `init(affprob, IDSolve())`, i.e. at NonlinearSolve's default Float64 `abstol = 3e-13`, unrelated to the tolerance the ODE is being integrated at. `AffectSystem` appends every `alg_equations(sys)` and `observed(sys)` to the affect equations, so any DAE whose residuals cannot reach `3e-13` in floating point -- the floor is `eps * magnitude` -- threw `UnsolvableCallbackError` at its first event even though the callback is solvable to the accuracy actually requested. Pass the triggering integrator's tolerances into the affect solve. Honouring them needs ImplicitDiscreteSolve 2.3, which forwards the tolerances `init` is given into its nonlinear solve. Co-Authored-By: Chris Rackauckas <[email protected]> Agent-Harness: Claude Code 2.1.260 Agent-Model: claude-opus-5[1m] Agent-Session: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD
CI status on the first push, read rather than assumed: 39 jobs red, all from the same cause — i.e. the 🤖 Posted by an AI agent — harness: Claude Code 2.1.260 · model: claude-opus-5[1m] |
Drop the two `hasproperty` guards: every SciMLBase integrator carries `.opts`
with `abstol`/`reltol`, and there is no official accessor to prefer over
`getproperty`, so the guards defended against an integrator nobody has produced.
The two remaining branches stay, and the docstring now says why, with the
citations checked against the installed versions:
- `false` is what `OrdinaryDiffEqCore` stores for a tolerance not supplied under
a discrete problem (`solve.jl:377-378`, `:389-390`, v4.16.0); `Bool <: Number`,
so forwarding it reaches `NonlinearSolveBase.get_tolerance` as a zero
tolerance. Measured: `init(prob, IDSolve(); abstol = 0.0)` on a solve whose
residual floors at eps returns `InitialFailure`, `abstol = false` succeeds.
- The nonlinear solve compares a scalar norm against the tolerance
(`NonlinearSolveBase/src/termination_conditions.jl:246-248`, v2.49.3), and the
ODE integrator's per-component array is sized for the parent system's
unknowns, not the affect's, so it cannot be passed through. `minimum` is a
deliberate choice: never solve the affect looser than any part of the
integration.
The new testset covers both branches directly; with them removed it fails 4 of 5.
Co-Authored-By: Chris Rackauckas <[email protected]>
Agent-Harness: Claude Code 2.1.260
Agent-Model: claude-opus-5[1m]
Agent-Session: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD
Pushed 2db9289 in response to review. Dropped: both function affect_tolerance(integ, name::Symbol)
tol = getproperty(integ.opts, name)
tol isa Bool && return nothing
tol isa AbstractArray && return isempty(tol) ? nothing : minimum(tol)
return tol
endKept, with the reasons now in the docstring and checked rather than assumed:
One correction to the record, since it changes how load-bearing this branch is: (Attempting the end-to-end discrete construction also runs into something unrelated: a Array — the nonlinear solve compares a scalar norm against the tolerance ( Tests — 0 failures, 0 errors; the 2 broken are pre-existing. Runic clean on both changed files. Nothing else in the PR was touched. 🤖 Posted by an AI agent — harness: Claude Code 2.1.260 · model: claude-opus-5[1m] |
- 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]>
…ces (#5106) `affect_tolerance` reads `integ.opts.abstol`/`.reltol` directly. #5080 dropped the `hasproperty` guards on the premise that "every SciMLBase integrator carries `.opts` with `abstol`/`reltol`", but `JumpProcesses`' `SSAIntegrator` stores `opts = (callback = CallbackSet(callback),)` (`SSA_stepper.jl:236`), a plain `NamedTuple` with no tolerances. Any `ImplicitAffect` fired from an SSA solve then threw `FieldError: type NamedTuple has no field 'abstol'`, which is the MTKBase `InterfaceII` failure on master (`jumpsystem.jl:109`). Return `nothing` — the nonlinear solver's own default, i.e. the pre-#5080 behaviour — when the tolerance is absent, leaving #5080's intent intact for every integrator that does carry tolerances. Agent-Harness: Claude Code 2.1.267 Agent-Model: claude-opus-5[1m] Agent-Session: https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn Claude-Session: https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Chris Rackauckas <[email protected]>
What and why
(ia::ImplicitAffect)(integ)solves the affect withinit(affprob, IDSolve()), i.e. atNonlinearSolve's default Float64
abstol = 3e-13, unrelated to the tolerance the ODE is beingintegrated at. Since
AffectSystemappends everyalg_equations(sys)andobserved(sys)to theaffect equations, any DAE whose residuals cannot reach
3e-13in floating point — the floor iseps * magnitude, so anything with terms ≳ 1e3 — throwsUnsolvableCallbackErrorat its firstevent even though the callback is solvable to the accuracy actually requested. Hit on a battery
model with O(1e4) concentrations at the first switch of an ordinary charge protocol.
This passes the triggering integrator's tolerances into the affect solve. Per @ChrisRackauckas:
"It should go to the tolerance of the ODE solver."
affect_toleranceis defensive becauseintegis not always an OrdinaryDiffEq integrator: noopts, or no such field onopts, givesnothing, which is exactly the current behaviour(NonlinearSolve's default). A per-component tolerance array is reduced to its tightest entry —
it is sized for the parent system's unknowns, not the affect's.
check_error(affsol) === ReturnCode.InitialFailureis left alone: with the tolerance fixed, afailure there is a real failure, and the existing "impossible affect" test in
symbolic_events.jl(
x ~ Pre(x) + 2againstx^2 + y^2 ~ 1) still throws, at any tolerance.Blocked on a release: the tolerance is only honoured by
ImplicitDiscreteSolve≥ 2.3.0(SciML/OrdinaryDiffEq.jl#4468 —
IDSolvecurrently drops the tolerancesinitwas given). The[compat]bound here is bumped to2.3, so this cannot resolve until thatis registered.
Verification
Issue with the MWE: #5079. Run on aarch64
Linux, Julia 1.10.12, with
lib/ModelingToolkitBaseand the ImplicitDiscreteSolve branchdevedinto a scratch environment.
Before (
git stash push lib/ModelingToolkitBase/src/systems/callbacks.jl, new testset runstandalone, ImplicitDiscreteSolve fix already in place — so this isolates the MTK half):
After (same testset):
Whole file (
symbolic_events.jl, the wayInterfaceIruns it —using ModelingToolkit; import ModelingToolkitBase; include(...)):0 failures, 0 errors; the 2 broken are pre-existing. In particular the neighbouring
Implicit affects with Pretestset (11/11), which includes the genuinely unsolvable affectx ~ Pre(x) + 2againstx^2 + y^2 ~ 1, still passes — a real failure is still reported as one.End to end, the issue's MWE now prints
solve returned Success, g = [0.4785, 0.5785]instead ofthrowing, and the state saved right after the affect satisfies the algebraic equation with
|residual| = 3.2e-9(itsFloat64floor is 1.2e-9), against the981the surroundingintegration leaves on this deliberately badly scaled equation.
Formatting: Runic clean on both changed files.
typosis not installed on this machine, so thespell check was not run locally.
What I did not verify
symbolic_events.jlwas run, not the wholeInterfaceIgroup (too large for the box).🤖 Posted by an AI agent — harness: Claude Code 2.1.260 · model: claude-opus-5[1m]
Conversation: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD
🤖 Generated with Claude Code
https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD