Repository navigation
Matrix-free Jacobians / Krylov linsolve for the Radau FIRK methods - #4166
Merged
Merged
Conversation
`initialize!(::AdaptiveRadauCache)` allocated `integrator.k[3:end]` with `similar`, leaving them uninitialized. Those slots hold one stage value each; the extrapolated initial guess reads `integrator.k[i + 2]` for all `i in 1:num_stages`, while only the first `num_stages` are ever written back. As soon as the controller raises the stage count, the newly used slots are read before anything has written them. The result was intermittent: on a 48-state Brusselator, `AdaptiveRadau()` returned `ReturnCode.Unstable` in 4 of 20 identical single-threaded runs, and the successful runs disagreed with each other (11 distinct final states out of 20). Fixing the initialization gives 0 of 20 unstable and 1 distinct result. Co-Authored-By: Chris Rackauckas <[email protected]>
…hods
RadauIIA3, RadauIIA5, RadauIIA9 and AdaptiveRadau rejected any non-concrete
Jacobian, so none of them could be combined with a Krylov `linsolve` — the one
combination that makes FIRK usable on large stiff systems at tight tolerances.
The W-transform already decouples a step into one real shifted solve plus one
shifted solve per complex-conjugate stage pair, each of the form `-γ M + J`.
The real stage is `SciMLOperators.WOperator`, which already applies that
lazily. The new piece is `ComplexWOperator`: a complex-`gamma` counterpart that
assembles the complex product from two real Jacobian-vector products, since
`J (a + bi) = Ja + i Jb`. That is what lets a real JVP operator — which cannot
be applied to a complex vector — back the complex stage pairs.
Also fixes two combinations broken independently of Krylov, both because
`build_J_W` returns a `WOperator` whenever a JVP exists and FIRK could not
index into one: `concrete_jac = true` with a factorization `linsolve`
(`MethodError` on `setindex!(::WOperator, ...)`), and `RadauIIA3` with a Krylov
`linsolve`, which had no guard at all (`MethodError` on
`similar(::JVPCache, ::Type{ComplexF64})`).
GaussLegendre still has no matrix-free path — it solves one coupled
`num_stages * length(u)` system rather than decoupled shifted ones — but now
says so and says what to use instead.
Closes SciML#4156
Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas
approved these changes
Aug 8, 2026
ChrisRackauckas
marked this pull request as ready for review
August 8, 2026 10:53
Contributor
|
How do we inform the used preconditioner about the special block structure for the block-stages? (xref https://arxiv.org/abs/2001.00711 or https://arxiv.org/pdf/2101.00512 section 3) |
Member
|
There are no block stages for Radau, so nothing required there, as long as the preconditioner works on the complex form. |
This was referenced Aug 8, 2026
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 #4156.
What changed
RadauIIA3,RadauIIA5,RadauIIA9andAdaptiveRadaunow work with a matrix-freelinsolve. The W-transform already decouples a step into one real shifted solve plus one shifted solve per complex-conjugate stage pair; each is-γ M + Jfor a differentγ, so each can be applied lazily through the same real Jacobian-vector product instead of being assembled entrywise.The real stage is
SciMLOperators.WOperator, which already exists and already does this. The new piece is the complex stage:ComplexWOperatorinlib/OrdinaryDiffEqFIRK/src/firk_operators.jl, a complex-gammacounterpart ofWOperatorthat assembles the complex product from two real Jacobian-vector products (J(a + bi) = Ja + i Jb). That is what lets a real JVP operator — which cannot be applied to a complex vector at all — back the complex-conjugate pairs.Everything else is plumbing at the four
alg_cache/perform_step!sites: pick the lazy or the concrete stage matrix, and setγon the lazy one instead of writingn²entries.Two combinations that were broken independently of Krylov also start working, because
build_J_Whands back aWOperatorwhenever a JVP exists and FIRK could not index into it:RadauIIA5(concrete_jac = true)(and the other three) — previouslyMethodError: no method matching setindex!(::WOperator, ...).firk_real_Wnow falls back to a concrete matrix when the linear solver wants one.RadauIIA3(linsolve = KrylovJL_GMRES())— previouslyMethodError: no method matching similar(::JVPCache, ::Type{ComplexF64});RadauIIA3had no guard at all.GaussLegendreis unchanged: it solves a single couplednum_stages * length(u)system rather than decoupled shifted ones, so there is nothing to apply lazily without a new block operator. Its error message now says that, and says what to use instead.Failing before, passing after
The issue's reproducer, extended to the neighbouring combinations, on a 40-state 1D Brusselator at
reltol = abstol = 1e-10.Before (at the parent commit):
After:
The new test file cannot run at all before this commit — it references
ComplexWOperator, which does not exist there — so the reproducer above is the before/after evidence rather than the test suite.Accuracy against a tight reference
Same problem,
reltol = abstol = 1e-10, reference fromRadauIIA5at1e-14.cj-kryisconcrete_jac = truewith GMRES,cj-luisconcrete_jac = truewith the default factorization.massmatrepeats it with a non-identity diagonal mass matrix.cj-lureproduces the dense-Jacobian result exactly, as it should — that path assembles and factorizes the sameW. The matrix-free results sit within an order of magnitude of the dense ones, in both directions; the residual difference is the usual inexact-Newton effect of solving each stage system toreltolinstead of exactly, and it shrinks with the tolerance (massmat RadauIIA9goes 4.4e-7 → 2.4e-7 on tightening1e-9 → 1e-11).Separately,
ComplexWOperatoris unit-tested against its dense form forI,λI, a diagonal, and a full mass matrix.nfaccountingEach complex stage solve spends two real Jacobian-vector products per Krylov iteration, and
dolinsolvecannot see them — it only accounts forWOperator. The operator counts its own products and drains them into the stats after each Newton iteration (a counter on the operator rather than one inferred fromlinres.iters, soAdaptiveRadau's threaded stage solves each tally into their own operator instead of racing onintegrator.stats).Counting the RHS calls directly and comparing against
sol.stats.nf, on a 12-state linear stiff problem at1e-9:Exact for the fully matrix-free path with both AD backends; the test suite asserts
sol.stats.nf == actualfor all four methods ×AutoForwardDiff/AutoFiniteDiff.The
kry+cjundercount is pre-existing and not FIRK-specific:dolinsolvegates its accounting onlinsolve.A isa WOperator && linsolve.A.J isa AbstractSciMLOperator, and withconcrete_jac = truetheJis a concrete matrix even though the products go throughjacvec. That affects every Newton-Krylov integrator (SDIRK, BDF, Rosenbrock) and belongs in a separate PR againstOrdinaryDiffEqDifferentiation.RadauIIA3reads 1.000 there only because it has no real stage solve, so all of its products go through the new operator.Verification
The single QA failure is pre-existing. The same run on this branch's parent gives the identical
19 passed, 1 failedwith the same:SciMLBasedocstring check, andlib/OrdinaryDiffEqFIRK [QA]is already red on master upstream: https://github.com/SciML/OrdinaryDiffEq.jl/actions/runs/31198062683.An earlier revision of this PR did add three real
ExplicitImportsfailures (alg_autodiffimported from a non-owner,SciMLOperators.getops,LinearSolve.needs_concrete_A). Two are gone —alg_autodiffis replaced by the algorithm's own publicautodifffield, and the speculativegetopsmethod is dropped.needs_concrete_Ais allow-listed intest/qa/qa.jlwith a comment: it is the only way to ask whether a linear solver can consume an operatorA,build_J_WinOrdinaryDiffEqDifferentiationreaches for the same name and ignores it the same way, and it is a make-public candidate upstream in LinearSolve.What I did not verify
Cannot resolve @ref for md"[DummyControllerCache](@ref)", thenmakedocs encountered an error [:cross_references]. Already red on master upstream for the same reason (https://github.com/SciML/OrdinaryDiffEq.jl/actions/runs/31198061733). Not introduced here; Render DummyControllerCache so its @ref resolves (fixes the master docs build) #4165 fixes it, and with that one-line change applied the full build succeeds, so thedocs/src/implicit/FIRK.mdaddition here does render.wrapprecsis commented out in the FIRK caches and stays that way;concrete_jac = truegives you the concreteJto build one from, but nothing wires it up.Val{false}) caches. They build a denseJdirectly and are untouched.lib/OrdinaryDiffEqFIRK/src/firk_addsteps.jlcarries a near-duplicate of the same W assembly and is not updated. Those methods take(integrator, cache[, repeat_step]), while every_ode_addsteps!call site inOrdinaryDiffEqCorepasses 11 arguments — I checked every call site's arity and none is ≤ 4, so they are unreachable. Rather than ship an untestable parallel change I left them; if they are ever wired up they will need the same treatment (or, better, to stop being a copy ofperform_step!).Things worth pushing back on
Wis re-formed every step, ignoring thedo_newWreuse heuristic. Settingγon an operator is O(1) and no factorization follows it, so there is nothing to amortize and a currentWis a strictly better Newton matrix. It also sidesteps a staleness hazard specific toAdaptiveRadau:do_newWlooks only atdt, so when the controller raisesnum_stagesthe newly usedW2[i]would otherwise still hold whatever shift it was last given — for a slot never used before, that is the construction-time placeholder. The concrete path keeps the original behaviour untouched.integrator.stats.nwtherefore increments every step on the matrix-free path, where the concrete path increments only whendo_newWsays so. That is accurate —Wreally is re-formed each step — but it does makenwmean something slightly different between the two paths.ComplexWOperatoris internal, not exported and notpublic. It is reachable only throughalg_cache, and I would rather not commit to it as API before someone has used FIRK+Krylov in anger.massmat RadauIIA9accuracy gap (4.4e-7 vs 4.4e-8 dense) is the largest divergence in the table. I attribute it to inexact Newton and checked that it converges with tolerance, but a reviewer who wants the linear solves driven tighter thanintegrator.opts.reltolshould say so.Please ignore until reviewed by @ChrisRackauckas.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Gstik9KAfxtNAz53ejBPwN