Skip to content

The boundary tangent unwraps a natural condition's fn_f (#1162) - #1251

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/adjoint-nitsche-tangent-1162
Nov 20, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/adjoint-nitsche-tangent-1162

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member

Fixes #1162.

The defect. With consistent_jacobian = True (the default), a solver unwraps the volume flux before differentiating it, so the tangent carries d(viscosity)/d(grad u). It does the same for a Nitsche condition's gradient term fn_F. The boundary term fn_f was differentiated as written, so a named expression in it was a constant to the derivative. Two examples:

  • the viscosity in a Nitsche penalty;
  • a user's coefficient in a natural flux.

The forward solve still converges, but the matrix is not the residual's derivative. An adjoint gradient, which goes through that matrix's transpose, was off by 4.1e-5 relative.

The fix. The boundary tangent is taken of _jacobian_source(fn_f), as the volume flux and fn_F already are. The residual is unchanged, so converged answers don't move. With consistent_jacobian = False (Picard), _jacobian_source returns the expression as it is, so that tangent is unchanged too.

Each solver family builds its boundary tangent in its own loop, and all four had the same gap. All four are changed:

  • SNES_Scalar;
  • SNES_Vector;
  • SNES_MultiComponent;
  • SNES_Stokes_SaddlePt, and so Stokes, VE-Stokes, Stokes_Constrained and Navier-Stokes.

Evidence. The issue's reproducer: unit box, viscosity (1 + 5 ε_II²)^(-1/4), inflow on Left, objective ∫ (x y u_x + u_y²), gradient with respect to a body-force amplitude, one rank.

Left wall before: relative gap after: relative gap Newton steps
add_nitsche_bc 4.11e-05 (issue) 2.86e-10 4
strong add_dirichlet_bc 5.80e-09 (issue) 1.78e-09 4
Nitsche, fn_f unwrapped by hand 4.08e-09 (issue) 2.86e-10 4

The Nitsche gradient now equals the hand-unwrapped one to all printed digits. The Newton step count is 4 before and after on this problem, so no change in convergence was measured here.

Tests. tests/test_1162_natural_bc_tangent_unwraps_its_expressions.py, tier B; reachable from scripts/test.sh through the test_11* glob.

  • A finite-difference check per family. Each case compares the assembled boundary blocks uu_G0 and uu_G1, read in PETSc's [fc*Nc + gc, df] layout, against a central difference of the boundary residual at one point:

    • Stokes with a Nitsche wall and a strain-rate dependent viscosity;
    • Poisson with a natural flux k(∇T)·T;
    • a vector projection with a natural flux k(∇u)·u.

    Before the fix, the gaps are 0.103, 0.103 and 0.055 relative: uu_G1 lacked the coefficient's derivative, and was zero for the projection. After, all are under 1e-6.

  • The issue's adjoint measurement as a test, with a tolerance of 1e-7 relative. Before: 4.1e-5. After: 2.9e-10.

test_1066_stokes_jacobian_layout.py, including its Nitsche boundary check, passes.

Runs. The serial files on Stokes, Poisson, Nitsche, natural conditions, the adjoint, projections, rotated and constrained conditions, nullspaces, Jacobians, Darcy, diffusion and boundaries, tier C excluded (89 files):

  • 570 passed, 1 xfailed;
  • 2 skipped: mcp is not installed, and a gmsh mesh differs on this host.

The coverage check passes.

Also in this PR (a separate commit, from the review of #1250):

Measured on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH).

Underworld development team with AI support from Claude Code

A 2 x 2 x 2 hex box with a P3 velocity and P3 multipliers, constrained
against essential side walls: faces carry four dofs and edges two, and the
symmetries orient both. It fails before #1250 (max |v| 0.1124 against
0.1222) and passes after. The tolerance comment gives the P2 multiplier's
wall normal velocity after the fix (2e-7, not 7e-6), and the scatter-back
note says the multiplier index set takes every dof of each point.

Underworld development team with AI support from Claude Code
…is (#1162)

With consistent_jacobian on, the volume flux and a Nitsche condition's fn_F
are unwrapped before they are differentiated; fn_f was differentiated as
written, so a named expression in it (the viscosity in a Nitsche penalty, a
user's coefficient in a natural flux) was a constant to the tangent. The
adjoint gradient through a Nitsche wall was 4.1e-5 off a central difference;
it is now 2.9e-10. All four solver families' boundary loops take the tangent
of _jacobian_source(fn_f); the residual and the Picard tangent are unchanged.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 20, 2026 04:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Verdict on b8bef59: ready to merge on green from this review.

How it was run

Builds:

  • This branch: a fresh build of b8bef59 in its own worktree. petsc_generic_snes_solvers.pyx was touched before the build, and generic_solvers.so is dated after it.
  • Development: the PR's merge base, bc59ebf. Its compiled sources (.pyx, .pxd, .pxi, .h, .c) and lock file are identical to those of a build of c2f8b4f. That build's install was used, with bc59ebf's two differing Python files laid over it (systems/ddt.py, visualisation/parallel.py). Its Python then matches bc59ebf's source.

Instrumentation: the benchmark used a small pytest plugin. It wraps every solver class's solve() and records, per outermost solve:

  • SNES iterations;
  • KSP iterations (getLinearSolveIterations);
  • the converged reason;
  • the solution vector.

The two builds ran the same test files from this branch, and the records were matched solve by solve.

Every parallel run was under the MPI supervisor with a timeout, pinned to four cores.

Findings

  1. Low: The fourth family has no test of its own. SNES_MultiComponent's boundary loop changed too (it backs MultiComponent_Projection), but the new file covers Stokes, Poisson and Vector_Projection only. Our check below shows it is right: on development its tangent misses a named coefficient by 5.5e-2, and on this branch the gap is 6e-12. A fourth case in test_1162_natural_bc_tangent_unwraps_its_expressions.py, the same as the vector one on uw.systems.MultiComponent_Projection(mesh, n_components=2), would pin it.

  2. Info: a warm Nitsche re-solve now takes fewer Newton steps.

    • Where: test_0028_convergence_reference.py, two warm Nitsche re-solves with a strain-rate viscosity: test_a_weakly_imposed_datum_is_measured_against_the_step and test_a_datum_that_starts_at_zero_counts_from_the_start.
    • Counts: 9 Newton steps on development, 2 here. KSP goes from 9 to 2.
    • Answers: the two solutions differ by 2.7e-5 relative. The solve's tolerance is 1e-3, and both reach it.

    This is the corrected tangent doing its job. The file's totals fall from 50 to 36 SNES steps and from 168 to 154 KSP steps.

1. #1162's reproducer (the issue's script), Newton with consistent_jacobian = True

left wall np development: Newton, gap this branch: Newton, gap
Nitsche 1 4, 4.11e-5 4, 2.86e-10
Nitsche 2 4, 4.11e-5 4, 1.55e-9
strong 1 4, 1.8e-9 4, 1.8e-9
strong 2 4, 5.2e-10 4, 5.2e-10

The gap is the adjoint sensitivity() against a central difference, relative.

  • Forward objective: J is the same on both builds to 1e-13 relative.
  • Strong wall: it gives the same sensitivity() on both builds. It has no natural condition, so the change does not reach it.

2–3. Boundary-tangent accuracy, per family

The PR test's own helper, _boundary_tangent_gap, compares the assembled uu_G0/uu_G1 with a central difference of u_f0 at one point. We ran it on both builds, with three kinds of flux k * u on Top:

  • k a named expression (uw.expression) of grad u;
  • k a named expression of u;
  • k the same thing as plain sympy.
family named k(grad u): dev → PR named k(u): dev → PR plain sympy: dev → PR
Poisson (SNES_Scalar) 1.0e-1 → 8.9e-12 2.3e-2 → 3.6e-12 7.6e-12 → 7.6e-12
Vector_Projection (SNES_Vector) 5.5e-2 → 6.3e-12 2.3e-2 → 5.7e-12 6.3e-12 → 6.3e-12
MultiComponent_Projection (SNES_MultiComponent) 5.5e-2 → 6.3e-12 2.3e-2 → 5.7e-12 6.3e-12 → 6.3e-12
Stokes natural (SNES_Stokes_SaddlePt) 5.5e-2 → 6.3e-12 2.3e-2 → 5.7e-12 6.3e-12 → 6.3e-12
Stokes Nitsche, strain-rate viscosity 1.0e-1 → 4.8e-11

A named expression of the unknown, not only of its gradient, was also missed on development, in every family. It is now right in every family.

Already-right tangents give the same answer, bitwise. Natural conditions written in plain sympy or linear, solved by Newton, at np 1 and 2 on both builds:

case Newton / KSP (np 1, both builds) solution, PR against development
Poisson, linear flux 2T 2 / 16 bitwise, np 1 and 2
Poisson, linear flux with a constant named coefficient 2 / 16 bitwise, np 1 and 2
Poisson, plain flux 5T^3 8 / 97 bitwise, np 1 and 2
Poisson, plain flux 5T sqrt(T^2 + 0.01) 6 / 72 bitwise, np 1 and 2
Stokes, linear traction 3v 1 / 1 bitwise, np 1 and 2
Stokes, plain traction 20 v |v|^2 3 / 3 bitwise, np 1 and 2
Stokes, plain traction 5 v sqrt(|v|^2 + 1e-4) 3 / 3 bitwise, np 1 and 2

The np 2 counts are also equal between builds. The fractional-power guard that _jacobian_unwrap adds does not move a plain-sympy tangent: the sqrt case is bitwise too.

Benchmark: iteration counts and answers, development | this branch

Scope:

  • every serial test file with a natural, Nitsche, Robin or free-surface condition (44 files);
  • the parallel files with one, at np 2 (13 files);
  • the empty-rank free-surface file at np 3, where it runs.

Many of these carry a strain-rate or solution-dependent flux or viscosity: the Nitsche files, the free-surface files, test_0022's adjoint files and the test_0028 convergence references. Totals are SNES and KSP iterations summed over every solve in the file. "Bitwise" counts solves whose solution vector is identical. The largest relative difference is max |dx| / max |x| over the file's solves.

Serial (np 1). Files that logged solver solves:

file solves SNES its dev | PR KSP its dev | PR bitwise largest rel. diff
test_0024_adjoint_boundary_misfit_and_boundary_sensitivity 7 7 | 7 7 | 7 7 0
test_0026_adjoint_dirichlet_datum_sensitivity 7 7 | 7 40 | 40 7 0
test_0027_adjoint_solve_guards 15 124 | 124 1378 | 1378 15 0
test_0028_convergence_reference 17 50 | 36 168 | 154 15 2.7e-5 (rtol 1e-3; finding 2)
test_0029_adjoint_field_in_dirichlet_datum 58 72 | 72 724 | 724 58 0
test_0032_adjoint_nitsche_boundary_terms 11 11 | 11 11 | 11 11 0
test_0033_adjoint_gradients_in_a_units_model 19 19 | 19 19 | 19 19 0
test_0745_two_models_in_one_process 20 12 | 12 12 | 12 20 0
test_0764_petsc_index_boundary_conditions 5 5 | 5 5 | 5 5 0
test_0786_stokes_jacobian_and_preconditioner_one_call_each 3 3 | 3 4 | 4 3 0
test_1000_poissonNaturalBC 3 3 | 3 24 | 24 3 0
test_1001_poisson_constants 9 9 | 9 35 | 35 9 0
test_1011_stokesSph 3 3 | 3 3 | 3 3 0
test_1013_stokes_fields_by_solver_number 15 12 | 12 24 | 24 15 0
test_1014_stokes_shell_nullspace 2 2 | 2 0 | 0 2 0
test_1018_rotated_freeslip 43 16 | 16 16 | 16 43 0
test_1053_ve_etd_variable_maxwell_time 27 27 | 27 27 | 27 27 0
test_1060_nitsche_freeslip 4 4 | 4 4 | 4 4 0
test_1061_constrained_freeslip 12 15 | 15 29 | 29 12 0
test_1065_nitsche_local_h 2 2 | 2 2 | 2 2 0
test_1069_boundary_wrapper_checks_and_replaces 15 12 | 12 102 | 102 12 0
test_1070_free_surface_in_a_units_model 46 36 | 36 87 | 87 30 3.1e-11
test_1070_free_surface_plume 105 78 | 78 191 | 191 68 2.0e-11
test_1070_postprocessing_geoid 1 0 | 0 0 | 0 1 0
test_1071_slcn_moving_mesh 12 12 | 12 48 | 48 12 0
test_1072_free_surface_spherical 38 18 | 18 27 | 27 34 8.9e-11
test_1074_free_surface_config_drift 4 2 | 2 2 | 2 4 0
test_1094_maxwell_step_load_history_flavours 25 25 | 25 25 | 25 25 0
test_1105_levelset_rotation 640 640 | 640 2950 | 2950 640 0
test_1145_stokes_adjoint_keeps_custom_mg 5 81 | 81 81 | 81 5 0
test_1162_a_natural_bc_enters_the_jacobian_once 14 8 | 8 59 | 59 14 0
test_1162_natural_bc_tangent_unwraps_its_expressions (new) 3 12 | 12 12 | 12 0 6.2e-12
test_1166_short_bc_value_with_components_refused 3 3 | 3 3 | 3 3 0
test_1174_a_second_add_fault_keeps_the_first 13 3 | 3 8 | 8 13 0
test_1184_general_pressure_split 7 7 | 7 7 | 7 7 0

The test_0022 files are in the extra serial set below; they pass on this branch.

np 2:

file solves SNES its dev | PR KSP its dev | PR bitwise (recorded rank vectors) largest rel. diff
test_0768_unowned_boundary_natural_bc_mpi 3 3 | 3 15 | 15 all recorded 0
test_0770_submesh_extract_mpi 3 1 | 1 1 | 1 all recorded 0
test_0787_field_dual_through_dirichlet_datum_parallel 14 14 | 14 121 | 121 all recorded 0
test_1062_constrained_stratum_guard_parallel 1 1 | 1 6 | 6 all recorded 0
test_1064_rotated_freeslip_parallel 9 0 | 0 0 | 0 all recorded 0
test_1069_boundary_normal_parallel 1 1 | 1 1 | 1 all recorded 0
test_1071_spherical_shell_geoid_parallel 1 0 | 0 0 | 0 all recorded 0
test_1080_radius_accessors_partition_independence 0 — — — —
test_1085_submesh_boundary_names 1 1 | 1 7 | 7 all recorded 0
test_1095_custom_mg_split_finest_parallel 1 0 | 0 0 | 0 all recorded 0
test_1140_rotated_analytic_normal_wall_nodes_parallel 4 0 | 0 0 | 0 all recorded 0
test_1169_solver_reset_scopes_parallel 21 21 | 21 29 | 29 all recorded 0
test_1074_free_surface_on_an_empty_rank_parallel (np 3; it skips at np 2) 14 4 | 4 10 | 10 33 / 42 rank vectors 2.5e-14

Totals:

  • Rises: no SNES or KSP count rose on any solve, serial or parallel.
  • Falls: the only change is the fall in finding 2.
  • Answers: the largest relative difference outside test_0028 is 8.9e-11, in the spherical free surface. That is inside its tolerance, with identical counts.
  • Linear conditions: files with only linear natural conditions are bitwise, for example test_1000_poissonNaturalBC, test_0764 and test_1001.
  • Rotated free-slip solves log 0 / 0. Their own KSP path does not go through the SNES counters the plugin reads, so their counts are not measured here. Their answers are bitwise.

4. The #1250 lows (commit 3e4e214)

What was run

run result
44 serial files, development 379 passed, 5 failed (the four new #1162 tests, and one artefact of our instrumentation, below), 2 skipped
the same, this branch 383 passed, 1 failed (the same artefact), 2 skipped
adjoint, Nitsche, constraint and test_0022 files not in that set (15 files), this branch, -m "not tier_c" 67 passed
13 parallel files at np 2, both builds all pass, identical counts (table above)
test_1074_free_surface_on_an_empty_rank_parallel at np 3, both builds 1 passed each
test_1200_constrained_stokes_p3_velocity_parallel, this branch, np 1 / 2 / 4 5 passed at each
its 3-D case on #1250's parent 1 failed, as expected
#1162 reproducer, family checks, already-right-tangent cases tables above

The instrumentation artefact:

  • Which test: test_1174_a_second_add_fault_keeps_the_first.py::test_a_split_fault_with_no_law_is_named_at_solve.
  • Why it failed: it checks the file a warning is attributed to, and our solve() wrapper added a frame. It failed the same way on both builds.
  • Without the plugin: it passes on this branch (2 passed).

Not run:

  • test_1064_constrained_spherical_shell_response.py: level 3, tier C, more than 40 minutes on one core.
  • Wall-clock comparisons: the two builds' JIT caches were not equally warm.
  • np above 4 (core budget on a shared machine).
  • macOS.

Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 7c4e1df into development Nov 20, 2026
10 checks passed
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