% !TEX root =  paper.tex

\section{Method}
%\begin{figure*}[t!]
%	\begin{center}
%		\subfloat[]{\includegraphics[width=0.24\linewidth]{./figures/methods/multicut_input.png}}
%		\subfloat[]{\includegraphics[width=0.24\linewidth]{./figures/methods/pre-multicut.png}}
% 		\subfloat[]{\includegraphics[width=0.24\linewidth]{./figures/methods/multicut-graph.png}}
% 		\subfloat[]{\includegraphics[width=0.24\linewidth]{./figures/methods/post-multicut.png}}
% 	\end{center}
% 	\caption{Outline of our approach, from left to right: result of the pixel-based segmentation and agglomeration algorithm; segments of several neurons from the initial segmentation; extracted skeletonized network of those segments; improved 3D reconstruction of the selected segments after graph construction and partitioning with constraints.}
% 	\label{fig:overview}
% \end{figure*}

%There are two types of errors that can occur in connectomics segmentations: \textit{split errors} and \textit{merge errors}.
%In a split error, two segments should have been merged into one neuronal process (Fig.~\ref{fig:improved-reconstruction}).
%In a merge error, one segment corresponds to more than one neuron.
%Generally, it is much more difficult to correct merge errors than to correct split errors, as the space of possible split proposals grows quickly~\cite{parag2015properties}.
%Thus, most reconstruction approaches are tuned towards over-segmentation with many more split than merge errors.
%The input of our method is the over-segmentation of EM image volumes from state-of-the-art pipelines.

Our method is illustrated in Fig.~\ref{fig:teaser_pipeline}.
From the input segmentation we generate a graph $G$ with nodes $N$ and edges $E$ with weights $w_e$.
The nodes correspond to labeled segments from the input data with edges between merge candidates.
We propose the following three steps to formulate and then partition the graph while obeying constraints from the underlying biology.
First, we only consider merging segments based on a skeletonized representation of the input segmentation (Fig.~\ref{fig:teaser_pipeline}a).
This enables us to reduce the number of edges in the graph based on prior knowledge on the shape of neuronal processes.
Second, we train a convolutional neural network that learns biological constraints based on the shapes of the input segmentation (Fig.~\ref{fig:teaser_pipeline}b).
This network generates probabilities that segments belong to the same neuron based only on the segmentations.
An example of one such learned constraint is that neurons have small turning radii (Fig.~\ref{fig:turn-radii}).
Third, we partition the graph using a lifted multicut formulation with additional acyclic constraints to enforce global biological constraints (Fig.~\ref{fig:teaser_pipeline}c).
The lifted multicut solution is globally consistent which we then augment to produce a tree-structured graph (i.e., one with no cycles).

\subsection{Skeleton-Based Graph Generation}
%\subsubsection{Node Generation}
\label{sec:skeletonization}
Most region merging methods create a region graph by removing small-sized segments and linking each pair of adjacent segments, which can still lead to a large graph size due to the irregular shape of neural structures.
We use a skeleton representation of the segmentation to reduce the graph size with the geometric constraints on the connectivity of two adjacent segments to prune edges.
% a bit trivial, as everyone is doing this. maybe no need to mention
% \subsubsection{Node Pruning}
% %The simplest node generation strategy creates one node for every unique segment label in the input volume.
% Some labels in the volume correspond to very small structures that are likely the result of segmentation errors, typically in regions with noisy raw image data.
% It is difficult to extract useful shape features from these segments because of their small, often random, shape.
% We prune these nodes from the graph by removing all segments with fewer than a empirical threshold $t_{seg} = 20,000$ voxels.
% This removes on average \TODO{XX}\% of the segments in our  datasets (Sec.~\ref{sec:dataset}), leaving us with around $\TODO{XX}$ nodes per dataset (\TODO{X nodes per micrometer}).
% Despite the large number of segments, these regions only take up \TODO{XX}\% of the total volume on average.

% \subsubsection{Edge Pruning}

\begin{figure}[t]
	\centering
	\begin{minipage}{0.45\linewidth}
		\includegraphics[width=\linewidth]{./figures/skeleton1.png}
	\end{minipage}
	\hfill
	\begin{minipage}{0.45\linewidth}
		\includegraphics[width=\linewidth]{./figures/skeleton2.png}
	\end{minipage}
	\begin{minipage}{0.45\linewidth}
		\includegraphics[width=\linewidth]{./figures/skeleton3.png}
	\end{minipage}
	\hfill
	\begin{minipage}{0.45\linewidth}
		\includegraphics[width=\linewidth]{./figures/skeleton4.png}
	\end{minipage}
	\caption{Example skeletons (in black) extracted from segments using a variant of the TEASER algorithm~\cite{sato2000teasar}. These skeletons not only capture the shape of the segmentation, but also provide endpoints useful for region merging proposals.}
	\label{fig:skeletonization}
\end{figure}

%A typical approach for generating edges produces one between all adjacent segments.
%Two segments $l_1$ and $l_2$ are considered adjacent if there is a pair of adjacent voxels with one labeled $l_1$ and the other labeled $l_2$.
%For example, pixel-based agglomeration methods such as mean agglomeration~\cite{lee2017superhuman} or waterz~\cite{funke2017deep} consider all pairs of adjacent segments for merging.
%However, this method produces too many edges in the graph for graph-based optimization approaches.
Our key observation is that if two segments belong to the same neuronal process, their skeleton end points should satisfy certain geometric constraints.
To extract the skeleton from each segment, we use a variant~\cite{zhao2014automatic} of the TEASER algorithm~\cite{sato2000teasar}.
This skeletonization algorithm repeatedly uses Dijkstra's algorithm to find the farthest voxel from a seed location.
Since this algorithm is non-linear in the number of voxels, we downsample the datasets so that there are voxel samples every $\SI{30}{\nano\meter}$ in each dimension.
Empirically, this reduced the running time for skeletonization by $30\times$ with minimal reduction in skeleton accuracy (${\sim}5\%$ fewer branches).
Fig.~\ref{fig:skeletonization} shows four examples of extracted skeletons (in black).
These skeletons consist of a sequence of \textit{joints}, locations that are locally a maximum distance from the segment boundary, with line segments connecting successive joints.
We refer to joints that have only one connected neighbor as \textit{endpoints}.
We find that approximately 70\% of the segments that are erroneously split have nearby endpoints (Fig.~\ref{fig:merge_candidates}).

Two segments, $s_1$ and $s_2$, receive a corresponding edge if the two following conditions hold.
First, endpoints in either $s_1$ or $s_2$ are within $t_{low}$ nm of any voxel in the other segment.
Second, there are endpoints in $s_1$ and in $s_2$ that are within $t_{high}$ nm of each other.
We store the midpoints between the two endpoints as the center of the potential merge in the set $\mathbb{S}_c$.
This algorithm produces a set of segments to consider for merging.
Only these pairs have a corresponding edge in the constructed graph.
We provide an empirical analysis of these parameters on the structure of the graph in the supplemental materials.

\begin{figure}[t]
	\begin{minipage}{0.4\linewidth}
		\includegraphics[width=\linewidth]{./figures/constraint_error.png}
	\end{minipage}
	\hfill
	\begin{minipage}{0.55\linewidth}
		\includegraphics[width=\linewidth]{./figures/constraint_success.png}
	\end{minipage}
	\caption{Geometric constraints for region merging. We show two region merging proposals. On the left, the segments do not belong to the same neuron, as evidenced by the sharp turning radius indicated by the arrow; while on the right, the segments should be merged due to the continuity of the 3D shape. Instead of using handcrafted geometric features, we train a convolutional neural networks to automatically learn them from the ground truth labels.}
	\label{fig:turn-radii}
\end{figure}
\subsection{Learning-based Edge Weight}
\label{sec:edge-weights}
We assign an edge weight $w_e$ to each edge corresponding to the probability that two nodes belong to the same neuronal process.
We train a 3D CNN model to learn these geometric constraints for valid connection between two segments from labeled volume.
\\~\\
\noindent\textbf{Edge Probability}
To predict the probabilities that two segments belong to the same neuron, we train a feed-forward convolutional network with three \textit{VGG-style} convolution blocks~\cite{chatfield2014return} and two fully connected layers before the final sigmoid activation.

For the input of the network, we extract a cubic region of interest (ROI) around each midpoint $e$ in $\mathbb{S}_c$ as input to the CNN.
The CNN receives three input channels for every voxel in the ROI around segments $l_1$ and $l_2$.
The input in all of the channels is in the set $\{-0.5, 0.5\}$.
The first channel is $0.5$ only if the corresponding voxel has label $l_1$.
The second channel is $0.5$ only if the corresponding voxel has label $l_2$.
The third channel is $0.5$ if the corresponding voxel is either $l_1$ or $l_2$.
We do not use the raw EM image information to avoid the need to retrain the network on datasets that have been stained differently or imaged at different resolution.
This reduces the need for generating costly manually-labeled ground truth.

%The first two max pooling layers are anisotropic with pooling only in the $x$ and $y$ dimensions.
%The output of this final pooling step is flattened into a 1D vector that is input into .
%The final layer produces probabilities with a sigmoid activation function~\cite{funahashi1989approximate}.
%All of the other activation functions are .

% covered in the experiment
%For training we use a stochastic gradient descent optimizer with Nesterov's accelerated gradient~\cite{nesterov1983method}.
%We employ dropouts of $0.2$ after every pooling layer and the first dense layer, and a dropout of $0.5$ after the final dense layer to prevent overfitting.
%We discuss all other network parameters in Sec.~\ref{sec:network-parameters}.

\begin{figure}[t]
	\centering
	\begin{minipage}{0.32\linewidth}
		\includegraphics[width=\linewidth]{./figures/split_error1.png}
	\end{minipage}
	\hfill
	\begin{minipage}{0.32\linewidth}
		\includegraphics[width=\linewidth]{./figures/split_error2.png}
	\end{minipage}
	\hfill
	\begin{minipage}{0.32\linewidth}
		\includegraphics[width=\linewidth]{./figures/split_error3.png}
	\end{minipage}
	\caption{Three erroneously split segments.}
	\label{fig:merge_candidates}
\end{figure}
\subsection{Optimization-Based Graph Partition}
\label{sec:optimization}
After graph construction, we segment the graph in a globally consistent manner while enforcing topological constraints on the output.
\\~\\
%\subsubsection{Lifted Multicut}
\noindent\textbf{Lifted Multicut}
After constructing the graph we seek to partition it into labels where every label corresponds to a neuronal process.
We formulate this graph partitioning problem as a multicut problem.
There are two primary benefits to using a multicut formulation.
First, the number of segments in the final graph is not predetermined but depends on the input.
Second, this minimization produces globally consistent solutions (i.e., a boundary remains only if the two corresponding nodes belong to unique segments)~\cite{keuper2015efficient}.

We apply the algorithms of Keuper et al.~\cite{keuper2015efficient} to produce a feasible solution to the multicut problem using greedy additive edge contraction.
Following their example, we employ the generalized lifted multicut formulation.
Traditional multicut solutions only consider the probabilities that two adjacent nodes belong to the same segment.
In the lifted extension to the problem, we can penalize non-adjacent nodes that belong to different segments.
These penalties between non-adjacent nodes are called lifted edges.
Ideally these lifted weights represent the probability that two nodes belong to the same neuron.
However, determining such probabilities is computationally expensive.
We approximate these probabilities by finding the maximal probable path between any two nodes using Dijkstra's algorithm~\cite{keuper2015efficient}.
This is an underestimate of the probability that two nodes belong to the same neuron since it does not consider all possible paths.
Since our graphs are sufficiently small, we can generate lifted edges between all pairs of nodes.
\\~\\
\noindent\textbf{Edge Weight}
We need to convert the probabilities into the following weighting scheme to solve the multicut problem with this heuristic~\cite{keuper2015efficient,andres2011probabilistic}.
Given the probability that the nodes belong to the same neuron is $p_e$, the edge weight $w_e$ is defined as
%p_e &=\mbox{CNN}(x_1, x_2) \\
\begin{align}
w_e = \log{\frac{p_e}{1 - p_e}} + \log{\frac{1 - \beta}{\beta}},
\end{align}
where $\beta$ is a tunable parameter that encourages over- or under-segmentation.
Since, there are many more lifted edges than adjacent edges, we scale down the lifted weights proportionally to their total number
~\cite{beier2017multicut}.
\\~\\
%\subsubsection{Topological Constraints}
\noindent\textbf{Topological Constraints}
By reformulating the segmentation problem as a graph partitioning one, we can enforce some global constraints on our result based on the underlying biology.
Traditional hierarchical clustering algorithms do not rely on such constraints but consider local decisions independently.
We enforce a global constraint that neurons are tree-structured and should not contain cycles.
The multicut problems returns a series of ``collapsed'' edges between nodes that belong to the same neuron.
We iterate over these edges in order of the probability of merge generated by our CNN.
We ``collapse'' an edge only if it does not create a cycle in the graph.


\section{Objects}
\label{sec:objects}
\texttt{Corryvreckan::Objects} are used to transfer data from and to the clipboard as well as to store it to \textit{root trees}.
\texttt{Objects} are inheriting from \texttt{ROOT::TObject} to automatically create the \texttt{trees} structure required to write and read the objects.
Dedicated modules, see \ref{sec:module_files} are provided to write/read objects.
Three base object types exist within \corry : \pixel, \cluster, \track.
A \track contains several \cluster, which can be connected to a particles
trajectory with different TrackModels.
A \cluster is a collection of hits, that has an additional center of gravity
which is used as cluster position.

In addition several specific TrackModel objects inheriting from Track and a
Spidr-signal are provided. The latter is required to read in data from the
SPIDR timepix3 readout system and described in \cite{vanderHeijden:2275140}.

\subsection{Pixel}
A \pixel contains the basic information of a particle hit from a detector. A column, row position and a time-stamp in nanoseconds as well as a charge information and a raw information is stored. Not every detector can provide all information. If the time-stamp is not provided it should be set to zero. Charge is assumed to be in eV per default, but can be overwritten by using the raw information, which can, for example, be an ADC value or a ToT. If this is also not provided/unknown it should be set to 1.

\subsection{Cluster}
A \cluster is a collection of several \pixel. These \pixel are typically
neighbors in space and close in time, but can be also arbitrary
defined. Every \cluster has a center that is used in \track to reconstruct a trajectory.

\subsection{Track}
A \track holds a collection of \cluster. Additionally, the track positions on
each detector plane defined in the geometry are provided. At any z-position, the
corresponding x/y position can be requested after the track has been fitted
with the track models listed below. After fitting, each track model also holds
a $\chi^2$ defining the quality of the fitted trajectory.

\subsubsection*{Straight-Line}
A straight line track ignores the effect of multiple scattering and describes
the particles path as a straight line. Hit uncertainties are taken into account.
The $\chi^2$ is defined by the resolution weighted sum of residuals $r_p$ in global coordinates:
\begin{equation}
\chi^2 =  \sum_{p=0}^{N} {\frac{r_{p_x}}{\sigma_{p_x}}}^{2} + {\frac{r_{p_y}}{\sigma_{p_y}}}^{2}
\end{equation}

\subsubsection*{General Broken Lines}
A General Broken Lines trajectory \cite{Kleinwort:2012np} includes uncertainties from
both, position measurement and scattering, simultaneously and reconstructs the
trajectory as a set of lines with kinks at the sensors planes. Scattering in
the volume between two planes is approximated by thin virtual scattering
layers close to the detector planes.

\subsubsection*{Multiplet}
Multiplets  split the reference systems in two parts: An upstream and downstream, individually fitted with one of the above track models.
Both tracks are  extrapolated to a user defined `z`-position in between and both, the transverse distance of the tracks ($\bold{d}$ )at the matching position as well as the change in direction (scattering kink) are calculated. The  $\chi^2$ is defined as:
\begin{equation}
	\chi^2 = {\chi^2}_{up} + {\chi^2}_{down} + \sqrt{\bold{d}\cdot\bold{d}}
\end{equation}


\documentclass[english]{../thermomemo/thermomemo}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array}% improves tabular environment.
\usepackage{dcolumn}% also improves tabular environment, with decimal centring.
\usepackage{booktabs}
\usepackage{todonotes}
\presetkeys{todonotes}{inline}{}
\usepackage{subcaption,caption}
\usepackage{xspace}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{snakes}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{mhchem}
\usepackage{siunitx}
%
\usepackage{xcolor}
\hypersetup{
  colorlinks,
  linkcolor={red!50!black},
  citecolor={blue!50!black},
  urlcolor={blue!80!black}
}
%
% Kolonnetyper for array.sty:
\newcolumntype{C}{>{$}c<{$}}
\newcolumntype{L}{>{$}l<{$}}
%
\newcommand*{\unit}[1]{\ensuremath{\,\mathrm{#1}}}
\newcommand*{\uunit}[1]{\ensuremath{\mathrm{#1}}}
%\newcommand*{\od}[3][]{\frac{\mathrm{d}^{#1}#2}{\mathrm{d}{#3}^{#1}}}% ordinary derivative
\newcommand*{\od}[3][]{\frac{\dif^{#1}#2}{\dif{#3}^{#1}}}% ordinary derivative
\newcommand*{\pd}[3][]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative
\newcommand*{\pdc}[3]{\frac{\partial^{2}#1}{\partial{#2}\partial{#3}}}% partial derivative
\newcommand*{\pdt}[3][]{{\partial^{#1}#2}/{\partial{#3}^{#1}}}% partial
                                % derivative for inline use.
\newcommand{\pone}[3]{\frac{\partial #1}{\partial #2}_{#3}}% partial
                                % derivative with information of
                                % constant variables
\newcommand{\ponel}[3]{\frac{\partial #1}{\partial #2}\bigg|_{#3}} % partial derivative with informatio of constant variable. A line is added.
\newcommand{\ptwo}[3]{\frac{\partial^{2} #1}{\partial #2 \partial
    #3}} % partial differential in two different variables
\newcommand{\pdn}[3]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative

% Total derivative:
\newcommand*{\ttd}[2]{\frac{\mathrm{D} #1}{\mathrm{D} #2}}
\newcommand*{\td}[2]{\frac{\mathrm{d} #1}{\mathrm{d} #2}}
\newcommand*{\ddt}{\frac{\partial}{\partial t}}
\newcommand*{\ddx}{\frac{\partial}{\partial x}}
% Vectors etc:
% For Computer Modern:

\DeclareMathAlphabet{\mathsfsl}{OT1}{cmss}{m}{sl}
\renewcommand*{\vec}[1]{\boldsymbol{#1}}%
\newcommand*{\vektor}[1]{\boldsymbol{#1}}%
\newcommand*{\tensor}[1]{\mathsfsl{#1}}% 2. order tensor
\newcommand*{\matr}[1]{\tensor{#1}}% matrix
\renewcommand*{\div}{\boldsymbol{\nabla\cdot}}% divergence
\newcommand*{\grad}{\boldsymbol{\nabla}}% gradient
% fancy differential from Claudio Beccari, TUGboat:
% adjusts spacing automatically
\makeatletter
\newcommand*{\dif}{\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{\mathop{\mathrm{\mathstrut d}}\nolimits^{#1}\gobblesp@ce}
\def\gobblesp@ce{\futurelet\diffarg\opsp@ce}
\def\opsp@ce{%
  \let\DiffSpace\!%
  \ifx\diffarg(%
    \let\DiffSpace\relax
  \else
    \ifx\diffarg[%
      \let\DiffSpace\relax
    \else
      \ifx\diffarg\{%
        \let\DiffSpace\relax
      \fi\fi\fi\DiffSpace}
\makeatother
%
\newcommand*{\me}{\mathrm{e}}% e is not a variable (2.718281828...)
%\newcommand*{\mi}{\mathrm{i}}%  nor i (\sqrt{-1})
\newcommand*{\mpi}{\uppi}% nor pi (3.141592...) (works for for Lucida)
%
% lav tekst-indeks/subscript/pedex
\newcommand*{\ped}[1]{\ensuremath{_{\text{#1}}}}
\newcommand*{\ap}[1]{\ensuremath{^{\text{#1}}}}
\newcommand*{\apr}[1]{\ensuremath{^{\mathrm{#1}}}}
\newcommand*{\pedr}[1]{\ensuremath{_{\mathrm{#1}}}}
%
\newcommand*{\volfrac}{\alpha}% volume fraction
\newcommand*{\surften}{\sigma}% coeff. of surface tension
\newcommand*{\curv}{\kappa}% curvature
\newcommand*{\ls}{\phi}% level-set function
\newcommand*{\ep}{\Phi}% electric potential
\newcommand*{\perm}{\varepsilon}% electric permittivity
\newcommand*{\visc}{\mu}% molecular (dymamic) viscosity
\newcommand*{\kvisc}{\nu}% kinematic viscosity
\newcommand*{\cfl}{C}% CFL number

\newcommand*{\cons}{\vec U}
\newcommand*{\flux}{\vec F}
\newcommand*{\dens}{\rho}
\newcommand*{\svol}{\ensuremath v}
\newcommand*{\temp}{\ensuremath T}
\newcommand*{\vel}{\ensuremath u}
\newcommand*{\mom}{\dens\vel}
\newcommand*{\toten}{\ensuremath E}
\newcommand*{\inten}{\ensuremath e}
\newcommand*{\press}{\ensuremath p}
\renewcommand*{\ss}{\ensuremath a}
\newcommand*{\jac}{\matr A}
%
\newcommand*{\abs}[1]{\lvert#1\rvert}
\newcommand*{\bigabs}[1]{\bigl\lvert#1\bigr\rvert}
\newcommand*{\biggabs}[1]{\biggl\lvert#1\biggr\rvert}
\newcommand*{\norm}[1]{\lVert#1\rVert}
%
\newcommand*{\e}[1]{\times 10^{#1}}
\newcommand*{\ex}[1]{\times 10^{#1}}%shorthand -- for use e.g. in tables
\newcommand*{\exi}[1]{10^{#1}}%shorthand -- for use e.g. in tables
\newcommand*{\nondim}[1]{\ensuremath{\mathit{#1}}}% italic iflg. ISO. (???)
\newcommand*{\rey}{\nondim{Re}}
\newcommand*{\acro}[1]{\textsc{\MakeLowercase{#1}}}%acronyms etc.
\newcommand*{\ousum}[2]{\overset{#1}{\underset{#2}{\sum}}}

\newcommand{\nto}{\ensuremath{\mbox{N}_{\mbox{\scriptsize 2}}}}
\newcommand{\chfire}{\ensuremath{\mbox{CH}_{\mbox{\scriptsize 4}}}}
%\newcommand*{\checked}{\ding{51}}
\newcommand{\coto}{\ensuremath{\text{CO}_{\text{\scriptsize 2}}}}
\newcommand{\celsius}{\ensuremath{^\circ\text{C}}}
\newcommand{\clap}{Clapeyron~}
\newcommand{\subl}{\ensuremath{\text{sub}}}
\newcommand{\spec}{\text{spec}}
\newcommand{\sat}{\text{sat}}
\newcommand{\sol}{\text{sol}}
\newcommand{\liq}{\text{liq}}
\newcommand{\vap}{\text{vap}}
\newcommand{\amb}{\text{amb}}
\newcommand{\tr}{\text{tr}}
\newcommand{\crit}{\text{crit}}
\newcommand{\entr}{\ensuremath{\text{s}}}
\newcommand{\fus}{\text{fus}}
\newcommand{\flash}[1]{\ensuremath{#1\text{-flash}}}
\newcommand{\spce}[2]{\ensuremath{#1\, #2\text{ space}}}
\newcommand{\spanwagner}{\text{Span--Wagner}}
\newcommand{\triplepoint}{\text{TP triple point}}
\newcommand{\wrpt}{\text{with respect to}\xspace}
\newcommand{\excess}{\text{E}\xspace}
\newcommand{\comb}{\text{comb}\xspace}
\newcommand{\FH}{\text{FH}\xspace}
\newcommand{\SG}{\text{SG}\xspace}
\newcommand{\NC}{\text{NC}\xspace}
\newcommand{\NGr}{\text{NG}\xspace}
\newcommand{\res}{\text{R}\xspace}
\newcommand{\scomp}{\text{s}\xspace}
\newcommand{\nsc}{\text{ns}\xspace}

\title{Phase envelope modelling}
\author{Morten Hammer}

\graphicspath{{gfx/}}

\begin{document}
\frontmatter
\tableofcontents
\section{Introduction}
The intention of this memo is to describe the equations used for mapping phase envelopes in thermopack.

\section{Liquid-Vapor envelopes}

The equations:

\begin{align}
   g_i &= \ln K_i + \ln \varphi^\vap_i - \ln \varphi^\liq_i , \quad
   i=1,\dots,n \label{eq:fug_eq}\\
   g_{n+1} &= \overset{n}{\underset{i=1}{\sum}}\left(Y_i-X_i\right), \label{eq:sum_eq} \\
   g_{n+2} &= S - S_\spec \label{eq:spec_eq}.
\end{align}

Relation between overall composition and phase compositions:
\begin{align}
  \label{eq:comp1}
  \vektor{X} &= \frac{\vektor{Z}}{1-\beta+\beta \vektor{K}},\\
  \label{eq:comp2}
  \vektor{Y} &= \frac{\vektor{K}\vektor{Z}}{1-\beta+\beta \vektor{K}}.
\end{align}

Vector form of the equations:
\begin{equation}
  \label{eq:G}
  \vektor{G}\left(\vektor{W}\right) = \begin{pmatrix}
    g_1 \\
    \vdots \\
    g_{n+2}
  \end{pmatrix}
\end{equation}

Variables:
\begin{equation}
  \label{eq:W}
  \vektor{W} = \begin{pmatrix}
    \ln \vektor{K} \\
    \ln T \\
    \ln P
  \end{pmatrix}
\end{equation}
\subsection{Differentials}
The Jacobean matrix needs the following differentials:
\begin{equation}
   \pd{X_i}{\ln K_i} = -\frac{K_iZ_i\beta}{\left(1-\beta+\beta K_i\right)^2} = -\frac{K_iX_i\beta}{\left(1-\beta+\beta K_i\right)} = -\beta\frac{Y_iX_i}{Z_i}.
\end{equation}

\begin{equation}
   \pd{Y_i}{\ln K_i} = -K_i\frac{K_i Z_i\beta}{\left(1-\beta+\beta K_i\right)^2} + K_i\frac{Z_i\beta}{\left(1-\beta+\beta K_i\right)} = -\frac{\left(1-\beta\right)}{\beta}\pd{X_i}{K_i} = \left(1-\beta\right)\frac{Y_iX_i}{Z_i}.
\end{equation}

\begin{equation}
   \pd{g_i}{\ln K_j} = \delta_{ij} + \left(\left(1-\beta\right)\pd{\ln \varphi^\vap_i}{Y_j} + \beta\pd{\ln \varphi^\liq_i}{X_j}\right)\frac{X_jY_j}{Z_j}.
\end{equation}

\begin{equation}
   \pd{g_{n+1}}{\ln K_j} = \frac{X_jY_j}{Z_j}.
\end{equation}

\section{Extension to include solids}

%z = beta*Y + (1-beta-beta_\scomp)*x + beta_\scomp
%y = Kx
% sum(y) = 1
% sum(x) = 1
% phi_\scomp - phi_g*y = 0

Since $\beta$ will vary along the saturation lines, it must be included as a variable. The same applies for $\beta_\sol$. Typically one of these will be fixed to zero when mapping a three-phase line, while the other is a variable.

The equation set must be extended with the following equilibrium relation:
\begin{equation}
  \label{eq:soleq}
   g_{n+3} = \ln \varphi^\vap_\scomp + \ln Y_\scomp - \ln \varphi^\sol .
\end{equation}

Earlier it was assumed, $\beta_\vap = \beta$, and $\beta_\liq = 1-\beta$. There are three options when extending to include solids, (1) to continue to assume this within the vapor-liquid part of the mixture, or (2) to use $\beta_\liq = 1-\beta-\beta_\sol$, or (3) to introduce a new variable for $\beta_\liq$. Since we typically need to specify one of the phase fractions to be zero, only the first and the last option can be used.

For the first option, the corrected fluid composition, $Z_i^*$, becomes,
\begin{equation}
  Z_i^* = \begin{cases}
    \frac{Z_i - \beta_\sol}{1-\beta_\sol},& \text{if } i = s\\
    \frac{Z_i}{1-\beta_\sol},            & \text{otherwise}.
\end{cases}
\label{eq:z_mod}
\end{equation}

For the third option, the new mass balance for the solid component, $Z_\scomp$, becomes:
\begin{equation}
  Z_i = \begin{cases}
    \beta_\vap Y_i + \beta_\liq X_i + \beta_\sol,& \text{if } i = s\\
    \beta_\vap Y_i + \beta_\liq X_i,            & \text{otherwise}.
\end{cases}
\end{equation}
Equation \ref{eq:G} for the fluid equilibrium then changes form completely. To simplify, it is therefore suggested to use the first approach.

Substituting Equation \ref{eq:z_mod} into equations \ref{eq:comp1} we get,
\begin{equation}
  X_i = \begin{cases}
    \frac{Z_i - \beta_\sol}{\left(1-\beta_\sol\right)\left(1-\beta+\beta K_\scomp\right)},& \text{if } i = s\\
    \frac{Z_i}{\left(1-\beta_\sol\right)\left(1-\beta+\beta K_\scomp\right)},            & \text{otherwise}.
\end{cases}
 \end{equation}
To calculate $\vektor{Y}$, we still use, $Y_i=K_iX_i$.
% \begin{align}
%   X_\scomp &= \frac{Z_\scomp -\beta_\sol}{1-\beta-\beta_\sol+\beta K_\scomp},\\
%   Y_\scomp &= K_\scomp\frac{Z_\scomp -\beta_\sol}{1-\beta-\beta_\sol+\beta K_\scomp}.
% \end{align}
To calculate the real gas ($\tilde{\beta}_\vap$) and liquid ($\tilde{\beta}_\liq$) phase fractions, the mass balance for the solid component yields,
\begin{align}
  \tilde{\beta}_\vap &= \frac{Z_{is} - X_{is} +
    \beta_\sol \left( X_{is} - 1\right)}{Y_{is}-X_{is}} \\
  \tilde{\beta}_\liq &= 1 - \tilde{\beta}_\vap - \beta_\sol
\end{align}
\subsection{Additional differentials}
\begin{equation}
   \pd{X_i}{\beta} = -\frac{Z_i^*\left(K_i - 1\right)}{\left(1-\beta+\beta K_i\right)^2} = -\frac{X_i\left(Y_i - X_i\right)}{Z_i^*}.
\end{equation}

\begin{equation}
   \pd{Y_i}{\beta} = -\frac{K_i Z_i^* \left(K_i - 1\right)}{\left(1-\beta+\beta K_i\right)^2} = -\frac{Y_i\left(Y_i - X_i\right)}{Z_i^*}.
\end{equation}

\begin{equation}
  \pd{X_i}{\beta_\sol} = \begin{cases}
    \frac{X_i}{\left(1-\beta_\sol\right)}\left(1-\frac{1}{Z_i^*}\right),& \text{if } i = s\\
    \frac{X_i}{\left(1-\beta_\sol\right)},            & \text{otherwise}.
\end{cases}
 \end{equation}

\begin{equation}
   \pd{Y_i}{\beta_\sol} = K_i\pd{X_i}{\beta_\sol}.
\end{equation}

\begin{align}
   \pd{g_i}{\beta} &= -\overset{n}{\underset{j=1}{\sum}}\left(\pd{\ln \varphi^\vap_i}{Y_j}Y_j - \pd{\ln \varphi^\liq_i}{X_j}X_j\right)\frac{\left(Y_i - X_i\right)}{Z_i^*}\\
   \pd{g_{n+1}}{\beta} &= \overset{n}{\underset{i=1}{\sum}}\left(K_i-1\right)\pd{X_i}{\beta}.
\end{align}
The differential with regards to $\beta_\sol$ will have the same
shape.

\begin{equation}
   \pd{g_{n+3}}{\ln K_i} = \left(\pd{\ln
       \varphi^\vap_\scomp}{Y_i} +
     \frac{\delta_{is}}{Y_\scomp}\right) \pd{Y_i}{\ln K_j} = \left(\pd{\ln
       \varphi^\vap_\scomp}{Y_i} +
     \frac{\delta_{is}}{Y_\scomp}\right) \left(1-\beta\right)\frac{Y_iX_i}{Z_i^*}.
\end{equation}

\begin{equation}
   \pd{g_{n+3}}{\ln T} = T\left(\pd{\ln \varphi^\vap_\scomp}{T} - \pd{\ln \varphi^\sol}{T}\right).
\end{equation}

\begin{equation}
   \pd{g_{n+3}}{\ln P} = P\left(\pd{\ln \varphi^\vap_\scomp}{P} - \pd{\ln \varphi^\sol}{P}\right).
\end{equation}

\begin{equation}
   \pd{g_{n+3}}{\beta} = \overset{n}{\underset{i=1}{\sum}}\pd{\ln \varphi^\vap_\scomp}{Y_i}\pd{Y_i}{\beta} + \frac{1}{Y_\scomp}\pd{Y_\scomp}{\beta}.
\end{equation}

The differential with regards to $\beta_\sol$ will have the same shape.
\subsection{Liquid-solid or vapor-solid equilibrium}
One equilibrium and on specification equation is required,
\begin{align}
   g_{1} &= \ln \varphi^\vap_\scomp + \ln Y_\scomp - \ln \varphi^\sol,\\
   g_{2} &= S - S_\spec \label{eq:spec_eq_ls}.
\end{align}
Variables:
\begin{equation}
  \label{eq:W_ls}
  \vektor{W} = \begin{pmatrix}
    \ln T \\
    \ln P
  \end{pmatrix}
\end{equation}
One of the variables must be specified, and $\beta_\sol$ must be set. The fluid mole fractions then become,
\begin{equation}
  Y_i = \begin{cases}
    \frac{Z_\scomp - \beta_\sol}{1-\beta_\sol}, & \text{if } i = s\\
    \frac{Z_i}{1-\beta_\sol}, & \text{otherwise}.
  \end{cases}
\end{equation}

For $Y_i$ the differentials are,
\begin{equation}
  \pd{Y_i}{\beta_\sol} = \begin{cases}
    \frac{Y_i - 1}{1-\beta_\sol}, & \text{if } i = s\\
    \frac{Y_i}{1-\beta_\sol}, & \text{otherwise}.
  \end{cases}
\end{equation}
Using these the other differentials are simple.

\section{Illustrations}
Figure \ref{fig:envelope} show an example of the phase diagram of a
multicomponent mixture with approximately \SI{91}{\percent} \ce{CO2}.
\begin{figure}[ht]
  \centering
  \includegraphics[width=0.6\textwidth]{envelope}
  \caption{Illustration of a multicomponent mixture
    (\ce{CO2}-\ce{H2}-\ce{N2}-\ce{O2}-\ce{CH4}) with all its phase
    areas. The blue and the black line encircle the vapor-liquid
    region. Above the red line and left of the orange line we have the
    vapor solid region. The green line is the solid appearance line in
    from the liquid phase. Right of the orange and left of the black
    and brown line there is a vapor-liquid-solid line. Between the
    purple and green line there is a liqid solid region. The mole
    fraction vector of the mixture is
    $\left[0.9094,0.0103,0.0402,0.0184,0.0217\right]$}.
  \label{fig:envelope}
\end{figure}

\section{Two-component system}
For a two-component system there is no three phase area, only a three
phase line. This means, that at the same temperature and pressure,
several equilibrium states can be found. The state differ only in
different solid fraction and different fluid phase fractions. That is;
while dry-ice freeze, the phase compositions are constant. The
chemical potential of the component freezing then remain constant, as
seen from Equation \ref{eq:soleq}.

This is illustrated in Figure \ref{fig:envelope2}. Figure
\ref{fig:envelope2} show the phase diagram of a mixture containing
\SI{87.5}{\percent} \ce{CO2} and \SI{12.5}{\percent} \ce{N2}.

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.6\textwidth]{envelope2}
  \caption{Illustration of a binary mixture (\ce{CO2}-\ce{N2}) with all
    its phase areas. The blue and the black line encircle the
    vapor-liquid region. Above the red line and left of the orange
    line we have the vapor solid region. The green line is the solid
    appearance line in from the liquid phase. It is seen that there is
    no three phase area, only a line, as the orange and black line
    coincide. The mole fraction vector of the mixture is
    $\left[0.875,0.125\right]$}.
  \label{fig:envelope2}
\end{figure}

The Gibbs' phase rule, state that the degree of freedom ($F$) is given
as number of components ($C$) and number of phases ($P$),
\begin{equation}
  F = C - P + 2.
\end{equation}
For three phases and two components, the degree of freedom
become 1. That is; the it is not possible to change the temperature
independently of the pressure, giving a three-phase line in
temperature-pressure space.

Applying the same rule to pure \ce{CO2}, no degree of freedom is seen
for the three-phase region, giving a triple point in
temperature-pressure space.

% \clearpage
% \bibliographystyle{plain}
% \bibliography{../thermopack}

\end{document}


\section{Updates}
\index{updates}

\MMonCa\ uses an update mechanism to call update events (to be described later) based on the following parameters:
\begin{description}
\item[time.decade\index{time.decade}] The update event wil be called this number of times per decade of simulated time. For instance, a value of 10 here will update at times 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, ...
\item[time.delta\index{time.delta}] The update event will be called at the specified interval. For instance, for {\tt time.delta}=5, at 5, 10, 15, 20, ...
\item[time.min\index{time.min}] Minimum time to start the updates.
\item[events\index{events}] The update event will be called each {\tt events}.
\end{description}

All the above options work at the same time. For instance, it is possible to have the updates 3 times per decade, and also every 10 seconds.

These update mechanisms are used for the following modules:

\begin{description}
\item[snapshots\index{snapshot}] \MMonCa\ outputs the status the simulation with a frequency specified in the {\tt MC/General/snapshot} update. This update produces two different actions. First, it outputs the current status, and it also calls the {\tt snapshot} procedure. The user can defined a snapshot procedure to extract information, save the time evolution of the simulation, etc. The option {\tt events} does not work for snapshots.
\item[charge model update\index{update!charge model}] The frequency at which the Poisson solver is called is specified in the {\tt MC/Electrostatic/update} parameter.
\item[mechanical update\index{update!mechanical}] The frequency at which the mechanical solver is called and the mechanical information fed into KMC is specified in the {\tt Mechanics/General/update} parameter.
\end{description}


\documentclass[fleqn]{goose-article}

\title{Energy barrier}
\author{Tom de Geus}
\hypersetup{pdfauthor={T.W.J. de Geus}}

\begin{document}

\maketitle

\section*{Protocol}

The protocol is as follows.
\begin{enumerate}
    \item An element is selected for triggering
    (in the example below chosen in the center of the system).
    Its location is denoted by $\vec{r}'$.

    \item A perturbation around a stress- and strain-free configuration is considered.
    To this end, the selected element (only) is subjected to an eigen stress
    $\bm{\sigma}'$.
    The corresponding equilibrium configuration then constitutes to
    the perturbation that will be used.
    It is characterised by the stress field $\delta \vec{u} (\vec{r})$, and corresponding
    stress $\delta \bm{\sigma} (\vec{r})$
    and strain $\delta \bm{\varepsilon} (\vec{r})$ fields.

    \item Two types of perturbations are considered:
    \begin{itemize}
        \item Simple shear:
        $\bm{\sigma}' = \bm{\sigma}'_s = \vec{e}_x \vec{e}_y + \vec{e}_x \vec{e}_y$.
        Gives: $\delta \vec{u}_s (\vec{r})$, $\delta \bm{\sigma}_s (\vec{r})$, and
        $\delta \bm{\varepsilon}_s (\vec{r})$.

        \item Pure shear:
        $\bm{\sigma}' = \bm{\sigma}'_p = \vec{e}_x \vec{e}_x - \vec{e}_y \vec{e}_y$.
        Gives: $\delta \vec{u}_p (\vec{r})$, $\delta \bm{\sigma}_p (\vec{r})$, and
        $\delta \bm{\varepsilon}_p (\vec{r})$.
    \end{itemize}

    For the triggered element the strain (and) stress are empirically
    of the following structure:
    \begin{itemize}
        \item Simple shear perturbation:
        $\delta \bm{\varepsilon}_s (\vec{r}') = \delta \gamma (\vec{e}_x \vec{e}_y + \vec{e}_x \vec{e}_y)$
        \item Pure shear perturbation:
        $\delta \bm{\varepsilon}_p (\vec{r}') = \delta \mathcal{E} (\vec{e}_x \vec{e}_x - \vec{e}_y \vec{e}_y)$
    \end{itemize}

    \item A perturbation $\Delta \vec{u}(\vec{r}) = s \delta \vec{u}_s (\vec{r}) + p \delta \vec{u}_p (\vec{r})$
    is then applied such that the yield surface is reached in the triggered element in such
    a way that the change in potential energy introduced by the perturbation is minimal.

\end{enumerate}

\begin{figure}[htp]
    \centering
    \captionsetup[subfigure]{justification=centering}
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_simple-shear_pos.pdf}
        \subcaption{
            Simple shear:
            $\delta \vec{u}_s (\vec{r})$
        }
        \label{fig:perturbation:simple-shear:pos}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_pure-shear_pos.pdf}
        \subcaption{
            Pure shear:
            $\delta \vec{u}_p (\vec{r})$
        }
        \label{fig:perturbation:pure-shear:pos}
    \end{minipage}
    \\
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_simple-shear_neg.pdf}
        \subcaption{
            Simple shear:
            $- \delta \vec{u}_s (\vec{r})$
        }
        \label{fig:perturbation:simple-shear:neg}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_pure-shear_neg.pdf}
        \subcaption{
            Pure shear:
            $- \delta \vec{u}_p (\vec{r})$
        }
        \label{fig:perturbation:pure-shear:neg}
    \end{minipage}
    \caption{
        Perturbation modes.
        The shown colour is the energy change resulting from the perturbation.
    }
    \label{fig:perturbation}
\end{figure}

\begin{figure}[htp]
    \centering
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_phase-diagram_energy.pdf}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_phase-diagram_energy-contour.pdf}
    \end{minipage}
    \caption{
        Change of internal energy, $\Delta E$, for a perturbation:
        $\Delta \vec{u}(\vec{r}) = s \delta \vec{u}_s (\vec{r}) + p \delta \vec{u}_p (\vec{r})$.
        A contour plot is also shown.
    }
    \label{fig:energy}
\end{figure}

\begin{figure}[htp]
    \centering
    \captionsetup[subfigure]{justification=centering}
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_phase-diagram_sig.pdf}
        \subcaption{
            Equivalent stress.
        }
        \label{fig:phase-diagram:sig}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{perturbation_phase-diagram_eps.pdf}
        \subcaption{
            Equivalent strain.
        }
        \label{fig:phase-diagram:eps}
    \end{minipage}
    \caption{
        Resulting
        \subref{fig:phase-diagram:sig} equivalent stress and
        \subref{fig:phase-diagram:eps} equivalent strain
        for a perturbation:
        $\Delta \vec{u}(\vec{r}) = s \delta \vec{u}_s (\vec{r}) + p \delta \vec{u}_p (\vec{r})$.
    }
    \label{fig:phase-diagram}
\end{figure}

\clearpage

\section*{Exploring the yield surface}

\paragraph{Yield surface}

Initially the strain deviator in the triggered element reads
\begin{equation}
    \bm{\varepsilon}_\mathrm{d}(\vec{r}') =
    \begin{bmatrix}
        \mathcal{E} & \gamma \\
        \gamma & - \mathcal{E}
    \end{bmatrix}
\end{equation}
After triggering the strain deviator is
\begin{equation}
    \bm{\varepsilon}_\mathrm{d}^*(\vec{r}') =
    \begin{bmatrix}
        \mathcal{E} + p \delta \mathcal{E} & \gamma + s \delta \gamma \\
        \gamma + s \delta \gamma & - \mathcal{E} - p \delta \mathcal{E}
    \end{bmatrix}
\end{equation}
To reach the yield surface one thus needs to solve
\begin{equation}
    (\mathcal{E} + p \delta \mathcal{E})^2 +
    (\gamma + s \delta \gamma)^2 =
    \varepsilon_y^2
\end{equation}
for $(s, p)$ (with $\varepsilon_y$ the relevant yield strain).

\paragraph{Change of energy}

The energy in the system reads
\begin{equation}
    E = \frac{1}{2} \int_\Omega
        \bm{\sigma}(\vec{r}) : \bm{\varepsilon}(\vec{r})
    \; \mathrm{d} \Omega
\end{equation}
After triggering:
\begin{equation}
    E^* = \frac{1}{2} \int_\Omega
        (\bm{\sigma}(\vec{r}) + \Delta \bm{\sigma}(\vec{r})) :
        (\bm{\varepsilon}(\vec{r}) + \Delta \bm{\varepsilon}(\vec{r}))
    \; \mathrm{d} \Omega
\end{equation}
where
$\Delta \bm{\sigma}(\vec{r}) = s \delta \bm{\sigma}_s(\vec{r}) + p \delta \bm{\sigma}_p(\vec{r})$
and
$\Delta \bm{\varepsilon}(\vec{r}) = s \delta \bm{\varepsilon}_s(\vec{r}) + p \delta \bm{\varepsilon}_p(\vec{r})$.
It is straightforward to show that the change of energy
\begin{equation}
    \Delta E = E^* - E =
    \int_\Omega
        \big(\bm{\sigma}(\vec{r}) + \tfrac{1}{2} \Delta \bm{\sigma}(\vec{r}) \big) :
        \Delta \bm{\varepsilon}(\vec{r})
    \; \mathrm{d} \Omega
\end{equation}
whereby in practice integration is performed numerically, e.g.\
\begin{equation}
    \Delta E = E^* - E =
    \sum\limits_q
        \delta \Omega_q \;
        \big(\bm{\sigma}_q + \tfrac{1}{2} \Delta \bm{\sigma}_q \big) :
        \Delta \bm{\varepsilon}_q
\end{equation}

\clearpage

\section*{Example}

Two examples are included:
A homogeneous medium that is subjected to shear in \cref{fig:example:shear}, and
the same problem additionally subjected to a vertical perturbation
of the top and bottom boundaries \cref{fig:example:prestress}.

\begin{figure}[htp]
    \centering
    \captionsetup[subfigure]{justification=centering}
    \begin{minipage}[t]{.40\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_shear_config.pdf}
        \subcaption{Initial configuration.}
    \end{minipage}
    \hspace{0.01\textwidth}
    \begin{minipage}[t]{.40\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_shear_config-perturbed.pdf}
        \subcaption{After perturbation.}
    \end{minipage}
    \\
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_shear_phase-diagram_eps.pdf}
        \subcaption{$\varepsilon$}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_shear_phase-diagram_energy.pdf}
        \subcaption{$\Delta E$}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_shear_phase-diagram_energy-contour.pdf}
        \subcaption{$\Delta E$}
    \end{minipage}
    \caption{
        (a--b) Starting and perturbed configuration for a homogeneous sheared system.
        (c--e) Phase diagram of a perturbation
        $\Delta \vec{u}(\vec{r}) = s \delta \vec{u}_s (\vec{r}) + p \delta \vec{u}_p (\vec{r})$
        of the configuration in (a).
        The perturbation is applied based on $(s, p)$ that lie on the yield surface and
        that minimise the increase in potential
        energy, as shown using a red dot.
    }
    \label{fig:example:shear}
\end{figure}

\begin{figure}[htp]
    \centering
    \captionsetup[subfigure]{justification=centering}
    \begin{minipage}[t]{.40\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_prestress_config.pdf}
        \subcaption{Initial configuration.}
    \end{minipage}
    \hspace{0.01\textwidth}
    \begin{minipage}[t]{.40\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_prestress_config-perturbed.pdf}
        \subcaption{After perturbation.}
    \end{minipage}
    \\
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_prestress_phase-diagram_eps.pdf}
        \subcaption{$\varepsilon$}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_prestress_phase-diagram_energy.pdf}
        \subcaption{$\Delta E$}
    \end{minipage}
    \hfill
    \begin{minipage}[t]{.31\textwidth}
        \centering
        \includegraphics[width=\textwidth]{example_prestress_phase-diagram_energy-contour.pdf}
        \subcaption{$\Delta E$}
    \end{minipage}
    \caption{
        (a--b) Starting and perturbed configuration for a homogeneous sheared system.
        (c--e) Phase diagram of a perturbation
        $\Delta \vec{u}(\vec{r}) = s \delta \vec{u}_s (\vec{r}) + p \delta \vec{u}_p (\vec{r})$
        of the configuration in (a).
        The perturbation is applied based on $(s, p)$ that lie on the yield surface and
        that minimise the increase in potential
        energy, as shown using a red dot.
    }
    \label{fig:example:prestress}
\end{figure}

\end{document}


\documentclass{article}

\usepackage{mathrsfs, amsmath}    % need for subequations
\usepackage{verbatim}   % useful for program listings
\usepackage{color}      % use if color is used in text
\usepackage{hyperref}   % use for hypertext links, including those to external documents and URLs

\allowdisplaybreaks

\begin{document}
\section{Decision Trees}
Decision tree learning is a type of supervised learning algorithm, whose goal is to take an object $O$ and assign it a prediction $y$ based on a vector of $M$ features $x_m$. The prediction $y$ can be drawn from a set of discrete classes (classification tree) or a set of continuous numbers (regression tree). A decision tree is a type of graph, in which each node represents an element of the prediction process. Assigned to each node is a feature $x_m$. The local decision process at each node is to decide which child node to travel to based on the value of $x_m$. The terminal nodes of the tree are known as leafs. The leafs are labeled with the possible values of the prediction $y$. Thus by traversing the decision tree from the root node to a terminal leaf each object $O$ can be assigned a prediction. The question becomes how do we train a decision tree to best predict the properties of a given dataset? Another way of formulating this question is, when growing the tree, how can we decide which feature to select at each node for the decision process?

\begin{lstlisting}[language=Python]

class DecisionNode:
    def __init__(self, feature_index=None, threshold=None,
                 leaf_value=None, left_child=None, right_child=None):
        self.feature_index = feature_index
        self.threshold = threshold
        self.leaf_value = leaf_value
        self.left_child = left_child
        self.right_child = right_child
\end{lstlisting}

\section{Splitting Algorithms}
The id3 algorithm is used
\end{document}


\chapter{\GP Performance Testing}
\label{chap:GPtesting}

Before installation all of the \GP fibers were tested for throughput
performance using the Wisconsin Test Stand
\citep{Bershady04,Crause08,Eigenbrot12}. This stand is a
double-differential imaging comparator. It consists of an input stage
that reimages an illuminated aperture through a controllable aperture
at an intermediate pupil, and an output stage that consists of a
collimator that places the output pupil from the reimager or fiber
output onto a CCD detector. The experiment consists of measuring the
input beam differentially between a straight-through configuration and
the collimated fiber output. During the entire process the stability
of the filtered input beam is monitored with a photo diode.

Tests were performed in the Johnson $V$ band with an input beam set to
match the WIYN input beam of \f6.3 without the 17\% central
obstruction of the telescope. The total throughput ($T_{\rm tot}$) is
defined as all of the fiber-output light captured by the CCD (roughly
corresponding to \f2.2, compared with the fiber numerical aperture
of \f2.3)
%From 54mm L3 / 1024px * 0.024mm/px. NA = 0.22 and N = (2*arctan(NA))^-1
divided by the all of the light from the input beam captured by the
same CCD. This gives a good indication of the total transmission
through the fiber, but ignores the effects of FRD on the delivered
throughput on the Bench Spectrograph due to the optical stops
therein. FRD describes the tendency for fibers to increase the entropy
in an optical beam; light injected into a fiber at a particular
$f$-ratio emerges at a smaller (faster) $f$-ratio \citep{Angel77}.

The primary impact of FRD is from light loss from obstructions inside
the Bench Spectrograph as detailed in \cite{Bershady04} and
\cite{Bershady05}.  With the upgrade \citep{Bershady08} the low-order
gratings and camera objective are sufficiently near the pupil and are
of sufficient size that the limiting stop is from the collimator. The
camera objective does begin to vignette for off-axis fields point in
wavelength and pseudo-slit position, but for operational purposes we
consider the on-axis field point for defining throughput losses due
to FRD.  The collimator accepts light up to \f4 with only minimal
obstruction, and is completely unobstructed at \f4.4, while the
optical design (in terms of aberrations) is optimized for \f5.

To characterize the impact of FRD on the the delivered throughput we
define the quantity
\begin{equation}
\label{GPtesting:eq:T_FRD}
  T_{\mathrm{4}} = \frac{F_{\mathrm{out}}(f<\f4)}{F_{\mathrm{in}}(f<\f6.3)},
\end{equation}
where $F_\mathrm{in}$ and $F_\mathrm{out}$ represent flux from the the
input and fiber output beams, respectively. $T_4$ is essentially a
throughput measurement that accounts for the impact of FRD, and should
be representative of how the fibers will perform as part of the
WIYN/Bench system.  We compute comparable quantities for output
f-ratios of \f4.5 and \f5.  The throughput in each of these apertures,
as well as the total throughput are included in Table
\ref{GPtesting:tab:GP_cal_full} as $T_{\rm tot}$, $T_4$, $T_{4.4}$,
$T_{5}$.

\begin{figure*}[htb]
  \centering
  \includegraphics[width=0.4\textwidth]{Appendix/figs/gradpak_map.pdf}
  \includegraphics[width=0.4\textwidth]{Appendix/figs/gradpak_L_map.pdf}
\vskip -0.25in
\caption[\GP throughput and and FRD
losses]{\label{GPtesting:fig:TL_FRD}\fixspacing Maps of laboratory
  measurements of the \GP IFU performance. Throughput
  ($T_{\mathrm{FRD}}$, Equation \ref{GPtesting:eq:T_FRD}) is shown in
  the left-hand panel, while throughput losses ($L_{\mathrm{FRD}}$,
  Equation \ref{GPtesting:eq:L_FRD}), is shown in the right-hand
  panel. Performance values are given in the color scale at the top of
  each panel.}
\end{figure*}

\begin{figure}
  \centering
  \includegraphics[width=0.8\textwidth]{Appendix/figs/gradpak_facefig.pdf}
  \caption[\GP face and polishing
  detail]{\label{GPtesting:fig:gradpak_face}\fixspacing Detail of \GP
    fiber face
    after polishing. The \val{25.4}{\mu m} (0\farcs24) shims between each
    block fiber-size group are visible in the central array. The hole
    in the 5th fiber row from the bottom is a fiber that was broken
    during polishing. The two sky-fiber groups, not shown to scale,
    have locations marked in Figure \ref{891_1:fig:GradPak}.  The
    \val{200}{\mu m} (1\farcs87) sky fibers are not visible in this image
    due to imperfect illumination conditions at the slit end when this
    picture was taken.}
\end{figure}

Figure \ref{GPtesting:fig:TL_FRD} contains throughput measurements for all of
the active \GP fibers. Throughput losses caused by FRD are spatially
coherent and are larger on the top and left (North and East) side of
the IFU. This is likely due to surface scattering at the IFU face
\citep{Eigenbrot12} caused by an uneven polish. Figure
\ref{GPtesting:fig:gradpak_face} shows evidence of this variable polish in the
two sky fiber groups; the SW sky group show significantly worse polish
than the NE group and has a correspondingly lower throughput. It is
important to note that the FRD losses do \emph{not} appear to be
caused by edge fibers pushing against the aluminum structure, as
evidenced by the relatively high throughput seen on the left side of
the IFU. This is consistent with previous studies \citep{Bershady04}
that suspected removal from an IFU molding fixture to be the primary
cause of stress-induced FRD. Variations in polish quality appears to
be caused by detritus from the aluminum fixture.

As a check on the lab measurements of $T_4$ we also compare total
fiber transmission recorded during the observing program described in
\S\ref{891_1:sec:obs}. For these measurements a stitched dome flat (see
\S\ref{891_1:sec:flats} was used as a good approximation of a uniform
illumination at the fiber input. The total light transmitted by each
fiber is computed by adding together all wavelength channels for each
fiber after the data were spectrally extracted. Figure
\ref{GPtesting:fig:count_tput} shows are comparison between lab
($T_4$) and on-telescope performance. ``Counts'' in this figure are
from dome-flats combined as described in \S\ref{891_1:sec:flats}), and
are the sum across all wavelengths of the extracted fiber traces.  As
expected from Figure \ref{GPtesting:fig:TL_FRD} the highest
throughputs are found in the middle of the array, with a gradual drop
off in performance towards the end of the slit. The right panel of
Figure \ref{GPtesting:fig:count_tput} shows a tight correlation
between our lab measurements and the on-sky performance. This shows
that stresses during installation and the performance of the Bench
Spectrograph only cause a $\pm$5\% rms modulation in the throughput
compared to what was measured in the lab.

We also measure the magnitude of FRD experienced by each
fiber. Because FRD represents a scattering of input light to larger
output angles a comparison between throughput at two different output
$f$-ratios gives an approximation of the severity of FRD in each
fiber. We define
\begin{equation}
\label{GPtesting:eq:L_FRD}
  L_\mathrm{FRD} = 1 - \frac{T_5}{T_4},
\end{equation}
which quantifies the amount of light scattered to smaller $f$-ratios
(larger angles) than \f5 as a measure of the severity of FRD
experienced by each fiber.

\begin{figure*}
  \centering
  \includegraphics[width=\textwidth]{Appendix/figs/gradpak_count_plots.pdf}
  \caption[\GP on-bench throughput
  performance]{\label{GPtesting:fig:count_tput}\fixspacing Left:
    Relative fiber
    transmission measured \emph{in-situ} on the WIYN Bench
    Spectrograph. Vertical lines demark transition between different
    fiber sizes, as labeled. Right: Comparison between the
    \emph{in-situ} performance and $T_4$, as measured in the lab after
    construction. The dashed line represents a linear regression to
    the data with given correlation coefficient and scatter. Points
    are color-coded by fiber number (slit position), and sky fibers
    are marked with black squares.}
\end{figure*}

\begin{figure*}
  \centering
  \includegraphics[width=\textwidth]{Appendix/figs/gradpak_Lplots.pdf}
  \caption[\GP on-bench FRD
  losses]{\label{GPtesting:fig:FRD_loss}\fixspacing Performance
    metrics for \GP. $L_\mathrm{FRD}$ (Equation
    \ref{GPtesting:eq:L_FRD}) as a function of slit location (right)
    and $T_4$ (transmission through an \f4 aperture). Points are
    color-coded by fiber number (slit position), and sky fibers are
    marked with black squares.}
\end{figure*}

Table \ref{GPtesting:tab:GP_cal_full} contains measurements and Figure
\ref{GPtesting:fig:TL_FRD} shows a map of $L_\mathrm{FRD}$ for each
\GP fiber. Figure \ref{GPtesting:fig:FRD_loss} compares this quantity
with $T_4$. We find that fibers with low $T_4$ also tend to have high
FRD losses ($L_\mathrm{FRD}$), which indicates that FRD is a
significant source of throughput loss in the \GP and Bench
Spectrograph system.  However this is relatively more pronounced for
smaller fiber sizes, as seen in the bifurcation in the right-hand
panel of Figure \ref{GPtesting:fig:FRD_loss}. Smaller fibers tend to
suffer from larger amounts of FRD, which may have been caused in \GP
due to handling-induced stresses; these fibers were more likely to
bend and tangle during construction.  The corollary is that lower
throughput in larger fibers isn't always the result of increased FRD.

A complete set of measurements showing FRD losses as a function of
output $f$-ratio for each fiber can be found in the supplemental
materials online at \url{www.astro.wisc.edu/~eigenbrot/PAK}.

% \input{gradpak_lab_table}

\bibliographystyle{thesis}
\bibliography{ms_n891_paper}

\chapter{Grating Optimization}
\label{chap:grating}

\begin{figure*}[htb]
\centering
\vskip -1.25in
  \includegraphics[width=\textwidth]{Appendix/figs/blaze_comp_land.pdf}
\vskip -1.25in
\caption[NGC 891 observing program grating
optimization]{\label{fig:grating_comp}\fixspacing Efficiency
  comparison between 400@4.2 and 600@10.1 gratings based on dome-flat
  exposures using the same fibers, spectrograph camera-collimator
  angle, and lamp temperature and intensity. Details are provided in
  text. The top panel shows counts for each grating, while the bottom
  panel shows their ratio and the prediction (line) based on the blaze
  functions for idealized gratings.}
\end{figure*}

% inferring camera fl. of 277.1mm,
% 400@4.2 with alpha=21.8 covers 3597-7867 A; blaze wave is 3496 A.
% 400@4.2 with alpha=21.53 covers 3372-7640 A; blaze wave is 3496 A.
% 600@10.1  with alpha=24.33 covers 3795-6657; blaze wave is 5666 A.

% Notes: effective camera fl at central waves of 5524 (g600) and 5733
% (g400) is 277.1 mm compared to nominal value of 285 mm. This is due to
% chromatic behvior of all-refractive camera and the ned to
% significantly modify focus for good image-quality across our primary
% bandpass.

% The band-pass in the plots can be shifted redward or bluerward with a
% commensurate shift (or offset) in the camera-collimator angle
% w.r.t. what is plotted here.

We chose the 400@4.2 grating out of the library of gratings available
on the WIYN Bench Spectrograph because it allows us to capture
simultaneously spectra from Ca H\&K to \Ha while still maintaining
relatively high efficiency at the blue end of the spectrum.  To
optimize our grating choice we compared the 400@4.2 grating to the
600@10.1 grating. The latter provides a narrower wavelength range but
at higher resolution, and, most importantly, the covered range is
marginally sufficient to meet our scientific objectives.  The 600@10.1
grating is often the ``go to'' grating for low-resolution programs
centered around \val{5500}{\AA}, particularly because it is the newest
reflection grating and purportedly has the highest diffraction
efficiency.

For testing during engineering time we compared the 400@4.2 grating
set to 21.8$^{\circ}$ and the 600@10.1 grating set to 24.33$^{\circ}$,
both for a fixed camera-collimator angle of 30$^{\circ}$ (this is the
nominal configuration for low-order gratings).\footnote{At these
  wavelengths the effective camera focal-length for this
  all-refractive compound optic is 277.1 mm not the nominal 285 mm
  quoted in reference manuals.} These grating incidence angles gave
wavelength ranges of \val{3600}{\AA}$< \lambda <$ \val{7867}{\AA} and
\val{3794}{\AA}$< \lambda <$ \val{6655}{\AA}, respectively. The blue
Hydra cable (300 $\mu$m core fibers) was used to observe dome flats
illuminated with identical lamp intensity (and temperature)
settings. The dome lamps are known to be stable to better than 10\%
over a broad wavelength range. Although we were concerned that
temperature instability might be a factor at the blue end of our
spectral range, our results are consistent with a highly stable
illumination over our full spectral range.

The fiber flux in the dome-flat spectra was measured on the raw
two-dimensional images before extraction. This was done in order to
exclude defocus effects from the fractional flux extracted along the
trace in wavelength, and between the two grating configurations for
which the detailed focus changes with wavelength are different.
Identical fibers and extraction regions were measured in both
configurations. The regions account for a small lateral shift along
the slit due to a slight difference in the alignment angle between
gratings orthogonal to the dispersion axis (this variance is just a
mechanical tolerance in the grating mount). Taking advantage of the
numerous broken Hydra fibers, we identified well-separated
transmitting fibers that had ample separation for extracting ``on''
and ``off'' signal regions (or apertures) at all wavelengths.  These
apertures were 10-11 pixels wide, and were adjacent on the CCD.  The
resulting counts were differenced and then scaled for the
corresponding exposure time and linear dispersion.

Figure \ref{fig:grating_comp} shows the result of this experiment. It
is evident from the top panel that the 400@4.2 has greater efficiency
compared to the 600@10.1 grating blueward of 4700\AA. Since the
spectrograph configurations were identical except for the gratings,
under the assumption that the dome-flat illumination was constant, the
ratio of the two curves is equivalent to the ratio of the grating
blaze functions. As the bottom panel shows, this is very close to what
would be expected from simply computing the theoretical blaze
functions for idealized versions of these two gratings. If anything,
the 400@4.2 appears to have 10\% higher efficiency across all
wavelengths than the idealized case.

\begin{figure*}[htb]
  \centering
\vskip -1.25in
  \includegraphics[width=\textwidth]{Appendix/figs/blaze_plot_land.pdf}
\vskip -1.25in
\caption[Comparison of coverage and blaze for 400 and 600 l/mm
gratings]{\label{fig:spec_config}\fixspacing Wavelength blaze and
  coverage for the Bench Spectrograph and the 400 l/mm grating blazed
  at 4.2 deg (top panel) and the 600 l/mm grating blazed at 10.1 deg
  (bottom panel) as a function of grating incidence angle and
  camera-collimator angle with the central wavelength held
  constant. Upper and lower wavelength limits and the blaze peak are
  marked with the red, blue, and thick-dashed lines, respectively. The
  minimal desired wavelength range between the most blue-shifted Ca-K
  line and most redshifted H$\alpha$ lines (3937-6480\AA; 300-800
  \kms) is shown as the grey shaded region The camera-collimator angle
  is marked with the thin dotted line, and corresponds to the
  right-hand vertical scale; the nominal value of 30 deg is marked
  with a dot.}
\end{figure*}

Since we are working with a bench-mounted spectrograph that in
principle is highly configurable, we also considered if additional
spectrograph layout modification might further optimize
performance. In particular, by altering both the grating (incidence)
angle and the camera-collimator angle, it is possible to change the
blaze wavelength while keeping the wavelength coverage on the detector
roughly constant. The concept is illustrated in \ref{fig:spec_config}.
The effect of increasing the camera-collimator angle for low-blaze
gratings shifts the blaze wavelength to the blue. This is not
desirable for the 400@4.2 grating which already has a very blue blaze
wavelength at the nominal camera-collimator angle of 30$^{\circ}$, but
this is potentially relevant for the 600@10.1 grating. Unfortunately,
significant shifts of the blaze wavelength require very large
camera-collimator angles that are both geometrically impractical on
the existing optical bench and lead to an unacceptable decrease in
spectral coverage. The exercise does conclude that a larger
camera-collimator angle of roughly 40$^{\circ}$ would be preferable
for the 600@10.1 grating, but its performance in the far blue would
still fall short of the 400@4.2 grating.


\section{Operation of the Vending Circuit}

\subsection{Initialization}
\begin{enumerate}
\item The states of the flip flops should reflect the amount of money that has been put into the machine.
\end{enumerate}

\subsection{How to Vend an Item}
\begin{enumerate}
\item We need to select the subtract function on the ALU instead of the add function. This is accomplished by switch number 7 on the board and a quad 2-1 mux (74LS157) that outputs the combinations of selector bits that tell the ALU to either add or subtract. You can call it the selector bit selector if you would like.
\begin{figure}[H]
\centering
\includegraphics[width=12cm]{selector-bit-selector} \\
%\vspace{1em} \hspace{1em}
\caption{The selector bit selector takes advantage of the fact that in the patterns for selecting the ADD and SUBTRACT functions on the ALU, $S_0 = S_4$ and $S_2 = S_3$}. Because of this, we can use only two of the four outputs to select addition (0110) and subtraction (1001).
\label{selector-bit-selector}
\end{figure}

\item Vending should only happen if the price of the item is less than or equal to the amount of money in the machine. We check this by subtracting the price of the item from the dollars in machine and checking the seventh bit of the output. Since we only work with 6 bit numbers, any non-zero value of the seventh bit will mean that an overflow occurred and that there is not enough money in the machine to vend the item. This check, however, should only occur if we are performing subtraction and since we use the same circuit components to do addition and subtraction we need some extra logic to make it work. The circuit shown below is used to determine when a clock pulse should be allowed to reach the flip flops.
\begin{figure}[H]
\centering
\includegraphics[width=12cm]{clock-enable} \\
%\vspace{1em} \hspace{1em}
\caption{This set of gates was chosen because there were three free NAND gates on the board as well as several OR gates. This is definitely not the simplest way to implement this logic, however. A more simplified version that is easier to follow is below in Figure \ref{clock-enable-simplified}.}
\label{clock-enable}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=10cm]{simplified-clock-enable} \\
%\vspace{1em} \hspace{1em}
\caption{The functionality of this circuit is equivalent to the one above in Figure \ref{clock-enable}, but it would have required adding a NOR chip and an AND chip to the board.}
\label{clock-enable-simplified}
\end{figure}
\end{enumerate}


\section{An Introduction to Isabelle}
\label{subsect.an.overview.of.isabelle}

We now provide a brief introduction to the key concepts and syntax of Isabelle/HOL.
Familiar readers may skip to Section~\ref{sect.abstract.convergence}.
A more detailed introduction can be found in the standard tutorial material~\cite{DBLP:books/sp/NipkowK14}.

\paragraph{Syntax of expressions.}

Isabelle/HOL is a logic with a strict, polymorphic, inferred type system.
\emph{Function types} are written $\tau_1 \Rightarrow \tau_2$, and are inhabited by \emph{total} functions, mapping elements of $\tau_1$ to elements of $\tau_2$.
We write $\tau_1 \times \tau_2$ for the \emph{product type} of $\tau_1$ and $\tau_2$, inhabited by pairs of elements of type $\tau_1$ and $\tau_2$, respectively.
In a similar fashion to Standard ML and OCaml, \emph{type operators} are applied to arguments in reverse order, and therefore $\tau\ \isa{list}$ denotes the type of lists of elements of type $\tau$, and $\tau\ \isa{set}$ denotes the type of mathematical (i.e., potentially infinite) sets of type $\tau$.
Type variables are written in lowercase, and preceded with a prime: ${\isacharprime}a \Rightarrow {\isacharprime}a$ denotes the type of a polymorphic identity function, for example.
\emph{Tagged union} types are introduced with the $\isacommand{datatype}$ keyword, with constructors of these types usually written with an initial upper case letter.

In Isabelle/HOL's term language we write $\isa{t} \mathbin{::} \tau$ for a \emph{type ascription}, constraining the type of the term $\isa{t}$ to the type $\tau$.
We write $\lambda{x}.\: t$ for an anonymous function mapping an argument $\isa{x}$ to $\isa{t(x)}$, and write the application of term $\isa{t}$ with function type to an argument $\isa{u}$ as $\isa{t\ u}$, as usual.
Terms of list type are introduced using one of two constructors: the empty list $[\,]$ or `nil', and the infix operator $\isa{\#}$ which is pronounced ``cons'', and which prepends an element to an existing list.
We use $[t_1, \ldots, t_n]$ as syntactic sugar for a list literal, and $\isa{xs} \mathbin{\isacharat} \isa{ys}$ to express the concatenation (appending) of two lists $\isa{xs}$ and $\isa{ys}$.
We write $\{\,\}$ for the empty set, and use usual mathematical notation for set union, disjunction, membership tests, and so on: $\isa{t} \cup \isa{u}$, $\isa{t} \cap \isa{u}$, and $\isa{x} \in \isa{t}$.
We write $t \longrightarrow s$ for logical implication between formulae (terms of type $\isa{bool}$).
Strictly speaking Isabelle is a logical framework, providing a weak meta-logic within which object logics are embedded, including the Isabelle/HOL object logic that we use in this work.
Accordingly, the implication arrow of Isabelle's meta-logic, $\isa{t} \Longrightarrow \isa{u}$, is required in certain contexts over the object-logic implication arrow, $t \longrightarrow s$, already introduced.
However, for purposes of an intuitive understanding, the two forms of implication can be regarded as equivalent by the reader, with the requirement to use one over the other merely being an implementation detail of Isabelle itself.
We will sometimes use the shorthand ${\isasymlbrakk}\isa{H}_1{\isacharsemicolon}\ \ldots{\isacharsemicolon}\ \isa{H}_n{\isasymrbrakk}\ {\isasymLongrightarrow}\ C$ instead of iterated meta-logic implications, i.e., $H_1\ {\isasymLongrightarrow}\ \ldots\ {\isasymLongrightarrow}\ H_n\ {\isasymLongrightarrow}\ C$.

\paragraph{Definitions and theorems.}

New non-recursive definitions are entered into Isabelle's global context using the $\mathbf{definition}$ keyword.
Recursive functions are defined using the $\mathbf{fun}$ keyword, and support pattern matching on their arguments.
All functions are total, and therefore every recursive function must be provably terminating.
The termination proofs in this work are generated automatically by Isabelle itself.

Inductive relations are defined with the $\mathbf{inductive}$ keyword.
For example, the definition
\begin{isabelle}
\isacommand{inductive} only-fives\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ list\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\\
~~~~{\isachardoublequoteopen}only-fives\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ {\isacharbar}\\
~~~~{\isachardoublequoteopen}{\isasymlbrakk}\ only-fives\ xs\ {\isasymrbrakk}\ {\isasymLongrightarrow}\ only-fives {\isacharparenleft}5\#xs{\isacharparenright}{\isachardoublequoteclose}
\end{isabelle}
\noindent
introduces a new constant $\isa{only-fives}$ of type $\isa{nat list} \Rightarrow \isa{bool}$.
The two clauses in the body of the definition enumerate the conditions under which $\isa{only-fives}\ \isa{xs}$ is true, for arbitrary $\isa{xs}$: firstly, $\isa{only-fives}$ is true for the empty list; and secondly, if you know that $\isa{only-fives}\ \isa{xs}$ is true for some $\isa{xs}$, then you can deduce that $\isa{only-fives}\ (5\#\isa{xs})$ (i.e., $\isa{xs}$ prefixed with the number 5) is also true.
Moreover, $\isa{only-fives}\ \isa{xs}$ is true in no other circumstances---it is the \emph{smallest} relation closed under the rules defining it.
In short, the clauses above state that $\isa{only-fives}\ \isa{xs}$ holds exactly in the case where $\isa{xs}$ is a (potentially empty) list containing only repeated copies of the natural number $5$.

Lemmas, theorems, and corollaries can be asserted using the $\isacommand{lemma}$, $\isacommand{theorem}$, and $\isacommand{corollary}$ keywords, respectively.
There is no semantic difference between these keywords in Isabelle.
For example,
\begin{isabelle}
~~~~\isakeyword{assumes}\ \=\kill
\isacommand{theorem} only-fives-concat{\isacharcolon}\\
~~~~\isakeyword{assumes}\>only-fives\ xs \isakeyword{and}\ only-fives\ ys\\
~~~~\isakeyword{shows}\>only-fives (xs \isacharat ys)
\end{isabelle}
\noindent
conjectures that if $\isa{xs}$ and $\isa{ys}$ are both lists of fives, then their concatenation $xs \mathbin{\isacharat} ys$ is also a list of fives.
Isabelle then requires that this claim be proved by using one of its proof methods, for example by induction.
Some proofs can be automated, whilst others require the user to provide explicit reasoning steps.
The theorem is assigned a name, here $\isa{only-fives-concat}$, so that it may be referenced in later proofs.

\paragraph{Locales.}

Lastly, we use \emph{locales}---or local theories~\cite{DBLP:conf/tphol/KammullerWP99,DBLP:conf/types/HaftmannW08}---extensively to structure the proof, as shown in Figure~\ref{fig.proof.structure}.
In programming terms, Isabelle's locales may be thought of as an interface with associated laws that implementations must obey.
In particular, a declaration of the form
\begin{isabelle}
~~~~\isakeyword{assumes}\ \=\kill
\isacommand{locale} semigroup =\\
~~~~\isakeyword{fixes}\>f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\\
~~~~\isakeyword{assumes}\>{\isachardoublequoteopen}f\ x\ (f\ y\ z)\ =\ f\ (f\ x\ y)\ z{\isachardoublequoteclose}
\end{isabelle}
\noindent
introduces a locale, with a fixed, typed constant $\isa{f}$, and a law asserting that $\isa{f}$ is associative.
Functions and constants may now be defined, and theorems conjectured and proved, within the context of the $\isa{semigoup}$ locale, i.e. definitions may be made ``generic'' in a semigroup.
This is indicated syntactically by writing $(\isacommand{in}\ \isa{semigroup})$ before the name of the constant being defined, or the theorem being conjectured, at the point of definition or conjecture.
Any function, constant, or theorem, marked in this way may make reference to $\isa{f}$, or the fact that $\isa{f}$ is associative.
\emph{Interpreting} a locale---such as $\isa{semigroup}$ above---involves providing a concrete implementation of $\isa{f}$ coupled with a proof that the concrete implementation satisfies the associated law, and is akin to implementing an interface.
Once interpreted, all functions, definitions, and theorems made within the $\isa{semigroup}$ locale become available to use for that concrete implementation.
Like interfaces, locales may be extended with new functionality, and may be specialised, by other ``sublocales'', forming a hierarchy.


\documentclass[11pt]{amsart}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{fp}  % Prevents issues with arithmetic overflow.
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage[section]{placeins}  % Prevents figure placement outside of section.
\usetikzlibrary{arrows, fixedpointarithmetic}

\newcommand{\shaft}{\mathrm{shaft}}

\definecolor{matlab1}{rgb}{0, 0.4470, 0.7410}
\definecolor{matlab2}{rgb}{0.8500, 0.3250, 0.0980}
\definecolor{matlab3}{rgb}{0.9290, 0.6940, 0.1250}
\definecolor{matlab4}{rgb}{0.4940, 0.1840, 0.5560}
\definecolor{matlab5}{rgb}{0.4660, 0.6740, 0.1880}
\definecolor{matlab6}{rgb}{0.3010, 0.7450, 0.9330}
\definecolor{matlab7}{rgb}{0.6350, 0.0780, 0.1840}

\title{MEKF}
\author{Makani Technologies LLC}
\date{October 2016\; (DRAFT)}

\begin{document}
\maketitle

\section{States}

\begin{equation}
  C_g^b \approx \hat C_g^b (I + [\psi_{gb}^g]_{\times})
\end{equation}

\begin{equation}
  \vec{b} \approx \hat{b} + \delta \vec{b}
\end{equation}

\section{Propagate}

\begin{equation}
  \delta \vec{\theta} = (\vec{\omega} - \vec{b}) \cdot \Delta t
\end{equation}

\begin{equation}
  \delta q = q(\delta \vec{\theta})
\end{equation}

\begin{equation}
  {q_g^b}_{k|k-1} = {q_g^b}_{k-1|k-1} \star \delta q
\end{equation}

\begin{equation}
  \delta \vec{x} = [\delta \vec{\theta}, \delta \vec{b}]^T
\end{equation}

\begin{equation}
  \mathbf{F} = \begin{bmatrix}
    \mathbf{C}(\delta q) & \Delta t \cdot \mathbf{I} \\
    \mathbf{0}           & \mathbf{I} \\
  \end{bmatrix}
\end{equation}

\begin{equation}
  \mathbf{\hat{x}}_{k+1|k} = \mathbf{F} \mathbf{\hat{x}}_{k|k} +
                             \mathbf{B} \mathbf{w}_{k}
\end{equation}

\begin{equation}
  \mathbf{P} = \mathbf{U} \mathbf{D} \mathbf{U}^T
\end{equation}

\begin{equation}
  \mathbf{F}_k = \frac{\partial \mathbf{f}}{\partial \mathbf{x}} \bigg|_{\mathbf{\hat{x}}_{k-1|k-1}}
\end{equation}

\begin{eqnarray}
  P_{k+1|k} &=& F_k P_{k|k} F_k^T + Q_k \\
            &=& F_k U_{k|k} D_{k|k} U_{k|k}^T F_k^T + B Q_k B^T \\
            &=& W_{k|k} D_{k|k} W_{k|k}^T
\end{eqnarray}

\begin{equation}
  \mathbf{D}_w = \begin{bmatrix}
    \mathbf{D}_{k|k} & \mathbf{0} \\
    \mathbf{0}       & \mathbf{D}_q \\
  \end{bmatrix}
\end{equation}

\begin{equation}
  \mathbf{W} = [\mathbf{F} \mathbf{U}, \mathbf{B}]
\end{equation}

\section{Correct}

\begin{equation}
  h(\mathbf{x}) \equiv C_g^b \vec{v}_g
                \approx \hat{C}_g^b (\mathbf{I} + \delta \vec{\theta}_{\times}) \vec{v}_g
\end{equation}

\begin{equation}
  \frac{\partial h}{\partial \delta \vec{\theta}} = -[\hat{C}_g^b \vec{v}_g]_{\times}
\end{equation}

\end{document}


\section{Simply connected Riemann surfaces}


% Demonstrating how to use Matlab and Latex to place data analysis results directly into a manuscript
% Emily Cooper, Stanford, 2014

\documentclass{article}
\usepackage{times}
\include{variables} % this is where we are including the matlab-generated variables file -- Must run matlab script before typesetting
\begin{document}

% title and author info
\title{How to load results from Matlab analyses into a Latex document}
\author{Emily A. Cooper\\
        Department of Psychology,\\
		Stanford University,\\
		Stanford, CA\\
		USA\\
		eacooper@stanford.edu}
\maketitle

\begin{abstract}
Use the Matlab script matex.m to write out variable definitions in the proper format to variables.tex. Read these into the Latex document using the 'include' command.
\end{abstract}

\section{Introduction}

\section{Methods}

We collected \numSamples{} samples in the \conditionNamesA{} and \conditionNamesB{} conditions.

\section{Results}

We performed a paired t-test to determine if there was a significant difference between the means of the two conditions (\conditionNamesA{} M = \meansA{}, \conditionNamesB{} M = \meansB{}, {\emph{p} = \p{}).

\begin{thebibliography}{9}

\end{thebibliography}

\end{document}


The \eslmod{histogram} module is for collecting scores, fitting
them to expected distributions, and displaying them.

The histogram automatically reallocates its bins as data points
arrive, so the caller only needs to provide some initial guidance
about bin size and ``phase'' (offset of the bins relative to the real
number line).  It accumulates counts in 64-bit unsigned integers, so
it can handle over $10^19$ total counts.  Optionally (and provided
that the caller knows it has enough memory to support this), a
``full'' histogram can be created and used to collect a sorted vector
of raw (unbinned) values.

Various different ways of fitting histogram data to different sorts of
expected distributions are supported, with interfaces to all of
Easel's statistical distribution modules. Data fitting is oriented
toward the case where the values are scores, with high scores being of
the most interest; for instance, routines for obtaining and fitting
the right (high-scoring) tail are provided, but not for the left tail.

Several of the output functions output data as XY data files suitable
for input into the popular and freely available \prog{xmgrace}
graphing program [\url{http://plasma-gate.weizmann.ac.il/Grace/}].

The API for the \eslmod{histogram} module is summarized in
Table~\ref{tbl:histogram_api}.

\begin{table}[hbp]
\begin{center}
{\small
\begin{tabular}{|ll|}\hline
    \apisubhead{Collecting data in an \ccode{ESL\_HISTOGRAM}}\\
\hyperlink{func:esl_histogram_Create()}{\ccode{esl\_histogram\_Create()}} & Create a new \ccode{ESL\_HISTOGRAM}.\\
\hyperlink{func:esl_histogram_CreateFull()}{\ccode{esl\_histogram\_CreateFull()}} & A \ccode{ESL\_HISTOGRAM} to keep all data samples.\\
\hyperlink{func:esl_histogram_Destroy()}{\ccode{esl\_histogram\_Destroy()}} & Frees a \ccode{ESL\_HISTOGRAM}.\\
\hyperlink{func:esl_histogram_Add()}{\ccode{esl\_histogram\_Add()}} & Add a sample to the histogram.\\
    \apisubhead{Declarations about binned data, before fitting}\\
\hyperlink{func:esl_histogram_DeclareCensoring()}{\ccode{esl\_histogram\_DeclareCensoring()}} & Collected data were left-censored.\\
\hyperlink{func:esl_histogram_DeclareRounding()}{\ccode{esl\_histogram\_DeclareRounding()}} & Declare collected data were no more accurate than bins.\\
\hyperlink{func:esl_histogram_SetTail()}{\ccode{esl\_histogram\_SetTail()}} & Declare only tail $>$ some threshold is considered "observed".\\
\hyperlink{func:esl_histogram_SetTailByMass()}{\ccode{esl\_histogram\_SetTailByMass()}} & Declare only right tail mass is considered "observed".\\
    \apisubhead{Accessing raw data samples}\\
\hyperlink{func:esl_histogram_GetRank()}{\ccode{esl\_histogram\_GetRank()}} & Retrieve n'th high score.\\
\hyperlink{func:esl_histogram_GetData()}{\ccode{esl\_histogram\_GetData()}} & Retrieve vector of all raw scores.\\
\hyperlink{func:esl_histogram_GetTail()}{\ccode{esl\_histogram\_GetTail()}} & Retrieve all raw scores above some threshold.\\
\hyperlink{func:esl_histogram_GetTailByMass()}{\ccode{esl\_histogram\_GetTailByMass()}} & Retrieve all raw scores in right tail mass.\\
    \apisubhead{Setting expected counts}\\
\hyperlink{func:esl_histogram_SetExpect()}{\ccode{esl\_histogram\_SetExpect()}} & Set expected counts for complete distribution.\\
\hyperlink{func:esl_histogram_SetExpectedTail()}{\ccode{esl\_histogram\_SetExpectedTail()}} & Set expected counts for right tail.\\
    \apisubhead{Output}\\
\hyperlink{func:esl_histogram_Write()}{\ccode{esl\_histogram\_Write()}} & Print a "pretty" ASCII histogram.\\
\hyperlink{func:esl_histogram_Plot()}{\ccode{esl\_histogram\_Plot()}} & Output a histogram in xmgrace XY format.\\
\hyperlink{func:esl_histogram_PlotSurvival()}{\ccode{esl\_histogram\_PlotSurvival()}} & Output $P(X>x)$ in xmgrace XY format.\\
\hyperlink{func:esl_histogram_PlotQQ()}{\ccode{esl\_histogram\_PlotQQ()}} & Output a Q-Q plot in xmgrace XY format.\\
\hyperlink{func:esl_histogram_Goodness()}{\ccode{esl\_histogram\_Goodness()}} & Evaluate fit between observed, expected. \\
\hline
\end{tabular}
}
\end{center}
\caption{The \eslmod{histogram} API.}
\label{tbl:histogram_api}
\end{table}

\subsection{Example of using the histogram API}

The example code below stores 10,000 samples from a Gumbel
distribution in a histogram, retrieves a vector containing the sorted
samples, fits a Gumbel distribution to that dataset, sets the expected
counts in the histogram, prints the observed and expected counts in an
ASCII histogram, and evaluates the goodness-of-fit.

\input{cexcerpts/histogram_example}

Some points of interest:

\begin{itemize}
\item When the histogram is created, the arguments \ccode(-100, 100, 0.5)
      tell it to bin data into bins of width 0.5, initially
      starting at -100 and ending at 100. This initialization
      is described below (see ``Specifying binning of data values'').

\item Samples are collected one at a time with
  \ccode{esl\_histogram\_Add()}.

\item After the data have been collected in a \emph{full} histogram, a
   vector of sorted raw data values can be retrieved using functions
   like \ccode{esl\_histogram\_GetData()}, and used to fit parameters
   of an expected distribution to the data.

\item In addition to the observed binned counts, you can optionally
   set \emph{expected} binned counts in the histogram by calling
   \ccode{esl\_histogram\_SetExpect()} and providing pointers
   to an appropriate distribution function and its parameters.

\item The \ccode{esl\_histogram\_Print()} function shows an ASCII text
   representation of the observed counts (and expected counts, if set)
   that looks a lot like FASTA's nice histogram output.

\item The \ccode{esl\_histogram\_Goodness()} function compares the
   observed and expected binned counts, and calculates two goodness of
   fit tests: a G-test, and a $\chi^2$ test.
\end{itemize}

\subsection{Specifying binning of data values}

The histogram collects data values into bins. When the histogram is
created, the bin width and the relative offset of the bins is
permanently set, and an initial range is allocated.

For example, the call \ccode{esl\_histogram\_Create(-10, 10, 0.5)}
creates 40 bins of width 0.5 from -10 to 10, with the first bin
collecting scores from $-10 < x \leq -9.5$, and the last bin
collecting scores $9.5 < x \leq 10.0$.

The lower bound of the initialization permanently sets the relative
offset of the bins. That is, \ccode{esl\_histogram\_Create(-10, 10,
0.5)} makes the first bin $-10 < x \leq -9.5$, whereas
\ccode{esl\_histogram\_Create(-10.1, 9.9, 0.5)} makes the first bin
$-10.1 < x \leq -9.6$.

Aside from that, the initial range is only a suggestion. You can add
any real-valued $x$ to the histogram. The histogram will silently
reallocate itself to a wider range as needed.  The ability of a
histogram to store data is effectively unlimited. Up to $2^{64}-1$
(more than $10^{19}$) counts can be collected. The histogram requires 16
bytes of storage per bin, and the number of bins it allocates scales
as $x_{\mbox{max}} - x_{\mbox{min}} / w$.

\subsection{Optional collection of raw data values: full histograms}

Normally a histogram would store only binned counts, so it can
efficiently summarize even very large numbers of samples.

In some cases it is useful to keep a list of the raw data values --
for instance, for more accurate parameter fitting to expected
distributions. This can be done by creating a ``full'' histogram with
\ccode{esl\_histogram\_CreateFull()} instead of
\ccode{esl\_histogram\_Create()}. (The example code above did this,
because it did parameter fitting to the raw data.) After data have
been collected in a full histogram, individual raw values or pointers
to sorted arrays of raw values can be retrieved using the
\ccode{esl\_histogram\_Get*} functions.

A full histogram may require much more memory: about 4 bytes per data
point. You may not want to use full histograms if your problem
involves collecting many ($> 10^9$, say) data points.

\subsection{Different parameter fitting scenarios}

By default, the data you collect are assumed to be \emph{complete}.
You observed all samples; if you fit to any expected distribution, the
expected distribution is assumed to describe the complete data; the
parameters of the expected distribution are to be fitted to an array
of the complete raw data samples; and any goodness of fit test is to
be applied to the complete data. This is the simplest, most obvious
case.

Other situations may arise. In addition to complete data, Easel is
designed to deal with four other cases:

\begin{enumerate}
\item The collected data are complete, and they are fit to a
      distribution that describes the complete data, but parameter
      fitting is done only in the right (highest-scoring) tail. This
      makes parameter fitting focus on the most important,
      high-scoring region of a score distribution, and ignore
      low-scoring outliers.

\item The collected data are complete, but they are fit to a
      distribution that only describes the right (highest scoring)
      tail, and the goodness-of-fit test is only performed on that
      tail. This case arises when we don't know the form of the
      expected distribution for the complete data, but the tail
      follows a predictable decay (an exponential tail, for example).

\item The collected data are left-censored such that no values $<
      \phi$ were recorded in the histogram, but the data are fit to a
      complete distribution that predicts the probability even of the
      censored (unobserved) values. Goodness of fit is only evaluated
      in the observed data. (This case is what is actually meant by
      left-censored data.)

\item The high-scoring right tail of the collected data are fit as the
      \emph{binned} counts in the histogram (not raw sample values) to
      a distribution that describes the tail, such as an
      exponential. This case becomes useful when the raw data values
      have limited precision (because of rounding, for example), which
      can cause numerical problems with parameter fitting to tails.
      Another case where this is useful is when there are so many data
      points that the data must be binned just as a matter of
      practicality (not enough memory to hold a full histogram).
\end{enumerate}

A variety of other situations can be dealt with by using different
combinations of the function calls that deal with these four cases.

\subsubsection{Focusing parameter fitting on the highest scores}

An example of focusing a Gumbel parameter fit on the right half of an
observed distribution:

\input{cexcerpts/histogram_example2}

The key differences from the complete data case are:

\begin{itemize}
\item Only the high-scoring 50\% of the data samples are
      retrieved, by calling
      \ccode{esl\_histogram\_GetTailByMass(h, 0.5, \&xv, \&n, \&z)}.
      This returns \ccode{z}, the number of samples that
      were \emph{censored}.

\item These data are fit to a Gumbel distribution
      as a \emph{left-censored} dataset by calling
      \ccode{esl\_gumbel\_FitCensored(xv, n, z, xv[0], \&mu, \&lambda)}.
\end{itemize}

The expected counts and the goodness of fit tests are still evaluated
for the complete data, even though the fit was performed only on the
highest scores.

\subsubsection{Fitting to a tail distribution}

An example of fitting an exponential tail to the high-scoring 10\% of
a Gumbel-distributed dataset:

\input{cexcerpts/histogram_example3}

The differences to note are:

\begin{itemize}
\item The tail is fit as if it is \emph{complete} data as far
      as the exponential distribution is concerned.

\item As a result, to use the exponential tail to predict expected
      data, we have to keep in mind how much probability mass the tail
      is supposed to predict (here, 10\%), and that
      is provided to
      \ccode{esl\_histogram\_SetExpectedTail()}, which specifically
      calculates expected counts for a tail.
\end{itemize}

\subsubsection{Fitting left-censored data}

Fitting a Gumbel distribution to data that are \emph{truly} left
censored looks a lot like the case where we extracted the high scoring
data for a censored fit:

\input{cexcerpts/histogram_example4}

\subsubsection{Fitting binned data to a tail distribution}

Normally, you want to fit parameters to the actual individual data
samples, not to binned data, because you'll get more accurate results.
An exception can arise when the data samples have limited precision
because they've been rounded off. Most distributions are not sensitive
to this, but some tail densities are, especially those with
singularities ($P(X=x) \rightarrow \infty$) at their origin. In such a
case, a fit to binned data may be superior, especially if you can
match the histogram's bins to the rounding procedure that was used.

The following code shows an example of fitting for samples that were
already rounded up to the nearest integer before adding them to the
histogram:

\input{cexcerpts/histogram_example5}

Issues to note:

\begin{itemize}
\item The \ccode{esl\_histogram\_Create(-100, 100, 1.0)} call
      defined bins that exactly match the rounding procedure
      defined by \ccode{ceil(x)} -- all $x$ that are rounded
      to the same value by \ccode{ceil(x)} would also go in
      the same bin of the histogram.

\item The \ccode{esl\_histogram\_SetTailByMass()} function sets flags
      in the histogram to demarcate the desired tail.  However,
      because the data have been binned, and we can only define the
      tail by a range of bins, it will generally be impossible to
      match the requested tail mass with adequate accuracy; the actual
      tail mass is $\geq$ the requested tail mass. It is returned
      to the caller, and it is the actual mass, not the requested mass,
      that should be used when setting expected counts.

\item The \ccode{esl\_histogram\_SetRounding()} declaration
      sets a flag in the histogram that tells binned parameter
      fitting functions that the origin of the fitted
      density ($\mu$) should be set at the lower bound of the smallest bin,
      rather than the smallest raw data value observed in that
      bin.
\end{itemize}