| 1 |
.. Documentation for the bcs demo from DOLFIN. |
| 2 |
|
| 3 |
.. _demo_pde_bcs_documentation: |
| 4 |
|
| 5 |
|
| 6 |
Set boundary conditions for meshes that include boundary indicators |
| 7 |
=================================================================== |
| 8 |
|
| 9 |
This demo is implemented in a single python file, :download:`demo_bcs.py`, |
| 10 |
which contains both the variational form and the solver. |
| 11 |
|
| 12 |
|
| 13 |
.. include:: ../common.txt |
| 14 |
|
| 15 |
Implementation |
| 16 |
-------------- |
| 17 |
|
| 18 |
This description goes through the implementation (in |
| 19 |
:download:`demo_bcs.py`) of a solver for the above described Poisson |
| 20 |
equation and how to set boundary conditions for a mesh that includes |
| 21 |
boundary indicators. |
| 22 |
|
| 23 |
First, the :py:mod:`dolfin` module is imported: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
from dolfin import * |
| 28 |
|
| 29 |
Then, we import the mesh and create a finite element function space |
| 30 |
:math:`V` relative to this mesh. In this case we create a |
| 31 |
:py:class:`FunctionSpace<dolfin.functions.functionspace.FunctionSpace>` |
| 32 |
``V`` consisting of continuous piecewise linear polynomials. |
| 33 |
|
| 34 |
.. code-block:: python |
| 35 |
|
| 36 |
# Create mesh and define function space |
| 37 |
mesh = Mesh("../aneurysm.xml.gz") |
| 38 |
V = FunctionSpace(mesh, "CG", 1) |
| 39 |
|
| 40 |
Now, we define the trial function u and the test function v, both living in the function space ``V``. We also define our variational problem, a and L. u and v are defined using the classes |
| 41 |
:py:class:`TrialFunction <dolfin.functions.function.TrialFunction>` and |
| 42 |
:py:class:`TestFunction<dolfin.functions.function.TrialFunction>`, |
| 43 |
respetively, on the :py:class:`FunctionSpace<dolfin.functions.functionspace.FunctionSpace>` ``V``. |
| 44 |
The source :math:`f` may be defined as a :py:class:`Constant <dolfin.functions.constant.Constant>`. |
| 45 |
The bilinear and linear forms, ``a`` and ``L`` respectively, are defined using UFL operators. |
| 46 |
Thus, the definition of the variational problem reads: |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
# Define variational problem |
| 51 |
u = TrialFunction(V) |
| 52 |
v = TestFunction(V) |
| 53 |
f = Constant(0.0) |
| 54 |
a = dot(grad(u), grad(v))*dx |
| 55 |
L = f*v*dx |
| 56 |
|
| 57 |
Before we can solve the problem we must specify the boundary conditions. |
| 58 |
We begin with specifying the values of the boundary conditions as :py:class:`Constant <dolfin.functions.constant.Constant>`s. |
| 59 |
Then we use the class :py:class:`DirichletBC <dolfin.fem.bcs.DirichletBC>` to define the Dirichlet boundary conditions: |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
# Define boundary condition values |
| 64 |
u0 = Constant(0.0) |
| 65 |
u1 = Constant(1.0) |
| 66 |
u2 = Constant(2.0) |
| 67 |
u3 = Constant(3.0) |
| 68 |
|
| 69 |
# Define boundary conditions |
| 70 |
bc0 = DirichletBC(V, u0, 0) |
| 71 |
bc1 = DirichletBC(V, u1, 1) |
| 72 |
bc2 = DirichletBC(V, u2, 2) |
| 73 |
bc3 = DirichletBC(V, u3, 3) |
| 74 |
|
| 75 |
:py:class:`DirichletBC <dolfin.fem.bcs.DirichletBC>` takes three arguments, the first one is our function space ``V``, |
| 76 |
the next is the boundary condition value and the third is the subdomain indicator which is information stored in the mesh. |
| 77 |
|
| 78 |
At this point we are ready to create a :py:class:`Function <dolfin.cpp.function.Function>` ``u`` to store the solution and call the solve function with the arguments |
| 79 |
``a == L``, ``u``, ``[bc0, bc1, bc2, bc3]``, as follows: |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
# Compute solution |
| 84 |
u = Function(V) |
| 85 |
solve(a == L, u, [bc0, bc1, bc2, bc3]) |
| 86 |
|
| 87 |
When we have solved the problem, we save the solution to file and plot it. |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
# Write solution to file |
| 92 |
File("u.pvd") << u |
| 93 |
|
| 94 |
# Plot solution |
| 95 |
plot(u, interactive=True) |
| 96 |
|
| 97 |
|
| 98 |
Complete code |
| 99 |
------------- |
| 100 |
|
| 101 |
.. literalinclude:: demo_bcs.py |
| 102 |
:start-after: # Begin demo |
| 103 |
|