Skip to content

The rotated nonlinear loop honours a user-set snes_max_it (#920) - #927

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/rotated-max-it-920
Nov 3, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/rotated-max-it-920

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 2, 2026

Copy link
Copy Markdown
Member

The rotated free-slip Newton loop always ran at most 50 increments, whatever the user set as snes_max_it.

Change

  • The cap.
    • SolverBaseClass._rotated_max_it() returns the user's snes_max_it, else 50.
    • SNES_Stokes_SaddlePt resolves the cap through its owned-option latch and pushes it, so a later standard-path solve reads the user's value back correctly.
    • Both rotated call sites pass it: _solve_rotated_if_registered and fault_contact.solve_with_fault.
  • The loop's passes. It runs max_it + 1 passes, and the last pass only measures the residual, as PETSc's SNES does. So a cap of N accepts a solve that converges in N increments, and a cap of 0 is one residual evaluation (it used to crash on an unbound residual).
  • Negative values mean what they mean to PETSc. -1 and -2 give the default of 50. -3 (PETSC_UNLIMITED) gives 10000, the bound PETSc's own snes_max_funcs puts on an unlimited solve. PETSc refuses values below -3 at setFromOptions.
  • Docs. rotated-freeslip.md documents the cap, and that staged Picard steps count against it on this path (the standard path's warm-up does not).

Tests

tests/test_1136_rotated_loop_honours_snes_max_it.py (tier B, serial batch 4) covers:

  • raising the cap past 50: tol 1e-11 takes 60 increments under a cap of 100;
  • lowering it: a cap of 3 stops with DIVERGED_MAX_IT;
  • a linear solve under a cap of 1, which converges;
  • a cap of 0;
  • negative caps at tol 1e-11: -1 stops at 50 with DIVERGED_MAX_IT, and -3 converges in more than 50;
  • Vector_Projection reading the user's cap;
  • solve_with_fault under a cap of 0.

The tests fail on development, where the cap is ignored.

On kaiju (Linux, PETSc 3.25.0):

  • The add-on matrix's Coulomb and rate-and-state fault cases, capped through snes_max_it, match the earlier runs that capped the loop by wrapping it: Newton 77 and 75, velocity iterations 308 and 269, slip 7.9858 and 6.6330.
  • tests/parallel at np 2 and 4, and tier A+B, show no failure beyond the development baseline.

Closes #920. Left open: #924 (the rotated loop also ignores a user-set snes_rtol, snes_atol and snes_stol, and the yield ladders' per-rung cap; filed from this review), and #876, #883, #884, #887, #910 and #911 (verdicts in the review comment).

Underworld development team with AI support from Claude Code

test_1018's power-law box under rotated free slip on every wall, with the
frozen (Picard) tangent, converges in 43 iterations. With snes_max_it = 3 the
loop must stop after three increments and report not converged; on
development it runs all 43.

Underworld development team with AI support from Claude Code
SolverBaseClass._solve_rotated_if_registered and fault_contact.solve_with_fault
called rotated_bc.solve_rotated_freeslip without max_it, so the loop always
ran to its default 50 and a user's snes_max_it never reached it. On the
add-on matrix's yielding-lid box, Coulomb and rate-and-state faults stopped
at 50 in their Picard phase with the residual still falling.

Both now pass max_it=solver._resolve_snes_max_it(50). SNES_Stokes_SaddlePt
already resolves the key through its owned-option record (#483); the base
class gains a plain resolver (a user-set snes_max_it, else the default) for
the vector solver, which also reaches the rotated path and never writes the
key itself. The rotated free-slip doc says so; test_1136 is in batch 4.

Underworld development team with AI support from Claude Code
…e cap hook

The review of 6c56b18 found, now that the cap is user-set:
- the loop never tested the residual after its last increment, so a cap of
  N reported failure on a solve that converged in N (43 of 43 on the
  power-law box), and a linear solve under a cap of 1 warned and dropped
  has_solution;
- snes_max_it = 0 ran no pass and crashed on an unbound rnorm;
- SNES_Stokes_SaddlePt resolved the key on the rotated path without pushing
  it, so its owned-option latch could mistake a later user value equal to a
  stale push for its own.

solve_rotated_freeslip now runs max_it increments and one more pass that
only measures the residual, as PETSc's SNES tests after its max_it-th
update; a cap of 0 is one residual evaluation. Both call sites ask the
solver for _rotated_max_it(): the base class reads a user-set snes_max_it,
else 50 (the vector solvers), and SNES_Stokes_SaddlePt resolves it through
its owned-option record and pushes it. The docstring and the subsystem doc
say that staged Picard steps count against the cap, unlike the standard
path's warm-up.

test_1136 now checks raising the cap past 50 (54 increments at tolerance
1e-9), lowering it with the exit reason DIVERGED_MAX_IT, a linear solve
under a cap of 1, a cap of 0, the vector solvers' resolver and
fault_contact.solve_with_fault. Five of its six fail on development; the
cap-of-1 case fails on 6c56b18.

Underworld development team with AI support from Claude Code
The second review found that a negative snes_max_it, which PETSc reads as
PETSC_DETERMINE / PETSC_DEFAULT (-1, -2: the default) or PETSC_UNLIMITED
(-3: no cap), reached the loop as range(0) and crashed it on an unbound
rnorm, after writing the fields; development ignored the option. The loop
now maps -3 to no cap and other negative values to 50, and its docstring
says so, and that a cap of 0 leaves a cold datum lift unapplied. The
comment on the increment count no longer says the index undercounts on the
cap exit.

test_1136 adds -1 and -3 (both converge the 43-increment solve), raises the
cap at tolerance 1e-11 (60 increments, ten past the default, where 1e-9
left four), says that the cap-of-1 test guards the measuring pass and also
passes on development, and carries its tier on each test.

Underworld development team with AI support from Claude Code
9fa9678 moved the tier onto each test and missed one, so the
"tier_a or tier_b" selection dropped the vector-solver resolver test.

Underworld development team with AI support from Claude Code
…nded

From the check of 9fa9678: the negative-cap test ran a 43-increment solve,
so it passed whether -1 meant 50 or no cap. It now runs the 60-increment
solve at tolerance 1e-11: -1 stops at 50 with DIVERGED_MAX_IT, -3 converges
past 50. PETSC_UNLIMITED lifted the loop's cap with no bound at all, where
PETSc still stops at its function-count limit (snes_max_funcs, 10000); the
loop evaluates one residual per increment, so -3 now stops there too. -2
(PETSC_CURRENT) is the default of 50, as the docstring says; PETSc refuses
anything below -3 when it reads the option.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 2, 2026 23:14
@lmoresi

lmoresi commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

Pre-PR adversarial review

Three rounds: 6c56b18, then 2425b4f and 9fa9678, then a final check of 9fa9678 and 1157b27. Each finding below was fixed on the branch or answered in the docs, except where it says otherwise.

Round 1

  • MEDIUM: a cap of exactly N reported failure on a solve that converged in N. The loop never tested the residual after its last increment. Fixed in 2425b4f: max_it + 1 passes, the last only measuring. test_1136 covers it.
  • MEDIUM: snes_max_it = 0 crashed (unbound rnorm). Fixed by the same change, and tested.
  • LOW: the owned-option latch misread a user value after a standard solve followed by rotated solves. Fixed: the rotated path pushes the resolved cap.
  • LOW: staged Picard steps count against the cap on the rotated path only. This is now documented in rotated-freeslip.md.
  • LOW: the tests did not reach the base resolver or solve_with_fault. Fixed: a Vector_Projection test and a solve_with_fault test.
  • LOW: the fixture only lowered the cap and did not pin the exit reason. Fixed: a test that raises the cap past 50, and DIVERGED_MAX_IT asserted.
  • LOW: duplicated option parsing. Partly fixed: one hook, _rotated_max_it, replaces the second resolver. The base class's hook still repeats the latch's integer parsing in six lines (raised again in round 2); left as is.

Round 2

  • MEDIUM: a negative cap crashed (range(0)). Fixed in 9fa9678: -1 and -2 map to 50, and -3 is unlimited.
  • LOW: a stale comment on iters, fixed.
  • LOW: a cap of 0 with a cold datum lift writes the unlifted starting state (v = 0), and on datum-only forcing the warning prints rel = 0. Left as is: a cap of 0 measures the residual and solves nothing.
  • LOW: a test-fragility note on the 54-increment fixture. Fixed: the raise test now runs at tol 1e-11 (60 increments).
  • LOW: a scripts/test.sh conflict with the pressure-rtol branch. That branch is not in this set.

Final check

  • MEDIUM: the negative-cap test could not tell -1 from -3, because the fixture converged under 50 either way. Fixed in 92a330c: at tol 1e-11, -1 stops at 50 with DIVERGED_MAX_IT and -3 converges in more than 50.
  • LOW: -3 had no bound. Fixed: 10000, PETSc's snes_max_funcs.
  • LOW: -2 is PETSC_CURRENT in PETSc 3.22+, and the rotated loop maps it to 50. That is documented. The standard path's handling of -2 after a pushed 0 predates the branch.
  • Not a defect: a ValueError branch for values below -3 was unreachable, because PETSc refuses them at setFromOptions (error 63). It was removed.

Attacks that failed

  • Routes into the rotated loop: SNES_Vector (ThinSheet, projections, adjoint loads), SNES_Stokes_SaddlePt (Stokes, VE_Stokes, Stokes_Constrained, NavierStokes) and solve_with_fault.
  • The push on the standard path and the inner SNES: the rotated path never calls snes.solve, and the standard path's behaviour is unchanged.
  • Every loop exit: Fhat, dhat and bhat are destroyed, and nonlinear_iterations == len(ksp_its).
  • Cap 0, cold and warm; continuation; cap 1 with a lift.
  • CI reach: serial batch 4.

Issue verdicts

Underworld development team with AI support from Claude Code

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.

🟡 Changes recommended

The solver mishandles PETSC_CURRENT by resetting the iteration cap and PETSC_UNLIMITED by imposing a finite cap, while failing to enforce function-evaluation budgets across line-search calls.

2 open issues
What changed in this PR

This PR addresses #920 by passing user-configured snes_max_it limits into Underworld3’s rotated free-slip and fault-contact nonlinear solvers.

Changes:

  • Adds iteration-cap resolution for both rotated solver entry points.
  • Checks convergence after the final permitted increment and supports a zero-increment solve.
  • Documents cap behavior and adds regression tests to the serial test runner.
File Description
tests/​test_1136_rotated_loop_honours_snes_max_it.py Tests cap values, convergence boundaries, and solver entry points.
src/​underworld3/​utilities/​rotated_bc.py Handles negative caps and adds the final residual-only pass.
src/​underworld3/​utilities/​fault_contact.py Passes the resolved cap into fault-contact solves.
src/​underworld3/​cython/​petsc_generic_snes_solvers.pyx Resolves and forwards user-configured rotated iteration caps.
scripts/​test.sh Registers the regression tests in serial batch 4.
docs/​developer/​subsystems/​rotated-freeslip.md Documents iteration limits and how Picard steps count.

Review effort: Balanced

Comment on lines +1197 to +1198
if max_it == PETSc.UNLIMITED:
max_it = 10000
Comment on lines +1199 to +1200
elif max_it < 0:
max_it = 50
@lmoresi
lmoresi merged commit f53bbff into development Nov 3, 2026
14 of 16 checks passed
lmoresi added a commit that referenced this pull request Nov 3, 2026
scripts/test.sh: batch 4 keeps both entries, tests/test_1134*py (#918) and
tests/test_1136*py (#920). check_test_coverage: all 386 test files reachable.

Underworld development team with AI support from Claude Code
lmoresi added a commit that referenced this pull request Nov 3, 2026
Underworld development team with AI support from Claude Code
lmoresi added a commit that referenced this pull request Nov 3, 2026
…ial row 5 counts

Underworld development team with AI support from Claude Code
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.

2 participants