I will illustrate this on a unit square (with a non-zero condition to show that it is applied).
In the following example I have constrained the x component at (0.5,0.5) to be 0.3:
from mpi4py import MPI
import dolfinx
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (2,)))
def single_dof(x):
return np.isclose(x[0], 0.5) & np.isclose(x[1], 0.5)
constrained_vertex = dolfinx.mesh.locate_entities(mesh, 0, single_dof)
constrained_dof = dolfinx.fem.locate_dofs_topological(V.sub(0), 0, constrained_vertex)
u_bc = dolfinx.fem.Constant(mesh, 0.3)
bc_x = dolfinx.fem.dirichletbc(u_bc, constrained_dof, V.sub(0))
uh = dolfinx.fem.Function(V)
uh.x.array[:] = 0.0
dolfinx.fem.set_bc(uh.x.array, [bc_x])
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "output.xdmf", "w") as xdmf:
xdmf.write_mesh(mesh)
xdmf.write_function(uh)