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

Hi All,

the demos and unit tests show how to assemble a matrix or vector with BCs applied if I have a form available (linear ufl form L and bilinear ufl form a below; form is the compiled dolfinx form of a).

# taken from demo_elasticity.py

# Assemble system, applying boundary conditions and preserving symmetry
A = assemble_matrix(form, [bc])
A.assemble()

b = assemble_vector(L)
apply_lifting(b, [a], [[bc]])
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)
set_bc(b, [bc])

Assume that I have some PETSc.Mat and PETSc.Vec available which I assembled e.g. with the use of a custom assembler. How would I go about setting BCs (homogenoeus and inhomogeneous) to these objects?

For the vector I guess I could still use dolfinx.fem.set_bc in the homgenoues case, but dolfinx.fem.apply_lifting requires a form representing the left hand side and cannot be used here to deal with the case of inhomogeneous dirichlet BCs. For the matrix I didn’t find anything in the demos or unit tests to set BCs to a after assembly.

Thanks,
Philipp

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)

Hi dokken,

Thank you for your answer. That surely solves the problem and it makes sense to apply the bcs during assembly rather than accessing the values again afterwards.

May I ask a question regarding the custom assembler?

I have a background in solid mechanics and I thought that the custom assembler may enable the user to implement material behaviour which requires solution of a constitutive equation on integration point level (like e.g. incremental plasticity).

Would that be a typical use of the custom assembler or what would a typical problem where a custom assembler is necessary look like?

The custom assembler can be used for anything, such as the problem you describe.
I would use a custom assembly whenever using standard UFL form language does not fully encapsulate what you would like to achieve.

For instance, I use it to enforce multi-point constraints , see for instance my assemble_matrix method.

There are many different levels of custom assembly, such as custom kernels (@jpdean might have some examples of this) , or custom addition to the matrix (as I do in my case).

Hi, is the linked presentation still around somewhere? the link is dead now unfortunately. I remember it was pretty helpful. Thanks!

You mean: FEniCS 2021: Linear multipoint constraints in FEniCSx ?

Sorry, i was actually trying to reply to your other response and to ask for the slides on the inhomogeneous Dirichlet BC application

No, but consider: https://hal.science/hal-03741809/document

1 Like