Skip to content

Guard cloned-space BC assembly in C++ - #4255

Open
masaqif wants to merge 1 commit into
FEniCS:mainfrom
masaqif:fix-petrov-galerkin-bc-validation
Open

Guard cloned-space BC assembly in C++#4255
masaqif wants to merge 1 commit into
FEniCS:mainfrom
masaqif:fix-petrov-galerkin-bc-validation

Conversation

@masaqif

@masaqif masaqif commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

When a standalone bilinear form uses distinct cloned FunctionSpaces, Dirichlet boundary conditions can zero constrained rows or columns while diagonal insertion is skipped: the latter only runs when the test and trial spaces have the same identity. This can leave the constrained matrix without the expected diagonal entry.

Add validation in the shared C++ assemble_matrix(..., bcs) path for distinct spaces on the same mesh that share a single dofmap and element signature. If a boundary condition belongs to either space, standalone assembly raises an error before assembling the matrix. PETSc block and MatNest sub-assembly bypass this check because diagonal handling occurs at the block level.

Regression tests cover MatrixCSR and PETSc assembly, scalar and vector spaces, and boundary conditions on either the trial or test space.

Validation at 1df8c57, rebased onto 700e977:

  • C++ library and Python bindings built in Developer mode in a Linux ARM64 container with PETSc 3.25.4 (linux-gnu-real64-32).
  • test_assembler.py, test_bcs.py, and test_assemble_submesh.py: 366 passed, 1 xfailed in serial; 365 passed, 1 skipped, 1 xfailed per rank with three MPI ranks.
  • clang-format, Ruff check/format, compileall, and git diff --check passed.
  • Strict library mypy reports three identical diagnostics on this branch and unchanged upstream 700e977 in the local environment; no additional diagnostics from this PR.

Split from #4249 per maintainer request.

AI assistance: OpenAI Codex assisted with the implementation, rebase, testing, and PR text.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from c004515 to 00a158a Compare July 1, 2026 15:05
@masaqif masaqif changed the title Validate BCs for Petrov-Galerkin matrix assembly Validate BCs for equivalent but distinct matrix spaces Jul 1, 2026
@masaqif

masaqif commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

I narrowed the validation after CI showed that general different-degree and submesh Petrov-Galerkin assembly with BCs is supported and already covered by tests. The current check now targets only equivalent same-mesh spaces represented by distinct FunctionSpace objects, and it is bypassed for recursive MatNest sub-block assembly so existing block formulations are not rejected.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from 00a158a to bb8ae42 Compare July 2, 2026 15:15
@masaqif

masaqif commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the CI issues: the BC validation helper now checks the C++ finite-element signature instead of calling the Python-only ufl_element(), and petsc.py has been formatted locally with Ruff.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from bb8ae42 to 59833d1 Compare July 3, 2026 15:07
@masaqif masaqif changed the title Validate BCs for equivalent but distinct matrix spaces Validate BCs for distinct matrix spaces sharing a dofmap Jul 3, 2026
@masaqif

masaqif commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Updated again after the latest CI results. The validation is now limited to distinct spaces that share the same dofmap, which keeps the existing submesh and mixed-domain assembly tests valid. I also updated the mypy ignore for the private recursive MatNest call and verified Ruff plus the separate CI-style mypy commands locally.

@garth-wells

Copy link
Copy Markdown
Member

Could you open the PR with a clear statement of the issue, and then how the PR fixes it? I don't know what "Reject Dirichlet boundary conditions ..." means.

Also, note that checks that can be performed in C++ should be done in C++ functions rather than in the Python layer.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from 59833d1 to 6d8a06e Compare July 9, 2026 15:07
@masaqif masaqif changed the title Validate BCs for distinct matrix spaces sharing a dofmap Guard cloned-space BC assembly in C++ Jul 9, 2026
@masaqif

masaqif commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, updated. The PR description now starts with the concrete issue and then explains the fix. I also moved the validation from Python into the shared C++ assemble_matrix(..., bcs) path. Python now only passes check_bcs=false for PETSc block/MatNest sub-assembly, where cloned spaces are valid and diagonal handling is performed at the block level.\n\nLocally checked: clang-format, Ruff, mypy for dolfinx/demo/test, compileall, and git diff --check.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from 6d8a06e to 3e1d848 Compare July 9, 2026 15:52
@masaqif

masaqif commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: fixed the docs failure by documenting the new C++ helper and pushed the amended commit.

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from 3e1d848 to 80506d9 Compare August 3, 2026 15:05
@masaqif

masaqif commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Quick status update: I rebased this onto current main, the PR is mergeable again, and all CI checks are passing, including Spack. The earlier feedback has been addressed by moving the validation into C++ and clarifying the issue/fix in the PR description. Is there anything else you'd like changed before review?

@masaqif
masaqif force-pushed the fix-petrov-galerkin-bc-validation branch from 80506d9 to 1df8c57 Compare September 9, 2026 07:35
@masaqif

masaqif commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 700e977 and pushed 1df8c57; GitHub now reports the PR as mergeable. The conflict resolution preserves the newer scalar PETSc insertion optimization and optional-argument handling, and forwards the BC-validation flag through scalar, blocked, and unrolled insertion paths.

The regression tests now cover scalar/vector spaces and boundary conditions on either the trial or test space. I built the C++ core and Python bindings in Developer mode in a Linux ARM64 container with PETSc 3.25.4 (linux-gnu-real64-32), then ran:

python3 -m pytest -q python/test/unit/fem/test_assembler.py python/test/unit/fem/test_bcs.py python/test/unit/fem/test_assemble_submesh.py
mpirun -np 3 python3 -m pytest -q python/test/unit/fem/test_assembler.py python/test/unit/fem/test_bcs.py python/test/unit/fem/test_assemble_submesh.py

Results: 366 passed, 1 xfailed in serial; 365 passed, 1 skipped, 1 xfailed on each of three MPI ranks. clang-format, Ruff check/format, compileall, and diff checks also passed.

Strict library mypy reports the same three diagnostics on this branch and unchanged upstream 700e977: an unused ignore in dolfinx/la/__init__.py and two CFFI Lib attribute diagnostics for MatSetValuesLocal/MatSetValuesBlockedLocal. There are no additional diagnostics from this PR in that comparison.

The PR description is now formatted with actual paragraphs and updated validation details. Fresh CI has started and its lint job has passed; the remaining build/test jobs are still running.

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