Skip to content

A natural BC that reads the unknown enters the Jacobian once (#1065) - #1068

Merged
lmoresi merged 5 commits into
developmentfrom
bugfix/natural-bc-jacobian-once-1065
Nov 12, 2026
Merged

lmoresi merged 5 commits into
developmentfrom
bugfix/natural-bc-jacobian-once-1065

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

SNES_Scalar, SNES_Vector and SNES_MultiComponent registered each natural BC's boundary Jacobian twice: once as the Jacobian and once as the preconditioner, with the same kernels. In these solvers the Jacobian and the preconditioner are one matrix, and both registrations are assembled into it. So the term df0/du of a natural BC that reads the unknown entered twice.

The residual was right, so converged answers were exact. But a linear Robin problem took 17 to 23 Newton iterations or failed, and an adjoint that transposes the assembled Jacobian carried the factor of 2.

This PR drops the preconditioner registration in those three classes. Stokes keeps a separate preconditioner matrix and is unchanged.

Because the boundary term now reaches the Jacobian only, those three solvers refuse a preconditioner matrix apart from the Jacobian while natural BCs are set. Before each solve they set up the SNES, and if the two matrices differ they raise a message naming the solver and #1070. That case would otherwise miss the term silently, and under -snes_mf_operator it already failed with PETSc error 56. A solve with one matrix, or without natural BCs, is unchanged.

Fixes #1065 and #1070 (both labelled fixed-in-PR; relabelled fixed-in-development on merge).

What changes, measured

The review paired 459 solves over 24 test files, branch against base. Only 4 solves changed, all in test_1162 and all toward fewer iterations:

Solve Base: Newton / KSP its Branch: Newton / KSP its
Poisson, Robin k = -1 17 / 132 2 / 14
Poisson, Robin k = -10 23 / 170 2 / 13
Poisson, Robin k = 2 19 / 258, DIVERGED_LINE_SEARCH 2 / 16
Vector projection, Robin 19 / 124 1 / 9

The other 455 solves are identical, including the adjoint KSP counts in test_0019 and test_1145. None of the existing files ran a scalar, vector or multi-component solve whose natural BC reads the unknown. So they show no collateral change, but they do not exercise the fix; test_1162 does.

Against a column-by-column finite-difference Jacobian, the base build's natural-BC term is 2.0000000001 times the finite difference in all three solver families. That holds for Robin, penalty, T², a gradient-reading BC and vector Nitsche. On the branch the ratio is 1.0000000000. A constant flux is unchanged.

For Stokes, J and P stay separate, with the boundary term once in each. Robin, nonlinear, penalty and Nitsche cases are bit-identical to base.

A Robin adjoint gradient now matches finite differences:

  • at k = 2, dJ/dp is -5.8333333299e-02 against FD -5.8333333320e-02;
  • at k = 0.5, 1.1666666665e-01 against 1.1666666666e-01, where base failed with DIVERGED_LINEAR_SOLVE.

A ThinSheet with a Robin condition, plain or rotated, went from 45 iterations with a line-search failure (or 26, not converged) to 1.

Test

test_1162 (tier B, 11 tests):

All fail on development; the refusal cases fail there with error 56. Putting the doubled registration back in SNES_MultiComponent alone fails the two multi-component cases (ratio 2.0000000005, 19 iterations). With the guard disabled, the three refusal cases fail. test_1162 passed at np 1, 2 and 4 at 1ef53b2. With the guard, the natural-BC regression set (24 files plus 4 projection files, serial) gives 224 passed and 1 xfail, the known one.

Review

H1's adversarial review returned "merge", with one condition (F1): no case used SNES_MultiComponent. The multi-component cases above answer it. H1 re-checked 1ef53b2: one mutant per family fails only its own cases. H1 re-checked the guard at 82522d3: across 512 solves paired against 1ef53b2 in 32 files, none changed its Newton or KSP count or its reason, nor its PETSc PCSetUp, KSPSetUp, SNESJacobianEval, SNESFunctionEval or SNESSolve counts. The guard's setUp moves out of SNESSolve rather than adding one. test_1162 passes at np 1, and at np 2 and 4 on every rank, where the refusal is uniform and does not hang. H1's record goes on this PR once their posting is cleared.

The review's F3 (a separate preconditioner matrix gets no natural-BC term, opened by this PR) is closed here by the refusal; F4 (-snes_mf_operator with a natural BC fails with error 56) becomes that message. Both are #1070. Making -snes_mf_operator work with natural BCs is not attempted.

Left open, filed:

This changes the forward Jacobian, so it merges on Louis's decision.

Underworld development team with AI support from Claude Code

SNES_Scalar, SNES_Vector and SNES_MultiComponent registered each natural BC's
boundary Jacobian as the Jacobian and again as the preconditioner, with the same
kernels. Their Jacobian and preconditioner are one matrix, and both
registrations are assembled into it, so the term df0/du of a natural BC that
reads the unknown entered twice. Measured against a column-by-column
finite-difference Jacobian, the hand-coded Robin part was 1.9999999996 times the
true one. The residual was right, so converged answers were exact, but a linear
Poisson problem with a Robin condition took 17 to 23 Newton iterations or
failed, a vector projection took 19, and an adjoint that transposes the
assembled Jacobian carried the factor. Stokes keeps a separate preconditioner
matrix and is unchanged.

The preconditioner registration is dropped in those three classes.

test_1162 (tier B): a linear Robin problem's first Newton step cuts the
residual to the linear solve's tolerance (development: ~0.3 a step), at the
exact top value of its 1-D profile, for k = -1, -10 and 2; the Robin part of
the Jacobian matches finite differences; a linear vector projection with a
Robin condition likewise. All five fail on development.

Underworld development team with AI support from Claude Code
Review F1: putting the doubled registration back in SNES_MultiComponent alone
left test_1162 passing, because no case used that solver. The finite-difference
check now runs on the scalar, vector and multi-component solvers, and the
one-Newton-step check on a vector and a multi-component projection with a Robin
condition. With the doubled registration back in SNES_MultiComponent alone, the
multi-component cases fail: the Robin part is 2.0000000005 times the finite
difference and the projection takes 19 iterations. Eight tests, all pass on
the branch.

Underworld development team with AI support from Claude Code
Underworld development team with AI support from Claude Code
The finite-difference test builds each SNES with a solve stopped at 0
iterations, which since #1062 warns that the solve diverged. The test now
expects that warning, so it runs clean under -W error::RuntimeWarning.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 11, 2026 15:17

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.

…#1070)

This branch registers a natural BC's boundary Jacobian on the Jacobian only,
because SNES_Scalar, SNES_Vector and SNES_MultiComponent assemble the Jacobian
and the preconditioner into one matrix. A preconditioner matrix apart from the
Jacobian would then get no boundary term, and under -snes_mf_operator the
boundary assembly writes into the matrix-free operator and fails with PETSc
error 56 (on development too).

Before each solve, those three solvers set up the SNES and, if natural BCs are
registered and the Jacobian and preconditioner are different matrices, raise a
RuntimeError that names the solver and #1070. A solve without natural BCs, or
with one matrix, is unchanged. Stokes keeps its separate preconditioner and is
not affected.

test_1162: -snes_mf_operator with a Robin condition is refused, in each of the
three solvers (development: error 56); with the guard disabled, all three fail.
The natural-BC regression set gives 224 passed and 1 xfail, the known one.

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.

2 participants