Skip to content

#901: the multigrid coarse solve is redundant LU with a zero-pivot shift on every route; no SVD - #947

Merged
lmoresi merged 17 commits into
developmentfrom
bugfix/rotated-fmg-coarse-solve
Nov 5, 2026
Merged

lmoresi merged 17 commits into
developmentfrom
bugfix/rotated-fmg-coarse-solve

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 5, 2026

Copy link
Copy Markdown
Member

The rotated free-slip route solved its multigrid coarse level with a dense SVD whenever the problem left rigid rotations free (#306). The SVD is O(n^3) on every rank and got slower as ranks were added: one factorisation of a 614-cell spherical-shell bottom level took 64 s at np 1 and 102 s at np 4. With LU, the whole Stokes solve takes 7 s at np 4. This replaces it with one rule on every route (Louis's ruling, 2026-11-05): redundant LU with a nonzero factor shift, and no SVD path.

Why LU is enough

  • The coarse operators are near-singular, not singular. On every hierarchy measured, lambda_min/lambda_max is 2.0e-5 to 2.4e-8 on the annulus (40 to 600 bottom cells) and 2.8e-4 on the shell. Plain redundant LU gives the SVD's answer at its iteration counts on every fixture.
  • The shift is for the singular case. It acts only on a pivot below PETSc's zero-pivot tolerance (2.2e-14 times the sum of the magnitudes of the row's off-diagonal entries), and then refactors with a small diagonal shift. On fix(rotated-bc): Schur preconditioning parity + 3D rotation nullspace — Zhong #248 blow-out (44 its -> 1) #306's case, a coarse level that is the fine mesh itself, the rotation is an exact null vector of the coarse operator. The shifted LU gives the SVD's answer to 1e-7 there (test_1111).
  • The gauge is fixed on the fine operator. The coarse solve sits inside the preconditioner: a direction it handles badly costs iterations, not the answer.

The change

  • The shift applies on every route, the native and standard custom-P ones included, not only the rotated one. It is in the shared bundle: mg_coarse_redundant_pc_factor_shift_type = nonzero. The bundle is its only writer: the coarse LU reads it at its set-up on every route.
    • This is a change to the standard path. Before, the native and standard routes ran PETSc's own default shift (INBLOCKS) at np > 1, which the bundle's redundant type keeps, and no shift at np 1, where that type replaces PETSc's shifted PCLU. Kaiju confirmed this in CI's environment: at np 2 and 4, development's coarse LU shows [INBLOCKS] and this branch's shows [NONZERO].
    • On a coarse level whose pivots stay clear of zero it changes nothing. Native and standard custom-P FMG on the annulus give bitwise-identical velocity and pressure at the same iterations with and without it, at np 1 (wombat) and at np 2 and 4 (kaiju).
    • test_1021 reads the shift off the live coarse KSP on all three routes.
  • The bundle's coarse="svd" variant is removed, not deprecated.
    • GEOMETRIC_MG_COARSE_SOLVERS is ("redundant", "gamg").
    • geometric_mg_bundle(coarse="svd") raises a ValueError that names the default. It is never a silent fallback (test_1021).
    • The Charter has no deprecation-period rule.
    • Scripts that set the PETSc option mg_coarse_pc_type svd themselves are unaffected. On the standard path the user's key is respected and the live coarse PC is an SVD (test_1021). The rotated path never took a petsc_options override for its coarse solve, before or after.
  • A rotated set-up that raises leaves no options behind. Both rotated installs drop every key under their per-solve prefix in a finally. Before, a failure after the multigrid set-up left 11 bundle and FAC keys in the global database (test_1021).
  • The coarse level is set up once per solve. The rotated route's inode-SOR loop re-assembled level 0, so the coarse PC was set up twice and an SVD was factored twice (test_1113).
    • Every SVD total quoted before this fix included the second factorisation.
    • A side effect: the np 1 LU now uses the inode kernel.
  • GAMG as the coarse solve, only when asked for: set_custom_fmg(..., coarse_solve="gamg"), for a bottom level too large to gather.
    • It runs FGMRES to 1e-2 with GAMG, an LU-plus-shift base and the restricted rigid-body near-null space.
    • Where both fit, it costs 1.7-4x a gathered LU's coarse work, at the same answer (test_1114, np 4).
  • Docs: one rule in the advanced multigrid guide, solvers.md, rotated-freeslip.md and fault-split-multigrid.md.
    • SVD appears only as something a user may set by hand.
    • Taking the shift off is not offered as a test for singularity: on test_1111's singular level, plain LU factored and converged 5e-7 off.
    • The SVD history and timings are in the CHANGELOG.

Measurements

Kaiju, 2026-11-01. The case is the production faulted annulus (zg95 TAf: split fault, custom-P FMG, rotated walls), 5 steps from checkpoint 20 on one node.

np A B C
16 2.77 s 2.83 s 0.32 s
52 — 5.51 s 0.85 s
  • What that table shows:
    • The coarse level is 6-9x cheaper, but its cost is not flat in np: per application it rises 0.10 to 0.24 ms on C and 0.89 to 1.54 ms on B.
    • Coarse set-up is 0.47-0.97 s on B, where each SVD is factored on both the set-up and the solve side, and about 0 on C.
  • Step time: the coarse level is 3-4 % of a step, so a step is about 5 % faster: 487 against 513 s at np 16, 339 against 366 s at np 52.
  • The answers match: step 21 v_rms 634.58 at np 16 and 634.62 at np 52 on both B and C, with the same Newton counts.
  • Caveats:

Record: https://claude.ai/artifact/ACJ6diJRBCGoBGvCa6aCxM

Tests

  • New:
  • Changed:
    • test_1021: the live shift on three routes, the svd error, a user's own coarse key reaching the standard route, and no options left after a failed rotated set-up.
    • test_1014: the bundle key.
  • test_1111, test_1112 and test_1114 gate their gmsh-recorded answers on the mesh fingerprint (cells and volume), so a host that triangulates differently skips those assertions.
  • Serial on wombat after the development merge: test_1111, test_1113, test_1021 and test_1014's bundle tests pass. test_1111 passes at e15f5fa (3 passed, 21 s).
  • On kaiju, in CI's environment (Linux, conda-forge PETSc 3.25.3), through the MPI supervisor as scripts/test.sh runs them:
    • test_1114 at bbf22f7: 2 passed at np 2 and 2 passed at np 4.
    • On af9d3c3, the serial rotated and custom-MG set (24 files): 210 passed, 2 skipped, 0 failed, with test_1111[shell] the one case that does not finish (below).
    • On af9d3c3, at np 2 and 4: test_1112 and test_1116 pass; test_1064 has 6 passed and 3 skipped.
    • test_1111[shell], as first written at cell size 0.5, did not finish on kaiju in an hour; every velocity sub-solve ran to its 200 cap. The cause is the mesh, not the coarse solve. Linux gmsh builds a different shell at 0.5 (571 coarse cells; macOS builds 614), and refinement folds some of its cells at the inner wall (Refining a spherical shell folds cells at the inner wall: SphericalShell and CubedSphere snap new wall nodes outward through coarse cells that lie on the wall #946). A gmsh seed that folds the same way on wombat fails with the dense SVD coarse solve and with the default normal too. Wombat's own 0.5 shell was folded as well, and converged in 18 velocity iterations where clean shells take 5. 4babb3a moves the case to cell size 0.3 and skips it on a folded mesh. On the 0.3 shell, the dense SVD coarse solve, set by hand, gives the shifted LU's answer to every printed digit (v_rms 1.8237602846) at the same iterations (5 velocity / 17 Schur), in 604 s against 17 s.
    • Linux gmsh's 0.3 shell is clean: 1030 coarse cells, none with four vertices on a wall, no folded cells. Its 0.5 shell folds 5 cells, as the diagnosis predicts. The refinement callback is the cause: with it off, neither 0.5 mesh folds (Refining a spherical shell folds cells at the inner wall: SphericalShell and CubedSphere snap new wall nodes outward through coarse cells that lie on the wall #946).
    • At e15f5fa:
      • test_1111 serial: 2 passed and 1 skipped. On Linux's 0.3 mesh the shell case solves, converges, carries the shift and the three modes. It skips only the recorded-answer comparison, because the mesh fingerprint differs (8240 cells; macOS 8792).
      • test_1114: 2 passed at np 2 and 2 passed at np 4.

Issues

Issue Verdict
#901 Partly fixed; not labelled fixed. Of its three "done when" criteria, two are met: the rotation is removed exactly on the fine level and the answer matches the SVD path to solver tolerance (test_1111), and the solve passes on macOS. The first is not met as written. The coarse cost per Newton iteration still rises from np 16 to np 52 (0.32 to 0.85 s), though it is 6-9x lower than before and 3-4 % of a step, and the step as a whole now gets faster with ranks (487 to 339 s). The redundant LU gathers the coarse level to every rank, so its cost grows with np.
#306 Its zero-pivot failure is handled by the shift. test_1111 is the forced-singular case.
#910 Still live: not marked fixed. On its fixture the warm fast-path failure is no longer seen at the tip (0 of 20 runs, against 4 of 12 with the SVD coarse solve forced). Marking it fixed needs a regression test.
#877 Still live. A 3-D model with units raises a TypeError in _rigid_rotation_fields (numpy.zeros_like on a unit-aware array), reached through _rigid_rotation_modes. The 2-D annulus works. From the code, this branch's _rigid_body_modes, the near-null space for coarse_solve="gamg", calls the same function, so a 3-D units model with the GAMG coarse solve fails the same way.
#946 Filed here, left open. Refining a spherical shell folds cells at the inner wall, in SphericalShell and in CubedSphere(simplex=True); the velocity multigrid can then fail outright. Found diagnosing test_1111[shell] on kaiju; the fix belongs in the meshing code, not in this branch.
#906 Still live. Under the default (faceted) normal, the shell's rotations are not null and are rejected, so they stay in the solution. The radial normal makes them exact. The fix is for a separate branch.

Four adversarial review rounds are posted as comments below: round 1 at 0502a9f, round 2 at b704140, and round 3 over 42e5565..9aa89dc, with its fixes in af9d3c3 and bca8434. test_1114's recorded-answer bound failed on kaiju at np 2 and 4; that is fixed in bbf22f7. Round 4 covers the test-only commits bbf22f7 and 4babb3a, with its fixes in e15f5fa.

Underworld development team with AI support from Claude Code

…ull modes

A closed free-slip wall leaves rigid rotations: one on an annulus, and three
on a spherical shell with the radial normal. The Galerkin-coarsened rotated
velocity block inherits them. The coarse level is then a dense SVD of the
gathered matrix, whose factorisation took 64 s at np 1 and 102 s at np 4 on a
614-cell shell bottom level.

test_1111 (annulus and shell, serial) and test_1112 (annulus, np 2) ask for
the null-mode coarse solve, recorded as coarse_solve, and for the SVD path's
answer at the measured iterations. On development both fail on the record
(KeyError: 'coarse_solve'); their earlier assertions (convergence, custom
FMG, the mode count) pass.

Underworld development team with AI support from Claude Code
… factors with PETSc's LU

When the rotated problem has verified rigid rotations, its coarse level was a
dense SVD of the gathered matrix. That costs O(n^3) on every rank, and the
cost rose with the rank count. The modes are known (the rotated null space),
so the coarse solve now uses them. custom_mg.NullModeCoarseSolve, a python PC
on the bottom level, does three things:

- it restricts the modes there by least squares, (P^T P) m_c = P^T m_f;
- it pins one unknown per mode, chosen by a pivoted QR of the gathered modes;
- it projects the modes out of the right-hand side and the solution, and
  factors the pinned system with PETSc's redundant LU.

A rigid rotation is a left null vector too, for any rheology, so the
projection holds for Newton tangents.

The choice is the bundle's new "null_modes" variant, selected by the
presence of verified modes, with no user option. Both rotated routes (saddle
and vector) take it. SVD stays as the fallback for modes counted but not
extractable. The solve records coarse_solve in _rotated_freeslip_info.

Measured: probe_coarse_depth.py in ~/+Simulations/fac_892/coarse_901.

| case, np 4 | coarse solve | vel / Schur its | coarse setup + solve | wall | v_rms |
|---|---|---|---|---|---|
| annulus, 600 bottom cells | svd | 4 / 9 | 69.3 + 73.1 s | 144 s | 4.7831843385 |
| | null modes | 4 / 9 | 0.004 + 0.023 s | 0.76 s | 4.7831843385 |
| shell, 614 bottom cells, radial normal | svd | 18 / 23 | 101.6 + 105.8 s | 216 s | 1.7726841207 |
| | null modes | 19 / 23 | 0.007 + 1.24 s | 7.1 s | 1.7726841152 |

At 144 bottom cells: 0.016 s against 1.67 s, with identical iterations
and answer.

test_1111 and test_1112 pass. test_1021's gate test now expects the
null-mode arm. All 227 serial tests and 18 tests/parallel tests at np 2 that
touch the rotated path or _configure_pcmg pass.

The rotated free-slip, solvers, fault-split multigrid and multigrid
preconditioning docs describe the new coarse solve.

Underworld development team with AI support from Claude Code
No glob in scripts/test.sh matched tests/test_1111_*, so
check_test_coverage.py would fail the serial job and the test would never
run. It joins batch 4 beside test_1110 and test_1120. All 383 files are
reachable.

Underworld development team with AI support from Claude Code
The rotated route sets up the velocity multigrid itself, then re-assembles
the rotated coarse levels' operators to turn off inode SOR. Level 0 was
in that loop, so the coarse operator's state moved and the coarse PC was
set up a second time at its first application inside ksp.solve. An eager
coarse solve paid its factorisation twice per solve: the dense SVD did.
PETSc's redundant LU factors lazily, which hid the doubling.

test_1113 puts a counting python PC, delegating to redundant LU, on the
coarse level and asks for one setup per linear solve. On development's
rotated path (the #902 build) it counts 2. It joins CI batch 4.

Underworld development team with AI support from Claude Code
…solve

The inode-SOR loop over the rotated coarse levels now starts at level 1.
Level 0 is the coarse direct solve: it is not smoothed by SOR, and its
direct solve factors its own gathered copy. Re-assembling its operator only
moved its state, which forced a second setup at the first application.
test_1113 passes: 1 setup, against 2 before.

The SVD timings quoted for #901 paid the factorisation twice. The shell's
"207 s" of SVD coarse work at np 4 is about 105 s with one factorisation
(102 s to factor). The docs and the bundle comment now say so. The per-
factorisation figures (64 s at np 1, 102 s at np 4) were one factorisation
and stand.

Underworld development team with AI support from Claude Code
Louis's direction: keep GAMG on the coarse level available for one case
only, a coarsest mesh too large for every rank to factor a gathered copy.
It is never a default and never automatic. set_custom_fmg(...,
coarse_solve="gamg") asks for it, on the rotated route and on the standard
(Stokes velocity block and single-field) route.

custom_mg.GamgCoarseSolve, a python PC on the bottom level:
- runs FGMRES to 1e-2 with GAMG on the Galerkin coarse operator, with a
  redundant LU and a nonzero shift at GAMG's own base;
- attaches the restricted rigid-body modes as the coarse operator's
  near-null space: the translations and rotations, carried into the wall
  frame with the normal rows zeroed on the rotated saddle route, and
  Cartesian on the standard route;
- attaches the verified rotations, where they exist, as an exact null
  space.

The vector route and single-field solvers use GAMG's own near-null space.
It is the bundle's "gamg" variant (the same python-PC key as "null_modes").

Measured in ~/+Simulations/fac_892/coarse_901/README.md (600-cell bottom
levels, np 4): the answer and outer iterations are a gathered LU's, at
4-10x its coarse cost. That cost is GAMG's own 15 to 40 cycles per coarse
solve; its base is at most 6% of it.

test_1114 (np 2 and 4, rotated annulus and standard no-slip box) asserts:
the live coarse PC is GamgCoarseSolve, the answer is the gathered LU's to
1e-8, and the iterations stay within the same solve's with the automatic
coarse solve. The multigrid guide says when to use it and what it costs.

Also from the review: _restrict_null_modes destroys its intermediate Vecs
(they leaked at np >= 2), and the bundle docstring no longer claims every
coarse variant sets the same keys.

All 463 serial tests (49 files) and 40 tests/parallel tests at np 2 (15
files) that touch custom-P multigrid or the rotated path pass.

Underworld development team with AI support from Claude Code
…t (Louis's ruling, A-prime)

The review found that plain redundant LU, with no null-mode handling, gives
the SVD's answer at its iteration counts on every fixture:
- the annulus with 40 to 600 bottom cells;
- the shell;
- the test_1021 gate fixture;
- the thin sheet;
- a faulted annulus with rotated coarse levels, at np 1 and 4.

The inherited rotations leave the coarse operator near-singular, not
singular (lambda_min/lambda_max 2e-5 to 2e-8). Louis ruled for A-prime:

- NullModeCoarseSolve, the "null_modes" bundle variant and the gate on
  the verified-mode count are gone. Both rotated routes use the shared
  coarse="redundant", and add mg_coarse_redundant_pc_factor_shift_type
  nonzero, which acts only on an exact zero pivot. The key is set under the
  velocity PC's prefix before its set-up, because PCREDUNDANT reads its LU's
  options at the coarse set-up and factors at the first application. It is
  dropped afterwards with the bundle's keys.
- The GAMG coarse option, the double-setup fix and test_1113 stay. GAMG
  still takes the verified rotations as its exact null space.

test_1111 (renamed _shifted_lu):
- The annulus and the shell record the redundant coarse solve, the SVD
  path's answer and the measured iterations.
- #306's case, a coarse level that is the fine mesh itself, checks the
  fixture (the restricted rotation is a null vector of the coarse operator,
  ||Aq|| <= 1e-10 ||A||), and that the solve gives the SVD's answer to 1e-7.
  With the shift disabled it fails: 5.1e-7 off. Since the double-setup fix
  stopped re-assembling level 0, the coarse operator keeps its inodes, and
  plain LU does not stop at the zero pivot there. It converges, less
  accurately. With the shift the answer is 7.5e-9 off.

test_1112 (renamed _shifted_lu) and test_1021 assert the redundant coarse
solve. The stale "svd" text is rewritten in rotated_bc, multigrid_options,
test_1021, the rotated free-slip, solvers and fault-split multigrid docs, and
the multigrid guide.

All 464 serial tests (49 files) and 40 tests/parallel tests at np 2 (15
files) that touch custom-P multigrid or the rotated path pass.

Underworld development team with AI support from Claude Code
…st-gated anchors

From the adversarial review of A-prime (b704140):

- The zero-pivot shift is set on the live coarse PC after the multigrid
  set-up (PCREDUNDANT passes it to its inner LU, which factors at the first
  application). There is no options key left to leak on an error path.
  GAMG's base keys are dropped in a finally.
- test_1111, test_1112 and test_1021 read "[NONZERO]" off the live coarse
  KSP's view. Their docstrings now state what the forced-singular case
  shows. After the double-setup fix, plain LU no longer stops at that
  pivot: it converges 5-6e-7 off the SVD answer, inside the solver
  tolerance, so the shift is checked directly.
- The answers and iteration counts recorded on gmsh meshes (test_1111,
  test_1112, test_1114) are gated on the mesh's fingerprint (global cells,
  volume), so a host whose gmsh triangulates differently skips them. The
  live shift, the coarse-solve record and the exact null-vector check hold
  on any host.
- test_1114 asserts recorded bounds and answers instead of comparing GAMG
  with the automatic run, and drops a tautology.
- The solver method set_custom_fmg takes coarse_solve.
- Level 0 keeps the inode kernel at every np. At np 1 the LU factors the
  level operator itself, which lost its inodes before; at np > 1 it factors
  a gathered copy, which always had them. The one measured case that moves
  is the #906 default-normal shell at np 1 (83 -> 112 velocity iterations);
  the sound fixtures are unchanged. Setting the flag without re-assembly does
  not switch the kernel, and re-assembling brings the second setup back.
  The comment and test_1113 say so.
- Figures corrected:
  - lambda_min/lambda_max is 2.0e-5 to 2.4e-8 on the annulus and 2.8e-4 on
    the shell; on the faulted annulus no ratio was measured;
  - GAMG costs 1.7x (annulus) to 4x (shell) a gathered LU at the shipped
    rtol 1e-2, and 10x at 1e-6;
  - the docs name the routes that give GAMG the rigid-body near-null space.
- describe() names the GAMG coarse solve. registered_coarse_solve is
  private. The unused SVD bundle variant is gone.
- One helper owns the velocity save and restore, writing .array; the
  rotation fields are built in one place. The modes are bit-identical to
  the .data construction, and the velocity is restored exactly, at np 1
  and 2 in 2-D and 3-D.

All 464 serial tests (49 files) and 40 tests/parallel tests at np 2 (15
files) that touch custom-P multigrid or the rotated path pass. test_1112 and
test_1114 also pass at np 4.

Underworld development team with AI support from Claude Code
#901)

Louis, 2026-11-05: make everything simpler, fewer special cases, no slow
SVD; on the singular case, keep the shift and have no SVD path.

The advanced multigrid guide, the solvers, rotated free-slip and
split-level subsystem docs now give one rule: every route factors the
coarse level with the shared redundant LU and a nonzero factor shift,
which acts only on a zero pivot, and a null space is fixed on the fine
operator, never by the coarse solve. The rotated route's coarse solve is
no longer described as a difference; that it sets the shift on the live
PC is stated as an implementation detail. The SVD override example is
replaced by taking the shift off, the diagnostic for a genuinely singular
level, and SVD is mentioned only as something a user may set by hand.
The SVD history and its timings move to the changelog.

Depends on the shared bundle carrying the shift on the standard route
too (multigrid_options.geometric_mg_bundle's redundant variant); on this
branch the shift is set on the rotated route only.

Underworld development team with AI support from Claude Code
… every route

The shared bundle's redundant variant set plain LU, so only the rotated route
(which sets the shift on its live coarse PC) carried the NONZERO factor shift.
The bundle now sets mg_coarse_redundant_pc_factor_shift_type = nonzero; the
rotated route keeps setting it on the live PC, since it deletes its keys before
that PC reads them.

- test_1021 reads the shift off the live coarse KSP on the native, standard
  custom-P and rotated routes and asserts all three carry it.
- test_1014 checks the key in the standard route's default bundle.
- On a nonsingular coarse level the shift changes nothing: native and standard
  custom-P FMG on the annulus give bitwise-identical velocity and pressure and
  the same iterations with and without it.
- The advanced guide no longer offers taking the shift off as a test for a
  singular level: test_1111's singular level factored without it and converged
  5e-7 off. The admission tolerance is stated as the code's relative 1e-8.

Underworld development team with AI support from Claude Code
Conflicts, each resolved by keeping both sides:
- scripts/test.sh: development's #907/#918/#920/#936 batch lines beside this
  branch's test_1111/test_1113.
- custom_mg.py: the standard velocity route takes development's FAC key
  return and smoother set-up (#892) with this branch's coarse variant and
  GAMG near-null space; set_custom_fmg's docstring carries development's
  fac_zone (#921) and verbose (#918) text and this branch's coarse_solve.
- rotated_bc.py: both rotated installs take development's level_patch_rows,
  _set_up_fac_smoothers and patch summary with this branch's null modes and
  live zero-pivot shift.

Underworld development team with AI support from Claude Code
geometric_mg_bundle(coarse="svd") raised the generic "coarse must be one of
('redundant', 'gamg')". It now says the variant was removed (#901) and names the
replacement: the default coarse="redundant", redundant LU with a zero-pivot
shift. A dense SVD can still be set through the PETSc option.

- test_1021 pins the error, and checks that a script setting
  fieldsplit_velocity_mg_coarse_pc_type=svd itself still gets an SVD on the
  standard route's live coarse PC.
- The CHANGELOG entry says the shift now applies on the native and standard
  custom-P routes too (bitwise-identical v and p on a nonsingular level), that
  the svd variant is removed rather than deprecated, and that scripts setting
  the PETSc option are unaffected.

Underworld development team with AI support from Claude Code
…on the error path, exact shift wording

- The rotated route's live-PC writer of the zero-pivot shift
  (_shift_coarse_zero_pivot) goes. PCSetUp_Redundant reads the bundle's
  shift key inside vel_pc.setUp(), before the keys are dropped, so the bundle
  is the only writer on every route (round 3 probe: with the helper a no-op the
  rotated coarse LU still shows [NONZERO]). The "deletes its keys before the
  coarse PC reads them" reason is corrected wherever it was given.
- A rotated set-up that raised after the multigrid set-up left its bundle and
  FAC keys in the global options database (11 on test_1021's fixture). Both
  rotated installs now drop every key under their per-solve prefix in the
  finally (_drop_options_under). test_1021 pins it, and fails with the sweep
  disabled.
- The shift is described as PETSc applies it: NONZERO acts on a pivot below the
  zero-pivot tolerance (relative to the row's off-diagonals) and refactors with
  a small diagonal shift. PETSc's own default coarse LU carries INBLOCKS
  (PCMGSetLevels_MG, not PCSetUp_MG); development's native and standard routes
  kept it at np > 1 and had none at np 1. The guide, solvers.md,
  rotated-freeslip.md, the CHANGELOG and the docstrings say so.
- The guide no longer says the coarse solve "cannot change the answer" (it is
  set to the solver's tolerance) and states the admission test's unassembled
  operator case; the CHANGELOG no longer says "never singular".
- The coarse="svd" error says the PETSc-option SVD works on the standard path
  only; the user-svd test's docstring says what it guards (key ownership on
  the custom-P standard route).

Underworld development team with AI support from Claude Code
…es; the bitwise identity was measured, not asserted

Underworld development team with AI support from Claude Code
…d LU (#901)

test_1114[standard] failed at np 2 and 4 in CI's environment on kaiju: the
GAMG-coarse v_rms was 1.24e-8 and 1.53e-8 from the gathered-LU value recorded on
another host, against a 1e-8 bound, at solver tolerance 1e-6. The gathered LU
itself moves that much with the partition and the host (7.9e-9 at np 1 on
wombat), while GAMG and LU in one run agree to 4.5e-11 or better (np 1, 2, 4).

The test now solves the gathered LU on the same mesh and partition and asserts
GAMG matches it to 1e-9, on any mesh. The recorded answer stays as a baseline at
1e-7, still gated on the mesh fingerprint.

Underworld development team with AI support from Claude Code
…t folds it (#946)

At cell size 0.5, gmsh can put all four vertices of a coarse cell on the shell's
inner wall. Refinement snaps the new edge midpoints outward onto the sphere, so
that cell's children fold inside out (#946).

- Wombat's mesh at 0.5 had 3 folded cells and converged in 18 velocity
  iterations.
- Kaiju's mesh, the same gmsh on Linux, ran every velocity sub-solve to the
  200 cap. test_1111[shell] did not finish in an hour.
- A gmsh seed that folds the same way on wombat fails with the SVD coarse solve
  and with the default normal too, so the coarse solve and the normal are not
  the cause.

At 0.3 the wombat shell has no coarse cell on a wall and no folded cell. It
solves in 5 velocity / 17 Schur iterations. The recorded answer there is the
shifted LU's; on the old shell, LU and SVD agreed to 1e-10. A shell that
refinement folded, by the signed volume in DMPlex order (the MMPDE fold guard's
convention), now skips the case instead of running it.

Underworld development team with AI support from Claude Code
test_1114:
- The same-run GAMG-against-LU bound is 1e-8, not 1e-9. The two solves agreed
  to 4.5e-11 because both stop well past their tolerance. A Schur solve that
  stops one iteration earlier moves them 2.4e-9 apart (rotated route, tolerance
  3e-6), with identical counts.
- The LU run's mesh is checked against the GAMG run's by fingerprint, so a
  different mesh fails as that rather than as a v_rms miss.

test_1111:
- The fold gate uses the library's `graph._tet_cells` and `_owned_cell_mask`.
  A rank with no cells counts zero instead of raising, and overlap cells are
  not counted twice.
- A watchdog: a solve that does not converge stays inside one KSPSolve, which
  holds the GIL, so pytest-timeout cannot stop it. faulthandler's watchdog
  does not need the GIL. After 10 minutes it prints every thread's stack to
  the real stderr and exits; the captured stderr would be lost on exit.
- The shell's recorded answer is the SVD path's too. The dense SVD coarse
  solve, set by hand on the 0.3 shell, gave the shifted LU's v_rms and
  iterations, in 604 s against 17 s.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 5, 2026 11:30
@lmoresi

lmoresi commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Adversarial review, round 1 (bugfix/rotated-fmg-coarse-solve at 0502a9f)

Reviewed by a separate agent against docs/developer/guides/adversarial-review.md, at np <= 4. The branch then had a null-mode coarse solve: the rotations restricted to the bottom level, one unknown pinned per mode, the modes projected out, and PETSc's redundant LU.

Findings and what was done

  1. CI did not reach test_1111. No glob in scripts/test.sh matched it.
    Resolution: it is in batch 4 (018546d).
  2. Plain redundant LU already gave the SVD's answer and iterations everywhere. That held on both annuli, the shell, the test_1021 gate fixture and the thin sheet, and, measured after the review, on a faulted annulus with rotated coarse levels at np 1 and 4. The coarse operators are near-singular, not singular: lambda_min/lambda_max is 2.0e-5 to 2.4e-8 on the annulus and 2.8e-4 on the shell.
    Resolution: Louis ruled A-prime. The null-mode class, its bundle variant and the gate on the mode count are gone. The rotated coarse solve is the shared redundant LU with a nonzero factor shift, which acts only on an exact zero pivot (b704140).
    • On a forced-singular case (a coarse level that is the fine mesh itself, so the rotation is an exact null vector), plain LU failed with reason -11 before the double-setup fix, and the shift rescued it.
  3. The tests could not catch a broken null-mode solve. Neither removing the pins nor dropping the projections failed them.
    Resolution: the class is gone. The tests now assert the shifted LU's contract (round 2 sharpened them).
  4. The pinning argument was exact only for an exactly singular operator, and these operators are not.
    Resolution: moot under A-prime.
  5. The vector-route arm had no test, and the svd fallback arm could not be reached.
    Resolution: both are gone with the gate.
  6. _restrict_null_modes leaked its intermediate Vecs at np >= 2.
    Resolution: fixed (9db23e5); the function now serves only the GAMG option.
  7. The pins were chosen on an exact tie.
    Resolution: moot under A-prime.
  8. Stale "svd" text.
    Resolution: rewritten (b704140).
  9. On development, the rotated route set the coarse level up twice per solve. The inode-SOR loop re-assembled level 0's operator, so the coarse PC was set up again at its first application, and an SVD was factored twice. Every SVD total quoted for Rotated free-slip annulus: the FMG coarse level is a dense SVD on one rank, so more ranks make the solve slower (caps kaiju at np 16) #901 included the second factorisation.
    Resolution: the loop starts at level 1. test_1113 counts the setups: 2 on development, 1 now (5a30a08). The quoted figures are corrected.

Also on the branch at Louis's request: GAMG as the coarse solve, only when asked for (set_custom_fmg(..., coarse_solve="gamg"), 9db23e5, test_1114).

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.

@lmoresi

lmoresi commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Adversarial review, round 2 (bugfix/rotated-fmg-coarse-solve at b704140, A-prime)

Reviewed by a separate agent, at np <= 4.

Findings and what was done

  1. Leaving level 0's inodes on changes the coarse factorisation at np 1. At np 1 PCREDUNDANT factors the level operator itself, so the flag picks the LU kernel. Development had the plain kernel there and the inode kernel at np > 1, where it factors a gathered copy. On the near-singular shell with the default normal (Rotated free slip on a spherical shell with the default normal: the rotations are rejected and the answer carries a rigid rotation #906), velocity iterations at np 1 go 83 → 112. They stay 88 at np 2 either way, and the sound fixtures are unchanged (4/7, 18/18).
    Resolution: kept. The inode kernel is now used at every np. Setting the flag without re-assembly does not switch the kernel (measured: still 112), and re-assembling brings back the second setup. The comment and test_1113's docstring now say what happens at np 1 and at np > 1.
  2. The forced-singular test did not show the failure the shift exists for. After the double-setup fix, plain LU no longer stops at the pivot there: it converges 5-6e-7 off the SVD answer, inside the solver tolerance. The test caught the shift's removal only through a bound below the solver tolerance, and nothing read the shift off the live PC.
    Resolution: test_1111, test_1112 and test_1021 read [NONZERO] from the live coarse KSP's view. test_1111's docstring states what the singular case does and does not show.
  3. test_1114 compared GAMG's iterations with the automatic run's (Charter §8), and one assertion was a tautology.
    Resolution: hard bounds (the largest counts measured at np 2 and 4) and recorded answers. The automatic run and the tautology are gone.
  4. coarse_solve was only on the module function.
    Resolution: the solver method set_custom_fmg takes it too.
  5. The shift key leaked into the options database on the error path.
    Resolution: no key now. The shift is set on the live coarse PC after the set-up, and PCREDUNDANT passes it to its inner LU. GAMG's base keys are dropped in a finally.
  6. Figures in the docs and comments were wrong.
    • The lambda ratios now read 2.0e-5 to 2.4e-8 on the annulus and 2.8e-4 on the shell; on the faulted annulus, plain LU's agreement is stated without a ratio, which was not measured.
    • GAMG's cost now reads 1.7x (annulus, 3.9 cycles) to 4x (shell, 14.6 cycles) at the shipped rtol 1e-2, and 10x and 38 cycles at 1e-6. The earlier figures had been measured against the deleted pinned LU.
    • The docs say which routes give GAMG the rigid-body near-null space.
    • The two stale comments are corrected.
  7. describe() reported the GAMG variant as "coarse python".
    Resolution: it says gamg.
  8. Style.
    • One helper now owns the save and restore of the velocity, and the rotation fields are built in one place.
    • The helper writes .array, not the flat .data. The modes are bit-identical to the .data construction, and the velocity is restored exactly, at np 1 and 2 in 2-D and 3-D.
    • registered_coarse_solve is private.
    • The unused SVD bundle variant is gone.
  9. The answers and iteration counts were recorded on gmsh meshes on macOS.
    Resolution: test_1111, test_1112 and test_1114 gate those assertions on the mesh's fingerprint (global cells, volume) and skip on a host whose gmsh builds a different mesh. The live shift, the coarse-solve record and the exact null-vector check hold on any host.

Issue verdicts

Checked and sound

  • The shift is read at the coarse set-up, and the factorisation is lazy. The view shows [NONZERO] at np 1 and 2 and after a second solve with a refreshed operator. No rotfs/rotvec keys are left on the success path.
  • GAMG:
    • its base is redundant LU with [NONZERO] on both routes at np 2;
    • its answers match the gathered LU's;
    • it survives an is_setup=False rebuild;
    • every reduction is collective in the same order on every rank.
  • A mesh-owned tail with no set_custom_fmg gets the redundant LU.
  • No doc still says the rotated coarse solve is SVD or null-mode.

@lmoresi

lmoresi commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Adversarial review, round 3

Covers 42e5565..9aa89dc: the coarse-solve docs, the shift in the shared bundle (4af4473), the merge of development (836f3e1), and the svd-removal error (9aa89dc). All runs were serial, np 1. The fixes are in af9d3c3.

Findings, and what was done

  • MEDIUM: the rotated route wrote the zero-pivot shift a second time, and the reason given for it was false.
    • The stated reason was that the route deletes its keys before the coarse PC reads them. In fact PCSetUp_Redundant reads the bundle's shift key inside vel_pc.setUp(), and the keys are dropped only after that.
    • Probe evidence: with the live-PC helper made a no-op, the rotated coarse LU still shows [NONZERO]. It loses it only when the bundle key goes too.
    • No test depended on the helper.
    • Fixed: _shift_coarse_zero_pivot is deleted, so the bundle is the only writer. The comments and four doc statements giving the false reason are corrected.
  • LOW: a rotated set-up that raised after the multigrid set-up left its options in the global database.
    • 11 keys were left under rotfs1_fieldsplit_vel_, the shift among them; the other 10 leak on development too.
    • Fixed: both rotated installs drop every key under their per-solve prefix in the finally.
    • test_1021 pins it: the new test fails with the sweep disabled.
  • LOW: "acts only on an (exact) zero pivot" is not what PETSc does. NONZERO fires when a pivot is at most 2.2e-14 times the sum of the magnitudes of the row's off-diagonal entries, then refactors with the whole diagonal shifted. Fixed: the docs and docstrings say this. On the UW3 fixtures the answers stay bitwise identical.
  • LOW: PETSc's own default coarse LU already carries a shift.
    • PCMGSetLevels_MG sets MAT_SHIFT_INBLOCKS. So, by source reading, development's native and standard routes ran INBLOCKS at np > 1 and no shift at np 1.
    • The guide said "the shift is the only addition" and named PCSetUp_MG.
    • Fixed: the guide, solvers.md and the CHANGELOG say this. The np > 1 view on development is with kaiju to confirm.
  • LOW: some doc statements went further than the evidence.
    • "No per-route difference": the rotated route takes no petsc_options override.
    • "Cannot change the answer": test_1111's singular level is 5e-7 off without the shift.
    • "Never singular": test_1111's constructed level is singular.
    • The 1e-8 admission test has an unassembled-operator case the docs did not mention.
    • Fixed: all four are reworded.
  • LOW: the svd error said SVD can be set "under the block's prefix". That does not hold on the rotated route. Fixed: the message says standard path only.
  • LOW: the docstring of the user-svd test misdescribed it. It is the only test that the custom-P standard route respects keys it did not write: it fails with owned=None. Fixed: the docstring says so.

Attacks that failed

  • The tests discriminate. Removing the bundle key fails test_1021's three-route test and test_1014. Reverting the error text fails the svd test. With neither shift writer, test_1111's singular case fails.
  • The shift_type=none override works on the native and standard routes. v and p are bitwise identical at equal iterations.
  • The merge resolution holds. The call order matches development in both rotated installs and the standard install. test_1111, test_1113, test_1115 and test_1134 pass.
  • The linear-reuse cache is unaffected. CI reach is complete: check_test_coverage.py passes.

Issues in this territory

Issue Verdict
#901 Partly fixed; not labelled fixed. The exact-removal, SVD-answer and macOS criteria are met. The scaling criterion (coarse cost per Newton iteration flat or falling from np 16 to 52) is not: 0.32 to 0.85 s, 6-9x lower than before, 3-4 % of a step.
#906 Still live. With the default normal, the shell rejects all three rotations at the tip.
#910 Still live. On its fixture the failure is not seen at the tip (0 of 20 runs, against 4 of 12 with the SVD coarse solve forced). It is not marked fixed without a regression test.
#877 Still live. A 3-D units model raises in _rigid_rotation_fields. The GAMG coarse path reaches the same function.
#911, #897 Still live; not touched.
#907 Already fixed in development.

Re-check of af9d3c3, serial

  • The prefix sweep drops nothing still in use.
    • The prefixes end in _ and come from a counter that only rises, so rotfs10_ never matches rotfs1_, and no prefix is reused.
    • A solver whose workspace was built under rotfs1_ was re-solved warm after ten other rotated solves, and again after an operator refresh. It converged, kept [NONZERO] and left 0 keys.
    • The GAMG coarse path also converged on cold, warm and refreshed solves, with 0 keys left.
  • Nothing remains of the removed shift reasons. _shift_coarse_zero_pivot, "deletes its keys" and "exact zero pivot" are gone from src, docs and tests.
  • Two wording points, fixed in bca8434: the tolerance sums the magnitudes of the off-diagonals, and the bitwise identity was measured on test_1021's fixtures, not asserted by a test.
  • The shift handles a singular level. On singular weighted Laplacians, plain LU fails every time (reason -11). NONZERO factors to a residual of about 1e-14.

Parallel checks (kaiju)

  • The shift view and bitwise identity at np 2 and 4 on af9d3c3, and [INBLOCKS] on development.
  • test_1112 at np 2.
  • The serial rotated and custom-MG set, and parallel test_1112/1114/1116/1064.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Adversarial review, round 4: bca8434..4babb3a (test-only)

Round 4 covers the two commits after round 3, both test-only:

The fixes are in e15f5fa.

Findings

  1. test_1114: the same-run bound of 1e-9 was luck, not a property. Medium. Fixed: the bound is now 1e-8.

    • At tolerance 1e-6 the GAMG and LU answers agree to 2.5e-11 (standard route) and 2.5e-12 (rotated route), serial. Both solves stop well past their tolerance.
    • At tolerance 3e-6 the rotated route's Schur solve stops one iteration earlier (6, not 7), and the two answers are 2.4e-9 apart, with identical counts.
    • So a partition that stops one iteration earlier would fail spuriously.
    • The comparison exists to catch a coarse solve that changes the problem being solved, and that would miss by orders of magnitude more than 1e-8.
  2. test_1111 had no timeout. Medium. Fixed: a faulthandler watchdog.

    • A non-converging solve stays inside one KSPSolve, and petsc4py holds the GIL through it (KSP.pyx, KSPSolve without nogil). Neither of pytest-timeout's methods can stop it there, signal or thread.
    • faulthandler.dump_traceback_later(600, exit=True) needs no GIL; we checked it against a C loop that holds the GIL.
    • Its dump goes to a copy of the real stderr, taken through the capture manager. Under pytest's default fd capture the dump was otherwise lost on exit; we checked that too.
  3. The fold gate raised on a rank with no cells and counted overlap cells twice. Low; the file is serial-only. Fixed: it uses graph._tet_cells, which returns None on an empty rank, and graph._owned_cell_mask.

  4. The shell's recorded answer was the shifted LU's own value, not an independent reference. Low. Fixed, by measurement: on the 0.3 shell, a dense SVD coarse solve set by hand gives v_rms 1.8237602846 at 5 / 17 iterations, the LU's value to every printed digit. It took 604 s against 17 s.

  5. test_1114 regenerated the mesh for the LU run without checking it was the same mesh. Low. Fixed: mesh_fingerprint(lu_mesh) == mesh_fingerprint(mesh) is asserted before the LU solve.

Attacks that failed

  • One orientation sign on a healthy mesh. In DMPlex closure order every cell of a healthy mesh is negative: 0.3 coarse 1099/1099, 0.3 fine 8792/8792, 0.5 coarse 614/614. The 0.5 fine level has 3 positive cells, centred at r ≈ 0.478, inside the inner sphere.
  • The gate separates folded from healthy meshes. On six 0.5 meshes (gmsh seeds 2, 3, 5, 6, 7 and the default), it is nonzero on exactly the three with a coarse cell whose four vertices lie on the inner wall, and zero on the others. Seed 2 converged and is still skipped, which is conservative.
  • A 2-D analogue. Points on a circle form a convex polygon, so a triangle with all three vertices on the inner circle lies inside the hole. The annulus cannot fold this way.
  • Leakage between the two test_1114 solves. Variable names and option prefixes are unique per solve, and GamgCoarseSolve drops its keys in a finally.
  • CI reach. test_1111 is in batch 4 (scripts/test.sh:370); test_1114 is in the parallel enumeration. Both are level_2 / tier_b.
  • The 1e-7 baseline. The largest drift measured is 1.53e-8. A Schur solve one iteration shorter moves the standard route by 2.5e-8.

Issue verdicts

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit eddab70 into development Nov 5, 2026
8 checks passed
lmoresi added a commit that referenced this pull request Nov 5, 2026
Underworld development team with AI support from Claude Code
gthyagi pushed a commit to gthyagi/underworld3 that referenced this pull request Nov 10, 2026
… a missing label moves nothing (underworldcode#903)

Parked work in progress: the serial tests pass, and the parallel test, the PR and the
review are still to come (see the end).

gmsh meshes a sphere in a parameter plane. Near a pole it can join four neighbouring
wall nodes along the long diagonal, and a structured surface splits every
quadrilateral the same way, so the wall creases toward the centre. On an inner or
internal wall, the pocket under the crease is meshed as one cell with all four
vertices on the sphere. The refinement callback then snaps the crease midpoint
outward past the unlabelled midpoint of the other diagonal and folds the children.

- meshing/_sphere_surface.py: `_flip_creased_sphere_edges` runs Lawson flips to the
  convex hull on every gmsh surface whose nodes lie on a sphere about the centre,
  between generate(2) and generate(3). It is called by SphericalShell,
  SphericalShellInternalBoundary, SegmentofSphere, CubedSphere, the segmented
  shell and ball, and both regional boxes.
- Mesh._build_refined_hierarchy applies every refinement callback through
  `_apply_refinement_callback`. Of each cell the callback inverted, the vertex it
  moved furthest goes back where refinement placed it, until none is inverted, with
  a warning. Vertex flags are shared over the coordinate DM, so every copy agrees.
- petsc_dm_find_labeled_points_local returns an empty array for a missing label
  instead of [0] (underworldcode#903). A refined ball had a NaN vertex.

Tests: tests/test_0001_sphere_refinement_folds.py has 7 cases. 6 fail on
development 1f87430 (creases, folded cells, no warning, NaN) and all pass here.
tests/parallel/test_0001_..._parallel.py has not run yet (np 2 and 4 go to kaiju).

Left to do:
- Merge origin/development (underworldcode#947), and re-record test_1111's shell, whose mesh
  changes with the flips.
- Run the 24 test files that build spherical meshes, on kaiju.
- Measure the guard's cost on large meshes.
- The segmented shell and ball still warn, because their Upper (and Lower) labels
  hold vertices off the wall: Upper reaches r 0.297 on the ball and r 0.72 on the
  shell. That is a labelling bug the guard now exposes.
- CubedSphere(numElements=3) holds one vertex back: a crease on a patch-boundary
  curve, which a per-surface flip cannot reach.
- TODO(BUG) markers: the internal-boundary shell's radius tolerance, and its
  unsnapped Internal surface.

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