Introduction to Python for FEniCS

Introduction
 redirect the "WIP" (work in progress) wiki page Egm6936.s11/FENICS WIP to a more permanent wiki page. Egm6321.f12 (talk) 14:15, 3 August 2012 (UTC)

 find the expansion for FEniCS (FE = Finite Element, what does "niCS" stand for ?) Egm6321.f12 (talk) 14:15, 3 August 2012 (UTC)

Origin of the name FEniCS
FEniCS is an acronym with FE representing finite element, CS representing computational software, and according to Anders Logg, a Senior Research Scientist with the FEniCS Project, "ni sits nicely in the middle". Andy Terrel also notes that the FEniCS software package was originally compiled at the University of Chicago, whose mascot is a phoenix, which likely inspired the name. A discussion of the topic can be found here on the FEniCS launchpad.

Article contents
This article acts as an introduction to the Python programming language and specifically its use with FEniCS.

The Python code below, taken from an example problem found here:Egm6936.s11/FENICS/Python, is explained on a line-by-line basis.

The structure of the example code can be applied to many problems but will require varying degrees of modification.

This article is designed to be self-reliant but an official Python tutorial can be found here: http://docs.python.org/tutorial/

The FEniCS Fundamentals page covers similar material, in addition to much more, found here: http://fenicsproject.org/documentation/tutorial/fundamentals.html

The official DOLFIN library can be found here: DOLFIN Library

Creating a Python code file
A Python code file can quickly be created from the Linux terminal. Entering the command "gedit" will open a blank document. Simply write the code in this document and save it into the desired directory with a .py extension. The file can be reopened for editing by entering "gedit filename.py". The code can be run by entering "Python filename.py".

Code can also be written using iPython, an interactive Python with many benefits and additional capabilities. Begin iPython by entering "ipython" in the terminal. You can quit iPython at any time by entering "quit". The code can be saved to your current directory by entering "save filename.py line#" where "line#" represents the lines you wish to save. For example, "save example.py 1-7 9 12" would save lines 1 through 7, 9, and 12 to a new file named example.py.

Code explanation
The code here imports classes like Interval, Functionspace, Function, etc. from the DOLFIN library. "dolfin" is a python package allowing access to the C++ software library for finite element computing, DOLFIN. FEniCS relies heavily on these classes so this will normally be the first line in your Python code. Including "from" causes the statement to repeatedly define names from the module. The "*" tells the statement to continue until all names in the module have been defined, hence, the full statement imports and defines all classes in the dolfin module.

 read the "Elements of Style" by Strunk and White (there is also an online 1918 version, an older version). never use the article "This" without a noun that follows it, since "This" by itself is very vague; it could stand for anything in the above-mentioned list of objects. Egm6321.f12 (talk) 14:42, 3 August 2012 (UTC)

Python manual reference
 give a link to the online python manual regarding the python statements "from" and "import". explain also the meaning of the "*".

see The Python Language Reference, section 6.12 on the "import" statement.

from a Unix user perspective, the "*" represents "everything", so that the above code line may mean to import all existing classes from the libray "dolfin" into the present python code. you need to verify this guess.

Egm6321.f12 (talk) 14:42, 3 August 2012 (UTC)

The import statement, including "from" and "*".

Defining the mesh and functionspace classes
 the wikipedia style (and the styles of orther publishers) is to capitalize only the first character in a title. you don't have to capitalize the first characters of every word in a title; see the above title. Egm6321.f12 (talk) 14:50, 3 August 2012 (UTC)

Code explanation
The code here first defines your mesh class as another DOLFIN class, Interval, following the syntax. The example interval runs from 0 to 1 with 20 divisions.

 explain also what "mesh" is as a python object, i.e., is it a class, a variable, etc. ? also what is "Interval" ? a function ? where can we find its code listing ? Egm6321.f12 (talk) 14:50, 3 August 2012 (UTC)

We then define a discrete function space V over the mesh. The functionspace class follows the syntax. Above, the standard Lagrange family of elements is used which can also be denoted by 'CG' for Continuous Galerkin. A degree of 1 is also shown. This denotes the standard linear Lagrange element, later yielding a computed u that is continuous and linearly varying over each mesh division.

Additional mesh definitions
The following lines can be used to create a unit square or rectangular mesh. Each cell created by the x and y divisions is divided in half to create two triangular elements.

Code explanation
Here the code defines new classes, BoundaryLeft and BoundaryRight, for the left and right boundaries. Note that variables x and y would be denoted by x[0] and x[1], respectively. We define the function "inside" to return true for all x points both on the boundary (on_boundary) and first 0, then 1. The function  checks that value1 is within machine precision of value2.

Code explanation
Here we create variables of the DOLFIN class  to assign as our values at the boundaries. The problem statement specifies that u(0)=0 and u(1)=1, demonstrated above.

Code explanation
The code here creates two variables of the previously created class types  and. These variables are used in the next step when applying the boundary conditions.

Code explanation
Here we apply the Dirichlet boundary conditions using the dolfin class  following the syntax. Note that the left and right boundaries are both included.

Code explanation
Here we define several variables including the dolfin classes  and   using the function space V. Constants f, g, nu, and ac are also defined to values given in the problem statement.

Code explanation
Here we define our a(u,v) and L(v), obtained by the derivation of the weak variational form. Note the syntax for inner product  and gradient.

Code explanation
Here the first line creates a function, u, over the function space V. Having defined a, L, and information about our essential boundary conditions, we use  to compute the solution, a finite element function u.

Code explanation
Here we plot the solution, u, seen below. Including  allows the user to manipulate the plot and is necessary to keep it on screen.


 * [[Image:L1DConDiff.png]]