Skip to content

Fix Poisson solver natural (Neumann) boundary conditions#60

Merged
lmoresi merged 1 commit intodevelopmentfrom
bugfix/poisson-natural-bc
Feb 28, 2026
Merged

Fix Poisson solver natural (Neumann) boundary conditions#60
lmoresi merged 1 commit intodevelopmentfrom
bugfix/poisson-natural-bc

Conversation

@lmoresi
Copy link
Member

@lmoresi lmoresi commented Feb 28, 2026

Summary

  • Fixed two bugs preventing natural BCs from working in the scalar Poisson solver (SNES_Scalar), resolving issue [BUG] -AttributeError: 'NaturalBC' object has no attribute 'fn_F' when using add_natural_bc #25
  • The boundary label in _setup_discretisation used per-boundary names ("Bottom") instead of the consolidated "UW_Boundaries" label
  • The _setup_solver method was missing the code to register compiled boundary residual/jacobian functions with PETSc — the SNES_Vector (Stokes) solver had this code, but SNES_Scalar did not
  • Removed the @pytest.mark.skip from the Poisson natural BC test, which now passes on all mesh types

Test plan

  • test_1000_poissonNaturalBC.py — 3/3 pass (simplex regular, simplex irregular, quad)
  • test_1000_poissonCart.py — 7/7 pass (Dirichlet-only Poisson, no regression)
  • test_1010_stokesCart.py — 6/6 pass (Stokes solver, no regression)
  • test_1011_stokesSph.py — 3/3 pass (Stokes with natural BCs, no regression)
  • Reproduced the exact user scenario from issue [BUG] -AttributeError: 'NaturalBC' object has no attribute 'fn_F' when using add_natural_bc #25 — solver converges correctly

Closes #25

Underworld development team with AI support from Claude Code

Two bugs prevented natural BCs from working in the scalar Poisson solver
(SNES_Scalar), while the Stokes solver (SNES_Vector) worked correctly:

1. _setup_discretisation registered natural BCs with per-boundary labels
   (e.g., "Bottom") instead of the consolidated "UW_Boundaries" label
   that _setup_solver expects.

2. _setup_solver had a placeholder comment but no code to register the
   compiled boundary residual/jacobian functions with PETSc via
   UW_PetscDSSetBdResidual/BdJacobian/BdJacobianPreconditioner.

The fix mirrors the working SNES_Vector pattern. Verified against an
analytical solution (T = x²y) on simplex and quad meshes.

Closes #25

Underworld development team with AI support from Claude Code
Copilot AI review requested due to automatic review settings February 28, 2026 21:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes natural (Neumann/flux) boundary-condition handling for the scalar Poisson SNES solver (SNES_Scalar), aligning it with the existing (working) natural-BC setup used by the vector/Stokes SNES solvers and re-enabling the previously skipped regression test.

Changes:

  • Use the consolidated PETSc DMLabel "UW_Boundaries" when adding natural BCs in SNES_Scalar._setup_discretisation.
  • Register compiled boundary residual/Jacobian functions with PETSc in SNES_Scalar._setup_solver (previously missing).
  • Unskip and re-enable test_1000_poissonNaturalBC.py.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_1000_poissonNaturalBC.py Re-enables the natural-BC Poisson regression test.
src/underworld3/cython/petsc_generic_snes_solvers.pyx Fixes scalar natural BC label usage and adds PETSc DS boundary term registration for compiled natural-BC functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lmoresi lmoresi merged commit 2a7c2b7 into development Feb 28, 2026
5 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.

2 participants