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

DirichletBC expressions evaluated away from the boundary

+3 votes

When defining an Expression for DirichletBC, I noticed that this expression is evaluated on points which do not sit on the boundary. Why is this? Are there ways to prevent it?

The following code highlights the behavior:

from dolfin import *

from matplotlib import pyplot as pp

class MyExpr(Expression):
    def eval(self, values, x):
        values[0] = 0.0
        pp.plot(x[0], x[1], 'xk')
        return

my_expr = MyExpr()

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, 'CG', 2)

u = TrialFunction(V)
v = TestFunction(V)

a = dot(grad(u), grad(v)) * dx
L = v * dx

bcs = DirichletBC(V, my_expr, 'on_boundary')

sol = Function(V)
solve(a == L, sol, bcs=bcs)

pp.show()

This marks all points at which DirichletBC is evaluated. Somewhat surprisingly, this does not only include points on the actual boudary, but also end- and midpoints of edges of cells adjecent to a boundary.

Result

asked Aug 29, 2013 by nschloe FEniCS User (7,120 points)
edited Aug 29, 2013 by nschloe

1 Answer

+1 vote
 
Best answer

DirichletBC is a misleading name. The class applies constraints to the system $Ax =b$. There are cases where once wants to prescribe $x_{i}$ inside the domain, which is why DirichletBC checks everywhere.

answered Aug 29, 2013 by Garth N. Wells FEniCS Expert (35,930 points)
selected Sep 11, 2013 by Jan Blechta

In the above example code, only those nodes are checked which are on or adjacent to the boundary. This must have to do with 'on_boundary'. Until now, I was under the impression that 'on_boundary' checks boundary nodes, not elements. Wrong?

How can I set boundary conditions only in the boundary nodes?

DirichetBC checks facets by default. `on_boudnary' is true if it is a boundary facet, i.e. is attached to only one cell.

If you want to check nodes, use the "geometric" option in DirichletBC.

if it is a boundary facet, i.e. is attached to only one cell

That seems to disagree with the above example. Besides the boundary edges, DirichletBC is called for the end points of all cells that have a boundary edge, including their interior edges.

Look into dolfin/fem/DirichletBC.cpp. Evaluation of Dirichlet value _g is done by restriction to cell. This does not mean that BC is applied on cell - this is only an evaluation mechanism.

In principle you could try to write another method which would use _g::eval(val, x, cell) instead of _g::restrict() to reduce number of evaluations of Dirichlet value. Maybe there is some good reason why this is not a feasible approach, Garth?

...