<i>
This program was contributed by Vladimir Yushutin and Timo Heister, Clemson University, 2023.

This material is based upon work partly supported by the National
Science Foundation Award DMS-2028346, OAC-2015848, EAR-1925575, and by the Computational
Infrastructure in Geodynamics initiative (CIG), through the NSF under Award
EAR-0949446, EAR-1550901, EAR-2149126 via the University of California -- Davis.
</i>

<a name="step-90-Intro"></a>
<h1>Introduction</h1>

In this tutorial, we implement the trace finite element method (TraceFEM) in deal.II. TraceFEM solves PDEs posed on a
possibly evolving $(dim-1)$-dimensional surface $\Gamma$ employing a fixed uniform background mesh of a $dim$-dimensional domain
in which the surface is embedded. Such surface PDEs arise in problems involving material films with complex
properties and in other situations in which a non-trivial condition is imposed on either a stationary or a moving interface.
Here we consider a steady, complex, non-trivial surface and the prototypical Laplace-Beltrami equation which is a counterpart of
the Poisson problem on flat domains.

Being an unfitted method, TraceFEM allows to circumvent the need of remeshing of an evolving surface if it is implicitly
given by the zero contour of a level-set function. At the same time, it easily provides with an extension of the
discrete solution to a neighborhood of the surface which turns out to be very handy in case of non-stationary interfaces and films.
Certainly, this flexibility comes with a price: one needs to design the nodes and weights for a quadrature customized
for each implicit intersection of the zero level-set and the background mesh. Moreover, these intersections may be of
arbitrary shape and size manifesting in the so-called "small cut" problem and requiring addition of a stabilization
form that restores well-conditioning of the problem.

Two aspects are of our focus. First, the surface approximation is separated from the discretization of the surface PDE,
e.g., a $Q_2$ discrete level-set and a $Q_1$ solution are possible on the same bulk triangulation.
Second, we make sure that the performance of TraceFEM in the parallel implementation corresponds to that of a classical
fitted FEM for a two-dimensional problem. We demonstrate how to achieve both goals by using a combination of MeshWorker
and NonMatching capabilities.

A natural alternative to TraceFEM in solving surface PDEs is the parametric surface finite element method. The latter
method relies on an explicit parametrization of the surface which may be not feasible especially for evolving interfaces
with an unknown in advance shape - in this sense, TraceFEM is a technique inspired by the level-set description of
interfaces. However, the parametric surface finite element method, when applicable, enjoys many well-known properties
of fitted methods on flat domains provided the geometric errors - which a present for both methods - are taken under control.


<h3>A non-trivial surface</h3>
A fitted FEM on a flat two-dimensional domain, if discretized by piecewise linears with $N$ degrees of freedom, typically results in
$O(h)=O(N^{-1/2})$ convergence rate of the energy error; requires $O(N)$ storage for the degrees of freedom; and,
more importantly, takes $O(N)$ of construction time to create them, i.e. to mesh the domain.  TraceFEM,
although solving a two-dimensional problem, relies on the inherently three-dimensional mesh on which the level-set
function must be defined and, if implemented naively, suffers from the increased storage and the increased construction
time in terms of the active degrees of freedom $N_a$ that actually
enters the scheme with, hopefully, $O(N_a^{-1/2})$ error. To combat these possible bottlenecks, we create iteratively
a mesh which is localized near the zero contour line of the level set function, i.e near the surface, to restore the aforementioned
two-dimensional performance typical for fitted FEM, see the first three typical iterations of this methodology below.

@image html step-90_prelim.png "Iterative localization of the zero contour of a typical level set" width=60%

The cells colored by red cary the active degrees of freedom (total number $N_a$) as the level set is not sign-definite
at support points. Notice also that the mesh is graded: any cell has at most 4 neighbors adjacent to each of 6 faces.

Once a desired geometry approximation $\Gamma_h$ is achieved using the iterative approach above, we can start forming the linear system
using the constructed normals and quadratures. For the purposes of the tutorial we choose a non-trivial surface $\Gamma$ given by
@f{equation*}{
  \frac{x^2}{4}+ y^2 + \frac{4  z^2} {(1 + 0.5  \sin(\pi  x))^{2}} = 1
@f}
The OY and OX views of this tamarind-shaped, exact surface $\Gamma$ are shown below along with the mesh after
three iterations (the approximation $\Gamma_h$ is not shown).

@image html step-90_surface.png "OY(left) and OZ(right) cross-sections of the background mesh along with the exact surface"  width=80%


<h3>Model problem</h3>

We would like to solve the simplest possible problem  defined on a surface, namely the Laplace--Beltrami equation,
@f{equation*}{
 -\Delta_\Gamma u + c u = f \qquad  \text{in }\, \Gamma,
@f}
where we take $c=1$ for concreteness. We added the term $cu$ to the left-hand side so the problem becomes well-posed
in the absence of any boundary; an alternative could be to take $c=0$ but impose the zero mean condition.

<h3>Manufactured exact solution</h3>
We choose the test solution and the right-hand side forcing
 as the restriction to $\Gamma$ of
@f{equation*}{
 u(x,y,z)=xy\,,\quad
 f(x,y,z)=xy + 2.0\,\mathbf{n}_x \mathbf{n}_y + \kappa  (y \mathbf{n}_x + x\mathbf{n}_y),
@f}
where the latter is manufactured using the exact normal $\mathbf{n}$, the exact Hessian $\nabla^2\mathbf{n}$ and the mean curvature,
$\kappa=\mathrm{div} n$ of the surface. Note that we do not need to impose any boundary conditions as the surface $\Gamma$ is closed.

<h3>The Trace Finite Element Method</h3>
TraceFEM is an unfitted method: the surface $\Gamma$  is immersed into a regular, uniform background mesh that
stays fixed even if the surface would be evolving.
To solve Laplace--Beltrami equation, we first construct a surface approximation $\Gamma_h$ by intersecting implicitly
the cells of the background mesh with the iso surface of an approximation of the level-set field. We note that we
never actually create any two-dimensional meshes for the surface but only compute approximate quadrature points and surface normals.
Next we distribute degrees of freedom over a thin subdomain $\Omega_h$
that completely covers $\Gamma_h$ and that consists of the intersected cells $\mathcal{T}_\Gamma^h$,
@f{equation*}{
 \mathcal{T}_\Gamma^h = \{ T \in \mathcal{T}^{h} : T \cap \Gamma_h \neq \emptyset \}.
@f}
The finite element space where we want to find our numerical solution, $u_h$, is now
@f{equation*}{
 V_h = \{ v \in C(\Omega_h) : v \in Q_p(T), \, T \in \mathcal{T}_\Gamma^h \},
@f}
where $\Omega_h$ is the union of all intersected cells from $\bigcup_{T \in \mathcal{T}_\Gamma^h} \overline{T}$.

To create $V_h$, we first add an FE_Q and an
FE_Nothing element to an hp::FECollection. We then iterate over each cell
$T$ and, depending on whether $T$ belongs to $\mathcal{T}_\Gamma^h$ or not,
we set the active_fe_index to either 0 or 1.
To determine whether a cell is intersected or not, we use the class NonMatching::MeshClassifier.

A natural candidate for a weak formulation  involves the following (bi)linear forms
@f{align*}{
 a_h(u_h, v_h) =  (\nabla_{\Gamma_h} u_h, \nabla_{\Gamma_h} v_h)_{\Gamma_h}+(u_h, v_h)_{\Gamma_h}\,,\qquad
 L_h(v_h)      =  (f^e,v_h)_{\Gamma_h}.
@f}
where $f^e$ is an extension (non-necessarily the the so-called normal extension) of $f$ from $\Gamma$ to $\Omega_h$. Note that the right-hand side $f$ of the Laplace-Beltrami
problem is defined on the exact surface $\Gamma$ only and we need to specify how to evaluate its action on the perturbed
approximate geometry $\Gamma_h$ which is immersed in $\Omega_h$. For the purposes of this test, the forcing $f$ is
manufactured using $u=xy$ and the level-set function and, therefore, is a function of Cartesian coordinates $x$,$y$,$z$.
The latter is identified with $f^e$ on $\Gamma_h$ and it is not the normal extension of the function $f$.

However, the so-called "small-cut problem" may arise and one should
introduce the stabilized version of TraceFEM: Find $u_h \in V_h$ such that
@f{equation*}{
 a_h(u_h,v_h) + s_h(u_h, v_h) = L_h(v_h), \quad \forall v_h \in V_\Omega^h.
@f}
Here the normal-gradient stabilization $s_h$ involves the three-dimensional integration over whole (but intersected) cells and is given by
@f{equation*}{
 s_h(u_h,v_h) = h^{-1}(\mathbf{n}_h\cdot\nabla u_h, \mathbf{n}_h\cdot\nabla v_h)_{\Omega_h},
@f}
Note that the $h^{-1}$ scaling may be relaxed for sufficiently smooth solutions such as the manufactured one, but we
choose the strong scaling to demonstrate the extreme case @cite traceFEM_review_2017.

<h3>Discrete Level Set Function</h3>
In TraceFEM we construct the approximation $\Gamma_h$ using the interpolant $\psi_h$ of the exact level-set function on the bulk triangulation:
@f{align*}{
 \Gamma_h &= \{x \in \mathbb{R}^{\text{3}} : \psi_h(x) = 0 \}.
@f}
The exact normal vector $\mathbf{n}$ is approximated by $\mathbf{n}_h=\nabla\psi_h/\|\nabla\psi_h\|$ which, together
with approximate quadrature for the integration over $\Gamma_h$,  leads to the so-called "geometrical error".
Luckily, one can show @cite traceFEM_review_2017 that the method converges optimally for the model problem
if the same element space $V_h$ is employed for the discrete functions and  for the interpolation of  the level set
function as if the exact domain would have been used. Furthermore, deal.II allows to choose independently the discrete
space for the solution and a higher-order discrete space for the level set function for a more accurate geometric approximation.
