Speed up (block) matrix assembly by merging SparsityPattern of the blocks

For this you should be able to just call

fem.petsc.assemble_matrix(
        mat,
        fem.form(form),
        bcs=bcs,
        diagonal=diagonal,
    )

and as shown in:

you can create your sparsity pattern (and send that into the create_matrix function)

sp = fem.create_sparsity_pattern(form)
sp.insert_diagonal(zero_blocks)
sp.assemble()
end = time.perf_counter()
A = cpp.la.petsc.create_matrix(mesh.comm, sp)

Here I used insert_diagonal as I only wanted diagonal entries, but you can use sp.insert to insert combinations of row and column entries: https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/wrappers/la.cpp#L254-L263

1 Like