%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\input{../header.tex}
\input{../newcommands.tex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%\preprint{}

\title{Streamline integration as a method for two-dimensional elliptic grid generation}
\author{M.~Wiesenberger, M.~Held, and L.~Einkemmer}
%\email{Matthias.Wiesenberger@uibk.ac.at}
%\affiliation{Institute for Ion Physics and Applied Physics, Association EURATOM-\"OAW,  University of
%   Innsbruck, A-6020 Innsbruck, Austria}
%\affiliation{Institute for Ion Physics and Applied Physics, Association EURATOM-\"OAW,  University of
   %Innsbruck, A-6020 Innsbruck, Austria}
%\address{Institute for Ion Physics and Applied Physics,  Universit\"at
   %Innsbruck,  A-6020 Innsbruck, Austria}
%\address{Numerical Analysis group,  Universit\"at Innsbruck, A-6020 Innsbruck, Austria}

\maketitle
\begin{abstract}
  [This writeup is based/copied on the recent article \cite{Wiesenberger2017}] \\
We propose a new numerical algorithm to construct a structured
numerical elliptic grid of a doubly connected domain. Our method is
applicable to domains with boundaries defined by two contour lines of a two-dimensional function.
Furthermore, we can adapt any analytically given boundary
aligned structured
grid, which specifically includes polar and Cartesian grids.
The resulting coordinate lines are orthogonal to the boundary.
Grid points as well as
the elements of the Jacobian matrix can be computed efficiently and up to machine precision.
In the simplest case we construct conformal grids, yet with the help of weight functions and monitor metrics we can control the
distribution of cells across the domain.
Our algorithm is parallelizable and easy to implement with
elementary numerical methods.
We assess the quality of grids by considering both the distribution of cell sizes and the accuracy of the solution to
elliptic problems.
Among the tested grids these key properties are best fulfilled by the grid constructed with the monitor metric approach.
%All in all, the grid constructed with a monitor metric
%yields the best accuracy and cell sizes compared

%Simple flux aligned orthogonal grids are suitable for the solution
%of flux aligned problems, but they exhibit
%very large ratios of maximal to minimal cell size.
%If we construct a conformal grid, the aspect ratio of the cells is constant by construction. However, the variation in cell size is large
%and the errors of the elliptic equation are high.
%The adapted grid and the grid with monitor metric yield smaller variation in size and smaller errors, where
%the monitor grid overall has the smallest ratios of maximal to minimal cell size.
%The errors and cell sizes are competitive with a previously suggested near conformal grid.
%
%It is based on the integration of the streamlines of the two vector fields that
%form the basis of the coordinate system.
%These vector fields are either built directly from the given function or from the solution of a suitably chosen elliptic equation (which
%can be solved once an initial grid has been constructed).
%We are able to construct conformal, orthogonal and curvilinear coordinates.
%The method is parallelizable and the metric elements can be computed with high accuracy.
%Furthermore, it is easy to implement as only the integration
%of well-behaved ordinary differential equations and the inversion
%of a linear elliptic equation are required.
%All our grids are orthogonal to the boundary of the domain, which is the major
%advantage over previously suggested grids.
\end{abstract}

\section{High precision elliptic grid generation} \label{sec:geometry}

Given is a function $\psi(x,y)$ in Cartesian coordinates.
We want to construct a grid on the region
bounded by the two lines
$\psi(x,y) = \psi_0$ and $\psi(x,y)=\psi_1$ with $\psi_0\neq\psi_1$.
The derivative of the function $\psi$ may not vanish within this region and on the boundary and
we further assume that the region is topologically a ring.
Note here that this excludes the description of domains with an X-point (saddle point) or O-point (local extremum).
The numerical grid is described by a mapping of the discretization of the rectangular
computational domain $(u,v) \in [0,u_1]\times[0,2\pi]$ to the physical domain $(x,y)$. $u_1$ is an unknown, which the grid generation process has to provide.

%Together with
%this mapping described by $x_i = x(u_i, v_i),\ y_i = y(u_i, v_i)$ we construct the derivatives
%$u_x(u_i,v_i),\ v_x(u_i, v_i),\ u_y(u_i, v_i),\ v_y(u_i,v_i)$ in order to
%transform tensors to the curvilinear coordinate system.
%Here and in the following we use the notation $u_x\equiv \frac{\partial u}{\partial x}$.
%The index $i$ is given by $i=0,1,\dots N_uN_v$, where $N_u$ and $N_v$ are the number of points
%in $u$ and $v$ respectively. The discretization of the computational domain need
%not be equidistant. We will for example use the Gauss-Legendre nodes
%needed for a discontinuous Galerkin discretization.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[htbp]
\centering
\includegraphics[trim = 0px 0px 0px 0px, clip, scale=1.0]{./drawing_magenta}
\caption{
  Sketch of the coordinate systems, coordinate lines and basis vector fields
  involved in our method.
}
\label{fig:sketch}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
As mentioned in the introduction and illustrated in Fig.~\ref{fig:sketch} the idea of our algorithm are two consecutive coordinate transformations constructed by streamline integration.
We denote the first transformation with $x(\zeta, \eta)$, $y(\zeta, \eta)$, where
the $\zeta$ coordinate is aligned with $\psi$ and $\eta$ is an angle-like coordinate. The solution of the
elliptic equation on this flux-aligned coordinate system is denoted with $\bar u(\zeta, \eta)$.
The second coordinate transformation is then denoted $\zeta(u,v)$, $\eta(u,v)$ with $u$ aligned to $\bar u$.

When deriving the algorithm we use basic methods and notational
conventions of differential geometry.
Here, we recommend the excellent Reference~\cite{Frankel} as an introduction to the topic.
We do so since in this approach the separate roles of the metric tensor,
the coordinate system and its base vector fields are very clear. This is
paramount for a concise description of our method.

In this Section we first show how the
basis vector fields can be integrated to construct coordinate lines
 with the example of orthogonal coordinates in Section~\ref{sec:orthogonal}. This follows
an introduction of some helpful quantities, our notation and streamlines
in Section~\ref{sec:preliminaries}.
%In Section~\ref{sec:orthogonal} we review the
%construction of orthogonal coordinates via the integration of
%streamlines, that is ordinary differential
%equations.
In the next step we transform, discretize and solve an elliptic equation on the flux aligned
coordinate system.
The solution $\bar u(\zeta, \eta)$ then takes the role of a new flux function in the flux aligned coordinates.
We can therefore repeat the streamline integration method in the flux aligned
coordinate system in order to construct coordinate lines of the final $u,v$ coordinates.
We discuss three examples of this method.
%In Section~\ref{sec:laplace} we discretize and
%solve the Laplace equation.
%with local discontinuous Galerkin (dG) methods.
In
Section~\ref{sec:conformal} we consider the simple Laplace and the Cauchy-Riemann equations~\eqref{eq:CR}
in order to construct conformal coordinates.
In Section~\ref{sec:adaption} we modify the elliptic equation to allow grid adaption, before
we introduce the monitor metric in Section~\ref{sec:metric}.
Finally, we present our algorithm in Section~\ref{sec:elliptic}.

\subsection{Preliminaries} \label{sec:preliminaries}
In a curvilinear
coordinate system $(\zeta,\eta)$ the components of the metric tensor and its inverse
take the form
\begin{align}
  \vec g(\zeta,\eta) = \begin{pmatrix}
    g_{\zeta\zeta} & g_{\zeta\eta}  \\
    g_{\zeta\eta} & g_{\eta\eta}
  \end{pmatrix}
  \quad
  \vec g^{-1}(\zeta,\eta) = \begin{pmatrix}
    g^{\zeta\zeta} & g^{\zeta\eta}  \\
    g^{\zeta\eta} & g^{\eta\eta}
  \end{pmatrix}
  \label{}
\end{align}
We denote $g = \det{\vec g} = g_{\zeta\zeta}g_{\eta\eta}-g_{\zeta\eta}^2$.
For Cartesian coordinates $(x,y)$ the elements of the inverse metric tensor are
transformed by
\begin{subequations}
\begin{align}
  g^{\zeta\zeta} &=  \zeta_x^2+\zeta_y^2\\
  g^{\zeta\eta} &=  \zeta_x \eta_x+\zeta_y \eta_y\\
  g^{\eta\eta} &=  \eta_x^2+\eta_y^2
  \label{}
\end{align}
\end{subequations}
since $g^{xx}=g^{yy}=1$ and $g^{xy}=0$.
Given $\zeta(x,y)$, $\eta(x,y)$ and its inverse $x(\zeta,\eta)$, $y(\zeta,\eta)$ recall that their Jacobian matrices are related by
\begin{align}
  \begin{pmatrix}
    x_\zeta & x_\eta \\
    y_\zeta & y_\eta
  \end{pmatrix} \bigg\rvert_{\zeta(x,y),\eta(x,y)}
  =
  \frac{1}{\zeta_x\eta_y - \zeta_y\eta_x }\begin{pmatrix}
    \eta_y & -\zeta_y \\
    -\eta_x & \zeta_x
  \end{pmatrix}\bigg\rvert_{x,y}
  \label{eq:inverse}
\end{align}
With the rules of tensor transformation it is easy to prove that the element of the volume form $\sqrt{g}$ is related to the Jacobian via
\begin{align}
  %\mathcal V^2:=\d x\wedge \d y = (x_uy_v - y_ux_v)\d u\wedge\d v = \frac{\d u\wedge\d v}{u_xv_y - u_yv_x} \equiv \sqrt{g} \d u\wedge \d v
%\iint \d V:= \iint \d x \d y = \iint (x_uy_v - y_ux_v)\d u\d v &=  \nonumber\\
%\iint\frac{\d u\d v}{u_xv_y - u_yv_x} &\equiv \iint \sqrt{g} \d u\d v
\sqrt{g} = (x_\zeta y_\eta-y_\zeta x_\eta) = (\eta_y\zeta_x -\eta_x\zeta_y)^{-1}
  \label{eq:vol}
\end{align}
The components of the gradient operator and the divergence in an arbitrary coordinate
system read
\begin{subequations}
\begin{align}
  \left( \nabla f \right)^i &= g^{ij}\partial_j f \\
  \nabla \cdot \vec A &= \frac{1}{\sqrt g}\partial_i \left( \sqrt{g} A^i  \right),
\end{align}
  \label{eq:arbitrary}
\end{subequations}
where we sum over repeated indices $i,j\in\{\zeta,\eta\}$ and define $\partial_\zeta \equiv \partial/\partial \zeta$, $\partial_\eta \equiv \partial/\partial \eta$.
%Note that
%\begin{align}
%  \d x = x_R \d R + x_Z \d Z\\
%  \d y = y_R \d R + y_Z \d Z
%  \label{}
%\end{align}
%and
%\begin{align}
%  \partial_x = R_x \partial_R + Z_x \partial_Z\\
%  \partial_y = R_y \partial_R + Z_y \partial_Z
%  \label{}
%\end{align}
%Figuratively $\d x$ and $\d y$ are the one-forms that form the lines of constant $x$ and $y$, while the coordinate lines are given by the streamlines of $\partial_x$ and $\partial_y$.
%Recall that the coordinate line $x$ is implicitly defined by the points with $y=const$ and vice versa\cite{Frankel}.
% Through the inverse derivatives
%Eq.~\eqref{eq:inverse} $\partial_y$ is related with $\d x$ and $\partial_x$ is
%related with $\d y$.
%
%The contravariant vectors $\nabla x$ and $\nabla y$ are the vectors that are everywhere perpendicular to $\d x$ and $\d y$.
%$\nabla x$ and $\nabla y$ are in general not parallel to $\partial_x$ and $\partial_y$, yet\footnote{ Some textbooks use the notation $e_x = \partial_x$ and $e^x=\nabla x$}
%\begin{align}
% \partial_y \cdot \nabla y = \partial_x \cdot \nabla x = 1\\
% \partial_x \cdot \nabla y = \partial_y \cdot \nabla x =0
%  \label{}
%\end{align}
%Now note that in order to reconstruct $R(x,y)$, $Z(x,y)$ we have to integrate the
%coordinate lines i.e. $\partial_x$ and $\partial_y$.
Finally, we introduce the
geometrical poloidal angle
\begin{align}
  \theta (x,y) = \begin{cases}
    +\arccos\left( \frac{x-x_0}{\sqrt{(x-x_0)^2 + (y-y_0)^2}} \right) \text{ for } y\geq y_0 \\
    -\arccos\left( \frac{x-x_0}{\sqrt{(x-x_0)^2 + (y-y_0)^2}} \right) \text{ for } y< y_0
  \end{cases}
  \label{eq:deftheta}
\end{align}
such that the differential 1-form
\begin{align}
  \d \theta =
    -\frac{y-y_0}{(x-x_0)^2+(y-y_0)^2} \d x
    +\frac{x-x_0}{(x-x_0)^2+(y-y_0)^2} \d y,
  \label{eq:theta}
\end{align}
where $(x_0,y_0)$ is any point inside the region bounded by $\psi_0$.

Streamline integration is the central part of our algorithm. Recall that
given a vector field $v(x,y)=v^x(x,y)\partial_x + v^y(x,y)\partial_y$ its streamlines are given by the equation
\begin{subequations}
\begin{align}
  \frac{\d x}{\d t } = v^x(x,y)|_{x(t), y(t)} \\
  \frac{\d y}{\d t } = v^y(x,y)|_{x(t), y(t)}
\end{align}
\label{eq:streamlines}
\end{subequations}
where $t$ is a parameter. Recall here that in differential geometry
the directional derivatives $\partial_x$ and
$\partial_y$ are the base vector fields of the coordinate system\footnote{
  Some textbooks (e.g.~\cite{haeseleer}) introduce the notation $\vec e_i := \partial \vec x /\partial x^i$
  and $\vec e^i := \nabla x^i$.
  While this formulation is suitable in many situations we refrain from
  using it since it mixes the metric into the basis vectors through the use of
  the gradient (cf. Eq.~\eqref{eq:arbitrary}).
  This is unpractical for our purposes.}.
Now recall that if we have a function $f(x,y)$
such that $f(x(t), y(t))$ is a one-to-one map from $t$ to $f$ we can re-parameterize
Eq.~\eqref{eq:streamlines} by
\begin{subequations}
\begin{align}
  \frac{\d x}{\d f } = \frac{\d x/\d t|_{t(f)}}{\d f/\d t|_{t(f)}} =
  \frac{v^x(x,y) }{ (v^x\partial_xf + v^y\partial_yf)(x,y) }\bigg |_{x(f), y(f)}\\
  \frac{\d y}{\d f } = \frac{\d y/\d t|_{t(f)}}{\d f/\d t|_{t(f)}} =
  \frac{v^y(x,y) }{ (v^x\partial_xf + v^y\partial_yf)(x,y) }\bigg |_{x(f), y(f)}
\end{align}
\label{eq:reparameter}
\end{subequations}
Furthermore, the derivative of any function $g(x,y)$ along the streamlines of $v$
parameterized by $f$ reads
\begin{align}
  \frac{\d g}{\d f }\bigg |_{x(f),y(f)} =
  \frac{(v^x\partial_x g + v^y\partial_y g)(x,y) }{ (v^x\partial_xf + v^y\partial_yf)(x,y) }\bigg |_{x(f),y(f)}
  \label{}
\end{align}

Figuratively, in any coordinate system $(\zeta,\eta)$ the 1-forms $\d \zeta$ and $\d \eta$ (the contravariant basis) are visualized by the lines (surfaces in higher dimensions)
of constant $\zeta$ and $\eta$.
At the same time the streamlines of the vector fields $\partial_\zeta$ and $\partial_\eta$ (the covariant basis)
give the coordinate lines of $\zeta$ and $\eta$ (cf. Fig.~\ref{fig:sketch}).
For example, if we hold $\eta$ constant and vary $\zeta$, we go along a streamline of
$\partial_\zeta$. This implies that in two dimensions $\d \eta$ and $\partial_\zeta$
trace the same line. The vector fields $\nabla \zeta$ and $\nabla \eta$ are associated to
$\d \zeta$ and $\d \eta$ through the metric tensor by Eq.~\eqref{eq:arbitrary} and are the vector fields that
are everywhere perpendicular to the lines of constant $\zeta$ and $\eta$, respectively.
It is important to realize that in general curvilinear coordinates the vector
fields $\nabla \zeta$ and $\nabla \eta$ point in
different directions than $\partial_\zeta$ and $\partial_\eta$.
The central point in our algorithm is the realization that once we can
express $\partial_\zeta$ and $\partial_\eta$ in terms of $\partial_x$ and
$\partial_y$ we can immediately construct the coordinate transformation
by integrating
streamlines of $\partial_\zeta$ and $\partial_\eta$ using Eq.~\eqref{eq:streamlines}. This
holds true even if $(x,y)$ were curvilinear coordinates. The components of the
one-forms $\d \zeta$ and $\d\eta$ in terms of $\d x$ and $\d y$ form
the elements of the Jacobian matrix of the transformation. These
are also necessary in order to transform any tensor (including the metric)
from the old to the new coordinate system.

\subsection{Orthogonal coordinates} \label{sec:orthogonal}
In general, orthogonal coordinates $\zeta, \eta$ with $\zeta$ aligned to $\psi$ are described by
\begin{subequations}
\begin{align}
  \d \zeta & = \zeta_x\d x +\zeta_y\d y =  f(\psi)(\psi_x \d x + \psi_y \d y) \\
  \d \eta  & = \eta_x \d x + \eta_y \d y = h(x,y) ( -\psi_y \d x + \psi_x \d y)
\end{align}
  \label{eq:orthogonal}
\end{subequations}
With Eq.~\eqref{eq:orthogonal} we have $g^{\zeta\eta} = \zeta_x\eta_x + \zeta_y\eta_y = 0$,
$g^{\zeta\zeta} = (\nabla\psi)^2 f^2$, $g^{\eta\eta} = (\nabla\psi)^2h^2$ and $\sqrt{g}^{\,-1} = (\nabla\psi)^2 h f$.
From Eq.~\eqref{eq:inverse} we directly see that the basis vector fields are
\begin{subequations}
\begin{align}
  \partial_\zeta&= x_\zeta\partial_x + y_\zeta\partial_y = \frac{1}{(\nabla\psi)^2f} (\psi_x \partial_x + \psi_y\partial_y) \\
  \partial_\eta &=  x_\eta\partial_x + y_\eta\partial_y =  \frac{1}{(\nabla\psi)^2h} (-\psi_y \partial_x + \psi_x\partial_y)
\label{eq:orthogonal_linesb}
\end{align}
\label{eq:orthogonal_lines}
\end{subequations}
i.e. $\partial_\zeta$ points into the direction of the gradient of $\psi$ and $\partial_\eta$ into the direction of constant $\psi=\text{const}$ surfaces.
Now, the coordinate system is defined up to the functions $f(\psi)$ and $h(x,y)$.
Note that $f$ must be a function of $\psi$ only since the restriction $\d(\d \zeta)=0$ must hold.
Furthermore, $f(\psi) = \d\zeta/ \d\psi \neq 0 $ is in principle an arbitrary function, yet we choose $f(\psi) = f_0 = \text{const}$.
With this choice we directly get
\begin{align}
  \zeta(x,y) = f_0(\psi(x,y)-\psi_0)
  \label{eq:zeta}
\end{align}
Note that $\zeta_0=0$ and $\zeta_1=f_0(\psi_1-\psi_0)$.
The up to now undefined function $h(x,y)$ is not arbitrary since $\d(\d \eta) = 0$ must hold. This is the requirement that $\d \eta$ must be
a closed form in order for the potential $\eta$ to exist.
We can express this as
\begin{align}
  (\psi_x\partial_x + \psi_y\partial_y) h = f(\nabla\psi)^2 \partial_\zeta h= -h\Delta \psi
  \label{eq:hequation}
\end{align}
where $\Delta \psi = \psi_{xx} + \psi_{yy}$ is the two-dimensional Laplacian.
Let us remark here that Eq.~\eqref{eq:hequation} can be written as $\nabla\cdot\left( h\nabla\psi \right)=0$, which makes the orthogonal grid an elliptic grid
with adaption function $h$ as becomes evident later.
In order to integrate this equation we need an initial condition for $h$.
We choose to first discretize the line given by $\psi(x,y) = \psi_0$.

As already mentioned $\partial_\eta$ is the vector field the streamlines of which give the
coordinate lines for $\eta$. We choose $h(x,y) = \text{const}$ on $\psi_0$. To this end
we parameterize the coordinate line by $\theta$ (cf. Eq.~\eqref{eq:reparameter})
\begin{subequations}
\begin{align}
  \left . \frac{\d x}{\d \theta}\right|_{\zeta=0}=\frac{x_\eta}{\theta_\eta} = \frac{-\psi_y}{\psi_x\theta_y - \psi_y \theta_x}\\
  \left . \frac{\d y}{\d \theta}\right|_{\zeta=0}=\frac{y_\eta}{\theta_\eta} = \frac{\psi_x}{\psi_x\theta_y - \psi_y \theta_x}\\
  \left . \frac{\d \eta}{\d \theta}\right|_{\zeta=0}=\frac{1}{\theta_\eta} = \frac{(\nabla\psi)^2h(\psi_0)}{\psi_x\theta_y - \psi_y \theta_x}
\end{align}
  \label{eq:etaline}
\end{subequations}
Let us define $h(\psi_0)$ such that $\eta \in [0,2\pi]$, that is,
\[ 2\pi = \oint_{\psi=\psi_0}\d \eta = \oint_0^{2\pi} \frac{\d \eta}{\d\theta}\bigg|_{\zeta=0}\d \theta \nonumber \]
  or
\begin{align}
  f_0 := h(\psi_0) = \frac{2\pi}{\int_0^{2\pi}\d\theta \frac{(\nabla\psi)^2}{\psi_x\theta_y - \psi_y \theta_x}}
  \label{eq:definef}
\end{align}
Here, we also fixed the constant $f_0$ such that our coordinate system $\zeta, \eta$ fulfills the Cauchy--Riemann condition~\eqref{eq:CR} on the boundary line $\psi_0$.
As initial point for the integration of Eq.~\eqref{eq:etaline} we can use any point with $\psi(x,y) = \psi_0$.
We then use $h(\psi_0)$ on the flux-surface $\psi_0$ as initial condition for the integration
of Eq.~\eqref{eq:hequation}.

We obtain coordinate lines by integrating the vector fields $\partial_\zeta$ and $\partial_\eta$ given in \eqref{eq:orthogonal_lines}. We start the construction by integrating $\partial_\eta$ for $\psi = \psi_0$, i.e. $\zeta=0$. This can be done since $h|_{\psi_0}$ is known.
The obtained points serve as starting points for the integration of $\partial_\zeta = f_0^{-1} \partial_\psi$.
In order to get $h$ we simply integrate Eq.~\eqref{eq:hequation}
%We use Nemov's algorithm \cite{Nemov1988} to compute $\eta_x$ and $\eta_y$ along the coordinate lines. Starting from $\nabla \psi\cdot \nabla \eta = 0$ we differentiate with respect to $x$ and $y$ and get
\begin{subequations}
\begin{align}
    \left .\frac{\d x}{\d \zeta}\right |_{\eta=\text{const}} &= \frac{\psi_x}{f_0(\nabla\psi)^2}\\
    \left .\frac{\d y}{\d \zeta}\right|_{\eta=\text{const}} &= \frac{\psi_y}{f_0(\nabla\psi)^2}\\
  %\frac{\d \eta_x}{\d \psi} &= -\frac{\psi_{xx}}{(\nabla\psi)^2}\eta_x - \frac{\psi_{xy}}{(\nabla\psi)^2}\eta_y, \\
  %\frac{\d \eta_y}{\d \psi} &= -\frac{\psi_{yx}}{(\nabla\psi)^2}\eta_x - \frac{\psi_{yy}}{(\nabla\psi)^2}\eta_y, \\
    \left .\frac{\d h}{\d \zeta}\right|_{\eta=\text{const}} &= - \frac{\Delta \psi}{f_0(\nabla\psi)^2} h%\\
  %\frac{\d h_x}{\d \zeta}|_{\eta=const} &= -\frac{ 2\psi_{xx}+ \psi_{yy} }{f_0(\nabla\psi)^2}h_x - \frac{\psi_{xy}}{f_0(\nabla\psi)^2}h_y - \frac{(\Delta\psi)_x}{f_0(\nabla\psi)^2} h, \\
  %\frac{\d h_y}{\d \zeta}|_{\eta=const} &= -\frac{\psi_{yx}}{f_0(\nabla\psi)^2}h_x - \frac{ 2\psi_{yy} + \psi_{xx}}{f_0(\nabla\psi)^2}h_y - \frac{(\Delta\psi)_y}{f_0(\nabla\psi)^2} h
\end{align}
  \label{eq:etacoordinates}
\end{subequations}
%The initial condition for $h_x$ and $h_y$ on $\psi_0$ are obtained as $\nabla h = - h \frac{\Delta\psi }{(\nabla\psi)^2} \nabla\psi$ from $\nabla\psi\cdot \nabla h$ and $\{\psi, h\} = 0$.
Note that if $\Delta\psi=0$, we directly get a conformal grid with this algorithm. This can be seen as then $h(\zeta,\eta) = f_0$. In~\ref{app:conf-field}
we briefly study the class of functions $\psi$ that are solutions of the Grad--Shafranov equation and satisfy $\Delta \psi=0$. This, however, is not true in general.

Let us further remark on the sign of $f_0$. It is our goal to construct a right handed
coordinate system and to have $\zeta_1>\zeta_0=0$.
The curves of constant $\theta$ surround $x_0, y_0$ in a mathematically positive direction.
That means that Eq.~\eqref{eq:definef} implies that $f_0>0$ if $\nabla \psi$ points away from $x_0, y_0$. If this is not the case, we obtain $f_0 < 0$.
On the other hand if $\zeta$ should increase from $\psi_0$ to $\psi_1$, we
need $f_0 <0$ for $\psi_1<\psi_0$ and $f_0>0$ for $\psi_1>\psi_0$.
We thus simply take the absolute value of Eq.~\eqref{eq:definef} and multiply
by $-1$ if $\psi_1<\psi_0$.
For ease of notation we do so also in the following without further notice.

Let us finally summarize the grid generation in the following algorithm; we
assume that the $\zeta$ coordinate is discretized by a list of
not necessarily equidistant values $\zeta_i$ with $i = 0,1,\dots N_\zeta-1$
and $\eta$ is discretized by a list of $\eta_j$ with $j = 0,1,\dots N_\eta-1$:
\begin{enumerate}
  \item Find an arbitrary point $(x,y)$ with $\psi(x,y) = \psi_0$
    and a point $x_0, y_0$ within the region bound by $\psi(x,y) = \psi_0$ for the definition of $\theta$ in Eq.~\eqref{eq:deftheta}
  \item Integrate Eq.~\eqref{eq:etaline} with $h=1$ over $\Theta=[0,2\pi]$ and use Eq.~\eqref{eq:definef} to compute $f \equiv f_0$ and $h(\psi_0)$.
    Use any convenient method for the integration of ordinary differential equations.
  \item Integrate one streamline of Eq.~\eqref{eq:orthogonal_linesb} with $h=f_0$
    from $\eta = 0\dots\eta_j$ for all $j$.
    The result is a list of $N_\eta$ coordinates $x(0,\eta_j), y(0,\eta_j)$ on the $\psi_0$ surface.
  \item Using this list and $h=f_0$ as starting values integrate Eq.~\eqref{eq:etacoordinates}
    from $\zeta=0\dots\zeta_i$ for all $i$ and all $\eta_j$. This gives the map $x(\zeta_i, \eta_j), y(\zeta_i, \eta_j)$ as well as $h(\zeta_i,\eta_j)$ for all $i$ and $j$.
  \item Last, using these results and
    Eq.~\eqref{eq:orthogonal} evaluate the derivatives
    $\zeta_x(\zeta_i,\eta_j)$, $\zeta_y(\zeta_i, \eta_j)$, $\eta_x(\zeta_i,\eta_j)$, and $\eta_y(\zeta_i, \eta_j)$ for all $i$ and $j$.
\end{enumerate}

%\subsection{The Laplace equation} \label{sec:laplace}
\subsection{Conformal coordinates} \label{sec:conformal}
A conformal mapping $u(x,y), v(x,y)$ has to satisfy the Cauchy--Riemann equations given in Eq.~\eqref{eq:CR}.
A direct consequence is that $u$ and $v$ are harmonic functions
\begin{align}
  \Delta u = \Delta v = 0
  \label{eq:harmonic}
\end{align}
Here, $\Delta=\nabla^2$ is the two-dimensional Laplacian with the divergence
and gradient operators defined in~\eqref{eq:arbitrary}.
First, we note that Eq.~\eqref{eq:harmonic} holds in every coordinate system.
Let us assume that we have constructed flux aligned  coordinates $(\zeta, \eta)$. These can be, but not necessarily have to be,
the orthogonal coordinates introduced in the last section.
Now, in order to construct conformal coordinates $u$, $v$ we first define
\begin{align}
u(\zeta,\eta):=c_0(\bar u(\zeta,\eta)-\psi_0)
  \label{eq:ubar}
\end{align}
and thus
\begin{align}
  %\bar u(\zeta,\eta) = \tilde u (\zeta, \eta) + \zeta
  \Delta \bar u(\zeta,\eta) = 0 %\tilde u (\zeta, \eta) + \psi(\zeta)
  %v = \tilde v + \eta
  \label{eq:ubar_harmonic}
\end{align}
where $\bar u( 0, \eta) = \psi_0$ and $\bar u( \zeta_1, \eta) =\psi_1$ fulfills Dirichlet boundary conditions in $\zeta$.
In $\eta$ we have periodic boundary conditions.
%With these definitions we get
%\begin{align}
%  %\Delta \tilde u &= -\Delta \zeta = -f_0\Delta \psi. %\\
%  \Delta \tilde u &=  -\Delta \psi. %\\
%  %\Delta \tilde v &= -\Delta \eta = \psi_x h_y - \psi_y h_x \equiv \{ \psi, h\}
%  \label{eq:harmonic_u}
%\end{align}
%We can discretize and solve Eq.~\eqref{eq:ubar_harmonic} in the $\zeta, \eta$ coordinate system by any high order method that
%quickly converges to a solution.
%We choose a high order local discontinuous Galerkin method~\cite{Cockburn2001, Held2016} in Section~\ref{sec:numerics}.

Note the analogy between Eq.~\eqref{eq:ubar} and Eq.~\eqref{eq:zeta}. Now, $\bar u$ is
equal to $\psi$ at the boundaries and its Laplacian vanishes in between.
In fact, $\bar u$ takes the role of $\psi$ in the following coordinate
transformation.
We introduce $c_0$ as a normalization constant with the same role as $f_0$ in the orthogonal coordinate transformation.
%Once we have determined $\tilde u$ and $\tilde v$ we can construct
%the inverse $\zeta( u,v)$ and $\eta(u,v)$ by Newton iterations of the form
%\begin{align}
%  \zeta^{n+1} = \zeta^n - \frac{1}{\det J|_{\zeta^n, \eta^n}} (v_y u - u_y v)|_{\zeta^n, \eta^n}\\
%  \eta^{n+1} = \eta^n - \frac{1}{\det J|_{\zeta^n, \eta^n}} (- v_x u + u_x v)|_{\zeta^n, \eta^n}\\
%  \label{}
%\end{align}
%which is in principle a repeated interpolation of the results of the elliptic equation. The interpolation is given by and has the same order as the dG method.
%With $\zeta, \eta$ at hand we determine $x(\zeta, \eta)$, $y(\zeta, \eta)$ by
%the previous transformation.
Having $\bar u(\zeta, \eta)$,
our idea is to construct the basis one-forms $\d u$ and $\d v$  in terms
of $\d \zeta$ and $\d \eta$ by transforming the Cauchy-Riemann equations to the
$\zeta, \eta$ coordinate system. Analogues to the algorithm in Section~\ref{sec:orthogonal} we can then construct the basis vector fields $\partial_u$ and $\partial_v$, appropriately choose a normalization and then use streamline
integration in the $\zeta, \eta$ coordinate system to construct the coordinates.
From the basis one-forms $\d u$ and $\d v$ we get the elements of the Jacobian
matrix of the transformation.
Before we do this in detail however, let us first discuss some alternative elliptic
equations to the simple Eq.~\eqref{eq:harmonic}.

%\subsection{Conformal coordinates} \label{sec:conformal}
%For simplicity of the argument let us assume that $(\zeta, \eta)$ are
%the orthogonal coordinates from Section~\ref{sec:orthogonal}. We will generalize
%the approach in Section~\ref{sec:elliptic}.
%
%We can use $\bar u(\zeta, \eta)$ as well as its derivatives $\bar u_\zeta(\zeta,\eta)$ and $\bar u_\eta(\zeta,\eta)$  and Eq.~\eqref{eq:CR}
%as a starting point to construct conformal coordinates $(u,v)$
%\begin{subequations}
%\begin{align}
% \d u &= c_0 \left( \bar u_\zeta \d \zeta + \bar u_\eta \d \eta\right)= u_x\d x + u_y\d y  \\
% \d v &= c_0 \sqrt{g} \left(-g^{\eta\eta} \bar u_\eta \d \zeta + g^{\zeta\zeta} \bar u_\zeta \d \eta\right)= v_x\d x + v_y\d y = -u_y\dx + u_x\d y
%\end{align}
%\label{eq:conformal_coords}
%\end{subequations}
%%The prefactor $a$ is given by $a^2 \equiv g^{\eta\eta}/g^{\zeta\zeta} = h^2/f_0^2$.
%The rightmost equalities are proven using the orthogonality of the $(\zeta, \eta)$
%coordinates and the relation for the inverse derivatives Eq.~\eqref{eq:inverse}.
%With this it is easy to prove that $g^{uu} = g^{vv} = (\nabla u)^2 = g^{\zeta\zeta}u_\zeta^2 + g^{\eta\eta}u_\eta^2$ and $g^{uv} = 0$.
%Furthermore, we get
%\begin{subequations}
%\begin{align}
%  \partial_u &= \frac{1}{c_0(\nabla u)^2} \left(g^{\zeta\zeta}u_\zeta \partial_\zeta + g^{\eta\eta}\bar u_\eta\partial_\eta\right) \label{eq:basis_conformala} \\
%  %= \frac{1}{c_0}\frac{ \bar u_\zeta\partial_\zeta + a^2 \bar u_\eta\partial_\eta }{ \left( \bar u_\zeta^2 + a^2 \bar u_\eta^2 \right)} \\
%  \partial_v &= \frac{1}{c_0\sqrt{g}(\nabla u)^2} \left( -\bar u_\eta \partial_\zeta + \bar u_\zeta\partial_\eta\right)
%  %= \frac{1}{c_0}\frac{   -a\bar u_\eta \partial_\zeta + a\bar u_\zeta \partial_\eta }{\left( \bar u_\zeta^2 + a^2 \bar u_\eta^2   \right)}
%  \label{eq:basis_conformal}
%\end{align}
%\end{subequations}
%%with
%%$J = c_0^2/a \left( \bar u_\zeta^2 + a^2 \bar u_\eta^2\right)$.
%As for the orthogonal coordinates we have to integrate these two vector fields to construct our coordinates.
%We begin with the integration of $\partial_v$ along the $u=0$ line.
%There we have
%\begin{align}
% \partial_v|_{u=0} = \eta_v \partial_\eta = \frac{1}{c_0 \bar u_\zeta} \partial_\eta,
%  \label{eq:normalize}
%\end{align}
%where we used $\bar u_\eta|_{\zeta=0} = 0$ and $\sqrt{g}g^{\zeta\zeta}|_{\zeta=0} = h(\zeta_0, \eta)/f_0 = 1$, since $h(\psi_0) = f_0$.
%We can use Eq.~\eqref{eq:normalize} to define $c_0$ such that $v\in[0,2\pi]$.
%In order to do so we simply integrate
%\begin{align}
%  v_1 = \int_0^{2\pi} \left .\frac{\d v}{\d \eta}\right|_{\zeta=0} \d \eta = \int_0^{2\pi} \frac{1}{\eta_v}\d \eta = c_0 \int_0^{2\pi} \bar u_\zeta(0,\eta) \d \eta := 2\pi
%  \label{eq:computec0}
%\end{align}
% with $v_0 = 0$ and choose $c_0$ such that $v_1=2\pi$.
%The integration can be done by evaluating $\bar u_\zeta$ at the Gaussian nodes and using Gauss--Legendre quadrature.
%Having done this we integrate, analog to Section~\ref{sec:orthogonal},
%$\partial_v $ to get starting points for the integration of $\partial_u$
%from $\bar u_0 = 0$ to $\bar u_1= c_0 \zeta_1$.
%In order to avoid out-of-bound errors we can artificially make the $\zeta,\eta$ box periodic.
%Note, that we can compute $u_x(\zeta, \eta)$ and $u_y(\zeta, \eta)$ from $\bar u(\zeta,\eta)$ by using the equations $u_x = u_\zeta \zeta_x + u_\eta\eta_x$, $u_y = u_\zeta \zeta_y + u_\eta\eta_y$ as soon as the constant $c_0$ becomes available.
%
%For the integration of the vector fields Eq.~\eqref{eq:basis_conformal} and the evaluation of derivatives Eq.~\eqref{eq:conformal_coords} we need to evaluate its components at points
%unequal to the Gaussian nodes. An interpolation method
%is thus needed. We use interpolation with
%the same order as the dG method we used for the solution of $\bar u(\zeta,\eta)$.
%
%The result of the algorithm is the list of points
%$\zeta(u_i,v_i)$, $\eta(u_i,v_i)$, which we can insert into
%$x(\zeta,\eta)$ and $y(\zeta, \eta)$ as well as $u_x(\zeta,\eta)$ and $u_y(\zeta,\eta)$ which have been computed previously. The derivatives $v_x$ and $v_y$ are given by the Cauchy--Riemann equations~\eqref{eq:CR}.

\subsection{Grid adaption} \label{sec:adaption}
Although the conformal grid is advantageous for elliptic equations (due to the vanishing metric
coefficients) the cell distribution is not very flexible; once the boundary is set
the conformal map is unique. We therefore have little control over the
distribution of cells.
We can use grid adaption techniques to overcome this restriction.
The idea is to modify the elliptic equations that $u$ and $v$ have
to fulfill. That is, we choose
\begin{align}
\nabla\cdot\left( \frac{\nabla u}{w}\right) = \nabla\cdot\left( w \nabla v\right) = 0
\label{eq:adaption}
\end{align}
where $w$ is an appropriately chosen weight function. The
cell size will be small in regions where $w$ is large and spread out in regions
where $w$ is small.
The Cauchy--Riemann equations~\eqref{eq:CR} are changed accordingly to
\begin{align}
v_x = -\frac{u_y}{w}\quad v_y = \frac{u_x}{w}
\label{eq:CR_adaption}
\end{align}
Let us remark here that it is straightforward to implement the weight function in the orthogonal grid generation.
In Section~\ref{sec:orthogonal} we simply replace the function $h$ by $h/w$.
Then we have
\begin{subequations}
\begin{align}
\partial_\zeta &= \frac{1}{f_0(\nabla\psi)^2} (\psi_x\partial_x + \psi_y \partial_y)\\
\partial_\eta &= \frac{w}{h(\nabla\psi)^2} (-\psi_y\partial_x + \psi_x \partial_y)
\end{align}
\end{subequations}
and $\nabla \psi\cdot \nabla (h/w) = -h/w \Delta\psi$.
A suitable choice for $w$ is
\begin{align}
w = |\nabla\psi|
\label{eq:weight_adaption}
\end{align}
as then the angle-like coordinate $\eta$ becomes the arc length on the $\psi_0$ line.

%For completeness, we explicitly state the formulas for the adaption of the conformal case here.
%These become apparent in
%the next section, where we generalize this approach.
%In a boundary adapted grid (i.e.~a grid constructed from a coordinate transform that exactly resolves the boundary) we solve the equation
%\begin{align}
%\nabla\cdot\left( \frac{1}{w} \nabla \bar u \right) = 0
%\label{eq:ubar_adapted}
%\end{align}
%with the familiar Dirichlet boundary conditions $\bar u|_{\partial\Omega} = \psi$.
%Then the coordinate lines are given by the vector fields
%\begin{subequations}
%\begin{align}
%  \partial_u &= \frac{1}{c_0(\nabla \bar u)^2}
%        (g^{\zeta\zeta}\bar u_\zeta \partial_\zeta + g^{\eta\eta}\bar u_\eta\partial_\eta)\label{eq:basis_adapteda}\\
%  \partial_v &= \frac{w}{c_0\sqrt{g}(\nabla \bar u)^2}
%          (-\bar u_\eta \partial_\zeta + \bar u_\zeta\partial_\eta)
%  \label{eq:basis_adapted}
%\end{align}
%\end{subequations}
%where $(\nabla \bar u)^2 = g^{\zeta\zeta}\bar u_\zeta^2 + g^{\eta\eta}\bar u_\eta^2$.
%The relation between $u$ and $v$ is given by
%\begin{align}
% v_x = -\frac{u_y}{w},\quad v_y = \frac{u_x}{w}
%\end{align}
%These formulas are very similar to the ones given in Section~\ref{sec:conformal}.

\subsection{Monitor metric and the heat conduction tensor} \label{sec:metric}
We follow Reference~\cite{Liseikin, Glasser2006, Vaseva2009} and replace the canonical metric tensor $\vec g$ by
a specifically tailored tensor $\vec G$ that takes the form
\begin{align}
\vec G(x,y) = \vec T \vec T + k^2\vec N \vec N + \eps(x,y)\vec I
\label{eq:metric}
\end{align}
with $\vec T = (-\psi_y, \psi_x)$ and $\vec N = -(\psi_x, \psi_y)$.
The vector $\vec T$ is tangential to the contour lines of $\psi$ while $\vec N$ is
normal to it.
We have
\begin{align}
  \sqrt{G} = \left[(\eps+k^2(\psi_x^2+\psi_y^2))(\eps+(\psi_x^2+\psi_y^2))\right]^{-1/2}
  \label{}
\end{align}
where $k<1$ is a constant and $\eps$ is a function that is nonzero in the neighborhood
of singularities, i.e.~where $\nabla \psi(x,y)=0$.
In our work we choose $k=0.1$ and $\eps(x,y)\equiv\eps =0.001$.
Note that the scalar product induced by $\vec G$ conserves perpendicularity
with respect to $\vec T$, i.e.~if
and only if
 any vector $\vec v \perp \vec T$ in the canonical metric, then it is also perpendicular to $\vec T$ in $\vec G$.
In our application this is important at the boundary.\\
%
Now, we consider the elliptic equation
\begin{align}
  \nabla\cdot(\sqrt{G}\vec G \nabla u) = 0\nonumber\\
  \partial_x(\sqrt{G} (G^{xx}\partial_x u + G^{xy}\partial_y u )) + \partial_y(\sqrt{G}(G^{yx}\partial_x u + G^{yy}\partial_y u )) = 0
  \label{eq:elliptic}
\end{align}
with Dirichlet boundary conditions. % $u|_{\partial\Omega} = \psi$.
The resulting grid coordinate $u$ is almost perfectly aligned in regions
far away from singularities and breaks the alignment in regions where $|\nabla\psi|$ is small or vanishes.

We now take a slightly more general approach and rewrite Eq.~\eqref{eq:elliptic}
\begin{align}
  \nabla\cdot(\vec \chi \nabla u) = 0
  \label{eq:elliptic_mod}
\end{align}
where $\chi(x,y)$ is a symmetric positive-definite contravariant tensor.
Then the conformal grid from Section~\ref{sec:conformal}, the grid adaption from Section~\ref{sec:adaption} as
well as the monitor metric can be considered special cases.
The grid adaption is recovered by setting $\vec \chi = 1/w\vec I$,
while the monitor metric is simply $\vec \chi = \sqrt{G}\vec G$.
The true conformal case is, of course, recovered by setting $\vec \chi = \vec I$.

This allows us to provide a commonly known physical interpretation of Eq.~\eqref{eq:elliptic_mod}.
If $u$ is a temperature, then the Dirichlet boundary condition
fixes a temperature at the boundary of our domain. The tensor $\vec \chi(x,y)$ is
then the anisotropic heat conduction tensor and the coordinate lines for $u$ are
the isothermal lines of the steady state solution to the heat diffusion problem.
This interpretation allows us to intuitively estimate how the
coordinate lines look like when a specific $\vec\chi$ is chosen.
In the case of grid adaption, if the weight function is large, the heat conduction
is low resulting in small temperature gradients and thus closely spaced grid cells.
On the other hand, let us reconsider Eq.~\eqref{eq:metric} for the case $\eps=0$.
Then we can write
\begin{align}
\vec \chi = \frac{1}{k}\hat t\hat t + k \hat n\hat n
= \chi_\parallel \hat t \hat t + \chi_\perp \hat n \hat n
  \label{eq:heat_conduction}
\end{align}
which for $k<1$ simply means that the heat conduction parallel to the magnetic field
is far stronger than perpendicular to it, which is in fact the case in an actual
fusion reactor.
The coordinate lines will thus tend to align with the magnetic flux
surfaces with the degree of alignment given by $k$ resulting
in an almost aligned grid.
In the limit of vanishing $k$ the alignment should be perfect.
If $|\nabla\psi|$ vanishes, the tensor~\eqref{eq:metric} reduces to $\vec \chi = \vec I$.

\subsection{The elliptic grids} \label{sec:elliptic}
Suppose that we have constructed a boundary aligned grid $(\zeta,\eta)$, which
may but not necessarily has to be the orthogonal grid from Section~\ref{sec:orthogonal}.
Now, we solve the general elliptic equation
\begin{align}
 \nabla\cdot(\vec \chi \nabla \bar u) = \partial_i(\sqrt{g} \chi^{ij}\partial_j \bar u) = 0
  \label{eq:general_elliptic}
\end{align}
in the transformed coordinate system with boundary conditions $\bar u|_{\partial\Omega}=\psi$. We set $u=c_0(\bar u -\psi_0)$. This means that we have to
transform the conduction tensor $\chi$ from Cartesian to flux coordinates,
which is done by the well known rules of tensor transformation
\begin{subequations}
\begin{align}
  \chi^{\zeta\zeta}(\zeta, \eta) &= (\zeta_x\zeta_x \chi^{xx} + 2\zeta_x\zeta_y\chi^{xy} + \zeta_y\zeta_y \chi^{yy})|_{x(\zeta,\eta), y(\zeta,\eta)}\\
  \chi^{\zeta\eta}(\zeta, \eta) &= (\zeta_x\eta_x \chi^{xx} + (\zeta_x\eta_y + \eta_x\zeta_y)\chi^{xy} + \zeta_y\eta_y \chi^{yy})|_{x(\zeta,\eta), y(\zeta,\eta)}\\
  \chi^{\eta\eta} (\zeta, \eta) &= (\eta_x\eta_x \chi^{xx} + 2\eta_x\eta_y\chi^{xy} + \eta_y\eta_y \chi^{yy}))|_{x(\zeta,\eta), y(\zeta,\eta)}
\end{align}
\label{eq:transformationChi}
\end{subequations}
The equivalent of the Cauchy--Riemann equations in this formulation reads
\begin{subequations}
\begin{align}
  v_\zeta = -\sqrt{g}(\chi^{\eta\zeta}u_\zeta + \chi^{\eta\eta}u_\eta)\\
  v_\eta = +\sqrt{g}(\chi^{\zeta\zeta}u_\zeta + \chi^{\zeta\eta}u_\eta)
\end{align}
  \label{eq:hodge_dual}
\end{subequations}
These are constructed such that
 $\nabla\cdot((\vec \chi/ \det \chi) \nabla v) = 0$.
The interested reader might notice that Eq.~\eqref{eq:hodge_dual} just
defines the components of the Hodge dual $\d v = \star\d u$
if $\chi$  is interpreted as a metric.
%In order to repeat the formalism in Section~\ref{sec:conformal}
%we simply replace $\psi$ by $u$ and $h$ by $\sqrt{g} h$ in this
%Section and note that $h(\zeta,\eta)=cte$ because of Eq.~\ref{eq:general_elliptic}.
%It becomes clear now that our algorithm for orthogonal and
%the conformal grid generation is actually the same.
We note that these equations are now valid for any grid that
we use to solve Eq.~\eqref{eq:general_elliptic}. If we find a boundary
aligned grid analytically, we can start the grid construction
directly with the solution of Eq.~\eqref{eq:general_elliptic}
and then proceed with the conformal grid generation.

The relevant equations for the streamline integration now read
\begin{subequations}
\begin{align}
    \d u &= c_0(\bar u_\zeta\d \zeta + \bar u_\eta \d \eta) \\
    \d v &= c_0\sqrt{g}(-(\chi^{\eta\zeta}\bar u_\zeta + \chi^{\eta\eta}\bar u_\eta)\d \zeta + (\chi^{\zeta\zeta}\bar u_\zeta + \chi^{\zeta\eta}\bar u_\eta)\d \eta)
\end{align}
\label{eq:contravariant_monitor}
\end{subequations}
which just means that $v$
is orthogonal to $u$ in the scalar product
generated by the symmetric tensor $\vec \chi$, which we denote by $\langle . , .\rangle_\chi$.
We have
$J=c_0^2 \sqrt{g} \langle \nabla \bar u,\nabla \bar u \rangle_\chi
  := c_0^2\sqrt{g} (\bar u_\zeta^2\chi^{\zeta\zeta} + 2 \bar u_\zeta \bar u_\eta \chi^{\zeta\eta} + \bar u_\eta^2\chi^{\eta\eta})$
and
\begin{subequations}
\begin{align}
  \partial_u &= \frac{1}{c_0\langle\nabla \bar u,\nabla \bar u\rangle_\chi}
        (\chi^{\zeta\zeta}\bar u_\zeta + \chi^{\zeta\eta}\bar u_\eta)\partial_\zeta +
        (\chi^{\eta\zeta}\bar u_\zeta + \chi^{\eta\eta}\bar u_\eta)\partial_\eta\label{eq:basis_monitora}\\
  \partial_v &= \frac{1}{c_0\sqrt{g}\langle\nabla \bar u,\nabla \bar u\rangle_\chi}
          (-\bar u_\eta \partial_\zeta + \bar u_\zeta\partial_\eta)
  \label{eq:basis_monitorb}
\end{align}
\label{eq:basis_monitor}
\end{subequations}
As for the orthogonal coordinates we have to integrate these two vector fields to construct our coordinates.
We begin with the integration of $\partial_v$ along the $\zeta=0$ line.
It is important to note that $\bar u_\eta|_{\zeta=0} = 0$ and thus
\begin{align}
  \eta_v(0, \eta) = \left( c_0\sqrt{g}\bar u_\zeta \chi^{\zeta\zeta} \right)|_{\zeta= 0, \eta}
  \label{eq:normalize}
\end{align}
We can use Eq.~\eqref{eq:normalize} to define $c_0$ such that $v\in[0,2\pi]$.
In order to do so we simply integrate
\begin{align}
  v_1 = \int_0^{2\pi} \left .\frac{\d v}{\d \eta}\right|_{\zeta=0} \d \eta = \int_0^{2\pi} \frac{1}{\eta_v}\d \eta = c_0 \int_0^{2\pi} \sqrt{g}\chi^{\zeta\zeta}\bar u_\zeta(0,\eta) \d \eta := 2\pi
  \label{eq:computec0}
\end{align}
 with $v_0 = 0$ and choose $c_0$ such that $v_1=2\pi$. This is the analogous
 equation to Eq.~\eqref{eq:definef}, with the difference that we can integrate
 Eq.~\eqref{eq:computec0} directly using numerical quadrature.
%The integration can be done by evaluating $\bar u_\zeta$ at the Gaussian nodes and using Gauss--Legendre quadrature.
Having done this we integrate, analogous to Section~\ref{sec:orthogonal},
$\partial_v $ on $\zeta=0$ to get starting points for the integration of $\partial_u$
from $\bar u_0 = 0$ to $\bar u_1= c_0 \zeta_1$.
Note, that we can compute the components of $\d u$ and $\d v$ in terms of $\d x$ and $\d y$ by using the transformation
\begin{subequations}
\begin{align}
  u_x(\zeta, \eta)  &= u_\zeta \zeta_x + u_\eta\eta_x,\ u_y(\zeta, \eta) = u_\zeta \zeta_y + u_\eta\eta_y\\
  v_x(\zeta, \eta)  &= v_\zeta \zeta_x + v_\eta\eta_x,\ v_y(\zeta, \eta) = v_\zeta \zeta_y + v_\eta\eta_y
\end{align}
  \label{eq:chain_rule}
\end{subequations}
as soon as the constant $c_0$ becomes available.

Note that the resulting grid will, in general, not be orthogonal
in the Euclidean metric. However, for all the cases we discuss
in this paper, the grid is orthogonal at the boundary.

The final algorithm now reads, assuming that $u$ is discretized
by not necessarily equidistant points $u_i$ with $i=0,1,\dots N_u-1$ and
$v$ by $v_j$ with $j=0,1,\dots N_v-1$ and that $x(\zeta,\eta)$, $y(\zeta, \eta)$
as well as the components of the Jacobian $\zeta_x(\zeta,\eta)$, $\zeta_y(\zeta,\eta)$, $\eta_x(\zeta,\eta)$ and $\eta_y(\zeta,\eta)$ are available from the first coordinate transformation (e.g. Section~\ref{sec:orthogonal}):
\begin{enumerate}
  \item Choose either $\vec \chi = \vec I$, $\vec \chi=1/w\vec I$ or $\vec \chi = \sqrt{G}\vec G$ depending on whether a conformal, adapted or monitor grid is desired.
  \item Discretize and solve the elliptic equation~\eqref{eq:general_elliptic} on the $\zeta,\eta$ grid for $\bar u(\zeta, \eta)$ with any
    method that converges.
    Use Eq.~\eqref{eq:transformationChi} to transform $\chi$ from Cartesian
    to the $\zeta, \eta$ coordinate system.
    The chosen resolution
    determines the accuracy of the subsequent streamline integration.
  \item Numerically compute the derivatives $\bar u_\zeta$ and $\bar u_\eta$
    and construct $\eta_v^{-1}(0, \eta)$ using Eq.~\eqref{eq:normalize} for $c_0=1$
  \item Integrate Eq.~\eqref{eq:computec0} to determine $c_0$.
  \item On the $\zeta, \eta$ grid compute $\eta_v$, $\zeta_u$ and $\zeta_v$ according to Eq.~\eqref{eq:basis_monitor} as well as $u_\zeta$, $u_\eta$, $v_\zeta$ and
    $v_\eta$ according to Eq.~\eqref{eq:contravariant_monitor}. Use Eq.~\eqref{eq:chain_rule} and the Jacobian of the $\zeta, \eta$ coordinates to compute $u_x$, $u_y$, $v_x$ and $v_y$.
  \item Integrate the streamline of $\partial_v$ for $\zeta=0$
    from $v=0\dots v_j$ for all $j$ on the $\zeta, \eta$ grid using the normalized component $\eta_v$.
    Interpolate $\eta_v(\zeta, \eta)$ when necessary.
  \item Using the resulting points as start values integrate $\partial_u$ from
    $u=0\dots u_i$ for all $i$ and all points. The result is the list
    of coordinates $\zeta(u_i, v_j)$, $\eta(u_i, v_j)$ for all $i$ and $j$.
  \item Interpolate $x\left( \zeta,\eta \right)$, $y\left( \zeta, \eta \right)$,
    $u_x(\zeta, \eta)$, $u_y(\zeta, \eta)$, $v_x(\zeta, \eta)$ and $v_y(\zeta, \eta)$ on this list.
\end{enumerate}
There are two differences in this algorithm from the one presented in
Section~\ref{sec:orthogonal}.
For the integration of the vector fields Eq.~\eqref{eq:basis_monitor} and the evaluation of derivatives Eq.~\eqref{eq:contravariant_monitor} we need to evaluate its components at arbitrary points.
An interpolation method is thus needed.
In order to avoid out-of-bound errors we can artificially make the $\zeta,\eta$ box periodic.
Second, the existence of the coordinate $v$ is guaranteed by the Cauchy--Riemann equations
and thus the $h$ function does not appear.

A suitable test for the implementation is e.g. the volume/area of the domain.
Being an invariant
the volume must be the same regardless of the coordinate system in use.

%Finally, we note that with the metric proposed in Eq.~\eqref{eq:metric}
%the orthogonal grid generated is the same as the orthogonal
%grid generated by the identity tensor in the case $\eps=0$.
Finally, as already mentioned several times the $\zeta,\eta$ coordinates in the algorithm can
be replaced by any structured, boundary aligned grid. In especially this means
that for example a Cartesian grid ($\psi(x,y) = y$, $\zeta=x$, $\eta=y$)
can be used if the boundary
is a rectangle. The periodic boundary condition in $\eta$ in Eq.~\eqref{eq:general_elliptic} can easily be replaced
by Neumann boundaries if necessary.
The proposed adaption function $w$ or the monitor metric $\vec G$
are in the same way only suggestions, are in principle independent of $\psi(x,y)$ and can be replaced by any suitable quantity.

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

\section*{Acknowledgements}
This work was supported by the Austrian Science Fund (FWF) W1227-N16 and Y398.
This work has been carried out within the framework of the EUROfusion Consortium and has received funding from the Euratom research and training programme 2014‐2018 under grant agreement No 633053. The views and opinions expressed herein do not necessarily reflect those of the European Commission.

%..................................................................
\bibliography{../references}

%..................................................................

\end{document}


\documentclass{beamer}

\input{../../shared_slides.tex}
\usepackage{changepage}
% https://openai.com/blog/deep-double-descent/

\title{Gradient Descent under strong convexity}
\date{\today}

\begin{document}
\maketitle
\frame{\tableofcontents}

\section{Introduction}%

\begin{frame}
  \frametitle{How fast can we go?}

  \begin{itemize}
    \item So far we explored different smoothness properties.
    \item Error decreased with $1/k$ or $1/\sqrt{k}$
    \item call these rates sublinear
    \item Linear rate means
          \begin{equation}
            Err(x_{k}) \le \frac{C}{\exp(k)}
          \end{equation}
          or
          \begin{equation}
            Err(x_{k+1}) \le q Err(x_k)
          \end{equation}
          with $q<1$.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Linear convergence}
  \begin{figure}[ht]
    \centering
    \includegraphics[width=\textwidth,height=0.8\textheight,keepaspectratio]{convergence_rates}
    % \caption{\label{fig:label} }
  \end{figure}
\end{frame}

\begin{frame}
  \frametitle{Example}

  \begin{itemize}
    \item Consider $f(x) = x^2$. Clearly, $f$ is $L=2$ smooth.
          \begin{itemize}
            \item So we can pick $\alpha=1/L = 1/2$ for GD:\
                  \begin{equation}
                    x_{k+1} = x_k - \frac12 \nabla f(x_k) = x_k -x_k = 0.
                  \end{equation}
            \item Converges \textbf{in one step!}
          \end{itemize}
    \item Same $f(x)=x^2$, but is also $L=4$ smooth.
          \begin{itemize}
            \item So we can pick $\alpha=1/L = 1/4$ for GD:\
                  \begin{equation}
                    x_{k+1} = x_k - \frac14 \nabla f(x_k) = x_k -\frac12 x_k = \frac12 x_k.
                  \end{equation}
            \item Converges \textcolor{blue}{exponentially}
                  \begin{equation}
                    f(x_k) = f\left(\frac{x_0}{2^k}\right) = \frac{1}{2^{2k}} x_0^2.
                  \end{equation}
           \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Strongly convexity}

  \begin{center}
    \textcolor{blue}{``Not too flat.''}
  \end{center}

  \begin{block}{Recall}
    Let $f: \R^d \to \R$ be a differentiable function, then we say $f$ is $\mu$-strongly convex if
    \begin{equation}
      f(y) \ge f(x) + \langle \nabla f(x), y-x \rangle + \frac{\mu}{2} \Vert y-x \Vert^2, \quad \forall x,y.
    \end{equation}
  \end{block}

\end{frame}

\begin{frame}
  \frametitle{Strong convexity}
  \begin{center}
    \textcolor{blue}{Can be lower bounded by a quadratic.}
  \end{center}
  \begin{figure}[ht]
    \centering
    \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{strongly_convex}
    % \caption{\label{fig:label} }
  \end{figure}

\end{frame}

\section{Convergence analysis}%
\label{sec:}

\begin{frame}
  \frametitle{Smooth strongly convex functions}

  \begin{theorem}
    Let $f:\R^d \to \R$ be $L$-smooth and $\mu$-strongly convex. Then GD with stepsize $\alpha=1/L$ and arbitrary starting point $x_0$ guarantees:
    \begin{enumerate}
      \item distance to solution decreases by a constant factor
            \begin{equation}
              \Vert x_{k+1}-x^* \Vert^2 \le \left(1-\frac{\mu}{L}\right) \Vert x_k -x^* \Vert^2.
            \end{equation}
      \item Gives \textcolor{blue}{exponential} decrease in function values
            \begin{equation}
              f(x_k)-f^* \le {\left(1-\frac{\mu}{L}\right)}^k \, \frac{L \Vert x_0 -x^* \Vert^2}{2}.
            \end{equation}
    \end{enumerate}
  \end{theorem}

\end{frame}

\begin{frame}
  \frametitle{Proof}
  \begin{equation}
    \begin{aligned}
      \Vert x_{k+1} - x^* \Vert^2 &\le \Vert x_k - \alpha \nabla f(x_k) - x^* \Vert^2 \\
      &= \Vert x_k-x^* \Vert^2 + 2 \alpha \langle \nabla f(x_k), x^*-x_k \rangle + \alpha^2 \Vert \nabla f(x_k) \Vert^2.
    \end{aligned}
  \end{equation}
  Now we use the stronger version of the gradient inequality, namely
  \begin{equation}
    \langle \nabla f(x_k), x^*-x_k \rangle + \frac{\mu}{2} \Vert x^*-x_k \Vert^2 \le f^* -f(x_k).
  \end{equation}
  Combined we deduce
  \begin{equation}
    \begin{aligned}
      \MoveEqLeft \Vert x_{k+1} - x^* \Vert^2 \\
      &\le \Vert x_k-x^* \Vert^2 + 2 \alpha \left(f^* -f(x_k) - \frac{\mu}{2} \Vert x^*-x_k \Vert^2 \right) + \alpha^2 \Vert \nabla f(x_k) \Vert^2\\
      &= \left(1-\frac{\mu}{L}\right)\Vert x_k-x^* \Vert^2 + 2 \alpha (f^* -f(x_k) ) + \alpha^2 \Vert \nabla f(x_k) \Vert^2.
    \end{aligned}
  \end{equation}

\end{frame}

\begin{frame}
  \frametitle{Proof II}
  \vspace{-0.5cm}
  \begin{adjustwidth}{-1em}{-1em}

  \begin{equation}
      \Vert x_{k+1} - x^* \Vert^2 \le \left(1-\frac{\mu}{L}\right)\Vert x_k-x^* \Vert^2 \underline{+ 2 \alpha (f^* -f(x_k) ) + \alpha^2 \Vert \nabla f(x_k) \Vert^2}
  \end{equation}
  is the desired statement up to an error which we can bound
  \begin{align}
    \underline{2 \alpha (f^* -f(x_k) ) + \alpha^2 \Vert \nabla f(x_k) \Vert^2} &= \frac{2}{L} (f^* -f(x_k) ) + \frac{1}{L^2} \Vert \nabla f(x_k) \Vert^2 \\
    &\le \frac{2}{L} (f(x_{k+1}) -f(x_k) ) + \frac{1}{L^2} \Vert \nabla f(x_k) \Vert^2 \\
    \intertext{\textcolor{blue}{sufficient decrease}}
    &\le -\frac{1}{L^2} \Vert \nabla f(x_k) \Vert^2 + \frac{1}{L^2} \Vert \nabla f(x_k) \Vert^2 =0.
  \end{align}
  So we can ignore this extra term and get (i):
  \begin{equation}
    \Vert x_{k+1}-x^* \Vert^2 \le \left(1-\frac{\mu}{L}\right) \Vert x_k -x^* \Vert^2.
  \end{equation}

  \end{adjustwidth}
\end{frame}

\begin{frame}
  \frametitle{Proof III}
  \textbf{Smoothness} of $f$ gives
  \begin{align}
    f(x_k) - f^* &\le \langle \nabla f(x^*), x_k-x^* \rangle + \frac{L}{2} \Vert x_k-x^* \Vert^2\\
    \intertext{together with the fact that $\nabla f(x^*)=0$ this gives}
    f(x_k) - f^* &\le \frac{L}{2} \Vert x_k-x^* \Vert^2.
  \end{align}
  If we combine this with (i)
  \begin{equation}
     f(x_k) - f^* \le \frac{L}{2} \Vert x_k-x^* \Vert^2 \le \frac{L}{2} {\left(1-\frac{\mu}{L}\right)}^k\Vert x_0-x^* \Vert^2.
  \end{equation}
\end{frame}

\begin{frame}
  \frametitle{Complexity: $\mathcal{O}(\log(1/\epsilon))$}
  With $D^2=\Vert x_0-x^* \Vert^2$ we have
  \begin{equation}
    f(x_k) - f^* \le \frac{L}{2} {\left(1-\frac{\mu}{L}\right)}^k D^2.
  \end{equation}
  So we can ensure $f(x_k)-f^* \le \epsilon$ after
  \begin{equation}
    k \ge \frac{L}{\mu} \log\left(\frac{D^2L}{2 \epsilon}\right)
  \end{equation}
  iterations.
  If we want accuracy $\epsilon=0.001$
  \begin{itemize}
    \item GD without strong convexity needs $\mathcal{O}(\epsilon^{-1})\approx1000$ iterations,
    \item whereas here we \textbf{only} need $\log(1000)\approx 7$.
  \end{itemize}

\end{frame}

\end{document}


%!TEX root =  ../main.tex

\subsection{Euler's Equation}
One of the most famous equations in mathematics is Euler's incredible formula, restated as
$e^{i\tau}-1=0$.  The majestic beauty of this equation is hard to overstate.  $e$ is the number
of constant growth, concealing an infinity of feedback within a finite number.  $i$ is the imaginary
number, square-root of $-1$.  $\tau$ is $2\pi$, an entire circle, an infinite number of points curving
around forever.  1 is the identity of multiplication.  0 is the identity of addition.  They all inter-relate,
showcasing the multi-dimensional nature of numbers, looping back on itself to make a finite
infinity.  Pure poetry.

\subsection{Review of $e$}
Many students do not encounter Euler's equation until they are taught Taylor's series, a way of
writing simple functions as an infinite sum of small terms.  But there is a geometric way to make
sense of Euler, and he himself said that $r\cdot e^{\theta i}=r(\sin{\theta}+i\cos{\theta})$, the now-familiar
``cis'' you have been learning about in this chapter.  You can immediately see that $e^{\tau i}$ is
$\sin{\tau}+i\cos{\tau}=1$, but where did the $e$ come from?  Where did it go?

Recall that $e$ is a non-algebraic number, requiring limits.  It comes from an infinite calculation,

$$\lim_{n\rightarrow\infty} \left(1+\frac{1}{n}\right)^n$$

Remember too, we built the graph of $y=e^x$ from a limit, namely $y=\lim_{n\rightarrow\infty}(1+\frac{x}{n})^n$.  This means $e^{i\tau}$ will be better and better approximated by larger and larger numbers
plugged into $(1+\frac{i\tau}{n})^n$.  In the exercise or problems for this section, you will build the up
the values of $n$ for yourself.

%\begin{tikzpicture}
%	\draw [<->] (-2,0) -- (2,0) node{R};
%	\draw [<->] (0,-2) -- (0,2) node{i};
%	\draw (0,0) circle (1);
%	\draw (1,3.142) -- (0,0) -- (1,0) -- (1,3.142) node{$1+i\pi$};
%\end{tikzpicture}

%\begin{tikzpicture}
%	\draw [<->] (-2,0) -- (2,0) node{R};
%	\draw [<->] (0,-2) -- (0,2) node{i};
%	\draw (0,0) circle (1);
%	\draw (1,1.571) -- (1,0) -- (0,0) -- (-1.467,3.142) node{$(1+\frac{\pi}{2}i)^2$} -- (1,1.571) -- (0,0);
%\end{tikzpicture}

%\begin{tikzpicture}
%	\draw [<->] (-2,0) -- (2,0) node{R};
%	\draw [<->] (0,-2) -- (0,2) node{i};
%	\draw (0,0) circle (1);
%	\draw (1,1.047) -- (1,0) -- (0,0) -- (-0.0966,2.094) -- (1,1.047) -- (0,0) -- (-2.289,1.993) node{$(1+%\frac{\pi}{3}i)^3$}-- (-0.0966,2.094);
%\end{tikzpicture}

\begin{tikzpicture}[scale=0.5]
	\draw [<->] (-8,0) -- (8,0) node{$\mathbb{R}$};
	\draw [<->] (0,-8) -- (0,8) node[anchor=south]{$i$};
	\draw (0,0) circle (1);

  \foreach \x in {1,2,3} {
                \draw [fill] ({\x*atan(6.283185/\x)}:{(sqrt(1+39.4784176/(\x*\x)))^\x}) node[anchor=south]{\x} circle (.1);
   }
  \foreach \x in {4,5,6} {
                \draw [fill] ({\x*atan(6.283185/\x)}:{(sqrt(1+39.4784176/(\x*\x)))^\x}) node[anchor=east]{\x} circle (.1);
   }
  \foreach \x in {8,12,100} {
                \draw [fill] ({\x*atan(6.283185/\x)}:{(sqrt(1+39.4784176/(\x*\x)))^\x}) node[anchor=north]{\x} circle (.1);
   }
\end{tikzpicture}

\subsection{Analytic Continuation}
Distribution of primes, Zeta function


\input{../../utils/header.tex}

\begin{document}

\title{Machine Learning (41204-01)\\HW \#6}
\author{Will Clark $\vert$ Matthew DeLio \\
\texttt{\{will.clark,mdelio\}@chicagobooth.edu} \\
University of Chicago Booth School of Business}
\date{\today}
\maketitle

\section{Baseline Predictive Models} \label{sec:baseline}

We use a multinomial logistic regression, a random forest, and a boosting tree as a set of baseline models/algorithms against which we can assess the performance of the neural networks discussed in \vref{sec:nnets}.

First, we use a multinomial logistic regression model with an L1 penalty for variable selection/reduction. We use five-fold cross validation to select the optimal regularization parameter for each classification (i.e. there are six logistic regression models, each with its own L1 penalty). On our test data set, we find that this model predicts movement type with 95.4\% accuracy. We can see in \vref{fig:heatmap_dmr,tab:conmat_dmr} that the model predicts extremely well overall. The most common mistake it makes is to confuse standing/sitting, and it sometimes has trouble distinguishing between walking vs. walking up or down stairs.\footnote{We find the heatmap representation of the confusion matrix to be a quick and helpful way to visualise a model's predictive accuracy. Full tabular results can be found in the appendix.}

Next, we try a random forest algorithm. The only parameter to tune for a random forest is the number of covariates sampled at each tree split (\texttt{mtry}). A rule of thumb is to set $\texttt{mtry}=\sqrt{p}$. In this case, $p=477$, so we estimated a random forest for each of $\texttt{mtry}\in(10, 15, 20, 25, 30)$ so that the range would be roughly centered around $\sqrt{p}$. Again using five-fold cross validation, we found the optimal \texttt{mtry} to be 10. This model predicts out of sample with 94.0\% accuracy, making it slightly less effective than our multinomial logit model above.\footnote{In sample accuracy was above 98\%, although the training and test set are composed of different groups of people, so we should not be surprised to see lower OOS accuracy.} We can see in \vref{fig:heatmap_rf,tab:conmat_rf} that this algorithm, like the one discussed above, also confuses sitting and standing. It also tends to get a bit more confused between walking vs. walking up/down stairs than the multinomial logit model.

Lastly, we try a boosting tree algorithm. The parameters we can use to tune and the ranges over which we sampled are:
\begin{itemize}
\item \texttt{interaction.depth} $\in (1, 5, 9)$
\item \texttt{n.trees} $\in (500, 1000, 1500, 2000)$
\item \texttt{shrinkage} $\in (0.01, 0.05)$
\end{itemize}
The optimal algorithm, again chosen by five-fold cross validation, used 2000 trees, interaction depth of 5, and a shrinkage parameter of 0.05. This algorithm predicts out of sample with 94.4\% accuracy, making it slightly more accurate than the random forest but still not as accurate as our multinomial logit model. We can see in \vref{fig:heatmap_boost,tab:conmat_boost} that this algorithm is worse than the two prior at distinguishing between sitting/standing, but it does slightly better than the random forest at distinguishing between walking vs. walking on stairs.

\section{Neural Nets} \label{sec:nnets}
To begin our investigation of using neural networks we start fairly modestly with a hyperbolic tangent activation, with 2-hidden layers containing 200 nodes each, trained over 10 epochs.  With these settings, we train the algorithm and find the out-of-sample accuracy to be a modest 92.6\%.  To improve on this, we keep the number of hidden layers at 2, but, using a rule-of-thumb, tweak the number of nodes in each hidden layer to: $\dim input + \dim classifiers = 477 + 6 = 483$.  With these hidden layers set we then try the following activations:
\begin{itemize}
\item TanH
\item TanH with Dropout
\item Rectifier With Dropout
\end{itemize}

We find that the \texttt{Rectifier with Dropout} performs the best and then turn our attention to tweak the hidden-/input-layer dropout ratio, and to add l1/l2 penalties (l2 penalties seem to hurt accuracy, so they are removed).  With accuracy standing at 93.2\%, with just 10 training epochs, we then decide to increase the number of epochs to 500 to obtain our final model (see \cref{lst:neural} for final settings).  Running the extra epochs increases out-of-sample accuracy to 94.6\%.

\Cref{tab:overall} shows a crude comparison of the overall accuracy by model type.  The neural network fairs well overall, inferior only to the multinomial regression in terms of raw accuracy.  Looking more closely at the types of class errors, we see that, compared to the other models, it mistakes sitting for laying more often than the other, but otherwise is performs similarly.  Finally, compared to the other tree-based supervised learning algorithms, the neural network trains faster and is easier to tune.

\input{overall.tex}

\begin{lstlisting}[float,label=lst:neural,caption=Final Deep Learning Configuration, language=R]
model.nn1 <- h2o.deeplearning(
  x = 1:p, y = p+1,
  training_frame = h2o.cbind(dat.h2o$X_train, dat.h2o$y_train),
  activation = "RectifierWithDropout",
  input_dropout_ratio = 0.50,
  hidden = c(p+y.nlevels, p+y.nlevels),
  hidden_dropout_ratios = c(0.5, 0.5),
  epochs = 500,
  l1 = 1e-5,
  model_id = "model.nn1")
\end{lstlisting}

\input{conmat_nn1.tex}
\input{conmat_stats_nn1.tex}

\begin{figure}[!htb]
  \centering
  \caption{Confusion Matrix Heatmap for Neural Network}
  \includegraphics[scale=.5]{heatmap_nn1.pdf}
  \label{fig:heatmap_nn1}
\end{figure}

\begin{appendices}

\clearpage
\section{Confusion Heatmaps for Baseline Models}

% \begin{landscape}
\begin{figure}[!htb]
  \centering
  \caption{Confusion Matrix Heatmaps for Baseline Predictive Models}
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Multinomial Logit}
    \includegraphics[width=\textwidth]{heatmap_dmr.pdf}
    \label{fig:heatmap_dmr}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Random Forest}
    \includegraphics[width=\textwidth]{heatmap_rf.pdf}
    \label{fig:heatmap_rf}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Boosting Tree}
    \includegraphics[width=\textwidth]{heatmap_boost.pdf}
    \label{fig:heatmap_boost}
  \end{subfigure}
\end{figure}
% \end{landscape}

\clearpage
\section{Confusion Matrices for Baseline Models}

\input{conmat_dmr.tex}
\input{conmat_stats_dmr.tex}

\input{conmat_rf.tex}
\input{conmat_stats_rf.tex}

\input{conmat_boost.tex}
\input{conmat_stats_boost.tex}

%\section{Code Listings}
%\lstinputlisting[label=lst:code, caption=Code Snippet, language=R]{../hw5.R}

\end{appendices}

\end{document}

% \input{.tex}

% \begin{figure}
%   \centering
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \hfill
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \caption{}
% \end{figure}

% \begin{figure}[!htb]
%   \centering
%   \caption{}
%   \includegraphics[scale=.5]{.pdf}
%   \label{fig:}
% \end{figure}


\section{Primality}

% From prime numbers to simple criptography

\frame{
{Part 3: Primality}

\tableofcontents[currentsection,hideallsubsections, firstsection=1, sections={1-5}]
}

\begin{frame}{Prime Numbers}

  Prime numbers are numbers that have \structure{no divisors}, others than 1 and themselves.\bigskip

  In many senses, primes numbers are the {\bf building blocks} of arithmetic.\bigskip

  \begin{block}{The Fundamental Theorem of Arithmetic}
    Every positive integer has a {\bf unique} prime factorization.
  \end{block}
\end{frame}

\subsection{Prime Number Factorization}

% Fundamental theorem of arithmetic: Every number can be decomposed in primes

\begin{frame}{Proving Prime Number Factorization}{Step 1}
    {\bf Lemma 1:} If $p$ is prime and $p|ab$, then $p|a$ or $p|b$

    \begin{proof}
      \begin{itemize}
        \item Let's assume that $p$ {\bf does not} divide $a$. So gcd$(a,p) = 1$
        \item Consequently, $\exists s,t. sa+tp = 1$
        \item Multiply everything by $b: sab+tpb = b$
        \item $p|sab \land p|tpb \implies p|(sab+tpb) \implies p|b$
      \end{itemize}
    \end{proof}

    {\bf Corolary 1:} If $p|a_1a_2a_3\ldots a_m$, then $\exists 0 < i \leq m, p|a_i$\\
    \hfill (You can prove this by induction on m)
\end{frame}

\begin{frame}{Proving Prime Number Factorization}{Step 2}

  In lecture 1 we showed that every positive integer was a product of prime numbers.\\
  Now we need to show that the prime number factorization is {\bf unique}:

  \begin{proof}
    Proof by contradiction:
    \begin{itemize}
      \item By WOP, consider $n$ the smallest positive integer with a \alert{non-unique} factorization;
      \item $n = p_1p_2\ldots p_k$ and $n = q_1q_2\ldots q_k$
      \item Consider that these factors are {\bf ordered} ($q_1 \geq q_2 \geq \ldots$)
      \item $p_1 \neq q_1$ (else, we could cancel then out, and find a smaller $n$ with 2 factorizations)
      \item Assume $p_1 > q_1$. $p_1|n \implies p_1|q_1q_2q_3\ldots q_k$
      \item However, $p_1 > q_1 \geq q_2 \geq q_3 \ldots\geq q_k$, which is a contradiction.
    \end{itemize}

  \end{proof}
\end{frame}

\subsection{Using Primes}
% How many primes are out there? (Prime number theorem)
\begin{frame}{Prime Properties}
  Because prime numbers are so important, we are interested in knowing how many are there. Consider the $\pi(n)$ set of prime numbers between 2 and $n$:

  \begin{equation*}
    \pi(n) ::= [\{p\in[2..n]|p\text{ is prime}\}]
  \end{equation*}
  \bigskip

  The \structure{Prime Number Theorem} states how the size of $\pi(n)$ grows:
  \begin{equation*}
    \lim_{n\to\infty}\frac{\pi(n)}{n/\ln n} = 1
  \end{equation*}\bigskip

  As a general idea, the frequency of primes around an integer $n$ is $\frac{1}{\ln n}$. In other words, the quantity of primes gets smaller as $n$ gets bigger.
\end{frame}

% Few primes leads to difficulty of prime factorization

% Cryptography
  % Every number can be composed of primes
  % Prime factorization is difficult
\begin{frame}[fragile]{Prime Cryptography}{The Turing Algorithm}
  Alan Turing, among his many works in computer science, proposed a cryptography method using prime numbers.\bigskip

  \begin{enumerate}
    \item Transform the message you want to send ($m$) into a prime number:
\begin{verbatim}
  v  i  c  t  o  r  y       (not prime)         (prime)   **
  22 09 03 20 15 18 25 -> 22090320151825 -> 2209032015182513
\end{verbatim}
    \item Choose a secret key ($k$) as a large prime, and share it with the receiver;
    \item The \emph{sender} calculates the \emph{encrypted message} as follows, and sends it.
    \begin{equation*}
      \hat{m} = m\times k
    \end{equation*}
    \item The \emph{receiver} recovers the \emph{original message} using the secret key:
    \begin{equation*}
      m = \hat{m}/k
    \end{equation*}
  \end{enumerate}
\end{frame}

\begin{frame}{Prime Cryptography}{How secure is the Turing Algorithm?}
  Because of the difficulty of finding the prime factors of a large number, it is very hard to find $m$ and $k$ if you only have $\hat{m}$.\bigskip

  So if an adversary only knows $\hat{m}$, it is very hard to break this code. But what happens if the adversary has {\bf two} secret messages, $\hat{m_1}$ and $\hat{m_2}$?\bigskip

  Remember that $\hat{m_1} = k\times m_1$ and $\hat{m_2} = k\times m_2$. $k$ is a \structure{common divisor} of $\hat{m_1}$ and $\hat{m_2}$, so:
  \begin{equation*}
    k = gcd(\hat{m_1}, \hat{m_2})
  \end{equation*}\bigskip

  So it is very easy to find the secret key of this algorithm. Can we improve it?
\end{frame}


\vssub
\subsection{~Propagation}
\vssub

In a numerical model, an Eulerian form of the balance equation
(\ref{eq:balance0}) is needed. This balance equation can either be written in
the form of a transport equation (with velocities outside the derivatives), or
in a conservation form (with velocities inside the derivatives). The former
form is valid for the vector wavenumber spectrum $N(\bk;\bx,t)$ only, whereas
valid equations of the latter form can be derived for arbitrary spectral
formulations, as long as the corresponding Jacobian transformation as
described above is well behaved \citep[e.g.,][]{tol:GAOS98b}. Furthermore, the
conservation equation conserves total wave energy/action, unlike the transport
equation. This is an important feature of an equation when applied in a
numerical model. The balance equation for the spectrum $N(k,\theta;\bx,t)$ as
used in \ws\ is given as (for convenience of notation, the spectrum is
henceforth denoted simply as $N$):

%----------------------------------%
% Plain-grid propagation equations %
%----------------------------------%
% eq:bal_plane
% eq:x_dot
% eq:k_dot
% eq:theta_dot

\begin{equation}
\frac{\partial N}{\partial t} + \nabla_x \cdot {\bf\dot{x}}N +
\frac{\partial}{\partial k} \dot{k}N +
\frac{\partial}{\partial \theta} \dot{\theta}N =
\frac{S}{\sigma} \: , \label{eq:bal_plane}
\end{equation}
\begin{equation}
{\bf \dot{x}} = {\bf c}_g + {\bf U} \: , \label{eq:x_dot}
\end{equation}
\begin{equation}
\dot{k} = - \frac{\partial \sigma}{\partial d}
\frac{\partial d}{\partial s} - {\bk} \cdot
\frac{\partial {\bf U}}{\partial s} \: , \label{eq:k_dot}
\end{equation}
\begin{equation}
\dot{\theta} = - \frac{1}{k} \left [
\frac{\partial \sigma}{\partial d} \frac{\partial d}{\partial m}
+ {\bk} \cdot \frac{\partial {\bf U}}{\partial m}
\right ] \: , \label{eq:theta_dot}
\end{equation}

\noindent
where ${\bf c}_g = (c_g \sin \theta, c_g \cos \theta$, $s$ is a coordinate in the
direction $\theta$ and $m$ is a coordinate perpendicular to $s$.
Equation~(\ref{eq:bal_plane}) is valid for Cartesian coordinates. For large-scale
applications, this equation is usually transferred to spherical coordinates,
defined by longitude $\lambda$ and latitude $\phi$, but maintaining the
definition of the local variance \citep[i.e., per unit surface, as
in][]{art:WAM88}

%---------------------------------%
% Spherical propagation equations %
%---------------------------------%
% eq:bal_sphere
% eq:phi_dot
% eq:lambda_dot
% eq:theta_g_dot

\begin{equation}
\frac{\partial N}{\partial t} +
\frac{1}{\cos \phi} \frac{\partial}{\partial \phi} \dot{\phi}
    N \cos \theta +
\frac{\partial}{\partial \lambda} \dot{\lambda}N +
\frac{\partial}{\partial k} \dot{k} N +
\frac{\partial}{\partial \theta} \dot{\theta}_g N
= \frac{S}{\sigma} \: ,\label{eq:bal_sphere}
\end{equation}
\begin{equation}
\dot{\phi} = \frac{c_g \cos \theta + U_\phi}{R}
\: , \label{eq:phi_dot}
\end{equation}
\begin{equation}
\dot{\lambda} =  \frac{c_g \sin \theta + U_\lambda}{R \cos \phi}
\: , \label{eq:lambda_dot}
\end{equation}
\begin{equation}
\dot{\theta}_g = \dot{\theta} -
\frac{c_g \tan \phi \cos \theta}{R}
\: , \label{eq:theta_g_dot}
\end{equation}

\noindent
where $R$ is the radius of the earth and $U_\phi$ and $U_\lambda$ are current
components. Equation~(\ref{eq:theta_g_dot}) includes a correction term for
propagation along great circles, using a Cartesian definition of $\theta$
where $\theta = 0$ corresponds to waves traveling from west to east. \ws\ can
be run using either Cartesian or Spherical coordinates. Note that unresolved obstacles
such as islands can be included in the equations. In \ws\ this is done at the
level of the numerical scheme, as is discussed in section~\ref{sub:num_obst}. Also, depth variations
at the scale of the wavelength can be introduced by a scattering source term described in
 section~\ref{sec:BS1}.

 Finally, both Cartesian  and spherical coordinates can be discretized in many ways, using quadrangles (rectangular, curvilinear or SMC grids) and triangles.
That aspect is treated in chapter~\ref{chapt:num}.


\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{gensymb}
\usepackage{geometry}
\geometry{margin=3cm}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usepackage{amsmath}
\usepackage{aircraftshapes}
\usepackage{subfig}

%opening
\title{Wind Drift Calculation}
\author{Karel De\,Vogeleer}

\newcommand{\Tt}{T_\mathrm{track}}
\newcommand{\Tc}{T_\mathrm{course}}
\newcommand{\Tw}{T_\mathrm{wind}}
\newcommand{\vGS}{v_\mathrm{GS}}
\newcommand{\vw}{v_\mathrm{wind}}
\newcommand{\vIAS}{v_\mathrm{AIS}}

\begin{document}

\maketitle

\section{Introduction}

Imagine an aircraft flying 95\,kts at 2000\,ft wanting to maintain a true track heading of 20\degree and a wind blows 12\,kts from 120\degree.
What heading does the aircraft have to maintain to compensate for the wind drift?
This document explains how to calculate the heading when subject to a wind drift.

Two different calculation approaches are explained, via \emph{Pythagoras's theorem} and the \emph{cosine rule}.
We will see that the former is a tad simpler than the latter, but both equally accurate.

Let's put forward a few variables to describe our wind drift problem:
\begin{itemize}
 \item $\vec{a}$ is the aircraft velocity,
 \item $\vec{w}$ is the wind velocity,
 \item $\vec{r}$ is the resulting velocity $\vec{a}+\vec{w}$.
\end{itemize}
A velocity\footnote{Wikipedia: the \emph{velocity} of an object is the rate of change of its position with respect to a frame of reference, and is a function of time. Velocity is equivalent to a specification of its speed and direction of motion (e.g. 60\,km/h to the north).} has a length $\|\cdot\|$ and a given direction or heading $\theta(\cdot)$.

In the context of speeds and an aircraft subject to wind, $\|\vec{a}\|$ is also referred to as the \emph{indicated airspeed} (IAS), and $\|\vec{r}\|$ is also known as the \emph{ground speed} (GS), the \emph{true track} ($T_\mathrm{t}$) is the heading of $\vec{r}$ and the \emph{true course} ($T_\mathrm{c}$) is the heading of $\vec{a}$.
The speeds can be in any unit, e.g., mph, kts, m/s or km/h, as long as the same unit is used consistently.

For the example in the introduction, the aircraft's true track is $T_\mathrm{t}=\theta(\vec{r})=20\degree$ and flying a ground speed of $\mathrm{GS}=\|\vec{r}\|=96.35$\,kts.
The wind blows in the direction of  $\theta(\vec{w})=300\degree$ blowing at $\|\vec{w}\|=12$\,kts.
This results in a true course heading $T_\mathrm{t}=\theta(\vec{a})=27.15\degree$ and an indicated airspeed of $\mathrm{IAS}=\|\vec{a}\|=95$\,kts.

\begin{figure}%
    \centering
    \subfloat[velocities and angles]{

     \centering
 \begin{tikzpicture}
  %\draw[thin,gray!40] (-2,-2) grid (2,2);
  \draw[gray,->,dashed] (0,-0.5) -- (0,4);
  \draw[gray,->,dashed] (-0.5,0) -- (4,0);
  \draw[gray,->,dashed] (4,3.5) -- (4,5);
  \draw[gray,->,dashed] (3.5,4) -- (5,4);
  \draw (1.9,2) node[above] {$\vec{a}$};
  \draw (3,1) node[below] {$\vec{r}$};
  \draw (5.1,3) node[right] {$\vec{w}$};
\begin{scope}[thick,decoration={markings,mark=at position 0.5 with {\arrow{>}}}]
  \draw[postaction={decorate}] (0,0) -- (4,4);
\end{scope}
\begin{scope}[thick,decoration={markings,mark=at position 0.55 with {\arrow{>}},mark=at position 0.45 with {\arrow{>}},mark=at position 0.5 with {\arrow{>}}}]
  \draw[postaction={decorate}] (0,0) -- (6,2);
\end{scope}
\begin{scope}[thick,decoration={markings,mark=at position 0.55 with {\arrow{>}},mark=at position 0.45 with {\arrow{>}}}]
  \draw[postaction={decorate}] (4,4) -- (6,2);
\end{scope}
  \node [aircraft top,fill=black,minimum width=0.75cm, rotate=45] at (0,0) {};
  \draw[gray,->] (0,1) arc (90:19:1);
  \draw[gray,->] (0,1.5) arc (90:45.5:1.5);
  \draw[gray,->] (4,4.5) arc (90:-43:0.5);
  \draw[gray] (0.75,1.3) node[above] {$\theta(\vec{a})$};
  \draw[gray] (-0.4,0.75) node[above] {$\theta(\vec{r})$};
  \draw[gray] (4.75,4.25) node[above] {$\theta(\vec{w})$};
\end{tikzpicture}

    }%
    \qquad
    \subfloat[speeds and drift]{

     \centering
 \begin{tikzpicture}
  \draw[gray,->,dashed] (0,-0.5) -- (0,4);
  \draw[gray,->,dashed] (-0.5,0) -- (4,0);
  \draw[gray,dashed] (4,4) -- (4.75,1.58);
  \draw[gray] (4.4,1.57) -- (4.65,1.65) -- (4.57,1.9);
  \draw (1.8,2) node[above] {$\|\vec{a}\|$};
  \draw[blue] (3.1,1) node[below] {$\|\vec{r}_\mathrm{a}\|$};
  \draw[red] (5.5,1.75) node[below] {$\|\vec{r}_\mathrm{w}\|$};
  \draw (5.1,3) node[right] {$\|\vec{w}\|$};
  \draw[->] (0,0) -- (4,4);
  \draw[->,blue] (0,0) -- (4.75,1.58);
  \draw[->,red] (4.75,1.58) -- (6,2);
  \draw[->] (4,4) -- (6,2);
  \node [aircraft top,fill=black,minimum width=0.75cm, rotate=45] at (0,0) {};
  \draw[gray,->] (5.283956,2.682788) arc (137.5:196:1);
  \draw[gray,->] (1.069876,1.051364) arc (45.5:21:1.5);
  \draw[gray] (3.85,2.2) node[above] {$\|\vec{w}_\mathrm{h}\|$};
  \draw[gray] (4.9,2.1) node[above] {$\beta$};
  \draw[gray] (1.55,0.65) node[above] {$\alpha$};
\end{tikzpicture}
    \label{fig:overview:b}
    }%
\caption{Example of the true velocity $\vec{r}$ of an aircraft subject to wind $\vec{w}$ when following a course $\vec{a}$. The \emph{true track} $T_\mathrm{t}$ is $\theta(\vec{r})$, while the \emph{true course} $T_\mathrm{c}$ equals $\theta(\vec{a})$ and the winds blows in the direction $T_\mathrm{w}$. $\|\vec{a}\|$ represents the \emph{indicated airspeed} AIS and the \emph{ground speed} GS is given by $\|\vec{r}\|$, which can be decomposed in $\vec{r}_\mathrm{a} + \vec{r}_\mathrm{w}$.}
\label{fig:overview}
\end{figure}

\section{Calculation}

The aim of the wind drift calculation is to obtain (1) our \emph{ground speed} (GS) $\|\vec{r}\|$, and (2) find our \emph{wind drift angle} $\alpha$.
Figure~\ref{fig:overview:b} shows the variables that we are going to use in the calculations.
Via the wind drift angle $\angle$ we are able to calculate the heading to steer
\begin{equation}
 T_\mathrm{t}=T_\mathrm{c}+\alpha\quad\text{or}\quad\theta(\vec{a}) = \theta(\vec{r}) + \alpha,
\end{equation}
\begin{equation}
 \text{GS}= \|\vec{r}\| = \|\vec{r}_\mathrm{a}\| + \|\vec{r}_\mathrm{w}\|.
\end{equation}

$\beta$ is an angle given by the initial condition of the problem.
We know the heading of the wind $\theta(\vec{w})$ and the direction in which we want to move $T_\mathrm{t}=\theta(\vec{r})$.
In the case of Figure~\ref{fig:overview}, the angle between the wind and the true track $T_\mathrm{t}$ is simply
\begin{equation}
 \beta = \theta(\vec{w}) - \theta(\vec{r}),
\end{equation}
as the left-hand side and the right-hand side of this equation are, in fact, vertically opposite angles.

Let's start the drift calculate for the example as shown in Figure~\ref{fig:overview}.
Later on we'll generalise the solution for cases where the wind blowes from other quadrants.

\subsection{Pythagoras}

The Pythagoras is fairly simple, it requires twice the application of the sine rule, and once Pythagoras's theorem.
The key to the solution is to calculate $\|\vec{w}_\mathrm{h}\|$ and $\|\vec{r}_\mathrm{w}\|$.
$\vec{w}_\mathrm{h}$ and $\vec{r}_\mathrm{w}$ form the wind velocity
\begin{equation}
 \vec{w}= \vec{w}_\mathrm{h}+\vec{r}_\mathrm{w},
\end{equation}
where $\vec{r}_\mathrm{w}$ is the projection of the wind velocity on the true track velocity.
Knowing $\|\vec{w}_\mathrm{h}\|$, $\|\vec{a}\|$, $\beta$ and $\|\vec{w}\|$, one can calculate $\alpha$, $\|\vec{r}\|_\mathrm{a}$ and $\|\vec{r}\|_\mathrm{b}$.

$\|\vec{w}_\mathrm{h}\|$ is obtained by applying the sine rule to $\beta$:
\begin{equation}
 \|\vec{w}_\mathrm{h}\| = \|\vec{w}\| \sin(\beta).\label{eq:wh}
\end{equation}
Now $\|\vec{w}_\mathrm{h}\|$ can be used to calculate $\alpha$ as we also know $\|\vec{a}\|$,
\begin{equation}
 \sin (\alpha) = \frac{\|\vec{w}_\mathrm{h}\|}{\|\vec{a}\|}.
\end{equation}
Replacing $\|\vec{w}_\mathrm{h}\|$ from Equation \ref{eq:wh}, and isolating $\alpha$ yields the drift angle $\alpha$:
\begin{equation}
 \alpha = \arcsin\left(\frac{\|\vec{w}\| \sin(\beta)}{\|\vec{a}\|}\right).
\end{equation}

To calculate $\|\vec{r}\|$ we need to know $\|\vec{r}_\mathrm{b}\|$ and $\|\vec{w}_\mathrm{w}\|$.
$\|\vec{r}_\mathrm{b}\|$ can be obtained by applying Pythagoras' theorem:
\begin{equation}
 \|\vec{r}_\mathrm{a}\| = \sqrt{\|\vec{a}\|^2-\|\vec{w}_\mathrm{h}\|^2}\label{eq:ra}
\end{equation}
Also, $\|\vec{r}_\mathrm{w}\|$ is obtained similar to $\|\vec{w}_\mathrm{h}\|$:
\begin{equation}
 \|\vec{r}_\mathrm{w}\| = \|\vec{w}\| \cos(\beta).\label{eq:rw}
\end{equation}
Now that we know both $\|\vec{r}_\mathrm{a}\|$ and $\|\vec{r}_\mathrm{w}\|$ we can calculate $\|\vec{r}\|$
\begin{equation}
 \|\vec{r}\| = \|\vec{r}_\mathrm{a}\| + \|\vec{r}_\mathrm{w}\|.\label{eq:r}
\end{equation}
Merging Equation~\ref{eq:ra} and Equation~\ref{eq:rw} in Equation~\ref{eq:r} yields the groundspeed
\begin{equation}
% \|\vec{r}\| &= \arctan\left(\frac{\|\vec{w}_\mathrm{h}\|}{\|\vec{a}\|}\right) + \|\vec{w}\| \cos(\beta)\\
 \|\vec{r}\| = \sqrt{\|\vec{a}\|^2-\left(\|\vec{w}\| \sin(\beta)\right)^2} + \|\vec{w}\| \cos(\beta).
\end{equation}
Alternatively, the groundspeed can be calculated in function of the the wind drift angle
\begin{equation}
% \|\vec{r}\| &= \arctan\left(\frac{\|\vec{w}_\mathrm{h}\|}{\|\vec{a}\|}\right) + \|\vec{w}\| \cos(\beta)\\
 \|\vec{r}\| = \|\vec{a}\|\cos(\alpha) + \|\vec{w}\| \cos(\beta).
\end{equation}

\subsection{Cosine Rule}

The cosine rule is a bit more complicated as we have to deal with the roots of a quadratic polynomial.
Let's get started by writing the cosine rule for $\beta$:
\begin{equation}
 \|\vec{a}\|^2 = \|\vec{w}\|^2 + \|\vec{r}\|^2 - 2 \|\vec{w}\| \|\vec{r}\| \cos(\beta)
\end{equation}
We are looking to find $\|\vec{r}\|$. In fact, if we look closer, this equation is a quadratic in $\|\vec{r}\|$:
\begin{equation}
 0 =  \|\vec{r}\|^2 - 2 \|\vec{w}\| \cos(\beta) \|\vec{r}\| + (\|\vec{w}\|^2 - \|\vec{a}\|^2).
\end{equation}
We can obtain $\|\vec{r}\|$ by finding the roots of the above equation.
Let's define $c_*$ as the coefficients of the second order polynomial:
\begin{subequations}
\begin{align}
 c_2 &= 1\\
 c_1 &= - 2 \|\vec{w}\| \cos(\beta)\\
 c_0 &= \|\vec{w}\|^2 -\|\vec{a}\|^2
\end{align}
\end{subequations}
The well known solution for a quadratic equation is as follows
\begin{equation}
 \|\vec{r}\| = \frac{-c_1\pm\sqrt{c_1^2-4c_2c_0}}{2c_2}.
\end{equation}
Inserting $c_0$, $c_1$ and $c_2$ yields the groundspeed
\begin{align}
 \|\vec{r}\| = \|\vec{w}\| \cos(\beta)\pm\sqrt{\frac{\|\vec{w}\| \cos(\beta)^2}{2}-(\|\vec{w}\|^2 -\|\vec{a}\|^2)}.
\end{align}
However, we obtain two roots, a negative and a positive one.
Only the positive root applies to our real world.
This corresponds to the summation case, and so we obtain the unambiguous groundspeed
\begin{align}
 \|\vec{r}\| = \|\vec{w}\| \cos(\beta)-\sqrt{\frac{\|\vec{w}\| \cos(\beta)^2}{2}-(\|\vec{w}\|^2 -\|\vec{a}\|^2)}.
\end{align}

At this point we know $\|\vec{a}\|$, $\|\vec{w}\|$, and $\|\vec{r}\|$.
This allows us to calculate the wind drift angle $\alpha$.
Applying the cosine rule once more
\begin{equation}
 \|\vec{w}\|^2 = \|\vec{r}\|^2 + \|\vec{a}\|^2 - 2 \|\vec{r}\|\|\vec{a}\| \cos(\alpha),
\end{equation}
and isolating $\alpha$ give us
\begin{equation}
 \alpha = \arccos\left(\frac{\|\vec{w}\|^2-(\|\vec{r}\|^2 + \|\vec{a}\|^2)}{- 2 \|\vec{r}\|\|\vec{a}\|}\right).
\end{equation}

%Here we assumed that $\beta$ was a positive value, which is necessary for the cosine rule to hold.
%If the

\section{Example}

TODO

\section{Implementation}

TODO

\end{document}