~ubuntu-branches/ubuntu/trusty/ffc/trusty

« back to all changes in this revision

Viewing changes to doc/manual/chapters/python.tex

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2009-09-25 09:41:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090925094139-nb845p3ffs8j5ike
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\chapter{Python interface}
 
2
 
 
3
\ffc{} provides a Python interface in the form of a standard
 
4
Python module. The following example demonstrates how to define and
 
5
compile the variational problem for Poisson's equation in a Python
 
6
script:
 
7
 
 
8
\begin{code}
 
9
from ffc import *
 
10
 
 
11
element = FiniteElement("Lagrange", "triangle", 1)
 
12
 
 
13
v = TestFunction(element)
 
14
u = TrialFunction(element)
 
15
f = Function(element)
 
16
 
 
17
a = dot(grad(v), grad(u))*dx
 
18
L = v*f*dx
 
19
 
 
20
compile([a, L], "Poisson")
 
21
\end{code}
 
22
 
 
23
At the basic level, the only difference between the command-line
 
24
interface and the Python interface is that one must add the
 
25
import statement of the \ffc{} module and that the function
 
26
\texttt{compile} must be called when using the Python interface.
 
27
 
 
28
\section{Compiling forms: \texttt{compile}}
 
29
 
 
30
The \texttt{compile} function expects a form (see
 
31
Section~\ref{sec:formlanguage}) or a list of forms as its first
 
32
argument. It also accepts up to four additional optional arguments:
 
33
\begin{code}
 
34
compile(forms, prefix, representation, language, options)
 
35
\end{code}
 
36
 
 
37
\subsection{Input arguments}
 
38
 
 
39
The \texttt{prefix} argument can be used to control the prefix of the
 
40
file containing the generated code, which we in the above example set
 
41
to \texttt{"Poisson"}. The suffix \texttt{".h"} will be added
 
42
automatically.
 
43
 
 
44
The \texttt{representation} argument can be used to control the form
 
45
representation used for precomputation and code generation. The
 
46
default value is \texttt{"tensor"}, which indicates that the code
 
47
should be generated based on a tensor representation of the
 
48
multilinear form as described
 
49
in~\cite{KirLog06,KirLog07}. Alternatively, \texttt{"quadrature"} may be
 
50
used to specify that code should be generated based on direct
 
51
quadrature at run-time (experimental).
 
52
 
 
53
The \texttt{language} option can be used to control the output
 
54
language for the generated code. The default value is \texttt{"ufc"},
 
55
which indicates that code should be generated in the UFC
 
56
format~\cite{www:ufc,ufcmanual}. Alternatively, \texttt{"dolfin"} may
 
57
be used to generate code according to the UFC format with a small set
 
58
of additional \dolfin{}-specific wrappers.
 
59
 
 
60
The \texttt{compile} function accepts a dictionary of special
 
61
code generation options. The default values for these options may be
 
62
accessed through the variable \texttt{FFC\_OPTIONS} available in
 
63
\ffc{}.
 
64
 
 
65
\subsection{Output arguments}
 
66
 
 
67
The \texttt{compile} function returns a tuple
 
68
\begin{code}
 
69
(form_data, form_representation)
 
70
\end{code}
 
71
where \texttt{form\_data} is a list of metadata
 
72
extracted for each input form and where \texttt{form\_representation}
 
73
is a list that holds a particular internal representation of each
 
74
input form. The form representation depends on the chosen
 
75
representation mode. Accessing this data is mainly intended for
 
76
developers.
 
77
 
 
78
\subsection{Compiling finite elements}
 
79
 
 
80
The \texttt{compile} function may also be used to compile finite
 
81
elements directly (without associated forms). The following example
 
82
demonstrates how to generate code for a fifth degree Lagrange finite
 
83
element on tetrahedra:
 
84
\begin{code}
 
85
from ffc import *
 
86
 
 
87
element = FiniteElement("Lagrange", "tetrahedron", 5)
 
88
compile(element, "P5")
 
89
\end{code}
 
90
 
 
91
\section{Just-in-time (JIT) compiler: \texttt{jit}}
 
92
 
 
93
The \texttt{jit} function expects a single form
 
94
as its first argument. It also accepts up to three additional optional arguments:
 
95
\begin{code}
 
96
jit(form, representation, language, options)
 
97
\end{code}
 
98
However, instead of generating code, the \texttt{jit} function returns
 
99
the \emph{compiled} form as a Python object. It does this by
 
100
generating code, compiling it (by calling the C++ compiler) and
 
101
wrapping it as a Python module (by calling Instant/SWIG).
 
102
 
 
103
The \texttt{jit} function returns a tuple
 
104
\begin{code}
 
105
(compiled_form, compiled_module, form_data)
 
106
\end{code}
 
107
where \texttt{compiled\_form} is the compiled form (a Python wrapper
 
108
for \texttt{ufc::form}), \texttt{compiled\_module} is a Python module
 
109
containing the compiled form, finite elements, dof maps etc (a Python
 
110
wrapper for the complete set of generated code), and
 
111
\texttt{form\_data} is form metadata generated from the input form.
 
112
 
 
113
The JIT compiler caches generated modules such that if a Python script
 
114
including a call to the JIT compiler is run twice (in the same
 
115
directory) the Python module is only generated once. The generated
 
116
modules are stored in a cache directory defined by Instant. To clean
 
117
the cache, run the command \texttt{instant-clean}.