I am trying to apply Dirichlet boundary conditions to a matrix generated via petsc4py
. E.g., this code,
import dolfin as dlf
from petsc4py import PETSc
mesh = dlf.UnitSquareMesh(100, 100)
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) # PETSc error with np >= 3
v.setType('standard')
v.setValues(range(N), [N]*N)
B_pet = PETSc.Mat()
B_pet.createAIJ([N,N], nnz=N)
B_pet.setDiagonal(v)
lgmap = PETSc.LGMap().create(W.dofmap().dofs())
B_pet.setLGMap(lgmap, lgmap)
B_pet.assemblyBegin()
B_pet.assemblyEnd()
B = dlf.PETScMatrix(B_pet)
bc.apply(B) # DOLFIN error with np = 2
works when run in one process, but generates the following error when run on 2 processes:
*** Error: Unable to apply boundary condition.
*** Reason: Dimension of function space (10201) too large for application of boundary conditions to linear system (5151 rows).
*** Where: This error was encountered inside BoundaryCondition.cpp.
and the following on 3 or more processes:
petsc4py.PETSc.Errorpetsc4py.PETSc.Error File "PETSc/Vec.pyx", line 158, in petsc4py.PETSc.Vec.setSizes (src/petsc4py.PETSc.c:88897)
: error code 62
[0] VecSetSizes() line 1381 in /home/miguel/SourceCode/petsc/src/vec/vec/interface/vector.c
[0] Invalid argument
[0] Int value must be same on all processes, argument # 3
I know this has to do with the way the matrices/vectors are split between the different processes, but am not sure how to fix it.