\documentclass[Thesis.tex]{subfiles}
\begin{document}
\chapter{Machine Learning}
\label{chp:machine-learning}

\glsresetall

\Gls{ml} is a field of study concerned with building
models\footnote{The term \emph{model} is used in a very general sense, and
refers to anything that takes information as input and produces a corresponding
output.} that can perform a task without the need for explicit instructions on
how to do so. The key word is \emph{learning}; we want to enable the model to
discover the best way to solve the task at hand within the constraints that we
have imposed on it.

A simple example of an \gls{ml} algorithm is linear regression, where we aim at
finding a linear function that best fits some observed data, with respect to some
metric of what constitutes a \emph{good fit}. Other examples include
dimensionality reduction, image classification, algorithmic trading and playing
chess. All of these examples \emph{could} be implemented in terms of a
large set of if-this-then-that rules, covering the explicit response to every
conceivable input. The problem with that is that we often have little to no idea
of how exactly to determine what to do for each case, not to mention the
potential infinity of rules we would need to cover all cases.

The idea of \gls{ml} is to instead describe a parameterized model that
maps inputs to outputs, and a metric to quantitatively describe how good or bad
the outputs are. Then we ask the model to find the set of parameters that will
maximize the goodness (or minimize badness) of the metric. In short, \gls{ml}
is saying \emph{what} you want done, rather than saying \emph{how} you
want it done.

This chapter is dedicated to presenting the relevant bits of \gls{ml} that will be
relevant to the work in this thesis. As the field is vast, only a very minimal
selection of topics will be covered, and the reader is encouraged to dive deeper
into topics where interest is sparked. We start with an overview of
the general process, exemplified by linear regression for simplicity. From there
we shall introduce neural networks, along with a more in depth discussion of
optimization strategies.

\section{General Procedure}

For our purposes, we will define the steps involved in developing a machine
learning model as follows:

\begin{enumerate}
  \item Define a model, $\hat f_{\hat\valpha}(\vx)$ dependent on some parameters $\hat\valpha$
  \item Define a cost function $C(\hat f_{\hat\valpha})$ - a metric of how far away from the
      ideal model $\hat f_{\hat\valpha}$ is
  \item Minimize $C(\hat f_{\hat\valpha})$ with respect to $\valpha$
\end{enumerate}

It is common to divide \gls{ml} into two main sub-domains: supervised and unsupervised
learning. Supervised learning is characterized by us having access to a data set
where both inputs \emph{and} desired outputs are specified. This is possibly the
most common case, or at least the case most people \emph{want} to work with.
Examples are many, and include most regression and classification tasks.

Unsupervised learning is (naturally) the other case, where we do not have
information about any output responses. In these cases we don't focus on
learning the output (as we have no idea of what we want that to be), but rather
on what we can learn about the inputs themselves. This can include
dimensionality reduction, clustering analysis and more.

Often we need to introduce another sub-domain for the cases that fall in
between; Reinforcement Learning. This is the domain of problems where we do not
have explicit knowledge of what outputs should be, but where we can still infer
something about whether or not an output was good. An example of this is the
problem of playing chess. While we can't label all board states with a
corresponding ``correct move'', we can say that moves that lead to
checkmate are probably good. Reinforcement learning tries to adapt a model so as
to maximize/minimize the reward/punishment that follows as a consequence of its
actions.

\Gls{vmc} is a type of reinforcement learning. This is because we
do not have information about exactly what the value of the wave function should
be for every configuration, but we still have a way to evaluate the goodness
of the wave function by calculating the energy it predicts. We'll go more in
depth into how all this connects to \gls{vmc} in~\cref{sec:reinf-learning-for-vmc}.

\section{Supervised Learning}

Even though our particular interest in machine learning is for its use in \gls{vmc}, a
lot of the ideas and techniques we need come from the supervised learning
domain. Because of that, we will quickly introduce all the relevant components in the
following sections. For simplicity and clarity, we will do this through the
simplest example we have; linear regression.

In general for a supervised learning task, we have the following ingredients:

\begin{itemize}
\item A data set, $\mathcal{D} = \{\vx_i, \vy_i\}_{i=1}^n$, of $n$ inputs $\vx_i$
  and corresponding expected outputs $\vy_i$
\item A proposed model $\hat\vy = \hat f_{\hat\valpha}(\vx)$ that we want to
    fit to the data
\item A cost function $\mathcal{C}(\hat f_{\hat\valpha}; \mathcal{D})$
  to minimize with respect to the model parameters $\hat \valpha$. Note that for
  supervised learning, the cost function is dependent on the data set, allowing
  us to define metrics on how well the model reproduces the data.
\item An optimization strategy for how we should approach the minimization
\end{itemize}

\subsection{Example: Linear Regression}

\begin{figure}[h]
  \centering
  \resizebox{0.7\linewidth}{!}{%
      \input{scripts/linear_regression_example.py.tex}
  }
  \caption[Example of linear regression]{Example of linear regression applied to a simple data set of a single
    input variable. The regression tries to filter away the noise and find the
    most likely actual trend line.\citesource{writing/scripts/linear_regression_example.py}}
  \label{fig:linear-regression-example}
\end{figure}

We assume that the true underlying process, $f_{\valpha}(\vx)$, that generated
$\mathcal{D}$ is linear in the inputs $\vx$. For simplicity, we will also
assume that the outputs are scalar, $\vy \defeq y$. That is, we assume the data
was generated in the following way:
\begin{align}
  y_i = f_{\valpha}(\vx_i) + \epsilon = \vx^T\valpha + \epsilon,
\end{align}
where $\valpha$ is a column vector of coefficients, and
$\epsilon\sim\mathcal{N}(0, \sigma^2)$ is normally distributed noise with zero
mean and variance $\sigma^2$, introduced via measurement inaccuracy
etc.\footnote{Note that this definition implies that $f_{\valpha}$
  has zero intercept, i.e. $f_{\valpha}(\vb{0}) = 0$. We can easily work around
  that, however, by adding a constant element to every input vector, i.e. let
  $\vx' \defeq (1\ \ \vx)$ be the new inputs. The second option is to simply
  center the data beforehand, by subtracting the mean $\overline y =
  \flatfrac{1}{n}\sum_{i=1}^n y_i$ from every $y_i$, and then optionally revert
  back whenever needed.}

\subsubsection{Model}

Based on the above assumption, we propose the following model:

\begin{align}
\hat f_{\hat\valpha}(\vx) &= \vx^T\hat\valpha.
\end{align}
Ideally we want to learn $\hat\valpha$ from the data such that
$\hat\valpha\simeq\valpha$.

\subsubsection{Cost Function}

In order to quantify which $\hat\valpha$ is optimal, we need a metric to compare
by. There are several conceivable choices here, many of which might lead us to
the correct $\valpha$. Most common, at least in this case, is to use the
so-called \gls{mse}:

\begin{align}
  \mathcal{C}_\text{MSE}(\hat f_{\hat\valpha}; \mathcal{D}) &= \frac{1}{n}\sum_{i=1}^n \abs{\hat f_{\hat\valpha}(\vx_i) - y_i}^2.
\end{align}
The ``learning'' task is now expressed simply as the following optimization problem:
\begin{align}
  \label{eq:mse-optimization-problem-example}
  \hat\valpha = \argmin_{\valpha'}\,\mathcal{C}_\text{MSE}(\hat f_{\valpha'}; \mathcal{D}).
\end{align}

\subsubsection{Optimization}

With data, model and cost function defined, the last step is to
solve~\cref{eq:mse-optimization-problem-example}. While this can in general be a
hard task (more on this in \cref{sec:ml-optimization}), we can
actually do this particular exercise analytically with some rather simple linear algebra. Skipping
the derivation, we get the following solution:
\begin{align}
  \hat\valpha = \qty(\vX^T\vX)^{-1}\vX\vy,
\end{align}
where $\vX = (\vx_1\ \vx_2\ \dots\ \vx_n)^T$ is the matrix with inputs laid out in
rows, and $\vy$ is the column vector of all outputs.

\subsubsection{Regularization}
\label{sec:regularization}
Often it turns out to be useful to add an extra term to the cost function
that depends on the size of the parameters $\hat\valpha$. This typically takes
the following form:

\begin{align}
  \mathcal{C}(\hat f_{\hat\valpha};\mathcal{D}) &=   \mathcal{C}_\text{MSE}(\hat f_{\hat\valpha};\mathcal{D}) + \gamma\norm{\hat\valpha}_p^p,
\end{align}
where $\norm{\cdot}_p$ is the $L^p$ norm, and $\gamma$ is a parameter
we set to control the degree of regularization (typically $\gamma\ll 1$). Most
used are $p=1$ (called the LASSO loss) and $p=2$ (called the Ridge loss).

The motivation for why we might want to do this is as follows: Imagine we have
a data set with $\vx_i = (x_i,\,2x_i)$, that is to say we have two variables per
data point, but the two variables are directly correlated. Lastly, assume that
$y_i = 3x_i$ is the underlying function. Without any regularization, all of the
following models are equally good:

\begin{align}
  \hat\valpha^{(1)} = (3, 0),\ \hat\valpha^{(2)} = (1, 1),\ \hat\valpha^{(3)} = (-997, 500).
\end{align}
The problem is that without regularization, this particular
optimization problem ill-formed, and does not have a unique solution. While all
of the above give perfect reconstruction of the data, solutions like
$\hat\valpha^{(3)}$ yield strange interpretations. According to this model, the
output is strongly negatively correlated with $x_1$, in complete contradiction
with the underlying truth. Adding regularization guides the model to prefer
$\hat\valpha$'s with smaller coefficients.

While this particular example is contrived, the point should be clear: In
some cases, depending on both the data and what model we choose, the model might
be too flexible for the task at hand. This is generally referred to as the
problem of \emph{overfitting}, and can lead to strange behavior. The opposite
problem, called \emph{underfitting}, refers to the case where the model is too
constrained, e.g. trying to fit a first order polynomial to
quadratic data.

\section{Gradient-based Optimization}
\label{sec:ml-optimization}

Most supervised learning tasks rely on a gradient-based optimization strategy,
with the umbrella name \gls{gd}.
These methods can be summarized as follows: We want to find $\vx^*$ such that

\begin{align}
  \vx^* = \argmin_{\vx} g(\vx),
\end{align}
for some function $g$ that should be minimized.\footnote{For maximization, simply minimize $-f(\vx)$. }
We solve this by starting at some initial guess $\vx^{(0)}$ and improve it by
iterating the following recurrence relation:

\begin{align}
  \begin{split}\label{eq:gradient-decent-definition}
    \vx^{(n+1)} &= \vx^{(n)} + \Delta \vx^{(n)} \\
    \Delta\vx^{(n)} &= -\eta \grad{g}(\vx^{(n)}),
  \end{split}
\end{align}
where $\eta$ is a suitably chosen number, typically $\eta \ll 1$. In the
limit $n\to\infty$ (if $\eta$ is small enough), this will converge to a minimum
for $g$.

When $g=\mathcal{C}(\hat f_{\hat\valpha}; \mathcal{D})$ we have some choice in
exactly how we should compute $\grad{\mathcal{C}}$. We could compute the cost
for the entire data set, or just a single data point. These two options are
referred to as \gls{gd} and \gls{sgd}, respectively. The latter is most
used because it is less computationally expensive for large data sets, as well
as less likely to get stuck in local minima. A hybrid of the two is also
possible, called mini-batch \gls{gd}.

Lastly we have various ways of extending the basic algorithm in
\cref{eq:gradient-decent-definition}, with more sophisticated ways of
determining $\Delta \vx^{(n)}$, some of which we will mention here.

\subsection{Fixed Learning Rate}

This is the version presented in \cref{eq:gradient-decent-definition}, in where
$\eta$ is chosen in advance and remains fixed throughout all iterations:
\begin{align}
  \label{eq:fixed-learning-rate-update-rule}
  \Delta \vx^{(n)} = -\eta \grad{g(\vx^{n})}.
\end{align}

\begin{itemize}
\item Pros:
  \begin{itemize}
    \item Easy to implement
    \item Intuitive and easy to adapt based on results
  \end{itemize}
\item Cons:
  \begin{itemize}
    \item If $\eta$ is to large it can lead to divergence or inaccurate results
    \item If $\eta$ is to small we have a higher chance of getting stuck in small, local minima.
    \item If $\eta$ is small we will need many iterations before convergence
    \item All elements of $\vx$ get the same $\eta$, even though the gradient
        elements can have significantly different magnitude and/or stability.
  \end{itemize}
\end{itemize}

\subsection{Momentum}

Two common problems arise when using the update rule
in~\cref{eq:fixed-learning-rate-update-rule}, namely that we tend to get stuck
in local minima and that unstable gradients can throw us off the right track.
The idea of momentum, first introduced by \textcite{Rumelhart-1986}, aims to
combat this by letting the update be a linear combination of the previous step
and the current one:

\begin{align}
  \label{eq:momentum-update-rule-def}
  \Delta\vx^{(n)} &= \alpha \Delta \vx^{(n-1)} - \eta \grad{g(\vx^{(n)})}.
\end{align}
This introduces another hyperparameter $\alpha$, which again should be set to
an appropriate value. Letting $\alpha=0$
recovers~\cref{eq:fixed-learning-rate-update-rule}, and increasing values
increases the update terms' inertia.

\subsection{Individual Learning Rates per Element}

The magnitude and/or stability of the elements of $\grad{g}$ can be
significantly different from each other. An optimal learning rate $\eta$ might
therefore not be optimal for any single element, but rather the best compromise
available. A simple fix is to use a separate learning rate for each dimension:

\begin{align}
  \Delta\vx^{(n)} = -\vb{\eta}\circ\grad{g}(\vx^{(n)}),
\end{align}
where $\circ$ denotes the Hadamard product (element-wise multiplication). This
allows us more flexibility and will improve results, while the obvious downside
is the increased need for hyperparameter tuning.

\subsection{Averaging}

Another idea useful to overcome unstable gradients or poorly converging updates
is that of \emph{averaging}, invented by \textcite{Polyak-1992}. We keep the
simple update rule from~\cref{eq:fixed-learning-rate-update-rule}, but the final
$\vx^*$ we use is set to the average of all the intermediate steps:

\begin{align}
  \label{eq:averaging-update-rule-def}
  \vx^* = \frac{1}{n}\sum_{i=0}^{n-1} \vx^{(i)}.
\end{align}
We may also find it useful to use a weighted average, so as to give more emphasis
to later estimates. An example is exponential decay weighting, used in the next section.

\subsection{ADAM: Adaptive Moment Estimation}
\label{sec:adam}

Among the state-of-the-art algorithms available, employing momentum,
averaging and several other ideas, is the ADAM algorithm, invented by
\textcite{KingmaB14}. It is slightly more involved, and the reader is encouraged
to read the aforementioned paper for an excellent presentation. The short story
has the algorithm defined as follows:\footnote{All operations on vectors are
  element-wise. Exponentiation is expressed by superscripts \emph{not}
  surrounded in parenthesis.}

\begin{align}
  \vb {m}^{(n+1)} &= \beta_1 \vb {m}^{(n)} + (1 - \beta_1)\grad{g(\vx^{(n)})}\\
  \vb {v}^{(n+1)} &= \beta_2 \vb {v}^{(n)} + (1 - \beta_2)\grad{g(\vx^{(n)})}\circ\grad{g(\vx^{(n)})}\\
  \vb{\hat m} &= \frac{\vb{m}^{(n+1)}}{1 - \beta_1^{n+1}}\\
  \vb{\hat v} &= \frac{\vb{v}^{(n+1)}}{1 - \beta_2^{n+1}}\\
  \Delta \vx^{(n)}&= -\eta\, \vb {\hat m}\oslash\sqrt{\vb {\hat v} + \epsilon},
  % \label{eq:adam-update-rule-def}
\end{align}
where $\eta$ is as before, $\beta_1$ and $\beta_2$ are decay rates for moment
estimates, and $\epsilon$ is a small number used to avoid division by zero
issues ($\oslash$ denotes Hadamard division, element-wise division). \textcite{KingmaB14} provide default values for all the parameters, and
in many cases these work excellently right out of the box.
\begin{align}
  \label{eq:adam-default-parameters}
  \begin{split}
    \eta &= 0.001\\
    \epsilon &= 10^{-8}
  \end{split}
  \begin{split}
    \beta_1 &= 0.9\\
    \beta_2 &= 0.999
  \end{split}
\end{align}

Especially important for us is how ADAM adapts \emph{individual} learning rates
per parameter. After all, given tens of thousands of parameters, what are the
chances that all of them have the same optimal learning rate? ADAM allows us to
account for different degrees of variability in the parameters, without the need
to manually tune them all.

We will make extensive use of ADAM in our \gls{vmc} calculations, as it proved
superior to vanilla \gls{sgd} in all cases. \cref{sec:verify-learning-rate-dep}
includes a comparison on a couple of selected optimization problems.

\section{Reinforcement Learning for VMC}
\label{sec:reinf-learning-for-vmc}

As mentioned, \gls{vmc} is a type of reinforcement learning problem. While we don't
have access to a data set of configurations and their corresponding wave
function amplitude, we can say something about the goodness of any particular
wave function by measuring the expected ground state energy.

Even though reinforcement learning typically differs quite a lot from supervised
learning in both model type, optimization strategy etc., they are not so
by definition. When considering \gls{vmc} in particular, we can treat
it exactly the same way we would on a supervised problem, with one major difference:
The cost function is \emph{not} dependent on a data set. We use the same type
of models, and the same strategies to minimize the cost function.

With this key change follows some differences. In particular, in some sense it
is impossible to overfit a model. Overfitting happens when a model performs
significantly better on the training data compared to data it has not seen
before. We detect overfitting in supervised learning by withholding some of
the training data and then evaluate the performance on this test data. This
measures the models ability to generalize information, as opposed to memorizing
exact values from the training data.

For reinforcement learning, however, what would the equivalent be? There are no
data to withhold. If a \gls{vmc} calculation suddenly results in an amazingly accurate
result, then we simply have an amazingly accurate wave
function. With this in
mind, why not use an incredibly complex model? To some extent, this is the exact
argument of this thesis, and we will use some highly complex models later on.
There is, however, two limiting issues with this approach. First is the
computational cost, which limits what we can feasibly do. Second, and more
importantly, learning becomes harder with more complex models. Just because the
model is theoretically capable of representing the wave function to great
accuracy, it does not mean that we will ever be able to learn the correct
parameters. With increasing complexity we increase the number of local
minima in the parameter space. In here lies the challenge, attempting to balance
the \emph{exploration} of all possible parameter configurations with \emph{exploitation} of
promising minima.

\section{Artificial Neural Networks}
\label{sec:artificial-neural-networks}
\glsunset{ann}
\glsunset{nn}
Artificial neural networks (\glspl{ann}, or just \glspl{nn}) are a broad category of
computational models, a representation of some general function $f:
\mathbb{R}^m\to\mathbb{R}^n$.\footnote{While there is nothing inherent about
  \glspl{ann} that limit us to only real values, we will not use
  complex valued networks in this work and therefore skip to mental burden of
  thinking about the complex numbers. Furthermore, complex values can simply be
  modeled by two real valued outputs if need be.} Their name and origin stems from an attempt to
model how neurons in animal brains react to inputs, and how they adapt to learn
new skills. As animals are quite effective at learning based on the inputs from
their surroundings, it seemed promising to emulate this
process when attempting to teach a computer new tricks. \Glspl{ann} have since been the
target of extensive research, and has branched out in numerous variations that
have little to no analog in animal brains.

While the original idea to use \glspl{ann} first appeared around the middle of the 20th
century, they where initially limited, in large part due to the lacking
computational capacity of that time, and saw little use for a long time. As
computing power increased exponentially, most state-of-the-art
machine learning models today are some variation on a neural network. This
includes tasks such as natural language processing~\cite{bert-2018}, image classification and
segmentation~\cite{gpipe-2018}, and playing games such as Go~\cite{deepmind-alpha-go-zero},
Chess~\cite{deepmind-alpha-zero} and Starcraft II~\cite{vinyals_babuschkin_chung_mathieu_2019}.

The following sections are devoted to presenting the type of artificial neural
networks that we will employ in this work, including mathematical underpinnings,
architecture choices and training strategies.

\subsection{Nodes and Connections}

\begin{figure}[h]
  \centering
  \input{illustrations/neural-network.tex}
  \caption[Illustration of an artificial neural network]{Illustration of a general feed-forward neural network architecture,
    here shown with four inputs nodes, one hidden layer of 5 nodes and a
    single output node.\citesource{writing/illustrations/neural-network.tex}}
  \label{fig:neural-network-example-diagram}
\end{figure}

First things first; What is a neural network? A neural
network\footnote{Specifically feed forward neural networks. There are some types
of exotic networks that does not fit well into this description, but they are
beyond the scope of this discussion.} is a way to express a certain type of
complex computation. We model a computation as a sequence of \emph{layers}, with
each layer consisting of \emph{nodes}, and some connections between the nodes in
each layer. By convention, we call the first layer the \emph{input} layer, the
last layer the \emph{output} layer, and layers in between are
generally referred to as \emph{hidden} layers.
\Cref{fig:neural-network-example-diagram} shows a graphical depiction of this
general structure, exemplified for a certain number of layers/nodes.

Each node can hold a scalar value, and this value is propagated forward thorough
the connections. Each connection has an associated weight (scalar) which is
multiplied to the node's value before it is passed along. Nodes with multiple
incoming connections take the sum as its value. Finally we might also include
bias nodes, which are nodes with a fixed value. These serve the same role as the
intercept in a linear model, and enable us to -- well, bias the output in some
direction. If we initialize the input layer with some values, we can think of
the network as a computation flow graph -- visually representing the way
information is manipulated as it moves towards a final output.

\subsubsection{Definition}
\label{sec:ml-ann-forward-def}

 Let $a_i^{l}$ be the value of the $i$'th node in the $l$'th layer, with
$a_i^{(0)}=x_i$ being the inputs to the network. Each layer $l$ in the network
consists of a weight matrix $\mat W^{(l)}$, such that $W_{ij}^{l}$ is the
connection between $a_j^{(l-1)}$ and $a_i^{(l)}$. We also have a set of biases
$b_i^{(l)}$ for each $a_i^{(l)}$, and finally a set
of activation functions $\sigma^{(l)}: \mathbb{R}\to\mathbb{R}$ (to be
explained).\footnote{In~\cref{fig:neural-network-example-diagram} the bias is
illustrated as a single node with multiple connections, while in the
mathematical notation we express the bias as a vector of values. These two are
equivalent, and the figure is drawn as is for clarity. In all equations and
code, bias is represented by as vector.} All this comes together to define the network output $f(\vx)$ as
follows:\footnote{Repeated indices within a term imply a summation. Note however
that this is not implied for superscripts. That is, $A_{ij}^{(l)}B_{jk}^{(l)}
\defeq \sum_{j} A_{ij}^{(l)}B_{jk}^{(l)}$}\footnote{While the example diagram
only has a single output, there is nothing stopping us from having
multidimensional outputs.}

 \begin{align}
   f(\vx) &= \vb{a}^{(L)}\\
   a_k^{(l)} &= \sigma^{(l)}(z_k^{(l)})\\
   z_k^{(l)} &= W^{(l)}_{ki}a_i^{(l-1)} + b^{(l)}_k
 \end{align}
This recursively defined result is calculated by starting at $l=0$ with
$\vb{a}^{(0)}\defeq \vx$ and calculating $\vb{a}^{(l+1)}$ iteratively. The
process of doing this is sometimes referred to as a \emph{forward propagation}.

\subsection{Activation Functions}
\label{sec:ml-activation-functions}

Say we set all $\sigma^{(l)}$ to the identity transformation (i.e. no
transformation). Then the above definition would be reduced to the following:

\begin{align}
  f(\vx) = \vb{z}^{(L)}\qq{with}
   z_k^{(l)} &= W^{(l)}_{ki}z_i^{(l-1)} + b^{(l)}_k
\end{align}
If you have had your morning coffee you might already see the issue here. This
is simply a series of affine transformations, and as such could be replaced with
one single, compound transformation: $\vb{z}^{L} = \mat{W}\vx + \vb{b}$. Turns
out, without an activation function, no matter how many layers we place in
between the input and output, we have just defined a convoluted way of
performing a simple affine transformation.

That is why we pump the value of each
node through a \emph{non-linear} function, $\sigma^{(l)}: \mathbb{R}\to\mathbb{R}$.
The functions $\sigma^{(l)}$ can really be any function we choose, and what to
pick largely depends on what computation we are trying to model. Some often used
options, with varying input/output domains, are listed in
\cref{tab:activation-functions-table}, and their plots are shown in
\cref{fig:activation-function-gallery}.

\subsubsection{Which Activation Functions to Choose}

While there is a great deal to possibly discuss about the pros and cons of various
activation functions, a deep dive into this is beyond the scope of this
thesis. In practice, choosing an activation function comes down to trial
and error. Much of the neural network theory we have today is really more a set
of best practices and experiences documented by others. Seldom do we have a
rigorous backing for the choices we make, and most often a simple ``this worked
best'' is as good as we get.\footnote{This might be slight exaggeration in some
cases. But consider the state-of-the-art, massive networks of today -
understanding every aspect of their behavior seems a daunting, if at all
possible task. Sometimes the reasonable thing is simply to make some guesses and
see what happens.}

That said there are some things we \emph{can} consider, especially for the
output layer. If we want to interpret the output as a probability, something
like the Sigmoid function could be useful because it outputs numbers in $[0,
1]$. Similarly, if we want to do regression, perhaps the output layer should
have the identity function (i.e. no activation) so that the full spectrum
$(-\infty, \infty)$ is possible. Finally, in our case, wave functions tend to
involve exponentials and so we shall make heavy use of the exponential function
as the output transformation on our networks.

\begin{figure}[h]
  \centering
  \input{illustrations/activation.tex}
  \caption[Gallery of selected activation functions]{Selection of activation functions, with their plots. See
    \cref{tab:activation-functions-table} for their definitions. Note that the
    plots for LReLU and EReLU involve a parameter which has been arbitrarily set
    to 0.1 in the plots, and that the actual behavior of the function might be
    different for other choices.\citesource{writing/illustrations/activation.tex}
  }
  \label{fig:activation-function-gallery}
\end{figure}

\begin{table}
  \caption[Selection of activation functions]{Selection of activation functions. See
    \cref{fig:activation-function-gallery} for the corresponding plots.
    $a\in\mathbb{R}$ is a (hyper)parameter tuning the behavior of some of the
    activation functions.}
  \label{tab:activation-functions-table}
  \begin{tabular}{ll}
    $\,$&\\
    Activation Function & Definition\\
    \hline\\
    Sigmoid & $\sigma(x) = \frac{1}{1 + \exp(-x)}$\\
    Hyperbolic Tangent & \(\sigma(x) = \tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}\)\\
    Exponential & \(\sigma(x) = \exp(x)\)\\
    \textbf{Re}ctified \textbf{L}inear \textbf{U}nit (ReLU) & \(\sigma(x) = \max(0, x)\)\\
    \textbf{L}eaky ReLU (LReLU) & \(\sigma(x) = \max(ax, x)\qq{where} 0 < a < 1\)\\
    \textbf{E}xponential \textbf{L}inear \textbf{U}nit (ELU) & \(\sigma(x) = \begin{cases}x &\qq{where} x \geq 0\\ a\qty(\exp(x) - 1) &\qotherwise\end{cases}\)
  \end{tabular}
\end{table}

\subsection{Computational Flexibility}

Part of the reason why neural networks are such a great tool is their
flexibility. By simply changing the design of the network, or changing the type
of activation function in a layer, we can adapt and change the model as we need.
Even two identical networks can be tuned to model completely different data.
Neural networks merely define a loose framework for how layers and nodes come
together, and free us up to experiment.

The expressiveness of neural networks turns out to be just about as powerful as
anything could be. That is because \emph{any} (reasonable) function can be
arbitrarily well approximated by a neural network with a single hidden
layer~\cite{Cybenko-1989}. With just a single hidden layer it turns out we might
need exponentially many nodes, but if allowed to be deeper (more layers), we can
have reasonable number of nodes as well and still achieve arbitrarily good
results~\cite{Hanin-2017}. This result is known as the Universal Approximation
Theorem~\cite{Cybenko-1989, Hanin-2017}. A formal description for this is not
reproduced here, as the main point is to stress the expressive power of
neural networks.
\clearpage

\subsection{Backpropagation}
\label{sec:ml-backprop}

Now suitably enthusiastic about the power of neural networks, we must put our
feet back on the ground for a bit and do some working out. After all, defining a
complex model does not help us much if we cannot efficiently train it. With all
these layers and connections, a legitimate concerns is how we can determine how
to optimize the weights and biases involved.

We will be employing the \acrlong{gd} strategy from \cref{sec:ml-optimization},
which requires us to compute the gradient of a cost function,
$\grad_{\valpha}\mathcal{C}(f(\vx))$, w.r.t. all model parameters $\valpha$. In
our case, the parameters are all the weights, $\mat{W}^{(l)}$, and biases $\vb{b}^{(l)}$.

While the following derivation might appear to have a daunting amount of indices
and partial derivatives, it is really just a chain rule bonanza, applying the
same trick over and over until the problem gives up and yields a neat
solution.

Starting with the derivatives for the biases of the final layer:\footnote{For
  clarity: Repeated indices within a factor (still) imply a sum over said
  indices, but this is \emph{not} implied for superscripts in parenthesis.}

\begin{align}
  \pdv{\mathcal{C}}{b_k^{(L)}} &=
\pdv{\mathcal{C}}{z_i^{(L)}}\pdv{z_i^{(L)}}{b_k^{(L)}}
=\pdv{\mathcal{C}}{z_i^{(L)}}\pdv{}{b_k^{(L)}}\qty(W_{ij}^{(L)}a_j^{(L-1)} +
b_i^{(L)}) = \pdv{\mathcal{C}}{z_k^{(L)}}\defeq \delta_k^{(L)},
\end{align}
where
\begin{align}
  \delta_k^{(L)}\defeq \pdv{\mathcal{C}}{z_k^{(L)}} =
\pdv{\mathcal{C}}{a^{(L)}_{i}}\pdv{a_i^{(L)}}{z_k^{(L)}} =
\pdv{\mathcal{C}}{a^{(L)}_{i}}\pdv{\sigma^{(L)}(z_i^{(L)})}{z_k^{(L)}} \defeq
\pdv{\mathcal{C}}{a^{(L)}_{k}}\dot\sigma^{(L)}(z_k^{(L)}).
\end{align}
We can easily calculate this last expression as the product of the derivative of
the cost function w.r.t. the network output (which should be doable for any
sensible cost function), and the derivative of the output layer activation
function evaluated at $z_k^{(L)}$ (which again is easy for any reasonable choice
of $\sigma^{(L)}$).

For the weights of the last layer we get the following:

\begin{align}
  \pdv{\mathcal{C}}{W_{kl}^{(L)}} =
\pdv{\mathcal{C}}{z_i^{(L)}}\pdv{z_i^{(L)}}{W_{kl}^{(L)}} =
\delta_i^{(L)}\pdv{}{W_{kl}^{(L)}}\qty(W_{im}a_m^{(L-1)} + b_i^{(L)}) = \delta_k^{(L)}a_l^{(L-1)}.
\end{align}
This way we can express all the derivatives we need through $\vb{a}^{(i)}$ and
$\vb{\delta}^{(i)}$. We already know how to compute $\vb{a}^{(i)}$ for all layers,
so we just need $\vb{\delta^{(i)}}$ for $i < L$. We get this, of course, using the
chain rule some more:\footnote{While this derivation ``just'' entails the chain
  rule and some bookkeeping, exactly how to apply the chain rule is not that
  obvious at all times. It is ``easy'' once presented, but not necessarily
  trivial to reproduce from scratch.}

\begin{align}
  \delta_k^{(l)} &\defeq \pdv{\mathcal{C}}{z_k^{(l)}}\\
                 &= \pdv{\mathcal{C}}{z_i^{(l+1)}}\pdv{z_i^{(l+1)}}{z_k^{(l)}}\\
                 &= \delta_i^{(l+1)} \pdv{z_i^{(l+1)}}{a_j^{(l)}}\pdv{a_j^{(l)}}{z_k^{(l)}} \\
                 &= \delta_i^{(l+1)}\pdv{}{a_j^{(l)}}\qty(W_{im}^{(l+1)}a_m^{(l)} + b_i^{(l+1)})\pdv{\sigma^{(l)}(z_j^{(l)})}{z_k^{(l)}}\\
                 &= \delta_i^{(l+1)}W_{ik}^{(l+1)}\dot\sigma^{(l)}(z_k^{(l)}).
\end{align}
These results can be summarized more succinctly in matrix form. We have:

\begin{align}
  \label{eq:backprop-delta-recursive}
  \vb{\delta}^{(l)} =
                      \begin{cases}
                        \grad_{\vb{a}^{L}}{\mathcal{C}}\circ
                        \dot\sigma^{(L)}(\vb{z}^{(L)}) &\qfor l = L\\[20pt]
                        \qty[\qty(\mat{W}^{(l+1)})^T \vb{\delta}^{(l+1)}]\circ
                            \dot\sigma^{(l)}(\vb{z}^{(l)})&\qfor l < L
                      \end{cases}
\end{align}
and the final gradients:
\begin{align}
  \label{eq:backprop-grads-recursive}
  \grad_{\vb{b}^{(l)}}{\mathcal{C}} = \vb{\delta}^{(l)}
  \qand \grad_{\mat{W}^{(l)}}{\mathcal{C}} = \vb{\delta}^{(l)}\vb{a}^{(l-1)}.
\end{align}
The process of calculating these gradients involve starting at the output layer,
working our way backwards, and is therefore referred to as
\emph{backpropagation}, with
\cref{eq:backprop-delta-recursive,eq:backprop-grads-recursive} referred to as
the \emph{backpropagation algorithm}.\\

With this final piece of the puzzle, we know everything we need from \gls{ml}
for the purposes of this thesis. The last remaining issue is how to introduce
these models into the framework of \gls{vmc}, to which we dedicate the next chapter.

\end{document}


\section{Reliability Models}
\label{sec:ReliabilityModels}

\newcommand{\aliasRequiredParameterDescription}[1]
{
	\xmlNode{#1}, \xmlDesc{string or float, required parameter}. See the above definition.
	If a string was provided, the reliability model would treat it as an input variable that came
	from other RAVEN entity. In this case, the variable must be listed in the subnode
	\xmlNode{variables} under \xmlNode{ExternalModel}.
}
\newcommand{\aliasOptionalParameterDescription}[1]
{
	\xmlNode{#1}, \xmlDesc{string or float, optional parameter}. See the above definition.
	If a string was provided, the reliability model would treat it as an input variable that came
	from other RAVEN entity. In this case, the variable must be listed in the subnode
	\xmlNode{variables} under \xmlNode{ExternalModel}.
	\default{1}
}

\textbf{Reliability Models} are the most frequently used in life data analysis
and reliability engineering. These models/functions give the probability of a component
operating for a certain amount of time without failure. As such, the reliability models
are function of time, in that every reliability value has an associated time value. In
other words, one must specify a time value with the desired reliability value. This degree
of flexibility makes the reliability model a much better reliability specification that the
mean time to failure (MTTF), which only represents one point along the entire reliability
model.

\subsection{The Probability Density and Cumulative Density Functions}
From probability and statistics, given a continuous random variable $X$, we denote:
\begin{itemize}
	\item The probability density function (pdf), as $f(x)$
	\item The cumulative density function (cdf), as $F(x)$.
\end{itemize}
If $x$ is a continuous random variable, then the probability of $x$ takes on a value in the
interval $[a,b]$ is the area under the pdf $f(x)$ from $a$ to $b$
\begin{equation}
  P(a\leq x\leq b) = \int_{a}^{b} f(x)dx
\end{equation}
The cumulative distribution function is a function $F(x)$ of a random variable $x$ and is
defined for a number $x_0$ by
\begin{equation}
  F(x_0) = P(x\leq x_0) = \int_{-\infty}^{x_0} f(s)ds
\end{equation}
That is, for a given value $x_0$, $F(x_0)$ is the probability that the observed value of $x$
would be, at most, $x_0$. The mathematical relationship between the pdf and cdf is given by:
\begin{equation}
  F(x) = \int_{-\infty}^{x} f(s)ds
\end{equation}
Conversely
\begin{equation}
  f(x) = - \frac{dF(x)}{dx}
\end{equation}
The functions most commonly used in reliability engineering and life data analysis, namely the
reliability function and failure rate function, can be determined directly from the pdf definition,
or $f(t)$. Different distributions exist, such as Lognormal, Exponential, Weibull, etc., and each of
them has a predefined $f(t)$. These distributions were formulated by statisticians, mathematicians,
and engineers to mathematically model or represent certain behavior. Some distributions tend to better
represent life data and are most commonly referred to as lifetime distributions.

\subsection{The Reliability and Failure Rate Models}
Given the mathematical representation of a distribution, we can derive all functions needed
for reliability analysis (i.e., reliability models/functions). This would only depend on the value of $t$
after the value of the distribution parameters are estimated from data.
Now, let $T$ be the random variable defining the lifetime of the component with cdf $F(t)$, which is the
time the component would operate before failure. The cdf $F(t)$ of the random variable $T$ is given by
\begin{equation}
  F(t) = \int_{-\infty}^{t} f(T)dT
\end{equation}
If $F(t)$ is a differentiable function, the pdf $f(t)$ is given by
\begin{equation}
  f(t) = - \frac{dF(t)}{dt}
\end{equation}
The reliability function or survival function $R(t)$ of the component is given by
\begin{equation}
  R(t) = P(T>t) = 1 - P(T\leq t) = 1-F(t)
\end{equation}
This is the probability that the component would operate after time t, sometimes called the survival probability.
The failure rate of a system during the interval $[t,t+\Delta t]$ is the probability that a failure per
unit time occurs in the interval, given that a failure has not occurred prior to t, the beginning of the
interval. The failure rate function (i.e., instantaneous failure rate, conditional failure rate) of the hazard
function is defined as the limit of the failure rate as the interval approaches zero
\begin{equation}
  \lambda (t)= \lim_{\Delta t\rightarrow 0} \frac{F(t+\Delta t) - F(t)}{\Delta tR(t)}
	 = \frac{1}{R(t)} \lim_{\Delta t\rightarrow 0} \frac{F(t+\Delta t) - F(t)}{\Delta t}
	 = \frac{1}{R(t)}\frac{dF(t)}{dt} = \frac{f(t)}{R(t)}
\end{equation}
The failure rate function is the rate of change of the conditional probability of a failure at time $t$.
It measures the likelihood that a component that has operated up until time $t$ fails in the next
instance of time.
Generally, $\lambda (t)$ is the one tabulated because it is measured experimentally and because it tends to
vary less rapidly with time than the other parameters. When $\lambda (t)$ is given, all other three
parameters, $F(t)$, $f(t)$, $R(t)$, can be computed as follows
\begin{equation}
  R(t) = \exp(-\int_{0}^{t} \lambda (s)ds)
\end{equation}
\begin{equation}
	f(t) = \lambda (t)R(t) = \lambda (t)\exp(-\int_{0}^{t} \lambda (s)ds)
\end{equation}
\begin{equation}
 	F(t) = 1 - R(t) = 1 - \exp(-\int_{0}^{t} \lambda (s)ds)
\end{equation}
The mean time between failure (MTBF) can be obtained by finding the expected value of the random variable
$T$, time to failure. Hence
\begin{equation}
  MTBF = E(T) = \int_{0}^{\infty} tf(t)dt = \int_{0}^{\infty} R(t)dt
\end{equation}

\subsection{The Lifetime Distributions or Aging Models}
We would consider several of the most useful reliability models based on different probability
distributions for describing the failure of continuous operating devices, including:
\begin{itemize}
	\item Exponential, model \xmlAttr{type} is \xmlString{exponential}
	\item Erlangian, model \xmlAttr{type} is \xmlString{erlangian}
	\item Gamma, model \xmlAttr{type} is \xmlString{gamma}
	\item Lognormal, model \xmlAttr{type} is \xmlString{lognorm}
	\item Fatigue Life, model \xmlAttr{type} is \xmlString{fatiguelife}
	\item Weibull, model \xmlAttr{type} is \xmlString{weibull}
	\item Exponential Weibull, model \xmlAttr{type} is \xmlString{exponweibull}
	\item Bathtub, model \xmlAttr{type} is \xmlString{bathtub}
	\item Power Law, model \xmlAttr{type} is \xmlString{powerlaw}
	\item Log Linear, model \xmlAttr{type} is \xmlString{loglinear}.
\end{itemize}

The specifications of these models must be defined within a RAVEN \xmlNode{ExternalModel}. This
XML node accepts the following attributes:
\begin{itemize}
	\item \xmlAttr{name}, \xmlDesc{required string attribute}, user-defined identifier of this model.
	\nb As with other objects, this identifier can be used to reference this specific entity from other
	input blocks in the XML.
	\item \xmlAttr{subType}, \xmlDesc{required string attribute}, defines which of the subtypes should
	be used. For reliability models, the user must use \xmlString{SR2ML.ReliabilityModel} as the subtype.
\end{itemize}
In the reliability \xmlNode{ExternalModel} input block, the following XML subnodes are required:
\begin{itemize}
	\item \xmlNode{variable}, \xmlDesc{string, required parameter}. Comma-separated list of variable
	names. Each variable name needs to match a variable used or defined in the reliability model or variable
	coming from another RAVEN entity (i.e. Samplers, DataObjects and Models).
	\nb For all the reliability models, the following outputs variables would be available. If the user
	added these output variables in the node \xmlNode{variables}, these variables would be also available to
	for use anywhere in the RAVEN input to refer to the reliability model output variables.
	\begin{itemize}
		\item \xmlString{pdf\_f}, variable contains the calculated pdf value or values at given time instance(s),
		(i.e., a series of times).
		\item \xmlString{cdf\_F}, variable contains the calculated cdf value or values at given time instance(s),
		(i.e., a series of times).
		\item \xmlString{rdf\_R}, variable contains the calculated reliability function (rdf) value or values at
		given time instance(s), (i.e., a series of times).
		\item \xmlString{frf\_h}, variable contains the calculated failure rate function (frf) value or values
		at given time instance(s), (i.e., a series of times).
	\end{itemize}
	\nb When the external model variables are defined, at run time, RAVEN initializes
	them and tracks their values during the simulation.
	\item \xmlNode{ReliabilityModel}, \xmlDesc{required parameter}. The node is used to define the reliability
	model, and it contains the following required XML attribute:
	\begin{itemize}
		\item \xmlAttr{type}, \xmlDesc{required string attribute}, user-defined identifier of the reliability model.
		\nb the types for different reliability models can be found at the beginning of this section.
	\end{itemize}
	In addition, this node accepts several different subnodes representing the model parameters depending on the
	\xmlAttr{type} of the reliability model. The common subnodes for all reliability models are:
	\begin{itemize}
		\item \xmlNode{Tm}, \xmlDesc{string or float or comma-separated float, required parameter}. Time instance(s)
		that the reliability models would use to compute the pdf, cdf, rdf, and frf values. If a string was provided,
		the reliability model would treat it as an input variable that came from entities of RAVEN. In this
		case, the variable must be listed in the sub-node \xmlNode{variables} under \xmlNode{ExternalModel}.
		\item \xmlNode{Td}, \xmlDesc{string or float, optional parameter}. The time that the reliability models start to be active.
		If a string was provided, the reliability model would treat it as an input variable that came
		from RAVEN entities. In this case, the variable must be listed in the sub-node \xmlNode{variables}
		under \xmlNode{ExternalModel}.
		\default{0.}
	\end{itemize}
\end{itemize}
In addition, if the user wants to use the \textbf{alias} system, the following XML block can be input:
\begin{itemize}
	\item \xmlNode{alias} \xmlDesc{string, optional field} specifies an alias for
	any variable of interest in the input or output space for the ExternalModel.
	%
	These aliases can be used anywhere in the RAVEN input to refer to the ExternalModel
	variables.
	%
	In the body of this node, the user specifies the name of the variable that the ExternalModel is
	going to use (during its execution).
	%
	The actual alias, usable throughout the RAVEN input, is instead defined in the
	\xmlAttr{variable} attribute of this tag.
	\\The user can specify aliases for both the input and the output space. As a sanity check, RAVEN
	requires an additional required attribute \xmlAttr{type}. This attribute can be either ``input'' or ``output.''
	%
	\nb The user can specify as many aliases as needed.
	%
	\default{None}
\end{itemize}

Example XML (Bathtub Reliability Model):
\begin{lstlisting}[style=XML]
	<ExternalModel name="bathtub" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="bathtub">

			<beta>1.</beta>
			<theta>1.0</theta>

			<Tm>tm</Tm>

			<alpha>1.0</alpha>
			<rho>0.5</rho>

			<c>0.5</c>
		</ReliabilityModel>

		<alias variable='bathtub_F' type='output'>cdf_F</alias>
		<alias variable='bathtub_f' type='output'>pdf_f</alias>
		<alias variable='bathtub_R' type='output'>rdf_R</alias>
		<alias variable='bathtub_h' type='output'>frf_h</alias>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Lognormal Model}
The probability density function of the lognormal is given by
\begin{equation}
	f(T_m) = \frac{1}{\alpha T_m\sqrt{2\pi}}\exp\left(-\frac{1}{2}\left(\frac{\ln{\frac{T_m-T_d}{\beta}}}{\alpha}\right)^2\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta >0$, $\beta$ is the scale parameter, $\alpha$ is the shape
parameter, and $T_d$ is the location parameter.
This model accepts the following additional sub-nodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="lognorm" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>
		<ReliabilityModel type="lognorm">

			<beta>1.</beta>

			<Tm>tm</Tm>

			<alpha>1.</alpha>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Exponential Model}
The probability density function of the exponential is given by
\begin{equation}
	f(T_m) = \lambda\exp\left(-\lambda\left(T_m-T_d\right)\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \lambda >0$, $\lambda$ is the mean failure rate or the inverse of scale parameter,
and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{lambda}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="exponential" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="exponential">

			<lambda>1.</lambda>

			<Tm>tm</Tm>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Weibull Model}
The probability density function of the Weibull is given by
\begin{equation}
	f(T_m) = \frac{\alpha}{\beta}\left(\frac{T_m-T_d}{\beta}\right)^{\alpha-1}\exp\left(-\left(\frac{T_m-T_d}{\beta}\right)^\alpha\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta >0$, and $\beta$ is the scale parameter, $\alpha$ is the shape
parameter, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="weibull" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="weibull">

			<beta>1.</beta>

			<Tm>tm</Tm>

			2.0

			<alpha>1.0</alpha>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Erlangian Model}
The probability density function of the Erlangian is given by
\begin{equation}
	f(T_m) = \frac{\lambda\left(\lambda T_m\right)^{k-1}\exp\left(-\lambda T_m\right)}{\left(k-1\right)!}
\end{equation}
where $T_m\geq T_d$, $T_d, \lambda >0$, and $\lambda$ is the inverse of scale parameter, $k$ is positive integer
that control the shape, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{k}
	\nb $k$ is a positive integer. If a float was provided, a warning would be raised.
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="erlangian" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="erlangian">

			<lambda>0.1</lambda>

			<Tm>tm</Tm>

			<k>2</k>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Gamma Model}
The probability density function of the Gamma is given by
\begin{equation}
	f(T_m) = \frac{\beta \left(\beta \left(T_m-T_d\right)\right)^{\alpha-1}\exp\left(-\beta\left(T_m-T_d\right)\right)}{\Gamma \left(\alpha\right)}
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta >0$, and $\beta$ is the inverse of scale parameter, $\alpha$ is the shape
parameter, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="gamma" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="gamma">

			<beta>0.1</beta>

			<Tm>tm</Tm>

			<alpha>2.</alpha>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Fatigue Life Model (Birnbaum-Saunders)}
The probability density function of the fatigue life is given by
\begin{equation}
	f(T_m) = \frac{\frac{T_m-T_d}{\beta}+1}{2\alpha\sqrt{2\pi\left(\frac{T_m-T_d}{\beta}\right)^3}}
				 \exp\left(-\frac{\left(\frac{T_m-T_d}{\beta}-1\right)^2}{2\left(\frac{T_m-T_d}{\beta}\right)\alpha^2}\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta >0$, and $\beta$ is the scale parameter, $\alpha$ is the shape
parameter, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="fatiguelife" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="fatiguelife">

			<beta>1.</beta>

			<Tm>tm</Tm>

			<alpha>1.0</alpha>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Exponentiated Weibull Model}
The probability density function of the exponentiated Weibull is given by
\begin{equation}
	f(T_m) = \gamma\alpha\left(1-\exp\left(-\left(\frac{T_m-T_d}{\beta}\right)^\alpha\right)\right)^{\gamma-1}
				 \left(\frac{T_m-T_d}{\beta}\right)^{\alpha-1}\exp\left(-\left(\frac{T_m-T_d}{\beta}\right)^\alpha\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta, \gamma>0$, and $\beta$ is the scale parameter, $\alpha$ and $\gamma$ is the shape
parameter, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
	\item \aliasRequiredParameterDescription{gamma}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="exponweibull" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="exponweibull">

			<beta>1.</beta>

			<Tm>tm</Tm>

			2.0

			<alpha>1.0</alpha>
			<gamma>0.5</gamma>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Bathtub Model}
The reliability function is given by:
\begin{equation}
  R(T_m) = \exp\left(-c\beta\left(\frac{T_m-T_d}{\beta}\right)^\alpha -(1-c)\left(\exp\left(\frac{T_m-T_d}{\theta}\right)^\rho -1\right)\right)
\end{equation}
The failure rate function is given by
\begin{equation}
  \lambda(T_m) = c\alpha\left(\frac{T_m-T_d}{\beta}\right)^{\alpha-1}+(1-c)\rho\left(\frac{T_m-T_d}{\theta}\right)^{\rho-1}
\end{equation}
The probability density function of the Bathtub is given by
\begin{equation}
	f(T_m) = \lambda(T_m) R(T_m)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta, \theta, \rho, c >0$, and $\beta, \theta$ are the scale parameters,
$\alpha, \rho$ are the shape parameters, $c \in [0,1]$ is the weight parameter, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasRequiredParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
	\item \aliasOptionalParameterDescription{theta}
	\item \aliasOptionalParameterDescription{rho}
	\item \aliasOptionalParameterDescription{c}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="bathtub" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="bathtub">

			<beta>1.</beta>
			<theta>1.0</theta>

			<Tm>tm</Tm>

			<alpha>1.0</alpha>
			<rho>0.5</rho>

			<c>0.5</c>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Power Law Model for Failure Rate Function}
The hazard rate satisfies a power law as a function of time
\begin{equation}
	\lambda(T_m) = \lambda + \alpha(T_m-T_d)^\beta
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta, \lambda >0$,
and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasOptionalParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
	\item \aliasOptionalParameterDescription{lambda}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="powerlaw" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="powerlaw">
			<beta>1.0</beta>
			<alpha>1.0</alpha>
			<lambda>0.5</lambda>
			<Tm>tm</Tm>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsubsection{The Log Linear Model for Failure Rate Function}
The hazard rate satisfies a exponential law as a function of time:
\begin{equation}
	\lambda(T_m) = \exp\left(\alpha+\beta(T_m-T_d)\right)
\end{equation}
where $T_m\geq T_d$, $T_d, \alpha, \beta >0$, and $T_d$ is the location parameter.
This model accepts the following additional subnodes:
\begin{itemize}
	\item \aliasOptionalParameterDescription{alpha}
	\item \aliasOptionalParameterDescription{beta}
\end{itemize}

Example XML:
\begin{lstlisting}[style=XML]
	<ExternalModel name="loglinear" subType="SR2ML.ReliabilityModel">
		<variables>cdf_F, pdf_f, rdf_R, frf_h, tm</variables>

		<ReliabilityModel type="loglinear">
			<beta>1.</beta>
			<alpha>1.</alpha>
			<Tm>tm</Tm>
		</ReliabilityModel>
	</ExternalModel>
\end{lstlisting}

\subsection{Reliability Models Reference Tests}
\begin{itemize}
	\item SR2ML/tests/reliabilityModel/test\_bathtub.xml
  \item SR2ML/tests/reliabilityModel/test\_erlangian.xml
	\item SR2ML/tests/reliabilityModel/test\_expon.xml
  \item SR2ML/tests/reliabilityModel/test\_exponweibull.xml
	\item SR2ML/tests/reliabilityModel/test\_fatiguelife.xml
  \item SR2ML/tests/reliabilityModel/test\_gamma.xml
	\item SR2ML/tests/reliabilityModel/test\_loglinear.xml
  \item SR2ML/tests/reliabilityModeltest\_lognorm.xml
	\item SR2ML/tests/reliabilityModel/test\_normal.xml
  \item SR2ML/tests/reliabilityModel/test\_powerlaw.xml
	\item SR2ML/tests/reliabilityModeltest\_weibull.xml
	\item SR2ML/tests/reliabilityModel/test\_time\_dep\_ensemble\_reliability.xml.
\end{itemize}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header2.tex}

\title{Phys 230A -- QFT -- HW01}
\author{UCLA, Fall 2014}
\date{\formatdate{22}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section*{Problem 1: Natural units}
\textit{
As standard in high energy physics, we work in natural units such that $\hbar = c = 1$, i.e. $c$ represents one unit of velocity. Furthermore, it is standard to express all dimensionful quantities in terms of GeV (to some power). This is convenient because $1~\text{GeV} \approx \text{rest energy of proton}$. Any dimensionful quantity which can be expressed in MKS units can be written in terms of GeV. Do so for the following.
}

\begin{enumproblem}

% part A
\item \textit{$\unit{1}{s}$}

% B
\item \textit{$\unit{1}{m}$}

% C
\item \textit{$\unit{1}{kg}$}

% D
\item \textit{Bohr radius}

% E
\item \textit{electron mass}

% F
\item \textit{Newton constant}

% G
\item \textit{age of universe}

\end{enumproblem}

\section*{Problem 2: EM field}
\textit{
Work out the Euler-Lagrange equations for the action
\begin{eqn}
S = -\frac{1}{4} \int \dif^4 x ~ F^{\mu\nu} F_{\mu\nu}, \qquad F_{\mu\nu} = \partial_\mu A_\nu - \partial_\nu A_\mu.
\end{eqn}
Identifying the $\v{E}$ and $\v{B}$ fields appropriately, verify that these equations (supplemented with the identity $\partial_\alpha F_{\mu\nu} + \partial_\mu F_{\nu\alpha} + \partial_\nu F_{\alpha\mu} = 0$) are equivalent to Maxwell's equations, e.g. as seen on the PAB elevator doors.
}

\section*{Problem 3: Hamiltonian of free scalar field}
\textit{
Starting from
\begin{eqn}
H = \frac{1}{2} \int \dif^3 x \left( \Pi_\phi^2 + (\v{\nabla} \phi)^2 + m^2 \phi^2 \right)
\end{eqn}
and the mode expansion for $\phi$, work out the form of $H$ in terms of $a_p$ and $a_p^\dagger$.
}

\section*{Problem 4: Momentum operator}
\textit{
For an interacting scalar field with Lagrangian
\begin{eqn}
\Lag = \frac{1}{2} \partial^\mu \phi ~ \partial_\mu \phi - V(\phi),
\end{eqn}
define the momentum operator
\begin{eqn}
\v{P} = - \int \dif^3 x ~ \Pi_\phi (x) \v{\nabla} \phi (x).
\end{eqn}
}

\begin{enumproblem}

% part A
\item \textit{
Show that $\v P$ is conserved.
}

% part B
\item \textit{
Show that
\begin{eqn}
[P_i, \phi(x)] = i \partial_i \phi(x).
\end{eqn}
}

% part C
\item \textit{
Show that
\begin{eqn}
e^{-i a^i p_i} \phi(x) e^{i a^i p_i} = \phi(x+a).
\end{eqn}
}

% part D
\item \textit{
In the case of a free scalar field, express $\v{P}$ in terms of $a_k$ and $a_k^\dagger$ operators.
}

\end{enumproblem}

\section*{Problem 5: Noether theorem}
\textit{
Consider a collection of $N$ real scalar field with Lagrangian
\begin{eqn}
\Lag = \sum_{i=1}^N \frac{1}{2} \partial^\mu \phi_i \partial_\mu \phi_i - V(\sum_{i=1}^N \phi_i \phi_i).
\end{eqn}
}

\begin{enumproblem}

% part A
\item \textit{
Show that $\Lag$ is invariant under the infinitesimal transformation
\begin{eqn}
\delta \phi_i = \epsilon \sum_j T_{ij} \phi_j, \qquad T_{ij} = -T_{ji}.
\end{eqn}
}

% part B
\item \textit{
Derive the corresponding Noether currents, and verify that they are conserved.
}

% part C
\item \textit{
How many independent conserved currents are there?
}

\end{enumproblem}

\section*{Problem 6: Boosts}
\textit{
Noether's theorem converts symmetries into conserved quantities. So what is the conserved quantity associated with Lorentz boosts?
}

\section*{Problem 7: Functional derivatives}

\begin{enumproblem}

% part A
\item \textit{
Consider an action with higher time derivatives
\begin{eqn}
S = \int_{t_1}^{t_2} \dif t ~ L(x, \dot x, \ddot x).
\end{eqn}
Specify boundary conditions such that $\fd{S}{x(t)}$ exists, and evaluate it to obtain the Euler-Lagrange equation for this system. Generalize to
\begin{eqn}
S = \int_{t_1}^{t_2} \dif t ~ L(x, \dot x, \ddot x, \dddot x, \dots).
\end{eqn}
}

% part B
\item \textit{
Suppose we want to impose mixed boundary conditions
\begin{eqn}
\od{x}{t} + \alpha x = 0 \quad \text{at $t=t_1$ and $t = t_2$}
\end{eqn}
for some constant $\alpha$. Starting from
\begin{eqn}
S = \int_{t_1}^{t_2} dt \left( \frac{1}{2} \mu \dot x^2 - V(x) \right),
\end{eqn}
show that one can add boundary terms to $S$ such that $\fd{S}{x}$ exists with these boundary conditions.
}

% part C
\item \textit{
The Hamiltonian form of the action is
\begin{eqn}
S = \int_{t_1}^{t_2} \dif t ~ (p \dot x - H(x,p)),
\end{eqn}
where $x(t)$ and $p(t)$ are treated as independent. Show that with appropriate boundary conditions the equations
\begin{eqn}
\fd{S}{x(t)} = \fd{S}{p(t)} = 0
\end{eqn}
are Hamilton's equations, and that these are equivalent to the Euler-Lagrange equations obtained from the Lagrangian version of the action.
}

\end{enumproblem}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lachaise Assignment
% LaTeX Template
% Version 1.0 (26/6/2018)
%
% This template originates from:
% http://www.LaTeXTemplates.com
%
% Authors:
% Marion Lachaise & François Févotte
% Vel (vel@LaTeXTemplates.com)
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% install directions
% ------------------
%
% if you get the error where stmaryrd.sty not installed, run
% $ sudo apt-get install texlive-science
% reference: https://askubuntu.com/questions/369268/missing-sty-package

%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass{article}

\input{structure.tex} % Include the file specifying the document structure and custom commands

%----------------------------------------------------------------------------------------
%	Title, author, institution, date, etc
%----------------------------------------------------------------------------------------

\title{T-10 paper simulation} % Title of the assignment

\author{John Brooks\\ \texttt{jwb2159@columbia.edu}} % Author name and email address

\date{Columbia University --- \today} % University, school and/or department name(s) and a date

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

\begin{document}

\maketitle % Print the title

%----------------------------------------------------------------------------------------
%	INTRODUCTION
%----------------------------------------------------------------------------------------

\section{Introduction} % Unnumbered section

This writeup covers the derivation implementation of Chudnovskiy's 2003 tearing mode simulation on the T-10 tokamak with a biased limiter \cite{2003}.  Some of the details I needed to fully implement the simulation were missing, and a later T-10 paper by Ivanov \cite{2014} fills in some of the gaps.

The code solves for the perturbed magnetic poloidal flux $\Psi(r,t)$ in the T-10 tokamak in the presence of m/n=2/1 tearing mode and with a biased limiter.  The idea of the simulation is that current sourced by the biased limiter into the plasma couples with the tearing mode and can result it changes in the mode's amplitude and frequency\cite{2003}.

\section{Code setup} % Numbered section

The domain for this problem is broken up into three sections: the region inside the resonant surface ($0<r<r_s-W/2$), the resonant surface ($r_s-W/2<r<r_s+W/2$ and the region outside ($r_s+W/2<r<b$) where $r_s$ is the location of the resonant surface, $W$ is the island width, and $b$ is the location of the vessel wall and magnetic sensor.

The inner and outer regions are a Boundary Value Problem (BVP) with a current source term at $a$, the location of the limiter.  The middle region is a time evolution problem of the tearing mode solved at $r_s$, and $\Psi$ within this region is assumed to be constant.  The middle region couples to the outer regions by providing Dirichlet boundary conditions to the BVPs.  In return, the BVPs dictate the tearing mode stability parameter which is needed to calculate the time evolution of the tearing mode.

\subsection{Boundary Value Problem (BVP)}

The BVP equations take the form,

\begin{equation} \label{eq:BVPEquations}
\begin{split}
\frac{\partial}{\partial r} &  \left( r \frac{\partial \Psi_C}{\partial r} \right)-\left( \frac{m^2}{r} +\frac{\mu_0 R}{B_T} \frac{\partial j / \partial r}{\mu(r)-n/m} \right) \Psi_C  = - \mu_0r \cdot \iota(t) \delta (r-a)  \\
 \frac{\partial}{\partial r} &  \left( r \frac{\partial \Psi_S}{\partial r} \right)-\left( \frac{m^2}{r} +\frac{\mu_0 R}{B_T} \frac{\partial j / \partial r}{\mu(r)-n/m} \right) \Psi_S =0
\end{split}
\end{equation}

\noindent where the $C$ and $S$ subscripts represent the cosine and sine components of $\Psi$.  These equations need to be solved for both the sine and cosine terms and in the inner and outer regions, resulting in 4 BVP equations.  Of note, $W$ is a function of time and grows and contracts as the island expands and contracts.  This results in the sizes of the three domains periodically expanding and contracting, making the solver more complicated.

To solve Eq.~\ref{eq:BVPEquations}, it first needs to be discretized.  Of note, there , and there are four versions of this equation (inner cosine, inner sine, outer cosine, and outer sine).  Fortunately, the discretized form is nearly identical:

\begin{equation} \label{eq:BVPSolved}
\begin{split}
 \frac{\partial \Psi^n}{\partial r} + r \frac{\partial^2 \Psi^n}{\partial r^2} -\alpha(r)  \Psi^n  & = + \beta(r,t) \\
 \frac{\Psi^{n+1}-\Psi^{n-1}}{2\Delta r}+r\left(\frac{\Psi^{n+1}-2\Psi^{n}+\Psi^{n-1}}{\Delta r^2}\right)-\alpha(r)\Psi^n & = + \beta(r,t)  \\
  \Psi^{n+1}\left(\frac{1}{2 \Delta r}+\frac{r}{\Delta r^2 }\right) + \Psi^n\left( \frac{-2r}{\Delta r^2} -\alpha(r) \right) + \Psi^{n-1}\left(-\frac{1}{2\Delta r} + \frac{r}{\Delta r^2} \right) & = + \beta(r,t) \\
  \Psi^{n+1} \ \gamma_{+1}(r) + \Psi^n \ \gamma_{0}(r) + \Psi^{n-1} \ \gamma_{-1}(r) & = + \beta(r,t) \\
A(r) \Psi(r) & = +\beta(r,t) \\
\end{split}
\end{equation}

\noindent and $\Psi$ is solved with python's \emph{scipy.sparse.linalg.spsolve} command.

The boundary conditions for $\Psi$ are

\begin{equation} \label{eq:BVPAMatrix}
\begin{split}
\Psi_{C,S}(0)&=0 \\
\Psi_{C,S}(b)&=0 \\
\Psi_{C,S}(r_s-W/2)&=\Psi_{C,S}(t,r_s) \\
\Psi_{C,S}(r_s+W/2)&=\Psi_{C,S}(t,r_s) \\
\end{split}
\end{equation}

\noindent where $\Psi_{C,S}(r_s)$ is time evolution of $\Psi$ solved at the surface and discussed in the following section.  To enforce these boundary conditions, $A$ and $\beta_{C,S}$ need to be set correctly.  For $A$, the on-diagonal corner entries are set to 1,

\begin{equation} \label{eq:BVPAMatrix}
\begin{split}
A &=
\begin{bmatrix}
1& 0 & 0 & & & & 0\\
 \gamma_{-1}(r)  & \gamma_{0}(r)      & \gamma_{+1}(r) &  &   &  \\
0 & \gamma_{-1}(r)       & \gamma_{0}(r)      & \gamma_{+1}(r)  &  &  \\
& & \ddots      & \ddots  & \ddots  & \\
& &    &\gamma_{-1}(r)       & \gamma_{0}(r)      & \gamma_{+1}(r) & 0 \\
&        &  &  & \gamma_{-1}(r)       & \gamma_{0}(r) & \gamma_{+1}(r) \\
0 &        &  &  & 0 &  0  & 1 \\
\end{bmatrix}. \\
\end{split}
\end{equation}

Above, the continuous form of $\beta$ is

\begin{equation} \label{eq:beta}
\begin{split}
\beta(r,t) &= \left\{
\begin{array}{l l}
- \mu_0r \cdot \iota(t) \delta (r-a) & \text{ for } \Psi_C \\
0 & \text{ for } \Psi_S\\
\end{array} \right. \\\
\end{split}
\end{equation}
and it needs to be solved for all four $\Psi$ terms.  In addition, the first and last terms in $\beta$ contain the BC values discussed above.  Therefore, it's discretized form is
\begin{equation} \label{eq:BVPAMatrix}
\begin{split}
\beta_{C \ inside}(t) &=
\begin{bmatrix}
0,& \ldots &  0, & \Psi_{C}(t,r_s)
\end{bmatrix}^T, \\
\beta_{S \ inside}(t) &=
\begin{bmatrix}
0,& \ldots &  0, & \Psi_{S}(t,r_s)
\end{bmatrix}^T, \\
\beta_{C \ outside}(t) &=
\begin{bmatrix}
\Psi_{C}(t,r_s), & 0, & \ldots &0, &-\mu_0r_s \cdot \iota(t)/\Delta r,  &0,&\ldots &  0
\end{bmatrix}^T, \\
\beta_{S \ outside}(t) &=
\begin{bmatrix}
\Psi_{S}(t,r_s), & 0, & \ldots &  0 \\
\end{bmatrix}^T, \\
\end{split}
\end{equation}
where the discretized delta function is $1/ \Delta r$ and is defined only at the $r=r_s$ grid.  Here, $\Delta r$ is the width of each radial grid.

The other terms in the above equations are

\begin{equation} \label{eq:BVPTerms}
\begin{split}
\alpha(r)&=\left( \frac{m^2}{r} +\frac{\mu_0 R}{B_T} \frac{\partial j / \partial r}{\mu(r)-n/m} \right) \\
\mu(r)&=1/q(r)\\
\iota(t) &= \frac{J(t)m}{2a} \\
  \gamma_{+1}(r) &=\left(\frac{1}{2 \Delta r}+\frac{r}{\Delta r^2 }\right) \\
  \gamma_{0}(r)& = \left( \frac{-2r}{\Delta r^2} -\alpha(r) \right)\\
  \gamma_{-1}(r)& = \left(-\frac{1}{2\Delta r} + \frac{r}{\Delta r^2} \right)
\end{split}
\end{equation}

In addition, first order center differencing was used to discretize the differential operators.

\begin{equation} \label{eq3}
\begin{split}
 \frac{\partial f^n}{\partial x} & = \frac{f^{n+1}-f^{n-1}}{2\Delta x} \\
 \frac{\partial^2 f^n}{\partial x^2} & = \frac{f^{n+1}-2f^n+f^{n-1}}{\Delta x^2} \\
\end{split}
\end{equation}

\subsection{Time step}

The BVP problem, discussed in the previous section, is solved for the domain inside of the rational surface and outside of the rational surface.  The time evolution of the tearing mode only occurs at $r_s$ and is evolved in time with a first order forward Euler step.  The solution for each time advance provides the boundary conditions for the BVPs which then need to be resolved and then the time advanced again.

The tearing mode's evolution equations are

\begin{equation} \label{eq3}
\begin{split}
 & \frac{\partial \Psi_{C,r_s}}{\partial t} =k a^2 \omega_R \frac{\Delta'_C(W)}{W}\Psi_{C,r_s}-\Omega \Psi_{S,r_s} \\
  & \frac{\partial \Psi_{S,r_s}}{\partial t} =k a^2 \omega_R \frac{\Delta'_S(W)}{W}\Psi_{S,r_s}+\Omega \Psi_{C,r_s}
\end{split}
\end{equation}

Discretizing and solving, it takes the form

\begin{equation} \label{eq3}
\begin{split}
\frac{\Psi_{C,r_s}^{n+1}-\Psi_{C,r_s}^{n}}{\Delta t} &=\left( k a^2 \omega_R \frac{\Delta'^{\ n}_{C}(W^n)}{W^n}\Psi_{C,r_s}^n-\Omega \Psi_{S,r_s}^n  \right ) \\
\Psi_{C,r_s}^{n+1} &=\Psi_{C,r_s}^{n}+\Delta t \left( k a^2 \omega_R \frac{\Delta'^{\ n}_C(W^{n})}{W^{n}}\Psi_{C,r_s}^{n}-\Omega \Psi_{S,r_s}^{n} \right )  \\
\Psi_{C,r_s}^{n+1} &=\Psi_{C,r_s}^{n}\left( 1 + \Delta t  k a^2 \omega_R \frac{\Delta'^{\ n}_C(W^{n})}{W^{n}}\right) - \Delta t \Omega \Psi_{S,r_s}^{n}   \\
\Psi_{C,r_s}^{n+1} &=\Psi_{C,r_s}^{n}\left( 1 + \zeta \frac{\Delta'^{\ n}_C(W^{n})}{W^{n}}\right) - \eta \Psi_{S,r_s}^{n}   \\
\end{split}
\end{equation}
and
\begin{equation} \label{eq3}
\begin{split}
\Psi_{S,r_s}^{n+1} &=\Psi_{S,r_s}^{n}\left( 1 + \zeta \frac{\Delta'^{\ n}_S(W^{n})}{W^{n}}\right) + \eta \Psi_{C,r_s}^{n}   \\
\end{split}
\end{equation}
where

\begin{equation} \label{eq3}
\begin{split}
 & W(r_s) = 4 \sqrt{\frac{\sqrt{\Psi^2_{C,r_s} + \Psi^2_{S,r_s}}}{-r_sB_T\mu'(r_s)/R} }\\
  & \Delta'_{C,S}(W)=\frac{\Psi'_{C,S}(r_s+W/2)-\Psi'_{C,S}(r_s-W/2)}{\Psi_{C,S}} \\
  & \zeta = \Delta t k a^2 \omega_R \\
  & \eta =  \Delta t \Omega \\
\end{split}
\end{equation}

Note that the prime symbol ($'$) is the radial derivative.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Adaptive Radial Domain Discretization}
As previously mentioned, the radial domain is solved over three regions: the region inside the resonant surface ($0<r<r_s-W/2$), the resonant surface ($r_s-W/2<r<r_s+W/2$ and the region outside ($r_s+W/2<r<b$).  The middle region is assumed to have constant $\Psi$ throughout, and only the center point (at $r_s$) is evolved in time.  This in turn sets the boundary conditions for the BVPs on either side of it.

The radial domain, $0 \leq r \leq b$, is discretized into $N$ grid points with uniform spacing, $\Delta r$.  However, the total number of points within each sub-domain is allowed to change as the width of the island grows and shrinks.  For example, if the island grows by 10 grid points, the center domain is expanded by 10 grid points, and inner and outer domains each contract by 5 grid points.  The advantage to solving it this way is that the $A$ matrix for the inner and outer sub-domains only needs to be partially resolved.  Were I to hold a fixed number of points in each of the three domains, each $A$ matrix would have to be fully resolved.  The downside to this method is that the resolution for each sub-domain changes with the island growth.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Initial Conditions}

The 2003 T-10 paper is a little vague in many regards but especially when it comes to its ICs.  Fortunately, a 2014 T-10 paper provides more details, but it is unclear if they are the same ICs that the 2003 paper used.  Below, I summarize the ICs that I'm using.

\begin{equation} \label{ICConstants}
\begin{split}
m &= 2 \\
n &= 1 \\
R &= 1.5 \ \text{m}\\
B_T &= 2.5 \ \text{T} \\
I_p &= 250e3 \ \text{A}\\
\Omega & =1e3 \ \text{Hz} \cdot 2 \pi \\
\omega_r & = 1/0.01 \ \text{Hz} \\
k &= \pi \\
b = r_{wall} & = 0.39 \ \text{m} \\
a = r_{limiter} & = 0.27 \ \text{m} \\
q(0) &= 0.7/0.85 \\
q(a) = q(r_{limiter}) &= 2.4 \\
B(b) = B(r_{wall}) & = 5e-4 \ \text{T}
\end{split}
\end{equation}

The functions used for $q$ and $j$ were unclear in the 2003 paper.  In fact, it states ``The plasma current pofile used in calculatio nwas adjusted to produce the saturated m=2 mode amplitude of 5e-4 T without the external helical current'' which isn't very helpful.  The 2014 paper suggests some possibilities.

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
j(r)=j(0) \left[ 1-(r/a)^2 \right]^l
\end{split}
\end{equation}

\noindent where

\begin{equation} \label{j}
\begin{split}
l=\left[ q(a)/q(0)\right]-1 = 1.914
\end{split}
\end{equation}

Because $j(0)$ was not provided but $I_p$ was, the above model for $j(r)$ was used, integrated $I_p = 2\pi \int_0^b j(r)r \ dr$ and compared with the provided $I_p$ until the correct $j(0)$ was found.  This provided the following profile and its derivative.

\begin{figure}[htb]
	\includegraphics[width=15cm]{images/jAndQ.png}
	\caption{Current and q profile
		\label{fig:jAndQ}}
\end{figure}

\noindent Possibly $q(0)$ and $q(a)$ could be adjusted to get slightly different current profiles.

The q-profile is a little more difficult.  The 2003 paper made no mention other than constraining $q(0)$ and $q(a)$.  The 2014 paper suggested the cylindrical approximation

\begin{equation} \label{qCylindrical}
\begin{split}
q_{cyl}(r\leq a)=\frac{2(l+1)B_T}{\mu_0 j(0) R} \frac{(r/a)^2}{1-\left[ 1-(r/a)^2\right]^{l+1}}.
\end{split}
\end{equation}
However, this equation is not valid for $r>a$.  To solve for $q(r>a)$, I combined $\oint B_{\theta} dl = \mu_0 I_p$ and $q=\frac{rB_T}{RB_{\theta}}$ to get
\begin{equation} \label{qCylindrical}
\begin{split}
q(r>a)=\frac{2\pi r^2B_T}{R\mu_0I_p}
\end{split}
\end{equation}
Fig.~\ref{fig:jAndQ}b shows $q(r)$ and the radial derivative of $1/q(r)$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Feedforward Results}

My first attempt to reproduce Chudnovskiy's work \cite{2003} is to reproduce his Figure 9.  Figure~\ref{fig:FFResults} below shows my reproduction.  There are some differences between the two, but I believe most of these are due to differences in initial conditions as these were not thoroughly documented in Chudnovskiy's work \cite{2003}.

\begin{figure}[htb]
	\includegraphics[width=15cm]{images/feedforwardResults.png}
	\caption{Feedforward results
		\label{fig:FFResults}}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Feedback Results}

Similar to the feedforward results, my feedback results look very similar to Chudnovskiy's work \cite{2003}, but the amplitudes are not quite correct.

\begin{figure}[htb]
	\includegraphics[width=15cm]{images/feedbackResults.png}
	\caption{Feedback results
		\label{fig:FFResults}}
\end{figure}

% %\bibliographystyle{plain}
% %\bibliography{bibFile.bib}
\bibliographystyle{apsrev}% ,isbn=false %[isbn=false]
\begin{thebibliography}{}

\bibitem{2003}
A. Chudnovskiy, Y. Gvozdkov, N. Ivanov, et. al.,
\emph{Nuclear Fusion}\href{http://iopscience.iop.org/article/10.1088/0029-5515/43/8/307/meta}{
{\bf 43} (2003).
}

\bibitem{2014}
N. V. Ivanov, A.M. Kakurin,
\emph{Physics of Plasmas}\href{https://aip.scitation.org/doi/10.1063/1.4897174}{
{\bf 21}, 102502 (2014).
}

\end{thebibliography}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Appendix A - Nondimensionalizing the BVP}

Presently, these nondimensional equations have not been implemented in the code.

We start by nondimensionalizing our variables and input parameters.

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\phi&\equiv\frac{\Psi}{\Psi_0} \\
\rho&\equiv\frac{r}{r_0} \\
\Gamma&\equiv\frac{J}{J_1}
\end{split}
\end{equation}
and other terms

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\delta(r-a) & = \delta(r_0 \rho - a) \\
& = \delta\left(r_0(\rho-\frac{a}{r_0})\right)\\
& = \frac{\delta\left(\rho-\frac{a}{r_0}\right)}{r_0}\\
& = \frac{\delta\left(\rho-1\right)}{r_0}\\
\end{split}
\end{equation}
where
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
r_0 \equiv a
\end{split}
\end{equation}

Also,
\begin{equation} \label{}
\begin{split}
\alpha&=\left( \frac{m^2}{r} +\frac{\mu_0 R}{B_T} \frac{\partial j}{\partial r}\frac{1}{\mu-n/m} \right) \\
&=\left( \frac{m^2}{r_0 \rho} +\frac{\mu_0 R j_0}{B_T r_0} \frac{\partial j^*}{\partial \rho}\frac{1}{\mu-n/m} \right) \\
&=\frac{1}{r_0}\left( \frac{m^2}{ \rho} +\frac{\mu_0 R j_0}{B_T} \frac{\partial j^*}{\partial \rho}\frac{1}{\mu-n/m} \right) \\
&=\frac{1}{r_0}\left( \frac{\alpha^*_1}{ \rho} +\frac{\partial j^*}{\partial \rho} \right) \\%&=\frac{\alpha^*}{r_0}
\end{split}
\end{equation}
where
%\begin{equation} \label{wessonCurrentProfile}
%\begin{split}
%\alpha^*(\rho)& \equiv \left( \frac{m^2}{ \rho} +\frac{\mu_0 R}{B_T} \frac{\partial j}{\partial \rho}\frac{1}{\mu-n/m} \right) \\
%\end{split}
%\end{equation}
\begin{equation} \label{}
\begin{split}
\alpha_1^* \equiv m^2
\end{split}
\end{equation}
and
\begin{equation} \label{}
\begin{split}
j_0 \equiv  \frac{B_T}{\mu_0 R} \left( \mu-n/m \right) \\
\end{split}
\end{equation}

Starting on the BVP, we get

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
 \frac{\partial \Psi}{\partial r} +r   \frac{\partial^2 \Psi}{\partial r^2} - \alpha \Psi & = - \mu_0r  \frac{Jm}{2a} \delta (r-a)\\
 \frac{\Psi_0}{r_0} \frac{\partial \phi}{\partial \rho} + r_0 \rho \frac{\Psi_0}{r_0^2}   \frac{\partial^2 \phi}{\partial \rho^2} - \frac{1}{r_0}\left( \frac{\alpha^*_1}{ \rho} +\frac{\partial j^*}{\partial \rho} \right) \Psi_0 \phi & = - \mu_0 r_0 \rho \frac{J_1 \Gamma}{2a} \frac{\delta (\rho - 1)}{a}\\
\frac{\partial \phi}{\partial \rho} + \rho   \frac{\partial^2 \phi}{\partial \rho^2} - \left( \frac{\alpha^*_1}{ \rho} +\frac{\partial j^*}{\partial \rho} \right) \phi & = - \frac{\mu_0 \rho \Gamma J_1 m}{2 \Psi_0} \delta (\rho - 1)\\
\frac{\partial \phi}{\partial \rho} + \rho   \frac{\partial^2 \phi}{\partial \rho^2} - \left( \frac{\alpha^*_1}{ \rho} +\frac{\partial j^*}{\partial \rho} \right)\phi & = - \rho \Gamma \delta (\rho - 1)\\
\end{split}
\end{equation}
where
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
J_1 \equiv 1 \\
\end{split}
\end{equation}
and
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\Psi_0 & \equiv  \frac{\mu_0 J_1 m}{2}
\end{split}
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Appendix B - Nondimensionalizing the time evolution equation}

We start by nondimensionalizing our variables

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\chi & = \frac{\Psi}{\Psi_1}\\
\tau & = \frac{t}{t_0}\\
\end{split}
\end{equation}
and moving onto several terms

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\Delta'&=\frac{ \left[\frac{\partial \Psi}{\partial r}\right]_{+}-\left[\frac{\partial \Psi}{\partial r}\right]_{-}}{\left.\Psi \right|_{r_s}}\\
& = \frac{\Psi_0}{\Psi_1}\frac{1}{r_0} \left(\frac{ \left[\frac{\partial \phi}{\partial \rho}\right]_{+}-\left[\frac{\partial \phi}{\partial \rho}\right]_{-}}{\chi}\right)\\
& = \frac{\Psi_0}{\Psi_1}\frac{\Delta'^{*} }{a} \\
\end{split}
\end{equation}
where

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\Delta'^*(\phi, \chi)& \equiv \left(\frac{ \left[\frac{\partial \phi}{\partial \rho}\right]_{+}-\left[\frac{\partial \phi}{\partial \rho}\right]_{-}}{\chi}\right)\\
\end{split}
\end{equation}

Also, the width

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
W & = \left. 4 \sqrt{ \frac{\sqrt{\Psi_C^2+\Psi_S^2}}{-r B_T\frac{\partial \mu}{\partial r}/R}} \right|_{r_s}\\
&= 4 \left( \frac{R \Psi_1 \sqrt{\chi_C^2+\chi_S^2}}{-r_0 \rho_s B_T \frac{1}{r_0}\left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}}\right)^{1/2} \\
&= 4 \left( - \frac{R \Psi_1 }{\rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}}\sqrt{\chi_C^2+\chi_S^2}\right)^{1/2}\\
\end{split}
\end{equation}

For convenience, we define
\begin{equation} \label{}
\begin{split}
W &=  \left( -  \frac{16 R \Psi_1 }{\rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}} \right)^{1/2} \left(\chi_C^2+\chi_S^2\right)^{1/4}\\
& \equiv W_0 W^*\\
\end{split}
\end{equation}
where the non-dimensional width is
\begin{equation} \label{}
\begin{split}
W^*\equiv \left(\chi_C^2+\chi_S^2\right)^{1/4}\\
\end{split}
\end{equation}
and the constant
\begin{equation} \label{}
\begin{split}
W_0 \equiv \left( -  \frac{16 R \Psi_1 }{\rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}} \right)^{1/2}
\end{split}
\end{equation}

Finally, the time evolution equation starts by
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\left [  \frac{\partial \Psi_C}{\partial t}\right. & = \left. k a^2 \omega_R \frac{\Delta '}{W}\Psi_C - \Omega \Psi_S \right]_{r_s}\\
\frac{\Psi_1}{t_0}\frac{\partial \chi_C}{\partial \tau} & = k a^2 \omega_R \left(  \frac{\Psi_0}{\Psi_1}\frac{\Delta'^{*} }{a} \right)    \left( - \frac{\rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}}{16 R \Psi_1 \sqrt{\chi_C^2+\chi_S^2} }\right)^{1/2}      \Psi_1 \chi_C - \Omega \Psi_1 \chi_S\\
\frac{\partial \chi_C}{\partial \tau} & =     \left( - \frac{\Psi_0^2 t_0^2 k^2 a^2 \omega_R^2 \rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}}{16 R \Psi_1^3 \sqrt{\chi_C^2+\chi_S^2} }\right)^{1/2}  \Delta'^{*}    \chi_C - \Omega t_0 \chi_S\\
\frac{\partial \chi_C}{\partial \tau} & =     \left(  \frac{1}{\chi_C^2+\chi_S^2} \right)^{1/4}       \left( \left[\frac{\partial \phi_C}{\partial \rho}\right]_{+}-\left[\frac{\partial \phi_C}{\partial \rho}\right]_{-}    \right) - \chi_S\\
\end{split}
\end{equation}
where

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
t_0 \equiv \frac{1}{\Omega}
\end{split}
\end{equation}
and

\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\Psi_1 \equiv  \left( \frac{-\Psi_0^2 t_0^2 k^2 a^2 \omega_R^2 \rho_s B_T \left.\frac{\partial \mu}{\partial \rho}\right|_{\rho_s}}{16R} \right ) ^{1/3}
\end{split}
\end{equation}

Similarly for $\chi_S$,
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
\frac{\partial \chi_S}{\partial \tau} & =     \left(  \frac{1}{\chi_C^2+\chi_S^2} \right)^{1/4}  \left( \left[\frac{\partial \phi_S}{\partial \rho}\right]_{+}-\left[\frac{\partial \phi_S}{\partial \rho}\right]_{-}    \right) + \chi_C\\
\end{split}
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Appendix C - Nondimensionalizing other equations}

The mode measurements at the wall, $B_{C,S}$, are put in terms of non-dimensional terms below without being non-dimensionalized itself.
\begin{equation} \label{wessonCurrentProfile}
\begin{split}
B_{C,S} &= \frac{\partial \Psi_{C,S}}{\partial r} \\
 &= \frac{\Psi_0}{r_0}\frac{\partial \phi_{C,S}}{\partial \rho} \\
\end{split}
\end{equation}

\end{document}