Skip to content

FEM: Fix 'zerob' BC in hcurl_grad_getmop - #14

Closed
hansec wants to merge 1 commit into
mainfrom
fix-hcurl-grad-zerob-mop
Closed

hansec wants to merge 1 commit into
mainfrom
fix-hcurl-grad-zerob-mop

Conversation

@hansec

@hansec hansec commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Primary changes

Fixes the 'zerob' boundary-condition branch of hcurl_grad_getmop (src/fem/hcurl_grad_operators.F90). Nothing in the repository calls this branch yet. Any use of it corrupted the heap ("double free or corruption"), which made it unusable:

  • Out-of-bounds write: Grad(H^1) boundary rows were zeroed in mop12(jr,:), but mop12 is sized (hcurl nce, hgrad nce). They are now zeroed in mop21(jr,:). The loop also now starts at cell_np+1, since vertex rows are already zeroed. As a result, every global-boundary row is zeroed in both blocks: mop11/mop12 for H(Curl) and mop21/mop22 for Grad(H^1).
  • Wrong block: unit diagonals for Grad(H^1) Dirichlet rows were added to block (1,1). They now go into block (2,2).
  • Inverted vertex ownership: vertex diagonals were only added for vertices shared with other processors, and for none in serial runs. They now follow the 'none' pattern: all non-processor-boundary vertices, plus shared vertices on the owning processor. The boundary-DOF loop now skips vertex DOFs, so each vertex gets exactly one unit diagonal.

Adds a regression test to src/tests/fem/test_hcurl_grad.{F90,py} (test_zerob_proj). It L2-projects an analytic field whose tangential component vanishes on the cube boundary, using the 'zerob' mass matrix, at orders 1–3, in serial and with MPI. It checks that the CG solve converges (true relative residual < 1e-8) and that the projection error matches reference values: 2.756e-2, 2.097e-3 and 1.460e-4 for orders 1, 2 and 3.

Secondary changes

None.

API changes

No API or input-file changes. The test program test_hcurl_grad has a new optional namelist flag, zerob_test, which defaults to F.

Testing

Built against Ubuntu 24.04 system libraries with OpenMPI and OMP_NUM_THREADS=2.

  • Before the fix, all 6 new test cases crash with heap corruption or a glibc malloc assertion. After the fix, all 6 pass.
  • The full fem suite passes: 313 passed, 221 skipped (the PETSc variants, since PETSc was not in the build), 0 failed.
  • The Fortran stack lint (generate_stack.py -l) is clean.

Generated by Claude Code

Fix several bugs in the 'zerob' boundary-condition branch of the
H(Curl) + Grad(H^1) mass matrix construction:

- Zero global-boundary Grad(H^1) rows in mop21 instead of mop12. The old
  code indexed mop12 (size hcurl nce x hgrad nce) with Grad(H^1) row
  indices, writing out of bounds and corrupting the heap. The loop now
  starts at cell_np+1 since vertex rows are already zeroed.
- Add unit diagonals for Grad(H^1) Dirichlet rows to block (2,2) rather
  than block (1,1).
- Add vertex Grad(H^1) diagonals following the 'none' pattern (interior
  vertices plus owned processor-boundary vertices), instead of only
  vertices shared with other processors, and skip vertex DOFs in the
  boundary-DOF loop so each gets exactly one unit diagonal.

Add a regression test to test_hcurl_grad that L2-projects an analytic
field with vanishing tangential boundary component using the 'zerob'
mass matrix at orders 1-3, serial and MPI, checking solver convergence
and projection error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NKQvs9uG822LSFyCgTSuSz
@hansec hansec closed this Nov 17, 2026
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