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

Issue\n\nFunctionSpace.clone() creates a distinct FunctionSpace object with a separate identity while sharing the same mesh and dofmap. This is useful for block formulations, but it is ambiguous for standalone matrix assembly with DirichletBCs.\n\nFor a bilinear form whose test and trial spaces are cloned from one another, a DirichletBC on either space is applied during row/column zeroing because the spaces share the same dofs. The diagonal insertion step, however, only runs when the test and trial FunctionSpace objects are identical. The constrained row/column can therefore be left without the expected diagonal entry.\n\nFix\n\nMove the validation into the C++ assemble_matrix(..., bcs) path. The check detects the cloned-space case only when the form is rank 2, the test and trial spaces are distinct objects, they are on the same mesh, they share the same single dofmap and element signature, and a DirichletBC belongs to either space. In that standalone assembly case, assembly now raises a clear error before producing an invalid constrained matrix.\n\nPETSc block and MatNest sub-assembly bypass this standalone check because cloned spaces are valid there and diagonal handling is performed at the block level.\n\nAdds regression coverage for both MatrixCSR and PETSc assembly.\n\nSplit out from #4249 per maintainer request.

@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.

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