\documentclass[10pt]{beamer}
%\usepackage{epsfig}             % inkludering av grafikk
\usepackage{graphicx}
\pgfdeclaremask{computer}{plots/th_r2_u}
\usepackage[english]{babel}
\usepackage{supertabular,alltt,verbatim,moreverb,subfigure}
\usepackage{amssymb,wasysym}
\usepackage{epsfig}
\newcommand{\mathbfx}[1]{{\mbox{\boldmath $#1$}}}

\mode<presentation>
%\mode<article>
% %{
%\usetheme{Warsaw}
%   % or ...\newcommand{\eps}{\varepsilon}
\renewcommand{\v}{\mathbf{v}}
\newcommand{\V}{\mathbf{V}}
\renewcommand{\L}{\mathbf{L}}
\renewcommand{\u}{\mathbf{u}}
\newcommand{\w}{\mathbf{w}}
\newcommand{\f}{\mathbf{f}}
\newcommand{\y}{\mathbf{y}}
\newcommand{\g}{\mathbf{g}}
\newcommand{\G}{\mathbf{G}}
\newcommand{\h}{\mathbf{h}}
\renewcommand{\H}{\mathbf{H}}
\newcommand{\n}{\mathbf{n}}
\renewcommand{\k}{\mathbf{k}}
\renewcommand{\l}{\mathbf{l}}
\renewcommand{\r}{\mathbf{r}}
\renewcommand{\t}{\mathbf{t}}
\newcommand{\s}{\mathbf{s}}
\renewcommand{\div}{\operatorname{div}}
%\newcommand{\R}{{\mathbb{R}}}
%\renewcommand{\P}{{\mathbf{\mathbb{P}}}}
%\newcommand{\PP}{{\mathbb{ P}}}
\newcommand{\R}{{\mathbf{R}}}
\renewcommand{\P}{{\mathbf{P}}}
\newcommand{\PP}{{\mathbf{ P}}}
%\usetheme{Darmstadt}
\usetheme{Warsaw}
%\setbeamercovered{transparent}
%   % or whatever (possibly just delete it)
% %}
%
%
% % or whatever

\usepackage{times}
\usepackage[latin1]{inputenc}
\title{ SyFi - A package for symbolic finite element computations }
\author{Kent-Andre Mardal}

\begin{document}
\begin{frame}
    \titlepage
\end{frame}

%\section{Outline}
%\section{Motivation}
\begin{frame}
\frametitle{Outline - Short Description of SyFi}
\begin{block}{Purpose}
\begin{itemize}
\item SyFi is a tool for defining polygons, polynomial spaces, 
	degrees of freedom, and   finite elements 
\item SyFi makes it easy to define weak forms (differentiation and
      integration of polynomials over polygons)  
\item SyFi generates efficient C++ code for the computation of matrices 
\end{itemize}
\end{block}


\begin{block}{Dependencies}
\begin{itemize}
\item SyFi relies heavily on GiNaC and Swiginac for the symbolic computations 
and code generation 
\item SyFi can generate matrices based on either a Dolfin or a Diffpack mesh (we
plan to include other meshes soon)
\item SyFi can generate either Epetra or PyCC matrices (we plan to include
	other matrices soon)  
\item 
We are working on generating UFC compliant code (Aln\ae s' talk)    
\end{itemize}

\end{block}
\end{frame}



%\section{GiNaC/Swiginac}
\begin{frame}
\frametitle{Short Description of GiNaC and Swiginac}
\begin{block}{GiNaC}
\begin{itemize}
\item GiNaC is a C++ library for symbolic mathematics 
\item Authors: C. Bauer, C. Dams A. Frink, V. Kisil, 
  R. Kreckel, A. Sheplyakov, J. Vollinga 


\item URL: www.ginac.de  
\item License : GPL 
\end{itemize}
\end{block}


\begin{block}{Swiginac}
\begin{itemize}
\item Swiginac is a Python interface to GiNaC 
\item Authors: O. Skavhaug and O. Certik  
\item URL: http://swiginac.berlios.de/ 
\item License: Open 
\end{itemize}
\end{block}
\end{frame}


\begin{frame}[fragile]
\frametitle{Swiginac Code Example}

\begin{verbatim}
from swiginac import *  
x = symbol('x'); y = symbol('y') 

f = x*x*y*y 
print "f = ", f 

dfdx = diff(f,x)
print "df/dx = ", dfdx 

intf = integral(x,0,1,f).eval_integ()
print "integral of f from 0 to 1: ", intf 

g = cos(x) 
print "Taylor series of cos(x) around x == 0.5: ", 
print g.series(x == 0.5, 10) 

\end{verbatim}


\end{frame}

\begin{frame}
\frametitle{SyFi Extends GiNaC/Swiginac}
\begin{block}{ GiNaC/Swiginac supports:} 
\begin{itemize}
\item Polynomials 
\item Differentiation with respect to one variable 
\item Integration with respect to one variable 
\end{itemize}
\end{block}
\begin{block}{Basic Extensions in SyFi:}
\begin{itemize}
\item Polynomial spaces (such as Legendre and Bernstein)  
\item Differentiations with respect to several 
variables 
\item Integration over polygons  
\end{itemize}
\end{block}

$\rightarrow$ SyFi extends GiNaC/Swiginac with the ingredients
typically needed in finite element methods  


\end{frame}

\begin{frame}[fragile]
\frametitle{Demo: Bernstein polynomials on a Triangle}

\begin{verbatim}
from swiginac import *
from SyFi import *
x = cvar.x; y = cvar.y 

t = ReferenceTriangle()

B = bernstein(2, t, 'a')
print "2. order Bernstein polynomial on a triangle ", B 

P = B[0]
dPdx = diff(P, x)  
print "Derivative with respect to x ", dPdx  

int_dPdx = t.integrate(dPdx) 
print "Integral of dPdx over an triangle ", int_dPdx 

int_line1_dPdx = t.line(1).integrate(dPdx) 
print "Integral of dPdx over an edge",  int_line1_dPdx 
\end{verbatim}


\end{frame}





\begin{frame}
\frametitle{Elements currently implemented in SyFi}
\begin{block}{Finite Elements}
\begin{itemize}
\item continuous and discontinuous Lagrangian elements (arbitrary order) 
\item Nedelec elements (arbitrary order) 
\item Nedelec H(div) elements (arbitrary order) 
\item Raviart-Thomas elements (arbitrary order) 
\item Crouzeix-Raviart elements 
\item Hermite elements 
\item Bubble elements 
\item Arnold-Falk-Winther elasticity element (weak symmetry) (arbitrary order) 
\end{itemize}
\end{block}
\end{frame}



\begin{frame}
\frametitle{Evaluation of Weak Forms in SyFi}
We will now demonstrate the computation of various element matrices
in SyFi 
\begin{itemize}
\item The mass matrix on a reference triangle:  
\[
\mathbfx{M}_{ij} = \int_T N_i \, N_j \, dx   
\]
\item The stiffness matrix on a mapped tetrahedron:  
\[
\mathbfx{A}_{ij} = \int_T (J^{-1} \nabla N_i) \cdot (J^{-1} \nabla N_j) \, dx   
\]
where $J$ is the Jacobian of the geometry mapping
\end{itemize}
\end{frame}








\begin{frame}[fragile]
\frametitle{Demo: Computing the Mass Matrix on the Reference Triangle }

\begin{verbatim}

from swiginac import *
from SyFi import *

t = ReferenceTriangle()
fe = LagrangeFE(t, 3) 

for i in range(0, fe.nbf()):
   for j in range(0, fe.nbf()):
      Aij = t.integrate(fe.N(i)*fe.N(i))

\end{verbatim}


\end{frame}

\begin{frame}[fragile]
\frametitle{Demo: Computing the Stiffness Matrix on a Mapped Tethrahedron}

\begin{verbatim}

from swiginac import *
from SyFi import *

t = ReferenceTetrahedron()
fe = LagrangeFE(t, 3) 

J = symbolic_matrix(3,3, ``J'')

for i in range(0, fe.nbf()):
   for j in range(0, fe.nbf()):
      integrand = inner(grad(J, fe.N(i)),  
                        grad(J, fe.N(j)))
      Aij = t.integrate(integrand) 
\end{verbatim}


\end{frame}




\begin{frame}
\frametitle{The Computation of the Jacobian matrix}
We will now demonstrate the computation the Jacobian matrix
for a nonlinear PDE
\begin{itemize}
\item $\mathbfx{u} = \sum_j u_j \mathbfx{N}_j$ 
\item 
Nonlinear convection diffusion equation
\[
\mathbfx{F}_i = \int_T (\mathbfx{u}\cdot \nabla \mathbfx{u})\cdot \mathbfx{N}_i   
 + \nabla \mathbfx{u}:\nabla \mathbfx{N}_i) \, dx    
\]

\item The Jacobian matrix 
\[
\mathbfx{J}_{ij} =  \frac{\partial \mathbfx{F}_i}{\partial u_j } 
\]
\end{itemize}
\end{frame}








\begin{frame}[fragile]
\frametitle{SyFi Code Example : Convection-Diffusion Matrix}
{
\renewcommand{\baselinestretch}{0.85}\footnotesize
\begin{verbatim}

.. initialize element 

for i in range(0,fe.nbf()): 

  # compute diffusion term 
  fi_diffusion = inner(grad(u), grad(fe.N(i))) 
  
  # compute convection term 
  uxgradu = (u.transpose()*grad(u)).evalm()  
  fi_convection = inner(uxgradu, fe.N(i), True) 

  fi = fi_diffusion + fi_convection
  Fi = polygon.integrate(fi) 

  for j in range(0,fe.nbf()): 
    # differentiate to get the Jacobian 
    Jij = diff(Fi, uj) 
    print "J[%d,%d]=%s "%(i,j,Jij)

\end{verbatim}
}
\end{frame}

\begin{frame}
\frametitle{Convection and Power-law Diffusion }
Now consider the Power-law model
\begin{itemize}
\item 
Nonlinear convection diffusion equation
\[
\mathbfx{F}_i = \int_T (\mathbfx{u}\cdot \nabla \mathbfx{u})\cdot \mathbfx{N}_i   
 + \mu(\mathbfx{u}) \nabla \mathbfx{u}:\nabla \mathbfx{N}_i) \, dx    
\]

\item The viscosity:  
\[
\mu = \mu_0 \|\nabla \mathbfx{u}\|^{2n} 
\]
\end{itemize}
\end{frame}




\begin{frame}[fragile]
\frametitle{SyFi Code Example : Convection and Power-law Diffusion }

{
\renewcommand{\baselinestretch}{0.85}\footnotesize
\begin{verbatim}

n = symbol("n") 

for i in range(0,fe.nbf()): 

  # nonlinear power-law diffusion term 
  mu = mu0*inner(grad(u), grad(u))
  fi_diffusion = pow(mu,n)*inner(grad(u), grad(fe.N(i))) 
  
  # nonlinear convection term 
  uxgradu = (u.transpose()*grad(u)).evalm()  
  fi_convection = inner(uxgradu, fe.N(i), True) 

  fi = fi_diffusion + fi_convection

  Fi = polygon.integrate(fi) 

  for j in range(0,fe.nbf()): 
    # differentiate to get the Jacobian 
    Jij = diff(Fi, uj) 
    print "J[%d,%d]=%s\n"%(i,j, Jij.evalf().printc())



\end{verbatim}
}


\end{frame}


\begin{frame}[fragile]
\frametitle{Inlining of C++ in Python}
\begin{block}{The module Instant}
\begin{itemize}
\item 
We have created a Python module for inlining
of C++ code in Python
\item 
It employs SWIG for the generation of wrapper code 
and distutils for compiling extenstion modules 
\end{itemize}
\end{block}
{
\renewcommand{\baselinestretch}{0.85}\footnotesize
\begin{verbatim}

from Instant import inline

add_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)
\end{verbatim}
}

\end{frame}

\begin{frame}
\frametitle{UFC compliant code generation}
\begin{block}{UFC}
\begin{itemize}
\item UFC - Unified Form-assembly Code  
\item Low level spesification of class declaration and 
function signatures for finite elements, element
tensors etc. 
\item Together with Logg, Aln\ae s, Skavhaug and Langtangen 
      we are working on spesifing UFC
\item Aln\ae s will talk more about it later today
\end{itemize}
\end{block}
\end{frame}



\begin{frame}[fragile]
\frametitle{Using SyFi/UFC within PyCC}
{
\renewcommand{\baselinestretch}{0.85}\footnotesize
\begin{verbatim}

import stuff.. 

n = 10 
m = Mesh.UnitSquare(n,n)

mf = UMF.UserMapMatrixFactory()
mf.select_mesh(m)


polygon = SyFi.ReferenceTriangle()
fe = SyFi.LagrangeFE(polygon, 1) 

def mass_integrand(u, v, G, Ginv):
    return inner(u, v)

mass_form = UfcCG.BiLinearForm(mass_integrand, name = "mass" ) 

compiled_fe, compiled_form = UfcCG.gen_extension_module(fe, mass_form)  

A = mf.compute_matrix(compiled_form)

\end{verbatim}
}

\end{frame}




\begin{frame}
\frametitle{Concluding Remarks}
\begin{block}{Present Use of SyFi}
\begin{itemize}
\item We have implemented a set of simple test examples within SyFi/PyCC
for the Poisson problem, Stokes problem, convection-diffusion problems
and Navier-Stokes equations 
\item We have used SyFi/PyCC in rather advanced applications concerning the
electrical activity of the heart 
\end{itemize}
\end{block}
\begin{block}{Future}
\begin{itemize}
\item Support other meshes and other matrices
\item Implement more elements
\item Continue developing solvers for fluid and solid mechanics 
\end{itemize}
\end{block}

\end{frame}



\end{document}







