Skip to content

Allow prepared BC data in Python matrix assemblers - #4648

Draft
garth-wells wants to merge 9 commits into
mainfrom
python/prepared-matrix-bcs
Draft

garth-wells wants to merge 9 commits into
mainfrom
python/prepared-matrix-bcs

Conversation

@garth-wells

@garth-wells garth-wells commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Repeated Python matrix assembly currently rebuilds constrained-dof markers and diagonal data from DirichletBC objects on every call. Allow bcs to accept either a BC sequence or reusable MatrixBCData, with public prepare_matrix_bcs factories for MatrixCSR and PETSc matrices. Both matrix-creation and in-place assembly overloads reuse prepared data; the CSR callback assembler also accepts its markers.

BC sequences use a diagonal value of 1 by default. Explicit legacy diag arguments remain honoured with a deprecation warning; custom diagonal values can instead be supplied during preparation. Prepared data includes PETSc/MATIS diagonal rows and sharing-adjusted values, and cannot be combined with diag. Local validation checks function spaces, marker layouts and relevant PETSc diagonal layouts.

Validation:

  • Ruff lint and format checks passed for all changed Python files.
  • Existing assembly, custom assembly, MATIS and PETSc diagonal tests: 270 passed, 3 skipped.
  • New regression tests: 34 passed in serial and on two MPI ranks, covering repeated assembly, scalar/vector spaces, CSR real/complex types, rectangular forms, callbacks, blocked/nested PETSc matrices, MATIS and invalid prepared data.
  • Warnings-as-errors verification of the new tests and affected existing tests: 44 passed in serial and on two MPI ranks.
  • Python documentation built successfully with Sphinx warnings treated as errors, reusing generated demo images.
  • Pyrefly with installed C++ stubs: no new diagnostics compared with the unchanged code; 12 existing errors remain.

AI assistance: Codex assisted with the implementation, tests and PR description.

@garth-wells garth-wells added performance python Pull requests that update Python code labels Oct 18, 2026
garth-wells and others added 5 commits October 24, 2026 13:57
- Share prepared-data validation between the CSR assemblers and
  assemble_matrix_fn, and validate only prepared data, not data
  just built from boundary conditions.
- Build the PETSc MatrixBCData from the private marker and diagonal
  records instead of flattening and repacking them on every assembly.
- Cache PETSc block index sets in prepared data and in LinearProblem,
  rather than rebuilding them on every blocked assembly.
- Attribute the diag DeprecationWarning to the caller, so it is shown
  under default warning filters.
- Raise TypeError when prepared data for the other matrix type is
  passed, instead of iterating it as boundary conditions.
- Check prepared diagonal rows (dtype, local range) and values (shape,
  scalar type), and reject a complex diag for a real scalar type.
- Document the per-block diagonal value layout, including MATIS shares.
- fem.LinearProblem caches prepared CSR BC data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- fem.MatrixBCData and the PETSc prepared data are frozen, slotted
  dataclasses rather than named tuples, so they are not sequences
  (and cannot be mistaken for a sequence of boundary conditions), and
  field order is not part of the API.
- Rename the PETSc class to BlockMatrixBCData, distinct from
  fem.MatrixBCData.
- Both derive from a private base, so the foreign-data check is a
  plain isinstance test rather than a named-tuple attribute probe.
- Tests use dataclasses.replace, and check both cross-backend
  directions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant