\chapter{Python interface}

\ffc{} provides a Python interface in the form of a standard
Python module. The following example demonstrates how to define and
compile the variational problem for Poisson's equation in a Python
script:

\begin{code}
from ffc import *

element = FiniteElement("Lagrange", "triangle", 1)

v = TestFunction(element)
u = TrialFunction(element)
f = Function(element)

a = dot(grad(v), grad(u))*dx
L = v*f*dx

compile([a, L], "Poisson")
\end{code}

At the basic level, the only difference between the command-line
interface and the Python interface is that one must add the
import statement of the \ffc{} module and that the function
\texttt{compile} must be called when using the Python interface.

\section{The \texttt{compile} function}

The \texttt{compile} function expects a form (see
Section~\ref{sec:formlanguage}) or a list of forms as its first
argument. It also accepts up to four additional optional arguments:
\begin{code}
compile(forms, prefix, representation, language, options)
\end{code}

The \texttt{prefix} argument can be used to control the prefix of the
file containing the generated code, which we in the above example set
to \texttt{"Poisson"}. The suffix \texttt{".h"} will be added
automatically.

The \texttt{representation} argument can be used to control the form
representation used for precomputation and code generation. The
default value is \texttt{"tensor"}, which indicates that the code
should be generated based on a tensor representation of the
multilinear form as described
in~\cite{KirLog06,KirLog07}. Alternatively, \texttt{"quadrature"} may be
used to specify that code should be generated based on direct
quadrature at run-time (experimental).

The \texttt{language} option can be used to control the output
language for the generated code. The default value is \texttt{"ufc"},
which indicates that code should be generated in the UFC
format~\cite{www:ufc,ufcmanual}. Alternatively, \texttt{"dolfin"} may
be used to generate code according to the UFC format with a small set
of additional \dolfin{}-specific wrappers.

Finally, the \texttt{compile} function accepts a dictionary of special
code generation options. The default values for these options may be
accessed through the variable \texttt{FFC\_OPTIONS} available in
\ffc{}.

\section{Compiling finite elements}

The \texttt{compile} function may also be used to compile finite
elements directly (without associated forms). The following example
demonstrates how to generate code for a fifth degree Lagrange finite
element on tetrahedra:
\begin{code}
from ffc import *

element = FiniteElement("Lagrange", "tetrahedron", 5)
compile(element, "P5")
\end{code}
