\documentclass[]{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{amssymb}

%title material
\title{Astronomy 400B Lecture 5: Stellar Orbits}
\author{Brant Robertson}
\date{February, 2015}

%include latex definitions
\input{astro400B_definitions.tex}

%begin the document
\begin{document}

%make the title, goes after document begins
\maketitle

%first section
\section{Motion Under Gravity}

Newton's Law of Gravity point mass $M$ attracts another mass $m$ separated by
distance $\vr$, causing a change in momentum $m\vv$ of:

\begin{equation}
\frac{d}{dt}(m\vv) = -\frac{GmM}{r^{3}} \vr
\end{equation}
\noindent
where $G$ is Newton's gravitational constant. For an $N$-body system, we have
\begin{equation}
\frac{d}{dt}(m_{i}\vv_{i}) = -\sum_{j\ne i}\frac{G m_{i} m_{j}}{|\vx_i - \vx_j|^{3}} (\vx_i - \vx_j)
\end{equation}
\noindent
This equation can be re-written as
\begin{equation}
\frac{d}{dt}(m \vv_i) = - m \nabla\Phi(\vx_i)
\end{equation}
\noindent
where
\begin{equation}
\Phi(\vx_i) = - \sum_{i\ne j} \frac{G m_j}{|\vx_i - \vx_j|}
\end{equation}
\noindent
is the gravitational potential supplied by the point mass distribution at positions $\vx_i$.
Note we have chosen to define the potential such that $\Phi(x)\to0$ as $x\to\infty$ but
this is arbitrary. Note that
\begin{equation}
\nabla = \left[\frac{\partial}{\partial x},\frac{\partial}{\partial y},\frac{\partial}{\partial x}\right]
\end{equation}

\subsection{Continuous Matter Distributions}
Now consider a continuous distribution of matter density $\rho(\vx)$.  The
potential generated by $\rho(\vx)$ is given by
\begin{equation}
\label{eqn:potential}
\Phi(\vx) = - \int \frac{G\rho(\vx')}{|\vx - \vx'|}d^{3}\vx'
\end{equation}
\noindent
Note that the integral is performed over $\vx'$.  The force $\vF$ per unit mass
is
\begin{equation}
\vF(\vx) = - \nabla \Phi(\vx) = - \int \frac{G\rho(\vx')(\vx-\vx')}{|\vx-\vx'|^{3}}d^{3}\vx'
\end{equation}

\subsection{Poisson's Equation}

Take Equation \ref{eqn:potential} and apply the Laplacian operator
\begin{equation}
\nabla^{2} \equiv \nabla \cdot \nabla = \left[\frac{\partial^{2}}{\partial x^{2}}+\frac{\partial^{2}}{\partial y^{2}}+\frac{\partial^{2}}{\partial z^{2}}\right]
\end{equation}
\noindent
to both sides.  Remembering that the operator acts on $\vx$ and not $\vx'$, we have
\begin{equation}
\label{eqn:pois_init}
\nabla^{2} \Phi(\vx) = - \int G \rho(\vx') \nabla^{2} \left(\frac{1}{|\vx-\vx'|}\right)d^{3}\vx'.
\end{equation}
\noindent
We can evaluate this by noting that
\begin{equation}
\label{eqn:nabla}
\nabla\left(\frac{1}{|\vx - \vx'|}\right) = -\frac{\vx-\vx'}{|\vx-\vx'|^3}, \nabla^{2}\left(\frac{1}{|\vx - \vx'|}\right) = 0.
\end{equation}
\noindent
So we conclude that outside of a very small region around $\vx$, $\nabla^{2}\Phi(\vx)=0$.
Let's take a spherical region $S(\epsilon)$ of radius $\epsilon$ centered on $\vx$. In proceeding, let's
note that
\begin{equation}
\nabla^{2} f(|\vx-\vx'|) = \nabla_{\vx'}^{2} f(|\vx-\vx'|)
\end{equation}
\noindent
for any function $f(|\vx - \vx'|)$. If we take $\epsilon$ to be small enough such
that $\rho(\vx)\approx$ a constant, then we can write
\begin{eqnarray}
\label{eqn:pois_med}
\nabla^{2}\Phi(\vx) &\approx& - G\rho(\vx) \int_{S(\epsilon)} \nabla^2 \left( \frac{1}{|\vx - \vx'|} \right)d^{3}\vx' \nonumber \\
&=& - G \rho(\vx)  \int_{S(\epsilon)} \nabla_{\vx'}^2 \left( \frac{1}{|\vx - \vx'|} \right)d V'.
\end{eqnarray}
\noindent
Now we get to use the {\it divergence} theorem
\begin{equation}
\int \nabla^{2} f dV = \oint \nabla f \cdot dS,
\end{equation}
\noindent
which allows us to write Equation \ref{eqn:pois_med} as
\begin{equation}
- G \rho(\vx)  \int_{S(\epsilon)} \nabla_{\vx'}^2 \left( \frac{1}{|\vx - \vx'|} \right)d V' = - G \rho(\vx) \oint_{S(\epsilon)} \nabla_{\vx'}\left(\frac{1}{|\vx-\vx'|}\right) \cdot d\vS'
\end{equation}
\noindent
By applying Equation \ref{eqn:nabla} and the identity $\nabla_{\vx'} f = -\nabla f$, we have
\begin{eqnarray}
- G \rho(\vx) \oint_{S(\epsilon)} \nabla_{\vx'}\left(\frac{1}{|\vx-\vx'|}\right) \cdot d\vS' &=&
-G \rho(\vx) \oint_{S(\epsilon)} \left(\frac{\vx-\vx'}{|\vx-\vx'|^{3}}\right) \cdot d\vS' \nonumber \\
&=& 4 \pi G \rho(\vx)
\end{eqnarray}

\subsection{Inside a Uniform Shell}

The gravitational force inside a spherical shell of uniform density is zero.  The potential is a
constant.

{\bf See Figure 3.1 of Sparke and Gallagher.}

The opening angle OA is the same as OB, so the ratio of the enclosed mass is (SA/SB)$^{2}$.  Since
the ratio of the forces scale like the inverse of this ratio (from the inverse square law), the
force contributions of the A and B patches are equal and opposite.

\subsection{Gravitational Potential Outside a Uniform Spherical Shell}

{\bf See Figure 3.2 of Sparke and Gallagher}

We are calculating the potential a uniform spherical shell of mass $M$ and radius $a$.
Consider a point $P$ a distance $r$. The contribution of a narrow cone of opening
solid angle $\Delta\Omega$ around another point $Q'$ is
\begin{equation}
\Delta\Phi[\vx(P)] = -\frac{GM}{|\vx(P) - \vx(Q')|} \frac{\Delta\Omega}{4\pi}
\end{equation}
\noindent
Now consider the potential $\Phi'$ at point $P'$ at a radius $a$ away from the
center of a shell of the same mass $M$ but with a radius $r$.
The contribution $\Delta\Phi'$ from the material in the same cone of solid
angle $\Delta\Omega$ but at point $Q$ a distance $r$ away is
\begin{equation}
\Delta\Phi'[\vx(P')] = - \frac{GM}{|\vx(P') - \vx(Q)|}\frac{\Delta\Omega}{4\pi}
\end{equation}
\noindent
but since $PQ'=P'Q$, $\Delta\Phi[\vx(P)] = \Delta\Phi'[\vx(P')]$.
When we integrate over $4\pi$, we have
\begin{equation}
\Phi[\vx(P)] = \Phi'[\vx(P')]= \Phi'[\vx=0] = -\frac{GM}{r}
\end{equation}
\noindent
The force associated with this spherical shell is just $F(r) = \nabla \Phi[\vx(P)] = -\frac{GMm}{r}$.
So the force outside the shell is the same as for a point mass at distance $r$.

Inside a spherical mass distribution $\rho(r)$, the
centripetal acceleration that allows for a circular
orbit must be the radial gravitational force inwards.
On a circular orbit, in terms of the circular velocity
$V$ this acceleration is just
\begin{equation}
a = \frac{V^{2}(r)}{r} = -F(r) = \frac{GM(<r)}{r^2}.
\end{equation}
\noindent
For a point mass, $V(r)\propto r^{-1/2}$.  No extended
distribution can have a circular velocity curve that
declines more rapidly than $\propto r^{-1/2}$.

Note that the potential of a distributed mass density $\rho(\vx)$
is not the same as for a point mass.  Instead, we have
\begin{equation}
\Phi(r) = - \left[ \frac{GM(<r)}{r} + 4 \pi G \int_{r}^{\infty} \rho(r') r' dr'\right].
\end{equation}
\noindent
But as long as the spherical mass distribution has a finite size, eventually we
will have
\begin{equation}
\Phi(\vx) \to - \frac{G M_{\mathrm{tot}}}{|\vx|}
\end{equation}
\noindent
at large enough radius.

\subsection{Moving Through a Potential}

If we are moving through a background potential $\Phi(\vx)$ with
velocity $\vx$, the potential we experience changes
with time according to $d\Phi/dt = \vv\cdot\nabla\Phi(\vx)$.
We can re-write Newton's equation as
\begin{equation}
\vv \cdot \frac{d}{dt} ( m\vv) + m \vv\cdot\nabla \Phi(\vx) = 0 = \frac{d}{dt}\left[\frac{1}{2}m\vv^{2} + m \Phi(\vx)\right]
\end{equation}
\noindent
Therefore, the total energy
\begin{equation}
\label{eqn:total_energy}
E \equiv \frac{1}{2} m\vv^{2} + m\Phi(\vx) =~\mathrm{const}
\end{equation}
\noindent.
We can write of course that $E = KE + PE$, where $KE = \frac{1}{2}m\vv^{2}$ and $PE = m\Phi(\vx)$.
The kinetic energy cannot be negative, and we adopt $\Phi(\vx)\to0$ as $\vx\to\infty$.  At position
$\vx$, an orbit is unbound only if the total energy $E>0$. The speed at this place in the
orbit must exceed the escape speed, which is found by setting Equation \ref{eqn:total_energy} to zero.
We then have
\begin{equation}
v_{e}^{2} = - 2 \Phi(\vx).
\end{equation}

\subsection{Angular Momentum}

The angular momentum of an orbit is $L = \vx \times m\vv$. The time rate of change is
\begin{equation}
\frac{dL}{dt} = \vx \times \frac{d}{dt} (m \vv) = -m\vx \times \nabla \Phi.
\end{equation}
\noindent
For a spherically symmetric distribution, the force is central and $dL/dt = 0$ (angular
momentum is conserved).
In an axisymmetric distribution, on the component of $L$ parallel to the symmetry
axis is conserved.

\subsection{Individual Energy is Not Conserved in a Time-Dependent Potential}

In a many-body system, the total energy of each star is not individually conserved.
The time derivative of the kinetic energy of star $i$ is
\begin{equation}
\sum_{i} \vv_i \cdot \frac{d}{dt} (m_i \vv_i) = \frac{d}{dt} KE = -\sum_{i,j;i\ne j} \frac{Gm_i m_j}{|\vx_i - \vx_j|^{3}}(\vx_i - \vx_j) \cdot \vv_i
\end{equation}
\noindent
Doing the same calculation on star $j$ and taking the dot product with $\vv_j$ gives
\begin{equation}
\frac{1}{2}\sum_{j} \frac{d}{dt} (m_j \vv_j \cdot \vv_j) = - \sum_{i,j; i\ne j}\frac{Gm_i m_j}{|\vx_i - \vx_j|^{3}}(\vx_j - \vx_i)\cdot \vv_j
\end{equation}
\noindent
Adding the RHS of these two equations gives
\begin{equation}
- \sum_{i,j; i\ne j}\frac{Gm_i m_j}{|\vx_i - \vx_j|^{3}}(\vx_i - \vx_j)\cdot(\vv_i - \vv_j) = \sum_{i,j;i\ne j}\frac{d}{dt}\left(\frac{Gm_i m_j}{|\vx_i - \vx_j|}\right).
\end{equation}
The potential energy $PE$ is a sum of pairs of potentials from individual objects
\begin{equation}
PE = -\frac{1}{2} \sum_{i,j;i\ne j}\frac{Gm_i m_j}{|\vx_i - \vx_j|} = \frac{1}{2} \sum_i m_i \Phi(\vx_i) = \frac{1}{2} \int \rho(\vx)\Phi(\vx)dV.
\end{equation}
\noindent
We divided by two so every object contributes only once.

We can now see that, for the whole collection of objects
\begin{equation}
2 \frac{d}{dt} \left[ KE - \frac{1}{2} \sum_{i,j;i\ne j} \frac{G m_i m_j}{|\vx_i - \vx_j|}\right] = 0.
\end{equation}
\noindent
This means the total energy of the system is conserved.

\subsection{External Forces}

Consider the total force on an object $i$ in
a many body system under the influence of an external force $\vF_{\mathrm{ext}}$.
\begin{equation}
\sum_i \frac{d}{dt} (m_i \vv_i) \cdot \vx_i = -\sum_{i,j; i\ne j} \frac{Gm_i m_j}{|\vx_i - \vx_j|^{3}} (\vx_i - \vx_j) \cdot \vx_i + \sum_i \vF_{\mathrm{ext}}^{i} \cdot \vx_i.
\end{equation}
\noindent
The force on the $j$th object is
\begin{equation}
\sum_j \frac{d}{dt} (m_j \vv_j) \cdot \vx_j = - \sum_{i,j;i\ne j }\frac{G m_i m_j}{|\vx_i - \vx_j|^{3}}(\vx_j - \vx_i)\cdot\vx_j + \sum_j \vF_{\mathrm{ext}}^{j} \cdot \vx_j
\end{equation}
\noindent
The left hand sides of these equations are equal, and are equal to
\begin{equation}
\frac{1}{2} \sum_i \frac{d^2}{dt^2}(m_i\vx_i \cdot \vx_i) - \sum_i m_i \vv_i \cdot \vv_i = \frac{1}{2} \frac{d^2I}{dt^2} -2 KE
\end{equation}
\noindent
where the moment of inertia $I$ is
\begin{equation}
I \equiv \sum_i m_i \vx_i \cdot \vx_i.
\end{equation}
\noindent
By averaging the force on $i$ and $j$, we find
\begin{equation}
\frac{1}{2} \frac{d^2 I}{dt^2} - 2 KE = PE + \sum_i \vF_{\mathrm{ext}}^{i} \cdot \vx_i
\end{equation}
\noindent
and averaging this over a short time interval $0<t<\tau$ gives
\begin{equation}
\frac{1}{2\tau}\left[ \frac{dI}{dt}(\tau) - \frac{dI}{dt}(0)\right] = 2 \ave{KE} + \ave{PE} + \sum_i \ave{\vF_{\mathrm{ext}}^{i} \cdot \vx_i}
\end{equation}
\noindent
If all objects in the system are bound, then $|\vx_i \cdot \vv_i|$ and $dI/dt$ will be finite.  As $\tau\to\infty$, the LHS goes to zero.  Then we have
\begin{equation}
2\ave{KE} + \ave{PE} + \sum_i \ave{\vF_{\mathrm{ext}}^{i} \cdot \vx_i} = 0
\end{equation}

\section{Two-Body Relaxation}

A potential can be thought of as a combination of a smooth and steep potential wells.
We can calculate the average time between strong encounters with the steep
potential wells supplied near individual stars. Suppose that the
stars have mass $m$ and typical velocities $V$. If two stars come
within $r$ of one another, their kinetic energies increase to balance the
change in potential energy.  We say there is a {\it strong encounter}
if the change in potential energy is comparable to their starting
kinetic energy.  In other words
\begin{equation}
\frac{Gm^{2}}{r} \gtrsim \frac{mV^{2}}{2}
\end{equation}
\noindent
and the radius must be smaller than
\begin{equation}
r \lesssim r_{s} \equiv \frac{2Gm}{V^2}.
\end{equation}
\noindent
In the solar neighborhood, $V\approx30~\km~s^{-1}$, and taking $m\sim0.5\Msun$ we have $r_s \approx 1~$AU.
For the Sun, there has been no strong encounter for $\sim4.5~\Gyr$. Over a time $t$, the Sun
has an encounter with all stars in a cylinder with volume $\pi r_s^2 V t$. If there is a number
density of $n$, then we are interested in the time when $n\pi r_s^2 V t = 1$.  This time is
\begin{equation}
t_s = \frac{V^3}{4\pi G^2 m^3 n} \approx 4 \times 10^{12}~\yr~\left(\frac{V}{10~\km~s^{-1}}\right)^3\left(\frac{m}{\Msun}\right)^{-2}\left(\frac{n}{1~\pc^{-3}}\right)^{-1}.
\end{equation}
\noindent
For $n\approx0.1~\pc^{-3}$, then $t_s\sim10^{15}~\yr$.

\subsection{Weak Encounters}

Instead of strong encounters, we need to calculate the effects of weaker encounters
on indivudal stars.  We do this via the impulse approximation, that tells us how to
approximate the change in velocity from a weak encounter.  As one object passes
another, the perpendicular force between them is
\begin{equation}
\vF_{\perp} = \frac{GmMb}{(b^2 + V^2t^2)^{3/2}} = M \frac{dV_{\perp}}{dt}.
\end{equation}
\noindent
Integrating over time, we find that
\begin{equation}
\Delta V_{\perp} = \frac{1}{M} \int_{-\infty}^{\infty} \vF_{\perp}(t) dt =\frac{2 G m}{bV}.
\end{equation}
Slower approaches result in larger perpendicular velocity changes. The
path of $M$ is bent via
\begin{equation}
\alpha = \frac{\Delta V_{\perp}}{V} = \frac{2Gm}{bV^2}.
\end{equation}
\noindent
A weak encounter requires $b$ to be larger than $r_s$.

The number of stars with mass $m$ passing $M$ with
separations between $b$ and $b+\Delta b$ is the product of
the number density $n$ and the volume $V t \cdot 2 \pi b\Delta b$.
We multiply by $\Delta V_{\perp}^{2}$ and integrate over $b$ to find
\begin{equation}
\ave{\Delta V_{\perp}^{2}} = \int_{b_{min}}^{b_{max}} n V t\left(\frac{2Gm}{bV}\right)^2 2 \pi b db = \frac{8 \pi G^2 m^2 nt}{V} \ln \left(\frac{b_{max}}{b_{min}}\right).
\end{equation}
\noindent
After a time $t_{\mathrm{relax}}$ such that $\ave{\Delta V_{\perp}^2} = V^2$, the expected perpendicular
velocity becomes about equal to its original forward speed.  The initial path is forgotten!
Defining $\Lambda\equiv (b_{max}/b_{min})$, we find the relaxation time is much shorter than
the strong interaction time as
\begin{equation}
t_{\mathrm{relax}} = \frac{V^3}{8\pi G^2 m^2 n \ln \Lambda} = \frac{t_s}{2 \ln \Lambda}
\end{equation}
\begin{equation}
t_{\mathrm{relax}} \approx \frac{2\times10^9~\yr}{\ln \Lambda}\left(\frac{V}{10~\km~s^{-1}}\right)^3\left(\frac{m}{\Msun}\right)^{-2}\left(\frac{n}{10~\pc^{-3}}\right)^{-1}.
\end{equation}
\noindent
So what is $\Lambda$?  Well, if $b<r_s$ the method can't be correct and we usually take $b_{min} = r_s$.  We
can then take $b_{max}$ to be the size of the whole system.  For the Sun, $r_s = 1$~AU.  If we
take $300~\pc \leq b_{max} \leq 30~\kpc$, then $\Lambda\approx18-22$.  So the exact value of
$\Lambda$ doesn't matter, as $t_{\mathrm{relax}}\approx10^{13}~\yr$.

In a cluster consisting of $N$ stars with mass $m$ and typical $V$, the average separation is about
half the size of the system $R$.  We have
\begin{equation}
\frac{1}{2}NmV^2 \sim \frac{G(Nm)^2}{2R},~\Lambda = \frac{R}{r_s} \sim \frac{GmN}{V^2} \cdot \frac{V^2}{2Gm}\sim\frac{N}{2}.
\end{equation}
The crossing time of the system is $t_{cross} \sim R/V$.  Since $N=4n\pi R^3 /3$, we have
\begin{equation}
\frac{t_{relax}}{t_{cross}} \sim \frac{V^4 R^2}{6 N G^2 m^2 \ln \Lambda} \sim \frac{N}{6 \ln (N/2)}.
\end{equation}
\noindent
For $N\sim10^{11}$ stars, the relaxation will take $10^9$ crossing times.  For globulars with $10^6$ stars,
the relaxation time for the whole system is $t_{\mathrm{relax}} \sim 10^4 t_{cross}$.

\subsection{Effects of Two-Body Relaxation}

Consider a cluster of stars, each with mass $m$.
The {\it Maxwellian} distribution gives the fraction $f$ of stars with
velocities between $v$ and $v+\Delta v$ as
\begin{equation}
4\pi f_M(E) v^{2} \Delta v
\end{equation}
\noindent
where
\begin{equation}
f_M(E)\propto \exp\left(-\frac{E}{kT} \right) = \exp \left\{ -\left[ m\Phi(\vx) + \frac{mv^{2}}{2} \right]
 \Big/ kT \right\}.
\end{equation}
The temperature is related to the average kinetic energy as
\begin{equation}
\frac{1}{2}m\ave{\vv^{2}(\vx)} = \frac{3}{2}kT.
\end{equation}
\noindent
But there are stars at large energies, and these will evaporate. The kinetic
energy of stars that escape will typically be
\begin{equation}
\left\langle \frac{1}{2} m v_2^2(\vx) \right\rangle = - \frac{1}{N} \sum_i m_i \Phi(\vx_i) = -\frac{2}{N} PE = \frac{4}{N} KE = 6 kT
\end{equation}
The fraction of stars that escape will be
\begin{equation}
\int_{\sqrt{12kT/m}}^{\infty} f(E) v^2 dv \Big/ \int_0^{\infty} f(E) v^2 dv = 0.0074 \approx \frac{1}{136}
\end{equation}
\noindent
The evaporation time will be $t_{evap} \sim 136 t_{relax}$.  For globulars this is longer than
the age of the universe, but for open clusters it is a few gigayears.

\section{Orbits of Disk Stars and Epicycles}

Now we will treat orbits in axisymmetric potentials, and we will use cylindrical polar
coordinates $(R,\phi,z)$. In an axisymmetric potential, $\partial\Phi/\partial\phi = 0$, and
angular momentum about the $z$-axis is conserved.  We adopt $l_z$ at the angular
momentum per unit mass, we can write
\begin{equation}
\frac{d}{dt}(R^2 \dot{\phi}) = 0;~l_z \equiv R^2 \dot{\phi} =~\mathrm{constant}.
\end{equation}
\noindent
The radial equation of motion is
\begin{equation}
\label{eqn:radial_motion}
\ddot{R} = R \dot{\phi}^2 - \frac{\partial \Phi}{\partial R} = - \frac{\partial \Phi_{\mathrm{eff}}}{\partial R}
\end{equation}
\noindent
\begin{equation}
\Phieff \equiv \Phi(R,z) + \frac{l_z^2}{2R^2}.
\end{equation}
Multiplying \ref{eqn:radial_motion} by $\dot{R}$ and integrating informs us that
for a star in the midplane $z=0$
\begin{equation}
\frac{1}{2}\dot{R}^2 + \Phieff(R,z=0;l_z) = ~\mathrm{constant}.
\end{equation}
\noindent
If $l_z\ne0$, then there is an angular momentum barrier at the center of the galaxy that
limits $R$ to some minimum value.

The vertical equation of motion is
\begin{equation}
\ddot{z} = -\frac{\partial \Phi}{\partial z} (R,z) = -\frac{\partial \Phieff}{\partial z} (R,z)
\end{equation}
\noindent
If the potential is an even function about $z=0$ there is no vertical force in the midplane.

Let's expand the potential about the average radius $R_g$ of a star in a Taylor series and
truncate at the first term
\begin{equation}
\ddot{z} = -z \left[ \frac{\partial^2 \Phi}{\partial z^2}(R_g, z)\right]_{z=0} \equiv - \nu^2(R_g) z
\end{equation}
\noindent
where $\nu$ is an angular frequency.  The solution to this equation is
a harmonic oscillator
\begin{equation}
z(t) = Z \cos (\nu t + \theta)
\end{equation}
\noindent
where $Z$ and $\theta$ are constants.

Circular orbits with $\dot{R} = 0$ are only possible at a radius $R_g$ where $\Phieff =~\mathrm{constant}$.
At this radius
\begin{equation}
\frac{\partial \Phi}{\partial R}(R_g,z=0) = \frac{l_z^2}{R_g^3} = R_g \Omega^{2}(R_g)
\end{equation}
\noindent
where $\Omega(R)$ is the angular speed of a circular orbit in the disk plane.  If $\Phieff$ has a
minimum at $R_g$, then the circular orbit will have the lowest $l_z$.  Any larger $l_z$
will oscillate about the circular orbit, in an {\it epicycle} about a point that
moves with $\Omega(R_g)$ in a circular orbit of radius $R_g$.

Let's set $R = R_g+x$ in Equation \ref{eqn:radial_motion}, assume that $x\ll R$, and expand to
first order in $x/R$ and $z/R$.  We find that
\begin{equation}
\ddot{x} \approx - x\left[\frac{\partial^2 \Phieff}{\partial R^2}\right]_{R_g}\equiv - \kappa^2(R_g)x
\end{equation}
\noindent
and
\begin{equation}
x(t) \approx X \cos(\kappa t + \psi).
\end{equation}
\noindent
The constant $\kappa$ is called the epicyclic frequency when $\kappa^2>0$.  If $\kappa^2<0$,
the orbit is unstable.

Recalling that in a circular orbit
\begin{equation}
R \Omega^2(R) = \frac{\partial \Phi(R,z=0)}{\partial R}
\end{equation}
\noindent
we can write
\begin{equation}
\kappa^2(R) = \frac{d}{dR}[R\Omega^2(R)] + \frac{3 l_z^2}{R^4} = \frac{1}{R^3}\frac{d}{dR}[(R^2\Omega)^2] = -4B\Omega
\end{equation}
\noindent
where $B$ is Oort's constant.  In the solar neighborhood $B<0$ and $\kappa^2$ is positive and the Solar
orbit is stable.  If $R^2\Omega(R)$, the angular momentum of a circular orbit, increases with $R$ then
all larger circular orbits are stable.  Near a black hole this isn't the case, and the inner
most circular orbit is at $R=6GM/c^2$.

\subsection{Azimuthal Motion in Epicycles}

Since $R$ changes in an epicyclical orbit, the azimuthal velocity must change to
compensate to keep $l_z$ constant.  This means that
\begin{equation}
\dot(\phi) = \frac{l_z}{R^2} = \frac{\Omega(R_g)R_g^2}{(R_g + x)^2} \approx \Omega(R_g)\left(1 - \frac{2x}{R_g}\right).
\end{equation}
\noindent
We can integrate this equation to find that the azimuthal position with time varies as
\begin{equation}
\phi(t) = \phi_0  + \Omega(R_g)t - \frac{1}{R_g}\frac{2\Omega}{\kappa}X \sin(\kappa t + \psi)
\end{equation}
\noindent
The first two terms are circular motion starting from $\phi_0$ with constant angular
frequency $\Omega(R_g)$.  The third term is a harmonic oscillation with the same
frequency $\kappa$ as the $x$ motion but $\pi/2$ out of phase, and
larger by a factor $2\Omega/\kappa$.
The motion is also retrograde!

\subsection{$\kappa$ vs. $\Omega$}

For a point mass $\kappa = \Omega$.  For a uniform sphere $\kappa = 2\Omega$.
The galaxy has $\Omega < \kappa < 2\Omega$, and near the sun $\kappa \approx 1.4 \Omega$.

\subsection{Relative Speed of Nearby Stars}

The relative speeds of stars with $R_g > R_0$ are
\begin{equation}
v_y = R_0[\dot{\phi} - \Omega(R_0)]\approx R_0 \left[ \Omega(R_g) - 2x \frac{\Omega(R_g)}{R_g} - \Omega(R_0)\right]
\end{equation}
\noindent
but since $R_0 = R_g + x$ and dropping terms in $x^2$ we have
\begin{equation}
v_y \approx -x\left[ 2\Omega(R_0) + R_0\left(\frac{d\Omega}{dR}\right)_{R_0}\right] = -\frac{\kappa^2 x}{2\Omega} = 2Bx
\end{equation}
\noindent
Taking the average of stars we can measure, we find
\begin{equation}
\ave{v_y^2} = \left(\frac{\kappa^2}{2\Omega}\right)^2\ave{x^2} = \frac{\kappa^2}{4\Omega^2}\ave{v_x^2}
\end{equation}
\noindent
and since $\kappa<2\Omega$, $\ave{v_y^2}<\ave{v_x^2}$.  For the thin disk, we find that $2\lesssim\ave{v_x^2}/\ave{v_y^2}\lesssim3$.

\end{document}


\subsection{Demand Forecasting with Classical Forecasting Methods}
\label{class_methods}

Forecasting became a formal discipline starting in the 1950s and has its
    origins in the broader field of statistics.
\cite{hyndman2018} provide a thorough overview of the concepts and methods
    established, and \cite{ord2017} indicate business-related applications
    such as demand forecasting.
These "classical" forecasting methods share the characteristic that they are
    trained over the entire $Y$ first.
Then, for prediction, the forecaster specifies the number of time steps for
    which he wants to generate forecasts.
That is different for ML models.


\documentclass[10pt]{article}
\usepackage[a4paper, margin=1in]{geometry}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage[style=iso]{datetime2}
\usepackage{amsmath}
\usepackage{eufrak}
\usepackage{amssymb}
\usepackage{mathtools}

\newcommand{\defeq}{\vcentcolon=}

\title{Automatic Differentiation Variational Inference \\ Mean Field Simplification and Constraint Scalar}
\author{Harshad Deo \\
  \href{mailto:harshad@moreficent.com}{harshad@moreficent.com} \\
  \href{mailto:harshad@simianquant.com}{harshad@simianquant.com}
}
\date{}

\setlength{\parindent}{0cm}

\hypersetup{
  colorlinks,
  citecolor=blue,
  filecolor=blue,
  linkcolor=blue,
  urlcolor=blue
}

\setlength{\parskip}{\baselineskip}%

\begin{document}

\maketitle

This monograph is a follow up to the article introducing Auto Differentiation Variational Inference and develops simplifying
assumptions that allow the technique to be used to train large, complex models over large datasets, for example for Bayesian
Deep Learning.

\section*{Decoupling The Latent Space}

The full rank approximation, developed in the previous monograph, allows all the latent variables ($\theta$) to be coupled
with each other through the correlation matrix of the variational approximation. As a result, the number of parameters
grows quadratically with the size of the latent space. This makes it intractable for
use in all but the simplest problems, say with a few thousand latent variables.

The mean field approximation assumes that all the latent variables are independent. This makes it practical to scale the
models, as the number of parameters grows linearly with the size of the latent space. Formally,

\begin{align*}
  q(\zeta, \phi) \defeq& \mathcal{N}\big(\zeta; \mu, \text{diag}(\exp(\omega) ^2)\big) \\
  =& \prod_{k=1}^K \mathcal{N}(\zeta_k; \mu_k, \exp(\omega_k)^2)
\end{align*}

With this parameterization, the space of the variational parameters is given by:

\begin{equation*}
  \Phi = \{\mu_1, \ldots, \mu_k, \omega_1, \ldots, \omega_k\} = \mathbb{R}^{2K}
\end{equation*}

And the elliptical standardization is given by:

\begin{equation*}
  \eta_k = S_{\phi}(\zeta) = \frac{\zeta_k - \mu_k}{\exp \omega_k}
\end{equation*}

Correlation between the latent variables, if required, now needs to be modelled in explicitly as a function of the
latent variables. For example, the Lewandowski-Kurowicka-Joe transform \footnote{Lewandowski, D., Kurowicka, D., \& Joe, H. (2009). Generating random correlation matrices based on vines and extended onion method. Journal of multivariate analysis, 100(9), 1989-2001.}
can be used to explicitly model a correlation matrix for a subset of the latent variables. This transformation, as well as several
other useful ones, ship with the library.

\section*{Entropy Simplification}

The contribution of the entropy of the variational approximation to the ELBO can be simplified as:

\begin{align*}
  \frac{1}{2}\ln| \det \Sigma ^ 2| &= \frac{1}{2}\ln |\det \big(\text{diag}(\exp(\omega) ^ 2)\big)|  \\
  &= \frac{1}{2}\ln \prod_{k=1}^{K}\big(\exp(\omega_k)^2\big) \\
  &= \ln \prod_{k=1}^{K}\exp \omega_k \\
  &= \sum_{k=1}^{K}\omega_k
\end{align*}

Therefore the optimization objective can be simplified to:

\begin{equation*}
  \phi^* = \underset{\phi}{\text{argmax}} \, \mathbb{E}_{\mathcal{N}(\eta; 0, 1)}\big[\log p(x, T^{-1}(S_\phi^{-1}(\eta))) + \log |\det J_{T^{-1}}(S_\phi^{-1}(\eta))|\big] + \sum_{k=1}^{K}\omega_k
\end{equation*}

\section*{Joint Probability Simplification}

The joint probability of the data and the parameters, $p\big(x, T^{-1}(S_\phi^{-1}(\eta))\big)$ can be factorized as the product
of the probability of the data given the parameters, and the prior probability of the parameters. The objective can thus be
reexpressed as:

\begin{equation*}
  \phi^* = \underset{\phi}{\text{argmax}} \, \mathbb{E}_{\mathcal{N}(\eta; 0, 1)}\big[\log p(x | T^{-1}(S_\phi^{-1}(\eta))) + \log p(T^{-1}(S_\phi^{-1}(\eta))) + \log |\det J_{T^{-1}}(S_\phi^{-1}(\eta))|\big] + \sum_{k=1}^{K}\omega_k
\end{equation*}

It is observed that the prior probability acts as a regularizing term on the distribution of the latent space. This gives
a clear and simple interpretation to standard regularization techniques like $L_1$ and $L_2$ regularization as being equivalent to
assigning Laplace and Gaussian priors respectively, with some fixed scale parameters.

\section*{Graph Simplification}

To simplify the expression further, the computational graph is assumed to be directed and acyclic, and the family of
constraint relaxation functions, $T(\theta)$, is restricted to that for which the Jacobian of the inverse transform is
a triangular matrix. In practice, this is a benign assumption as it is true for practically all models and all commonly used
transformations. Since the Jacobian of a triangular matrix is the product of its diagonal elements,
the computational graph can be factorized into mutually exclusive subgraphs $U_1 \ldots U_M, M \leq K$ such that
$U_m, 1 < m \leq M$ is only dependent on $U_1 \ldots U_{m - 1}$. Let

\begin{align*}
  K_m \defeq&\quad \text{Indices of the latent space for the m-th subgraph} \\
  \tau_m \defeq&\quad \text{The contribution of the m-th subgraph to the jacobian of the inverse constraint transform} \\
  z_m \defeq&\quad \text{The sampled outputs of the m-th subgraph, implicitly being functions of $\phi$} \\
  \rho_m \defeq&\quad \text{The averaged log prior probability of the m-th subgraph} \\
  \mathcal{P}(\cdot) \defeq&\quad \text{The averaged log probability of the arguments}
\end{align*}

$\tau_m$ is equal to the sum of the diagonal elements in rows $K_m$ of the jacobian of the inverse constraint transform matrix. The optimization
objective can be reexpressed as:

\begin{equation*}
  \phi^* = \underset{\phi}{\text{argmax}} \, \mathcal{P}(x | z_M) + \sum_{m=1}^{M}\Big(\rho_m(z_1, \ldots, z_m ) + \tau_m + \sum_{j \in K_m}\omega_j \Big)
\end{equation*}

The first term can be interpreted as the log probability of the output given the input and the model. Each of the summands of the
second term can be interpreted as a \textit{constraint scalar} that ensures that the latent variables for the subgraph satisfy their
domain constraint and are regularized by their priors. Let $\mathcal{C}_m$ denote the constraint scalar for subgraph $m$. The optimization
objective can be reexpressed as:

\begin{equation*}
  \phi^* = \underset{\phi}{\text{argmax}} \, \mathcal{P}(x | z_M) + \sum_{m=1}^{M}\mathcal{C}_m(z_1, \ldots, z_m )
\end{equation*}

This can be interpreted by analogy with the architecture of modern deep learning libraries, like TensorFlow and PyTorch:

\begin{enumerate}
  \item $P$ is equivalent to the cost function. For example, if the output is normally distributed with a fixed variance, the optimization objective is mathematically identical to a Mean Square Error loss.
  \item Instead of a single tensor, each subgraph outputs a tuple. The first is a sample tensor of the output and the second is a scalar constraint term.
  \item All of the constraint scalars are summed and added to the loss given by the cost function.
\end{enumerate}

These parallels motivate the abstractions of the library.

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \input{./Ressources/Presentation/Preamble4to3.tex} % Layout 4:3
\input{./Ressources/Presentation/Preamble16to9.tex} % Layout 16:9
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\input{./_Config.tex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\Rplus}{\protect\hspace{-.1em}\protect\raisebox{.35ex}{\smaller{\smaller\textbf{+}}}}
\newcommand{\Cpp}{\mbox{C\Rplus\Rplus}\xspace}
\newcommand{\CppTw}{\mbox{C\Rplus\Rplus 20}\xspace}

\newcommand{\Datum}{\today}

\renewcommand{\PraesentationFusszeileZusatz}{| Amortized Time Complexity of Union-Find in Isabelle/HOL}

\title{Amortized Time Complexity of Union-Find in Isabelle/HOL \newline \newline {\Large Colloquium to the Bachelor's Thesis \newline}}
\author{\PersonTitel{} \PersonVorname{} \PersonNachname}
\institute[]{\UniversitaetName \\ \FakultaetName \\ \LehrstuhlName {\newline}
Supervisor: Prof. Tobias Nipkow, Ph.D. {\newline} Advisor: Maximilian P.L. Haslbeck, M.Sc.}
\date[\Datum]{\PersonStadt, October 16, 2019}
\subject{Amortized Time Complexity of Union-Find in Isabelle/HOL}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\input{./Ressources/Presentation/Beginning.tex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FOLIENSTIL: Standard
% !!!ÄNDERUNG HIER:!!!
\PraesentationMasterStandard
%\PraesentationMasterKopfzeileDreizeiler
%\PraesentationMasterWeissBlau

%\PraesentationStartseiteFlaggen
\PraesentationStartseiteUhrenturm
\PraesentationTitelseite % Fügt die Startseite ein

\begin{frame}
	\frametitle{Overview}
	\vspace{1cm}
	\begin{minipage}[s]{\textwidth}
	\tableofcontents
	\end{minipage}
\end{frame}

\section{Union-Find}
\begin{frame}
\frametitle{Union-Find}
\begin{itemize}
	\item Models a partial equivalence relation
over a finite domain
	\item Implemented by disjoint set forests
\item The graph structure is represented by an array
	\item Supports \textbf{Union} and \textbf{Find} operations
\end{itemize}
\vspace{-2cm}
\begin{figure}
	\centering
	\begin{minipage}{5cm}
		\caption{Two equivalence classes}\end{minipage}
	\begin{minipage}{8cm}\includegraphics[scale=1.0]{UnionFindFigure}\end{minipage}
\end{figure}
\end{frame}

\subsection{Path Compression and Union by Rank}
\begin{frame}
\frametitle{Path Compression and Union by Rank}
\begin{itemize}
	\item As with many tree-based data structures, trees should be kept flat
	\item This is done in two ways:
	\begin{enumerate}
		\item \textbf{Path Compression}
		\item \textbf{Union by Rank}
	\end{enumerate}
\end{itemize}
\begin{figure}
	\centering
	\begin{minipage}{5cm}
		\caption{Illustration of Path Compression}\end{minipage}
	\begin{minipage}{8cm}\includegraphics[scale=0.8]{PathCompressionFigure}\end{minipage}
\end{figure}
\end{frame}

\subsection{Design Choices}
\begin{frame}
	\frametitle{Design Choices}
	\begin{itemize}
		\item Done in Imperative/HOL, which can then be exported to several languages
		\item The arrays used and therefore the domain are static
		\item We define the operations in Imperative/HOL:
		\begin{itemize}
			\item \includegraphics{RepofDef}
			\item \includegraphics{CompressDef}
			\item \includegraphics{RepofcDef}
			\item \includegraphics{CmpDef}
			\item \includegraphics{UnionBigDef.png}
		\end{itemize}
	\end{itemize}
\end{frame}

\section{History of the Proof}
\begin{frame}
	\frametitle{History of the Proof}
	\begin{itemize}
		\item Several, slightly different results about the amortized time complexity of the operations of Union-Find
		\item The first result involving $\alpha$ by Tarjan in 1975. The proof was simplified until the version in CLRS
		\item In 1989 Fredman and Saks prove (in some sense) the optimality of the result
		\item The latest result by Alstrup et al. in 2014 tightens the bound
		\item In 2017, Charguéraud and Pottier formalize the result in Coq
		\item Now, we present this formalization in Isabelle/HOL
		\item In all these proofs, but especially since Alstrup et al., most work is needed for the abstract analysis
	\end{itemize}
\end{frame}

\section{Timeline of the Project}
\begin{frame}
	\frametitle{Timeline of the Project}
	\vspace{-0.7cm}
	\begin{table}
		\centering
		\begin{minipage}[t]{\linewidth}
			\color{gray}
			\ytl{May}{Background work: Familiarisation with Sepreftime and the paper by Charguéraud and Pottier}
			\vspace{-0.1cm}
			\ytl{May 15th}{Official thesis registration}
			\vspace{-0.1cm}
			\ytl{June}{Cleanup of the previous work by Lammich and Haslbeck}
			\vspace{-0.1cm}
			\ytl{June 5th}{Armaël Guéneau visits TUM}
			\vspace{-0.1cm}
			\ytl{June}{\textbf{Bottom-up}: Ackermann and InverseNatNat theories and important definitions}
			\vspace{-0.1cm}
			\ytl{July}{First abstract proofs: rank, level, index}
			\vspace{-0.1cm}
			\ytl{July 16th}{\textbf{Top-down}: Sanity check of definitions done, skipping the rest and proof of the Hoare-Triples}
			\vspace{-0.1cm}
			\ytl{August}{Exam phase}
			\vspace{-0.1cm}
			\ytl{September}{Parallel writing of the thesis, proof gap-filling and correction of definitions}
			\vspace{-0.1cm}
			\ytl{September 8th}{Complete sound theory}
			\vspace{-0.1cm}
			\ytl{Semptember 13th}{Submission of the thesis}
			\vspace{-0.1cm}
			\bigskip
		\end{minipage}%
	\end{table}
\end{frame}

\section{Overview of the Isabelle Proof}
\begin{frame}
	\frametitle{Overview}
	\vspace{-2.5cm}
	\begin{figure}
		\includegraphics[scale=0.8]{OverviewFigure}
	\end{figure}
\end{frame}

\section{Inverses of Functions $f: \mathbb{N} \rightarrow \mathbb{N}$}
\begin{frame}
	\frametitle{Inverses of Functions $f: \mathbb{N} \rightarrow \mathbb{N}$}
	\begin{minipage}[t]{0.55\linewidth}
		\vspace{-4cm}
	\begin{itemize}
		\item Defined in InverseNatNat.thy
		\item Upper inverse $\alpha_f$ and Lower inverse $\beta_f$
		\item Lemmas to change the proof obligations between inverses and the original function
		\item Used for Inverse Ackermann Function, but also the ``index'' of a node
	\end{itemize}
	\end{minipage}
\begin{minipage}[c]{0.4\linewidth}
\includegraphics[scale=0.7]{InverseNatNatFigure}
\end{minipage}
\end{frame}

\section{The Ackermann Function and its Inverse}
\begin{frame}
	\frametitle{The Ackermann Function and its Inverse}
	\begin{itemize}
		\item There is no single Ackermann Function ``$A$'' or inverse Ackermann Function ``$\alpha$''
		\item All definitions share the property of growing faster than any primitive recursive function
		\item $\alpha$ grows \textbf{very} slowly
		\item We use the definition of $A$ and $\alpha$ by Tarjan:
	\end{itemize}
\begin{minipage}{0.45\linewidth}
\begin{dfn} Ackermann Function
	\begin{align*}
	A \, 0 \, x &= x + 1  \\
	A \, (k + 1)\, x &= (A\, k)^{(x + 1)}\, x
	\end{align*}
\end{dfn}
\end{minipage}
\begin{minipage}{0.45\linewidth}
\begin{dfn} Inverse Ackermann Function
	\begin{align*}
	\alpha \, n &= \min \{ k \,\mid\, A\, k\, 1 \geq n \}\\
	\alpha_r \, n &= 1 + \min\{ k \,\mid\, A\, k\, r \geq (n + 1) \}
	\end{align*}
\end{dfn}
\end{minipage}

\begin{itemize}
	\item In Isabelle/HOL, the definitions are expressed slightly differently and make use of InverseNatNat.thy
\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{The Ackermann Function and its Inverse}
	\vspace{0.6cm}
	\begin{lma}{observable\_universe\_$\alpha$}
		\newline
		\textbf{Assume:} $n \leq 10^{80} $
		\begin{equation}
		\alpha \, n \leq 4 \label{universealpha}
		\end{equation}
	\end{lma}
\begin{itemize}
	\item $10^{80}$ is an estimate of the number of atoms in the universe
\end{itemize}

\begin{lma}{$\alpha$\_n\_0\_$\alpha$\_logn}
	\newline
	\textbf{Assume:} $16 \leq n$
	\begin{equation}
	\alpha \, n \leq 1 + (\alpha \, (\log{n}))
	\end{equation}
\end{lma}
\begin{itemize}
	\item $\alpha$ and $\alpha(\log\, n)$ are asymptotically equivalent
	\item True for every primitive recursive strictly monotonic function
\end{itemize}
\end{frame}

\section{Separation Logic with Time Credits}
\begin{frame}
	\frametitle{Separation Logic with Time Credits}
	\begin{itemize}
		\item Logic to reason about mutable resources in a heap. Enables Hoare-Triple definition
		\item It also allows for ``pure'' assertions, predicates independent of the heap content
		\item Time Credits can also appear in assertions and are required to execute atomic operations
		\item The components of the logic are: \begin{itemize}
			\item $\uparrow(P)$
			\item true and false
			\item $p \mapsto_a xs$
			\item $P_1 * P_2$
			\item $\exists_A x.\, P$
		\end{itemize}
	\item The Framework by Haslbeck implementing this for Isabelle/HOL, as well as this thesis as a usage example will be published in the AFP.
	\end{itemize}
\end{frame}

\subsection{Amortized Analysis with Time Credits}
\begin{frame}
	\frametitle{Amortized Analysis with Time Credits}
	\begin{itemize}
		\item We define a potential function $\Phi$ that measures the ``entropy'' of the data structure
		\item The idea is to ``hide'' $\Phi$ credits in the assertion defining the data structure
		\item If we are able to prove a Hoare-Triple of the form:
		\begin{equation*}
		\langle \mathrm{invar}(\mathcal{D}) * \$(\Phi(\mathcal{D})) * \underbrace{\$(f(\mathcal{D}))}_{\text{Advertised Cost}} \rangle
		\quad \mathtt{op}(\mathcal{D}) \quad
		\langle \mathrm{invar}(\mathcal{D'}) * \$(\Phi(\mathcal{D}')) \rangle
		\end{equation*}
		for any $\Phi$ and $f \in \mathcal{O}(g)$, we can conclude that the operation \texttt{op} has an amortized cost in $\mathcal{O}(g)$
		\item Remaining questions: \begin{itemize}
			\item What $\Phi$ do you choose? How does $\Phi$ evolve?
			\item How do you prove the functional correctness? \newline
			$\Longrightarrow$ Mathematical analysis of the data structure
		\end{itemize}
	\end{itemize}
\end{frame}

\section{The Potential Function $\Phi$}
\begin{frame}
	\frametitle{The Potential Function $\Phi$}
	\vspace{0.6cm}
	\begin{definition}{Potential for a single node}
		\begin{equation*}
		\philr i :=
		\begin{cases}
		\alpha_r\, (\RR_r\, i) \cdot (1 + (\RR_r\, i)) & \mathrm{if}\, \LL!i=i \,   \\
		(\alpha_r\, (\RR_r\, i) - \level i) \cdot \RR_r\, i - \iindex i + 1  &\mathrm{if}\, \alpha_r\, (\RR_r\, i) = \alpha_r\, (\RR_r\, (\LL!i))\\
		0 & \mathrm{otherwise}
		\end{cases}
		\end{equation*}
	\end{definition}

\begin{definition}{Potential of the data structure}
	\begin{equation}
	\Philr :=  \sum_{i = 0}^{|\LL| - 1}{\philr i}
	\end{equation}
\end{definition}

\begin{itemize}
	\item $\{0,\dots, |\LL|-1\}$ is in this case the domain of our equivalence relation
\end{itemize}

\end{frame}

\section{Results of the Thesis}
\begin{frame}
	\frametitle{Results of the Thesis}
	The $\,\,\mathrm{is\_uf}$ Assertion takes a relation and two arrays as arguments and ensures:\begin{itemize}
		\item The well-formedness of the disjoint set forest and the ranks
		\item That the relation modeled by the array is the one given
		\item That the necessary potential is stored as Time Credits
	\end{itemize}
	\vspace{0.6cm}
	\begin{dfn}
	\begin{align}
	\begin{split}
	\mathrm{is\_uf}\, \mathcal{X} \, (s,p) :=
	&\exists_A \LL\, \RR.\, p \mapsto_a \LL * s \mapsto_a \RR\, * \\
	&\uparrow (\ufaalpha = \mathcal{X} \land \mathrm{invar\_rank}\, \LL \, \RR)\, * \\
	&\$(4\cdot\Philr)
	\end{split}
	\end{align}
	\end{dfn}
\vspace{-0.5cm}
\end{frame}

\begin{frame}
	\frametitle{Results of the Thesis}
	\vspace{0.6cm}
	\begin{minipage}{0.48\linewidth}
\begin{lma}
	\begin{equation*}
	\mathrm{uf\_cmp\_time}\, \in \mathcal{O}(\alpha_r\,n)
	\end{equation*}
\end{lma}

\begin{thm}
	\,\newline
	\begin{align*}
	\begin{split}
	\langle \mathrm{is\_uf}\, \mathcal{X}\, u\, * &\$(\mathrm{uf\_cmp\_time}\, |\mathrm{Dom}\, \mathcal{X}|)\rangle \quad \\
	&\mathtt{uf\_cmp}\, u\, i\, j\\
	&\langle \mathrm{is\_uf}\, \mathcal{X}\, u\, * \uparrow(r \leftrightarrow (i,j)\in\mathcal{X}) \rangle_t
	\end{split}
	\end{align*}
\end{thm}
	\end{minipage}
	\begin{minipage}{0.40\linewidth}
\begin{lma}
	\begin{equation*}
	\mathrm{uf\_union\_time}\, \in \mathcal{O}(\alpha_r\,n)
	\end{equation*}
\end{lma}
\begin{thm}\,\newline
	\textbf{Assumes: } $i,j \in \mathrm{Dom}\, \mathcal{R}$
	\begin{align*}
	\begin{split}
	\langle \mathrm{is\_uf}\, \mathcal{X}\, u\, * &\$(\mathrm{uf\_union\_time}\, |\mathrm{Dom}\, \mathcal{X}|)\rangle \quad \\
	&\mathtt{uf\_union}\, u\, i\, j\\
	&\langle \mathrm{is\_uf}\, (\mathrm{per\_union}\, \mathcal{X}\, i\, j)\rangle_t
	\end{split}
	\end{align*}
\end{thm}
\end{minipage}
\vspace{-0.4cm}
\begin{itemize}
	\item \texttt{uf\_union} corresponds to the \textbf{Union} and \texttt{uf\_cmp} to the \textbf{Find} operation
	\item These theorems follow from abstract results about disjoint set forests
	\item The whole proof is about 5KLoc long in Isabelle/HOL
\end{itemize}
\end{frame}

\section{Conclusions}
\begin{frame}
	\frametitle{Conclusions}
	\begin{itemize}
		\item Formalization of the state-of-the-art result about Union-Find
		\item Comprehensive theory about Ackermann, including quantitative and asymptotic results
		\item The proofs of the imperative programs are still too long:
		 \begin{itemize}
			\item The automation does not deal well with arithmetic
			\item It also instantiates existentials too aggressively
			\item Repetitive proofs generated by branching $\Longrightarrow$ optimization possible
		\end{itemize}
		\item Formal verification of non-trivial results about runtime, not only correctness are possible
		\item Some overhead, but this helps to reveal hidden assumptions
	\end{itemize}
\end{frame}

%\PraesentationMasterKopfzeileDreizeiler
\begin{frame}
\frametitle{\,}
\Huge Thank You for listening! \newline
\Huge Any questions?
\end{frame}

\begin{frame}
	\frametitle{Important Definitions}
	\vspace{1cm}
	\begin{definition}{invar\_rank}
		\begin{align}
		\mathrm{invar\_rank}\, \LL \, \RR :=
		&\mathrm{ufa\_invar} \, \LL \, \land \\
		&|\LL| = |\RR| \, \land \\
		&(\forall (i,j) \in \ufabstart. \, \RR!i < \RR!j) \\
		&(\forall i < |\LL|.\, \LL!i = i \longrightarrow 2^{\RR!i} \leq |\mathrm{descendants}_\LL \, i|)
		\end{align}
	\end{definition}
	\begin{definition}{ufa\_invar}
	\begin{equation}
	\mathrm{ufa\_invar} \, \LL \, := \forall i < |\LL|.\, i \in \mathrm{Dom}\, \repof \land \LL!i < |\LL|
	\end{equation}
	\end{definition}

\begin{definition}{ufa\_$\beta$\_start}
\begin{equation}
\ufabstart := \{(x,y) \,\mid\, x<|\LL| \land y<|\LL| \land x \neq y \land \LL!x = y \}
\end{equation}
\end{definition}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document} % !!! NICHT ENTFERNEN !!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%-------------------------------------------------------------------------------
\FloatBarrier\section{Static model of educational choice}
%-------------------------------------------------------------------------------
Consider the framework of the generalized Roy model presented in class for the static analysis of educational choice.

\begin{boenumerate}

\item Write down and briefly describe the key equations of the model.

\item Formally define the conventional average treatment effects and describe their limited policy relevance. What is potentially lost by focusing on average effects instead of looking at the whole distribution of individual benefits?

\item Define and describe the concept of essential heterogeneity. How does its presence and absence affect the relationship between the conventional average treatment effect parameters. Please integrate the conventional average treatment effects in the absence of essential heterogeneity into Figure \ref{Distribution of effects} which already shows a hypothetical distribution of individual-specific benefits.

\begin{figure}[htp]\centering
\caption{Distribution of effects}\label{Distribution of effects}\scalebox{0.35}
{\includegraphics{fig-static-model-distribution-canvas}}
\end{figure}

\item Define and describe the marginal benefit of treatment. What exactly is the conditioning set? Complete the empty canvas below by sketching the marginal benefit of treatment in the presence and absence of essential heterogeneity. Ensure that both axes are properly labeled.

\begin{figure}[htp]\centering
\caption{Marginal benefit of treatment}\scalebox{0.35}
{\includegraphics{fig-static-model-marginal-benefit-canvas}}
\end{figure}

\item What are the main findings in \cite{Carneiro.2011} on the marginal benefit of a college education?

\item Briefly outline the shortcomings of a static model of educational choice compared to a dynamic model.
\end{boenumerate}

Consider the following parameterization of the generalized Roy model presented in class for the static analysis of educational choice.

\begin{align*}
Y_1 & = 0.25     & D = \mathbbm{1}[\,0.50 > U\,] \\
Y_0 & = U &
\end{align*}

Assume that $U$ is unobservable and follows a uniform distribution between zero and one. Please be careful about correctly labeling all graphs that you decide to include in your answers.

\begin{boenumerate}

\item Define the individual effect of treatment. What are the sources of heterogeneity in the model. What fraction of individuals have a positive benefit of treatment?  How many do select into treatment?

\item Formally define the conventional average treatment effects and describe their limited policy relevance. How does the distribution of benefits for the model above look like? What is its exact range? Please mark the part of the distribution conditional on treatment status. Calculate the conventional effects of treatment.

\item Define and describe the concept of essential heterogeneity. Does the parameterized model exhibit essential heterogeneity? Please explain your answer.

\item Define and describe the marginal benefit of treatment $B^{MTE}$. How exactly does the $B^{MTE}$ for the parameterized model above look like?

\end{boenumerate}


\documentclass[inequalities.tex]{subfile}

\begin{document}
	\section{Smoothing and Isolated Fudging}\label{sec:smoothfudging}
	In many inequalities of the form
		\begin{align*}
			f(x_{1},\ldots,x_{n})
				& \geq a
		\end{align*}
	for some real number $a$, we may be able to establish the property that $f$ assumes smaller values when the difference between two variables $x_{i}$ and $x_{j}$ decreases. In such cases, we can use the fact that $f$ assumes the smallest value when $x_{1}=\ldots=x_{n}$. This is known as the \textit{smoothing principle}. Recall that we used a similar argument for arithmetic-geometric mean inequality when we replaced the product $a_{1}a_{2}$ by $\bar{a}(\bar{a}+k-h)$ where $a_{1}=\bar{a}-h$ and $a_{2}=\bar{a}+k$.
		\begin{problem}[USA $1996$]\label{prob:usa1996-3}
			Let $a_{0},\ldots,a_{n}$ be real numbers in the interval $\left(0,\frac{\pi}{2}\right)$. If
				\begin{align*}
					\tan\left(a_{0}-\dfrac{\pi}{4}\right)+\ldots+\tan\left(a_{n}-\dfrac{\pi}{4}\right)
						& \geq n-1
				\end{align*}
			prove that
				\begin{align*}
					\tan{a_{0}}\cdots\tan{a_{n}}
						& \geq n^{n+1}
				\end{align*}

				\begin{solution}
					If $x_{i}=\tan\left(a_{i}-\frac{\pi}{4}\right)$ so $-1<x_{i}<1$ and
						\begin{align*}
							x_{i}
								& = \dfrac{\tan{a_{i}}-1}{1+\tan{a_{i}}}\\
								& = \dfrac{y_{i}-1}{1+y_{i}}
						\end{align*}
					where $y_{i}=\tan{a_{i}}$. Then
						\begin{align*}
							y_{i}
								& = \dfrac{1+x_{i}}{1-x_{i}}
						\end{align*}
				\end{solution}
		\end{problem}
\end{document}


\documentclass[english]{../thermomemo/thermomemo}
\pdfminorversion=4
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
%\input{mathdef}
\usepackage[per-mode=symbol]{siunitx}
\usepackage[numbers]{natbib}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{array}% improves tabular environment.
\usepackage{dcolumn}% also improves tabular environment, with decimal centring.

\usepackage{booktabs}
\usepackage{a4wide}
\usepackage{xspace}
\usepackage{todonotes}
\presetkeys{todonotes}{inline}{}
\usepackage{subcaption,caption}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{snakes}
\usepackage{verbatim}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  linkcolor=blue,
  urlcolor=blue,
  citecolor=blue
}
%
% Egendefinerte
%
% Kolonnetyper for array.sty:
\newcolumntype{C}{>{$}c<{$}}% for å slippe å taste inn disse $
\newcolumntype{L}{>{$}l<{$}}% for å slippe å taste inn disse $
%
\newcommand*{\unit}[1]{\ensuremath{\,\mathrm{#1}}}
\newcommand*{\uunit}[1]{\ensuremath{\mathrm{#1}}}
%\newcommand*{\od}[3][]{\frac{\mathrm{d}^{#1}#2}{\mathrm{d}{#3}^{#1}}}% ordinary derivative
\newcommand*{\od}[3][]{\frac{\dif^{#1}#2}{\dif{#3}^{#1}}}% ordinary derivative
\newcommand*{\pd}[3][]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative
\newcommand*{\pdc}[3]{\frac{\partial^{2}#1}{\partial{#2}\partial{#3}}}% partial derivative
\newcommand*{\pdt}[3][]{{\partial^{#1}#2}/{\partial{#3}^{#1}}}% partial
                                % derivative for inline use.
\newcommand{\pone}[3]{\frac{\partial #1}{\partial #2}_{#3}}% partial
                                % derivative with information of
                                % constant variables
\newcommand{\ponel}[3]{\frac{\partial #1}{\partial #2}\bigg|_{#3}} % partial derivative with informatio of constant variable. A line is added.
\newcommand{\ptwo}[3]{\frac{\partial^{2} #1}{\partial #2 \partial
    #3}} % partial differential in two different variables
\newcommand{\pdn}[3]{\frac{\partial^{#1}#2}{\partial{#3}^{#1}}}% partial derivative

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

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

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

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

\title{Stability and critical points of mixtures}
\author{Morten Hammer}

\graphicspath{{gfx/}}

\begin{document}
\frontmatter
\tableofcontents
\section{Introduction}
This memo is to present methods for calculating stability and critical points of
mixture. Background information on stability of mixtures and the calculation of
critical points is found in \citet{Reid1977}, \citet{Heidemann1980},
\citet{Michelsen1984} and \citet[Chap. 9]{Michelsen2007}.
\section{Limit of stability formulated in temperature and pressure}
\label{sec:tp_fomulation}
The stability criterion for a mixture with composition $z$, formulated
in temperature and pressure, is given by
\begin{equation}
  \label{eq:TPDorg}
  \TPD(\vektor{w}) = \underset{i}{\sum}w_i\left(\mu_i(\vektor{w})-\mu_i(\vektor{z})\right),
\end{equation}
where \TPD is the tangent plane distance function, and $\mu$ is the
chemical potential. If looking at global stability, $\vektor{w}$ is any
composition. In the case of local stability, $\vektor{w}$ is any perturbation
of $\vektor{z}$. If \TPD is non-negative the mixture $\vektor{z}$ is stable.

A modified stability condition for a reduced tangent plane
distance \tpd, is given in Equation \ref{eq:tpd}. \tpd is evaluated
using composition variables treated as mole numbers, $\vektor{Y}$, instead of
composition, and have better properties and are easier to solve
numerically that the $TPD$ formulation.
\begin{equation}
  \tpd(\vektor{Y}) = 1.0 + \underset{i}{\sum}Y_i\left(\ln Y_i + \ln \varphi_i(\vektor{Y})-\ln z_i - \ln \varphi_i(\vektor{z})\right).
  \label{eq:tpd}
\end{equation}
Here $\varphi$ is the fugacity.

Differentiating Equation \ref{eq:tpd}, we get
\begin{align}
  \vektor{g} &= \pd{\tpd}{\vektor{Y}} = \ln Y_i + \ln \varphi_i(\vektor{Y})-\ln z_i - \ln \varphi_i(\vektor{z}),\\
  H_{ij} &= \frac{\partial^2\tpd}{\partial Y_i \partial Y_j} = \frac{\delta_{ij}}{Y_i} + \frac{\partial \ln \varphi_i(\vektor{Y})}{Y_j}.
  \label{eq:dtpd}
\end{align}

Looking at local stability of a mixture phase with composition $\vektor{z}$, a
perturbation of $\vektor{z}$, $\vektor{Y} = \vektor{z} + \vektor{e}$, for small $\vektor{e}$ must give a positive
\tpd. A Taylor series expansion from $\vektor{Y}=\vektor{z}$ yields,
\begin{align}
  \tpd(\vektor{Y}) =& \tpd(\vektor{z}) + \left(\pd{\tpd}{\vektor{z}}\right)^\intercal\vektor{e} + \frac{1}{2}\vektor{e}^\intercal\left(\pd[2]{\tpd}{\vektor{z}}\right)\vektor{e} + \dots \\
  =& \tpd + \vektor{g}^\intercal \vektor{e} + \frac{1}{2}\vektor{e}^\intercal\vektor{H}\vektor{e} + \dots.
  \label{eq:tpdTaylor}
\end{align}
Since $\tpd(\vektor{z}) = 0$ and $\vektor{g}(\vektor{z}) = 0$, the local stability limit therefore become,
\begin{equation}
  \text{det}\left(H\right) = 0,
  \label{eq:detM}
\end{equation}
or equivalent $\lambda_{\text{min}} = 0$, where $\lambda_{\text{min}}$
is the minimum eigenvalue of $\vektor{H}$.

To improve the scaling and the numerical properties of the problem,
\citet{Michelsen1984} introduces a scaling of the composition
variables,
\begin{equation}
  \label{eq:X}
  X_i = \frac{Y_i-z_i}{\sqrt{z_i}}.
\end{equation}
Further $X_i = s u_i$, where $\vektor{u}$ is a normalized vector,
$\vektor{u}^\intercal\vektor{u} = 1$. The perturbed composition then becomes,
\begin{equation}
  \label{eq:Y}
  Y_i = z_i + s u_i\sqrt{z_i}.
\end{equation}
Differentiating Equation \ref{eq:tpd} \wrpt $X_i$ we get,
\begin{align}
  \pd{\tpd}{X_i} = \sqrt{z_i}g_i,\\
  \frac{\partial^2\tpd}{\partial X_i \partial X_j} = B_{ij} &=  \sqrt{z_i}\sqrt{z_j}H_{ij}.
  \label{eq:dtpdX}
\end{align}

Further, if we let $\vektor{u}$ be the eigenvector corresponding to the smallest
eigenvalue, $\lambda_{\text{min}}$, of $\vektor{B}$, we have
\begin{equation}
  \label{eq:b}
  b = \frac{1}{2}\vektor{u}^{\top}\vektor{B}\vektor{u} = \frac{1}{2} \lambda_{\text{min}}.
\end{equation}

\subsection{Numerical solver}
Solving for $b = 0$ in Equation \ref{eq:b}, for a specified pressure
or temperature is complicated. The solution value ($T$ ot $p$) will
lie near the value ($T$ ot $p$) where the underlying equation of state
loses a density root (phase). A discontinuity will therefore be
located close to the solution, excluding the possibility of using a
bracketing solver, unless the position where the phase root disappears
is know. If we are dealing with a cubic equation of state, the
position where the model looses a density root can easily be
determine, otherwise not. See Figure \ref{fig:meta_init}.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{meta_init}
  \caption{Minimum eigenvalue plotted as a function of $T$ for given
    pressure, $P$ and for given specific volume, $V$. The
    meta-stable limit is located at $T=\SI{181.65}{\kelvin}$. The $TP$
    line have a discontinuity (phase disappear) located close to the
    meta-stable limit, while the $TV$ line is monotonous. The plot is
    generated for a mixture of \SI{90}{\percent} methane and
    \SI{10}{\percent} ethane.}
  \label{fig:meta_init}
\end{figure}

Specifying a pressure and solving for a temperature at the meta-stable
limit, has been found possible for low pressures. The saturation
temperature, when treating the fluid as a pure fluid, at low
pressures, can be used as initial value of the search for a
meta-stable limit of a gas/liquid. We then know that the solution will
lie below (gas) and above (liquid) the initial guess. Using a
Newton-Raphson (NR) solver we will take steps in a direction towards
the solution, but with the possibility of overshooting the
solution. Since the eigenvalue of the stability matrix, $\vektor{B}$,
changes abruptly when a phase disappears, and another phase is selected
by the EOS density solver, a line search is used to back track if the
search step becomes to large.

The NR solver must use numerical differentials since analytical
differentials are difficult to derive in the general case.

For larger pressures, where the metastable limits of the gas and the
liquid come close together, it has proven difficult to guaranty
solution to the correct meta-stable limit. To overcome this problem,
an alternative formulation is needed.

\section{Alternative formulation}
Michelsen and Heidemann \cite{Heidemann1980} suggest that a
formulation in the variables $T$ and $v$ is more robust than the $P$
and $T$ formulation. One reason for this is that following the meta
stable limit, the volume will be monotonous.

The Heidemann tangent plane distance for $T$ and $v$ are given as,
\begin{equation}
  \label{eq:tpd_TV}
  \TPD\left(T,V,\mathbf{Y}\right) =
 A\left(T,V,\mathbf{Y}\right) - A\left(T,V_0,\mathbf{z}\right) +
 P\left(T,V_0,\mathbf{z}\right)\left(V - V_0\right) -
 \underset{j}{\sum}\mu\left(T,V_0,\mathbf{z}\right)\left(Y_j - z_j\right)
 .
\end{equation}

The Michelsen \citet[Chap. 9]{Michelsen2007} reduced tangent plane distance for $T$ and $v$ are given as,
\begin{equation}
  \label{eq:tpd_TV_M}
  \tpd(T,V,\mathbf{Y}) =
  \underset{j}{\sum}Y_j\left[\ln f_j \left(T,V,\mathbf{Y}\right) -
    \ln f_j\left(T,V_0,\mathbf{z}\right)\right] -
  \left[P\left(T,V,\mathbf{Y}\right) - P_0\right]\frac{V}{RT}
  .
\end{equation}

The Helmholtz energy can be written as follows,
\begin{equation}
  \label{eq:A}
 A\left(T,V,\mathbf{Y}\right) = - P\left(T,V,\mathbf{Y}\right)V +
 \underset{j}{\sum}Y_j\mu\left(T,V,\mathbf{Y}\right)
 .
\end{equation}

The fugacity coefficient definition
\begin{equation}
  \label{eq:fugacity}
  R T \ln f_j\left(T,V,\mathbf{Y}\right) =
  \pd{A\left(T,V,\mathbf{Y}\right)}{Y_j} = \mu_j \left(T,V,\mathbf{Y}\right).
\end{equation}
give the following relation:
\begin{equation}
  \label{eq:A2}
 A\left(T,V,\mathbf{Y}\right) = - P\left(T,V,\mathbf{Y}\right)V +
 \underset{j}{\sum}Y_j\ln f_j\left(T,V,\mathbf{Y}\right).
\end{equation}

Note that we are not working with the reduced Helmholtz energy, but
the real Helmholtz energy.

Using Equations \ref{eq:A}, \ref{eq:fugacity} and \ref{eq:A2}, and
$V=V_0$ it is seen that Equation \ref{eq:tpd_TV} and Equation
\ref{eq:tpd_TV_M} represent the same stability criteria:
\begin{align}
  \label{eq:tpd_TV_M2}
  tpd(T,V,\mathbf{Y}) =&
  \underset{j}{\sum}Y_j\left[\ln f_j \left(T,V,\mathbf{Y}\right) -
    \ln f_j\left(T,V_0,\mathbf{z}\right)\right] -
  \left[P\left(T,V,\mathbf{Y}\right) - P_0\right]\frac{V}{RT},\\
  =& \frac{1}{RT}\left(-P\left(T,V,\mathbf{Y}\right) V +
     \underset{j}{\sum}Y_j\mu_j \left(T,V,\mathbf{Y}\right) \right)\\
  & - \frac{1}{RT}\left( - P_0V_0 + P_0\left(V_0-V\right)
    -\underset{j}{\sum}\left(Y_j - z_j + z_j\right)\mu_j\left(T,V_0,\mathbf{z}\right)
  \right)
  ,\\
  =& \frac{1}{RT}\left( A\left(T,V,\mathbf{Y}\right) -
     A\left(T,V_0,\mathbf{z}\right) + P_0\left(V_0-V\right) - \underset{j}{\sum}\left(Y_j - z_j\right)\mu_j\left(T,V_0,\mathbf{z}\right)\right),
\end{align}

Differentiating Equation \ref{eq:tpd_TV_M} w.r.t. $n_i$, and using $V=V_0$ we
get
\begin{equation}
  \label{eq:tpd_TV_n}
  g_i =
 \ln f_i\left(T,V_0,\mathbf{Y}\right) +
 \ln f_i\left(T,V_0,\mathbf{z}\right),
\end{equation}
and
\begin{equation}
  \label{eq:tpd_TV_h}
  h_{ij} =
 \ln f_{ij}\left(T,V,\mathbf{Y}\right) .
\end{equation}

The same variable scaling is used for this formulation, as in the
previous section (Equation \ref{eq:X}). Using the temperature-pressure
formulation to calculate an initial point, Section
\ref{sec:tp_fomulation}, for a low pressure, the entire meta-stable
limit can be mapped using steps in the volume variable. Stepping with
fixed increments in $\ln v$ was found optimal considering resolution of
the line.

An example, plotting the meta-stable limit together with the phase
envelope for a mixture of \SI{90}{\percent} methane and
\SI{10}{\percent} ethane, is plotted in Figure \ref{fig:meta}.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{meta}
  \caption{Phase envelope and meta-stable limit plotted for a mixture
    of \SI{90}{\percent} methane and \SI{10}{\percent} ethane. The
    critical point is plotted using a black dot.}
  \label{fig:meta}
\end{figure}

\subsection{Single component}
The pure fluid stability limit is the well-known,
\begin{equation}
  \label{eq:dpdv_t}
  \left(\pd{p}{v}\right)_T = 0.
\end{equation}

The mapping of the meta-stable limit for pure fluids currently use a
solver for Equation \ref{eq:dpdv_t}. For robust and fast mapping,
extrapolation of volume is performed between each step in temperature.

%\todo{Investigate if multi-component approach can be used for single components as well.}

An example, plotting the meta-stable limit together with the
saturation line for pure \coto, is plotted in Figure
\ref{fig:singleMeta}.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.6\textwidth]{singleMeta}
  \caption{Saturation line and meta-stable limit plotted for pure
    \coto. Peng-Robinson equation of state with van der Waals mixing
    rules are applied.}
  \label{fig:singleMeta}
\end{figure}

\section{Critical point}
``A critical point is a stable point which lies on the stability
limit.'' - \citet{Heidemann1980}. The implications from this is that,
if \tpd is Taylor expanded, the cubic form must vanish. See
\cite{Heidemann1980} for more details.

% The critical will lie on the meta-stable line and also satisfying,
% \begin{equation}
%   q_{ijk} = \left(\frac{\partial^3\tpd}{\partial z_i \partial z_j \partial z_k}\right)_{T,V\text{ or }P} % \Delta z_i \Delta z_j \Delta z_k.
%   \label{eq:q}
% \end{equation}

\subsection{Critical point solver}
Since the third order differentials of \tpd typically is difficult and
time consuming to construct analytically, \citet{Michelsen1984}
developed an approach to solve for a critical point numerically
without calculating these differentials. The main components of this
algorithm is given below for the formulation in temperature an volume.
Using $F = \tpd$, and Taylor expanding in $s$, we get
\begin{align}
F(\mathbf{X} = s\mathbf{u}) = F_1(s) = \overset{\infty}{\underset{m=0}{\sum}}\left(\frac{s^m}{m!}\right)\left(\frac{\partial^m F_1}{\partial s^m}\right)_{s=0} = as + bs^2 + cs^3 + ds^4 + \mathcal{O}(s^5),
\label{eq:sTaylor}
\end{align}
since $F_1(s=0) = 0$. Further,
$a = \left(\partial F_1/\partial s\right)_{s=0} =
\underset{i}{\sum}\sqrt{z_i}u_ig_i(s=0)=0$. For $b$ we have,
\begin{equation}
b = \frac{1}{2}{\underset{i,j}{\sum}}B_{ij}u_iu_j = \frac{1}{2}\mathbf{u}^\intercal \mathbf{B} \mathbf{u},
\label{eq:bdef}
\end{equation}
identical to the definition in Equation \ref{eq:b}. The smallest value
of $b$ is found by choosing $\mathbf{u}$ to be the eigenvector
corresponding to the minimum eigenvalue of $\mathbf{B}$. We then get,
\begin{equation}
\mathbf{B} \mathbf{u} = \lambda_{\text{min}} \mathbf{u}, \mathbf{u}^\intercal\mathbf{u} = 1
\label{eq:brelation}
\end{equation}

For $c$ we have,
\begin{equation}
  c = \frac{1}{6}{\underset{i,j,k}{\sum}}q_{ijk}u_iu_ju_k.
\label{eq:cdef}
\end{equation}

Differentiating Equation \ref{eq:sTaylor} \wrpt $s$, we get,
\begin{align}
\pd{F_1}{s} =  2bs + 3cs^2 + 4ds^3 + \mathcal{O}(s^4),
\label{eq:sTaylords}
\end{align}
and this equals
\begin{align}
\pd{F_1}{s} = \underset{i}{\sum}\pd{F}{Y_i}\pd{Y_i}{s} = \underset{i}{\sum}\sqrt{z_i}u_ig_i.
\label{eq:dfds}
\end{align}
Evaluating at $s=\epsilon$ and $s=-\epsilon$, we get
\begin{align}
\left(\pd{F_1}{s}\right)_{\epsilon} &= 2b\epsilon + 3c\epsilon^2 + 4d\epsilon^3 + \mathcal{O}(\epsilon^4),\\
\left(\pd{F_1}{s}\right)_{-\epsilon} &= -2b\epsilon + 3c\epsilon^2 - 4d\epsilon^3 + \mathcal{O}(\epsilon^4).\\
\label{eq:dfdseps}
\end{align}
Adding the Equations above we get a Equation for $c$,
\begin{align}
c = \frac{1}{6\epsilon^2}\left(\left(\pd{F_1}{s}\right)_{\epsilon} + \left(\pd{F_1}{s}\right)_{-\epsilon}\right) + \mathcal{O}(\epsilon^2).
\label{eq:c}
\end{align}
\subsubsection{Critical point solver Jacobean}
In order to construct a Newton-Raphson solver differentials for the Jaocobian is
required.  Differentiating Equation \ref{eq:brelation} \wrpt temperature, we get
the relation,
\begin{equation}
\mathbf{B}_T \mathbf{u} + \mathbf{B} \mathbf{u}_T = \lambda_{T} \mathbf{u} + \lambda_{\text{min}} \mathbf{u}_T, \mathbf{u}^\intercal\mathbf{u}_T = 0
\label{eq:breldiffT}
\end{equation}
Multiplying with $\mathbf{u}^\intercal$, and simplifying we get,
\begin{align}
\mathbf{u}^\intercal \mathbf{B}_T \mathbf{u} + \mathbf{u}^\intercal\mathbf{B} \mathbf{u}_T &= \lambda_{T} \mathbf{u}^\intercal\mathbf{u} + \lambda_{\text{min}} \mathbf{u}^\intercal\mathbf{u}_T,\\
\mathbf{u}^\intercal \mathbf{B}_T \mathbf{u}  &= \lambda_{T}.
\label{eq:breldiffTuT}
\end{align}
Rearranging Equation \ref{eq:breldiffT}, we get
\begin{equation}
\left(\mathbf{B} - \lambda_{\text{min}}I\right) \mathbf{u}_T = \lambda_{T} \mathbf{u}  - \mathbf{B}_T \mathbf{u}.
\label{eq:breldiffTR}
\end{equation}
We see that if $\mathbf{B}_T \mathbf{u}$ is known, it is possible to
find $\mathbf{u}_T$. The elements of $\mathbf{B}_T \mathbf{u}$, is
\begin{align}
\label{eq:Btu}
  \left(\mathbf{B}_T \mathbf{u}\right)_i &= \sqrt{z_i}\underset{j}{\sum}\sqrt{z_j}u_j \pd{\ln f_{ij}}{T} = \sqrt{z_i} \pd{}{s}\left(\pd{\ln f_{i}}{T}\right)_{s=0} \\
                                         &\approx \sqrt{z_i} \left(\frac{1}{2\epsilon}\right)\left[\left(\pd{\ln f_{i}}{T}\right)_{s=\epsilon} - \left(\pd{\ln f_{i}}{T}\right)_{s=-\epsilon} \right].
\end{align}
Using $\mathbf{B}_T \mathbf{u}$, $\lambda_T$ is determined, and further $b_T = \lambda_T/2$. To determine $c_T$, Equation \ref{eq:sTaylor} is used,
\begin{align}
\pd{F_1}{T} = b_Ts^2 + c_Ts^3 + d_Ts^4 + \mathcal{O}(s^5).
\label{eq:dF1dT_Taylor}
\end{align}
Further differentiating Equation \ref{eq:tpd_TV_M}, we get
\begin{align}
\label{eq:dF1dT}
\pd{F_1}{T} &= \pd{\tpd}{T} +
  \underset{j}{\sum}\pd{\tpd}{Y_j}\pd{Y_j}{T},\\
\pd{\tpd}{T} &= \underset{j}{\sum}Y_j\left[\ln f_{jT} \left(\mathbf{Y}\right) -
     \ln f_{jT}\left(\mathbf{z}\right)\right] -
   \left[P_T\left(\mathbf{Y}\right) -
               P_T\left(\mathbf{z}\right)\right]\frac{V}{RT} \\
            &+\left[P\left(\mathbf{Y}\right) - P\left(\mathbf{z}\right)\right]\frac{V}{RT^2}\\
\pd{\tpd}{Y_j}\pd{Y_j}{T} &=g_j\sqrt{z_j}s(\mathbf{u}_T)_j
\end{align}
Evaluating equations \ref{eq:dF1dT} and \ref{eq:dF1dT_Taylor} at $\epsilon$ and $-\epsilon$ and combining, we get,
\begin{align}
c_T = \frac{1}{2\epsilon^3}\left(\left(\pd{F_1}{T}\right)_{\epsilon} - \left(\pd{F_1}{T}\right)_{-\epsilon}\right) + \mathcal{O}(\epsilon^2).
\label{eq:cT}
\end{align}
The differentials \wrpt volume can be determined in the same manner.

% \ref{eq:A}
% \begin{align}
%   \label{eq:dA}
%  \pd{A}{n_i} &= - P_iV + \mu_i
%  \underset{j}{\sum}n_j\pd{\mu}{n_i} = \mu_i \\
%  P_iV &= \underset{j}{\sum}n_j\pd{\mu}{n_i} \\
% F_1(T,V,n(T,V)) &=
%   \underset{j}{\sum}n_j\left[\ln f_j \left(T,V,\mathbf{n}\right) -
%     \ln f_j\left(T,V_0,\mathbf{z}\right)\right] -
%   \left[P\left(T,V,\mathbf{n}\right) - P_0\right]\frac{V}{RT}\\
%   \pd{F_1}{n_i} &= \ln f_i \left(T,V,\mathbf{n}\right) + \ln
%                   f_i\left(T,V_0,\mathbf{z}\right) + \underset{j}{\sum}n_j\left[\ln f_{ji} \left(T,V,\mathbf{n}\right) -
%     \ln f_{ji}\left(T,V_0,\mathbf{z}\right)\right] -
%    \left[P_i\left(T,V,\mathbf{n}\right) - P_{0,i}\right]\frac{V}{RT}\\
% \left[P\left(T,V,\mathbf{n}\right) - P_0\right]\frac{V}{RT}
% \end{align}
\clearpage
\bibliographystyle{plainnat}
\bibliography{../thermopack}

\end{document}