Skip to content

[bot] Solve implicit affects at the ODE solver's tolerance - #5080

Merged
ChrisRackauckas merged 2 commits into
masterfrom
affect-solve-tolerances
Sep 7, 2026
Merged

ChrisRackauckas merged 2 commits into
masterfrom
affect-solve-tolerances

Conversation

@ChrisRackauckas

Copy link
Copy Markdown
Member

🚧 Filed by an AI agent running as @ChrisRackauckas — Chris has not reviewed this PR. Please ignore it until reviewed by @ChrisRackauckas.
Harness: Claude Code 2.1.260 · Model: claude-opus-5[1m]
Conversation: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD

What and why

(ia::ImplicitAffect)(integ) solves 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. Since AffectSystem appends every alg_equations(sys) and observed(sys) to the
affect equations, any DAE whose residuals cannot reach 3e-13 in floating point — the floor is
eps * magnitude, so anything with terms ≳ 1e3 — throws UnsolvableCallbackError at its first
event 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_tolerance is defensive because integ is not always an OrdinaryDiffEq integrator: no
opts, or no such field on opts, gives nothing, 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.InitialFailure is left alone: with the tolerance fixed, a
failure there is a real failure, and the existing "impossible affect" test in symbolic_events.jl
(x ~ Pre(x) + 2 against x^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 — IDSolve currently drops the tolerances
init was given). The [compat] bound here is bumped to 2.3, so this cannot resolve until that
is registered.

Verification

Issue with the MWE: #5079. Run on aarch64
Linux, Julia 1.10.12, with lib/ModelingToolkitBase and the ImplicitDiscreteSolve branch deved
into a scratch environment.

Before (git stash push lib/ModelingToolkitBase/src/systems/callbacks.jl, new testset run
standalone, ImplicitDiscreteSolve fix already in place — so this isolates the MTK half):

Implicit affect solves at the ODE solver's tolerance: Error During Test at mini_test.jl:7
  Got exception outside of a @test
  The callback defined by the following equations:

  0 ~ -g(t) + 1.0e7(-x(t) + y(t)^2)
  x(t) ~ Pre(x(t))
  w(t) ~ Pre(w(t))
  g(t) ~ 0.1 + Pre(g(t))

  is not solvable. Please check that the algebraic equations and affect equations are correct, ...

Test Summary:                                        | Error  Total   Time
Implicit affect solves at the ODE solver's tolerance |     1      1  44.3s

After (same testset):

Test Summary:                                        | Pass  Total   Time
Implicit affect solves at the ODE solver's tolerance |    3      3  51.4s

Whole file (symbolic_events.jl, the way InterfaceI runs it — using ModelingToolkit; import ModelingToolkitBase; include(...)):

Test Summary:       | Pass  Broken  Total      Time
Symbolic Event Test |  382       2    384  15m49.7s

0 failures, 0 errors; the 2 broken are pre-existing. In particular the neighbouring
Implicit affects with Pre testset (11/11), which includes the genuinely unsolvable affect
x ~ Pre(x) + 2 against x^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 of
throwing, and the state saved right after the affect satisfies the algebraic equation with
|residual| = 3.2e-9 (its Float64 floor is 1.2e-9), against the 981 the surrounding
integration leaves on this deliberately badly scaled equation.

Formatting: Runic clean on both changed files. typos is not installed on this machine, so the
spell check was not run locally.

What I did not verify

  • Only symbolic_events.jl was run, not the whole InterfaceI group (too large for the box).
  • aarch64 Linux (Jetson AGX Orin), Julia 1.10.12 only. Not run on x86 or 1.12.
  • The downstream battery model that motivated this was not re-run against this branch.

🤖 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

`(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
@ChrisRackauckas

Copy link
Copy Markdown
Member Author

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

CI status on the first push, read rather than assumed: 39 jobs red, all from the same cause —

ERROR: LoadError: Unsatisfiable requirements detected for package ImplicitDiscreteSolve [3263718b]

i.e. the ImplicitDiscreteSolve = "2.3" bound in lib/ModelingToolkitBase/Project.toml, which is
not registered yet. It becomes resolvable once SciML/OrdinaryDiffEq.jl#4468
merges and 2.3.0 is tagged; nothing else can be learned from this run. Runic, runic-suggestions and
the spell check all pass.


🤖 Posted by an AI agent — harness: Claude Code 2.1.260 · model: claude-opus-5[1m]
Conversation: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD

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
@ChrisRackauckas

Copy link
Copy Markdown
Member Author

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

Pushed 2db9289 in response to review.

Dropped: both hasproperty guards. Every SciMLBase integrator has .opts with abstol/reltol, there is no official accessor to prefer over getproperty, and there is no integrator in hand that needed them. affect_tolerance is now four lines:

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
end

Kept, with the reasons now in the docstring and checked rather than assumed:

Bool — OrdinaryDiffEqCore/src/solve.jl:377-378 and :389-390 (v4.16.0) store false for a tolerance not supplied under a discrete problem. Bool <: Number, and NonlinearSolveBase.get_tolerance(u, false, Float64) returns 0.0 (measured), i.e. a zero tolerance. Measured end to end on a solve whose residual floors at eps:

abstol = nothing  -> Success          u = 1.4142135623730951  resid = 4.44e-16
abstol = false    -> Success          u = 1.4142135623730951  resid = 4.44e-16
abstol = 0.0      -> InitialFailure   u = 1.4                 resid = -0.04
abstol = 3.0e-13  -> Success          u = 1.4142135623730951  resid = 4.44e-16

One correction to the record, since it changes how load-bearing this branch is: false reaching IDSolve is already mapped back to the default by SciML/OrdinaryDiffEq.jl#4468 (nlsolve_tolerance(::Bool) = nothing) — that is why the false row above passes. Since the affect solve always goes through IDSolve and this PR's compat floor is ImplicitDiscreteSolve = "2.3", the branch here is the outer of two guards, not the one doing the work, and no end-to-end test can discriminate it — an affect firing under a discrete-problem integrator passes with the branch removed. I did not add a test that would pass either way and call it a regression test. What I added instead is a direct unit test of the two translations, which does discriminate: with both branches removed it fails 4 of its 5 assertions (false === nothing, twice, and the two array cases). The docstring now says explicitly that it is the outer guard.

(Attempting the end-to-end discrete construction also runs into something unrelated: a Pre(g) on a discrete parameter inside a shift-system affect fails in mtkcompile with ArgumentError: gₜ₋₁(t) is present in the system but gₜ₋₁(t) is not an unknown. Not chased here.)

Array — the nonlinear solve compares a scalar norm against the tolerance (NonlinearSolveBase/src/termination_conditions.jl:246-248, v2.49.3: objective = apply_norm(mode.internalnorm, du); criteria = abstol), so an array is not a supported input; and the ODE integrator's per-component array is sized for the parent system's unknowns while the affect's nonlinear problem has its own, so it would be the wrong length even if it were. minimum is flagged in the docstring as a deliberate choice — the tightest component, so the affect is never solved looser than any part of the integration.

Tests — symbolic_events.jl, run the way InterfaceI runs it:

Test Summary:       | Pass  Broken  Total      Time
Symbolic Event Test |  387       2    389  22m43.3s

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]
Conversation: https://claude.ai/code/session_01UBTNFZ4A8Ciw27TmVHkpRD

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 7, 2026 06:57
@ChrisRackauckas
ChrisRackauckas merged commit 1c1513b into master Sep 7, 2026
15 of 45 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]>
ChrisRackauckas added a commit that referenced this pull request Sep 10, 2026
…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]>
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.

1 participant