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)
     a = -inner(grad(u), grad(v))*dx + inner(u,v)*dx
     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. enter image description here

Would any body please help?


closed as off-topic by Bill Barth, Brian Borchers, Wolfgang Bangerth, Christian Clason, Paul Sep 22 '14 at 15:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ This is a question you should ask on the FEniCS mailing lists. $\endgroup$ – Wolfgang Bangerth Sep 22 '14 at 3:10
  • $\begingroup$ Or rather on FEniCS Q&A. See fenicsproject.org/support for details. $\endgroup$ – Jan Blechta Sep 22 '14 at 4:52
  • $\begingroup$ Thankyou for suggestion.Initially I thought the same, but given the large number of people here, I choose to put it here. $\endgroup$ – Sohail Sep 22 '14 at 5:23
  • $\begingroup$ 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. $\endgroup$ – Christian Clason Sep 23 '14 at 12:46
  • $\begingroup$ Also, abs(x<tol) makes no sense - did you mean abs(x)<tol? $\endgroup$ – Christian Clason Sep 23 '14 at 12:48

Browse other questions tagged or ask your own question.