I am trying to understand why boundary conditions cannot be applied to a matrix originally built using petsc4py
, even after wrapping it with dolfin
's PETScMatrix
wrapper. This code shows how to create the error.
import dolfin as dlf
from petsc4py import PETSc
mesh = dlf.UnitSquareMesh(2,2)
W = dlf.FunctionSpace(mesh, 'CG', 1)
def boundary(x, on_boundary):
return on_boundary
bc = dlf.DirichletBC(W, dlf.Constant(0.0), boundary)
N = mesh.num_vertices()
v = PETSc.Vec()
v.create()
v.setSizes(N)
v.setType('standard')
v.setValues(range(N), [N]*N)
B_pet = PETSc.Mat()
B_pet.createAIJ([N,N], nnz=N)
B_pet.setDiagonal(v)
B_pet.assemblyBegin()
B_pet.assemblyEnd()
B = dlf.PETScMatrix(B_pet)
bc.apply(B) # error
The error given by this code is:
*** Error: Unable to set given (local) rows to identity matrix.
*** Reason: some diagonal elements not preallocated (try assembler option keep_diagonal).
*** Where: This error was encountered inside PETScMatrix.cpp.