How to set BCs for PETSc Matrices and PETSc Vectors in dolfinx

Hi,

I would suggest you look into how to implement lifting in your custom assembly.
For instance have a look at this presentation on inhomogeneous Dirichlet boundary conditions.

To make a short summary of the method, what one does is:

  • During matrix assembly, remove each column and row corresponding to a Dirichlet dof
  • Add 1 on the diagonal of the matrix.
  • Assemble the vector L without DirichletBC
  • Apply lifting, aka. L -= Ag, where A is the assembled matrix with all entries (also those that are Dirichlet conditions), g is a vector equal to the inhomogeneous Dirichlet BC at dofs with this BC, and zero elsewhere. The lifting approach can be done element wise, and reduces to an assembly over all cells with a Dirichlet BC, (see for instance https://github.com/FEniCS/dolfinx/blob/0dc1828741fc6aceff182ed7a6c48a039206cb46/cpp/dolfinx/fem/assembler.h#L62-L70)