Repository navigation
A natural BC that reads the unknown enters the Jacobian once (#1065) - #1068
Merged
Merged
Conversation
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
This was referenced Nov 11, 2026
…#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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_operatorit 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:
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:
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):
-snes_mf_operatorwith a Robin condition is refused with the Scalar/vector/multi-component solvers: a separate preconditioner matrix gets no natural-BC term, and -snes_mf_operator with a natural BC fails (error 56) #1070 message, in all three solvers.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_operatorwith a natural BC fails with error 56) becomes that message. Both are #1070. Making-snes_mf_operatorwork 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