Skip to content

fix Nitsche symmetry term on cylindrical and spherical meshes - #1289

Open
hhy2022 wants to merge 2 commits into
festim-dev:mainfrom
hhy2022:fix-nitsche-non-cartesian
Open

hhy2022 wants to merge 2 commits into
festim-dev:mainfrom
hhy2022:fix-nitsche-non-cartesian

Conversation

@hhy2022

@hhy2022 hhy2022 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Description

Summary

The symmetry term of weakly enforced Dirichlet BCs (Nitsche) is now written like the bulk diffusion term in cylindrical and spherical coordinates:

Coordinate system Before After
cartesian n·grad(v) n·grad(v) (unchanged)
cylindrical n·grad(v) r n·grad(v / r)
spherical n·grad(v) r**2 n·grad(v / r**2)

This follows the diffusion term in HydrogenTransportProblem.create_formulation and festim.drift.drift_form. DirichletBCBase.weak_formulation takes a new coordinate_system argument (default cartesian), passed by both HydrogenTransportProblem and HydrogenTransportProblemDiscontinuous.

Related Issues

Fixes #1278

Motivation and Context

In cylindrical coordinates, conservation comes from the test function v = r, which is in the P1 space. The bulk term r grad(u)·grad(v/r) vanishes for it, but the symmetry term n·grad(v) does not (grad(r) = 1), so particles are not conserved when a weakly enforced HenrysBC / SievertsBC is coupled to an enclosure.

With the MWE posted in #1278 (largest relative change of the total number of H atoms):

Henry before Henry after Sieverts before Sieverts after
cartesian 2.6e-11 2.6e-11 1.4e-16 1.4e-16
cylindrical 3.7e-06 5.3e-11 4.4e-07 2.8e-16
spherical 1.7e-04 1.8e-04 2.4e-05 2.6e-05

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 🔨 Code refactoring (no functional changes, no API changes)
  • 📝 Documentation update
  • ✅ Test update (adding missing tests or correcting existing tests)
  • 🔧 Build/CI configuration change

Testing

  • All existing tests pass locally (pytest)
  • I have added new tests that prove my fix is effective or that my feature works

Code Quality Checklist

  • My code follows the code style of this project (Ruff formatted: ruff format .)
  • My code passes linting checks (ruff check .)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas

Documentation

  • I have updated the documentation accordingly (if applicable)
  • I have added docstrings to new functions/classes following the project conventions

Additional Notes

Spherical is not exact, and cannot be with P1 elements. Conservation would need v = r**2 in the test space. The remaining drift is the same as with SurfaceReactionBC (no Nitsche) and is O(h^2):

n cells Henry before Henry after SurfaceReactionBC
60 1.72e-04 1.83e-04 1.85e-04
120 4.29e-05 4.58e-05 4.63e-05
240 1.07e-05 1.14e-05 1.16e-05

The spherical symmetry term is changed for consistency with the bulk term.

With assistance from AI.

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.15%. Comparing base (1bd7a80) to head (2e567ff).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1289   +/-   ##
=======================================
  Coverage   96.14%   96.15%           
=======================================
  Files          57       57           
  Lines        5165     5177   +12     
=======================================
+ Hits         4966     4978   +12     
  Misses        199      199           
Flag Coverage Δ
dolfinx-v0.10.0 93.27% <100.00%> (+0.01%) ⬆️
dolfinx-v0.11.0 96.02% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hhy2022
hhy2022 marked this pull request as ready for review November 5, 2026 16:38
@RemDelaporteMathurin

Copy link
Copy Markdown
Collaborator

Not quite sure I understand what the Nitsche method cannot be fixed in spherical too here. @jorgensd any pointer?

@hhy2022

hhy2022 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

The Nitsche term is fixed in spherical too. The drift that remains doesn't come from the BC but from the bulk formulation with P1 elements (element_degree=1), and it is the same with SurfaceReactionBC.

FESTIM writes the spherical diffusion term as r**2 * grad(u)·grad(v / r**2), i.e. it tests with v / r**2. Summing the discrete equations into a particle balance needs the test function v = r**2 (like v = r in cylindrical). r is in P1 but r**2 isn't, so with P1 the balance can't be exact in spherical, whatever the BC.

With P2 elements (element_degree=2) r**2 is in the space, and spherical becomes exact with this PR (same MWE as in #1278, 60 cells, largest relative change of the total number of H atoms):

spherical P1 P2
HenrysBC, main 1.72e-04 1.95e-07
HenrysBC, this PR 1.83e-04 2.77e-11
SurfaceReactionBC 1.85e-04 2.69e-15

2.77e-11 is the same level as cartesian (2.59e-11), set by the solver tolerance.

Making spherical exact with P1 would need the formulation written the other way, r**2 * grad(u)·grad(v) with every term weighted by r**2 (time derivative, sources, fluxes, ...), so that the conserved test function is v = 1. That changes every spherical simulation, so I'd keep it out of this PR. Happy to open an issue if you think it's worth it.

Side note: to run the P2 case I had to pass element_degree in HydrogenTransportProblemDiscontinuous.initialise, which calls define_function_spaces(subdomain) without it, so Settings(element_degree=2) is currently ignored in the discontinuous problem.
With assistance from AI.

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.

[BUG] Weakly enforced BCs not exact with non-cartesian meshes

2 participants