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

Boundary conditions are not preserved under projections onto FE-spaces

+3 votes

I have troubles in understanding how fenics applies Dirichlet boundary conditions.

In particular, I have a function u on a domain D and I know the Dirichlet values g of u at the boundary. Say I project u onto a FE-space V to get uh, then I get a difference between uh and uh after having imposed the Dirichlet data.

|uh - bc.apply(uh)| ~ 1e-7

I understand, that an L2-projection does not preserve function values at the nodes but

  • the error should get smaller with finer discretizations
  • I expect a zero error for linear u and piecewise linear finite elements

which is not the case.

What am I doing wrong? Maybe it's just a conceptual misunderstanding.

Below is my dolfin code to reproduce my statements

import numpy as np
from dolfin import *

mesh = UnitSquareMesh(64, 64)
V = VectorFunctionSpace(mesh, "CG", 1)
u = Expression(('x[1]','0'))

ufun = project(u, V)
ufunbc = project(u, V)

# definition of the boundary
def boundary(x, on_boundary): 
    return (x[1] > 1.0 - DOLFIN_EPS 
        or x[0] > 1.0 - DOLFIN_EPS 
        or x[1] < DOLFIN_EPS 
        or x[0] < DOLFIN_EPS)

# take the function values as dirichlet data
diridata = u 
bc = DirichletBC(V, diridata, boundary)

# apply boundary conditions
bc.apply(ufunbc.vector())

plot(ufun-ufunbc)

print np.linalg.norm(ufun.vector().array() - ufunbc.vector().array())

print np.allclose(ufun.vector().array(), ufunbc.vector().array())
asked Jun 12, 2013 by Jan FEniCS User (8,290 points)
edited Jun 12, 2013 by Jan

1 Answer

+4 votes
 
Best answer

Try using a tighter tolerance in the projection:

solve(inner(TestFunction(V), TrialFunction(V)) * dx == inner(TestFunction(V), u) * dx, ufun,
  solver_parameters = {"linear_solver":"cg", "preconditioner":"sor", "krylov_solver":{"absolute_tolerance":1.0e-16, "relative_tolerance":1.0e-16}})

or a direct solver:

ufun = project(u, V, solver_type = "lu")
answered Jun 12, 2013 by James R. Maddison FEniCS User (2,230 points)
selected Jun 12, 2013 by Jan

That solved it. Thanks a lot!

...