How can I find the closest node to an arbitrary coordinates?

Dirichlet BCs are applied on the DOFs of the FunctionSpace. These coincide with the mesh vertices for P1 elements.

Why not something simple such as the following (not for parallel):

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(16, 16)
V = FunctionSpace(mesh, 'P', 2)  # because P1 would be boring
# get coordinates of DOFs
dof_coords = V.tabulate_dof_coordinates()

# random location
x0 = np.array([0.51241, 0.56245])

# find nearest DOF:
dof = np.argmin(np.linalg.norm(dof_coords - x0, axis=1))

print('dof {}, x = {}'.format(dof, dof_coords[dof]))

# now define a DirichletBC at that point
bc = DirichletBC(V, Constant(123),
                 'near(x[0], {x}) && near(x[1], {y})'.format(x=dof_coords[dof][0], y=dof_coords[dof][1]),
                 'pointwise')

PS: Regarding your linked question, the docstring of the function near is: near(x0: float, x1: float, eps: float = 3e-16). It doesn’t give you the nearest dof but all dofs within abs(x1-x2) < eps.

2 Likes