\documentclass[prd]{revtex4}
\usepackage{amsmath, graphicx}
\usepackage{grffile}
\usepackage{dcolumn}
\usepackage{bm}
\usepackage{epsfig}
\usepackage{mathrsfs}  %  package for the "curly" fonts
\usepackage{subfigure}
\usepackage{multirow}
\usepackage{epstopdf}
\usepackage{amsmath}
\usepackage{algorithmicx}
\usepackage{amssymb}
\usepackage{tensor}
\usepackage[math]{cellspace}
\usepackage{bookmark}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{hyperref}
\usepackage{slashed}
\usepackage{youngtab}
\usepackage{physics}
\usepackage{tensor}

\newcommand*\apost{\textsc{\char13}}

\newcommand{\TODO}[1]{{\color{red}[}{\color{red}TODO:} {\color{blue}#1}{\color{red}]}}
\newcommand{\NOTE}[1]{{\color{blue}[#1]}}

\makeatletter
\renewcommand*\env@matrix[1][\arraystretch]{%
  \edef\arraystretch{#1}%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}
\makeatother

\topmargin 0.30in
\textheight 9.00in

 \addtolength{\voffset}{-2cm}

\begin{document}

\title{Einstein Equations and General Relativistic Hydrodynamical System in Spherically Symmetric Metric with Horizon Penetrating Coordinate}

\author{Hyun Lim}

%\pacs{}
\maketitle

We present a horizon penetrating coordinate for spherically symmetric metric. We adopt $G=c=1$ unit system. Consider usual Schwarzschild line element in BL coordinate for practice
\begin{align}
\label{eqn:schw_bl}
ds^2 = - \left(1- \frac{2M}{r} \right) dt^2 + \left(1- \frac{2M}{r} \right)^{-1} dr^2 + r^2 d\Omega^2
\end{align}
where $M$ is a mass. Compare this with usual 3+1 line element form $ds^2 = - \alpha^2 dt^2 + \gamma_{ij} ( dx^2 + \beta^i dt)(dx^j + \beta^j dt)$ we can identify the lapse $\alpha^2 = \left(1- \frac{2M}{r} \right)$. As we know, the line element(Eqn.~\ref{eqn:schw_bl}) is singular at the horizon ($r=2M$) and lapse collapse to zero. This can be problematic because equations of motion for the metric can be become exponentially unstable in the presence of a coordinate singularity without some regularization technique.

One way to resolve this problem is to move to a horizon penetrating coordinate system where this singularity is not present. The Kerr-Schild coordinates are one such coordinate system.

For example, Schwarzschild solution in spherical type Kerr-Schild coordinates
\begin{align}
\alpha &= \sqrt{\frac{r}{r+2M}} \\
\beta^r &= \frac{2M}{r+2M} \\
\beta_r &=\frac{2M}{r} \\
\beta^\theta &= \beta^\varphi = 0 \\
K_{ij} &= \textrm{diag} \left[ -\frac{2M(r+M)}{\sqrt{r^5 (r+2M)}} , 2M \sqrt{\frac{r}{r+2M}}, K_{\theta \theta} \sin^2 \theta \right]
\end{align}
Schwarzschild solution in Cartesian type Kerr-Schild coordinate
\begin{align}
\alpha &= \sqrt{\frac{r}{r+2M}} \\
\beta^i &= \frac{2M}{r} \frac{x^i}{r+2M} \\
\beta_i &=\frac{2M x_i}{r^2} \\
K_{ij} &= \-\frac{2M}{r^4} \sqrt{\frac{r}{r+2M}} \left[ \left( \frac{M}{r}+2 \right) x_i x_j - r^2 \delta_{ij} \right]
\end{align}
where $x^i = (x,y,z)$ which is usual spatial Cartesian coordinate. In both cases, we can see lapse is regular at the horizon.

General spherical symmetric line element in polar-areal form
\begin{align}
\label{eqn:ss-met-pa}
ds^2 = - \alpha(r)^2 dt^2 + a(r)^2 dr^2 + r^2 d \Omega^2
\end{align}
where $\alpha$ is referred as lapse function. Compare with above Schwarzschild solution, $\alpha = 1/a$.

Now consider a transformation of the Schwarzschild time t coordinate to a new generic coordinate $\hat{t}$ according to
\begin{align}
d\hat{t} = dt + a^2 \sqrt{1-\frac{g}{a^2}} dr
\end{align}
where $g(r)$ is arbitrary function. Substitute this into $ds^2 = -\alpha^2 dt^2 + a^2 dr^2 + r^2 d\Omega^2$ gives
\begin{align}
ds^2 &= - \alpha^2 \left (d\hat{t} - a^2 \sqrt{1-\frac{g}{a^2}} dr\right)^2 + a^2 dr^2 + r^2 d\Omega^2 \nonumber \\
&= - \alpha^2 d \hat{t}^2 + 2 \sqrt{1-\frac{g}{a^2}} d\hat{t} dr + g dr^2 + r^2 d \Omega^2
\end{align}
Compare this with usual 3+1 framework
\begin{align}
ds^2 = - \alpha^2 d \hat{t}^2 + \gamma_{ij} (dx^i + \beta^i d\hat{t}\,)(dx^j + \beta^j d\hat{t}\,)
\end{align}
and so into the lapse $\alpha = 1/\sqrt{g}$, the shift $\beta_i = (\sqrt{1-g/a^2},0,0)$ or $\beta^i = \gamma^{ij} \beta_j$ and the spatial metric of the constant $\hat{t}$ hypersurface $\gamma_{ij} = diag(g,r^2,r^2\sin^2 \theta)$.

If we choose $\alpha = \sqrt{1-2M/r} = 1/a$ and $g = 1+2M/r$ like in previous (which we will use this), we get
\begin{align}
ds^2 = - \left(1 - \frac{2M}{r} \right) d \hat{t}^2 + \frac{4M}{r} d \hat{t} dr + \left(1+\frac{2M}{r} \right) dr^2 + r^2 d\Omega^2
\end{align}
which is Schwarzschild in Kerr-Schild coordinate (or Eddington-Finkelstein coordinate). And correspondingly, $\alpha = \sqrt{r/(r+2M)}$, $\beta_i = (2M/r,0,0)$, and $\gamma_{ij} = diag(1+2M/r,r^2,r^2\sin^2 \theta)$ which are same as above.

As you can see here, the KS (or EF) form of the metric represents an analytic expansion of the Schwarzschild solution from the region $2M < r< \infty$ to  $0<r<\infty$. Thus, we apply this coordinate transformation for our equations.

It is good to rewrite the metric into usual $3+1$ variable form i.e. keep it geometric variables (should be careful of confusion) with considering time dependent case (This is almost same as Marsa and Choptuik\apost s paper). Here, we use $t$ for time coordinate that we used above.
\begin{align}
\label{eqn:gen-sph-met}
ds^2 = (-\alpha^2 + a^2 \beta^2) dt^2 + 2a^2 \beta dt dr + a^2 dr^2 + r^2 b^2 d \Omega^2
\end{align}
where $\alpha$, $a$, $b$, and $\beta$ are functions of $r$ and $t$, and $d\Omega^2$ is the metric of unit sphere. From this, we can calculate non-vanishing components of connection coefficients and Ricci tensors for $i,j$, and $k$ (spatial indices)
\begin{align}
&\Gamma\indices{^r_{rr}} = \frac{\partial_r a}{a}, \,\,\,\,\,\,\,\, \Gamma\indices{^r_{\theta \theta}} = - \frac{rb \partial_r (rb)}{a^2}, \,\,\,\,\,\,\,\, \Gamma\indices{^\theta_{r \theta}} = \frac{\partial_r (rb)}{rb} \nonumber  \\
&\Gamma\indices{^r_{\varphi \varphi}} = -\sin^2 \theta \frac{rb \partial_r (rb)}{a^2}, \,\,\,\,\,\,\,\, \Gamma\indices{^\varphi_{r \varphi}} = \Gamma\indices{^\theta_{r \theta}} \nonumber \\
&\Gamma\indices{^\theta_{\varphi \varphi}} = -\sin \theta \cos \theta, \,\,\,\,\,\,\,\, \Gamma\indices{^\varphi_{\varphi \theta}} = -\cot \theta \nonumber
\end{align}
\begin{align}
R\indices{^r_r} &= -\frac{2}{arb} \partial_r \left(\frac{\partial_r (rb)}{a} \right) \\
R\indices{^\theta_\theta} &= \frac{1}{ar^2b^2} \left[a-\partial_r \left(\frac{rb \partial_r (rb)}{a} \right) \right] \\
\end{align}

%In terms of horizon penetrating coordinate, we can generalize it in 3+1 form
%\begin{align}
%\label{eqn:gen-sph-met}
%ds^2 = (-\alpha^2 + a^2 \beta^2) dt^2 + 2a^2 \beta dt dr + a^2 dr^2 + r^2 b^2 d \Omega^2
%\end{align}
%where $\alpha$, $a$, $b$, and $\beta$ are functions of $r$ and $t$, and $d\Omega^2$ is the metric of unit sphere. This is nothing but ingoing Eddington-Finkelstein coordinate system (IEF). Consider Schwarzschild again in IEF
%\begin{align}
%ds^2 = - \left( 1- \frac{2M}{r} \right) dV^2 + 2 dV dr + r^2 d \Omega^2
%\end{align}
%Define a timelike coordinate $t = V-r$ then metric becomes
%\begin{align}
%ds^2 = - \left( 1- \frac{2M}{r} \right) dt^2 + \frac{4M}{r} dt dr +  \left( 1+ \frac{2M}{r} \right) dr^2 + r^2 d \Omega^2
%\end{align}
%Compare this with Eqn.~\ref{eqn:gen-sph-met}, various following metric components can be found
%\begin{align}
%\alpha &= \sqrt{\frac{r}{r+2M}} \\
%\beta &=\frac{2M}{r+2M} \\
%a &=\sqrt{\frac{r+2M}{r}}
%\end{align}
%and so on. Note that we can also fix the spatial degree of coordinate freedom by introducing a shifting areal coordinate $R \equiv  r + f(t)$ where $f(t)$ is some undetermined function.

\section{Theoretical Model}

Here, as a beginning set up, we first use the perfect fluid approximation for the matted model. So the stress-energy tensor takes form
\begin{align}
\label{eqn:EMtPF}
T_{ab} = (\rho + P) u_a u_b +P g_{ab}
\end{align}
where $u^a(r,t)$ is the 4-velocity of a given perfect element, $P(r,t)$ is the isotropic pressure, $\rho(r,t) = \rho_0(r,t) (1+\epsilon(r,t))$ is the energy density, $\rho_0 (r,t)$ is the rest-mass energy density, and $\epsilon(r,t)$ is the specific internal energy.

The equations of motion for this case are derive from the local conservative equations for energy and baryon number such that
\begin{align}
\nabla_a T\indices{^a_b} &= 0 \\
\nabla_a (\rho_0 u^a) &= 0
\end{align}

We are using HRSC so we would like to write this in terms of flux-conservative form such that
\begin{align}
\partial_t {\bf U} + \partial_i {\bf F^i} = {\bf \Psi}
\end{align}
where ${\bf U}$,
\begin{align}
{\bf U} =
\begin{pmatrix}
\sqrt{\gamma} W \rho_0 \\
\sqrt{\gamma} \alpha T\indices{^t_j} \\
\alpha^2 \sqrt{\gamma} T^{tt} - \sqrt{\gamma} W \rho_0
\end{pmatrix}
\end{align}

and ${\bf F^i}$
\begin{align}
{\bf F^i} =
\begin{pmatrix}
\sqrt{\gamma} W \rho_0 v^i \\
\sqrt{\gamma} \alpha T\indices{^i_j} \\
\alpha^2 \sqrt{\gamma} T^{ti} - \sqrt{\gamma} W \rho_0 v^i
\end{pmatrix}
\end{align}

and ${\bf \psi}$
\begin{align}
{\bf \Psi} =
\begin{pmatrix}
0\\
\frac{1}{2}\sqrt{\gamma} \alpha T\indices{^{ab}}g_{ab,j} \\
\alpha^2 \sqrt{\gamma} (T^{at} \partial_a \alpha - \Gamma\indices{^0_{ab}} T^{ab} \alpha )
\end{pmatrix}
\end{align}
where $W$ is Lorentz factor such that $W = \alpha u^t$ and $v^i = u^i/u^t$. Under our choice of system, $u^a = (u^t, u^r, 0, 0)$ so we can reduce
\begin{align}
\partial_t ( r^2 a b W \rho_0) + \partial_r (r^2 a b W \rho_0 v^r) &= 0 \\
\partial_t (r^2 a b \alpha T\indices{^t_r}) + \partial_r ( r^2 a b \alpha T\indices{^r_r}) &= \frac{1}{2} r^2 a b T^{ab} g_{ab,r} \\
\partial_t ( \alpha^2 r^2 a b T^{tt} - r^2 a b W \rho_0 ) + \partial_r(\alpha^2 r^2 a b T^{tr} - r^2 a b W \rho_0 v^r) &= \alpha r^2 ab (T^{at} \partial_a \alpha - \Gamma\indices{^0_{ab}} T^{ab} \alpha )
\end{align}

Here I omit $\sin$ term in the metric determinant because it will be cancelled out anyway

It is useful to define variables like below
\begin{align}
D &= \rho_0 ab W  \\
E &= \rho_0 h  W^2 -P \\
S &= \rho_0 h W^2 v\\
\tau &= E-D
\end{align}
And nonzero components of $T^{ab}$ which we are using
\begin{align}
T\indices{^t_t} &= -E \\
T\indices{^t_r} &= \frac{ab}{\alpha} S \\
T\indices{^r_r} &=Sv+P\\
T\indices{^\theta_\theta}&=T\indices{^\varphi_\varphi}=P
\end{align}
where we define fluid velocity in Eulerian observer
\begin{align}
v = \frac{ab}{\alpha} v^r = \frac{ab u^r}{\alpha u^t}
\end{align}
then also $W=1 / \sqrt{1-v^2}$ and $h=1+\epsilon+P/\rho_0$ which is specific enthalpy.  Then we can reduce
\begin{align}
\partial_t ( r^2 D) + \partial_r \left(\frac{r^2\alpha}{ab}D v\right) &= 0 \\
\partial_t (r^2 S) + \partial_r \left( \frac{r^2 \alpha}{ab}( E v + P)\right) &= \frac{1}{2} r^2 a b T^{ab} g_{ab,r} \\
\partial_t ( r^2\tau) + \partial_r\left(\frac{r^2\alpha}{ab}(S - D v)\right) &= \alpha r^2 ab (T^{at} \partial_a \alpha - \Gamma\indices{^0_{ab}} T^{ab} \alpha )
\end{align}
or in the form
\begin{align}
\partial_t {\bf u} + \frac{1}{r^2} \partial_r (X r^2 {\bf f}) = \psi
\end{align}
\begin{align}
{\bf u} =
\begin{pmatrix}
D \\
S \\
\tau
\end{pmatrix}, \,\,\,\,
{\bf f} =
\begin{pmatrix}
Dv \\
Ev + P \\
S - Dv
\end{pmatrix}, \,\,\,\,
{\bf \psi} =
\begin{pmatrix}
0 \\
\frac{ab}{2} a b T^{ab} g_{ab,r} \\
\alpha ab (T^{at} \partial_a \alpha - \Gamma\indices{^0_{ab}} T^{ab} \alpha )
\end{pmatrix}
\end{align}
where $X= \alpha / (ab)$ which is purely geometric factor. Some detail evaluation of RHS source terms are in \href{https://github.com/hlim88/PBH-NS/tree/master/tools}{here}

\iffalse
First, we define variables.
%\begin{align}
%{\bf q} = \begin{bmatrix}
%D\\
%\Pi\\
%\Phi
%\end{bmatrix} , \,\,\,\,\,\,\,
%{\bf f} = \begin{bmatrix}
%Dv\\
%v(\Pi+P) + P\\
%v(\Phi + P) + P
%\end{bmatrix} , \,\,\,\,\,\,\,
%{\bf \psi} = \begin{bmatrix}
%0\\
%\Sigma\\
%-\Sigma
%\end{bmatrix}
%\end{align}
%where $v$ is Eulerian velocity of fluid such that $ v = a u^r / (\alpha u^t)$, $X=\alpha/a$ is a purely geometric quantity and
%\begin{align}
%D &= a \rho_0 W\\
%\Pi &= E-D+S\\
%\Phi &=E-D-S\\
%S &= \rho_0 h W^2 v \\
%E &=\rho_0 h W^2 - P
%\end{align}
where $W$ is Lorentz factor such that $W = \alpha u^t=1/\sqrt{1-v^2}$ with fluid velocity $v =(a u^r) / (\alpha u^t)$ and $h=1+\epsilon+P/\rho_0$ which is specific enthalpy. In our case, $u^a = (u^t, u^r, 0, 0)$

And nonzero components of $T^{ab}$ which we are using
\begin{align}
T\indices{^t_t} &= -E \\
T\indices{^t_r} &= \frac{a}{\alpha} S \\
T\indices{^r_r} &=Sv+P\\
T\indices{^\theta_\theta}&=T\indices{^\varphi_\varphi}=P
\end{align}

Using these variables under the metric which we consider, $\nabla_a (\rho_0 u^a)=0$ (continuity equation) gives
\begin{align}
\partial_t (\rho_0 u^t) + \partial_r (\rho_0 u^r) + \Gamma\indices{^t_{tt}}(\rho_0 u^t) + \Gamma\indices{^t_{tr}}(\rho_0 u^r) = 0
\end{align}
In terms of our variables, $\rho_0 u^t = D/\alpha$, $\rho_0 u^r = D v /a$ so
\begin{align}
\partial_t (D/\alpha) + \partial_r (D/\alpha) + \Gamma\indices{^t_{tt}}(D/\alpha) + \Gamma\indices{^t_{tr}}(D v / a) = 0
\end{align}

$\nabla_a T\indices{^a_b}=0$ gives
\begin{align}
\partial_a T\indices{^a_b} + \Gamma\indices{^a_{ac}}T\indices{^c_b} - \Gamma\indices{^c_{ab}}T\indices{^a_c} = 0
\end{align}
The covariant $t$-component of above equation gives energy equation $T\indices{^t_t} = - E$
\begin{align}
&\partial_t T\indices{^t_t} + \Gamma\indices{^t_{tc}}T\indices{^c_t} - \Gamma\indices{^c_{tt}}T\indices{^t_c} = 0 \nonumber \\
&\rightarrow \partial_t T\indices{^t_t} + \Gamma\indices{^t_{tt}}T\indices{^t_t} +\Gamma\indices{^t_{tr}}T\indices{^r_t}- \Gamma\indices{^t_{tt}}T\indices{^t_t} - \Gamma\indices{^r_{tt}}T\indices{^t_r} = 0 \nonumber \\
&\rightarrow \partial_t E + \Gamma\indices{^t_{tr}} \frac{a^3}{\alpha^3} S + \Gamma\indices{^r_{tt}} \frac{a}{\alpha} S = 0
\end{align}
Next, consider the covariant $r$-component
\begin{align}
&\partial_t T\indices{^t_r} + \Gamma\indices{^t_{tc}}T\indices{^c_r} - \Gamma\indices{^c_{tr}}T\indices{^t_c} = 0 \nonumber \\
&\rightarrow \partial_t T\indices{^t_r} + \Gamma\indices{^t_{tt}}T\indices{^t_r} +\Gamma\indices{^t_{tr}}T\indices{^r_r}- \Gamma\indices{^t_{tr}}T\indices{^t_t} - \Gamma\indices{^r_{tr}}T\indices{^t_r} = 0 \nonumber \\
&\rightarrow \partial_t \left(\frac{a}{\alpha} S\right) + \Gamma\indices{^t_{tt}}(\frac{a}{\alpha} S)  +\Gamma\indices{^t_{tr}}(Sv+P) + \Gamma\indices{^t_{tr}}E - \Gamma\indices{^r_{tr}}\frac{a}{\alpha} S= 0 \nonumber
\end{align}
Non-vanishing connection coefficients are evaluated via Mathematica. You can find it \href{https://github.com/hlim88/PBH-NS/tree/master/tools}{here}. After all simplifications, we have
\fi

%Here, we use the fact that $\sqrt{-g} = \alpha \sqrt{\gamma}$ where $\gamma = det(\gamma_{ij})$ which came from above metric.

Now consider the Einstein\apost s equations. Define below quantities that are appearing in the 3+1 equations
\begin{align}
\rho_{hydro} &= n_a n_b T^{ab} = \rho_0 h W^2 - P \\
S_i^{hydro} &= - \gamma_{ia} n_b T^{ab}  = \rho_0 h W u_i \\
S_{ij}^{hydro} &= \gamma_{ia} \gamma_{ib} T^{ab} = P \gamma_{ij} + \rho_0 h u_i u_j \\
S_{hydro} &= \gamma^{ij} S_{ij} = 3 P + \rho_0 h (W^2 -1)
\end{align}

The Einstein\apost s equations in the ADM form are
\begin{align}
\label{eqn:adm:gam}
\partial_t \gamma_{ij} &= - 2 \alpha K_{ij} + D_i \beta_j + D_j \beta_i \\
\label{eqn:adm:K}
\partial_t K\indices{^i_j} &= \alpha (R\indices{^i_j} + K K\indices{^i_j}) - D^i D_j \alpha - 8 \pi \alpha \left(S\indices{^i_j} - \frac{1}{2} \delta\indices{^i_j} (S-\rho)\right) \nonumber \\
& + \beta^k \partial_k K\indices{^i_j} + K\indices{^i_k} \partial_j \beta^k - K\indices{^k_j} \partial_k \beta^i
\end{align}
where $D_i$ is covariant derivative on spatial hypersurface. Momentum and Hamiltonian constraints are
\begin{align}
R+K^2 - K_{ij} K^{ij} = 16 \pi \rho \\
D_i K\indices{^i_j} - D_j K = 8 \pi S_j
\end{align}

Substitute hydro source terms ($\rho$, $S$ etc) from above then we have
\begin{align}
\partial_t K\indices{^i_j} &= \alpha (R\indices{^i_j} + K K\indices{^i_j}) - \gamma^{ik} (\partial_i \partial_k \alpha - \Gamma\indices{^l_{ik}} \partial_l \alpha) - 8 \pi \alpha \left(\frac{1}{2} \delta\indices{^i_j} (\rho_0 h - 2 P) + \rho_0 h u^i u_j \right) \nonumber \\
& + \beta^k \partial_k K\indices{^i_j} + K\indices{^i_k} \partial_j \beta^k - K\indices{^k_j} \partial_k \beta^i \\
\partial_t \gamma_{ij} &= - 2 \alpha K_{ij} + D_i \beta_j + D_j \beta_i \\
&R+K^2 - K_{ij} K^{ij} = 16 \pi (\rho_0 h W^2 -P)\\
&D_i K\indices{^i_j} - D_j K = 8 \pi \rho_0 h W u_j
\end{align}

From our choice of metric/coordinate system, we calculated non-trivial connection coefficients and Ricci tensors. Also, metric form suggests that $\beta^i = (\beta^r,0,0)$, $K\indices{^i_j} = diag(K\indices{^r_r}, K\indices{^\theta_\theta},K\indices{^\theta_\theta})$. Using these facts, the evolution equations for geometric quantities are
\begin{align}
\partial_t a &= - \alpha a K\indices{^r_r} + \partial_r (a \beta^r) \\
\partial_t b &= - \alpha b K\indices{^\theta_\theta} + \frac{\beta^r}{r} \partial_r (r \beta^r) \\
\partial_t K\indices{^r_r} &= \beta^r \partial_r K\indices{^r_r} + \alpha K\indices{^r_r} K - \frac{1}{a} \partial_r \left( \frac{\partial_r \alpha}{a} \right) - \frac{2 \alpha}{arb} \partial_r \left( \frac{\partial_r (rb)}{a} \right) \nonumber \\
& - 4 \pi \alpha \left[ (1+2 u^r u_r) \rho_0 h - 2P \right] \\
\partial_t K\indices{^\theta_\theta} &= \beta^r \partial_r K\indices{^\theta_\theta} + \alpha K\indices{^\theta_\theta} K - \frac{\alpha}{r^2 b^2} - \frac{1}{ar^2b^2} \partial_r \left( \frac{\alpha r b \partial_r (rb)}{a} \right) \nonumber \\
& - 4 \pi \alpha (\rho_0 h -2 P)
\end{align}
From constraints
\begin{align}
 \frac{1}{ar^2b^2} \left[a-\partial_r \left(\frac{rb \partial_r (rb)}{a} \right) \right] -\frac{2}{arb} \partial_r \left(\frac{\partial_r (rb)}{a} \right) + 2K\indices{^\theta_\theta}(K\indices{^\theta_\theta}+2 K\indices{^r_r}) &= 16 \pi (\rho_0 h W^2 -P) \nonumber \\
 \frac{\partial_t (r b)}{rb} (K\indices{^\theta_\theta} - K\indices{^r_r}) - \partial_r K\indices{^\theta_\theta} &= 4 \pi \rho_0 h W u_r
\end{align}

We can apply different choice of slicing (i.e. gauge choice) to reduce/determine above system. Possible (or simple) choices would be maximal or polar slicing.

\subsection{Choice of Gauge}

\subsubsection{Maximal Slicing}
First, we consider maximal slicing i.e. $K=\partial_t K = 0$ then

\begin{align}
\partial_t a &= - \alpha a K\indices{^r_r} + \partial_r (a \beta^r) \\
\partial_t b &=  \frac{\alpha b}{2} K\indices{^r_r} + \frac{\beta^r}{r} \partial_r (r \beta^r) \\
\partial_t K\indices{^r_r} &= \beta^r \partial_r K\indices{^r_r}  - \frac{1}{a} \partial_r \left( \frac{\partial_r \alpha}{a} \right) - \frac{2 \alpha}{arb} \partial_r \left( \frac{\partial_r (rb)}{a} \right)
-  \frac{2 \alpha}{r^2 b^2} - \frac{2}{ar^2b^2} \partial_r \left( \frac{\alpha r b \partial_r (rb)}{a} \right)  \nonumber \\
& - 8 \pi \alpha \left[ (2+2 u^r u_r) \rho_0 h - 4P \right]
\end{align}
From constraints
\begin{align}
 \frac{1}{ar^2b^2} \left[a-\partial_r \left(\frac{rb \partial_r (rb)}{a} \right) \right] -\frac{2}{arb} \partial_r \left(\frac{\partial_r (rb)}{a} \right) - \frac{3}{2}(K\indices{^r_r})^2 &= 16 \pi (\rho_0 h W^2 -P) \nonumber \\
  \partial_r K\indices{^r_r} -\frac{3 \partial_t (r b)}{rb}  K\indices{^r_r} &= 8 \pi \rho_0 h W u_r
\end{align}
Fluid EOM parts are same as previous

For lapse, we use
\begin{align}
\partial_t K = - D^2 \alpha + \alpha (K^{ij}K_{ij} + 4 \pi (\rho + S))+ \beta^i D_i K
\end{align}
In our choice of gauge, this can be reduced
\begin{align}
D^2 \alpha &= \alpha ( K^{ij} K_{ij} + 4 \pi (\rho + S)) = \alpha \left( K^{ij} K_{ij} + 8 \pi \left[P + \rho_0 h \left(W^2 - \frac{1}{2}  \right)\right]\right) \nonumber \\
&= \alpha \left( 2(K\indices{^r_r})^2 + 8 \pi \left[P + \rho_0 h \left(W^2 - \frac{1}{2}  \right)\right]\right)
\end{align}
Also, for shift, we use
\begin{align}
\partial_t \ln \sqrt{\gamma} = - \alpha K + D_i \beta^i
\end{align}
In maximal slicing, this reduces
\begin{align}
D_i \beta^i = - \partial_t \ln \sqrt{\gamma}
\end{align}
or we can write
\begin{align}
\partial_i \beta^i = - \partial_t \sqrt{\gamma}
\end{align}
This shows that the proper volume element $\sqrt{\gamma}$ satisfies a continuity equation in maximal slicing.

In terms of our metric choice and variable
\begin{align}
\partial_r \beta^r = \frac{b}{2b+\beta^r} \left[\frac{\alpha K\indices{^r_r}}{2} - \frac{(\beta^r)^2 b}{r}- \frac{\partial_r a}{a} \beta^r \right]
\end{align}

\subsection{Spherical-G-BSSN with 1+log and $\Gamma$ driver}
Another possible way to describe this system is writing the equations in terms of BSSN form. This makes every equations
in hyperbolic form which do not require to solve elliptic equations.

We follow the usual treatment in BSSN variable. Also, we still keep HPC-SS metric which is described previous section

Let \apost s consider usual BSSN form (make conformal transformation $\hat{\gamma} \rightarrow e^{4\phi} \gamma$ i.e.
\begin{align}
\phi = \frac{1}{12} \ln(\gamma / \hat{\gamma})
\end{align}
It is common to define a new variable $\chi = e^{-2 \phi}$ instead of using the conformal factor $\phi$. (Perhaps, we get similar result..) Evolution equation for $\chi$ is
\begin{align}
\partial_t \chi = \beta^r \partial_r \chi - \frac{1}{3} \chi \left[ \alpha K - \partial_r \beta^r - \beta^r \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right) \right]
\end{align}
We also use traceless part of the extrinsic curvature $\hat{A}_{ij}$. In SS, we have $\hat{A}^r_r$ and $\hat{A}^\theta_\theta$. Note that as $\hat{A}_{ij}$ is traceless $\hat{A}^r_r+2\hat{A}^\theta_\theta=0$. Evolution equations for spatial metric $\gamma$ give
\begin{align}
\partial_t a &= \beta^r \partial_r a + 2 a \partial_r \beta^r - \frac{2a}{3}\left[ \partial_r \beta^r +\beta^r \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right)\right] -2 \alpha a \hat{A}^r_r \\
\partial_t b &= \beta^r \partial_r b + 2 b \frac{\beta^r}{r} - \frac{2b}{3} \left[\partial_r \beta^r + \beta^r \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right)\right] + \alpha b \hat{A}^r_r
\end{align}
Evolution equation for trace of extrinsic curvature $K$ is
\begin{align}
\partial_t K &= \beta^r \partial_r K - \frac{\chi^2}{\alpha} \left[\partial_r^2 \alpha - \partial_r \left(\frac{\partial_r a}{2a} - \frac{\partial_r b}{b} +\frac{\partial_r \chi}{\chi} - \frac{2}{r} \right) \right] \nonumber \\
&+ \alpha \left(\frac{3}{2} (\hat{A}^r_r)^2 + \frac{1}{3} K^2 \right) + 4 \pi \alpha (\rho + S^r_r + S^\theta_\theta)
\end{align}
We have only one independent component of traceless part of the extrinsic curvature. Evolution equations for that is
\begin{align}
\partial_t \hat{A}^r_r &= \beta^r \partial_r \hat{A}^r_r - \frac{\chi^2}{\alpha}\left( \left[\partial_r^2 \alpha - \partial_r \left(\frac{\partial_r a}{2a} - \frac{\partial_r \chi}{\chi} \right) \right] -\frac{1}{3} \left[\partial_r^2 \alpha - \partial_r \left(\frac{\partial_r a}{2a} - \frac{\partial_r b}{b} +\frac{\partial_r \chi}{\chi} - \frac{2}{r} \right) \right] \right)\nonumber \\
&+\frac{\alpha}{3}(2 R\indices{^r_r} - R\indices{^\theta_\theta}) + \alpha K \hat{A}^r_r - 16\pi \alpha ( S^r_r - S^\theta_\theta)
\end{align}
Finally, we obtain the evolution equation for $\hat{\Delta}^r$ which is the radial component of conformal connection function which is defined by $\hat{\Delta}^i = \hat{\gamma}^{mn}( \hat{\Gamma}\indices{^i_{mn}} - \mathring{\Gamma}\indices{^i_{mn}})$ where $\mathring{\Gamma}\indices{^i_{mn}}$ is defined by $\mathring{\gamma}\indices{_{ij}}$ which is flat metric in spherical coordinate i.e. $\mathring{\gamma}=r^2 \sin^2 \theta$
\begin{align}
\partial_t \hat{\Delta}^r &= \beta^r \partial_r \hat{\Delta}^r - \hat{\Delta}^r \partial_r \beta^r + \frac{1}{a} \partial^2 \beta^r + \frac{2}{b} \partial_r \left(\frac{\beta^r}{r} \right) \nonumber \\
&+\frac{1}{3}\left[\frac{1}{a} \left\{\partial_r^2 \beta^r + \partial_r \beta^r  \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right) + \beta^r \partial_r \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right)\right\} + 2 \hat{\Delta}^r \left\{\partial_r \beta^r +\beta^r \left(\frac{\partial_r (a b^2)}{2 a b^2} + \frac{2}{r}\right)\right\}\right]  \nonumber \\
&-\frac{2}{a} (\hat{A}^r_r \partial_r \alpha + \alpha \partial_r \hat{A}^r_r) + 2 \alpha \left(\hat{A}^r_r \hat{\Delta}^r + \frac{3}{rb} \hat{A}^r_r \right) \nonumber \\
&+ \frac{2\alpha}{a} \left[ \partial_r \hat{A}^r_r - \frac{2}{3} \partial_r K - \frac{3\hat{A}^r_r}{\chi} \partial_r \chi + \frac{3}{2} \hat{A}^r_r \left(\frac{2}{r} + \frac{\partial_r b}{b} \right) - 8 \pi S_r \right]
\end{align}
We also evaluate Hamiltonian and momentum constraints to monitor our numerical evolutions
\begin{align}
\mathcal{H} &= -\frac{2}{arb} \partial_r \left(\frac{\partial_r (rb)}{a} \right) + \frac{1}{ar^2b^2} \left[a-\partial_r \left(\frac{rb \partial_r (rb)}{a} \right) \right] - \frac{3}{2} (\hat{A}^r_r)^2 + \frac{2}{3} K^2 - 16 \pi \rho = 0 \\
\mathcal{M} &= \partial_r \hat{A}^r_r - \frac{2}{3} \partial_r K - \frac{3 \hat{A}^r_r}{2\chi}\partial_r \chi + \frac{2}{3} \hat{A}^r_r \left(\frac{2}{r} + \frac{\partial_r b}{b} \right) - 8 \pi S_r = 0
\end{align}
\subsubsection{Gauge Choice}
In addition to SS-GBSSN variables, we still have two more variables that need to be chosen. Here, we use 1+log condition for lapse and Gamma driver condition for shift.
The form of this slicing condition is
\begin{align}
\partial_t \alpha = -2\alpha K
\end{align}
We have only radial component for shift vector so Gamma driver condition is
\begin{align}
\partial_t B^r &= \frac{3}{4} \partial_t \hat{\Delta}^r \\
\partial_t \beta^r &= B^r
\end{align}
We introduce an auxiliary variable $B^r$ to reduce derivative order of time from second to first

%Another possible choice to set the lapse is demanding that the ingoing combination of tangent vectors $\vec{\partial}_t - \vec{\partial}_r$ be null. This gives a condition on the metric : $g_{tt} - 2 g_{tr} + g_{rr}=0$. This gives $\alpha = a(1-\beta)$

%Further, a sufficient set of Einstein\apost s equations for geometric variable $\alpha$ and $a$ are given by the nontrivial component of momentum constraint
%\begin{align}
%\partial_t a = - 4 \pi r \alpha a^2 S
%\end{align}
%and by the polar slicing condition which follows from the demand that metric have the spherically symmetric form for all time
%\begin{align}
%\partial_r (\ln \alpha) = a^2 \left[4 \pi r (Sv+P) + \frac{m}{r^2} \right]
%\end{align}
%and from Hamiltonian constraint
%\begin{align}
%\partial_r a = a^3 \left(4 \pi r E - \frac{m}{r^2} \right)
%\end{align}

%To solve these sets of equations on Schwarzschild background in KS, we consider following coordinate transformation in time
%\begin{align}
%\hat{t} = t + 2 M \ln | \frac{r}{2M} - 1 | + k
%\end{align}
%where $k$ is arbitrary constant. So for arbitrary function $F$
%\begin{align}
%\frac{\partial F}{\partial t} = \frac{\partial \hat{t}}{\partial t} \frac{\partial F}{\partial \hat{t}} = \left(1+\frac{2MXv}{r-2M} \right) \frac{\partial F}{\partial \hat{t}} = \left(1+\frac{2Mv}{r} \right) \frac{\partial F}{\partial \hat{t}}
%\end{align}
%We apply this rule to above equations for HPC

%From the metric,
%\begin{align}
%ds^2 = - \alpha^2 dt^2 + a^2 dr^2 + r^2 d \Omega^2
%\end{align}
%Identify $a = \sqrt{r/(r-2M)}$ which is usual Schwarzschild in Schwarzschild coordinate then Einstein\apost s equations and energy conservation imply the TOV system of ODEs
%\begin{align}
%\frac{d m}{dr} &= 4 \pi r^2 \rho \\
%\frac{d P}{dr} &= - (\rho + P) \frac{M + 4 \pi r^3 P}{r(r-2M)} \\
%\frac{d (\ln \alpha)}{d r} &= \frac{M+4\pi r^3 P}{r(r-2M)}
%\end{align}
%Again $\rho = \rho_0 ( 1+ \epsilon)$. Now consider this system in Kerr-Schild coordinate which provides horizon penetrating. Metric becomes from previous section
%\begin{align}
%ds^2 = (- \alpha^2 + a^2 \beta^2) dt^2 + 2 a^2 \beta dt dr + a^2 dr^2 + r^2 d \Omega^2
%\end{align}
%In this case, $\alpha$, $a$, and $\beta$ are function of $r$. Also we define

%obtaining TOV equation. Here, we still have spherical symmetry and staticity so
%\begin{align}
%\frac{d}{dr} ( \sqrt{-g} \rho_0 u^1) &= 0 \nonumber \\
%\frac{d}{dr} ( \sqrt{-g} T^1_0 ) &= 0 \nonumber
%\end{align}
%where $T_{ab}=(\rho + P) u_a u_b + Pg_{ab}$ or $T_{ab}=\rho_0 h u_a u_b + Pg_{ab}$ where $h = 1+\epsilon+p/\rho_0$ which is specific entalpy

\subsection{Initial Data}
Our initial NS model is approximated by solution of TOV. After the initial data calculation, an in-going velocity profile is added to drive the star to collapse. We follow the way is described in (https://arxiv.org/pdf/gr-qc/0107045.pdf). First, specifying the coordinate velocity
\begin{align}
U \equiv \frac{dr}{dt} = \frac{u^r}{u^r}
\end{align}
of the star. In general, the profile take the algebraic form $U_g(x) = A_0 (x^3 - B_0 x)$ where $x \equiv  r/R_{star}$ and $R_{star}$ is the radius of the TOV solution.

In this work, we set two profiles
\begin{align}
U(x) =
\begin{cases}
U_1(x) = U_{crit} ( x^3 - 3x) & x < x_{tlv} \\
\\
U_2(x) = 0 & \textrm{otherwise} \\
\end{cases}
\end{align}
$U_{crit}$ is the amplitude that occurs critical collapse, and $x_{tlv}$ the region that forms black hole.

Our interest is interaction between BH inside of NS we set $x_{tlv}$ is small value such as 1\% of size of star i.e. $x_{tlv} = 0.01$ since $x$ is normalized radius by star radius ($x_{tlv}$ must be smaller than $1$).

%\subsection{Analytic Case}

%For code test and validation, we use well-known Michel problem

\end{document}


\documentclass{article}

\usepackage{a4wide}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{fixltx2e}
\usepackage{color}
\usepackage{hyperref}

\title{Making a Cellular Potts Model of Collective Migration}
\author{Inge Wortel \and Johannes Textor}
\date{May 1st, 2020}

\begin{document}

\maketitle

In this exercise, you are going to use the Cellular Potts modeling framework to create cells \emph{in silico}. You will first learn how to use the model's many parameters to create cells of realistic shapes and motility patterns. Then you will go a step further and model several migrating cells that interact with each other.

\section*{Learning Goals}
\begin{itemize}
\item Understand how the different parameters of a Cellular Potts Model (CPM) interact with each other, and how this influences cell behavior.
\item Realize that it can be difficult to tune the parameters of a CPM.
\item Apply this knowledge to create different modes of cell migration in the CPM, and explain how this helps understand those migration modes.
\item Apply the CPM to investigate the dynamics of collective cell migration \emph{in silico}.
\end{itemize}

Please go to {\color{blue}\href{http://computational-immunology.org/cpm/collective.html}{computational-immunology.org/cpm/collective.html}}. This is an implementation of a special version of the Cellular Potts Model in which cells can migrate \cite{niculescu_crawling_2015}.
Note that \emph{this web page does not work properly in Internet Explorer}. It does work in either Firefox, Chrome, or Safari -- so we recommend using any of those browsers for this exercise.

\section*{Notes on the CPM}

%At {\color{blue}\href{http://computational-immunology.org/talks/2018-10-02-nijmegen/}{computational-immunology.org/talks/2018-10-02-nijmegen/}}, you can check back the lecture slides on the CPM.

Remember that in a CPM, copy attempts have a higher chance of succeeding if they lower the total energy $H$ of the system. In other words, $\Delta H = H_\text{after copy} - H_\text{before copy}$ should be negative to guarantee that the copy attempt will work. Copy attempts with positive $\Delta H$ can still succeed, but do so with a lower probability that depends on the temperature of the system. In general, the formula for $\Delta H$ of a CPM looks something like this:

\begin{equation}
	\Delta H = \Delta H_\text{adhesion} + \Delta H_\text{Volume} + \Delta H_\text{...}  + ...
\end{equation}

Thus, we build up $\Delta H$ from all the different energetic factors we want the cell to consider -- and we can always add stuff to make the model more complex. But what parameters do we need to tune to get realistic cells? In the following exercises, you will get a feel for how you can model cell behavior with a CPM by tuning the parameters that control the energy $\Delta H$.

\section*{Exercises}

\subsection*{A very basic CPM}

In this exercise, we will first examine a very basic CPM in which cells do not (yet) migrate.
Make sure that the field is empty (hit refresh or "remove all cells"), and that the parameters
have the following values:

\begin{center}
\setlength{\tabcolsep}{3pt}
\small
	\begin{tabular}{l l l l l l l l l l }
		Adhesion\textsubscript{cell-matrix} & Adhesion\textsubscript{cell-cell} &
			Volume & $\lambda$\textsubscript{Volume} &
			Perimeter & $\lambda$\textsubscript{P} &
			Max\textsubscript{Act} & $\lambda$\textsubscript{Act} &
			T & Framerate \\ \hline
		20 & 0 & 500 & 50 & 340 & 0 & 0 & 0 & 20 & 1 \\
	\end{tabular}

\end{center}

We will now investigate how the basic CPM parameters -- controlling adhesion, cell volume, and cell perimeter (circumference) -- influence behavior (this means you can ignore the $\lambda$\textsubscript{Act} and Max\textsubscript{Act} parameters for now). This exercise is meant mostly to give you an idea of what the CPM parameters do, and the questions are to guide your thinking -- so you don't have to write everything down. Try to spend no longer than 30-40 minutes on this exercise before continuing to the next.

\begin{enumerate}
	\item Make sure all the parameters are set as in the table above, click "seed cell" and then "start". What do you see? What kind of motion does this cell have?
	\item Now set the Adhesion\textsubscript{cell-matrix} to 0. What happens to the cell? Why do you think that happens? (Hint: look back to the description of the CPM and adhesion energy in the lecture...) Also try a negative value for Adhesion\textsubscript{cell-matrix}. What is the meaning of positive or negative adhesion values here?
	\item \textit{(Optional)} Instead of setting the Adhesion\textsubscript{cell-matrix} back to 20, try setting the Adhesion\textsubscript{cell-cell} to -20 while having the Adhesion \textsubscript{cell-matrix} still at 0. Does that have the effect that you expected? Why/why not do you think that is? Hint: try drawing a grid like you saw in the lecture for a copy attempt you are interested in. Do the two adhesion energies change in the same way for that copy attempt?
	\item Return to the parameters in the table above. With these parameters, the cell is given an ideal volume (500 pixels), and a "level of importance" of this volume for the energy ($\lambda$\textsubscript{Volume}). Try making the cell bigger or smaller (what parameter should you change?). How can you make the volume unimportant for the energy -- and what happens then? What happens when you make $\lambda$\textsubscript{Volume} really large (say, 1000)?
	\item So far, we have considered $\Delta H$ with only terms for adhesion energy and cell volume. We will now investigate the effect of the cell perimeter (circumference). The cell already has a target perimeter (340), but this is currently not taken into account in the calculation of $\Delta H$. For that, we need to make $\lambda$\textsubscript{P} non-zero. Try setting it to 2. What happens to the cell? Try making the cell "membrane" more or less ruffled. How would you do that?
\item Set the perimeter to 340 and $\lambda$\textsubscript{P} to 2. Now change the Adhesion\textsubscript{cell-matrix} to 0 again. Does this have the same effect as it did in question 2? Why do you think that is?
\item \textit{(Optional)} Play around with the volume and perimeter parameters for a while (using the adhesion parameters from the table, or try your own). How can you change the cell? Can you change the parameters independently of each other? And what happens if you change the temperature?
\end{enumerate}

\subsection*{Cell migration: the Act model}

We will now investigate the Act model, an extension of the CPM that allows the cells to migrate \cite{niculescu_crawling_2015}. This model adds an extra term to the system energy, so that:

\begin{equation}
	\Delta H = \Delta H_\text{adhesion} + \Delta H_\text{Volume} + \Delta H_\text{Perimeter} + \Delta H_\text{Act}
\end{equation}

In this model, pixels that were recently added to the cell remember their recent "protrusive activity". This makes them more likely to protrude again. This positive feedback is controlled by the energy term $\Delta H_\text{Act}$, which is negative (favourable!) when a recently active pixel tries to copy itself into a less active pixel. The Act model has two extra parameters: $\lambda$\textsubscript{Act}, which controls how important the positive feedback is relative to the other $\Delta H$ energies, and Max\textsubscript{Act}, which determines how long pixels "remember" that they were active. In this exercise, we will see what happens when we vary those two parameters of the model. In particular, we will see that we can reproduce two very different "modes" of migration: amoeboid and keratocyte-like (see lecture).

Before starting this exercise, please refresh the page and ensure that the parameters of the CPM are set as follows:

\begin{center}
\setlength{\tabcolsep}{3pt}
\small
	\begin{tabular}{l l l l l l l l l l }
		Adhesion\textsubscript{cell-matrix} & Adhesion\textsubscript{cell-cell} &
			Volume & $\lambda$\textsubscript{Volume} &
			Perimeter & $\lambda$\textsubscript{P} &
			Max\textsubscript{Act} & $\lambda$\textsubscript{Act} &
			T & Framerate \\ \hline
		20 & 0 & 500 & 50 & 340 & 2 & 20 & 0 & 20 & 1 \\
	\end{tabular}

\end{center}

\begin{enumerate}
	\item Seed a cell and click "start". You should now see colored pixels at the border of the cell, which indicate the "activity" that pixels remember (because we have set Max\textsubscript{Act} to 20). Other than the color of the pixels, does the cell behave in a different way than with Max\textsubscript{Act} = 0? Why/why not?
	\item Set $\lambda$\textsubscript{Act} to 100. Would you describe this movement as random or persistent?
	\item \textit{(Optional)} What happens when you set $\lambda$\textsubscript{P} to 0 now? Why do you think that is? (Reset it to 2 before going to the next question)
	\item What happens when you increase $\lambda$\textsubscript{Act} further? (Try steps of 100).
	\item \textit{(Optional)} If you increase $\lambda$\textsubscript{Act} to very high values (eg 1000), the cell is prone to breaking in pieces. Can you fix that by altering some other CPM parameter again? (Note: you may have to increase $\lambda$\textsubscript{Act} further when you have done this... Does that make sense to you?)
	\item Reset $\lambda$\textsubscript{Act} to 0, change Max\textsubscript{Act} to 80, and repeat questions 1,2, and 4 above. What do you see?
	\item \textit{(Optional)} If you have time, play around with different combinations of $\lambda$\textsubscript{Act} and Max\textsubscript{Act}. Can you get a clue of what they are doing -- beyond the mathematical description given above?
	\item \textit{(Optional)} Try halving or doubling the cell's target volume. That won't work. What do you need to change to get the same behavior as before? What does that mean for your model (in other words: to what extent are your choices of parameters important for the behaviour you see? How worried should you be about getting parameters "wrong" and drawing the wrong conclusions?)?
	\item The nice thing about computer models is that you know exactly which "rules" you put in, and therefore that those are the only rules that the cells will follow. However, the behavior that follows from those "rules" is not always obvious (that's why we run the simulation in the first place!). In fact, the most interesting computer models can sometimes produce behavior that you did not explicitly put in there. We call this "emergent" behavior. Can you think of an example of emergent behavior in the Act model? What does that tell you about migration in a living cell?
\end{enumerate}

\subsection*{Migration in a multicellular system}

In this last exercise, we will investigate what happens when there are many cells. Before starting, please refresh the page to clear the grid, and then ensure that the parameters have the following values:

\begin{center}
\setlength{\tabcolsep}{3pt}
\small
	\begin{tabular}{l l l l l l l l l l }
		Adhesion\textsubscript{cell-matrix} & Adhesion\textsubscript{cell-cell} &
			Volume & $\lambda$\textsubscript{Volume} &
			Perimeter & $\lambda$\textsubscript{P} &
			Max\textsubscript{Act} & $\lambda$\textsubscript{Act} &
			T & Framerate \\ \hline
		20 & 0 & 200 & 50 & 180 & 2 & 20 & 200 & 20 & 5 \\
	\end{tabular}

\end{center}

Note that the framerate is not actually a parameter of the model, but it specifies how often the program draws the updated grid (eg framerate of 5 means visualize only 1 in every 5 "frames" of the movie). Setting it to 5 will speed up the animation.

\begin{enumerate}
	\item To seed many cells at once, click "+100 cells" (this will take a while...). What do you see?
	\item Now, refresh the page, reset the parameters, increase Max\textsubscript{Act} to 80, and wait for a while to let the cells adjust. What happens? How is this different from what you saw with Max\textsubscript{Act} = 20?
	\item Try other values of Max\textsubscript{Act} in between 20 and 80. What happens as you gradually increase Max\textsubscript{Act}?
	\item \textit{(Optional)} What happens when you set Adhesion\textsubscript{cell-cell} to a negative value?
	\item To what extent are your conclusions dependent on the density of cells on your grid? How could you test that?
\end{enumerate}

\bibliographystyle{abbrv}
\bibliography{ref.bib}

\end{document}


\section{Derivation of equations}
\subsection{Impurity centers in photorefractive crystals}
Impurity centers have a big effect on the dynamic gratings formation in
photorefractive crystals. We will consider the following model of
photorefractive effect. Two coherent light beams are superimposed in a
photorefractive crystal and as result we have an interference pattern,
i.e. spatial space distribution with are of high and low intensity of
the light inside photorefractive crystal. Photoelectrons are excited
from  impurity centers $N_D$
in the areas of hight intensity. The photoelectrons migrate through
crystal by means of diffusion and trapped at donator cites $N_D^{+}$ in the
crystal. The process produces an inhomogeneous distribution of electric
charges and as result the electric space change field. The
electro-optic effect invokes the space distribution of refractive
index.

There are several parameters that are used for impurity centers
description. The most important are the photoionization
cross-section $s$ and the recombination constant $\gamma_R$. The
photoionization
cross-section $s$ is a characteristic of photoelectrons exciting
process. The recombination constant $\gamma_R$ is a characteristic of
recombination process.

The additional parameters that are used for impurity centers
description are concentration of impurity centers $N_D$ and acceptors
$N_A$. The necessity of $N_A$ introducing is the following. There are
ionized impurity centers at the dark conditions thus
\(\left.N_D^{+}\right|_{I=0} \ne
0\). But the crystal is neutral itself, thus there have to be
additional impurity centers $N_A$ that compensate $N_D^{+}$ at the
dark conditions \(N_A = \left.N_D^{+}\right|_{I=0}\).

Fig.~\ref{fig:td_phr} shows two types
of crystal that we consider. The first one is a photorefractive
crystal with one impurity center (left figure) and the second one with
2 impurity center (right figure).

\input ./fig_td_phr.tex

The electric space change field $E_{sc}$ can be described by the following
equation in the case of one impirity center:
\begin{eqnarray}
\label{eqEnd_1_phr}
\tau_{eff} \frac {\partial E_{sc}} {\partial t} =
- i E_D \left(m + m_0\right)
- \nonumber \\
- E_{sc}
\left( 1 - i \frac{E_D}{E_q}\right),
\end{eqnarray}
where $E_D$ is the diffusion field, $E_q$ is the maximum
saturation field.  The $m_0$ describes is initial refractive index grating that
always presents in a photorefractive crystal.

The refractive index can be gotten from space charge field $E_{sc}$
with Pockels equation
\begin{equation}
\delta \varepsilon = \alpha E_{sc},
\label{eqElectroOpt_phr}
\end{equation}
where $\alpha$ is the electro-optical coefficient.

There are several typical times that describe the system. The speed of
photoelectrons excitation is described by $tau_I$:
\[
\tau_I = \frac{1}{s I_0 + \gamma_R n_0},
\]
where $I_0$ the light intensity, $n_0$ is the photoelectrons
density at the conduction band:
\[
n_0 = \frac{s I (N_D - N_A)}{\gamma_R N_A}.
\]
The photoelectrons recombination process is described by $tau_R$:
\[
\tau_R = \frac{1}{\gamma_R N_A}.
\]
In addition to $tau_I$ and $tau_R$ we introduce the Maxwell relaxation
time $\tau_m$:
\[
\tau_m = \frac{\varepsilon}{4 \pi \mu n_0}
\]
and a value $tau_D$ that describes the diffusion process
\[
\tau_D = \frac{1}{K^2 D},
\]
where $D$ is the coefficient of diffusion and $K$ is the propagation
vector of refractive index grating.

At (\ref{eqEnd_1_phr}) we introduced a time that describes the system
integrally:
\[
\tau_{eff} = \tau_m \frac{\tau_R}{\tau},
\]
where for $\tau$ we have:
\[
\frac{1}{\tau} = \frac{1}{\tau_I} + \frac{1}{\tau_R} + \frac{1}{\tau_D}.
\]

For a crystal with 2 impurity centers the space charge field $E_{sc}$
is a sum of two fields that describe influence of different impurity
centers:
\[
E_{sc} =  E_{sc}^{(1)} + E_{sc}^{(2)},
\]
For main class of impurity centers $E_{sc}^{(1)}$ we have the following
equation:
\begin{eqnarray}
\label{eqEsc_1_End_2_phr}
\tau_{eff}
\frac {\partial E_{sc}^{(1)}} {\partial t} =
- i E_D \left(m + m_0^{(1)}\right)
- \nonumber \\
- E_{sc}^{(1)}
\left( 1 - i \frac{E_D}{E_q^{(1)}} \right)
 -  E_{sc}^{(2)}
\left( 1+ i \frac{E_D}{E_m^{(1)}}
\right),
\nonumber \\
\left. {E_{sc}}^{(1)} \right|_{t=0} = 0.
\end{eqnarray}
For the second one $E_{sc}^{(2)}$ we have the following:
\begin{eqnarray}
\label{eqEsc_2_End_2_phr}
\frac {\partial E_{sc}^{(2)}} {\partial t} =
- \frac{i \left( m+ m_0^{(2)} \right) E_m^{(2)}}{\tau_m^{(2)}}
- \frac{E_{sc}^{(2)}}{\tau_I^{(2)}},
\nonumber \\
\left. {E_{sc}}^{(2)} \right|_{t=0} = 0.
\end{eqnarray}

We introduced a new parameter $E_m^{(1,2)}$ at (\ref{eqEsc_1_End_2_phr}) and
(\ref{eqEsc_2_End_2_phr}) that is defined by
\[
E_m^{(1,2)} = \frac{\gamma_R^{(1,2)} N_{A_0}^{(1,2)}}{K \mu}.
\]

\subsection{Problem statement}

\input ./fig_td1_dpcm.tex

We will consider a double phase conjugate mirror model. At the model
two incoherent light beams drop on a photorefractive
crystal from different sides as it is shown at
Fig.~\ref{fig:td1_dpcm}. As well as the light beams are incoherent
they can be considered independently.

\input ./fig_td2_dpcm.tex

The light field in photorefractive crystal is represented in the
following form (see Fig.~\ref{fig:td2_dpcm}):
\[
\begin{array}{l}
\vec{E}_1=\vec{E}_{1s}+\vec{E}_{1c}+\vec{E}_{1f},\\
\vec{E}_2=\vec{E}_{2s}+\vec{E}_{2c}+\vec{E}_{2f},
\end{array}
\]
where $\vec{E}_{1s}$ is the signal wave that has a structure of the
light beam drops on the crystal from the left side; $\vec{E}_{1c}$ is the
conjugated wave that has a structure of the
light beam drops on the crystal from the right side; $\vec{E}_{1f}$ is
the rest that can be considered as a wave is scattered on the
crystal's inhomogeneities. The components of
$\vec{E}_2$ that propagates from right side of the crystal have the
same meaning.

Assume
\[
\vec{E}_{1s}=A_{1s}(z,t)\vec{e}_1(\vec{r})e^{-i(\vec{k}_1\vec{r})},
\]
\[
\vec{E}_{1c}=A_{1c}(z,t)\vec{e}_2^\ast(\vec{r})e^{i(\vec{k}_2\vec{r})},
\]
\[
\vec{E}_{1f}=A_{1f}(z,t)\vec{e}_{1f}(\vec{r},t)e^{i(\vec{k}_2\vec{r})},
\]
where $\vec{e}_1(\vec{r})$ is the distribution for a wave drops on the
crystal from the left side; $\vec{e}_2(\vec{r})$ is the distribution for a
wave drops on the crystal from the right side; $\vec{e}_{1f}(\vec{r})$
is the distribution for stochastically scattered light.

It should be mentioned that gain for plane waves are different in
different directions. Thus the distribution function
$\vec{e}_{1f}(\vec{r})$ has a maximum in maximum gain direction.
We assume that the initial light beams directions are chosen as the
conjugated beams propagate in maximum gain direction. Therefore we
choose one propagation vector $\vec{k}_2$ for both $\vec{E}_{1c}$ and
$\vec{E}_{1f}$.

Equations for $\vec{E}_2$ are same:
\[
\vec{E}_{2s}=A_{2s}(z,t)\vec{e}_2(\vec{r})e^{-i(\vec{k}_2\vec{r})},
\]
\[
\vec{E}_{2c}=A_{2c}(z,t)\vec{e}_1^\ast(\vec{r})e^{i(\vec{k}_1\vec{r})},
\]
\[
\vec{E}_{2f}=A_{2f}(z,t)\vec{e}_{2f}(\vec{r},t)e^{i(\vec{k}_1\vec{r})},
\]

The distribution functions $\vec{e}_1(\vec{r})$, $\vec{e}_2(\vec{r})$ and
$\vec{e}_{1f,2f}(\vec{r})$ should be normalized:
\begin{eqnarray}
\int \limits_{S} \left( \vec{e}_1 \vec{e}_1^\ast \right) ds=1,
\int \limits_{S} \left( \vec{e}_2 \vec{e}_2^\ast \right) ds=1,
\nonumber \\
\left\langle \int \limits_{S} \left(\vec{e}_{1f}
\vec{e}_{1f}^{\ast} \right) ds \right\rangle = 1,
\left\langle \int \limits_{S} \left(\vec{e}_{2f}
\vec{e}_{2f}^{\ast} \right) ds \right\rangle = 1,
\label{eqNorm_dpcm}
\end{eqnarray}
where $\left\langle\right\rangle$ means ensemble averaging. In addition
\begin{eqnarray}
\left\langle e_{1f} \right\rangle = 0,
\left\langle e_{2f} \right\rangle = 0.
\label{eqZeroF_dpcm}
\end{eqnarray}
The light beam from the left side of the crystal has the following form
\begin{equation}
\vec{E}_1=A_{1s}\vec{e}_1 e^{-i(\vec{k}_1 \vec{r})} +
A_{1c}\vec{e}_2^\ast e^{i(\vec{k}_2 \vec{r})} +
A_{1f}\vec{e}_{1f} e^{i(\vec{k}_2 \vec{r})}
\label{eqFieldLeft}
\end{equation}
and for the beam from the right side
\begin{equation}
\vec{E}_2=A_{2s}\vec{e}_2 e^{-i(\vec{k}_2 \vec{r})} +
A_{2c}\vec{e}_1^\ast e^{i(\vec{k}_1 \vec{r})} +
A_{2f}\vec{e}_{2f} e^{i(\vec{k}_1 \vec{r})}.
\label{eqFieldRight}
\end{equation}

\subsection{Equations derivation}

The equations for refractive index gratings can be devided into to sub
classes. The first one is for a crystal with one impurity center:
\begin{eqnarray}
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M}
=
i B \Gamma \frac{(A_{1s}^\ast A_{1c}+A_{2s}^\ast A_{2c})}{I_0}
+ \frac{M_0}{\tau_{eff}}
, \nonumber \\
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M_{1f}} =
i C_1 \Gamma \frac{ A_{1s}^{\ast} A_{1f}}{I_0}
+ \frac{M_{1f_0}}{\tau_{eff}}
, \nonumber \\
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M_{2f}} =
i C_2 \Gamma  \frac{A_{2s}^{\ast} A_{2f}}{I_0}
+ \frac{M_{2f_0}}{\tau_{eff}}
,
\label{eqGrating_1_dpcm}
\end{eqnarray}
where
\[
B = \left.const\right|_t,
C_{1,2} = \left.const\right|_t,
M_0 = \left.const\right|_t,
M_{1f_0} = \left.const\right|_t,
M_{2f_0} = \left.const\right|_t.
\]

For the second case (crystal with 2 impurity centers) we will write
equations that are conjugated to
(\ref{eqM_2_dpcm}, \ref{eqM1f_2_dpcm},
\ref{eqM2f_2_dpcm}):
\begin{eqnarray}
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M^{(1)}}
=
i B \Gamma \frac{(A_{1s}^{\ast} A_{1c}+A_{2s}^{\ast} A_{2c})}{I_0}
-
\nonumber \\
-\frac{1}{\tau_{eff}}
\left(
1 - i \frac{E_D}{E_m^{(1)}}
\right) M^{(2)}
+ \frac{M_0^{(1)}}{\tau_{eff}},
\nonumber \\
\left(
\frac{\partial}{\partial{t}} + \frac{1}{\tau_I^{(2)}}
\right)
{M^{(2)}}
= i \frac{\tau_{eff}}{\tau_m^{(2)}}
 B \Gamma^{(2)} \frac{(A_{1s}^{\ast} A_{1c}+A_{2s}^{\ast} A_{2c})}{I_0}
+ \frac{M_0^{(2)}}{\tau_{m}^{(2)}}
,
\nonumber \\
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M_{1f}^{(1)}}
=
i C_1 \Gamma \frac{(A_{1s}^{\ast} A_{1f})}{I_0}
-
\nonumber \\
- \frac{1}{\tau_{eff}}
\left(
1 - i \frac{E_D}{E_m^{(1)}}
\right) M_{1f}^{(2)}
+ \frac{M_{1f_0}^{(1)}}{\tau_{eff}},
\nonumber \\
\left(
\frac{\partial}{\partial{t}} + \frac{1}{\tau_I^{(2)}}
\right)
{M_{1f}^{(2)}}
= i \frac{\tau_{eff}}{\tau_m^{(2)}}
C_1 \Gamma^{(2)} \frac{(A_{1s}^{\ast} A_{1f})}{I_0}
+ \frac{M_{1f_0}^{(2)}}{\tau_{m}^{(2)}}
,
\nonumber \\
\left(\frac{\partial}{\partial{t}}
+\frac{1}{\tau_{eff}}
\left(
1 + i \frac{E_D}{E_q}
\right)
\right){M_{2f}^{(1)}}
=
i C_2 \Gamma \frac{(A_{2s}^{\ast} A_{2f})}{I_0}
-
\nonumber \\
- \frac{1}{\tau_{eff}}
\left(
1 - i \frac{E_D}{E_m^{(1)}}
\right) M_{2f}^{(2)}
+ \frac{M_{2f_0}^{(1)}}{\tau_{eff}},
\nonumber \\
\left(
\frac{\partial}{\partial{t}} + \frac{1}{\tau_I^{(2)}}
\right)
{M_{2f}^{(2)}}
= i \frac{\tau_{eff}}{\tau_m^{(2)}}
C_2 \Gamma^{(2)} \frac{(A_{2s}^{\ast} A_{2f})}{I_0}
+ \frac{M_{2f_0}^{(2)}}{\tau_{m}^{(2)}}
.
\label{eqGrating_2_dpcm}
\end{eqnarray}
Where
\[
B = \left.const\right|_t,
C_{1,2} = \left.const\right|_t,
\]
\[
M_0^{(1,2)} = \left.const\right|_t,
M_{1f_0}^{(1,2)} = \left.const\right|_t,
M_{2f_0}^{(1,2)} = \left.const\right|_t.
\]

The coefficients $B$ and $C_{1,2}$ at (\ref{eqGrating_1_dpcm},
\ref{eqGrating_2_dpcm}) can be understood as overlap ratios. Thus they
define the part of the crystal where interaction occurs. The the
coefficients should  satisfy the following conditions:
$B < 1$ and $C_{1,2} < 1$.

The equations for scattered fields were gotten in our previouse paper
\cite{OurDPCM}.

For the left light beam's components we got the following result
\begin{eqnarray}
\frac{\partial{A_{1s}}}{\partial{z}} =
- i D \left\{
A_{1c} M^{\ast} + A_{1f} M_{1f}^{\ast} \right\},
\nonumber \\
\frac{\partial{A_{1c}}}{\partial{z}} = -
i D A_{1s} M,
\nonumber \\
\frac{\partial{A_{1f}}}{\partial{z}} =
- i D A_{1s} M_{1f},
\label{eqAmpl_left_dpcm}
\end{eqnarray}

For the right light beam's components we got the following result
\begin{eqnarray}
\frac{\partial{A_{2s}}}{\partial{z}} =
 i D
 \left\{
A_{2c} M^{\ast} + A_{2f} M_{2f}^{\ast} \right\},
\nonumber \\
\frac{\partial{A_{2c}}}{\partial{z}} =
i D
A_{2s} M,
\nonumber \\
\frac{\partial{A_{2f}}}{\partial{z}} =
i D
A_{2s} M_{2f},
\label{eqAmpl_right_dpcm}
\end{eqnarray}

At (\ref{eqAmpl_right_dpcm}) and (\ref{eqAmpl_left_dpcm}) we assumed
that $\theta_1 =\theta_2=\theta$ thus
$D=\frac{\omega}c\frac 1 {2 \cos \theta}\sqrt{\frac\mu\epsilon}$.


%$Author: jlconlin $
%$Date: 2007-03-24 10:35:35 -0600 (Sat, 24 Mar 2007) $
%$Revision: 33 $
%$Id: ArnoldiMC.tex 33 2007-03-24 16:35:35Z jlconlin $
\documentclass[12pt]{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{setspace}
\usepackage[algo2e, ruled, linesnumbered]{algorithm2e}
\usepackage[usenames, dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}

\SetKwComment{Comment}{$\triangleright$ }{}
\dontprintsemicolon

\newtheorem{theorem}{Theorem}
\newtheorem{coro}[theorem]{Corollary}

\author{Jeremy Conlin}
\title{Arnoldi's Method in Monte Carlo}

\begin{document}
\maketitle

\begin{abstract}
This document was written to help the author understand Monte Carlo Arnoldi's method.  There are mistakes and many assumptions are not explicitly stated.  USE AT YOUR OWN RISK! No warranty expressed or implied.
\end{abstract}

\section{Iteration}
One Arnoldi Iteration begins with sampling the fission source and tracking those particles with Monte Carlo transport.  This should be the only portion of this method that uses Monte Carlo transport.  The remainder should be the same as the deterministic method.

\subsection{Monte Carlo Transport}
The Monte Carlo portion begins by sampling from a fission source.  In this formulation, the fission source is a histogram source where some bins may have negative values.  The number of neutrons sampled from this distribution is constant between Monte Carlo cycles (although it doesn't have to be).

After the neutrons are sampled, they are transported as in a traditional Monte Carlo Power Method calculation.  The neutrons create additional fission neutrons at every collision.  The number of fission neutrons added to the bank at each collision is dependent on the weight of the colliding neutron,
\begin{equation} \label{eq:num-fission}
    N = \left\lfloor\omega\left(\frac{\nu\Sigma_f}{\Sigma_t}\right) + \xi\right\rfloor\,,
\end{equation}
while the weight of the neutron is reduced as
\begin{equation}
    \omega = \omega'\left(\frac{\Sigma_s}{\Sigma_t}\right).
\end{equation}

A neutron is transported until it leaves the slab geometry or its weight becomes too small ($\omega < 0.2$) at which point, it plays Russian Roulette.  Currently, the survival probability is: $p_s = 0.8$, that is, 80\% of the time, the particle will be killed.  The weight of a surviving particle is increased as
\begin{equation}
    \omega = \omega'\frac{1}{p_s}.
\end{equation}

\subsubsection{Negative Weight Neutrons}
Arnoldi's method cannot guarantee you won't get negative sources.  More often than not, you will have negative sources.  So how can we deal with this?  What exactly is a negative fission source?  The answer isn't as difficult as it might first seem.  A particle sampled from a negative source has a negative weight.  Every time a negative weight neutron makes a score, it subtracts from the tally.

We can envision our transport as two independent Monte Carlo transport problems; one whose source is the positive portion of the PDF and the other source is the absolute value of the negative PDF.  Both sources are sampled, transported, tallied and binned.  The negative source is then subtracted from the positive source in each bin.

The total number of particles transported is
\begin{equation}
    h = N_p + N_n,
\end{equation}
where $N_p$ is the number of neutrons from the positive PDF and $N_n$ is the number of neutrons from the negative PDF.  The ratio of the number of positive weight particles to negative weight particles is ratio of the probability of picking a positive weight particle to the probability of picking a negative weight particle,
\begin{equation}\label{eq:PDFprob}
    \frac{N_p}{N_n} = \frac{\int{Q_p}}{\int{Q_n}}\,.
\end{equation}

So what do we do with negative weight particles?  Using Eq. \ref{eq:num-fission} a negative weight particle will \emph{never} add a neutron to the fission bank.  I suspect what we need to do is modify Eq. \ref{eq:num-fission} to use the absolute value of the weight;
\begin{equation}
    N = \left\lfloor|\omega|\left(\frac{\nu\Sigma_f}{\Sigma_t}\right) + \xi\right\rfloor\,
\end{equation}
I also need take into account the sign of the particle's weight (but not it's magnitude) when binning a fission bank.

Similarly when I decide whether or not I should play Russian Roulette, I need to see if the absolute value of the weight is less than some cutoff.
\normalcolor

\subsection{``Matrix-ify'' Fission Bank}
Monte Carlo transport returns a new fission bank---a collection of fission neutrons---that traditionally would have been used directly for the next cycle.  With Arnoldi's Method we need to take this fission bank and turn it into something like a vector.  This vector must be able to be multiplied by a scalar and added and subtracted to other vectors.

We have chosen to turn our fission banks into vectors by binning or making a histogram out of the fission neutrons.  The value or height in each bin represents the number of fission neutrons in each geometrical bin.  The histogram is normalized and the bin values become the elements of the vector.

\subsubsection{Fission Source Normalization}
I don't understand why, but this is how you normalize the fission source, immediately after it has been binned, before you apply Arnoldi to it,
\begin{equation}
    q_{k+1} = q_{k+1}\frac{1}{N}\int{\left|q_k\right|}.
\end{equation}
By normalizing in this way, the bins are the probabilities of a fission occurring in that bin \emph{per source particle}.

\begin{figure}[h]
    \includegraphics[width=6in, keepaspectratio]{Whiteboard.jpg}
\end{figure}

\subsection{Orthogonalize} \label{sec:Orthogonalize}
In the $k^{th}$ iteration, the operation
\begin{equation}
    q_{k+1} = Aq_k,
\end{equation}
is performed where $A$ is what I will call the Monte Carlo operator, and the $q$'s are fission sources.  We already have an orthonormal set of fission sources $\{q_1, q_2, \dots, q_k\}$ which span the Krylov subspace.  For each of these previously generated sources, we perform two calculations,
\begin{subequations} \label{eq:Orthogonalize}
\begin{gather}
    h_{j,k} = \left<q_j, Aq_k\right> = \left<q_j,q_{k+1}\right> \\
    q_{k+1} = q_{k+1} - h_{j,k}*q_j,
\end{gather}
\end{subequations}
to orthogonalize $q_{k+1}$ and also to create an upper hessenberg matrix, $h$, which has the same eigenvalues as the linear operator $A$ we are investigating.

Applying Eqs. \ref{eq:Orthogonalize} for every previously generated source ($q_j$) on the current source, $q_{k+1}$ will make $q_{k+1}$ orthogonal to all $q_j$'s.  Now all that remains is to normalize $q_{k+1}$ and add one more entry to our upper hessenberg matrix;
\begin{subequations}\begin{gather}
    h_{k+1,k} = \left<q_{k+1},Aq_{k}\right> = \left<q_{k+1},q_{k+1}\right> = \left\|q_{k+1}\right\|_2 \\
    q_{k+1} = q_{k+1}/h_{k+1,k}.
\end{gather}\end{subequations}

\subsection{Eigenvalues}
The Arnoldi formulation proves that our upper hessenberg matrix, $h$, and our matrix (or linear operator) of interest, $A$, are similar and therefore have the same eigenvalues.  Finding the eigenvalues of $h$ is easy because $h$ is small compared to $A$ and also because it is upper hessenberg.  Therefore, to find the eigenvalues of $A$, we create $h$ through the orthogonalization process defined in section \ref{sec:Orthogonalize} and then use our favorite eigenvalue calculator.

\subsection{Eigenvectors}
Even though $h$ and $A$ are similar, this doesn't guarantee they have the same eigenvectors.  In fact they won't because the dimension of the eigenvectors of $h$ will be much smaller than those of $A$.

The eigenvectors of $A$ associated with the eigenvalues calculated from the similar matrix $h$, can be calculated as a linear combination of the orthonormal vectors, $q_j$.  The expansion coefficients for eigenvector $i$ are the elements of the $i^{th}$ eigenvector of $h$.

\subsection{Residual}
The residual norm gives some indication of how close we have converged upon our solution.  Saad (1992) shows that the residual norm is just the last component of the eigenvector $y_i^{(m)}$ multiplied by $h_{m+1,m}$.  This works well in the deterministic calculation, but I'm not sure it will works for all cases in the Monte Carlo calculation.

Suppose, if you will, that you have a point source in a---nearly---infinite medium.  The orthonormal vectors calculated will have non-zero elements in the middle few bins, and zeros elsewhere.  When calculating the eigenvector of the Monte Carlo operator, the outer elements of the eigenvector will also be zero.  For an infinite medium problem, the residual will be zero immediately.

Saad does say that the residual norms are ``not always indicative of actual errors'', but ``are quite helpful in deriving stopping procedures.''  For Monte Carlo applications of Arnoldi's Method, it seems clear that this simple calculation of the residual may give incorrectly small values and we might stop prematurely.

\section{Uncertainty}
Once the eigenvalue has been found, we must be able to place an uncertainty about it.  Our Matrix-Vector product is stochastic and our uncertainty should be in a form of standard deviation.

\section{Restarting}
One can restart Arnoldi's method after some number of iterations to reduce the memory requirements and the number of orthogonalization steps.  After a predetermined number of iterations, the simulation is stopped, the eigenvector of interest is calculated and this eigenvector (e.g. fission source) is used as the initial guess for an entirely new Arnoldi process.

\section{Preliminary Results}
I have a working version of Monte Carlo Arnoldi's method.  In this section, I present some arguments to give confidence to the results received.

\subsection{Uncertainty}
Currently we have no direct way of determining the uncertainty of a Monte Carlo Arnoldi simulation.  To get a rough estimate, I ran 50 simulations and calculated their average and variance/standard deviation.  The geometry was discretized into 10 bins.  I ran this series of simulations five times with 1000 histories per iteration and five times with 10,000 histories per cycle.  With 1000 histories per iteration the standard deviation was 0.01; with 10,000 histories per cycle the standard deviation was 0.004.

\subsection{Residual}
In Arnoldi's method, we can calculate the residual norm, a simple measure of the convergence of the calculated eigenvalues to the true eigenvalues.  Once the residual is sufficiently small iteration stops.  In deterministic Arnoldi's method, this is a good way to determine stopping procedures.

When investigating the Monte Carlo version of Arnoldi's method it would be nice to see the residual norm decrease as a function of Arnoldi iteration just as the deterministic does, but with the stochastic noise.  In Figure \ref{fig:Residual} the residual norm for both deterministic and Monte Carlo simulations are shown.  The deterministic simulation is a $1000 \times 1000$ diagonal matrix with the vector $[1,2,\ldots, 1000]$ on the diagonal.  The Monte Carlo simulation had slab geometry with half-width 0.5 mfp and discretized into 10 spatial bins.  Although the residual of the Monte Carlo simulation doesn't decrease as much as the deterministic residual, it does have the same trend of decreasing.

In my (limited) experience, the dominant eigenvalue is determined to machine precision before the residual is sufficiently low.  I believe this is due to the stochastic nature of Monte Carlo methods.  The residual hovers around $10^{-3}$ until the Krylov subspace has been spanned and then immediately drops to $10^{-13}$ or smaller.  The maximum number of iterations before the subspace has been spanned is equal to the number of spatial bins.  A simulation with 100 spatial bins would span the Krylov subspace in at most 100 iterations, but the eigenvalue is found---to within machine precisions---in about 50 iterations.
\begin{figure}[h]\centering
    \includegraphics[width=5in, keepaspectratio]{ArnoldiDtmResidual.pdf}
    \includegraphics[width=5in, keepaspectratio]{ArnoldiMCResidual.pdf}
    \caption{Residual norm as a function of the number of Arnoldi Iterations.  Top figure is Deterministic and the ``blips'' are when Arnoldi restarted.  The bottom figure is Monte Carlo.}
    \label{fig:Residual}
\end{figure}

\subsection{Eigenvalue and Timing Comparisons}
Comparing Monte Carlo Arnoldi's method to a traditional Power method is difficult.  First of all, we don't yet have a good way of determining the uncertainty in the eigenvalue for Arnoldi's method.  Second, it may be difficult to determine when the Power method has converged.

In this illustration I have used 1000 histories per iteration/cycle.  As indicated previously, this is roughly an uncertainty of 0.01 in Arnoldi's method.  The Power method used 20 inactive cycles and the uncertainty was comparable with that of Arnoldi's method.  The geometry was slab geometry with varying half-width.  In both Arnoldi's method and the Power method, the fission source was discretized into 10 spatial bins.  I show the results in Table \ref{tbl:Timing} where the results are compared with ``benchmark'' results from Martin and Duderstadt\footnote{William R. Martin and James J. Duderstadt, \emph{Nuclear Science an Engineering}, \textbf{62}, 371-390 (1970)}.

\begin{table}[h]\centering
    \caption{Eigenvalue and timing comparisons.}
    \label{tbl:Timing}
    \vspace{11pt}
    \begin{tabular}{|c|c|c|c|c|c|} \hline
        Half width [mfp] & M\&D & MC Arnoldi & time [s] & MC Power & time [s] \\ \hline
        0.5  & 0.448278 & 0.4520 & 2.1 & 0.4492 $\pm$ 0.011 & 4.8  \\ \hline
        1.0  & 0.643416 & 0.6438 & 2.5 & 0.6423 $\pm$ 0.019 & 5.7  \\ \hline
        5.0  & 0.952601 & 0.9545 & 4.2 & 0.9581 $\pm$ 0.024 & 8.6  \\ \hline
        10.0 & 0.985831 & 0.9899 & 4.8 & 0.9950 $\pm$ 0.025 & 9.0  \\ \hline
        30.0 & ---      & 1.0049 & 4.7 & 1.0078 $\pm$ 0.019 & 9.4  \\ \hline
    \end{tabular}
\end{table}

With this comparison it seems that one can accurately calculate the eigenvalue using a Monte Carlo version of Arnoldi's method.  From these results, it looks like Arnoldi's method is faster than the Power method, however it will take a more careful and detailed tests to make a definite decision.

\subsection{Eigenvector}
I am still working out the details of calculating the dominant eigenvector associated with the (correct) eigenvalue calculated.  In the tests shown in Table \ref{tbl:Timing} I can get the correct shape---roughly cosine---for each test.  Sometimes the eigenvector comes out upside-down.  This isn't too much of a problem since we know the eigenvector is only unique times a multiplicative constant; this constant could be -1.  Almost always, the eigenvector is negative at the edges of the slab.  This is more cause for concern; we know the eigenvector (the flux) must be zero or positive everywhere.  What then is to be done with negative components of the eigenvector?  I have plotted two resulting eigenvectors in Figure \ref{fig:Vector}, both were calculated with 10,000 histories per iteration.

\begin{figure}[h]\centering
    \includegraphics[width=5in, keepaspectratio]{NegativeEigenvector.pdf}
    \includegraphics[width=5in, keepaspectratio]{PositiveEigenvector.pdf}
    \caption{Eigenvector from two separate Monte Carlo Arnoldi simulations showing potential problems.}
    \label{fig:Vector}
\end{figure}

\newpage
\section{Algorithms}
\begin{algorithm2e}
    \SetVline
    \caption{Deterministic Arnoldi Process (freely borrowed from Watkins(2002))}\label{alg:DtmArm}
    $q_1 = q/\left\|q\right\|_2$ \;
    \For{$k=1,\ldots,m-1$}{
        $q_{k+1} \gets Aq_k$\;
        \For(\Comment*[f]{Orthogonalize}){$j=1, \ldots, k$}{
            $h_{jk} \gets \left<q_j,q_{k+1}\right>$\;
            $q_{k+1} \gets q_{k+1} - q_jh_{jk}$}
        $h_{k+1,k} \gets \left\|q_{k+1}\right\|_2$\;
        \If(\Comment*[f]{Span \{$q_1, \ldots, q_k$\} is invariant under $A$}){$h_{k+1,k} = 0$}{
            quit.}
        $q_{k+1} \gets q_{k+1}/h_{k+1,k}$\;
    }
\end{algorithm2e}

\begin{algorithm2e}
    \SetVline
    \caption{Deterministic Arnoldi Process, Python Style}\label{alg:DtmArnPy}
    \For(\Comment*[f]{$I$ = \# of Arnoldi Iterations.}){$k=1, \ldots, I+1$}{
        $q \gets AQ_{k-1}$\Comment*[f]{$(k-1)^{\textrm{st}}$ or last column of $Q$} \;
        \For{$j=1, \ldots, k+1$}{
            $h_{j-1,k-1} = \left<Q_{j-1},q\right>$ \Comment*[f]{$(j-1)^{\textrm{st}}$ column of $Q$} \;
            $q \gets q - Q_{j-1}*h_{j-1,k-1}$}
        $h_{k,k-1} \gets \left\|q\right\|_2$ \;
        $q = q/h_{k,k-1}$ \;
        \If(\Comment*[f]{Residual too small.}){$h_{k,k-1}*y_m < 10^{-10}$}{
            quit\Comment*[f]{See Saad 1992 p.175-76.}}
        $Q = \left[Q|q\right]$ \Comment*[f]{Append $q$ as last column of $Q$}}
\end{algorithm2e}

\begin{algorithm2e}
    \SetVline
    \caption{Monte Carlo Arnoldi Process}\label{alg:MCArnoldi}
    \For{$k=1, \ldots, iterations + 1$}{
        $q = AQ_k = \ldots$ \Comment*[f]{Monte Carlo portion} \;
        $q = \left(q/N\right)\int{\left|Q\right|}$ \Comment*[f]{Monte Carlo normalization} \;
        \For{$j=1, \ldots, k+1$}{
            $h_{j-1,k-1} = \left<Q_{j-1},q\right>$ \;
            $q = q - h_{j-1,k-1}*Q_{j-1}$}
        $h_{k,k-1} = \left\|q\right\|_2$ \Comment*[f]{Arnoldi normalization}\;
        $q = q/h_{k,k-1}$ \;
        Calculate eigenpairs\;
        Sort eigenpairs\;
        \If(\Comment*[f]{Residual too small.}){$h_{k,k-1}*y_m < 10^{-10}$}{
            quit Arnoldi.  Start Power Method.\Comment*[f]{See Saad 1992 p.175-76.}}
        $Q = \left[Q|q\right]$ \Comment*[f]{Append $q$ as last column of $Q$}}
\end{algorithm2e}

\end{document}


%
%  THIS FILE IS COMMON
%
\subsection{Bond Orders}\label{bonds}
\index{Valency|ff}\index{Lone pairs|(}\index{Bond order}\index{Wiberg indices}
Three quantities can be derived~\cite{bonds} from the density matrix for use in
discussing bonding.  These are: atomic bond index, anisotropy, and  bond
order.

The density matrix, $P$, can be decomposed into sub-matrices representing atoms
or interactions between atoms.  The three  quantities just mentioned can then
be defined in terms of these sub-matrices.

\subsubsection{Atomic bond index}
A measure of the valency of an atom.
\begin{equation}
V_A=\sum_{\lambda\in A}2P_{\lambda\lambda}-\sum_{\lambda\in A}\sum_{\sigma\in A}P_{\lambda\sigma}^2.
\end{equation}
Typical valencies are: 1.0 for hydrogen, 2.0--2.4 for oxygen to 3.8--4.0 for
carbon. The maximum valency of an atom is equal to the number of atomic
orbitals, e.g.\  1 or 4 (for a $sp^3$ system), or 9 (in MNDO-$d$). This maximum
is only achieved when the orbital population is 1.00, and all off-diagonal
terms on the atom are zero.

\subsubsection{Anisotropy}
A measure of the number of lone-pairs on an atom.
\begin{equation}
L_A=\sum_{\lambda\in A}\sum_{\sigma\in A}P_{\lambda\sigma}^2 - \sum_{l=0}^{k}
\frac{1}{2l+1}(\sum_{\lambda=l^2+1}^{(l+1)^2}P_{\lambda\lambda})^2.
\end{equation}
Typical numbers of lone-pairs are: 0 in, for example, hydrogen and carbon, 1 for
 nitrogen in amines, and 2 in oxygen.

To see how this expression is derived, consider an atom having valence orbitals
defined by angular quantum numbers $l=0,1,\ldots,k$.  For H, $k=0$, for all
other elements, $k=1$. In order to have spherical symmetry, all orbitals in any
shell must be equally occupied. In addition, since the product of any two
different atomic orbitals is non-spherical, all off-diagonal density matrix
terms on any one atom must be zero.

\index{Lone pairs|)}
For a spherical atom having the atomic populations $s^pp^qd^r$, the valency
would be:
\begin{equation}
V_A=p^2+\frac{q^2}{3}+\frac{r^2}{5},
\end{equation}
or, in general:
\begin{equation}
V_A = \sum_{l=0}^{k} \label{eq:V_A}
\frac{1}{2l+1}(\sum_{\lambda=l^2+1}^{(l+1)^2}P_{\lambda\lambda})^2.
\end{equation}
Since, in general, atoms are non-spherical, then:
\begin{equation}
V_A=\sum_{\lambda\in A}\sum_{\sigma\in A}P_{\lambda\sigma}^2 \label{eq:V_A2}.
\end{equation}
The difference between equations \ref{eq:V_A} and \ref{eq:V_A2} is a measure of how
unspherical the atom is.

\subsubsection{Bond order}
A measure of the number of bonds between atoms in a compound.
\begin{equation}
B_{AB}=\sum_{\lambda\in A}\sum_{\sigma\in B}P_{\lambda\sigma}^2
\end{equation}
Typical bond-orders are: 1.0, e.g.,  C-C in ethane; 2.0, e.g.,  C=C  in
ethylene; 3.0, e.g.,  C$\equiv$C in acetylene.  Bond orders of less than about
0.1--0.2 are indicative of ``no bond''.

The ideas here are an extension of Wiberg's indices~\cite{wiberg}.

Given the normal semiempirical density matrix, it is easy to show that
\begin{equation}
P^2=2P,
\end{equation}
from which it follows that
\begin{equation}
P_{\lambda\lambda}=1/2\sum_{\sigma}P_{\lambda\sigma}^2.
\end{equation}
This is the starting point for the derivation of
\begin{equation}
V_A=\sum_{B\neq A}B_{AB},
\end{equation}
from which the above definitions follow.


\section{Concepts}

\subsection{Overview}
Numerical Analysis takes place within the context of a
discrete computing machine with finite register sizes.  To
be specific, we can use the Intel 386/387 as the least
common denominator for Modula-3.  Further, Modula-3 is
currently implemented with a 32-bit Word and IEEE 32-bit and
64-bit floating points numbers, which happen to mirror the
386/387 hardware.  A large part of numerical analysis is
tuning algorithms so that they are not tripped up by the
finite limitations of the hardware.

First we must understand the number representations.  Then
we can discuss the typical errors which can arise.  Next we
will discuss ways around them.  Finally, we will layout an
approach to numerical analysis routines.

\subsection{Arithmetic Logic Unit (ALU)}
See Craw87, pg 18.

An ALU is the integer heart of a typical CPU.  It handles
raw machine word operations like:
\begin{itemize}
   \item  rotate
   \item  shift
   \item  bitset and reset
   \item  compare
   \item  test for sign
   \item  test for carry
\end{itemize}

It also does "mathematical" cardinal and integer operations
like:
\begin{itemize}
   \item  add
   \item  sub
   \item  mul
   \item  div
\end{itemize}

The ALU's 32 bits are typically drawn as 0..31 bits, with
the most significant figure on the left.

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
    31 & 30 & 29 & $\cdots$ & 2 & 1 & 0 \\ \hline
\end{tabular}
\end{center}

The raw values in the register are typically as given as
bitstrings of "0" and "1".  Modula-3 conveniently allows
this to be shown as a literal, preceded by the radix marker
"2\_".  You don't have to show the 0's to the left of the
most significant 1, but it doesn't hurt.  Often for
formatting, it is a good idea to left-pad with leading 0's
to a given width:

\begin{tt} \begin{verbatim}
     2_1010101010101010101010101010101 (*all 32 bits used*)
     2_10001010 (*just lowest 8 bits used*)
     2_00001011 (*just lowest 4 bits are used*)
\end{verbatim} \end{tt}

If you do nothing special, these literals will be fitted
into a full 32-bit INTEGER when they are assigned to a
variable.  But by defining types (and variables) with fewer
bits, we could save space:

\begin{tt} \begin{verbatim}
     RECORD
       i:=2_1010;                     (*32-bit INTEGER*)
       j:BITS  4 FOR [0..15]:=2_1010; (* 4-bit CARDINAL*)
       k:BITS 21 FOR [0..1000000];    (* 21-bit CARDINAL*)
       l:BITS  7 FOR [-5..50];        (* 7-bit INTEGER*)
     END;
\end{verbatim} \end{tt}

This makes sense in a packed record, but probably not
elsewhere.  For numerical analysis, Integer.i3 provides integers
and cardinals of 8, 16, 32 bits.

Completely independent of the choice of bit lengths is the
choice of radix for representing literals.  While binary
literals help clarify some numerical code, other radixes
might help elsewhere.  Unlike many languages, Modula-3
allows radixes from 2..16, e.g.,
\begin{tt} \begin{verbatim}
     8_03470
     16_12AF1
     3_012
     11_12AB3
\end{verbatim} \end{tt}

While raw bits can be used to implement many data types,
here we are only concerned with numbers.  Unsigned numbers
build up as binary numbers, for $0 \dots (2^{32} - 1)$.  Signed numbers
are represented in 2's complement form, for $-2^{31} \dots (2^{31} -
1)$.  See Craw87, pg 7 for details.

The critical thing to know about 2's complement is that the
32nd bit is used to indicate sign.  Thus, it is not
available for use in building number magnitudes.  Modula-3
reflects this by making {\tt CARDINAL}  go $0 \dots (2^{31} -1)$,
making it a subtype of {\tt INTEGER}.  What about unsigned numbers from
$2^{31} \dots (2^{32}-1)$?  You must use Word.T for those, and use the
Word module's operations.  While these look like function
calls (and thus slow), they are really compiled inline as
the obvious assembler statisticements.

What can go wrong?  Integer matrix is a sure thing, so long as
you don't overflow (e.g., go beyond $2^{31}-1$ for {\tt CARDINAL}) or
underflow (e.g., go below $0$ for {\tt CARDINAL}s or $-2^{32}$ for
{\tt INTEGER}s).  Modula-3 traps these conditions, but you should
plan your algorithm to avoid the conditions.  Use multi-
precision packages if you need more resolution.

For m3na, we will use {\tt CARDINAL}s when we mean counting
numbers (such as for array indices), and {\tt INTEGER}s when we
mean quantities which might be negative.

\subsection{Floating Point Unit (FPU)}
See Craw87, pp 20-30.

The 387 FPU provides IEEE 32-bit, 64-bit, and 80-bit reals.
These are structured as a sign bit, an exponent, and a
significand (also known as mantissa):
\begin{verbatim}
     32-bit
          sign        =1 bit
          exponent    =8 bits
          significand =23 bits (really 24 due to hidden 1)
          range       =+/- 3.39E38
          precision   =+/-1.18E-38

     64-bit
          sign        =1 bit
          exponent    =11 bits
          significand =52 (really 53 due to hidden 1)
          range       =+/- 1.08E308
          precision   =+/- 2.23E-308

     80-bit
          sign        =1 bit
          exponent    =15 bits
          significand =64 bits
          range       =+/- 1.19E4932
          precision   =+/- 3.36E-4932
\end{verbatim}

There are several problems in using these finite
representations for real numbers.
\begin{description}
\item[Out of Range]
     Suppose you want to represent one googol (1.0E100).  It
     just can't be done in the 32-bit format.  That may seem
     fanciful for practical problems, but intermediate
     values (e.g., sums of squares) might go beyond the 32-
     bit limits.

\item[Representation Error]
     Many decimal numbers do not convert exactly to/from
     binary.  You have to use a representation whose
     precision is sufficient that this error is not a
     problem.

\item[Truncation Error]
     Suppose you add two numbers in 32-bit format: $a=1.0e6$ and $b=1.00123$.

     To get a valid addition, we would need to get both a
     and b into the significand, both with the same
     exponent: $(1.0+0.00000100123)*10^6 = 1.00000100123e6$.
     There aren't enough bits in the significand to
     represent this.  It gets truncated to: $1.000001e6$.

     What we need to know is how small one number can be
     relative to another before part of the smaller gets
     lopped off.  This small amount is usually known as
     $\epsilon$ (epsilon).  The value is differnet for various
     representations, giving EPS32, EPS64, and EPS80.
     These are calculated as:
     \begin{verbatim}
          max = 2^sigbits
          rel prec = 1/max = 2^(-sigbits)=~ sigbits * ln(2)/ln(10)

          32-bit: about 1.0E-7 = ESP32
          64-bit: about 1.0E-17= EPS64
          80-bit: about 1.0E-24= EPS80
     \end{verbatim}

     As a consequence, there is no point in trying to tune
     an iteration solution beyond a certain point, e.g.:
     \begin{tt} \begin{verbatim}
         IF delta < value*EPS32 THEN
            EXIT; (*can't do any better*)
         ELSE
            value:=value+delta; (*room for improvement*)
         END;
     \end{verbatim} \end{tt}

     Truncation can happen as in the above example with
     addition and subtraction.  It can also happen with
     multiplication.  This is particularly true for
     intermediate values (such as squares).  It is worth
     your while to arrange to normalize the calculation
     toward 1.0 before doing multiplications.  Or use a
     longer real format.

\item[Roundoff Error]
     If you are in danger of truncating, you may round up or
     down.  If the rounding is not done right, it will
     introduce a bias into the calculation.

     Fortunately, Modula-3 reals are specified as IEEE
     reals, which include extra digits to assure proper
     rounding.
\end{description}

\subsection{32-bit vs 64-bit}
Is REAL32 good enough?  NR92, pg 25, suggests it is, and in
fact uses 32-bit reals for its printed algorithms.

However, Gems90 uses 64-bit reals, despite operating in a
speed-driven environment with low end-result resolution
(viewable by human eye).  Hopkins says:
\begin{quote}
     "Generally, if the real arithmetic has a mantissa of
     less than about 30 bits, then a double precision
     version of the library is produced.  In fact the double
     precision [64-bit] version is by far the most common,
     since most architectures which mimic IBM floating-point
     hardware and the IEEE floating-point standard all
     define the mantissa of real variables to be around 24
     bits.  This accuracy is not sufficient for reliable
     numerical computation."  Hopk88, pg 34.
\end{quote}

Further, the Modula-3 Math library is 64-bit.  Thus it
appears the choice is REAL64.

\subsection{Timing}
Each machine will have different timing for its hardware
ALU and FPU operations, and each compiler will have
different approaches for calling upon these operations (thus
further diversifying timing).  However, it is probable that
for the class of problems we are addressing, we can
determine the rough relative timing cost for operations.

Craw87 provides timing data in the appendices.  While every
combination of source/dest is slightly different, we can use
the "best-case" for the register-to-memory operation as the
exemplar.  If we normalize the cycles to let integer add (32
bit) be "1", and round to integers, the ratios are:
\begin{verbatim}
     iadd32         1
     imul32         5
     idiv32         6
     fadd32         3
     fadd64         4
     fmul32         4
     fmul64         5
     fdiv32         12
     fdiv64         13
\end{verbatim}

In other words, integers are a lot faster than floating-
point, and divide is the killer.  Interestingly, 64-bit is
not much worse than 32-bit.  Thus our choice of 64-bit as
the basic format is not all that extravagant.

\subsection{Development Approach}
Here is our approach to developing routines:
\begin{enumerate}
     \item Pick an interesting topic.  If you aren't
     interested, you won't stick with it long enough to do a
     good job.

     \item Gather relevant sources (e.g., texts, code
     libraries, journal articles).  Determine an effective
     user interface.  Determine test cases from known-good
     sources such as matrix packages or handbooks.

     \item Study the problem until you can explain the standard
     implementation(s) line by line.  You may need to work
     backwards and forwards from the mathematical formulas
     to the code.  You typically need to work out simple
     (but not too simple) examples, e.g., 4x4 matrix.

     \item Study the iterative operations for truncation
     conditions.  Do you need to renormalize the data toward
     1.0?  Do EPS checks?  Use a longer real format?

     \item Implement your version of the algorithm.
     \begin{enumerate}
          \item Test it for basic correctness against known
          good values.  Don't worry about being off by a few
          parts in a thousand.  At this stage you should be
          concerned with wildly wrong results.  If it made
          sense on paper, and you got it to compile, then
          chances are it is a fencepost error (off-by-one)
          in the iteration conditions.  Debug as needed.

          \item Test for full accuracy against known-good
          values.  Does your algorithm fall apart with big
          or little numbers?  Is it erratic?  Check for
          truncation errors and more fencepost errors.
          Debug as needed.

          \item Test for speed against published algorithms.
          Use the Time module and multi-second runs.
       \end{enumerate}
\end{enumerate}

When it comes time to worry about truncation errors and
timing, look for normalization toward the center of the
range.  The classic is the hypotenuse of a right triangle:
\begin{verbatim}
          hyp:=sqrt(a^2 + b^2); (*where a>b*)
          (*a^2 is in danger of producing an overflow
            although the final result
            is perfectly in the representable range.*)

          (*Instead, do:*)
          r:=(b/a);
          hyp:=abs(a)*sqrt(1.0+r*r);
\end{verbatim}

When it comes time to worry about speed, consider:
\begin{enumerate}
     \item Precalculate anything you can:
     \begin{itemize}
          \item Compile-time constant propagation
          \item Runtime initialization
          \item Calltime initialization
          \item Outer loop instead of inner loop
          \item Common subexpressions
      \end{itemize}

     \item Unroll loops, especially if you know the algorithm will
     only need a few iterations.
\end{enumerate}

Do you really need to do these things?  Modern compilers
know a great deal about such local optimizations.  If you
can do it and the code becomes more readable and more
maintainable, then do so.  But don't screw up clean code for
a few percent speedup.  You will still be debugging when a
faster computer shows up on your desk.  Instead use your
time looking for fundamentally better algorithms.  If you
still need speed, get out your assembler.