FFC: A Compiler for Variational Forms - FEniCS Project

FFC: A Compiler for Variational Forms - FEniCS Project FFC: A Compiler for Variational Forms - FEniCS Project

fenicsproject.org
from fenicsproject.org More from this publisher

<strong>FFC</strong>: A <strong>Compiler</strong> <strong>for</strong> <strong>Variational</strong> <strong>Forms</strong>Anders Logglogg@tti-c.orgToyota Technological Institute at ChicagoToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 1


<strong>FFC</strong>: the <strong>FEniCS</strong> Form <strong>Compiler</strong>• Automates a key step in the implementation of finiteelement methods <strong>for</strong> partial differential equations• Input: a variational <strong>for</strong>m and a finite element• Output: optimal C/C++ codea(v, u) = ∫ Ω∇v(x) · ∇u(x) dx<strong>FFC</strong>Poisson.h>> ffc [-l language] poisson.<strong>for</strong>mToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 2


Design goalsPrimary goals:• Any <strong>for</strong>m• Any element• Maximum efficiencySecondary goals:• Create a new standard in <strong>for</strong>m evaluation• Integration with other <strong>FEniCS</strong> projects (Robert Kirby, UofC)• Integration with PETSc tools (Matthew Knepley, Argonne)Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 3


Why a compiler?• Add a new level of automation◦ Don’t have to write the code anymore◦ Less chance of making mistakes• Add a new level of generality (any <strong>for</strong>m, any element)• Add a new level of efficiency◦ New techniques of <strong>for</strong>m evaluation◦ New techniques of <strong>for</strong>m optimization (FErari)GeneralityEfficiency<strong>Compiler</strong>Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 4


Outline• Background• Form evaluation• Implementation• Demonstration• The <strong>FEniCS</strong> projectToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 5


BackgroundToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 6


Main goal: the Automation of CMMAu = fU ≈ uTOL > 0• Input: Model Au = f and tolerance TOL > 0• Output: Solution U ≈ u satisfying ‖U − u‖ ≤ TOL• Produce a solution U satisfying a given accuracyrequirement, using a minimal amount of workToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 7


A key step: the automation of discretizationAu = fF(x) = 0(V, ˆV )• Input: Model Au = f and discrete representation (V, ˆV )• Output: Discrete system F(x) = 0• Produce a discrete system F(x) = 0 corresponding to themodel Au = f <strong>for</strong> the given discrete representation (V, ˆV )Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 8


Basic algorithm: the (Galerkin) finite element methoda(v, u) = L(v)(V, ˆV )F(x) = 0• Input: <strong>Variational</strong> problem a(v, u) = L(v) <strong>for</strong> all v anddiscrete representation (V, ˆV )• Output: Discrete system F(x) = 0Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 9


Basic example: Poisson’s equation• Strong <strong>for</strong>m: Find u ∈ C 2 (Ω) with u = 0 on ∂Ω such that−∆u = fin Ω• Weak <strong>for</strong>m: Find u ∈ H0 1 (Ω) such that∫∫∇v(x) · ∇u(x) dx = v(x)f(x) dxΩΩ<strong>for</strong> all v ∈ H 1 0(Ω)• Standard notation: Find u ∈ V such thata(v, u) = L(v)<strong>for</strong> all v ∈ ˆVwith a : ˆV × V → R a bilinear <strong>for</strong>m and L : ˆV → R a linear<strong>for</strong>m (functional)Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 10


Obtaining the discrete systemLet V and ˆV be discrete function spaces. Thena(v, U) = L(v)<strong>for</strong> all v ∈ ˆVis a discrete linear system <strong>for</strong> the approximate solution U ≈ u.With V = span{φ i } M i=1 and ˆV = span{ˆφ i } M i=1 , we obtain the linearsystemAx = b<strong>for</strong> the degrees of freedom x = (x i ) of U = ∑ Mi=1 x iφ i , whereA ij = a(ˆφ i , φ j )b i = L(ˆφ i )Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 11


Computing the linear system: assemblyNoting that a(v, u) = ∑ K∈T a K(v, u), thematrix A can be assembled byA = 0<strong>for</strong> all elements K ∈ TA += A KThe element matrix A K is defined byA K ij = a K (ˆφ i , φ j )<strong>for</strong> all local basis functions ˆφ i and φ j on KToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 12


Form evaluationToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 13


Multi-linear <strong>for</strong>msConsider a multi-linear <strong>for</strong>ma : V 1 × V 2 × · · · × V n → Rwith V 1 , V 2 , . . . , V n function spaces on the domain Ω• Typically, n = 1 (linear <strong>for</strong>m) or n = 2 (bilinear <strong>for</strong>m)• Assume V 1 = V 2 = · · · = V n = V <strong>for</strong> ease of notationWant to compute the rank n element tensor A K defined byA K i = a K (φ i1 , φ i2 , . . . , φ in )with {φ i } d i=1 the local basis on K and multi-indexi = (i 1 , i 2 , . . . , i n )Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 14


The (affine) map F K : K 0 → Kx 3X 3 = (0,1)Kx 2x = F K (X)F Kx 1K 0XF K (X) = x 1 Φ 1 (X) + x 2 Φ 2 (X) + x 3 Φ 3 (X)X 1 = (0,0) X 2 = (1,0)Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 15


Example 1: the mass matrix• Form:a(v, u) =∫Ωv(x)u(x) dx• Evaluation:A K i =∫Kφ i1 φ i2 dx∫= detF ′ KK 0Φ i1 (X)Φ i2 (X) dX = A 0 i G Kwith A 0 i = ∫ K 0Φ i1 (X)Φ i2 (X) dX and G K = detF ′ KToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 16


Example 2: Poisson• Form:• Evaluation:∫A K i =K= detF ′ Ka(v, u) =∫Ω∇φ i1 (x) · ∇φ i2 (x) dx∇v(x) · ∇u(x) dx∫∂X α1 ∂X α2 ∂Φ i1 ∂Φ i2dX = A 0∂x β ∂x β K 0∂X α1 ∂XiαG α Kα2with A 0 iα = ∫ K 0∂Φ i1∂X α1∂Φ i2∂X α2dX and G α K = detF ′ K∂X α1∂x β∂X α2∂x βToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 17


Example 3: Navier–Stokes• Form:a(v, u) =• Evaluation:∫A K i = φ i1 · (w · ∇)φ i2 dxK= detF ′ K∫Ωv · (w · ∇)u dx∫∂X α3w α2 Φ i1 [β]Φ ∂Φ i 2[β]α2 [α 1 ]∂x α1 K 0∂X α3dX = A 0 iαG α Kwith A 0 iα = ∫ K 0Φ i1 [β]Φ α2 [α 1 ] ∂Φ i 2[β]∂X α3dX andG α K = detF ′ K∂X α3∂x α1w α2SIAM :: CSE 05 – February 15 2005 – p. 18Toyota Technological Institute at Chicago :: Anders Logg


Tensor representationIn general, the element tensor A K can be represented as theproduct of a reference tensor A 0 and a geometry tensor G K :A K i= A 0 iαG α K• A 0 : a tensor of rank |i| + |α| = n + |α|• G K : a tensor of rank |α|Basic idea:• Precompute A 0 at compile-time• Generate optimal code <strong>for</strong> run-time evaluation of G K andthe product A 0 iα Gα KToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 19


Example: Poisson with P 2 elements in 2D3 33 31 01 00 10 10 00 00 -40 -4-4 0-4 01 10 03 00 00 -10 00 40 00 00 0-4 -40 00 01 10 0-1 00 00 30 04 00 0-4 -40 00 00 00 00 04 00 40 08 44 8-8 -4-4 00 -4-4 -80 0-4 -40 00 00 -40 -4-8 -4-4 08 44 80 44 0-4 -40 0-4 0-4 00 00 00 -4-4 -80 44 08 44 8Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 20


ImplementationToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 21


<strong>FFC</strong>• Implemented in Python (click <strong>for</strong> details)• Access: Python module or command-line interface• FIAT used as the finite element backend• Optimization through FErari• Supports multiple output languages (<strong>for</strong>mats)• Primary target: DOLFIN/PETSca(v, u) = ∫ Ω ∇v(x) · ∇u(x) dx SIAM :: CSE 05 – February 15 2005 – p. 22<strong>FFC</strong>Poisson.hToyota Technological Institute at Chicago :: Anders Logg


Basic usage: compiling a <strong>for</strong>m1. Implement the <strong>for</strong>m using your favorite text editor (emacs):2. Compile the <strong>for</strong>m using <strong>FFC</strong>:>> ffc poisson.<strong>for</strong>mThis will generate C++ code (Poisson.h) <strong>for</strong> DOLFINToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 23


Basic usage: solving the PDE#include #include "Poisson.h"using namespace dolfin;int main(){Mesh mesh("mesh.xml.gz");Poisson::FiniteElement element;Poisson::BilinearForm a; Poisson::LinearForm L(f);Matrix A; Vector x, b;FEM::assemble(a, L, A, b, mesh, element);GMRES::solve(A, x, b);Function u(mesh, element, x);File file("poisson.dx");file


DemonstrationToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 25


Examples• a(v, u) = ∫ Ωvu dx• a(v, u) = ∫ Ω ∇v · ∇u dx = ∫ Ω∂v ∂u∂x i ∂x idx• a(v, u) = ∫ Ω v · (w · ∇)u dx = ∫ Ω v iw j ∂u i /∂x j dx• Click to run demonstrationRequirements: <strong>FFC</strong> 0.1.4-1 and FIAT 0.1.1, available from the<strong>FEniCS</strong> web page: http://www.fenics.org/Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 26


The <strong>FEniCS</strong> projectToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 27


The <strong>FEniCS</strong> projectA free software project <strong>for</strong> the Automation of ComputationalMathematical Modeling (ACMM), developed at• The Toyota Technological Institute at Chicago• The University of Chicago• Chalmers University of Technology (Göteborg, Sweden)• Argonne National Laboratory (Chicago)More in<strong>for</strong>mation at http://www.fenics.org/Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 28


People• Todd Dupont, University of Chicago• Johan Hoffman, Chalmers University of Technology• Johan Jansson, Chalmers University of Technology• Claes Johnson, Chalmers University of Technology• Matthew Knepley, Argonne National Laboratory• Robert C. Kirby, University of Chicago• Mats G. Larson, Umeå University• Anders Logg, Toyota Technological Institute at Chicago• Ridgway Scott, University of Chicago• F. Bengzon, N. Ericsson, G. Foufas, D. Heintz, R. Hemph, P. Ingelström, K. Kraft,A. Krusper, A. Mark, A. Nilsson, E. Svensson, J. Tilander, T. Svedberg,H. Svensson, W. VillanuevaToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 29


<strong>Project</strong>s• DOLFIN, the C++ interface of <strong>FEniCS</strong>◦ Hoffman, Jansson, Logg, et al.• FErari, optimized <strong>for</strong>m evaluation◦ Kirby, Knepley, Scott• <strong>FFC</strong>, the <strong>FEniCS</strong> Form <strong>Compiler</strong>◦ Logg• FIAT, automatic generation of finite elements◦ Kirby, Knepley• Ko, simulation of mechanical systems◦ Jansson• Puffin, light-weight version <strong>for</strong> Octave/MATLAB◦ Hoffman, LoggToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 30


TODO list• Integrate <strong>FFC</strong> with DOLFIN (almost there)• Integrate <strong>FFC</strong> with FErari (the next step)• Add a (non-trivial) parser• Support just-in-time compilation• Support non-affine mappings• Manual, installation, man page, . . .• Further in<strong>for</strong>mation:http://www.fenics.org/ffc/Toyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 31


Additional slidesToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 32


The Automation of CMMAu = f(iv)U ≈ uÃũ = ˜f(i)F(x) = 0(ii)X ≈ xU ≈ uTOL(iii)(V, ˆV )tol > 0U ≈ uToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 33


ComponentsToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 34


Representation of <strong>for</strong>ms• Need to build a data structure to represent <strong>for</strong>ms:V B = { ∂|·| φ (·)∂x (·): φ : N → V }V P = {c ∏ v : v ∈ V B , c ∈ R}V S = { ∑ v : v ∈ V P }Note: V B ⊂ V P ⊂ V S ⊂ {v : N r × Ω → R}• V S is an algebra (a vector space with multiplication):v, w ∈ V S ⇒ v + w ∈ V Sv, w ∈ V S ⇒ vw ∈ V SSIAM :: CSE 05 – February 15 2005 – p. 35Toyota Technological Institute at Chicago :: Anders Logg


Evaluation of <strong>for</strong>msFor any v = ∑ c ∏ ∂ |·| φ (·) /∂x (·) , we haveA K i = a K (φ i1 , φ i2 , . . . , φ in ) == ∑ ( ∫= ∑ c ′ αK= ∑ A 0 iαG α K,∫Kv i dxc ∏ )∂ |·| φ (·) /∂x (·) dx(∫∏)∂ |·| Φ (·) /∂X (·) dXK 0( ∫where A 0 iα = ∏ )K 0∂ |·| Φ (·) /∂X (·) dX and iα Gα K = c′ αiiαToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 36


Implementation (click to return)• Build a class hierarchy: BasisFunction, Product, Sumcorresponding to the spaces V B ⊂ V P ⊂ V S• Overload operators +, -, *, [·], .dx(·) :⎧+ : V B × V B → V S * : V B × V B → V P⎪⎨⎧⎪+ : V B × V P → V S ⎨ * : V B × V P → V P. . .. . .⎪⎩⎪+ : V S × V S → V ⎩ S * : V S × V S → V S• Examples:a = v*u*dxa = v.dx(i)*u.dx(i)*dxa = v[i]*w[j]*u[i].dx(j)*dxL = v*f*dx + v*10*dsToyota Technological Institute at Chicago :: Anders LoggSIAM :: CSE 05 – February 15 2005 – p. 37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!