Repository navigation
The Nitsche condition's continuity-row term is assembled (#1072) - #1145
Conversation
Stokes add_nitsche_bc built a wall term for the continuity rows and its Jacobian blocks, but registered the condition as one boundary entry on the velocity field. PETSc integrates a boundary entry only for the field it is registered with, so the term never reached the pressure rows. Its sign was also opposite to the one the +q div(u) continuity residual needs. Each Nitsche condition now registers a second boundary entry on the pressure field; the continuity-row residual and the pressure-velocity boundary Jacobian are wired to it, and the term is -(n.d)(u.d - g). compute_boundary_residual_fields(residual_field_id=1) returns that term, and the verbose view prints the second entry. test_1060 gains three tests (the block relation, the summed continuity residual in a closed box, the diagnostic), loses the xfail on test_nitsche_converges, and bounds Nitsche against essential at 1.5e-3. Underworld development team with AI support from Claude Code
Adversarial review, before the PR openedMade by a fresh reviewing agent for W1 (the integration session, which owns the solver code) on the uncommitted branch, then the fixes below were applied and the build and tests rerun. Run on macOS (12-core Apple M4), serial only: nothing here was run on more than one rank. "Before" is Result. The sign and the registration hold on every route we could reach on one rank. Six findings were fixed in the branch. One is not fixed here and is filed (#1143). Findings1.
The 2. 3. Two sentences of the CHANGELOG entry (fixed). The sentence on the flow through the wall and the sentence on answers that change now carry the measured numbers. 4. Comments (fixed). The comments in the changed code were reworded to state what the code is for. 5. The verbose view did not print the second entry (fixed). A verbose solve now prints a second weak form for each Nitsche wall, with 6. Docs. 7. Tests (fixed). The new tests had no tier of their own (the module sets one for every test, which the charter does not want; the module line is older and left). The bound in What holds
Tolerances and coordinate matchesLibrary change: none; a condition is found by its position in the list of natural conditions and the id PETSc returned. Tests: listed in the PR body. The one coordinate comparison ( Not checkedMore than one rank: the new entry on a rank that holds no facet of the wall, Issue verdicts
Underworld development team with AI support from Claude Code |
|
Parallel check at e4f879c: the library change is READY. The continuity-row entry is assembled collectively and correctly on every rank, including ranks that hold no facet of the Nitsche wall. Two of the new or changed tests are not rank-safe; fix them in this PR (conditions below). Measured by H1 (review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH)) on the branch built at e4f879c, with development built at d66c7da for comparison. MPI jobs ran under the supervisor. Logs are kept by the reviewer. 1. A rank with no facet of the Nitsche wall
So every rank lacks at least one of the two walls, and at np 3 and 4 band some ranks hold neither. The library, measured directly. This is a rank-safe form of
The rest of
On development, the new continuity tests fail on every rank as intended. 2.
|
| np | e4f879c | d66c7da |
|---|---|---|
| 2 | 7 passed, 1 skipped | 7 passed, 1 skipped |
| 4 | 7 passed, 1 skipped | 7 passed, 1 skipped |
Conditions (tests only)
test_the_boundary_residual_diagnostic_returns_the_continuity_row_termis not rank-safe.- It asserts on each rank's own sum of the wall's pressure rows. At np 2 natural, the rank with no Top facet reads 0 against −1/6, and it fails.
- It then skips the Bottom call that the other rank makes, so the job stops in MPI: "Message truncated" under
-q, or a hang until the supervisor under the band layout. np.abs(rows["velocity"]).max()also raises on a rank with no wall rows.- Fix:
max(initial=0.0), and anallreduceof both quantities before asserting. That is the form measured above. - CI runs
tests/serially, so CI does not see this. But the file is otherwise rank-safe, and this is the one test that exercises the collective.
test_nitsche_matches_essentialcompares each rank's own nodes. At np 4 band one rank measures 1.587e-3 against the 1.5e-3 bar; the global difference is not what is measured.- Reduce the two sums over the ranks.
- Also, the docstring says "within 2e-3" while the assertion is 1.5e-3.
3. The note's annulus scripts (UWTN 2026-016), serially, before and after
- Scripts: the note repository at 39335ce (
articles/boundary-conditions-on-non-planar-boundaries/examples), unmodified. md5 ofleak.py,stress.pyandtiming.pyare kept with the logs. - Builds: development d66c7da, and this branch at e4f879c.
- Host: the workstation named above, with each script pinned to one logical CPU. Every gamma listed converged on both builds; the scripts check the SNES reason.
Leak against the facet normal (leak.py sweep). Only the Nitsche column moves. Penalty, node penalty, multiplier ("constraint") and rotated are identical on both builds.
| cell size | Nitsche, d66c7da | Nitsche, e4f879c | penalty | penalty_node | constraint | rotated |
|---|---|---|---|---|---|---|
| 0.150 | 4.14e-03 | 4.09e-03 | 4.51e-03 | 3.31e-03 | 8.37e-04 | 7.28e-11 |
| 0.100 | 2.49e-03 | 2.45e-03 | 2.49e-03 | 2.71e-03 | 2.32e-04 | 5.69e-11 |
| 0.075 | 1.81e-03 | 1.79e-03 | 8.48e-04 | 2.62e-03 | 5.50e-05 | 1.04e-10 |
| 0.050 | 6.07e-04 | 6.06e-04 | 9.51e-04 | 2.59e-03 | 1.18e-05 | 9.73e-11 |
Leak against gamma (leak.py params):
| gamma | 1 | 10 | 100 | 1000 | 1e4 | 1e5 |
|---|---|---|---|---|---|---|
| d66c7da | 9.15e-03 | 1.81e-03 | 3.34e-04 | 2.84e-05 | 3.53e-05 | 1.55e-05 |
| e4f879c | 1.11e-02 | 1.79e-03 | 3.34e-04 | 2.84e-05 | 3.53e-05 | 1.46e-05 |
Surface stress error against the exact Kramer CylindricalStokes answer (stress.py sweep): exact σ_rr = 0.1506696429 cos 2θ on r = 1.
| cell size | Nitsche, d66c7da | Nitsche, e4f879c | penalty_node | constraint | rotated | rotated (reaction) | multiplier |
|---|---|---|---|---|---|---|---|
| 0.150 | 7.80e-02 | 7.48e-02 | 2.54e-02 | 2.44e-02 | 2.44e-02 | 6.82e-03 | 8.57e-03 |
| 0.100 | 3.33e-02 | 3.25e-02 | 1.08e-02 | 9.89e-03 | 9.87e-03 | 3.27e-03 | 8.46e-04 |
| 0.075 | 1.98e-02 | 1.94e-02 | 7.26e-03 | 6.33e-03 | 6.30e-03 | 2.08e-03 | 1.70e-03 |
| 0.050 | 8.58e-03 | 8.44e-03 | 3.64e-03 | 2.69e-03 | 2.68e-03 | 1.08e-03 | 1.41e-03 |
The controls are identical on both builds.
Nitsche gamma at cell 0.075 (stress.py params), as leak / velocity error / stress error:
| gamma | d66c7da | e4f879c |
|---|---|---|
| 1 | 6.24e-03 / 4.97e-03 / 1.94e-03 | 8.55e-03 / 3.10e-03 / 1.57e-03 |
| 10 | 2.24e-03 / 4.95e-03 / 1.98e-02 | 2.28e-03 / 2.55e-03 / 1.94e-02 |
| 100 | 4.06e-05 / 2.38e-03 / 6.77e-03 | 4.07e-05 / 2.43e-03 / 6.76e-03 |
| 1000 | 9.96e-06 / 2.42e-03 / 6.35e-03 | 9.97e-06 / 2.43e-03 / 6.35e-03 |
| 1e4 | 9.28e-06 / 2.40e-03 / 6.31e-03 | 9.28e-06 / 2.40e-03 / 6.31e-03 |
- The velocity error at gamma 1 and 10 drops by about half, to the level of the larger gammas. That is the continuity term now being assembled.
- From gamma 100 up the two builds agree to the printed digits.
- No gamma in 1 to 1e4 diverged on either build. Within the range the script sweeps, there is no divergence to report.
- The penalty tables are identical on both builds.
Control (stress.py control), identical on both builds: free slip gives leak 8.47e-01, velocity error 5.76, stress error 9.96e-01. Rotated gives 9.67e-11, 2.42e-03, 6.30e-03.
Timing (timing.py), seconds for solve / recover:
| cell size | velocity nodes | build | penalty_node | Nitsche | constraint | rotated |
|---|---|---|---|---|---|---|
| 0.020 | 28 362 | d66c7da | 0.68 / 0.829 | 0.72 / 0.853 | 1.06 / 0.011 | 0.45 / 0.027 |
| 0.020 | 28 362 | e4f879c | 0.68 / 0.793 | 0.72 / 0.845 | 1.04 / 0.011 | 0.44 / 0.027 |
| 0.013 | 71 400 | d66c7da | 1.76 / 2.121 | 1.86 / 2.245 | 2.74 / 0.023 | 1.16 / 0.043 |
| 0.013 | 71 400 | e4f879c | 1.73 / 2.050 | 1.86 / 2.223 | 2.72 / 0.024 | 1.17 / 0.044 |
- The Nitsche solve costs the same with the continuity term assembled.
- The timing ran with nothing else of the reviewer's on the machine. Other users' jobs were running (load average about 15 on 96 threads), so differences of a few per cent are not significant.
Checks
- Attribution: the commit and the PR body end with the repository line, with no trailer and no emoji.
scripts/check_test_coverage.py: OK (446 files).
Underworld development team with AI support from Claude Code
Two tests asserted on each rank's own part of a quantity that is only meaningful over the whole mesh. The boundary-residual test summed the continuity-row term over the rank's own entries, so a rank holding no facet of the wall read 0 against -1/6, failed, and left the other rank alone in the next collective call. The comparison with the essential solution took the L2 difference over the rank's own nodes, which at np 4 read 1.587e-3 on one rank against the bound of 1.5e-3. Both now reduce over every rank before asserting, and the comparison with the penalty solution uses the same helper. The docstring of the Nitsche comparison gives the bound the test asserts (1.5e-3). Serial values are unchanged: 1.1704e-03 and 1.5155e-03; 10 passed. Found by the parallel check of this branch at np 2 to 4 on a 96-thread Linux workstation; not run on more than one rank here. Underworld development team with AI support from Claude Code
|
At 321cdda: the two tests named in the parallel check above now reduce over every rank. Tests only; no library file changed.
Serial, on macOS (12-core Apple M4): 10 passed, with 1.1704e-03 (Nitsche) and 1.5155e-03 (penalty) unchanged. Not run by us on more than one rank; the re-check at np 2 to 4 is the parallel desk's. Underworld development team with AI support from Claude Code |
|
Re-check at 321cdda (tests only): READY. Both conditions are met.
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
…ed Nitsche condition The curved-boundary page recommended Nitsche with gamma=10 for free slip. The project ruling is rotated strong free slip first, Nitsche for a condition that must change during the run. The page now leads with add_rotated_freeslip_bc, says what gamma does and what to choose it against, and its Nitsche numbers on the Cartesian box are rerun on the code with the continuity-row term assembled (underworldcode#1072, underworldcode#1145). The Nitsche row of the elliptical-annulus comparison is removed: a rerun does not reproduce it, and the methods split into two groups on that geometry (stated on the page). adaptive-meshing: the ruling is stated, and the Nitsche diagnoses made before underworldcode#1145 are marked as not re-verified. free-surface-convection: a link into a session memory is replaced by what local_h does. Underworld development team with AI support from Claude Code
What this does
A Stokes solve with
add_nitsche_bcnow assembles the Nitsche term in the continuity rows, with the sign the continuity residual needs. Before, that term was built and never integrated, and a closed box with Nitsche on some walls and essential conditions on the others did not converge (#1072).This changes what a solver in
petsc_generic_snes_solversassembles, so it is row 17 of the solver register. Louis Moresi signed that row off on 2026-11-13, as relayed to us by the dev hub (the session that tracks PRs and merges). It has been run on one rank only. It merges on the parallel check at 2 and 4 ranks listed under "What was not run", and green CI.Every Stokes answer with a Nitsche condition whose direction is not tangential to the wall changes, including solves that converged before.
Cause
add_nitsche_bcbuilds two wall terms: one for the momentum rows and one,fn_p, for the continuity rows, with the Jacobian blocks of both. The condition was registered with PETSc as one boundary entry on the velocity field. PETSc integrates a boundary entry only for the field it is registered with (DMPlexComputeBdResidual_InternalandDMPlexComputeBdJacobian_Internaltake the row field from the entry), so the residual and the pressure-velocity Jacobian block that were attached to that entry for field 1 were never assembled. The velocity-pressure block was, so the assembled matrix lost the relation between its two off-diagonal blocks.The sign of
fn_pwas also wrong, which had no effect while the term was not assembled.The sign
The momentum residual is$\int \nabla v : \sigma - \int_\Gamma v\cdot(\sigma n) - \int v\cdot f$ with $\sigma = \tau - pI$ , so its pressure part in the volume is $-\int p,\nabla\cdot v$ . The continuity residual in this code is $+\int q,\nabla\cdot u$ . The pressure-velocity block of the volume terms is therefore minus the transpose of the velocity-pressure block.
Nitsche's consistency term in the momentum rows is$-\int_\Gamma (v\cdot d),(\sigma n\cdot d)$ for a constraint along $d$ , and its pressure part is $+\int_\Gamma (v\cdot d)(n\cdot d),p$ (in the code, $-\int_\Gamma q,(n\cdot d)(u\cdot d - g)$ . The code had $+$ .
f0 += p (n.d) d). For the wall terms to stand in the same relation as the volume terms, the continuity rows must carryWith that sign, summing the continuity residual over a constant$q$ gives $\int \nabla\cdot u - \int_\Gamma (u\cdot n - g)$ on a wall where $d = n$ : the flux through the other walls plus $\int_\Gamma g$ . In a closed box with essential conditions on the other walls and data of zero net flux this is zero for any velocity, so the constant pressure is again a left null vector of the Jacobian and the right-hand side is compatible with it. That is the property the stalled solves lacked.
The change
src/underworld3/cython/petsc_generic_snes_solvers.pyx, Stokes solver only:fn_p = -(n.d)(u.d - g).<boundary>_continuity. The multiplier constraints are already registered twice for the same reason.compute_boundary_residual_fields(boundary, residual_field_id=1)returns the continuity-row term of a Nitsche wall. It raisedValueErrorbefore, because it looked for a condition registered on field 1.There is no tolerance and no coordinate comparison in the library change. A condition is identified by its position in the solver's list of natural conditions and by the id PETSc returns for the boundary entry; the entry's name is not used to find it.
Not changed: the velocity-row terms, the penalty scale,
gamma, the vector solver's Nitsche condition (it has no pressure), the zero pressure-pressure boundary block.The published form
Underworld technical note 2026-016 (boundary conditions on non-planar boundaries) writes the symmetric Nitsche term as$-\int_\Gamma (n\cdot\sigma(w)\cdot n)(u\cdot n)$ , with $w$ the test function. Read with $\sigma(w) = 2\eta,\varepsilon(w) - qI$ , the stress of the velocity and pressure test functions together, it splits into $-\int_\Gamma 2\eta,(n\cdot\varepsilon(w)\cdot n)(u\cdot n) + \int_\Gamma q,(u\cdot n)$ . The second piece is the continuity-row term the code never assembled. Its sign agrees with this fix: the note's single form carries continuity as $-q,\nabla\cdot u$ , and the code carries it as $+q,\nabla\cdot u$ , so the code's term is $-q,(n\cdot d)(u\cdot d - g)$ . The equation in the note stands. In the code
thetascales the viscous half of the symmetric term only; the pressure half is always on.Tests
tests/test_1060_nitsche_freeslip.py:test_the_continuity_row_carries_the_nitsche_term: on the assembled Jacobian of the free-slip box, the pressure-velocity block is minus the transpose of the velocity-pressure block.test_the_continuity_residual_sums_to_zero_in_a_closed_box: at a random velocity and pressure, the continuity residual summed over the pressure unknowns is zero.test_the_boundary_residual_diagnostic_returns_the_continuity_row_term: fortest_nitsche_convergesloses itsxfail.test_nitsche_matches_essential: the bound on the difference from the essential answer goes from 0.01 to 1.5e-3.On this branch the file gives 10 passed. On
development(at 70b240c with #1137) the same file gives 5 failed, 5 passed: the three new tests,test_nitsche_convergesandtest_nitsche_matches_essential.Measurements
All on one rank, macOS, 12-core Apple M4. "Before" is
developmentat 70b240c with the refusal of #1137 (the solver file is otherwisedevelopment's); "after" is this branch. Reason is the SNES converged reason (2 converged on the residual, 4 stopped on a small step, -3 linear solve failed, -6 line search failed).Free-slip box, cell size 1/8 (the fixture of
test_1060; difference from the essential answer, relative):gamma12.5gamma10Manufactured solution (stream function$\sin\pi x,\sin\pi y$ , free slip on four walls, P2/P1, tolerance 1e-9; errors are $L_2$ norms of $u - u_e$ and $p - p_e$ ):
After the fix the velocity error falls at order 2.9 to 3.0 and the pressure error at order 2.0, as with the essential condition. The largest$|u\cdot n|$ on the walls is 4.5e-03, 5.9e-04 and 7.4e-05 at the three sizes, the same to 1 % before and after.
Other routes (difference from the strong condition's answer unless said):
Stokes_Constrained), Nitsche on BottomKSP_DIVERGED_PCSETUP_FAILEDtheta0 / -1n.d = 0, no continuity-row term)A datum with net flow into a closed box has no solution; the strong condition and Nitsche both stop with reason -3 at zero iterations.
SolCx of the technical note (viscosity contrast 1e6, Nitsche free slip on Top,
gamma10, tolerance 1e-9; stress error is relative to the analytic solution):Two of the three solves before the fix report convergence (reason 4) with a stress error of order ten to a hundred. After the fix, at 32 cells and contrasts from 10 to 1e6, every Nitsche solve converges in one step with a stress error within 1.1 % of the essential one; with
gammafrom 1 to 1000 the stress error is between 7.56e-02 and 7.87e-02.Before the fix, at 32 cells: no contrast from 10 to 1e4 converged (reasons -6, -6, -6 and -5, the last at the cap of 50 steps), with stress errors of 426, 9.8, 10.5 and 3.3. With
gammaat 1, 10, 12.5, 100 and 1000 the stress error was 2.57 (reason -5), 13.4, 3.86, 0.239 and 0.0837 (reason 4): only where the penalty dominates did the answer approach the essential one (0.0756).Spherical shell response (the Zhong kernel fixture of
tests/test_1064_constrained_spherical_shell_response.py, Nitsche route, one linear solve; error against the analytic topography response):gammaThe linear solve converged in all four runs (KSP reason 2 or 3, residual 1.1e-09 or less).
The default
gammaThe boundary-condition rulings say a
gammaof 10 "was a cliff, a hundred times worse than 12.5" after the cell size was redefined (#734), and the default is 12.5. On the shell fixture today there is no cliff between the two values, before or after this change (table above), and the linear solve converges in every run, so those numbers were not taken from a stalled solve. The cliff recorded in #734 (2.36 % and 5.67 % atgamma10) was measured at an earlier definition of the cell size; we did not rebuild that commit, so we cannot say whether that measurement came from a stalled solve. After the fix the two values differ by less than 0.04 % of the response on the shell and by 0.08 % of the velocity on the box (0.20 % against 0.12 %). Nothing here asks for the default to move from 12.5.Published and recorded numbers this changes
gamma, the timing row) were produced with the term missing. They need a rerun; the scripts need theassesspackage, which the machine used here does not have.docs/advanced/curved-boundary-conditions.mdquoted the box difference as 0.08 % atgamma=10, which neither build gives. It now says 0.20 % atgamma10 and 0.12 % at the default.docs/developer/CHANGELOG.mdhas the entry.docs/developer/design/CONVERGENCE_REFERENCE_POLICY.mdsays a weak datum contributesgamma mu/h gto the residual at zero. A datum normal to the wall now also puts(n.d) gin the continuity rows. Read, not measured; not edited here.Tolerances and coordinate matches
In the library change: none.
In the tests added or changed:
mismatch < 1e-12 * scaleon the block relation (measured 2.6e-17 relative; 0.33 with the term absent);abs(net) < 1e-12 * sizeon the summed continuity residual (5.3e-17; 4.6e-2 with the term absent); the guardsscale > 0.5andsize > 1e-3that the quantity compared against is not zero;diff < 1.5e-3on Nitsche against essential (1.17e-3 measured; 3.36e-3 with the term absent; 1.98e-3 atgamma10);< 1e-12and== 0.0in the diagnostic test. Unchanged and older than this change: the penalty bound 0.01, the wall-flow bounds 0.1 and 1e-12, and the one coordinate comparison, which picks the wall nodes byabs(y - 1) < 1e-10.What was run
Serial only, on macOS (12-core Apple M4).
tests/test_1060_nitsche_freeslip.pydevelopment)test_0010_cell_size_geometry,test_0016_solver_description_contract,test_0028_convergence_reference,test_0030_capability_guides,test_0052_collective_guard_scan,test_0641_wave_c_api_shims,test_1060_nitsche_freeslip,test_1064_constrained_spherical_shell_response,test_1065_nitsche_local_h,test_1065_rotation_gauge_freeslip,test_1066_rotated_meets_essential,test_1066_stokes_jacobian_layouttest_fast_schur_constrained_matches_zhong_velocity_response, older than this change and not a Nitsche case)scripts/check_test_coverage.pyThe branch is based on
developmentat 70b240c.developmenthas since gained #1137 (d66c7da), which touches the same solver file; the two merge without conflict, and the build tested here does not contain #1137.In-memory changes to the fixed build, on the box: with the old sign assembled the solve ends with reason -6 and a block mismatch of 0.655 of the block norm; with the term absent, reason -6 and 0.327. Registration on the wrong field cannot be made in memory (the registration is compiled); the build before the fix is that case.
A solver that is set up again (a changed body force or viscosity, the rewire flag, a second solve) gives the same answer as a new solver and registers nothing twice.
What was not run
tests/test_1060_nitsche_freeslip.pyat 2 and 4 ranks, including a partition where some rank holds no facet of the Nitsche wall (the new entry on the pressure field is registered on every rank), andtests/parallel/test_1069_boundary_normal_parallel.py.xfailremoved fromtest_nitsche_convergesrecorded that one host stopped at the pressure sub-solve's iteration cap.assess).Found beside the change, not fixed here
sensitivity()reads only the value term of a natural or Nitsche condition, not its flux term, and not the continuity-row term this PR assembles: sensitivity() leaves out the flux and continuity-row terms of a natural or Nitsche condition: wrong-signed gradient for a Nitsche datum #1143.tests/test_1060_nitsche_freeslip.pysets its tier for the whole module, where the style charter wants one tier on each test, and one test repeatstier_b. The tests added here carry their own mark; the module line is left.docs/advanced/curved-boundary-conditions.mdpassesgamma=10in its examples and recommends Nitsche where the rulings prefer rotated free slip;docs/developer/guides/adaptive-meshing.mdrecords Nitsche diagnoses made with the term missing. These pages go to the curved-walls desk with the technical note's correction.Issue state
fixed-in-PR.An adversarial review was made before this PR opened; its record is in the first comment.
Solver register: row 17.
Underworld development team with AI support from Claude Code