%%%%%%%%%%%%%%%%%%%%%definitions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\input{../../doc/related_pages/header.tex}
\input{../../doc/related_pages/newcommands.tex}
\usepackage{minted}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%DOCUMENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\title{Testing Advection Schemes}
\author{ M.~Wiesenberger}
\maketitle

\begin{abstract}
  This is a program to test various advection schemes on the 2d incompressible Euler
  equation used in Reference~\cite{Einkemmer2014}.
\end{abstract}

\section{Equations}
We implement the 2d incompressible Euler equation
\begin{subequations}
\begin{align}
 \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = 0 \\
 -\Delta \phi = \omega \label{eq:euler_poisson_elliptic}
\end{align}
\label{eq:euler_poisson}
\end{subequations}
with vorticity $\omega$ and stream-function $\phi$.
The Poisson bracket is given by $\{ \phi, \omega\} := \phi_x \omega_y - \phi_y \omega_x$.
Eq.~\eqref{eq:euler_poisson} is a reformulation of the standard conservative form
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \nabla\cdot({\vec v \omega}) = 0 \\
\nabla\cdot\vec v = 0 \quad \omega = -(\nabla\times \vec v)\cdot \zhat
\end{align}
\label{eq:euler_conservative}
\end{subequations}
with $v_x = - \phi_y$ and $v_y = \phi_x$
or since the divergence of $\vec v$ is $0$, $\nc \vec v = 0$ we have the advection form
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \vec v\cn \omega = 0 \\
    -\Delta\phi = \omega \quad v_x = -\phi_y \quad v_y = \phi_x
\end{align}
\label{eq:euler_advection}
\end{subequations}

Eqs.~\eqref{eq:euler_poisson} have an infinite amount of conserved quantities
among them the total vorticity $V$, the kinetic energy $E$ and the enstrophy $\Omega$
 \begin{align}
     V := \int_D \omega \dA\quad
     E :=\frac{1}{2} \int_D \left( \nabla \phi\right)^2 \dA \quad
     \Omega:= \frac{1}{2} \int_D \omega^2 \dA
 \end{align}

\section{Initialization}
Input file format: \href{https://en.wikipedia.org/wiki/JSON}{json} \\
We will consider several different initial conditions in order to test
our numerical methods
\subsection{Lamb Dipole}
The Lamb dipole is a stationary solution to the Euler equations~\cite{Nielsen1997} with infinite
boundary conditions
\begin{align}
    \omega(x,y,0) = \begin{cases}
        \frac{2\lambda U}{J_0(\lambda R)} J_1(\lambda R) \cos \theta,\ r < R,\\
        0, \text{ else}
    \end{cases}
\end{align}
Unfortunately, for a finite box this is not an exact solution any more.
on the domain $[0,1]\times [0,1]$.
The Lamb dipole is chosen with the following parameters in the input file
\begin{minted}[texcomments]{js}
"grid" :
{
    "x" : [0.0,1.0], // Choose x box boundaries appropriately
    "y" : [0.0,1.0], // Choose y box boundaries appropriately
    "bc" : ["DIR", "PER"] // Choose boundary conditions [x,y] appropriately
}
"init" :
{
    "type"      : "lamb", // This choice necessitates the following parameters
    "velocity"  : 1.0,  //  blob speed $U$
    "sigma"     : 0.1,  // dipole radius in units of $l_x$
    "posX"      : 0.5,  // in units of $l_x$
    "posY"      : 0.8  // in units of $l_y$
}
\end{minted}
\subsection{Manufactured Solution}
We manufacture a solution via
\begin{align}
    \phi(x,y,t) &=
    x \exp\left( - \frac{ x^2 + (y+vt)^2}{\sigma^2}\right) \\
    \omega(x,y,t) &= -\Delta \phi = -\sigma^{-4} \left[ 4\phi(x,y,t) ( x^2-2\sigma^2  + (y+tv)^2)\right]
\end{align}
which is solution to the modified equations
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = S(x,y,t) \\
    -\Delta \phi = \omega
\end{align}
\label{eq:euler_poisson_modified}
\end{subequations}
with the source
\begin{align}
    S(x,y,t) =& 8 x \sigma^{-6}(y+vt)\exp\left( - 2\frac{ x^2 + (y+vt)^2}{\sigma^2} \right) \nonumber\\
    &\left(-\sigma^2  + \exp\left( \frac{ x^2 + (y+vt)^2}{\sigma^2} \right) v( -3\sigma^2 + x^2 + (y+vt)^2) \right)
\end{align}
on the domain $[-1,1]\times [-1,1]$.

The manufactured solution is chosen with the following parameters in the input file
\begin{minted}[texcomments]{js}
"grid" :
{
    "x" : [-1.0,1.0], // Choose x box boundaries appropriately
    "y" : [-1.0,1.0], // Choose y box boundaries appropriately
    "bc" : ["DIR", "PER"] // Choose boundary conditions [x,y] appropriately
}
"init" :
{
    "type"      : "mms", // This choice necessitates the following parameters
    "velocity"  : 1.0, //  blob speed $v$
    "sigma"     : 0.2  // the width $\sigma$
}
\end{minted}
\subsection{Simple sine function}
A simple sine function is given by
\begin{align}
    \omega(x,y,0) = 2 \sin(x)\sin(y)
\end{align}
which has an analytical solution
\begin{align}
\omega(x,y,t) = 2 \sin(x)\sin(y)\exp( -(2\nu)^s t)
\end{align}
if there is artificial viscosity and is invariant else.
on the domain $[0,2\pi]\times [0,2\pi]$.
This solution is chosen with the following parameters in the input file
\begin{minted}[texcomments]{js}
"grid" :
{
    "x" : [0.0, 6.283185307179586], // Choose x box boundaries appropriately
    "y" : [0.0, 6.283185307179586], // Choose y box boundaries appropriately
    "bc" : ["DIR", "PER"] // Choose boundary conditions [x,y] appropriately
}
"init" :
{
    "type"      : "sine" // No other parameters are necessary
}
\end{minted}

\subsection{ Double Shear layer}
Here, we follow~\cite{Liu2000} and test the scheme on a double shear layer problem.
\begin{align}
    \omega(x,y,0) = \begin{cases}
        \delta \cos(x) - \frac{1}{\rho} \text{sech}^2 \left(\frac{y-\pi/2}{\rho}\right),\ y \leq \pi \\
        \delta \cos(x) + \frac{1}{\rho} \text{sech}^2 \left(\frac{3\pi/2-y}{\rho}\right),\ y > \pi \\
    \end{cases}
\end{align}
where $\rho = \pi/15$ and $\delta =0.05$ on the domain $[0,2\pi]\times [0,2\pi]$.
This solution will quickly roll-up and generate smaller and smaller scales.
A thin shear layer corresponds to $\rho = \pi/50$ or smaller.
The double shear layer initialization is chosen with the following parameters in the input file
\begin{minted}[texcomments]{js}
"grid" :
{
    "x"  : [0.0, 6.283185307179586], // Choose x box boundaries appropriately
    "y"  : [0.0, 6.283185307179586], // Choose y box boundaries appropriately
    "bc" : ["PER", "DIR"] // Choose boundary conditions [x,y] appropriately
}
"init" :
{
    "type"  : "shear", // This choice necessitates the following parameters
    "rho"   : 0.20943951023931953, // The width $\rho = \pi/15$ \\
    "delta" : 0.05 // The velocity $v$ \\
}
\end{minted}

\section{Numerical methods}
Our goal is to try out various time integration and advection discretization techniques.
We know from Godunov's theorem
that any linear advection scheme of order 2 or higher is prone to oscillations.
\subsection{Spatial grid}
The spatial grid is a two-dimensional Cartesian product-grid adaptable with the following parameters
\begin{minted}[texcomments]{js}
"grid" :
{
    "n"  :  3, // The number of polynomial coefficients
    "Nx"  : 48, // Number of cells in x
    "Ny"  : 48, // Number of cells in y
    "x"  : [0.0,1.0], // Boundaries in x
    "y"  : [0.0,1.0], // Boundaries in y
    "bc" : ["DIR", "PER"] // Boundary conditions in [x,y]
}
\end{minted}
\subsection{Time steppers}
Possible time-steppers are the explicit and semi-implicit multistep schemes
as well as the Shu-Osher scheme that originally incorporated limiters in the dG scheme.
\begin{minted}[texcomments]{js}
"timestepper" :
{
    // The semi-implicit multistep scheme (only in combination with
    // viscosity regularization)
    "type"     : "ImExMultistep",
    "tableau"  : "ImEx-BDF-3-3", //  Any ImEx tableau *
    "dt"       : 2e-3, // Fixed timestep
    "eps_time" : 1e-9 // Accuracy requirement for implicit solver
},
"timestepper":
{
    // An explicit Runge Kutta method with filter (viscosity is treated
    // explicitly)
    "type"      : "Shu-Osher",
    "tableau"   : "SSPRK-3-3", // Any Shu-Osher tableau *
    "dt"        : 1e-3 // Fixed Time-step
},
"timestepper",
{
    // an explicit multistep class with the option to use a filter (viscosity
    // is treated explicitly)
    "type"      : "FilteredExplicitMultistep"
    "tableau"   : "eBDF-3-3" // Any explicit multistep tableau *
    "dt"        : 2e-3 // Fixed timestep
},
\end{minted}
*See the dg documentation for what tableaus are available.
\subsection{Regularization technique}
Choose either no regularization or artificial viscosity or modal filtering by the following
parameters in the input file.

For no regularization choose
\begin{minted}[texcomments]{js}
"regularization",
{
    "type" : "none" //No regularization
}
\end{minted}

For artificial viscosity Eqs.~\eqref{eq:euler_poisson} are modified to
\begin{subequations}
\begin{align}
    \frac{\partial \omega}{\partial t} + \{ \phi, \omega\} = -(-\nu \Delta)^s \omega\\
 -\Delta \phi = \omega
\end{align}
\label{eq:euler_poisson_viscous}
\end{subequations}
where $\nu$ is the viscosity coefficient and $s=1,2,3,\cdots$ is the order
\begin{minted}[texcomments]{js}
"regularization",
{
    "type"      : "viscosity", // Artificial viscosity
    "order"     : 2 , // Order: 1 is normal diffusion, 2 is hyperdiffusion, can be
    // arbitrarily high, but higher orders might take longer to solve or restrict
    // the CFL condition
    "nu"        : 1e-3, // Viscosity coefficient
    "direction" : "centered" //Direction of Laplacian: forward or centered
}
\end{minted}
The other regularization method is the modal filter that applies an exponential filter
\begin{align}
    \begin{cases}
    1 \text{ if } \eta < \eta_c \\
    \exp\left( -\alpha  \left(\frac{\eta-\eta_c}{1-\eta_c} \right)^{2s}\right) \text { if } \eta \geq \eta_c \\
    0 \text{ else} \\
    \eta := \frac{i}{n-1}
    \end{cases}
\end{align}
and is choosable with the following parameters
\begin{minted}[texcomments]{js}
"regularization",
{
    "type"  : "modal", // Not choosable for \textbf{ImExMultistep} timestepper
    "order" : 8,  // Order: normally 8 or 16
    "eta_c" : 0.5, // cutoff wavelength below which no damping is applied
    "alpha" : 36.0 // damping coefficient determining damping for highest
    //wavenumber
}
\end{minted}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Elliptic solver}
Currently, the multigrid solver is the only one choosable
to solve Eq.~\eqref{eq:euler_poisson_elliptic}
\begin{minted}[texcomments]{js}
"elliptic",
{
    "type"      : "multigrid", // Actually a nested iterations method
    "stages"    : 3,  // Number of stages (3 is best in virtually all cases)
    "eps_pol"   : [1e-6,10,10],
    // Accuracy requirement on each stage of the
    // multigrid scheme. $\eps_0 = \eps_{pol,0}$, $\eps_i = \eps_{pol,i} \eps_{pol,0}$  for $i>1$. \\
    "direction" : "centered" // Direction of the Laplacian: forward or centered
}
\end{minted}
\subsection{Advection schemes}
The following parameters control the advection scheme in the code
\begin{minted}[texcomments]{js}
"advection",
{
    "type" : "arakawa", // Discretize Eqs.\eqref{eq:euler_poisson} using Arakawa's scheme \cite{Einkemmer2014}
    "type" : "centered" // Discretize Eqs.\eqref{eq:euler_conservative} using the centered flux
    "type" : "upwind" // Discretize Eqs.\eqref{eq:euler_conservative} using the upwind flux
    "type" : "centered-advection" // Discretize Eqs.\eqref{eq:euler_advection} using the centered flux
    "type" : "upwind-advection" // Discretize Eqs.\eqref{eq:euler_advection} using the upwind flux
    "multiplication" : "pointwise"
    // The multiplications in the scheme are done pointwise in nodal space
    "multiplication" : "projection"
    // The multiplications in the scheme are done by first interpolating to a
    // higher polynomial grid with $n_{fine} = 2n$, and projecting the result back
    // to the coarse grid
}
\end{minted}

\section{Compilation and useage}
The program shu\_b.cu compiles with
\begin{verbatim}
make <shu_b> device = <omp gpu>
make <shu_hpc> device = <omp gpu>
\end{verbatim}
and depends on both GLFW3 and NETCDF. If GLFW3 is not available then compile shu\_hpc which avoids this dependency.
Run with
\begin{verbatim}
path/to/feltor/src/shu/shu_b input.json
\end{verbatim}

\subsection{Output structure}

We can either display the results in real-time to screen using the glfw3 library or
write the results to a file in netcdf-4 format.
This is regulated by the output paramters in the input file
\begin{minted}[texcomments]{js}
"output":
{
    // Use glfw to display results in a window while computing (requires to
    // compile with the glfw3 library)
    "type"  : "glfw"
    "itstp"  : 4, // The number of steps between outputs of 2d fields \\
    // Use netcdf to write results into a file
    // (see next section for information about what is written in there)
    "type"  : "netcdf"
    "itstp"  : 4, // The number of steps between outputs of 2d fields \\
    "maxout"  : 500 // The total number of field outputs. The endtime is
    //T=itstp*maxout*dt
}
\end{minted}
\subsection{Structure of output file}
Output file format: netcdf-4/hdf5
%
%Name | Type | Dimensionality | Description
%---|---|---|---|
\begin{longtable}{lll>{\RaggedRight}p{7cm}}
\toprule
\rowcolor{gray!50}\textbf{Name} &  \textbf{Type} & \textbf{Dimension} & \textbf{Description}  \\ \midrule
inputfile  &             text attribute & 1 & verbose input file as a string \\
time                     & Coord. Var. & 1 (time) & time at which fields are written \\
x                        & Coord. Var. & 1 (x) & x-coordinate  \\
y                        & Coord. Var. & 1 (y) & y-coordinate \\
xc                       & Dataset & 2 (y,x) & Cartesian x-coordinate  \\
yc                       & Dataset & 2 (y,x) & Cartesian y-coordinate \\
vorticity                & Dataset & 3 (time, y, x) & electon density $n$ \\
potential                & Dataset & 3 (time, y, x) & electric potential $\phi$  \\
vorticity\_1d            & Dataset & 1 (time) & Vorticity integral $V$  \\
enstrophy\_1d            & Dataset & 1 (time) & Enstropy integral $\Omega$  \\
energy\_1d               & Dataset & 1 (time) & Total energy integral computed using $E = \int_D \phi\omega \dA$ \\
time\_per\_step          & Dataset & 1 (time) & Average computation time for one step \\
error                    & Dataset & 1 (time) & Relative error to analytical solution if available, 0 else \\
\bottomrule
\end{longtable}
The output fields are determined in the file \texttt{feltor/src/lamb\_dipole/diag.h}.

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

\end{document}


\subsection{Abstract algebra}

Abstract algebra allows us to discuss properties of types of mathematical structures.

Rather than construct a specific object, and explore its properties, we can explore the properties of an abstract structure with certain definitions. We can then apply findings from this to an any structure which meets the definition.

\subsubsection{Examples of abstract algebra}

We explore:

\begin{itemize}
\item Groups
\item Rings
\item Fields
\item Vector spaces
\item Inner product spaces
\end{itemize}


\documentclass{article}
\title{ALO for Logistic Regression and Poisson Regression}
\author{Yuze Zhou}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\begin{document}
\section{ALO for Logistic Regression}
\subsection{ALO for Logistic Regression with Lasso penalty}
\paragraph{}First Let's rewrite the optimization problem with the loss functions separated for each observation, therefore the loss function goes:
\begin{center}
$-\sum\limits_{i}(y_{i}x_{i}^{\tau}\beta+log(1+exp(x_{i}^{\tau}\beta)))+\lambda_{1}||\beta||_{1}$
\end{center}
\paragraph{}Where, separatively, the loss function is $l(x_{i}^{\tau}\beta ;y_{i}) = y_{i}x_{i}^{\tau}\beta+log(1+exp(x_{i}^{\tau}\beta))$ and the regularizer is $R(\beta) = \lambda_{1}||\beta||_{1}$, from which we could derive the dual optimal $\hat{\theta} = y - \frac{e^{X\beta}}{1+e^{X\beta}}$, as well as the conjugate functions of them as $l^{*}(-\theta_{i};y_{i})=(y_{i}-\theta_{i})ln\frac{y_{i}-\theta_{i}}{1-(y_{i}-\theta_{i})}-ln\frac{1}{1-(y_{i}-\theta_{i})}$,
\begin{center}
$R^{*}(\beta) = \left\{
\begin{aligned}
0 \quad ||\beta||_{\infty} \leq \lambda_{1}\\
\infty \quad o.w.\\
\end{aligned}
\right.$
\end{center}
\paragraph{}From the results of the conjugate functions above, we could also obtain the derivatives of the loss functions and the Jacobian of the regularizer:
\begin{center}
$\dot{l}^{*}(-\theta_{i};y_{i}) = ln \frac{y_{i}-\theta_{i}}{1-(y_{i}-\theta_{i})}$\\
$\ddot{l}^{*}(-\theta_{i};y_{i}) = \frac{1}{(y_{i}-\theta_{i})(1-(y_{i}-\theta_{i}))}$
\end{center}
\paragraph{}Recall (15) from the \textbf{General Smooth Loss}, the quadratic surrogate of the dual problem is $\min\limits_{u} \frac{1}{2}\sum\limits_{i}(u_{i}-\frac{\hat{\theta}_{i}\ddot{l}^{*}(-\hat{\theta}_{i};y_{i})+\hat{y}_{i}}{\sqrt{\ddot{l}^{*}(-\hat{\theta}_{i};y_{i})}})^{2}+R^{*}(X^{\tau}Ku)$, where $K =diag(\sqrt{\ddot{l}^{*}(-\hat{\theta}_{i};y_{i})})$ therefore the Jacobian at $y_{u} = \frac{\hat{\theta}_{i}\ddot{l}^{*}(-\hat{\theta}_{i};y_{i})+\hat{\theta}_{i}}{\sqrt{\ddot{l}^{*}(-\hat{\theta}_{i};y_{i})}}$ could locally be treated as the projection onto the orthogonal complement of the polyhedra $\{||X^{\tau}Ku||_{\infty} \leq \lambda_{1}\}$, thus $J = I - X_{u,E}(X_{u,E}^{\tau}X_{u,E})^{-1}X_{u,E}$, where $X_{u,E}$ are the columns of $X_{u}=X^{\tau}K$, such that the columns in the set $E = \{|X_{i}^{\tau}\theta| = \lambda_{1}\}$ are selected. Take everything to (17), $y^{/i} = K_{ii}(y_{u,i}-\frac{K_{ii}\hat{\theta}_{i}}{J_{ii}})$, we could obtain the alo for the $i$th observation.
\subsubsection{ALO for Logistic Regression with Lasso Penalty with intercept included}
\paragraph{}If we include intercept for the model but it will not be penalized, and rewrite $\tilde{x_{i}} = [1; x_{i}]$, $\tilde{X} = [
\textbf{1},X]$ and $\tilde{\beta} = [\beta_{0},\beta]$, the loss function stays in the same form as the case with no intercept, that is $l(\tilde{x_{i}}^{\tau}\tilde{\beta};y_{i})= -(y_{i}\tilde{x_{i}}^{\tau}\tilde{\beta}+log(1+exp(\tilde{x_{i}}^{\tau}\tilde{\beta})))$, and the but the regularizer will be different, it becomes $R(\tilde{\beta}) = \lambda_{1}||D\tilde{\beta}||_{1}$, where $D = [\textbf{0},I]$. Therefore, the corresponding dual optimal becomes $\hat{\theta} = y - \frac{e^{\tilde{X}\tilde{\beta}}}{1+e^{\tilde{X}\tilde{\beta}}}$, the conjugate of the loss function will stay the same, but the conjugate for $R(\tilde{\beta})$ shall be changed.
\begin{center}
$R^{*}(\tilde{\beta}) = \left\{
\begin{aligned}
0 & \quad if \quad \beta_{0} = 0 \quad and \quad \beta_{i} \leq \lambda_{1}\\
+\infty & \quad o.w.
\end{aligned}
\right.$
\end{center}
\paragraph{}Given the new regularizer and loss function, we could derive the new $\tilde{X}_{u} = K^{-1}\tilde{X}$, and the corresponding Jacobian is therefore $J = I -\tilde{X}_{u,\tilde{E}}(\tilde{X}_{u,\tilde{E}}^{\tau}\tilde{X}_{u,\tilde{E}})^{-1}\tilde{X}_{u,\tilde{E}}^{\tau}$, where $\tilde{E}$ denotes the columns of $\tilde{X}_{u}$ such that $\tilde{E} = \{j:\tilde{X}_{j}^{\tau}\theta \leq \lambda_{1}\}$.
\subsection{ALO for Logistic Regression with Elastic Net Penalty}
\paragraph{}The optimization problem for logistic regression with elastic net penalty is:
\begin{center}
$-\sum\limits_{i}(y_{i}x_{i}^{\tau}\beta+log(1+exp(x_{i}^{\tau}\beta)))+\lambda_{1}||\beta||_{1}+\lambda_{2}||\beta||_{2}^{2}$
\end{center}
\paragraph{}The optimization problem is the same except the regularizer is changed, therefore the only thing different is the conjugate function of the regularizer, $R^{*}$ and the corresponding Jacobian, here $R(\beta) = \lambda_{1}||\beta||_{1}+\lambda_{2}||\beta||_{2}^{2}$:
\begin{center}
$R^{*}(\beta) = \sum\limits_{|u_{i}| > \lambda_{1}} \frac{(\lambda_{1}-|u_{i}|)^{2}}{4\lambda_{2}}$
\end{center}
\paragraph{}The corresponding Jacobian is $J = (I + \frac{1}{2\lambda_{2}}X_{u,E}X_{u,E}^{\tau})$, where $X_{u,E}$ are the columns of $X_{u}=K^{-1}X$, such that the columns in the set $E = \{|X_{i}^{\tau}\theta| = \lambda_{1}\}$ are selected. Take everything to (17), $y^{/i} = K_{ii}(y_{u,i}-\frac{K_{ii}\hat{\theta}_{i}}{J_{ii}})$, we could obtain the alo for the $i$th observation.
\subsubsection{ALO for Logistic Regression with Elastic Net Penalty with intercept}
\paragraph{}The loss function for logistic regression with elastic net penalty with intercept is the same as the case without an intercept, however, the regularizer function is different. The dual optimal will also be $\hat{\theta} = y - \frac{e^{X\beta}}{1+e^{X\beta}}$. Using the same notations $D$ and $\tilde{\beta}$in the logistic regression with lasso penalty with intercept part, the regularizer now becomes $\lambda_{1}||D\beta||_{1}+\lambda_{2}||D\beta||_{2}^{2}$, the conjugate of which is therefore:
\begin{center}
$R^{*}(\beta) = \left\{
\begin{aligned}
\sum\limits_{i:|\beta_{i}|>\lambda_{1}}\frac{(\lambda_{1}-|\beta_{1}|)^{2}}{4\lambda_{2}} &\quad if \quad \beta_{0}=0\\
+\infty \quad o.w.
\end{aligned}
\right.$
\end{center}
\paragraph{}First denote the set $\{E:|X_{j}^{\tau}\theta| > \lambda_{1}\}$ and $A = \frac{1}{2\lambda_{2}}\tilde{X}_{u,E}\tilde{X}_{u,E}^{\tau}$, the new Jacobian becomes:
\begin{center}
$J = (I+A)^{-1}-\frac{(I+A)^{-1}\textbf{1}\textbf{1}^{\tau}(I+A)^{-1}}{\textbf{1}^{\tau}(I+A)^{-1}\textbf{1}}$
\end{center}
\paragraph{}By plugging everything into (17), we could obtain the alo.
\section{ALO for Poisson Regression}
\subsection{ALO for Poisson Regression with Lasso Penalty}
\paragraph{}The optimization function for Poisson regression with lasso penalty is:
\begin{center}
$\sum\limits_{i}-y_{i}x_{i}^{\tau}\beta+e^{x_{i}^{\tau}\beta}+log(y_{i}!) + \lambda_{1}||\beta||_{1}$
\end{center}
\paragraph{}The regularizer is the same as the logistic regression with the lasso penalty case, thus the Jacobian will also be the same, therefore we only have to focus on the loss function $l(x_{i}^{\tau}\beta ; y_{i}) = -y_{i}x_{i}^{\tau}\beta+e^{x_{i}^{\tau}\beta}+log(y_{i}!)$. The optimal solution for the dual problem $\hat{\theta} = y - e^{X\beta}$ and the conjugate of the loss function is $l^{*}(-\theta_{i};y_{i}) = (y_{i}-\theta_{i})ln(y_{i}-\theta_{i})-(y_{i}-\theta_{i})$, the corresponding derivatives are therefore:
\begin{center}
$\dot{l}^{*}(-\theta_{i};y_{i}) = ln(y_{i}-\theta_{i})$\\
$\ddot{l}^{*}(-\theta_{i};y_{i}) = \frac{1}{y_{i}-\theta_{i}}$
\end{center}
\paragraph{}By plugging everything into (17), we obtain the alo for Poisson regression with the lasso penalty.
\subsubsection{ALO for Poisson Regression with Lasso Penalty with Intercept}
\paragraph{}Using the same notations $\tilde{X}$ and $\tilde{\beta}$, the loss function now becomes $l(x_{i}^{\tau}\beta ; y_{i}) = -y_{i}\tilde{x}_{i}^{\tau}\tilde{\beta}+e^{\tilde{x}_{i}^{\tau}\tilde{\beta}}+log(y_{i}!)$, and the dual optimal now becomes $\hat{\theta} = y - e^{\tilde{X}\beta}$. The regularizer is also the same as the logistic regression with lasso penalty with lasso penalty with intercept case, therefore the Jacobian is of the same formula, thus by plugging everything into (17), we could obtain the alo.
\subsection{ALO for Poisson Regression with Elastic Net Penalty}
\paragraph{}The loss function for Poisson regression with elastic net penalty is the same as that of Poisson regression with the lasso penalty and the regularizer of it is the same as that of logistic regression with elastic net penalty, thus by plugging everything into (17), we could obtain the alo for Poisson regression with elastic net penalty.
\subsubsection{ALO for Poisson Regression with Elastic Net Penalty with Intercept}
\paragraph{}Here the loss function is the same as that of Poisson Regression with Lasso penalty with intercept case and the regularizer is the same as Logistic Regression with elastic net penalty with intercept, thus the formula for the derivatives of loss functions, the dual optimal as well as the Jacobian could be directly obtained using the same formula as mentioned before.
\end{document}


\subsection{Interpolation}
This section decribes all the functions in the file ``ptwXY\_interpolation.c''.

\subsubsection{ptwXY\_interpolatePoint}
\setargumentNameLengths{interpolation}
This function interpolates an $x$ value between the points (x1,y1) and (x2,y2) to obtain its $y$ value
for the requested \highlight{interpolation}.
\CallingC{fnu\_status ptwXY\_interpolatePoint(}{statusMessageReporting *smr,
    \addArgument{ptwXY\_interpolation interpolation,}
    \addArgument{double x,}
    \addArgument{double *y,}
    \addArgument{double x1,}
    \addArgument{double y1,}
    \addArgument{double x2,}
    \addArgument{double y2 );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{interpolation}{Type of interpolation to perform (see Section~\ref{interpolationSection}).}
    \argumentBox{x}{The $x$ value at which the $y$ value is desired.}
    \argumentBox{x1}{The $x$ value of the first point.}
    \argumentBox{y1}{The $y$ value of the first point.}
    \argumentBox{x2}{The $x$ value of the second point.}
    \argumentBox{y2}{The $y$ value of the second point.}
    \vskip 0.05 in \noindent
If the interpolation flag is invalid or ( x1 $>$ x2 ) then \highlight{nfu\_invalid\-Interpolation} is returned.
If logarithm interpolation is requested for an axis, and one of the input values for that axis is less than or equal to 0.,
then \highlight{nfu\_invalid\-Interpolation} is also returned. If interpolation is \highlight{ptwXY\_interpolationOther} then
\highlight{nfu\_otherInterpolation} is returned.

\subsubsection{ptwXY\_flatInterpolationToLinear}
This function returns a linear-linear interpolated representation of \highlight{ptwXY}.
\setargumentNameLengths{upperEps}
\CallingC{ptwXYPoints *ptwXY\_flatInterpolationToLinear(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{double lowerEps,}
    \addArgument{double upperEps );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{lowerEps}{The amount to adjust every interior point down in x.}
    \argumentBox{upperEps}{The amount to adjust every interior point up in x}
    \vskip 0.05 in \noindent
For every interior point (i.e., $(x_i,y_i)$ for $0 < i < n - 1$ where n is the number of points), two points may be added.
The positions of these points depend on \highlight{lowerEps} and \highlight{upperEps} as follows:
\begin{description}
    \item[lowerEps ==  0 and upperEps ==  0:] This condition is not allowed. status is set to \highlight{nfu\_bad\-Input} and NULL is returned.
        This condition is also returned if either \highlight{lowerEps} or \highlight{upperEps} is negative.
    \item[lowerEps $>$ 0 and upperEps ==  0:] At each interior point $(x_i,y_i)$ the two points $(x_m,y_{i-1})$ and $(x_i,y_i)$ are set.
    \item[lowerEps ==  0 and upperEps $>$ 0:] At each interior point $(x_i,y_i)$ the two points $(x_i,y_{i-1})$ and $(x_p,y_i)$ are set.
    \item[lowerEps $>$ 0 and upperEps $>$ 0:] At each interior point $(x_i,y_i)$, this point is removed and the two
        points $(x_m,y_{i-1})$ and $(x_p,y_i)$ are set.
\end{description}
where $x_m$ and $x_p$ are given in Table~\ref{flatInterpolationToLinear}.
\begin{table}
\begin{center}
\begin{tabular}{|c|l|l|}  \hline
                & $x_m$                    & $x_p$                          \\ \hline \hline
    $x_i <  0$  & $x_i ( 1 + \epsilon_l )$ & $x_p = x_i ( 1 - \epsilon_p )$ \\ \hline
    $x_i == 0$  & $ -\epsilon_l $          & $ \epsilon_p $                 \\ \hline
    $x_i >  0$  & $x_i ( 1 - \epsilon_l )$ & $x_p = x_i ( 1 + \epsilon_p )$ \\ \hline
\end{tabular}
\end{center}
\caption{The value of $x_m$ and $x_p$ used to adjust interior points in \highlight{ptwXY\_fla-Interpolation\-To\-Linear}.
    Here, $ \epsilon_l = $ \highlight{lowerEps} and $ \epsilon_p = $ \highlight{upperEps}. \label{flatInterpolationToLinear}}
\end{table}

\subsubsection{ptwXY\_toOtherInterpolation}
This function returns \highlight{ptwXY} converted to interpolation \highlight{interpolation}.
\setargumentNameLengths{interpolation}
\CallingC{ptwXYPoints *ptwXY\_toOtherInterpolation(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{ptwXY\_interpolation interpolation,}
    \addArgument{double accuracy );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{interpolation}{The interpolation to convert to.}
    \argumentBox{accuracy}{The accuracy of the conversion.}
    \vskip 0.05 in \noindent
Currently, \highlight{interpolation} can only be \highlight{ptwXY\_\-interpolation\-LinLin}.

\subsubsection{ptwXY\_toUnitbase}
This function returns a unit-based version of \highlight{ptwXY}.
\setargumentNameLengths{scaleRange}
\CallingC{ptwXYPoints *ptwXY\_toUnitbase(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{int scaleRange );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{scaleRange}{The y-values are not scaled if this is 0.}
    \vskip 0.05 in \noindent
Unitbasing maps the domain to 0 to 1 by scaling each x-value as
\begin{equation}
    x_i = ( x_i - x_0 ) / ( x_{n-1} - x_0 )
\end{equation}
and if \highlight{scaleRange} is not 0, scaling each y-value as
\begin{equation}
    y_i = y_i \times ( x_{n-1} - x_0 ) \ \ \ .
\end{equation}
Unitbasing is most useful for pdf's.

\subsubsection{ptwXY\_fromUnitbase}
This function undoes the unit base mapping done by \highlight{ptwXY\_toUnitbase}.
\setargumentNameLengths{scaleRange}
\CallingC{ptwXYPoints *ptwXY\_fromUnitbase(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{double domainMin,}
    \addArgument{double domainMax,}
    \addArgument{int scaleRange );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{domainMin}{The lower domain for the returned \highlight{ptwXYPoints} instances.}
    \argumentBox{domainMax}{The upper domain for the returned \highlight{ptwXYPoints} instances.}
    \argumentBox{scaleRange}{The y-values are not scaled if this is 0.}
    \vskip 0.05 in \noindent
Each x-value is scaled as
\begin{equation}
    x_i = ( {\rm domainMax} - {\rm domainMin} ) \times x_i + {\rm domainMin}
\end{equation}
and if \highlight{scaleRange} is not 0, each y-value is scaled as
\begin{equation}
y_i = y_i / ( {\rm domainMax} - {\rm domainMin} ) \ \ \ \
\end{equation}

\subsubsection{ptwXY\_unitbaseInterpolate}
This function returns a \highlight{ptwXYPoints} instance that is the unit-base interpolation of \highlight{ptwXY1} at $w_1$
and \highlight{ptwXY2} at $w_2$ at the w-value $w$.
\setargumentNameLengths{interpolation}
\CallingC{ptwXYPoints *ptwXY\_unitbaseInterpolate(}{statusMessageReporting *smr,
    \addArgument{double w,}
    \addArgument{double w1,}
    \addArgument{ptwXYPoints *ptwXY1,}
    \addArgument{double w2,}
    \addArgument{ptwXYPoints *ptwXY2,}
    \addArgument{scaleRange );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{w}{The w-value to interpole to.}
    \argumentBox{w1}{The lower w-value}
    \argumentBox{ptwXY1}{A pointer to a \highlight{ptwXYPoints} object at w1.}
    \argumentBox{w2}{The upper w-value}
    \argumentBox{ptwXY2}{A pointer to a \highlight{ptwXYPoints} object at w2.}
    \argumentBox{scaleRange}{The y-values are not scaled if this is 0.}
    \vskip 0.05 in \noindent


%%%%%%%%%%%%%%%%%%%%%definitions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%DOCUMENT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
%\preprint{}

\title{The parallel derivative on structured grids}
\author{M.~Wiesenberger and M.~ Held}

\maketitle

\abstract{
This write-up shows how we numerically treat parallel derivatives in a non field
aligned coordinate system. It is mainly based on References~\cite{Hariri2014,Held2016,Stegmeir2017} and contains parts from References~\cite{WiesenbergerPhD, HeldPhD}
}
\section{Discretization of parallel derivatives} \label{sec:parallel}
We introduce the method
and discuss some problems arising from the boundaries of the computational domain.

\subsection{The flux coordinate independent approach} \label{sec:parallela}
Given is a vector field $\vec v(R,Z)$ in cylindrical coordinates $R,Z,\varphi$ independent of $\varphi$ and we want to
discretize the derivative $\vec v \cdot\nabla f \equiv \nabla_\parallel f$.
The vector field $\vec v$ might be the magnetic unit vector field but the algorithm works
for any vector field $\vec v$ with $v^\varphi\neq 0$, in particular $\vec v$ does not need
to have unit length.

We begin with the formulation of a field-aligned discretization.
To every smooth vector field $\vec v(\vec x)$ there is a unique curve of which the
tangent in a point $p$ is the value of $\vec v(p)$ at that point. It is given by
the solution of the differential equation
\begin{align}
  \frac{\d z^i}{\d s} = v^i(\vec z)|_{\vec z(s)}
    \label{eq:integralcurve}
\end{align}
where $z^i$ is one of $(R, Z, \varphi)$ and $v^i$ are the contravariant components
of $\vec v$ in cylindrical coordinates.
Note here that $s$ does NOT necessarily denote the distance
(especially since we do not require the existence of a metric at this point).
Moreover, by definition we have
\begin{align}
    \frac{\d f(\vec z(s))}{\d s} = \vec v\cdot \nabla f|_{\vec z(s)}
    \label{eq:fieldline_original}
\end{align}
along a field line parameterized by $s$.
This means that instead of $\vec v \cdot \nabla f$ we can choose to discretize ${\d f}/{\d s}$.

Let us divide the $\varphi$ direction into $N_\varphi$ equidistant planes of
$\Delta \varphi$. Unfortunately, from Eq.~\eqref{eq:integralcurve} we cannot easily determine
$\Delta s$ for given $\Delta \varphi$.
Thus, it is better to reparameterize Eq.~\eqref{eq:fieldline_original} with $\varphi$ instead of $s$:
\begin{subequations}
\begin{align}
    \frac{\d R}{\d\varphi}&= \frac{v^R}{v^\varphi},\\ %\frac{R}{I}\frac{\partial\psi}{\partial Z},\\
    \frac{\d Z}{\d\varphi}&=\frac{v^Z}{v^\varphi},\\%-\frac{R}{I}\frac{\partial\psi}{\partial R}.
    \frac{\d s}{\d\varphi}&=\frac{1}{v^\varphi}
\end{align}
\label{eq:fieldline}
\end{subequations}
We integrate Eqs.~\eqref{eq:fieldline} from $\varphi=\varphi_k$ to $\varphi=\varphi_k\pm \Delta \varphi$
with initial condition
\begin{align}
    (R(\varphi_k), Z(\varphi_k), s(\varphi_k) ) = (R, Z, s_k).
    \label{}
\end{align}
where $k$ is the numbering of the planes and $s_k$ is arbitrary.
Let us characterize the solution $(R(\varphi_k\pm \Delta \varphi), Z(\varphi_k\pm \Delta \varphi))$ to Eqs.~\eqref{eq:fieldline} as the flow generated by $\vec v/v^\varphi$
\begin{align}
    \Tpm\vec z \equiv \Tpm[R, Z, \varphi]:= ( R(\pm \Delta\varphi), Z( \pm \Delta\varphi), \varphi\pm\Delta \varphi),
    \label{}
\end{align}
Obviously we have $\Tm\circ\Tp = \Eins$, but $\Tpm$ is not necessarily unitary since $\vec v/v^\varphi$ is in general
not divergence free.

We now fit a second order polynomial through $(R,Z,\varphi)$, $\Tp [R,Z,\varphi]$ and $\Tm[R,Z,\varphi]$. We approximate the first and second derivative at $(R,Z,\varphi)$ by evaluating the derivative of the
polynomial at that point.
\begin{align} \label{eq:paralleldis}
    \nabla_\parallel f \equiv \frac{df}{ds}
    \rightarrow
    \left(\frac{ 1}{s_{k+1}-s_{k-1}} - \frac{1}{s_k - s_{k-1}}\right) &f_{k-1}  \nonumber\\
    +\left(\frac{ 1}{s_{k}-s_{k-1}} - \frac{1}{s_{k+1} - s_{k}}\right) &f_k\nonumber\\
    +\left(\frac{ 1}{s_{k+1}-s_{k}} - \frac{1}{s_{k+1} - s_{k-1}}\right) &f_{k+1}
    %\frac{f\left(T_{\Delta\varphi}^+\vec z_k\right)-f\left(T_{\Delta\varphi}^-\vec z_k\right)}{s_{k+1}-s_{k-1}},
\end{align}
where $\vec z_k = (R,Z,\varphi_k)$ and $f_k = f(\vec z_k)$
and $f_{k\pm 1} := f(\Tpm \vec z_k)$.
Note that Eq.~\eqref{eq:paralleldis} reduces to
the familiar centered difference formula in case of equidistant spacings.
Eq.~\eqref{eq:paralleldis} is slightly different from Reference~\cite{Hariri2014}, where the $\d f/\d\varphi v^\varphi$ is discretized to avoid integrating $s$.
However, our discretization has the advantage that it can be used for higher
order derivatives as well (e.g. $\d^2f/\d \varphi^2$), which is not possible otherwise due to the chain rule
for $v^\varphi$.

Since the $R$ and $Z$ coordinates are still discretized in the dG framework we note that in our work
the interpolation of $f$ on the transformed points $\Tpm\vec z$
is naturally given by interpolating the base polynomials.
Let us for a moment omit the $Z$ coordinate for ease of notation.
If $(R_{nj}, \varphi_k)$ are the grid points,
we call $(R^+_{nj}, \varphi_{k+1}) := \Tp[R_{nj}, \varphi_k]$ and
$(R_{nj}^-, \varphi_{k-1}) := \Tm[R_{nj}, \varphi_k]$ the transformed coordinates along
the field lines. We then have
\begin{subequations}
\begin{align}
    f(\Tp\vec z) = f( R^+_{nj}, \varphi_{k+1}) = \bar f_{k+1}^{ml}p_{ml}(R^+_{nj}) =: (I^+)_{nj}^{ml}f_{(k+1)ml} , \\
    f(\Tm\vec z) = f( R^-_{nj}, \varphi_{k-1}) = \bar f_{k-1}^{ml}p_{ml}(R^-_{nj}) =: (I^-)_{nj}^{ml}f_{(k-1)ml} ,
\end{align}
\label{eq:interpolation}
\end{subequations}
where the backward transformations of $\bar{ \vec f}$ are hidden in $I$.
Thus, the interpolation of all the necessary points can simply be written as a matrix-vector product, where the interpolation matrices $I^+$  and $I^-$ are independent of time since
the field lines are constant in time. The order of this interpolation is given by $P$, the number of polynomial coefficients.
A consistency check is the relation $I^+\circ I^- = \Eins$.

The discretization~\eqref{eq:paralleldis} can now be written as a matrix vector product
\begin{align}
\nabla_\parallel f \rightarrow  \left[S^+ \circ \Eins^+\otimes I^+ + S^0  + S^-\circ \Eins^- \otimes I^-  \right] \vec f,
    \label{}
\end{align}
where $S^+$, $S^0$ and $S^-$ are the diagonal matrices that contain the prefactors
in Eq.~\eqref{eq:paralleldis}.
This discretization is not skew-symmetric since the
field lines are not volume-preserving, or~$(I^+)^\mathrm{T} \neq I^-$.
In fact, the adjoint of the parallel derivative is
\begin{align}
    \nabla_\parallel^\dagger f = - \nabla\cdot(\vec v\ f ) \neq -\nabla_\parallel f.
    \label{}
\end{align}
Note that with this relation we can define the parallel
diffusion operator as
\begin{align}
    \Delta_\parallel := -\nabla_\parallel^\dagger \nabla_\parallel = (\nabla\cdot \vec{ \hat v}) \nabla_\parallel + \nabla_\parallel^2 ,
    \label{}
\end{align}
which is indeed the parallel part of the full Laplacian $\Delta = \nabla\cdot( \vec{ \hat v} \nabla_\parallel + \nabla_\perp)$.
$\vec{ \hat v} $ is the unit vector $\vec v/ |\vec v|$.

Note that the second order derivative $\nabla_\parallel^2$ can be
discretized using
\begin{align}\label{eq:second_order}
    \frac{\d^2 f}{\d s^2} \rightarrow
     \frac{2f_{k+1}}{(s_{k+1}-s_k)(s_{k+1}-s_{k-1})}
    -\frac{2f_{k}}{(s_{k+1}-s_k)(s_{k}-s_{k-1})} \nonumber\\
    +\frac{2f_{k-1}}{(s_{k}-s_{k-1})(s_{k+1}-s_{k-1})}
\end{align}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Change of coordinates}
In principle the above considerations hold in any
coordinate system $\eta,\zeta,\varphi$, since the directional derivative is
an intrinsic operation.
The only question is how to integrate the field lines in the
$\eta, \zeta,\varphi$ system
since we assumed that our vector field $\vec v(\vec x)$ was given
analytically in
cylindrical coordinates. There are two possibilities.
First, interpolate $R(\zeta_i, \eta_i), Z(\zeta_i, \eta_i)$ for
all $i$, then integrate $\vec v$ in $(R,Z)$ space and finally use
Newton iteration to find $\zeta(R^\pm_i, Z^\pm_i), \eta(R^\pm_i, Z^\pm_i)$.
The downside here is that it is difficult to tell when and where the fieldline leaves the simulation domain. (However this is true also for the
following approach, See Section~\ref{sec:boundary})

The second possibility (the one currently implemented)
is to integrate entirely in the
transformed coordinate system $\zeta, \eta, \varphi$.
The magnetic field can be easily transformed since we have the
Jacobian of the coordinate transformation
\begin{align}
    v^\zeta(\zeta, \eta) &= \left(\frac{\partial \zeta}{\partial R} v^{R} + \frac{\partial \zeta}{\partial Z}v^Z\right)_{R(\zeta, \eta), Z(\zeta, \eta)} \\
    v^\eta(\zeta, \eta) &= \left(\frac{\partial \eta}{\partial R} v^{R} + \frac{\partial \eta}{\partial Z}v^Z\right)_{R(\zeta, \eta), Z(\zeta, \eta)} \\
    v^\varphi(\zeta, \eta) &= v^\varphi({R(\zeta, \eta), Z(\zeta, \eta)})
    \label{eq:field_trafo}
\end{align}
The fieldline equations~\eqref{eq:fieldline} are still
\begin{subequations}
\begin{align}
\frac{\d \zeta}{\d\varphi} &= \frac{v^\zeta}{v^\varphi}\\
\frac{\d \eta}{\d\varphi} &= \frac{v^\eta}{v^\varphi}\\
\frac{\d s}{\d \varphi} &= \frac{1}{v^\varphi}
\end{align}
\label{eq:fieldlines_coords}
\end{subequations}
The issue here is that when integrating fieldlines we
have to interpolate the vector field $\vec v$ at arbitrary points
instead of simply evaluating the exact values.
However, the interpolation error vanishes with order $P$ in the
perpendicular plane so in order to mitigate this error
we transform $\vec v$ on a finer grid/higher order polynomials for more accurate
integration.
Apart from the issue of how to get the transformed vector field
the remaining algorithm for $\vec v\cdot\nabla$ is entirely unchanged
and Eq.~\eqref{eq:paralleldis} still holds.

\subsection{Boundary conditions} \label{sec:boundary}
The question is what to do when a fieldline intersects with the boundary
of the simulation domain before reaching the next plane.
Boundary conditions are formulated by either setting a value
on the boundary of the domain (Dirichlet) or by fixing
the derivative perpendicularly to the boundary (Neumann), or
a combination of both ( Robin).

\subsubsection{ Boundary conditions perpendicular to the wall}
The issue with Neumann boundary conditions is that they usually prescribe
derivatives perpendicular to the boundary
while the fieldlines are in general not perpendicular to the boundary.
One possible approach is to introduce ghostcells at the
places where fieldlines end. The value of the ghostcells are
as if we Fourier transformed the fields on the simulation domain
with the correct boundary conditions and thus have a periodic
extension of the fields beyond the boundaries.
For example, for Neumann boundary
conditions the field is effectively mirrored at the boundary, while for
Dirichlet boundary conditions the values are the negative mirror image. In one
dimension this reads
\begin{align}
f(x) = \pm f(2x_b-x)
\end{align}
where $x_b$ is the closest boundary $+$ is for Neumann, and $-$ for Dirichlet conditions.
This is very easily implemented (at least as long as we only allow
homogeneous Boundary conditions) since we don't actually have to
perform the Fourier transformation, we only need to mirror the end
coordinates at the boundary and then choose either the positive (Neumann) or
negative (Dirichlet) interpolation.

The above procedure works for cylindrical coordinates where we can
evaluate and thus integrate the vector field $\vec v$ even outside the domain.
This is unfortunately not true for transformed coordinates.
For now we have to rely on the fieldlines being aligned to the
boundary in these cases to avoid boundary conditions altogether.

The downside of this approach is that the mirrored point of a field-line
can lie very far away from the wall if the resolution in $\varphi$ is low (which
is the motivation to implement FCI in the first place) and
in addition also on a different flux surface.
In principle we need to increase the $\varphi$ resolution until we resolve
the perpendicular direction, in order to reliably converge with this method.
In practice we can often run a simulation stably with low resolution nevertheless.

In order to avoid coupling different flux surfaces through the boundary condition, we have the possibility to
mirror also the flux surfaces at the boundary.
This approach converges well and works stably if the flux surface is (or is close to) perpendicular to the wall.

\subsubsection{ Boundary conditions parallel to the fieldline}
The natural way to implement boundary conditions is to work them into the
interpolating polynomial. The boundary condition then replaces a third
point by setting
either a predefined value (Dirichlet) or derivative (Neumann) on the boundary.
Evaluating the interpolating polynomial at the middle point then yields
(assuming that $\Tp \vec z$ lies outside the boundary and $s_k <s_b^+ < s_{k+1}$ is where the boundary lies)
\begin{align} \label{eq:paralleldis_neup}
    \frac{df}{ds}
    \underset{\textsc{NEU}}{\rightarrow}
    \left(\frac{ 1}{s_{k}-s_{k-1}} - \frac{1}{2(s_b^+-s_k) + (s_k-s_{k-1})} \right) &(f_k-f_{k-1})\nonumber\\
    +\left(\frac{s_k - s_{k-1}}{2(s_b^+-s_k) + (s_k-s_{k-1})}\right) &f_{b+}'\\
\label{eq:second_order_neup}
    \frac{\d^2 f}{\d s^2}
    \underset{\textsc{NEU}}{\rightarrow}
    \frac{2}{2(s_b^+-s_k) + (s_k-s_{k-1})}\left(  f_{b+}' - \frac{1}{s_k - s_{k-1}}(f_k-f_{k-1})\right)
\end{align}
where $f_{b+}'$ is the value of the  derivative on the boundary (currently we only allow homogeneous boundary conditions, i.e. $f_{b+}' \equiv 0$).
If the boundary lies at $s_{k-1}<s_b^-<s_k$ then we have
\begin{align} \label{eq:paralleldis_neum}
    \frac{df}{ds}
    \underset{\textsc{NEU}}{\rightarrow}
    \left(\frac{ 1}{s_{k+1}-s_k} - \frac{1}{2(s_k - s_b^-) + (s_{k+1}-s_k)} \right) &(f_{k+1}-f_k)\nonumber\\
    +\left(\frac{s_{k+1} - s_k}{2(s_k - s_b^-) + (s_{k+1}-s_k)}\right) &f_{b-}'
    \\
\label{eq:second_order_neum}
    \frac{\d^2 f}{\d s^2}
    \underset{\textsc{NEU}}{\rightarrow}
    \frac{2}{2(s_k -s_b^-) + (s_{k+1}-s_{k})}\left(  -f_{b-}' + \frac{1}{s_{k+1}-s_k} (f_{k+1} - f_k)\right)
\end{align}
while if the fieldline intersects the wall on both ends we have
\begin{align} \label{eq:paralleldis_neupm}
    \frac{df}{ds}
    \underset{\textsc{NEU}}{\rightarrow}
    \frac{ 1}{s_b^+ - s_b^-}\left[({ s_k - s_b^-})f_{b+}' + ({s_{b}^+-s_k}) f_{b-}'\right]    \\
\label{eq:second_order_neupm}
    \frac{\d^2 f}{\d s^2}
    \underset{\textsc{NEU}}{\rightarrow}
    \frac{ 1}{s_b^+ - s_b^-}( f_{b+}' - f_{b-}')
\end{align}
The formulas for Dirichlet boundary conditions are the same as the original
formulas, with $s_{k+1}$, $f_{k+1}$ replaced by $s_b^+$, $f_b^+$ and/or $s_{k-1}$, $f_{k-1}$ replaced by $s_b^-$, $f_b^-$.

There is no difference between those formulas and actually evaluating the
interpolating polynomial at a ghost point and then using that point in the
original formulas. The reason is that the interpolating polynomial is unique
and thus is the value of its derivatives at $s_k$.

For the above formulas to work we need to find the exact place where the fieldline intersects
the boundary.
We have to find
$\varphi_b$ such that the result of the integration of Eq.~\eqref{eq:fieldline} from
$\varphi$ to $\varphi_b$ lies on the boundary.
The angle $\varphi_b$ can be found by a bisection algorithm knowing that $\varphi_k<\varphi_b < \varphi_k + \Delta\varphi$.
This kind of procedure is known as a shooting method.
Another possibility is to trick the fieldline integrator into finding the point for us.
We do this by setting $\vec v \equiv 0$ on all points outside the simulation box, which
makes the ODE integrator stop once it crosses the domain boundary.
This works fairly well with the adaptive embedded Runge Kutta method that we use
and in particular also works in transformed coordinates.

The advantage of this method is that the parallel derivative
does not couple different field lines through the boundary conditions. Thus
the dynamics on each field-line can be completely independent.
%However, the problem with this procedure in practice is the small
%distance between the starting point and the point where the corresponding
%fieldline intersects the boundary. This seriously deteriorates the
%CFL condition. (To ease the CFL condition
%was the reason to devise the algorithm in the first place)

\subsubsection{Avoiding boundary conditions in non-aligned systems} \label{sec:avoid}

When computing in non-aligned coordinate systems
one idea to avoid boundary conditions
is to simply cut the contribution from field lines
that leave the computational domain. While this might work in practice
it is \textbf{highly unclear} what numerical and physical side-effects this procedure might have.

Another solution would be to change the
vector field $\vec v$ and only retain the toroidal part of $\vec v$ on the
boundary ( $v^R|_{\partial\Omega} = v^Z|_{\partial\Omega} =0$). The fieldlines then have a kink on the boundary $\partial\Omega$.
On the other hand we can implement boundary conditions consistent with
the perpendicular ones since the fieldlines never leave the domain.
We simply interpolate the quantity to derive on the inner side of the
domain boundary (Neumann conditions = "No boundary condition") or
set the value to zero (Dirichlet condition).
\textbf{Unfortunately, when testing this procedure with an analytical solution
the error does not converge neither for Dirichlet nor for Neumann.}

\subsection{Poloidal limiters}
A poloidal limiter can simply be implemented via a boundary condition in $\varphi$.
As long as the form of the limiter is aligned with a flux-function we do not have to
integrate a field line in order to determine which points lie in the
limiter-shadow. It is therefore straightforward to implement ghost-cells
in that case.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{The adjoint methods}
\subsection{A grid refinement approach}
The idea is to discretize the operation $\nabla\cdot( \vec v .)$ by
taking the adjoint of the discretization for $\nabla_\parallel$ i.e. Eq.~\eqref{eq:paralleldis}.
Remember that the adjoint of a matrix
involves the volume element (including dG weights). This means that after you've transposed the
parallel derivative Eq.~\eqref{eq:paralleldis}, simply bracket the result
by $1/\sqrt{g}$ and $\sqrt{g}$ to get the adjoint.

While the idea of simply transposing the discretization matrices sounds appealing the problem
is that the resulting discretization does not converge.
One idea to solve this problem \cite{Stegmeir2017} is
to bracket the parallel derivative by interpolation ($Q$) and
projection ($P$) matrices:
\begin{align}
    \nabla^c_\parallel &= P\nabla_\parallel^f Q \\
    \nabla^{c\dagger}_\parallel &= P \nabla^{f\dagger}_\parallel Q
    \label{eq:sandwich}
\end{align}
where $f$ and $c$ denote fine and coarse grid respectively.
In this way the projection integrals
\begin{align*}
    \int\dV (\nabla_\parallel f) p_i(x)p_j(y)
    \label{}
\end{align*}
are computed more precisely.
The size of the fine grid should therefore be as large as
possible.
We first notice that one interpolation matrix can be absorbed
in the parallel derivative since this also consists of
interpolation operations.
\begin{align}
    \nabla^c_\parallel &= P\nabla_\parallel^{fc} \\
    \nabla^{c\dagger}_\parallel &= \nabla^{fc\dagger}_\parallel Q
    \label{eq:sandwich}
\end{align}
Note that the matrix-matrix multiplications in Eq.~\eqref{eq:sandwich} can
be precomputed and stored. The memory requirements
in the final computations are
therefore the same  as in the old version. (Not entirely, since
the diagonal $v^\varphi/\Delta \varphi$ matrix does not commute with $Q$ or $P$).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Some Thoughts}
In order to understand what the adjoint operators do let us denote $\Tp$ as the push-forwward operator. Then we have
\begin{align}
    \int f(\vec x) \Tp h(\vec x) \sqrt{g(\vec x)}\d^3x% \\
    %=  \int f(\vec x) h(\Tm \vec x)\sqrt{g(\vec x)}\d^3x \\
    %=  \int f(\Tp \vec x') h(\vec x')\sqrt{g(\Tp \vec x')}J^{-1}( \Tp\vec x') \d^3x' \\
    =  \int \frac{1}{\sqrt{g(\vec x')}}\Tm\left[J^{-1}(\vec x')\sqrt{g(\vec x')}f(\vec x')\right] h(\vec x')\sqrt{g(\vec x')}   \d^3x' \nonumber \\
    \equiv  \int (\Tp)^\dagger\left[f(\vec x)\right] h(\vec x)\sqrt{g(\vec x)}   \d^3x
    \label{}
\end{align}
$J$ is the determinant of the Jacobian $\partial(\vec x')/\partial(\vec x)$ with $\vec x' = \Tm \vec x$.
In the last step we simply replaced the dummy variable $\vec x'$ with $\vec x$ again and identified the relevant terms
as the adjoint operator:
\begin{align}
    (\Tp)^\dagger f(\vec x ) := \frac{1}{\sqrt{g(\vec x)}} \Tm\left[\sqrt{g(\vec x)} J^{-1}(\vec x) f(\vec x) \right]
    \label{}
\end{align}
This means that numerically the adjoint of the push-forward
operator should be a valid discretization of its inverse.
Note that $\sqrt{g}J^{-1}(\vec x) = \sqrt{g'(\Tm \vec x)}$.
With this we can write
\begin{align}
    (\Tp)^\dagger f(\vec x ) := \sqrt{\frac{g'(\vec x)}{g(\vec x)}} \Tm\left[f(\vec x) \right]
    \label{}
\end{align}
Note that $\Tp [fh] = \Tp f \Tp h$ might not
hold on the discrete level. Also the question is how $J$ enters
on the discrete level. We have to multiply $\sqrt{g}$ artificially when we form the adjoint.
Theoretically $J$ could be hidden somehow when we integrate the fieldlines, so the information could be contained in the discrete version? (Maybe in the back-projection?)

If we integrate streamlines of the any vector field $\vec v$, then we have
\begin{align}
    \frac{\d J}{\d \varphi} = J(\vec x ) \nabla\cdot\vec v
    \label{}
\end{align}
along these streamlines~\cite{something}.
If the streamlines are divergence free, we have $J=1$.
A numerical test could be ( if we neglect the volume form in the adjoint)
\begin{align}
    (\Tp)^\dagger \left[J(\vec x)\Tp f(\vec x)\right] - f(\vec x) = 0
    \label{}
\end{align}
The numerical computation of $J$ might a bit tricky at the boundaries.
In a flux-aligned $\zeta, \eta$ it should be feasible but in cylindrical coordinates I don't know how. Maybe we can simply cut the last few cells before the boundary.
Even easier might be
\begin{align}
    \left(\Tp\right)^\dagger J(\vec x ) = 1
    \label{}
\end{align}

Finally, let us assume that $\vec v = \bhat$ is the magnetic field
unit vector. Then we have analytically $\nabla\cdot \vec B= \nabla_\parallel^\dagger B = 0$.
Numerically, this is true if we have
\begin{align}
\left(\Tp\right)^\dagger B^\varphi  = \left(\Tm\right)^\dagger B^\varphi = B^\varphi
\end{align}

\section{Algorithm}
Given are the components $v^i(R,Z)$ for $i\in\{R,Z,\varphi\}$ and a compuational grid (in the following the ``coarse grid``)
\begin{itemize}
  \item generate a fine grid by multiplying the cell numbers of the given coarse grid topologcially (metric and Jacobian of the fine grid are not needed)
  \item integrate the fieldlines for the fine grid:
    \begin{itemize}
      \item evaluate the starting points on the \textbf{coarse} grid in computational space
      \item For a curvilinear grid set up a (higher order, currently 7) grid for the
        interpolation of the vector components $v^i$ and push forward the vector components
        to the curvilinear coordinate system
      \item Integrate the fieldline equations
\begin{subequations}
\begin{align}
\frac{\d \zeta}{\d\varphi} &= \frac{v^\zeta}{v^\varphi}\\
\frac{\d \eta}{\d\varphi} &= \frac{v^\eta}{v^\varphi} \\
\frac{\d s}{\d\varphi} &= \frac{1}{v^\varphi}
\end{align}
\label{eq:fieldlines_converted}
\end{subequations}
    with the given starting points and $s=0$ from $\varphi=0$ until $\varphi = \pm\Delta \varphi$. (Currently we use a Prince-Dormand method with stepsize control for this step).
      \item store the results in $s_{k+1}$ and $s_{k-1}$.
      \item create an interpolation matrix that interpolates from the coarse grid
        to the fine grid
      \item use the interpolation matrix to generate the plus/minus points for the fine grid
    \end{itemize}
  \item create the interpolation matrices that interpolate from the given coarse grid
    to the plus/minus points of the fine grid
  \item create a projection matrix that projects from the fine grid to the coarse grid
  \item compute the matrix-matrix multiplications $P\cdot I^\pm$ as well as their transposes
\end{itemize}

\paragraph{Notes on the MPI implmentation}
It is advantageous to construct $\nabla_\parallel^{fc}$
as s row-distributed matrix with global indices.
This is because a column distributed matrix can be easily (without mpi-communication) multiplied
with a row distributed matrix especially if the indices are global indices.
Each process just multiplies its local matrices.
\begin{align}
M = C\cdot R
\end{align}
This is not true if we started with a column distributed matrix.
The result is then a row distributed matrix with global indices.
From the global indices the gather map/matrix and the local
indices can be constructed.
We note here that we even don't need to construct the gather matrix
for $\nabla_\parallel^{fc}$, only the one for $\nabla_\parallel^c$ is
needed.
\section{Field aligned initialization} \label{sec:parallelc}

An important aspect of our simulations is a judicious initialization of the
fields. We want structures to be field-aligned in the beginning of the simulation with
a possible modulation along the direction of the field line.
If a Gaussian shape is used, we call $\sigma_\parallel$ the extension in parallel
direction and write
\begin{align}
    f_0(R,Z,\varphi) = F(R,Z,\varphi) \exp\left( - \frac{(\varphi-\varphi_0)^2}{2\sigma_\parallel^2}\right),
    \label{eq:parallelInit}
\end{align}
where $F$ is a function that is invariant under the field line transformations
\begin{subequations}
\begin{align}
    \Tp F(\vec z) &= F( \Tp \vec z) \overset{!}{=} F(\vec z) \text{ (pull-back),} \\
    \Tm F(\vec z) &= F( \Tm \vec z) \overset{!}{=} F(\vec z) \text{ (push-forward).}
\end{align}
\label{}
\end{subequations}
We can use these relations to construct aligned structures
by active transformations of some given field.
Our idea is to initialize a two-dimensional field $F(R,Z, \varphi_k)$ in a given plane $k$ and
transform this field to all other planes using the recursive relations
\begin{subequations}
\begin{align}
    F( R, Z, \varphi_{k+1}) = \Tm F( R, Z, \varphi_{k+1}) = F(R^-, Z^-, \varphi_k),\\
    F( R, Z, \varphi_{k-1}) = \Tp F( R, Z, \varphi_{k-1}) = F(R^+, Z^+, \varphi_k),
\end{align}
    \label{eq:recursiveInit}
\end{subequations}
which is the statement that $F$ in the next plane equals the push-forward
and $F$ in the previous plane equals the pull-back of $F$ in the current plane.
Note here that Eq.~\eqref{eq:interpolation} applies for the required interpolation
procedures.

\section{Numerical tests}
The test programs for the parallel derivative are located in
\code{path/to/feltor/inc/geometries}.
To every shared memory test \code{*\_t.cu}
there is a corresponding distributed memory \code{*\_mpit.cu} program.
\code{ds\_t.cu} tests the cylindrical grid
with boundary conditions.
\code{ds\_guenther\_t.cu} tests the cylindrical grid
without boundary conditions i.e. no fieldline leaves the domain.
\code{ds\_curv\_t.cu} tests the implementation
on a flux-aligned grid again with no fieldline leaving the domain.
Finally, \code{ds\_straight\_t.cu} tests the implementation of
completely straight fieldlines and the boundary conditions in the
parallel direction.

The magnetic field in \textsc{Feltor} is given by
\begin{align}
  \vec B = \frac{R_0}{R}( I(\psi_p) \hat e_\varphi + \nabla\psi_p \times\hat e_\varphi)
\end{align}
This gives rise to magnetic field strength and components
\begin{align}
  B = \frac{R_0}{R} \sqrt{ I^2 + \left( \nabla\psi_p \right)^2} \\
  B^R = \frac{R_0}{R}\frac{\partial\psi_p}{\partial Z} \quad
  B^Z = -\frac{R_0}{R}\frac{\partial\psi_p}{\partial R}\quad
  B^\varphi = \frac{R_0I}{R^2} \\
  \nabla \cdot\bhat = -\nabla_\parallel \ln B = -\frac{R_0}{RB^2} [B, \psi_p]
  \label{}
\end{align}
where $[.,.]$ is the Poisson bracket. Note that
in order to compute analytical testfunctions we use
\begin{align}
\nabla_\parallel f &= b^R\partial_R f + b^Z\partial_Z f + b^\varphi \partial_\varphi f\\
\Delta_\parallel f &= \nabla\cdot(\bhat\bhat\cdot \nabla f)
= (\nabla\cdot\bhat) \nabla_\parallel f + (\nabla_\parallel b^j) \partial_j f
+ b^ib^j\partial_i \partial_j f
\label{}
\end{align}
with
\begin{align}
\nabla_\parallel b^R &= (\nabla\cdot \bhat) b^R
+ \frac{\psi_Z (\psi_{RZ} - \psi_Z/R) - \psi_{ZZ}\psi_R}{I^2 + (\nabla\psi)^2} \\
\nabla_\parallel b^Z &= (\nabla\cdot \bhat) b^Z
+ \frac{\psi_R (\psi_{RZ} + \psi_Z/R) - \psi_{RR}\psi_Z}{I^2 + (\nabla\psi)^2} \\
\nabla_\parallel b^\varphi &= (\nabla\cdot\bhat) b^\varphi
+ \frac{\psi_Z(I_R/R - 2I/R^2) - I_Z\psi_R/R}{I^2 + (\nabla\psi)^2}
\label{}
\end{align}
where we used $\psi_R \equiv \partial \psi_p /\partial R$ and
$I_R \equiv \partial I(\psi_p) /\partial R$.

\subsection{Cylindrical grid and boundary conditions}
A simple but non-trivial choice for the poloidal flux is
\begin{align}
  \psi_p = \frac{1}{2} \left( (R-R_0)^2 + Z^2 \right) \equiv \frac{1}{2} r^2
  \label{eq:circular}
\end{align}
We choose $R_0 = 10$ and $I=20$ in order to keep the q-factor for the $r=1$ flux surface at $2$.
We set up a domain
$R\in[R_0-1, R_0+1]$,
$Z\in[-1,1]$ and
$\varphi \in [0,2\pi]$ and choose
\begin{align}
    f(R,Z,\varphi) = (\cos(\pi (R-R_0))+1)( \cos(\pi Z /2)+1)\sin(\varphi)
  \label{}
\end{align}
On the chosen domain $f$ respects both Neumann and Dirichlet boundary conditions
both along the field and perpendicular to the wall.
In Tables~\ref{tab:ds_cylindrical_dirichlet1} and
\ref{tab:ds_cylindrical_dirichlet10} we show the convergence of the solution
for various operators and Dirichlet boundary conditions. Note that the same
tables for Neumann boundary conditions exhibit similar values.
Apparently, the discretization for the divergence does not converge even if we
increase the refinement.

\begin{table*}[ht]
\begin{centering}
\footnotesize
\hspace*{-2cm}
\input{ds_cylindrical_dirichlet1.tex}
\caption{Convergence Table for Dirichlet boundary conditions and $m=1$,
$N_R=N_Z=N$. Centered discretization with boundary conditions along the fieldline. The table for Neumann conditions exhibits similar numbers.}
\label{tab:ds_cylindrical_dirichlet1}
\end{centering}
\end{table*}

\begin{table*}[ht]
\begin{centering}
\footnotesize
\hspace*{-2cm}
\input{ds_cylindrical_dirichlet10.tex}
\caption{Convergence Table for Dirichlet boundary conditions and $m=10$,
$N_R=N_Z=N$. Centered discretization with boundary conditions along the fieldline. The table for Neumann conditions exhibits similar numbers.}
\label{tab:ds_cylindrical_dirichlet10}
\end{centering}
\end{table*}

\subsection{The G\"unther field}
A simple choice for the flux function that makes fieldlines stay
inside a square box is
\begin{align}
  \psi_p = \cos(\pi (R-R_0)/2) \cos(\pi Z /2)
\label{}
\end{align}
Again, we set up a domain
$R\in[R_0-1, R_0+1]$,
$Z\in[-1,1]$ and
$\varphi \in [0,2\pi]$ and choose
\begin{align}
  f_1(R,Z,\varphi) = -\psi_p(R,Z)\cos(\varphi)\\
  f_2(R,Z,\varphi) = -\psi_p(R,Z)\cos(\varphi) + (R-R_0)^2/4 + Z(R-R_0)/4
  \label{}
\end{align}
\begin{table*}[ht]
\begin{centering}
\footnotesize
\hspace*{-2cm}
\input{ds_guenther10.tex}
\caption{Convergence Table for $m=10$ and $N_R=N_Z=N$ and the G\"unther field. Centered discretizations, no boundary condition.
}
\label{tab:ds_guenther10}
\end{centering}
\end{table*}
\subsection{Curvilinear grid}
Here we choose the general Solov'ev equilibrium for
$\psi_p$ and choose to use $f_1$ or $f_2$ again.
We use the simple orthogonal flux to construct
grids on the ring bounded by $\psi_p=-20$ and $\psi_p=-4$.
\begin{table*}[ht]
\begin{centering}
\footnotesize
\hspace*{-2cm}
\input{ds_curv1000.tex}
\caption{Convergence Table for $m=1000$, $N_R = 2$, $N_Z=N$ on a curvilinear grid. Centered discretizations, no boundary condition.
}
\label{tab:ds_curv1000}
\end{centering}
\end{table*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Performance considerations}
The performance of the main matrix-vector multiplication depends, as do all our
routines, on the number of memory loads and stores per vector element.
A dG interpolation matrix in two dimensions has $n^2$ points per line since
it uses all $n^2$ points in one dG-cell to compute the result.
A matrix-vector multiplication with the interpolation matrix thus needs $n^2+3$
vector loads and stores, with the $3$ coming from loading the input vector and
loading and storing the output vector.
However, since we use the refinement-projection aproach to our discretization
the number of points per line of the interpolation matrix increases to typically
$4n^2$ since now also neighboring cells are used for the computation.

For the discretization of $\nabla_\parallel$ this in total means that the number
of memory operations per element is
\begin{align}
m &= 2\cdot(4n^2+3)+5\quad \text{ (refined)} \\
m &= 2\cdot(n^2+3)+5\quad \text{ (unrefined)}
\end{align}
with $n$ the number of polynomial coefficients.
This is comparable to one iteration of a CG method in three dimensions.

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

\end{document}


\section{A Distance Sensitive Encoding Protocol}

\subsection{Idea}

In the previous section we discussed various LDP Protocols that function extremely well for numerical values (histogram type), with accuracy that is completely acceptable. However, when the number of users is limited, (eg under 1000), we made the observation that the accuracy error is extremely large. This is mainly due to the fact that the probability of an item to be chosen is independent of the distance between the true and the selected value.

Thus, for the needs of this Thesis, a new L.D.P. protocol was constructed. The idea proposed is to \emph{have the probability of choosing an element of the domain to depend on the distance from the true value}. This could prove very helpful for histogram values, but does not make any sense for categorical values. From now on, we are going to focus on histogram values.

Based on our idea, the probabilities' distribution, in comparison with the distribution of the D.E. protocol, will look like the one in the following figures.

\begin{figure}[!htb]\centering
    \includegraphics[width=0.5\textwidth]{images/D.E. Idea.png}
    \caption{D.E. protocol's Probabilistic Distribution}
\end{figure}

\begin{figure}[!htb]\centering
    \includegraphics[width=0.5\textwidth]{images/Our Idea.png}
    \caption{Distance Sensitive protocol's Probabilistic Distribution}
\end{figure}

Thus, there is an area around the true value that has high probability to be selected. The width of this area(from now on $\theta$) is defined by the epsilon setting that the user wants to use. The idea for having a specific area and not decreasing our probabilities as low as it goes when we diverge from the true value, is based on our need to be able to serve low epsilon values, as the first would be a big no for when the users want to use a high privacy setting.

\subsection{Mathematical Background}

In order to achieve our goal, when in the selected area, we should include in the denominator the quantity
$$
|x - i|
$$
where x is the true value, and the i the false one that we are looking at in order to report it, in order for the probability to depend on the distance of the reported values.

When we are out of this area, the denominator will have a constant value, proportional to the boundaries of the selected area.

\emph{Example}: Let's suppose that we have a domain size of 100, and our $\theta$ value is 4. All the probabilities outside the area, should be in reverse proportion of $\theta$, thus 4.

Like all probabilistic algorithms though, the sum of the probabilities for all the items in the domain size, must be 1. Thus if we chose the probability to be in the shape of $\frac{a}{|x-i|}$, the partial sum of the series will not give as simple results.

It is known that the series in the form of $a_n = \frac{1}{n}$ does not converge, and its partial sums can only be computed using a complex approximation formula. These characteristics make this type of series very hard to use, and so we have to think of a more handy type.

A type of series that is known to have easy to compute partial sums, are the telescopic series, such as $b_n = \frac{1}{n (n+1)}$. It is known that
$$
\sum_{n = 1}^{n = k} b_n = 1 - \frac{1}{k + 1}
$$
something that will prove extremely useful moving forward.

So, taking into consideration the quantity $|x - i|$ and the telescopic series $b_n$, we conclude that the probability of each non-true element in our selected area will be of the shape:

\begin{align}
    \mathbf{q = \frac{a}{|x-i|(|x-i| + 1)}}
\end{align}
and outside of that area:

\begin{align}
    \mathbf{s = \frac{a}{\theta \cdot (\theta + 1)}}
\end{align}

The probability $p$ of selecting the true value will have to meet specific criteria that we are going to define later on.

\subsection{Building the Protocol}
Ww are now going to find what the alpha parameter will be, as it is not constant, but clearly it depends on the domain size and the probability $p$. In order to find it, we must keep in mind that all the probabilities of selecting an item from our domain, must add up to $1$.

In order to find out the $\alpha$ value, we must solve the following equation:

\begin{align*}
    p + \sum_{i = x - \theta}^{i = x + \theta} q + \sum_{i = 1}^{i = x - \theta -1} s + \sum_{i = x + \theta + 1}^{i = d} s = 1
\end{align*}

At this point, we must note that $\alpha$ although not a constant, can be held out of the sums, because it is obviously independent from the $i$ variable, that is the variable parsing through the domain in order to retrieve the false elements' probabilities. Thus, we have:

\begin{align*}
        p + \sum_{i = x - \theta}^{i = x + \theta} q + \sum_{i = 1}^{i = x - \theta -1} s + \sum_{i = x + \theta + 1}^{i = d} s = 1 \Longleftrightarrow \\
      \sum_{i = x - \theta}^{i = x + \theta} \frac{a}{|x-i|(|x-i| + 1)} + \sum_{i = 1}^{i = x - \theta -1} \frac{a}{\theta(\theta+1)} + \sum_{i = x + \theta + 1}^{i = d} \frac{a}{\theta(\theta+1)} = 1 - p \Longleftrightarrow \\ \dots \Longleftrightarrow \\
    a = \frac{\theta(\theta + 1) (1 - p)}{2\theta^2 - 2\theta + d - 1}
\end{align*}

The proof for the mathematical equations leading to the extraction of the alpha value, can be found in the First Appendix of the Thesis.

\subsection{Epsilon Requirements}

The epsilon value is the most essential in these protocols, as it determines the privacy level that the protocol yields.

Recalling the definition of LDP, we must follow the following rule:

\begin{center}
An algorithm $A$ satisfies \espilon-LDP \textit{iff} for any input $v_1$ and $v_2$, we have
\begin{align*}
    \forall y \in Range(A): \frac{Pr[A(v_1) = y]}{Pr[A(v_2) = y]} \leq e^{\epsilon}
\end{align*}

\end{center}

Thus, in order to determine the epsilon value for our algorithm, it must satisfy even the worst case of this equation. The fraction gets bigger, if we put on the biggest probability on the numerator, and the smallest probability of all in the denominator.

The numerator must have the probability $p$, and the denominator $s$, the probability of all of the elements outside of the $\theta$ area.

We want for $p$ to be the biggest probability among all, but not extremely high, in order to be able to restrict the growth of epsilon. Hence, we are going to set it as double of the probabilities of its exact neighbours. The 2 neighbours have $|i-x| = 1$, thus $q = \frac{a}{2}$, so we are going to set $\mathbf{p = 2 \cdot \frac{a}{2} = a}$, where $a$ is the quantity defined above, depending on the domain size, and the $\theta$ value.

Now, if we set $p = a$, then the $a$ equation changes, and now our aplha parameter only depends on the domain size and the $\theta$ selected. So, we proceed as following:
\begin{align*}
    a = \frac{\theta(\theta + 1) (1 - p)}{2\theta^2 - 2\theta + d - 1} \Longleftrightarrow{(p = a)}\\
    a = \frac{\theta(\theta + 1) (1 - a)}{2\theta^2 - 2\theta + d - 1} \Longleftrightarrow \\
    a (2\theta^2 - 2\theta + d - 1) = \theta(\theta + 1) - (\theta^2 + \theta)a \Longleftrightarrow\\
    a (3\theta^2 - \theta + d - 1) = \theta(\theta + 1) \Longleftrightarrow \\
    \mathbf{a = \frac{\theta(\theta + 1)}{3\theta^2 - \theta + d - 1}}
\end{align*}

Obviously, we observe the constraint that $\theta > 0$, and this is a special case of our protocol, that can be represented by the direct encoding protocol.

We now have:

\begin{align*}
    \frac{Pr[A(v_1) = y]}{Pr[A(v_2) = y]} = e^{\espilon} \Longleftrightarrow
    q e^{\epsilon} = p \Longleftrightarrow \\
    \frac{a}{\theta(\theta + 1)} \cdot e^{\epsilon} =  a \Longleftrightarrow \\
    \theta(\theta + 1) = e^{\epsilon} \Longrightarrow \\
    \theta^2 + \theta - e^\epsilon = 0
\end{align*}

If we solve the quadratic equation, and reject its one (illegal) solution, we get that:

\begin{align}
    \theta = \lfloor \frac{\sqrt{4e^{\epsilon} + 1} - 1}{2}\rfloor
\end{align}

So, to conclude, in order for the protocol to function, the user must provide just the epsilon setting, from which the $\theta$ constant is computed, and so the probabilities for each item of the domain will be selected.

\subsection{Protocol Definition}

We are now ready to define our protocol, by determining the 3 basic operations for an LDP protocol, the \emph{encoding}, the \emph{perturbation} and the \emph{aggregation methods}.

We are going to use the following symbols:
\begin{itemize}
    \item $D$: The protocol's domain. In this set, we have each $i$ for $1 \leq i \leq |D|$ as each item in the domain $D$
    \item $a$: The quantity that we computed in the previous section
    \item $\theta$: The constant used in the previous section, which denotes the area around the true value that the probabilities will be higher than others.

\end{itemize}

\textbf{Encoding:} The encoding procedure is trivial. Just like the Wang paper, we are just going to set:
\begin{align*}
    Encode(v) = v
\end{align*}

for each value $v$ of the domain. The values are going to be randomized during the perturbation step.

\textbf{Perturbation:} Given the previous section, the randomization during the perturbation step is define as following:

\begin{equation*}
    Pr[Perturb(x) = i] =
	\begin{cases}
		p = a & \mbox{if } i = x \\
		q = \frac{a}{|c|(|c| + 1)}  &  c = \min{(\theta, |i-x|)}  \mbox{, otherwise}	\end{cases}
\end{equation*}

where $i$ is the value selected each time, and $x$ our initial selection.

\textbf{Aggregation:} The aggregation step was the most tricky during the building of the protocol. A similar approach to the aggregation of pure protocols was chosen, but with a few changes. After several different tries, the optimal aggregation found, was the following: the protocol supports only the reported values corresponding to the true one, thus $Support(v) = v$. However, the $p^*$ quantity is the sum of all the probabilities inside the area:

\begin{align*}
    p^* = \sum_{x\in (-\theta, \theta)} p(x)
\end{align*}

Finally, the $q*$ quantity is the probability of choosing an element from outside the θ area, thus equal to $s$. Hence, the estimation generated for a value $v$ of the possible answers in the domain is defined as following:

\begin{align*}
    \text{Estimation} = \frac{\sum_{j} 1_{support(v^j)}(i) - nq^*}{p^* - q^*}
\end{align*}

\subsection{Extreme Cases}

The downside of a complicated protocol, are of course some extreme cases for the $x, \theta \text{ and } i$ values, all of which we are going to examine in this chapter. The definition of the protocol is going to be altered, and the constraints increased, in order to support those extreme cases.

\textbf{Extreme theta cases:} Of course, we have the constraint that $0 < \theta \leq d$, but what happens when its value is equal to one of the bounds?
\begin{itemize}
    \item When $\theta \leq 0$, our protocol can not function, as this assignment will result in $a = 0$, something that is prohibited, because the probabilities will not sum to 1. In order to ensure that $\theta$ is at least 1, the user must provide at least an $\epsilon = ln(2)$.

    \item When $\theta = 1$, we can see that the third case in the perturbation step does not exist, thus we have only the first 2 cases. There, $p = a = \frac{2}{d+1}$, and for every other $i$, $q = \frac{a}{2}$, something that is similar with the Direct Encoding protocol.

    \item When $\theta = d$, which realistically can only happen when d is extremely small, then our protocol functions as designed, and has its best behavior. However, if the selection of epsilon results in such big a theta, then the user does not have extreme privacy demands.
\end{itemize}

\textbf{Extreme x values:} Even when the epsilon value and the domain size are normal, in some cases we might face a certain difficulty: if $x - \theta < 0$ or $x + \theta > d$, some of the items in our area are actually outside of our domain boundaries. This results in the sum of the items in the probabilistic distribution to be below 1, something not acceptable.

In order to fix it, we are going to "transfer" those probabilities inside the boundaries of our domain, while not messing with the highest probability, as this would result in problems with the definition of D.P., an thus the value of theta.

The idea is to increase the other selections' probabilities by a bit, in order to fill the gap created, while leaving the maximum as initially created. We are going to boost all of the domain's items, by a portion of $\frac{m}{d - 1}$ (as we are altering $d-1$ elements), where $m$ is the sum of the probabilities of the items outside the bounds of our domain.

However, we are not interested in transferring the whole $Pr[Perturb(x) = i]$, but only its difference from the item with the lowest probability, which is $s =  \frac{a}{\theta(\theta+1)}$. Thus, the $m$ values are defined as following:
\begin{align*}
    m = \sum_{i < 0 \bigcup |i-x|<\theta} Pr[Perturb(x) = i] - \frac{a}{\theta(\theta+1)}
\end{align*}

for the case of $x - \theta < 0$, and as

\begin{align*}
    m = \sum_{i \geq d \bigcup |i-x|<\theta} Pr[Perturb(x) = i] - \frac{a}{\theta(\theta+1)}
\end{align*}

for the second one.
\\\bigskip
Now, the probabilistic distribution can be altered as following:

\begin{equation*}
    Pr[Perturb_{DS}(x) = i] =
	\begin{cases}
		p = a & \mbox{if } i = x \\
		q = \frac{a}{|c|(|c| + 1)} + \frac{m}{d - 1}  &  c = \min{(\theta, |i-x|)}  \mbox{, otherwise}	\end{cases}
\end{equation*}

The definition of D.P. is not altered, because again in the best case we have a probability of $p = a$, and in the worst case $s = \frac{a}{\theta(\theta+1)}$.

\subsection{Implementation}

The most difficult part of the implementation of our protocol consists of creating the probabilistic distribution for each element of the domain, depending on the true value. This can prove to be costly, if we have a large domain or if we are in the case of the extreme x values.

However, we do not need to compute every single probability, as it is clear from the definition that they are independent from the true value: they only depend on $a$ (and on the domain size in case of an extreme x value). The quantity $|i - x|$ can only take values in the range of $[1,\theta]$, thus constant for every possible true value. Moreover, for the domain values outside of the area, the probability is fixed and equal to $\frac{a}{\theta(\theta + 1)}$. Hence, the probabilities can be computed in advance, either by each user, or given to the protocol by the aggregator.

The protocol has been implemented using Python, and can be found in the GitHub repository of this Thesis. Moving forward, we are going to use this implementation in order to conduct some testings to ensure the protocol's functionality.

\subsection{Experiments}
First up, we are going to perform the epsilon measurements that we did for the other protocols, this time excluding the Random Matrix approach, and including the D.S. protocol. The results, when running with the Kantorovich metric, are the following:

\begin{figure}[!htb]\centering
    \includegraphics[width=1\textwidth]{images/epsilon_our_kant.png}
    \caption{Epsilon measurements for D.S. protocol compared by Kantorovich Distance}
\end{figure}

The first observation is the \emph{strange form of the curve of our protocol}. This can be easily explained: the theta value used to determine the area around the true value, is depended on epsilon, but has a floor function applied to it. Thus, for a specific range of ε, the protocol produces the same results.

An other observation is that\emph{ our protocol lacks efficiency for low values of epsilon}, that is natural, since small θ values do not help our idea at all. However, when epsilon gets higher than 1.5 (and thus theta rises above 2), the results are more than satisfying: \emph{our protocol has the best behaviour for epsilon in the range of $\mathbf{(2, 2.5)}$.}

The real test though, is how our protocol behaves for an increasing number of users: we must check if it produces better accuracy error than the competitors. This is our next testing, where we are going to set $\epsilon = \ln(20)$, in order for the conditions to be favorable for each one of the protocols. The results are shown in the \textbf{Figure 4.10}.

\begin{figure}[!htb]\centering
    \includegraphics[width=1\textwidth]{images/users_our_kant.png}
    \caption{Increasing users measurements for D.S. protocol compared by Kantorovich Distance}
\end{figure}

For the specific epsilon setting, \emph{our protocol produces extremely good accuracy error for a small number of users.}, beating by a lot the U.E. protocol. The comparisons have been made using the Kantorovich metric, the most characteristic of them all, as it takes into account the distance between the real answers and the projections, exactly what our protocol is designed to do. However, we are also going to perform the same testings using the Manhattan metric. The results are shown in the \textbf{Figure 4.11}.

\begin{figure}[!htb]\centering
    \includegraphics[width=1\textwidth]{images/users_our_l1.png}
    \caption{Increasing users measurements for D.S. protocol compared by Manhattan Distance}
\end{figure}

The results of the Manhattan-driven tests are similar to the ones made with the Kant. metric. However, we observe that \emph{when the number of users rises, our protocol has worse behaviour in comparison to the other pure ones}. This happens mainly because of the other protocols, that by the law of big numbers, have good accuracy because of the higher probability of choosing the true answer. On the other hand, in our protocol, this probability is reduced and shared with the other elements in the area covered by theta.

\subsection{Conclusions}
In general, \emph{the D.S. protocol succeeds when the number of the participants in a survey is extremely low}, and functions similarly with the other protocols for an increasing number of users. The downside is that it does not always takes full advantage of the epsilon setting, as explained in a previous section. However, the results are more than satisfying. Hence, \emph{this is a fully functioning protocol that can be used for the application of L.D.P., especially in a situation when few people take part in the survey.} The protocol will be further tested in more extreme cases, but this is beyond the scope of this Thesis.


\section{MST vs. Shortest Path}

%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Sharing edges}
  \begin{exampleblock}{Sharing edges (Problem 6.6)}
	\begin{itemize}
	  \item $G = (V, E), w(e) > 0$
	  \item Given $s$: all sssp trees from $s$ must share some edge with some MST of $G$
	\end{itemize}
  \end{exampleblock}

  \vspace{0.60cm}
  \centerline{a lightest edge leaving $s$}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{SP \emph{vs.} MST}
  \begin{exampleblock}{SP \emph{vs.} MST (Problem 6.10--6)}
	\xmark\; The shortest path between two nodes is necessarily part of some MST.
  \end{exampleblock}

  \vspace{0.50cm}
  \begin{exampleblock}{SPT \emph{vs.} MST (Problem 6.17)}
	\xmark\; The shortest-path tree computed by Dijkstra's algorithm is necessarily an MST.
  \end{exampleblock}

  \fignocaption{width = 0.20\textwidth}{figs/mst-sssp.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Edge weights}
  \begin{exampleblock}{Edge weights (Problem 6.19)}
	MST \emph{vs.} SPT (from $s$):
	\[
	  w(e) \ge 0, w'(e) = w(e) + 1
	\]
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%


\section*{\hypertarget{_some_equations}{Some Equations}}

\subsection*{\hypertarget{_number_theory}{Number Theory}}

The equation
$a^2 + b^2 = c^2$ has infinitely many
non-proportional integer solutions.
The integer solutions of the equation
\[
  a^3 + b^3 = c^3
\]
are trivial: at least one entry is
zero and the others are "obvious"

\subsection*{\hypertarget{_calculus}{Calculus}}

A definite integral:
\[
  \int_0^1 x^n dx = \frac{1}{n}
\]

The fundamental theorem of calculus:
\[
   \frac{d}{dx} \int_a^x f(t) dt = f(x)
\]

\subsection*{\hypertarget{_linear_algebra}{Linear algebra}}

A matrix:
\[
M = \left[
  \begin{array}{ c c }
	 1 & 2 \\
	 3 & 4
  \end{array} \right]
\]


\chapter{Localization}
\label{chp:local}

The localization problem, also known as position estimation or position tracking, is the most basic perceptual problem in robotics.
This is because some kind of knowledge about the location of the robot or other objects around the robot are required for almost all robotic tasks.
The core principle of robot localization can be seen as a problem of coordinate transformation.
The robot will always exist in some kind of "world map" with a global coordinate system, that is independent from the robot own pose - position and orientation.
Being able to express an object of interest from the world map, in the robots own coordinate frame is essential for robot navigation.
The robot pose is therefore needed to be able to do a correct coordinate transformation.
Herein lies two problems.
First the robot can't measure a perfect pose directly, but instead have to extract that information from noisy sensor data, and second a single measurement often isn't sufficient to do a pose estimate, and the robot instead have to integrate data over time to determined the pose.

This leads to the sense move cycle seen on figure \ref{fig:sense_move_cycle}.
Here the location og the robot is modelled as a probability distribution, because both sensing and moving is noisy actions that cannot guarantee a perfect result.
Each time the robot do a sensor measurement information is gained in the probability distribution of the robot location, and each time the robot moves information is lost in the probability distribution of the robot location.

\myFigure{Theory/Localization/sense_move_cycle}{This figure show the basic cycle of localization, with the two steps sense and move. When sensing information is gained about the robots location, and when moving information is lost about the robots location.}{fig:sense_move_cycle}{0.9}

\section{Markov localization}

The Markov localization algorithm is a multimodal probabilistic model with a discrete state space.
This basically means a multi dimensional grid, where each tile have a probability of the robot being in that specific tile.
Figure \ref{fig:markov_localization_code} show the basic steps in the Markov localization algorithm.
Though a cycle of sensing and measuring the probability of the robot being in each tiles is updated in each iteration og the algorithm.

\myFigure{Theory/Localization/markov_localization_code}{Basic implementation of Markov localization.}{fig:markov_localization_code}{0.7}

Line 4 in figure \ref{fig:markov_localization_code} is the sensing step, which is based on Bayes' theorem.
The algorithm is iterating though all tiles in the state space, and calculating the probability of being in that tile given a sensor measurement.
Bayes' rule state that the posterior state estimate is the product of the prier state estimate times the measurement probability given the prier state estimate, normalized by the total measurement probability of the entire state space.

\begin{equation}
\label{eq:Bayes_theorem}
P(X_{i}^{t} \mid Z^{t}) = \frac{P(Z^{t} \mid X_{i}^{t}) * P(X_{i}^{t})}{\sum_{j} P(Z^{t} \mid X_{j}^{t}) * P(X_{j}^{t})}
\end{equation}

Line 3 in figure \ref{fig:markov_localization_code} is the movement step, which is based on the theorem of total probability seen in equation \ref{eq:total_probability}.
The algorithm is iterating though all tiles in the state space, and calculating the total sum of all possible way to end up in that tile based on the probability of the motion.

\begin{equation}
\label{eq:total_probability}
P(X_{i}^{t}) = \sum_{j} P(X_{j}^{t-1}) * P(X_{i}^{t} \mid X_{j}^{t-1})
\end{equation}

\pagebreak

Figure \ref{fig:markov_localization_example} show the Markov localization algorithm in action when preforming measurements and movements.
It can be seen that the robot gets an increasing better position estimate after each measurement.

\myFigure{Theory/Localization/markov_localization_example}{Example of Markov localization algorithm. Each picture depicts the robots position in a hallway, together with a probability distribution of where the robot believe it is and the probability of the measurement. a) Starts with a uniform distribution. b) and d) Acquire a more precise probability distribution by making a measurement. C) and e) Movement result in less accurate probability distribution.}{fig:markov_localization_example}{0.7}

In general Markov localization is a good algorithm for localization, and it gives the ability of model multi-modal probabilities, but as an expense in memory because the probability of every tile in the state space have to be calculated.
This means that Markov localization is not a good choice in cases with a many dimensional state space.


% arara: pdflatex
% arara: bibtex
% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\usepackage{graphicx}  % for including graphics
\usepackage{amsmath}   % math symbols like operator notation
\usepackage{siunitx}     % properly format quantities with units
\usepackage{fancyhdr}  % control headers
\usepackage{physics}
\usepackage{fullpage}

\title{Numerical Evaluation of the Arrhenius Integral}
\author{C.D. Clark III}

\begin{document}
\maketitle

The Arrhenius
Integral model for thermal damage requires the evaluation of the integral\cite{WELCH--2011--optical-thermalresponseoflaser-irradiatedtissue}
\begin{equation}
  \label{eqn:arr}
  \Omega\qty(\tau) = \int\limits_{0}^{\tau} A e^{\frac{-Ea}{RT\qty(t)}} dt.
\end{equation}
If the temperature, $T\qty(t)$, is predicted using a numerical simulation, i.e. a finite-difference or finite-element model, then the temperature is calculated at a discrete set of times, $(t_0, t_1,
\ldots)$. This limits the methods that can be used to evaluate Eq. \ref{eqn:arr} numerically to those that work on predefined nodes (Gaussian quadrature, for example, cannot be used).

The usual methods for numerically evaluating an integral with pre-defined nodes would be Reimann sum, Trapezoid Rule,
and Simpson's Rule. These methods are simple to implement, but approximate the integrand as piece-wise, linear, and
parabolic, respectivly.

\section{Recasting the integral}
If we assume that the temperature between two times, $t_0$ and $t_1$ can be written as a linear function of time,
\begin{equation}
  T\qty(t) = mt + T_0
\end{equation}
and inserting into this into Eq. \ref{eqn:arr} gives,
\begin{equation}
  \Omega = \int\limits_{t_0}^{t_1} A e^{\frac{-Ea}{R\left(mt + T_0\right)}} dt.
\end{equation}
Changing the integration variable to $T$,
\begin{align}
  dT &= mdt \\
  \Omega &= \int\limits_{T_0}^{T_1} \frac{A}{m}e^{\frac{-Ea}{RT}} dT.
\end{align}
Where $T_0$ and $T_1$ are the temperature at $t_0$ and $t_1$. Next, we substitute $u = \frac{E_a}{RT}$ and change the integration variable again to $u$.
\begin{align}
  u &= \frac{E_a}{R} \frac{1}{T}  \\
  du &= -\frac{E_a}{R} \frac{1}{T^{2}} dT  \\
  dT &= -\frac{E_a}{R} \frac{1}{u^{2}} du  \\
  \Omega &= -\frac{A}{m}\frac{E_a}{R} \int\limits_{E_a / R T_0}^{E_a / R T_1} \frac{e^{-u}}{u^2} du  \\
  \label{eqn:arr_2}
         &=  \frac{A}{m}\frac{E_a}{R} \int\limits_{E_a / R T_1}^{E_a / R T_0} \frac{e^{-u}}{u^2} du.
\end{align}
The integrand can be evaluated ``analytically'' using the \emph{exponential integral}, $E_n$, which is defined as\cite{1970--handbookofmathematicalfunctionswithformulas}
\begin{equation}
  E_n\qty(x) = \int\limits_{1}^{\infty} \frac{e^{-xt}}{t^n} dt
\end{equation}
The variable substitution $y = xt$ gives
\begin{align}
  dy &= xdt \\
  dt &= \frac{1}{x}dy \\
  E_n\qty(x) &= x\int\limits_{x}^{\infty} \frac{e^{-y}}{y^n} dy
\end{align}
Comparing this to Eq. \ref{eqn:arr_2}, it is simple to show that
\begin{align}
  \Omega &= \frac{A}{m}\frac{E_a}{R} \left[\frac{E_2\qty{E_a/RT_1}}{E_a/RT_1} - \frac{E_2\qty{E_a/RT_0}}{E_a/RT_0}\right] \\
         &= \frac{A}{m}              \left[T_1 E_2\qty{E_a/RT_1}              - T_0 E_2\qty{E_a/RT_0}             \right]
\end{align}
Finally, we note that the slope, $m$, can be determined from $T_0$ and $T_1$ if $t_1$ and $t_0$ are known (rise over run),
\begin{align}
  \label{eq:quadrature}
  \Omega &= A \frac{t_1 - t_0}{T_1 - T_0}              \left[T_1 E_2\qty{E_a/RT_1}              - T_0 E_2\qty{E_a/RT_0}             \right]
\end{align}
This gives the accumulated damage over a time $t_1 - t_0$, when the temperature rise is linear. If the actual
temperature rise is not linear, then we can discretize the profile into small segments, such that
the temperature is approximatly linear over the segment.

It is reasonalble to think that this approximation
(treating the temperature as linear over some time interval) is better than assuming that the damage rate
is linear over the same time. Figure \ref{fig:arrhenius_rate} shows the Arrhenius rate as a function of time for an example
thermal profile. The figure shows that the Arrhenius rate is concave on both sides its peak, which means that the error accumlated by the
trapezoid rule will have the same sign on each side. The trapezoid rule will \emph{over}-predict the accumulated damage. On the otherhand,
if the thermal profile is approimated as linear segments, the accumulated damage will be \emph{under}-predicted while the temperature increases, and
then be \emph{over}-predicted when the temperature begins to fall.

\section{Numerical Implementation}

Equation \ref{eq:quadrature} provides an analytic expession to evaluate the Arrhenius integral over some time period where the temperature rise is linear. To numerically integrate
the Arrhenius integral for a full thermal profile, we simply break the integral up into small time intervals, use Equation \ref{eq:quadrature} to evaluate the integral over each
time inteval, and then them all up. However, we must be careful because the there is a $T_1 - T_0$ in the denominator. If the temperature is constant, we will divide by zero.

If $T_0 = T_1 = T$, then Equation \ref{eq:quadrature} gives
\begin{align}
  \Omega = A(t_1 - t_0)\qty( \frac{ T E_2\qty{E_a/RT} - T E_2\qty{E_a/RT} } { T - T} ) = A(t_1 - t_0) \frac{0}{0}.
\end{align}
To evaluate, we need to apply L'H\^{o}pital's rule for $T_1 - T_0 \rightarrow 0$. Apparently, this limit will give $A(t_1 - t_0) e^{-E_a/RT}$, since this is the integral for a constant temperature.

A numerical implementation will need to check for the case that $T_0 = T_1$ and use the constant temperature quadrature. If $T_0$ and $T_1$ are stored as floating point numbers, then
we will probably need to check that they are ``close'', rather than ``equal''. Note that, for a linear temperature rise from $T_0$ and $T_1$, evaluating Equation \ref{eq:quadrature} will
to give a numerical value between $Ae^{-E_a/RT_0}(t_1 - t_0)$ and $Ae^{-E_a/RT_1}(t_1 - t_0)$. We can therefore write a limit on the maximum error in incurred by any numerical approximation to integral over duration $t_1 - t_0$.
\begin{align}
  \epsilon \le \frac{\abs{ Ae^{-E_a/RT_1}(t_1 - t_0) - Ae^{-E_a/RT_0}(t_1 - t_0)} }{Ae^{-E_a/RT_0}(t_1 - t_0)} = \abs{e^{-\frac{E_a}{R} \qty(\frac{1}{T_1} - \frac{1}{T_0})} - 1}
\end{align}
This gives a metric for deciding if $T_1$ and $T_0$ are ``close enough''. Given a tolerance, or maximum allowed error, we
\begin{align}
  \frac{R}{E_a} \ln \qty( \epsilon + 1 ) \le \abs{ \frac{1}{T_1} - \frac{1}{T_0} }
\end{align}
For small $\epsilon$, $\ln \qty(\epsilon + 1) \approx \epsilon$,
\begin{align}
 \frac{1}{T_1} - \frac{1}{T_0} \ge \frac{R}{E_a}\epsilon
\end{align}

\begin{figure}
\includegraphics{./arrhenius_rate.png}
\caption{\label{fig:arrhenius_rate} The Arrhenius rate, $Ae^{-E_a/RT(t)}$, plotted for an example thermal profile. }
\end{figure}

\bibliography{references_database}
\bibliographystyle{plain}

\end{document}