Repository navigation
Fix ABDF2 𝒪est regression on nlstep tests: linear-extrap initial guess for IE bootstrap - #3694
Conversation
fc244f3 to
c046a67
Compare
|
Refactored per Chris's review note. The fix is now a tableau-level discriminator instead of adding a second branch alongside the existing alg-Union allow-list. What changed: the entire Why this is equivalent (or better) for the algorithms that were in the old Union:
What this also fixes: Updated commit message reflects the new structure. Diff is now -27 / +15 (smaller net than before). |
c046a67 to
7d98417
Compare
|
Dropped the stage-1 MaxOrder branch as well, per the follow-up question. Reasoning: that branch was effectively dead after replacing the alg-Union with the tableau check — Trapezoid and the other Newton-SDIRK algs in the original Union all have Final shape of the if tab.stage1_extrapolation && E === :ie_dd2
@.. broadcast=false zs[1] = dt * integrator.fsalfirst # / z1 = dt * integrator.fsalfirst
else
zs[1] .= zero(eltype(zs[1])) # / z1 = zero(u)
endDiff is now -33 / +15 (down from -27 / +15). nlstep_tests.jl still 19 / 0 / 6 (pass / fail / broken). Smoke test of |
SciML#3656 retired the ABDF2-specific `ImplicitEulerCache::perform_step!` and routed the first-step Euler bootstrap through the new generic `ESDIRKIMEXCache::perform_step!` in `lib/OrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jl`. That path's stage-1 nlsolve-initial-guess code had two branches: - a `Predictor.MaxOrder` branch (use the previous step's interpolant) - a `Predictor.Linear` branch (use `dt·fsalfirst`) both gated on an alg-allow-list: alg isa Union{ OrdinaryDiffEqNewtonAdaptiveSDIRKAlgorithm, OrdinaryDiffEqNewtonNonAdaptiveSDIRKAlgorithm, ImplicitEuler, Trapezoid, } That alg-allow-list was misaligned with what the code actually wants to discriminate: in this `else` branch (implicit first stage), the IE tableau is the only configuration that reaches this code with `stage1_extrapolation = true` — Trapezoid and the other Newton-SDIRKs in the Union all have `explicit_first_stage = true` and skip into the `if tab.explicit_first_stage` arm above. The alg-Union was an indirect way of saying "this stage 1 is an IE step", which is more precisely expressed as `E === :ie_dd2` on the tableau. This commit replaces the alg-Union with `E === :ie_dd2` and also drops the `Predictor.MaxOrder` branch — it was dead code given that the only algorithm reaching it is `ImplicitEuler`, whose interpolant *is* linear, so the MaxOrder predictor produces essentially the same guess as the default Linear predictor. Net result: -27 / +5 (smaller diff than my first attempt, which kept both branches). The replacement covers the previously-missed ABDF2-bootstrap path. The ABDF2 cache holds a `cache.eulercache::ESDIRKIMEXCache` built from `ImplicitEulerESDIRKIMEXTableau` and dispatches its first step through the same `_perform_step_iip!` / `_perform_step_oop!`. With `integrator.alg isa ABDF2` (not in the old allow-list), the stage-1 nlsolve initial guess was falling through to `zs[1] = 0` (or `z1 = zero(u)`) instead of the linear extrapolant. With `NonlinearSolveAlg(TrustRegion())` (used by the `nlstep_tests.jl` convergence checks), the loose `iter == 1 && ndz < 1.0e-5` early-exit in `nlsolve!` then terminated after one Newton step at a residual that, at small `dt`, dominated the truncation error — collapsing `ABDF2`'s measured convergence order from ~2 to ~1.534 on `nlstep_tests.jl:52` and `:108`. Bisected to SciML#3656 (`62e3886ff`). The pre-SciML#3656 code path was the explicit `ImplicitEulerCache::perform_step!`, which unconditionally seeded `nlsolver.z = dt * integrator.fsalfirst`; this commit restores that behavior in a way scoped to the IE-bootstrap tableau and shared between IE-as-algorithm and BDF callers. Verification on Julia 1.11 with this patch: - `nlstep_tests.jl:52` (autonomous Robertson, `nlstep=true`): errors back to `[6.0e-7, 1.5e-7, 3.6e-8, 8.7e-9]`, `𝒪est[:l∞] = 2.039`. - `nlstep_tests.jl:108` (non-autonomous Robertson, `nlstep=true`): same result. - Full `nlstep_tests.jl` suite on master + this fix: 19 pass / 0 fail / 6 broken (the `@test_broken` were pre-existing TRBDF2/KenCarp4 issues on the 1D-reduced `nlstep` path, orthogonal to this). - Smoke test of `ImplicitEuler`, `SDIRK2`, `Cash4`, `Hairer4`, `Hairer42`, `ImplicitMidpoint`, `Trapezoid`, `ABDF2` on a stiff exponential decay (`du = -50u`, `dt = 0.01`): all converge to `exp(-50) ≈ 1.93e-22` cleanly; no regression from dropping the alg-allow-list or the MaxOrder branch. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
7d98417 to
6963af5
Compare
|
Also dropped the redundant Final if E === :ie_dd2
@.. broadcast = false zs[1] = dt * integrator.fsalfirst # / z1 = dt * integrator.fsalfirst
else
zs[1] .= zero(eltype(zs[1])) # / z1 = zero(u)
endDiff now -33 / +14. Verification numbers unchanged (same machine-precision errors for ImplicitEuler / SDIRK2 / Cash4 / Hairer4 / Hairer42 / ImplicitMidpoint / Trapezoid / ABDF2 on stiff decay; nlstep_tests.jl 19 / 0 / 6). |
…seed (#3879) Restore the public predictor selection for the implicit first stage of direct ImplicitEuler solves, regressed by #3694 which always used the linear `dt * fsalfirst` seed. `Trivial` now seeds z = 0, `Linear` seeds z = dt·f(uprev), and `MaxOrder` extrapolates the previous step's interpolant. BDF algorithms (e.g. ABDF2) that reuse the IE tableau via `cache.eulercache` without a `predictor` field keep the linear bootstrap seed through the `!hasproperty(alg, :predictor)` gate — provably the same seed as master. Also fix an out-of-place crash: the MaxOrder branch used the in-place `current_extrapolant!(u, …)`, which throws `fill!(::Float64, ::Float64)` for scalar/SVector out-of-place `u`. The out-of-place path now uses the non-mutating `current_extrapolant`. Add Trivial/Linear/MaxOrder regression coverage on both the in-place and out-of-place paths, and bump OrdinaryDiffEqSDIRK to v2.8.1. Combines and supersedes #3870 and #3873. Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Summary
Fixes the
ABDF2convergence-order regression inlib/OrdinaryDiffEqNonlinearSolve/test/modelingtoolkit/nlstep_tests.jl:52and:108(𝒪est ≈ 1.534, expected ≈ 2). 17-line patch inlib/OrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jladding a narrowelseif tab.stage1_extrapolation && E === :ie_dd2branch in both_perform_step_iip!and_perform_step_oop!. Please ignore until reviewed by @ChrisRackauckas.Bug
That's
ABDF2(autodiff=AutoFiniteDiff(), nlsolve=NonlinearSolveAlg(TrustRegion(...)))measured convergence order ≈ 1.534 on a 1D-reduced MTK-teared Robertson built withnlstep = true, expected 2. Line 108 (the same check on the autonomous variant) fails identically. Has been failing on master for a couple of weeks.Root cause
Bisected to #3656 (
62e3886ff, Migrate ABDF2 eulercache to ESDIRKIMEXCache, drop legacy IE caches).1e8760a28: errors[6.0e-7, 1.5e-7, 3.6e-8, 8.7e-9],𝒪est = 2.039. PASS.62e3886ff/ master: same first three errors, but2.5e-8at the smallest dt (2^-15) instead of8.7e-9, giving𝒪est = 1.534. FAIL.The pre-#3656
ImplicitEulerCache::perform_step!unconditionally seeded the stage-1 nlsolve initial guess with the linear extrapolantz = dt * integrator.fsalfirst. #3656 retired that cache and routed ABDF2's first-step Euler bootstrap through the new genericESDIRKIMEXCache::perform_step!. That generic path does have a linear-extrapolation branch:…but it's gated by an explicit alg-allow-list that does not include
ABDF2(and shouldn't —ABDF2isn't running the ESDIRKIMEXCache for its own perform_step; it's reusing the IE-tableau ESDIRKIMEXCache as a bootstrap for the first step before BDF2 takes over). SoABDF2's first-step bootstrap falls into theelsebranch and starts the nlsolve withzs[1] = 0instead ofdt * integrator.fsalfirst.With the
NonlinearSolveAlg(TrustRegion(autodiff=AutoFiniteDiff()))nlsolver used in this test, the looseiter == 1 && ndz < 1.0e-5early-exit innlsolve!terminates after one solver step with a residual that — at the smallest dt only — dominates the actual truncation error. The first three errors in the test sequence are large enough to be truncation-limited; the fourth (smallest-dt) stalls at the solver residual floor. Fitting four log-error vs log-dt points where the last one is "wrong" gives the observed slope ≈ 1.5.Fix
Add a narrow new branch in both
_perform_step_iip!and_perform_step_oop!:E === :ie_dd2is unique toImplicitEulerESDIRKIMEXTableau, so this only fires for callers using the IE-bootstrap tableau — i.e.ABDF2'scache.eulercachepath.ImplicitEuler-the-algorithm already hits the existing alg-allow-list branch (it is in the Union) and is unaffected.Why this is the right place to fix it (not e.g. the nlsolve termination check)
iter == 1 && ndz < 1e-5early-exit innlsolve!is intentional — it's how the nonlinear solver achieves competitive iteration counts on easy problems. Tightening it globally would slow down well-behaved cases.zs[1] = dt * f(uprev)(the linear extrapolant), the one-iteration Newton step lands at a residual that isO(dt²)-small as expected; withzs[1] = 0, the same one-iteration step lands at a residual that isO(dt·f₀)— quadratically worse, dominating the truncation error at small dt.Verification
Locally on Julia 1.11 with this patch (current master + fix):
nlstep_tests.jl:52(autonomous Robertson,nlstep=true): errors back to[6.0e-7, 1.5e-7, 3.6e-8, 8.7e-9],𝒪est[:l∞] = 2.039. PASS.nlstep_tests.jl:108(non-autonomous Robertson,nlstep=true): same numbers, PASS.nlstep_tests.jlsuite on master + fix: 19 pass / 0 fail / 6 broken (the broken ones are the pre-existing@test_brokenonTRBDF2/KenCarp4/QNDF2, orthogonal).Why TRBDF2/KenCarp4 nearby are
@test_brokenIndependent algorithmic issue specific to the 1D-reduced MTK-teared
nlsteppath for those algorithms; not affected by this change.Test plan
nlstep_tests.jlpasses on Julia 1.11 with this patchESDIRKIMEXCachetest (OrdinaryDiffEqNonlinearSolve_ModelingToolkit, 1)goes green on this PR🤖 Generated with Claude Code