# FEniCs: help in implementing the boundary condition for 1D problem [closed]

I have just started learning FEniCS and have used: http://www.scientificpython.net/pyblog/fenics-linear-two-point-bvp to write a script for solving:

           u'' + u = 1

u(0) = 1, u'(1) = 0


with exact solution,

           u(x) = exp(-x)/ exp(-1) + x + ( -1+exp(-1) )/ exp(-1)


Clearly the weak formulation of the above problem is:

           -(u', v') + (u,v) = (g,v) ; with g = 1


Here is the edited code:

    from dolfin import *

# definig mesh
mesh = IntervalMesh(20, 0, 1)

# definig Function space on this mesh using Lagrange polynoimals of degree 2.
V = FunctionSpace(mesh, "CG", 2)

# definign boundary values
#u0 = Constant(0)
u0 = Expression("x[0]")

# this functions checks whether the input x is on the boundary or not.
def DirichletBoundary(x, on_boundary):
tol = 1e-14
return on_boundary and abs(x < tol)

# Enforcing u = u0 at x = 0
bc = DirichletBC(V, u0, DirichletBoundary)

# Setting up the variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(1)
g = Constant(1)
L = f*v*dx

# solving the variational problem.
u = Function(V)
solve( a == L, u, bc)

# plotting solution
plot(u, interactive = True)


Clearly the solution plot does not incorporate the boundary condition u(0) = 1.

• Why would you expect your solution to satisfy $u(0)=1$, if you are imposing the boundary condition $u(0)=0$ (assuming that is what x[0] evaluates to; I didn't check)? If you set u0=Constant(1) or similar, you get the correct behavior. – Christian Clason Sep 23 '14 at 12:46
• Also, abs(x<tol) makes no sense - did you mean abs(x)<tol? – Christian Clason Sep 23 '14 at 12:48