Repository navigation
A Stokes adjoint keeps the custom velocity hierarchy and refreshes it from J^T - #1023
Conversation
The forward solve installs the hierarchy on the velocity block just before its SNES solve (auto_inject_custom_mg). adjoint_solve switches the tangent and solves with the same KSP; the switch resets the preconditioner, and the fieldsplit rebuilt the velocity block as a one-level multigrid with no coarse correction and no FAC smoother: 23 velocity iterations at np 1 and 29 at np 2 against the forward's 4 on a split fault box (hyperion). adjoint_solve now runs the same injection after setting the operators, on the kernel-transposed and the matrix-transposed routes alike; the rotated route builds its own preconditioner and is unchanged. test_1145, tier B: on the fault box the adjoint's velocity block is the forward's three-level multigrid with its FAC smoother, in at most two more iterations than the forward's. On development the block is a one-level mg. The adjoint test set (9 files) passes, 62 tests. Underworld development team with AI support from Claude Code
…stom-mg-1016 Underworld development team with AI support from Claude Code # Conflicts: # scripts/test.sh
…view, B1) The first fix ran the velocity block's injection after adjoint_solve had assembled the tangent at the forward state. The install assembles at a zero vector to reach the fieldsplit, so a nonlinear adjoint solved with the tangent at u = 0 and reported converged: a power-law adjoint 48 % off in the velocity on the kernel route, an eta(v_x) one 4 %, against 1e-10 for a dense solve of K^T mu = b (wombat; hyperion measured 12 to 75 % at np 1 and 2). The injection now comes right after the tangent switch and before the state's assembly, on every route but the rotated one; the assembled J then is the state's, and the velocity levels re-Galerkin from it at the KSP's set-up. Power law under Newton and under a Picard forward, and eta(v_x) on the matrix route: all within 2e-10 of the dense reference. test_1145 gains those three cases against a dense reference (the consistent tangent at the state, the pressure constant removed where it is free). With the tangent put back at u = 0 at run time, after the state's assembly as the first fix left it, all three fail; the linear case could not see it, since J(0) = K there. The 7 test files that call adjoint_solve pass (60). Left as it is on development: the matrix route's velocity levels hold J's block, not J^T's, so it takes 9 to 11 velocity iterations against the forward's 5 to 6 (the answer is right). A forced PC set-up and a matrix-state bump do not refresh the block; filed separately. Underworld development team with AI support from Claude Code
…1021, #1016 review) _solve_transposed handed the KSP new matrices for J^T and P^T. The outer KSP took them, but the fieldsplit's velocity block kept its last set-up: J's velocity block on a Newton forward (9 to 11 velocity iterations against 5 to 6 on J^T, #1021, also on development), and after a Picard forward the injection's assembly at u = 0, which ran the velocity solve to its cap of 200 (hyperion's re-check; 129 here). Where the pattern is symmetric, as an assembled finite-element operator's is (compared by CSR across ranks), the transposed values now go into J's and P's own storage, with the null spaces swapped, and are copied back after the solve; the preconditioner chain refreshes from them as at a Newton step. During the solve the velocity block equals J^T's to round-off, and the matrix route takes 5 to 6 velocity iterations, Newton or Picard forward, with the multiplier within 1.5e-10 of a dense solve. A non-symmetric pattern takes the new matrices as before. test_1145: a matrix-route Picard case, and the adjoint's velocity iterations bounded by the forward's plus two; with the old swap put back, both matrix-route cases fail (9 and 131 against 7). The dense-reference cases skip at np > 1, where the reference indexes global numbering into one rank's rows (hyperion). The earlier note that copying J^T in hit PETSc error 56 was a probe's own fault: Mat.convert("dense") with no target converts in place, and the probe had turned the solver's J dense. The test's reference now converts a copy. Underworld development team with AI support from Claude Code
…stom-mg-1016 Underworld development team with AI support from Claude Code # Conflicts: # scripts/test.sh
Review record for #1023 (#1016, #1021)On 884d66e, the head: ready to merge, with no open merge condition. It took three rounds; each re-check is a section below.
884d66e merges development 22cf4e6 (#1018) into c6e0cf6. The branch's own diff in
Left, as the PR body says: N1 (null-space slots, latent), L1 (intended), and L2 (#1020's pattern). Paths under Adversarial review: bugfix/stokes-adjoint-custom-mg-1016Tip reviewed: 98d0c8b (built as
Housekeeping:
Method:
VerdictNot ready to merge. There is a silent-wrong-answer regression (B1). The fix does restore the hierarchy. On the issue's case, the adjoint's velocity block is 3 levels with the FAC composite, at 4 velocity iterations against base's 23 (np 1) and 29 (np 2). But the injection rebuilds the velocity block through
It converges (reason 2) and returns a wrong multiplier, with no warning:
On base the Newton cases are correct to 1e-7. test_1145 uses a linear viscosity, where J(0) = K, so it cannot see this. FindingsB1 (blocker). A nonlinear adjoint on the kernel route, or any adjoint after a Picard forward, is solved with the tangent at u = 0Mechanism:
Measured, as relative error against the dense reference. "Fine op vs K(0)ᵀ" is the live velocity operator compared with the tangent assembled at zero.
P1 (pre-existing, on base too; it becomes worse with any B1 fix on the matrix route). On the matrix route the coarse operators are not formed from JᵀOn
L1 (low). Behaviour changes for solvers without
|
| route, mg | base, per cycle | branch, per cycle | note |
|---|---|---|---|
| kern, fmg | 38 | 60 | linear in both |
| kern, none | 32 | 46 | linear in both |
| mat, fmg / none | flat at 36 / 30 | flat at 36 / 30 |
- The extra entries come from the per-adjoint reinstall: transfers rebuilt by
h.build, plus a new PCMG. - The options database is flat: 49 keys (fmg) and 46 (none) on both builds.
- The growth exists on base too. The branch adds about 22 entries per cycle; they are deferred destroys, not lost objects
[read]. - Cost: every kernel-route adjoint rebuilds the transfers and does one extra Jacobian assembly. The
#622comment ininject_custom_mgmeasured that pair as most of a warm solve on a large mesh[read].
Which route each test and case runs (instrumented)
| case / test | by_kernels |
_solve_transposed calls |
injection installs |
|---|---|---|---|
test_1145 fixture = p2_adjoint.py = probe kern lin fmg |
True (kernel) | 0 | 1 |
probe mat * (natural BC on Right) |
False (matrix) | 1 | 0 (lin, pl, nv); 1 (pic) |
| test_0022_rotated_adjoint, test_1131 | rotated route, so the changed branch is not entered [read] |
— | — |
The kernel route is chosen whenever there are no natural_bcs and no rotated free-slip (_adjoint_by_kernels, petsc_generic_snes_solvers.pyx:2683) [read].
Hierarchy on the other (matrix) route, branch, np 1 and np 2: 3 levels with the composite on the finest, for lin, pl, nv and pic. On base the matrix route already kept 3 levels and the composite for lin, pl and nv, because nothing rebuilt the SNES. Base dropped to 1 level only after a Picard forward (37 iterations at np 1, 51 at np 2).
Iteration counts (attack 3)
| np 1, adjoint | np 1, forward after | np 2, adjoint | np 2, forward after | |
|---|---|---|---|---|
p2_adjoint.py, base |
1 level, 23 | 3 levels, 4 | 1 level, 29 | 3 levels, 4 |
p2_adjoint.py, branch |
3 levels, 4 | 3 levels, 4 | 3 levels, 4 | 3 levels, 4 |
- In both cases the forward itself takes 3 levels with the composite, at 4 iterations.
- Sequence F A A F A F A at np 2 (
probe_seq.py):
| route, mg | branch | base |
|---|---|---|
| kern, fmg | 3 levels, 4 iterations at every step | adjoints 1 level, 28 iterations |
| mat, fmg | 4 then 5, identical to base | 4 then 5 |
- μ is bit-identical across the repeated adjoints, with drift 0.0.
Tests (attack 5)
| file | branch np 1 | branch np 2 | base np 2 |
|---|---|---|---|
| test_1145 | 1 passed | 1 passed | 1 failed (mg, 1 level) |
| test_0018_adjoint_support_record | 14 passed | 1 failed, 13 passed | same 1 failed, 13 passed |
| test_0019_adjoint_solve | 8 passed | 1 failed, 7 passed | same |
| test_0021_adjoint_misfit_through_gradient | 1 passed | 1 passed | 1 passed |
| test_0022_adjoint_operator_from_kernels | 2 passed | 2 passed | 2 passed |
| test_0022_rotated_adjoint | no test done in 56 min, stopped by PID (base: none done in 42 min, stopped) | hang, killed at 600 s | same hang |
| test_1131_rotated_nullspace_from_mesh | 24 passed (452 s) | hang, killed at 600 s | same hang |
| test_1017_custom_mg_stokes | 5 passed | 5 passed | 5 passed |
| test_1017_nested_native_and_fac | 6 passed | rc 142 after the 1st file's output | same rc 142 |
| parallel/test_1017_custom_mg_parallel_mpi | — | 6 passed | 6 passed |
Notes on the table:
- test_1145 fails alone on base at np 1, as it should:
("the adjoint's velocity block is not the forward's hierarchy", 'mg', 1). - 7 test files call
adjoint_solveon the branch (6 on base), not 9. With the two serial test_1017 files the count is 9, so that is what we ran. - Every np 2 failure in the table is the same on base:
- test_0018
test_the_text_transcript_notes_a_refusal_once_per_change: a per-ranktmp_pathfile is missing. - test_0019
test_poisson_gradient_in_the_diffusivity_matches_finite_differences: a zero-size max on one rank. - test_0022_rotated and test_1131 hang in a collective in
_solve_rotated_iterative, which the supervisor diagnosed. - test_1017_nested exits with rc 142.
- test_0018
- These are serial files that are not written for 2 ranks. Only test_1145 and parallel/test_1017 are meaningful at np 2, and the failures above are not regressions.
- test_1145 at np 2 is meaningful in that the mesh is distributed and the base still fails. But it remains linear-only, so it cannot see B1.
- Mutation: with
auto_inject_custom_mga no-op insideadjoint_solve(mutplug.py), test_1145 fails at np 1 with the same message as base. The test does guard the fix. - None of the adjoint tests can catch B1:
- test_0019's nonlinear Stokes FD tests use a mesh with no custom hierarchy;
- test_1145 is linear.
Attacks that failed
- Route coverage. The hierarchy (3 levels and the composite) is restored on both routes, at np 1 and np 2.
- Linear correctness. Linear viscosity is correct on both routes and both rank counts: 1.5e-7 to 3.1e-7 at tol 1e-6, and 2.5e-11 to 4.3e-11 at tol 1e-10. That is the same as base.
- Non-symmetric block, matrix route. With a non-symmetric velocity block (‖A−Aᵀ‖/‖A‖ = 5.7e-3 to 5.9e-3 for nv), the matrix route is correct (2.5e-7, and 2.4e-11 at tol 1e-10).
- The power-law tangent's velocity block is symmetric (6e-17), as it must be for η(Einv2). It tests the J(0) defect, not the asymmetry.
- The strain-component law η(E01) did not converge its forward solve within 600 s, so it was dropped.
- Repeat adjoints. A second adjoint and interleaved F/A sequences are stable, with identical μ.
- Options DB. No growth.
- Forward after the adjoint. Unchanged: 3 levels, 4 iterations.
- Rotated route. The code path is unchanged
[read]. test_1131 passes serially (24 passed). test_0022_rotated_adjoint finished no test in 56 min on the branch, nor in 42 min on base, so it was stopped on both. test.sh gives it its own shard for 27 to 55 min, so this is consistent with its known cost, not a regression.
Merge conditions
- Fix B1. The adjoint must solve with the tangent at the forward state after the injection: inject before
computeJacobian, or reassemble after it.- Add a nonlinear case to test_1145: a Newton viscosity on the kernel route and a Picard forward on the matrix route.
- Compare μ against an LU or dense reference, or against the same fixture with the custom hierarchy removed.
- A mutation that leaves J at zero must fail it.
- Handle the matrix route's coarse levels with the fix (P1). Otherwise mat-route Picard adjoints go from 8 to 200 velocity iterations once B1 is fixed. Check the level operators against PᵀJᵀP.
- Optional:
- say in the docstring or CHANGELOG that the auto-resolved hierarchy now also reaches the adjoint (L1);
- consider caching so the transfers are not rebuilt on every kernel-route adjoint (L2).
Could not test
- The strain-component law η = 1/(1+4 E01²): its Newton forward solve made no progress in 600 s on both builds, so we killed it by PID. The non-symmetric case used η(v_x) instead.
- 3-D, and np > 2.
- A real rebuild with a fix. B1's fix was only simulated in-process, by copying J back after the install.
- The root cause of P1: why the coarse levels are not re-Galerkined after
setOperators(Jt). We measured it but did not trace it in PETSc[read]. - The rotated adjoint at np 2 through the serial files, which hang on both builds. We did not build a parallel rotated adjoint probe.
- Wall-time cost of the per-adjoint reinstall on a large mesh.
- test_0022_rotated_adjoint to completion on either build: stopped at 56 min (branch) and 42 min (base), before its first test reported.
Re-check of 09d82e1
Tip: 09d82e1 (wt-1016, rebuilt). It is c244304, the merge of development 80d3ebe with #1014, plus one commit: auto_inject_custom_mg now runs right after the tangent switch and before the state's Jacobian is assembled, on every non-rotated route. It does not copy J back.
Base: development 80d3ebe (wt-dev80).
Probes and logs are in runs/2026-11-06/a1016/review2/: the same probe_adj.py and probe_seq.py as before, plus probe_rot.py, mutj0.py, mutold.py and logs/. Both worktrees are clean, and test files ran from a git archive of 09d82e1. We used at most 23 cores.
Verdict
Merge after one change. B1 is fixed.
- Every non-rotated case is now correct against the dense reference, at np 1 and np 2: Newton, Picard, the non-symmetric η(v_x), and the fault mesh without
set_custom_fmg. - The kernel route's operators are now right. The live fine operator is K(state)ᵀ, and the coarse operators are PᵀKᵀP to 6e-17.
- One case remains (P1′). After a Picard forward on the matrix route the multigrid is built on the tangent at u = 0. The velocity block then runs to its 200-iteration cap and reports DIVERGED_ITS on every rank count we ran. The outer solve still converges and μ is right to 2.8e-7, but 80d3ebe takes 37 (np 1) or 51 (np 2) velocity iterations on the same case, at one level.
- This is the real fix, not our earlier simulation. The author's "matrix route = development" holds for the matrix route after a Newton or linear forward. There the injection is a no-op.
- test_1145 does not cover P1′. Its only matrix-route case is Newton, which never reinstalls.
B1: condition 1
err is the relative error against the dense solve of Kᵀμ = b at the state. fine~KᵀT / fine~K(0)ᵀ compare the live fine operator with Kᵀ and with the tangent at a zero vector. L1, L0 vs PᵀKᵀP compare the coarse levels with the Galerkin chain from Kᵀ.
case (fault box, set_custom_fmg) |
np | 09d82e1 err (all / velocity) | velocity its | 80d3ebe err | 80d3ebe its | 09d82e1 fine |
09d82e1 L1, L0 vs PᵀKᵀP |
|---|---|---|---|---|---|---|---|
| kern pl (Newton) | 1 | 1.6e-7 / 3.1e-7 | 5 | 1.6e-7 | 30 (1 level) | 6e-17 / 0.70 | 6e-17, 1.5e-16 |
| kern pl (Newton) | 2 | 2.8e-7 / 6.4e-7 | 6 | 2.8e-7 | 42 | 6.5e-17 / 0.70 | 6e-17, 1.5e-16 |
| kern nv, tol 1e-6 | 1 | 4.1e-7 / 5.5e-7 | 5 | 4.1e-7 | 26 | 5e-17 / 0.24 | 6e-17, 1.4e-16 |
| kern nv, tol 1e-6 | 2 | 3.9e-7 / 5.9e-7 | 5 | 4.0e-7 | 35 | 6e-17 / 0.24 | 6e-17, 1.5e-16 |
| kern nv, tol 1e-10 | 1 | 3.6e-11 / 9.7e-11 | 6 | 3.6e-11 | 34 | 6e-17 / 0.24 | 6e-17, 1.5e-16 |
| kern nv, tol 1e-10 | 2 | 2.1e-11 / 7.5e-11 | 6 | 2.2e-11 | 45 | 6e-17 / 0.24 | 6e-17, 1.6e-16 |
| kern lin | 1 / 2 | 3.0e-7 / 3.1e-7 | 5 / 5 | 3.0e-7 / 3.2e-7 | 24 / 34 | — | 6e-17 |
Picard forward:
- We compared μ with 80d3ebe's μ, and with the dense reference of the Newton run on the same state and right-hand side. That reference is valid because both forwards converge to the same state within 1e-6.
- With the reference K from the Picard kernel,
errreads 3.7e-2 to 1.4e-1 on both builds alike, so we ignore it.
| case | np | 09d82e1 vs 80d3ebe | 09d82e1 vs dense reference | 80d3ebe vs dense reference | 09d82e1 its | 80d3ebe its |
|---|---|---|---|---|---|---|
| kern pic fmg | 1 | 1.9e-8 | 1.8e-7 | 1.8e-7 | 5 | 30 (1 level) |
| kern pic fmg | 2 | 3.9e-8 | 3.0e-7 | 3.0e-7 | 6 | 42 |
| mat pic fmg | 1 | 4.5e-8 | 2.8e-7 | 2.8e-7 | 200 (DIVERGED_ITS) | 37 (1 level) |
| mat pic fmg | 2 | 4.6e-8 | 2.4e-7 | 2.4e-7 | 200 (DIVERGED_ITS) | 51 (1 level) |
Fault mesh without set_custom_fmg (the auto-resolved hierarchy):
| case | np | 09d82e1 err | 09d82e1 its | 80d3ebe err | 80d3ebe its |
|---|---|---|---|---|---|
| kern pl | 1 / 2 | 1.6e-7 / 2.8e-7 | 2 levels, 10 / 12 | 1.6e-7 / 2.8e-7 | 1 level, 30 / 42 |
| kern nv | 1 / 2 | 4.1e-7 / 4.0e-7 | 9 / 11 | 4.1e-7 / 4.0e-7 | 26 / 35 |
| kern pic | 1 / 2 | 2.1e-8 / 3.9e-8 from 80d3ebe | 10 / 12 | — | 30 / 42 |
Condition 1: met.
Condition 2, matrix-route levels (P1/P1′)
- After a Newton or linear forward, the matrix route is identical to 80d3ebe. The injection is a no-op (install count 0), and the error and iterations are equal to every printed digit:
| case | np | err (both builds) | velocity its (both builds) |
|---|---|---|---|
| mat nv, tol 1e-6 | 1 / 2 | 2.5e-7 / 1.6e-7 | 9 / 10 |
| mat nv, tol 1e-10 | 1 / 2 | 1.8e-11 / 2.4e-11 | 13 / 14 |
| mat pl | 1 / 2 | 1.8e-7 / 7.6e-8 | 5 / 6 |
- The levels still come from J, not Jᵀ, as on development. mat nv at tol 1e-10, on both builds: L1 is 4.9e-7 from PᵀKP and 6.2e-3 from PᵀKᵀP. That is P1 and was there before; not a regression.
- After a Picard forward, the 200-iteration case is reachable on the real fix. The SNES rebuild for the consistent tangent makes the injection reinstall. The install assembles at zero, and the PC is set up on J(0). The state's tangent then goes into J, and
_solve_transposedswaps in a new Jᵀ, which only the outer operator picks up. Live operators, mat pic at np 1 and np 2:
| operator | compared with | relative difference |
|---|---|---|
| finest-level smoother | K(0) | 1.1e-2 |
| finest-level smoother | Kᵀ | 1.3 |
| coarsest operator | PᵀK(0)P | 5.3e-3 to 5.5e-3 |
| level 1 | PᵀKᵀP | 1.39 |
The velocity block hits max_it 200 with reason −3 while the outer solve reports 2. μ is right (2.4e-7 to 2.8e-7), so the cost is iterations and a diverged inner solve.
- The author's figure of 7e-11 at 9 to 11 iterations matches our mat nv (Newton) rows, not mat pic.
- Condition 2: partly met.
- Met for Newton and linear forwards: no change from development.
- Not met after a Picard forward: 200 against 37 / 51. It is a narrow case (matrix route, Picard forward, custom hierarchy), but it is a regression against development in its iterations.
- Fix: on the matrix route, inject after
ksp.setOperators(Jt, Pt)inside_solve_transposed, or reset the velocity PC there. Add a matrix-route Picard case to test_1145.
The tests: condition 3
test_1145:
| build | np 1 | np 2 |
|---|---|---|
| 09d82e1 | 4 passed | 3 failed, 1 passed |
| 80d3ebe | 3 failed, 1 passed | 4 failed |
- On 80d3ebe at np 1, the hierarchy test and the two kernel-route nonlinear cases fail on
getMGLevels() == 3; the matrix-route case passes. At np 2 that case fails too, on the test's dense reference (see below). - At np 2 the three nonlinear cases fail on the branch from the test's own dense reference:
IndexError: index 1747 is out of bounds for axis 0 with size 1745andLinAlgError: Incompatible dimensions._dense_adjoint_errortakesJ.convert("dense").getDenseArray(), which holds only the local rows, and indexes it with the global pressure IS.- The file runs serially in test.sh (
batch 4), so CI is unaffected. - But the cases should skip at np > 1 or gather the matrix, like
probe_adj.py. (low)
- The file runs serially in test.sh (
Mutants, at np 1 on 09d82e1:
| mutant | what it does | result |
|---|---|---|
mutj0 |
sets J ← J(0) right after the state's assembly | 3 failed (all nonlinear): 0.55, 0.55, 0.22 |
mutold |
the 98d0c8b order: the injection moved to just after the state's assembly | 2 failed (kernel-route Newton and Picard, 0.55 each); the matrix-route η(v_x) case passes, because no reinstall happens on a Newton matrix route |
The test guards B1 where B1 lived. It cannot see P1′.
The 7 adjoint files, serially: 09d82e1 and 80d3ebe are identical, with everything passing.
- 0018: 14
- 0019: 8
- 0021: 1
- 0022_from_kernels: 2
- 0022_rotated_adjoint: 7 passed, in 28 s on 09d82e1 and 28 s on 80d3ebe, against no test finished in 56 min on ba3fc23/98d0c8bd. One convergence reference for every solver: warm starts, the linear floor, the rotated loop (#911, #985, #924) #1014 shows.
- 1131: 24 passed, in 448 s and 443 s.
The 7 files at np 2: 09d82e1 and 80d3ebe are identical except test_1145.
- 0021, 0022_from_kernels and 0022_rotated pass.
- 0018 and 0019 have the same one-rank failures as before, on both builds.
- 1131 hangs in a collective on both, and the supervisor killed it at 600 s. It is a serial file.
Condition 3: met for np 1 (nonlinear Newton and Picard cases against a dense reference, and mutants caught). Partly met overall: the nonlinear cases break at np 2, and the matrix-route Picard case is missing.
Regressions (attack 4)
Repeats and sequences (F A A F A F A, np 2, velocity iterations by step):
| route, mg | 09d82e1 | 80d3ebe |
|---|---|---|
| kern, fmg | 3 levels, 4 at every step | adjoints at 1 level, 28 |
| kern, none | 9 / 10 | adjoints at 28 |
| mat, fmg | 4 / 5, identical to 80d3ebe | 4 / 5 |
| mat, none | 9 / 10, identical to 80d3ebe | 9 / 10 |
μ drift is 0.0 across the repeated adjoints, and the options database is flat at 49 and 46 keys.
Deferred destroys per F+A cycle at np 2, over 10 cycles without garbage_cleanup:
| route, mg | 09d82e1 | 80d3ebe |
|---|---|---|
| kern, fmg | +60 | +38 |
| kern, none | +46 | +32 |
| mat | flat | flat |
These are unchanged from 98d0c8b. L2 stands: the transfers are rebuilt and the Jacobian assembled once more on every kernel-route adjoint.
Rotated adjoint at np 2 (probe_rot.py): an annulus with rotated free slip on both walls, three adjoints each, with and without set_custom_fmg.
- Reasons are 2, 2, 2 on both builds.
- μ is identical (fmg: norm 5939.500970009243 on both). Without fmg the norm differs at 1e-15.
- The rotated route is untouched.
Merge conditions now
- B1, the tangent at the state: met.
- Matrix-route levels from Jᵀ: partly met.
- Newton or linear forward: identical to development.
- Picard forward with a custom hierarchy: still built on J(0), 200 / DIVERGED_ITS against development's 37 / 51.
- P1 itself (levels from J, not Jᵀ) is unchanged and was there before.
- test_1145 nonlinear cases: partly met.
- Met at np 1, with the mutants caught.
- The nonlinear cases fail at np 2 on the test's dense reference (local rows indexed globally).
- No matrix-route Picard case.
Not re-tested: a timing of the matrix-route Picard adjoint; 3-D; np > 2.
Re-check of c6e0cf6
Tip: c6e0cf6 (wt-1016, rebuilt). It is 09d82e1 plus one commit:
_solve_transposedcopies Jᵀ's and Pᵀ's values into J's and P's own storage, swaps J's null spaces, solves, and copies the saved values back in afinally.- The copy is used only when a CSR-pattern test, allreduced with LAND, says Jᵀ has J's pattern. Otherwise the old swap runs.
- test_1145 gains a matrix-route Picard case and an iteration bound. Its dense-reference cases skip at np > 1.
Base: 80d3ebe (wt-dev80).
Probes and logs are in runs/2026-11-06/a1016/review3/:
probe_adj.pynow also replicates the author's pattern verdict on each rank. It records whether the outer KSP operator is J itself (the in-place path), compares J's and P's CSR before and after_solve_transposedbitwise on every rank, compares null-space handles, and solves forward from zero after the adjoint.A16_RAISEforces an exception from inside the adjoint's KSP solve.probe_pattern.pydrives_solve_transposeddirectly on synthetic MPIAIJ matrices.- The mutants are
mutj0.pyandmutswap.py.
Both worktrees are clean. We used at most 23 cores, and no process was killed.
Verdict
Ready to merge. Every merge condition is met.
- One latent nit (N1): with a null space but no transpose null space, J comes back with a transpose null space it did not have. UW always sets both, so it does not arise here.
- Two test-coverage notes: the dense-reference cases skip at np > 1, and no real UW operator exercises the non-symmetric-pattern fallback.
- The forward solve after the adjoint is now bitwise identical to the first forward. On 80d3ebe it was not.
Attack 1: the matrix route, and the kernel route unchanged
err is against our dense reference, gathered to rank 0. For a Picard forward the reference is the Newton run's dense solve on the same converged state and right-hand side. Iterations are the velocity iterations of the adjoint.
| case | np | c6e0cf6 err | c6e0cf6 its | 80d3ebe err | 80d3ebe its | c6e0cf6 μ vs 80d3ebe μ |
|---|---|---|---|---|---|---|
| mat pic (power law, Picard), fmg | 1 | 2.8e-7 | 5 | 2.8e-7 | 37 (1 level) | 4.0e-8 |
| mat pic, fmg | 2 | 2.4e-7 | 6 | 2.4e-7 | 51 | 3.6e-8 |
| mat pic, fmg | 4 | — | 6 | — | 50 | 4.2e-8 |
mat pic, no set_custom_fmg |
1 / 2 | 2.8e-7 / 2.4e-7 | 2 levels, 10 / 12 | same | 1 level, 37 / 51 | 4.2e-8 / 3.4e-8 |
| mat nvpic (η(v_x), Picard) | 1 / 2 / 4 | 5.0e-7 / 4.5e-7 / 4.6e-7 | 5 / 5 / 5 | 5.1e-7 / 4.6e-7 / 4.6e-7 | 32 / 42 / 43 | 2.6e-8 / 3.0e-8 / 3.5e-8 |
| mat nv (η(v_x), Newton), tol 1e-6 | 1 / 2 / 4 | 2.5e-7 / 1.6e-7 / 1.6e-7 | 5 / 5 / 5 | same | 9 / 10 / 10 | 2.3e-8 (np 4) |
| mat nv, tol 1e-10 | 1 / 2 | 1.8e-11 / 2.4e-11 | 6 / 6 | 1.8e-11 / 2.4e-11 | 13 / 14 | — |
| mat pl (Newton) | 1 / 2 | 1.8e-7 / 7.6e-8 | 5 / 6 | same | 5 / 6 | — |
| mat lin | 1 / 2 | 2.6e-7 / 1.5e-7 | 5 / 5 | same | 5 / 5 | — |
| matns pl (matrix route with a pressure null space) | 1 / 2 / 4 | 1.6e-7 / 2.8e-7 / 2.0e-7 | 5 / 6 / 6 | same | 5 / 6 / 6 | 3.0e-12 (np 4) |
- The 200-iteration / DIVERGED_ITS case of 09d82e1 is gone: 5–6 iterations, with a converged velocity block (reason 2).
- On our fixtures the author's figures are: η(v_x) Newton 9 → 5 (confirmed); power-law Picard 37–51 → 5–6 (the author has 129 → 6 on test_1145's fixture). η(v_x) Picard on development is 32–43 here; the author has 11.
- The kernel route is unchanged from 09d82e1, number for number:
| case, np 1 / np 2 | err | its |
|---|---|---|
| kern pl, fmg | 1.6e-7 / 2.8e-7 | 5 / 6 |
| kern nv, fmg | 4.1e-7 / 3.9e-7 | 5 / 5 |
| kern pic, fmg | 1.9e-8 / 3.9e-8 from 80d3ebe | 5 / 6 |
| kern nv, no fmg | 4.1e-7 / 4.0e-7 | 9 / 11 |
_solve_transposed is never entered there.
Attack 2: the CSR pattern test on MPIAIJ
- The test is valid.
getValuesCSRon MPIAIJ returns each local row's columns sorted, in global numbering. Jᵀ of a square matrix with equal row and column layouts has the same row ownership. So "the row pointers and column indices are equal on every rank" is exactly "the global patterns are equal".- It cannot say True when the patterns differ.
- It could only say False on an equal pattern if the row and column layouts differed, and that errs towards the safe fallback
[read].
- Explicit zeros: a stored zero at (i, j) with nothing at (j, i) is kept by MatTranspose (nnz 319 against 319), so the patterns differ and the test says False.
- Synthetic MPIAIJ (
probe_pattern.py, np 1, 2 and 4), with the residual ‖Jᵀx − b‖/‖b‖ and J restored bitwise in every case:
| case | local verdicts at np 4 | combined | path | residual |
|---|---|---|---|---|
| symmetric pattern with cross-rank couplings and non-symmetric values | all True | True | in place | 1e-14 to 7e-14 |
| one extra entry (i, j) without (j, i), crossing ranks | [F, T, T, F] | False | swap (fallback) | 2e-14 to 7e-14 |
| asymmetry local to the last rank only | [T, T, T, F] | False on all ranks | swap | 7e-14 |
| explicit zero without its mirror | — | False | swap | ≤ 7e-14 |
- Real UW operators: every matrix-route run (both builds, np 1, 2 and 4) found a symmetric pattern, with local verdicts True on every rank.
- J is
seqaijormpiaij, and P is a separate Mat. - DMPlex preallocates by adjacency, which is symmetric
[read]. - We found no UW solver that produces a non-symmetric pattern. The fallback is verified on synthetic matrices only.
- J is
- N1 (nit, latent). With a null space and no transpose null space on J, J comes back with both set:
nsp_before [T, F]→nsp_after [T, T]at np 1, 2 and 4. The in-place path sets J's transpose null space and restores only the slots that were set before. UW's_attach_*nullspacealways sets both to the same object (pyx:6482-6492, 10471-10476), so no UW path reaches this[read]. Fix: save both handles and restore each, including clearing one that was unset.
Attack 3: copy and restore
- Values: after every matrix-route
adjoint_solve(22 runs: np 1, 2 and 4, linear, Newton, Picard, with and without a null space), J's and P's CSR is bitwise equal to what it was before, on every rank. - Null spaces: the handles are restored on J and P, with and without the pressure null space (
matns). - Forced exception: we raised from inside the adjoint KSP solve (the
A16_RAISEmonitor), for mat nv and mat pic at np 1 and 2.adjoint_solveraises PETSc.Error(101).- J and P are still restored bitwise and the null spaces are restored; the restore is in
finally. - The next forward solve converges (SNES reason 2, 5 velocity iterations).
- The next forward solve, started from zero after the adjoint:
- c6e0cf6: identical iterations to the first forward and a bitwise identical solution, in all 10 cases (np 1 and 2).
- 80d3ebe under the old swap: mat nv's solution moves by 3.2e-12 (np 1) and 1.2e-12 (np 2), and matns pl's by 8.2e-9 and 5.0e-9.
- So the copy-back increments the matrix state and the PC refreshes, and no stale Galerkin product or factor survives. The old swap left one.
- The forward solve straight after the adjoint (not from zero) matches 80d3ebe bitwise at np 1 and 2, and to ≤ 1.1e-14 at np 4. That solve starts at the converged state (0 SNES iterations), so it is a weaker check.
Attack 4: #1021, matrix-route Galerkin levels
mat nv, tol 1e-10:
| c6e0cf6 | 80d3ebe | |
|---|---|---|
| L1 vs PᵀKᵀP | 0.0 | 6.2e-3 |
| L1 vs PᵀKP | 6.2e-3 | 4.9e-7 |
| L0 vs PᵀKᵀP | 0.0 | 1.2e-2 |
| L0 vs PᵀKP | 1.2e-2 | 4.8e-7 |
| finest smoother vs Kᵀ | 0.0 | 5.7e-3 (and 4.5e-7 from K) |
| velocity its, np 1 / 2 | 6 / 6 | 13 / 14 |
The levels, the smoothers and the outer operator all come from Jᵀ now. #1021 is fixed.
Attack 5: tests
test_1145:
| build | np 1 | np 2 |
|---|---|---|
| c6e0cf6 | 5 passed | 1 passed, 4 skipped |
| 80d3ebe | 5 failed | 1 failed, 4 skipped |
- On 80d3ebe at np 1, the failures include the hierarchy assertion
('mg', 1)and the matrix-route iteration bound(9, 5). - At np 2 the dense-reference cases skip, so nothing nonlinear runs. Our probe covers np 2 and np 4.
Mutants, at np 1 on c6e0cf6:
| mutant | what it does | result |
|---|---|---|
mutj0 |
J at u = 0 after the state's assembly | 4 failed: errors 0.55, 0.55, 0.22, 0.88 |
mutswap |
the old 09d82e1 swap back in | 2 failed on the new iteration bound: η(v_x) matrix route (9, 5), power-law matrix-route Picard (131, 5) |
Both mutants fail.
The 7 adjoint files, c6e0cf6 against 80d3ebe:
- Serially, identical and all passing: 0018 14, 0019 8, 0021 1, 0022_from_kernels 2, 0022_rotated 7 (28 s), 1131 24 (454 s against 445 s).
- At np 2, identical apart from test_1145 above.
- 0021, 0022_from_kernels and 0022_rotated pass.
- 0018 and 0019 each fail one test, the same one on both builds (per-rank
tmp_pathand a zero-size max). - 1131 hangs in a collective on both. These are serial files.
Attack 6: regressions
Sequences (F A A F A F A, np 2, velocity iterations by step):
| route, mg | c6e0cf6 | 80d3ebe |
|---|---|---|
| kern, fmg | 3 levels, 4 throughout | adjoints at 1 level, 28 |
| kern, none | 9 / 10 | adjoints at 28 |
| mat, fmg | 4 / 5, identical | 4 / 5 |
| mat, none | 9 / 10, identical | 9 / 10 |
μ drift is 0.0, and the options database is flat at 49 and 46 keys.
Deferred destroys per forward+adjoint cycle (np 2, 10 cycles):
| route, mg | c6e0cf6 | 80d3ebe |
|---|---|---|
| kern, fmg | 60 | 38 |
| kern, none | 46 | 32 |
| mat | flat (36 / 30) | flat (36 / 30) |
These are unchanged from 98d0c8b and 09d82e1, and come from the kernel-route reinstall (L2). The in-place copy adds nothing on the matrix route.
Rotated adjoint at np 2 (probe_rot.py, an annulus with free slip on both walls, three adjoints): reasons 2, 2, 2, and μ identical to 80d3ebe (norm 5939.500970009243 with fmg, 5939.549189882579 without).
Merge conditions now
- B1, the tangent at the state: met. Every kernel-route case is unchanged from 09d82e1. The matrix-route Picard μ is 2.4e-7 to 2.8e-7 from the dense reference and 4e-8 from 80d3ebe.
- Matrix-route levels from Jᵀ (P1 / Stokes adjoint, matrix route: the velocity multigrid is built from J's block, not J^T's #1021): met. The levels are PᵀKᵀP to 0.0, against development's 6.2e-3. Velocity iterations: Newton 9–10 → 5, Picard 37–51 → 5–6, η(v_x) Picard 32–43 → 5. Restore is bitwise, it survives an exception, and the next forward solve is bitwise identical.
- test_1145 nonlinear cases: met. At np 1 it passes and both mutants fail. At np 2 its 4 dense cases skip by design, so its parallel coverage is the linear hierarchy check only; our probes cover np 2 and 4.
Optional:
- N1: restore the null-space slots exactly.
- A unit test for the fallback path. No UW operator reaches it, but
probe_pattern.py's non-symmetric case would do. - L2, the extra deferred destroys on the kernel route, still stands.
Not tested: 3-D; wall time; np > 4; a UW operator with a structurally non-symmetric pattern (none found).
Underworld development team with AI support from Claude Code
…ering Underworld development team with AI support from Claude Code
A Stokes adjoint now runs the velocity block's custom-P hierarchy, as the forward solve does (#1016). When the adjoint has to transpose the matrix explicitly, its preconditioner is now set up from Jᵀ rather than J (#1021).
The problems
#1016.
adjoint_solveswitches the tangent and solves with the forward's KSP. The switch reset the preconditioner, and the fieldsplit rebuilt the velocity block as a one-level multigrid, with no coarse correction and no FAC smoother. On a split fault box the adjoint took 23 velocity iterations at np 1 and 29 at np 2, against the forward's 4. It still converged.#1021. On the matrix route,
_solve_transposedgave the KSP new matrices for Jᵀ and Pᵀ. The outer KSP took them, but the velocity block kept its last set-up:The change
adjoint_solvecallsauto_inject_custom_mgbefore the adjoint assembles its tangent at the forward state. The install assembles at a zero vector to reach the fieldsplit. In the first version the injection ran after the state's assembly, and a nonlinear adjoint then solved with the tangent at u = 0. That was wrong by 12 to 75 % in the velocity, and reported converged (B1 in hyperion's review)._solve_transposedcopies Jᵀ's (and Pᵀ's) values into J's and P's own storage, wherever the sparsity pattern is symmetric, as an assembled finite-element operator's is. It swaps the null spaces, solves, and copies the saved values back in afinally.Evidence
Velocity iterations of the adjoint, on the fault box with
set_custom_fmg. The adjoint is checked against a dense solve of Kᵀμ = b, with K the consistent tangent at the state.tests/test_1145_stokes_adjoint_keeps_custom_mg.py, tier B:Serial, the 7 adjoint test files: 61 passed. At np 2 they match development apart from test_1145.
Reviews
Hyperion reviewed it three times; the last verdict, on c6e0cf6, is ready to merge. What it left:
set_custom_fmgnow gets the auto-resolved hierarchy in the adjoint as well: 2 levels and 9 to 10 iterations, against development's 1 level and 24 to 34.Since that review, development 22cf4e6 (#1018) has been merged in. The only conflict was one batch line each in
scripts/test.sh. test_1145 and test_1146 together: 9 passed.Issues
Underworld development team with AI support from Claude Code