Skip to content

sensitivity(): the facet part reads every boundary term of a natural or Nitsche condition (#1143) - #1144

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/sensitivity-boundary-terms-1143
Nov 13, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/sensitivity-boundary-terms-1143

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

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 term fn_f alone, tested against μ. A Nitsche condition also assembles a flux term fn_F, tested against ∇φ, and on Stokes a continuity-row term fn_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:

  • ∫_Γ (∂fn_F/∂m) : ∇μ. ∇μ is the adjoint's gradient for a scalar unknown and its Jacobian for a vector one.
  • ∫_Γ (∂fn_p/∂m) λ, with λ the pressure adjoint (lam=, the pressure part of the adjoint_solve target). If a parameter enters fn_p and lam is not given, sensitivity refuses and names lam, 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):

parameter development central difference this branch
lid datum m sin(πx) imposed by Nitsche along Top −7.333e-2 (wrong sign) +4.524e-2 equal to 1e-6
viscosity, beside a tangential Nitsche wall −4.626e-4 (14 % off) −4.055e-4 equal to 1e-6
normal datum m sin(2πy)/10 imposed by Nitsche on Right (enters fn_p) 2.850e-5 (this branch before the fn_p term) 1.681e-3 equal to 1e-6

The first two fail on development and pass here. The third fails on this branch before its fn_p commit (be3de0b) and passes after it. A fourth test checks that a normal datum without lam is 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 an fn_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.

  • In this repository: no test, example or developer note takes an adjoint with a Nitsche condition. The only test that has both words, test_0028, uses a mesh gradient, not an adjoint.
  • Outside it: campaign drivers on other machines, and UWTN notes' unpublished scripts, were not searched. Any adjoint gradient computed with add_nitsche_bc on 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)

  • I have read the Style Charter and followed it over the surrounding code (S2)
  • No drive-by refactors, renames, or "while I was here" cleanups (S9)
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers (S8)
  • No hedging names (maybe_ / try_ / do_) and no commented-out code (S3, S4)
  • Every exception swallow states its sanctioned failure mode in a comment (S4)
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7)
  • Parallel safety considered (S11): the facet integral is the existing collective boundary integral; nothing rank-dependent is added
  • No pixi.toml / pixi.lock or other dependency changes riding in an unrelated PR
  • AI-assisted work carries the attribution line below

Underworld development team with AI support from Claude Code

…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
Copilot AI balanced review requested due to automatic review settings November 13, 2026 07:59

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.

…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
@lmoresi lmoresi changed the title sensitivity(): the facet part reads a Nitsche condition's flux term too (#1143, fn_F half) sensitivity(): the facet part reads every boundary term of a natural or Nitsche condition (#1143) Nov 13, 2026
@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Adversarial review, round 2: commit be3de0b (the fn_p term of sensitivity())

From W1 (the integration desk; macOS, 12-core Apple M4, serial runs only), by a reviewer that had not seen round 1. Round 1 stands for the fn_F term; this round is the commit made since.

Scope: git diff e519995a be3de0be only (the continuity-row term against lam, its refusal, two tests). Measured on macOS, 12-core Apple M4 (serial runs only), at head be3de0b, conda-forge PETSc 3.25.3, built from the clean tree. Everything parallel is by reading, and marked so.

Result in one line. The term is right on every fixture we tried (2-D and 3-D, six routes); we found no error in sign, field or shape. The findings are about when the refusal fires, its message, what the tests leave unpinned, and three silent gaps beside the new block that the branch does not touch.

What we ran that would have shown a wrong term

Our own fixture, not the PR's: unit box, cell 1/6, P2/P1, J = ∫ (x y u_x + u_y²) (a load with a state-independent part), tolerance 1e-12, central difference at h and 2h. Each case also forms the three facet parts by hand; in every row the by-hand sum minus sensitivity() is exactly 0.

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, with R_p the assembled boundary residual on the pressure field (compute_boundary_residual_fields("Left", residual_field_id=1) differenced at m ± h): +1.6824355650e-01. The integral sensitivity() 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 lam changes 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_p and no lam" passes all four tests (table below). No test anywhere calls sensitivity() without lam on a solver carrying a Nitsche condition for a parameter outside fn_p. At the head that call works (viscosity, gamma: same value with and without lam); one assertion in test_the_viscosity_beside_a_nitsche_wall would pin it.
  • match="pressure adjoint" is also the text of the ValueError in _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), but match="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). J is 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 J nor 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 and wrt, and lam is None; the fn_f and fn_F integrals 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) and TranscriptAdjoint.gradient (line 781) pass lam. 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_0019 8 passed, test_0021 1, test_0023 2, test_0024 2, test_0026 3, test_0027 8, test_0029 12.

Other classes and routes

  • Rotated free-slip adjoint route and Stokes_Constrained with a Nitsche wall: measured, rows above.
  • Scalar and vector solvers with Nitsche, and add_natural_bc: by reading, fn_p is None and 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_p is 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.
  • TranscriptAdjoint through a Nitsche condition; a masked (one-sided) condition; gamma=0 with theta=0; a curved boundary; a units model; quads in 3-D.
  • test_0022_rotated_adjoint.py (long) and the full suite.
  • Linux.

Issue verdicts

Underworld development team with AI support from Claude Code

claude and others added 3 commits November 14, 2026 07:10
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants