Please make the example reproducible. Currently you have only posted snippets of code, which doesn’t make it easy for people to reproduce the problem.
An example of how to make your code reproducible is:
import dolfinx
from mpi4py import MPI
import ufl
from petsc4py import PETSc
import numpy as np
domain = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
Ve = ufl.VectorElement("CG", domain.ufl_cell(), 2)
Pe = ufl.FiniteElement("CG", domain.ufl_cell(), 1)
We = ufl.MixedElement([Ve, Pe])
W = dolfinx.fem.FunctionSpace(domain, We)
def no_slip(x):
return np.logical_or(np.logical_or(np.isclose(x[0], 0), np.isclose(x[0], 1)),
np.isclose(x[1], 0))
noslip_v = np.zeros(domain.geometry.dim, dtype=PETSc.ScalarType)
facets = dolfinx.mesh.locate_entities_boundary(domain, 1, no_slip)
bc_noslip = dolfinx.fem.dirichletbc(
noslip_v, dolfinx.fem.locate_dofs_topological(W.sub(0), 1, facets), W.sub(0))
The issue here is that the block size of W.sub(0)
is 1. You can check this by looking at W.sub(0).dofmap.bs
. This means that it has unrolled all it’s dofs with respect to the dimension of the vector space.
To create a bc for your case, I would:
from IPython import embed
import dolfinx
from mpi4py import MPI
import ufl
from petsc4py import PETSc
import numpy as np
domain = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
Ve = ufl.VectorElement("CG", domain.ufl_cell(), 2)
Pe = ufl.FiniteElement("CG", domain.ufl_cell(), 1)
We = ufl.MixedElement([Ve, Pe])
W = dolfinx.fem.FunctionSpace(domain, We)
def no_slip(x):
return np.logical_or(np.logical_or(np.isclose(x[0], 0), np.isclose(x[0], 1)),
np.isclose(x[1], 0))
# Set zero BC
noslip_v = np.zeros(domain.geometry.dim, dtype=PETSc.ScalarType)
V, _ = W.sub(0).collapse()
u_bc = dolfinx.fem.Function(V)
u_bc.x.set(0)
facets = dolfinx.mesh.locate_entities_boundary(domain, 1, no_slip)
bc_noslip = dolfinx.fem.dirichletbc(
u_bc, dolfinx.fem.locate_dofs_topological((W.sub(0), V), 1, facets), W.sub(0))
w = dolfinx.fem.Function(W)
# Fill with non-zero data to illustrate input
w.x.set(2)
dolfinx.fem.petsc.set_bc(w.vector, [bc_noslip])
with dolfinx.io.VTXWriter(domain.comm, "output.bp", [w.sub(0).collapse()]) as vtx:
vtx.write(0)