$$ \newcommand{\dt}{\Delta t} \newcommand{\tp}{\thinspace .} \newcommand{\uex}{{u_{\small\mbox{e}}}} \newcommand{\x}{\boldsymbol{x}} \newcommand{\dx}{\, \mathrm{d}x} \newcommand{\ds}{\, \mathrm{d}s} \newcommand{\Real}{\mathbb{R}} \newcommand{\uI}{u_{_0}} \newcommand{\ub}{u_{_\mathrm{D}}} \newcommand{\GD}{\Gamma_{_\mathrm{D}}} \newcommand{\GN}{\Gamma_{_\mathrm{N}}} \newcommand{\GR}{\Gamma_{_\mathrm{R}}} \newcommand{\inner}[2]{\langle #1, #2 \rangle} $$




Setting multiple Dirichlet conditions

In the previous section, we used a single function \( \ub(x,y) \) for setting Dirichlet conditions on two parts of the boundary. Often it is more practical to use multiple functions, one for each subdomain of the boundary. Let us return to the case from the section Combining Dirichlet and Neumann conditions and redefine the problem in terms of two Dirichlet conditions: $$ \begin{alignat*}{2} - \nabla^2 u &= f \quad&&\mbox{in } \Omega, \\ u &= u_{_\mathrm{L}} &&\mbox{on } \GD^{^{\mathrm{L}}}, \\ u &= u_{_\mathrm{R}} &&\mbox{on } \GD^{^{\mathrm{R}}}, \\ - {\partial u\over\partial n} &= g &&\mbox{on } \GN \tp \end{alignat*} $$ Here, \( \GD^{^{\mathrm{L}}} \) is the left boundary \( x=0 \), while \( \GD^{^{\mathrm{R}}} \) is the right boundary \( x=1 \). We note that \( u_{_\mathrm{L}}(x, y) = 1 + 2y^2 \), \( u_{_\mathrm{R}}(x, y) = 2 + 2y^2 \), and \( g(x, y)=4y \).

For the boundary condition on \( \GD^{^{\mathrm{L}}} \), we define the usual triple of an expression for the boundary value, a function defining the location of the boundary, and a DirichletBC object:

u_L = Expression('1 + 2*x[1]*x[1]', degree=2)

def boundary_L(x, on_boundary):
    tol = 1E-14
    return on_boundary and near(x[0], 0, tol)

bc_L = DirichletBC(V, u_L, boundary_L)

For the boundary condition on \( \GD^{^{\mathrm{R}}} \), we write a similar code snippet:

u_R = Expression('2 + 2*x[1]*x[1]', degree=2)

def boundary_R(x, on_boundary):
    tol = 1E-14
    return on_boundary and near(x[0], 1, tol)

bc_R = DirichletBC(V, u_R, boundary_R)

We collect the two boundary conditions in a list which we can pass to the solve function to compute the solution:

bcs = [bc_L, bc_R]
solve(a == L, u, bcs)

Note that for boundary values that do not depend on \( x \) or \( y \), we might replace the Expression objects by Constant objects.