% \section{Appendix}

% \subsection{A thermodynamic upper bound on $\rho_u$ for an ideal gas does not exist}
% We used experimental data to characterize the equation of state $\rho(\mu)$ for methane and hydrogen in Figs. X when determining an upper bound on the deliverable capacity $rho_u$. Of course, these $\rho(\mu)$ curves account for attraction and repulsion (excluded volume) between the gas molecules. An interesting question is, does $\rho_u$ present a maximum for an ideal gas when scanning all possible uniform background potential energy fields $\V$?

% The density of an ideal gas $\rho(\mu)\propto e^{\beta\mu}$. According to eqn.~\ref{eq:DofPhi}, when a uniform background potential $\V$ is imposed, the usable capacity of an ideal gas is:
% \begin{equation}
%     \rho_{u,ig}(\V; \mu_H, \mu_L) \propto e^{-\beta \phi} \left( e^{\beta \mu_H} - e^{\beta \mu_L} \right).
% \end{equation} This shows that $\rho_{u,ig}$ grows unbounded as we make the background potential energy field more attractive. Thus, gas-gas repulsion (excluded volume) is critical to describe when assessing usable capacity limits.

% \clearpage
% \section{Old stuff}

% \unsure[inline]{The reality: $E_{gh}=E_0(\mathbf{x}, \mathbf{\theta})$ where $\mathbf{x}$ is center of mass of gas molecule and $\mathbf{\theta}$ includes orientation of gas (rigid assumption here). How to go from this to saying spatial uniform is best for upper bound? David, you said this essentially knocks out states? And upper bound for $n(P_H)$ is not upper bound for $D$! awkward. So maybe instead of saying this is a thermodynamic upper bound, say that, we pose a simple model to estimate at best, what the performance could be. Then we need to multiply for factor that accounts for material taking up space.
% }

% For a generous upper bound on the amount adsorbed at $P_H$, we neglect the space taken up by the atoms comprising the material (which would manifest in a repulsive potential energy field felt by the gas) and endow the control volume with a spatially uniform potential energy field $E_{gh}$. The potential energy of the control volume with $n$ adsorbed molecules is then, under these assumptions:
% \begin{equation}
%     E(n) = n E_{gh} +E_{gg}(n/V).
% \end{equation} The grand-canonical partition function $\Xi$ of the control volume is:
% \begin{equation}
%     \Xi(\mu, V, T)=\displaystyle \sum_{n=0}^\infty (\Lambda ^{-3} V)^n e^{\beta (\mu -E_{gh})n - \beta E_{gg}(n/V)}.
%     \label{eq:xi}
% \end{equation} From eqn.~\ref{eq:xi}, we see that the grand-canonical partition function of the control volume endowed with background potential energy $E_{gh}$ is that of a real gas exhibiting chemical potential $\mu^*$:
% \begin{equation}
%     \Xi_{real gas}(\mu^*, V, T)=\displaystyle \sum_{n=0}^\infty (\Lambda ^{-3} V)^n e^{\beta \mu^*n -\beta E_{gg}(n/V)}.
%     \label{eq:xi_real}
% \end{equation} with $\mu^*=\mu-E_{gh}$. This indicates that, under these assumptions, the thermodynamic properties of the control volume endowed with background energy field $E_{gh}$ at chemical potential $\mu$ are the same as the cognate real gas but with chemical potential increased by $-E_{gh}$.

% \unsure[inline]{OKAY, the above is not optimal. so mean field theory is restrictive, this is more general. I think we can write the super general partition function of the real gas, then argue that, whatever that partition function is (we cant write it and don't know it), a *uniform* background energy will not bias any microstate over another if N is the same. the only necessary thing here is that Ugg and Ugh are additive.}

% {\color{blue}
% \subsection{Why does the chemical potential shift?!}
% The classical view of a real gas in a container $\Omega$ where $|\Omega | =V$:

% \begin{multline}
%     \Xi_{real gas}(\mu, V, T)= \\ \displaystyle \sum_{N=0}^\infty \Lambda^{-3N} \int_{\Omega} \cdots \int_{\Omega} e^{-\beta E_{gg}(\xvec_1, ..., \xvec_N)} e^{\beta \mu N} d\xvec_1 \cdots d\xvec_N.
%     \label{eq:xi_realgas}
% \end{multline} We do not know the complicated function $E_{gg}(\xvec_1, ..., \xvec_N)$ that characterizes the multi-body potential of a configuration of gas molecules in the volume. However, we will represent the effect of $E_{gg}$ from experimental $(\rho, P, T)$ gas data from NIST~\cite{nist}.

% If we endow the control volume $\Omega$ with a spatially uniform potential energy field $E_{gh}$, the potential energy of $E$ of a microstate is modeled as two additive contributions from gas-host and gas-gas interactions:
% \begin{equation}
%     E(\xvec_1, ..., \xvec_N) = N E_{gh} + E_{gg}(\xvec_1, ..., \xvec_N),
% \end{equation} where the gas-gas interactions $E_{gg}$ are equivalent as in the real gas, which is justified by the \emph{spatially uniform} background potential energy that does not e.g. cause electronic density on the gas molecule to redistribute or incentivize the molecule to stretch \unsure{this part trips me up}. Under a Canonical ensemble, the background potential energy is spatially uniform and thus does not change the probability of observing a microstate compared to the cognate gas phase. The grand-canonical partition function of the control volume with spatially uniform energy is:
% % Because a \emph{spatially uniform} background energy does not bias any Canonical ensemble gas microstate, the gas-gas potential

% \begin{multline}
%     \Xi(\mu, V, T)= \\ \displaystyle \sum_{N=0}^\infty \Lambda^{-3N} \int_{\Omega} \cdots \int_{\Omega} e^{-\beta E_{gg}(\xvec_1, ..., \xvec_N)} e^{\beta N( \mu - E_{gh})}  d\xvec_1 \cdots d\xvec_N \\
%      \Xi_{real gas}(\mu - E_{gh}, V, T).
%     \label{eq:xi_backgroundenergy}
% \end{multline}

% Comparing eqn.~\ref{eq:xi_backgroundenergy} to eqn.~\ref{eq:xi_realgas} indicates that the thermodynamic properties of the control volume endowed with background energy field $E_{gh}$ at chemical potential $\mu$ are the same as the cognate real gas with chemical potential increased by $-E_{gh}$.

% }

% The material penalty. Unfortunately the material penalty is correlated with the ability to create background energy field; need material to create the potential field!

% \section{Molecular insights}
% Given that our method of determining a

% \section{Conclusions}

% Consider a

% \section{Discussion}

% While our thermodynamic upper bound for gas delivery via a pressure swing was discussed within the context of nanoporous materials, we note that our methodology applies to a generic control volume regardless of its contents. That is, the external field within the control volume imposed by the metal-organic framework could be replaced by... instead of adsorption, absorption in a liquid; same applies right?

% The isothermal deliverable capacity we investigated in this work neglects heat released/consumed upon ad/desorption, which changes the adsorbed amount at both filling and fully used conditions \cite{mota1997dynamics,chang1996behavior}. Intriguingly, advanced metal-organic frameworks that undergo first-order structural phase transitions upon ad/desorption of gas are being developed that intrinsically offset the heat of ad/desorption when undergoing the structural transition \cite{mason2015methane}.

% \section{Acknowledgement}

% We gratefully acknowledge

% \section{Another attempt at establishing upper bound}
% We will begin by considering the dilute limit, in which the guests may be assumed to be non-interacting.  At low concentration (in both MOF and gas phase), the equilibrium between the MOF and the gas phase has the usual behavior of any chemical reaction, where the equilibrium constant is governed by the change in Gibbs free energy.
% \newcommand\gas{\text{gas}}
% \newcommand\mof{\text{MOF}}
% \begin{align}
%     \frac{\rho_\mof}{\rho_\gas} &= e^{\beta \Delta G^\ominus}
% \end{align}
% This relationship is true for any substrate, and indicates that a large Gibbs free energy of adsorption will lead to a higher concentration.  We take this low concentration limit to define $\Delta G^\ominus$.  As we increase the concentration of guests, however, we need to use the \emph{activity} rather than the concentration in the equilibrium relation:
% \begin{align}
%     \frac{a_\mof}{a_\gas} &= e^{\beta \Delta G^\ominus}\label{eq:activity-ratio}
% \end{align}
% where $a_\mof$ is the activity of the guests in the substrate.  This is again precisely true.  The remaining challenge is to relate the activity to the concentration of guests.  \davidsays{Note that by taking a logarithm of each side we can relate this to chemical potentials, but that gets confusing because it really means "internal" chemical potentials.  Advantage of talking about activities is that an activity by definition is "internal" and is normally used in this way.}

% \maybecut{
% We note that the activity is defined as an exponential of the internal chemical potential:
% \begin{align}
%     a &\equiv e^{\beta (\mu-\mu^\ominus)}
% \end{align}
% where $\mu_0$ is the standard chemical potential for the species.  In our case, the species is identical for each state, so we will use the same standard chemical potential and it will drop out of the ratio in Eq.~\ref{eq:activity-ratio}.  This tells us that the "internal" chemical potential of the gas and the MOF will differ by $\Delta G^\ominus$
% }

% \subsection{Activity in the substrate}
% The task now is to model the relationship between the density of guests and their activity in the substrate.  If the effect of the substrate were to add a uniform attractive potential energy felt by the guests, then the relationship between activity and density would be identical in the substrate and the gas (i.e. the only difference would be the potential energy given by $\Delta G^\ominus$).

% We aim to show that the density corresponding to a given activity would be lower for any non-uniform interaction that results in the same $\Delta G^\ominus$.  Further, we will argue that this discrepancy from ideal behavior will be greater at higher densities, which will mean that using the equation of state for the pure substance provides an upper bound on the deliverable capacity of any substrate.

% \davidsays{I don't think the reasoning here can be universal, because in some substances the concentration is higher than that of an ideal gas.  I think we need to argue that adsorption on a substrate is primarily of interest for gasses that do not liquify at a the temperature we are considering (i.e. we are supercritical).  This means that repulsive guest-guest interactions dominate over attractive interactions, and the density goes down in a sense.}


\chapter{Moving the Planets}
The decision to use circular orbits also influenced how we moved the planets in out modle. We were able to use the {\em rotate} function from \matlab{}. This function allows us to rotate the objects by a number of degrees around a vector at a given position. Due to the flexibility, we can use this function for all the movement in the modle. This includes the movement along the orbit, the spin of planets and keeping the rotation axis of planets aligned.\\

To animate the modle, we move all objects using the {\em rotate} function and then refresh the graph. We defined this to be one tick. This way of animating the modle may be very simple, but proved to have some unpleasant side effects as described below.

\section{Earth's Spin}
We defined the {\em 'normal'} speed of the modle to be so that the earth rotates one degree around the sun per tick. This means that it takes earth 360 ticks to complete one year's worth of rotation. This on the other hand means that, rounding the number for this explanation, earth has to rotate once around itself per tick. This would result in, again rounding the numbers, 365 earth days per earth year which, as we all know, would be correct. Rotating earth 360 degrees around itself per tick results in no visible spin of earth. Using the real numbers would still result in only a very slow spin, due to the fact that it rotates a bit more than 360 degrees per tick\\

We {\em solved} this problem by giving earth an arbitrary speed of rotation around itself.

\section{Increased Speed}
Basically the same problem occurs when increasing the speed of the animation using the slider. As described in the chapter {\em UI Elements}, the slider only increases the angle of rotation. Setting the speed to higher values leads to visual effects similar to those of aliasing. The basic principle of aliasing is that the sample rate of the measuring device is to slow for what it is measuring, which leads to distorted results. In our case, we generate to few steps to create a smooth animation. This makes planets jump, rather than smoothly move, and rotate clockwise rather than counter clockwise. As a simple example, if  the rotation angle is set to 90 degrees earth would jump three times before returning to its starting position. This is drastically different to the 360 steps earth makes at normal speed. \\

As already the one above, we weren't able to fix the problem and could only restrict the maximum speed, which works for the planets in the outer solar system but not so much for the planets closer to the sun.


\documentclass{llncs}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% General %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[dvipsnames]{xcolor}
\usepackage{array}
\usepackage{nameref}
\usepackage{acronym}
\usepackage{diagbox}
\usepackage{enumitem}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{caption}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mise en page %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage[left=2cm,right=2cm,top=3cm,bottom=3.2cm]{geometry}
%\usepackage[T1]{fontenc}
%\usepackage[english]{babel}
%\usepackage[latin1]{inputenc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% References %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[colorlinks=true,linkcolor=black,filecolor=magenta,urlcolor=black,citecolor=blue]{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Maths %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{bm}
%\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
%\usepackage[cmintegrals]{newtxmath}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TIK'Z %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Algorithmic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Graphics %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{url}
\usepackage{subfig}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{subfloat}
\usepackage{epstopdf}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Commands specific for this paper %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\input{commands}
\newcommand{\fieldcard}{q} %cardinality of a finite field
\newcommand{\fieldchar}{p} %characteristic of a finite field
\newcommand{\princhar}{\chi} %principal character of a finite field
\DeclareMathOperator{\ReLU}{ReLU}

\newif\ifeprint
\eprinttrue

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

\title{Faster homomorphic comparison operations for BGV and BFV}
\author{Ilia Iliashenko\inst{1} \and Vincent Zucca\inst{2,3}}
\institute{imec-COSIC, Dept.\ Electrical Engineering, KU Leuven, Belgium \\ \email{ilia@esat.kuleuven.be}  \and DALI, Université de Perpignan Via Domitia, France, \\ \email{vincent.zucca@univ-perp.fr} \and
    LIRMM, Univ Montpellier, Montpellier, France, \\ \email{vincent.zucca@lirmm.fr}
}

\begin{document}
\maketitle

\begin{abstract}
  Fully homomorphic encryption (FHE) allows to compute any function on encrypted values.
  However, in practice, there is no universal FHE scheme that is efficient in all possible use cases.
  In this work, we show that FHE schemes suitable for arithmetic circuits (e.g. BGV or BFV) have a similar performance as FHE schemes for non-arithmetic circuits (TFHE) in basic comparison tasks such as less-than, maximum and minimum operations.
  Our implementation of the less-than function in the HElib library is up to 3 times faster than the prior work based on BGV/BFV.
  It allows to compare a pair of 64-bit integers in 11 milliseconds, sort 64 32-bit integers in 19 seconds and find the minimum of 64 32-bit integers in 9.5 seconds on an average laptop without multi-threading.
\end{abstract}

\section{Introduction}
\label{sec:introduction}
\input{intro.tex}

\section{Background}
\label{sec:background}
\input{background.tex}

% \section{Homomorpic Encryption}
% \label{sec:HE}
% \input{homomorphic-encryption.tex}

\section{Optimising the comparison circuits over $\mathbb{F}_p$}
\label{sec:comparison-circuit}
\input{comparison-circuit.tex}

\section{Applications}
\label{sec:applications}
\input{applications}

\section{Implementation Results}
\label{sec:impl-results}
\input{implem-results.tex}

% \section{Old ideas}
% \input{ideas}

\section{Conclusion}
\input{conclusion.tex}

\bibliographystyle{plain}
\bibliography{abbrev3,biblio}

\appendix
\input{appendix.tex}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{a4}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{float}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage{cite}
\usepackage{textgreek}
\usepackage{amsfonts}
\usepackage{amsmath}

\usepackage[margin=1in]{geometry}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=matlab,
  aboveskip=5mm,
  belowskip=5mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\title{
  {\Huge \bf Power Systems Lab}\\
  \vspace{0.25in}

  {\bf Experiment 3}\\
  Laboratory Report
  \vspace{1in}
}
\author{
  \bf Syed Alisamar Husain, 17BEE012\\
  B.Tech Electrical Engg, 8th Semester
}

\begin{document}
  \begin{titlepage}
    \maketitle
    \vspace*{\fill}
    \begin{center}
      {\bfseries Department of Electrical Engineering} \\
      Jamia Millia Islamia, New Delhi
    \end{center}
    \thispagestyle{empty}
  \end{titlepage}

  \newpage
  \begin{center}
    \huge Experiment 3
    \vspace{0.5in}
  \end{center}

  \section{Objective}
  To find the receiving end voltage, power and voltage regulation of a
  medium length transmission line using MATLAB.

  {\bf Let the given problem be as follows:}
  \begin{center}
    \itshape
    The sending-end voltage on a 3-phase, 60 Hz, transmission line is 120 kV.\\
    The line is 100 km long and has the following constants,\\
    Impedance (Z) =  0.017 + j 0.12 $\Omega$ per km\\
    Admittance (Y) = j 1.2 $\times 10^{-6}$ $S$ per km\\
    Find the receiving end voltage and power if the line is delivering a
    load of 17.12 MVA. Also find the voltage regulation.
  \end{center}

  \section{Theoretical Background}
  The transmission matrix, i.e. the ABCD parameters, of the line relate the
  sending end voltage and current to the receiving end voltage and current, according
  to the following equation:

    \begin{center}
      \begin{math}
        \begin{bmatrix}
          V_S \\ I_S
        \end{bmatrix}
        =
        \begin{bmatrix}
          A & B \\
          C & D
        \end{bmatrix}
        \begin{bmatrix}
          V_R \\ I_R
        \end{bmatrix}
      \end{math}

      \vspace{12pt}
      \begin{math}
        \begin{bmatrix}
          V_R \\ I_R
        \end{bmatrix}
        =
        \begin{bmatrix}
          A & B \\
          C & D
        \end{bmatrix}^{-1}
        \begin{bmatrix}
          V_S \\ I_S
        \end{bmatrix}
      \end{math}
    \end{center}

  To find the receiving end power, we can simply use the following:

    \begin{center}
      \begin{math}
        S_R = V_R I_R
      \end{math}
    \end{center}

    \subsection*{Voltage Regulation}
    {\bf Voltage regulation} is a measure of change in the voltage magnitude
    between the sending and receiving end of a component.
    It is commonly used in power engineering to describe the percentage
    voltage difference between no load and full load voltages distribution
    lines, transmission lines, and transformers.

    The voltage regulation can be calculated as follows:

      \begin{center}
        \% Regulation = $ \dfrac{|V_S| - |V_R|}{|V_R|} * 100 $
      \end{center}

  \section{Implementation}
  \begin{lstlisting}
    % Receiving end voltage, power and voltage regulation of
    % 3-phase medium transmission line.

    % 17BEE012 - Alisamar Husain

    Vs = 120;     % Sending voltage in kV
    Ss = 17.12;   % Sending power in MVA

    Zp  = 0.017 + 0.12i; % Impedance per km
    Yp  = 1.2e-6i;       % Admittance per km
    len = 100;           % Length in km

    % ABCD Parameters
    Z = len * Zp;
    Y = len * Yp;

    A = ((Y*Z)/2) + 1;
    B = Z;
    C = Y*(((Y*Z)/4) + 1);
    D = A;

    T = [ A B; C D; ]
    T = inv(T);

    % Calculating sending end voltage and power

    Is = Ss / Vs;           % Sending current

    Rc = T \ [Vs;Is];
    Vr = Rc(1)              % Received voltage
    Ir = Rc(2);             % Received current

    Sr = abs(Vr * Ir)       % Received power in MVA

    % Calculating voltage regulation

    R = ((Vs - abs(Vr))/abs(Vr)) * 100 % Voltage regulation
  \end{lstlisting}

  \pagebreak
  \section{Observations}
  \begin{figure}[H]
    \centering
    \includegraphics{img/run.png}
    \caption{Result in MATLAB}
    \label{result}
  \end{figure}
  The result of the above program with the given parameters
  is shown in figure \ref{result}.

  \section{Result}
  For the given problem, the calculated {\bf receiving end voltage is 119.6831 kV.}
  The {\bf receiving end power is 17.1492 MVA,} which means a power loss of 29.2 KVA
  and a voltage drop of 1.7313 kV over the line.

  The calculated {\bf voltage regulation of the transmission line is 0.2648\%.}

  Thus we calculated the requied quantities using a program in MATLAB.

\end{document}


\section{Gaussian process submodels}
\label{sec:gp-sub}

\pkg{PyMC} \citep{pymc} represents random variables as objects of class \code{Stochastic}. For example, the following code produces a normally-distributed random variable, labelled \code{'a'}, with mean zero and precision one:
\begin{CodeChunk}
\begin{CodeInput}
a = pymc.Normal('a',0,1)
\end{CodeInput}
\end{CodeChunk}
The current value of variable \code{a} can be queried and updated as follows:
\begin{CodeChunk}
\begin{CodeInput}
print a.value
a.value = 0.5
\end{CodeInput}
\end{CodeChunk}
The logarithm of \code{a}'s probability density function, evaluated at its current value, can be obtained via \code{a.logp}. PyMC has a large library of optimized log-probability density/mass functions, and readily incorporates new functions created by users.

PyMC variables can serve as parents of other variables; for example, the following code produces a normally-distributed variable labelled \code{'b'} whose mean is \code{a}:
\begin{CodeChunk}
\begin{CodeInput}
b = pymc.Normal('b',a,1)
\end{CodeInput}
\end{CodeChunk}

Any Python function can be used to transform random variables via objects of class \code{Deterministic}. For example, the following code produces a variable labelled \code{'c'} whose value is the square of \code{b} 's value:
\begin{CodeChunk}
\begin{CodeInput}
@pymc.deterministic
def c(b=b):
    return b**2
\end{CodeInput}
\end{CodeChunk}
Deterministic variables can be used as parents of other variables, stochastic or deterministic.

Because it allows any \proglang{Python} function to be used as a log-probability density/mass function or variable transformation, PyMC supports a very large model space. This package enlarges that model space to include Gaussian processes via the \code{GaussianProcess} object, which is a random variable whose value is a \code{Realization} object.

Because a Gaussian process is an infinite-dimensional variable, it is not feasible to compute anything like a probability density function for it \footnote{A density with respect to a base measure could be used, but would introduce new complications. For example, Gaussian process measures with the Mat\`ern covariance function with different degrees of differentiability are not mutually continuous. \textbf{ref}}. \code{GaussianProcess}es therefore have no \code{logp} attribute, and \code{GaussianProcess}es cannot be handled by \pkg{PyMC}'s standard MCMC machinery.

However, the evaluation $f(x_*)$ of Gaussian process $f$ on mesh $x_*$ is a simple multivariate normal random variable, which has a \code{logp} attribute and can be handled by the standard machinery. If $f(x_*)$ is incorporated in the model as a variable, a minor extension to the standard machinery (implemented by this package) makes it possible to handle $f$ itself as well. Pairs of $f$ and $f(x_*)$ variables are housed in container objects of class \code{GPSubmodel}.

\subsection{Example: nonparametric regression with unknown mean and covariance parameters}\label{sub:BasicMCMC}

A Gaussian process submodel is created in \code{pymc/examples/gp/PyMCmodel.py} with the following call:
\begin{CodeChunk}
\begin{CodeInput}
sm = gp.GPSubmodel('sm',M,C,fmesh)
\end{CodeInput}
\end{CodeChunk}
There are two stochastic variables in the submodel: \code{sm.f} and \code{sm.f_eval}. The first is the actual Gaussian process $f$: a stochastic variable valued as a \code{Realization} object. The second is $f(x_*)$, where $x_*$ is the input argument \code{fmesh}.

Once the Gaussian process submodel has been created, other variables depend on $f$ and $f(x_*)$ in any way that can be expressed in \proglang{Python} code. In \code{\pkg{PyMC}model.py}, the observation $d$ depends on $f(x_*)$ in a very standard way:
\begin{CodeChunk}
\begin{CodeInput}
d = pymc.Normal('d',mu=sm.f_eval, tau=1./V, value=init_val, observed=True)
\end{CodeInput}
\end{CodeChunk}
In section \textbf{ref}, a \code{GaussianProcess}'s children depend on it in a highly nonstandard way.

% The full probability model is shown as a directed acyclic graph in figure \ref{fig:unobservedModel}. It illustrates the conditional independence relationships between the variables in a GP submodel.

The file \code{pymc/examples/gp/MCMC.py} fits the probability model created in \code{\pkg{PyMC}model.py} using MCMC. The part of the file that actually dispatches the MCMC is very simple:
\begin{CodeChunk}
\begin{CodeInput}
GPSampler = MCMC(PyMCmodel)
GPSampler.isample(iter=5000,burn=1000,thin=100)
\end{CodeInput}
\end{CodeChunk}
The file's output is shown in figure \ref{fig:MCMCOutput}. Note that after the MCMC run \code{GPSampler.trace('sm_f')[:]} yields a sequence of \code{Realization} objects, which can be evaluated on new arrays for plotting. GP realizations can even be tallied on disk using the HDF5 backend via \pkg{PyTables} \citep{tables}, see \cite{pymc}.

% \begin{figure}
%     \centering
%         \epsfig{file=figs/unobservedModel.pdf, width=15cm}
%     \caption{The \pkg{PyMC}-generated directed acyclic graph representation of the extended nonparametric regression model created by \code{pymc/examples/gp/PyMCModel.py}. Ellipses represent \code{Stochastic} objects (variables whose values are unknown even if their parents' values are known), triangles represent \code{Deterministic} objects (variables whose values can be determined if their parents' values are known), and rectangles represent \code{Stochastic} objects with the \code{isdata} flag set to \code{True} (data). Rectangles represent potentials. Arrows point from parent to child. The submodel contains the Gaussian process \code{sm\_f} and its evaluation \code{sm\_f\_eval} on input array \code{sm\_mesh}. It also contains the mean \code{sm\_M\_eval} of \code{sm\_f\_eval} and the lower-triangular Cholesky factor \code{sm\_S\_eval} of its covariance matrix, and a potential \code{sm_fr_check} that forces that covariance matrix to remain positive definite. The actual covariance evaluation \code{sm\_C\_eval} is not needed by the model, but it is exposed for use by Gibbs step methods.}
%     \label{fig:unobservedModel}
% \end{figure}

\begin{figure}
    \centering
        \epsfig{file=figs/gibbsSamples.pdf,width=8cm}
        % \epsfig{file=figs/metroSamples.pdf,width=10cm}
    \caption{The output of \code{pymc/examples/gp/MCMC.py}. The left-hand panel shows all the samples generated for the Gaussian process $f$, and the right-hand panel shows the trace of $f(0)$.}
    \label{fig:MCMCOutput}
\end{figure}

\section{Step methods}
\label{sec:step-methods}
Since $f$ has no \code{logp} attribute, the Metropolis-Hastings family of step methods \citep{pymc} cannot be used to update $f$, $f(x_*)$ or any of their mean or covariance parameters. This package uses a relatively simple work-around that will be described here. Throughout this section, the parents of $f$ are denoted $P$ and the children $K$.

\subsection{Step methods that handle parents of Gaussian processes}
If a probability density function for $f$ were available, the Metropolis-Hastings acceptance ratio for a proposed value $P_p$ of the parents \emph{and} a proposed value $\tilde f$ for $f$ would be:
\begin{eqnarray*}
    \frac{p(K|f_p)\ p(f_p|P_p)\ q(P)}{p(K|f)\ p(f|P)\ q(P_p)}
\end{eqnarray*}
where $q$ denotes the proposal density. Now, suppose we proposed a value for $f$ conditional on the proposed values for the parents $P$ \emph{and} $f(x_*)$. The new acceptance ratio would become
\begin{eqnarray*}
    \frac{p(K|f_p)\ p(f_p|f(x_*), P_p)\ p(f(x_*) | P_p)\ q(f_p|f(x_*),f_p, P_p)\ q(P)}{p(K|f)\ p(f|f(x_*), P)\ p(f(x_*) | P)\ q(f_p|f(x_*),f,P)\ q(P_p)}
\end{eqnarray*}
 The problematic terms are the ones with $f$ or $f_p$ in the consequent position:
\begin{eqnarray*}
    p(f_p|f(x_*), P),\\ q(f|f(x_*),f_p,P),\\ p(f|f(x_*), P),\\ q(f_p|f(x_*),f,P_p),
\end{eqnarray*}
These can be made to cancel by choosing a proposal distribution as follows:
\begin{eqnarray*}
    q(f_p|f(x_*),f,P_p) = p(f_p|f(x_*), P).
\end{eqnarray*}
In other words, if $f$ is proposed from its prior distribution conditional on $f(x_*)$ and its parents whenever $f(x_*)$ is proposed, the intractable terms don't have to be computed. This argument can be made more rigorous by replacing $f$ with its evaluation at all the points at which its value would ever be needed.

\smallskip
To summarize, any Metropolis-Hastings step method can handle the parents of $f$, as well as $f(x_*)$, if it proposes values for $f$ jointly with its target variable as outlined above.

This minor alteration in jumping strategy is implemented by the function \code{wrap_metropolis_for_gp_parents}, which takes a subclass of \code{Metropolis} as an argument and returns a new step method class with altered \code{propose} and \code{reject} methods. The function automatically produces modified versions of all Metropolis step methods in \pkg{PyMC}'s step method registry (\code{Metropolis}, \code{AdaptiveMetropolis}, etc.) \citep{pymc}. The modified step methods are automatically assigned to parents of Gaussian processes.

\subsection{Choosing a mesh}

The mesh points $x_*$ are the points where Metropolis-Hastings step methods can `grab' the value of $f$ to moderate the variance of its proposal distribution. If $x_*$ is an empty array, $f$'s value will be proposed from its prior, and rejection rates are likely to be quite large. If $x_*$ is too dense, on the other hand, computation of the log-probability of $f(x_*)$ will be expensive, as it scales as the cube of the number of points in the mesh.This continuum is illustrated in figure \ref{fig:meshpropose}. Finding the happy medium requires some experimentation.

If $f$'s children depend on its value only via its evaluation on the mesh, the likelihood terms $p(K|f_p)$ and $p(K|f)$ will cancel. In other words, if the mesh is chosen so that $p(K|f)=p(K|f(x_*))$ then the proposed value of $f$ will have no bearing on the acceptance probability of the proposed value of $f(x_*)$ or of the parents $P$. This is the situation in \code{\pkg{PyMC}Model.py}. Such a mesh choice will generally improve the acceptance rate.

\begin{figure}
    \centering
        \epsfig{file=figs/nomeshpropose.pdf,width=4cm}
        \epsfig{file=figs/lightmeshpropose.pdf,width=4cm}
        \epsfig{file=figs/densemeshpropose.pdf,width=4cm}
    \caption{Several possible proposals of $f$ (curves) given proposed values for $f(x_*)$ (heavy dots) with no mesh (top), a sparse mesh (middle), and a dense mesh (bottom). Proposal distributions' envelopes are shown as shaded regions, with means shown as broken lines. With no mesh, $f$ is proposed from its prior and the acceptance rate will be very low. A denser mesh permits a high degree of control over $f$, but computing the log-probability will be more expensive.}
    \label{fig:meshpropose}
\end{figure}

\subsection{Gibbs steps}
If all of $f$'s children, $K$, depend on it as follows:
\begin{eqnarray*}
    K_i|f \stackrel{\textup{\tiny ind}}{\sim} \textup{Normal}(f(x_{*i}),V_i)
\end{eqnarray*}
then $f(x_*)$ can be handled by the \code{GPEvaluationGibbs} step method. This step method is used in \code{MCMC.py}:
\begin{CodeChunk}
\begin{CodeInput}
GPSampler.use_step_method(gp.GPEvaluationGibbs, GPSampler.submod, \
    GPSampler.V, GPSampler.d)
\end{CodeInput}
\end{CodeChunk}
The initialization arguments are the Gaussian process submodel that contains $f$, the observation variance of $f$'s children, and the children, in this case the vector-valued normal variable $d$.

\code{GPEvaluationGibbs} covers the standard submodel encountered in geostatistics, but there are many conjugate situations to which it does not apply. If necessary, special step methods can be written to handle these situations. If \code{GPEvaluationGibbs} is not assigned manually, $f(x_*)$ will generally be handled by a wrapped version of \code{AdaptiveMetropolis} \citep{pymc}, which implements the algorithm of Haario, Saksman and Tamminenn \textbf{ref}.

\section{Geostatistical example}\label{sub:geostat}
\begin{figure}
    \centering
        \epsfig{file=figs/elevmean.pdf, width=5cm}
        \epsfig{file=figs/elevvar.pdf, width=5cm}
    \caption{The posterior mean and variance surfaces for the $v$ variable of the Walker lake example. The posterior variance is relatively small in the neighborhood of observations, but large in regions where no observations were made.}
    \label{fig:walker}
\end{figure}
\begin{figure}
    \centering
        \epsfig{file=figs/elevdraw0.pdf, width=5cm}
        \epsfig{file=figs/elevdraw1.pdf, width=5cm}
    \caption{Two realizations from the posterior distribution of the $v$ surface for the Walker Lake example. Elevation is measured in meters.}
    \label{fig:walkerreal}
\end{figure}
Bayesian geostatistics is demonstrated in the folder \code{pymc/examples/gp/more_examples/Geostatistics}. File \code{getdata.py} downloads the Walker Lake dataset of Isaaks and Srivastava \citep{isaaks} from the internet and manipulates the $x$ and $y$ coordinates into the array format described in section \ref{sec:highdim}. File \code{model.py} contains the geostatistical model specification, which is

\begin{eqnarray*}
    d|f \sim \textup{Normal}(f(x),V)\\
    f|M,C \sim \textup{GP}(M,C) \\
    M:x\rightarrow m\\
    C:x,y,\mathtt{amp},\mathtt{scale},\mathtt{diff\_degree}\rightarrow \mathtt{matern.euclidean}(x,y;\mathtt{amp},\mathtt{scale},\mathtt{diff\_degree})\\
    p(m)\propto 1\\
    \mathtt{amp}\sim \textup{Exponential}(7e-5) \\
    \mathtt{scale}\sim \textup{Exponential}(4e-3) \\
    \mathtt{diff\_degree}\sim \textup{Uniform}(.5,2)\\
    V\sim \textup{Exponential}(5e-9)\\
\end{eqnarray*}

File \code{mcmc.py} fits the model and produces output maps.  The output of \code{mcmc.py} is shown in figures \ref{fig:walker} and \ref{fig:walkerreal}. Figure \ref{fig:walker} shows the posterior mean and variance of the $v$ variable of the dataset, which is a function of elevation (see \cite{isaaks}, appendix A).


%!TEX root = ../../report.tex

\subsubsection{Shape Grammars} % (fold)
\label{ssub:shape_grammars}

Shape Grammars can be considered grammars for design. Instead of having symbols or letters as components of the alphabet, it has shapes that can be in 2D or 3D, and has production rules that are composed by these shapes, that specify the evolution of the system. With this process, similar to the L-Systems explained before, the shape starts from a seed, i.e. a usually simple shape and can evolve to one big and/or complex shape.

The process is performed in two steps, the recognition of a shape and the replacement according to the rules previously defined.

Figure~\ref{fig:SGrammars} exemplifies one shape grammar with one rule, and the evolution of the application of this rule to the shapes iteratively. In this image, it is shown that from very simple initial shape, a complex from can be generated after a few iterations.

\begin{figure}
        \centering
		%\begin{subfigure}[b]{0.55\textwidth}
			\includegraphics[width=0.45\textwidth]{img/Theory/Shape_Grammars/Grammar.png}
			%\caption{a)}
			%\label{fig:SGGrammar}
		%\end{subfigure}

         %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
          %(or a blank line to force the subfigure onto a new line)
		%\begin{subfigure}[b]{0.55\textwidth}
			\includegraphics[width=0.45\textwidth]{img/Theory/Shape_Grammars/Recursion.png}
			%\caption{b)}
			%\label{fig:SGRecursion}
		%\end{subfigure}
        \caption{a) Grammar Tiles b) Recursion steps}
        \label{fig:SGrammars}
\end{figure}

In the CityEngine \cite{Muller2006} system, this is applied to the generation of buildings using 3D blocks for the main form, and 2D shapes to design the facades.

% \begin{figure}[htbp]
% 	\centering
% 	\includegraphics[width=0.55\textwidth]{img/Theory/Shape_Grammars/Edificio.png}
% 	\caption{Simple Building}
% 	\label{fig:SGBuilding}
% \end{figure}

Figure~\ref{fig:SGBuilding} shows a simple building that I modelled using CityEngine and its CGA Shape Grammar (Section~\ref{sub:cityengine}). But CGA is powerful enough to model much more complex buildings like the one in Figure~\ref{fig:CEBuilding}.

% \begin{figure}[htbp]
% 	\centering
% 	\includegraphics[width=0.55\textwidth]{img/Theory/Shape_Grammars/Capturar.png}
% 	\caption{Complex Building \cite{Muller2006}}
% 	\label{fig:CEBuilding}
% \end{figure}

\begin{figure}
\centering
\begin{minipage}{.48\textwidth}
  \centering
  \includegraphics[width=.5\linewidth]{img/Theory/Shape_Grammars/Edificio.png}
  \captionof{figure}{Simple Building}
  \label{fig:SGBuilding}
\end{minipage}
~~
\begin{minipage}{.48\textwidth}
  \centering
  \includegraphics[width=0.8\linewidth]{img/Theory/Shape_Grammars/Capturar.png}
  \captionof{figure}{Complex Building \cite{Muller2006}}
  \label{fig:CEBuilding}
\end{minipage}
\end{figure}

% subsubsection shape_grammars (end)


%\documentclass{article}
\documentclass[prb,superscriptaddress,amsmath]{revtex4}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{color}

\bibliographystyle{unsrt}
\begin{document}

\title{Automating Sensitivity Analysis in the Delphi pipeline}

\maketitle

\section{Introduction}

There exist a growing need to  quantitatively understand  the scope and behavior of
physical models that aim to study complex phenomena. However, an over-abundance of such
models can often present challenges as they span a wide range of parameter
spaces. Further, the complexity of such mechanistic models, both dynamic
and static, make the structural relationship between the parameters and model output
non-trivial thereby preventing one from identifying and estimating the effects
of the most relevant parameters. Senstitivity analysis provides a route for
overcoming this challenge by determining the effects of parameter uncertainty
on models in a non-intrusive manner which extends its applicability to any
general system.

The aim of the AutoMATES initiative is to integrate different
computational models under a common semantically rich representation from  a
variety of sources (text, equations, and source code) that will
allow domain experts to perform interesting analysis on scientific models \cite{Adarsh20a}. This
typically involves four different components viz., Extraction, Grounding,
Comparison, and Augmentation.
Extraction and Grounding together are responsible for creating a generalized intermediate
representation (Grounded Function Network - GrFN) of source codes that are linked to information about the
underlying models, equations, as well as the
physical variables extracted from scientific documents and other relevant text. Once such a unified framework has been
created, independent models can be compared (Comparison) and even augmented (Augmentation) for validation purposes.
Through our GrFN representation,
we can analyze the structural and functional properties of the network that will allow us to
identify regions of parameter spaces where
our selected models display similar features/properties.

\section{Sensitivity Analysis}

The robustness of any model that is dependent on a set of input variables can be tested by performing a sensitivity analysis.
Sensitivity analysis is a measurement of the influence of the input parameters
on the variability in model output. It therefore not only allows the experimenter to
understand the structural relationship between the input and output but also
acts a filtering mechanism to identify the most dominant input parameters
in model uncertaintiy quantification.
In general, there are two types of sensitivity analysis - i) local, ii) global.
Local sensitivity analysis refers to the measurement of model output
variability as a result of perturbations within a single input parameter.
Global sensitivity analysis involves the estimation of changes in the response
function due to variations in the entire input parameter space.
In this article, we shall restrict ourselves to the discussion of global
sensitivity methods. Again, there are multiple methods that perform  global
sensitivity analysis \cite{Saltelli07a}, namely, weighted average local sensitivity analysis,  partial
rank correlation coefficient, multiparameteric sensitivity  analysis, Fourier
amplitude sensitivity analysis (FAST) \cite{Saltelli99a, Chan97a} and lastly, Sobol's method
\cite{Sobol01a, Saltelli02a, Saltelli10a}.
Sobol's method is based on ANOVA (Analysis of Variances) represenation wherein
the model output is decomposed into summands of
variance in the  input parameters with different dimensions \cite{Sobol90a}. This global method
is particularly useful as it can determine the relative contributins of
individual parameters as well as the interaction between them, as given by
the integrands of higher dimensions, to the overall output variance.
The decomposition of the generalized model output $f(x)$ is shown below.

\begin{equation}
f(x) = f_0 + \sum_i f_i(x_i) + \sum_{i < j} f_{ij}(x_i, x_j) + ... + f_{12
...n} (x_1, x_2, ..., x_n)
\end{equation}

Here, $x_1, x_2, ... x_n$ are the $n$ independent model input parameters that
span a unit-dimensional hypercube.
Under the assumption that $f(x)$ is square integrable, in addition to the constraint
below (ANOVA representation),
\begin{equation}
\int_{0}^{1} f_{i_1 ...i_s} (x_{i_1},...x_{i_s}) dx_k = 0
\label{ANOVA}
\end{equation}
 where $k = i_1, ... i_s$, one computes the variances of $f(x)$ ($D$) and $f_{i_1, ... , i_s} (x_{i_1}, ...,
x_{i_s})$ ($D_{i_1 ... i_s}$) to be the following:
\newline
\begin{equation}
D = \int f^2 dx - f_o^2
\end{equation}
and
\begin{equation}
D_{i_1, ..., i_s} = \int f^2_{i_1, ..., i_s} dx_{i_1}, ... dx_{i_s}
\end{equation}

The global sensitivity indices are now given by $S_{i_1}...{i_s}$ in the equation
below:

\begin{equation}
S_{i_1}...{i_s} = \frac {D_{i_1}...{i_s}}{D}
\end{equation}

The integer ``s'' is the dimension or the order of the sensitivity index.
Since, the sensitivity indices are essentially a measure of the output
variance as a function of the input features, they must be non-negative.
Under the assumption of independent input variables, the sum of all sensitivity
indices should be one \cite{Sobol01a, Glen12a}.

The first order Sobol index which measures the direct influence of a single
parameter on the output variance is therefore given by $S_i = \frac{D_i}{D}$
or alternatively, $S_i = \frac{\mathbb{V}(\mathbb{E}(Y|X_i))}{\mathbb{V}(Y)}$.
$Y$ ($X_i$) is the output (input), $\mathbb{E}(Y|X_i)$ measures the conditional
expectation of $Y$ with $X_i$ fixed whereas $\mathbb{V}(Q)$ is the variance of
$Q$ \cite{Saltelli10a}.
While one can in principle calculate the indices upto all possible orders, for
practical purposes, computation of first ($S_1$) and second ($S_2$) order as
well as the total ($S_T$)
Sobol indices is considered to be standard.

Computation of Sobol indices is done through a quasi-Monte Carlo method. The
first step involves the generation of a quasi-random, low discrepancy sequence
of numbers that are sampled uniformly which lead to faster convergence and
higher accuracy  than a
naive Monte Carlo approach. These sequences are known as Sobol sequences \cite{Sobol67a}.
Following this, the model output is evaluated from the sampled data points.
This process is repeated thousands of times to yield the expectation and
variance from the sets of model outputs. The number of samples required to
achieve a high  degree of accuracy depend on the number of input parameters.

\bibliography{sensitivity}

\end{document}


\chapter{Stereo Correlation}
\label{ch:correlation}

In this chapter we will dive much deeper into understanding the core
algorithms in the Stereo Pipeline.  We start with an overview of the
five stages of stereo reconstruction.  Then we move into an in-depth
discussion and exposition of the various correlation algorithms.

The goal of this chapter is to build an intuition for the stereo
correlation process.  This will help users to identify unusual results
in their \acp{DEM} and hopefully eliminate them by tuning various
parameters in the \texttt{stereo.default} file (appendix
\ref{ch:stereodefault}).  For scientists and
engineers who are using \acp{DEM} produced with the Stereo Pipeline, this
chapter may help to answer the question, ``What is the Stereo Pipeline
doing to the raw data to produce this \ac{DEM}?''

A related question that is commonly asked is, ``How accurate is a \ac{DEM}
produced by the Stereo Pipeline?''  This chapter does not yet address
matters of accuracy and error, however we have several efforts underway
to quantify the accuracy of Stereo Pipeline-derived \acp{DEM}, and will be
publishing more information about that shortly.  Stay tuned.

The entire stereo correlation process, from raw input images to a
point cloud or DEM, can be viewed as a multistage pipeline as depicted
in Figure~\ref{fig:asp}, and detailed in the following sections.

\begin{figure}[tb]
  \centering
  \includegraphics[width=13cm]{images/asp.pdf}
  \caption{Flow of data through the Stereo Pipeline.}
  \label{fig:asp}
\end{figure}

\section{Pre-Processing}

The first optional (but recommended) step in the process is least
squares Bundle Adjustment, which is described in detail in
Chapter~\ref{ch:bundle_adjustment}.

Next, the left and right images are roughly aligned using one of the
four methods: (1) a homography transform of the right image based on
automated tie-point measurements, (2) an affine epipolar transform of
both the left and right images (also based on tie-point measurements as
earlier), the effect of which is equivalent to rotating the original
cameras which took the pictures, (3) a 3D rotation that achieves
epipolar rectification {\it(only implemented for Pinhole sessions for
missions like MER or K10 -- see sections \ref{mer:example} and
\ref{k10:example})} or (4) map-projection of both the left and right
images using the \ac{ISIS} \texttt{cam2map} command or through the more
general \texttt{mapproject} tool that works for any cameras supported by
ASP (see section \ref{mapproj-example} for the latter).  The first three
options can be applied automatically by the Stereo Pipeline when the
\texttt{alignment-method} variable in the \texttt{stereo.default} file
is set to \texttt{affineepipolar}, \texttt{homography}, or
\texttt{epipolar}, respectively.

The latter option, running {\tt cam2map}, {\tt cam2map4stereo.py}, or
{\tt mapproject} must be carried out by the user prior to
invoking the {\tt stereo} command.  Map-projecting the images using
\ac{ISIS} eliminates any unusual distortion in the image due to the
unusual camera acquisition modes (e.g. pitching ``ROTO'' maneuvers
during image acquisition for \ac{MOC}, or highly elliptical orbits and
changing line exposure times for the \acl{HRSC}, \acs{HRSC}).  It also
eliminates some of the perspective differences in the image pair that
are due to large terrain features by taking the existing low-resolution
terrain model into account (e.g., the \acl{MOLA}, \acs{MOLA};
\acl{LOLA}, \acs{LOLA}; \acl {NED}, \acs {NED}; or \acl{ULCN},
\acs{ULCN}, 2005 models).

In essence, map-projecting the images results in a pair of very
closely matched images that are as close to ideal as possible given
existing information. This leaves only small perspective differences
in the images, which are exactly the features that the stereo
correlation process is designed to detect.

For this reason, we recommend map-projection for pre-alignment of most
stereo pairs. Its only cost is longer triangulation times as more math
must be applied to work back through the transforms applied to the images. In
either case, the pre-alignment step is essential for performance
because it ensures that the disparity search space is bounded to a
known area.  In both cases, the effects of pre-alignment are taken
into account later in the process during triangulation, so you do not
need to worry that pre-alignment will compromise the geometric
integrity of your \ac{DEM}.

In some cases the pre-processing step may also normalize the pixel
values in the left and right images to bring them into the same
dynamic range.  Various options in the {\tt stereo.default} file
affect whether or how normalization is carried out, including
\texttt{individually-normalize} and
\texttt{force-use-entire-range}.  Although the defaults work in
most cases, the use of these normalization steps can vary from data
set to data set, so we recommend you refer to the examples in Chapter
\ref{ch:examples} to see if these are necessary in your use case.

Finally, pre-processing can perform some filtering of the input
images (as determined by \\ \texttt{prefilter-mode}) to reduce noise
and extract edges in the images.  When active, these filters apply
a kernel with a sigma of \texttt{prefilter-kernel-width} pixels
that can improve results for noisy images (\texttt{prefilter-mode}
must be chosen carefully in conjunction with \texttt{cost-mode},
see Appendix~\ref{ch:stereodefault}).  The pre-processing modes
that extract image edges are useful for stereo pairs that do not
have the same lighting conditions, contrast, and absolute brightness
\citep{Nishihara84practical}.  We recommend that you use the defaults
for these parameters to start with, and then experiment only if
your results are sub-optimal.

\section{Disparity Map Initialization}
\label{d-sub}

Correlation is the process at the heart of the Stereo Pipeline.  It is
a collection of algorithms that compute correspondences between pixels
in the left image and pixels in the right image.  The map of these
correspondences is called a {\em disparity map}.  You can think of a
disparity map as an image whose pixel locations correspond to
the pixel $(u,v)$ in the left image, and whose pixel values
contain the horizontal and vertical offsets $(d_u, d_v)$ to the
matching pixel in the right image, which is $(u+d_u, v+d_v)$.

The correlation process attempts to find a match for every pixel in
the left image. The only pixels skipped are those marked invalid in
the mask images. For large images (e.g. from \ac{HiRISE}, \acl{LROC},
\acs{LROC}, or WorldView), this is very expensive computationally, so
the correlation process is split into two stages.  The disparity map
initialization step computes approximate correspondences using a
pyramid-based search that is highly optimized for speed, but trades
resolution for speed. The results of disparity map initialization are
integer-valued disparity estimates.  The sub-pixel refinement step
takes these integer estimates as initial conditions for an iterative
optimization and refines them using the algorithm discussed in the
next section.

We employ several optimizations to accelerate disparity map
initialization: (1) a box filter-like accumulator that reduces
duplicate operations during correlation \citep{Sun02rectangular}; (2) a
coarse-to-fine pyramid based approach where disparities are estimated
using low-resolution images, and then successively refined at higher
resolutions; and (3) partitioning of the disparity search space into
rectangular sub-regions with similar values of disparity determined in
the previous lower resolution level of the
pyramid \citep{Sun02rectangular}.

\begin{figure}[bt]
  \centering
  \includegraphics[width=13cm]{images/correlation/correlation_400px.png}
  \caption{The correlation algorithm in disparity map initialization
    uses a sliding template window from the left image to find the
    best match in the right image.  The size of the template window
    can be adjusted using the \texttt{H\_KERN} and \texttt{V\_KERN} parameters in the
    \texttt{stereo.default} file, and the search range can be adjusted
    using the \{\texttt{H},\texttt{V}\}\texttt{\_CORR\_}\{\texttt{MIN}/\texttt{MAX}\} parameters.}
  \label{fig:correlation_window}
\end{figure}

Naive correlation itself is carried out by moving a small, rectangular
template window from the from left image over the specified search
region of the right image, as in Figure~\ref{fig:correlation_window}.
The ``best'' match is determined by applying a cost function that
compares the two windows. The location at which the window evaluates
to the lowest cost compared to all the other search locations is
reported as the disparity value. The \texttt{cost-mode} variable allows you
to choose one of three cost functions, though we recommend normalized
cross correlation \citep{Menard97:robust}, since it is most robust to
slight lighting and contrast variations between a pair of
images. Try the others if you need more speed at the cost of quality.

Our implementation of pyramid correlation is a little unique in that
it is actually split into two levels of pyramid searching. There is a
\texttt{\textit{output\_prefix}-D\_sub.tif} disparity image that is
computed from the greatly reduced input images \texttt{*-L\_sub.tif}
and \texttt{\textit{output\_prefix}-R\_sub.tif}. Those ``sub'' images
have their size chosen so that their area is around 2.25 megapixels,
a size that is easily viewed on the screen unlike the raw source
imagery. The low-resolution disparity image then defines the per
thread search range of the higher resolution disparity,
\texttt{\textit{output\_prefix}-D.tif}.

This solution is imperfect but comes from our model of multi-threaded
processing. ASP processes individual tiles of the output disparity
in parallel. The smaller the tiles, the easier it is to distribute
evenly among the CPU cores. The size of the tile unfortunately
limits the max number of pyramid levels we can process. We've struck
a balance where every 1024 by 1024 pixel area is processed individually
in a tile. This practice allows only 5 levels of pyramid processing.
With the addition of the second tier of pyramid searching with
\texttt{\textit{output\_prefix}-D\_sub.tif}, we are allowed to
process beyond that limitation.

Any large failure in the low-resolution disparity image will be
detrimental to the performance of the higher resolution disparity. In
the event that the low-resolution disparity is completely unhelpful, it
can be skipped by adding \texttt{corr-seed-mode 0} in the
\texttt{stereo.default} file and using a manual search range (section
\ref{sec:search_range}). This should only be considered in cases where
the texture in an image is completely lost when subsampled. An example
would be satellite imagery of fresh snow in the Arctic. Alternatively,
\texttt{\textit{output\_prefix}-D\_sub.tif} can be computed at a sparse
set of pixels at full resolution, as described in section
\ref{sparse-disp}.

An alternative to computing \texttt{\textit{output\_prefix}-D.tif}
from sub-sampled images (\texttt{corr-seed-mode 1}) or skipping it
altogether (\texttt{corr-seed-mode 0}), is to compute it from a
lower-resolution DEM of the area (\texttt{corr-seed-mode 2}). In this
situation, the low-resolution DEM needs to be specified together with its estimated
error. See section \ref{corr_section} for more detailed information as
to how to specify these options. In our experiments, if the input DEM
has a resolution of 1 km, a good value for the DEM error is about 10 m,
or higher if the terrain is very variable.

\subsection{Debugging Disparity Map Initialization}

Never will all pixels be successfully matched during stereo
matching. Though a good chunk of the image should be correctly
processed. If you see large areas where matching failed, this could be
due to a variety of reasons:

\begin{itemize}
\item In regions where the images do not overlap, there should be no
  valid matches in the disparity map.
\item Match quality may be poor in regions of the images that have
  different lighting conditions, contrast, or specular properties of
  the surface.
\item Areas that have image content with very little texture or
  extremely low contrast may have an insufficient signal to noise
  ratio, and will be rejected by the correlator.
\item Areas that are highly distorted due to different image
  perspective, such as crater and canyon walls, may exhibit poor
  matching performance. This could also be due to failure of the
  preprocessing step in aligning the images. The correlator can not
  match images that are rotated differently from each other or have
  different scale/resolution. Mapprojection is used to at least
  partially rectify these issues (section \ref{mapproj-example}).
\end{itemize}

Bad matches, often called ``blunders'' or ``artifacts'' are also
common, and can happen for many of the same reasons listed above.  The
Stereo Pipeline does its best to automatically detect and eliminate
these blunders, but the effectiveness of these outlier rejection
strategies does vary depending on the quality of the input imagery.

When tuning up your {\tt stereo.default} file, you will find that
it is very helpful to look at the raw output of the disparity map
initialization step.  This can be done using the {\tt disparitydebug}
tool, which converts the \texttt{\textit{output\_prefix}-D.tif}
file into a pair of normal images that contain the horizontal and
vertical components of disparity.  You can open these in a standard
image viewing application and see immediately which pixels were
matched successfully, and which were not. Stereo matching blunders
are usually also obvious when inspecting these images.  With a good
intuition for the effects of various {\tt stereo.default} parameters
and a good intuition for reading the output of {\tt disparitydebug},
it is possible to quickly identify and address most problems.

If you are seeing too many holes in your disparity images, one option
that may give good results is to increase the size of the
correlation kernel used by \texttt{stereo\_corr} with the
\texttt{--corr-kernel} option.  Increasing the kernel size will
increase the processing time but should help fill in regions of
the image where no match was found.

\begin{figure}[tb]
  \centering
  \includegraphics[width=17cm]{images/correlation/stereo_corr_box_compare.png}
  \caption{The effect of increasing the correlation kernel size from 35 (left)
  to 75 (right). This location is covered in snow and several regions lack
  texture for the correlator to use but a large kernel increases the chances
  of finding useful texture for a given pixel.}
  \label{fig:corr-kernel-size-effect}
\end{figure}

\begin{figure}[tb]
  \centering
  \includegraphics[width=17cm]{images/correlation/quantile_filter_result.png}
  \caption{The effect of using the \texttt{rm-quantile} filtering option
  in \texttt{stereo\_corr}. In the left image there are a series of high
  disparity "islands" at the bottom of the image.  In the right image quantile
  filtering has removed those islands while leaving the rest of the image intact.}
  \label{fig:quantile-filtering-effect}
\end{figure}

\subsection{Search Range Determination}
\label{sec:search_range}

In some circumstances, the low-resolution
disparity \texttt{D\_sub.tif} may fail to get computed, or it may be
inaccurate. This can happen for example if only very small features are
present in the original images, and they disappear during the resampling
that is necessary to obtain \texttt{D\_sub.tif}. In this case, it is
possible to set \texttt{corr-seed-mode} to 0, and manually set a search
range to use for full-resolution correlation via the parameter
\texttt{corr-search}. In \texttt{stereo.default} this parameter's entry
will look like:

\begin{verbatim}
        corr-search -80 -2 20 2
\end{verbatim}

The exact values to use with this option you'll have to discover
yourself. The numbers right of \texttt{corr-search} represent the
horizontal minimum boundary, vertical minimum boundary, horizontal
maximum boundary, and finally the horizontal maximum boundary within
which we will search for the disparity during correlation.

It can be tricky to select a good search range for the \texttt{stereo.default} file.
That's why the best way is to let \texttt{stereo} perform an automated
guess for the search range. If you find that you can do a
better estimate of the search range, take look at the intermediate
disparity images using the \texttt{disparitydebug} program to figure
out which search directions can be expanded or contracted. The output
images will clearly show good data or bad data depending on whether
the search range is correct.

The worst case scenario is to determine the search range manually. For
example, for ISIS images, both images could be opened in \texttt{qview}
and the coordinates of points that can be matched visually can be
compared. Subtract line,sample locations in the first image from the
coordinates of the same feature in the second image, and this will yield
offsets that can be used in the search range.  Make several of these
offset measurements and use them to define a line,sample bounding box,
then expand this by 50\% and use it for \texttt{corr-search}.  This will
produce good results in most images.

Also, if you are using an alignment option, you'll instead want to
make those disparity measurements against the written L.tif and R.tif
files (see chapter \ref{chapter:outputfiles}) instead of the original input files.

\subsection{Local Homography}
\label{sec:local_hom}

Local homography decomposes the left image into tiles,
and tries to find the best homography transform from each
tile to the right image before computing the correlation.
This is more fine-grained than using a global homography transform.

This approach is experimental. We suggest instead the map-projection
approach be used (section \ref{mapproj-example}), as that one
is even more fine-grained, and does not suffer from artifacts
that may arise from the local homography piecewise approach.

This option can be turned on with the flag \texttt{use-local-homography}.

\subsection{Semi-Global Matching}
\label{sec:sgm}

A new option for integer stereo correlation available in ASP is the
popular semi-global matching algorithm introduced in
\citep{hirschmuller_sgm_original}.  The algorithm is not typically used for
DEM generation but it has been used successfully to process HRSC images
\citep{hirschmuller_hrsc_with_sgm}.  The version of the algorithm implemented
by ASP has a few modifications relative to the original implementation.  The
most significant difference is that ASP's implementation performs a 2D disparity
search, similar to what is done in the NG-fSGM algorithm
\citep{xiang_2016_low_complexity_fsgm}.  Since ASP processes a wide variety of
cameras with varying degrees of metadata quality, the standard assumption
with SGM that the disparity search can be performed only along a one-dimensional
epipolar line does not hold.  The other major change is that ASP uses a multi-
resolution hierarchical search combined with a compressed memory scheme similar
to what is used in the SGM algorithm \citep{rothermel2012sure_isgm}.  With
these two features the SGM algorithm can be used for unrectified, larger images.
ASP also supports a mode using the MGM algorithm \citep{facciolo2015mgm},
referred to in some places in the documentation as Smooth SGM.  This algorithm
reduces the amount of high frequency artifacts in textureless regions at the
cost of a longer run time.  ASP also offers the option of a hybrid SGM/MGM mode
where MGM is used only for the final resolution level which obtains results
somewhere between the pure SGM and MGM options.

The greatest advantage of the SGM algorithm over the normal ASP correlation algorithm
is an improved ability to find disparity matches in areas of repetitive or low texture.
SGM can also discern finer resolution features than the standard correlation algorithm
since it tends to use much smaller matching kernels.  Along with these advantages come several disadvantages.
First,  SGM is computationally expensive and requires a lot of memory.  Second, in some
situations it can produce noticeable artifacts at tile boundaries.  Third, it can
sometimes produce inaccurate results in textureless regions.  With careful parameter
selection and usage these disadvantages can be mitigated.

In order to use SGM, pass in \texttt{stereo-algorithm}.  Use 1 to use SGM or 2 to use MGM.
To process large images you must use the \texttt{parallel\_stereo} program instead
of the \texttt{stereo} program.  \texttt{parallel\_stereo} replaces the refinement stage
with a new seam blending stage to suppress artifacts along tile borders.  Without this step
SGM can produce artifacts along tile borders.  The
\texttt{stereo} program can be used as long as the \texttt{corr-tile-size} command
is set large enough to fit the entire image into a single processing tile.
When running SGM, a single ASP  process will handle only one tile at a time but it
will use multiple threads per tile, as opposed to normal stereo where each tile uses its
own thread.  MGM is currently limited to using 8 simultaneous threads but SGM does not have a limit.
When running \texttt{parallel\_stereo} use the following options:

\begin{itemize}
\item Specify the \texttt{collar-size} option or leave it at the default value.  Increasing this value decreases
the chances of seeing artifacts along tile borders but increases processing time and memory usage.
\item Set the \texttt{corr-tile-size} option to determine the tile size, keeping in mind that larger tile sizes
produce better results but consume more memory.  The collar size you selected will enlarge the processed tile size.
\item Set the \texttt{processes} option keeping in mind memory constraints as discussed earlier.  Each process will
run one simultaneous SGM instance and consume memory.
\item The \texttt{corr-memory-limit-mb} parameter limits the number of megabytes of memory that can be used by SGM/MGM.
This limit is per-process. To be safe, make sure that you have more RAM available than the value of this parameter
multiplied by the number of processes.
\item \texttt{job-size-w} and \texttt{job-size-h} are set equal to \texttt{corr-tile-size}.  Do not override them!
\end{itemize}

By setting these parameters in the manner described, each process will generate a single SGM tile which will then be
blended in the new blend step.  Each process can use multiple threads with \texttt{threads-singleprocess} without
affecting the stereo results.

When SGM or MGM is specified, certain stereo parameters have their default values
replaced with values that will work with SGM.  You can still manually specify these
options.

\begin{itemize}
\item Cost Mode (default 4).  Mean absolute distance (MAD)
(\texttt{cost-mode <= 2}) usually does not work well.
The census transform mode (\texttt{cost-mode 3}) \citep{zabih1994census} tends to
perform better overall but can produce artifacts on featureless terrain.
The ternary census transform mode (\texttt{cost-mode 4}) \citep{hua2016texture}
is a modification of the census transform that is more stable on low contrast terrain
but may be less accurate elsewhere.
\item Kernel size.  SGM kernels must always be symmetric.
The SGM algorithm works with much smaller kernel sizes than the regular integer
correlator so the default large kernel is not recommended.
The MAD cost mode can be used with any odd kernel size (including size 1) but the census
cost modes can only be used with kernel sizes 3, 5, 7, and 9.  Size 7 is usually a good choice.
\item Xcorr-Threshold. By default, this is disabled in order to nearly halve the (long) run time of the SGM
algorithm.  Set \texttt{xcorr-threshold} to >= 0 to turn it back on.  If you set the
\texttt{min-xcorr-level} parameter to 1 you can perform cross correlation on the smaller resolution levels
without spending the time to run it on the largest resolution level.
\item The median and texture filters in the \texttt{stereo\_fltr} tool.  These filters
were designed specifically to clean up output from the SGM algorithm and are especially useful in suppressing
image artifacts in low-texture portions of the image.  A median filter size of 3 and a texture filter size of 11
are good starts but the best values will depend on your input images. The \texttt{texture-smooth-scale}
parameter will have to be adjusted to taste, but a range of 0.13 to 0.15 is typical for icy images.  These
values are enabled by default and must be manually disabled.  If your images have good texture throughout
it may be best to disable these filters.
\item The \texttt{prefilter-mode} setting is ignored when using SGM.
\item The \texttt{subpixel-mode} setting is ignored when using SGM unless it is set to
one of the "SGM" modes, 6 through 10.  You can try these modes, particularly the cosine algorithm,
but the default subpixel algorithm should work well.  The SGM performs parabola subpixel interpolation as part of
the \texttt{stereo\_corr} step and does no work in the \texttt{stereo\_rfne} module.  This means that
after dealing with the long SGM processing time you do not need to follow it up with a slow subpixel option!
\end{itemize}

% Would be nice to improve this figure
\begin{figure}[!htbp]
  \centering
  \subfigure{\includegraphics[width=0.6\textwidth]{images/correlation/icebridge_input_crop.png}}
  \hfil
  \begin{subfigure}
    {\includegraphics[width=0.47\textwidth]{images/correlation/icebridge_subpix3_crop.png}}
    {\includegraphics[width=0.47\textwidth]{images/correlation/icebridge_sgm_crop.png}}
  \end{subfigure}
  \caption{A section of a NASA IceBridge image with a pair of hill-shaded DEMs below it
  showing the difference between default ASP processing and processing using the SGM algorithm.}
  \label{fig:corr-sgm-example}
\end{figure}

Figure \ref{fig:corr-sgm-example} shows a comparison between two stereo modes. The DEM on the left was generated using the default stereo parameters and
  \texttt{-{}-subpixel-mode 3}.  The DEM on the right was generated using the command:

\begin{verbatim}
  stereo --stereo-algorithm 1 --threads 1 --xcorr-threshold -1 --corr-kernel 7 7 \
  --corr-tile-size 6400 --cost-mode 4 --subpixel-mode 0 --median-filter-size 3   \
  --texture-smooth-size 13 --texture-smooth-scale 0.13
\end{verbatim}

Some grid pattern noise is visible in the image produced using
SGM. Using \texttt{-\/-stereo-algorithm 2} should reduce it. And, as
mentioned earlier, for large images which won't fit in memory,
\texttt{-\/-corr-tile-size} can be set to a value like 4096, and
\texttt{parallel\_stereo} should be used.

\section{Sub-pixel Refinement}
\label{sec:subpixel}

Once disparity map initialization is complete, every pixel in the
disparity map will either have an estimated disparity value, or it
will be marked as invalid.  All valid pixels are then adjusted in the
sub-pixel refinement stage based on the \texttt{subpixel-mode}
setting. % Subpixel refinement is not additive step.

The first mode is parabola-fitting sub-pixel refinement
(\texttt{subpixel-mode 1}).  This technique fits a 2D parabola to
points on the correlation cost surface in an 8-connected neighborhood
around the cost value that was the ``best'' as measured during
disparity map initialization. The parabola's minimum can then be
computed analytically and taken as as the new sub-pixel disparity
value.

This method is easy to implement and extremely fast to compute, but it
exhibits a problem known as pixel-locking: the sub-pixel disparities
tend toward their integer estimates and can create noticeable ``stair
steps'' on surfaces that should be smooth
\citep{Stein06:attenuating,Szeliski03sampling}.  See
for example Figure~\ref{fig:parabola_subpixel}. Furthermore, the parabola
subpixel mode is not capable of refining a disparity estimate by more
than one pixel, so although it produces smooth disparity maps, these
results are not much more accurate than the results that come out of
the disparity map initialization in the first place.  However, the
speed of this method makes it very useful as a ``draft'' mode for
quickly generating a \ac{DEM} for visualization (i.e. non-scientific)
purposes. It is also beneficial in the event that a user will simply
downsample their DEM after generation in Stereo Pipeline.

\begin{figure}[tb]
\centering
  \subfigure[{\tt Left Image}]{\includegraphics[width=2in]{images/correlation/sub4-AS15-M-1134_crop_400px.png}
    \label{fig:left_input_image}}
  \subfigure[{\tt Parabola Subpixel Mode}]{\includegraphics[width=2in]{images/correlation/3D_mode0_400px.png}
    \label{fig:parabola_subpixel}}
  \subfigure[{\tt Bayes EM Subpixel Mode}]{\includegraphics[width=2in]{images/correlation/3D_mode3_400px.png}}

  \subfigure[{\tt Right Image}]{\includegraphics[width=2in]{images/correlation/sub4-AS15-M-1135_crop_400px.png}
    \label{fig:right_input_image}}
  \subfigure[{\tt Parabola Hillshade}]{\includegraphics[width=2in]{images/correlation/hillshade_mode0_400px.png}}
  \subfigure[{\tt Bayes EM Hillshade}]{\includegraphics[width=2in]{images/correlation/hillshade_mode3_400px.png}}

\caption{Left: Input images.  Center: results using the parabola draft
  subpixel mode (\texttt{subpixel-mode = 1}). Right: results using the Bayes
  EM high quality subpixel mode (\texttt{subpixel-mode = 2}).}
\label{fig:parabola_results}
\end{figure}

For high quality results, we recommend \texttt{subpixel-mode 2}:
the Bayes EM weighted affine adaptive window correlator.  This
advanced method produces extremely high quality stereo matches that
exhibit a high degree of immunity to image noise.  For example
Apollo Metric Camera images are affected by two types of noise
inherent to the scanning process: (1) the presence of film grain
and (2) dust and lint particles present on the film or scanner.
The former gives rise to noise in the \ac{DEM} values that wash out real
features, and the latter causes incorrect matches or hard to detect
blemishes in the \ac{DEM}.  Attenuating the effect of these scanning
artifacts while simultaneously refining the integer disparity map
to sub-pixel accuracy has become a critical goal of our system, and
is necessary for processing real-world data sets such as the Apollo
Metric Camera data.

The Bayes EM subpixel correlator also features a deformable template
window from the left image that can be rotated, scaled, and translated
as it zeros in on the correct match in the right image.  This
adaptive window is essential for computing accurate matches on crater
or canyon walls, and on other areas with significant perspective
distortion due to foreshortening.

This affine-adaptive behavior is based on the Lucas-Kanade template
tracking algorithm, a classic algorithm in the field of computer
vision \citep{Baker04:lucas-kanade}.  We have extended this technique;
developing a Bayesian model that treats the Lucas-Kanade parameters
as random variables in an Expectation Maximization (EM) framework.
This statistical model also includes a Gaussian mixture component
to model image noise that is the basis for the robustness of our
algorithm.  We will not go into depth on our approach here, but we
encourage interested readers to read our papers on the topic
\citep{nefian:bayes_em, broxton:isvc09}.

However we do note that, like the computations in the disparity map
initialization stage, we adopt a multi-scale approach for sub-pixel
refinement. At each level of the pyramid, the algorithm is initialized
with the disparity determined in the previous lower resolution level
of the pyramid, thereby allowing the subpixel algorithm to shift the
results of the disparity initialization stage by many pixels if a better
match can be found using the affine, noise-adapted window.  Hence,
this sub-pixel algorithm is able to significantly improve upon the
results to yield a high quality, high resolution result.

Another option when run time is important is \texttt{subpixel-mode 3}:
the simple affine correlator.  This is essentially the Bayes EM mode
with the noise correction features removed in order to decrease the
required run time.  In data sets with little noise this mode can yield
results similar to Bayes EM mode in approximately one fifth the time.

\section{Triangulation}

When running an ISIS session, the Stereo Pipeline uses geometric
camera models available in \ac{ISIS} \citep{anderson08:isis}.  These
highly accurate models are customized for each instrument that
\ac{ISIS} supports.  Each \ac{ISIS} ``cube'' file contains all of the
information that is required by the Stereo Pipeline to find and use
the appropriate camera model for that observation.

Other sessions such as DG (\textit{Digital Globe}) or Pinhole, require that
their camera model be provided as additional arguments to the
\texttt{stereo} command. Those camera models come in the form of an
XML document for DG and as \texttt{*.pinhole, *.tsai, *.cahv,
  *.cahvor} for Pinhole sessions. Those files must be the third and
forth arguments or immediately follow after the 2 input images for
\texttt{stereo}.

\begin{figure}[h]
\centering
  \subfigure[{\tt Framing Camera Model}]{\includegraphics[width=3in]{images/correlation/pinhole_model_400px.png}
    \label{fig:framing}}
  \subfigure[{\tt Pushbroom Camera Model}]{\includegraphics[width=3in]{images/correlation/linescan_model_400px.png}
\label{fig:linescan}}
\caption{Most remote sensing cameras fall into two generic categories
  based on their basic geometry.  Framing cameras (left) capture an
  instantaneous two-dimensional image.  Linescan cameras (right)
  capture images one scan line at a time, building up an image over
  the course of several seconds as the satellite moves through the
  sky.}
\label{fig:camera_models}
\end{figure}

\ac{ISIS} camera models account for all aspects of camera geometry,
including both intrinsic (i.e. focal length, pixel size, and lens
distortion) and extrinsic (e.g. camera position and orientation)
camera parameters.  Taken together, these parameters are sufficient to
``forward project'' a 3D point in the world onto the image plane of
the sensor.  It is also possible to ``back project'' from the camera's
center of projection through a pixel corresponding to the original 3D
point.

\begin{figure}[tb]
  \centering
  \includegraphics[width=12cm]{images/correlation/triangulation_400px.png}
  \caption{Once a disparity map has been generated and refined, it can
    be used in combination with the geometric camera models to compute
    the locations of 3D points on the surface of Mars.  This figure
    shows the position (at the origins of the red, green, and blue vectors) and
	orientation of the Mars Global Surveyor at
    two points in time where it captured images in a stereo pair.}
  \label{fig:triangulation}
\end{figure}

Notice, however, that forward and back projection are not symmetric
operations.  One camera is sufficient to ``image'' a 3D point onto a
pixel located on the image plane, but the reverse is not true.  Given
only a single camera and a pixel location $x = (u,v),$ that is the
image of an unknown 3D point $P = (x,y,z)$, it is only possible to
determine that $P$ lies somewhere along a ray that emanates from the
camera's center of projection through the pixel location $x$ on the
image plane (see Figure~\ref{fig:camera_models}).

Alas, once images are captured, the route from image pixel back to 3D
points in the real world is through back projection, so we must bring
more information to bear on the problem of uniquely reconstructing our
3D point.  In order to determine $P$ using back projection, we need
{\em two} cameras that both contain pixel locations $x_1$ and $x_2$
where $P$ was imaged.  Now, we have two rays that converge on a point
in 3D space (see Figure~\ref{fig:triangulation}). The location where
they meet must be the original location of $P$.

In practice, the two rays rarely intersect perfectly because any
slight error in the camera position or pointing information will
effect the rays' positions as well.  Instead, we take the {\em closest
  point of intersection} of the two rays as the location of point
$P$.

Additionally, the actual distance between the rays at this point is an
interesting and important error metric that measures how
self-consistent our two camera models are for this point.  You will
learn in the next chapter that this information, when computed and
averaged over all reconstructed 3D points, can be a valuable statistic
for determining whether to carry out bundle adjustment. Distance
between the two rays at their closest intersection is recorded in the
fourth channel of the point cloud file,
\texttt{\textit{output-prefix}-PC.tif}. This information can be
brought to the same perspective as the output DEM by using the
\textit{-\/-error} argument on the \texttt{point2dem} command.

This error in the triangulation, the distance between two rays,
\emph{is not the true accuracy of the DEM}. It is only another
indirect measure of quality. A DEM with high triangulation error
is always bad and should have its images bundle-adjusted. A DEM
with low triangulation error is at least self consistent but could
still be bad. A map of the triangulation error should only be
interpreted as a relative measurement. Where small areas are found
with high triangulation error came from correlation mistakes and
large areas of error came from camera model inadequacies.


\documentclass[fleqn,reqno]{amsart}
\usepackage{amsbsy}
\allowdisplaybreaks
\newcommand{\pdd}[2]{\frac{\partial{#1}}{\partial{#2}}}
\newcommand{\spdd}[3]{\frac{\partial^{2}{#1}}{\partial{#2}\partial{#3}}}
\newcommand{\sd}[2]{\frac{\partial^{2}{#1}}{\partial{#2}^2}}
\newcommand{\av}[1]{\overline{#1}}
\newcommand{\ldd}[2]{\frac{d{#1}}{d{#2}}}
\newcommand{\inlineldd}[2]{d{#1}/d{#2}}
%remove the following command to get rid of double space.
\linespread{1.6}
\title{Fire in single material ICE.}
\author{Stanislav G. Borodai}
\begin{document}
\maketitle
\section{Introduction.}
The following discussion represents a pathway as to how to introduce
combustion in ICE code. It is only an initial proposal and should be treated as
such. Only single material ICE combustion implementation will be discussed,
the generalization to multi-material would be derived after satisfactory progress is
obtained in single material combustion modeling in ICE code. Also, the initial task
would be only to incorporate mixing and reaction models in ICE, other necessary
models to perform LES pool fire simulation in ICE such as turbulence models,
radiation models, etc. would be discussed and implemented upon satisfactory progress obtained
on this initial task. This means that the only simulation possible to achieve
after completion of this step is laminar compressible adiabatic fire.
The ability of such simulation to handle long time scale (on the order of 10 seconds)
fires has then to be verified.
However, even
before this simulation, simulation of laminar air jet and helium laminar jet in air need to
be performed. First one is to verify the boundary conditions available in ICE,
second is to verify ability of ICE to handle variable density flows upon
initial implementation of cold flow mixing models (no reaction). Along with
these simulations, standard ICE simulations like pressure wave propagation
need to be performed upon implementation of mixing and reaction models to
verify that new formulation preserves the ability of ICE to handle standard
compressible flow cases.

\section{General considerations.}
\label{s1}
The main idea behind introducing combustion in ICE comes from examining
a general form of a subgrid scale equation of state used in combustion:
\begin{align}
\label{e1}
\rho=\rho(p,f,h,...),
\end{align}
where $\rho$ is density, $p$ is pressure, $f$ is mixture fraction,
$h$ is enthalpy and $...$ are other combustion related variables like
reaction progress variable, omitted here without loss of generality.
From (\ref{e1}), convective or Lagrangian time derivative of $\rho$
would be
\begin{align}
\label{e2}
\ldd{\rho}{t}=\left.\pdd{\rho}{p}\right|_{f,h}\ldd{p}{t}+
\left.\pdd{\rho}{f}\right|_{p,h}\ldd{f}{t}+
\left.\pdd{\rho}{h}\right|_{p,f}\ldd{h}{t},
\end{align}
where subscripts indicate that the derivatives are computed
with these variables being constant.
From (\ref{e2}) and continuity equation, one obtains divergence
constraint for the velocity $\boldsymbol{u}$:
\begin{align}
\label{e3}
\pdd{u_i}{x_i}=-\frac{1}{\rho}\left.\pdd{\rho}{p}\right|_{f,h}\ldd{p}{t}-
\frac{1}{\rho}\left.\pdd{\rho}{f}\right|_{p,h}\ldd{f}{t}-
\frac{1}{\rho}\left.\pdd{\rho}{h}\right|_{p,f}\ldd{h}{t}.
\end{align}
Lagrangian time derivatives of independent variables in~(\ref{e3}), except
pressure,
have to be computed explicitly each time step, and partial derivatives in~(\ref{e3})
will be computed from tabulated sungrid scale combustion models.
The divergence constraint~(\ref{e3}) will be used in the modified ICE
pressure projection in section~\ref{s3} to include combustion effects.

\section{Governing equations}
\label{s2}
Let us examine the subset of governing equations involved. Only the equations
pertaining to the combustion will be presented, along with the equation for
fluxing velocity necessary to explain the method. The rest of ICE algorithm
can be used unchanged. The equations are:
\begin{align}
\label{e4}
\ldd{\rho f}{t}=\pdd{}{x_j}\left(D_f \pdd{f}{x_j}\right),
\end{align}
\begin{align}
\label{e5}
\ldd{\rho h}{t}=\pdd{}{x_j}\left(D_h \pdd{h}{x_j}\right)+\ldd{p}{t}+Q,
\end{align}
\begin{align}
\label{e6}
\ldd{\rho u^f_i}{t}=-\pdd{p}{x_i}+\pdd{\mu s_{ij}}{x_j}+(\rho_{ref}-\rho)g_i,
\end{align}
where $D_f$ and $D_h$ are the resolved scale diffusion coefficients for mixture fraction and enthalpy respectively,
$Q$ is heat loss or gain due to radiation, $\boldsymbol{u}^f$ is fluxing velocity, $\mu$ is viscosity,
$\rho_{ref}$ is reference density, $\boldsymbol{g}$ is gravity vector and $s_{ij}$ is twice the deviatoric
part of strain-rate tensor:
\begin{align}
\label{e7}
s_{ij}=\pdd{u_i}{x_j}+\pdd{u_j}{x_i}-\frac{2}{3}\delta_{ij}\pdd{u_i}{x_i}.
\end{align}
Note that, as written, the resolved scale diffusion coefficients and viscosity combine molecular effects, and LES model effects.
The equation of state~(\ref{e1}) from section~\ref{s1} finishes the set of equations to be discussed.
Also note that in the current formulation
the energy equation is presented in enthalpy form. The advantage of this form is that
(when there is no radiation) the absolute enthalpy is a conserved quantity in combustion process,
no explicit reaction rate source terms, which are often hard to compute,
are necessary in the energy equation. This set of equations, except that for the
fluxing velocity, is taken from the current Arches approach. However, in this first approach
it is also assumed that Diffusion coefficients for mixture fraction and enthalpy
are the same and proportional to viscosity, with a constant proportionality
coefficient.

\section{Modified single material ICE algorithm.}
\label{s3}
In this section the discrete ICE algorithm, modified to include combustion effects,
will be discussed. Since the current goal of ICE is to run in implicit
mode for pressure to overcome acoustic CFL time step limit, only the modification
to implicit in pressure ICE will be discussed. Explicit ICE extension can be
obtained in the same manner, if necessary. Also, since everything but pressure
is computed explicitly, the operator splitting errors are unavoidable. Thus,
the implicit iteration encompassing this whole part of algorithm may be
required to remove these errors.

\begin{enumerate}
\item{Assuming that all the information for some time $N$ is available, the first step would
be to compute $f^{N+1}$ and $h^{*,N+1}$ and their Lagrangian derivatives from
equations (\ref{e4})-(\ref{e5}). The Lagrangian time derivative of pressure
on the right hand side of (\ref{e5}) is not available, so it would be assumed zero for this step.
That is why enthalpy has star superscript.
Other splitting errors in this step would be produced form division of $(\inlineldd{\rho f}{t})^{N+1}$,
$(\inlineldd{\rho h}{t})^{N+1}$, $(\rho f)^{N+1}$, $(\rho h)^{N+1}$ by $\rho^N$ to
obtain the values in question. If no radiation model is present, then $Q$ term in
enthalpy equation (\ref{e5}) becomes zero, and enthalpy equation for this step becomes exactly
the same as mixture fraction equation (\ref{e4}). This means that flame is adiabatic with
no pressure effects. In this case adiabatic enthalpy can be computed from simple formula:
\begin{align}
\label{e8}
h^{*,N+1}=h^{*,N+1}_a=f*h_{fuel}+(1-f)*h_{air},
\end{align}
given heats of formation $h_{fuel}$ and $h_{air}$ for fuel and air respectively, so that
explicit transport of enthalpy by equation (\ref{e5}) is not necessary.}

\item{Second step is to get predicted value of $\rho^{*,N+1}$ given $f^{N+1}$,$h^{*,N+1}$, and old pressure $p^N$.
This step, in general, would be a subgrid scale chemistry table lookup.
Partial derivatives for
equation (\ref{e3}) should be obtained from table at this step as well.
In adiabatic case equation (\ref{e3}) does not depend on enthalpy. Note that one
more splitting error has been introduced, since new value of pressure is not yet
available.}

\item{Third step would be computation of fluxing velocity and pressure projection.
One can define pressure correction as:
\begin{align}
\label{e9}
p^c=p^{N+1}-p^{N}.
\end{align}
Then, Lagrangian value
of fluxing velocity $\boldsymbol{u}^{f*,N+1}$ is defined as (also using (\ref{e9}) in (\ref{e6})):
\begin{align}
\label{e10}
&u^{f*,N+1}_i=\nonumber\\
&\frac{1}{\rho^{*,N+1}}\left(
u^{f,N}+\Delta t \left(-\pdd{p^N}{x_i}+\pdd{}{x_j}\left(\mu^N \pdd{s^N_{ij}}{x_j}\right)+
(\rho_{ref}-\rho^N)g_i\right)\right).
\end{align}
The velocity $\boldsymbol{u}^{f*,N+1}$ includes all the right hand side of (\ref{e6}) but the gradient of pressure correction term.
Again, one more splitting error has been introduced, since $\rho^{N+1}$ is not available yet.
The actual fluxing velocity $\boldsymbol{u}^{f,N+1}$ can then be obtained from the equation:
\begin{align}
\label{e11}
u^{f,N+1}_i=u^{f*,N+1}_i-\frac{\Delta t}{\rho^{*,N+1}}\pdd{p^c}{x_i},
\end{align}
after pressure correction $p^c$ has been solved for.

To derive the equation for pressure, one would apply divergence operator to the both sides of (\ref{e11})
\begin{align}
\label{e12}
\pdd{u^{f,N+1}_i}{x_i}=\pdd{u^{f*,N+1}_i}{x_i}-\pdd{}{x_i}\left(\frac{\Delta t}{\rho^{*,N+1}}\pdd{p^c}{x_i}\right),
\end{align}
and assuming that divergence of fluxing velocity at time $N+1$ is given by constraint (\ref{e3}),
using (~\ref{e9}) in (\ref{e3}), one would get the equation for the pressure correction $p^c$:
\begin{align}
\label{e13}
&\left(\pdd{}{x_i}\left(\frac{\Delta t}{\rho^{*,N+1}}\pdd{}{x_i}\right)
-\frac{1}{\rho^{*,N+1} \Delta t}\left.\pdd{\rho}{p}\right|_{f,h}^{N+1}\right)p^c=\nonumber\\
&\pdd{u^{f*,N+1}_i}{x_i} +
\frac{1}{\rho^{*,N+1}}\left.\pdd{\rho}{f}\right|_{p,h}^{N+1}{\ldd{f}{t}}^{N+1}+
\frac{1}{\rho^{*,N+1}}\left.\pdd{\rho}{h}\right|_{p,f}^{*,N+1}{\ldd{h}{t}}^{*,N+1},
\end{align}
where the first term on the right hand side of (\ref{e13}) can be computed taking divergence of (\ref{e10}).
Again, all the variables with star subscript, except for $\boldsymbol{u}^{f,N+1}$, indicate that splitting
error has been introduced in their computation.
Now, pressure correction $p^c$ can be obtained solving (\ref{e13}) with appropriate boundary conditions.
Note that if right hand side of (\ref{e13}) and boundary conditions do not depend on $p^c$, no outer
iteration while solving (\ref{e13}) is required. After pressure correction has been solved for,
fluxing velocity can be obtained from (\ref{e10}).}

\item{The last step would be to update enthalpy with now known pressure effect, if desired:
\begin{align}
\label{e14}
h^{N+1}=h^{*,N+1}+\frac{p^c}{\rho^{*,N+1}\Delta t},
\end{align}
and get updated density $\rho^{N+1}$ by table lookup, but now as a function of $f^{N+1}$, ${h^{N+1}}$, and $p^{N+1}$ now
available from (\ref{e9}).
This step concludes the modification to the ICE algorithm, the rest of it can be done as usual.
Again, please not that with all splitting errors committed, it may be necessary to perform the procedure above
in implicit manner. Also, the Lagrangian pressure time derivative present in equation (\ref{e5}) can be
treated the same way as it was done in (\ref{e13}), but benefit of doing so requires further investigation.}
\end{enumerate}

\end{document}


\documentclass[USenglish]{report}
\usepackage[notref,notcite]{showkeys}
\usepackage{../mathsim}
%\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{svg.path}
%\usepackage[math]{iwona}
\usepackage[backend=biber,style=alphabetic]{biblatex}
\addbibresource{all.bib}

\def\constref#1{C_{\text{\ref{#1}}}}
\title{\textbf{Notes on Applied Mathematics}
\\[5mm]
{\large Iterative Methods and Preconditioning}}
\author{Guido Kanschat}
\date{\today}

%\renewcommand{\thechapter}{B.\arabic{chapter}}

\makeindex
\begin{document}
\maketitle

\section*{Preface}
\input{../preface}
\thispagestyle{empty}
\begin{todo}
  Note: yellow boxes indicate text which is missing in the current
  version and will be added soon.
\end{todo}
\setcounter{page}{0}

\tableofcontents

\chapter{Preliminaries}

\begin{notation}
  Very often in these notes, we will abbreviate the term ``there is a
  positive constant $C$ independent of certain parameters, such that $x \le C
  y$'' by the expression
  \begin{gather}
    \label{eq:main:1}
    x \lesssim y.
  \end{gather}
  Furthermore, if additionally for $C\ge 1$, there holds  $y \le Cx$,
  then we write
  \begin{gather*}
    x \simeq y.
  \end{gather*}
  Sometimes, it may be useful to be able refer to the implicit
  constant in such equations, which will be done in the form  $\constref{eq:main:1}$.
\end{notation}

\chapter{Introduction to separable Hilbert spaces}
\input{lafa}

\chapter{Iterative methods in finite and infinite dimensional spaces}

\begin{remark}
  This part of the notes deals with preconditioning of symmetric
  operators, or those, which have a dominating symmetric part. The
  theory of preconditioning methods for nonsymmetric and in particular
  non-normal operators is currently not well developed and thus will not
  be covered by these notes.
\end{remark}

\begin{notation}
  Iterative methods will be considered in a Hilbert space $X$ with
  inner product $\scal(.,.)$.
\end{notation}

\input{itintro}
\input{richardson}
\input{cg}

\chapter{Schwarz methods}
\label{cha:iteration:schwarz-methods}
\input{schwarz}

\chapter{Multigrid methods}
\label{cha:iteration:multigrid-methods}
\input{mg}

%\printbibliography % Das sorgt nur für Schwierigkeiten, kann ja später wieder rein ...
%\printindex
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End: