# Specifying boundary conditions¶

## Inhomogeneous natural boundary conditions¶

The first problem we considered was the PDE

Δu+u=f
together with the homogeneous Neumann boundary condition u/n=0$\partial u/\partial n=0$ on Ω$\partial\Omega$. This is the easiest boundary condition to implement with finite elements: you have to do precisely nothing! (By contrast, Neumann boundary conditions are a bit of a chore for finite differences.) FEniCS can handle many other types of boundary conditions as well, just about all the boundary conditions that make sense for such an equation.

Only slightly more complicated than homogeneous Neumann boundary conditions are the inhomogeneous Neumann boundary conditions u/n=g$\partial u/\partial n=g$ on Ω$\partial\Omega$. This adds an additional term to the right hand side of the weak formulation, so uV$u\in V$ satisfies

b(u,v)=F(v),vV,
where again V=H1(Ω)$V=H^1(\Omega)$ and
but now
F(v)=Ωfvdx+Ωgvds.
(Make sure that you understand this!) Of course the finite element solution uhVh$u_h\in V_h$ is still defined by b(uh,v)=F(v)$b(u_h,v)= F(v)$, vVh$v\in V_h$, with this new definition of F(v)$F(v)$.

To implement the new term in FEniCS is straightforward. First we have to define an Expression for g$g$, just as we did for f$f$. For example, if g(x,y)=x2y$g(x, y) = x^2 y$, we would use

In []:
g = Expression('pow(x[0], 2)*x[1]')


Next we add the extra term to the definition of F$F$, which becomes

In []:
F = f*v*dx + g*v*ds


The remainder of the program need not be changed.

Remark: Note that, in addition to the "measure" dx which is used to signal integration over the domain, here we use the measure ds which means integration over the boundary of the domain. It is also possible to single out a part of the domain or a part of the boundary and integrate over that with a measure like dx(1) or ds(3). We do not need this yet, but will for more complicated boundary conditions (see below).

## Essential boundary conditions¶

Instead of the Neumann boundary condition, we can impose the Dirichlet boundary condition u=g$u=g$. This is an essential boundary condition, meaning that it is imposed as a constraint on the space V$V$ rather than by changing the bilinear form b$b$ or the linear functional F$F$. The weak formulation is now to find uV=H1(Ω)$u\in V=H^1(\Omega)$ such that u=g$u=g$ on Ω$\partial\Omega$ and satisfying

b(u,v)=Ωfvdxfor all vV such that v=0 on Ω.
On the discrete level, uh$u_h$ is restricted to elements of Vh$V_h$ which interpolate g$g$ on the boundary, i.e., any DOFs associated to vertices or edges on the boundary must give the same value when applied to uh$u_h$ as when applied to g$g$. We say that the DOFs of uh$u_h$ on the boundary are constrained to g$g$. In the case of Lagrange elements of degree 2, for instance, this means that uh$u_h$ must equal g$g$ at the vertices on the boundary and the midpoints of edges on the boundary. Similarly the DOFs of the test function v$v$ on the boundary are constrained to be zero.

FEniCS imposes these constraints this after assembling the stiffness matrix and load vector, by modifying entries associated to boundary DOFs. For this, we need to specify to FEniCS which DOFs are to be constrained (in this case all the DOFs on the boundary), and what they are to be constrained to (namely g$g$). This is accomplished by invoking the procedure

DirichletBC(Vh, g, bdry)

where the three arguments are the function space to constrain, the function to constrain to, and the portion of the domain where DOFs are to be constrained. The function g$g$ can be defined as an Expression just as for the Neumann boundary condition. When the Dirichlet boundary condition is to be imposed on the whole boundary of Ω$\Omega$, we can simply take bdry = DomainBoundary(). The relevant code is then:

In []:
g = Expression('...')
bc = DirichletBC(Vh, g, DomainBoundary())
uh = Function(Vh)
solve(b == F, uh, bc)


Note that the solve call now takes a third argument, specifying the essential boundary condition.

## Mixed boundary conditions¶

Sometimes we have different boundary conditions on different, complementary, parts of the boundary. For example, let ΓD$\Gamma_D$ be the bottom side of the unit square, and ΓN$\Gamma_N$ the union of the remaining three sides, and consider the solution of the boundary value problem

Δu=0in Ω,u=g on ΓD,un=0 on ΓN.
The weak formulation then seeks uV=H1(Ω)$u\in V=H^1(\Omega)$ constrained to equal g$g$ on ΓD$\Gamma_D$ such that
for all vV$v\in V$ constrained to zero on ΓD$\Gamma_D$. In this case we cannot use DomainBoundary(), but must define the subset ΓD$\Gamma_D$. This is done by defining a python function and passing it as the third argument of DirichletBC. We might choose to call the function GammaD. It takes two arguments, point x, and a Boolean on_boundary. When FEniCS needs to identify whether some point of the domain belongs to ΓD$\Gamma_D$ it will call GammaD(x, on_boundary) with x set to the point (the array of its coordinates x[0] and x[1]), and on_boundary set to True or False according to whether the point x$x$ belongs to Ω$\partial\Omega$ or not. Our function must return the value True or False according to whether the point x$x$ belongs to ΓD$\Gamma_D$ or not. Since ΓD$\Gamma_D$ is the bottom edge of the square, the test is simply whether the second coordinate is equal to zero. However one should never test floating point numbers for exact equality, since they may suffer from small round off errors. FEniCS provides near(x[1], 0.) for this. It returns True if x[1] is within round off error of zero. Thus we can specify ΓD$\Gamma_D$ as follows:

In []:
def GammaD(x, on_boundary):
return near(x[1], 0)


From this point on the code is similar to that discussed above.

In []:
g = Expression('...')
uh = Function(Vh)
solve(b == F, uh, bc)


Note that for this problem, with f=0$f=0$ and no inhomogeneous Neumann data, the linear functional F=0$F=0$. This can be entered as F = Constant(0.)*v*dx.

Remark. People are often confused about what to do at the interface where the boundary conditions switch from Neumann to Dirichlet. In our example, this would consist of the two points (0,0)$(0,0)$ and (1,0)$(1,0)$, the end points of the bottom side ΓD$\Gamma_D$. Should the DOFs associated with these points be constrained, as for a Dirichlet BC or left unconstrained as for a Neumann BC? The answer is that they should be constrained. The constraint u=g$u=g$ is a continuous condition: if it holds on a subset of Ω$\partial\Omega$, it should hold on the closure of that set.

## Multiple Dirichlet boundary conditions¶

It is possible to have multiple Dirichlet boundary conditions, say u=g1$u=g_1$ on the bottom side and u=g2$u=g_2$ on the top side. We would then define two different DirichletBC, each just as above, say bc1 and bc2, and then call solve as

solve(b == F, uh, [bc1, bc2])

## Neumann BC on part of the boundary¶

When inhomogeneous Neumann conditions are imposed on part of the boundary, we may need to include an integral like ΓNgvds$\int_{\Gamma_N} gv\,ds$ in the linear functional F$F$. If we can define the expression g$g$ on the whole boundary, but so that it is zero except on ΓN$\Gamma_N$ (extension by zero), we can simply write this integral as Ωgvds$\int_{\partial\Omega} gv\,ds$ and nothing new is needed. Otherwise, we need to define a "measure" ds(1) which integrates only over ΓN$\Gamma_N$. The same approach can be used for multiple Neumann boundary conditions. The definition of such special measures is confusing because it involves several complex datastructures. One must first define a MeshFunction which assigns an integer to each edge, then define the relevant portion of the boundary using a subclass of the FEniCS class SubDomain, then use this subclass to set the values of the mesh function, and then use the mesh function to make the new measure. Here is some typical code for this, which redefines ds so that ds(1) integrates over the left half of the boundary (where the first coordinate is <1/2$<1/2$), ds(0) integrates over the right half of the boundary, and ds() integrates over the whole boundary.

In []:
# create a mesh function which assigns an unsigned integer (size_t) to each edge
mf = MeshFunction("size_t", mesh, 1) # 3rd argument is dimension of an edge
mf.set_all(0) # initialize the function to zero
# create a SubDomain subclass, specifying the portion of the boundary with x[0] < 1/2
class Lefthalf(SubDomain):
def inside(self, x, on_boundary):
return x[0] < 0.5 and on_boundary
lefthalf = Lefthalf() # instantiate it
# use this lefthalf object to set values of the mesh function to 1 in the subdomain
lefthalf.mark(mf, 1)
# define a new measure ds based on this mesh function
ds = Measure("ds")[mf]


## Robin boundary conditions¶

Robin boundary conditions take the form u/n+αu=g$\partial u/\partial n + \alpha u = g$, i.e., a linear combination of Dirichlet and Neumann boundary conditions. These can be treated much as for Neumann boundary conditions, in that they are natural, not essential. In the case of Robin boundary conditions, not only is the functional F$F$ modified, but also the bilinear form b$b$.

## Periodic boundary conditions¶

In the case of periodic boundary conditions, the boundary condition does not constrain individual points on the boundary, but relates the values at two different points. We will not discuss this further except to say that this possibility is available in FEniCS.

In [5]:
# this cell is present to set the notebook style
from IPython.core.display import HTML
def css_styling():