\input{header.tex}

\graphicspath{
{images/png/}{images/}{images/plots/}
}

\begin{document}

\setcounter{tocdepth}{2}
\tableofcontents
\newpage

\section{Laplace equation}
\label{chap:laplace}

For a computational domain $\Omega\subset \R^d$ the Laplace equation is given by
\begin{equation}\label{eq:laplace}
  \begin{array}{ll}
    Δu = 0 \quad \text{on }\Omega.
  \end{array}
\end{equation}
A classical solution $u: \Omega \to \R$ fulfills \eqref{eq:laplace}. For a unique solution also boundary conditions have to be specified, e.g.
\begin{equation}
  \begin{array}{rcll}
    ∇u(\bfx) \cdot \bfn &=& 0 \quad &\text{on } \Gamma_N,\\[4mm]
    u(\bfx) &=& u_0(\bfx) \quad &\text{on } \Gamma_D,
  \end{array}
\end{equation}
where the homogeneous Neumann-type boundary conditions for $\bfx \in \Gamma_N$ set the flux over the boundary in normal direction $\bfn$ to zero and the Dirichlet-type boundary conditions on $\Gamma_D$ prescribe a value for $u$ on the boundary.

\subsection{Analytical solution}
%
The solution for a 1D case on $\Omega = [0,l]$ with $u_0(0)=a$, $u_0(l)=b$, simply is
\begin{equation*}
  \begin{array}{lll}
    u(x) = a + (b-a)\,x/l.
  \end{array}
\end{equation*}
For the 2D case on $\Omega = [0,1]^2$ with Dirichlet boundary conditions
\begin{equation*}
  \begin{array}{lll}
    u_0(x_1,1) = \sin(k\,\pi\,x_1), \quad k \in \N  \qquad &\text{\say{top}},\\[4mm]
    u_0(x_1,0) = u_0(0,x_2) = u_0(1,x_2) = 0  \quad &\text{\say{left, right and bottom}},\\[4mm]
  \end{array}
\end{equation*}
we have the solution
\begin{equation*}
  \begin{array}{lll}
    u(x_1,x_2) = c_1\,\sin(k\,\pi\,x_1)\,e^{k\,\pi\,x_2} + c_2\,\sin(k\,\pi\,x_1)\,e^{-k\,\pi\,x_2}, \qquad \text{where}\\[4mm]
    c_1 = 1/\big(2\,\sinh(k\,\pi)\big), \quad c_2=-1/\big(2\,\sinh(k\,\pi)\big).
  \end{array}
\end{equation*}
Also see exercise in \say{Grundlagen des Wissenschaftlichen Rechnens} (2013 3.2d).

\subsection{Finite Element formulation}

By multiplication of a testfunction $\phi\in H^1_0(\Omega)$ and integration follows the weak formulation of \eqref{eq:laplace}:
\begin{equation}
  \begin{array}{ll}
    \ds\int_{\Omega}Δu\,\phi\,\d \bfx = 0 \quad \forall \phi\in H^1_0(\Omega)
  \end{array}
\end{equation}
For a definition of $H^1_0$ see section \ref{sec:hilbert}.

The Laplace operator can be written as $Δu=∇\cdot(∇u)$. Applying divergence theorem in form of \eqref{eq:gauss1} with $f=\phi$ and $\bfF=∇u$ yields
\begin{equation}
  \begin{array}{ll}
    -\ds\int_{\Omega}∇u \cdot ∇\phi \,\d \bfx + \ds\int_{\p \Omega} (\phi\,∇u)\cdot\bfn\,\d \bfx  = 0 \quad \forall \phi\in H^1_0(\Omega)
  \end{array}
\end{equation}
Because $\phi$ is zero on the boundary, the second term vanishes:
\begin{equation}
  \begin{array}{ll}
    -\ds\int_{\Omega}∇u \cdot ∇\phi \,\d \bfx = 0 \quad \forall \phi\in H^1_0(\Omega)
  \end{array}
\end{equation}

Now we have to specify a finite-dimensional ansatz space to choose the solution function from. We do this by specifying a basis and take the span of it: $V:=\spn\{\phi_1, \dots \phi_n\}$.

The numerical solution is given by
\begin{equation}
  \begin{array}{ll}
    u_h(\bfx) = \s{i=1}{N} u_i\,\phi_i(\bfx).
  \end{array}
\end{equation}
We also take $V$ as the space of testfunctions.
Plugging this into \eqref{eq:laplace_weak} yields
\begin{equation}\label{eq:laplace_discretized}
  \begin{array}{ll}
    -\s{i=1}{N} u_i \ds\int_{\Omega}∇\phi_i\cdot ∇\phi_j\,\d\bfx = 0 \quad \text{for }j=1,\dots,N.
  \end{array}
\end{equation}
The minus sign is kept for similarity with later mentioned problem equations that also have a right-hand side.

A reasonable choice of ansatz functions are functions that have limited support. We discretize the domain $\Omega$ by Finite Elements $\Omega_e$,
\begin{equation}
  \begin{array}{ll}
    \Omega = \overset{M}{\underset{e=1}{\bigcup}} \,\Omega_e = \Omega_1 \dot{\cup} \cdots \dot{\cup} \Omega_M,
  \end{array}
\end{equation} and define nodes with global indices $N(e)$ on each element $e$. Interpolating ansatz functions are now chosen such that they have the value 1 at only one node and the value 0 at all other nodes. The support is contained just within the elements that are adjacent to the node where the function is 1.

\subsection{Ansatz functions}
A simple choice that fulfills the requirements are first-order Lagrange functions $L_{i,p},p=1$ which are defined a follows for $d=1$ and depicted in \cref{fig:lagrange}.
\begin{equation}
  \begin{array}{ll}
    \varphi_i: [0,1] \to \R,\quad
    \varphi_1(x) = L_{1,1}(x) := 1-x, \qquad \varphi_2(x) = L_{2,1}(x) := x
  \end{array}
\end{equation}
For higher dimensions they are composed by a tensor product ansatz.
\begin{equation}
  \begin{array}{ll}
    \varphi_i(\bfx) = \bfL_{i}(\bfx) := \prod\limits_{k=1}^{d} L_{j,1}(x_k)
  \end{array}
\end{equation}
The local numbering of the ansatz functions of an element proceeds fastest in the first dimension then in the second and so on as shown in \cref{fig:element1}

\begin{figure}
  \centering
  \subfig{element1}{4cm}{Numbering and element coordinate system for a 2D first-order Lagrange element}\,
  \subfig{element2}{4.5cm}{Arbitrarily shaped element}
  \,
  \subfigpdf{lagrange}{6cm}{first order Lagrange ansatz functions}
  \caption{2D first-order Lagrange element}
  \label{fig:2d-lagrange}
\end{figure}

\subsection{Transformation of integration domain}
The definition of the ansatz functions was in parameter space, i.e. on the unit interval $[0,1]^d$. The corresponding coordinate system is $\bfxi = \{\xi_1, \dots \xi_d\}$. However, integration over the elements $\Omega_e$ of the computational domain is required. The node coordinates which define the elements are given in the global coordinate system $\bfx = \{x_1, \dots x_d\}$. A mapping from $\bfxi$ to $\bfx$ can be performed using multi-linear interpolation between the nodal coordinates $\bfx^i$:
\begin{equation}\label{eq:multilagrange}
  \begin{array}{ll}
    \bfx(\bfxi) = \Phi(\bfxi) := \ds\sum\limits_{i} \bfL_i(\bfxi)\,\bfx^i.
  \end{array}
\end{equation}
Note that again Lagrange functions of first order appear, but this is part of the parameter space to global space mapping and independent of the choosen ansatz functions. For 1D and 2D problems Eq.~\eqref{eq:multilagrange} can be written out as:
\begin{equation}\label{eq:fe_phi}
  \begin{array}{ll}
    \text{1D:}\quad
    \Phi(\xi_1) =& (1-\xi_1)\,\bfx^1 + \xi_1\,\bfx^2\\[4mm]
    \text{2D:}\quad
    \Phi(\bfxi) =& (1-\xi_1)\,(1-\xi_2)\,\bfx^1 + \xi_1\,(1-\xi_2)\,\bfx^2 + (1-\xi_1)\,\xi_2\,\bfx^3 + \xi_1\,\xi_2\,\bfx^4.\\[4mm]
    \text{3D:}\quad
    \Phi(\bfxi) =&
      (1-\xi_1)\,(1-\xi_2)\,(1-\xi_3)\,\bfx^1 + \xi_1\,(1-\xi_2)\,(1-\xi_3)\,\bfx^2 + (1-\xi_1)\,\xi_2\,(1-\xi_3)\,\bfx^3 + \xi_1\,\xi_2\,(1-\xi_3)\,\bfx^4\\[4mm]
      &+ (1-\xi_1)\,(1-\xi_2)\,\xi_3\,\bfx^5 + \xi_1\,(1-\xi_2)\,\xi_3\,\bfx^6 + (1-\xi_1)\,\xi_2\,\xi_3\,\bfx^7 + \xi_1\,\xi_2\,\xi_3\,\bfx^8

  \end{array}
\end{equation}
The node numbering and coordinate frames are defined by \cref{fig:element2}.
The Jacobians of $\Phi$, ${J_\Phi = \d \bfx/\d \bfxi}$ for the 1D and 2D case are given by:
\begin{equation*}
  \begin{array}{lll}
    \text{1D:}\quad &J_\Phi(\xi_1) = \Phi'(\xi_1) = \bfx^2-\bfx^1\\[4mm]
    \text{2D:}\quad &J_\Phi(\bfxi) = \mat{(1-\xi_2)\,(
    \bfx^2-\bfx^1)+\xi_2\,(\bfx^4-\bfx^3) & (1-\xi_1)
    \,(\bfx^3-\bfx^1) +\xi_1\,(\bfx^4-\bfx^2)}\\[4mm]
    \text{3D:}\quad &J_\Phi(\bfxi) = \mat{J_{\Phi,1}(\bfxi) & J_{\Phi,2}(\bfxi) & J_{\Phi,3}(\bfxi)}\\[4mm]
    & J_{\Phi,1}(\bfxi) =
     (1-\xi_2)\,(1-\xi_3)\,(\bfx^2-\bfx^1)
     +\xi_2\,(1-\xi_3)\,(\bfx^4-\bfx^3)
     +(1-\xi_2)\,\xi_3\,(\bfx^6-\bfx^5)
     +\xi_2\,\xi_3\,(\bfx^8-\bfx^7) \\[4mm]
    & J_{\Phi,2}(\bfxi) =
     (1-\xi_1)\,(1-\xi_3)\,(\bfx^3 -\bfx^1)
     + \xi_1\,(1-\xi_3)\,(\bfx^4-\bfx^2)
     + (1-\xi_1)\,\xi_3\,(\bfx^7-\bfx^5)
     + \xi_1\,\xi_3\,(\bfx^8-\bfx^6) \\[4mm]
    & J_{\Phi,3}(\bfxi) =
     (1-\xi_1)\,(1-\xi_2)\,(\bfx^5 -\bfx^1)
     + \xi_1\,(1-\xi_2)\,(\bfx^6-\bfx^2)
     + (1-\xi_1)\,\xi_2\,(\bfx^7-\bfx^3)
     + \xi_1\,\xi_2\,(\bfx^8-\bfx^4)

  \end{array}
\end{equation*}
In order to invert the mappings $\Phi$ from parameter to world space, we proceed:
\begin{equation*}
  \begin{array}{lll}
    \text{1D:}\quad &\bfx^p_i = \bfx_i^1 + \xi_1\,(\bfx^2_i - \bfx^1_i), \quad \forall i \in \{1,2,3\}\\[4mm]
    & \Rightarrow \quad \xi_1 = (\bfx^p_i - \bfx^1_1) / (\bfx^2_i - \bfx^1_i) \quad \forall i \in \{1,2,3\}\\[4mm]
    \text{2D:}\quad &
  \end{array}
\end{equation*}

The point in world space, $\bfx^{p}$ is computed by the sum over shape functions, $\phi^{L}$ and control points of the element, $\bfx^{L}$.
\begin{equation*}
  \begin{array}{lll}
    \bfx^{p}_i(\bfxi) = \Phi_i(\bfxi) = \ds\sum\limits_{L} \phi^{L}_i(\bfxi)\,\bfx^{L} \\[4mm]
    \bfx^{p} = \bfM(\bfxi)\,\bfx,
  \end{array}
\end{equation*}
where $\bfM(\bfxi)$ is a $n \times d$ matrix with $n$ nodes and dimension $d$ and the entries
\begin{equation*}
  \begin{array}{lll}
    \bfM_{iL}(\bfxi) =  \phi^{L}_i(\bfxi)
  \end{array}
\end{equation*}

Starting from \eqref{eq:laplace_discretized} we now plug in the Lagrange ansatz functions for $\phi$. Then the respective functions only have to be integrated over the elements where they are defined.
We get
\begin{equation}\label{eq:laplace_discretized0}
  \begin{array}{ll}
     -\s{e=1}{M} \sum_{i\in N(e)} u_i \ds\int_{\Omega_e} ∇\phi_i(\bfx)\cdot ∇\phi_j(\bfx)\,\d\bfx = 0 \quad \text{for }j=1,\dots,N,
  \end{array}
\end{equation}
where the sum over $i\in N(e)$ is over the nodes of element $e$. The expression $∇\phi(\bfx)$ means, that the gradient is with respect to $\bfx$, despite the function $\phi$ being defined in parameter space, i.e. ${∇\phi(\bfx) = ∇_\bfx \phi(\Phi^{-1}(\bfx)) = ∇_\bfx\phi(\bfxi)}$.

The integration domain, $\Omega_e$, is described by the mapping from parameter space, $\Omega_e = \Phi([0,1]^d)$.
At every point $\bfp = \Phi(\bfxi)$ the gradients are with respect to orthogonal coordinates in the tangent space of the point.
The tangent space coordinates for a particular point given by $\bfxi$ are introduced as $\bfzeta(\bfxi)=(\zeta_1(\bfxi), \dots, \zeta_d(\bfxi))$. The scaling is like in world space, i.e. the following holds:
\begin{equation*}
  \begin{array}{lll}
    \left|\p{\zeta_i}{\xi_i}\right| = \left|\p{\Phi}{\xi_i}\right|.
  \end{array}
\end{equation*}
The integral in \eqref{eq:laplace_discretized0} is then
\begin{equation}\label{eq:m_int}
  \begin{array}{ll}
     \ds\int_{\Omega_e} ∇\phi_i(\bfx)\cdot ∇\phi_j(\bfx)\,\d\bfx =
     \ds\int_{\Phi([0,1]^d)} ∇_{\bfzeta(\bfxi(\bfx))}\phi_i(\bfx)\cdot ∇_{\bfzeta(\bfxi(\bfx))}\phi_j(\bfx)\,\d\bfx.
  \end{array}
\end{equation}
Depending on dimension this is resolved differently.

\textbf{1D case.} With one dimension, we can choose $\zeta_1 = \xi_1 \cdot s$, where $s$ is the scaling factor between the different length scales in world space ($\zeta$) and parameter space ($\xi$). Then with $\d\phi/\d\zeta = \d\phi/\d\xi\cdot \d\xi/\d\zeta$ and $\d\xi/\d\zeta = s^{-1}$ we get
\begin{equation*}
  \begin{array}{lll}
    \ds\int_{\Omega_e} ∇\phi_i(\bfx)\cdot ∇\phi_j(\bfx)\,\d\bfx =
     \ds\int_{\Phi([0,1])} \d{\phi_i(\bfx)}{\xi} \d{\phi_j(\bfx)}{\xi}\Big(\ub{\d{\xi}{\zeta}}{=:s^{-1}}\Big)^2 \,\d\bfx.
  \end{array}
\end{equation*}
The scaling factor can be computed by
\begin{equation*}
  \begin{array}{lll}
    \p{\Phi}{\xi} = \p{\zeta}{\xi} = s \quad \Rightarrow \quad s = \Phi'(\xi_1) = \Vert \bfx^2 - \bfx^1 \Vert_2.
  \end{array}
\end{equation*}
After transformation of the integration domain to parameter space this yields the following formula for the stiffness matrix:
\begin{equation*}
  \begin{array}{lll}
    \ds\int_{\Omega_e} ∇\phi_i(\bfx)\cdot ∇\phi_j(\bfx)\,\d\bfx = \ds\int_{[0,1]} \d{\phi_i(\xi)}{\xi} \d{\phi_j(\xi)}{\xi} s^{-2}\mathcal{J}_1(\xi)\,\d\xi
  \end{array}
\end{equation*}

\textbf{3D case.}
In 3D we use the world coordinate system as $\bfzeta$-frame, i.e. $\bfzeta_1 = \bfe_1, \bfzeta_2 = \bfe_2, \bfzeta_3 = \bfe_3$.
%
We transform the integration domain from global to local coordinate frame using  \eqref{eq:integration_transformation_dd} and get:
\begin{equation}\label{eq:fe_integral}
  \begin{array}{ll}
     -\s{e=1}{M} \sum_{i\in N(e)} u_i \ds\int_{[0,1]^d} ∇_{\bfzeta(\bfxi)}\phi_i(\bfxi)\cdot ∇_{\bfzeta(\bfxi)}\phi_j(\bfxi)\,\mathcal{J}_d(\bfxi)\,\d\bfxi = 0 \quad \text{for }j=1,\dots,N.
  \end{array}
\end{equation}
For a transformation of the gradients to parameter space we need the Jacobian $J_{\Phi}$ of the coordinate mapping, $\bfx =\Phi(\bfxi)$, which consists of the entries
\begin{equation*}
  \begin{array}{lll}
    \big(J_{\Phi}\big)_{i,j} = \d{x_i}{\xi_j}.
  \end{array}
\end{equation*}
Note that the Jacobian of $\Phi: \R^d \to \R^3$ might not be quadratic in general, but a $3 \times d$ matrix with $d \leq 3$. Only in the special 3D-case it is quadratic and can therefore be inverted.

Assuming that $\Phi$ is invertible on $\Omega$ the inverse function theorem states
\begin{equation*}
  \begin{array}{lll}
    J_{\Phi^{-1}} = J_\Phi^{-1}.
  \end{array}
\end{equation*}
%
Executing the chain rule on a derivative in world space, $\d \phi / \d x_k$, yields:
\begin{equation*}
  \begin{array}{lll}
    \d{\phi(\bfxi)}{x_k} = \s{\ell=1}{d}\d{\phi(\bfxi)}{\xi_\ell} \d{\xi_\ell(\bfxi)}{x_k}
  \end{array}
\end{equation*}
and for the whole gradient vector:
\begin{equation*}
  \begin{array}{lll}
    ∇_\bfx\phi(\bfxi) = J^{-\top}_\Phi(\bfxi) ∇_{\bfxi} \phi(\bfxi),
  \end{array}
\end{equation*}
where $J^{-\top}_\Phi = (J^{-1}_\Phi)^\top$.

Now the expression $∇_\bfx\phi_i\cdot ∇_\bfx\phi_j$ can be computed:
\begin{equation*}
  \begin{array}{lll}
    ∇_\bfx\phi_i(\bfx)\cdot ∇_\bfx\phi_j(\bfx)
    &= \s{k=1}{d} \d{\phi_i(\bfxi)}{x_k}\d{\phi_j(\bfxi)}{x_k}
    = \s{k=1}{d} \Big(\s{\ell=1}{d}\d{\phi_i(\bfxi)}{\xi_\ell} \d{\xi_\ell(\bfxi)}{x_k} \s{\ell=1}{d}\d{\phi_j(\bfxi)}{\xi_\ell} \d{\xi_\ell(\bfxi)}{x_k} \Big)\\[8mm]
    &= J^{-\top}_\Phi(\bfxi) ∇_{\bfxi} \phi_i(\bfxi) \cdot
    J^{-\top}_\Phi(\bfxi) ∇_{\bfxi} \phi_j(\bfxi)\\[4mm]
    &= \big(J^{-\top}_\Phi(\bfxi) ∇_{\bfxi} \phi_i(\bfxi)\big)^\top
    J^{-\top}_\Phi(\bfxi) ∇_{\bfxi} \phi_j(\bfxi)\\[4mm]
    &= ∇_{\bfxi} \phi_i(\bfxi)^\top \ub{J^{-1}_\Phi(\bfxi)
    J^{-\top}_\Phi(\bfxi)}{=:T_\Phi(\bfxi)} ∇_{\bfxi} \phi_j(\bfxi).
  \end{array}
\end{equation*}
With the definition of the finite element Laplace operator  transformation matrix $T_\Phi(\bfxi) := J^{-1}_\Phi(\bfxi)
    J^{-\top}_\Phi(\bfxi)$ the transformation becomes:
\begin{equation*}
  \begin{array}{lll}
    ∇_\bfx\phi_i(\bfx)\cdot ∇_\bfx\phi_j(\bfx) = ∇_{\bfxi} \phi_i(\bfxi) \cdot T_\Phi(\bfxi) ∇_{\bfxi} \phi_j(\bfxi).
  \end{array}
\end{equation*}
The inverse transpose $M_\Phi(\bfxi) := T_\Phi(\bfxi)^{-\top} = J_\Phi(\bfxi)^{\top}J_\Phi(\bfxi)$ is called the metric tensor of the mapping $\Phi$.

\textbf{2D case.}
In two dimensions we consider the 2D manifold embedded in $\R^3$ with the mapping $\Phi: [0,1]^2 \to \Omega \subset \R^3$. At a fixed point $\bfp \in \Omega$ with $\Phi(\bfxi_p) = \bfp$ we first determine the tangent vectors $\bfzeta_1(\bfxi_p), \bfzeta_2(\bfxi_z)$. The first tangent vector is defined to lie on the $\xi_1$ coordinate direction, the second vector is then constructed to be orthogonal to the first. To define the tangent vector $\bfzeta_1$ we use a curve in parameter space:
\begin{equation*}
  \begin{array}{lll}
    \gamma(t) = \mat{\gamma_1(t) \\ \gamma_2(t)} = \mat{\xi_{p1}+t \\ \xi_{p2} }.
  \end{array}
\end{equation*}
Then we define
\begin{equation*}
  \begin{array}{lll}
    \bfzeta_1 &= (\Phi \circ \gamma_1)'(0)
    = \p{t}\Phi\big(\gamma(t)\big)|_{t=0} \\[4mm]
    &= \p{\Phi(\gamma(0))}{\xi_1}\ub{\gamma_1'(t)}{=1} + \p{\Phi(\gamma(0))}{\xi_2}\ub{\gamma_2'(t)}{=0}\\[4mm]
    &= \p{\Phi(\xi_p)}{\xi_1}.
  \end{array}
\end{equation*}
Similar we define the helper tangent vector $\bfzeta_h$ along the $\xi_2$ coordinate, which is then
\begin{equation*}
  \begin{array}{lll}
    \bfzeta_h = \p{\Phi(\xi_p)}{\xi_2}.
  \end{array}
\end{equation*}
$\bfzeta_1$ and $\bfzeta_h$ are not orthogonal in general. Therefore a third tangent vector $\bfzeta_2 = \bfzeta_1\times \bfzeta_h \times \bfzeta_1$ will be defined.

It is the tangent vector of a curve
\begin{equation*}
  \begin{array}{lll}
    \delta(t) = \mat{\delta_1(t) \\ \delta_2(t)} = \mat{\xi_{p1}+\cos(α)t \\ \xi_{p2}+\sin(α)t }.
  \end{array}
\end{equation*}
The angle $\beta$ between $\bfzeta_1$ and $\bfzeta_h$ is given by
\begin{equation*}
  \begin{array}{lll}
    \cos(\beta) |\bfzeta_1| |\bfzeta_h| = \bfzeta_1 \cdot \bfzeta_h
    \quad \Rightarrow \quad \beta = \arccos\left(\dfrac{\bfzeta_1 \cdot \bfzeta_h}{|\bfzeta_1| |\bfzeta_h|}\right)
  \end{array}
\end{equation*}
Then by relating angles we get
\begin{equation*}
  \begin{array}{lll}
    \dfrac{\pi/2}{\beta} = \dfrac{α}{\pi/2} \quad \Rightarrow \quad \alpha = \dfrac{\pi^2}{4\,\beta}.
  \end{array}
\end{equation*}
So the tangent vector becomes
\begin{equation}\label{eq:tangent_vector}
  \begin{array}{lll}
    \bfzeta_2 = (\Phi \circ \delta)'(0) = \p{\Phi(\bfxi_p)}{\xi_1}\cos(α) + \p{\Phi(\bfxi_p)}{\xi_2}\sin(α) = \bfzeta_1\,\cos(α) + \bfzeta_h\,\sin(α).
  \end{array}
\end{equation}
Another approach is to use the formula
\begin{equation*}
  \begin{array}{lll}
    \bfzeta_2 = \bfzeta_1\times \bfzeta_h \times \bfzeta_1,
  \end{array}
\end{equation*}
which leads to the expression
\begin{equation*}
  \begin{array}{lll}
    \bfzeta_2 = \mat{
    -\p{\Phi_2}{\xi_1}(\p{\Phi_1}{\xi_1}\p{\Phi_2}{\xi_2} - \p{\Phi_1}{\xi_2}\p{\Phi_2}{\xi_1}) - \p{\Phi_3}{\xi_1}(\p{\Phi_1}{\xi_1}\p{\Phi_3}{\xi_2} - \p{\Phi_1}{\xi_2}\p{\Phi_3}{\xi_1}) \\[4mm]
 \p{\Phi_1}{\xi_1}(\p{\Phi_1}{\xi_1}\p{\Phi_2}{\xi_2} - \p{\Phi_1}{\xi_2}\p{\Phi_2}{\xi_1}) - \p{\Phi_3}{\xi_1}(\p{\Phi_2}{\xi_1}\p{\Phi_3}{\xi_2} - \p{\Phi_2}{\xi_2}\p{\Phi_3}{\xi_1})\\[4mm]
 \p{\Phi_1}{\xi_1}(\p{\Phi_1}{\xi_1}\p{\Phi_3}{\xi_2} - \p{\Phi_1}{\xi_2}\p{\Phi_3}{\xi_1}) + \p{\Phi_2}{\xi_1}(\p{\Phi_2}{\xi_1}\p{\Phi_3}{\xi_2} - \p{\Phi_2}{\xi_2}\p{\Phi_3}{\xi_1})
    }.
  \end{array}
\end{equation*}
The mapping between parameter space and tangent space is given by $\Psi_\bfp$:
\begin{equation*}
  \begin{array}{lll}
    \Psi_\bfp : [0,1]^2 \to T_\bfp\Omega,\\[4mm]
    \Psi_\bfp(\bfxi) = \bfp + c_1(\bfxi)\,\hat{\bfzeta}_1 + c_2(\bfxi)\,\hat{\bfzeta}_2.\\[4mm]
  \end{array}
\end{equation*}
The basis vectors $\hat{\bfzeta}_i$ are the normalized tangent vectors, $\hat{\bfzeta}_i = \bfzeta_i / |\bfzeta_i|$. And the coefficients are:
\begin{equation*}
  \begin{array}{lll}
    c_1(\bfxi) = \big(\xi_1-\xi_{p1}-(\xi_2-\xi_{p2})\cos(α)/\sin(α)\big)\,l_1\\[4mm]
    c_2(\bfxi) = (\xi_2-\xi_{p2})/\sin(α)\,l_2,
  \end{array}
\end{equation*}
with the lengths $l_1 = |\bfzeta_1|, l_2 = |\bfzeta_2|$.

The Jacobian of $\Psi$ and its inverse are as follows:
\begin{equation*}
  \begin{array}{lll}
    J_\Psi = \mat{l_1 & -l_1\,\cos(α)/\sin(α) \\ 0 & l_2/\sin(α)},\qquad
    J_\Psi^{-1} = J_{\Psi^-1} = \mat{1/l_1 & \cos(α)/l_2 \\ 0 & \sin(α)/l_2}.
  \end{array}
\end{equation*}
The inverse contains the entries
\begin{equation*}
  \begin{array}{lll}
    (J^{-1}_{\Psi})_{i,j} = \d{\xi_i}{\zeta_j}.
  \end{array}
\end{equation*}

To compute the gradients of a function with respect to $\zeta$, we use the chain rule:
%
\begin{equation*}
  \begin{array}{lll}
    \d{\phi(\bfxi)}{\zeta_k} = \s{\ell=1}{d}\d{\phi(\bfxi)}{\xi_\ell}\d{\xi_\ell(\bfxi)}{\zeta_k},
  \end{array}
\end{equation*}
so the gradient becomes
\begin{equation*}
  \begin{array}{lll}
    ∇_{\bfzeta} \phi(\bfxi) = J_{\Psi}^{-\top}∇_{\bfxi}\phi(\bfxi),
  \end{array}
\end{equation*}
where $J^{-\top}_{\Psi} = (J_\Psi^{-1})^\top$.

Analogous to the 3D case we define
\begin{equation*}
  \begin{array}{lll}
    T_\Psi(\bfxi) := J_{\Psi}^{-1}(\bfxi)J_\Psi^{-\top}(\bfxi) = \mat{\cos(α)^2/l_2^2 + 1/l_1^2 & \sin(α)\,\cos(α)/l_2^2 \\ \sin(α)\,\cos(α)/l_2^2 & \sin(α)^2/l_2^2}
  \end{array}
\end{equation*}
and get:
\begin{equation*}
  \begin{array}{lll}
    ∇_{\bfzeta}\phi_i(\bfx)\cdot ∇_{\bfzeta}\phi_j(\bfx) = ∇_{\bfxi} \phi_i(\bfxi) \cdot T_\Phi(\bfxi) ∇_{\bfxi} \phi_j(\bfxi).
  \end{array}
\end{equation*}
%Note that the computational domain $\Omega\subset \R^3$ is always considered to be embedded in $\R^3$. The 1D and 2D cases where the mesh is fully contained within a 1D or 2D subspace are then a specialization of the general case. Think of the lower dimensional meshes as a curve ($d=1$) or a bended surface ($d=2$) embedded in $\R^3$.

\subsection{Evaluation of the integral term}\label{chap:integral1}
The integral in \eqref{eq:fe_integral} defines for $i$ and $j$ the entries $m_{ij}$ of the \emph{stiffness matrix} $M$.
The equation can be written in matrix form as
%
\begin{equation*}
  \begin{array}{lll}
    M\,\bfu = \bfzero,
  \end{array}
\end{equation*}
where $M$ contains the entries
\begin{equation*}
  \begin{array}{lll}
    m_{ij} = -\ds\int_{\Omega}∇\phi_i\cdot ∇\phi_j\,\d\bfx = -\s{e=1}{M} \sum_{i\in N(e)} \ds\int_{[0,1]^d} ∇\phi_i(\bfxi)\cdot T_\Phi(\bfxi)∇\phi_j(\bfxi)\,\mathcal{J}_d(\bfxi)\,\d\bfxi
  \end{array}
\end{equation*}
and $\bfu = (u_1, \dots, u_N)^\top$ is the solution vector. Given $M$ the solution $\bfu$ is computed by an appropriate linear system solver.

The integral for $m_{i,j}$ depends via $\mathcal{J}_d$ on the shape of the elements. In general, it has to be evaluated numerically. However, for special simple cases it can be computed analytically.
This includes scenarios in $d=1,2,3$ dimensions where the elements are on a rectilinear cartesian grid.

If the grid is arbitrary, analytical computation for 1D is still simple. For $d=2,3$ it is still possible, but involves more lengthy derivations that are usually performed using a computer algebra system such as \verb|sympy|. In this section the 1D and 2D cases are derived, the python \verb|sympy| code for 2D and 3D is contained in the \verb|doc| directory for further reference.

For all 1D meshes that are embedded in a 3D domain as well as rectangular cartesian 2D and 3D meshes the terms $T_\Phi(\bfxi)$ and $\mathcal{J}_d(\bfxi)$ are constant within each element, i.e. do not depend on $\bfxi$. In that case we can take $\mathcal{J}_d$ out of the integral.

\textbf{1D case.}
We now compute $m_{ij}$ for $d=1$. The transformation term $\mathcal{J}_1(\xi)$
is defined as
%
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_1(\xi) = \Vert \Phi'(\xi)\Vert_2.
  \end{array}
\end{equation*}
Using the parametric representation of $\Phi$ given in \eqref{eq:fe_phi}, we derive
%
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_1(\xi) = \Vert \Phi'(\xi)\Vert_2 = \Vert \bfx^2 - \bfx^1 \Vert_2,
  \end{array}
\end{equation*}
which is the length of the element $e$ with nodes $\bfx^1$ and $\bfx^2$. We define it to be $l_e := \Vert\bfx^2-\bfx^1\Vert_2$, and thus have $\mathcal{J}_1(\xi) = l_e$. The scaling factor is then $s = l_e$ and the total prefactor of the integral becomes
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_1\,s^{-2} = 1/l_e.
  \end{array}
\end{equation*}

%We use the derivatives of the Lagrange functions,
%\begin{equation}
%  \begin{array}{ll}
%    L_{1,1}'(\xi) = -1, \quad L_{2,1}'(\xi) = 1.\\[4mm]
%  \end{array}
%\end{equation}

\textbf{2D case.}
For 2D we assume a rectangular element that lies in a $z=\text{constant}$ plane with side lengths $l_{1,e}$ and $l_{2,e}$ in $\xi_1$ and $\xi_2$ directions.
The mapping from $\bfxi=(\xi_1,\xi_2)$ to $\bfx$ coordinate frame is given by
\begin{equation}
  \begin{array}{ll}
    \Phi(\bfxi) = \bfx^1 + \mat{\xi_1\,l_{1,e} \\[2mm] \xi_2\,l_{2,e}}.
  \end{array}
\end{equation}
Then we derive
\begin{equation*}
  \begin{array}{lll}
    J_{\Phi}(\bfxi) = \mat{l_{1,e}  & 0 \\[2mm] 0 & l_{2,e}}
  \end{array}
\end{equation*}
and
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_2 = \sqrt{\det \big(J_{\Phi}(\bfxi)^\top J_{\Phi}(\bfxi)\big)} = |l_{1,e}\,l_{2,e}|.
  \end{array}
\end{equation*}
The tangent vectors $\bfzeta_1$ and $\bfzeta_h$ are computed to be
\begin{equation*}
  \begin{array}{lll}
    \bfzeta_1 = \p{\Phi(\bfxi_p)}{\xi_1} = (l_{1,e},0)^\top,\quad
    \bfzeta_h = \p{\Phi(\bfxi_p)}{\xi_2} = (0, l_{2,e})^\top,
     \quad l_1 = |\bfzeta_1| = l_{1,e}.
  \end{array}
\end{equation*}
The angle in between is
\begin{equation*}
  \begin{array}{lll}
    \beta = \arccos\left(\dfrac{\bfzeta_1\cdot \bfzeta_h}{|\bfzeta_1||\bfzeta_h|}\right) = \dfrac{\pi}{2}.
  \end{array}
\end{equation*}
The angle in parameter space is then also $\alpha = \pi^2 / (4\beta) = \pi/2$.
Using \eqref{eq:tangent_vector} the orthogonal tangent vector $\bfzeta_2$ becomes $\bfzeta_2 = \bfzeta_h$. Then $l_2 = |\bfzeta_h| = l_{2,e}$.

This leads to a transformation matrix
\begin{equation*}
  \begin{array}{lll}
    T_\Psi(\bfxi) = \mat{\cos(α)^2/l_2^2 + 1/l_1^2 & \sin(α)\,\cos(α)/l_2^2 \\[2mm] \sin(α)\,\cos(α)/l_2^2 & \sin(α)^2/l_2^2} = \mat{1/l_{1,e}^2 & 0 \\[2mm] 0 & 1/l_{2,e}^2}.
  \end{array}
\end{equation*}
When the mesh resolution is uniform, i.e. $l_{1,e} = l_{2,e} = l_e$, the transformation matrix becomes a scaled identity matrix, $T_{\Psi}(\bfxi) = l_e^{-2}\,\bfI$ and the transformation can be moved out of the integral. The prefactor is then
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_2\,l_e^{-2} = 1.
  \end{array}
\end{equation*}

\textbf{3D case.}
Similar to the 2D case when a rectangular 3D grid with grid widths $l_{1,e}, l_{2,e},l_{3,e}$ is assumed, the transformation factor becomes
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_3 = |l_{1,e}\,l_{2,e}\,l_{3,e}|.
  \end{array}
\end{equation*}
With the world mapping $\Phi(\bfxi)$ and its Jacobian $J_{\Phi}(\bfxi)$ given by
\begin{equation*}
  \begin{array}{lll}
    \Phi(\bfxi) = \bfx^1 + \mat{\xi_1\,l_{1,e}\\[2mm]
    \xi_2\,l_{2,e}\\[2mm]
    \xi_3\,l_{3,e}}
  \end{array}, \quad
  J_{\Phi}(\bfxi) = \mat{l_{1,e}&0&0 \\[2mm] 0 & l_{2,e} & 0 \\[2mm] 0 & 0 & l_{3,e}},
\end{equation*}
the transformation matrix becomes
\begin{equation*}
  \begin{array}{lll}
    T_\Phi(\bfxi) := J^{-1}_\Phi(\bfxi)
    J^{-\top}_\Phi(\bfxi) = \mat{l_{1,e}^{-2}&0&0 \\[2mm] 0 & l_{2,e}^{-2} & 0 \\[2mm] 0 & 0 & l_{3,e}^{-2}}.
  \end{array}
\end{equation*}
With uniform grid lengths, $l_{1,e}=l_{2,e}=l_{3,e}=l_e$, we get $T_\Phi(\bfxi) = l_e^{-2}\,\bfI$. The prefactor of the integral yields
\begin{equation*}
  \begin{array}{lll}
    \mathcal{J}_3\,l_e^{-2} = l_e.
  \end{array}
\end{equation*}

In the presented special cases with uniform grid resolution $\mathcal{J}_d$ did not depend on the integration domain, which allows to compute the factor separately:
\begin{equation*}
  \begin{array}{lll}
    \ds\int_{\Omega} ∇\phi_i \cdot T ∇\phi_j\,\mathcal{J}_d\,\d\bfxi = \mathcal{J}_d\,l_e^{-2}\ds\int_{\Omega} ∇\phi_i \cdot ∇\phi_j\,\d\bfxi.
  \end{array}
\end{equation*}

Now the term $-\int ∇\phi_i\cdot ∇\phi_j \,\d\xi$, remains to be computed. We compute values at the nodes and visualize them using \emph{stencil notation}. For a fixed node $i$ we compute the respective values for adjacent nodes $j$. The result for $i=j$ is underlined in the stencil, the values for adjacent nodes are placed left, right, top and bottom, in the position of the respective nodes.

We first compute element-wise stencils that state the contribution of a single element. If all elements have the same length properties, the element contributions can be summed up to get the total value at the nodes which is shown in the nodal stencils. From these stencils we can easily set up the stiffness matrix for a non-varying, equidistant mesh.

\begin{table}[h]
\centering
\begin{tabular}{l|l|l|l}
    dim & element contribution & node stencil\\
    \hline
    1D: &
\begin{minipage}{6cm}
  \begin{equation*}
     \left[\begin{array}{ccc}
        \underline{-1} & 1\\
    \end{array}\right] \quad
  \end{equation*}
\end{minipage}
    &
\begin{minipage}{6cm}
  \begin{equation*}
    \left[\begin{array}{ccc}
        1 & \underline{-2} & 1\\
    \end{array}\right]
  \end{equation*}
\end{minipage}
     \\[4mm]
     \hline
    2D:&
\begin{minipage}{6cm}
  \begin{equation*}
    \left[
      \begin{array}{ccc}
        1/6 & 1/3 \\
        \underline{-2/3} & 1/6
      \end{array}
    \right]
  \end{equation*}
\end{minipage}  &
\begin{minipage}{6cm}
  \begin{equation*}
      \dfrac13\left[
        \begin{array}{ccc}
          1 & 1 & 1\\
          1 & \underline{-8} & 1 \\
          1 & 1 & 1
        \end{array}
      \right]
  \end{equation*}
\end{minipage}  \\[4mm]
    \hline
    3D: &
\begin{minipage}{6cm}
  \begin{equation*}
    \begin{array}{ll}
      \text{center:} &
      \left[\begin{array}{ccc}
          0 & 1/12\\
          \underline{-1/3} & 0\\
      \end{array}\right] \\[4mm]
      \text{top:}&
      \left[\begin{array}{ccc}
          1/12 & 1/12\\
             0 & 1/12\\
      \end{array}\right]
    \end{array}
  \end{equation*}
\end{minipage} &
\begin{minipage}{6cm}
  \begin{equation*}
    \begin{array}{ll}
      \text{bottom:} &
      \dfrac1{12}
      \left[\begin{array}{ccc}
          1 & 2 & 1\\
          2 & 0 & 2\\
          1 & 2 & 1
      \end{array}\right] \\[4mm]
      \text{center:} &
      \dfrac1{12}
      \left[\begin{array}{ccc}
          2 & 0 & 2\\
          0 & \underline{-32} & 0\\
          2 & 0 & 2
      \end{array}\right] \\[4mm]
      \text{top:}&
      \dfrac1{12}
      \left[\begin{array}{ccc}
          1 & 2 & 1\\
          2 & 0 & 2\\
          1 & 2 & 1
      \end{array}\right]
    \end{array}
  \end{equation*}
\end{minipage}
\end{tabular}
\caption{Stencils of the Finite Element stiffness matrix of $-Δu$ for an equidistant mesh with uniform resolution. Note, that for 1D, 2D and 3D elements of mesh width $h$, the prefactors of the integral over the element domain are $1/h$, 1 and $h$.}
\end{table}

\subsection{Triangle ansatz functions in 2D}

In the following, the FEM formulation for quadratic triangles in 2D are derived.
\begin{figure}
  \centering
  \begin{subfigure}{0.4\textwidth}
    \def\svgwidth{5cm}
    \input{images/triangle_ansatz_linear.pdf_tex}
    \caption{Nodes of a triangle for linear ansatz functions}
  \end{subfigure}
  \quad
  \begin{subfigure}{0.4\textwidth}
    \def\svgwidth{5cm}
    \input{images/triangle_ansatz.pdf_tex}
    \caption{Nodes of a triangle for quadratic ansatz functions}
  \end{subfigure}
  \label{fig:triangle_ansatz}
\end{figure}
%
\Cref{fig:triangle_ansatz} shows a triangle for quadratic ansatz functions. The mapping from the parameter space with $\xi_1, \xi_2$ to the actual space is given by the following definition of $\bfx = \Phi(\bfxi)$:
\begin{equation*}
  \begin{array}{lll}
    \Phi(\bfxi) = \sum\limits_{i\in\{0,1,2,3,6,8\}} \phi_i(\bfx)\,\bfx^i\\[4mm]
    \phi_0(\bfx) = (\xi_1 + \xi_2 - 1)\,(2\xi_1 + 2\xi_2 - 1)   \\[4mm]  %  1.0*(1.0*xi1 + 1.0*xi2 - 1.0)*(2.0*xi1 + 2.0*xi2 - 1.0)
    \phi_1(\bfx) = 4\,\xi_1\,(1 - \xi_1 - \xi_2)                \\[4mm]  % -4.0*xi1*(xi1 + xi2 - 1)
    \phi_2(\bfx) = \xi_1\,(2\xi_1 - 1)                          \\[4mm]  % 1.0*xi1*(2.0*xi1 - 1.0)
    \phi_3(\bfx) = 4\,\xi_2\,(1 - \xi_1 - \xi_2)                \\[4mm]  % -4.0*xi2*(xi1 + xi2 - 1)
    \phi_6(\bfx) = \xi_2\,(2\xi_2 - 1)                          \\[4mm]  % 1.0*xi2*(2.0*xi2 - 1.0)
    \phi_8(\bfx) = 4\,\xi_1\,\xi_2                              \\[4mm]  % 4.0*xi1*xi2
  \end{array}
\end{equation*}
The Jacobian of this mapping is computed as:
\begin{equation*}
  \begin{array}{lll}
    J_\Phi(\bfxi) = \mat{J_{1} & J_{2}}\quad \text{with}\\[4mm]
    J_{1} = (4\,\xi_1 + 4\,\xi_2 - 3)\,\bfx^0 - (8\,\xi_1 + 4\,\xi_2 - 4)\,\bfx^1 + (4\,\xi_1 - 1)\,\bfx^2 - 4\,\xi_2\,\bfx^3 + 4\,\xi_2\,\bfx^5,\\[4mm]
    J_{2} = (4\,\xi_1 + 4\,\xi_2 - 3)\,\bfx^0 - 4\,\xi_1\,\bfx^1 - (4\,\xi_1 + 8\,\xi_2 - 4)\,\bfx^3 + (4\,\xi_2 - 1)\,\bfx^4 + 4\,\xi_1\,\bfx^5.
  \end{array}
\end{equation*}
Integration of the element stiffness and mass matrices according to \eqref{eq:m_int}:
\begin{equation*}
  \begin{array}{lll}
    k_{ij}^\text{el} = \ds\int_{\Omega_e} ∇\phi_i(\bfx)\cdot ∇\phi_j(\bfx)\,\d\bfx =
     \ds\int\limits_{\xi_2=0}^1 \int\limits_{\xi_1=0}^{1-\xi_2} ∇_\bfx \phi_i(\bfxi)\cdot ∇_\bfx \phi_j(\bfxi)\,\mathcal{J}_d\,\d\xi_1\,\d\xi_2, \quad \text{with}\\[4mm]
    ∇_\bfx \phi(\bfxi) = J_\Phi^{-\top}(\bfxi)\,∇_{\bfxi}\, \phi_i(\bfxi)\\[4mm]
    m_{ij}^\text{el} = \ds\int_{\Omega_e} \phi_i(\bfx)\, \phi_j(\bfx)\,\d\bfx =
    \ds\int\limits_{\xi_2=0}^1 \int\limits_{\xi_1=0}^{1-\xi_2} \phi_i(\bfxi)\,\phi_j(\bfxi)\,\mathcal{J}_d\,\d\xi_1\,\d\xi_2
  \end{array}
\end{equation*}
For the quadrature see \cite{zienkiewicz2005finite}.

Analogous for linear ansatz functions:
\begin{equation*}
  \begin{array}{lll}
    \Phi(\bfxi) = \sum\limits_{i\in\{0,1,2\}} \phi_i(\bfx)\,\bfx^i\\[4mm]
    \phi_0(\bfx) = (1-\xi_1-\xi_2)   \\[4mm]
    \phi_1(\bfx) = \xi_1 \\[4mm]
    \phi_2(\bfx) = \xi_2
  \end{array}
\end{equation*}
The Jacobian is given by
\begin{equation*}
  \begin{array}{lll}
    J_\Phi(\bfxi) = \mat{J_{1} & J_{2}}\quad \text{with}\\[4mm]
    J_{1} = -\bfx^0 + \bfx^1,\\[4mm]
    J_{2} = -\bfx^0 + \bfx^2
  \end{array}
\end{equation*}

\begin{figure}
  \centering
  \begin{subfigure}{0.45\textwidth}
    \def\svgwidth{\textwidth}
    \input{images/triangle_ansatz_linear4.pdf_tex}
    \caption{Nodes of a triangle for linear ansatz functions}
  \end{subfigure}
  \quad
  \begin{subfigure}{0.45\textwidth}
    \def\svgwidth{\textwidth}
    \input{images/triangle_ansatz4.pdf_tex}
    \caption{Nodes of a triangle for quadratic ansatz functions}
  \end{subfigure}
  \label{fig:triangle_ansatz}
  \caption{The red circle is the origin of the coordinate frame for every triangle.}
\end{figure}
%
\subsection{Boundary Conditions}
\label{sec:bc}
The Neumann-type boundary condition
%
\begin{equation*}
  \begin{array}{lll}
    ∇u(\bfx)\cdot \bfn = 0 \qquad \text{on }\Gamma_N
  \end{array}
\end{equation*}
%
is satisfied automatically by the Galerkin finite element formulation. Starting from the left hand side of \eqref{eq:laplace_weak} and using Divergence theorem we get:
%
\begin{equation*}
  \begin{array}{lll}
    -\i{\Omega}{} ∇u\cdot ∇\phi \,\d \bfx = -\i{∂\Omega}{} \phi\,\big(∇u\cdot \bfn\big) \,\d \bfx + \i{\Omega}{} Δu\,\phi  \,\d \bfx = 0 \qquad ∀ \phi \in H^1_0(\Omega)
  \end{array}
\end{equation*}
%
Because $Δu = 0$ on $\Omega$ we get $∇u\cdot \bfn=0$ on the boundary.

\subsubsection{Dirichlet boundary conditions in strong form}
Dirichlet boundary conditions can be easily considered at the discretized system. For each condition $u_i = u_{0,i}$ that enforces the degree of freedom $i\in I_\text{BC}$ to have the value $u_{0,i}$ we modify the linear system of equations. In the  right hand side vector we subtract from the value $f_{j}$ the product of $a_{ji}$ and the given value $u_{0,i}$ for every $j\neq i$, i.e. the new value is $\hat{f_j} = f_j - a_{ji}\,u_{0,i}$. We set $f_i = u_{0,i}$. In the matrix we zero the row and column that contain the entry $a_{ii}$, i.e. $a_{ij} = a_{ji} = 0, ∀j\neq i$ and set $a_{ii}=1$. As an example, consider the system
%
\begin{equation*}
  \begin{array}{lll}
    \mat{m_{11} & m_{12} & m_{13} \\ m_{21} & m_{22} & m_{23} \\ m_{31} & m_{32} & m_{33}}
    \mat{u_1 \\ u_2 \\ u_3} = \mat{0 \\ 0 \\ 0}
  \end{array}
\end{equation*}
with the Dirichlet boundary condition $u_3 = u_{0,3}$. The modified system is then given by
%
\begin{equation*}
  \begin{array}{lll}
    \mat{m_{11} & m_{12} & 0 \\ m_{21} & m_{22} & 0 \\ 0 & 0 & 1}
    \mat{u_1 \\ u_2 \\ u_3} = \mat{-m_{13}\,u_{0,3} \\ -m_{23}\,u_{0,3} \\ u_{0,3}}.
  \end{array}
\end{equation*}
%
\subsubsection{Dirichlet boundary conditions in weak form}
%
We modify the system \eqref{eq:laplace_discretized} and add the weak form of the Dirichlet boundary conditions:
%
\begin{equation*}
  \begin{array}{lll}
    -\s{i=1}{N} u_i \ds\int_{\Omega}∇\phi_i\cdot ∇\phi_j\,\d\bfx = -\s{i\in I_\text{BC}}{}u_{0,i}\ds\int_{\Omega}∇\phi_i\cdot ∇\phi_j\,\d\bfx \quad \text{for }j=1,\dots,N.
  \end{array}
\end{equation*}

\subsection{Function spaces}
\label{sec:hilbert}
%
For the weak solutions $u$ of the problems we do not need to request $\CC^2(\Omega)$, since only the first derivatives are needed and only in a weak sense. Therefore $u\in H^1_0(\Omega)$ suffices.

The Hilbert space $H^1(\Omega)$ is the Sobolev space $\W^{1,2}(\Omega)$ which is defined using weak derivatives. The concept of weak derivatives generalizes the classical derivatives.

Let $u,v\in \Lloc(\Omega)$ and $\alpha \in \N^d_0$ a multi-index. Then $v$ is called \emph{weak derivative} of $u$ of order $\alpha$ if
\begin{equation}
  \begin{array}{ll}
    \i{\Omega}{}u(\bfx) \D^\alpha \phi(\bfx) \,\d \bfx = (-1)^{|\alpha|} \i{\Omega}{} v(\bfx)\,\phi(\bfx)\,\d \bfx
  \end{array}
\end{equation}
for all $\phi \in \CC^\infty_0(\Omega)$. We then write $\D^\alpha u = v$. The derivative with the multi-index, $\D^\alpha$ is given by
\begin{equation}
  \begin{array}{ll}
    \D^\alpha = \dfrac{\p^{|\alpha|}}{\p^{\alpha_1}_{x_1} \cdots \p^{\alpha_d}_{x_d}}
  \end{array}
\end{equation}

If $u$ is differentable in a classical sense, the classical derivatives are also the weak derivatives.

Now we define the \emph{Sobolev} space $\W^{1,2}(\Omega)$ (1=first order weak derivatives, 2=derivatives in $\L^2(\Omega)$)) as follows:
\begin{equation}
  \begin{array}{ll}
    \W^{1,2}(\Omega) := \{ u \in \Lloc(\Omega) \mid |\alpha| \in \N^d_0, |\alpha| \leq 1, \D^\alpha u \text{ exists}, \D^\alpha u \in \L^2(\Omega)\}.
  \end{array}
\end{equation}
With an appropriate Sobolev norm, $\W^{1,2}$ is a Banach space, i.e. complete (Cauchy series converge in it).

Together with the scalar product
\begin{equation}
  \begin{array}{ll}
    (u,v)_{H^1} := \sum\limits_{|\alpha|\leq 1} \i{\Omega}{}{\D^\alpha u(\bfx) \,\D^\alpha v(\bfx) \,\d\bfx}
  \end{array}
\end{equation}
we get the Hilbert space $H^1(\Omega) := \W^{1,2}(\Omega)$.

With $H^1_0(\Omega) := \{u \in H^1(\Omega) \mid u(\bfx) = 0 \text{ for } \bfx \in \p \Omega\}$ we denote the subspace of functions that are 0 on the boundary.

%-------------------------------------------------------------------------------------------------

\section{Poisson Equation}
The Poisson equation is a generalization of the Laplace equation and is given by
%
\begin{equation*}
  \begin{array}{lll}
    Δu = f\qquad \text{on }\Omega.
  \end{array}
\end{equation*}
%
It can be subject to the same boundary conditions as Laplace equation, i.e. Neumann-type boundary conditions
%
\begin{equation*}
  \begin{array}{lll}
    ∇u(\bfx) \cdot \bfn = 0 \qquad \text{on }\Gamma_N,
  \end{array}
\end{equation*}
%
as well as Dirichlet-type boundary conditions
%
\begin{equation*}
  \begin{array}{lll}
    u(\bfx) = u_0(\bfx) \qquad \text{on }\Gamma_D.
  \end{array}
\end{equation*}
The finite element formulation proceeds similar to Chap.~\ref{chap:laplace}, multiplication of a testfunction $\phi \in H^{1}_0(\Omega)$ and integration yields
\begin{equation*}
  \begin{array}{lll}
    \ds\int_{\Omega}Δu\,\phi\,\d \bfx = \int_{\Omega} f\,\phi\,\d \bfx, \quad \forall \phi \in H^1_0(\Omega).
  \end{array}
\end{equation*}
Applying divergence theorem we get
\begin{equation}\label{eq:poisson_divergence}
  \begin{array}{lll}
    -\ds\int_{\Omega} ∇u\cdot ∇\phi \,\d \bfx = \int_{\Omega} f\,\phi\,\d \bfx \quad \forall \phi \in H^{1}_0(\Omega).
  \end{array}
\end{equation}
Like the solution $u(\bfx)$ also the right hand side $f(\bfx)$ has to be spatially discretized by a linear combination of coefficients and basis functions:
%
\begin{equation*}
  \begin{array}{lll}
    u_h(\bfx) = \s{i=1}{N}u_i\,\phi_i(\bfx),\\[4mm]
    f_h(\bfx) = \s{i=1}{N}f_i\,\phi_i(\bfx).
  \end{array}
\end{equation*}
By again choosing the space of testfunctions to be the same as the span of basis functions, ${V=\spn\{\phi_1, \dots, \phi_n\}}$ we get the Galerkin formulation as
\begin{equation*}
  \begin{array}{lll}
    -\s{i=1}{N} u_i \int_\Omega ∇\phi_i\cdot ∇\phi_j \,\d\bfx = \s{i=1}{N}f_i \int_\Omega \phi_i\cdot \phi_j \,\d\bfx \quad \text{for }j = 1, \dots, N.
  \end{array}
\end{equation*}
The domain $\Omega$ is again decomposed into disjoint elements $\Omega_e, e=1,\dots, M$ and integration has only be performed over the elements where none of the basis function vanish.

The first integral term, $\int_{\Omega} ∇\phi_i\cdot ∇\phi_j\,\d\bfx$, has to be computed as described in Section \ref{chap:integral1}. How to compute the second integral term, $\int_{\Omega} \phi_i\cdot \phi_j\,\d\bfx$ is shown in the following.

Similar as before, the integration domain is transferred from element space to parameter space. For this a transformation factor $\mathcal{J}_d$ has to be considered, which is constant for some special cases as discussed in \cref{chap:integral1}.

For the remaining integral, $\int_{\Omega} \phi_i\cdot \phi_j\,\d\bfxi$ node stencils are provided in the following table.

\begin{table}[h]
\centering
\begin{tabular}{l|l|l|l}
    dim & element contribution & node stencil\\
    \hline
    1D: &
\begin{minipage}{6cm}
  \begin{equation*}
     \dfrac16\left[\begin{array}{ccc}
        \underline{2} & 1\\
    \end{array}\right] \quad
  \end{equation*}
\end{minipage}
    &
\begin{minipage}{6cm}
  \begin{equation*}
    \dfrac16\left[\begin{array}{ccc}
        1 & \underline{4} & 1\\
    \end{array}\right]
  \end{equation*}
\end{minipage}
     \\[4mm]
     \hline
    2D:&
\begin{minipage}{6cm}
  \begin{equation*}
    \dfrac1{36}\left[\begin{array}{ccc}
        2 & 1 \\
        \underline{4} & 2
      \end{array}
    \right]
  \end{equation*}
\end{minipage}  &
\begin{minipage}{6cm}
  \begin{equation*}
      \dfrac1{36}\left[
        \begin{array}{ccc}
          1 & 4 & 1\\
          4 & \underline{16} & 4 \\
          1 & 4 & 1
        \end{array}
      \right]
  \end{equation*}
\end{minipage}  \\[4mm]
    \hline
    3D: &
\begin{minipage}{6cm}
  \begin{equation*}
    \begin{array}{ll}
      \text{center:} &
      \dfrac1{216}\left[\begin{array}{ccc}
          4 & 2\\
          \underline{8} & 4\\
      \end{array}\right] \\[4mm]
      \text{top:}&
      \dfrac1{216}\left[\begin{array}{ccc}
          2 & 1\\
          4 & 2\\
      \end{array}\right]
    \end{array}
  \end{equation*}
\end{minipage} &
\begin{minipage}{6cm}
  \begin{equation*}
    \begin{array}{ll}
      \text{bottom:} &
      \dfrac1{216}\left[\begin{array}{ccc}
          1 & 4 & 1\\
          4 & 16 & 4\\
          1 & 4 & 1
      \end{array}\right] \\[4mm]
      \text{center:} &
      \dfrac1{216}
      \left[\begin{array}{ccc}
          4 & 16 & 4\\
          16 & \underline{64} & 16\\
          4 & 16 & 4
      \end{array}\right] \\[4mm]
      \text{top:}&
      \dfrac1{216}
      \left[\begin{array}{ccc}
          1 & 4 & 1\\
          4 & 16 & 4\\
          1 & 4 & 1
      \end{array}\right]
    \end{array}
  \end{equation*}
\end{minipage}
\end{tabular}
\caption{Stencils of the Finite Element right hand side for an equidistant mesh with uniform resolution. Note, that for 1D, 2D and 3D elements of mesh width $h$, the prefactors of the integral over the element domain are $h$, $h^2$ and $h^3$.}
\end{table}

\section{Generalized Laplace operator}
%
The Laplace equation $Δu=0$ describes steady-state matter-/heat-/current flow where the computed quantity $u$ designates the potential that induces the flow $\bfF$, which can be modelled as being directed against the potential gradient, $\bfF = -∇u$. In terms of heat transfer this is \emph{Fick's law}. By considering the conservation law of the flowing quantity we assume $∇\cdot \bfF = 0$ which leads directly to the Laplace equation.

If the medium in which the flow occurs is non-isotropic this can be modelled by adjusting the relationship between the negative potential gradient, $-∇u$, and the induced flow direction, $F$. By applying a linear map $A$ onto the negative gradient vector which can be thought of being the sum of contributions in coordinate directions, each contribution vector of a coordinate direction gets scaled and projected to a new direction. The flow is then $\bfF = -A∇u$ and the resulting equation is called generalized Laplace equation, reading
\begin{equation*}
  \begin{array}{lll}
    ∇\cdot (A ∇u) = 0.
  \end{array}
\end{equation*}
The parantheses can also be neglected. $∇\cdot A∇$ is referred to as generalized Laplace operator.

The derivation of the Finite Element formulation proceeds analoguos to \cref{chap:laplace}.
Multiplication with a testfunction $\phi$ yields:
\begin{equation}
  \begin{array}{ll}
    \ds\int_{\Omega}∇\cdot A ∇u\,\phi\,\d \bfx = 0 \quad \forall \phi\in H^1_0(\Omega)
  \end{array}
\end{equation}
Applying divergence theorem \eqref{eq:gauss1} with $f=\phi$ and $\bfF=A ∇u$ yields
\begin{equation}
  \begin{array}{ll}
    -\ds\int_{\Omega}A ∇u \cdot ∇\phi \,\d \bfx + \ds\int_{\p \Omega} (\phi\,A∇u)\cdot\bfn\,\d \bfx  = 0 \quad \forall \phi\in H^1_0(\Omega).
  \end{array}
\end{equation}
Because $\phi$ is zero on the boundary, the boundary integral vanishes:
\begin{equation}\label{eq:laplace_weak}
  \begin{array}{ll}
    -\ds\int_{\Omega}A ∇u \cdot ∇\phi \,\d \bfx = 0 \quad \forall \phi\in H^1_0(\Omega).
  \end{array}
\end{equation}
The discretization remains the same and leads to an integral term of
%
\begin{equation*}
  \begin{array}{lll}
    m_{ji} = -\ds\int_{Ω}A∇\phi_i\cdot ∇\phi_j\,\d\bfx.
  \end{array}
\end{equation*}
for the stiffness matrix $M$. For these term the stencil notation cannot be applied in general. The stiffness matrix has to be computed using e.g. numerical quadrature.

%-------------------------------------------------------------------------------------------------

\section{Diffusion Equation}

The diffusion equation is given by
\begin{equation}\label{eq:diffusion}
  \begin{array}{lll}
    u_t = c\,Δu, \qquad c \in \R
  \end{array}
\end{equation}
For the finite element formulation it can be seen as a Poisson equation with right hand side $u_t$, neglecting the constant $c$ for now.
We get from \eqref{eq:poisson_divergence}:
\begin{equation}\label{eq:diffusion_derivation1}
  \begin{array}{ll}
    -\ds\int_{\Omega} ∇u\cdot ∇\phi \,\d \bfx = \int_{\Omega} u_t\,\phi\,\d \bfx \quad \forall \phi \in H^{1}_0(\Omega).
  \end{array}
\end{equation}
The derivative in time, $u_t$ can be discretized by a differential quotient as follows:
\begin{equation*}
  \begin{array}{lll}
    u_t = \dfrac{u^{(t+1)} - u^{(t)}}{dt},
  \end{array}
\end{equation*}
where $dt$ is the time step width. In space we formulate the quantity again with a discrete basis:
\begin{equation*}
  \begin{array}{lll}
    u_h^{(t)}(\bfx) = \s{i=1}{N} u_i^{(t)}\,\phi_i(\bfx).
  \end{array}
\end{equation*}
Substituting into \eqref{eq:diffusion_derivation1} yields:
\begin{equation*}
  \begin{array}{lll}
    & -\ds\int_{\Omega} ∇u^{(t)}\cdot ∇\phi \,\d \bfx = \dfrac{1}{dt}\int_{\Omega} \Big(u^{(t+1)} - u^{(t)}\Big))\,\phi\,\d \bfx \quad \forall \phi \in H^{1}_0(\Omega)\\[4mm]
    \Leftrightarrow\quad & -\s{i=1}{N} u^{(t)}_i \ds\int_Ω ∇\phi_i \cdot ∇\phi_j\,\d\bfx = \dfrac{1}{dt}\s{i=1}{N}(u_i^{(t+1)} - u_i^{(t)})\ds\int_Ω \phi_i \cdot \phi_j\,\d\bfx\qquad \text{for }j = 1,\dots, N.
  \end{array}
\end{equation*}
We use the matrix notation with the stiffness matrix, $\bfK$, and the mass matrix, $\bfM$. For the explicit Euler scheme $u^{(t+1)} = u^{(t)} + dt\,f(t,u^{(t)})$, we get
\begin{equation*}
  \begin{array}{lll}
    \bfK\bfu^{(t)} &= \dfrac1{dt} \bfM\Big(\bfu^{(t+1)} - \bfu^{(t)}\Big)\\[4mm]
    \bfu^{(t+1)} &= \bfu^{(t)} + dt \,\bfM^{-1}\bfK\bfu^{(t)}\\[4mm]
                 &= \Big(\bfI + dt \,\bfM^{-1}\bfK\Big)\bfu^{(t)}
                .
  \end{array}
\end{equation*}
For an implicit Euler scheme $u^{(t+1)} = u^{(t)} + dt\,f(t+1,u^{(t+1)})$, we get
\begin{equation*}
  \begin{array}{lll}
    \bfK\bfu^{(t+1)} &= \dfrac1{dt} \bfM\Big(\bfu^{(t+1)} - \bfu^{(t)}\Big)\\[4mm]
     (I - dt\,\bfM^{-1}\bfK)\,\bfu^{(t+1)}&= \,\bfu^{(t)}\\[1em] \textrm{or}\\[1em]
     \displaystyle(\bfK-\frac{\bfM}{dt})\bfu^{(t+1)}&=\,\displaystyle -\frac{\bfM}{dt}\bfu^{(t)}
  \end{array}
\end{equation*}
The second form may be beneficial in case of variable time step.\\[1em]
For the Crank-Nicolson scheme $u^{(t+1)} = u^{(t)} + dt\,(f(t+1,u^{(t+1)})+f(t,u^{(t)}))/2$, we get
\begin{equation*}
	\begin{array}{lll}
		\dfrac{\bfK(\bfu^{(t+1)}+\bfu^{(t)})}{2} &= \dfrac1{dt} \bfM\Big(\bfu^{(t+1)} - \bfu^{(t)}\Big)\\[4mm]
		(I - \dfrac{dt}{2}\,\bfM^{-1}\bfK)\,\bfu^{(t+1)}&= \,\bfu^{(t)}(I + \dfrac{dt}{2}\,\bfM^{-1}\bfK)
	\end{array}
\end{equation*}
or
\begin{equation*}
  \begin{array}{lll}
    \big(\dfrac1{2}\bfK-\dfrac{1}{dt}\bfM\big) \bfu^{(t+1)} = \big(-\dfrac12{\bfK} - \dfrac{1}{dt}\bfM\big) \bfu^{(t)}
  \end{array}
\end{equation*}
\subsection{Lumped Mass Matrix}
For linear or bilinear basis functions
\begin{equation*}
\left\{
  \begin{array}{ll}
    m'_{i,j}=0 &i\ne j\\
    m'_{i,i}=S(i) &i=1,...,n
  \end{array},
\right.
\end{equation*}
where $S(i)=\sum_{j=1}^{n}m_{i,j}$ for $i=1,...,n$.\\[0.5em]
For orders of basis greater than two suggested from Hinton et al. \cite{hinton_rock_zienkiewicz_1976}:
\begin{equation}
\left\{
  \begin{array}{ll}
	m'_{i,j}=0 &i\ne j\\
	m'_{i,i}=\frac{S}{D} m_{i,i} & i=1,...,n
  \end{array}
\right.
\end{equation}
where $S=\sum_{i=1}^{n}\sum_{j=1}^{n} m_{i,j}$ and $D=\sum_{i=1}^{n}m_{i,i}$.

%------------------------------------------------------------------------------------------------
\subsection{Derivation with boundary conditions}
\subsubsection{Diffusion problem}
In general, the weak form of a diffusion problem discretized with Crank-Nicolson,
\begin{equation}\label{eq:weak_form0}
  \begin{array}{lll}
    ∇\cdot (\bfsigma ∇u) = u_t, \qquad \p{u}{\bfn} = f \quad \text{on }\Gamma_f, \qquad \p{u}{\bfn} = 0 \quad \text{on } ∂Ω\backslash \Gamma_f \\[4mm]
    \Rightarrow\quad \ds\int_Ω \big(\theta\,∇\cdot (\bfsigma ∇u^{(i+1)}) + (1-\theta)\,∇\cdot (\bfsigma ∇u^{(i)})\big)\,\phi \,\d\bfx = \dfrac{1}{dt} \ds\int_Ω(u^{(i+1)} - u^{(i)})\,\phi\,\d\bfx, \quad \forall \phi \in V_h\\[4mm]
  \end{array}
\end{equation}
Discretize in space with $u = \sum_j u_j\,\varphi_j, V_h = \spn\{\varphi_j | j = 1\dots N\}$ and using Divergence theorem
\begin{equation}\label{eq:weak_form1}
  \begin{array}{lll}
    \ds\sum\limits_{j=1}^{N} \big(\theta\,u_j^{(i+1)} + (1-\theta)\,u_j^{(i)}\big)  \left(-\ds\int_Ω (\bfsigma∇\varphi_j)\cdot ∇\phi_k \,\d\bfx + \ds\int_{∂Ω} (\bfsigma\,∇\varphi_j\cdot \bfn)\phi_k \,\d\bfx  \right) \\[4mm]
    \quad = \dfrac{1}{dt} \sum\limits_{j=1}^{N} \big(u_j^{(i+1)} - u_j^{(i)}\big) \ds\int_Ω \varphi_j\,\phi_k\,\d\bfx, \quad \forall k = 1\dots N.\\[4mm]
  \end{array}
\end{equation}
The ansatz functions for discretization and the test functions are chosen to be equal, $\phi_j=\varphi_j$.

The formulation can be written in matrix notation as
\begin{equation}\label{eq:diffusion_weak_form_matrix}
  \begin{array}{lll}
    \bfA\,\bfu^{(i+1)} = \bfb(\bfu^{(i)}),
  \end{array}
\end{equation}
where
\begin{equation}\label{eq:diffusion_weak_form_matrix2}
  \begin{array}{lll}
    \bfA = \theta\,(\bfK + \bfB) -\dfrac{1}{dt}\bfM, \\[4mm]
    \bfb = \big((\theta-1)\,(\bfK + \bfB) - \dfrac{1}{dt} \bfM \big)\,\bfu^{(i)},
  \end{array}
\end{equation}
with
\begin{equation*}
  \begin{array}{lll}
     \bfK_{kj} = -\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx \qquad \text{(note, the minus sign is correct for $+Δ$)},\\[4mm]
     \bfB_{kj} = \ds\int_{\Gamma_f} (\bfsigma\,∇\varphi_j\cdot \bfn)\varphi_k \,\d\bfx,\\[4mm]
     \bfM_{kj} = \ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx,
  \end{array}
\end{equation*}
or written in component form:
\begin{equation*}
  \begin{array}{lll}
    \bfA_{kj} = \theta\,\left(-\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx + \ds\int_{∂Ω} (\bfsigma\,∇\varphi_j\cdot \bfn)\varphi_k \,\d\bfx \right) - \dfrac{1}{dt}\,\ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx,\\[4mm]
    \bfb_k = \ds\sum\limits_{j=1}^{N} -(1-\theta)\,u_j^{(i)}\,\left(-\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx + \ds\int_{∂Ω} (\bfsigma\,∇\varphi_j\cdot \bfn)\varphi_k \,\d\bfx \right)
     + \dfrac{1}{dt} \ds\sum\limits_{j=1}^{N} (-u_j^{(i)}) \ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx.
  \end{array}
\end{equation*}
So far we did not plug in the boundary conditions. For $f=0$ we get $\bfB = \bfzero$. The case $f \neq 0$ is handled in the next subsection.

\subsubsection{Boundary conditions}
The boundary condition $\bfsigma\,∇u\cdot\bfn = f$ can be written as
\begin{equation*}
  \begin{array}{lll}
    \bfsigma\,∇u\cdot\bfn = \s{j=1}{N}u_j\,(\bfsigma\,∇\varphi_j\cdot \bfn) = f,
  \end{array}
\end{equation*}
We discretize the flow over the boundary, $f$, by different ansatz functions, $\psi_j$, with coefficients $f_j$:
\begin{equation*}
  \begin{array}{lll}
    f = \s{j=1}{N}f_j\,\psi_j
  \end{array}
\end{equation*}
We get from \eqref{eq:weak_form1}
\begin{equation*}
  \begin{array}{lll}
    \ds\sum\limits_{j=1}^{N} \big(\theta\,u_j^{(i+1)} + (1-\theta)\,u_j^{(i)}\big)
     \left(-\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx \right)
      + \ds\int_{\Gamma_f} \big(\theta\,f^{(i+1)} + (1-\theta)\,f^{(i)}\big)\,\varphi_k \,\d\bfx \\[4mm]
    \quad = \dfrac{1}{dt} \sum\limits_{j=1}^{N} \big(u_j^{(i+1)} - u_j^{(i)}\big) \ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx, \quad \forall k = 1\dots N,\\[4mm]
    \Leftrightarrow \quad
    \ds\sum\limits_{j=1}^{N} \big(\theta\,u_j^{(i+1)} + (1-\theta)\,u_j^{(i)}\big)
     \left(-\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx \right)
      + \ds\sum\limits_{j=1}^{N} \big(\theta\,f_j^{(i+1)} + (1-\theta)\,f_j^{(i)}\big)
      \ds\int_{\Gamma_f} \psi_j\,\varphi_k \,\d\bfx \\[4mm]
    \quad = \dfrac{1}{dt} \sum\limits_{j=1}^{N} \big(u_j^{(i+1)} - u_j^{(i)}\big) \ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx, \quad \forall k = 1\dots N,\\[4mm]
  \end{array}
\end{equation*}
In matrix notation,
\begin{equation}\label{eq:diffusion_weak_form_matrix}
  \begin{array}{lll}
    \bfA\,\bfu^{(i+1)} = \bfb(\bfu^{(i)}),
  \end{array}
\end{equation}
we have
\begin{equation}\label{eq:diffusion_weak_form_matrix2}
  \begin{array}{lll}
    \bfA = \theta\,\bfK -\dfrac{1}{dt}\bfM, \\[4mm]
    \bfb = \big((\theta-1)\,\bfK - \dfrac{1}{dt} \bfM \big)\,\bfu^{(i)} - \bfB_{\Gamma_f}\,\big(\theta\,\bff^{(i+1)} + (1-\theta)\,\bff^{(i)}\big),
  \end{array}
\end{equation}
with
\begin{equation*}
  \begin{array}{lll}
     \bfK_{kj} = -\ds\int_Ω \bfsigma\,∇\varphi_j\cdot ∇\varphi_k \,\d\bfx \qquad \text{(note, the minus sign is correct for $+Δ$)},\\[4mm]
     \bfM_{kj} = \ds\int_Ω \varphi_j\,\varphi_k\,\d\bfx,\\[4mm]
     \bfB_{\Gamma_f,kj} = \ds\int_{\Gamma_f} \psi_j\,\varphi_k \,\d\bfx,
  \end{array}
\end{equation*}

\subsubsection{Laplace problem}
We consider $∇\cdot (\bfsigma ∇\bfu) = 0$ with Neumann boundary condition, $ \partial (\bfsigma\,\bfu)/\partial \bfn = \bff$.
This leads to
\begin{equation*}
  \begin{array}{lll}
    (\bfK + \bfB)\,\bfu = \bfzero \qquad \text{or} \qquad \bfK\,\bfu + \bfB_{\Gamma_f}\,\bff = 0.
  \end{array}
\end{equation*}

\subsubsection{Two coupled Laplace problems}
We have the domain as in \cref{fig:two-domains} and the following equations:
\bild{two-domains}{5cm}{Two domains}
\begin{equation*}
  \begin{array}{lll}
    ∇\cdot (\bfsigma_1 ∇u_1) = \bff_1, \quad \text{on } \Omega_1 \qquad \partial u_1/\partial \bfn = g_1 \quad \text{on } Γ_{f1}\\[4mm]
    ∇\cdot (\bfsigma_2 ∇u_2) = \bff_2, \quad \text{on } \Omega_2 \qquad \partial u_2/\partial \bfn = g_2 \quad \text{on } Γ_{f2}\\[4mm]
    u_1 = u_2, \quad q := (\bfsigma_1 ∇u_1)\cdot\bfn = (\bfsigma_1 ∇u_2)\cdot\bfn \quad \text{on }Γ_{12}
  \end{array}
\end{equation*}
The flux over $Γ_{12}$ is
\begin{equation*}
  \begin{array}{lll}
    q(\bfx) := (\bfsigma_1∇u_1(\bfx))\cdot \bfn = \ds\s{i=1}{N}u_i\, (\bfsigma_1 ∇\varphi_i(\bfx))\cdot \bfn \quad \text{for } \bfx \in Γ_{12}.
  \end{array}
\end{equation*}
This is only defined for quadratic ansatz functions. The flux condition is therefore forced in a weak form.

The Finite Element formulation leads to
\begin{equation*}
  \begin{array}{lll}
    \bfK_{\bfsigma_1}\,\bfu_1 = \bff_1 - \bfB_{\Gamma_{f1}}\,\bfg_1 - \bfB_{\Gamma_{12}}\,\bfq_1,\\[4mm]
    \bfK_{\bfsigma_2}\,\bfu_2 = \bff_2 - \bfB_{\Gamma_{f2}}\,\bfg_2 - \bfB_{\Gamma_{12}}\,\bfq_2,\\[4mm]
    \bfB_{\Gamma_{12}}\,\bfq_1 = -\bfB_{\Gamma_{12}}\,\bfq_2
  \end{array}
\end{equation*}
where
\begin{equation*}
  \begin{array}{lll}
     \bfK_{\bfsigma,kj} = -\ds\int_Ω (\bfsigma ∇\varphi_j)\cdot ∇\varphi_k \,\d\bfx \qquad \text{(note, the minus sign is correct for $+Δ$)},\\[4mm]
     \bfB_{\Gamma_f,kj} = \ds\int_{\Gamma_f} \psi_j\,\varphi_k \,\d\bfx.
  \end{array}
\end{equation*}

Combining the two equations we get the system
%
\begin{equation*}
  \begin{array}{lll}
    \left[\begin{array}{@{}cc@{}}
     \bfK_{\bfsigma_{1}}&\\[2mm]
     &\bfK_{\bfsigma_{2}}
    \end{array}\right]
    \left[\begin{array}{@{}c@{}}
      \bfu_{1}\\[2mm]
      \bfu_{2}
    \end{array}\right]
    =
    \left[\begin{array}{@{}c@{}}
     \bff_{1} - \bfB_{Γ_{f1}}\,\bfg_{1} - \bfB_{\Gamma_{12}}\,\bfq_1\\[2mm]
     \bff_{2} - \bfB_{Γ_{f2}}\,\bfg_{2} - \bfB_{\Gamma_{12}}\,\bfq_2
    \end{array}\right]
  \end{array}
\end{equation*}
%
Here, $\bfu_1$  and $\bfu_2$ are the degrees of freedom in $Ω_{1} ∪ Γ_{12}$ and $Ω_2 ∪ Γ_{12}$. The degrees of freedom on $Γ_{12}$ appear in $\bfu_1$ as well as in $\bfu_2$.
$\bfK_{\bfsigma_{1}}$ and $\bfK_{\bfsigma_{2}}$ are computed by integrating only over the domains $Ω_{1}$ and $Ω_{2}$, respectively.
of freedom in $Ω_1\backslash Γ_{12}$ and $Ω_2\backslash Γ_{12}$, $\bfK_{\sigma_{12}} = \bfK_{\sigma_1} + \bfK_{\sigma_2}$ \text{ on } $Γ_{12}$.
%
Static condensation of $q_1 = -q_2$:

\begin{equation*}
  \begin{array}{lll}
   \bfB_{\Gamma_{12}}\,\bfq_2 = \bff_{2} - \bfB_{Γ_{f2}}\,\bfg_{2} - \bfK_{\bfsigma_{2}}\,\bfu_{2}\\[4mm]
    \Rightarrow\quad \bfK_{\bfsigma_{1}}\,\bfu_{1} = \bff_{1} - \bfB_{Γ_{f1}}\,\bfg_{1} + \bff_{2} - \bfB_{Γ_{f2}}\,\bfg_{2} - \bfK_{\bfsigma_{2}}\,\bfu_{2}\\[4mm]
    \Rightarrow\quad \bfK_{\bfsigma_{1}}\,\bfu_{1} + \bfK_{\bfsigma_{2}}\,\bfu_{2} = \bff_{1} - \bfB_{Γ_{f1}}\,\bfg_{1} + \bff_{2} - \bfB_{Γ_{f2}}\,\bfg_{2}\\[4mm]

  \end{array}
\end{equation*}
%
\begin{equation*}
  \begin{array}{lll}
    \left[\begin{array}{@{}cc@{}}
     \bfK_{\bfsigma_{1}}& \bfK_{\bfsigma_{2}}
    \end{array}\right]
    \left[\begin{array}{@{}c@{}}
      \bfu_{1}\\[2mm]
      \bfu_{2}
    \end{array}\right]
    =
    \left[\begin{array}{@{}c@{}}
     \bff_{1} + \bff_{2} - \bfB_{Γ_{f1}}\,\bfg_{1} - \bfB_{Γ_{f2}}\,\bfg_{2}
    \end{array}\right]
  \end{array}
\end{equation*}
This means the systems is solved normally as if there was no separation in $\Omega_1, \Omega_2$.
%------------------------------------------------------------------------------------------------

\section{Models of Electrophysiology}
In the following the Bidomain and Monodomain models are derived.
\subsection{The Bidomain Model}

The Bidomain model considers two computational domains of intra-cellular and extra-cellular space. In a homogenised view it is assumed that these domains share space such that at each spatial point there coexist both domains at the same time. Both domains have their own conductivity tensors $\bfsigma_i, \bfsigma_e$ and electric potential $\phi_i, \phi_e$, as depicted in \cref{fig:bidomain}. An electric current $I_m$ between the computational domains is possible and has to pass the cell membrane. For specifying boundary conditions of the Bidomain equations, a third domain, the surrounding body, is assumed, that receives a current from the extracellular domain, but not from the intracellular space. For Monodomain equation this current is later set to 0.

\bild{bidomain}{10cm}{Setting of the Bidomain model}

Starting point of the derivation is a form of Ohm's Law by which the current density $J$ is a result of an electric field $E$ that exists in a medium with electric conductivity $\bfsigma$:
\beq
  \ba{ll}
    J = \bfsigma\,E.
  \ea
\eeq
Assuming quasi static conditions, the field strength $E$ is given by the negative gradient of a potential field:
\beq
  \ba{ll}
    E = -∇\phi.
  \ea
\eeq
For the current densities $J_i$ and $J_e$ in the two domains we then have:
\beq
  \ba{ll}
    J_i = -\bfsigma_i\,∇\phi_i,\qquad J_e = -\bfsigma_e\,∇\phi_e.
  \ea
\eeq
Because of the spatial coexistence of the two domains the divergence of the current density in one domain has to be the negative of the divergence at the same point in the other domain,
\beqno\label{eq:pre2}
  \ba{ll}
    ∇\cdot J_i = -∇\cdot J_e \quad \Leftrightarrow \quad -∇\cdot(\bfsigma_i\,∇\phi_i) = ∇\cdot (\bfsigma_e\,∇\phi_e).
  \ea
\eeqno
The current enabling this relationship is the membrane current $I_m$ such that:
\beqno\label{eq:AmIm}
  \ba{ll}
    ∇\cdot(\bfsigma_i\,∇\phi_i) = A_m\,I_m.
  \ea
\eeqno
The factor $A_m$ characterizes the membrane area to domain volume relationship of the cell membrane. Then $I_m$ is a quantity per area. It is the external current in the Hodgkin-Huxley model, \cref{eq:V_m}, such that \cref{eq:AmIm} becomes:
\beq
  \ba{ll}
    ∇\cdot(\bfsigma_i\,∇\phi_i) = A_m\,\big(C_m\,\p{V_m}{t} + I_{ion}(V_m)\big).
  \ea
\eeq

Using $V_m = \phi_i - \phi_e$, the intracellular potential $\phi_i$ can be eliminated yielding
\beqno\label{eq:bidomain1}
  \ba{ll}
    ∇\cdot(\bfsigma_i\,∇V_m) + ∇\cdot (\bfsigma_i\,∇\phi_e) = A_m\,\big(C_m\p{V_m}{t} + I_{ion}(V_m)\big).
  \ea
\eeqno
The second equation comes from \cref{eq:pre2} which is also formulated with $V_m$ instead of $\phi_i$:
\beqno\label{eq:bidomain2}
  \ba{ll}
    &∇\cdot\big(\bfsigma_i\,∇(V_m + \phi_e)\big) = -∇\cdot(\bfsigma_e\,∇\phi_e)\\[4mm]
    \Leftrightarrow \quad & ∇\cdot\big((\bfsigma_i + \bfsigma_e)\,∇\phi_e) + ∇\cdot (\bfsigma_i\,∇V_m) = 0,
  \ea
\eeqno
\Cref{eq:bidomain1,eq:bidomain2} form the Bidomain Equations.

\subsection{The Monodomain Model}

Under assumption that the anisotropy of the tissue at a point inside and outside the muscle cell is equal, the two Bidomain Equations can be reduced to the single Monodomain Equation. The assumption can be expressed as
\beq
  \ba{ll}
    \bfsigma_i = k\cdot \bfsigma_e
  \ea
\eeq
for a real factor $k$. Plugging $\bfsigma_e = \bfsigma_i/k$ into the second Bidomain Eq. \eqref{eq:bidomain2} yields
\beqno\label{eq:int1}
  \ba{ll}
    &∇\cdot\big((1+\dfrac{1}{k})\,\bfsigma_i\,∇\phi_e\big) + ∇\cdot (\bfsigma_i\,∇V_m) = 0\\[4mm]
    \Leftrightarrow \quad & ∇\cdot(\bfsigma_i\,∇\phi_e) = -\dfrac{k}{k+1}\,∇\cdot(\bfsigma_i\,∇V_m).
  \ea
\eeqno
Now \cref{eq:int1} can be combined with the first Bidomain Eq. \eqref{eq:bidomain1} to get:
\beq
  \ba{ll}
    ∇\cdot(\bfsigma_i\,∇V_m) - \dfrac{k}{k+1}\,∇\cdot(\bfsigma_i\,∇V_m) = A_m\,\big(C_m\p{V_m}{t} + I_{ion}(V_m)\big)\\[4mm]
    \dfrac{1}{k+1}\,∇\cdot(\bfsigma_i\,∇V_m) = A_m\,\big(C_m\p{V_m}{t} + I_{ion}(V_m)\big)
  \ea
\eeq
For the 1D case where the muscle fibre is modeled as a single line this can be simplified to:
\beq
  \ba{ll}
    ∇\cdot\Big(\ub{\dfrac{\bfsigma_i}{\bfsigma_i/\bfsigma_e+1}}{=\dfrac{\bfsigma_e\,\bfsigma_i}{\bfsigma_e+\bfsigma_i}}∇V_m\Big) = A_m\,\big(C_m\p{V_m}{t} + I_{ion}(V_m)\big)
  \ea
\eeq
By defining the effective conductivity ${\bfsigma_{\text{eff}} := (\bfsigma_e\,\bfsigma_i)/(\bfsigma_e+\bfsigma_i)}$ the 1D Monodomain Equation can be written as:
\beqno\label{eq:monodomain_1D}
  \ba{ll}
    ∇\cdot(\bfsigma_{\text{eff}}\,∇V_m) = A_m\,\big(C_m\p{V_m}{t} + I_{ion}(V_m)\big).
  \ea
\eeqno
This is a 1D PDE in the variable $V_m$ and is usually solved with the boundary condition
\beq
  \ba{ll}
    (\bfsigma_{\text{eff}}∇V_m)\cdot \bfn^M = 0 \quad \text{on }\Gamma^M.
  \ea
\eeq
which inhibits current to the outer domain. $\bfn^M$ is the outward normal vector on the border $\Gamma^M$ of the muscle fibre surface.
The spatial discretization of the diffusion term on the left-hand side of \cref{eq:monodomain_1D} can be achieved by using 1D finite elements.

%------------------------------------------------------------------------------------------------
\subsection{The 1D Monodomain Equation}

We consider 2 domains: intra- and extracellular space. The setting is homogenised such that the domains occupy the same space.
The domains have electric potentials $\phi_i, \phi_e$ and conductivities $\sigma_i, \sigma_e$, the membrane voltage is defined as $V_m = \phi_i-\phi_e$.
%
\begin{figure}
  \def\svgwidth{6cm}
  \input{images/bidomain_setting1.pdf_tex}\quad
\end{figure}
The current density can be described by a potential:
%
\begin{equation*}
\begin{array}{lll}
j_e = -\sigma_e\,\dfrac{\partial\phi_e}{\partial x},\quad j_i = -\sigma_i\,\dfrac{\partial\phi_i}{\partial x}
\end{array}
\end{equation*}

\begin{figure}
  \def\svgwidth{6cm}
  \input{images/bidomain_setting2.pdf_tex}\quad
\end{figure}
Conservation of charges holds, changes in current density affect the other domain.
%
\begin{equation}\label{eq:bidom1}
  \begin{array}{lll}
    &\dfrac{\partial}{\partial x} j_i = -\dfrac{\partial}{\partial x} j_e\\[4mm]
    \quad\Leftrightarrow\quad & \dfrac{\partial}{\partial x} \Big(\sigma_i\,\dfrac{\partial\phi_i}{\partial x}\Big)
      = -\dfrac{\partial}{\partial x} \Big(\sigma_e\,\dfrac{\partial\phi_e}{\partial x}\Big)
  \end{array}
\end{equation}
The current through the domain is given by
\begin{equation}\label{eq:bidom2}
  \begin{array}{lll}
      \dfrac{\partial}{\partial x} \Big(\sigma_i\,\dfrac{\partial\phi_i}{\partial x}\Big)  &= I_m\\[4mm]
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)
  \end{array}
\end{equation}

We assume $\sigma_i = k\cdot \sigma_e$ and substitute $V_m = \phi_i - \phi_e$ to eliminate $\phi_i$:
\begin{equation}\label{eq:subsbidom}
  \begin{array}{lll}
    \dfrac{\partial}{\partial x} \Big(\sigma_i\,\dfrac{\partial\phi_i}{\partial x}\Big) =
    \dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial(V_m + \phi_e)}{\partial x}\Big)
  \end{array}
\end{equation}

Plugging this into \eqref{eq:bidom1} yields
\begin{equation}\label{eq:subs2bidom}
  \begin{array}{lll}
    &\dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial(V_m + \phi_e)}{\partial x}\Big)
      = -\dfrac{\partial}{\partial x} \Big(\sigma_e\,\dfrac{\partial\phi_e}{\partial x}\Big)    \\[4mm]
    \quad\Leftrightarrow\quad &
    \dfrac{\partial}{\partial x} \Big((k+1)\cdot \sigma_e\,\dfrac{\partial\phi_e}{\partial x}\Big) = -\dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial V_m}{\partial x}\Big) \\[4mm]
    \quad\Leftrightarrow\quad &
    \dfrac{\partial}{\partial x} \Big(\sigma_e\,\dfrac{\partial\phi_e}{\partial x}\Big) = -\dfrac{\partial}{\partial x} \Big(\dfrac{k}{k+1}\cdot \sigma_e\,\dfrac{\partial V_m}{\partial x}\Big)\\[4mm]
  \quad\Leftrightarrow\quad &
    \dfrac{\partial}{\partial x} \Big(k\,\sigma_e\,\dfrac{\partial\phi_e}{\partial x}\Big) = -\dfrac{\partial}{\partial x} \Big(\dfrac{k}{k+1}\cdot k\, \sigma_e\,\dfrac{\partial V_m}{\partial x}\Big)
  \end{array}
\end{equation}

Using the substitution \eqref{eq:subsbidom} in \eqref{eq:bidom2} yields
\begin{equation*}
  \begin{array}{lll}
      \dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial(V_m + \phi_e)}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)
  \end{array}
\end{equation*}
Starting from \eqref{eq:bidom1} with \eqref{eq:subs2bidom} we get
\begin{equation*}
  \begin{array}{lll}
      &\dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial(V_m + \phi_e)}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
      \quad\Leftrightarrow\quad &
       \dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial V_m}{\partial x}\Big)
      + \dfrac{\partial}{\partial x} \Big(k\cdot \sigma_e\,\dfrac{\partial \phi_e}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
      \quad\Leftrightarrow\quad &
       \dfrac{\partial}{\partial x} \Big(\big(1 - \dfrac{k}{k+1}\big)\cdot k\, \sigma_e\,\dfrac{\partial V_m}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
      \quad\Leftrightarrow\quad &
       \dfrac{\partial}{\partial x} \Big(\dfrac{1}{k+1}\cdot \sigma_i\,\dfrac{\partial V_m}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
    \quad\Leftrightarrow\quad &
       \dfrac{\partial}{\partial x} \Big(\dfrac{1}{\sigma_i/\sigma_e+1}\cdot \sigma_i\,\dfrac{\partial V_m}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
    \quad\Leftrightarrow\quad &
       \dfrac{\partial}{\partial x} \Big(\dfrac{\sigma_i\,\sigma_e}{\sigma_i+\sigma_e}\,\dfrac{\partial V_m}{\partial x}\Big)
      &= A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big)\\[4mm]
  \end{array}
\end{equation*}

This leads to the Monodomain equation:
\begin{equation*}
  \begin{array}{lll}
  \dfrac{\partial}{\partial x} \Big(\sigma_\text{eff}\,\dfrac{\partial V_m}{\partial x}\Big) = A_m\Big(C_m \dfrac{\partial V_m}{\partial t} + I_\text{ion}(V_m)\Big),\qquad
  \text{with }\sigma_\text{eff}:= \sigma_i || \sigma_e = \dfrac{\sigma_i\,\sigma_e}{\sigma_i+\sigma_e}
  \end{array}
\end{equation*}
\subsection{Numerical treatment}
Equation solved for $\dfrac{\partial V_m}{\partial t}$:
\begin{equation*}
  \begin{array}{lll}
  \dfrac{\partial V_m}{\partial t} = -\dfrac{1}{C_m}I_\text{ion}(V_m) + \dfrac{1}{A_m\,C_m} \dfrac{\partial}{\partial x}\Big(\sigma_\text{eff}\,\dfrac{\partial}{\partial x}V_m\Big)
  \end{array}
\end{equation*}
Employ finite differences in $t$ and Godunov operator splitting:
\begin{equation*}
  \begin{array}{lll}
  \dfrac{V_m^\ast - V_m^{(k)}}{\Delta t} &= -\dfrac{1}{C_m}\,I_\text{ion}(V_m^{(k)}),\\[4mm]
  \dfrac{V_m^{(k+1)} - V_m^{*}}{\Delta t} &= \dfrac{1}{A_m\,C_m}\dfrac{\partial}{\partial x}\Big(\sigma_\text{eff}\,\dfrac{\partial}{\partial x}V_m^{(k+1)}\Big),\\[4mm]
  \end{array}
\end{equation*}

\begin{equation*}
  \begin{array}{lll}
  V_m^{(k+1)} = V_m^{*} + \Delta t \cdot \dfrac{1}{A_m\,C_m}\dfrac{\partial}{\partial x}\Big(\sigma_\text{eff}\,\dfrac{\partial}{\partial x}V_m^{(k+1)}\Big),
  \end{array}
\end{equation*}
The CellML model gives the right hand side $f(V_m^{(k)}) = -\dfrac{1}{C_m}\,I_\text{ion}(V_m^{(k)})$. When solving the diffusion equation the value of $C_m$ should match.
%------------------------------------------------------------------------------------------------

The bidomain equation can be used for computation of EMG ($\phi_e$), after the Heidlauf model, when only $V_m$ is known.
\begin{equation*}
  \begin{array}{lll}
    \div\big((\bfsigma_i + \bfsigma_e)\,\grad \phi_e\big) = - \div(\bfsigma_i\,\grad V_m)
  \end{array}
\end{equation*}

%------------------------------------------------------------------------------------------------
\section{Solid Mechanics Model of Muscle Contraction}\label{sec:model_muscle_contraction}

Muscle contraction is described on the organ level by a description of solid mechanics. Because of possibly large strains, a nonlinear hyperelastic formulation is used. For mathematical foundations in continuum mechanics, we refer to basic literature such as the books of Holzapfel \cite{holzapfel2000nonlinear} and Marsden and Hughes \cite{marsden1994mathematical}, as well as literature on the application of the Finite Element Method in continuum mechanics \cite{zienkiewicz1977finite,SUSSMAN1987357,zienkiewicz2005finite}.

% assumptions
We consider the 3D muscle domain $\Omega_0=\Omega_M \subset \R^3$ in reference configuration at time $t=0$, that deforms into a current configuration $\Omega_t$ at time $t$. The material points are given by $\bfX \in \Omega_0$. The corresponding points $\bfx \in \Omega_t$ in the current configuration are defined by the placement function $\bfx = \bfvarphi_t(\bfX)$. In the following, capital letters refer to quantities in material or Lagrangian description, i.e., defined in the reference configuration and small letters refer to quantities in spatial or Eulerian description, i.e., defined in the current configuration.

The relation of point coordinates in the current configuration with respect to the reference configuration can also be described by the displacements field $\bfu$:
%
\begin{align*}
  \bfx(\bfX) = \bfX + \bfu(\bfX).
\end{align*}
The current velocity $\bfv$ is the time derivative of the displacements, $\bfv := \dot{\bfu}$.

The foundation of continuum mechanics usually builds on three balance principles: conservation of mass, of momentum and of angular momentum. In the following, these principles are presented in their Eulerian forms.

First, we assume \emph{conservation of mass} in terms of the densities $\rho_0(\bfX)$ and $\rho(\bfx)$ in reference and current configurations:
%
\begin{align*}
  \ds\int\limits_{V_0} \rho_0\,\d V = \int\limits_{V_t} \rho \,\d v.
\end{align*}
%
The equation holds for all corresponding subdomains $V_0\subset \Omega_0$ and $V_t \subset \Omega_t$. With the intermediate step of deducing $\d/\d t \int_{\Omega_t} \rho \,\d v=0$, we get the following differential equation:%
\begin{align}\label{eq:contraction_helper1}
  \dot{\rho}(\bfv,t) + \rho(\bfx,t)\,\div\big(\bfv(\bfx,t)\big) = 0.
\end{align}
%

As muscle tissue largely consists of water, it is typically assumed to be an incompressible domain. This is equivalent to a constant density, $\dot{\rho}=0$, and, thus, \cref{eq:contraction_helper1} reduces to
%
\begin{align}\label{eq:assumption_1_local}
  \div(\bfv(\bfx,t)) = 0.
\end{align}
%

The second assumption is the \emph{balance of momentum}, which is expressed as %
\begin{align*}
  \d{t} \ds\int\limits_{V_t} \rho\,\bfv\, \d v = \ds\int\limits_{V_t} \rho\,\bfb \,\d v + \ds\int\limits_{∂V_t} \bft \,\d a.
\end{align*}
Here, $\bfb$ describes a body force and $\bft$ describes a traction force that acts on the surface of the current configuration. The corresponding differential form is given by the following differential equation:%
\begin{align}\label{eq:assumption_2_local}
  \rho\,\dot{\bfv}(\bfx,t) = \rho\,\bfb(\bfx,t) + \div\bfsigma(\bfx,t).
\end{align}
%
The second order Cauchy stress tensor $\bfsigma$ has units of force per area and is defined by the relation $\bft = \bfsigma \bfn$ between a traction force $\bft$ in a virtual cut out of the body at $\bfx$ and the normal vector $\bfn$ of the cut area.

The third assumption is the \emph{balance of angular momentum} and can be formulated using the 3D cross-product:%
\begin{align*}
  \d{t} \ds\int\limits_{V_t} \bfx \times (\rho\,\bfv)\, \d v = \ds\int\limits_{V_t} \bfx \times (\rho\,\bfb) \,\d v + \ds\int\limits_{∂V_t} \bfx \times \bft\,\d a.
\end{align*}
%
This can be shown to be equivalent to the symmetry of the Cauchy stress tensor, $\bfsigma = \bfsigma^\top$.

A further assumption in the multi-scale muscle framework is to only consider isothermal conditions.
An activated muscle performs work and energy is added to the system by metabolism. Further, the muscle is not thermodynamically isolated. The system is not closed regarding conversion and transfer of energy and, thus, the balance of energy cannot be modeled easily.

The mathematical description has to be closed by defining a constitutive relation between stresses and strains. The usual approach for hyperelastic materials is to define a strain energy function $\Psi$. This scalar function is formulated in terms of the right Cauchy-Green tensor $\bfC$, which is related to the strain of the deformed body. Then, the stresses are given by%
\begin{align*}
  \bfS = 2\p{\Psi(\bfC)}{\bfC},
\end{align*}
where $\bfS$ is the second Piola-Kirchhoff stress tensor, which in the incompressible case is the pull-back of the Cauchy stress $\bfsigma$ used in the balance of momentum in \cref{eq:assumption_2_local}.
%which is related to the Cauchy stress $\bfsigma$ used in the balance of momentum in \cref{eq:assumption_2_local}.

%It is usually formulated in terms of the five principle strain invariants $I_1$ to $I_5$. A derivative of $\Psi$ is used to derive a term for the stress. More details follow in \cref{sec:discretization_mechanics}.

In the muscle contraction model of \cite{Heidlauf2013}, the strain energy function is additively composed of two passive terms, one isotropic, one anisotropic, and one additional active term:
\begin{align*}
  \Psi(\bfC) = \Psi_\text{isotropic}(I_1,I_2) + \Psi_\text{anisotropic}(\lambda_f) + \Psi_\text{active}(\gamma).
\end{align*}
The isotropic term $\Psi_\text{isotropic}$ is formulated in terms of the strain invariants $I_1=\tr(\bfC)$ and $I_2=\big(\tr(\bfC)^2 - \tr(\bfC^2)\big)/2$. The anisotropic term $\Psi_\text{anisotropic}$ depends on the fiber stretch $\lambda_f$. The active term $\Psi_\text{active}$ yields the active stress that results from muscular activation, which is described by the activation parameter $\gamma$.
%Note the missing dependency on the third invariant $I_3$, which is constant because of the enforced incompressibility.

The passive behavior of muscle tissue is modeled by a transversely isotropic Mooney-Rivlin material.
The isotropic part is given by the Mooney-Rivlin formulation:%
\begin{align}\label{eq:mooney_rivlin}
  \Psi_\text{isotropic}(I_1,I_2) = c_1\,(I_1 - 3) + c_2\,(I_2-3).
\end{align}
The values of the two material parameters $c_1$ and $c_2$ can be determined by compression tests and are summarized in the work of \cite{Heidlauf2013}.

The anisotropic behavior depends only on the fiber stretch $\lambda_f$. The formulation in \cite{Heidlauf2013} uses two material parameters $b$ and $d$ and the following function:
\begin{align*}
  \Psi_\text{anisotropic}(\lambda_f) = \dfrac{b}{d}(\lambda_f^d - 1) - b\,\log(\lambda_f).
\end{align*}
%

The active contribution is directly formulated in terms of the second Piola-Kirchhoff stress $\bfS$. The relation between the active stress $\bfS_\text{active}$ and the active contribution $\Psi_\text{active}$ of the strain energy function as well as the definition of $\bfS_\text{active}$ is given as follows:
\begin{align}\label{eq:active_stress_term}
  \bfS_\text{active} = \dfrac{1}{\lambda_f}\p{\Psi_\text{active}}{\lambda_f} \bfA \otimes \bfA = \dfrac{1}{\lambda_f} \cdot S_\text{max,active}\cdot f_\ell(\lambda_f)\cdot\bar{\gamma}\, \bfA \otimes \bfA.
\end{align}
%
Here, the resulting active stress tensor $\bfS_\text{active}$ is the second order tensor oriented according to the material fiber direction $\bfA: \Omega_0 \to \R^3$ and given by the dyadic product $\bfA \otimes \bfA = A_{i}\,A_{j}\,\bfe_i \otimes \bfe_j$, scaled by the maximum active stress parameter $S_\text{max,active}$, a function $f_\ell$ that models the force-length relation, and the 3D homogenized value $\bar{\gamma}$ of the activation parameter $\gamma \in [0,1]$ following from the half-sarcomere model.

In the deforming body fat layer, the active stress contribution is disregarded. For simulating tendons, different material models can be used such as the model proposed by Carniel et al. \cite{Carniel2017}, which describes microstructural interactions between collagen fibers and their matrix in addition to the elastic response of the fibers themselves. To alter the material model, the definition of $\Psi$ can simply be changed while all other equations remain intact. Similarly, other material models can be defined using the framework of the strain energy function.

In short summary, the following system of equations describes the continuum mechanics model of muscle contraction:
%
\begin{subequations}\label{eq:contraction}
  \begin{align}
    \div(\bfv) &= 0, \qquad &&\text{(incompressibility)} \label{eq:contraction_1}\\[4mm]
    \rho\,\dot{\bfv} &= \rho\,\bfb + \div\bfsigma, && \text{(balance of linear momentum)}\label{eq:contraction_2}\\[4mm]
    \bfsigma &= \bfsigma^\top, && \text{(balance of angular momentum)}\label{eq:contraction_3}\\[4mm]
    \bfS &= 2\p{\Psi(\bfC)}{\bfC}, && \text{(constitutive equation)}\label{eq:contraction_4}
  \end{align}
\end{subequations}
with a material model that defines the strain energy function $\Psi$.

Dirichlet boundary conditions for the displacements $\bfu$ and velocities $\bfv$ can fix certain parts of the muscle, e.g., at the attachment points of the tendons:
\begin{align*}
  \bfu(\bfx,t) &= \bar{\bfu}(t), & \bfv(\bfx,t) &= \bar{\bfv}(t) \quad &&\text{for } \bfx \in ∂\Omega_\text{Dirichlet}.
\end{align*}
%
Initial conditions for $\bfu$ and $\bfv$ define the initial pose of the muscle tissue:
%
\begin{align*}
  \bfu(\bfx,0) &= \bfu_0(\bfx), & \bfv(\bfx,0) &= \bfv_0(\bfx) \quad &&\text{for } \bfx \in \Omega_M.
\end{align*}
%
Additionally, Neumann boundary conditions can be used to prescribe traction forces on the surface.

The description of the multi-scale model \cite{Roehrle2012,Heidlauf2013} assumes quasi-static conditions, which means that the velocities are set to zero, $\bfv=\bfzero$, and inertial terms are neglected. As a consequence, the incompressibility constraint in \cref{eq:contraction_1} has to be formulated differently and the balance of momentum in \cref{eq:contraction_2} reduces to $\rho\,\bfb + \div \bfsigma = 0$.
However, our implementation extends the model to the fully dynamic formulation given in \cref{eq:contraction_1,eq:contraction_2,eq:contraction_3,eq:contraction_4}.

More details on the mechanics equations, their discretization and the resulting numerical scheme to obtain the solution functions $\bfu$ and $\bfv$ are given in \cref{sec:discretization_mechanics}.

\subsection{Discretization and Solution Approach for the Solid Mechanics Model }\label{sec:discretization_mechanics}

The following sections provides a more profound introduction of solid mechanics to complement the overview given before. We also describe the Finite Element discretization of the solid mechanics model and the algorithms used to obtain a numeric solution.

Dynamic \emph{finite elasticity} methods considering large strains and generic hyperelastic materials, both compressible and incompressible, are less frequently used than \emph{linear elasticity} descriptions with linearizations at various levels. Nonetheless, corresponding formulations exist in literature with sometimes varying conventions and symbols.

The implementation of a solver for such generic descriptions exploiting parallel execution and integrating a multi-scale biomechanics model, being a contribution of this work, is an interdisciplinary endeavour. Therefore, we introduce consistent notation and summarize the required basics and the derivation up to the final algorithm  such that it may serve also readers that are not specialized in the field of continuum mechanics. The derivation largely follows the book of Holzapfel \cite{holzapfel2000nonlinear} and the discretization follows the work of Zienkiewicz, Taylor et al. \cite{zienkiewicz1977finite,zienkiewicz2005finite}.

\subsection{Geometric Description}\label{sec:geometric_description}

% introduce quantities
% F, C, E, E(u), variation δE
%  S, P, sigma

We begin with the geometric description of the material body and define the basic quantities that are later used to describe the physics.
As introduced in \cref{sec:model_muscle_contraction}, the function $\bfvarphi_t:\Omega_0\to \Omega_t$ maps points $\bfX$ in the reference configuration $\Omega_0$ to points $\bfx = \bfX + \bfU$ in the current configuration $\Omega_t$ using the displacement field $\bfU(\bfX)$. The displacement field formulated in terms of points $\bfx$ in the current configuration is denoted by $\bfu(\bfx)=\bfU(\bfX(\bfx))$.

The deformation gradient $\bfF$ is the second order tensor that is obtained by differentiating the function $\bfvarphi_t$. It is given using the unit vectors $\bfe_i$ and components $F_{aA}$:
\begin{align*}
  \bfF &= F_{aA}\,\bfe_a \otimes \bfe_A, \quad && F_{aA} = \p{x_a}{X_A}.
\end{align*}
Capital and small indices refer to reference and current configuration, respectively. The deformation gradient can also be expressed using the displacement field $\bfU$:
\begin{align}\label{eq:solid1}
  \bfF = \bfI + ∇\bfU.
\end{align}

Here and in the following, the gradient symbol $∇$ refers to differentiation with respect to material coordinates $\bfX$.
We assume cartesian coordinates.

% tangent, normal, volume map
The determinant of the deformation gradient is $J:= \det \bfF>0$. It is positive for any physically valid transformation.
The deformation gradient is used to map geometric quantities from the reference to the current configuration:
\begin{subequations}\label{eq:geometry_maps}
  \begin{align}
    \bft &= \bfF\,\bfT, & \text{(tangent map)} \label{eq:tangent_map}\\[4mm]
    \bfa &= \cof(\bfF)\,\bfA, & \text{(normal map)} \label{eq:normal_map}\\[4mm]
    v &= J\,V. & \text{(volume map)}\label{eq:volume_map}
  \end{align}
\end{subequations}
%
As given in \cref{eq:tangent_map} and visualized in \cref{fig:geometric_quantities}, the tensor $\bfF$ maps material tangents $\bfT$ in $\Omega_0$ to the corresponding spatial line elements $\bft$ in $\Omega_t$.
Accordingly, the spatial stretch at a point $\bfx \in \Omega_t$ in a certain direction is given by $\lambda=\sqrt{\bflambda^\top\,\bflambda}$ with $\bflambda = \bfF\,\bfM$, where $\bfM$ is a material line element with unit length pointing in the respective Lagrangian direction.

In \cref{eq:normal_map}, the cofactor of $\bfF$ given by $\cof(\bfF) = J\,\bfF^{-\top}$ maps normals $\bfA$ and surface areas $|\bfA|$ from $\Omega_0$ to the corresponding values $\bfa$ and $|\bfa|$ in $\Omega_t$. Nanson's formula, $\d \bfa = \cof(\bfF)\,\d\bfA$, is used to transform surface integrals from Eulerian to Lagrangian description.
Note that tangents at a point $\bfX$ live in the tangent space $T_\bfX\Omega_0$ and normals live in the co-tangent space $T^\ast_\bfX\Omega_0$.

\Cref{eq:volume_map} describes the volume map from $\Omega_0$ to $\Omega_t$, which simply scales the reference volume $V$ by the determinant $J$ to obtain the volume $v$ in the current configuration.

% geometric quantities
\begin{figure}
  \centering%
  \def\svgwidth{0.7\textwidth}
  \input{images/geometric_quantities.pdf_tex}%
  \caption{Vector spaces and variables used in the geometric description of the solid mechanics model. The left side shows the reference configuration with tangent and co-tangent space of point $\bfX$. The right side shows tangent and co-tangent space for the current domain and a point $\bfx$. The spatial stretch $\lambda$ is defined by mapping a material element $\bfM$ to the current configuration. The maps $\bfvarphi_t$, $\bfF$ and $\bfF^{-\top}$ map tangents $\bfT,\bft$ and normals $\bfA,\bfa$ between the configurations.}%
  \label{fig:geometric_quantities}%
\end{figure}

Furthermore, the deformation gradient $\bfF$ is used to define the right Cauchy Green tensor $\bfC = \bfF^\top\bfF$, which maps from tangent to co-tangent space in reference configuration, and subsequently the Green-Lagrange strain tensor:
%
\begin{align*}
  \bfE = \dfrac12(\bfC - \bfI).
\end{align*}
%
This strain measure can be interpreted as comparing the current Lagrangian metric $\bfC$, a measure for the symmetric part of the current deformation, with the reference metric which is the identity. Using \cref{eq:solid1}, the Green-Lagrange strain tensor can be formulated in terms of derivatives of the displacements:%
\begin{align}\label{eq:green_lagrange_u}
  \bfE &= \dfrac12\big((∇\bfU)^\top + ∇\bfU + ∇\bfU^\top ∇\bfU\big).
\end{align}

% stress measures
To employ the physical balance principles described in \cref{sec:model_muscle_contraction}, we need to define stress measures. The Cauchy stress tensor $\bfsigma$ has been introduced in \cref{sec:model_muscle_contraction} using Euler's cut principle where the mechanical action on an arbitrary cut out of the domain is represented by a traction vector $\bft$ as contact force per surface area. The traction vector acts on the current configuration and is a function of the position $\bfx \in \Omega_t$ and the local orientation of the cut given by the normal vector $\bfn$. The stress tensor $\bfsigma$ is defined by Cauchy's theorem:
\begin{align*}
  \bft = \bfsigma \cdot \bfn.
\end{align*}
Thus, the Cauchy stress describes the \say{true stress} of contact forces per deformed area. Both slots of the second order tensor are associated with the current configuration. More specifically, $\sigma$ is contravariant and maps from a normal $\bfn$ in co-tangent space $T^\ast_\bfx\Omega_t$ to the traction $\bft$ in tangent space $T_\bfx\Omega_t$. While the physical description is natural in this Eulerian setting, the numerical treatment is more convenient in the Lagrangian setting, where we can integrate over a non-deforming domain.
Moreover, a two-point setting, where surface areas are measured in the undeformed configuration and traction forces are measured in the deformed configuration, is often useful in engineering. This is the natural setting, e.g., in tension tests. Therefore, other stress measures involving the reference configuration are defined.

% numerics -> physics
% pull-back, push-back
Using the mappings presented in \cref{eq:geometry_maps}, all quantities can be transformed between both configurations.
The physical derivation can be carried out equivalently in a Lagrangian or Eulerian setting and switching between them is possible at any point in the derivation. For this purpose, two operations are defined: the pull-back $\varphi^\ast(\bfg) = \bfF^\top\bfg\,\bfF$ and push-forward operations $\varphi_\ast(\bfG) = \bfF^{-\top}\bfG\,\bfF^{-1}$, which bring tensors from Eulerian to Lagrangian description and vice-versa.

Continuum mechanical models establish equations for the unknown displacement function $\bfu$ and its evolution in time via relations between stresses and strains. In the following, we focus on descriptions of the stress.

% sigma, 1st PK, 2nd PK
The first Piola-Kirchhoff stress tensor $\bfP$ measures contact forces in the current configuration with regard to the area of the reference configuration and relates to the Cauchy stress as $\bfP = \bfsigma\,\cof(\bfF)$. The second Piola-Kirchhoff tensor $\bfS$ is a fully Lagrangian field given as the pull-back of the Cauchy stress scaled by $J$:%
\begin{align*}
  \bfS = \varphi^\ast(J\,\bfsigma) = J\,\bfF^{-1}\bfsigma\,\bfF^{-\top}.
\end{align*}
It appears in the summary of the muscle contraction model in \cref{eq:contraction} in connection with the strain energy function $\Psi$.

% stress tensors
\begin{figure}
  \centering%
  \def\svgwidth{0.5\textwidth}
  \input{images/stress_tensors.pdf_tex}%
  \caption{Stress tensors and geometric maps that can be used together in a solid mechanics formulation. The right Cauchy-Green tensor $\bfC$  and the second Piola-Kirchhoff stress $\bfS$ are dual Eulerian tensors and map between tangent space $T_\bfX\Omega_0$ and co-tangent space $T^\ast_\bfX\Omega_0$ in the reference domain. The deformation gradient $\bfF$ and the first Piola-Kirchhoff stress $\bfP$ are dual two-point tensors mapping from the reference to the current configuration.
  The Eulerian metric $\bfg$ which is the identity in cartesian coordinates and the Kirchhoff stress $J\,\bfsigma$ (where $\bfsigma$ is the Cauchy stress) are the dual objects in the Eulerian setting. All three pairs of dual tensors are linked together by transformations such as pull-back and push-forward.
  }%
  \label{fig:stress_tensors}%
\end{figure}

\Cref{fig:stress_tensors} summarizes the geometric maps by black arrows and the stress measures by red arrows.
To relate strains and stresses in a material model, the corresponding tensors have to be dual objects. Here, three settings are possible: The Eulerian setting uses the metric $\bfg$ and the dual Kirchhoff stress $J\,\bfsigma$. The two-point setting uses the deformation gradient $\bfF$ and the dual first Piola-Kirchhoff stress $\bfP$. The Lagrangian setting uses the right Cauchy-Green tensor $\bfC$ and the dual second Piola-Kirchhoff stress $\bfS$.
Different pairs of strain and stress tensors can be used.
The Lagrangian setting defines the right Cauchy-Green tensor $\bfC$ and the dual second Piola-Kirchhoff stress $\bfS$.
We consider these quantities in the further derivations, because the Lagrangian formulation is naturally used to derive the discretization schemes.

\subsection{A Linearized Model as an Exemplary Description of Solid Mechanics}\label{sec:linearized_mechanics_model}

The quantities introduced in \cref{sec:geometric_description} are linked together by various relations, which are summarized in a digram in \cref{fig:tonti_diagram}. The goal is to find the relationship between given forces (top left in \cref{fig:tonti_diagram}) and the resulting deformation of the body described by the displacements (top right in \cref{fig:tonti_diagram}).
Prescribed external traction forces $\bfT$ and external or inertial body forces $\bfB$ act on the body and result in stresses $\bfS$ satisfying the \emph{equilibrium} relation. A \emph{material law} connects stresses $\bfS$ and strains $\bfE$. The \emph{kinematics} of the body determine the relationship between displacements $\bfu$ and strains $\bfE$. Geometric Dirichlet boundary conditions prescribe displacements and Neumann boundary conditions such as traction forces contribute to the stress field.

Whereas the equilibrium relation is linear, the material and kinematic descriptions can both be chosen to be linear or nonlinear.
In cases of small strains, geometric and material linearity can be assumed.

In this section, we present a simplified, static model where all of the assumed relations are linear.
This model serves as a prototype for the subsequent derivation of the fully nonlinear model. Besides the nonlinear model, our software OpenDiHu also implements the linearized description. The linear model exhibits better numerical properties and can be solved faster than the generic model. Thus, it can serve as a toy problem and for mechanical systems, where the linearization assumption is valid.

% Tonti diagram
\begin{figure}
  \centering%
  \def\svgwidth{\textwidth}
  \input{images/tonti_diagram.pdf_tex}%
  \caption{The three relations between various quantities that compose the solid mechanics model: Equilibrium links traction and body forces $\bfT$ and $\bfB$ to the stresses $\bfS$. A material model connects them to strains $\bfE$. The kinematic relations yield the resulting displacement field $\bfu$. Note that all quantities in this diagram are given in Lagrangian formulation.}%
  \label{fig:tonti_diagram}%
\end{figure}
% linear, static and dynamic

Using variational calculus, the system response of external forces and infinitesimal, compatible, virtual displacements $δ\bfu$ is studied.
We start with the \emph{principle of virtual work}, which states that in equilibrium the virtual work $δW$ performed by external forces along virtual displacements $δ\bfu$ is zero. Equivalently, the external virtual work $δW_\text{ext}$ is equal to the internal virtual work $δW_\text{int}$.

The external virtual work $δW_\text{ext}$ is given by external forces $\bft$ and the virtual displacements $δ\bfu$ at the same location. The internal virtual work $δW_\text{int}$ is the body's response in terms of stresses $\bfsigma$ and virtual strains $\bfeps$.
In summary, the equilibrium equation is given by:
\begin{align}
  δW_\text{int}(\bfu,δ\bfu) &= δW_\text{ext}(δ\bfu) \qquad && \forall δ\bfu \in H^1_0(\Omega)\label{eq:linearized_helper1}\\[4mm]
  \quad \Leftrightarrow \quad \ds\int_\Omega \bfsigma(\bfu) : δ\bfeps\,\d\bfx &= \ds\int_{∂\Omega} \bft : δ\bfu\,\d \bfx &&\forall δ\bfu \in H^1_0(\Omega).\label{eq:linearized_helper1b}
\end{align}
Here, the vectors contain the degrees of freedom of a Finite Element discretization. The operator \say{:} denotes the component-wise product.

Often, it is easier to write the equations in component form. Indices $a,b,c,\dots$ are used to specify a dimension index in $\{1,\dots,d\}$. The letters $L,M \in \{1,\dots,N\}$ designate indices over degrees of freedom in a mesh with $N$ nodes. The Einstein sum convention is used where repeated indices implicitly indicate summation, except when the indices are in parantheses.
Thus, the right hand side of \cref{eq:linearized_helper1b} with ansatz functions $\phi^L$ and the degrees of freedom $δu_a^L$ of $δ\bfu$ can be written as:
\begin{align*}
  \bff_a = \ds\int_{∂\Omega} t_{(a)}\,δu_{(a)}^L\,\phi^L \,\d \bfx.
\end{align*}

The linear material model is Hooke's law, given by
%
\begin{align}\label{eq:linearized_helper2}
  \bfsigma = \C:\bfeps
\end{align}
with the fourth order material tensor%
\begin{align*}
  \C_{abcd} = K\,δ_{ab}\,δ_{cd} + μ\,(δ_{ac}\,δ_{bd} + δ_{ad}\,δ_{bc} - \dfrac23 δ_{ab}\,δ_{cd}).
\end{align*}
The bulk modulus $K$ is a measure for the (in-)compressibility and the shear modulus $\mu$ specifies the elastic shear stiffness. $δ_{ab}$ is the Kronecker delta.
The material tensor $\C$ exhibits the following major and minor symmetries:%
\begin{subequations}\label{eq:symmetries}
\begin{align}
  \C_{abcd} &= \C_{cdab}, \quad &\text{(major symmetries)}\\[4mm]
  \C_{abcd} &= \C_{bacd} = \C_{abdc} = \C_{badc}, \quad & \text{(minor symmetries)}
\end{align}
\end{subequations}
effectively reducing the number of independent entries from 81 to 21 for 3D domains.

The third relation according to \cref{fig:tonti_diagram} is the kinematics relation between displacements $\bfu$ and strains $\bfeps$.
The strain expression given in \cref{eq:green_lagrange_u} is linearized by neglecting products of the derivatives and using the spatial displacements $\bfu$ instead of $\bfU$:
\begin{align}\label{eq:linearized_helper3}
  \bfeps = \dfrac12\big((∇\bfu)^\top + ∇\bfu\big).
\end{align}
Because of small displacements, we do not distinguish between reference and current configuration, and \cref{eq:linearized_helper1} combines the strain measure $\bfeps$, which is derived from the Lagrangian Green-Lagrange strain with the Eulerian Cauchy stress $\bfsigma$.

By combining \cref{eq:linearized_helper1,eq:linearized_helper2,eq:linearized_helper3} and discretizing displacements and virtual displacements, we get the linear matrix equation
\begin{align}\label{eq:linearized_helper4}
  \bfK\,\bfu = \bff.
\end{align}
The stiffness matrix $\bfK$ has rows and columns for every combination of degree of freedom $L,M \in \{1,\dots,N\}$ and dimension indices $a,b \in \{1,2,3\}$. The entries are given by:
\begin{align*}
  \bfK_{LaMb} = \ds\int_{\Omega} \mathbb{C}_{adbc}\p{\phi^L(\bfx)}{x_{d}}\p{\phi^M(\bfx)}{x_{c}}\,\d \bfx.
\end{align*}
%

The resulting model in \cref{eq:linearized_helper4} describes the passive behavior of a body under the linearization assumptions. For muscle tissue, we also need to incorporate active stresses that are generated at the sarcomeres in the muscle. We add an active stress term $\bfsigma^\text{active}$ to the external virtual work in \cref{eq:linearized_helper1}, yielding the model:
%
\begin{align}\label{eq:linearized_helper5}
  δW_\text{int}(\bfu,δ\bfu) &= \bff + \ds\int_\Omega \bfsigma^\text{active} : δ\bfeps_{-}\,\d\bfx &&\forall δ\bfu \in H^1_0(\Omega).
\end{align}
%
The active stress is associated with compression, i.e., negative virtual strains $δ\bfeps < 0$. Therefore, we use $δ\bfeps_{-}$ which is defined equal to $δ\bfeps$ for $δ\bfeps < 0$ and zero otherwise.
From \cref{eq:linearized_helper5}, we get the same discretized linear system as in \cref{eq:linearized_helper4}, but with an additional term $\bff^\text{ active}$ at the right hand side that contains the discretized prescribed active stress field $\bfsigma^\text{active}_{ab}(\bfx)$:
\begin{align*}
  \bff^\text{ active}_{La} = \ds\int_{Ω}\bfsigma^\text{active}_{ab}(\bfx)\,\p{\phi^L(\bfx)}{x_{b}} \,\d\bfx.
\end{align*}
%

\subsection{Nonlinear Material Modeling}\label{sec:material_modeling}
%
Next, we present the derivation of a nonlinear model that forgoes all linearization assumptions of small strains. We begin with the description of the material law, which links strains and stresses.

As noted in \cref{sec:discretization_mechanics}, the strain energy function $\Psi$ is used to define the material model and it is linked to the second Piola-Kirchhoff stress $\bfS$ by the relation%
\begin{align}\label{eq:material_model_helper1}
  \bfS = 2\p{\Psi(\bfC)}{\bfC}.
\end{align}

The \emph{principle of material objectivity} requires that material properties are invariant under a change of observer. As a result, the \emph{representation theorem for isotropic materials} states that the stress tensor can be represented using three strain invariants $I_1, I_2$ and $I_3$. For a transversely isotropic material, two invariants $I_4$ and $I_5$ that depend on the anisotropy direction $\bfa_0$ (corresponding to a fiber direction) are added.
Consequently, we can formulate the strain energy function $\Psi=\Psi(I_1,I_2,I_3,I_4,I_5)$ in terms of these invariants. The principle strain invariants $I_1$ to $I_3$ of the right Cauchy-Green tensor $\bfC$ and the additional anisotropic invariants $I_4$ and $I_5$ are defined as:
\begin{align*}
  &I_1(\bfC) = \tr(\bfC),  &
  &I_2(\bfC) = \dfrac12\big(\tr(\bfC)^2 - \tr(\bfC^2)\big), &
  I_3(\bfC) = \det(\bfC) = J^2,\\[4mm]
  &I_4(\bfC,\bfa_0) = \bfa_0 \cdot \bfC \, \bfa_0, &
  &I_5(\bfC,\bfa_0) = \bfa_0 \cdot \bfC^2 \, \bfa_0. &
\end{align*}
The fiber stretch is related to the fourth invariant by $\lambda_f = \sqrt{I_4}$. Note that requiring incompressibility is equivalent to enforcing $J=1$, and, in this case, we get ${I_3(\bfC) = 1}$.

It is convenient to use a decoupled description, where the deformation gradient $\bfF$ and the right Cauchy-Green tensor $\bfC$ are multiplicatively decomposed into volume-changing (volumetric) and volume-preserving (isochoric) parts:%
\begin{align*}
  \bfF &= (J^{1/3}\bfI)\,\bar{\bfF},  & \bfC &= (J^{2/3}\bfI)\,\bar{\bfC}.
\end{align*}
%
Here, the volumetric parts are the identity tensors scaled by a power of the determinant $J$ of the deformation gradient. The isochoric or distortional parts $\bar{\bfF}$ and $\bar{\bfC}$ are given by%
\begin{align*}
  \bar{\bfF} &= J^{-1/3}\,\bfF,  & \bar\bfC &= J^{-2/3}\,\bfC.
\end{align*}
The reduced invariants $\bar{I}_1$ to $\bar{I}_5$ of the reduced right Cauchy-Green tensor $\bar\bfC$ are defined accordingly.
Similarly, the strain energy function has a decoupled representation with volumetric part $\Psi_\text{vol}$ and isochoric part $\Psi_\text{iso}$:
\begin{align*}
  \Psi = \Psi_\text{vol}(J) + \Psi_\text{iso}(\bar{\bfC}) = \Psi_\text{vol}(J) + \Psi_\text{iso}(\bar{I}_1,\bar{I}_2,\bar{I}_4,\bar{I}_5).
\end{align*}

Using the decoupled form, any incompressible material can be modeled with the \emph{penalty method} as follows.
The material behaviour is given by the isochoric strain energy $\Psi_\text{iso}(\bar{\bfC})$, e.g., by employing the Mooney-Rivlin model in \cref{eq:mooney_rivlin}. The volumetric part is defined as
\begin{align*}
  \Psi_\text{vol}(J) &= \kappa\,G(J) \qquad \text{with } G(J) = \dfrac12 (J-1)^2,
\end{align*}
with the incompressibility parameter $\kappa$ and the penalty function $G(J)$. This function is strictly convex and approaches zero as $J$ approaches 1. For large values of $\kappa$, the behavior is nearly incompressible. A disadvantage of this method is, that the resulting system becomes singular for $J \to 1$.

A better approach in this regard is to use a mixed formulation, where incompressibility is enforced exactly using a Lagrange multiplier. This approach is also implemented in OpenDiHu and is the preferred method for incompressible materials.

In OpenDiHu, the strain energy function of a new material can be given using the following four terms:
%
\begin{align*}
  \Psi = \Psi_\text{vol}(J) + \Psi_\text{iso}(\bar{I}_1,\bar{I}_2,\bar{I}_4,\bar{I}_5) + \Psi_1(I_1,I_2,I_3) + \Psi_2(\bfC,\bfa_0).
\end{align*}
The decoupled form is available with $\Psi_\text{vol}$ and $\Psi_\text{iso}$, the coupled form for isotropic materials can be used via $\Psi_1$. The term $\Psi_2$ gives the most flexibility, as the constitutive model can be directly formulated using the right Cauchy-Green tensor $\bfC$ and the fiber direction $\bfa_0$. The unused terms among $\Psi_\text{vol},\Psi_\text{iso},\Psi_1$ and $\Psi_2$ can be defined as constant zero. The incompressibility constraint using Lagrange multipliers can be switched on or off such that both incompressible and compressible materials can be computed.
%

\subsection{Derivation of the Stress Tensor and the Elasticity Tensor}\label{sec:stress_and_elasticity}
Following \cref{eq:material_model_helper1}, the second Piola-Kirchhoff stress $\bfS$ is given by a the derivative of the strain energy function $\Psi$ with respect to $\bfC$.
For the representation using the invariants, the chain rule has to be used:%
\begin{align*}
   \bfS &= 2\,\p{\Psi(\bfC)}{\bfC} = \p{\Psi}{I_a}\p{I_a}{\bfC}.
\end{align*}
Using the decoupled form, the resulting stresses are also decoupled as $\bfS = \bfS_\text{vol}+\bfS_\text{iso}$. The volumetric stress $\bfS_\text{vol}$ describes the elastic response to compression, the isochoric stress $\bfS_\text{iso}$ describes the response to the deviatoric deformation. In the following, all steps to compute these stresses are listed. The rationale is to give a condensed reference of the implemented algorithm in OpenDiHu to facilitate further development.
For the derivation of all intermediate steps, we refer to the literature \cite{holzapfel2000nonlinear}.

At first, the reduced stress tensor $\bar{\bfS}$ that neglects the volumetric change is formulated as:%
%
\begin{align*}
  \bar{\bfS} = 2\p{\Psi_\text{iso}(\bar{I}_1,\bar{I}_2,\bar{I}_4,\bar{I}_5)}{\bar{\bfC}} &= \bar{\gamma}_1\,\bfI + \bar{\gamma}_2\,\bar{\bfC}
  + \bar{\gamma}_4\, \bfa_0 \otimes \bfa_0 + \bar{\gamma}_5\,(\bfa_0 \otimes \bar{\bfC}\,\bfa_0 + \bfa_0\bar{\bfC}\otimes \bfa_0).
\end{align*}
In case of an isotropic material, the terms involving $\bfa_0$ are not needed. The prefactors are given by derivatives of the strain energy function with respect to the reduced invariants:
%
\begin{align*}
  \bar{\gamma}_1 &= 2\left(\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_1} + \bar{I}_1\,\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_2}\right),
  &\bar{\gamma}_2 &= -2\p{\Psi_\text{iso}(\bar{I}_1, \bar{I}_2)}{\bar{I}_2},
  &\bar{\gamma}_4 &= 2\p{\Psi_\text{iso}}{\bar{I}_4}\\[4mm]
  \bar{\gamma}_5 &= 2\p{\Psi_\text{iso}}{\bar{I}_5}
\end{align*}
%
Using the fourth order identity tensor $\mathbb{I}$ and the projection tensor $\mathbb{P}$,%
\begin{align*}
  (\mathbb{I})_{abcd} &= \delta_{ac}\,\delta_{bd}, &
  \mathbb{P} &= \mathbb{I} - \dfrac13 \bfC^{-1} \otimes \bfC,
\end{align*}
the stress tensors can finally be computed as
\begin{align*}
  \bfS_\text{iso} &= J^{-2/3}\mathbb{P}:\bar{\bfS}, &
  \bfS_\text{vol} &= J\,p\,\bfC^{-1}, &
  \bfS &= \bfS_\text{iso} + \bfS_\text{vol}.
\end{align*}
In the compressible case including the penalty method, the value of $p$, that is needed for $\bfS_\text{vol}$, is given by the constitutive model as $p = \d \Psi_\text{vol}(J)/\d J$. In the incompressible case, $p$ is the unknown Lagrange multiplier that gets computed as part of the numerical solution. In that case, $p$ has the physical meaning of the hydrostatic pressure.

Another important quantity for the numerical solution is the fourth order elasticity tensor $\C$, which is defined as
\begin{align*}
  \C = 2\p{\bfS(\bfC)}{\bfC} = 4\dfrac{\partial^2 \Psi(\bfC)}{\partial\bfC\partial\bfC}.
\end{align*}
It is the derivative of the stress tensor and is required in the Jacobian matrix of an iteration of the nonlinear Newton solver. Like the material tensor in \cref{eq:symmetries}, it shows major and minor symmetries and has 21 independent entries.

Like the stress tensor, the elasticity tensor is also additively composed into a volumetric term $\C_\text{vol}$ and an isochoric term $\C_\text{iso}$. The volumetric term can be computed by:%
\begin{align*}
  \mathbb{C}_\text{vol} &= J\,\tilde{p}\,\bfC^{-1} \otimes \bfC^{-1} - 2\,J\,p\,\bfC^{-1} \odot \bfC^{-1}, &
  \big(\bfC^{-1} \odot \bfC^{-1}\big)_{abcd} &= \dfrac12\big(C^{-1}_{ac}\,C^{-1}_{bd} + C^{-1}_{ad}\,C^{-1}_{bc}\big).
\end{align*}
The term includes two pressure variables $\tilde{p}$ and $p$. In the incompressible formulation, both variables equals the Lagrange multiplier $p$. For the compressible formulation, $\tilde{p}$ is derived as $\tilde{p} = p + J\,\d p/\d J$ and $p$ is computed from the volumetric strain energy function as stated above.

\clearpage
The isochoric term $\mathbb{C}_\text{iso}$ of the elasticity tensor follows from the following list of quantities to compute:%
\begin{align*}
  &\bar{\delta}_1 = 4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_1} + 2\,\bar{I}_1\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_2} +\dfrac{∂\Psi_\text{iso}}{∂\bar{I}_2} + \bar{I}_1^2\,\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}\right), \,
  \bar{\delta}_2 = -4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_2} + \bar{I}_1\,\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}\right),\\[4mm]
  &\bar{\delta}_3 = 4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_2}, \quad
  \bar{\delta}_4 = -4\dfrac{∂\Psi_\text{iso}}{∂\bar{I}_2}, \quad
  \bar{\delta}_5 = 4\left(\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_1\,∂\bar{I}_4} +\bar I_1 \dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_4}\right),\\[4mm]
  &\bar{\delta}_6 = -4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_2\,∂\bar{I}_4}, \,\,\,\,
  \bar{\delta}_7 = 4\dfrac{∂^2\Psi_\text{iso}}{∂\bar{I}_4\,∂\bar{I}_4}, \,\,\,\,
  \mathbb{I}_{abcd} = δ_{ac}\,δ_{bd}, \,\,\,\,
  \bar{\mathbb{I}}_{abcd} = δ_{ad}\,δ_{bc}, \,\,\,\,
  \mathbb{S} = (\mathbb{I} + \bar{\mathbb{I}}) / 2, \\[4mm]
  &\p{\bar I_5}{\bar\bfC} = \bfa_0 \otimes \bar\bfC\,\bfa_0 + \bfa_0\,\bar\bfC \otimes \bfa_0, \quad
  \dfrac{∂^2\bar{I}_5}{∂\bar{\bfC}∂\bar{\bfC}} = \p{\bar{\bfC}}(\bfa_0 \otimes \bar\bfC\,\bfa_0 + \bfa_0\,\bar\bfC \otimes \bfa_0),\\[4mm]
  &\bar{\mathbb{C}} = J^{-4/3}\bigg(\bar{\delta}_1\,\bfI \otimes \bfI + \bar{\delta}_2\,\big(\bfI \otimes \bar{\bfC} + \bar{\bfC} \otimes \bfI\big) + \bar{\delta}_3\bar{\bfC} \otimes \bar{\bfC} + \bar{\delta}_4\,\mathbb{S}
  +\bar{δ}_5\,(\bfI \otimes \bfa_0 \otimes \bfa_0 + \bfa_0 \otimes \bfa_0 \otimes \bfI)\\[4mm]
  &\hspace*{1cm} +\bar{δ}_6\,(\bar{\bfC} \otimes \bfa_0 \otimes \bfa_0 + \bfa_0 \otimes \bfa_0 \otimes \bar{\bfC})
  +\bar{δ}_7\,(\bfa_0 \otimes \bfa_0 \otimes \bfa_0 \otimes \bfa_0) \\[4mm]
  &\hspace*{1cm} + \bar{δ}_8\,\Big(\bfI \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bfI \Big)
  + \bar{δ}_9\,\Big(\bar{\bfC} \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bar{\bfC} \Big) + \bar{δ}_{10}\Big(\p{\bar{I}_5}{\bar{\bfC}} \otimes \p{\bar{I}_5}{\bar{\bfC}}\Big) \\[4mm]
  &\hspace*{1cm}+ \bar{δ}_{11} \Big(\bfa_0 \otimes \bfa_0 \otimes \p{\bar{I}_5}{\bar{\bfC}} + \p{\bar{I}_5}{\bar{\bfC}} \otimes \bfa_0 \otimes \bfa_0 \Big) + \bar{δ}_{12} \dfrac{∂^2\bar{I}_5}{∂\bar{\bfC}∂\bar{\bfC}}\bigg)\\[4mm]
  &\tilde{\mathbb{P}} = \bfC^{-1} \odot \bfC^{-1} - \dfrac13 \bfC^{-1} \otimes \bfC^{-1} \\[4mm]
  &\mathbb{C}_\text{iso} = \mathbb{P} : \bar{\mathbb{C}} : \mathbb{P}^\top + \dfrac23 J^{-2/3} \bar{\bfS} : \bfC\,\tilde{\mathbb{P}} - \dfrac23\big(\bfC^{-1}\otimes \bfS_\text{iso} + \bfS_\text{iso}\otimes \bfC^{-1}\big)
\end{align*}
Then, $\C = \C_\text{vol} + \C_\text{iso}$ can be calculated.
%
%

% invariants: I1-I5
% transversely isotropic
% reduced invariants, reduced quantities for compressible materials
% strain energy function, derivative
% elasticity tensor
% -> computation of S and C

\subsection{Derivation of the Static Hyperelastic Finite Element Model}\label{sec:static_hyperelastic_fe_model}

In \cref{sec:linearized_mechanics_model}, the ingredients of a solid mechanics model derivation consisting of equilibrium, material and kinematic equations were outlined and used to derive a linearized description. For the nonlinear model, the material equations were discussed in \cref{sec:material_modeling} and the stress and elasticity tensors were derived in \cref{sec:stress_and_elasticity}. This section uses these building blocks and presents the full derivation for the generic hyperelastic Finite Element model.

First, we assume a static, incompressible problem. The equilibrium equation can be formulated in terms of the \emph{Hellinger-Reissner energy functional} $\Pi_L(\bfu,p)$, which describes the potential energy of the system depending on the displacement and pressure functions $\bfu$ and $p$.
The functional is additively composed of internal and external potential energy:
\begin{align*}
  \Pi_L(\bfu,p) = \Pi_\text{int}(\bfu,p) + \Pi_\text{ext}(\bfu).
\end{align*}
The external energy functional is formulated by
\begin{align*}
  \Pi_\text{ext}(\bfu) = -\ds\int_{\Omega_0} \bfB\, \bfu\,\d V - \ds\int_{∂\Omega_0^t}\bar{\bfT}\,\bfu\,\d S,
\end{align*}
with body force $\bfB$ in reference configuration and prescribed surface traction $\bar{\bfT}$ on the traction boundary $∂\Omega_0^t$.
The internal energy functional is given by:
%
%
\begin{align}\label{eq:mechanics_helper1}
  \Pi_\text{int}(\bfu,p) = \ds\int_{\Omega_0} \Psi_\text{iso}\big(\bar\bfC(\bfu)\big)\,\d V
    + \ds\int_{\Omega_0} p\,\big(J(\bfu) - 1\big)\,\d V.
\end{align}
%
Here, $\Psi_\text{iso}$ is the isochoric strain-energy density function in terms of the reduced right Cauchy-Green tensor $\bar{\bfC}$.
The first term in \cref{eq:mechanics_helper1} describes the isochoric elastic response of the material, the second term adds the incompressibility constraint $J=1$ with the Lagrange multiplier $p$. The value of $p$ is computed as part of the model and can be identified as the hydrostatic pressure. Therefore, the second term is interpreted as the elastic response to compression and is included in the internal energy functional $\Pi_\text{int}$.

According to the \emph{principle of stationary potential energy}, the system is in equilibrium, if the potential energy functional is stationary.
This is the case, if the first variation $δ\Pi_L$ is zero.
Using the additive structure of $\Pi_L$, we can express the principle of stationarity as
\begin{subequations}
  \begin{align}
    D_{δ\bfu}\Pi_L(\bfu, p) &= D_{δ\bfu}\Pi_\text{int}(\bfu,p) + D_{δ\bfu}\Pi_\text{ext}(\bfu) \overset{!}{=} 0, & \forall δ\bfu  \label{eq:variations_functional_zero_a}\\[4mm]
    D_{δp}\Pi_L(\bfu, p) &= D_{δp}\Pi_\text{int}(\bfu,p) \overset{!}{=} 0 & \forall δp. \label{eq:variations_functional_zero_b}
  \end{align}
\end{subequations}
The variations of the internal and external energy functionals are defined as
\begin{align}\label{eq:def_variation}
  D_{δ\bfu}\Pi(\bfu) &= \d{\eps} \Pi(\bfu + \epsδ\bfu)\big|_{\eps=0}, &
  D_{δp}\Pi(p) &= \d{\eps} \Pi(p + \epsδp)\big|_{\eps=0}.
\end{align}
They can be identified as the internal and external virtual work,
\begin{align*}
  D_{δ\bfu}\Pi_\text{int}(\bfu,p) &= δW_\text{int}, & D_{δ\bfu}\Pi_\text{ext}(\bfu) &= -δW_\text{ext}.
\end{align*}
Thus, \cref{eq:variations_functional_zero_a} can be expressed as
\begin{align*}
  δW_\text{int} - δW_\text{ext} &= 0,
\end{align*}
which is the form of the equilibrium equation that was used in \cref{eq:linearized_helper1} in the derivation of the linearized model in \cref{sec:linearized_mechanics_model} . The Euler-Lagrange equations corresponding to the variational problem are the local incompressibility constraint and the partial differential equation of balance of momentum presented in \cref{eq:contraction_1,eq:contraction_2}.

Executing the derivative in the definitions of the variations in \cref{eq:def_variation} yields the following terms:
\begin{align*}
  &D_{δ\bfu}\Pi_\text{int}(\bfu,p)  = \ds\int_{\Omega_0} \bfS(\bfu,p): δ\bfE(δ\bfu)\,\d V,
  \qquad D_{δp}\Pi_\text{int}(\bfu,p) =\ds\int_{\Omega_0} \big(J(\bfu) - 1\big)δp\,\d V, \\[4mm]
  &D_{δ\bfu}\Pi_\text{ext}(\bfu) = -\ds\int_{\Omega_0} \bfB\cdot δ\bfu\,\d V - \ds\int\limits_{∂\Omega^t_0} \bar{\bfT}\cdot δ\bfu\,\d S,
\end{align*}
where the variational variables $δp,δ\bfu$ and $δ\bfE$ are the virtual pressure, virtual displacements, and virtual strains.

% discretization
We discretize the solutions of the functional for the displacements $\bfu(\bfx)$ and pressure $p(\bfx)$ and their variations using different ansatz functions $\phi^L$, $L=1,\dots,N_u$ and $\psi^L$, $L=1,\dots,N_p$:
\begin{align*}
   u_a &= \hat{u}_a^L \phi_{(a)}^L, & δu_a &= δ\hat{u}_a^L \phi_{(a)}^L,   & p &= \hat{p}^L \psi^L, & δp &= δ\hat{p}^L \psi^L.
\end{align*}
The displacements function is vector-valued and given by $\bfu(\bfx) = (u_1(\bfx), u_2(\bfx), u_3(\bfx))^\top$. The vectors containing the degrees of freedom are denoted by $\hat{\bfu} = (\hat{u}^L)_{L=1,\dots,N_u}$ and $\hat{\bfp} = (\hat{p}^L)_{L=1,\dots,N_p}$.

The kinematics equation to compute virtual strains from virtual displacements follows from \cref{eq:green_lagrange_u} in Lagrangian description and is given by $δ\bfE = \sym(\bfF^\top ∇\bfu)$ or in discretized form, where the subscript comma $\square_{,A}$  indicates the derivative with respect to the indexed coordinate $\bfX_A$:
\begin{align*}
  δE_{AB} &= \dfrac12\left(F_{aB}\, \phi_{(a),A}^M + F_{aA}\, \phi_{(a),B}^M\right)δ\hat{u}_{a}^M.
\end{align*}
%
% summarize equations
In summary, the discretized nonlinear equations are given by
\begin{align*}
  δW_\text{int}(\bfu,p) - δW_\text{ext} &= 0 \qquad &\forall\,δ\bfu, \\[4mm]
  D_{δp}\Pi_L(\bfu) &= 0 \qquad &\forall\,δp,
\end{align*}
with the discretized terms
\begin{subequations}\label{eq:mechanics_static_system}
  \begin{align}
    δW_\text{int}({\bfu},{p})  = \ds\int_{\Omega}\dfrac12  S_{AB}(\bfu,p)\, \left(F_{aB}\, \phi_{(a),A}^M + F_{aA}\, \phi_{(a),B}^M\right)δ\hat{u}_{a}^M \,\d V,\\[4mm]
    δW_\text{ext}  = \ds\int_{\Omega} B_a \phi_{(a)}^M\,δ\hat{u}^M_a \,\d V +\ds\int_{∂\Omega}  \bar{T}^L_a\,\phi_{(a)}^L\, \phi_{(a)}^M\,δ\hat{u}^M_a\,\d S, \\[4mm]
    D_{δp}\Pi_L(\bfu) = \ds\int_\Omega \big(J(\bfu) - 1)\,δp\,\d V .
  \end{align}
\end{subequations}

\subsection{Nonlinear Solver for the Solid Mechanics Model}\label{sec:solver_static_hyperelastic_fe_model}

% Newton solver
The governing nonlinear system of equations is solved by a Newton scheme. We define the vector of the unknown degrees of freedom as $(\hat{\bfu},\hat{p}) =: \bfz$. Then, the nonlinear equation takes the general form $\bfW(\bfz) = 0$. By linearization around a value $\bfz$, we get%
\begin{align*}
  \bfW(\bfz+Δ\bfz) = \bfW(\bfz) + \bfJ\,Δ\bfz + o(\bfz + Δ\bfz),
\end{align*}
with the increment $Δ\bfz = (Δ\hat\bfu, Δ\hat{p})$ and the Jacobian matrix $\bfJ = \partial {\bfW}/\partial {\bfz}$.
Neglecting the sublinear error term $o(z + Δz)$, we can start from an initial guess $\bfz^{(0)}$ and proceed to find the root of $\bfW$ using the the following iterative Newton scheme:%
\begin{subequations}\label{eq:newton_scheme}
  \begin{align}
    \bfJ\,Δ\bfz^{(n)} = -\bfW(\bfz^{(n)}),\label{eq:mechanics_linear_system}\\[4mm]
    \bfz^{(n+1)} = \bfz^{(n)} + Δ\bfz^{(n)}.
  \end{align}
\end{subequations}
\Cref{eq:mechanics_linear_system} is a linear system of equations with the system matrix given by $\bfJ$, which has to be solved in every iteration step $n$. The linear system of equations can be expressed as follows:
\begin{align}\label{eq:static_newton_iteration}
  \matt{\bfk_{δ\bfu,Δ\bfu} & \bfk_{δp,Δ\bfu}^\top \\[2mm]
  \bfk_{δp,Δ\bfu} & \bfzero} \, \matt{Δ\hat{\bfu} \\[2mm] Δ\hat{p}}
  =
  \matt{-\bfR_{δ\bfu} \\[2mm] -\bfR_{δp}}.
\end{align}
The definition of the right hand sides $\bfR_{δ\bfu} = δW_\text{int} - δW_\text{ext}$ and $\bfR_{δp}=D_{δp}\Pi_L$ is given in \cref{eq:mechanics_static_system}. The system matrix is composed as follows. The upper left part consists of 3 times 3 blocks of submatrices, each with size $N_u \times N_u$ and the entries given by:
\begin{align*}
  \bfk_{δ\bfu,Δ\bfu,(L,a),(M,b)} &= \ds\int_\Omega \phi_{(a),B}^L\tilde{k}_{abBD}\phi_{(b),D}^M\,\d V &\text{with}\quad
  \tilde{k}_{abBD} &= δ_{ab}\,S_{BD} + F_{aA}\,F_{bC}\,\mathbb{C}_{ABCD}.
\end{align*}
Here, $S_{BD}$ and $\mathbb{C}_{ABCD}$ are entries of the second Piola-Kirchhoff stress tensor $\bfS$ and the elasticity tensor $\mathbb{C}$. The computation of these terms uses the description in \cref{sec:stress_and_elasticity}.

The lower left part of the system matrix in \cref{eq:static_newton_iteration} is given by 1 times 3 blocks of submatrices, each with size $N_p \times N_u$ and entries given by:
\begin{align*}
  \bfk_{δp,Δ\bfu,L,(M,a)} = \ds\int_\Omega J\,\psi^L\,(F^{-1})_{Ba}\,\phi_{(a),B}^M \,\d V.
\end{align*}
The upper right part equals the transposed lower left block such that the system matrix is symmetric. Solving the system in \cref{eq:static_newton_iteration} in every iteration of the Newton scheme in \cref{eq:newton_scheme} converges to the solution of the static solid mechanics problem.

\subsection{Derivation and Solution of the Dynamic Hyperelastic Finite Element Model}\label{sec:solver_dynamic_hyperelasticity_fe_model}
% dynamic hyperelasticity (6.9.2)
Based on the static formulation that was described in \cref{sec:static_hyperelastic_fe_model,sec:solver_static_hyperelastic_fe_model}, we now formulate a dynamic model that takes into account the inertia of the contracting muscle.

We add an unknown velocity function $\bfv: \Omega_t \to \R^3$ to the formulation. The additional equation $\dot{\bfu} = \bfv$ relates the displacements and the velocity. Moreover, a new inertial body force $\bfB_\text{a} = \rho_0\,\dot{\bfv}$ is added.

The time derivatives are discretized to timesteps $t=i\cdot \dt$ with an implicit Euler scheme:
\begin{align*}
  \dot{\bfu} &\leadsto \dfrac1{\dt}(\bfu^{(i+1)} - \bfu^{(i)}), & \dot{\bfv} &\leadsto \dfrac1{\dt}(\bfv^{(i+1)} - \bfv^{(i)}).
\end{align*}
%

Because of the added inertial body force, the external virtual work now depends on the vector of unknowns.
In consequence, we split the external virtual work $δW_\text{ext}$ into a dead part $δW_\text{ext,dead}$ that solely depends on external forces and an inertial part:%
\begin{align*}
  δW_\text{ext} = δW_\text{ext,dead} + \ds\int_{\Omega} \rho_0\,\dfrac{v^{(i+1),L}_{(a)} - v^{(i),L}_{(a)}}{dt}\,\phi_{(a)}^L\, \phi_{(a)}^M\,δ\hat{u}^M_a \,\d V = 0.
\end{align*}
In summary, the system of equations to proceed from timestep $i$ to $(i+1)$ is given as:
\begin{subequations}\label{eq:mechanics_dynamic}
  \begin{align}
    δW_\text{int}({\bfu^{(i+1)}},p^{(i+1)}) - δW_\text{ext}(\bfv^{(i)},\bfv^{(i+1)}) &= 0 \qquad &&\forall\,δ\bfu,\label{eq:mechanics_dynamic1}\\[4mm]
    \dfrac1{\dt}(\bfu^{(i+1)} - \bfu^{(i)}) - \bfv^{(i+1)} &= 0,\label{eq:mechanics_dynamic2}\\[4mm]
    D_{δp}\Pi_L(\bfu^{(i+1)}) &= 0 \qquad &&\forall\,δp.\label{eq:mechanics_dynamic3}
  \end{align}
\end{subequations}
Here, \cref{eq:mechanics_dynamic1} is the principle of virtual work, \cref{eq:mechanics_dynamic2} relates displacements $\bfu$ and velocities $\bfv$ and \cref{eq:mechanics_dynamic3} is the incompressibility constraint.

The system is again solved using the Newton scheme presented in \cref{sec:solver_static_hyperelastic_fe_model}.
The linear system for each Newton iteration takes the following form:
\begin{align*}
  \matt{
    \bfk_{δ\bfu,Δ\bfu} & \bfl_{δ\bfu,Δ\bfv} & \bfk_{δp,Δ\bfu}^\top \\[2mm]
    \bfl_{δ\bfv,Δ\bfu} & \bfl_{δ\bfv,Δ\bfv} & \bfzero \\[2mm]
    \bfk_{δp,Δ\bfu} & \bfzero & \bfzero
  } \,
  \matt{Δ\hat{\bfu} \\[2mm] Δ\hat{\bfv} \\[2mm] Δ\hat{p}}
  =
  \matt{-\bfR_{δ\bfu} \\[2mm] -\bfR_{δ\bfv} \\[2mm] -\bfR_{δp}}.
\end{align*}
The entries $\bfk_{δ\bfu,Δ\bfu}$ and $\bfk_{δp,Δ\bfu}$ are the same as in the static case in \cref{eq:static_newton_iteration}.
The other non-zero entries are given by
\begin{align*}
  \bfl_{δ\bfu,Δ\bfv,(L,a),(M,b)} &= \dfrac1{\dt}\delta_{ab} \ds\int_{\Omega} \rho_0\,\,\phi_{(b)}^M \,\phi_{(a)}^L \,\d V, &
  \bfl_{δ\bfv,Δ\bfu,(L,a),(M,b)} &= \dfrac{1}{\dt}\delta_{ab}\,\delta^{LM},\\[4mm]
  \bfl_{δ\bfv,Δ\bfv,(L,a),(M,b)} &= -\delta_{ab}\,\delta^{LM}.
\end{align*}

Note that in the dynamic problem, the system matrix is unsymmetric. It would be symmetric if the entries $\bfl_{δ\bfu,Δ\bfv}$ and $\bfl_{δ\bfv,Δ\bfu}^\top$ were be the same. This would be the case for a density of one, $\rho_0 = 1$, and if the term $\int_{\Omega} \phi_{b}^M \phi_{a}^L \,\d V$ would be replaced by $\delta_{ab}\delta^{LM}$. The second condition means that a lumped mass matrix would be used where the diagonal entries are set to the row sums of the original matrix.

We discretize the Finite Element solution in space by \emph{Taylor-Hood} elements. This type of element uses quadratic ansatz functions $\phi$ for the displacements and velocities and linear ansatz functions $\psi$ for the Lagrange multiplier or hydrostatic pressure $p$ on a 3D hexahedral mesh. This choice was proven to exhibit no locking \cite{zienkiewicz2005finite}. Locking is a phenomenon of degraded convergence of the Finite Element method for solid mechanics problems and occurs for improper discretization schemes.

For a compressible material, the incompressibility constraint which is the last equation in the systems \cref{eq:mechanics_static_system} or \cref{eq:mechanics_dynamic} is removed. Instead of solving for the pressure $p$ as a Lagrange multiplier, the value is given by the constitutive model as described in \cref{sec:material_modeling}. In consequence, the system matrix of the linear system of equations that is solved in the Newton iterations has a smaller size for compressible materials.

Moreover, the size varies depending on whether the static or the dynamic problem given in \cref{sec:solver_static_hyperelastic_fe_model,sec:solver_dynamic_hyperelasticity_fe_model} is solved. Assuming a linear mesh with $N_p$ degrees of freedom and a quadratic mesh with $N_u$ degrees of freedom, the square system matrix has $3\,N_u$ rows and columns for a static compressible formulation, $3\,N_u + N_p$ for a static incompressible formulation, $6\,N_u$ for a dynamic compressible model, and $6\,N_u+N_p$ for a dynamic incompressible model.

% static compressible:    3*N_u
% static incompressible:  3*N_u + N_p
% dynamic compressible:   3*N_u + 3*N_u
% dynamic incompressible: 3*N_u + 3*N_u + N_p

In any case, the mechanics model can be linked to the subcellular model by defining the active stress as given in \cref{eq:active_stress_term}. Since the active stress does not depend directly on the passive behavior, the active stress term can be added as a constant to the passive stress term. This constant also has no influence on the jacobian matrix $\bfJ$. As the subcellular model depends on the fiber stretch $\lambda_f = \sqrt{I_4}$, there is a feedback loop between the subcellular and the solid mechanics model.

Details on the connection to the subcellular model as well as details on the numeric solution schemes for the nonlinear Newton solver and the linear solvers in every Newton iteration, including the solver schemes, how initial values are chosen and measures to speed up convergence such as load stepping are discussed in the implementation and result sections.

%To speed up the computation, the initial guess of the vector of unknowns in every timestep is linearly extrapolated from the two previous timesteps.

\section{Model Order Reduction}
Considering the total reduction, we get the system to be solved for the fully reduced state $\tilde{\mathbf{z}}$:
\begin{equation*}
  \begin{array}{l}
	\tilde{\mathbf{z}}^{*} =
	\tilde{\mathbf{z}}^{(t)}+
	\mathbf{V}_k^{\text{T}} \textbf{FG}(\mathbf{v}_\mathrm{m}^{(t)}, \mathbf{y}^{(t)})
	\label{e:POD_full_a},\\[0.5em]
	\tilde{\mathbf{z}}^{(t+1)} = \tilde{\mathbf{z}}^{*}+\mathbf{V}_k^{\text{T}}
	\textbf{A}_\mathrm{enh} \mathbf{V}_k  \tilde{\mathbf{z}}^{(t+1)},
  \end{array}
\end{equation*}
where $\textbf{FG}(\mathbf{v}_\mathrm{m}, \mathbf{y})$ and $ \textbf{A}_\mathrm{enh}$
as well as the full state recovery are defined by
\begin{equation*}
  \textbf{FG}(\mathbf{v}_\mathrm{m}, \mathbf{y}) := \!  \,\left[\begin{array}{c}
  	\textbf{F}_1(\mathbf{v}_\mathrm{m},\mathbf{y})\\
  	\textbf{F}_2(\mathbf{v}_\mathrm{m},\mathbf{y})\\
  	\vdots \\
  	\textbf{F}_n(\mathbf{v}_\mathrm{m},\mathbf{y})\\
  	\textbf{G}_1(\mathbf{v}_\mathrm{m},\mathbf{y})\\
  	\textbf{G}_2(\mathbf{v}_\mathrm{m},\mathbf{y})\\
  	\vdots \\
  	\textbf{G}_n(\mathbf{v}_\mathrm{m},\mathbf{y})
  \end{array}\right],\,
  \textbf{A}_\mathrm{enh} := \left[ \begin{array}{c}
  	\textbf{A}_1\\
  	\textbf{A}_2\\
  	\textbf{A}_n\\
  	0 \\
  	\vdots \\
  	0
  \end{array} \right], \,
  \mathbf{V}_k  \tilde{\mathbf{z}}=\left[ \begin{array}{c}
  	\mathbf{v}_{\mathrm{m},1} \\
  	\mathbf{v}_{\mathrm{m},2} \\
  	\vdots \\
  	\mathbf{v}_{\mathrm{m},n} \\
  	\mathbf{y}_1 \\
  	\mathbf{y}_2 \\
  	\vdots \\
    \mathbf{y}_n \\\end{array} \right].
\end{equation*}
%------------------------------------------------------------------------------------------------
\section{Quadrature}

The aim is to compute a good approximation for the integral
%
\begin{equation*}
  \begin{array}{lll}
    I := \i{0}{1}f(\xi)\,\d \xi
  \end{array}
\end{equation*}
with a low number of function evaluations $f(\xi)$.
\subsection{Gaussian quadrature}
The Gaussian quadrature rule approximates the integral by
%
\begin{equation*}
  \begin{array}{lll}
    I \approx \s{i=1}{n}f(\xi_i)\,w_i
  \end{array}
\end{equation*}
with appropriate \emph{Gauss points} $\xi_i$ and weights $w_i, i=1,\dots,n$. The sampling points and weights are chosen such that the rule approximates polynomials of degree $p_\text{exact}=2\,n-1$ exactly. Some values are listed below.

\begin{table}[ht]
\centering
\begin{tabular}{c|c|c|c}
    $n$& $\xi_i$& $w_i$ & $p_\text{exact}$\\[4mm]
    \hline&&\\[-4mm]
    $1$& $\dfrac12$ & $1$ & $1$\\[4mm]
    \hline
    $2$& $\pm \dfrac{1+\sqrt{3}}{2\sqrt{3}}$ & $\dfrac12$ & $3$\\[4mm]
    \hline
    $3$& $\dfrac12$ & $\dfrac49$ & $5$ \\[4mm]
     & $\pm \dfrac{\sqrt{3}+\sqrt{5}}{2\sqrt{5}}$ & $\dfrac{5}{18}$ &
\end{tabular}
\caption{Gauss points and weights}
\end{table}

Note: Literature on Gauss quadrature often describes the case of an integral $\int_{-1}^{1} f(x) \,\d x$. The transformation is given by:
%
\begin{equation*}
  \begin{array}{lll}
    \i{0}{1} f(\xi) \,\d \xi = \dfrac12 \i{-1}{1} f\big(1/2+x/2\big) \,\d x.
  \end{array}
\end{equation*}

\subsection{Clenshaw-Curtis quadrature}
Clenshaw-Curtis quadrature approximates the function by a number of Chebyshev polynomials for which the exact integral is known.
The function $f$ is evaluated at the $n+1$ roots of the Chebyshev polynomial $T_{n+1}$, which are:
\begin{equation*}
  \begin{array}{lll}
    \xi_i = \cos(i\pi/n), \quad i = 0,\dots,n, \,n \text{ even}.
  \end{array}
\end{equation*}
The function can be written as Chebyshev series
%
\begin{equation*}
  \begin{array}{lll}
    f(\xi) = \dfrac{a_0}{2} T_0(\xi) + \s{i=1}{∞} a_i\,T_i(x)
  \end{array}
\end{equation*}
where the coefficients result from discrete cosine transform. The approximated integral is computed by
\begin{equation*}
  \begin{array}{lll}
    I \approx a_0 + \s{i=1}{n/2-1} \dfrac{2\,a_{2\,i}}{1-(2\,i)^2} + \dfrac{a_n}{1-n^2}.
  \end{array}
\end{equation*}
The coefficients are
%
\begin{equation*}
  \begin{array}{lll}
    a_{2\,i} = \dfrac{2}{n}\Bigg(\dfrac{f(1)+f(-1)}{2} + f(0)\,(-1)^i + \s{i=1}{n/2-1}\big(f(\xi_i) + f(-\xi_i)\big)\cos(k/2\,\xi_i)\Bigg)
  \end{array}
\end{equation*}

This quadrature rule approximates polynomials with degree $p_\text{exact} = m-1$ exactly when using $m$ sampling points. However for some non-polynomial functions its accuracy may be better than the respective Gauss quadrature.

%------------------------------------------------------------------------------------------------
\section{Propositions}
In this section some propositions are collected such that they can be referenced when needed.

\subsection{Divergence theorem}
\textit{Also called Gauss's theorem.}
Let $U \subset \R^d$ be a compact set with a piecewise smooth boundary $\p U$, $\bfF: U \to \R^d$ a continuously differentiable vector field. Then:
\begin{equation}\label{eq:gauss}
  \begin{array}{ll}
    \ds\int_U ∇\cdot\bfF(\bfx) \,\d \bfx = \ds\int_{\p U} \bfF(\bfx)\cdot \bfn\,\d \bfx.
  \end{array}
\end{equation}
For $d=2$ one gets \emph{Stoke's theorem}.

\subsubsection{Corollary}
Replacing $\bfF$ of \eqref{eq:gauss} by ${f\,\bfF}$ yields the following proposition:

For a differentable function $f: U \to \R$ and a vector field $\bfF: U \to \R^d$ the following holds:
\begin{equation}\label{eq:gauss1}
  \begin{array}{ll}
     \ds\int_U f(∇\cdot\bfF) \,\d \bfx = \ds\int_{\p U} (f\,\bfF)\cdot\bfn\,\d \bfx -\ds\int_U \bfF \cdot ∇f \,\d \bfx
  \end{array}
\end{equation}
Now set $\bfF\equiv (1,0,\dots), (0,1,\dots), \dots$ to get the following vector-valued identity:

For a differentable function $f: U \to \R$ the following holds:
\begin{equation}
  \begin{array}{ll}
    \ds\int_U ∇f(\bfx) \,\d \bfx = \ds\int_{\p U} f(\bfx)\,\bfn\,\d \bfx
  \end{array}
\end{equation}

\subsection{Classical Stoke's theorem}
Let $U\subset \R^3$ be an open set, $V$ a 2-manifold in $U$ with boundary $\p V$ and $\bfF: U \to \R^3$ a continuously differentiable vector field. Then:
\begin{equation}
  \begin{array}{ll}
    \ds\varointctrclockwise_{\p V} \bfF\cdot\d s = \ds\int_{V} \big(∇\times \bfF\big) \cdot \bfn \,\d\bfx,
  \end{array}
\end{equation}
where $\bfn$ is the normal on the surface $V$.

\subsection{Integration on manifolds}
In the following it is outlined how to integrate on 1D and 2D domains that are embedded in $\R^d$. The formalism of manifolds is omitted for simplicity.

\subsubsection{1D curve integrals}
Let $U\subset \R$ be an open set (the parameter space) and $\Phi:U \to \R^d$ a smooth mapping that defines a curve $\Omega=\Phi(U)$ embedded in $\R^d$. An integrable function $g:\Omega \to \R$ can then be integrated as follows:
%
\begin{equation}\label{eq:integration_transformation_1d}
  \begin{array}{ll}
    \ds\int_{\Phi(U)} g(\bfx) \,\d\bfx = \int_{U} g\big(\Phi(\xi)\big)\,\Vert \Phi'(\xi)\Vert_2 \, \d \xi
  \end{array}
\end{equation}

\subsubsection{2D surface integrals}
Let $U \subset \R^2$ be an open set (parameter space), $\Phi:U \to \Phi(U)=:\Omega \subset\R^3$ a diffeomorphism, $\Phi$ maps parameters $\bfxi=(\xi_1,\xi_2) \in U$ to points in world space $\bfx \in \Omega$. The inverse map $\Phi^{-1} : \Omega \subset \R^3 \to \R^2$ assigns coordinates $(\xi_1,\xi_2)$ to each point $\bfx\in\Omega$. We name $\Phi^{-1}(\bfx) = (x(\bfx),y(\bfx))$ in the following formula. The integration of a 2-dimensional function $g:\Omega \to \R$ is performed as follows.
%
\begin{equation}\label{eq:integration_transformation_2d}
  \begin{array}{ll}
    \ds\int_{\Phi(U)} g(\bfx) \,\d\bfx
    &  = \ds\int_{U} g\big(\Phi(\bfxi)\big)
    \sqrt{\det \big(J_{\Phi}(\bfxi)^\top J_{\Phi}(\bfxi)\big)}\,\d\bfxi\\[4mm]

    & = \ds\int_{U} g\big(\Phi(\bfxi)\big)
    \sqrt{\det\mat{\d{\Phi}{\xi_1} \cdot \d{\Phi}{\xi_1} & \d{\Phi}{\xi_1} \cdot \d{\Phi}{\xi_2}  \\[4mm]
    \d{\Phi}{\xi_1} \cdot \d{\Phi}{\xi_2} & \d{\Phi}{\xi_2} \cdot \d{\Phi}{\xi_2}}}\,\d\bfxi\\[4mm]

     & = \ds\int_{U} g\big(\Phi(\bfxi)\big)
    \sqrt{\Big\Vert \d{\Phi}{\xi_1}\Big\Vert_2^2\,\Big\Vert \d{\Phi}{\xi_2}\Big\Vert_2^2 - \Big(\d{\Phi}{\xi_1} \cdot \d{\Phi}{\xi_2}\Big)^2 }\,\d\bfxi\\[4mm]

  \end{array}
\end{equation}

\subsubsection{Substitution on domains with same dimensionality}
\emph{Integration by substitution}, \textit{German \say{Transformationssatz}}, also \emph{change of variables rule}.
Let $U \subset \R^d$ be an open set, $\Phi:U \to \Phi(U) \subset\R^d$ a diffeomorphism ($\Phi$ bijective and continuously differentiable, inverse map $\Phi^{-1}$ also continuously differentiable).

Then $g:\Phi(U) \to \R$ is integrable on $\Phi(U)$ if and only if the function $\bfxi \mapsto g(\Phi(\bfxi))\,|\det(J_{\Phi}(\bfxi))|$ is integrable on $U$. The following holds:
\begin{equation}\label{eq:integration_transformation_3d}
  \begin{array}{ll}
    \ds\int_{\Phi(U)} g(\bfx)\,\d \bfx = \ds\int_U g(\Phi(\bfxi))\,|\det(J_{\Phi}(\bfxi))|\,\d \bfxi,
  \end{array}
\end{equation}
where $J_{\Phi}$ is the Jacobian of $\Phi$.

\subsubsection{Summary}
The transformation rules \cref{eq:integration_transformation_1d,eq:integration_transformation_2d,eq:integration_transformation_3d} can be summarized in a unified form as follows.

Let $U \subset \R^d, d\in\{1,2,3\}$ be an open set (parameter space), $\Phi:U \to \Phi(U)=:\Omega \subset\R^d$ a diffeomorphism that maps parameters $\bfxi \in U$ to points in world space $\bfx \in \Omega$. A function defined in parameter space, $f:U\to \R$, can then be integrated as follows in world space.
%
\begin{equation}\label{eq:integration_transformation_dd}
  \begin{array}{lll}
    \ds\int_{\Phi(U)} f\big(\Phi^{-1}(\bfx)\big)\,\d \bfx = \ds\int_U f(\bfxi)\,\mathcal{J}_d(\bfxi)\,\d \bfxi,
  \end{array}
\end{equation}
where the definition of $\mathcal{J}_d(\bfxi)$  depends on the dimension $d$ as follows:
%
\begin{equation*}
  \begin{array}{rll}
    \mathcal{J}_1(\xi) &= \Vert \Phi'(\xi) \Vert_2 &\quad \text{for }d=1, \bfxi=\xi \in U \subset \R,\\[4mm]
    \mathcal{J}_2(\bfxi) &= \sqrt{\det \big(J_{\Phi}(\bfxi)^\top J_{\Phi}(\bfxi)\big)} &\quad \text{for }d=2, \bfxi\in U \subset \R^2, \phi^{-1}(\bfx) =: \big(x(\bfx), y(\bfx)\big),\\[4mm]
    \mathcal{J}_3(\bfxi) &= |\det (J_{\Phi}\big(\bfxi)\big)| &\quad \text{for }d=3, \bfxi\in U \subset \R^3.
  \end{array}
\end{equation*}

% -------------- Literaturseite --------------------
\newpage
\nocite{*}
\bibliography{literatur}{}
\bibliographystyle{abbrv}

% -------------- Anhang ------------
%\appendix
%\input{8_anhang.tex}

\end{document}