10.07.2015 Views

SyFi - A package for symbolic finite element ... - FEniCS Project

SyFi - A package for symbolic finite element ... - FEniCS Project

SyFi - A package for symbolic finite element ... - FEniCS Project

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong>computationsKent-Andre MardalNovember 8, 2006Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Outline - Short Description of <strong>SyFi</strong>Purpose<strong>SyFi</strong> is a tool <strong>for</strong> defining polygons, polynomial spaces, degreesof freedom, and <strong>finite</strong> <strong>element</strong>s<strong>SyFi</strong> makes it easy to define weak <strong>for</strong>ms (differentiation andintegration of polynomials over polygons)<strong>SyFi</strong> generates efficient C++ code <strong>for</strong> the computation ofmatricesDependencies<strong>SyFi</strong> relies heavily on GiNaC and Swiginac <strong>for</strong> the <strong>symbolic</strong>computations and code generation<strong>SyFi</strong> can generate matrices based on either a Dolfin or aDiffpack mesh (we plan to include other meshes soon)<strong>SyFi</strong> can generate either Epetra or PyCC matrices (we plan toinclude other matrices soon)We are working on generating UFC compliant code (Alnæs’ talk)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Short Description of GiNaC and SwiginacGiNaCGiNaC is a C++ library <strong>for</strong> <strong>symbolic</strong> mathematicsAuthors: C. Bauer, C. Dams A. Frink, V. Kisil, R. Kreckel, A.Sheplyakov, J. VollingaSwiginacURL: www.ginac.deLicense : GPLSwiginac is a Python interface to GiNaCAuthors: O. Skavhaug and O. CertikURL: http://swiginac.berlios.de/License: OpenKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Swiginac Code Examplefrom swiginac import *x = symbol(’x’); y = symbol(’y’)f = x*x*y*yprint "f = ", fdfdx = diff(f,x)print "df/dx = ", dfdxintf = integral(x,0,1,f).eval_integ()print "integral of f from 0 to 1: ", intfg = cos(x)print "Taylor series of cos(x) around x == 0.5: ",print g.series(x == 0.5, 10)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


<strong>SyFi</strong> Extends GiNaC/SwiginacGiNaC/Swiginac supports:PolynomialsDifferentiation with respect to one variableIntegration with respect to one variableBasic Extensions in <strong>SyFi</strong>:Polynomial spaces (such as Legendre and Bernstein)Differentiations with respect to several variablesIntegration over polygons→ <strong>SyFi</strong> extends GiNaC/Swiginac with the ingredients typicallyneeded in <strong>finite</strong> <strong>element</strong> methodsKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Demo: Bernstein polynomials on a Trianglefrom swiginac import *from <strong>SyFi</strong> import *x = cvar.x; y = cvar.yt = ReferenceTriangle()B = bernstein(2, t, ’a’)print "2. order Bernstein polynomial on a triangle ", BP = B[0]dPdx = diff(P, x)print "Derivative with respect to x ", dPdxint_dPdx = t.integrate(dPdx)print "Integral of dPdx over an triangle ", int_dPdxint_line1_dPdx = t.line(1).integrate(dPdx)print "Integral of dPdx over an edge", int_line1_dPdxKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Elements currently implemented in <strong>SyFi</strong>Finite Elementscontinuous and discontinuous Lagrangian <strong>element</strong>s (arbitraryorder)Nedelec <strong>element</strong>s (arbitrary order)Nedelec H(div) <strong>element</strong>s (arbitrary order)Raviart-Thomas <strong>element</strong>s (arbitrary order)Crouzeix-Raviart <strong>element</strong>sHermite <strong>element</strong>sBubble <strong>element</strong>sArnold-Falk-Winther elasticity <strong>element</strong> (weak symmetry)(arbitrary order)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Evaluation of Weak Forms in <strong>SyFi</strong>We will now demonstrate the computation of various <strong>element</strong>matrices in <strong>SyFi</strong>The mass matrix on a reference triangle:∫M ij = N i N j dxThe stiffness matrix on a mapped tetrahedron:∫A ij = (J −1 ∇N i ) · (J −1 ∇N j ) dxTwhere J is the Jacobian of the geometry mappingTKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Demo: Computing the Mass Matrix on the ReferenceTrianglefrom swiginac import *from <strong>SyFi</strong> import *t = ReferenceTriangle()fe = LagrangeFE(t, 3)<strong>for</strong> i in range(0, fe.nbf()):<strong>for</strong> j in range(0, fe.nbf()):Aij = t.integrate(fe.N(i)*fe.N(i))Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Demo: Computing the Stiffness Matrix on a MappedTethrahedronfrom swiginac import *from <strong>SyFi</strong> import *t = ReferenceTetrahedron()fe = LagrangeFE(t, 3)J = <strong>symbolic</strong>_matrix(3,3, ‘‘J’’)<strong>for</strong> i in range(0, fe.nbf()):<strong>for</strong> j in range(0, fe.nbf()):integrand = inner(grad(J, fe.N(i)),grad(J, fe.N(j)))Aij = t.integrate(integrand)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


The Computation of the Jacobian matrixWe will now demonstrate the computation the Jacobian matrix <strong>for</strong> anonlinear PDEu = ∑ j u jN jNonlinear convection diffusion equation∫F i = (u · ∇u) · N i + ∇u : ∇N i ) dxThe Jacobian matrixTJ ij = ∂F i∂u jKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


<strong>SyFi</strong> Code Example : Convection-Diffusion Matrix.. initialize <strong>element</strong><strong>for</strong> i in range(0,fe.nbf()):# compute diffusion termfi_diffusion = inner(grad(u), grad(fe.N(i)))# compute convection termuxgradu = (u.transpose()*grad(u)).evalm()fi_convection = inner(uxgradu, fe.N(i), True)fi = fi_diffusion + fi_convectionFi = polygon.integrate(fi)<strong>for</strong> j in range(0,fe.nbf()):# differentiate to get the JacobianJij = diff(Fi, uj)print "J[%d,%d]=%s "%(i,j,Jij)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Convection and Power-law DiffusionNow consider the Power-law modelNonlinear convection diffusion equation∫F i = (u · ∇u) · N i + µ(u)∇u : ∇N i ) dxTThe viscosity:µ = µ 0 ‖∇u‖ 2nKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


<strong>SyFi</strong> Code Example : Convection and Power-lawDiffusionn = symbol("n")<strong>for</strong> i in range(0,fe.nbf()):# nonlinear power-law diffusion termmu = mu0*inner(grad(u), grad(u))fi_diffusion = pow(mu,n)*inner(grad(u), grad(fe.N(i)))# nonlinear convection termuxgradu = (u.transpose()*grad(u)).evalm()fi_convection = inner(uxgradu, fe.N(i), True)fi = fi_diffusion + fi_convectionFi = polygon.integrate(fi)<strong>for</strong> j in range(0,fe.nbf()):# differentiate to get the JacobianJij = diff(Fi, uj)print "J[%d,%d]=%s\n"%(i,j, Jij.evalf().printc())Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Inlining of C++ in PythonThe module InstantWe have created a Python module <strong>for</strong> inlining of C++ code inPythonIt employs SWIG <strong>for</strong> the generation of wrapper code and distutils<strong>for</strong> compiling extenstion modulesfrom Instant import inlineadd_func = inline("double add(double a, double b){ return a+b; }")print "The sum of 3 and 4.5 is ", add_func(3, 4.5)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


UFC compliant code generationUFCUFC - Unified Form-assembly CodeLow level spesification of class declaration and functionsignatures <strong>for</strong> <strong>finite</strong> <strong>element</strong>s, <strong>element</strong> tensors etc.Together with Logg, Alnæs, Skavhaug and Langtangen we areworking on spesifing UFCAlnæs will talk more about it later todayKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Using <strong>SyFi</strong>/UFC within PyCCimport stuff..n = 10m = Mesh.UnitSquare(n,n)mf = UMF.UserMapMatrixFactory()mf.select_mesh(m)polygon = <strong>SyFi</strong>.ReferenceTriangle()fe = <strong>SyFi</strong>.LagrangeFE(polygon, 1)def mass_integrand(u, v, G, Ginv):return inner(u, v)mass_<strong>for</strong>m = UfcCG.BiLinearForm(mass_integrand, name = "mass" )compiled_fe, compiled_<strong>for</strong>m = UfcCG.gen_extension_module(fe, mass_<strong>for</strong>m)A = mf.compute_matrix(compiled_<strong>for</strong>m)Kent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations


Concluding RemarksPresent Use of <strong>SyFi</strong>We have implemented a set of simple test examples within<strong>SyFi</strong>/PyCC <strong>for</strong> the Poisson problem, Stokes problem,convection-diffusion problems and Navier-Stokes equationsWe have used <strong>SyFi</strong>/PyCC in rather advanced applicationsconcerning the electrical activity of the heartFutureSupport other meshes and other matricesImplement more <strong>element</strong>sContinue developing solvers <strong>for</strong> fluid and solid mechanicsKent-Andre Mardal<strong>SyFi</strong> - A <strong>package</strong> <strong>for</strong> <strong>symbolic</strong> <strong>finite</strong> <strong>element</strong> computations

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

Saved successfully!

Ooh no, something went wrong!