Setting Delta-function-like initial condition

I am having trouble setting a delta-function-like initial condition for the heat equation.
The algorism I would like to make is,

  1. Import mesh
  2. Search for the node point q which is closest to a point p_0
  3. Set initial condition u_0 = delta(q)

I tried to use PointSource() function as shown in the program below.
The problem is that when p_0 is not exactly on a node point of the imported mesh, delta function is interpolated and there emerge four different node points that are non-zero.
Is there any way to set non-zero value to only one node point of the mesh?

import numpy as np
from fenics import *

mesh = Mesh(filename)
V = FunctionSpace(mesh, 'P', 1)

u_n = Function(V)
u_n.vector()[:] = 0.0
p = PointSource(V, Point(x_0,y_0,z_0), 1.0)
p.apply(u_n.vector())

Thank you.

Consider:

from dolfin import *

p = Point(0.41, 0.42, 0)

mesh = UnitSquareMesh(10,10)
tree = mesh.bounding_box_tree()

# Find which cell point is located in
cell, distance = tree.compute_closest_entity(p)
vertices = mesh.cells()[cell]

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

# Find the closest vertex in the cell
vertex_coordinates = mesh.coordinates()[vertices]
dist = 1e3
closest = None
for i, v in enumerate(vertex_coordinates):
    p_ = Point(v)
    dist_ = p.distance(p_)
    print(v)
    if dist_ < dist:
        dist = dist_
        closest_local = i
closest_vertex = vertices[closest_local]

# Find dof corresponding to vertex
vtd_p = vertex_to_dof_map(V)
dof_number = vtd_p[closest_vertex]

print(closest_vertex)
# Set value to 1
v = Function(V)
v.vector()[dof_number] = 1
print("Function at ", V.tabulate_dof_coordinates()[dof_number], " is ", v.vector().get_local()[dof_number])
1 Like

Thank you so much, it worked!