# How to setting Dirichlet boundary condition by node index?

Dear all,
I can create unstructured mesh by myself, and mark node index on boundary.
I use the Poisson example code, to get result file, “poisson000000.vtu”.
The figure is show by paraview. You can get node inde (id = 1055) in the figure.

In the example code, we can get boundary by node location.

``````def boundary(x):
return x < DOLFIN_EPS or x > 1.0 - DOLFIN_EPS
``````

So I want to set boundary by node index. Does anyone know that?Help me please. Thank you very mush.

The Poisson example code:

``````from dolfin import *

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)

def boundary(x):
return x < DOLFIN_EPS or x > 1.0 - DOLFIN_EPS

u0 = Constant(0.0)
bc = DirichletBC(V, u0, boundary)

u = TrialFunction(V)
v = TestFunction(V)
f = Expression("10*exp(-(pow(x - 0.5, 2) + pow(x - 0.5, 2)) / 0.02)", degree=2)
g = Expression("sin(5*x)", degree=2)
L = f*v*dx + g*v*ds

u = Function(V)
solve(a == L, u, bc)

plot(u)
# Save solution in VTK format
file = File("poisson.pvd")
file << u
``````

You can have a look at: Boundary conditions on edges or nodes - #6 by dokken

I am a novice Fenics user. Thanks for getting back to me.

Do you mean that?

``````class Gammathree(SubDomain):
def inside(self, x, on_boundary):
return near(x, 0, DOLFIN_EPS) and near(x, 0, DOLFIN_EPS) and near(x, 0, DOLFIN_EPS)
class Gammatwo(SubDomain):
def inside(self, x, on_boundary):
return near(x, 0, DOLFIN_EPS) and near(x, 0, DOLFIN_EPS) and near(x, 1, DOLFIN_EPS)
class Gammaone(SubDomain):
def inside(self, x, on_boundary):
return near(x, 0, DOLFIN_EPS) and near(x, 1, DOLFIN_EPS) and near(x, 0, DOLFIN_EPS)
point3 = Gammathree()
point2 = Gammatwo()
point1 = Gammaone()

# Define Dirichlet boundary (x = 0 or x = 1)
c = Constant((0.0, 0.0, 0.0))
r = Constant((1.0, 0.00, 0.0))
bc1= DirichletBC(V, c, point3)
bc21 = DirichletBC(V.sub(0), Constant(0.0), point2)
bc22 = DirichletBC(V.sub(1), Constant(0.0), point2)
bc3 = DirichletBC(V.sub(0), Constant(0.0), point1)
``````

I have node index on boundry, so I want to use node index to set.
Can you give me more opinions or suggestion?

You cannot use the node index, as the index will not necessary be the same in dolfin (as dolfin might renumber for locallity, ordering and parallel distribution).
You could mark the node (for instance by a geometric search as shown above) and then use the pointwise method inside the DirichletBC.

Dear @dokken ,
I totally understand your commentary.
As the red dot on the picture, I want to set the DirichletBC on such unstructured mesh, and the mesh density is not consistent. Is there a good way to provide me?

Again, as you know the coordinate, create a condition similar to:

``````class Gammatwo(SubDomain):
def inside(self, x, on_boundary):
return near(x, 375, DOLFIN_EPS) and near(x, -52, DOLFIN_EPS) and near(x, 409.051, DOLFIN_EPS)
point2 = Gammatwo()
bc = DirichletBC(V.sub(0), Constant(0.0), point2, method="pointwise")
``````