\documentclass[11pt]{beamer}
\usetheme{Madrid}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{svg}

\usepackage{tikz-cd}

\author[\texttt{sebastiano.tronto@uni.lu}]{Sebastiano Tronto}
\title{Mathematical Software - Introduction}
\logo{\includegraphics[scale=0.1]{img/unilu.jpg}}
%\institute{University of Luxembourg}

\date{2021-02-19}

\begin{document}

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

\begin{frame}
  \tableofcontents
\end{frame}

\section{What?}
\begin{frame}{What}
  \begin{columns}
    \column{0.6\textwidth}
    \begin{itemize}
      \item {\bf Latex} for writing scientific text
      \item {\bf Sage} for computations
    \end{itemize}

    \column{0.4\textwidth}
    \includegraphics[scale=0.1]{img/latex.png}
    \vspace{0.5cm}

    \includesvg[scale=0.215]{img/sage}
  \end{columns}
\end{frame}

\subsection{Latex}
\begin{frame}{Latex}
  \includegraphics[scale=0.1]{img/latex.png}
  \begin{itemize}
    \item A typesetting system, has been around since the 1980s
    \item Currently the best way to write scientific texts (Math, Physics...)
    \item Can be used for lecture notes, homework, articles, books, graphics
          (with TikZ), this presentation...%, shopping list...
    \item ``What you get is what you \emph{mean}'', rather than what you
          \emph{see}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Latex - Example}
  An equation like this:
  \begin{align*}
    e^x =& \sum_{n=0}^{\infty} \frac{x^n}{n!} \\
        =& 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots
  \end{align*}
  is written in LaTeX as:
  \begin{verbatim}
    \begin{align*}
      e^x =& \sum_{n=0}^{\infty} \frac{x^n}{n!} \\
          =& 1 + x + \frac{x^2}{2} +
             \frac{x^3}{6} + \cdots
    \end{align*}
  \end{verbatim}
\end{frame}

\subsection{Sage}
\begin{frame}{Sage}
  \includesvg[scale=0.215]{img/sage}
  \begin{itemize}
    \item Free and open source Mathematical software
    \item Basically python with a lot of Math libraries
    \item Builds up on existing software such a Pari/GP, NumPy, R...
    \item Popular for Computational Algebra (Number Theory, Cryptography...)
  \end{itemize}
\end{frame}

\section{When?}
\begin{frame}{When}
  \begin{tabular}{r|c|c}
    %Date & Time & Topics \\
    %\hline
    February 19 & \texttt{14:00 - 17:30} & Introduction, LaTeX fundamentals  \\
    \hline
    March 12    & \texttt{14:00 - 17:30} & More advanced LaTeX topics        \\
    \hline
    March 26    & \texttt{14:00 - 17:30} & LaTeX: presentations, graphics    \\
    \hline
    April 2     & \texttt{14:00 - 17:30} & ??? (LaTeX or Sage)               \\
    \hline
    April 23    & \texttt{14:00 - 17:30} & Sage (???)                        \\
    \hline
    May 7       & \texttt{14:00 - {\bf 18:15}} & Sage (???)                  \\
    \hline
    May 21      & \texttt{14:00 - {\bf 18:15}} & Sage (???)
  \end{tabular}
\end{frame}

\section{How?}
\begin{frame}{How}
  \begin{itemize}
    \item \textbf{Remote teaching} at least until April 2 included (probably
          always).
    \item \textbf{Learn by doing} graded homework (4-5 assignments), non-graded
          exercises, free practice. No final exam.
  \end{itemize}
\end{frame}

\end{document}


\usepackage{lipsum}
\usepackage{amsmath,amssymb,amsthm,amsfonts,dsfont,mathabx}             % AMS Math
\usepackage{cancel}
\usepackage{cite}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
%\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
%\usepackage{ulem}
%\usepackage[counterclockwise]{rotating}
\usepackage[left=4.0cm,right=2.5cm,top=2.2cm,bottom=2.2cm,includefoot,includehead,headheight=13.6pt]{geometry}
\renewcommand{\baselinestretch}{1.05}
%\usepackage{datetime}
% Table of contents for each chapter

\usepackage[nottoc, notlof, notlot]{tocbibind}
\usepackage[english]{minitoc}
\setcounter{minitocdepth}{2}
\mtcindent=15pt
% Use \minitoc where to put a table of contents

\usepackage{aecompl}

% Glossary / list of abbreviations
\usepackage{multicol}
\usepackage[intoc]{nomencl}
\renewcommand{\nomname}{Glossary of terms, Abbreviations, \\ Acronyms and Symbols}
\makenomenclature
\makenomenclature
\setlength{\nomitemsep}{-\parsep}
\setlength{\nomitemsep}{0.05pt}

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

% Links in pdf
\usepackage{xcolor}
\definecolor{linkcol}{rgb}{0,0,0.4}
\definecolor{citecol}{rgb}{0.5,0,0}

% definitions.
% -------------------

\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}

% Some useful commands and shortcut for maths:  partial derivative and stuff

\newcommand{\uddu}{\uparrow\downarrow\downarrow\uparrow}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\vect}[1]{\displaystyle\overrightarrow{#1}}
\newcommand{\ket}[1]{\displaystyle\vert #1\rangle}
\def\abs{\operatorname{abs}}
% \def\argmax{\operatornamewithlimits{arg\,max}}
% \def\argmin{\operatornamewithlimits{arg\,min}}
\def\diag{\operatorname{Diag}}
\newcommand{\eqRef}[1]{(\ref{#1})}

\usepackage{rotating}                    % Sideways of figures & tables
%\usepackage{bibunits}
%\usepackage[sectionbib]{chapterbib}          % Cross-reference package (Natural BiB)
%\usepackage{natbib}                  % Put References at the end of each chapter
                                         % Do not put 'sectionbib' option here.
                                         % Sectionbib option in 'natbib' will do.
\usepackage{fancyhdr}                    % Fancy Header and Footer

% \usepackage{txfonts}                     % Public Times New Roman text & math font

%%% Fancy Header %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fancy Header Style Options

\pagestyle{fancy}                       % Sets fancy header and footer
\fancyfoot{}                            % Delete current footer settings

%\renewcommand{\chaptermark}[1]{         % Lower Case Chapter marker style
%  \markboth{\chaptername\ \thechapter.\ #1}}{}} %

%\renewcommand{\sectionmark}[1]{         % Lower case Section marker style
%  \markright{\thesection.\ #1}}         %

\fancyhead[LE,RO]{\bfseries\thepage}    % Page number (boldface) in left on even
% pages and right on odd pages
\fancyhead[RE]{\bfseries\nouppercase{\leftmark}}      % Chapter in the right on even pages
\fancyhead[LO]{\bfseries\nouppercase{\rightmark}}     % Section in the left on odd pages

\let\headruleORIG\headrule
\renewcommand{\headrule}{\color{black} \headruleORIG}
\renewcommand{\headrulewidth}{1.0pt}
\usepackage{colortbl}
\arrayrulecolor{black}

\fancypagestyle{plain}{
  \fancyhead{}
  \fancyfoot{}
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
}

% \usepackage{MyAlgorithm}
% \usepackage[noend]{MyAlgorithmic}

%%% Clear Header %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clear Header Style on the Last Empty Odd pages
\makeatletter

\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else%
  \hbox{}%
  \thispagestyle{empty}%              % Empty header styles
  \newpage%
  \if@twocolumn\hbox{}\newpage\fi\fi\fi}

\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prints your review date and 'Draft Version' (From Josullvn, CS, CMU)
\newcommand{\reviewtimetoday}[2]{\special{!userdict begin
    /bop-hook{gsave 20 710 translate 45 rotate 0.8 setgray
      /Times-Roman findfont 12 scalefont setfont 0 0   moveto (#1) show
      0 -12 moveto (#2) show grestore}def end}}
% You can turn on or off this option.
\reviewtimetoday{\today}{Draft Version}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newenvironment{maxime}[1]
{
\vspace*{0cm}
\hfill
\begin{minipage}{0.5\textwidth}%
%\rule[0.5ex]{\textwidth}{0.1mm}\\%
\hrulefill $\:$ {\bf #1}\\
%\vspace*{-0.25cm}
\it
}%
{%

\hrulefill
\vspace*{0.5cm}%
\end{minipage}
}

\let\minitocORIG\minitoc
\renewcommand{\minitoc}{\minitocORIG \vspace{1.5em}}

\usepackage{multirow}
\usepackage{diagbox}

\newenvironment{bulletList}%
{ \begin{list}%
	{$\bullet$}%
	{\setlength{\labelwidth}{25pt}%
	 \setlength{\leftmargin}{30pt}%
	 \setlength{\itemsep}{\parsep}}}%
{ \end{list} }

\newtheorem{definition}{Definition}
\renewcommand{\epsilon}{\varepsilon}

% centered page environment

\newenvironment{vcenterpage}
{\newpage\vspace*{\fill}\thispagestyle{empty}\renewcommand{\headrulewidth}{0pt}}
{\vspace*{\fill}}

% Change this to change the informations included in the pdf file

\usepackage[palatino]{quotchap}
\usepackage{tikz}
\usepackage{adjustbox} % scaling pictures
\usepackage{afterpage} % allows for the rotation

% \usepackage{fontspec}

\usepackage{setspace}

% %%\usepackage[pdftex]{graphicx}
\usepackage{subfig} %for subfloat images
\usepackage{stmaryrd}

\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage[titletoc]{appendix}

% \usepackage{floatpag} % so that with sideway figures i can remove the page number
\newcommand{\acknowledgments}{
  \chapter*{Acknowledgments}
  \noindent

      \input{frontmatter/thanks}

  \vspace*{\fill} \newpage
  \setcounter{page}{1}
  \pagenumbering{arabic}
}

\usepackage[mathscr]{eucal}
\usepackage{textcomp}

\newenvironment{theabstract}[1]%
{\markboth{#1}{#1}%
{\large\noindent\rule{1ex}{1ex}\hspace{\stretch{1}}%
{\textbf{\textit{#1}}}%
\hspace{\stretch{1}}\rule{1ex}{1ex}}
\addcontentsline{toc}{chapter}{#1} \adjustmtc
\vskip 0.2in
\begin{large}}%
{\end{large}}

\newcolumntype{R}[2]{%
    >{\adjustbox{angle=#1,lap=\width-(#2)}\bgroup}%
    l%
    <{\egroup}%
}
\newcommand*\rot{\multicolumn{1}{R{70}{1em}}}% no optional argument here, please!

\definecolor{linkcol}{rgb}{0,0,0.4}
\definecolor{citecol}{rgb}{0.5,0,0}

\usepackage{hyperref}
\hypersetup
{
pdftitle="PhDNaylorPeter",
pdfauthor="Peter NAYLOR", %auteur du document
pdfsubject="Bioinformatic", %sujet du document
%pdftoolbar=false, %barre d'outils non visible
pdfmenubar=true, %barre de menu visible
pdfhighlight=/O, %effect of clicking on a link
colorlinks=true, %couleurs sur les liens hypertextes
pdfpagemode=UseNone, %aucun mode de page
pdfpagelayout=SinglePage, %ouverture en simple page
pdffitwindow=true, %pages ouvertes entierement dans toute la fenetre
%liens en couleur pour internet!!!
%linkcolor=linkcol, %couleur des liens hypertextes internes
%citecolor=citecol, %couleur des liens pour les citations
%urlcolor=linkcol %couleur des liens pour les url
%liens en noir pour impression!!!
linkcolor=linkcol, %couleur des liens hypertextes internes
citecolor=citecol, %couleur des liens pour les citations
urlcolor=linkcol %couleur des liens pour les url
}


\documentclass[11pt, oneside]{article}   	% use "amsart" instead of "article" for AMSLaTeX format

% \usepackage{draftwatermark}
% \SetWatermarkText{Draft}
% \SetWatermarkScale{5}
% \SetWatermarkLightness {0.9}
% \SetWatermarkColor[rgb]{0.7,0,0}

\usepackage{geometry}                		% See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   		% ... or a4paper or a5paper or ...
%\geometry{landscape}                		% Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    		% Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}				% Use pdf, png, jpg, or eps� with pdflatex; use eps in DVI mode
								% TeX will automatically convert eps --> pdf in pdflat
								% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{url}
\usepackage{subcaption}
\usepackage{authblk}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{fixltx2e}
\usepackage{hyperref}
\usepackage{alltt}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{float}
\usepackage{bigints}
\usepackage{braket}
\usepackage{siunitx}

%
% so you can do e.g., \begin{bmatrix}[r] (or [c] or [l])
%

\makeatletter
\renewcommand*\env@matrix[1][c]{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}}
\makeatother

\newcommand{\argmax}{\operatornamewithlimits{argmax}}
\newcommand{\argmin}{\operatornamewithlimits{argmin}}

\title{A Few Notes on Algebraic Structures (WIP)}
\author{David Meyer \\ dmm@\{1-4-5.net,uoregon.edu\}}

\date{Last update: November 19, 2015}							% Activate to display a given date or no date

\begin{document}
\maketitle

\section{Introduction}

\begin{center}
\begin{table}[H]
\scalebox{0.83}{
\begin{tabular}{l | c | c | c | c |  c | c}
{\bf Structure}           & $\textbf{ABO}^1$ & {\bf Identity}  & {\bf Inverse} & {\bf $\text{Distributive}^2$} & {\bf $\text{Commutative}^3$} & {\bf Comments} \\
\hline
Semigroup                & \checkmark & no                            & no                            & N/A                & no                           & $(S,\circ)$                                    \\
Monoid                      & \checkmark & \checkmark             & no                            & N/A                & no                           & Semigroup plus idenity  $\in S$   \\
Group                        & \checkmark & \checkmark             & \checkmark              & N/A               & no                           & Monoid plus inverse $\in S$         \\
Abelian Group           & \checkmark & \checkmark             & \checkmark              & N/A               & \checkmark             & Commutative group                      \\
$\text{Ring}_{+}$       & \checkmark & \checkmark             & \checkmark              & \checkmark  & \checkmark             & Abelian group under $+$               \\
$\text{Ring}_{*}$        & \checkmark & \checkmark             & no                            & \checkmark  &  no                           & Monoid under $*$                          \\
$\text{Field}_{(+,*)}$  & \checkmark & \checkmark $(+,*)$ & \checkmark $(+,*)$  & \checkmark  & \checkmark             & Abelian group under $+$ and $*$  \\
Vector Space             & \checkmark & \checkmark $(+,*)$ & \checkmark $(+)$    & \checkmark  & \checkmark              & Abelian group under $+$
\end{tabular}}
\caption{A Few Algebraic Structures and Their Features}
\label{tab:algebraic_structures}
\end{table}
\end{center}

\noindent
\textbf{Abbreviations:}

\begin{enumerate}
\item \textbf{ABO:} Associative Binary Operation
\begin{itemize}
\item $(x \circ y) \circ z = x \circ  (y \circ z)$  for all $x, y, z \in S$
\item $x \circ y \in S$ (closure under $\circ$)
\end{itemize}

\item \textbf{Distributive:} Distributive Property
\begin{itemize}
\item Left Distributive Property: $x * (y+z )= (x*y) + (x*z)$ for all $x, y, z \in S$
\item Right Distributive Property: $(y + z) * x = (y*x) + (z*x)$ for all $x, y, z \in S$
\item $*$ is \emph{distributive} over $+$ if $*$ is left and right distributive
\end{itemize}

\item \textbf{Commutative:} Commutative Property
\begin{itemize}
\item $x \circ y = y \circ x {\mbox{ for all }}x,y\in S$
\end{itemize}
\end{enumerate}

\noindent
\textbf{Notes:}
\begin{itemize}
\item Table \ref{tab:algebraic_structures} implies that $\text{F} \subset \text{R} \subset \text{G} \subset \text{M} \subset \text{SG}$
\item $\text{VS} \subset \text{G}_{+}$ (vector spaces are Abelian groups under $+$)
\item $\text{F} \subset \text{VS}$ since the field axioms require a multiplicative inverse ($a^{-1}$) while vector spaces do not
\end{itemize}

\section{Acknowledgements}

\newpage
\bibliographystyle{plain}
\bibliography{/Users/dmm/papers/bib/qc}

\end{document}


\documentclass[11pt]{article}
\usepackage[top=1.5cm,bottom=1.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{url}
\usepackage{hyperref}
\usepackage{xcolor}
%
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ifthen}
\usepackage{mathpartir}
\usepackage{mathtools}
\input{../../slides/macros}
%

\newcommand{\HL}[1]{\boxed{#1}}

\begin{document}

\title{CO663 Part II: Worksheet 4 (\emph{assessed})}

\date{\vspace{-15ex}}
\maketitle

% \pagenumbering{gobble}

\newcommand{\lazyeval}[1]{\framebox{\parbox[c][#1]{\textwidth}{
      \color{white}{h}%
    }}}

\section{Introduction}

The objective of this class is to put in practice the concepts of
programming language design by adding \emph{lists} to language
$\lggeT$.
%
This is an individual exercise.

\begin{center}
  \emph{This class assessment is worth {\color{red} 3} marks.}
\end{center}

Submit your work via \emph{Moodle} (within the deadline specified on the
Moodle page).

\section{Syntax}

Here is the syntax for $\lggeT$ with lists (new parts of \HL{highlighted}).

\[
\begin{array}{llclll}
  \TYPES & \tau & \Coloneqq & \tynat  & \tynat & \text{natural}
  \\
         &&& \HL{\tylist{\tau}} &  \HL{ [\tau] } & \text{lists}
  \\
         &&& \tyarr{\tau_1}{\tau_2}  & \tau_1 \rightarrow \tau_2 & \text{function}
  \\
  \\
  \EXPS & e & \Coloneqq & \var{x} & \var{x} & \text{variable}
  \\
         &&& \ez &  \ez & \text{zero}
  \\
         &&& \esucc{e} &  \esucc{e} & \text{successor}
  \\
         &&& \elam{\tau}{x}{e} & \clam{\tau}{x}{e} & \text{abstraction}
  \\
         &&& \eapp{e_1}{e_2} & \capp{e_1}{e_2} & \text{application}
  \\
         &&& \eiter{e_0}{x}{e_1}{e} & {\citer{e_0}{x}{e_1}{e}}   & \text{iterator}
  \\
         &&& \HL{[\,]} & \HL{[\,]} & \text{empty list}
  \\
         &&& \HL{\econs{e}{e'}} & \HL{\ccons{e}{e'}} & \text{list constructor}
  \\
         &&& \HL{\edeclist{e_0}{x}{y}{e_1}{e}} & \HL{{\cdeclist{e_0}{x}{y}{e_1}{e}}}   &\text{list deconstructor}
\end{array}
\]

The construct $\econs{e}{e'}$ simply builds a new list from an element
$e$ and another list $e'$ by adding $e$ at the front of $e'$.
%
The construct $\edeclist{e_0}{x}{y}{e_1}{e}$ is a
\emph{binder} which introduces two new variables ($\var{x}$ and
$\var{y}$).
%
This construct should behave differently when $e$ is the empty list
(behave as $e_0$). If $e$ is non-empty, then it is divided into a head
(bound to $\var{x}$) and a tail (bound to $\var{y}$).

\section{Exercise 1: add typing rules for the new syntax constructs}
Here are the typing rules for $\lggeT$, add new rules to support lists.
\[
\tyrule
{\ttyrulename{var}}
{\,}
{\tyjudge{\Gamma_1, \var{x}: \tau, \Gamma_2}{\var{x}}{\tau} }
\qquad\qquad\qquad
\tyrule
{\ttyrulename{nat}}
{\,}
{\tyjudge{\Gamma}{\ez}{\tynat}}
\qquad\qquad\qquad
\tyrule
{\ttyrulename{num}}
{\tyjudge{\Gamma}{{e}}{\tynat{}}}
{\tyjudge{\Gamma}{\esucc{e}}{\tynat{}}}
\]
%
\[
\tyrule
{\ttyrulename{lam}}
{\tyjudge{\Gamma, \var{x} : \tau_1}{e}{\tau_2}}
{\tyjudge{\Gamma}{\elam{\tau_1}{x}{e}}{\tyarr{\tau_1}{\tau_2}}}
\qquad\qquad\qquad
\tyrule
{\ttyrulename{ap}}
{
  \tyjudge{\Gamma}{e_1}{\tyarr{\tau_1}{\tau}}
  \and
  \tyjudge{\Gamma}{e_2}{\tau_1}
}
{
  \tyjudge{\Gamma}{\eapp{e_1}{e_2}}{\tau}
}
\]
\[
\tyrule
{\ttyrulename{ite}}
{
  \tyjudge{\Gamma}{e}{\tynat}
  \and
  \tyjudge{\Gamma}{e_0}{\tau}
  \and
  \tyjudge{\Gamma, \var{x} : \tau}{e_1}{\tau}
}
{
  \tyjudge{\Gamma}{\eiter{e_0}{x}{e_1}{e}}{\tau}
}
\]

\section{Exercise 2: add semantic rules for the new syntax constructs}

Here are the evaluation rules for $\lggeT$, add new rules to support lists.

% \subsection{Values}
\[
\semrule{\tsemrulename{z}}
{\,}
{\valjudge{\ez}}
\qquad \qquad  \qquad
\semrule{\tsemrulename{s}}
{\valjudge{e}}
{\valjudge{\esucc{e}}}
\qquad \qquad  \qquad
\semrule
{\tsemrulename{lam}}
{\,}
{\valjudge{\elam{\tau}{x}{e}}}
\]

% \subsection{Reductions}

\[
\semrule
{\tsemrulename{ss}}
{\jtrans{e}{e'}}
{
  \jtrans{\esucc{e}}{\esucc{e'}}
}
\qquad \qquad  \qquad
%
\semrule
{\tsemrulename{ap}}
{\jtrans{e_1}{e'_1}}
{
  \jtrans{\eapp{e_1}{e_2}}{\eapp{e'_1}{e_2}}
}
\]
%
%
\[
\semrule
{\tsemrulename{lan}}
{\valjudge{e_1}
  \and \jtrans{e_2}{e'_2}
}
{\jtrans{\eapp{e_1}{e_2}}{\eapp{e_1}{e'_2}}}
\qquad \qquad  \qquad
\semrule
{\tsemrulename{lav}}
{\valjudge{e_2}}
{\jtrans{\eapp{\elam{\tau}{x}{e_1}}{e_2}}{\subs{e_1}{e_2}{x}}}
\]
%
\[
\semrule
{\tsemrulename{rin}}
{\jtrans{e}{e'}}
{
  \jtrans{\eiter{e_0}{x}{e_1}{e}}{\eiter{e_0}{x}{e_1}{e'}}
}
\qquad \qquad  \qquad
%
\semrule
{\tsemrulename{r0}}
{\,}
{
  \jtrans{\eiter{e_0}{x}{e_1}{\ez}}{e_0}
}
%
\]
\[
%
\semrule
{\tsemrulename{rs}}
{\valjudge{{e}}}
{
  \jtrans{\eiter{e_0}{x}{e_1}{\esucc{e}}}{\subs{e_1}{{\eiter{e_0}{x}{e_1}{{e}}}}{x}}
}
\]

\section*{\LaTeX\ template}
The \LaTeX\ sources of this document are available in:
\url{https://github.com/ukc-co663/pl-design-latex-template}.

% \end{document}

\section{Model answer}

\subsection{Typing rules}
\[
\tyrule
{\ttyrulename{dlist}}
{
  \tyjudge{\Gamma}{e}{\tylist{\tau}}
  \and
  \tyjudge{\Gamma}{e_0}{\tau'}
  \and
  \tyjudge{\Gamma, \var{x} : \tau, \var{y}: {\tylist{\tau}}}{e_1}{\tau'}
}
{
  \tyjudge{\Gamma}{\edeclist{e_0}{x}{y}{e_1}{e}}{\tau'}
}
\]

\[
\tyrule
{\ttyrulename{e}}
{\,}
{
  \tyjudge{\Gamma}{[\,]}{\tylist{\tau}}
}
\qquad \qquad  \qquad \qquad
\tyrule
{\ttyrulename{cons}}
{
  \tyjudge{\Gamma}{e}{{\tau}}
  \and
  \tyjudge{\Gamma}{e'}{\tylist{\tau}}
}
{
  \tyjudge{\Gamma}{\econs{e}{e'}}{\tylist{\tau}}
}
\]

\subsection{Semantic rules}

\[
\semrule{\tsemrulename{cons}}
{\valjudge{e}
  \and
  \valjudge{e'}
}
{\valjudge{\econs{e}{e'}}}
\qquad \qquad  \qquad \qquad
\semrule{\tsemrulename{empty}}
{\,
}
{\valjudge{[\,]}}
\]

\[
\semrule
{\tsemrulename{cin}}
{\jtrans{e}{e'}}
{
  \jtrans{\edeclist{e_0}{x}{y}{e_1}{e}}{\edeclist{e_0}{x}{y}{e_1}{e'}}
}
\qquad \qquad  \qquad
%
\semrule
{\tsemrulename{c0}}
{\,}
{
  \jtrans{\edeclist{e_0}{x}{y}{e_1}{[\,]}}{e_0}
}
%
\]
\[
%
\semrule
{\tsemrulename{cs}}
{\,}
{
  \jtrans{\edeclist{e_0}{x}{y}{e_1}{\econs{e}{e'}}}
  {\twosubs
    {e_1}
    {e}{x}
    {e'}{y}
  }
}
\]

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass[12pt]{article}
%\documentclass[12pt,landscape]{article}

\include{preamble}

\newcommand{\instr}{Your answer will consist of a string (e.g. \texttt{aebgd}) where the order of the letters does not matter nor does upper / lowercase.}

\title{Math 341 Spring 2020 \\ Midterm Examination Two}
\author{Professor Adam Kapelner}

\date{Thursday, April 23, 2020}

\begin{document}
\maketitle

%\noindent Full Name \line(1,0){410}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent I acknowledge and agree to uphold this Code of Academic Integrity. \\

%\begin{center}
%\line(1,0){250} ~~~ \line(1,0){100}\\
%~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
%\end{center}

\normalsize

\section*{Instructions}

This exam is 80 minutes (variable time per question) and closed-book. You are allowed \textbf{one} page (front and back) of a \qu{cheat sheet}, one table of reference and scrap paper but no graphing calculator. Please read the questions carefully. No food is allowed, only drinks. %If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in \textit{any} widely accepted mathematical notation which could be resolved to an exact or approximate number with the use of a computer. I advise you to skip problems marked \qu{[Extra Credit]} until you have finished the other questions on the exam, then loop back and plug in all the holes. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem [8min] We return the topic of inference for a new player's baseball batting average, the $\theta$ in a binomial likelihood for $n$ at bats. Using previous data, we create an informative prior of $\alpha = 42.3$ and $\beta = 127.7$.

\benum

\subquestionwithpoints{10} A new player enters baseball and he has 3 hits out of 7 at bats. What is the probability of the next 40 at bats he will have 11 or more hits. Record the letter(s) of all the following that are \textbf{true}.

\begin{enumerate}[(a)]
\item pbinom(11, 40, 3 / 7)
\item 1 - pbinom(10, 40, 3 / 7)
\item pbetabinom(11, 40, 3 / 7)
\item 1 - pbetabinom(10, 40, 3 / 7)
\item pbetabinom(11, 40, 42.3, 127.7)
\item 1 - pbetabinom(10, 42.3, 127.7)
\item pbetabinom(11, 40, 45.3, 131.7)
\item 1 - pbetabinom(10, 45.3, 131.7)
\item $\displaystyle\sum_{x_* = 11}^{40} \binom{40}{x_*} \frac{B(45.3 +x_*, 171.7 - x_*)}{B(45.3, 131.7)}$
\item 1 - $\displaystyle\sum_{x_* = 1}^{10} \binom{40}{x_*} \frac{B(45.3 +x_*, 171.7 - x_*)}{B(45.3, 131.7)}$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $X \sim \normnot{\theta}{\sigsq}$ and let $k$ denote the \emph{fully reduced} kernel of a distribution.

\benum

\subquestionwithpoints{12} Record the letter(s) of all the following that are \textbf{true}.

\begin{enumerate}[(a)]
%\item $\cprob{X}{\theta,\sigsq} = \tothepow{2\pi\sigsq}{-\half}e^{-\oneover{2\sigsq}(x - \theta)^2}$

\item $\ck{X}{\theta,\sigsq} = \displaystyle\tothepow{\sigsq}{-\half}e^{-\oneover{2\sigsq}(x - \theta)^2}$
\item $\ck{\sigsq}{\theta,X} = \displaystyle\tothepow{\sigsq}{-\half}e^{-\oneover{2\sigsq}(x - \theta)^2}$
\item $\ck{\theta}{\sigsq,X} = \displaystyle\tothepow{\sigsq}{-\half}e^{-\oneover{2\sigsq}(x - \theta)^2}$

\item $\ck{X}{\theta,\sigsq} = e^{-\oneover{2\sigsq}(x - \theta)^2}$
\item $\ck{\sigsq}{\theta,X} = e^{-\oneover{2\sigsq}(x - \theta)^2}$
\item $\ck{\theta}{\sigsq,X} = e^{-\oneover{2\sigsq}(x - \theta)^2}$

\item $\ck{X}{\theta,\sigsq} = e^{-\frac{x^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$
\item $\ck{\sigsq}{\theta,X} = e^{-\frac{x^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$
\item $\ck{\theta}{\sigsq,X} = e^{-\frac{x^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$

\item $\ck{X}{\theta,\sigsq} = e^{-\frac{\theta^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$
\item $\ck{\sigsq}{\theta,X} = e^{-\frac{\theta^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$
\item $\ck{\theta}{\sigsq,X} = e^{-\frac{\theta^2}{2\sigsq}}e^{\frac{\theta x}{\sigsq}}$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\Xoneton \iid \poisson{\theta}$ and use the Jeffrey's prior for $\theta$. We wish to test if $\theta > 1$.

\benum

\subquestionwithpoints{10} Record the letter(s) of all the following expressions that correctly compute the Bayesian p-value at the $\alpha_0 = 5\%$ level.

\begin{enumerate}[(a)]
\item pgamma(0.05, $\sum x_i + 0.5, n$)
\item 1 - pgamma(0.05, $\sum x_i + 0.5, n$)
\item pgamma(1, $\sum x_i + 0.5, n$)
\item 1 - pgamma(0.05, $\sum x_i + 0.5, n$)
\item qgamma(0.05, $\sum x_i + 0.5, n$)
\item $[$qgamma(0.025, $\sum x_i + 0.5, n$), qgamma(0.975, $\sum x_i + 0.5, n$)$]$
\item pgamma(0.05, $\sum x_i, n$)
\item 1 - pgamma(0.05, $\sum x_i , n$)
\item pgamma(1, $\sum x_i, n$)
\item 1 - pgamma(1, $\sum x_i, n$)
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\Xoneton \iid \poisson{\theta}$ and use the prior of indifference for $\theta$. We wish to find the probability that $x_* = 5$, i.e. the realization of the next observation is equal to 5.

\benum

\subquestionwithpoints{8} Record the letter(s) of all the following expressions that correctly compute the probability of interest.

\begin{enumerate}[(a)]
\item pgamma($5, \sum x_i + 1, n$)
\item dgamma($5, \sum x_i + 1, n$)
\item qnbinom($n / (n + 1), \sum x_i + 1, 5$)
\item dnbinom($5, \sum x_i + 1, n / (n + 1)$)
\item ppois(5, $\xbar$)
\item ppois(5, $\theta$)
\item dpois(5, $\theta$)
\item dpois(5, $\xbar$)
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $\sigsq$ known. You wish to design a prior that has the strength of 3 observations centered at 1.
\benum

\subquestionwithpoints{10} Record the letter(s) of all the following expressions that correctly specify this prior.

\begin{enumerate}[(a)]
\item $\cprob{\theta}{\sigsq} = \normnot{0}{3}$
\item $\cprob{\theta}{\sigsq} = \normnot{0}{\infty}$
\item $\cprob{\theta}{\sigsq} = \normnot{1}{\sigsq / 3}$
\item $\cprob{\theta}{\sigsq} = \normnot{1}{\sigsq / 3^2}$
\item $\cprob{\theta}{\sigsq} = \normnot{1}{\sigsq}$
\item $\cprob{\theta}{\sigsq} = \invgammanot{3/2}{3/2}$
\item $\cprob{\theta}{\sigsq} = \invgammanot{3/2}{3\sigsq/2}$
\item $\cprob{\theta}{\sigsq} = \invgammanot{3}{3\sigsq}$
\item $\cprob{\theta}{\sigsq} = \invgammanot{3}{\infty}$
\item $\cprob{\theta}{\sigsq} = \invgammanot{1}{\infty}$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $\sigsq$ known. Using the Laplace prior, find the probability that $\theta$ is greater than $\xbar$.
\benum

\subquestionwithpoints{8} Record the letter(s) of all the following expressions that correctly compute this probability.

\begin{enumerate}[(a)]
\item 0.5
\item $\prob{\theta > \thetahatmmse}$
\item $\prob{\theta > \thetahatmmae}$
\item $\prob{\theta > \thetahatmap}$
\item pnorm(0.5, $\xbar$, $\sigma / \sqrt{n}$)
\item qnorm(0.5, $\xbar$, $\sigma / \sqrt{n}$)
\item pnorm($\xbar$, $\xbar$, $\sigma / \sqrt{n}$)
\item 1 - pnorm($\xbar$, $\xbar$, $\sigma / \sqrt{n}$)
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $n = 20$ observations, a data average of $\xbar = 13.9$ and $\sigsq = 5^2$. Using the Jeffrey's prior, find the distribution that the next observation $x_*$ is realized from.

\benum
\subquestionwithpoints{12} Record the letter(s) of all the following expressions that correctly compute this probability.

\begin{enumerate}[(a)]
\item $\normnot{13.9}{5^2}$
\item $\normnot{13.9}{5^2 / 20}$
\item $\normnot{13.9}{5^2 + 5^2 / 20}$
\item $\normnot{13.9}{(5 + 5 / \sqrt{20})^2}$
\item $T_{13.9}\parens{5^2}$
\item $T_{13.9}\parens{5^2 / 20}$
\item $T_{13.9}\parens{5^2 + 5^2 / 20}$
\item $T_{13.9}\parens{(5 + 5 / \sqrt{20})^2}$
\item $T_{20}\parens{13.9, 5^2}$
\item $T_{20}\parens{13.9, 5^2 / 20}$
\item $T_{20}\parens{13.9, 5^2 + 5^2 / 20}$
\item $T_{20}\parens{13.9, (5 + 5 / \sqrt{20})^2}$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $\theta$ known. If you wished to design a prior based on two pseudo-observations, $y_1 = 0$ and $y_1 = 1$, what would the prior form be?

\benum

\subquestionwithpoints{10} Record the letter(s) of all the following expressions that correctly specify the prior distribution.

\begin{enumerate}[(a)]
\item $\cprob{\sigsq}{\theta} = \invgammanot{0}{0}$
\item $\cprob{\sigsq}{\theta} = \invgammanot{1}{0}$
\item $\cprob{\sigsq}{\theta} = \invgammanot{1/2}{1/2}$
\item $\cprob{\sigsq}{\theta} = \invgammanot{1/2}{\theta^2(1-\theta)^2/2}$
\item $\cprob{\sigsq}{\theta} = \invgammanot{1}{\theta^2(1-\theta)^2/2}$
\item $\cprob{\sigsq}{\theta} = \invgammanot{1}{\theta^2(1-\theta)^2}$
\item $\cprob{\sigsq}{\theta} = \normnot{0}{0.5}$
\item $\cprob{\sigsq}{\theta} = \normnot{0}{\infty}$
\item $\cprob{\sigsq}{\theta} = \normnot{0.5}{\sigsq / 2}$
\item $\cprob{\sigsq}{\theta} = $ Deg(0)
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $\theta = 0$ i.e. the mean is known to be zero. Let the prior be Jeffrey's and you then observe $x_1 = 3.45, x_2 = 1.87$ and $x_3 = 5.03$.

\benum

\subquestionwithpoints{10} Record the letter(s) of all the following expressions that provide a Bayesian estimate (rounded to the nearest two decimals). This Bayesian estimate must be one we studied in this class.

\begin{enumerate}[(a)]
\item 2.50
\item 1.58
\item qgamma(0.5, 3, 40.70)
\item qgamma(0.5, 1.5, 20.35)
\item qinvgamma(0.5, 3, 40.70)
\item qinvgamma(0.5, 1.5, 20.35) %%
\item 40.70 %%
\item 13.57
\item 8.14 %%
\item 0
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem [8min] Let $\mathcal{F}$ be the $\iid$ normal model with $\theta = 0$ i.e. the mean is known to be zero. Let the prior be Jeffrey's and you then observe $x_1 = 3.45, x_2 = 1.87$ and $x_3 = 5.03$. We are interested in the distribution that $x_*$, the next observation, is drawn from.

\benum

\subquestionwithpoints{10} Record the letter(s) of all the following expressions that provide the correct posterior predictive distribution with parameters specified to the nearest two decimal places.

\begin{enumerate}[(a)]
\item $T_{3}\parens{3.45, 2.50}$
\item $T_{3}\parens{3.45, 13.57}$
\item $T_{3}\parens{0, 2.50}$
\item $T_{3}\parens{0, 13.57}$
\item $T_{3}\parens{0, 1.58^2 + 1.58^2 / 3}$
\item $\normnot{0}{1.58^2}$
\item $\normnot{3.45}{1.58^2}$
\item $\normnot{0}{1.58^2 + 1.58^2 / 3}$
\item $\normnot{3.45}{1.58^2 + 1.58^2 / 3}$
\item $\normnot{0}{13.57}$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


\chapter{Adaptive remeshing}\label{chap:Adaptivity}

\section{Motivation}
Historically, numerical analysts concerned themselves
with \emph{a priori} error bounds of particular numerical
schemes, i.e.\@ \ asymptotic analyses of the order of convergence
of a discretisation with respect to some discretisation parameter
such as mesh sizing $h$ or polynomial order $p$. However, such
\emph{a priori} error bounds do not provide useful estimates
of the simulation error of a particular physical system on
a particular mesh for a specified norm: they merely describe
how that error behaves as the discretisation is modified. Since
such \emph{a priori} error bounds involve the unknown exact solution,
they are, in general, not computable.

In the late 1970s, the pioneering work of Babu\v ska
and Rheinboldt laid the foundations for \emph{a posteriori}
error estimates \citep{babuska1978,babuska1978b}.
In contrast to \emph{a priori} bounds,
\emph{a posteriori} error estimates involve only
the approximate computed solution and data from the problem,
and are thus computable (or approximately so, if they involve the
solution of an auxiliary problem). These error
estimates can then be used in an adaptive loop, modifying
the discretisation until some user-specified error criterion
is reached. Most \emph{a posteriori} error estimation literature deals with estimating
the error in the natural norm induced by the bilinear form
of the problem, the energy norm.
For a review of \emph{a posteriori} error estimation
with emphasis on energy norm estimation
see the books of Verf\"urth \citep{verfurth1996} and
Ainsworth and Oden \citep{ainsworth2000}. The goal-oriented
adaptive framework of Rannacher and co-workers, which estimates
the error in the computation of a given goal functional, is detailed
in \citet{becker2001} and \citet{bangerth2003}.

Once \emph{a posteriori} estimates have been computed,
there are many possible ways of modifying the discretisation
to achieve some error target. These include $h$-adaptivity,
which changes the connectivity of the mesh \citep{berger1989}; $p$-adaptivity,
which increases the polynomial order of the approximation
\citep{babuska1994}; and $r$-adaptivity, which relocates
the vertices of the mesh while retaining the same connectivity
\citep{budd2009}. Combinations of these methods are also
possible (e.g., \citet{houston2001,ledger2003}).

This review focuses on adaptive remeshing in multiple dimensions, as this is
the technology available in \fluidity.
This approach is the most powerful of all $hr$-adaptive methods, since the meshes produced are not constrained
by the previous mesh; therefore, this approach allows for maximum flexibility in
adapting to solution features. However, this flexibility comes
at a cost: guiding the adaptive remeshing procedure (choosing
what mesh to construct), executing the adaptation (constructing
the chosen mesh) and data transfer of solution fields (from the previous mesh to
the newly adapted mesh) become more complicated than with hierarchical refinement.

This chapter is divided as follows. Firstly, we orient the user by describing a typical
adaptive loop used in \fluidity\ (section \ref{sec:typical_adaptive_loop}). Before
discussing how to configure mesh adaptivity, we introduce some necessary background ideas:
section \ref{sec:meshes_and_metrics} describes how a metric tensor field
may be used to encode the desired mesh, and section \ref{sec:adaptive_remeshing_technology} describes how the
mesh optimisation procedure in \fluidity\ uses this metric tensor to generate an adapted mesh. Finally,
the chapter ends with a discussion of how to use mesh adaptivity in \fluidity, with a discussion
of the various algorithms offered and their advantages and disadvantages (section \ref{sec:using_mesh_adaptivity}).

\section{A typical adaptive loop} \label{sec:typical_adaptive_loop}
This section presents a typical adaptive loop, as used in the lock--exchange example, section \ref{sec:lock_exchange}
(examples/lock\_exchange/lock\_exchange.flml).

\begin{enumerate}
\item The initial conditions of the prognostic variables (velocity, temperature) are applied.
\item The adaptive procedure is invoked 6 times to adapt the initial mesh to represent the
desired initial conditions. This involves:
  \begin{enumerate}
  \item Computing the Hessian of velocity (which is zero, as the initial velocity condition is zero).
  \item Converting the Hessian of velocity into a metric tensor (in this case, the tensor will request the maximum
        edge length size everywhere, as velocity can be represented exactly). This depends on the norm chosen and the minimum
        and maximum edge length sizes.
  \item Computing the Hessian of temperature (which is nonzero, as the initial temperature condition is a step-function).
  \item Converting the Hessian of temperature into a metric tensor (in this case, the tensor will request fine resolution
        at the step, and the maximum edge length size everywhere else).
  \item The size requirements of these two metrics are merged to give one metric tensor describing the desired mesh.
  \item The metric is then smoothed with a gradation algorithm to avoid large jumps in mesh sizing.
  \item The metric is scaled so that the expected number of nodes it will yield after adapting is no more than the maximum number of nodes.
  \item The metric is then passed to the adaptivity algorithm, which modifies the mesh until it satisfies the sizing requirements given in the metric.
  \item Since the simulation time has not yet advanced, the initial conditions are reapplied on the newly-generated adapted mesh.
  \end{enumerate}
     This procedure is iterated 6 times so that the adaptive algorithm can recognise the anisotropy of the step.
\item Once the initial mesh has converged, the simulation time loop proceeds. The lock--exchange simulation invokes
the adaptive algorithm every 10 timesteps to ensure that the dynamics do not extend beyond the zone of adapted
resolution. Each invocation of the adaptive algorithm involves:
  \begin{enumerate}
  \item Computing the Hessians of velocity and temperature (both of which will be nonzero).
  \item Converting the Hessians to metrics (again depending on the norms chosen and the minimum and maximum edge length sizes).
  \item Merging the metrics for each field to yield a combined metric which satisfies both of their size demands.
  \item The metric is then smoothed and scaled, as before, and passed to the adaptivity library.
  \item Once the adaptivity library is finished, the field data from the previous mesh is transferred to the new mesh by interpolation.
  \end{enumerate}
\end{enumerate}

\section{Representing meshes as metric tensors} \label{sec:meshes_and_metrics}
The process of adaptive remeshing divides naturally into three parts. The first is to
decide what mesh is desired; the second is to actually generate that mesh; the third is to
transfer data from the old mesh to the new mesh. The form
of communication between the first two stages is a \emph{metric}: a symmetric positive-definite tensor field
which encodes the desired geometric properties of the mesh. This representation was first introduced
in \citet{vallet1990}, and has proven key to the success of adaptive remeshing methods.
This section describes the mathematical details of how a metric encodes the desired size
and shape information: however, understanding this section is not necessary to successfully
use adaptivity in \fluidity.

Firstly, consider how the size and shape information about a mesh might be encoded. A natural
first attempt would be to define a size function $h(x)$ which describes the desired mesh
spacing at a particular point. This works, and is used in many isotropic adaptive algorithms.
However, its deficiency becomes clear when we consider the anisotropic examples that we
wish to exploit: consider again the step function initial condition of the lock--exchange
example. In this case, we want the mesh sizing across the step to be small (to capture the
jump), but we want the mesh sizing along the step to be large (parallel to the step, the
field does not change at all). Therefore, the \emph{mesh sizing desired is not only a function
of space, it is also a function of direction}. At a given point, the desired mesh sizing differs
in different directions. This cannot be encoded in a scalar size function, as that associates
only one number with each point in space. The solution to this problem is to increase the rank
of the object returned by the size function, from a rank-0 single real value to a rank-2 tensor.

Let $M$ be a symmetric positive-definite tensor (constant over the domain for now).
$M$ induces an inner product by
\begin{equation}
\langle x, y\rangle_M = x^T M y
\end{equation}
and so $M$ also induces a notion of distance between two points in the usual manner:
\begin{equation}
d_M(x, y) = ||x - y||_M = \sqrt{\langle x - y, x - y \rangle}.
\end{equation}
Note that the symmetric positive-definite condition is necessary for the tensor
to induce a sense of distance: for otherwise, the tensor does not induce
a norm.

Now let $\metric$ be a symmetric positive-definite tensor field. As long as
$\metric$ is sufficiently smooth, then it also induces an inner product
(see \citet{simpson1994} for details). We say that a mesh $\cal{T}$ satisfies
$\metric$ if every edge has unit edge length with respect to its inner product;
$\cal{T}$ is a unit mesh with respect to this metric.

The metric \emph{warps space}, in the same manner that space is warped in General Relativity.
The metric encodes a new sense of distance, and a regular unit mesh (a mesh of edge length
one) is built with respect to this new sense of distance. For example, if the metric encodes
that two points are ``far apart'', then when a unit mesh is built, many mesh
nodes will lie between them, and the mesh concentration will be dense. Conversely, if the metric
encodes that two points are ``close together'', then when a unit mesh is built,
few mesh nodes will lie between them and the mesh concentration will be sparse.

In the adaptive procedure employed in \fluidity, the metric passed to the adaptivity
library encodes the desired mesh sizing. That is, the adaptive procedure is a function
that takes in a metric and returns a mesh matching that metric. Conversely, it is also
possible to derive the metric for a given mesh. In order to see how the metric representing a mesh may be derived, consider the transformation from the reference element to the physical
element inherent in the finite element assembly procedure \citep{femtools}.
Let $K$ be an element of a linear simplicial mesh. $K$ is geometrically characterised by the affine map
$T_K: \hat{K} \rightarrow K$, where $\hat{K}$ is the reference isotropic element. Since
$T_K$ is assumed affine, we can write this transformation as
\begin{equation}
x = T_K(\hat{x}) = J_K\hat{x} + t_K,
\end{equation}
where $J_K$ is the Jacobian of the transformation. The metric for this element is derived
from the polar decomposition of the Jacobian, which yields
\begin{equation}
J_K = M_K Z_K
\end{equation}
where $M_K$ is a symmetric positive-definite matrix referred to as the metric,
and $Z_K$ is orthogonal \citep{micheletti2006}. The geometric properties of a simplicial mesh can be represented
as a piecewise constant (\Pzero) field over it. The same argument applies to
nonlinear or non-simplicial elements, but here the Jacobian varies over the element,
and so the metric does too.

The book of \citet{george1998} gives a thorough discussion of the role of metrics
in mesh adaptivity. F. Alauzet and co-workers have recently published some thought-provoking
papers advocating that metrics are in fact the continuous analogue of meshes: in the same
way as the continuous equations are discretised to yield the discrete equations, a continuous
metric is discretised to give a mesh on which those equations are solved. For more details, see
e.g. \citet{alauzet2006b,courty2006,alauzet2008,loseille2010i,loseille2010ii}.

\section{Adaptive remeshing technology} \label{sec:adaptive_remeshing_technology}
This section briefly reviews the second problem solved by the adaptivity algorithm: given the desired
size and shape information, how can a mesh satisfying this be generated?

Given a metric $\metric$, there are three main ways to generate a mesh which satisfies
that metric: global remeshing, local remeshing, and mesh optimisation. For a thorough discussion
of the history of these three approaches, see \citet{farrell2009i}.

Global remeshing is the act of generating an entirely new mesh of the same domain satisfying the sizing
specification, by means of an automatic mesh generator. This was first introduced in \citet{peraire1987} by incorporating
sizing and directionality inputs to an advancing front mesh generator. While this is the fastest
approach if the existing mesh and the desired mesh are very different, developing a robust metric-aware
mesh generator is very difficult, and to the best of the author's knowledge no open-source algorithm
is available for this problem.

By contrast, local remeshing is a mechanism of adaptive remeshing in which cavities of elements are removed
and the hole remeshed \citep{hassan1998}. These cavities are identified by measuring
their conformity to the given sizing specification. Again, this relies on the availability of a robust
metric-aware mesh generator to fill the cavities.

Mesh optimisation is a mechanism of adaptive remeshing which deforms the previous mesh
to the adapted mesh by a sequence of local operations. The first ingredient in a mesh optimisation algorithm is the functional: how
the quality of an element is determined, and how one element is deemed better than
another. In the adaptive remeshing case, the functional is the point at which the metric
comes in to play: the basic job of the functional is to measure the compatibility of the
element to the metric specifying the size and shape of that element. Many different functionals
have been considered in the literature: for a review, see \citet{knupp2003}. For the
functional used in the 2D case, see \citet{vasilevskii1999}; for the functional used in the
3D case, see \citet{pain2001}.

The second ingredient in a mesh optimisation algorithm is the set of operations the
algorithm may perform in an attempt to improve the mesh. Example operations might include
merging adjacent elements, splitting an element into two or more child elements, swapping
edges or faces, and moving nodes in the mesh. For diagrams of some example optimisation
operations see figure \ref{fig:edge_operations}.

\begin{figure}[h]
\centering
\subfigure[2D]{\includegraphics[width=0.475\textwidth, clip = True, trim = 35mm 165mm 32.5mm 20mm]{./adaptivity_images/piggott2009_pg4}} \hspace{7mm}
\subfigure[3D]{\includegraphics[width=0.45\textwidth, clip = True, trim = 25mm 35mm 95mm 185mm]{./adaptivity_images/pain2001_pg3}}
\caption[Example of mesh modification operations.]{Example of mesh modification operations. (a): Node insertion or edge splitting, node deletion or edge collapse, edge swap and node movement in two dimensions \citep[2D, from][]{piggott2009}. (b): Edge to face and face to edge swapping in three dimensions \citep[3D, from][]{pain2001}.}
\label{fig:edge_operations}
\end{figure}

The mesh optimisation library provisionally executes one or more optimisation operations, and then
invokes the functional to decide whether those operations improved the mesh. If the
mesh was improved, then the proposed changes are committed; otherwise, they are reverted. The
scheduling of the operations has a large impact on the effectiveness of the procedure \citep{li2005},
and ensuring that the optimisation operations are robust is quite delicate \citep{compere2010a}.

Mesh optimisation is available by default in three dimensions using the algorithm
of \citet{pain2001}. It is available in two dimensions using the \texttt{mba2d} algorithm of
\citet{vasilevskii1999} if \fluidity\ was configured with
the \configureflag{--enable-2d-adaptivity} flag, section \ref{sec:configuring_the_build_process}; this is not enabled by default
as \texttt{mba2d} is licensed under the GPL, while \fluidity\ is licensed under the LGPL,
and so its default inclusion would cause licensing complications.

\section{Using mesh adaptivity} \label{sec:using_mesh_adaptivity}
This section contains the practical advice for configuring and optimising
adaptive simulations. Further information about the configuration options can be found in section \ref{sec:config_adapt}

The first golden rule is to start with a fixed-mesh simulation that works
(i.e., gives sensible answers for the resolution used). Mesh adaptivity is a complex
extra nonlinear operation bolted on to the discretisation, and it will make reasoning
about and debugging the simulation more difficult. Therefore, make sure that you
are happy with the well-posedness of the problem, the stability of the discretisation,
the convergence of the solutions, etc. before attempting to employ adaptivity.

Secondly, be aware that employing adaptivity can require a nontrivial amount of tuning
to get exactly the result desired. Be prepared to iterate on the adaptivity settings,
and make changes conservatively, one parameter at a time. As you gain more experience
of the adaptivity options offered in \fluidity, it will become easier to identify
problems and how they can be solved.

As a first step, configure \fluidity\ to adapt only to one field, usually the most
important in the simulation. The adaptivity options for each field are found in the
\option{\ldots/adaptivity\_options} option under each field. As the initial settings,
choose \option{absolute\_measure} (section \ref{sec:absolute_metrics}), and set \option{p\_norm}
to 2 (section \ref{sec:norm_choice}). This configures the metric to optimise for the $L_2$ norm
of interpolation error. Under the \option{InterpolationErrorBound}, set the value to a constant,
and as an initial guess use 10\% of the $L_2$ norm of the field under consideration (this
value is available in the .stat file). See section \ref{sec:adaptivity_weights} for more information
about this setting. Unless you are running a simulation with DG fields, leave the interpolation
algorithm as \option{consistent\_interpolation}; if you have DG fields, set their interpolation
algorithm to \option{galerkin\_projection}. This is explained in further detail in section \ref{sec:interpolation_algorithms}.

Field-specific adaptivity options are configured under the field in question, and non-field-
specific options are configured in \option{/mesh\_adaptivity/hr\_adaptivity}. It is recommended
that the operator first configure the adaptivity options so that it is only invoked at the
very start of the simulation (using the \option{adapt\_at\_first\_timestep} option), and only once this is configured satisfactorily move on to dynamic adaptivity. There are several reasons for this. Firstly, since the adaptive algorithm is invoked
at the start of the simulation, the time between configuration and feedback is lessened, making it
easier for the operator to get a feel for the effect of the different settings. Secondly, since the
system state at the initial condition is known exactly, the complicating effects of interpolation
algorithms are excised. Thirdly, accurately representing the initial condition is usually a requirement
for a sensible answer at later simulation times, and so configuring the initial adaptivity is
necessary anyhow.

For the initial adaptive configuration, set the period in timesteps to 20, and the maximum number
of nodes to be 200000 times the number of processors you are using. (This is deliberately set high
so that it does not influence the mesh produced; later on, it can be turned down to control the
computational requirements of the simulation. See section \ref{sec:max_min_nodes}.) Under \option{/mesh\_adaptivity/hr\_adaptivity/enable\_gradation},
set the \option{gradation\_parameter} to 2. This allows the edge length to double from element to element,
and is quite a weak gradation parameter. (Again, we wish to minimise the influence of the gradation
algorithm on the mesh produced; if necessary, the metric can be smoothed by reducing this value. See section
\ref{sec:gradation_parameter}.) Under \option{/mesh\_adaptivity/hr\_adaptivity/tensor\_field::MaximumEdgeLengths/anisotropic\_symmetric/constant},
set the \emph{diagonal} entries to the diameter of your domain. (The maximum edge length can be anisotropic,
in the same way that the desired edge length can be anisotropic. See section \ref{sec:max_min_edge_length}.)
Similarly, under \option{\ldots/tensor\_field::MinimumEdgeLengths/anisotropic\_symmetric/constant}, set the
\emph{diagonal entries} to something small (e.g., the diameter of the domain divided by 10000). Again, we wish
to remove the influence of these settings by default, so that their effect can be applied by the operator
if it is desired.

Activate \option{/mesh\_adaptivity/hr\_adaptivity/adapt\_at\_first\_timestep}, and set the \option{number\_of\_adapts}
to 6. Under \option{/mesh\_adaptivity/hr\_adaptivity/debug}, activate the \option{write\_adapted\_state} option. This
ensures that after each invocation of the adaptivity algorithm the mesh is dumped, so that the convergence of
the adaptive algorithm for the initial condition may be inspected. Finally, configure the timestepping
options so that the simulation terminates after one timestep, so that the adaptivity settings may be configured
for the initial condition.

Now run the problem, and inspect the adapted\_state\_*.vtu that result. Each VTU is the \emph{output}
of the adaptivity loop, and hopefully convergence of the adaptive procedure towards some
mesh should be observed. If the adapted mesh is satisfactory, then record the adaptivity parameters
used for this field, and repeat the procedure on any other fields to which you wish to adapt that
have nontrivial boundary conditions. Once you are happy with the initial mesh, run the simulation further
and inspect the output of the adaptivity library, tuning the parameters to get the optimal mesh. (In this case, you may wish to cache the output of
the adaptation to the initial condition with \option{/mesh\_adaptivity/hr\_adaptivity/adapt\_at\_first\_timestep/output\_adapted\_mesh}.)
The following sections offer guidance on the parameters that can be varied and the effect that
this has on the adaptive simulation.

\subsection{Choice of norm} \label{sec:norm_choice}
The basic strategy used to compute the error metric is to \emph{control the interpolation
error} of the function represented. Let $f(x)$ be a continuous, exact field, and let
$f_h(x)$ be its representation on the finite element mesh. Then, the interpolation error
is defined as $\left| f - f_h \right|$, which is itself a function over the domain $\Omega$.
The ``size'' of this interpolation error may be quantified in different ways, using different norms.
Historically, the interpolation error was first controlled in the $L_{\infty}$ norm, which considers
the maximum value of the interpolation error over the domain. The metric formulation which
controls the $L_{\infty}$ norm is the simplest, and remains the default in \fluidity. Since the $L_{\infty}$
norm considers only the least accurate point in the domain, without regard to the relative size of the
interpolation error there, then it can have a tendency to focus the resolution entirely on the
dynamics of largest magnitude. Other authors have instead proposed the use of the $L_p$ norm, which incorporates
more influence from dynamics of smaller magnitude \citep{alauzet2008,loseille2010ii}. Empirical experience indicates that choosing $p=2$, and hence the $L_2$ norm, generally gives better results, and for that reason we recommend it as the default for all new adaptivity configurations.

\subsection{Absolute, relative and $p$-- metrics} \label{sec:absolute_metrics}
Consider again the metric for controlling the $L_{\infty}$ norm of the interpolation error. This metric takes the form
\begin{equation}
M = \frac{\left|H\right|}{\epsilon},
\end{equation}
where $H$ is the Hessian of the field under consideration and $\epsilon$ is the target interpolation error. As mentioned
in the previous section, this metric tends to neglect smaller-scale dynamics, and so \citet{castrodiaz1997} proposed
an alternative metric formulation to fix this. They suggested to compute
\begin{equation}
M = \frac{\left|H\right|}{\max(\epsilon \cdot \left|f\right|, \epsilon_{\textrm{min}})},
\end{equation}
where $f$ is the field under consideration, $\epsilon$ is now a \emph{relative tolerance}, and $\epsilon_{\textrm{min}}$ is a
user-configurable parameter. For example, if $\epsilon = 0.01$, then the tolerance on the denominator of the metric formulation
will be 1\% of the value of the field, and so it will scale the target interpolation error with the magnitude of the field.
$\epsilon_{\textrm{min}}$ is the minimum tolerance, and is employed to ensure that the denominator never becomes zero.
However, empirical experience indicates that this metric formulation is very sensitive to the value of $\epsilon_{\textrm{min}}$,
and that it generally yields poor results. The approach of using the $L_p$ norm instead of the relative $L_{\infty}$ norm is much more mathematically rigorous, and for this reason the relative metric option is deprecated.

The metric that controls the $L_p$ norm of the interpolation error takes the form \citep{chen2007,loseille2010ii}
\begin{equation}
M = \det\left|H\right|^{-\frac{1}{2p+n}} \frac{\left|H\right|}{\epsilon} \, ,
\end{equation}
where $p \in \mathbb{Z}$ and $n$ is the dimension of the space.

The options for the metric are selected by field and can be found under \option{name\_of\_field/adaptivity\_options}. For more information see section \ref{sec:configuring_fluidity_error_metric}.

\subsection{Weights} \label{sec:adaptivity_weights}
The target value of the norm of the interpolation error is set in the \option{InterpolationErrorBound}
field under the \option{\ldots/adaptivity\_options} for a particular field. Usually, this
will be constant throughout space and time, but advanced users may wish to vary this, and so
it is possible to set this value as a Python field. When configuring an adaptive simulation
for the first time, the general advice would be to start with a high weight and thus a high
interpolation error ($10\%$ of the range of the field is a good rule of thumb), and reduce the weight as necessary to represent the desired dynamics.
In all cases, the field weights should be the main parameters to vary to control the resolution
of the adapted meshes.

\subsection{Gradation parameter} \label{sec:gradation_parameter}
In numerical simulations, a smooth transition from small elements to large elements is generally important for mesh quality.
A mesh sizing function derived from error considerations may
yield sudden changes in desired mesh edge length, due to the
nature of the problem being resolved. Such sudden changes are undesirable
in a mesh:
for example, sudden changes in mesh sizing
can cause the spurious reflection of waves \citet{bazant1978,bangerth2001}.
Therefore, a mesh gradation algorithm
is applied to smooth out sudden variations in the mesh sizing function.

Various mesh gradation algorithms have been introduced to solve this problem.
\citet{lohner1996} uses various functions of distance to point sources where edge
length is specified by the user to control the isotropic sizing function for an
advancing front grid generator. \citet{owen2000} applies natural neighbour
interpolation to smooth sudden variations in an isotropic sizing function.
\citet{persson2006} bounds the gradient of an isotropic sizing function by
solving a partial differential equation. \citet{borouchaki1998} introduced two
gradation algorithms for scalar isotropic mesh sizing functions, bounding the
gradient of the sizing function or the ratio of the length of two adjacent
edges, along with anisotropic generalisations of these. These algorithms have
been successfully applied in many diverse application areas (e.g.
\citet{frey2004, alauzet2003, laug2002, lee2003}).

\fluidity\ uses an algorithm based on that of \citet{borouchaki1998}. It has only one
user-configurable parameter, \option{/mesh\_adaptivity/hr\_adaptivity/enable\_gradation/gradation\_parameter}.
This number constrains the rate of growth in desired edge lengths along an edge. A value
of 1 would force the mesh to have constant edge length everywhere. A value of 2 would allow
desired edge lengths to double from node to node. The default value is 1.5. Optionally, the
algorithm may be disabled by choosing \option{/mesh\_adaptivity/hr\_adaptivity/disable\_gradation}
instead of \option{/mesh\_adaptivity/hr\_adaptivity/enable\_gradation}.

\subsection{Maximum and minimum edge length tensors} \label{sec:max_min_edge_length}
For robustness of the mesh adaptivity procedure, and to limit refinement/coarsening of the
mesh it is possible to set maximum and minimum allowed edge length sizes. The input to
these quantities are tensors allowing one to impose different limits in different directions.
Assuming that these directions are aligned with the coordinate axes allows one to define
diagonal tensors.

There are both good and bad reasons that one may need to impose these constraints. The good
reasons are based on physics: a maximum size may be based on the size of the domain, or that it
resolves a spatial scale that allows an instability to develop (the latter case would be better
handled with a more advanced \emph{a posteriori} error measure of course), or simply a consequence of
the time/memory constraints of the machine the problem is running on. The bad reason is to control
the mesh when the field weights have been chosen poorly. However, it is often
unavoidable that the weights and max/min edge length sizes will be chosen in tandem to achieve
an appropriate mesh, especially for experienced users --- new users should be wary whenever maximum
and particularly minimum size constraints are actually hit in the mesh and as a first stage should
look to vary the weights to achieve the mesh they desire.

Finally, note that these constraints are achieved through manipulations to the metric, which in turn
controls an optimisation procedure. They are therefore not hard constraints and one may observe the
constraints being broken (slightly) in places.

\subsection{Maximum and minimum numbers of nodes} \label{sec:max_min_nodes}
Similar to the edge length size constraint above, it is possible to limit the maximum and minimum number of nodes
that the mesh optimisation procedure returns. For reasons very similar to above this is potentially dangerous,
but somewhat necessary.

This is effected by computing the expected number of nodes from the given metric. If the expected
number of nodes is greater than the maximum number of nodes, the metric resolution is homogenously
increased so that the expected number of nodes is the maximum number of nodes. Similarly, if a minimum
limit on the number of nodes is employed, the metric resolution is homogenously decreased if the metric
would yield a mesh with fewer nodes.

For new users, altering the weights should be the primary way to control the
size of the adapted mesh.

\subsection{Metric advection}
\label{section:metric_advection_general}
Metric advection is a technique that uses the current flow velocity to advect the metric forward in time over the period until the next mesh adapt. This allows an estimate of the mesh resolution required at each time--step before the next adapt to be obtained and incorporated into the adapted mesh \citep{wilson_phdthesis_2009}. Each component of the metric is advected as a scalar using a control volume scheme with the time--step determined by a specified Courant number section \ref{sec:ND_control_volume_advection}.

With metric advection, mesh resolution is `pushed ahead' of the flow such that, between mesh adapts, the dynamics of interest are less likely to propagate out of the region of higher resolution. This leads to a larger area that requires refinement and, therefore, an increase in the number of nodes. However, metric advection can allow the frequency of adapt to be reduced whilst maintaining a good representation of the dynamics

In the lock--exchange example, section \ref{sec:lock_exchange}, consider an adapted mesh with high resolution in the region of the interface between the two fluids. As the simulation proceeds, the gravity current fronts may propagate out of this high resolution region before the next adapt. This can lead to a more diffuse interface due to increased numerical diffusion from the advection method at coarser resolutions. Metric advection would increase the resolution in the adapted mesh not only in the interface, but also ahead of it, in the region into which the gravity current fronts will propagate.

The options for metric advection can be found under \option{/mesh\_adaptivity/hr\_adaptivity/metric\_advection}, section \ref{sec:configuring_fluidity_metric_advection}.

\section{Interpolation} \label{sec:interpolation_algorithms}
As mentioned previously, the application of adaptive remeshing
divides naturally into three sub-problems. The first is to decide what mesh
is desired; the second is to actually generate that mesh.
The third, discussed here, is how to interpolate any necessary
data from the previous mesh to the adapted one.

This problem has received less attention from the adaptive remeshing
community, with consistent interpolation (interpolation by basis function
evaluation) almost universally
used. Many papers in the adaptive remeshing literature do not even mention
its use.

There are several good reasons for this. The drawbacks of consistent
interpolation can be summarised as having suboptimal interpolation error,
its unsuitability for discontinuous fields, and its lack of conservation.
Firstly, for stationary problems, the
interpolated solution is only used as an initial guess for the next solve,
so any errors introduced in the interpolation have a minimal effect.
Secondly, even for transient simulations, the interpolation error introduced
is often acceptably low, provided the adapted mesh is suitable for the representation
of the data. Thirdly, its unsuitability for
discontinuous solutions and its loss of conservation are unimportant for
the majority of applications of adaptive remeshing.

Nevertheless, there are good reasons to consider the mesh-to-mesh interpolation
problem. Firstly, computing the interpolation with optimal accuracy in the $L_2$
norm is an interesting mathematical question in its own right. Secondly, consistent interpolation is unsuited to discontinuous Galerkin methods,
which are increasingly popular. For these cases, consistent interpolation is not defined, and the averaging inherent in the pseudo-interpolation
operators is diffusive and cannot exploit discontinuous functions in the target
function space. Thirdly, consistent interpolation is inherently nonconservative,
which is a key requirement for the discretisation of certain problems.
Without a conservative interpolation operator available, adaptive remeshing
cannot be applied to these problems.

The standard method, consistent interpolation, consists of evaluating
the previous solution at the locations of the nodes in the adapted
mesh, and taking these values as the coefficients of the associated shape
functions. As basis function evaluation is trivially available for
any finite element method, the only difficulty is the problem of mesh
association: the identification of which basis functions to evaluate for
a given node in the adapted mesh, i.e. to identify in which element of
the previous mesh each node of the adapted mesh lies. The relevant element
is referred to as the parent element of the node.

\citet{peraire1993} discuss interpolation between meshes in the context
of non-nested multigrid methods. The authors observe that Galerkin projection
is optimal in the $L_2$ norm, note that its assembly necessitates computing
the inner products of the basis functions of both meshes, and comment that
this computation is very difficult because the basis functions are defined on different
supports. No mention of mesh intersection is made; however, the authors demonstrate
that if the inner products are approximated with numerical quadrature on the donor
mesh, the resulting approximate projection is still conservative. Despite
this conservation property, the use of this procedure to compute the inner
products is discouraged as it is very inaccurate.

\citet{lohner1995} discusses the mesh association problem in detail.
The author discusses brute-force searching, methods of subdividing
space, and develops an advancing-front vicinity searching algorithm.
The algorithm exploits the connectivity of the target and donor meshes.
Since adjacent nodes in the target will lie in nearby elements in the donor mesh,
the algorithm uses the parenthood information for nodes which have already
been interpolated to provide clues for the search for the parent of unprocessed
nodes.

\citet{george1998} discuss the necessity
of solution interpolation after adaptive remeshing, note the
non-conservative character of consistent interpolation, and propose
the use of the Galerkin projection from mesh to mesh by means of
mesh intersection. Galerkin projection is the optimally accurate
projection in the $L_2$ norm, and is conservative, but its implementation
is very difficult. The fundamental reason for this difficulty is that
the method requires the computation of the inner products of the basis
functions of the two meshes. In order to compute these exactly,
the supermesh of the two meshes must be constructed, which is quite involved.
Although they comment that in their experience
this provides a satisfactory algorithm for solution transfer,
they give no examples. The reader is referred to a technical report
by R. Ouachtaoui to be published in 1997 for further discussion; it appears,
however, that this technical report was never published. \citet{geuzaine1999}
also discuss the Galerkin projection between two-dimensional meshes; however,
rather than integrating over the supermesh, the integrals appear to be
computed over the target mesh.
This is less accurate than assembling over the supermesh,
and therefore should be referred to as an approximate Galerkin projection.
A similar approach is taken by \citet{parent2008}.

\citet{farrell2009a} was the first to present the application of
supermeshing to adaptive remeshing, and the first to describe
a bounded variant of the Galerkin projection. The supermeshing
algorithm was drastically improved in \citet{farrell2009c}, and
this paper describes the Galerkin projection algorithm used in \fluidity.

In summary, the choice should be consistent interpolation, unless
any of the following conditions hold:
\begin{itemize}
\item The simulation has a discontinuous prognostic field which must be interpolated.
\item Conservation of some field is crucial for the dynamics.
\end{itemize}
In such cases, Galerkin projection should be applied. If both conservation
and boundedness are desired, a bounded variant of the Galerkin projection
algorithm is available \citep{farrell2009a}, but this is only implemented
for \Pone fields.

The default choice is consistent interpolation, as specified by
\option{\ldots/consistent\_interpolation}. If Galerkin projection
is to be used, change this to \option{\ldots/galerkin\_projection}. Under
\option{galerkin\_projection}, choose either \option{continuous} or \option{discontinuous},
depending on whether the field exists on a continuous or discontinuous mesh respectively.
(In the continuous case, Galerkin projection involves the solution of a linear system
involving the target mesh mass matrix, which is why additional options under \option{\ldots/galerkin\_projection/continuous/solver} are required
to configure how this linear system is solved.) If Dirichlet conditions should be enforced through the Galerkin projection
procedure, set the \option{\ldots/galerkin\_projection/honour\_strong\_boundary\_conditions} option.

If the bounded variant of Galerkin projection is desired, activate the
\option{\ldots/galerkin\_projection/continuous/bounded::Diffuse} option. The algorithm
to bound the Galerkin projection is iterative, and the user must set a limit on the number
of iterations it will perform with \option{boundedness\_iterations}. If the bounds are known
\emph{a priori}, then these may be specified in the \option{bounds} option; otherwise, the
bounds are derived from the field values before interpolation.

\section{Parallel adaptivity}
\label{sec:parallel_adaptivity}
\index{zoltan}

The approach taken to adaptivity in parallel is as follows:

\begin{itemize}
\item Each process adapts their local mesh, excluding halo elements.
\item The mesh is re-partitioned with high edge-weighting applied to those
elements below the element quality cutoff.
\item Repeat the two steps above up to \option{adapt\_iterations} (default value of $3$).
\item Finally re-partition without applying edge-weighting to return a
load balanced mesh.
\end{itemize}

Zoltan is a parallel partitioning and data distribution library
\citep{devine2002}. It is used within \fluidity\ to do the mesh re-
partitioning during the parallel adaptivity. Zoltan gives access to
various different partitioning libraries; ParMETIS, PT-Scotch as well as
its own graph and hypergraph partitioners. For the intermediate adapt iterations
(when edge-weighting is applied) the partitioner to be used by default
is Zoltan graph and this can be changed using the \option{partitioner} option
in Diamond. For the final adapt iteration where load balance is our
only concern the default partitioner is ParMETIS and this can be
changed using the \option{final\_paritioner} option.

The high edge-weighting being applied to poor quality elements aims to
prevent those elements being on a partition boundary after the
re-partitioning. This is to prevent them being locked for the next
adapt iteration. Zoltan gives priority to producing load balanced
partitions so for these intermediate adapts we loosen the Zoltan
parameter, load imbalance tolerance, to allow Zoltan to produce load
imbalanced partitions but take into account the edge-weighting we are
applying. The default load imbalance tolerance is 1.5 but this can be
changed in the Zoltan options.

More information on the Zoltan options can be found in section \ref{sec:configuring_fluidity_zoltan_options}

\section{The cost of adaptivity}

The operations involved with adapting the mesh (forming the metric, remeshing, interpolation) have an associated computational cost. Whilst, in most cases, this overhead will be small, it should be noted. In general, the cost associated with formation of the absolute, relative and $p$--metrics is the same as the time is spent in formation of the Hessian, which is required for all three. Metric--advection will increase the time spent in metric formation due to the additional solutions of the advection equation required. The cost will depend on the chosen CFL number. Finally, consistent interpolation is faster than Galerkin projection (due to the formation of the supermesh). Bounded Galerkin projection will also require more time than unbounded Galerkin projection due to the bounding procedure.

The percentage of time spent in the adaptivity routines for simulations of the lock--exchange are given in table \ref{table:adaptivity_cost}, section \ref{sec:lock_exchange}. These values should be taken as a guide rather than a definitive value for all simulations. For example, the number of iterations used in the linear solve of the bounding procedure for Galerkin projection (and hence cost) will depend not only on the interpolation step but on how well bounded the solution is to begin with which in turn depends on the choice of discretisation method.

\begin{table}[h]
\centering
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
\multicolumn{4}{|c|}{Adaptivity options} & \multicolumn{3}{c|}{$\%$ of total simulation time spent in adaptivity routines} \\ \hline
Metric & Adapt & Interpolation & Metric      &   Metric formation    & Remesh & Total          \\
                & frequency & method & advection &                       &  and interpolation    &                \\ \hline
$M_\infty$ & 10 & CI & no              &  4.0                  &    3.5               &   7.5               \\
$M_\infty$ & 10 & CI & yes -- 5              &  25.7                 &    2.2                & 27.9             \\
$M_\infty$ & 10 & CI & yes -- 10            & 18.5                 &    2.6                & 21.1             \\
$M_\infty$ & 40 & CI & yes -- 5           &  29.5                 &    0.7                &   30.1         \\
$M_\infty$ & 40 & CI & yes -- 10           &  17.8                &   0.9                &  18.7            \\
 $M_\infty$ & 10 & bd GP & no          & 3.5                   &   14.0                &  17.5             \\
$M_2$ & 10 & CI & no	& 4.1			& 3.4			& 7.5		 \\ \hline
\end{tabular}
\caption[Percentage of total simulation run time spent in adaptive mesh routines for adaptive mesh simulations of the lock--exchange.]{Percentage of time spent in adaptivity routines for adaptive mesh simulations of the lock--exchange, section \ref{sec:lock_exchange} \citep{hiester2011}. The simulations were run from $t=0\,$s until $t=25.025\,$s, the time the free--slip head nears the end wall. This is equivalent to 1001 time--steps and 100 adapts when adapting every 10 time--steps and 25 adapts when adapting every 40 time--steps. The adapt frequency is given in number of time steps. For the interpolation method CI: consistent interpolation and bd GP: bounded Galerkin projection. For those simulations that use metric advection the number given is the CFL number used to determine the time step used in the calculation of advection of the metric components.}
\label{table:adaptivity_cost}
\end{table}


\subsection{Effects of the Pixel Size and Time Step Length}
\label{pixels_intervals}

As elaborated in Sub-section \ref{grid}, more order aggregation leads to a
    higher overall demand level and an improved pattern recognition in the
    generated time series.
Consequently, individual cases tend to move to the right in tables equivalent
    to Table \ref{t:results}.
With the same $ADD$ clusters, forecasts for pixel sizes of $2~\text{km}^2$ and
    $4~\text{km}^2$ or time intervals of 90 and 120 minutes or combinations
    thereof yield results similar to the best models as revealed in Tables
    \ref{t:results}, \ref{t:hori}, \ref{t:vert}, and \ref{t:ml} for high
    demand.
By contrast, forecasts for $0.5~\text{km}^2$ pixels have most of the cases
    (i.e., $n$) in the no or low demand clusters.
In that case, the pixels are too small, and pattern recognition becomes
    harder.
While it is true, that \textit{trivial} exhibits the overall lowest MASE
    for no demand cases, these forecasts become effectively worthless for
    operations.
In the extreme, with even smaller pixels we would be forecasting $0$ orders
    in all pixels for all time steps.
In summary, the best model and its accuracy are determined primarily by the
    $ADD$, and the pixel size and interval length are merely parameters to
    control that.
The forecaster's goal is to create a grid with small enough pixels without
    losing a recognizable pattern.