This page was generated from unit-1.3-dirichlet/dirichlet.ipynb.

# 1.3 Dirichlet boundary conditions¶

This tutorial goes in depth into the mechanisms required to solve the Dirichlet problem

with a **nonzero** Dirichlet boundary condition

The same mechanisms are used in solving boundary value problems involving operators other than the Laplacian.

You will see how to perform these tasks in NGSolve: - extend Dirichlet data from boundary parts, - convert boundary data into a volume source, - reduce inhomogeneous Dirichlet case to the homogeneous case, and - perform all these tasks automatically within a utility.

If you are just interested in the *automatic utility,* then please skip to the last part of the tutorial.

## Spaces with Dirichlet conditions on part of the boundary¶

```
[1]:
```

```
import netgen.gui
from ngsolve import *
from netgen.geom2d import unit_square
mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))
mesh.GetBoundaries()
```

```
[1]:
```

```
('bottom', 'right', 'top', 'left')
```

The `unit_square`

has its boundaries marked as `left`

, `right`

, `top`

and `bottom`

. Suppose we want non-homogeneous Dirichlet boundary conditions on

Then, we set the space as follows:

```
[2]:
```

```
fes = H1(mesh, order=2, dirichlet="left|right")
```

Compare this space with the one without the `dirichlet`

flag:

```
[3]:
```

```
fs2 = H1(mesh, order=2)
fes.ndof, fs2.ndof # total number of unknowns
```

```
[3]:
```

```
(129, 129)
```

Thus, the `dirichlet`

flag did not change `ndof`

. In NGSolve the unknowns are split into two groups: * dirichlet dofs (or constrained dofs), * free dofs.

The facility `FreeDofs`

gives a `BitArray`

such that `FreeDofs[dof]`

is True if and only if `dof`

is a free degree of freedom.

```
[4]:
```

```
print("free dofs of fs2 without \"dirichlet\" flag:\n",
fs2.FreeDofs())
print("free dofs of fes:\n", fes.FreeDofs())
```

```
free dofs of fs2 without "dirichlet" flag:
0: 11111111111111111111111111111111111111111111111111
50: 11111111111111111111111111111111111111111111111111
100: 11111111111111111111111111111
free dofs of fes:
0: 00001111000011110000111111111111111111101010110111
50: 11111111101101101111111111111110101110111111111111
100: 11111111111111111111111111111
```

The space

`fs2`

without`dirichlet`

flag has only free dofs (no dirichlet dofs).The other space

`fes`

has a few dofs that are marked as*not*free. These are the dofs that are located on the boundary regions we marked as`dirichlet`

.

## Extension of boundary data¶

We use the standard technique of reducing a problem with essential non-homogeneous boundary conditions to one with homogeneous boundary condition using an extension. The solution \(u\) in \(H^1\) satisfies

and

for all \(v_0\) in \(\in H_{0,D}^1 = \{ v \in H^1: v|_{\Gamma_D} = 0\}\). Split the solution

where \(u_D\) is an extension of \(g\) into \(\Omega\). Then we only need to find \(u_0\) in \(H^1_{0,D}\) satisfying the homogeneous Dirichlet problem

for all \(v_0\) in \(H_{0,D}^1\). These are the **issues to consider** in this approach:

How to define an extension \(u_D\) in the finite element space?

How to form and solve the system for \(u_0\)?

Let us address the first in the following example.

Suppose we are given that

```
[5]:
```

```
g = sin(y)
```

We interpolate \(g\) on the boundary of the domain and extend it to zero on the elements not having an intersection with \(\Gamma_D\).

```
[6]:
```

```
gfu = GridFunction(fes)
gfu.Set(g, BND)
Draw(gfu)
```

The keyword `BND`

tells `Set`

that `g`

need only be interpolated on those parts of the boundary that are marked `dirichlet`

.

Thus, `gfu`

now contains the extension \(u_D\). Next, we turn to set up the system for \(u_0\).

## Forms and assembly¶

In NGSolve, bilinear and linear forms are defined independently of the dirichlet flags. Matrices and vectors are set up with respect to all unknowns (free or constrained) so they may be restricted to any group of unknowns later.

```
[7]:
```

```
u, v = fes.TnT()
a = BilinearForm(fes, symmetric=True)
a += grad(u)*grad(v)*dx
a.Assemble()
```

If \(A=\) `a.mat`

is the matrix just assembled, then we want to solve for

or

where we have block partitioned using free dofs (\(F\)) and dirichlet dofs (\(D\)) as if they were numbered consecutively (which may not be the case in practice) for ease of presentation. The first row gives

Since we have already constructed \(u_D\), we need to perform these next steps:

Set up the right hand side from \(f\) and \(u_D\).

Solve a linear system which involves only \(A_{FF}\).

Add solution: \(u = u_0 + u_D\).

## Solve for the free dofs¶

We need to assemble the right hand side of \(A_{FF} u_{0,F} = f_F - [A u_D]_F\), namely

```
[8]:
```

```
f = LinearForm(fes)
f += 1*v*dx
f.Assemble()
r = f.vec.CreateVector()
r.data = f.vec - a.mat * gfu.vec
```

The implementation of

by sparse solvers is achieved by the following:

```
[9]:
```

```
gfu.vec.data += a.mat.Inverse(freedofs=fes.FreeDofs()) * r
Redraw()
```

## The automatic utility `BVP`

¶

NGSolve also provides a `BVP`

facility in the `solvers`

submodule, within which the above steps are performed automatically. You provide \(A\), \(f\), a grid function `gfu`

with your boundary condition \(g\), and a preconditioner. Then `BVP`

solves the problem with non-homogeneous Dirichlet boundary condition and overwrites `gfu`

with the solution.

```
[10]:
```

```
gfu.Set(g, BND)
c = Preconditioner(a,"local") #<- Jacobi preconditioner
#c = Preconditioner(a,"direct") #<- sparse direct solver
c.Update()
solvers.BVP(bf=a, lf=f, gf=gfu, pre=c)
Redraw()
```

```
it = 0 err = 1.324197630852001
it = 1 err = 0.7482753330831278
it = 2 err = 0.6935365444529646
it = 3 err = 0.5530815561584945
it = 4 err = 0.45367830635440876
it = 5 err = 0.273155145697958
it = 6 err = 0.19515430657478777
it = 7 err = 0.11089771808731898
it = 8 err = 0.05456942143195432
it = 9 err = 0.03848730835321277
it = 10 err = 0.025822078243051882
it = 11 err = 0.018212750534154568
it = 12 err = 0.010781043321490684
it = 13 err = 0.006592401333924948
it = 14 err = 0.003873041113096247
it = 15 err = 0.00191029988923335
it = 16 err = 0.0008755946467775094
it = 17 err = 0.0004089159296759808
it = 18 err = 0.0002564404510203847
it = 19 err = 0.00012480800222510282
it = 20 err = 6.769009877447314e-05
it = 21 err = 3.30687120949363e-05
it = 22 err = 1.9759787455699028e-05
it = 23 err = 1.2224226967564614e-05
it = 24 err = 6.288984116832275e-06
it = 25 err = 3.3377578397719732e-06
it = 26 err = 1.570161093800561e-06
it = 27 err = 8.680106877075922e-07
it = 28 err = 4.2713392565253617e-07
it = 29 err = 1.9287597213516832e-07
it = 30 err = 9.768634694805674e-08
it = 31 err = 4.611695990767521e-08
it = 32 err = 2.491303943496577e-08
it = 33 err = 1.2560326821667888e-08
```