A FEniCS Tutorial - FEniCS Project
A FEniCS Tutorial - FEniCS Project A FEniCS Tutorial - FEniCS Project
This document presents a FEniCS tutorial to get new users quickly up and running with solving differential equations. FEniCS can be programmed both in C++ and Python, but this tutorial focuses exclusively on Python programming, since this is the simplest approach to exploring FEniCS for beginners and since it actually gives high performance. After having digested the examples in this tutorial, the reader should be able to learn more from the FEniCS documentation, the numerous demos, and the FEniCS book Automated Solution of Differential Equations by the Finite element Method: The FEniCS book, edited by Logg, Mardal, and Wells, to be published by Springer early 2012. The tutorial is still in an initial state so the reader is encouraged to send email to the author on hpl@simula.no about typos, errors, and suggestions for improvements. 1 Fundamentals FEniCS is a user-friendly tool for solving partial differential equations (PDEs). The goal of this tutorial is to get you started with FEniCS through a series of simple examples that demonstrate • how to define the PDE problem in terms of a variational problem, • how to define simple domains, • how to deal with Dirichlet, Neumann, and Robin conditions, • how to deal with variable coefficients, • how to deal with domains built of several materials (subdomains), • how to compute derived quantities like the flux vector field or a functional of the solution, • how to quickly visualize the mesh, the solution, the flux, etc., • how to solve nonlinear PDEs in various ways, • how to deal with time-dependent PDEs, • how to set parameters governing solution methods for linear systems, • how to create domains of more complex shape. The mathematics of the illustrations is kept simple to better focus on FEniCS functionality and syntax. This means that we mostly use the Poisson equation and the time-dependent diffusion equation as model problems, often with input data adjusted such that we get a very simple solution that can be exactly reproduced by any standard finite element method over a uniform, structured mesh. This latter property greatly simplifies the verification of the implementations. Occasionally we insert a physically more relevant example to remind the reader 4
that changing the PDE and boundary conditions to something more real might often be a trivial task. FEniCSmayseemtorequireathoroughunderstandingoftheabstractmathematical version of the finite element method as well as familiarity with the Python programming language. Nevertheless, it turns out that many are able to pick up the fundamentals of finite elements and Python programming as they go along with this tutorial. Simply keep on reading and try out the examples. You will be amazed of how easy it is to solve PDEs with FEniCS! Reading this tutorial obviously requires access to a machine where the FEniCS software is installed. Section 7.6 explains briefly how to install the necessary tools. AlltheexamplesdiscussedinthefollowingareavailableasexecutablePython source code files in a directory tree. 1.1 The Poisson equation Our first example regards the Poisson problem, −∇ 2 u(x) = f(x), x in Ω, (1) u(x) = u 0 (x), x on ∂Ω. (2) Here, u(x) is the unknown function, f(x) is a prescribed function, ∇ 2 is the Laplace operator (also often written as ∆), Ω is the spatial domain, and ∂Ω is the boundary of Ω. A stationary PDE like this, together with a complete set of boundary conditions, constitute a boundary-value problem, which must be precisely stated before it makes sense to start solving it with FEniCS. In two space dimensions with coordinates x and y, we can write out the Poisson equation as − ∂2 u ∂x 2 − ∂2 u = f(x,y). (3) ∂y2 The unknown u is now a function of two variables, u(x,y), defined over a twodimensional domain Ω. The Poisson equation arises in numerous physical contexts, including heat conduction, electrostatics, diffusion of substances, twisting of elastic rods, inviscid fluid flow, and water waves. Moreover, the equation appears in numerical splitting strategies of more complicated systems of PDEs, in particular the Navier-Stokes equations. Solving a physical problem with FEniCS consists of the following steps: 1. Identify the PDE and its boundary conditions. 2. Reformulate the PDE problem as a variational problem. 3. Make a Python program where the formulas in the variational problem are coded, along with definitions of input data such as f, u 0 , and a mesh for the spatial domain Ω. 5
- Page 1 and 2: A FEniCS Tutorial Hans Petter Langt
- Page 3: 7 Miscellaneous Topics 82 7.1 Gloss
- Page 7 and 8: The proper statement of our variati
- Page 9 and 10: """ FEniCS tutorial demo program: P
- Page 11 and 12: polynomial approximations over each
- Page 13 and 14: Instead of nabla_grad we could also
- Page 15 and 16: solution during program development
- Page 17 and 18: The demo program d2_p2D.py in the s
- Page 19 and 20: All mesh objects are of type Mesh s
- Page 21 and 22: Here, T is the tension in the membr
- Page 23 and 24: """ FEniCS program for the deflecti
- Page 25 and 26: Figure 3: Plot of the deflection of
- Page 27 and 28: Figure 4: Example of visualizing th
- Page 29 and 30: Let us continue to use our favorite
- Page 31 and 32: 1.11 Computing Functionals After th
- Page 33 and 34: e_Ve.vector()[:] = u_e_Ve.vector().
- Page 35 and 36: It is possible to restrict the inte
- Page 37 and 38: Other functions for visualizing 2D
- Page 39 and 40: 1 0.8 Contour plot of u 4 3.5 3 2.5
- Page 41 and 42: accordingly: f = −6, { −4, y =
- Page 43 and 44: Here, Γ 0 is the boundary x = 0, w
- Page 45 and 46: The object A is of type Matrix, whi
- Page 47 and 48: with exact solution u(x) = x 2 . Ou
- Page 49 and 50: The variational formulation of our
- Page 51 and 52: iter += 1 solve(a == L, u, bcs) dif
- Page 53 and 54: tol = 1E-14 def left_boundary(x, on
This document presents a <strong>FEniCS</strong> tutorial to get new users quickly up and<br />
running with solving differential equations. <strong>FEniCS</strong> can be programmed both<br />
in C++ and Python, but this tutorial focuses exclusively on Python programming,<br />
since this is the simplest approach to exploring <strong>FEniCS</strong> for beginners and<br />
since it actually gives high performance. After having digested the examples in<br />
this tutorial, the reader should be able to learn more from the <strong>FEniCS</strong> documentation,<br />
the numerous demos, and the <strong>FEniCS</strong> book Automated Solution of<br />
Differential Equations by the Finite element Method: The <strong>FEniCS</strong> book, edited<br />
by Logg, Mardal, and Wells, to be published by Springer early 2012.<br />
The tutorial is still in an initial state so the reader is encouraged to send<br />
email to the author on hpl@simula.no about typos, errors, and suggestions for<br />
improvements.<br />
1 Fundamentals<br />
<strong>FEniCS</strong> is a user-friendly tool for solving partial differential equations (PDEs).<br />
The goal of this tutorial is to get you started with <strong>FEniCS</strong> through a series of<br />
simple examples that demonstrate<br />
• how to define the PDE problem in terms of a variational problem,<br />
• how to define simple domains,<br />
• how to deal with Dirichlet, Neumann, and Robin conditions,<br />
• how to deal with variable coefficients,<br />
• how to deal with domains built of several materials (subdomains),<br />
• how to compute derived quantities like the flux vector field or a functional<br />
of the solution,<br />
• how to quickly visualize the mesh, the solution, the flux, etc.,<br />
• how to solve nonlinear PDEs in various ways,<br />
• how to deal with time-dependent PDEs,<br />
• how to set parameters governing solution methods for linear systems,<br />
• how to create domains of more complex shape.<br />
The mathematics of the illustrations is kept simple to better focus on <strong>FEniCS</strong><br />
functionality and syntax. This means that we mostly use the Poisson equation<br />
and the time-dependent diffusion equation as model problems, often with input<br />
data adjusted such that we get a very simple solution that can be exactly reproduced<br />
by any standard finite element method over a uniform, structured mesh.<br />
This latter property greatly simplifies the verification of the implementations.<br />
Occasionally we insert a physically more relevant example to remind the reader<br />
4