A FEniCS Tutorial - FEniCS Project

A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project

fenicsproject.org
from fenicsproject.org More from this publisher
19.06.2015 Views

4. Add statements in the program for solving the variational problem, computing derived quantities such as ∇u, and visualizing the results. We shall now go through steps 2–4 in detail. The key feature of FEniCS is that steps 3 and 4 result in fairly short code, while most other software frameworks for PDEs require much more code and more technically difficult programming. 1.2 Variational Formulation FEniCS makes it easy to solve PDEs if finite elements are used for discretization in space and the problem is expressed as a variational problem. Readers who are not familiar with variational problems will get a brief introduction to the topic in this tutorial, but getting and reading a proper book on the finite element method in addition is encouraged. Section 7.8 contains a list of some suitable books. The core of the recipe for turning a PDE into a variational problem is to multiply the PDE by a function v, integrate the resulting equation over Ω, and perform integration by parts of terms with second-order derivatives. The function v which multiplies the PDE is in the mathematical finite element literature called a test function. The unknown function u to be approximated is referred to as a trial function. The terms test and trial function are used in FEniCS programs too. Suitable function spaces must be specified for the test and trial functions. For standard PDEs arising in physics and mechanics such spaces are well known. In the present case, we first multiply the Poisson equation by the test function v and integrate, ∫ ∫ − (∇ 2 u)vdx = fvdx. (4) Ω Ω Then we apply integration by parts to the integrand with second-order derivatives, ∫ ∫ ∫ − (∇ 2 ∂u u)vdx = ∇u·∇vdx− vds, (5) Ω Ω ∂Ω ∂n where ∂u ∂n is the derivative of u in the outward normal direction at the boundary. The test function v is required to vanish on the parts of the boundary where u is known, which in the present problem implies that v = 0 on the whole boundary ∂Ω. The second term on the right-hand side of (5) therefore vanishes. From (4) and (5) it follows that ∫ ∫ ∇u·∇vdx = fvdx. (6) Ω This equation is supposed to hold for all v in some function space ˆV. The trial function u lies in some (possibly different) function space V. We refer to (6) as the weak form of the original boundary-value problem (1)–(2). Ω 6

The proper statement of our variational problem now goes as follows: Find u ∈ V such that ∫ ∫ ∇u·∇vdx = fvdx ∀v ∈ ˆV. (7) Ω The test and trial spaces ˆV and V are in the present problem defined as ˆV = {v ∈ H 1 (Ω) : v = 0 on ∂Ω}, V = {v ∈ H 1 (Ω) : v = u 0 on ∂Ω}. In short, H 1 (Ω) is the mathematically well-known Sobolev space containing functions v such that v 2 and ||∇v|| 2 have finite integrals over Ω. The solution of the underlying PDE must lie in a function space where also the derivatives are continuous, but the Sobolev space H 1 (Ω) allows functions with discontinuous derivatives. Thisweakercontinuityrequirementofuinthevariationalstatement (7), caused by the integration by parts, has great practical consequences when it comes to constructing finite elements. To solve the Poisson equation numerically, we need to transform the continuous variational problem (7) to a discrete variational problem. This is done by introducing finite-dimensional test and trial spaces, often denoted as ˆV h ⊂ ˆV and V h ⊂ V. The discrete variational problem reads: Find u h ∈ V h ⊂ V such that ∫ ∫ ∇u h ·∇vdx = fvdx ∀v ∈ ˆV h ⊂ ˆV . (8) Ω Ω The choice of ˆV h and V h follows directly from the kind of finite elements we want to apply in our problem. For example, choosing the well-known linear triangular element with three nodes implies that ˆV h and V h are the spaces of all piecewise linear functions over a mesh of triangles, where the functions in ˆV h are zero on the boundary and those in V h equal u 0 on the boundary. Themathematicsliteratureonvariationalproblemswritesu h forthesolution of the discrete problem and u for the solution of the continuous problem. To obtain(almost)aone-to-onerelationshipbetweenthemathematicalformulation of a problem and the corresponding FEniCS program, we shall use u for the solution of the discrete problem and u e for the exact solution of the continuous problem, if we need to explicitly distinguish between the two. In most cases, we will introduce the PDE problem with u as unknown, derive a variational equation a(u,v) = L(v) with u ∈ V and v ∈ ˆV, and then simply discretize the problem by saying that we choose finite-dimensional spaces for V and ˆV. This restriction of V implies that u becomes a discrete finite element function. In practice, thismeansthatweturnourPDEproblemintoacontinuousvariational problem, create a mesh and specify an element type, and then let V correspond to this mesh and element choice. Depending upon whether V is infinite- or finite-dimensional, u will be the exact or approximate solution. It turns out to be convenient to introduce the following unified notation for linear weak forms: a(u,v) = L(v). (9) Ω 7

4. Add statements in the program for solving the variational problem, computing<br />

derived quantities such as ∇u, and visualizing the results.<br />

We shall now go through steps 2–4 in detail. The key feature of <strong>FEniCS</strong> is that<br />

steps 3 and 4 result in fairly short code, while most other software frameworks<br />

for PDEs require much more code and more technically difficult programming.<br />

1.2 Variational Formulation<br />

<strong>FEniCS</strong> makes it easy to solve PDEs if finite elements are used for discretization<br />

in space and the problem is expressed as a variational problem. Readers who are<br />

not familiar with variational problems will get a brief introduction to the topic<br />

in this tutorial, but getting and reading a proper book on the finite element<br />

method in addition is encouraged. Section 7.8 contains a list of some suitable<br />

books.<br />

The core of the recipe for turning a PDE into a variational problem is to<br />

multiply the PDE by a function v, integrate the resulting equation over Ω, and<br />

perform integration by parts of terms with second-order derivatives. The function<br />

v which multiplies the PDE is in the mathematical finite element literature<br />

called a test function. The unknown function u to be approximated is referred<br />

to as a trial function. The terms test and trial function are used in <strong>FEniCS</strong><br />

programs too. Suitable function spaces must be specified for the test and trial<br />

functions. For standard PDEs arising in physics and mechanics such spaces are<br />

well known.<br />

In the present case, we first multiply the Poisson equation by the test function<br />

v and integrate, ∫ ∫<br />

− (∇ 2 u)vdx = fvdx. (4)<br />

Ω Ω<br />

Then we apply integration by parts to the integrand with second-order derivatives,<br />

∫ ∫ ∫<br />

− (∇ 2 ∂u<br />

u)vdx = ∇u·∇vdx− vds, (5)<br />

Ω Ω ∂Ω ∂n<br />

where ∂u<br />

∂n<br />

is the derivative of u in the outward normal direction at the boundary.<br />

The test function v is required to vanish on the parts of the boundary where u is<br />

known, which in the present problem implies that v = 0 on the whole boundary<br />

∂Ω. The second term on the right-hand side of (5) therefore vanishes. From (4)<br />

and (5) it follows that<br />

∫ ∫<br />

∇u·∇vdx = fvdx. (6)<br />

Ω<br />

This equation is supposed to hold for all v in some function space ˆV. The trial<br />

function u lies in some (possibly different) function space V. We refer to (6) as<br />

the weak form of the original boundary-value problem (1)–(2).<br />

Ω<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!