How efficiently set the DirichletBC in Fenics?


Currently, I am solving a nonlinear problem with a large network mesh.
I need to set DirichletBC values to 5000+ terminal points of the network.

I am using a for loop to assign the values now, a brief example is shown below:

V = FunctionSpace(mesh, "CG", 1)

bcs = [ ]
for p in range(0,4000):

  BCvalue = value_table[p]  # value_table is a np.array of the terminal points value

  def u0_boundary(x, on_boundary):
       tol = 1e-14
       return on_boundary and abs(term_nodes_coord[p][0]-x[0] ) < tol  # term_nodes_coord is the coordinates of terminal points in [ x, y, z]
  bc_set = DirichletBC(V, BCvalue, u0_boundary)

I believe the for-loop process really slows down the solver’s speed. Is there any way I can set all BCs faster? I know the indices and coordinates of all the terminal points. Can I mark all terminal points and only call DirichletBC() once?

Many thanks for the help!