Repository navigation
sensitivity(): the facet part reads every boundary term of a natural or Nitsche condition (#1143) - #1144
Conversation
…oo (#1143) sensitivity() took the facet part of a parameter in a natural or Nitsche condition from the value term fn_f only, tested against mu. A Nitsche condition also assembles a flux term fn_F, tested against grad(phi), and its part was left out. A lid datum imposed by Nitsche came out with the wrong sign, and the viscosity beside a Nitsche wall was 14% off. The facet part now adds (d fn_F / dm) : grad(mu) on the condition's boundary. The continuity-row term fn_p of a Stokes Nitsche condition is recorded but not assembled by the forward operator yet (#1072), so it has no part here. When #1072 lands it is tested against the pressure adjoint lam, and the normal-datum case of #1143 can then be closed. tests/test_0032_adjoint_nitsche_boundary_terms.py (tier B), against central differences: - the lid datum: development -7.333e-2 against FD +4.524e-2; here equal to 1e-6; - the viscosity beside a tangential Nitsche wall: development 14% off; here equal to 1e-6. The adjoint suite passes (80). Underworld development team with AI support from Claude Code
…pressure adjoint (#1143) Since #1145 a Stokes Nitsche condition's continuity-row term fn_p, -(n.d)(u.d - g), is assembled on the pressure field. A parameter in a normal datum enters it, and its part of the gradient is d(fn_p)/dm tested against the pressure adjoint lam. Without it, a normal datum's gradient was 2.85e-5 against a central difference of 1.68e-3. Now they agree to 1e-6. Called without lam, sensitivity refuses, naming lam, instead of dropping the term; the decision depends on the condition and the arguments alone, so it is the same on every rank. Underworld development team with AI support from Claude Code
Adversarial review, round 2: commit be3de0b (the
|
| case | sensitivity(lam=) |
central difference | rel. gap | fn_p part |
without lam |
|---|---|---|---|---|---|
| normal datum, default direction, open outflow | +1.6773633569e-01 | +1.6773633569e-01 | 6.4e-13 | +1.682436e-01 | refuses |
same, mesh qdegree=2 |
+1.6773633569e-01 | +1.6773633569e-01 | 6.1e-12 | +1.682436e-01 | refuses |
| same, structured quads | +1.6777361930e-01 | +1.6777361930e-01 | 7.8e-12 | +1.685411e-01 | refuses |
same, theta=-1 |
+1.6773633542e-01 | +1.6773633543e-01 | 6.0e-11 | +1.682463e-01 | refuses |
oblique d=(√3/2, 1/2), normal=(-1,0) (datum in fn_f, fn_F, fn_p: −6.49e-02 / +1.13e-01 / +1.19e-01) |
+1.6694098395e-01 | +1.6694098395e-01 | 4.5e-12 | +1.186652e-01 | refuses |
| viscosity beside a normal Nitsche wall | −3.4964407852e-04 | −3.4964407758e-04 | 2.7e-09 | 0 (symbolically) | same value, no refusal |
the penalty gamma as a named expression |
−8.1998331394e-10 | −8.1999962376e-10 | 2.0e-05 (difference noise at 1e-9) | 0 (symbolically) | same value, no refusal |
tangential lid datum, normal=(0,1) given |
+3.4910148511e-02 | +3.4910148510e-02 | 4.2e-12 | 0 (symbolically) | same value |
| tangential lid datum, default normal | +3.4910148511e-02 | +3.4910148510e-02 | 4.2e-12 | +0.000000e+00 (not symbolically zero) | refuses (finding 1) |
| two Nitsche walls, one parameter, closed box with null space | +1.6666666667e-01 | +1.6666666667e-01 | 1.2e-13 | +5.611910e-02 and +1.105869e-01 | refuses |
| two walls, separate parameters | +7.7122331627e-02 / +1.0489026789e-01 | equal | 8.5e-13 / 5.1e-12 | +7.724289e-02 / +1.037760e-01 | refuses |
| two conditions on one wall (normal datum + tangential zero) | +1.6773633879e-01 | +1.6773633879e-01 | 3.7e-13 | +1.682399e-01 | refuses |
solver.gradient(misfit, parameters=[m]) |
+1.6773633569e-01 | +1.6773633569e-01 | 6.4e-13 | (passes lam itself) |
n/a |
rotated free-slip on Top and Bottom + the Nitsche inflow (adjoint through solve_rotated_adjoint) |
+4.0525257126e-01 | +4.0525257127e-01 | 2.2e-11 | +4.060789e-01 | refuses |
Stokes_Constrained, multiplier wall on Top + the Nitsche inflow |
+2.1591999186e-01 | +2.1591999186e-01 | 1.4e-12 | +2.164819e-01 | refuses |
| 3-D box, cell 1/3, default normal | +1.1133306307e-01 | +1.1133306307e-01 | 2.9e-12 | +1.143204e-01 | not tried |
Two independent checks of the same term:
λ · ∂R_p/∂m, withR_pthe assembled boundary residual on the pressure field (compute_boundary_residual_fields("Left", residual_field_id=1)differenced atm ± h): +1.6824355650e-01. The integralsensitivity()forms: +1.6824355650e-01. The momentum rows of that field-1 residual are 0.0, so the row is where The Nitsche condition's continuity-row term is assembled (#1072) #1145 put it.- In the closed box, adding 7 to
lamchanges the result by 4.2e-16: the term does not see the pressure-adjoint level when the datum carries no net flux.
Findings
1. The refusal fires for a tangential datum when the normal is the default (low; behaviour, not a wrong number).
n·d is built from mesh.boundary_normal(boundary), a P1 field, so ∂fn_p/∂m is a non-zero expression even where its value is zero. Measured: lid datum on Top, direction=(1,0), normal=None: sensitivity(mu, m) raises "…enters the continuity-row term…"; with lam the fn_p part is +0.000000e+00 and the result is the central difference to 4.2e-12. With normal=(0,1) given, the same call without lam returns +3.4910148511e-02. So on Stokes any Nitsche datum with the default normal needs lam, whatever its direction, and the message states a dependence that is not there on a flat wall. This is not a regression against development (there the call returned the wrong-signed number). test_a_lid_datum_imposed_by_nitsche always passes lam, so neither behaviour is pinned. We would either say "pass lam for any Nitsche datum on a saddle point" in the docstring and message, or leave it and add the test.
2. The message names the parameter by its value (low).
Measured text: sensitivity: 1.00000000000000 enters the continuity-row term of the Nitsche condition on 'Left', …. f"{wrt}" of a uw.expression prints its value; wrt.name is m_f. The same pattern is already in the _essential_datum_sensitivity messages, so this is inherited, but the new line repeats it (petsc_generic_snes_solvers.pyx, the raise at ~3083).
3. Tests: one mutant survives, and three things are unpinned (low).
- A refusal widened to "any condition with an
fn_pand nolam" passes all four tests (table below). No test anywhere callssensitivity()withoutlamon a solver carrying a Nitsche condition for a parameter outsidefn_p. At the head that call works (viscosity,gamma: same value with and withoutlam); one assertion intest_the_viscosity_beside_a_nitsche_wallwould pin it. match="pressure adjoint"is also the text of theValueErrorin_adjoint_forms(~line 2690, "a saddle-point adjoint needs the pressure adjoint too"). In this fixture the new block is the only source (the removed and silent mutants both give DID NOT RAISE), butmatch="continuity-row"would say which refusal is meant.rel=1e-6: measured gaps at the head are 5.6e-12 (lid), 1.1e-08 (viscosity), 5.7e-12 (normal datum).Jis quadratic in a datum amplitude, so the central difference is exact to round-off. Every mutant we tried is off by order 1, so the slack costs nothing today; 1e-8 would still hold for the two datum tests.- The two adjoint tests compare two numbers computed in the same run; neither
Jnor the gradient is recorded. A change of a Nitsche default moves both together and the test stays green. Values at the head, if a recorded anchor is wanted: +4.523679062811e-02, −4.054884226645e-04, +1.680922326276e-03. - Coverage of the new block by the PR's tests: one wall, 2-D,
direction=(1,0)with the default normal, closed box. Our rows above cover the rest.
Tiers and reach are in order: module level_2, one tier_b per test; scripts/check_test_coverage.py reports "OK: all 456 test files are reachable"; the file is in shard 4's glob tests/test_00[0-4]*py. The file launches no MPI. 4 passed in 22.9 s.
4. Documentation not updated with the change (low).
The sensitivity docstring says a parameter in a natural condition "adds a facet part" and mentions lam only for an essential datum. The Nitsche rulings in docs/developer/guides/boundary-condition-rulings.md say nothing about a Nitsche datum as a control needing lam. The branch touches no guide.
5. Three readers of the boundary forms do not read fn_p (by reading, not run).
_boundary_reads_of, _forms_of/_reads_parameter in adjoint.py, and _refuse_essential_datum_parameter read fn_f and fn_F only. A datum is in fn_f through the penalty and in fn_F through the symmetric term, so this matters only with gamma=0 and theta=0, where the datum is in fn_p alone. We did not construct that case.
Outside the branch's lines, measured at this head
The branch's diff against development is two files (52 lines of sensitivity() and the test file), so these are development's behaviour. None is caused by this PR and none is asked of it. We found no open issue for any of them; they have gone to the development hub, which decides where each is filed.
A. A parameter in the volume continuity term is dropped silently. stokes.constraints = div u − m s(x,y): sensitivity() returns +0.0000000000e+00 with and without lam; central difference +4.0152661967e-02. adjoint_integrand tests F0 and F1 against mu and never reads PF0. The boundary case now refuses; the volume case, the same omission, returns zero.
B. A parameter in an add_constraint_bc datum is dropped silently. Stokes_Constrained, datum m sin(πx)/10 on Top: sensitivity() returns +0.0000000000e+00; central difference −6.2925036044e-03. The constraint data live in _block_constraint_bcs, which sensitivity() does not read, and adjoint_support() reports supported.
C. With a strain-rate-dependent viscosity and consistent_jacobian=True, the adjoint through a Nitsche wall is inexact. Power law (1 + 5 ε_II²)^(−1/4), parameter in the body force (in no boundary term): gap 4.11e-05 with a Nitsche inflow wall, 4.54e-09 with the same wall imposed strongly (differences at h and 2h agree to 9 digits, so it is not the difference). The normal-datum parameter in the same setting: 6.4e-07. Cause, tested: the uu_G0/uu_G1 boundary blocks differentiate bc.fn_f as written (~lines 11297–11311), while fn_F goes through _jacobian_source (~11339); the viscosity inside fn_f is an opaque named expression, so its derivative with respect to the velocity gradient is missing from the assembled tangent. Replacing the condition's fn_f by _jacobian_unwrap(fn_f) before set-up (same residual value) brings the gap to 2.81e-09. The forward solve converges either way; the adjoint transposes the tangent it is given.
Mutants
Applied in memory: a pytest plugin replaces SolverBaseClass.sensitivity with a line-for-line Python copy of the method at be3de0b, one change per run. The unmutated copy ("control") gives the same four verdicts as the compiled method. No source file was edited and nothing was rebuilt.
| mutant of the new block | …normal_datum_imposed_by_nitsche |
…normal_datum_needs_the_pressure_adjoint |
lid, viscosity |
|---|---|---|---|
| control (no change) | pass | pass | pass |
| sign flipped | fail (−1.6239e-03 against +1.6809e-03) | pass | pass |
| block removed | fail (+2.8501e-05) | fail (did not raise) | pass |
lam.sym[0] → mu.sym[0] |
fail (+2.8478e-05) | pass | pass |
refusal removed (term dropped when lam is None) |
pass | fail (did not raise) | pass |
refuses whenever the condition has an fn_p and lam is None |
pass | pass | pass: survives |
The removed mutant reproduces the PR body's 2.850e-5.
The refusal
- Same on every rank: by reading. It is decided by
dp.is_zero_matrix, a symbolic test on the condition andwrt, andlam is None; thefn_fandfn_Fintegrals before it are collective and run on every rank in the same order, so every rank raises at the same point. Not run on more than one rank. - Fires when it need not: finding 1.
- Silent when it should fire: not found for a scalar parameter. Finding 5 for the readers in
adjoint.py. - Library callers:
adjoint.gradient(line 576) andTranscriptAdjoint.gradient(line 781) passlam. In-repository callers that do not (test_0019,test_0021,test_0022, the fault-segments example, the scripts guide) carry no Nitsche condition, by listing. Run at the head:test_00198 passed,test_00211,test_00232,test_00242,test_00263,test_00278,test_002912.
Other classes and routes
- Rotated free-slip adjoint route and
Stokes_Constrainedwith a Nitsche wall: measured, rows above. - Scalar and vector solvers with Nitsche, and
add_natural_bc: by reading,fn_pisNoneand the block is skipped;test_0024's natural-condition sensitivities pass. - A Nitsche condition with the continuity-row term switched off: there is no such option;
fn_pis always built on Stokes (zero in value for a tangential direction, finding 1).
Not run
- Anything on more than one rank (this machine is serial only), including the rank-independence of the refusal.
- Development itself: not rebuilt. The "development" column of the PR body was not re-measured, apart from the removed-block mutant.
TranscriptAdjointthrough a Nitsche condition; a masked (one-sided) condition;gamma=0withtheta=0; a curved boundary; a units model; quads in 3-D.test_0022_rotated_adjoint.py(long) and the full suite.- Linux.
Issue verdicts
- sensitivity() leaves out the flux and continuity-row terms of a natural or Nitsche condition: wrong-signed gradient for a Nitsche datum #1143: fixed by this branch, in full as titled (flux and continuity-row terms of a natural or Nitsche condition). Labelled
fixed-in-PR. A, B and C above are different roots (volume continuity row, multiplier row, forward boundary tangent) and are not part of it; if they are filed, the PR body should name them as left open. - Nitsche Stokes (test_1060) diverges: DIVERGED_LINE_SEARCH after 3 Newton steps, and the test asserts only shape #1072 (
fixed-in-development): label stands. The continuity row is assembled at this head (the assembled-residual check above); this branch does not change it. - adjoint_solve(target=...) does not validate the target spaces: a mismatch is a bare numpy broadcast error #781 and solver.gradient() takes the adjoint of an unconverged forward solve and returns a wrong-signed number #804: already fixed on development by adjoint_solve: own linear tolerance, target-space check, refuse an unconverged solve (#769, #781, #804) #1038 (merge b618b0f); the label was missing. Probe: the eight tests of
tests/test_0027_adjoint_solve_guards.pypass on a build of development bc3d536 (with the Jacobian routine of Stokes assembles the Jacobian and the preconditioner's matrix one to a call, so a rank without cells assembles like the rest (works around #962) #1156, which does not touch the adjoint), among them the two wrong-space refusals andtest_the_adjoint_of_an_unconverged_solve_is_refused. Both now carryfixed-in-development, with the probe on each issue. - Adjoint field dual is silently zero for a field that enters through a Dirichlet datum #787: the refusal the issue asked for is on development (
test_0029, 12 passed at this head); the dual through a datum is still not assembled. Still live in that part;_boundary_reads_ofdoes not readfn_p(finding 5). - adjoint_solve inherits the forward solve's INEXACT Newton tolerance: every sensitivity on an iteratively-solved problem is silently short by 15-20% #769, sensitivity() returns 0.0 for a parameter carried by an essential BC — indistinguishable from 'no influence' #766, solver.sensitivity() returns dJ/dm in SI, not in the non-dimensional system: wrong by the reference scales #763, _peel_except raises SympifyError on a dimensional expression, killing EVERY row of a sensitivity table #765, Adjoint: the Dirichlet-datum sensitivity is off by (L_ref in m)^2 in a units model #1048, Adjoint: every Dirichlet-datum sensitivity segfaults at np >= 2 (_essential_datum_owner, empty stratum) #1067, Stokes adjoint_solve drops the custom-FMG velocity hierarchy and its FAC patch: one-level mg, 29 iterations against the forward's 4 #1016, Stokes adjoint, matrix route: the velocity multigrid is built from J's block, not J^T's #1021, Replay/adjoint linearises about a state the forward run never visited: snapshots restore fields but not parameter values #773, Scalar and vector solvers: a natural BC that reads the unknown enters the Jacobian twice (Newton 17-23 its on a linear Robin problem) #1065, mesh.cell_size() is partition-dependent (kd-tree of this rank's centroids) — and add_nitsche_bc(local_h=True) is the DEFAULT, so Nitsche answers move 6.6e-03 between rank counts #569, Nitsche penalty scale regressed with mesh.cell_size(): the Zhong spherical-shell response went from 0.2% to 2.4-5.7% off #734 (
fixed-in-development): not re-probed; not touched by this branch. - Block-constrained Stokes combined with rotated free-slip is silently unguarded #464, Stokes_Constrained: consolidated state of the open work (supersedes #244, #248, #254, #495) #651, Stokes_Constrained: parallel-correctness residuals (partition-dependent [p,λ] gauge + constraint-assembly drift + lossy knockout) #254, Constrained free-slip is partition-dependent (3.4%): boundary normals accumulated rank-locally — and the rotated tests compare against a golden recorded on another host #564, Stokes_Constrained on a 3-D shell: np 8 differs from np 2 and 4 by 1.55e-5 (velocity L2), independent of solver tolerance (#651 item 1) #1150 (
Stokes_Constrained): not touched; not probed.
Underworld development team with AI support from Claude Code
…eader (#1143, W1's review) - The refusal names the parameter, not its value, and says that a tangential datum with the default normal also needs lam (its continuity-row derivative is zero in value but not symbolically). - The essential-datum guard and adjoint.py's two readers of boundary forms (the field-read detector and _forms_of) read fn_p as well as fn_f and fn_F. - The docstring and the boundary-condition rulings say a Stokes Nitsche datum needs lam. - test_0032: the refusal matched by its own text; a parameter fn_p does not read (the viscosity) is not refused, which kills a refusal widened to any condition with an fn_p. Underworld development team with AI support from Claude Code
Fixes #1143: all three boundary terms of a natural or Nitsche condition now reach
sensitivity(). Development is merged in (e519995), with #1145, which assembles the Nitsche continuity-row term.The defect.
sensitivity()built the facet part of a parameter in a natural or Nitsche condition from the value termfn_falone, tested against μ. A Nitsche condition also assembles a flux termfn_F, tested against ∇φ, and on Stokes a continuity-row termfn_p, tested against the pressure test function. Their parts of the gradient were left out. For a datum imposed by Nitsche the result could have the wrong sign.The fix. The facet loop in
sensitivity()(petsc_generic_snes_solvers.pyx) also adds, on the condition's boundary:lam=, the pressure part of theadjoint_solvetarget). If a parameter entersfn_pandlamis not given,sensitivityrefuses and nameslam, instead of dropping the term. The decision rests on the condition and the arguments alone, so it is the same on every rank.No forward assembly, solver option or default changes.
Tests.
tests/test_0032_adjoint_nitsche_boundary_terms.py, tier B. These are the issue's box and functional, against central differences. Measured serially on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH):m sin(πx)imposed by Nitsche along Topm sin(2πy)/10imposed by Nitsche on Right (entersfn_p)fn_pterm)The first two fail on development and pass here. The third fails on this branch before its
fn_pcommit (be3de0b) and passes after it. A fourth test checks that a normal datum withoutlamis refused. The adjoint and Nitsche test files pass, 96 of 96.Which existing results could be affected. Only an adjoint taken through a Nitsche condition, which has an
fn_F, and on Stokes anfn_p. A natural condition with a value only (fn_f) was already handled;test_0024's boundary sensitivities are of that kind and are unchanged.test_0028, uses a mesh gradient, not an adjoint.add_nitsche_bcon a parameter in the condition (its datum, or a viscosity or coefficient it reads) should be recomputed.Checklist (UW3 Style Charter — docs/developer/UW3_STYLE_CHARTER.md)
level_*andtier_*markers (S8)maybe_/try_/do_) and no commented-out code (S3, S4).array/mesh.X.coords— nowith ....access(...), nomesh.data(S7)pixi.toml/pixi.lockor other dependency changes riding in an unrelated PRUnderworld development team with AI support from Claude Code