Pointsource on specific points python

Take a look at the following minimal example:

from dolfin import *
N = 5
mesh = UnitSquareMesh(2*N,2*N)
V = FunctionSpace(mesh,"CG",1)

# Setting a value using a PointSource, as suggested in the post title:
p = PointSource(V,Point(1-DOLFIN_EPS,0.5),1.0)
p.apply(u.vector())

# Can also set using a BC, which has the advantage of being convenient
# for applying a constraint during a solve.  The trick is that you need to
# pass the optional argument "pointwise".
bc = DirichletBC(V,Constant(1.0),"near(x[0],0)&&near(x[1],0.5)","pointwise")
u = Function(V)
bc.apply(u.vector())

# Note: In both cases, the point where the value is set needs to correspond to
# a DoF of the FunctionSpace.  

# Check: Function is set to 1 at both points:
from matplotlib import pyplot as plt
plot(u)
plt.show()
4 Likes