\chapter{Potentials}

\section{Functional forms of force fields}

The molecular energy can be described as an Taylor expansion in bonds, bends, torsions, etc.
\begin{equation}
 \begin{split}
 U=&\sum_{\text{bonds}} U_r\left(r\right)+\sum_{\text{bends}} U_\theta\left(\theta\right)+
   \sum_{\text{torsions}} U_\phi\left(\phi\right)+\sum_{\text{out-of-plane bends}} U_\chi\left(\chi\right)
   +\sum_{\text{non-bonded}}U_{nb}\left(r\right)\\
   &+\sum_{\text{bond-bond}}U_{bb'}\left(r,r'\right)
   +\sum_{\text{bond-bend}}U_{b\theta'}\left(r,\theta\right)
   +\sum_{\text{bend-bend}}U_{\theta\theta'}\left(\theta,\theta'\right)\\
   &+\sum_{\text{bond-torsion}}U_{r\phi}\left(r,\phi,r'\right)
   +\sum_{\text{bend-torsion}}U_{\theta\phi}\left(\theta,\phi,\theta'\right)+\dots
  \end{split}
  \label{Eq: force field}
\end{equation}
This expansion is believed to capture all the chemical entities we can think of, such as
atoms, bonds, angles, etc, and physical properties like equilibrium structures, vibrational spectra, etc.
The cross terms are not ad-hoc functions, but arise naturally from this expansion.
For example, bonds and bends interact, as the bend angle becomes smaller the bond lengths tend to increase.
Their inclusion leads to two advantages: 1) they increase the accuracy of the force field
(especially the vibrational frequencies), and 2) they
increase the transferability of the diagonal terms $U_r\left(r\right),U_\theta\left(\theta\right),
U_\phi\left(\phi\right),U_\chi\left(\chi\right)$.
On top of the terms in Eq. \ref{Eq: force field} one can add ad hoc terms, such as hydrogen bonding, that
are not adequately accounted for otherwise.

Eq. \ref{Eq: force field} is historically referred to as an \emph{force field}. The name arose from the lowest
order approximation using only springs with \emph{force constants}. Force fields have matured
and have become quite accurate and many parameters exists for a wide range of structure. These parameters
are crucial and determine the quality of the force field. Unfortunately, deriving high quality parameters
remains more than a art rather than a science. However, some progress has been made and in the end of the chapter
some algorithms are described how to obtain them.

The terms in Eq. \ref{Eq: force field} consists of a functional form, force constants
(a resistance against a change from the optimum value), and a reference value.
The functional form is chosen such as to be an accurate description of the true potential energy (either
known from experiment or from quantum mechanics), although one can simplify the functional form to decrease
computational evaluation time of the energy at the cost of diminished accuracy. This tradeoff has almost vanished
for intra-molecular potentials but is still an issue for the non-bonded terms.
The reference value is \emph{not} the equilibrium value (except by chance).
For example, bond lengths are affected by all other terms in the force field and the more strained a molecule
the farther the bond equilibrium length will deviate from its reference value. This means that one can not simply
take the equilibrium values from known experiment.

\section{Bonded potentials diagonal terms}

\subsection{Bond-stretching potentials}

The bond stretching potential describes the change in energy as the bond stretches and contracts.
The simplest functional form would be Hook's law:
\begin{equation}
  U=\frac{1}{2} k \left(r-r_0\right)^2
\end{equation}
where $k$ is the force constant and $r_0$ the reference value for the bond. This form is computationally
very fast, but not very realistic. It is well known that it is easier to stretch a bond than it is to
compress a bond. The `Morse' potential is an-harmonic and provides a much better description of the energy
\begin{equation}
  U= D\left(1-e^{-\alpha\left(r-r_0\right)}\right)^2
\end{equation}
Expanding around the equilibrium value leads to
\begin{equation}
 U=D\alpha^2\left(r-r_0\right)^2\left[1-\alpha\left(r-r_0\right)+\frac{7}{12}\alpha^2\left(r-r_0\right)^2\dots\right]
\end{equation}
The first terms is the harmonic potential (with $k=2D\alpha^2$) and for organic structures where distortions
from equilibrium are small the difference between the potentials are small. However, for larger deviations
the Morse potential provides a significantly better description.
The Morse potential provides a restoring force which goes to zero at long distances. For minimizations
starting far equilibrium could result in non-convergence. Some force fields solved this problem by
using modification of Hook's law. MM2 added a cubic term making the bond an-harmonic. However, this leads to large
negative energies for poor initial geometries with large distortions. MM3 added the quartic term to solve this.
Note the $7/12$ terms in the MM2/3 functional forms originate from the Taylor expansion of the Morse potential,
and the cubic and quartic terms are chosen to mimic the Morse potentials for moderate distortions.
Dinur and Hagler proposed a functional form based on inverse bond lengths which follows the true potential energy
compared to QM over an even wider range
\begin{equation}
 U=U_0+C_2\left(\frac{1}{r}-\frac{1}{r_0}\right)^2+C_3\left(\frac{1}{r}-\frac{1}{r_0}\right)^3
\end{equation}

\noindent
The implemented bond-potentials:
\begin{itemize}

  \item{HARMONIC\_BOND}
  \begin{equation}
  U=\frac{1}{2} p_0 \left(r-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA.

  \item{CORE\_SHELL\_SPRING}
  \begin{equation}
  U=\frac{1}{2} p_0 r^2
  \end{equation}
  1 argument: $p_0/k_B$ in units of K/\AA$^2$.

  \item{MORSE\_BOND}
  \begin{equation}
  U=p_0\left[\left(1-e^{-p_1\left(r-p_2\right)}\right)^2-1\right]
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ in \AA$^{-1}$, and $p_2$ in \AA.

  \item{LJ\_12\_6\_BOND}
  \begin{equation}
  U=\frac{p_0}{r^{12}}-\frac{p_1}{r^{6}}
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K\,\AA$^{12}$, and $p_1/k_B$ in units of K\,\AA$^6$.

  \item{LENNARD\_JONES\_BOND}
  \begin{equation}
  U=4 p_0 \left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^{6}\right]
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K, $p_1$ in \AA.

  \item{BUCKINGHAM\_BOND}
  \begin{equation}
  U=p_0 e^{-p_1 r}-\frac{p_2}{r^{6}}
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, and $p_2/k_B$ in K\,\AA$^6$.

  \item{RESTRAINED\_HARMONIC\_BOND}
  \begin{equation}
  U=\begin{cases}
      \frac{1}{2} p_0\left(r-p_1\right)^2 & \qquad \left|r-p_1\right|\leq p_2\\
      \frac{1}{2} p_0 p_2^2+p_0 p_2\left(\left|r-p_1\right|-p_2\right) & \qquad \left|r-p_1\right|> p_2
     \end{cases}
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, and $p_2$ in \AA.

  \item{QUARTIC\_BOND}
  \begin{equation}
  U=\frac{1}{2} p_0 \left(r-p_1\right)^2+ \frac{1}{3} p_2 \left(r-p_1\right)^3+ \frac{1}{4} p_3 \left(r-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, $p_2/k_B$ in K/\AA$^3$, and $p_3/k_B$ in K/\AA$^4$.

  \item{CFF\_QUARTIC\_BOND}
  \begin{equation}
  U=p_0 \left(r-p_1\right)^2+ p_2 \left(r-p_1\right)^3+ p_3 \left(r-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, $p_2/k_B$ in K/\AA$^3$, and $p_3/k_B$ in K/\AA$^4$.

  \item{MM3\_BOND}
  \begin{equation}
  U=p_0 \left(r-p_1\right)^2\left(1-2.55\left(r-p_1\right)+\frac{7}{12}\,2.55^2\,\left(r-p_1\right)^2\right)
  \end{equation}
  2 arguments: $p_0$ in units of mdyne/\AA\, molecule, $p_1$ in \AA.

  \item{RIGID\_BOND}\\
  Use for connections between rigid units.

  \item{FIXED\_BOND}\\
  Use for bonds constraint using the `SHAKE' and `RATTLE'-algorithm. Applies to Monte-Carlo, Molecular Dynamics, and minimization.

  \item{MEASURE\_BOND}\\
  A histogram of the bond-distance can be computed.

\end{itemize}

\subsection{Urey-Bradley potentials}

The Urey-Bradley potential is sometimes used to account for the repulsion between two atoms bound to a common
atom. In more modern force field they are replaced by bond/bend cross potentials.
Urey-Bradley are essentially just bonds between 1-3 nearest neighbor atoms and
the same range of potentials is offered as for 1-2 bonds in RASPA.

\begin{itemize}

  \item{HARMONIC\_UREYBRADLEY}
  \begin{equation}
  U=\frac{1}{2} p_0 \left(r-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA.

  \item{MORSE\_UREYBRADLEY}
  \begin{equation}
  U=p_0\left[\left(1-e^{-p_1\left(r-p_2\right)}\right)^2-1\right]
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ in \AA$^{-1}$, and $p_2$ in \AA.

  \item{LJ\_12\_6\_UREYBRADLEY}
  \begin{equation}
  U=\frac{p_0}{r^{12}}-\frac{p_1}{r^{6}}
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K\,\AA$^{12}$, and $p_1/k_B$ in units of K\,\AA$^6$.

  \item{LENNARD\_JONES\_UREYBRADLEY}
  \begin{equation}
  U=4 p_0 \left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^{6}\right]
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K, $p_1$ in \AA.

  \item{BUCKINGHAM\_UREYBRADLEY}
  \begin{equation}
  U=p_0 e^{-p_1 r}-\frac{p_2}{r^{6}}
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, and $p_2/k_B$ in K\,\AA$^6$.

  \item{RESTRAINED\_HARMONIC\_UREYBRADLEY}
  \begin{equation}
  U=\begin{cases}
      \frac{1}{2} p_0\left(r-p_1\right)^2 & \qquad \left|r-p_1\right|\leq p_2\\
      \frac{1}{2} p_0 p_2^2+p_0 p_2\left(\left|r-p_1\right|-p_2\right) & \qquad \left|r-p_1\right|> p_2
     \end{cases}
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, and $p_2$ in \AA.

  \item{QUARTIC\_UREYBRADLEY}
  \begin{equation}
  U=\frac{1}{2} p_0 \left(r-p_1\right)^2+ \frac{1}{3} p_2 \left(r-p_1\right)^3+ \frac{1}{4} p_3 \left(r-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, $p_2/k_B$ in K/\AA$^3$, and $p_3/k_B$ in K/\AA$^4$.

  \item{CFF\_QUARTIC\_UREYBRADLEY}
  \begin{equation}
  U=p_0 \left(r-p_1\right)^2+ p_2 \left(r-p_1\right)^3+ p_3 \left(r-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_1$ in \AA, $p_2/k_B$ in K/\AA$^3$, and $p_3/k_B$ in K/\AA$^4$.

  \item{MM3\_UREYBRADLEY}
  \begin{equation}
  U=p_0 \left(r-p_1\right)^2\left(1-2.55\left(r-p_1\right)+\frac{7}{12}\,2.55^2\,\left(r-p_1\right)^2\right)
  \end{equation}
  2 arguments: $p_0$ in units of mdyne/\AA\, molecule, $p_1$ in \AA.

 \item{RIGID\_UREYBRADLEY}\\
  Use for connections between rigid units.

  \item{FIXED\_UREYBRADLEY}\\
  Use for bonds constraint using the `SHAKE' and `RATTLE'-algorithm. Applies to Monte-Carlo, Molecular Dynamics, and minimization.

  \item{MEASURE\_UREYBRADLEY}\\
  A histogram of the Urey-Bradley distance can be computed.

\end{itemize}

\subsection{Bending potential}

The simplest approach for an angle potential is the harmonic potential
\begin{equation}
  U=\frac{1}{2} k \left(\theta-\theta_0\right)^2
\end{equation}
Angles are much softer than bonds, especially in zeolites where a Si-O-Si angle ranges
between 135 and 180 degrees.
A problem with all polynomial representations of angles is that angles of 180 degrees results
in singular point (unless the reference angle is 180 degrees). The case of 0 degree is not possible
due to repulsion of the i and k atoms in the i-j-k bend.
The singularity is due to the fact that the force expression of such a polynomial
contains a factor $1/\sin\left(\theta\right)$. A common solution is to use a trigonometric function
\begin{equation}
  U=\frac{1}{2} k \left[\cos\left(\theta\right)-\cos\left(\theta_0\right)\right]^2
\end{equation}
Note that close to the maximum these potentials have no restoring force, but for small distortions this
is not a problem.
The MM force fields use higher order terms. A six power term was needed to describe the highly bent
bicyclo[1.1.1]pentane. Cubic terms and higher become desirable when the bending is more then 10-15 degrees.
MM3 angle bending has been divided into in-plane and out-of-plane bending for planar trigonal centers.

\begin{itemize}

  \item{HARMONIC\_BEND,CORE\_SHELL\_BEND}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left(\theta_{ijk}-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/rad$^2$ and $p_1$ in degrees.

  \item{QUARTIC\_BEND}
  \begin{equation}
  U=\frac{1}{2} p_0 \left(\theta_{ijk}-p_1\right)^2+
     \frac{1}{3} p_2 \left(\theta_{ijk}-p_1\right)^3+
     \frac{1}{4} p_3 \left(\theta_{ijk}-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/rad$^2$, $p_1$ in degrees, $p_2/k_B$ in K/rad$^3$, and $p_3/k_B$ in K/rad$^4$.

  \item{CFF\_QUARTIC\_BEND}
  \begin{equation}
  U=p_0 \left(\theta_{ijk}-p_1\right)^2+
    p_2 \left(\theta_{ijk}-p_1\right)^3+
    p_3 \left(\theta_{ijk}-p_1\right)^4
  \end{equation}
  4 arguments: $p_0/k_B$ in units of K/rad$^2$, $p_1$ in degrees, $p_2/k_B$ in K/rad$^3$, and $p_3/k_B$ in K/rad$^4$.

  \item{HARMONIC\_COSINE\_BEND}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left(\cos\theta_{ijk}-\cos p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K and $p_1$ in degrees.

  \item{COSINE\_BEND}\\
  \begin{equation}
  U=p_0\left(1+\cos\left(p_1\theta_{ijk}-p_2\right)\right)
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ dimensionless, and $p_2$ in degrees.

  \item{MM3\_BEND}
  \begin{equation}
  \begin{split}
  U=\frac{1}{2}p_0 \left(\theta_{ijk}-p_1\right)^2\Bigl(1-0.014\left(\theta_{ijk}-p_1\right)+
   5.6\times10^{-5}\left(\theta_{ijk}-p_1\right)^2&-7\times10^{-7}\left(\theta_{ijk}-p_1\right)^3\\
   &+2.2\times10^{-8}\left(\theta_{ijk}-p_1\right)^4
   \Bigr)
  \end{split}
  \end{equation}
  2 arguments: $p_0$ in units of mdyne\,\AA/rad$^2$, $p_1$ in degrees.
  \item{MM3\_IN\_PLANE\_BEND}
  \begin{equation}
  \begin{split}
  U=\frac{1}{2}p_0 \left(\theta_{ijk}-p_1\right)^2\Bigl(1-0.014\left(\theta_{ijk}-p_1\right)+
   5.6\times10^{-5}\left(\theta_{ijk}-p_1\right)^2&-7\times10^{-7}\left(\theta_{ijk}-p_1\right)^3\\
   &+2.2\times10^{-8}\left(\theta_{ijk}-p_1\right)^4
   \Bigr)
  \end{split}
  \end{equation}
  2 arguments: $p_0$ in units of mdyne\,\AA/rad$^2$, $p_1$ in degrees. The bend is `in-plane' and only applicable to bends in a defined planar trigonal centers.
  The bend is dependend on the fourth atom of the trigonal center.

  \item{FIXED\_BEND}\\
   Use for bend-angle constraint using the `SHAKE' and `RATTLE'-algorithm. Applies to Molecular Dynamics and minimization.
   Does not work (yet) in Monte-Carlo.

  \item{MEASURE\_BEND}\\
  A histogram of the bend angle can be computed.

\end{itemize}

\subsection{Wilson inversion-bend potential}

\begin{figure}[t]
  \centering
  \includegraphics[width=7.5cm]{./Potentials/WilsonAnglePlus.jpg}
  \includegraphics[width=7.5cm]{./Potentials/WilsonAngleMinus.jpg}
  \caption{The definition of the Wilson inversion-bend angle $\chi$.
  On the left a positive Wilson angle, and on the right a negative Wilson angle.}
  \label{Fig: Wilson definition}
\end{figure}

Common planar molecule that contain a double bond or sp$^2$ hybridization form planar groups with trigonal centers.
For example: the carbon and nitrogen centers in formamide, and the carbon centers in benzene.
The mode of motion is different from bond stretching, bending, and internal rotation.
The associated harmonic potential is
\begin{equation}
  U=\frac{1}{2} k \left(\chi\right)^2
\end{equation}
with $\chi$ the out-of-plane angle. Two possible definitions are in use
\begin{enumerate}
\item{the distance of the central atom from the plane defined by the other three atoms (pyramid height),}
\item{the average angle between any bond that extends from the central atom and the plane defined by the
other two bonds}.
\end{enumerate}
Note that an alternative to the out-of-plane angle is the \emph{improper torsion} using
\begin{equation}
  U=\frac{1}{2} k \left(1-\cos 2\chi\right)
\end{equation}
The out-of-plane potential can also be used for non-planar structure, for example in united-atom for chiral
centers to avoid inversion of the chiral center. Another example of its use is coordination complexes
where now the plane of the ligands need no longer be defined exactly.
In square planar complexes it is necessary to define an average plane through the ligands
(usually the least-square plane).
Note that the definition include one central atom
which is listed as the second in $a-b-c-d$: $a$, $c$, and $d$ are bonded to the central atom $b$.
The inversion angle potential is the average of the three possible inversion angle terms.

\begin{itemize}
  \item{HARMONIC\_INVERSION}\\
 \begin{equation}
  U=\frac{1}{2}p_0\left(\chi_{ijk}-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/rad$^2$ and $p_1$ in degrees.
  \item{HARMONIC\_COSINE\_INVERSION}\\
 \begin{equation}
  U=\frac{1}{2}p_0\left(\cos\left(\chi_{ijk}\right)-\cos\left(p_1\right)\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K and $p_1$ in degrees.
  \item{PLANAR\_INVERSION}\\
 \begin{equation}
  U=p_0\left(1-\cos\left(\chi\right)\right)
  \end{equation}
  1 argument: $p_0/k_B$ in units of K.
  \item{MM3\_INVERSION}\\
  \begin{equation}
  \begin{split}
  U=\frac{1}{2}p_0 \left(\chi-p_1\right)^2\Bigl(1-0.014\left(\chi-p_1\right)+
   5.6\times10^{-5}\left(\chi-p_1\right)^2&-7\times10^{-7}\left(\chi-p_1\right)^3\\
   &+2.2\times10^{-8}\left(\chi-p_1\right)^4
   \Bigr)
  \end{split}
  \end{equation}
  2 arguments: $p_0$ in units of mdyne\,\AA/rad$^2$, $p_1$ in degrees.

  \item{FIXED\_INVERSION\_BEND}\\
   Use for inversion bend-angle constraint using the `SHAKE' and `RATTLE'-algorithm.
   Applies to Molecular Dynamics and minimization. Does not work (yet) in Monte-Carlo.
\end{itemize}

\begin{figure}[t]
  \centering
  \includegraphics[width=7.5cm]{./Potentials/TorsionAnglePlus.jpg}
  \includegraphics[width=7.5cm]{./Potentials/TorsionAngleMinus.jpg}
  \caption{The definition of the dihedral angle $\phi$: the angle between the planes formed by atoms a-b-c and b-c-d.
   On the left a positive dihedral angle, and on the right a negative dihedral angle.}
  \label{Fig: Torsion definition}
\end{figure}

\subsection{Torsion potential}

Intramolecular rotations about bonds do not occur freely. A possible description with a physical interpretation
is the three-term Fourier expansion
\begin{equation}
U=\frac{V_1}{2}\left[1+\cos\phi\right]+
  \frac{V_2}{2}\left[1-\cos2\phi\right]+
  \frac{V_3}{2}\left[1+\cos3\phi\right]
\end{equation}
\begin{enumerate}
\item{the 1 fold-term has been attributed to residual dipole-dipole interactions, to Van der Waal interactions,
or to any other direct interaction between atoms not accounted for otherwise,}
\item{the 2-fold arises from conjugation or hyper conjugation, being geometrically related to p orbitals,}
\item{and the 3-fold term has a steric (or bonding/anti-bonding) origin.}
\end{enumerate}
The values for 4-fold or higher are small and it is not known whether these are essential to include.
It may be that Van der Waals and dipole interactions already take care of these effects.
Torsions are even softer than bond angles. All possible values can be found in structures. Therefore, the
energy function must be valid over the entire range, the function must be periodic, and for reasons of
symmetry have stationary points at 0 and 180 degrees. The periodicity is the number of minima for the
potential, usually 3 for an sp$^3$-sp$^3$ bond and 2 for a conjugate bond.

The definition of a torsion includes two central and two terminal atoms. The term `torsional' means
an internal rigid rotation and `dihedral' means a rotation of two vicinal bonds about a middle bond.
\begin{itemize}
  \item{HARMONIC\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left(\phi_{ijkl}-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/rad$^2$, $p_1$ in degrees.

  \item{HARMONIC\_COSINE\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[\cos\left(\phi_{ijkl}\right)-\cos\left(p_1\right)\right]^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K, $p_1$ in degrees.

  \item{THREE\_COSINE\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K

  \item{MM3\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0,p_1,p_2$ in units of kcal/mol.

  \item{CFF\_DIHEDRAL}\\
  \begin{equation}
  U=p_0\left[1-\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1-\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

  \item{CFF\_DIHEDRAL2}\\
  \begin{equation}
  U=p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1+\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

  \item{SIX\_COSINE\_DIHEDRAL}\\
  The Ryckaert-Bellemans potentials is often used for alkanes, the use implies exclusion of VDW-interactions
  between the first and last atoms of the dihedral, and $\phi'=\phi-\pi$ is defined according to the
  polymer convention $\phi'(trans)=0$.
  \begin{align}
  U=&\sum_{n=0}^5 p_n \cos^n\left(\phi'_{ijkl}\right)\\
    =&p_0+p_1\cos\left(\phi'_{ijkl}\right)+p_2\cos^2\left(\phi'_{ijkl}\right)+p_3\cos^3\left(\phi'_{ijkl}\right)
    p_4\cos^4\left(\phi'_{ijkl}\right)+p_5\cos^5\left(\phi'_{ijkl}\right)
  \end{align}
  6 arguments: $p_0/k_B,\dots,p_5/k_B$ in units of K.
  Rewritten in terms of $\phi$ the potential reads
  \begin{equation}
  U=p_0-p_1\cos\left(\phi_{ijkl}\right)+p_2\cos^2\left(\phi_{ijkl}\right)
    -p_3\cos^3\left(\phi_{ijkl}\right)+p_4\cos^4\left(\phi_{ijkl}\right)
    -p_5\cos^5\left(\phi_{ijkl}\right)
  \end{equation}

  \item{TRAPPE\_DIHEDRAL}\\
  \begin{equation}
  U=p_0+p_1\left[1+\cos\left(\phi_{ijkl}\right)\right]+
        p_2\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
        p_3\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  4 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B$ in units of K.

  \item{CVFF\_DIHEDRAL}\\
  \begin{equation}
  U=p_0\left[1+\cos\left(p_1\phi_{ijkl}-p_2\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ dimensionless, and $p_2$ in degrees.

  \item{OPLS\_DIHEDRAL}\\
  \begin{equation}
  U= \frac{1}{2}p_0+
    \frac{1}{2}p_1\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_3\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  4 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B$ in units of K.

  \item{FOURIER\_SERIES\_DIHEDRAL}\\
  The general form of a Fourier expansion is:
  \begin{equation}
  U=\sum_{n=1}^6\left[a_n\cos\left(n\phi\right)+b_n\sin\left(n\phi\right)\right]
  \end{equation}
  This form uses equilibrium angles of 0 for $n=1,3,5$ and 180 for $n=2,4,6$
  \begin{equation}
  \begin{split}
  U=&\frac{1}{2}p_0\left[1+\cos\phi\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi\right)\right]+\\
    &\frac{1}{2}p_3\left[1-\cos\left(4\phi\right)\right]+
    \frac{1}{2}p_4\left[1+\cos\left(5\phi\right)\right]+
    \frac{1}{2}p_5\left[1-\cos\left(6\phi\right)\right]
  \end{split}
  \end{equation}
  6 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B,p_4/k_B,p_5/k_B$ in units of K.

  \item{FOURIER\_SERIES\_DIHEDRAL\_2}\\
  The general form of a Fourier expansion is:
  \begin{equation}
  U=\sum_{n=1}^6\left[a_n\cos\left(n\phi\right)+b_n\sin\left(n\phi\right)\right]
  \end{equation}
  This form uses equilibrium angles of 0 for $n=1,3,4,5,6$ and 180 for $n=2$
  \begin{equation}
  \begin{split}
  U=&\frac{1}{2}p_0\left[1+\cos\phi\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi\right)\right]+\\
    &\frac{1}{2}p_3\left[1+\cos\left(4\phi\right)\right]+
    \frac{1}{2}p_4\left[1+\cos\left(5\phi\right)\right]+
    \frac{1}{2}p_5\left[1+\cos\left(6\phi\right)\right]
  \end{split}
  \end{equation}
  6 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B,p_4/k_B,p_5/k_B$ in units of K.

  \item{FIXED\_DIHEDRAL}\\
   Use for dihedral-angle constraint using the `SHAKE' and `RATTLE'-algorithm.
   Applies to Molecular Dynamics and minimization. Does not work (yet) in Monte-Carlo.
\end{itemize}

\begin{center}
\shadowbox{
\begin{minipage}{16cm}
\begin{quote}
The following identities are convenient when dealing with torsions:
\begin{equation}
\begin{split}
\cos 1x &= \cos x\\
\cos 2x &= -1+2\cos^2 x\\
\cos 3x &= -3\cos x+4\cos^3x\\
\cos 4x &= 1-8\cos^2x+8\cos^4x\\
\cos 5x &= 5\cos x-20\cos^3x+16\cos^5x\\
\cos 6x &= -1+18\cos^2 x-48\cos^4 x+32\cos^6x
\end{split}
\end{equation}

\begin{equation}
\begin{split}
\sin 1x &= \sin x\\
\sin 2x &= (\sin x) (2\cos x)\\
\sin 3x &= (\sin x) (-1+4\cos^2 x)\\
\sin 4x &= (\sin x ) (-4\cos x +8\cos^3x)\\
\sin 5x &= (\sin x) (1-12\cos^2x+16\cos^4x)\\
\sin 6x &= (\sin x) (6\cos x-32\cos^3x+32\cos^5x)
\end{split}
\end{equation}
\end{quote}
\end{minipage}}
\end{center}

\subsection{Improper torsion potential}

\begin{figure}[t]
  \centering
  \includegraphics[width=7.5cm]{./Potentials/ImproperTorsionAnglePlus.jpg}
  \includegraphics[width=7.5cm]{./Potentials/ImproperTorsionAngleMinus.jpg}
  \caption{The most common (CVFF, DLPOLY) definition of the improper dihedral angle
   $\phi$: the angle between the planes formed by atoms `a-c-d' and `c-d-b'.
   On the left a positive improper dihedral angle, and on the right a negative improper dihedral angle.
   The atoms need to be listed in the order `a-c-d-b'. Note that an exchange of atoms `c' and `d' leads to a change
   of sign, but \emph{not} in magntitude.}
  \label{Fig: Improper Torsion definition}
  \includegraphics[width=7.5cm]{./Potentials/ImproperTorsionAngleTypeA.jpg}
  \includegraphics[width=7.5cm]{./Potentials/ImproperTorsionAngleTypeB.jpg}
  \caption{A second definition of the improper dihedral angle (CHARMM, AMBER). The central atom is `c', and the improper torsion
   is enter as `a-b-c-d'. Howevere, an exchange of terminal atoms leads to a change in magntitude and the improper torsion
   needs to be symmetrized by adding two additional improper torsions `b-d-c-a' and `d-a-c-b' and rescaling the force constant
   by a factor of $1/3$.}
\end{figure}

The improper torsion is an alternative for the out-of-plane angle, and a possible definition is
\begin{equation}
  U=\frac{1}{2} k \left(1-\cos 2\chi\right)
\end{equation}
It is termed `improper torsion' because it simply treats the four atoms in the plane as if they were
bonded in the same way as in a true torsional angle. Note that the definition include one central atom
which is listed as the second in $a-b-c-d$: $a$, $c$, and $d$ are bonded to the central atom $b$.
Improper torsions are often used to keep sp2 atoms planar and sp3 atoms in a tetrahedral geometry.

The CHARMM convention is to list the central atom first, while there are no rules how to order the other three atoms.
Hence, six possibilities exist for the definition of an improper torsion. The AMBER convention is that the out-of-plane
atom is listed in the third position and the order of the other atoms is determined alphabetically by atom type, and
by the atom number (i.e. the order in the molecule) when atom types are identical.

\begin{itemize}
  \item{HARMONIC\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left(\phi_{ijkl}-p_1\right)^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K/rad$^2$, $p_1$ in degrees.

  \item{HARMONIC\_COSINE\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[\cos\left(\phi_{ijkl}\right)-\cos\left(p_1\right)\right]^2
  \end{equation}
  2 arguments: $p_0/k_B$ in units of K, $p_1$ in degrees.

  \item{THREE\_COSINE\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

  \item{MM3\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0,p_1,p_2$ in units of kcal/mol.

  \item{CFF\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=p_0\left[1-\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1-\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

  \item{CFF\_IMPROPER\_DIHEDRAL2}\\
  \begin{equation}
  U=p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1+\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

  \item{SIX\_COSINE\_IMPROPER\_DIHEDRAL}\\
  The Ryckaert-Bellemans potentials is often used for alkanes, the use implies exclusion of VDW-interactions
  between the first and last atoms of the dihedral, and $\phi'=\phi-\pi$ is defined according to the
  polymer convention $\phi'(trans)=0$.
  \begin{align}
  U=&\sum_{n=0}^5 p_n \cos^n\left(\phi'_{ijkl}\right)\\
    =&p_0+p_1\cos\left(\phi'_{ijkl}\right)+p_2\cos^2\left(\phi'_{ijkl}\right)+p_3\cos^3\left(\phi'_{ijkl}\right)
    p_4\cos^4\left(\phi'_{ijkl}\right)+p_5\cos^5\left(\phi'_{ijkl}\right)
  \end{align}
  6 arguments: $p_0/k_B,\dots,p_5/k_B$ in units of K.
  Rewritten in terms of $\phi$ the potential reads
  \begin{equation}
  U=p_0-p_1\cos\left(\phi_{ijkl}\right)+p_2\cos^2\left(\phi_{ijkl}\right)
    -p_3\cos^3\left(\phi_{ijkl}\right)+p_4\cos^4\left(\phi_{ijkl}\right)
    -p_5\cos^5\left(\phi_{ijkl}\right)
  \end{equation}

  \item{TRAPPE\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=p_0+p_1\left[1+\cos\left(\phi_{ijkl}\right)\right]+
        p_2\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
        p_3\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  4 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B$ in units of K.

  \item{CVFF\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U=p_0\left[1+\cos\left(p_1\phi_{ijkl}-p_2\right)\right]
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K, $p_1$ dimensionless, and $p_2$ in degrees.

  \item{OPLS\_IMPROPER\_DIHEDRAL}\\
  \begin{equation}
  U= \frac{1}{2}p_0+
    \frac{1}{2}p_1\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_3\left[1+\cos\left(3\phi_{ijkl}\right)\right]
  \end{equation}
  4 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B$ in units of K.

  \item{FOURIER\_SERIES\_IMPROPER\_DIHEDRAL}\\
  The general form of a Fourier expansion is:
  \begin{equation}
  U=\sum_{n=1}^6\left[a_n\cos\left(n\phi\right)+b_n\sin\left(n\phi\right)\right]
  \end{equation}
  This form uses equilibrium angles of 0 for $n=1,3,5$ and 180 for $n=2,4,6$
  \begin{equation}
  \begin{split}
  U=&\frac{1}{2}p_0\left[1+\cos\phi\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi\right)\right]+\\
    &\frac{1}{2}p_3\left[1-\cos\left(4\phi\right)\right]+
    \frac{1}{2}p_4\left[1+\cos\left(5\phi\right)\right]+
    \frac{1}{2}p_5\left[1-\cos\left(6\phi\right)\right]
  \end{split}
  \end{equation}
  6 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B,p_4/k_B,p_5/k_B$ in units of K.

  \item{FOURIER\_SERIES\_IMPROPER\_DIHEDRAL\_2}\\  The general form of a Fourier expansion is:
  \begin{equation}
  U=\sum_{n=1}^6\left[a_n\cos\left(n\phi\right)+b_n\sin\left(n\phi\right)\right]
  \end{equation}
  This form uses equilibrium angles of 0 for $n=1,3,4,5,6$ and 180 for $n=2$
  \begin{equation}
  \begin{split}
  U=&\frac{1}{2}p_0\left[1+\cos\phi\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi\right)\right]+\\
    &\frac{1}{2}p_3\left[1+\cos\left(4\phi\right)\right]+
    \frac{1}{2}p_4\left[1+\cos\left(5\phi\right)\right]+
    \frac{1}{2}p_5\left[1+\cos\left(6\phi\right)\right]
  \end{split}
  \end{equation}
  6 arguments: $p_0/k_B,p_1/k_B,p_2/k_B,p_3/k_B,p_4/k_B,p_5/k_B$ in units of K.
  \item{FIXED\_IMPROPER\_DIHEDRAL}\\
   Use for improper-dihedral-angle constraint using the `SHAKE' and `RATTLE'-algorithm.
   Applies to Molecular Dynamics and minimization. Does not work (yet) in Monte-Carlo.

\end{itemize}

\section{Non-bonded potentials}

\subsection{Van der Waals potentials}

The general expression for Van der Waals potentials when using a cutoff distance is
\begin{equation}
 U_{ij}^{\text{VDW}}=\begin{cases}
    U_{ij}\left(r_{ij}\right)& \text{if }r_{ij}\leq r_c\\
    0 & \text{otherwise}
   \end{cases}
\end{equation}

\begin{itemize}

  \item{NONE}\\
  \begin{equation}
    U=0
  \end{equation}
  zero parameters.
\item{$\begin{array}{l}\text{LENNARD\_JONES}\\
      \text{LENNARD\_JONES\_SMOOTHED3}\\
      \text{LENNARD\_JONES\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=
      4 p_0 \left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^6\right]
  \end{equation}
  2 parameters: $p_0/k_B$ in units of K, and $p_1$ in \AA.
\item{$\begin{array}{l}\text{FEYNMAN\_HIBBS\_LENNARD\_JONES}\\
      \text{FEYNMAN\_HIBBS\_LENNARD\_JONES\_SMOOTHED3}\\
      \text{FEYNMAN\_HIBBS\_LENNARD\_JONES\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=4 p_0 \left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^6\right]
      +\frac{\hbar^2}{24 p_2 k_B T} 4 p_0\left[132\left(\frac{p_1}{r}\right)^{12}-30\left(\frac{p_1}{r}\right)^6\right]\frac{1}{r^2}
  \end{equation}
  3 parameters: $p_0/k_B$ in units of K, $p_1$ in \AA, and $p_2$ is the reduced mass in unified atomic mass units.

\item{$\begin{array}{l}\text{FEYNMAN\_HIBBS2\_LENNARD\_JONES}\\
      \text{FEYNMAN\_HIBBS\_LENNARD\_JONES2\_SMOOTHED3}\\
      \text{FEYNMAN\_HIBBS\_LENNARD\_JONES2\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=4 p_0 \left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^6\right]
      +4 p_0\left[132\left(\frac{p_1}{r}\right)^{12}-30\left(\frac{p_1}{r}\right)^6\right]\frac{p_2}{r^2}
  \end{equation}
  3 parameters: $p_0/k_B$ in units of K, $p_1$ in \AA, and $p_2$ in units of $\AA^2$.

  \item{LENNARD\_JONES\_SHIFTED\_FORCE}\\
  \begin{equation}
       U=4 p_0 \left\{\left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^6\right]-
           \left[\left(\frac{p_1}{r_c}\right)^{12}-\left(\frac{p_1}{r_c}\right)^6\right]+
           \left[12\left(\frac{p_1}{r_c}\right)^{12}-6\left(\frac{p_1}{r_c}\right)^{6}\right]\frac{\left(r-r_c\right)}{r_c}\right\}
  \end{equation}
  2 parameters: $p_0/k_B$ in units of K, and $p_1$ in \AA.

  \item{LENNARD\_JONES\_SHIFTED\_FORCE2}\\
  \begin{equation}
       4 p_0 \left\{\left[\left(\frac{p_1}{r}\right)^{12}-\left(\frac{p_1}{r}\right)^6\right]
       +\left[6\left(\frac{p_1}{r_c}\right)^{12}-3\left(\frac{p_1}{r_c}\right)^6\right]\frac{r^2}{r_c^2}
       +7\left(\frac{p_1}{r_c}\right)^{12}+4\left(\frac{p_1}{r_c}\right)^6\right\}
  \end{equation}
  2 parameters: $p_0/k_B$ in units of K, and $p_1$ in \AA.

\item{$\begin{array}{l}\text{POTENTIAL\_12\_6}\\
      \text{POTENTIAL\_12\_6\_SMOOTHED3}\\
      \text{POTENTIAL\_12\_6\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=
      \frac{p_0}{r^{12}}-\frac{p_1}{r^6}
  \end{equation}
   2 parameters: $p_0/k_B$ in units of K\,\AA$^{12}$, and $p_1/k_B$ in units of K\,\AA$^6$.

\item{$\begin{array}{l}\text{POTENTIAL\_12\_6\_2\_0}\\
      \text{POTENTIAL\_12\_6\_2\_0\_SMOOTHED3}\\
      \text{POTENTIAL\_12\_6\_2\_0\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=
      \frac{p_0}{r^{12}}+\frac{p_1}{r^6}+\frac{p_2}{r^2}+p_3
  \end{equation}
   4 parameters: $p_0/k_B$ in units of K\,\AA$^{12}$, $p_1/k_B$ in units of K\,\AA$^6$, $p_2/k_B$ in units of K\,\AA$^2$,
   and $p_3$ in units of K.

\item{$\begin{array}{l}\text{MORSE}\\
      \text{MORSE\_SMOOTHED3}\\
      \text{MORSE\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 \left[(1-{e^{-p_1*(r-p_2)}})^2-1\right]
  \end{equation}
   3 parameters: $p_0/k_B$ in units of K, $p_1$ in units of $\AA^{-1}$ and $p_2$ in units of \AA.

\item{$\begin{array}{l}\text{MORSE2}\\
      \text{MORSE2\_SMOOTHED3}\\
      \text{MORSE2\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 \left[e^{p_1*(1-r/p_2)}-2e^{(p_1/2)*(1-r/p_2)}\right]
  \end{equation}
   3 parameters: $p_0/k_B$ in units of K, $p_1$ in units of $\AA^{-1}$ and $p_2$ in units of \AA.

\item{$\begin{array}{l}\text{MORSE3}\\
      \text{MORSE3\_SMOOTHED3}\\
      \text{MORSE3\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 \left[\left(1-e^{\left(\frac{-\ln{2}}{2^{\nicefrac{1}{6}}-1}\right)\left(\frac{r}{p_2}-2^{\nicefrac{1}{6}}\right)}\right)^2-1\right]
  \end{equation}
   2 parameters: $p_0/k_B$ in units of K $p_2$ in units of \AA. This form of the Morse potential resembles the Lennard-Jones potential.

\item{$\begin{array}{l}\text{CFF\_9\_6}\\
      \text{CFF\_9\_6\_SMOOTHED3}\\
      \text{CFF\_9\_6\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=
      \frac{p_0}{r^{9}}-\frac{p_1}{r^6}
  \end{equation}
   2 parameters: $p_0/k_B$ in units of K\,\AA$^{9}$, and $p_1/k_B$ in units of K\,\AA$^6$.

\item{$\begin{array}{l}\text{CFF\_EPS\_SIGMA}\\
      \text{CFF\_EPS\_SIGMA\_SMOOTHED3}\\
      \text{CFF\_EPS\_SIGMA\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U_{ij}=
      p_0 \left[2\left(\frac{p_1}{r}\right)^{9}-3\left(\frac{p_1}{r}\right)^6\right]
  \end{equation}
  2 parameters: $p_0/k_B$ in units of K, and $p_1$ in \AA.

\item{$\begin{array}{l}\text{BUCKINGHAM}\\
      \text{BUCKINGHAM\_SMOOTHED3}\\
      \text{BUCKINGHAM\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
  U=
     p_0 e^{-p_1 r}-\frac{p_2}{r^{6}}
  \end{equation}
  3 parameters: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, and $p_2$ in K\,\AA$^6$. Warning: in literature sometimes $\rho=\frac{1}{p_1}$ is given,
  $\rho$ is usually around 0.3-0.4 \AA, $p_1$ is usually around 2-4 \AA$^{-1}$.

\item{$\begin{array}{l}\text{BUCKINGHAM2}\\
      \text{BUCKINGHAM2\_SMOOTHED3}\\
      \text{BUCKINGHAM2\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
  U=\begin{cases}
     10^{10}  & r<p_3\\
     p_0 e^{-p_1 r}-\frac{p_2}{r^{6}} & \text{otherwise}
    \end{cases}
  \end{equation}
  4 parameters: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, $p_2$ in K\,\AA$^6$, and $p_3$ in [\AA].
  Warning: in literature sometimes $\rho=\frac{1}{p_1}$ is given,
  $\rho$ is usually around 0.3-0.4 \AA, $p_1$ is usually around 2-4 \AA$^{-1}$.

\item{$\begin{array}{l}\text{MM3\_VDW}\\
      \text{MM3\_VDW\_SMOOTHED3}\\
      \text{MM3\_VDW\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U_{ij}= \begin{cases}
       \sqrt{p_0^i p_0^j}\left[1.84\times10^5 e^{-\frac{12}{P}}-2.25\,P^6\right] & \text{if }P\geq 3.02\\
       \sqrt{p_0^i p_0^j} 192.27 P^2  & \text{if }P<3.02
       \end{cases}
  \end{equation}
  with $P=\frac{p_1^i+p_1^j}{r_{ij}}$ and where $p_1^i$ and $p_1^j$ are the VDW radii of atoms $i$ and $j$,
  and $r_{ij}$ the separation distance in \AA\ between atoms $i$ and $j$. \\
  2 arguments: $p_0$ in units of kcal/mol, $p_1$ in units of \AA.

\item{$\begin{array}{l}\text{MATSUOKA\_CLEMENTI\_YOSHIMINE}\\
      \text{MATSUOKA\_CLEMENTI\_YOSHIMINE\_SMOOTHED3}\\
      \text{MATSUOKA\_CLEMENTI\_YOSHIMINE\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0e^{-p_1 r_{ij}}+p_2e^{-p_3 r_{ij}}
  \end{equation}
   4 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, $p_2/k_B$ in units of K, and $p_3$ in units of \AA$^{-1}$.

\item{$\begin{array}{l}\text{GENERIC}\\
      \text{GENERIC\_SMOOTHED3}\\
      \text{GENERIC\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 e^{-p_1 r}-\frac{p_2}{r^4}-\frac{p_3}{r^6}-\frac{p_4}{r^8}-\frac{p_5}{r^{10}}
  \end{equation}
  6 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, $p_2/k_B$ in units of K\,\AA$^{4}$, $p_3/k_B$ in units of K\,\AA$^{6}$,
  $p_4/k_B$ in units of K\,\AA$^{8}$, and $p_5/k_B$ in units of K\,\AA$^{10}$.

\item{$\begin{array}{l}\text{PELLENQ\_NICHOLSON}\\
      \text{PELLENQ\_NICHOLSON\_SMOOTHED3}\\
      \text{PELLENQ\_NICHOLSON\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 e^{-p_1 r}-f_6 \frac{p_2}{r^6}-f_8\frac{p_3}{r^8}-f_{10}\frac{p_4}{r^{10}}
  \end{equation}
  with
  \begin{equation}
   f_{2n}=1-\sum_{k=0}^{2n}\frac{\left(p_1 r_{ij}\right)^k}{k!} e^{-p_1 r_{ij}}
  \end{equation}
  5 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, $p_2/k_B$ in units of K\,\AA$^{6}$,
  $p_3/k_B$ in units of K\,\AA$^{8}$, and $p_4/k_B$ in units of K\,\AA$^{10}$.

\item{$\begin{array}{l}\text{HYDRATED\_ION\_WATER}\\
      \text{HYDRATED\_ION\_WATER\_SMOOTHED3}\\
      \text{HYDRATED\_ION\_WATER\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U= p_0 e^{-p_1 r}-\frac{p_2}{r^4}-\frac{p_3}{r^6}-\frac{p_4}{r^{12}}
  \end{equation}
  5 arguments: $p_0/k_B$ in units of K, $p_1$ in units of \AA$^{-1}$, $p_2/k_B$ in units of K\,\AA$^{4}$,
  $p_3/k_B$ in units of K\,\AA$^{6}$, and $p_4/k_B$ in units of K\,\AA$^{12}$.

\item{$\begin{array}{l}\text{MIE}\\
      \text{MIE\_SMOOTHED3}\\
      \text{MIE\_SMOOTHED5}\end{array}$}\\
The Mie-potential \cite{Mie1903}
  \begin{equation}
    U=
      \left(\frac{p_0}{r^{p_1}}-\frac{p_2}{r^{p_3}}\right)
  \end{equation}
   4 arguments: $p_0/k_B$ in units of K \AA$^{p_1}$, $p_1$ dimensionless,
  $p_2/k_B$ in units of K \AA$^{p_3}$, and $p_3$ dimensionless.

\item{$\begin{array}{l}\text{BORN\_HUGGINS\_MEYER}\\
      \text{BORN\_HUGGINS\_MEYER\_SMOOTHED3}\\
      \text{BORN\_HUGGINS\_MEYER\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U_{ij}=p_0 e^{p_1\left(p_2-r_{ij}\right)}-\frac{p_3}{r_{ij}^6}-\frac{p_4}{r_{ij}^8}
  \end{equation}
  5 arguments: $p_0/k_B$ in units of K, $p_1$ dimensionless, $p_2$ in units of \AA, $p_3/k_B$ in units of K\,\AA$^{6}$, and
  $p_4/k_B$ in units of K\,\AA$^{8}$.

\item{$\begin{array}{l}\text{HYDROGEN}\\
      \text{HYDROGEN\_SMOOTHED3}\\
      \text{HYDROGEN\_SMOOTHED5}\end{array}$}\\
  \begin{equation}
    U=
      \frac{p_0}{r^{12}}-\frac{p_1}{r^{10}}
  \end{equation}
   2 arguments: $p_0/k_B$ in units of K\,\AA$^{12}$, and $p_1/k_B$ in units of K\,\AA$^{10}$.

\end{itemize}

\subsection{Tail corrections}

\subsubsection*{energy}

\begin{equation}
 U^{\text{Tail}}=\frac{2 \pi}{V}\sum_a \sum_b N_a N_b \left[\int_{r_c}^\infty r^2 U\left(r\right)\, dr\right]
\end{equation}

\begin{tabular}{|l|l|}
\hline
potential & $\int_{r_c}^\infty r^2 U\left(r\right)$\\
\hline\hline
  LENNARD\_JONES &
      $\frac{4}{3}\,p_0\,p_1^3 \left[\frac{1}{3}\left(\frac{p_1}{r}\right)^{9}-\left(\frac{p_1}{r}\right)^3\right]$\\
  LENNARD\_JONES\_SHIFTED\_FORCE & -\\
\hline
\end{tabular}

\subsubsection*{pressure}
\begin{align}
 P^{\text{Tail}}=&-\sum_a \sum_b \frac{2\pi}{3V} N_a N_b \left[\int_{r_c}^\infty r^2\, r\frac{\partial U\left(r\right)}{\partial r}\, dr\right]\\
    =&\sum_a \sum_b\left(\frac{2\pi}{3V} r_c^3 N_a N_b U\left(r_c\right)+U^{\text{Tail}}\right)
\end{align}
\subsubsection*{chemical potential}
\begin{equation}
 \beta \mu^{\text{Tail}}=2 U^{\text{Tail}}
\end{equation}

\subsection{Electrostatics}

\subsubsection{Charge-charge interaction}
\begin{itemize}
\item{Ewald}\\
The potential energy for a system of charges in a periodic system can be written as
\begin{equation}
 U=U^{\text{real}}+U^{\text{rec}}
\end{equation}
where
\begin{equation}
 \begin{split}
 U^{\text{real}}
  &=\sum_{i<j} q_i q_j \frac{\text{erfc}\left(\alpha r_{ij}\right)}{r_{ij}}\\
 U^{\text{rec}}
  &=\frac{2\pi}{V}\sum_{\mathbf{k}\not=0}\frac{1}{k^2} e^{-\frac{k^2}{4\alpha^2}}
  \left(\left|\sum_{i=1}^N q_i\cos\left(\mathbf{k}\cdot\mathbf{r}_i\right)\right|^2+
   \left|\sum_{i=1}^N q_i\sin\left(\mathbf{k}\cdot\mathbf{r}_i\right)\right|^2\right)
 -\sum_i\frac{\alpha}{\sqrt{\pi}}q_i^2
 \end{split}
\end{equation}
where $q_i$ and $q_j$ are the charges of particle $i$ and $j$, respectively, $\mathbf{r}_i$ the position of atom $i$, $V$ the volume of the cell,
$\alpha$ a damping factor, $k$ the wavelength, and `erfc' the error function complement. The expression gives the \emph{exact} solution for charges
in a periodic system up to arbitrary precision. One part is computed in `real' space, and the long-range part is more conveniently computed in Fourier space.

\item{CoulombTruncated}
\begin{equation}
 U=\begin{cases}
    \sum_{i<j} \frac{1}{4\pi\epsilon}\frac{q_i q_j}{r_{ij}}& \text{if }r_{ij}\leq r_c\\
    0 & \text{otherwise}
   \end{cases}
\end{equation}

\item{CoulombShifted}
\begin{equation}
 U=\begin{cases}
    \sum_{i<j} \frac{q_i q_j}{4\pi \epsilon }\left(\frac{1}{r_{ij}}-\frac{1}{r_c}\right)& \text{if }r_{ij}\leq r_c\\
    0 & \text{otherwise}
   \end{cases}
\end{equation}

\item{CoulombSmoothed}
\item{Wolf}

\end{itemize}

\subsubsection{Charge-dipole interaction}

\begin{itemize}
\item{Ewald}\\
\item{CoulombTruncated}
\begin{equation}
 U=\begin{cases}
    \sum_{i,j} \frac{1}{4\pi\epsilon} \frac{-q_i}{r^2_{ij}}
      \left({\boldsymbol \mu}_j\cdot\mathbf{r}_{ij}\right)& \text{if }r_{ij}\leq r_c\\
    0 & \text{otherwise}
   \end{cases}
\end{equation}
\end{itemize}

\subsubsection{Dipole-dipole interaction}

\begin{itemize}
\item{Ewald}\\
\item{CoulombTruncated}
\begin{equation}
 U=\begin{cases}
    \sum_{i,j} \frac{1}{4\pi\epsilon}\frac{1}{r^3_{ij}}
    \left[{\boldsymbol \mu}_i\cdot {\boldsymbol \mu}_j-3\frac{\left({\boldsymbol\mu}_i\cdot\mathbf{r}_{ij}\right)
    \left(\mathbf{r}_{ij}\cdot{\boldsymbol \mu}_j\right)}{r^2_{ij}}\right]& \text{if }r_{ij}\leq r_c\\
    0 & \text{otherwise}
   \end{cases}
\end{equation}
\end{itemize}

\section{Bonded potentials cross terms}

\subsection{Bond-bond potential}

\begin{itemize}
  \item{CFF\_BOND\_BOND\_CROSS,CVFF\_BOND\_BOND\_CROSS}\\
  \begin{equation}
  U=p_0\left(r-p_1\right)\left(r'-p_2\right)
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K/\AA$^2$, $p_0$ and $p_1$ in \AA.
\end{itemize}

\subsection{Bond-bend potential}

\begin{itemize}
  \item{CFF\_BOND\_BEND\_CROSS,CVFF\_BOND\_BEND\_CROSS}\\
  \begin{equation}
  U=\left(\theta-p_0\right)\left[p_1\left(r-p_2\right)+p_3\left(r'-p_4\right)\right]
  \end{equation}
  5 arguments: $p_0$ in degrees, $p_1/k_B$ in units of K/\AA/rad, $p_2$ in \AA, $p_3/k_B$ in units of K/\AA/rad, $p_4$ in \AA.

  \item{MM3\_BOND\_BEND\_CROSS}
  \begin{equation}
  U=p_0\left[\left(r-p_1\right)+\left(r'-p_2\right)\right]\left(\theta-p_3\right)
  \end{equation}
  4 arguments: $p_0$ in mdyne/rad, $p_1$ and $p_2$ in \AA, and $p_3$ in degrees.

  \item{TRUNCATED\_HARMONIC}
  \begin{equation}
  U=\frac{1}{2}p_0\left(\theta-p_1\right)^2 e^{-\frac{r_{ij}^8+r_{ik}^8}{p_2^8}}
  \end{equation}
  3 arguments: $p_0/k_B$ in K/rad$^2$, $p_1$ in degrees, and $p_2$ in units of \AA.

  \item{SCREENED\_HARMONIC}
  \begin{equation}
  U=\frac{1}{2}p_0\left(\theta-p_1\right)^2 e^{-\left(\frac{r_{ij}}{p_2}+\frac{r_{ik}}{p_3}\right)}
  \end{equation}
  4 arguments: $p_0$ in K/rad$^2$, $p_1$ in degrees, $p_2$ and $p_3$ in units of \AA.

  \item{SCREENED\_VESSAL}
  \begin{equation}
  U=\frac{p_0}{8\left(\theta_{ijk}-\pi\right)^2}
    \left[\left(p_1-\pi\right)^2-\left(\theta_{ijk}-\pi\right)^2\right]^2
    e^{-\left(\frac{r_{ij}}{p_2}+\frac{r_{ik}}{p_3}\right)}
  \end{equation}
  4 arguments: $p_0$ in K/rad$^2$, $p_1$ in degrees, $p_2$ and $p_3$ in units of \AA.

  \item{TRUNCATED\_VESSAL}
  \begin{equation}
  U=p_0\left[
   \theta_{ijk}^{p_2}\left(\theta_{ijk}-p_1\right)^2
     \left(\theta_{ijk}+p_1-2\pi\right)^2-\frac{p_2}{2}\pi^{p_2-1}
      \left(\theta_{ijk}-p_1\right)^2\left(\pi-p_1\right)^3
    e^{-\frac{r_{ij}^8+r_{ik}^8}{p_3^8}}
    \right]
  \end{equation}
  4 arguments: $p_0$ in K/rad$^{4+p_2}$, $p_1$ in degrees, $p_2$ dimensionless, and $p_3$ in \AA.

\end{itemize}

\subsection{Bend-bend potential}

\begin{itemize}
  \item{CFF\_BEND\_BEND\_CROSS,CVFF\_BEND\_BEND\_CROSS}\\
  \begin{equation}
  U=p_0\left(\theta-p_1\right)\left(\theta'-p_2\right)
  \end{equation}
  3 arguments: $p_0$ in units of K/rad$^2$, $p_1$ and $p_2$ in units of degrees.

 \item{MM3\_BEND\_BEND\_CROSS}
  \begin{equation}
  U=-p_0\left(\theta-p_1\right)\left(\theta'-p_2\right)
  \end{equation}
  3 arguments: $p_0$ in units of mdyne/rad$^2$, $p_1$ and $p_2$ in units of degrees.
\end{itemize}

\subsection{Bond-torsion potential}

The bond-torsions potential correlates the torsion $i-j-k-l$ with the central bond $j-k$,
or with the two terminating bonds.

\begin{itemize}
 \item{MM3\_BOND\_TORSION\_CROSS}\\
The MM3 bond-torsion potential correlates the torsion $i-j-k-l$ with the central bond $j-k$
  \begin{equation}
  U=\frac{1}{2}p_0\left(r-p_3\right)\left(1+\cos\phi\right)+
    \frac{1}{2}p_1\left(r-p_3\right)\left(1+\cos2\phi\right)+
    \frac{1}{2}p_2\left(r-p_3\right)\left(1+\cos3\phi\right)
  \end{equation}
  4 arguments: $p_0,p_1,p_2$ in units of kcal/mol, $p_3$ the reference length of the central bond in \AA.
\end{itemize}

\subsection{Bend-torsion potential}

\begin{itemize}
  \item{CFF\_BEND\_TORSION\_CROSS,CVFF\_BEND\_TORSION\_CROSS}\\
  \begin{equation}
  U=p_0\left(\theta-p_1\right)\left(\theta'-p_2\right)\cos\phi
  \end{equation}
  3 arguments: $p_0$ in units of K/rad$^3$, $p_1$ and $p_2$ in units of degrees.
\item{SMOOTHED\_DIHEDRAL}
  \begin{equation}
  U=p_0\left(1+\cos(p_1\phi_{ijkl}-p_2\right)S\left(\theta_{ijk}\right)S\left(\theta_{jkl}\right)
  \end{equation}
  3 arguments: $p_0/k_B$ in units of K/rad$^2$, $p_1$ dimensionless, and  $p_2$ in degrees.

\item{SMOOTHED\_THREE\_COSINE\_DIHEDRAL}
  \begin{equation}
  U=\left\{\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]\right\}
    S\left(\theta_{ijk}\right)S\left(\theta_{jkl}\right)
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

\item{SMOOTHED\_CFF\_DIHEDRAL}
  \begin{equation}
  U=\left\{p_0\left[1-\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1-\cos\left(3\phi_{ijkl}\right)\right]\right\}
    S\left(\theta_{ijk}\right)S\left(\theta_{jkl}\right)
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K.

\item{SMOOTHED\_CFF\_DIHEDRAL2}
  \begin{equation}
  U=\left\{p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    p_1\left[1+\cos\left(2\phi_{ijkl}\right)\right]+
    p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]\right\}
    S\left(\theta_{ijk}\right)S\left(\theta_{jkl}\right)
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K/rad.

\item{NICHOLAS\_DIHEDRAL}
 \begin{equation}
  U=\left\{\frac{1}{2}p_0\left[1+\cos\left(\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_1\left[1-\cos\left(2\phi_{ijkl}\right)\right]+
    \frac{1}{2}p_2\left[1+\cos\left(3\phi_{ijkl}\right)\right]\right\}
    S\left(\theta_{ijk}\right)
  \end{equation}
  3 arguments: $p_0/k_B,p_1/k_B,p_2/k_B$ in units of K/rad.

\item{SMOOTHED\_CFF\_BEND\_TORSION\_CROSS}

\begin{equation}
 U=S\left(\theta_1\right)
 \left[
 p_0*(Theta_1-p_1)*(\theta_2-p_2)\cos(\phi)
\right]
S\left(\theta_2\right)
\end{equation}
  3 arguments: $p_0/k_B$ in units K/rad$^3$, $p_1$ and $p_2$ in units of degrees.
\end{itemize}

\noindent The smoothing function $S\left(\theta\right)$ is defined as
\begin{equation}
 S\left(\theta\right)=\begin{cases}
  1 & \theta<\theta_{\text{on}}\\
  \left(\theta_{\text{off}}-\theta\right)^2
       \frac{\theta_{\text{off}}+2\theta-3\theta_{\text{on}}}
       {\left(\theta_{\text{off}}-\theta_{\text{on}}\right)^3}& \theta\geq\theta_{\text{on}}
  \end{cases}
\end{equation}
with $\theta_{\text{on}}=170^\circ$ and $\theta_{\text{off}}=180^\circ$.

\bibliographystyle{unsrt}
\bibliography{Potentials/biblio}


\documentclass{beamer}

\input{../../shared_slides.tex}
\DeclareMathOperator*{\perm}{perm}

\title{Matrix scaling}

\begin{document}
\maketitle
\frame{\tableofcontents}

\section{Introduction}%

\begin{frame}
  \frametitle{Introduction}
  \textbf{given:} a matrix $A \in \R^{m\times n}_{\ge0}$, vectors $r \in \R_{>0}^m$ and $c \in \R^n_{>0}$\\
  \textbf{find:} nonneg.\ diagonal matrices $X$ and $Y$ such that for
  \begin{equation}
    B = XAY
  \end{equation}
  it holds that:
  \begin{equation}
    B \mathbbm{1}_n = r \quad \text{and} \quad B^T \mathbbm{1}_m = c
  \end{equation}
  where $\mathbbm{1}_n = (1, \dots, 1)$ exactly $n$-times.
  Equivalently
  \begin{equation}
    \Vert B_{i,:} \Vert_1 = r_i \quad \text{and} \Vert B_{:, j} \Vert = c_j.
  \end{equation}

  \begin{block}{}
    \centering
    In this case $A$ is called $(r,c)$-scalable.
  \end{block}
  \onslide<2->{%
    If $\Vert r \Vert_1 \neq \Vert c \Vert_1$ this is not possible.
  }
\end{frame}

\begin{frame}
  \frametitle{Visualization of diagonal scaling}

  \begin{equation}
    \begin{aligned}
    B = \begin{bmatrix}
      x_1 & & & \\
      & x_2 & & \\
      & & \ddots & \\
      & & & x_m
    \end{bmatrix}
    A
    \begin{bmatrix}
      y_1 & & & \\
      & y_2 & & \\
      & & \ddots & \\
      & & & y_n
    \end{bmatrix}
    \\
    =
    \begin{bmatrix}
      a_{1,1}x_1y_1 & a_{1,2}x_1y_2 & \cdots & a_{1,n} x_1y_m \\
      \vdots   & \ddots & & \\
      a_{m,1}x_m y_1 & & \cdots & a_{m,n}x_m y_m
    \end{bmatrix}
    \end{aligned}
  \end{equation}
  \begin{block}{\textbf{Application:} Ill conditioned linear system $Az = b$.}
    Can multiply both sides by $X$  and substitute $z= Yv$ to get instead
    \begin{equation}
      XAY v = X b
    \end{equation}
  \end{block}
\end{frame}

\section{Matchings}%
\label{sec:}

\begin{frame}
  \frametitle{$(0-1)$ matrices | bipartite graphs}
  \begin{minipage}{0.5\textwidth}
    \begin{equation}
      \begin{bmatrix}
        0 & 1 & 1 \\
        1 & 0 & 0 \\
        0 & 1 & 1
      \end{bmatrix}
    \end{equation}
  \end{minipage}
  \begin{minipage}{0.35\textwidth}
    \begin{figure}[ht]
      \centering
      \includegraphics[width=\textwidth]{bipartite-graph.png}
      \caption{bipartite graph}
    \end{figure}
  \end{minipage}

    \begin{definition}
      \begin{itemize}
        \item A \textbf{matching} is a set of edges without common vertices.
        \item A \textbf{perfect matching} is a matching which covers all vertices.
      \end{itemize}
    \end{definition}
    Applications:
    \begin{itemize}
      \item marriage problem
      \item Hitchcock transport problem
    \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Finding the number of perfect matchings}
  Finding one is easy (polynomial time).
  Finding all is in \# P (i.e.\ hard!).
  \begin{block}{Consider $m=n$, $A\in \R^{n \times n}$}
    Recall:
    \begin{equation}
      \begin{aligned}
        &\text{(determinant)} \quad \det A = \sum_{\sigma} \sign(\sigma) \prod_{i=1}^n a_{i,\sigma(i)}  \\
        &\text{(permanent)} \quad \perm A = \sum_{\sigma} \prod_{i=1}^n a_{i,\sigma(i)}
      \end{aligned}
    \end{equation}
  \end{block}
  \begin{block}{Observation}
    For a $(0,1)-$matrix $A$, $\perm A$ is the number of perfect matchings.
    % This means that computing permanent must be hard.
  \end{block}
  One is easy to compute the other one hard. How can this be?
  % Diagionalize via gaussion elimination gives O(n^3) algorithm for computing determinant.
  % This doesn't work for permanent because it is not invariant under these operation (not multilinear).
\end{frame}

\section{Permanent}%
\label{sec:}

\begin{frame}
  \frametitle{Lower bounding the permanent}
  % This notion also appears in optimal transport
  \begin{definition}
    A matrix $A \in \R^{m\times n}_{\ge0}$ is called \textbf{doubly stochastic}, if sum of every row and every column is $1$.
  \end{definition}

  \begin{block}{van der Waerden (1926) conjectured}
    For doubly stochastic matrices the following \emph{lower bound} holds
    \begin{equation}
      \perm A \ge \frac{n!}{n^n}.
    \end{equation}
  \end{block}
  \begin{equation}
   \text{Is tight for } A = \begin{bmatrix}
      1/n & \cdots & 1/n \\
      \vdots & \ddots & 1/n \\
      1/n & \dots & 1/n
    \end{bmatrix}
  \end{equation}
  Proved in 1980.
\end{frame}

\begin{frame}
  \frametitle{Matrix scaling to approximate the permanent}
  \begin{block}{}
    If a $(0, 1)$-matrix $A$ can be scaled to be doubly stochastic, i.e.\ it is $\left(\mathbbm{1}, \mathbbm{1}\right)$-scalable, then
    we can apply lower bound
    \begin{equation}
      \perm B = \perm (XAY) = \left(\prod_i x_i\right) \left(\prod_j y_j\right) perm A
    \end{equation}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Matrix scaling as an optimization problem}
  \begin{itemize}
    \item \textbf{given:} $A, r, c$
    \item \textbf{find:} $X, Y$ such that $B=XAY$ fulfills $B\mathbbm{1}_m=r$ and $B \mathbbm{1}_n=c$.
    \item $m+n$ unknowns
          \item $m+n$ constraints
  \end{itemize}
  Consider the (\emph{nonconvex}) function
  \begin{equation}
    g(x,y) = \langle x, A y \rangle - \langle r, \log x \rangle - \langle c, \log y \rangle
  \end{equation}
  with derivative (coordinatewise)
  \begin{equation}
    \label{eq:grad-original-formulation}
    \begin{aligned}
      \nabla_x g(x,y) = Ay - \frac{r}{x} \\
      \nabla_y g(x,y) = A^T x - \frac{c}{y}.
    \end{aligned}
  \end{equation}
\end{frame}

\begin{frame}
  \frametitle{Reparametrizing this system}
  Via reparametrization $x= e^\xi$ and $y=e^{\eta}$ we get
  \begin{equation}
    f(\xi, \eta) = \sum_{i,j} a_{i,j} e^{\xi_i + \eta_j} - \langle r, \xi \rangle - \langle c, \eta \rangle
  \end{equation}
  which is \textbf{\emph{convex}}. It's gradient is given by
  \begin{equation}
    \label{eq:grad-convex-reformulation}
    \frac{\partial f}{\partial \xi_i} = \sum_{j=1}^{n} a_{i,j} e^{\xi_i + \eta_j} - r_i
  \end{equation}
  Easy to see that the optimality condition of~\eqref{eq:grad-convex-reformulation} and~\eqref{eq:grad-original-formulation} agree.\\
  $\Rightarrow$ the nonconvex function only has \emph{global} minimizers.
\end{frame}

\begin{frame}
  \frametitle{Matrix scaling as an optimization problem [contd]}
  It is easy to see that a solution $(x,y)$ of
  \begin{equation}
    \begin{aligned}
      Ay - \frac{r}{x} = 0 \\
      A^T x - \frac{c}{y} = 0
    \end{aligned}
  \end{equation}
  defines a solution to the \emph{matrix scaling} problem via $X=\diag x$ and $Y=\diag y$
  \begin{equation}
    \left(\begin{array}{c}
            a_{1 1} y_1 + a_{1 2} y_2 + \cdots \\
            a_{2 1} y_1 + a_{2 2} y_2 + \cdots \\
            a_{m 1} y_1 + a_{m 2} y_2 + \cdots \\
          \end{array}\right)
        \begin{array}{c}
          \cdot x_1 = r_1\\
          \cdot x_2 = r_2\\
          \cdot x_m = r_m
        \end{array}
      \end{equation}
\end{frame}

\begin{frame}
  \frametitle{}
  The question remains: how to minimize
  \begin{equation}
    g(x,y) = \langle x, A y \rangle - \langle r, \log x \rangle - \langle c, \log y \rangle
  \end{equation}
  \vspace{-0.5cm}
  \begin{block}{alternating minimization}
    Given a problem
    \begin{equation}
      \min_{x,y} \phi(x,y)
    \end{equation}
    repeat:
    \begin{align}
      x_{k+1} &= \argmin_x \phi(x,y_k) \\
      y_{k+1} &= \argmin_y \phi(x_{k+1}, y).
    \end{align}
  \end{block}
    Makes sense as long as the \textcolor{blue}{subproblems are easy} (e.g. convex).
  \begin{align}
    &\text{opt. cond.\ for $x$:} \quad Ay - \frac{r}{x} = 0 \\
    &\text{opt. cond.\ for $y$:} \quad A^T x - \frac{c}{y} = 0
  \end{align}
\end{frame}

% change this to algorithmx package
\begin{frame}
  \frametitle{Sinkhorn's Algorithm}

  \begin{block}{Sinkhorn '60}
    Given $(x_0, y_0)$, for $k=1,\dots$
    \begin{equation}
      \begin{aligned}
        x_{k+1}&= \frac{r}{A y_k} \\
        y_{k+1}&= \frac{c}{A x_{k+1}} \\
      \end{aligned}
    \end{equation}
  \end{block}
  Linear convergence if $a_{i,j} > 0$.
  \textbf{Q:} What if $A$ is not $(r,c)$-scalable?
  % Then the optimization problem has no solution as FOC are never fulfilled
\end{frame}

\end{document}


\chapter{Signed Distance Field Model Development and Data}\label{ch:appendix-a}

\section{Detailed Signed Distance Field Model Formulation}

\subsection{Fixed Distance Model Development}

The utilization of the signed distance field as a preconditioner for ray tracing
operations can be modeled as an evaluation of the combined probability space for
particles with a current position, $\vec{p}$, and a next physics event location,
$\vec{n}$, after traveling a distance, $d$. The fraction of this probability
space in which signed distance values can be used to rule out surface crossings
for next surface intersections is then considered to be the theoretical
utilization of the signed distance field. An initial form for this probability
space can found in Equation \ref{appeq:util_model}.

\begin{equation}
  \label{appeq:util_model}
\int_{V_{sphere}}\int_{V_{track}} p_p(r) p_n(d) \, \mathrm{d}V_{sphere}\mathrm{d}V_{track}
\end{equation}

In this model, the starting location of particles, $\vec{p}_{p}(r,\phi,\theta)$, is
uniformly distributed, $p_p(r)=1$, throughout a sphere of radius, $R$, centered
at the origin.  The location of the next event, $\vec{p}_{n}(d,\alpha,\beta)$, where $d$
is the distance traveled by the particle, $\alpha$ is the interior angle between
the particle's \textit{position} vector and the particle's sampled direction
vector, and $\beta$ represents an azimuthal angle for directions traveled with
angle of departure, $\alpha$.  Figure \ref{fig:model} depicts these variables, $r$,
$d$, and $\alpha$ more clearly.

The outer integral in Equation \ref{appeq:util_model} represents all possible particle
positions within the geometric sphere and expands to

\begin{equation}
\int_{0}^{R}\int_{0}^{2\pi}\int_{0}^{\pi}\int_{V_{track}} r^2\sin{\phi} \, \mathrm{d}\phi
\mathrm{d}\theta \mathrm{d}r \,  p_n(d) \mathrm{d}V_{track}
\end{equation}

The inner integral over $V_{track}$ then expands to

\begin{equation}
\small \int_{0}^{R}\int_{0}^{2\pi}\int_{0}^{\pi}\int_{0}^{\infty}\int_{0}^{2\pi}\int_{0}^{\pi}
r^2\sin{\phi} \, p_n(d) d^2 \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}\beta \mathrm{d}d \, \mathrm{d}\phi
\mathrm{d}\theta \mathrm{d}r
\end{equation}

Integration of $\phi$, $\theta$, and $\beta$ can now be performed with
the knowledge that they are symmetric with respect to the problem and
integration of $p_n(d)$ does not rely on them.

\begin{equation}
\small 8\pi^2  \int_{0}^{R}\int_{0}^{\infty}\int_{0}^{\pi} p_n(d) \,
r^2 \, d^2 \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}d \, \mathrm{d}r
\end{equation}

In order to represent particles traveling a fixed distance, the relationship in Equation \ref{appeq:pn_fixed}
is applied.

\begin{equation}
  \label{appeq:pn_fixed}
  p_n(d) = \frac{\delta(d-\lambda)}{d^{2}}
\end{equation}

The evaluation of this integral then gives a representation of all the query
space available to the problem

\begin{equation}
  \label{appeq:A_fixed}
\small A = 8\pi^2  \int_{0}^{R}\int_{0}^{\infty}\int_{0}^{\pi} \delta(d-\lambda) \,
r^2 \, \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}d \, \mathrm{d}r
\end{equation}

and represents all geometric query space, labeled $A$, for a sphere of radius,
$R$ and a fixed distance traveled, $\lambda$. The condition for preconditioner
utilization without error consideration is as follows

\begin{equation}
  SDV(\vec{p}) + SDV(\vec{n}) > |\vec{p}-\vec{n}|
  \label{appeq:condition_no_error}
\end{equation}
\begin{align*}
 &SDV - \, signed \, distance \, value \, function \\
 &\vec{p} - \, particle's \, current \, position \\
 &\vec{n} - \, particle's \, next \, event \, location \\
 &h - \, mesh \, step \, size \\
\end{align*}

To apply this within the spherical geometry, the signed distance function of a
sphere with radius, $R$, from Equation \eqref{eq:sdf_sphere} is applied

\begin{equation}
R-|\vec{p}| + R - |\vec{n}| >   |\vec{p}-\vec{n}|
\end{equation}

The right hand side of this inequality can be described as the distance
traveled, $d$, and the magnitude of $\vec{p}$ can be represented
by the variable $r$.

\begin{equation}
 R-r + R - |\vec{n}(d,\alpha,\beta)| > d
\end{equation}

Reducing the next event location, $\vec{n}(d,\alpha,\beta)$, into an expression
in terms of $r$, $d$, and $\alpha$ requires further examination of the
problem. Because the coordinates of $n$ depend on the current particle position,
the magnitude of $n$ with respect to the geometry origin must be obtained to get
a correct form for the signed distance value. Again, Figure \ref{fig:model} depicts the
value of $n$ graphically for reference. The magnitude of n can then be described
using the law of cosines as

\begin{equation}
|n(d,\alpha,\beta)| = \sqrt{r^2 + d^2 - 2rd \cos{\pi-\alpha}}
\end{equation}
inserting this into the inequality gives

\begin{equation}
R-r + R - \sqrt{r^2 + d^2 + 2rd \cos{\alpha}} > d
\end{equation}

The inequality has now been reduced to the three variables seen in Equation
\ref{appeq:A_fixed}: $r$, $d$, and $\alpha$. This can be applied to construct
limits of integration representing boundaries of space in which the SDF can be
utilized. As described in Chapter \ref{ch:preconditioning}, $\alpha_{min}$ can
be used as a limit on the integral over $\textrm{d}\alpha$. It is also mentioned
that $\alpha_{min}$ is undefined until $d > R-r$ as shown in Equations
\ref{appeq:alpha_min_below} and \ref{appeq:alpha_min}.

\begin{equation}
  d < R-r : \alpha_{min} = 0
  \label{appeq:alpha_min_below}
\end{equation}

\begin{equation}
  \alpha_{min} > \arccos\Bigg ( \frac{(2R-r-d)^2-d^2-r^2}{2 d r} \Bigg )
  \label{appeq:alpha_min}
\end{equation}

\begin{figure}[ht]
  \centering
  \includesvg{../images/model_cases_fixed_distance}{width=\textwidth}
  \caption[Depiction of utilization model scenarios.]{Depiction of modeling
    cases. Left: an example of a track for which $d < R - r$. Middle: an example
    of a track for which $R-r < d < R$ and can be preconditioned.  Right: an
    example of a track for which $R-r < d < R$ and cannot be preconditioned.}
  \label{appfig:modeling_cases}
\end{figure}

In order to account for the fact that the form of $\alpha_{min}$ is undefined
until $d = R-r$, a Heaviside function is applied before applying it as a limit
on the particle's angle of departure from the position vector. Similarly,
because the $\alpha_{min}$ condition is undefined after $d=R$ a Heaviside
function is used to limit the condition to $\pi$ for any distances traveled
larger than $R$.

\begin{equation}
  \small
  \begin{split}
  \alpha_{min} =& (H(d-(R-r))-H(d-R)) \arccos\Bigg ( \frac{(2R-r-d)^2-d^2-r^2}{2 d r} \Bigg ) \\
  &+ \pi \, H(d-R)
  \end{split}
  \label{appeq:alpha_min_heaviside}
\end{equation}

By inserting this condition as a lower limit of the $d\alpha$ integration,
Equation \ref{appeq:subs_a_cond} will give all utilized space, $US$, in the query space
of the simulation.

\begin{equation}
\small US = 8\pi^2  \int_{0}^{R}\int_{0}^{\infty}\int_{\boldsymbol{\alpha_{min}}}^{\pi} \delta(d-\lambda) \,
r^2 \, d^2 \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}d \, \mathrm{d}r
\label{appeq:subs_a_cond}
\end{equation}

Evaluating and simplifying this fully formed integral gives us the model
presented in Chapter \ref{ch:preconditioning}.

\begin{equation}
U_{theoretical} = \frac{US}{A} =  \frac{(1-H(\lambda-R))(2R-\lambda)(R-\lambda)}{2R^2}
\end{equation}

\subsection{Sampled Distance Model Development}

%% After the agreement of the simulation results and analytic model for signed
%% distance field utilization for the fixed distance traveled case, the simulation
%% was used to produce a similar set of results in which the distance is
%% sampled based on the standard probability for distance to interaction in a
%% medium with a cross section, $\Sigma$, or mean free path $\lambda
%% =1/\Sigma$. This results in the probability distribution function shown in
%% Equation \ref{appeq:pn_sampled} for the particle distance traveled in this scenario.

%% \begin{equation}
%%   \label{appeq:pn_sampled}
%% p_n(d) \propto \frac{e^{-\Sigma d}}{d^{2}} = \frac{e^{-\frac{d}{\lambda}}}{d^{2}}
%% \end{equation}

%% \begin{figure}[ht]
%% \centering
%% \includesvg{../images/sdf_sampled_dist_results}{width=0.5\textwidth}
%% \caption{Results of the model for the theoretical utilization limit with the
%% results of the simulation for a sampled distance traveled case.}
%% \label{fig:sdf_sampled_dist}
%% \end{figure}

%% %has to be in this section for latex reasons. grumble grumble...
%% \begin{table*}[!h]
%%   \centering
%%   \begin{tabular}{lcccc}
%%           \multicolumn{5}{l}{\textbf{\textit{Source Location:}} <0,0,-1>} \\
%%           \textbf{Implementation} & \textbf{ctme (min)} & \textbf{wall time
%%             (min)} & \textbf{time ratio} & \textbf{precond. utilization}\\
%%           \hline
%%           MCNP6 & 0.17 & 0.14 & 1 & N/A \\
%%           DAG-MCNP6 & 1841.33 & 1841.33 & ~11,000 & N/A \\
%%           DAG-MCNP6 w/ SDF & 0.48 & 0.46 & 2.82 & 0.94\\
%%           \multicolumn{5}{l}{} \\
%%           \multicolumn{5}{l}{\textbf{\textit{Source Location:}} <0,0,10>} \\
%%           \textbf{Implementation} & \textbf{ctme (min)} & \textbf{wall time
%%             (min)} & \textbf{time ratio} & \textbf{precond. utilization}\\
%%           \hline
%%           MCNP6 & 0.18 & 0.18 & 1 & N/A \\
%%           DAG-MCNP6 & 11.12 & 11.16 & 62 & N/A \\
%%           DAG-MCNP6 w/ SDF & 0.50 & 0.52 & 2.89 & 0.96 \\

%%   \end{tabular}
%%   \caption{Performance results for an MCNP6 test case involving electron
%%     transport of a 1 keV-100 keV photon source incident on an Fe/W target. 5,000
%%     histories were run in this test problem.}
%%   \label{tab:inp066_results}
%% \end{table*}

%% Following the same process as in the fixed distance case by plugging Equation \ref{appeq:pn_sampled} into
%% Equation \ref{appeq:subs_a_cond}, the utilization form for the sampled distance case is
%% shown in Equation \ref{appeq:sampled_limit}.

%% \begin{equation}
%%   \label{appeq:sampled_limit}
%%   U_{theoretical} = \frac{US}{A} = \frac{ \frac{1}{2} \lambda(R - 2 \lambda) e^{\frac{-R}{\lambda}} + \lambda^2 - \frac{3}{2} R \lambda + R^2 }{R^2}
%% \end{equation}

%% The results of this set of simulations can be seen in
%%  Figure \ref{fig:sdf_sampled_dist}. In this scenario, it is not expected that the
%% utilization will approach zero when $\lambda = 100\, cm$, as the actual distance
%% sampled may be considerably less than the provided mean free path for the
%% simulation. Overall utilization values in this scenario for $\lambda$ from 0 to
%% 100 cm remain higher than the corresponding fixed distance simulation cases as
%% is expected in a sampled distance case. Utilization values remain high for
%% relatively large increases in mesh step size, $h$. This is important to
%% application of the data structure given concerns regarding its potentially high
%% memory footprint for large volumes. For example if the utilization of the signed
%% distance field drops $~20\%$ when going from a step size of 1 cm to 6.21 cm, but
%% the memory footprint of the data structure will have decreased by a factor of
%% $6.21^3$ or $239.5$ as well. The optimization of the mesh step size with respect
%% to its effect on utilization will also need to be included in future models of
%% the utilization.

The sampled distance probability distribution presented in Chapter \ref{ch:preconditioning}
is as follows:

\begin{equation}
  p = \frac{e^{-\Sigma d}}{d^{2}} = \frac{e^{-\frac{d}{\lambda}}}{d^{2}}
\end{equation}

with distances sampled using the function

\begin{equation}
  d = -\lambda ln(c)
\end{equation}

where $c$ is randomly sampled with a uniform distribution between 0 and 1

An examination of the change of variables in the general form for the utilized
space from Equation \eqref{eq:util_model} gives

\begin{equation}
  \frac{dp}{dd} = -\frac{p}{\lambda}
\end{equation}

\begin{equation}
  d = 0 \rightarrow c = 1
\end{equation}

\begin{equation}
  d = \infty \rightarrow c = 0
\end{equation}

Resulting in an integral with the following form for the sampled distance model

\begin{equation}
  \int_{0}^{R}\int_{0}^{2\pi}\int_{0}^{\pi}\int_{1}^{0}\int_{0}^{2\pi}\int_{0}^{\pi}
-r^2\sin{\phi} \, \lambda c ln(c)^2 \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}\beta \mathrm{d}c \, \mathrm{d}\phi
\mathrm{d}\theta \mathrm{d}r
\end{equation}

As in the fixed distance case, the condition for $\alpha_{min}$ is a piece-wise function based on the distance traveled. The expression for d changes slightly for this case however, given that the integral is now being performed over the variable $c$.

\begin{equation}
  d < R-r : \alpha_{min} = 0
\end{equation}

\begin{equation}
  d > R-r : \alpha_{min} = \arccos\Bigg ( \frac{(2R-r-d)^2-d^2-r^2}{2 d r} \Bigg
  )
\end{equation}

Now that the distance traveled is being used to construct these two regions in
the model, this integral must be separated into two pieces, one with limits of $0$
to $R-r$ and another with limits $R-r$ to $R$. Based on the distance sampling
distribution, these values become

\begin{equation}
  d_{min} = R-r \rightarrow c_{max} = e^{(-\frac{(R-r)}{\lambda})}
\end{equation}

\begin{equation}
  d_{max} = R   \rightarrow c_{min} = e^{(-\frac{R}{\lambda})}
\end{equation}

and the resulting integral becomes

\begin{equation}
  \int_{0}^{R}\int_{0}^{2\pi}\int_{0}^{\pi}\int_{1}^{c_{max}}\int_{0}^{2\pi}\int_{0}^{\pi}
-r^2sin(\phi) \, \lambda c ln(c)^2 sin(\alpha) \, \mathrm{d}\alpha \mathrm{d}\beta \mathrm{d}c \, \mathrm{d}\phi
\mathrm{d}\theta \mathrm{d}r +
\end{equation}

\begin{equation}
  \int_{0}^{R}\int_{0}^{2\pi}\int_{0}^{\pi}\int_{c_{max}}^{c_{min}}\int_{0}^{2\pi}\int_{alpha_{min}}^{\pi}
-r^2\sin{\phi} \, \lambda c ln(c)^2 \sin{\alpha} \, \mathrm{d}\alpha \mathrm{d}\beta \mathrm{d}c \, \mathrm{d}\phi
\mathrm{d}\theta \mathrm{d}r
\end{equation}

The evaluation of this integral gives the final form of the analytic preconditioner limit from Equation \eqref{eq:sdf_model_sampled}

\begin{align}
    \begin{split}
    U_{sampled} = \frac{\frac{1}{2} \lambda (R - 2 \lambda) e^{-\frac{R}{\lambda}} + \lambda^{2} - \frac{3}{2} R \lambda + R^{2} }{R^{2}}
  \end{split}
\end{align}

%% It is quite possible that utilization in either th interior or exterior
%% resion of the sphere is being under represented. Regardless of the cause, examining the contribution to
%% utilization from each of these regions (shown in
%%  Figure \label{fig:util_region_contributions}) is an interesting endeavor.

%% \begin{figure}[ht]
%% \centering
%% \includesvg{../images/util_contributions}{width=\textwidth}
%% \caption{A plot of the total predicted utilization along with the contriubtions
%%   from the inner region ($d < R-r$) and outer region ($d > R-r$).}
%% \label{fig:util_region_contributions}
%% \end{figure}

%% Figure \ref{fig:util_region_contributions} depicts the utilization of the SDF
%% data structure for various values of the mean free path, $\lambda$.
%% When $\lambda$ is very small, particles' next event locations
%% rarely reach the outer region condition of $ d > R - r $. As $\lambda$ increases, particles are
%% more likely to enter that region and its contribution increases greatly. Then
%% as particles begin to travel distances on the order of the sphere radius the
%% utilization decreases. The interior region utilization acts as one would
%% expect. When particles travel small distances with respect to the sphere radius,
%% there is high utilization, but as the particles begin to travel further its
%% utilization rapidly decreases. The contribution from the outer region defines the
%% significance of using both the current position signed distance value as well as the
%% next event location's signed distance value to precondition ray fire calls in DAGMC.

\subsection{Inclusion of Error in Model Development}

As provided in Chapter \ref{ch:preconditioning}, the condition for avoiding a
ray fire call when including error associated with signed distance value
interpolation is:

\begin{equation}
  SDV(\vec{p}) + SDV(\vec{n}) > |\vec{p}-\vec{n}| + 2\varepsilon(h)
  \label{appeq:condition}
\end{equation}
\begin{align*}
 &SDV - \, signed \, distance \, value \, function \\
 &\vec{p} - \, particle's \, current \, position \\
 &\vec{n} - \, particle's \, next \, event \, location \\
 &h - \, mesh \, step \, size \\
 &\varepsilon(h) - \, error \, evaluation \, for \, signed \, distance \, values \\
\end{align*}

The limits of the $\alpha_{min}$ condition need to be adjusted yet again to
account for the error that will be subtracted from the signed distance values. This becomes a relatively straightforward process in which the error is also subtracted from the arguments to the Heaviside functions in Equation \ref{appeq:alpha_min_heaviside}. Accounting for interpolation error reduces the maximum possible distance the particle can travel and still be preconditioned. It also reduces the value of $d_{min}$ where $\alpha_{min}$ becomes non-zero.

\begin{equation}
\small
\begin{split}
\alpha_{min} =& \arccos\Bigg ( \frac{(2R-r-d-2\epsilon)^2-d^2-r^2}{2 d r} \Bigg )[H(d-(R-r-\epsilon))-H(d-(R-\epsilon))]  \\
&+ \pi \, H(d-(R-\epsilon))
\end{split}
\label{appeq:min_alpha_w_error}
\end{equation}

After these adjustments to the $\alpha_{min}$ condition, the integral can be evaluated and simplified to give the form found in Equation \eqref{eq:sdf_util_sampled_w_error}:

\begin{equation}
  US_{sampled} = \frac{(R-\epsilon) (\frac{1}{2} \lambda ( R - 2\lambda - \epsilon ) e^{\frac{-R + \epsilon}{\lambda}} + \lambda^{2} - \frac{3}{2}\lambda(R - \epsilon) + (R-\epsilon)^{2})}{R^3}
  \label{appeq:sdf_util_sampled_w_error}
\end{equation}

%% For a direct evaluation of the utilization based on
%% the mesh step size, $h$, the formula for the error can be substituted for
%% $\epsilon$ to give

%% \begin{equation}
%% \small
%% \begin{split}
%% \alpha_{min} =& (H(d-(R-r-\sqrt{3}h))-H(d-(R-\sqrt{3}h))) \arccos\Bigg ( \frac{(2R-r-d-2\sqrt{3}h)^2-d^2-r^2}{2 d r} \Bigg ) \\
%% &+ \pi \, H(d-(R-\sqrt{3}h))
%% \end{split}
%% \end{equation}


\hypertarget{haskell-expressions}{%
\section{Haskell Expressions}\label{haskell-expressions}}

In each of the following problems some declarations are given. Give the
most general type of each declared value, and if the value is not a
function, then also the result of evaluating it.

\hypertarget{lambda-expressions}{%
\subsection{Lambda expressions}\label{lambda-expressions}}

\begin{lstlisting}[language=Haskell]
f01 :: Num a => a -> a
f01 = \x -> 2 * x
f01' = \x -> 2 * x :: Num a => a -> a
f01'' () = \x -> 2 * x :: Num a => () -> a -> a
f01''' _ = \x -> 2 * x :: Num a => p -> a -> a

f02 = \x -> \y -> x + y :: Num a => a -> a -> a
f03 = \x y -> x + y :: Num a => a -> a -> a
f04 = = \y -> x + y ==> error (x not known)
f05 = \(x, y) -> x + y :: Num a => (a, a) -> a
f06 = \[x, y] -> x + y :: Num a => [a] -> a

f07 = [\x -> x +1, \x -> 2* x, \x -> x ^2] :: Num a => [a -> a]
f08 = head f07 5 :: Num a => a
f09 = \x -> x :: p -> p
f10 = [f09, \x -> x +1] :: Num a => [a -> a]
f11 = \_ -> (\x -> x +1 , \() -> 'a') :: Num a => p -> (a -> a, () -> Char)
\end{lstlisting}

\hypertarget{sections}{%
\subsection{sections}\label{sections}}

\begin{lstlisting}[language=Haskell]
x ^+^ y = x^2 + y^2
g01 = (^+^) :: Num a => a -> a -> a
g02 = (^+^2) :: Num a => a -> a
g03 = (3^+^) :: Num a => a -> a
g04 = (3^+^2) :: Num a => a

g05 x y = 2*x + 3*y :: Num a => a -> a -> a
g06 = (`g05` 2) :: Num a => a -> a
g07 = (2`g05`) :: Num a => a -> a
g08 = g06 3 :: Num a => a
g09 = g07 4 :: Num a => a

g10 x y z = 2*x + 3*y + 4*z :: Num a => a -> a -> a -> a
g14 x = (g10 (x +1)) :: Num a => a -> a -> a -> a
g15 = g14 2 3 4 :: Num a => a

g16 n = \x -> ([(+), (-), (*)] !! n ) x 2 :: Num a => Int -> a -> a
g17 = g16 1 5 :: Num a => a
\end{lstlisting}

\hypertarget{list-comprehensions}{%
\subsection{List comprehensions}\label{list-comprehensions}}

\begin{lstlisting}[language=Haskell]
h01 = [ x | x <- [1 .. 5]] :: (Num a, Enum a) => [a]
h02 = [2*x | x <- [1 .. 5]] :: (Num a, Enum a) => [a]
h03 = [ x-y | x <- [1 .. 3], y <- [1 .. 4]] :: (Num a, Enum a) => [a]
h04 = [ x-y | y <- [1 .. 3], x <- [1 .. 4]] :: (Num a, Enum a) => [a]
h05 = [ x+y | x <- [1 .. 3], y <- [1 .. 4], x >= y ] :: (Num a, Enum a, Ord a) => [a]
h06 = [ head x | x <- ["dimdi", "schnurpsel", "zumsel"]] :: [Char]
h07 = [ x | (x:_) <- ["dimdi", "schnurpsel", "zumsel"]] :: [Char]
h08 = [ xs | ('s':xs) <- ["dimdi", "schnurpsel", "zumsel"]] ==> error (s unkown)
\end{lstlisting}

\clearpage


% Copyright 2011-2015 David Hadka.  All Rights Reserved.
%
% This file is part of the MOEA Framework User Manual.
%
% Permission is granted to copy, distribute and/or modify this document under
% the terms of the GNU Free Documentation License, Version 1.3 or any later
% version published by the Free Software Foundation; with the Invariant Section
% being the section entitled "Preface", no Front-Cover Texts, and no Back-Cover
% Texts.  A copy of the license is included in the section entitled "GNU Free
% Documentation License".

\chapter{Defining New Problems}
\label{chpt:problems}

The real value of the MOEA Framework comes not from the algorithms and tools it provides, but the problems that it solves.  As such, being able to introduce new problems into the MOEA Framework is an integral aspect of its use.

Throughout this chapter, we will show how a simple multiobjective problem, the Kursawe problem, can be defined in Java, C/C++, and in scripting languages.  The formal definition for the Kursawe problem is provided below.

\begin{equation}
  \label{eq:mop}
  \begin{aligned}
    & \underset{\vect{x} \in \mathbb{R}^L}{\text{minimize}}
      & & F(\vect{x}) = (f_1(\vect{x}), f_2(\vect{x})) \\
    & \text{where}
      & & f_1(\vect{x}) = \sum_{i=0}^{L-1} -10\text{e}^{-0.2\sqrt{x_i^2 + x_{i+1}^2}}, \\
    & & & f_2(\vect{x}) = \sum_{i=0}^{L} \left|x_i\right|^{0.8} + 5\sin\left(x_i^3\right).
  \end{aligned}
\end{equation}

\begin{important}
The MOEA Framework only works on minimization problems.  If any objectives in your problem are to be maximized, you can negate the objective value to convert from maximization into minimization.  In other words, by minimizing the negated objective, your are maximizing the original objective.  See section \ref{sect:maximizing} for additional details on dealing with maximization objectives.
\end{important}

\section{Java}
Defining new problems in Java is the most direct and straightforward way to introduce new problems into the MOEA Framework.  All problems in the MOEA Framework implement the \class{Problem} interface.  The \class{Problem} interface defines the methods for characterizing a problem, defining the problem's representation, and evaluating solutions to the problem.  In practice, you should never need to implement the \class{Problem} interface directly, but can extend the more convenient \class{AbstractProblem} class.  \class{AbstractProblem} provides default implementations for many of the methods required by the \class{Problem} interface.  The code example below shows the Kursawe problem defined by extending the \class{AbstractProblem} class.

\begin{lstlisting}[language=Java]
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.EncodingUtils;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.AbstractProblem;

public class Kursawe extends AbstractProblem {

	public Kursawe() {
		super(3, 2);
	}

	@Override
	public Solution newSolution() {
		Solution solution = new Solution(numberOfVariables,
				numberOfObjectives);

		for (int i = 0; i < numberOfVariables; i++) {
			solution.setVariable(i, new RealVariable(-5.0, 5.0));
		}

		return solution;
	}

	@Override
	public void evaluate(Solution solution) {
		double[] x = EncodingUtils.getReal(solution);
		double f1 = 0.0;
		double f2 = 0.0;

		for (int i = 0; i < numberOfVariables - 1; i++) {
			f1 += -10.0 * Math.exp(-0.2 * Math.sqrt(
					Math.pow(x[i], 2.0) + Math.pow(x[i+1], 2.0)));
		}

		for (int i = 0; i < numberOfVariables; i++) {
			f2 += Math.pow(Math.abs(x[i]), 0.8) +
					5.0 * Math.sin(Math.pow(x[i], 3.0));
		}

		solution.setObjective(0, f1);
		solution.setObjective(1, f2);
	}

}
\end{lstlisting}

Note on line 9 in the constructor, we call \java{super(3, 2)} to set the number of decision variables (3) and number of objectives (2).  All that remains is defining the \java{newSolution} and \java{evaluate} methods.

The \java{newSolution} method is responsible for instantiating new instances of solutions for the problem, and in doing so defines the decision variable types and bounds.  In the \java{newSolution} method, we start by creating a new \class{Solution} instance on lines 14-15.  Observe that we must pass the number of decision variables and objectives to the \class{Solution} constructor.  Next, we define each of the decision variables and specify their bounds on lines 17-19.  For the Kursawe problem, all decision variables are real values ranging between $-5.0$ and $5.0$, inclusively.  Finally, we complete this method by returning the \class{Solution} instance.

The \java{evaluate} method is responsible for evaluating solutions to the problem.  A solution which has been generated by an optimization algorithm is passed as an argument to the \java{evaluate} method.  The decision variables contained in this solution are set to the values specified by the optimization algorithm.  The evaluate method must extract these values, evaluate the problem, and set the objective values.

Since the Kursawe problem contains all real-valued decision variables, we can cast the decision variables to an array using the helpful methods of the \class{EncodingUtils} class on line 26.  Use of \class{EncodingUtils} is encouraged for extracting the decision variables from a solution.  Then on lines 27 to 38, we use those decision variables to evaluate the Kursawe problem.  Finally, on lines 40-41, we assign the two objectives for this problem.

At this point, the problem is completely defined and can be used with the MOEA Framework.  To use this problem with the \class{Executor}, \class{Instrumenter} and \class{Analyzer} classes introduced in \chptref{chpt:executor}, you pass a direct references to the problem class using the \java{withProblemClass} method.  For example, we can optimize the Kursawe problem we just defined with the following code:

\begin{lstlisting}[language=Java]
new Executor()
		.withProblemClass(Kursawe.class)
		.withAlgorithm("NSGAII")
		.withMaxEvaluations(10000)
		.run();
\end{lstlisting}

Note how we pass the reference to the problem with \java{Kursawe.class}.  The name of the class, \java{Kursawe}, is followed by \java{.class}.  The MOEA Framework then calls the constructor for the problem class, which in this case is the empty (no argument) constructor, and proceed to optimize the problem.

Problems can also define constructors with arguments.  For example, consider a problem that needs to load data from a file.  For this to work, define a constructor in the problem class that accepts the desired inputs.  In this case, our constructor would be called \java{public ProblemWithArgument(File dataFile) { ... }}.  You can then solve this problem as shown below.

\begin{lstlisting}[language=Java]
new Executor()
		.withProblemClass(ProblemWithArgument.class,
        new File("inputFile.txt"))
		.withAlgorithm("NSGAII")
		.withMaxEvaluations(10000)
		.run();
\end{lstlisting}

\section{C/C++}
It is often the case that the problem / model / computer simulation you are working with is written in a different programming language, such as C/C++.  With a little work, it is possible to connect that C/C++ problem to the MOEA Framework and optimize its inputs / parameters.  In the following example, we will demonstrate how to connect the MOEA Framework to a simple C program.  We continue using the Kursawe problem, which if written in C would appear as follows:

\begin{lstlisting}[language=C]
#include <math.h>

int nvars = 3;
int nobjs = 2;

void evaluate(double* vars, double* objs) {
	int i;
	objs[0] = 0.0;
	objs[1] = 0.0;

	for (i = 0; i < nvars - 1; i++) {
		objs[0] += -10.0 * exp(-0.2 * sqrt(
				pow(vars[i], 2.0) + pow(vars[i+1], 2.0)));
	}

	for (i = 0; i < nvars; i++) {
		objs[1] += pow(abs(vars[i]), 0.8) +
				5.0 * sin(pow(vars[i], 3.0));
	}
}
\end{lstlisting}

Note how the \cpp{evaluate} method takes two arguments, \cpp{vars} and \cpp{objs}, which coincide with the inputs (the decision variables) and the outputs (the objective values).  Now we need to define how the \cpp{evaluate} method connects to the MOEA Framework.  This connection is established using the following code.

\begin{lstlisting}[language=C]
#include <stdlib.h>
#include "moeaframework.h"

int main(int argc, char* argv[]) {
	double vars[nvars];
	double objs[nobjs];

	MOEA_Init(nobjs, 0);

	while (MOEA_Next_solution() == MOEA_SUCCESS) {
		MOEA_Read_doubles(nvars, vars);
		evaluate(vars, objs);
		MOEA_Write(objs, NULL);
	}

	MOEA_Finalize();
	return EXIT_SUCCESS;
}
\end{lstlisting}

First, line 2 includes the \file{moeaframework.h} file.  This header is provided by the MOEA Framework and defines all the functions needed to communicate with the MOEA Framework.  All such functions begin with the prefix \cpp{MOEA\_}.  You can find the \file{moeaframework.h} file in the source code distribution in the folder \folder{examples/} along with additional examples.

Lines 4-18 define the main loop for the C/C++ program.  Lines 5-6 initialize the storage arrays for the decision variables and objectives.  Line 8 calls \cpp{MOEA\_Init} to initialize the communication between C/C++ and the MOEA Framework.  The \cpp{MOEA\_Init} method takes the number of objectives and constraints as arguments.  Once initialized, we can begin reading and evaluating solutions.  Line 10 loops as long as we successfully read the next solution using \cpp{MOEA\_Next\_solution()}.  Line 11 extracts the real valued decision variables, filling the array \cpp{vars}.  Line 12 invokes the \cpp{evaluate} method to evaluate the problem.  This results in the array \cpp{objs} being filled with the resulting objective values.  Line 13 writes the objectives back to the MOEA Framework.  The second argument to \cpp{MOEA\_Write} is \cpp{NULL} in this example, since the Kursawe problem is unconstrained.  This loop repeats until no more solutions are read.  At this point, the C/C++ program terminates by invoking \cpp{MOEA\_Finalize()} and exiting.  The complete source code is shown below.

\begin{lstlisting}[language=C]
#include <stdlib.h>
#include <math.h>
#include "moeaframework.h"

int nvars = 3;
int nobjs = 2;

void evaluate(double* vars, double* objs) {
	int i;
	objs[0] = 0.0;
	objs[1] = 0.0;

	for (i = 0; i < nvars - 1; i++) {
		objs[0] += -10.0 * exp(-0.2 * sqrt(
				pow(vars[i], 2.0) + pow(vars[i+1], 2.0)));
	}

	for (i = 0; i < nvars; i++) {
		objs[1] += pow(abs(vars[i]), 0.8) +
				5.0 * sin(pow(vars[i], 3.0));
	}
}

int main(int argc, char* argv[]) {
	double vars[nvars];
	double objs[nobjs];

	MOEA_Init(nobjs, 0);

	while (MOEA_Next_solution() == MOEA_SUCCESS) {
		MOEA_Read_doubles(nvars, vars);
		evaluate(vars, objs);
		MOEA_Write(objs, NULL);
	}

	MOEA_Finalize();
	return EXIT_SUCCESS;
}
\end{lstlisting}

You can save this C code to \file{kursawe.c} and compile it into an executable.  If using the GNU C Compiler (gcc), you can compile this code with the following command on Linux or Windows.  Note that you will need both \file{moeaframework.h} and \file{moeaframework.c} in the same directory as \file{kursawe.c}.

\begin{lstlisting}[language=bash,breakatwhitespace=true]
gcc -o kursawe.exe kursawe.c moeaframework.c -lm
\end{lstlisting}

At this point, we now switch back to Java and define the problem class by extending the \class{ExternalProblem} class.  We extend the \class{ExternalProblem} class instead of the \class{AbstractProblem} class since \class{ExternalProblem} understands how to communicate with the executable we just compiled.  The code snippet below shows the complete Java class for this example.

\begin{lstlisting}[language=Java]
import org.moeaframework.core.Solution;
import org.moeaframework.core.variable.RealVariable;
import org.moeaframework.problem.ExternalProblem;

public class ExternalKursawe extends ExternalProblem {

	public ExternalKursawe() {
		super("kursawe.exe");
	}

	public int getNumberOfVariables() {
		return 3;
	}

	public int getNumberOfObjectives() {
		return 2;
	}

	public int getNumberOfConstraints() {
		return 0;
	}

	@Override
	public Solution newSolution() {
		Solution solution = new Solution(getNumberOfVariables(),
				getNumberOfObjectives(), getNumberOfConstraints());

		for (int i = 0; i < numberOfVariables; i++) {
			solution.setVariable(i, new RealVariable(-5.0, 5.0));
		}

		return solution;
	}

}
\end{lstlisting}

Note how we still need to define the number of variables, objectives, and constraints in addition to defining the \java{newSolution} method.  However, we no longer include the \java{evaluate} method.  Instead, we reference the executable we previously created on line 8.  The MOEA Framework will launch the executable and use it to evaluate solutions to the problem.

Our work is now complete.  We can now solve this ``external'' version of the Kursawe problem just like the pure Java implementation shown earlier in this chapter.

\begin{lstlisting}[language=Java]
new Executor()
		.withProblemClass(ExternalKursawe.class)
		.withAlgorithm("NSGAII")
		.withMaxEvaluations(10000)
		.run();
\end{lstlisting}

\begin{tip}
It is helpful to test the C/C++ program manually prior to running it with the MOEA Framework.  Tests can be performed by launching the C/C++ program and manually typing in inputs.  In this example, the program requires 3 real values entered on a single line.
\end{tip}

\begin{lstlisting}[language=Plaintext]
-2.5 1.25 0.05
\end{lstlisting}

Once the enter key is pressed, the program will output the two objectives to the console:

\begin{lstlisting}[language=Plaintext]
-13.504159423733775 6.966377092192072
\end{lstlisting}

Additional inputs can be provided, or press Ctrl+D to terminate the program.

\section{Scripting Language}
Problems can also be defined in one of the many scripting languages available via the Java Scripting API.  Supported languages include Javascript, Python, Ruby, Scheme, Groovy and BeanShell.  Java SE 6 includes Rhino, a Javascript scripting engine, out-of-the-box.  The following code snippet shows the Rhino Javascript code for defining the Kursawe problem.

\begin{lstlisting}[language=JavaScript]
importPackage(java.lang);
importPackage(Packages.org.moeaframework.core);
importPackage(Packages.org.moeaframework.core.variable);

function getName() {
	return "Kursawe";
}

function getNumberOfVariables() {
	return 3;
}

function getNumberOfObjectives() {
	return 2;
}

function getNumberOfConstraints() {
	return 0;
}

function evaluate(solution) {
	x = EncodingUtils.getReal(solution);
	f1 = 0.0;
	f2 = 0.0;

	for (i = 0; i < getNumberOfVariables() - 1; i++) {
		f1 += -10.0 * Math.exp(-0.2 * Math.sqrt(
				Math.pow(x[i], 2.0) + Math.pow(x[i+1], 2.0)));
	}

	for (i = 0; i < getNumberOfVariables(); i++) {
		f2 += Math.pow(Math.abs(x[i]), 0.8) +
				5.0 * Math.sin(Math.pow(x[i], 3.0));
	}

	solution.setObjective(0, f1);
	solution.setObjective(1, f2);
}

function newSolution() {
	solution = new Solution(getNumberOfVariables(),
			getNumberOfObjectives());

	for (i = 0; i < getNumberOfVariables(); i++) {
		solution.setVariable(i, new RealVariable(-5.0, 5.0));
	}

	return solution;
}

function close() {

}
\end{lstlisting}

Note how all methods defined by the \class{Problem} interface appear in this code.  Also note how we can invoke Java methods and constructors through the scripting language.  The specifics of how to implement functions and invoke existing methods are specific to the scripting language chosen.  Refer to the documentation for the scripting language for details.

Save this script to an appropriate file with the correct file extension for the scripting language.  Since the script in this example is written in the Rhino Javascript language, we save the file to \file{kursawe.js}.  Solving this Javascript version of the Kursawe problem is nearly identical to all previous examples, as shown below.

\begin{lstlisting}[language=Java]
new Executor()
		.withProblemClass(ScriptedProblem.class,
				new File("kursawe.js"))
		.withAlgorithm("NSGAII")
		.withMaxEvaluations(10000)
		.run();
\end{lstlisting}

The only difference is on lines 2-3, where we specify the problem class as \java{ScriptedProblem.class} and pass as an argument the file \file{kursawe.js}.  The \java{ScriptedProblem} class loads the file, determines the appropriate scripting engine, and uses that scripting engine to evaluate solutions to the problem.

\section{Conclusion}
This chapter introduced the various means for introducing new problems to the MOEA Framework.  This includes implementing problems in Java, C/C++, and in one of the many supported scripting languages.  Care must be taken when choosing which language to use, as each method has different advantages and drawbacks.  One key consideration is the speed of each method.  The table below shows the wall-clock time for the three methods discussed in this chapter.  These times were produced on an Intel\copyright Core\texttrademark 2 Duo @ 2.13 GHz.

\par
\begin{center}
\begin{tabular}{ll}
  Method & Time (Seconds) \\
  \hline
  Java & 1.218 \\
  C/C++ & 4.011 \\
  Scripted (Javascript) & 24.874
\end{tabular}
\end{center}

Observe that using C/C++ incurs an overhead of approximately $0.000278$ seconds per evaluation.  For the simple Kursawe problem used as the example throughout this chapter, the overhead outweighs the evaluation time.  One would expect, however, that larger and more complex problems will benefit from potentially faster C/C++ implementations.  Furthermore, as one would expect, the scripted implementation in Javascript incurs a significant performance penalty.