This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to create a DirichletBC on a single vertex or DOF?

+1 vote

Dear all,

I want to create a DirichletBC at a single vertex, but haven't succeeded yet. I'm not sure if it's caused by the defined SubDomain class or the DirichletBC. I tried both method = 'pointwise' and method = 'topological', but neither one works. Could anyone help?

Attached is the minimal code:

from dolfin import *

# create a 3*3 mesh
nx = 3
mesh = UnitSquareMesh(nx, nx)
V = FunctionSpace(mesh, 'CG', 1)
v = TestFunction(V)

class OriginPoint(SubDomain):
    def inside(self, x, on_boundary):
        '''
        I expect to get a single vertex at the origin, but it seems not working.
        '''
        tol = 1.0/nx
        return on_boundary and x[0] < tol and x[1] < tol
originpoint = OriginPoint()

# create a rhs linear vector
b = assemble(v*Constant(0.0)*dx)

# create a DirichletBC
bc = DirichletBC(V, Constant(1.0), originpoint)#, method = 'pointwise'

print 'Before applying BC...'
print b.array()

bc.apply(b)
print 'After applying BC...'
print b.array()
print "changed entries:"
print bc.get_boundary_values()
related to an answer for: Marking a point on the boundary
asked Jan 17, 2016 by Chao Zhang FEniCS User (1,180 points)
edited Jan 17, 2016 by Chao Zhang

1 Answer

+2 votes
 
Best answer

Hi, if DirichletBC is applied pointwise, then the on_boundary flag passed to your inside method is always False, see here. In turn the function never returns True. The fix is therefore

from dolfin import *

# create a 3*3 mesh
nx = 3
mesh = UnitSquareMesh(nx, nx)
V = FunctionSpace(mesh, 'CG', 1)
v = TestFunction(V)

class OriginPoint(SubDomain):
    def inside(self, x, on_boundary):
        '''
        I expect to get a single vertex at the origin, but it seems not working.
        '''
        tol = 1.0/nx
        return x[0] < tol and x[1] < tol 
originpoint = OriginPoint()

# create a rhs linear vector
b = assemble(v*Constant(0.0)*dx)

# create a DirichletBC
bc = DirichletBC(V, Constant(1.0), originpoint, method='pointwise')

print 'Before applying BC...'
print b.array()

bc.apply(b)
print 'After applying BC...'
print b.array()
print "changed entries:"
print bc.get_boundary_values()
answered Jan 18, 2016 by MiroK FEniCS Expert (80,920 points)
selected Jan 18, 2016 by Chao Zhang

Thanks very much, Miroslav!

...