## 2 Automated Mathematical Modelling  
In the context of a finite element solver, it is possible to divide the code into problem-specific and generic components. The approach which we follow is to generate automatically code for those parts which are specific to a given finite element variational form and to make use of, and develop, where necessary, reusable library components for tasks which are independent of the precise finite element variational form. The code which is specific to a particular finite element variational problem is the finite element basis, the degree of freedom mapping and the element matrices and vectors. For complicated problems, particularly those that involve a number of coupled equations and combinations of different and possibly unusual finite element spaces, developing the code to compute element matrices and vectors is error prone, tedious and time consuming. Furthermore, developing high performance code by hand for complicated problems is not trivial. A number of efforts are currently under way to address the gulf between performance and generality in scientific software design (several of which are listed in [7]). An approach to reconciling mathematical expressiveness and generality with performance is automated code generation using a compiler for variational forms [6; 7; 8]. A domain specific language can be created which mirrors the standard mathematical notation for variational methods, thereby providing a high degree of expressiveness, and a compiler can be used to generate low-level code from the high-level input. The compiler approach permits different strategies for representing element matrices and vectors [6; 10] and various optimisation approaches can be employed. In particular, optimisations can be employed that are not tractable in hand-written code [6; 8]. In this work, we will present generalisations of the FEniCS Form Compiler (FFC) [6; 8; 13] for extended finite element methods. Firstly though we illustrate the compiler approach for a conventional continuous problem. For the weighted Poisson equation on the domain \(\Omega\subset\mathbb{R}^{d}\), \(1\leq d\leq 3\) with homogeneous Dirichlet boundary conditions, the variational form of the problem reads: given sufficiently regular functions \(w:\Omega\rightarrow\mathbb{R}\), and \(f:\Omega\rightarrow\mathbb{R}\), find \(u\in H_{0}^{1}\left(\Omega\right)\) such that  
\[a\left(v,u\right)=L\left(v\right)\quad\forall\;v\in H_{0}^{1}\left(\Omega \right), \tag{1}\]  
where  
\[a\left(v,u\right) =\int_{\Omega}w\
abla v\cdot\
abla u\,d\Omega, \tag{2}\] \[L\left(v\right) =\int_{\Omega}vf\,d\Omega. \tag{3}\]  
The term \(a\left(v,u\right)\) is known as the \"bilinear form\" and \(L\left(v\right)\) is known as the \"linear form\". A finite element formulation of this problem follows from replacing the function space \(H_{0}^{1}\left(\Omega\right)\) by a suitable finite element space \(V\subset H_{0}^{1}\left(\Omega\right)\). If we choose a continuous piecewise linear Lagrange finite element basis in three dimensions (\(d=3\)) on tetrahedra,  
\[V=\left\{v\in H_{0}^{1}\left(\Omega\right),v|_{\Omega_{e}}\in P^{1}\left( \Omega_{e}\right)\forall e\right\}, \tag{4}\]  
where \(\Omega_{e}\) is a cell of the triangulation of \(\Omega\) and \(P^{1}\) is space of standard linear Lagrange shape functions on a tetrahedron. The input to the form compiler FFC for this variational problem reads:  
element = FiniteElement(\"Lagrange\", \"tetrahedron\", 1) v = TestFunction(element) u = TrialFunction(element) w = Function(element) f = Function(element) a = w*dot(grad(v), grad(u))*dx L = v*f*dx In this example, element defines the finite element space, v and u are the test and trial functions, respectively, and w and f are supplied functions. It is assumed in the above input that \(w\) and \(f\) come from the finite element space \(V\) or will be interpolated in \(V\).