Skip to content

#907: a cold rotated solve whose first linear solve fails raises - #916

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/rotated-solve-raises-on-failure
Oct 31, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/rotated-solve-raises-on-failure

Conversation

@lmoresi

@lmoresi lmoresi commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

A rotated free-slip solve whose first linear solve failed used to write its zero initial guess into the fields as if it were a solution, and only warned. This PR makes a cold start whose first linear solve fails raise, on every rank.

Closes #907

What was wrong

The rotated route runs its own Newton loop, and an outer linear solve that failed hard (a preconditioner that failed, a breakdown, a NaN) only warned. On a cold start, a failure at iteration 0 left the velocity at zero. A caller that did not read _rotated_freeslip_info["converged"] took the zeros for a solution. Found with a singular multigrid coarse level under plain LU (#901).

A second defect was found in the review. PCMG and PCREDUNDANT never reset a failed preconditioner's reason at set-up, so a cached workspace whose KSP failed hard failed every later solve on that solver at iteration 0, even after the cause was gone. That was already true on development.

The fix

  • When it raises. On a cold start (a zero initial guess, including a warm request on zero fields, with or without a datum) whose first outer linear solve fails hard, with no step accepted. The iteration cap (-3) is not a hard failure.
  • The message names the KSP reason, from solve_report.ksp_reason_string.
  • The order. The loop leaves through its normal teardown without writing the fields. The solver records the result and the convergence status, and then raises. A caller that catches the error reads converged = False, the KSP reason and a negative SNES reason.
  • The callers that raise: Stokes.solve, the vector solver, fault_contact.solve_with_fault and FaultNetwork.solve. The adjoint warns.
  • The opt-out. solver.tolerate_linear_failure = True (declared on the solver base class) warns and continues, as before. The fields then hold the zero guess, and a failed datum lift is not applied.
  • Elsewhere a hard failure warns and keeps the state:
    • on a warm start;
    • after an accepted step. A solve driven past round-off fails its last linear solve this way.
  • A workspace whose KSP failed hard is never cached, whether the failure raised or warned. The next solve builds afresh, at the cost of one full KSP set-up (Rotated solve past round-off fails its last linear solve; since #907 a time loop rebuilds its KSP every step #911).

The standard SNES path is unchanged: it only warns on divergence.

Tests

  • test_1115 (tier B, serial, 7 cases) forces the failure with a coarse-level python PC that reports a failure at every application, and with a NaN forcing for the fault route. It checks:
    • after a caught raise, the records read failure and the fields keep a preset value;
    • the opt-out leaves the zero guess, and with a datum it does not apply the lift;
    • a warm start whose first step fails keeps its state, and the next solve converges once the cause is gone;
    • a warm request on zero fields with a datum raises;
    • a solve after a caught raise converges;
    • solve_with_fault raises on test_0846's split box (reason -9) and keeps the fields.
  • tests/parallel/test_1116 (tier B) fails the preconditioner on rank 0 only. PETSc reduces the failed reason, and every rank raises. It passes at np 2 and 3.
  • The tests added in review rounds 2 and 3 were each run before their fix and failed. The round-1 cases check behaviour that the round-1 review's probes showed broken on 52d1789. The datum-lift test fails with its guard removed.

The rotated and fault-contact set passes: 346 serial and 27 parallel at np 2.

Docs

  • rotated-freeslip.md states the convergence contract, the opt-out and the cost of dropping a failed workspace.
  • ROTATED_FREESLIP_LINEAR_REUSE.md lists a hard failure under Invalidation.
  • split-node-faults.md says the rate-and-state loop's cold starts raise.
  • The faults guide's status row covers the raise, and two names that no longer exist are gone from it.
  • The docstrings of solve_with_fault and FaultNetwork.solve say they raise.

Review

Three rounds of adversarial review are in the first comment.

Issues

Underworld development team with AI support from Claude Code

The rotated route runs its own Krylov loop and warned and went on when the
outer linear solve failed. With a preconditioner failing at iteration 0
(KSP_DIVERGED_PC_FAILED, -11), it returned with the velocity at zero.

The failure is forced by a coarse-level python PC that reports a failure at
every application, so it fails the same way on any build. (#901's singular
coarse case under plain LU stops failing once #901's double-setup fix lands.)
- test_1115 (serial): the solve raises, naming the reason. With
  solver.tolerate_linear_failure = True it keeps the last iterate and
  records the reason.
- test_1116 (np >= 2): every rank raises.

On development the raise tests fail ("DID NOT RAISE"), and the tolerate
case passes, since that is development's behaviour. test_1115 joins CI
batch 4.

Underworld development team with AI support from Claude Code
… raises

On a hard failure of the outer linear solve (fieldsplit-Schur, vector or
direct LU), the rotated Newton loop used to warn and go on: a
preconditioner that failed, a breakdown, a NaN, a residual that grew. On
the first step that left the fields at the initial guess, and with a
preconditioner failing at iteration 0 the velocity stayed at zero.

Now such a failure raises on every rank, naming the KSP reason, when no
Newton step has been accepted yet. solver.tolerate_linear_failure = True
keeps the old behaviour: warn and keep the guess. The decision reads the
KSP's converged reason and the line-search record, which every rank holds
alike.

After an accepted step the fields hold that iterate. The line search
rejects a failed increment and the loop's convergence warning stands, so a
hard failure only warns there. test_0022's fixture is that case: its
power-law TI tangent is driven to 1e-11, and the sixth linear solve fails
at round-off (a NaN in the GAMG velocity sub-solve) with Newton already at
2.45e-10 of the reference. A first version raised there and broke it.

An iteration cap (KSP_DIVERGED_ITS) and the velocity sub-solve's own
reason only warn, as before.

test_1115 and test_1116 pass (serial, np 2 and 3), as does test_0022.
All 341 serial tests and 27 tests/parallel tests at np 2 that touch the
rotated path or fault contact pass. rotated-freeslip.md states the rule.

Underworld development team with AI support from Claude Code
From the adversarial review of 52d1789:

- A warm re-solve of a state at round-off raised. On test_0022's fixture at
  tolerance 1e-11, 12 of 12 warm re-solves raised: on a warm start the guess
  is the caller's own state. The raise is now for a cold start (a zero
  initial guess) with no step accepted. A warm start warns and keeps its
  state. The reviewer's probe now warns on all 12.
- The raise skipped the loop's teardown and records. A caught raise left
  _rotated_freeslip_info and the SNES reason reading success, and an
  uncached solve leaked its objects. Now:
  - the loop records the failure, destroys the iteration's temporaries and
    leaves through the normal teardown, with SNES reason -3
    (DIVERGED_LINEAR_SOLVE), without writing the fields;
  - the solver raises after recording the result and the convergence
    status.
- With the opt-out, a cold datum lift applied a failed (NaN) increment. The
  lift is no longer taken from a failed solve.
- tolerate_linear_failure is declared with the solver's other options.
- The message uses solve_report.ksp_reason_string. That names -11
  KSP_DIVERGED_PCSETUP_FAILED, as everywhere else, where a duplicate table
  had called it KSP_DIVERGED_PC_FAILED.
- A hard failure is judged by the caller. The loop raises or warns, and the
  adjoint warns, so no "proceeding with the last iterate" warning prints
  before a raise.
- test_1115 checks three things:
  - after a caught raise the records read failure and the fields are not
    written;
  - the opt-out writes the zero guess;
  - a warm start whose first step fails keeps its state, to 1e-8 (the
    write-back re-applies the rotation gauge, 3e-10).
  test_1116 fails rank 0 only: PETSc reduces the PC's failed reason, and
  every rank raises. Its timeout statement is corrected.
- rotated-freeslip.md and the faults guide's status row describe the
  behaviour.

All 342 serial tests and 27 tests/parallel tests at np 2 that touch the
rotated path or fault contact pass. test_1116 passes at np 2 and 3.

Underworld development team with AI support from Claude Code
…ot cached

From the second adversarial review (of 4d57036):

- fault_contact.solve_with_fault, and FaultNetwork.solve through it, call
  the loop directly and never raised. Since 4d57036 they left the fields
  unwritten with no warning, and the result's U was zero. They now raise
  the same message after recording the result.
- A caught raise poisoned the cached workspace. PCMG and PCREDUNDANT never
  reset a failed preconditioner's reason at set-up, so every later solve on
  the solver failed at iteration 0, even with the cause removed. This was
  pre-existing for any hard failure on a cached solver. A workspace whose
  KSP failed hard is no longer cached; it is destroyed.
- Two warnings before the raise contradicted it: the unconverged-exit
  warning, and the velocity sub-solve's. The first is skipped when the
  raise follows; the second prints only when the outer solve did not fail
  hard. rotated-freeslip.md no longer says an unconverged exit always
  leaves the last iterate.
- The cold start is read before the warm path imposes the datum. A warm
  request on zero fields with a datum is a cold start.
- On the raise path the result's rotation_gauge is None, not the previous
  solve's record. The Returns list documents linear_failure.
- The getattr guard on the declared opt-out is gone.

test_1115 gains four tests: a solve after a caught raise converges,
solve_with_fault raises (a NaN forcing on test_0846's split box, reason
-9), a warm request on zero fields with a datum raises, and a tolerated
cold failure with a datum leaves finite fields. The first three fail on
4d57036. The last fails with round 1's lift guard removed.

All 346 serial tests and 27 tests/parallel tests at np 2 that touch the
rotated path or fault contact pass. test_1116 passes at np 2 and 3.

Underworld development team with AI support from Claude Code
From the third adversarial review (of eac2c90):

- The drop of a workspace that failed hard on a warm start, or after an
  accepted step, was untested: every new test reached it through the cold
  raise. test_1115's warm case now solves once more with the cause
  removed, and must converge. It fails when the drop is limited to the
  raise.
- The tolerated cold failure with a datum leaves the fields exactly zero.
  The test asserted only that they were finite, which a datum snap after
  the failed lift would also satisfy.
- rotated-freeslip.md gives the drop its own paragraph. It covers raised
  and warned failures alike, and states the cost: a full KSP set-up, 38.7 s
  to 76.5 s on a 39k-dof FMG fixture, paid every step by a time loop driven
  past round-off (#911). It also says the velocity sub-solve's warning
  gives way to the outer failure.
- ROTATED_FREESLIP_LINEAR_REUSE.md lists a hard failure under
  Invalidation.
- solve_with_fault and FaultNetwork.solve say they raise.
  split-node-faults.md says the rate-and-state loop's cold starts raise
  on a first-step failure.
- adapt-on-top-faults.md no longer names solve_rotated_freeslip_nonlinear
  or a nonlinearity-probe dispatch; neither exists.

Left open: #910 (a warm solve on the fast path fails at iteration 0 with
no cause) and #911 (a solve past round-off fails its last linear solve;
since this branch, a time loop rebuilds its KSP every step).

test_1115 (7) and test_0846 pass.

Underworld development team with AI support from Claude Code

lmoresi commented Oct 31, 2026 •

Copy link
Copy Markdown
Member Author

Adversarial review, round 1 (bugfix/rotated-solve-raises-on-failure at 52d1789)

Reviewed by a separate agent against docs/developer/guides/adversarial-review.md, at np <= 2.

Findings and what was done

  1. A warm re-solve of a state already at round-off raised. On test_0022's fixture at tolerance 1e-11, every warm re-solve of a 1.2e-10 state raised (12 of 12). The verdict was "no step accepted", and on a warm start the guess is the caller's own state.
    Resolution: the raise is now for a cold start only: a zero initial guess, no step accepted, no opt-out. A warm start keeps the caller's state and warns. The reviewer's probe now warns on all 12. test_1115 adds a warm case (a changed problem, a failing first step) and checks that the state is kept to 1e-8: the write-back re-applies the rotation gauge, which moves it by 3e-10.
  2. The raise skipped the loop's teardown and records. After a caught raise, _rotated_freeslip_info and the SNES reason still read success, and an uncached solve leaked its objects.
    Resolution: the loop records the failure, destroys the iteration's temporaries and leaves through the normal teardown, with SNES reason -3 (DIVERGED_LINEAR_SOLVE). The solver raises after recording the result and the convergence status. test_1115 checks converged = False, reason -11 and a negative SNES reason after catching the error.
  3. The message and the docs misdescribed the fields.
    Resolution: on a raise, the fields are not written. test_1115 checks a preset value survives, and the message and docs say so. With the opt-out, the fields hold the zero guess.
  4. With the opt-out, a cold datum lift applied a failed (NaN) increment.
    Resolution: the lift is not taken from a failed solve.
  5. The opt-out was an undeclared attribute.
    Resolution: tolerate_linear_failure = False is declared with the solver's other options. The docs and this PR state that the standard SNES path only warns.
  6. The reason names duplicated solve_report.ksp_reason_string, and the two disagreed.
    Resolution: the message uses ksp_reason_string, so -11 is KSP_DIVERGED_PCSETUP_FAILED, as everywhere else. The tests match reason -11, and -3 is described as petsc4py's DIVERGED_MAX_IT.
  7. A "proceeding with the last iterate" warning printed just before the raise.
    Resolution: a hard failure is judged by the caller: the loop raises or warns, and the adjoint warns. The iterative solves warn themselves only on the iteration cap.
  8. test_1116 failed symmetrically, and its timeout statement was wrong.
    Resolution: the failure is now reported on rank 0 only. PETSc reduces the PC's failed reason, every rank raises, and the test passes at np 2 and 3. The docstring says the thread timeout fires on the rank waiting in Python, and the MPI launcher then ends the others.
  9. The capability guides did not mention the raise.
    Resolution: the faults guide's status row (adapt-on-top-faults.md) does now.

Observed, not changed: after one forced failure on a cached solver, later solves kept failing at iteration 0 even with the cause removed. It looks like the failed reason stays latched on the cached KSP's PC. It was not shown for a failure PETSc raises itself.

Issue verdicts


Adversarial review, round 2 (bugfix/rotated-solve-raises-on-failure at 4d57036)

Reviewed by a separate agent, at np <= 3. Each new regression test was run on 4d57036 first and failed there. The datum-lift test was shown to fail with round 1's guard removed.

Findings and what was done

  1. fault_contact.solve_with_fault (and FaultNetwork.solve) never raised. They call the loop directly, and since 4d57036 the fields were left unwritten without a warning. The rate-and-state loop in split-node-faults.md would then pass U = 0 to the fault-state update while the fields still showed the previous step.
    Resolution: solve_with_fault raises the same message after recording the result. test_1115 forces a native failure (a NaN forcing, reason -9) on test_0846's split box: the call raises and the fields keep their preset value.
  2. A caught raise poisoned the cached workspace. PCMG and PCREDUNDANT never reset a failed preconditioner's reason, so every later solve on that solver failed at iteration 0, even with the cause removed. This was pre-existing for any hard failure on a cached solver; the branch documents catching the error and continuing.
    Resolution: a workspace whose KSP failed hard is not cached; it is destroyed, and the next solve builds a fresh one. test_1115 raises, removes the cause, and the next solve converges.
  3. Two warnings before the raise contradicted it ("the fields hold the last (unconverged) iterate", and the velocity sub-solve's "proceeding with the last iterate").
    Resolution: the unconverged-exit warning is skipped when the raise follows. The velocity sub-solve warns only when the outer solve did not fail hard. On the raise path, test_1115 prints no warning. rotated-freeslip.md no longer says an unconverged exit always leaves the last iterate.
  4. The gauge record was the previous solve's after a raise.
    Resolution: rotation_gauge is None on the raise path.
  5. A warm request on zero fields with a datum was not treated as a cold start. The warm path imposes the datum before the check.
    Resolution: the cold start is read before the datum is imposed. test_1115 covers it.
  6. Tests and docs.
    Resolution:
    • test_1115 has regression tests for the datum lift, a solve after a caught raise, and solve_with_fault.
    • The Returns list documents linear_failure and what U holds on that path.
    • The getattr guard on the declared opt-out is gone.

Issue verdicts

Checked and sound

  • The raise path frees its temporaries and the uncached context: no live petsc4py objects accumulate across repeated raises, cached or with a datum.
  • After a raise:
    • converged is False, the KSP reason and a negative SNES reason are recorded, has_solution is False and the transcript outcome is written;
    • the continuation alpha is restored.
  • The cold-start test is a collective norm. With no accepted step, a raise can only happen at iteration 0; Picard and continuation steps count as accepted.
  • A rank-0-only preconditioner failure raises on every rank (test_1116 at np 3).
  • A warm re-solve at round-off, and a failure after an accepted step, warn and keep the state.

Adversarial review, round 3 (bugfix/rotated-solve-raises-on-failure at eac2c90)

A narrow review of the round-2 commit by a separate agent, serial, in the worktree environment.

Findings and what was done

  1. A time loop driven past round-off now rebuilds its workspace every step. On test_0022's fixture at tolerance 1e-11, every step fails hard on its last increment, so every step drops the cache. A rebuild doubles a 39k-dof FMG solve (38.7 s to 76.5 s); the rotation and null space take 0.44 s of that, the fresh KSP set-up the rest. At 1e-6 and 1e-9 every step keeps the cache.
    Resolution: the drop stays. A kept workspace fails every later solve (finding 2 shows it natively), so on development such a loop was wrong after its first failure. The trigger and the cost are now documented. Not running a linear solve at the round-off floor, or clearing the latch in place, is left to Rotated solve past round-off fails its last linear solve; since #907 a time loop rebuilds its KSP every step #911.
  2. Outside the diff: a warm solve on the fast path fails at iteration 0 with no injected failure. On test_1115's annulus with development's SVD coarse solve, warm re-solves from 1.05 * v_solved failed in 16 of 26 serial runs. The pressure sub-KSP (GASM) hits DTOL after 30 iterations, then the velocity sub-solve sees a NaN. The previous solve, with the same right-hand side, converged. Emulating the old caching, one such failure made all later solves fail; with this branch the next solve recovers.
    Resolution: filed as Warm rotated solve on the fast path fails at iteration 0 with no cause (pressure sub-KSP DTOL, then NaN) #910; the cause is not isolated.
  3. The drop after a warm failure was untested.
    Resolution: test_1115's warm case solves again with the cause removed and must converge. It fails when the drop is limited to the raise.
  4. The tolerated datum test asserted only finite fields.
    Resolution: it asserts exact zeros, as the tolerated case without a datum does.
  5. Docs.
    Resolution:
    • rotated-freeslip.md gives the drop its own paragraph, for raised and warned failures, with the cost, and corrects the sub-solve warning sentence.
    • ROTATED_FREESLIP_LINEAR_REUSE.md lists it under Invalidation.
    • The docstrings of solve_with_fault and FaultNetwork.solve say they raise.
    • split-node-faults.md says the rate-and-state loop's cold starts raise.
    • adapt-on-top-faults.md drops two names that no longer exist.

Issue verdicts

Checked and sound

  • A workspace from the cache that fails is destroyed exactly once, and nothing in _rotated_linear_cache still refers to it. Q and Qt are left to the result, which the reaction recoveries read.
  • PETSc -log_view shows no growth in creations minus destructions over 1 and 4 drop-and-rebuild cycles.
  • Direct LU caches nothing across solves, so it has nothing to latch.
  • The solve after a drop converges, within 5.9e-9 of the cached solution at tolerance 1e-6.
  • cold_start is rank-safe. zero_init_guess short-circuits, and otherwise u.norm() is collective at the same point on every rank. The cache decision follows the global KSP reason.

Underworld development team with AI support from Claude Code

Copilot AI balanced review requested due to automatic review settings October 31, 2026 20:14

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…ilure

D (#892) and the #894 fix both changed rotated_bc.py after this branch's CI
ran. Clean merge; CI re-runs on the combination before #916 merges.

Underworld development team with AI support from Claude Code
@lmoresi
lmoresi merged commit 70b0f77 into development Oct 31, 2026
8 checks passed
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