?? python.tex
字號:
\chapter{Python interface}\ffc{} provides a Python interface in the form of a standardPython module. The following example demonstrates how to define andcompile the variational problem for Poisson's equation in a Pythonscript:\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))*dxL = v*f*dxcompile([a, L], "Poisson")\end{code}At the basic level, the only difference between the command-lineinterface and the Python interface is that one must add theimport statement of the \ffc{} module and that the function\texttt{compile} must be called when using the Python interface.\section{Compiling forms: \texttt{compile}}The \texttt{compile} function expects a form (seeSection~\ref{sec:formlanguage}) or a list of forms as its firstargument. It also accepts up to four additional optional arguments:\begin{code}compile(forms, prefix, representation, language, options)\end{code}\subsection{Input arguments}The \texttt{prefix} argument can be used to control the prefix of thefile containing the generated code, which we in the above example setto \texttt{"Poisson"}. The suffix \texttt{".h"} will be addedautomatically.The \texttt{representation} argument can be used to control the formrepresentation used for precomputation and code generation. Thedefault value is \texttt{"tensor"}, which indicates that the codeshould be generated based on a tensor representation of themultilinear form as describedin~\cite{KirLog06,KirLog07}. Alternatively, \texttt{"quadrature"} may beused to specify that code should be generated based on directquadrature at run-time (experimental).The \texttt{language} option can be used to control the outputlanguage for the generated code. The default value is \texttt{"ufc"},which indicates that code should be generated in the UFCformat~\cite{www:ufc,ufcmanual}. Alternatively, \texttt{"dolfin"} maybe used to generate code according to the UFC format with a small setof additional \dolfin{}-specific wrappers.The \texttt{compile} function accepts a dictionary of specialcode generation options. The default values for these options may beaccessed through the variable \texttt{FFC\_OPTIONS} available in\ffc{}.\subsection{Output arguments}The \texttt{compile} function returns a tuple\begin{code}(form_data, form_representation)\end{code}where \texttt{form\_data} is a list of metadataextracted for each input form and where \texttt{form\_representation}is a list that holds a particular internal representation of eachinput form. The form representation depends on the chosenrepresentation mode. Accessing this data is mainly intended fordevelopers.\subsection{Compiling finite elements}The \texttt{compile} function may also be used to compile finiteelements directly (without associated forms). The following exampledemonstrates how to generate code for a fifth degree Lagrange finiteelement on tetrahedra:\begin{code}from ffc import *element = FiniteElement("Lagrange", "tetrahedron", 5)compile(element, "P5")\end{code}\section{Just-in-time (JIT) compiler: \texttt{jit}}The \texttt{jit} function expects a single formas its first argument. It also accepts up to three additional optional arguments:\begin{code}jit(form, representation, language, options)\end{code}However, instead of generating code, the \texttt{jit} function returnsthe \emph{compiled} form as a Python object. It does this bygenerating code, compiling it (by calling the C++ compiler) andwrapping it as a Python module (by calling Instant/SWIG).The \texttt{jit} function returns a tuple\begin{code}(compiled_form, compiled_module, form_data)\end{code}where \texttt{compiled\_form} is the compiled form (a Python wrapperfor \texttt{ufc::form}), \texttt{compiled\_module} is a Python modulecontaining the compiled form, finite elements, dof maps etc (a Pythonwrapper for the complete set of generated code), and\texttt{form\_data} is form metadata generated from the input form.The JIT compiler caches generated modules such that if a Python scriptincluding a call to the JIT compiler is run twice (in the samedirectory) the Python module is only generated once.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -