%-----------------------------------------------------------------------------
% plotfiles
%-----------------------------------------------------------------------------
\section{Plotfiles}

\maestro\ outputs plotfile specifically for visualization and analysis.
Table~\ref{vis:table:plotfile} lists the quantities stored in a plotfile.
Not all of these may be present, dependent on what options were used in
creating the plotfile.

By default, plotfiles store double precision data, but if
\runparam{single\_prec\_plotfiles} {\tt = T} is set, then the data is
converted to single precision before outputting---this is done to
reduce file sizes.

\renewcommand{\arraystretch}{1.3}
{\footnotesize
\begin{center}
\begin{longtable}{|l|p{2.25in}|p{2.5in}|}

\caption{Plotfile quantities}
\label{vis:table:plotfile} \\
%
\hline \multicolumn{1}{|c|}{\textbf{plotfile variable name}} &
       \multicolumn{1}{|c|}{\textbf{description}} &
       \multicolumn{1}{|c|}{\textbf{runtime parameter controlling output}} \\ \hline
\endfirsthead

\multicolumn{3}{c}%
{{\tablename\ \thetable{}---continued}} \\
\hline {plotfile variable name} &
       {description} &
       {runtime parameter controlling output} \\ \hline
\endhead

\multicolumn{3}{|r|}{{\em continued on next page}} \\ \hline
\endfoot

\hline
\endlastfoot

{\tt x\_vel}   & $\ut$  & -- \\
%\hline
{\tt y\_vel}   & $\vt$  & -- \\
%\hline
{\tt z\_vel}   & $\wt$  & 3-d runs only \\
%\hline
{\tt density}  & $\rho$ & -- \\
%\hline
{\tt rhoh}   & $(\rho h)$ & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and \runparam{plot\_h\_with\_use\_tfromp} {\tt = T}) \\
%\hline
{\tt h}      & $(\rho h)/\rho$   & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and {\tt plot\_h\_with\_use\_tfromp = T}) \\
%\hline
{\tt X(*)}   & $(\rho X_k)/\rho$ & \runparam{plot\_spec} \\
%\hline
{\tt tracer} & tracers           & \runparam{plot\_trac} \\
%\hline
{\tt w0\_x}  & $w_0 \er \cdot \ex$ & \runparam{plot\_base} \\
%\hline
{\tt w0\_y}  & $w_0 \er \cdot \ey$ & {\tt plot\_base} \\
%\hline
{\tt w0\_z}  & $w_0 \er \cdot \ez$ & {\tt plot\_base} \\
%\hline
{\tt divw0}  & $\nabla \cdot w_0$  & {\tt plot\_base} \\
%\hline
{\tt rho0}   & $\rho_0$            & {\tt plot\_base} \\
%\hline
{\tt rhoh0}  & $(\rho h)_0$         & {\tt plot\_base} \\
%\hline
{\tt h0}     & $(\rho h)_0/\rho_0$  & {\tt plot\_base} \\
%\hline
{\tt p0}     & $p_0$                & {\tt plot\_base} \\
%\hline
{\tt radial\_velocity}  & $\Ubt \cdot \er + w_0$ & \runparam{spherical} {\tt == 1} \\
%\hline
{\tt circum\_velocity}  & $| \Ubt - (\Ubt \cdot \er) \er |$ &  {\tt spherical == 1}  \\
%\hline
{\tt magvel}              & $| \Ubt + w_0 \er |$  & -- \\
%\hline
{\tt momentum}            & $\rho | \Ubt + w_0 \er |$  & -- \\
%\hline
{\tt vort}                & $| \nabla \times \Ubt |$   & -- \\
%\hline
{\tt S}                   & $S$   & -- \\
%\hline
{\tt rhopert}             & $\rho - \rho_0$  & -- \\
%\hline
{\tt rhohpert}          & $(\rho h) - (\rho h)_0$ & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and {\tt plot\_h\_with\_use\_tfromp = T}) \\
%\hline
{\tt tfromp}              & $T(\rho, p_0, X_k)$  & -- \\
%\hline
{\tt tfromh}            & $T(\rho, h, X_k)$    & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and {\tt plot\_h\_with\_use\_tfromp = T}) \\
%\hline
{\tt deltaT}            & $[T(\rho, h, X_k) - T(\rho, p_0, X_k)]/T(\rho, h, X_k)$  & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and {\tt plot\_h\_with\_use\_tfromp = T}) \\
%\hline
{\tt deltap}            & $|p(\rho,h,X_k) - p_0|/p_0$ & {\tt use\_tfromp = F} or ({\tt use\_tfromp = T} and {\tt plot\_h\_with\_use\_tfromp = T}) \\
%\hline
{\tt tpert}               & $T(\rho,h,X_k) - \overline{T}$ if {\tt use\_tfromp = F}; $T(\rho,p_0,X_k) - \overline{T}$ otherwise & -- \\
%\hline
{\tt Machnumber}          & $| \Ubt + w_0 \er | / c(\rho,p_0,X_k)$ & -- \\
%\hline
{\tt soundspeed}        & $c(\rho,p_0,X_k)$ & \runparam{plot\_cs} \\
%\hline
{\tt deltagamma}          & $\Gamma_1(\rho,p_0,X_k) - \gammabar$ & -- \\
%\hline
{\tt entropy}             & $s(\rho,p_0,X_k)$ & -- \\
%\hline
{\tt entropypert}         & $[s(\rho,p_0,X_k) - \overline{s}]/\overline{s}$ & -- \\
%\hline
{\tt sponge} or {\tt sponge\_fdamp} & $1/(1 + \Delta t \kappa f_\mathrm{damp})$ by default; $f_\mathrm{damp}$ if \runparam{plot\_sponge\_fdamp = T} & -- \\
%\hline
{\tt pi}                 & $\pi$ & -- \\
%\hline
{\tt gpi\_x}           & $\nabla \pi \cdot \ex$ & \runparam{plot\_gpi} \\
%\hline
{\tt gpi\_y}           & $\nabla \pi \cdot \ey$ & {\tt plot\_gpi} \\
%\hline
{\tt gpi\_z}           & $\nabla \pi \cdot \ez$ & {\tt plot\_gpi} \\
%\hline
{\tt pioverp0}         & $\pi / p_0$ & {\tt plot\_base} \\
%\hline
{\tt p0pluspi}         & $p_0 + \pi$ & {\tt plot\_base} \\
%\hline
{\tt omegadot(*)}      & $\dot{\omega}_k$ & \runparam{plot\_omegadot} \\
%\hline
{\tt enucdot}          & $(\rho \Hnuc)/\rho$ & \runparam{plot\_Hnuc} \\
%\hline
{\tt Hext}             & $(\rho \Hext)/\rho$ & \runparam{plot\_Hext} \\
%\hline
{\tt eta\_rho}         & $\etarho$ & \runparam{plot\_eta} \\
%\hline
{\tt thermal}          & $\nabla \cdot \kappa \nabla T$ & \runparam{use\_thermal\_diffusion} \\
%\hline
{\tt conductivity}     & $\kappa(\rho, T, X_k)$ & {\tt use\_thermal\_diffusion} \\
%\hline
{\tt ad\_excess}       & $\nabla - \nabla_\mathrm{ad}$ & \runparam{plot\_ad\_excess} \\
%\hline
{\tt particle\_count}  & number of particles in a cell & \runparam{use\_particles} \\
%\hline
{\tt processor\_number} & processor number containing the cell's data & \runparam{plot\_processors} \\
%\hline
{\tt pi\_divu}         & $\pi \nabla \cdot \tilde{\Ub}$ (a measure of energy conservation) & \runparam{plot\_pidivu} \\
\end{longtable}
\end{center}
}

\renewcommand{\arraystretch}{1.0}

\subsection{Mini vs.\ regular plotfiles}
\label{vis:sec:miniplotfile}

\maestro\ can manage two independent sets of plotfiles.  This allows you to
output the default plotfiles, which contain a lot of variables, sparsely,
and output mini-plotfiles much more frequently.  A mini-plotfile is controlled
by the analogous runtime parameters as the main plotfiles:

\begin{itemize}
\item \runparam{mini\_plot\_int} is the interval in steps between successive plotfiles
\item \runparam{mini\_plot\_deltat} is the interval in time between successive plotfiles
\item \runparam{mini\_plot\_base\_name} is the base name that prefixes the plotfiles.  The
  default is {\tt miniplt}
\end{itemize}

To set the fields that are stored in the mini plotfile, a second set
of runtime parameters is used: \runparam{mini\_plot\_var1}, {\tt
  mini\_plot\_var2}, $\ldots$, {\tt mini\_plot\_var9}.  These can be set to
any of the following:
\begin{itemize}
  \item {\tt "density"}
  \item {\tt "species"}: this gets all of the mass fractions
  \item the name of an individual species in the network (like {\tt "helium-4"})
  \item {\tt "radvel"}: this gets both the radial and circumferential velocity
  \item {\tt "velocity"}: all three componets
  \item {\tt "temperature"}: this is either $T(\rho,p_0)$ or $T(\rho,h)$, depending
    on the value of {\tt use\_tfromp}
  \item {\tt "enuc"}: the nuclear energy generation rate
  \item {\tt "mach"}: the Mach number
\end{itemize}

\subsection{the \amrex\ file format}

\maestro\ stores the plotfile data in a hierarchical directory format,
with each level's data stored in a separate subdirectory.  Some meta-data
is stored in the top-level to help interpret the structure.  The basic format is:
\begin{itemize}
\item {\tt plt00000/}
  \begin{itemize}
    \item {\tt Header}
    \item {\tt job\_info}
    \item {\tt Level\_00/}
      \begin{itemize}
        \item {\tt Cell\_D\_00000}
        \item {\ldots}
        \item {\tt Cell\_H}
      \end{itemize}
    \item {\tt model\_cc\_00000}
    \item {\tt model\_ec\_00000}
  \end{itemize}
\end{itemize}
%
In the main directory, {\tt Header} contains the information required
to interpret the data stored on disk.  We describe this below.  The
{\tt job\_info} file is a plaintext file that contains a lot of
information about the run (where it was run, when it was run, compiler
options, runtime options, etc.).  It is not needed to interpret the
data.  Finally, the {\tt model\_cc\_00000} and {\tt model\_ec\_00000}
contain the \maestro\ basestate information for the cell-centered and
edge-centered basestate quantities respectively.  These files are not
typically used for visualization.

\subsubsection{\tt Header}

The main {\tt Header} is written by {\tt fabio\_ml\_multifab\_write\_d} by
processor 0.  The information contained is the following:
\begin{quote}
NavierStokes-V1.1 \\
{\em number of variables} \\
{\em variable 1 name} \\
{\em variable 2 name} \\
$\vdots$ \\
{\em last variable name} \\
{\em number of dimensions} \\
{\em simulation time} \\
{\em number of levels} (0-based) \\
{\em physical domain minimum coordinate} (dm numbers) \\
{\em physical domain maximum coordinate} (dm numbers) \\
{\em jump in refinement between level 0 and 1} \\
$\vdots$ \\
{\em jump in refinement between level $n-2$ and $n-1$} \\
~\\

\end{quote}

%-----------------------------------------------------------------------------
% amrvis
%-----------------------------------------------------------------------------
\section{Visualizing with \amrvis}

\amrvis\ is a tool developed together with \amrex\ to visualize
datasets from codes built around the \amrex\ library (including
\fboxlib\ used here).  You can download the \amrvis\ source from:
\\ \url{https://ccse.lbl.gov/Downloads/downloadAmrvis.html} \\

\amrvis\ exists in the C++ \amrex\ framework, so the build system is
slightly different.  A different executable is needed for 2- vs.\ 3-d
datasets.  Edit the {\tt GNUmakefile} and set the compilers (probably
{\tt g++} and {\tt gfortran}) and the dimensionality, and turn off any
of the volume rendering options.  You will need to have the Motif library
installed on your system (or a replacement, such as {\tt lesstif}.

Once the code is built, you visualize a dataset as:
\begin{verbatim}
amrvis3d.Linux.g++.gfortran.ex pltfile
\end{verbatim}
where {\tt pltfile} is the name of the plotfile directory.  Different
variables can be selected from the drop down menu at the top.  Middle
and right clicking in 3-d select the slice planes, and shift + middle
or right will extract 1-d lines through the data.  In 2-d, middle and
right clicking alone extract 1-d lines.

If \amrvis\ cannot find the {\tt Palette} file, then the plots will be
in grayscale.  To fix this, copy the {\tt amrvis.defaults} and {\tt
  Palette} files to your home directory and edit {\tt amrvis.defaults} so that
the {\tt palette} line points to the {\tt Palette} file, e.g.:
\begin{verbatim}
palette               /home/username/Palette
\end{verbatim}

%-----------------------------------------------------------------------------
% VisIt
%-----------------------------------------------------------------------------
\section{Visualizing with \visit}

%-----------------------------------------------------------------------------
% data_procesing
%-----------------------------------------------------------------------------
\section{Python visualization scripts}
\label{sec:vis:python}

{\tt AmrPostprocessing/python} provides some simple commandline
tools for doing visualizations of \amrex\ plotfiles (note: a subset
of these are distributed directly with \amrex\ in {\tt amrex/Tools/Py\_util/}).  The main drivers
are written in python and use a set of Fortran routines, compiled with
{\tt f2py} to interface with the plotfile data.  To use the routine,
you will need to have {\tt matplotlib} and {\tt f2py} installed.  On a
machine running Fedora linux, you can install these packages via
\begin{verbatim}
yum install python-matplotlib f2py
\end{verbatim}
%
The library required by the python routines can be built by typing
'{\tt make}' in that directory.  If successful, you should find
a library {\tt fsnapshot.so}.

The path to {\tt fsnapshot.so} should be included in your {\tt PYTHONPATH}
environment variable.  This can be done by adding:
\begin{verbatim}
export PYTHONPATH="${PYTHONPATH}:/home/user/AmrPostprocessing/python}
\end{verbatim}
to your {\tt .bashrc}.

It is recommended that you use {\tt matplotlib} version 1.2.0 or
higher.  If the fonts look strange in the output files, you can try
installing the {\tt lyx-fonts} package and deleting your {\tt
  .matplotlib} directory, and trying again.

\subsection{\tt plotsinglevar.py}

{\tt plotsinglevar.py} does visualizations of 2-d \amrex\ plotfiles,
and slices through 3-d \amrex\ plotfiles.  A simple plot can be made
via:
\begin{verbatim}
plotsinglevar.py --log -o test.png plt00000/ tfromp
\end{verbatim}
This will make a plot of ``{\tt tfromp}'' from the plotfile {\tt plt00000} with log scaling,
and store the output in {\tt test.png}.  See Figure~\ref{fig:python}.
If you don't do `{\tt -o}', then a default output filename consisting of the
plotfile name + component will be used.

\begin{figure}[t]
\centering
\includegraphics[height=0.4\textheight]{\visfigpath/plt00000_tfromp}
\caption[Basic plot of {\tt reacting\_bubble} done with {\tt plotsinglevar.py}]
{\label{fig:python} Plot of {\tt reacting\_bubble} done with the python
  script {\tt plotsinglevar.py}.}
\end{figure}

If you list 2 different variables after the plotfile name, then they
will be plotted side-by-side in a single figure.  For example,
\begin{verbatim}
plotsinglevar.py plt00000/ tfromp enucdot
\end{verbatim}
produces the output shown in figure~\ref{fig:python_two}.

\begin{figure}[t]
\centering
\includegraphics[height=0.4\textheight]{\visfigpath/plt00000_tfromp_enucdot}
\caption[Plot of two variables from {\tt reacting\_bubble} done with {\tt plotsinglevar.py}]
{\label{fig:python_two} Plot of {\tt reacting\_bubble} done with the
  python script {\tt plotsinglevar.py} showing 2 variables plotted
  from a single plotfile.}
\end{figure}

Additional options include `{\tt -m}' to specify the minimum data
value, `{\tt -M}' to specify the maximum data value, and `{\tt --eps}'
to make an EPS plot instead of PNG.  Running the script with no parameters
will give the full list available options.

Limited 3-d support is available.  When run as with a plotfile name
and variable, it will plot slices ($x$-$y$, $x$-$z$, and $y$-$z$)
through the center of the domain.  The option `{\tt --origin}'
will put the slices through the origin.

\subsection{\tt contourcompare.py}

{\tt contourcompare.py} takes two or three plotfiles and a single variable as arguments
and plots contours of the datasets on the same set of axes.  This is
form comparisons of different runs.  Running the script with no parameters
will give the full list available options.

For example:
\begin{verbatim}
contourcompare.py tfromp plt00000 other_plt00000
\end{verbatim}
will make a contour plot of the variable {\tt tfromp} from the data in
{\tt plt00000} and {\tt other\_plt00000} shown on the same axes.

\subsection{\tt runtimevis.py}

The {\tt runtimevis.py} script is designed to be run from a submission
script to produce plots from plotfiles as they are produced.  This is
accomplished by hooking it into the {\tt process} scripts described in
Chapter~\ref{ch:managingjobs}.

The script itself reads in an inputs file, {\tt vis.in}, that
describes the variables to plot.  From 1 to 6 variables can be
plotting from a plotfile.  The script does its best to organize them
in columns and rows to maximize the plot area.  The image is always
output at 1280$\times$720 pixels, corresponding to 720p HD resolution.
For each variable, a block of the form:
\begin{verbatim}
[varname]
min = 1
max = 2
log = 1
\end{verbatim}
is supplied.  If {\tt min} or {\tt max} are omitted, then the data
limits are computed automatically.  If {\tt log} is omitted, then no
log is taken of the data before plotting.  The script is then run as:
\begin{verbatim}
runtimevis.py plt00000
\end{verbatim}

%-----------------------------------------------------------------------------
% yt
%-----------------------------------------------------------------------------
\section{\label{sec:vis:yt} Visualizing with \yt}

\yt is a Python package for analyzing and visualizing simulation data,
and understand that \amrex\ data from \maestro\ and \castro\ (along
with many other simulation codes). For more
information, see the \yt homepage at \url{http://yt-project.org/} and
\cite{yt}.

\noindent {\bf Note: for \maestro\ data, you need to use \yt 3.0 or later.}

Some sample scripts that use \yt\ with \maestro\ data are contained in
{\tt MAESTRO/Util/yt/}.

\subsection{Installing \yt}
The easiest way to obtain \yt is through the use of an installation script:
\begin{lstlisting}[language=bash,mathescape=false]
> wget http://hg.yt-project.org/yt/raw/yt-3.0/doc/install_script.sh
> bash install_script.sh
\end{lstlisting}
By default, this \yt install script will download and install, in its
own isolated environment, all the secondary utilities needed to get
\yt up and running.  Note that this currently includes installing {\it
  hdf5, zlib, bzip2, libpng, freetype, python, numpy, matplotlib,
  mercurial, ipython, h5py, Cython, Forthon} as well as a \yt {\it
  mercurial} bundle of changes.  You can turn off the automatic
installation of any of these particular packages by setting the
appropriate {\tt INST\_}* variable to zero in the install script; you
may have to then change some of *{\tt \_DIR} variables to point to
your own particular installation of that package.  It is usually best
to just let \yt install its own stuff, which ensures things are
working properly.

After the install script has finished, and assuming you let \yt
install its own packages, you'll need to {\it prepend} some
environment variables with \yt locations so that your system finds
those first and stops looking.  \yt provides a simple script to
do this, which will be announced upon successful completion.

%% \begin{lstlisting}[language=bash,mathescape=false]
%% # yt stuff
%% export YT_DEST=~/install/yt/current/yt-x86_64
%% export PATH=${YT_DEST}/bin/:$PATH
%% export PYTHONPATH=${YT_DEST}/lib/python2.6/site-packages/:$PYTHONPATH
%% export LD_LIBRARY_PATH=${YT_DEST}/lib/:$LD_LIBRARY_PATH
%% \end{lstlisting}
%% where I have {\tt ~/install/yt/current} as a soft link to my \yt
%% installation directory.

\subsection{Working with \yt}
The \yt installation provides both an interactive, {\it iPython}-like,
interface or the ability to import \yt modules for use in a batch
script.  The interactive interface should be in your {\tt \$PATH} if
you've followed the instructions in the previous section; to start it,
simply type {\tt iyt} in a terminal.
\begin{lstlisting}[language=Python]
> iyt

Welcome to yt!

In [1]:
\end{lstlisting}
Codes like {\tt Enzo} use what are called {\it parameter files} to
describe the general information---number of levels, domain
dimensions, time, etc.---of a a dataset.  \yt likes to grab this
information before working on any specific data; this is accomplished
via the convenience method \textcolor{blue}{\tt load}:
\begin{lstlisting}[language=Python]
In [1]: pf = load("plt00166")
\end{lstlisting}
Note that some older versions of \yt needed an {\tt inputs} file
in the same directory as the plotfile, but as of \yt 3.0, all the
necessary metadata is obtained from the {\tt job\_info} file inside
the plotfile directory.
The {\tt load} method returns an instance of the {\tt StaticOutput} class.  One
of the easiest ways of handling plots is via a {\tt PlotCollection}
object
\begin{lstlisting}[language=Python]
In [2]: center = (pf.domain_right_edge + pf.domain_left_edge) / 2.0
In [3]: pc = PlotCollection(pf,center)
\end{lstlisting}
By default, the {\tt PlotCollection} constructor places the center of
the plot to be at the location of peak density.  Here we have
calculated the center of the domain by accessing the lower and upper
domain boundaries via the {\tt numpy} arrays {\tt
  pf.domain\_left\_edge} and {\tt pf.domain\_right\_edge},
respectively.  Note that up until this point, \yt has not actually
loaded the AMR dataset.

Now we would like to take some slices of tfromp in the dataset and
generate some 2-d plots.  To do this, we will use the {\tt
  PlotCollection}'s \textcolor{blue}{\tt add\_slice} method:
\begin{lstlisting}[language=Python]
In [4]: pc.add_slice("tfromp",0)
In [5]: pc.add_slice("tfromp",1)
In [6]: pc.add_slice("tfromp",2)
\end{lstlisting}
The first call to \textcolor{blue}{\tt add\_slice} builds an {\tt
  AMRHierarchy} object associated with {\tt pf}.  The {\tt
  AMRHierarchy} object contains information about the actual dataset,
such as its layout in the simulation domain or on disk.  Building the
hierarchy is expensive, but once it is built the data it contains can
be accessed via attributes and dictionary lookup.  In other words, the
subsequent \textcolor{blue}{\tt add\_slice} operations are relatively
cheap.  The first parameter to the \textcolor{blue}{\tt add\_slice}
method is obviously the variable we want; the second (optional)
parameter specifies an coordinate axis orthogonal to the slice being
made---0 for x, 1 for y, 2 for z.  Now we want to save the plots from
the {\tt PlotCollection}; this is done with the \textcolor{blue}{\tt
  save} method, which takes an optional basename for the generated files:
\begin{lstlisting}[language=Python]
In [7]: pc.save("my_cool_images")
\end{lstlisting}
This generates the following image files:
\begin{lstlisting}[language=Python]
Out[7]:
['my_cool_images_Slice_x_tfromp.png',
 'my_cool_images_Slice_y_tfromp.png',
 'my_cool_images_Slice_z_tfromp.png']
\end{lstlisting}
Figure \ref{fig:yt_slice} shows an example of one of the slice images.
Note that this was a quick and dirty generation of the image---there
is a lot of space around the figure, which can be removed with various
options to the \yt methods.  Also, the user can specify derived
variables, log-scaling, annotations, etc. For more information see the
documentation at \url{http://yt-project.org/docs/dev/}\, .

\begin{figure}[!h]\label{fig:yt_slice}
\centering
\includegraphics[height=0.4\textheight]{\visfigpath/my_cool_images_Slice_x_tfromp_trim}
\caption{Example slice through 3-d dataset with \yt.}
\end{figure}

When writing a script to use in batch mode, one has to manually import
the import modules needed to work with \yt.  As such, all scripts must
import from the {\tt yt.mods} module, which is essentially a
convenience module that sets up the appropriate \yt namespace.
%% As an aside, if you want to import a module as another variable in
%% the local namespace---e.g. {\tt import this.module as
%% local\_variable}---then this should be done via the various API's
%% for that particular piece of code.  For example,
For completeness, below is a script containing our example above.
\begin{lstlisting}[language=Python]
# load our namespace
from yt.mods import *

# the plotfile I'm interested in
fn = "plt00166"

# load it into a StaticOutput object
pf = load(fn)

# find the center of the domain
center = (pf.domain_right_edge + pf.domain_left_edge) / 2.0

# associate a PlotCollection with the pf
pc = PlotCollection(pf,center)

# add some slices of tfromp
pc.add_slice("tfromp",0)
pc.add_slice("tfromp",1)
pc.add_slice("tfromp",2)

# save our plots to a files with basename
# "my_cool_images"
pc.save("my_cool_images")
\end{lstlisting}

\subsection{2-d datasets}

To visualize 2-d data, you can use the {\tt SlicePlot} function,
picking the normal direction to be {\tt "z"}:
\begin{lstlisting}
from yt.mods import *
 pf = load("plt00085")
s = SlicePlot(pf, "z", "tfromp")
s.save("tfromp.eps")
\end{lstlisting}
This generates the figure shown in Figure~\ref{fig:yt2d}.

\begin{figure}[!h]
\centering
\includegraphics[height=0.4\textheight]{\visfigpath/tfromp}
\caption{\label{fig:yt2d} Example slice through 3-d dataset with \yt.}
\end{figure}

\subsection{Volume Rendering}


\chapter{Implementation}

In this chapter the implementation details will be described.
For this, first the used technologies are explained.
After this, the database schema that was used for storing the meta-features and the evaluations will be described.
Last but not least the used algorithms, optimizers and features will be mentioned.

\section{Technologies}

\begin{itemize}
    \item Python as programming language
    \item scikit-learn \cite{PedregosaFABIANPEDREGOSA2011Scikit-learn:Perrot} as library for implementing the algorithms and the metrics.
    \item Used algorithms KMeans, KMedoids\cite{Kaufman1987ClusteringMedoids} and GMM.
    Implementations taken from scikit-learn, besides KMedoids which was taken from scikit-learn extra\footnote{https://scikit-learn-extra.readthedocs.io/en/latest/user_guide.html#k-medoids}.
    \item For all algorithms the number of max iterations were set to 10.
    \item KMeans was implemented using the  \textit{k-means++} \cite{Arthur2007K-means++:Seeding} method
    and KMedoids with a similar method, called \textit{k-medoids++}.
    \item The metrics that were used were all that are implemented in scikit-learn, which means 6 external and 3 internal were used.
    \item The Silhouette coefficient \cite{Rousseeuw1987Silhouettes:Analysis} was implemented using only 10\% of the data with random sampling.
    \item The reason is the high runtime which would exceed the possibilities and resources of this work.
    \item For the online phase only the 3 internal metrics were used.
    \item The optimizers that were used were all that were described in ... \xtodo{reference here background section of optimizers}
    \item The implementation of the Random and the Bayes optimizer was done using the scikit-optimize \cite{Head2018Scikit-optimize} implementation.
    \item Reason for this library: in python, easy to add warmstarting, ask-and-tell interface.
    \item The Hyperband and BOHB optimizers were implemented using the implementation mentioned in \cite{Falkner2018BOHB:Scale}, which contains an implementation for both Hyperband and BOHB.
    \item The architecture was kept quite flexible so that running each optimizer which each metric on each implemented algorithm is possible.
    \item Goal is to compare the different optimizers and metrics per optimizer.
    \item Relational database was used because the relationship between the metafeatures, dataset and the results of the optimizers need to be stored.
    \item Also the schema should not change.
    \item PostgreSQL\footnote{https://www.postgresql.org/} used as database.
    \item The meta-feature extraction was done using the pymfe\footnote{https://pypi.org/project/pymfe/} \cite{Rivolli2018TowardsMeta-learning} package.
    \item For this all meta-features were used that do not need class labels.
\end{itemize}

\section{Database Schema}
\begin{itemize}
    \item For accessing the PostgreSQL database instance, the python package SQLAlchemy \cite{sqlalchemy} was used.
    \item More specific, the Object-relational mapper was used.
    \item Schema of the database is shown in \cref{fig:dbschema}.
    Describe in more detail.
    \begin{figure}
        \centering
        \includegraphics[width=0.7\textwidth]{graphics/database_schema.png}
        \caption{Overview of the database schema that was used to store the meta-features as well as the results of the optimizers.}
        \label{fig:dbschema}
    \end{figure}

    \xtodo{Show schema for algo selection as well and explain difference? But actually not that much changed...}
\end{itemize}

\section{Generation of Training and Test Datasets}

In this section the datasets that were used for the evaluation will be described.
\begin{itemize}
    \item For Offline phase of the concept, training data is needed.
    \item Introduce notation
    \item
\end{itemize}


\section{Introduction}

\begin{frame}{How is cancer usually treated?}
      \begin{figure}[h]
        \centering
        \includegraphics[width=\textwidth]{compe_release}
        \caption{Competitive release under SOC\footnotemark[1]}
      \end{figure}
      \begin{itemize}
        \item<1-> Conventional therapy aims to reduce tumour burden \cite{Frei}
        \item<2-> SOC\footnotemark[1]: Drugs applied at MTD\footnotemark[2]
        \item<3-> Cells have heterogeneous sensitivity $\rightarrow$ without therapy - sensitive keep resistant in check
        \item<4-> MTD\footnotemark[2] eliminates sensitive $\rightarrow$ resistant tumour doesn't respond to therapy \cite{Scott}
      \end{itemize}
      \footnotetext[1]{SOC: Standard-Of-Care}
      \footnotetext[2]{MTD: Maximum Tolerated Dose}
\end{frame}

\begin{frame}{How to avoid competitive release?}
  \begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{at}
    \caption{Control under AT\footnotemark[1]}
  \end{figure}
  \begin{itemize}
    \item<1-> AT\footnotemark[1]: apply drugs at lower, fluctuating doses \cite{Gatenby}
    \item<2-> During drug holiday $\rightarrow$ sensitive compete with resistant
    \item<3-> AT\footnotemark[1] outcome depends on competition
  \end{itemize}
  \footnotetext[1]{AT: Adaptive Therapy}
\end{frame}

\begin{frame}{System of Study}
  \begin{columns}
    \begin{column}{0.65\textwidth}
      \begin{itemize}
        \item<1-> Castration-Resistant Prostate Cancer (CRPC)
        \item<2-> Difficult to cure with current treatments
        \item<2-> Shift in goal to extend survival
        \item<4-> Prostate cells: AR\footnotemark[1] that trigger proliferation when activated by testosterone \cite{Heinlein}
      \end{itemize}
    \end{column}
    \begin{column}{0.35\textwidth}
        \onslide<3->{\begin{figure}
        \centering
        \includegraphics[width=\textwidth]{schematic}
        \caption{Schematic representation of cell types}
      \end{figure}}
    \end{column}
  \end{columns}
  \onslide<3->{\begin{table}
    \centering
    \begin{tabular}{|l|c|c|c|c|}
    \hline
    Cell type & \parbox[t]{1.8cm}{Testosterone\\dependent} & \parbox[t]{1.8cm}{Testosterone\\producing} & Mechanism of resistance\\
    \hline
    $T^+$ & Yes & No & N/A \\
    $T^p$ & Yes & Yes & Cholesterol $\xrightarrow{CYP17\alpha}$ Testosterone \\
    $T^-$ & No & No & AR\footnotemark[1] mutation\\
    \hline
    \end{tabular}
  \end{table}}
  \footnotetext[1]{AR: Androgen Receptors}
\end{frame}

\section{How do we model this?}
\begin{frame}{System of equations}
  \begin{columns}
    \begin{column}{0.55\textwidth}
      \onslide<1->{\begin{equation}
        \frac{dy_i}{dt} = r_i y_i (1 - \frac{\sum_j y_j}{1 + K_{i,max} f_i(O_2) f_i(test)} )- \delta_i y_i
        \label{celleq}
      \end{equation}}
      \onslide<2->{\begin{equation}
        f_i(res) = \begin{cases}
          1 &\text{if } ul_{res,i} \leq res\\
          \frac{res-ll_{res,i}}{ul_{res,i}-ll_{res,i}} &\text{if } ll_{res,i} < res < ul_{res,i}\\
          0 &\text{if } res \leq ll_{res,i}\\
        \end{cases}
        \label{freseq}
      \end{equation}}
      \onslide<3->{\begin{equation}
        \frac{dO_2}{dt} = p_{O_2} - \sum_i \mu_{O_2,i} y_i - \lambda_{O_2} O_2
        \label{o2eq}
      \end{equation}
      \begin{equation}
        \frac{d(test)}{dt} = p_{test} y_{T^p} - \sum_i \mu_{test,i} y_i - \lambda_{test} test
        \label{testeq}
      \end{equation}}
    \end{column}
    \begin{column}{0.45\textwidth}
      \onslide<2->{\begin{figure}[h]
        \centering
        \includegraphics[width=\textwidth]{f_res}
        \caption{Function dependence of carrying capacity on resource}
      \end{figure}}
      \onslide<4->{\begin{itemize}
        \item Assumptions: No mutation, no spatial structure, well mixed
        \item Defined $\mathbb{R}_{\geq 0}$, $y_i < 1 =$ extinction
      \end{itemize}}
    \end{column}
  \end{columns}
  \footnotetext[1]{$i \in \{T^+,T^p,T^-\}$, $res \in \{O_2,test\}$}
  \footnotetext[2]{$ll$: lower limit, $ul$: upper limit}
\end{frame}

\begin{frame}{Parameters Used}
\begin{itemize}
  \item<1-> Some values directly from literature based on cell line data \cite{Jain, HailJr}
  \begin{itemize}
    \item $T^+$: LNCaP
    \item $T^p$: 22Rv1
    \item $T^-$: PC3
  \end{itemize}
  \item<2-> Some values derieved from literature via contraint equations \cite{atcc, Steward, Titus}
  \item<3-> Study Parameters:
    \begin{itemize}
      \item Resource limitations: varied using lower and upper limits
      \item Initial seeding: different ratios of cell types and total population
    \end{itemize}
\end{itemize}

\end{frame}


% When using TeXShop on the Mac, let it know the root document.
% The following must be one of the first 20 lines.
% !TEX root = ../design.tex

\chapter{Latent Dirichlet Allocation (LDA)}

\begin{moduleinfo}
\item[Author] \href{mailto:shengwen.yang@emc.com}{Shengwen Yang} (version 0.6 only)
\item[History]
    \begin{modulehistory}
        \item[v0.6] Initial revision of design document, complete rewrite of module, standard parallel implementation (support for local model update tuple by tuple and global model update iteration by iteration)
        \item[v0.1] Initial version (An approximated implementation which has memory problem for big datasets)
    \end{modulehistory}
\end{moduleinfo}

LDA is a very popular technique for topic modeling. This module implements a parallel Gibbs sampling algorithm for LDA inference.

\section{Overview of LDA}
LDA\cite{Blei:2003} is a very popular technique for discovering the main themes or
topics from a large collection of unstructured documents and has been widely
applied to various fields, including text mining, computer vision, finance,
bioinformatics, cognitive science, music, and social sciences.

With LDA, a document can be represented as a random mixture of latent topics,
where each topic can be characterized by a probability distribution over a
vocabulary of words. Given a large text corpus, LDA will be able to infer a set
of latent topics from the corpus, each represented with a multinomial
distribution over words, denoted as $P(w|z)$, and infer the topic distribution
for each document, represented as a multinomial distribution over topics,
denoted as $P(z|d)$.

Several methods have been proposed for the inference of LDA, including
variational Bayesian, expectation propagation, and Gibbs
sampling\cite{griffiths04finding}. Among of these methods, Gibbs sampling is
the most widely used one because it is simple, fast, and has very few
adjustable parameters. Besides, Gibbs sampling is easy to parallelize and easy
to scale up, which allows us to utilize a cluster of machines to deal with very
big datasets.

\section{Gibbs Sampling for LDA}
\subsection{Overview}
Althouth the derivation of Gibbs sampling for LDA is complicated, the results are very simple. The following equation tells us how to sample a new topic for a word in a corpus:

\begin{equation}
P(z_i=k|\textbf{z}_{-i},\textbf{w}) \propto \frac{nwz_{-i,k}^{(w_i)} +\beta}{nz_{-i,k}+W \beta}\times(ndz_{-i,k}^{(d_i)}+\alpha)
\end{equation}
where:
\begin{itemize}
\item $i$ - index of word in the corpus
\item $d_i$ - docid of the $i_{th}$ word
\item $w_i$ - wordid of the $i_{th}$ word
\item $k$ - the $k_{th}$ topic, where $1 <= k <= T$, and $T$ is the number of topics
\item $z_i$ - topic assignment for the $i_{th}$ word
\item $\textbf{z}_{-i}$ - topic assignments for other words excluding the $i_{th}$ word
\item $\textbf{w}$ - all words in the corpus
\item $ndz$ - per-document topic counts
\item $nwz$ - per-word topic counts
\item $nz$ - corpus-level topic counts
\end{itemize}

According to this equation, we can update the topic assignment to each word sequnetially. This process can be iterated enough times until the conditional distribution reachs a stable state.

\subsection{Parallization}
The parallization of the above algirhtm is very straightforward. The basic idea is to distribute a large set of documents to a cluster of segment nodes and allow each segment node to do Gibbs sampling on a subset of documents locally. Note that at the end of each iteration, the local models generated on each segment node will be merged to generate a global model, which will be distributed to each segment node at the begining of next iteration.

Refer to \cite{Wang:2009} for a similar parallel implementation based on MPI and MapReduce.

\subsection{Formal Description}
\begin{algorithm}[gibbs-lda$(D, T, \alpha, \beta)$] \label{alg:gibbs-lda}

\alginput{
\\Dataset $D$,
\\topic number $T$,
\\prior on per-document topic distribution  $\alpha$,
\\prior on per-word topic distribution $\beta$
}

\algoutput{
\\Per-document topic distribution $P(z|d)$,
\\per-word topic distribution $P(z|w)$
}

\begin{algorithmic}[1]
    \State $ndz \set 0$
    \State $nwz \set 0$
    \State $nz \set 0$
    \For{$d \in D$}
        \For{$w \in W_d$}
            \State $z \set \text{random}(T)$
            \State $ndz[d,z] \set ndz[d,z] + 1$
            \State $nwz[w,z] \set nwz[w,z] + 1$
            \State $nz[z] \set nz[z] + 1$
            \State $Z[d,w] \set z$
        \EndFor
    \EndFor

    \Repeat
        \For{$d \in D$}
            \For{$w \in W_d$}
                \State $z_{old} \set Z[d,w]$
                \State $z_{new} \set \text{gibbs-sample}(z_{old}, ndz, nwz[w], nz, \alpha, \beta)$
                \State $Z[d,w] \set z_{new}$
                \State $ndz[d,z_{old}] \set ndz[d,z_{old}] - 1$
                \State $nwz[w,z_{old}] \set nwz[w,z_{old}] - 1$
                \State $nz[z_{old}] \set nz[z_{old}] - 1$

                \State $ndz[d,z_{new}] \set ndz[d,z_{new}] + 1$
                \State $nwz[w,z_{new}] \set nwz[w,z_{new}] + 1$
                \State $nz[z_{new}] \set nz[z_{new}] + 1$
            \EndFor
        \EndFor
    \Until {Stop condition is satisfied}

    \State $P(z|d) \set \text{normalize}(ndz, \alpha)$
    \State $P(z|w) \set \text{normalize}(nwz, \beta)$
\end{algorithmic}
\end{algorithm}

\begin{description}
    \item[Parallelism] The inner loop is sequential.
        The outer loop is data-parallel and model averaging is used.
\end{description}

\subsection{Implementation as User-Defined Function}

Algorithm\texttt{gibbs-lda} is implemented as the user-defined function \texttt{lda\_train}.

\begin{center}
    \begin{tabularx}{\textwidth}{rlXl}
        \toprule%
        \textbf{Name} & \textbf{Description} & \textbf{Type}
        \\\otoprule

        In &
        \texttt{data\_table} &
        Table containing the training dataset &
        Relation
        \\\midrule

       In &
        \texttt{voc\_size} &
        Size of vocabulary &
        Integer
        \\\midrule

        In &
        \texttt{topic\_num} &
        Number of topics &
        Integer
        \\\midrule

        In &
        \texttt{iter\_num} &
        Number of iterations &
        Integer
        \\\midrule

        In &
        \texttt{alpha} &
        Prior on per-document topic distribution &
        Double
        \\\midrule

        In &
        \texttt{beta} &
        Prior on per-word topic distribution &
        Double
        \\\midrule

        Out &
        \texttt{model\_table} &
        Table containing the model information &
        Relation
        \\\midrule

        Out &
        \texttt{output\_data\_table} &
        Table containing the per-document topic counts and topic assignments &
        Relation
        \\\bottomrule
     \end{tabularx}
\end{center}

Internally, two work tables are used alternately in the iterative Gibbs sampling process, one as input, another as output. The key part of an iteration is implemented essentially using the following SQL:

\begin{sql}[emph={work_table_out,work_table_in,__newplda_gibbs_sample,__newplda_count_topic_agg,model}]
    INSERT INTO work_table_out
    SELECT
        distid,
        docid,
        wordcount,
        words,
        counts,
        madlib.__newplda_gibbs_sample(
            words,
            counts,
            doc_topic,
            model,
            alpha,
            beta,
            voc_size,
            topic_num)
    FROM
    (
        SELECT
            distid,
            docid,
            wordcount,
            words,
            counts,
            doc_topic,
            model
        FROM
        (
            SELECT
                madlib.__newplda_count_topic_agg(
                    words,
                    counts,
                    doc_topic[topic_num + 1:array_upper(doc_topic, 1)]
                                    AS topic_assignment,
                    voc_size,
                    topic_num) model
            FROM
                work_table_in
        ) t1
        JOIN
        work_table_in
    ) t2
\end{sql}

Note that within the \texttt{madlib.\_\_newplda\_gibbs\_sample} function, the \texttt{model} parameter will be read in the first invocation and stored in the memory. In the incoming invocations within the same query, the parameter will be ignored. In this way, the model can be updated by an invocation and the updated model can be transferred to the next invocation.

The above SQL can be further rewritten to eliminate the data redundancy and reduce the overhead of joining operation, and thus improve the overall performance. This is very useful when the product of $voc\_size \times topic\_num$ is very large. See below for the rewritten SQL:

\begin{sql}[emph={work_table_out,work_table_in,__newplda_gibbs_sample,__newplda_count_topic_agg,model}]
    INSERT INTO work_table_out
    SELECT
        distid,
        docid,
        wordcount,
        words,
        counts,
        madlib.__newplda_gibbs_sample(
            words,
            counts,
            doc_topic,
            model,
            alpha,
            beta,
            voc_size,
            topic_num)
    FROM
    (
        SELECT
            dcz.distid,
            dcz.docid,
            dcz.wordcount,
            dcz.words,
            dcz.counts,
            dcz.doc_topic,
            chunk.model
        FROM
        (
            SELECT
                distid, docid, model
            FROM
            (
                SELECT
                    madlib.__newplda_count_topic_agg(
                        words,
                        counts,
                        doc_topic[topic_num + 1:array_upper(doc_topic, 1)]
                                AS topic_assignment,
                        voc_size,
                        topic_num) model
                FROM
                    work_table_in
            ) t1,
            (
                SELECT
                    distid,
                    min(docid) docid
                FROM
                    work_table_in
                GROUP BY distid
            ) t2 -- One row per-segment
        ) chunk -- Ideally only one row per-segment
        RIGHT JOIN work_table_in dcz
        ON (dcz.distid = chunk.distid AND dcz.docid = chunk.docid)
        ORDER BY distid, docid -- Local data manipulation, no data redistribution
    ) joined -- Ideally only one row per-segment has the fully joined data
\end{sql}


\section{Results and Discussion} \label{sec:res-dic}
    In this section we will first present all the results obtained during the implementation of the implemented solution, including tables and graphs generated. Once introduced, they will be discussed and compared.

    \subsection{Results} \label{sec:resultados}
        At Figures \ref{fig:train90} and \ref{fig:train150} we are able to see the training losses of the two different experiences made with the implemented neural network. The Figure \ref{fig:train90} shows the training loss for 90 epochs when we didn't use the pretrained weigths of FlowNet, while the Figure \ref{fig:train150}, shows the results for 150 epochs using the pretrained weigths of FlowNet. These results, as we are going to further discuss were a lot worst than the obtained by \cite{wang2017deepvo}.

        After fit the model we work on validate the obtained weights on a different sequence that were designated as a test dataset. The first measure we did were measure the accuracy of the system. The accuracy were calculated by the follow equation:
        \begin{equation}
            acc = \frac{1}{N} \sum_{i=1}^{N}\sum_{k=1}^{t} \left\| \hat{p_k} - p_k \right\|^{2}_2 + \left\| \hat{\varphi_k} - \varphi_k \right\|^{2}_2
        \end{equation}
        And we obtained an accuracy of \emph{54.70\%}. We also analyse the individual erros for position (x,y,z) and orientation, obtaining results of \emph{9.81\%} and \emph{58.84\%}.

        Lastly, we can present as a result the odometry provided by our system ploted with the original position, the ground truth one. Figures \ref{fig:seq2}, \ref{fig:seq4} and \ref{fig:seq5} shows that results. The blue line on the figures show the ground truth position given by the simulator and the green line the odometry given by our model.

    \subsection{Discussion} \label{sec:discussao}
        Firts we are going to compare the training losses obtained in our experiment for two different scenarios under the same dataset. Comparing Figures \ref{fig:train90} and \ref{fig:train150}, it's possible to realize that the pretrained weigths of FlowNet weren't a big differential to fitting the model. Both of the experiments shows signals of over-fit, because the validation loss don't reduce as the same ratio of training loss. Actually, the validation still the same all over both experiments. The main reason of that results are, very likely, the number of images and poses used on training, that were far bellow the necessary.

        Since we have an experiment that we were looking for to reproduce, we can also compare our results with them. If we look for Figure \ref{fig:10}, we realized that Wang's model were a lot better fitted and different of ours didn’t show any signal of over-fit. A main difference in both experiments is the number of images used for training, at \cite{wang2017deepvo} they used almost 7 thousand images from the Kitti dataset, while we used among 2 thousand images, which reinforce our previous idea that the main reason of over-fit were the number of images used for fit the model.

        Analysing the accuracy results lead us to a very interesting perspective about the system performance, that is well better when predict angles deslocation than position. The most likely reason for that is on how images were collected in the simulator, the deslocation between to images is very abrupt.

        Lastly is time to analyse the most visual of all results we obtained, the images comparing the ground truth and the odometry. If we look for Figure \ref{fig:seq2} it's possible to see that the ground truth has a lot of abrupt changes on both axis directions and this changes are not identified by the model. Figure \ref{fig:seq4}, on the other hand, has a much smoother trajectory, what makes the odometry a lot more accurate, but still not able to manage a sharp turn. Figure \ref{fig:seq5} has the smoothest of all the trajectories, even on that the model were far away to show a good preview.

        The same way compared the training losses of our project with Wang's, we can also compare our odometry with the one obtained by Wang in \cite{wang2017deepvo} (Figure \ref{fig:11}), even showing some big differences between the ground truth and the odometry the system is much more accurate than the one we obtained. There are few possible reasons for that. First, the fact that our model had over-fit. Second, the images fom the Kitti dataset our way more friendly than the ones from the simulator, since they are obtained by an autonomous car which made it smoother, with a constant velocity and also, the images are rectified (Image rectification is a transformation process used to project images onto a common image plane).


\documentclass{article}

\RequirePackage{luatex85}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{polar}
\usepgfplotslibrary{smithchart}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{dateplot}
\usepackage{grffile}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{tikzscale}
\usepackage{currfile}

\begin{document}

\section{Track}

\subsection{Plot}

\begin{center}
    \includegraphics[width=\linewidth]{{{track}}}
\end{center}

\subsection{Distances}
{{distancestable}}

\section{Time profiles}

\subsection{Rotation}
\begin{center}
    \includegraphics[width=\linewidth]{{{direction}}}
\end{center}

\subsection{Speed}
\subsubsection{Speed}
\begin{center}
    \includegraphics[width=\linewidth]{{{speed}}}
\end{center}

\subsubsection{Track speed}
\begin{center}
    \includegraphics[width=\linewidth]{{{trackspeed}}}
\end{center}

\subsection{Angular change}
\begin{center}
    \includegraphics[width=\linewidth]{{{angularchange}}}
\end{center}

\subsection{Path length}
\begin{center}
    \includegraphics[width=\linewidth]{{{pathlength}}}
\end{center}

\end{document}


\documentclass[12pt]{amsart}
\usepackage[margin=0.5in]{geometry}
  % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{bsymb}
\usepackage{calculational}
\usepackage{ulem}
\usepackage{hyperref}
\usepackage{unitb}

\newcommand{\REQ}{\text{REQ}}

\begin{document}

\section{Strategy}

\begin{itemize}
  \item
\end{itemize}
\section{Model m0 --- Requests and non-deterministic handling}
  \input{lock-free-deque/machine_m0.tex}
\begin{machine}{m0}
  \with{sets}
  \newset{\REQ}
  \newevent{add}{add}
  \newevent{handle}{handle}
  \[ \variable{ req : \set [\REQ] } \]
  \begin{description}
    \comment{req}{set of pending requests}
  \end{description}
  \[ \param{add}{ r : \REQ } \]
  \[ \param{handle}{ r : \REQ } \]
  \begin{align*}
      \initialization{m0:init0}{ req = \emptyset } \\
      \evguard{add}{m0:guard}{ \neg r \in req } \\
      \evbcmeq{add}{m0:act0}{req}{ req \bunion \{ r \} } \\
      \cschedule{handle}{m0:sch0}{ \neg req = \emptyset } \\
      \evguard{handle}{m0:grd0}{ r \in req } \\
      \evbcmeq{handle}{m0:act0}{req}{ req \setminus \{ r \} }
  \end{align*}

\noindent
\end{machine}
\section{Model m1 --- Version numbers and individual fairness}
  \input{lock-free-deque/machine_m1.tex}
\begin{machine}{m1}
    \refines{m0}
  \[ \variable{ ver : \Int } \]
  \begin{description}
    \comment{ver}{ serial number of the current data structure state }
  \end{description}
  \[ \indices{handle}{ v : \Int } \]
  \promote{handle}{r}
  \removecoarse{handle}{m0:sch0}
  \removeguard{handle}{m0:grd0}
  % \removeact{handle}{m0:act0}
  \[\witness{handle}{r}{r \in req}\]
  \[\witness{handle}{v}{v = ver}\]
  \begin{align}
      \initialization{m1:init0}{ ver = 0 } \\
      \cschedule{handle}{m1:sch0}{ r \in req } \\
      \cschedule{handle}{m1:sch1}{ v = ver } \\
      % \evguard{handle}{m1:grd0}{ r = r0 } \\
      \evbcmeq{handle}{m1:act0}{ver}{ ver + 1 }
  \end{align}
\end{machine}
\section{Model m2 --- Specialized events}
  \input{lock-free-deque/machine_m2}
\begin{machine}{m2}
  \refines{m1}
  \[ \variable{pshL,pshR,popR,popL : \set [\REQ]} \]
  \begin{description}
    \comment{pshL}{replaces $req$. Set of push\_left requests }
    \comment{pshR}{replaces $req$. Set of push\_right requests }
    \comment{popL}{replaces $req$. Set of pop\_left requests }
    \comment{popR}{replaces $req$. Set of pop\_right requests }
  \end{description}
  \begin{align}
    \initialization{m2:init0}{pshL = \emptyset} \\
    \initialization{m2:init1}{popL = \emptyset} \\
    \initialization{m2:init2}{pshR = \emptyset} \\
    \initialization{m2:init3}{popR = \emptyset}
  \end{align}
  \subsection{Specialize \emph{handle}}
  % \begin{align*}
    \refiningevent{handle}{handle:popL}{handle\_popL}
    \refiningevent{handle}{handle:popR}{handle\_popR}
    \refiningevent{handle}{handle:pushL}{handle\_pushL}
    \refiningevent{handle}{handle:pushR}{handle\_pushR}
  % \end{align*}
  \splitevent{handle}{handle:popL,handle:popR,handle:pushR,handle:pushL}
  \begin{align*}
    \definition{Req}{ pshL \bunion
        pshR \bunion
        popL \bunion
        popR }
  \end{align*}
  \begin{align}
    \invariant{m2:inv0}{ Req = req } \\
    \invariant{m2:inv1}{ pshL \binter pshR = \emptyset } \\
    \invariant{m2:inv2}{ pshL \binter popL = \emptyset } \\
    \invariant{m2:inv3}{ pshL \binter popR = \emptyset } \\
    \invariant{m2:inv4}{ pshR \binter popL = \emptyset } \\
    \invariant{m2:inv5}{ pshR \binter popR = \emptyset } \\
    \invariant{m2:inv6}{ popL \binter popR = \emptyset } \\
    \cschedule{handle:pushL}{m2:sch0}{ r \in pshL } \\
    \evbcmeq{handle:pushL}{m2:act0}{pshL}{pshL \setminus \{ r \}} \\
    \cschedule{handle:pushR}{m2:sch0}{ r \in pshR } \\
    \evbcmeq{handle:pushR}{m2:act0}{pshR}{pshR \setminus \{ r \}} \\
    \cschedule{handle:popL}{m2:sch0}{ r \in popL } \\
    \evbcmeq{handle:popL}{m2:act0}{popL}{popL \setminus \{ r \}} \\
    \cschedule{handle:popR}{m2:sch0}{ r \in popR } \\
    \evbcmeq{handle:popR}{m2:act0}{popR}{popR \setminus \{ r \}}
    % \invariant{m2:inv1}{ popL \subseteq req } \\
    % \invariant{m2:inv2}{ pshR \subseteq req } \\
    % \invariant{m2:inv3}{ popR \subseteq req }
  \end{align}
  \[ \variable{ ppd : \set [\REQ] } \]
  \begin{align}
    \initialization{m2:init4}{ ppd = \emptyset } \\
    \evbcmeq{handle:popR}{m2:act1}{ppd}{ ppd \bunion \{r\} } \\
    \evbcmeq{handle:popL}{m2:act1}{ppd}{ ppd \bunion \{r\} }
  \end{align}
  \newevent{return}{return}
  \[ \indices{return}{ r : \REQ } \]
  \begin{align}
    \cschedule{return}{m2:sch0}{ r \in ppd } \\
    \evbcmeq{return}{m2:act0}{ppd}{ ppd \setminus \{ r \} }
  \end{align}
\subsection{Specialize \emph{add}}
    \refiningevent{add}{add:popL}{add\_popL}
    \refiningevent{add}{add:popR}{add\_popR}
    \refiningevent{add}{add:pushL}{add\_pushL}
    \refiningevent{add}{add:pushR}{add\_pushR}
  \hide{\splitevent{add}{add:popL,add:popR,add:pushR,add:pushL}}
  \begin{align}
    \evbcmeq{add:popL}{m2:act0}{popL}{popL \bunion \{r\}} \\
    \evbcmeq{add:pushL}{m2:act1}{pshL}{pshL \bunion \{r\}} \\
    \evbcmeq{add:popR}{m2:act2}{popR}{popR \bunion \{r\}} \\
    \evbcmeq{add:pushR}{m2:act3}{pshR}{pshR \bunion \{r\}}
  \end{align}
\subsection{Data refinement}
  \removevar{req}
  \removeinit{m0:init0}
  \[\initwitness{req}{req = \emptyset}\]
  \removeact{handle:popR}{m0:act0}
  \removeact{handle:pushR}{m0:act0}
  \removeact{handle:popL}{m0:act0}
  \removeact{handle:pushL}{m0:act0}
  \removecoarse{handle:popR}{m1:sch0}
  \removecoarse{handle:pushR}{m1:sch0}
  \removecoarse{handle:popL}{m1:sch0}
  \removecoarse{handle:pushL}{m1:sch0}
  \removeguard{add:popR}{m0:guard}
  \removeguard{add:pushR}{m0:guard}
  \removeguard{add:popL}{m0:guard}
  \removeguard{add:pushL}{m0:guard}
  \begin{align*}
  \evguard{add:popR}{m2:grd0}
    { \neg r \in Req } \\
  \evguard{add:pushR}{m2:grd0}
    { \neg r \in Req } \\
  \evguard{add:popL}{m2:grd0}
    { \neg r \in Req } \\
  \evguard{add:pushL}{m2:grd0}
    { \neg r \in Req }
  \end{align*}
  \removeact{add:popR}{m0:act0}
  \removeact{add:pushR}{m0:act0}
  \removeact{add:popL}{m0:act0}
  \removeact{add:pushL}{m0:act0}
\end{machine}
  \newcommand{\OBJ}{\mathcal{O}bj}
\section{Model m3 --- The Contents}
  \input{lock-free-deque/machine_m3}
\begin{machine}{m3}
  \refines{m2}
  \with{functions}
  \with{intervals}

  \[ \newset{\OBJ}  \]
  \[ \variable{ p,q : \Int } \]
  \[ \variable{ qe : \Int \pfun \OBJ } \]
  \begin{align}
    \invariant{m3:inv0}{ qe \in \intervalR{p}{q} \tfun \OBJ } \\
    \invariant{m3:inv1}{ p \le q } \\
    \initialization{m3:init0}{ p = 0 \land q = 0} \\
    \initialization{m3:init1}{ qe = \emptyfun }
  \end{align}
  \subsection{Push}
    \[\variable{insL : \REQ \pfun \OBJ} \]
    \[\variable{insR : \REQ \pfun \OBJ} \]
  \begin{description}
    \comment{insL}{parameter for the \emph{push left} operation}
    \comment{insR}{parameter for the \emph{push right} operation}
  \end{description}
  \begin{align}
    \invariant{m3:inv2}{ insL \in pshL \tfun \OBJ } \\
    \invariant{m3:inv3}{ insR \in pshR \tfun \OBJ } \\
    \initialization{m3:init2}{ insL = \emptyfun } \\
    \initialization{m3:init4}{ insR = \emptyfun }
  \end{align}
  \begin{align}
    \evbcmeq{handle:pushL}{m3:act0}{p}{p - 1} \\
    \evbcmeq{handle:pushL}{m3:act1}{qe}{ qe \2| (p \0- 1 \fun insL.r)} \\
    \evbcmeq{handle:pushR}{m3:act0}{q}{q + 1} \\
    \evbcmeq{handle:pushR}{m3:act1}{qe}{ qe \2| (q \fun insR.r)}
  \end{align}

  \[ \param{add:pushL}{ obj : \OBJ } \]

  \begin{align}
    \evbcmeq{add:pushL}{m3:act0}
      {insL}{ insL \1| r \fun obj } \\
    \evbcmeq{handle:pushL}{m3:act2}
      {insL}{ \{ r \} \domsub insL }
  \end{align}

  \[ \param{add:pushR}{ obj : \OBJ } \]

  \begin{align}
    \evbcmeq{add:pushR}{m3:act0}
      {insR}{ insR \1| r \fun obj } \\
    \evbcmeq{handle:pushR}{m3:act2}
      {insR}{ \{ r \} \domsub insR }
  \end{align}
  \[ \variable{ res : \REQ \pfun \OBJ } \]
  \[ \variable{ result : \OBJ } \]
  \[ \variable{ emp : \Bool } \]
  \subsection{Pop}
  \begin{align}
    \initialization{m3:init3}{ res = \emptyfun } \\
    \invariant{m3:inv4}{ res \in ppd \pfun \OBJ } \\
    \evbcmeq{return}{m3:act0}{res}{ \{r\} \domsub res} \\
    \evbcmsuch{return}{m3:act1}{result}{ r \in \dom.res \implies result' = res.r } \\
    \evbcmeq{return}{m3:act2}{emp}{ (r \in \dom.res) }
  \end{align}
  \splitevent{handle:popL}{handle:popL:empty,handle:popL:non:empty}
  \splitevent{handle:popR}{handle:popR:empty,handle:popR:non:empty}
  \refiningevent{handle:popL}{handle:popL:empty}{handle\_popL\_empty}
  \refiningevent{handle:popL}{handle:popL:non:empty}{handle\_popL\_non\_empty}
  \refiningevent{handle:popR}{handle:popR:empty}{handle\_popR\_empty}
  \refiningevent{handle:popR}{handle:popR:non:empty}{handle\_popR\_non\_empty}
  \[ \dummy{v : \Int} \]
  \replace{handle:popL}{m3:sch0}{m3:prog0}
  \begin{align*}
    & \progress{m3:prog0}{v = ver}{p = q \lor p < q}
    \refine{m3:prog0}{implication}{}{}
  \end{align*}
  \begin{align}
    \cschedule{handle:popL:non:empty}{m3:sch0}{ p < q } \\
    \cschedule{handle:popL:empty}{m3:sch0}{ p = q } \\
    \evbcmeq{handle:popL:non:empty}{m3:act0}{res}{ res \1| (r \fun qe.p) } \\
    \evbcmeq{handle:popL:non:empty}{m3:act1}{p}{p+1} \\
    \evbcmeq{handle:popL:non:empty}{m3:act2}{qe}{ \{p\} \domsub qe }
  \end{align}
  \replace{handle:popR}{m3:sch0}{m3:prog0}
  \begin{align}
    \cschedule{handle:popR:non:empty}{m3:sch0}{ p < q } \\
    \cschedule{handle:popR:empty}{m3:sch0}{ p = q } \\
    \evbcmeq{handle:popR:non:empty}{m3:act0}{res}{ res \1| (r \fun qe.(q\0-1)) } \\
    \evbcmeq{handle:popR:non:empty}{m3:act1}{q}{q-1} \\
    \evbcmeq{handle:popR:non:empty}{m3:act2}{qe}{ \{q-1\} \domsub qe }
  \end{align}
\end{machine}
\section{Model m4 --- Memory nodes}
\newcommand{\Addr}{\text{Addr}}
  \input{lock-free-deque/machine_m4}
\begin{machine}{m4}
  \refines{m3}
  \newset{\Addr}
  \[ \variable{ rep : \Int \pfun \Addr } \]
  \[ \constant{ dummy : \Addr } \]
  \begin{align*}
    \assumption{m4:asm0}{ \neg \finite{\Addr} } \\
    \invariant{m4:inv8}{ \finite.(\ran.rep) } \\
    \invariant{m4:inv0}{ \injective{rep} } \\
    \invariant{m4:inv1}
      { rep \in \intervalR{p}{q} \tfun \Addr } \\
    \invariant{m4:inv2}{ \neg dummy \in \ran.rep } \\
    \initialization{m4:init0}{ rep  = \emptyfun } \\
    \evbcmeq{handle:popL:non:empty}{m4:act0}
      {rep}{ \{ p \} \domsub rep } \\
    \evbcmeq{handle:popR:non:empty}{m4:act0}
      {rep}{ \{ q-1 \} \domsub rep } \\
    \evbcmeq{handle:pushL}{m4:act0}
      {rep}{ rep \2| (p\0-1 \fun n) } \\
    \evguard{handle:pushL}{m4:grd0}
      { \neg n \in \ran.rep \bunion \{dummy\} } \\
    \evbcmeq{handle:pushR}{m4:act0}
      {rep}{ rep \2| (q \fun n) }  \\
    \evguard{handle:pushR}{m4:grd0}
      { \neg n \in \ran.rep \bunion \{dummy\} }
  \end{align*}
  \[ \param{handle:pushL}{ n : \Addr } \]
  \[ \param{handle:pushR}{ n : \Addr } \]
\end{machine}
  \input{lock-free-deque/machine_m5}
\begin{machine}{m5}
  \refines{m4}
\subsection{Data structure}
    % \refiningevent{handle}{handle:popL}{handle\_popL}
    % \refiningevent{handle}{handle:popR}{handle\_popR}
    \refiningevent{handle:pushL}{handle:pushL:empty}{handle\_pushL\_empty}
    \refiningevent{handle:pushL}{handle:pushL:non:empty}{handle\_pushL\_non\_empty}
    \refiningevent{handle:pushR}{handle:pushR:empty}{handle\_pushR\_empty}
    \refiningevent{handle:pushR}{handle:pushR:non:empty}{handle\_pushR\_non\_empty}
  \splitevent{handle:pushR}{handle:pushR:empty,handle:pushR:non:empty}
  \splitevent{handle:pushL}{handle:pushL:empty,handle:pushL:non:empty}
    \refiningevent{handle:popL:non:empty}{handle:popL:one}{handle\_popL\_one}
    \refiningevent{handle:popL:non:empty}{handle:popL:more}{handle\_popL\_more}
    \refiningevent{handle:popR:non:empty}{handle:popR:one}{handle\_popR\_one}
    \refiningevent{handle:popR:non:empty}{handle:popR:more}{handle\_popR\_more}
  \splitevent{handle:popR:non:empty}{handle:popR:one,handle:popR:more}
  \splitevent{handle:popL:non:empty}{handle:popL:one,handle:popL:more}
  \[ \definition{Node}{
      \left[
      \begin{array}{l}
        'item : \OBJ \\
      , 'left : \Addr \\
      , 'right : \Addr
      \end{array} \right]
      } \]
  \[ \variable{ link : \Addr \pfun
      \{ 'item : \OBJ
      , 'left : \Addr
      , 'right : \Addr  \} } \]
  \begin{align}
    \invariant{m5:inv1}
      { link \in \ran.rep \tfun Node } \\
    \invariant{m5:inv2}
      { \qforall{i}{\betweenR{p}{i}{q}}{ qe.i = link.(rep.i).'item } } \\
    \initialization{m5:init1}
      { link = \emptyfun }
  \end{align}
\subsection{Pop}
  \begin{align}
    \initialization{m5:in0}{ RH = dummy } \\
    \initialization{m5:in1}{ LH = dummy } \\
    \evbcmeq{handle:popL:one}{m5:act1}{link}
      { \{ LH \} \domsub link } \\
    \evbcmeq{handle:popL:one}{m5:act2}{LH}
      { dummy } \\
    \evbcmeq{handle:popL:one}{m5:act5}{RH}
      { dummy } \\
    \evbcmeq{handle:popR:one}{m5:act1}{link}
      { \{ RH \} \domsub link } \\
    \evbcmeq{handle:popR:one}{m5:act2}{RH}
      { dummy } \\
    \evbcmeq{handle:popR:one}{m5:act4}{LH}
      { dummy } \\
    \evbcmeq{handle:popL:more}{m5:act1}{link}
      { \{ LH \} \domsub link } \\
    \evbcmeq{handle:popL:more}{m5:act2}
      {LH}{ link.LH.'right } \\
    \evbcmeq{handle:popR:more}{m5:act1}{link}
      { \{ RH \} \domsub link } \\
    \evbcmeq{handle:popR:more}{m5:act2}{RH}
      { link.RH.'left }
  \end{align}
\subsection{Push}
\paragraph{\eqref{m5:inv0}}
  \[ \variable{ RH,LH : \Addr } \]
  \removeguard{handle:pushL:empty}{m4:grd0}
  \removeguard{handle:pushL:non:empty}{m4:grd0}
  \removeguard{handle:pushR:empty}{m4:grd0}
  \removeguard{handle:pushR:non:empty}{m4:grd0}
  \begin{align}
    \evguard{handle:pushL:empty}{m5:grd0}
      { \neg n \in \dom.link \bunion \{dummy\} } \\
    \evguard{handle:pushL:non:empty}{m5:grd0}
      { \neg n \in \dom.link \bunion \{dummy\} } \\
    \evbcmeq{handle:pushL:empty}{m5:act1}
      {link}{ \begin{array}{ll}
            & link \\
        \2| & n \fun \left[ \begin{array}{l}
           'item  := insL.r, \\
           'left  := dummy, \\
           'right := dummy
        \end{array} \right]
      \end{array}
          } \\
    \cschedule{handle:pushL:empty}{m5:sch1}
      { LH = dummy } \\
    \cschedule{handle:pushL:non:empty}{m5:sch1}
      { \neg LH = dummy } \\
    \evbcmeq{handle:pushL:non:empty}{m5:act1}
      {link}{ \begin{array}{ll}
            & link \\
        \2| & n \fun \left[ \begin{array}{l}
           'item  := insL.r, \\
           'left  := dummy, \\
           'right := LH
        \end{array} \right] \\
          \2 | & LH \fun (link.LH) [ 'left := n ]
      \end{array}
          } \\
    \evbcmeq{handle:pushL:empty}{m5:act2}
      {LH}{ n } \\
    \evbcmeq{handle:pushL:empty}{m5:act3}
      {RH}{ n } \\
    \evbcmeq{handle:pushL:non:empty}{m5:act2}
      {LH}{ n } \\
    \evguard{handle:pushR:empty}{m5:grd0}
      { \neg n \in \dom.link \bunion \{dummy\} } \\
    \evbcmeq{handle:pushR:empty}{m5:act1}
      {link}{ \begin{array}{ll}
            & link \\
        \2| & n \fun \left[
        \begin{array}{l}
           'item  := insR.r, \\
           'left  := dummy, \\
           'right := dummy
        \end{array} \right]
      \end{array}
          } \\
    \evbcmeq{handle:pushR:empty}{m5:act2}
      {RH}{ n } \\
    \evbcmeq{handle:pushR:empty}{m5:act3}
      {LH}{ n } \\
    % \cschedule{handle:pushR:empty}{m5:sch0}
    %   { r \in pshR } \\
    \evguard{handle:pushR:non:empty}{m5:grd0}
      { \neg n \in \dom.link \bunion \{dummy\} } \\
    \cschedule{handle:pushR:empty}{m5:sch1}
      { RH = dummy } \\
    \cschedule{handle:pushR:non:empty}{m5:sch1}
      { \neg RH = dummy } \\
    \evbcmeq{handle:pushR:non:empty}{m5:act1}
      {link}{ \begin{array}{ll}
            & link \\
        \2| & n \fun \left[
        \begin{array}{l}
           'item  := insR.r, \\
           'left  := RH, \\
           'right := dummy
        \end{array} \right] \\
          \2 | & RH \fun (link.RH) [ 'right := n ]
      \end{array}
          } \\
    \evbcmeq{handle:pushR:non:empty}{m5:act2}
      {RH}{ n }
  \end{align}
  % \replace{handle:pushL}{m5:sch0}{m5:prog0}
  % \replace{handle:pushR}{m5:sch0}{m5:prog1}
  \[ \dummy{ r : \REQ } \]
  % \begin{align}
    % \progress{m5:prog0}{ r \in pshL }{ r \in \dom.nL } \\
    % \progress{m5:prog1}{ r \in pshR }{ r \in \dom.nR }
  % \end{align}
\paragraph{\eqref{m5:inv3}}
  % \begin{align}
    % \invariant{m5:inv6}
    %   { \qforall{r}{ r \in \dom.nL }{item.(nL.r).'item = insL.r} }\\
    % \evbcmeq{handle:pushL}{m5:act1}{nL}{ \{ r \} \domsub nL }
  % \end{align}
  % \begin{align}
    % \invariant{m5:inv7}
      % { \qforall{r}{ r \in \dom.nR }{item.(nR.r).'item = insR.r} }\\
    % \evbcmeq{handle:pushR}{m5:act1}{nR}{ \{ r \} \domsub nR }
  % \end{align}
\subsection{ Linking }
% \subsection{ \eqref{m5:inv10} }
  \begin{align}
    \invariant{m5:inv11}
      { \qforall{i}{\betweenR{p}{i}{q-1}}{ link.(rep.i).'right = rep.(i+1) } } \\
    \invariant{m5:inv12}
      { \qforall{i}{\betweenR{p}{i}{q-1}}{ link.(rep.(i+1)).'left = rep.i } } \\
    \invariant{m5:inv13}
      { p < q \2\implies LH = rep.p  } \\
    \invariant{m5:inv14}
      { p < q \2\implies RH = rep.(q-1) } \\
    \invariant{m5:inv15}
      { p = q \2\implies LH = dummy  } \\
    \invariant{m5:inv16}
      { p = q \2\implies RH = dummy }
  \end{align}
\subsection{New Progress Properties}
  \begin{align}
    \invariant{m5:inv7}{ \finite.(\dom.link) }
  \end{align}
  \removevar{qe,rep,p,q}
  \removeact{handle:popL:one}{m3:act0}
  \removeact{handle:popL:more}{m3:act0}
  \begin{align}
  \evbcmeq{handle:popL:one}{m5:act3}
      {res}{ res \1| (r \fun link.LH.'item) } \\
  \evbcmeq{handle:popL:more}{m5:act3}
      {res}{ res \1| (r \fun link.LH.'item) }
  \end{align}
  \removeact{handle:popL:one}{m3:act2} \\
  \removeact{handle:popL:more}{m3:act2} \\
  \removeact{handle:popR:one}{m3:act0} \\
  \removeact{handle:popR:more}{m3:act0} \\
  % \evbcmeq{handle:popR:non:empty}{m3:act0}{res}{ res \1| (r \fun qe.(q\0-1)) } \\
  \begin{align}
    \evbcmeq{handle:popR:one}{m5:act3}
      {res}{ res \1| (r \fun link.RH.'item) } \\
    \evbcmeq{handle:popR:more}{m5:act3}
      {res}{ res \1| (r \fun link.RH.'item) }
  \end{align}
  \removeact{handle:popR:one}{m3:act2} \\
  \removeact{handle:popR:more}{m3:act2} \\
  \removeact{handle:pushL:empty}{m3:act1} \\
  \removeact{handle:pushL:non:empty}{m3:act1} \\
  \removeact{handle:pushR:empty}{m3:act1} \\
  \removeact{handle:pushR:non:empty}{m3:act1} \\
  \removeinit{m3:init1}
  \removeinit{m4:init0}
  \removeinit{m3:init0}
  \begin{align}
    \initwitness{qe}{qe = \emptyfun} \\
    \invariant{m5:inv4}{ p = q \equiv LH = dummy } \\
    \invariant{m5:inv5}{ p = q \equiv RH = dummy } \\
    \initwitness{rep}{ rep = \emptyfun } \\
    \initwitness{p}{ p = 0 } \\
    \initwitness{q}{ q = 0 }
  \end{align}
  \removecoarse{handle:popR:one}{m3:sch0}
  \removecoarse{handle:popR:more}{m3:sch0}
  \begin{align*}
    \cschedule{handle:popR:one}{m5:sch0}{ \neg RH = dummy } \\
    \cschedule{handle:popR:one}{m5:sch1}{ RH = LH } \\
    \cschedule{handle:popR:more}{m5:sch0}{ \neg RH = dummy } \\
    \cschedule{handle:popR:more}{m5:sch1}{ \neg RH = LH }
  \end{align*}
\removecoarse{handle:popL:one}{m3:sch0}
\removecoarse{handle:popL:more}{m3:sch0}
\removecoarse{handle:popL:empty}{m3:sch0}
\removecoarse{handle:popR:empty}{m3:sch0}
\begin{align*}
  \cschedule{handle:popL:one}{m5:sch0}{ \neg LH = dummy } \\
  \cschedule{handle:popL:one}{m5:sch1}{ LH = RH } \\
  \cschedule{handle:popL:more}{m5:sch0}{ \neg LH = dummy } \\
  \cschedule{handle:popL:more}{m5:sch1}{ \neg LH = RH } \\
  \cschedule{handle:popR:empty}{m5:sch0}{ RH = dummy } \\
  \cschedule{handle:popL:empty}{m5:sch0}{ LH = dummy }
\end{align*}
\removeact{handle:popL:one}{m3:act1}
\removeact{handle:popL:more}{m3:act1}
\removeact{handle:popR:one}{m3:act1}
\removeact{handle:popR:more}{m3:act1}
\removeact{handle:pushL:empty}{m3:act0}
\removeact{handle:pushL:non:empty}{m3:act0}
\removeact{handle:pushR:empty}{m3:act0}
\removeact{handle:pushR:non:empty}{m3:act0}

  \removeact{handle:popL:one}{m4:act0}
  \removeact{handle:popL:more}{m4:act0}
  \removeact{handle:popR:one}{m4:act0}
  \removeact{handle:popR:more}{m4:act0}
  \removeact{handle:pushL:empty}{m4:act0}
  \removeact{handle:pushR:empty}{m4:act0}
  \removeact{handle:pushL:non:empty}{m4:act0}
  \removeact{handle:pushR:non:empty}{m4:act0}

\end{machine}

\begin{machine}{m6}
  \refines{m5}
\[ \variable{ tailL,tailR : \Addr \pfun \{ 'item : \OBJ
      , 'left : \Addr
      , 'right : \Addr  \} } \]
\[ \definition{Shared}{ link | tailL | tailR } \]
\end{machine}

\section{Complete Development}

\input{lock-free-deque/machine_m0}
\input{lock-free-deque/machine_m1}
\input{lock-free-deque/machine_m2}
\pagebreak
\input{lock-free-deque/machine_m3}
\pagebreak
\input{lock-free-deque/machine_m4}
\pagebreak
\input{lock-free-deque/machine_m5}
\pagebreak
\input{lock-free-deque/machine_m6}

\end{document}