Repository navigation
The boundary tangent unwraps a natural condition's fn_f (#1162) - #1251
Conversation
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
|
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)) Verdict on b8bef59: ready to merge on green from this review.
How it was runBuilds:
Instrumentation: the benchmark used a small pytest plugin. It wraps every solver class's
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. #1162's reproducer (the issue's script), Newton with
|
| 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:
ka named expression (uw.expression) of grad u;ka named expression of u;kthe 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_0028is 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_0764andtest_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)
- The 3-D hex case,
test_in_3d_the_faces_are_oriented_too: P3 velocity, P3 multipliers, 2x2x2 hexes. It passes at np 1, 2 and 4: the file gives 5 passed at each. On Stokes_Constrained: keep the section's symmetries when the multipliers are pinned (P3 velocity 26% low) #1250's parent, c902390 on a build of exactly that commit, it fails withmax |v| constrained 0.11239472, essential 0.12217759. - The test comment now gives the P2 multiplier's wall normal velocity after the fix (2e-7).
- The docstring note now says the multiplier index set takes every dof of each point.
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
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 termfn_F. The boundary termfn_fwas differentiated as written, so a named expression in it was a constant to the derivative. Two examples: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 andfn_Falready are. The residual is unchanged, so converged answers don't move. Withconsistent_jacobian = False(Picard),_jacobian_sourcereturns 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.add_nitsche_bcadd_dirichlet_bcfn_funwrapped by handThe 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 fromscripts/test.shthrough thetest_11*glob.A finite-difference check per family. Each case compares the assembled boundary blocks
uu_G0anduu_G1, read in PETSc's[fc*Nc + gc, df]layout, against a central difference of the boundary residual at one point:k(∇T)·T;k(∇u)·u.Before the fix, the gaps are 0.103, 0.103 and 0.055 relative:
uu_G1lacked 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):
mcpis 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):
test_1200: 2 × 2 × 2 hexes, P3 multipliers, about 25 s. It fails before Stokes_Constrained: keep the section's symmetries when the multipliers are pinned (P3 velocity 26% low) #1250 (max |v| 0.1124 against 0.1222) and passes after._constrain_interior_multipliers_in_section's docstring.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