\chapter{Discrete two-body reactions}
\label{Sec:2-body}
This section describes how the contribution to the transfer matrix
is calculated for data consisting of probability densities for
the cosine of the angle of deflection in discrete 2-body reactions.
In this case, the probability densities are always given in
the center-of-mass frame.
Because
the transfer matrices are defined in terms of laboratory
coordinates, the computations involve a boost.

For all except very light-weight targets, the mapping from
center-of-mass to laboratory coordinates is usually done using
Newtonian mechanics.  The discussion given here is therefore
Newtonian.  A relativistic treatment is presented in Appendix~\ref{Appendix-relativity}.
The choice of Newtonian or relativistic mechanics is determined by
the value of the \textsf{kinetics} input parameter to \gettransfer\ as
explained in Section~\ref{Sec:relativistic}.  Of course, relativistic mechanics must
be used if either the incident particle or the outgoing particle is a
photon.

For discrete 2-body reactions,  the center-of-mass energy of the emitted particle
is determined by the energy $E$ of the incident particle.  Consequently, the
energy-angle probability density $\picm(\Ecm', \mucm \mid E)$ in the
center-of-mass frame is given by
\begin{equation}
  \picm(\Ecm', \mucm \mid E) =
  g( \mucm \mid  E) \, \delta( \Ecm' - \Psi( E ) )
  \label{prob_cm}
\end{equation}
for the function~$\Psi$ given below in Eq.~(\ref{E_cm}).
From here on, the energy~$E$ and direction cosine~$\mu$
of the outgoing particle will be marked with the subscript ``lab'' or ``cm'' to
indicate that the variable is in the laboratory or center-of-mass frame.

Because of Eq.~(\ref{prob_cm}), the data for discrete 2-body reactions consist
of angular probability densities $g( \mucm \mid E)$ given in the
center-of-mass frame, either as a 2-dimensional table
for given incident energy~$E$ and direction cosine~$\mucm$
or as Legendre coefficients $c_\ell(E)$ for
\begin{equation}
  g( \mucm \mid  E) =
  \sum_\ell
  \left(
     \ell + \frac{1}{2}
  \right)
    c_\ell(E)P_\ell( \mucm ).
  \label{cmLegendre}
\end{equation}

This section begins with an overview of Newtonian mechanics
for discrete 2-body problems.  In particular, the form of the function
$\Psi$ in Eq.~(\ref{prob_cm}) is derived, as is the boost from the
center-of-mass to the laboratory frame.  The section closes
with an examination of the use of angular probability data
$g( \mucm \mid E)$ in the computation of the integrals
Eqs.~(\ref{Inum}) and~(\ref{Ien}) used in the calculation of the
transfer matrix.

\section{Newtonian mechanics of discrete 2-body reactions}
\label{Sec:2-body-Newton}
Only a summary of the results is given here; for more information,
see the reference~\cite{endep}.  A relativistic treatment is
developed in Appendix~\ref{Appendix-relativity}.  It is assumed that the target is at rest
and that the incident particle has
energy $E$ in laboratory coordinates.

The following
notations are used for the masses of the particles involved:\\
 \Input{$\myi$,}{  the mass of the incident particle,}\\
 \Input{$\mtarg$,}{ the mass of the target,}\\
 \Input{$\myo$,}{ the mass of the emitted particle,}\\
 \Input{$\mres$,}{ the mass of the residual.}\\
For the conversion
between center-of-mass and laboratory coordinates, define
the mass ratios
$$
  \gamma = \frac
    { \myi \myo }
    { ( \myi + \mtarg )^2 },
$$
$$
   \beta = \frac
    { \mres }
    { \myo + \mres },
$$
and
$$
   \alpha = \frac
    {\beta \mtarg}
    { \myi + \mtarg }.
$$

Velocity vectors are printed in bold face $\textbf{V}$ with
magnitude (speed) in math italics
$$
  V = | \textbf{V} |.
$$

For a target at rest and an incident particle with
energy $E$ in laboratory coordinates, the center of mass
moves in the direction of motion of
the incident particle with velocity $\Vtrans$ having magnitude squared
\begin{equation}
  \vtrans^2 = \Vtrans^2 =
    \frac{ 2\myi  E}
    { ( \myi + \mtarg )^2 }.
  \label{Vtrans-length}
\end{equation}

The reaction may have a nonzero energy value $Q$, arising
for example from the excitation level of the target
and/or residual nucleus in inelastic scattering.  A nonzero
$Q$ value may also arise from the
mass difference in a knock-on reaction.
It follows from conservation of energy and momentum that in
center-of-mass coordinates the energy of the emitted
particle is given by
\begin{equation}
  \Ecm' = \Psi( E ) = \alpha E + \beta Q.
  \label{E_cm}
\end{equation}
This defines the function $\Psi$ appearing in Eq.~(\ref{prob_cm}).
The speed of the outgoing particle in the
center-of-mass frame is
\begin{equation}
  \vcm' = |\Vcm'| = \sqrt{ \frac{ 2\Ecm' }{\myo} }.
    \label{V-cm-outgoing}
\end{equation}
It follows from Eq.~(\ref{E_cm}) that for an endothermic
reaction ($Q < 0)$, the threshold is at
$$
  E = \frac{-\beta Q}{\alpha}.
$$

\subsection{The boost to the laboratory frame}
As illustrated in Figure~\ref{Fig:2-body-boost}, the boost from center-of-mass
to laboratory coordinates is obtained by adding the velocities
\begin{equation}
  \Vlab' = \Vtrans + \Vcm'.
 \label{V-lab-2-body}
\end{equation}
Consequently, the energy of the outgoing particle in the
laboratory frame is
$$
  \Elab' = \frac{ \myo {\Vlab'}^2 }{2} =
        \frac{ \myo }{2}
        ( \vtrans^2 + {\vcm'}^2 + 2\Vtrans \cdot \Vcm' ).
$$
In terms of the notation Eq.~(\ref{E_cm}) and
\begin{equation}
  \Etrans' = \frac{ \myo \vtrans^2 }{2} =
  \gamma E,
  \label{E_trans}
\end{equation}
this equation takes the form
\begin{equation}
  \Elab' = \Etrans' + \Ecm' + 2 \mucm \sqrt{ \Etrans' \Ecm' }.
  \label{E_lab}
\end{equation}
Here, $\mucm$ is the direction cosine defined by the
relation
$$
   \Vtrans \cdot \Vcm' = \mucm \vtrans \vcm'.
$$

\begin{figure}
\input{fig4-1}
\end{figure}

It is also necessary to determine the direction cosine $\mulab$ in
the laboratory frame for
$$
     \Vtrans \cdot \Vlab' = \mulab \vtrans \vlab'.
$$
This is most easily derived from the trigonometry in Figure~\ref{Fig:2-body-boost}
$$
  \mulab  \vlab' =
   \vtrans + \mucm \vcm'.
$$
In terms of the energies defined in Eqs.~(\ref{E_cm}), (\ref{E_trans}),
and~(\ref{E_lab}), this relation takes the form
\begin{equation}
  \mulab  = \frac
   { \sqrt{ \Etrans' } + \mucm \sqrt{ \Ecm'} }
    {\sqrt{\Elab'}}
    \quad \text{if $\Elab' > 0$.}
  \label{get_mu}
\end{equation}

It is clear from Eq.~(\ref{V-lab-2-body}) that
$$
  \Elab' = \frac{\myo {\vlab'}^2}{2} = 0,
$$
if and only if
$$
  \Vcm' = - \Vtrans.
$$
In this case, the value of $\mulab$ is undefined.

\section{Computation of the transfer matrix from data for
discrete 2-body reactions}
Consider the use of data $g(\mucm \mid E)$ in Eq.~(\ref{prob_cm})
in the computation of integrals for the transfer matrix
Eqs.~(\ref{Inum}) and~(\ref{Ien}), either as tables or as Legendre
coefficients in Eq.~(\ref{cmLegendre}).  In these integrals
the multiplicity is always $M(E) = 1$ for discrete 2-body reactions.
The discussion given here concentrates on the
evaluation of the integral in Eq.~(\ref{Inum}).  The integral in
Eq.~(\ref{Ien}) differs only in that its integrand contains an extra
factor $\Elab'$, the energy of the outgoing particle in the laboratory frame.

Because the probability density data $g(\mucm \mid E)$ in
Eq.~(\ref{prob_cm}) is given in center-of-mass coordinates, it is
desirable to transform the integrals Eqs.~(\ref{Inum})
to the center-of-mass frame.
The center-of-mass form of the integral Eq.~(\ref{Inum}) is
\begin{equation}
    \Inum_{g,h,\ell} =
   \int_{\calE_g}dE \, \sigma ( E ) w(E) \widetilde \phi_\ell(E)
     \int_{\mucm} d\mucm  \,  g(\mucm \mid E)
     \int_{\Ecm'} d\Ecm' \, P_\ell( \mulab ) \,
        \delta(\Ecm' - \Psi(E) )
  \label{cmint}
\end{equation}
with $\Psi(E)$ as given by Eq.~(\ref{E_cm}).
The range of integration over $\mucm$ and $\Ecm'$ in
Eq.~(\ref{cmint})  is such that for fixed incident energy $E$ in $\calE_g$,
the energy $\Elab'$ of the outgoing particle given by Eq.~(\ref{E_lab}) lies
in~$\calE_h'$.

Integration of Eq.~(\ref{cmint})  with respect to $\Ecm'$ yields the result that
\begin{equation}
    \Inum_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) w(E) \widetilde \phi_\ell(E)
    \int_{\mucm} d\mucm  \,
     P_\ell( \mulab ) g(\mucm \mid E),
  \label{muEint}
\end{equation}
where it is understood that the direction cosine $\mulab$ in the laboratory
frame is calculated from Eq.~(\ref{get_mu}) and that the range of
integration over $\mucm$ is such that $E$ is in~$\calE_h'$.

The \gettransfer\ code steps through the data $g(\mucm \mid E)$
to compute contributions to the entries of the transfer matrix in
Eq.~(\ref{muEint}).
The case of tabular data with direct interpolation (Section~\ref{Sec:direct-interp}) is
illustrated in the laboratory frame in Figure~\ref{Fig:2-body-region-lab}.
This figure shows an integration region identified by an incident energy
bin~$\calE_g$ and an outgoing energy bin~$\calE_h'$.  The data
$g(\mucm \mid E)$ are given at incident energies $E_{k-1}$ and
$E_k$, such that the interval $E_{k-1} < E < E_k$ overlaps the
energy bin~$\calE_g$.  Furthermore, it is assumed that data entries
$g(\mucm \mid E)$ for $\mucm = \mucmjm$ and $\mucm = \mucm$ are
given at $E = E_{k-1} $ or at $E = E_k$ and that the table contains
no entries $g(\mucm \mid E_{k-1})$ or $g(\mucm \mid E_k)$ for
$\mucmjm < \mucm < \mucm$.  Any missing data values
$g(\mucmjm \mid E_{k-1})$ or $g(\mucmj \mid E_{k-1})$ or
$g(\mucmjm \mid E_k)$ or~$g(\mucmj \mid E_k)$ are computed
by interpolation with respect to~$\mucm$.  The integration region
in the laboratory frame
for the contribution of such a set of data to the integral $\Inum_{g,h,\ell}$
in Eq.~(\ref{cmint}) is the shaded area of Figure~\ref{Fig:2-body-region-lab}.  This region is
mapped to center-of-mass coordinates in Figure~\ref{Fig:2-body-region-cm}.

\begin{figure}
\input{fig4-2}
\end{figure}

\begin{figure}
\input{fig4-3}
\end{figure}

When the tabular data are interpolated by the method of cumulative
points of Section~\ref{Sec:cumProb}, the geometry is complicated by the
local unit-base transformations, but the basic ideas are the same.
Finally, for probability density data $g( \mucm \mid E)$ given as
Legendre coefficients in Eq.~(\ref{cmLegendre}), the only significant
difference is that the range of direction cosines becomes $-1 \le \mucm \le 1$
with the limitation that the energy $E$ of the outgoing particle lies in the
energy bin~$\calE_h'$.

\section{Format of data in the input file}
For tabulated probability density data $g( \mucm \mid E)$, the
data identifier as in Section~\ref{data-model}, is\\
  \Input{Process: two body transfer matrix}{}\\
and for the Legendre coefficients it is\\
  \Input{Process: Legendre two body transfer matrix}{}

\subsection{Data for both forms of probability density}
\label{Sec:2-body-data}
Because the boost from the center-of-mass frame to the laboratory
frame depends on the rest masses of the particles, these must be included
in the input file as described in Section~\ref{model-info}.
For most reactions, the format for doing so is\\
  \Input{Projectile's mass:}{$\myi$} \\
 \Input{Target's mass:}{$\mtarg$} \\
 \Input{Product's mass:}{$\myo$} \\
 \Input{Reaction's Q value:}{$Q$} \\
The values of these quantities must be in the same units as
the energy bin boundaries.
The mass of the residual $\mres$ is then computed using
\begin{equation}
   \mres = \myi + \mtarg - \myo - Q.
 \label{2-body-mres}
\end{equation}

The mass of the residual may be given using the command\\
 \Input{Residual's mass:}{$\mres$}\\
but this is overridden by the result of Eq.~(\ref{2-body-mres})
unless the residual is a photon, $\mres = 0$.  In that case,
the value of $\myo$ is modified to enforce the
validity of Eq.~(\ref{2-body-mres}).

The code may use either Newtonian or relativistic mechanics in its
computations as specified in Section~\ref{Sec:relativistic}.  Relativistic
mechanics is used, however, if any of the particles involved in the reaction
is a photon.

The specifications that the energy $E$ of the incident particle is given in the laboratory
frame and the direction cosine $\mucm$ in the center-of-mass frame are,
Section~\ref{Reference-frame},\\
  \Input{Projectile Frame:  lab}{}\\
  \Input{Product Frame:  CenterOfMass}{}

\subsection{Angular probability density tables}
\label{Sec:angular-table}
The identification line for tabulated angular probability densities
is\\
  \Input{Angular data:}{$n = K$}\\
where $K$ is the number of incident energies~$E$.
This is followed by the interpolation rules for probability
densities from Section~\ref{interp-flags-probability}\\
  \Input{Incident energy interpolation:}{probability interpolation flag}\\
  \Input{Outgoing cosine interpolation:}{list interpolation flag}

There are then $K$ blocks, one for each incident energy $E_k$,\\
  \Input{Ein: $E_k$:}{$n = J_k$}\\
with $J_k$ pairs of values $\mucmj$ and $g(\mucm \mid E_k)$.  Thus,
with incident energy in MeV
a table of angular probability densities $g( \mucm \mid E)$ may look like\\
  \Input{Angular data:}{$n = 22$}\\
  \Input{Incident energy interpolation:}{lin-lin direct}\\
  \Input{Outgoing cosine interpolation:}{lin-lin}\\
  \Input{ Ein: 1.500000000000e-01 : n = 2}{}\\
   \Input{\indent -1.000000000000e+00  5.000000000000e-01}{}\\
    \Input{\indent 1.000000000000e+00  5.000000000000e-01}{}\\
  \Input{ Ein: 2.000000000000e-01 : n = 2}{}\\
  \Input{\indent  -1.000000000000e+00  4.550000000000e-01}{}\\
    \Input{\indent 1.000000000000e+00  5.450000000000e-01}{}\\
   \Input{\indent  } {$\cdots$}\\
  \Input{ Ein: 2.000000000000e+01 : n = 29}{}\\
   \Input{\indent -1.000000000000e+00  3.873180000000e-02}{}\\
  \Input{\indent  -9.500000000000e-01  2.943580000000e-02}{}\\
   \Input{\indent -9.000000000000e-01  2.582090000000e-02}{}\\
   \Input{\indent  } {$\cdots$}\\
    \Input{\indent 9.000000000000e-01  2.530490000000e+00}{}\\
   \Input{\indent 9.500000000000e-01  3.873180000000e+00}{}\\
    \Input{\indent 1.000000000000e+00  8.262750000000e+00}{}

\subsection{Legendre coefficients of angular probability density}
\label{Sec:2-bodyLegendreData}
Legendre coefficient data of the form Eq.~(\ref{cmLegendre})
for discrete 2-body reactions are given as\\
    \Input{Legendre coefficients:}{$n = K$}\\
where $K$ is the number of incident energies~$E$.
This is followed by the interpolation rule for simple lists
from Section~\ref{interp-flags-list}\\
  \Input{Interpolation:}{list interpolation flag}

The file closes with $K$ sets of data\\
    \Input{Ein: $E_k$:}{$n = L_k$}\\
with $L_k$ Legendre coefficients $c_\ell(E_k)$ for $\ell = 0$, 1,
\ldots\ , $L_k - 1$ in Eq.~(\ref{cmLegendre}).  With incident energy
in units of MeV, an example of this portion of the input file is\\
   \Input{Legendre coefficients: n = 17}\\
  \Input{Interpolation:}{lin-lin}\\
  \Input{  Ein: 1.843100e+00:  n = 3}{}\\
    \Input{\indent 1.000000e+00}{}\\
    \Input{\indent 0.000000e+00}{}\\
    \Input{\indent 0.000000e+00}{}\\
   \Input{\indent  } {$\cdots$}\\
  \Input{ Ein: 2.000000e+01:  n = 12}{}\\
    \Input{\indent 1.000000e+00}{}\\
    \Input{\indent 4.640500e-01}{}\\
    \Input{\indent 2.320700e-01}{}\\
    \Input{\indent 8.593700e-02}{}\\
    \Input{\indent 5.338700e-02}{}\\
    \Input{\indent 2.465600e-02}{}\\
    \Input{\indent -1.500600e-03}{}\\
    \Input{\indent -1.756300e-02}{}\\
   \Input{\indent  -1.108000e-02}{}\\
    \Input{\indent 1.931100e-02}{}\\
    \Input{\indent 1.150900e-02}{}\\
    \Input{\indent 5.643500e-03}{}

{
\newcommand{\Vacm}{\textbf{V}_{\text{1,cm}}}
\newcommand{\vacm}{V_{\text{1,cm}}}
\newcommand{\muacm}{\mu_{\text{1,cm}}}
\newcommand{\Valab}{\textbf{V}_{\text{1,lab}}}
\newcommand{\valab}{V_{\text{1,lab}}}
\newcommand{\Vbcm}{\textbf{V}_{\text{2,cm}}}
\newcommand{\Vblab}{\textbf{V}_{\text{2,lab}}}
\newcommand{\vblab}{V_{\text{2,lab}}}
\newcommand{\mualab}{\mu_{\text{1,lab}}}
\newcommand{\vbcm}{V_{\text{2,cm}}}
\newcommand{\mubcm}{\mu_{\text{2,cm}}}
\newcommand{\mayo}{m_{1,e}}
\newcommand{\mares}{m_{1,r}}
\newcommand{\mbyo}{m_{2,e}}
\newcommand{\mbres}{m_{2,r}}

\section{Two consecutive discrete 2-body reactions}
\label{Sec:2-step-2-body}
The \ENDFdata\ library~\cite{ENDFdata} contains data for one
reaction in the form of a sequence of two discrete 2-body reactions.
In this reaction, an incident deuteron hits a triton, with
an outgoing excited ${}_2^4\text{He}$ nucleus and a neutron residual.
The  excited ${}_2^4\text{He}$ nucleus then decays into a proton plus
a triton.

\begin{figure}
\input{fig4-4}
\end{figure}

In the current version of \gettransfer, the probability
density $g( \muacm \mid E)$
for the outgoing excited particle from the first step
must be represented as a table of Legendre
coefficients $c_\ell(E)$
for the expansion Eq.~(\ref{cmLegendre}).
The breakup second step is assumed
to be isotropic in the frame of the excited outgoing particle
from the first step.  A Newtonian analysis is given
here; see Appendix~\ref{Sec:2-step-2-body-rel} for a
relativistic version.

For the first step of the
reaction the masses are $\myi$ for the incident particle,
$\mtarg$ for the target, $\mayo$ for the outgoing particle
which breaks up, and $\mares$ for the residual.  The $Q$-value
of the first step is denoted by~$Q_1$.

Figure~\ref{Fig:2-step-2-body-step1} illustrates
the notation used in analysis of the first step of this reaction.
This figure is basically a copy of Figure~\ref{Fig:2-body-boost}.
In the discussion of
this figure, velocity vectors are denoted with bold
face~$\textbf{V}$ and their
lengths with math italics~$V$.

In Figure~\ref{Fig:2-step-2-body-step1}, the vector $\Vtrans$
is the velocity of the center of
mass for the first step of the reaction, and its magnitude is
as in Eq.~(\ref{Vtrans-length}).  For determination of the
center-of-mass velocity $\vacm'$
of the excited outgoing particle from the first step,
Equation~(\ref{E_cm}) is modified to the form
$$
  \frac{\mayo ({\vacm'})^2}{2} =
    \frac{ \mtarg \mares E} { ( \myi + \mtarg ) ( \mayo + \mares ) } +
    \frac{ \mares Q_1} { ( \mayo + \mares ) },
$$
where $E$ is the energy of the incident particle and the
target is at rest.
If $\muacm$ is the direction cosine for~$\Vacm'$,
then the velocity $\Valab'$ in the laboratory frame of the
excited outgoing particle from the first step satisfies the equation
$$
  ({\valab'})^2 = \vtrans^2 + ({\vacm'})^2 +
    2 \muacm \vtrans \vacm'.
$$

According to Eq.~(\ref{get_mu}), if $\valab' > 0$, then
the direction cosine~$\mualab$ is given by
\begin{equation*}
  \mualab  = \frac
   { \vtrans'  + \muacm  \vacm' }
    {\valab'}
\end{equation*}
For $\valab' = 0$, one may set $\mualab = 1$.

For the second (breakup) step,
$\mbyo$ denotes the mass of the outgoing particle and
$\mbres$ the mass of the residual, and $Q_2$ is
the $Q$-value.

Figure~\ref{Fig:2-step-2-body-step2} shows this second step
projected onto the plane determined by the vectors of the first step.
In this figure the full 3-dimensional geometry must be taken into
account because the emission is isotropic.  The point~$O'$ in the figure
identifies the center of mass of the breakup step.  An orthonormal
$(\xi, \eta, \zeta)$-coordinate system
is introduced with origin at~$O'$. If $\valab' > 0$, the $\xi$-axis
is chosen parallel to
the vector~$\Valab'$; otherwise, it is taken parallel to~$\Vtrans$.
If  the vectors $\Vtrans$ and~$\Valab'$ generate a plane, then
the $\eta$-axis is selected to lie in this plane.  For colinear
$\Vtrans$ and~$\Valab'$, the $\eta$-axis may be in any direction
perpendicular to the $\xi$-axis.
The $\zeta$-axis is chosen perpendicular to the $(\xi, \eta)$-plane.

In this reference frame the magnitude of the
velocity~$\Vbcm'$ of the outgoing
particle from the breakup step is obtained from Equation~(\ref{E_cm})
as
$$
  \frac{\mbyo (\vbcm')^2}{2} =
    \frac{ \mbres Q_2} { ( \mbyo + \mbres ) }.
$$
Because the breakup is isotropic, the vector~$\Vbcm'$ in
Figure~\ref{Fig:2-step-2-body-step2}
with tail at~$O'$ has its head uniformly distributed on
the sphere~$\Sigma_0$.  For a fixed $\Valab'$ and angle~$\theta_2$
between $\Vbcm'$ and the $\xi$-axis, the head of~$\Vbcm'$ lies
on a circle on~$\Sigma_0$, which is projected as the line segment from
$A$ to~$B$ in  Figure~\ref{Fig:2-step-2-body-step2}.
The magnitude~$\vblab'$ of the velocity of the final emitted
particle is the same for all vectors~$\Vbcm'$ with heads on
the segment from $A$ to~$B$, and in the case that the head is
at~$B$ it is clear that
\begin{equation}
  ({\vblab'})^2 = (\valab')^2 + (\vbcm')^2 +
    2 \mubcm \valab' \vbcm',
 \label{2-step-2-body-V}
\end{equation}
where $\mubcm = \cos \theta_2$.
Furthermore, the energy of the final outgoing particle in the
laboratory frame is
\begin{equation}
  \Elab' = \frac{ \mbyo (\vblab')^2 }{2}.
 \label{2-step-2-body-E}
\end{equation}

For this 2-step reaction, in the computation of the
elements of the transfer matrix Eq.~(\ref{muEint}) is
replaced by
\begin{equation}
    \Inum_{g,h,\ell} =
     \int_{\calE_g} dE \, \sigma ( E ) w(E) \widetilde \phi_\ell(E)
    \int_{\muacm} d\muacm  \, g(\muacm \mid E)
    \int_{\Sigma_{0,h}} d\sigma_0 \, P_\ell( \mulab ).
  \label{2-step-muEint}
\end{equation}
In this integral $\Sigma_{0,h}$ is the subset of $\Sigma_0$ on which
$\Elab'$ from Eqs.~(\ref{2-step-2-body-V}) and~(\ref{2-step-2-body-E})
lies in the outgoing energy bin~$\calE_h'$,
and $d\sigma_0$ is the differential surface area
on the sphere~$\Sigma_0$ normalized so that
$$
  \int_{\Sigma_0} d\sigma_0 = 1.
$$

The direction cosine~$\mulab$ in Eq.~(\ref{2-step-muEint})
is obtained from
\begin{equation}
  \Vblab' \cdot \Vtrans = \mulab  \vblab' \vtrans.
  \label{2-step-mulab}
\end{equation}
The geometry used in the computation of $\mulab$ is illustrated
in Figure~\ref{Fig:2-step-2-body-step2}.
This figure shows a case in which
$\Vbcm'$ lies outside of the $(\xi, \eta)$-plane.  The
$\zeta$-component of~$\Vbcm'$ is orthogonal to~$\Vtrans$,
so it suffices to work with the projection of $\Vbcm'$ onto
the $(\xi, \eta)$-plane in the computation of~$\mulab$
in Eq.~(\ref{2-step-mulab}).  Consequently, if $\vblab' > 0$, it easily seen that
\begin{equation}
  \mulab = \frac{ \mualab ( \valab' + \xi ) - \eta \sqrt{ 1 - \mualab^2 } }
             { \vblab'}.
  \label{2-step-mulab-xi}
\end{equation}
For $\vblab' = 0$, the value of~$\mulab$
is taken as~$\mulab = 1$.

\begin{figure}
\input{fig4-5}
\end{figure}

It remains to identify the set~$\Sigma_{0, h}$ in the integral
Eq.~(\ref{2-step-muEint}).
In terms of the coordinate~$\zeta$, the surface of the sphere~$\Sigma_0$ may
be written as
\begin{equation}
  \zeta = \pm \sqrt{
     (\vbcm')^2 - \xi^2 - \eta^2 }
     \quad \text{for $\xi^2 + \eta^2 \le (\vbcm')^2.$}
  \label{2-step-Sigma-xi}
\end{equation}
Because of the mirror symmetry in the $(\xi, \eta)$ plane, it suffices to work with the
positive square root
\begin{equation}
  \zeta = \sqrt{
     (\vbcm')^2 - \xi^2 - \eta^2 }
     \quad \text{for $\xi^2 + \eta^2 \le (\vbcm')^2.$}
  \label{2-step-def-zeta}
\end{equation}
The energy
$\Elab'$ in Eq.~(\ref{2-step-2-body-E}) may lie in the outgoing
energy bin~$\calE_h'$ for fixed incident energy~$E$
and for step~1 direction cosine~$\muacm$.  If it does so
for at least 2 values of the step~2 direction cosine~$\mubcm$,
then $\Elab'$ is in~$\calE_h'$ for $\mubcm$
on an interval
\begin{equation}
  a_h \le \mubcm \le b_h
  \label{2-step-ab-range}
\end{equation}
with
$$
  -1 \le a_h < b_h \le 1.
$$
Consequently, for the hemisphere in Eq.~(\ref{2-step-Sigma-xi}) the integral
over $\Sigma_{0,h}$ in Eq.~(\ref{2-step-muEint}) may be written as
\begin{equation}
   \int_{\Sigma_{0,h}} d\sigma_0 \, P_\ell( \mulab ) =
   \frac{1}{2 \pi \vbcm' }
   \int_{ a_h \vbcm}^{b_h \vbcm} d\xi \int_{- \sqrt{ (\vbcm')^2 - \xi^2 }}
                           ^{\sqrt{ (\vbcm')^2 - \xi^2 }} d\eta \,
          \frac {P_\ell( \mulab ) } { \sqrt{ (\vbcm')^2 - \xi^2 - \eta^2}}.
  \label{2-step-Sigma-int}
\end{equation}
In this integral the change of variables
\begin{alignat}{2}
  \xi &= \vbcm' \, \mubcm
    \quad &\text{for $a_h \le \mubcm \le b_h$,}
      \label{2-step-def-xi} \\
  \eta &= \vbcm' \sqrt{ 1 - \mubcm^2 } \, \sin w
    \quad &\text{for $-\pi/2 \le w \le \pi/2$}
  \label{2-step-def-eta}
\end{alignat}
leads to the relation
\begin{equation}
   \int_{\Sigma_{0,h}} d\sigma_0 \, P_\ell( \mulab ) =
   \frac{1}{2 \pi }
   \int_{ a_h}^{b_h} d\mubcm \int_{- \pi/2}
                           ^{\pi/2} dw \,
          P_\ell( \mulab ) .
 \label{2-step-int-sigma}
\end{equation}
This representation is used in the calculation of the
subintegral over $\Sigma_{0,h}$ in Eq.~(\ref{2-step-muEint}).

\subsection{The input file for a 2-step 2-body reaction}
In the input file to \gettransfer, the identifier for this reaction is\\
  \Input{Process: two step two body reaction}{}\\
The particle masses and the $Q$-values for this reaction are
given by\\
 \Input{Target's mass:}{$\mtarg$}\\
 \Input{Projectile's mass:}{$\myi$}\\
 \Input{First residual's mass:}{$\mares$}\\
 \Input{First product's mass:}{$\mayo$}\\
 \Input{First step's Q value:}{$Q_1$}\\
 \Input{Second product's mass:}{$\mbyo$}\\
 \Input{Second residual's mass:}{$\mbres$}\\
 \Input{Second step's Q value:}{$Q_2$}\\
The mass $\mayo$ of the excited outgoing particle
from the first step is recalculated using
\begin{equation}
   \mayo = \myi + \mtarg - \mares - Q_1.
 \label{2-step-mayo}
\end{equation}
This is because the values on the right-hand side
of Eq.~(\ref{2-step-mayo}) are usually known to high accuracy.
In addition, the value of $Q_2$ is computed using
$$
  Q_2 = \mayo - \mbyo - \mbres.
$$

For the first step of the reaction, the Legendre
coefficients $c_\ell(E)$ for the expansion
Eq.~(\ref{cmLegendre}) of the probability
density $g( \muacm \mid E)$
for the outgoing excited particle are given as
in Section~\ref{Sec:2-bodyLegendreData}.

An example of the model-dependent portion of the input
file, Section~\ref{model-info}, is as follows.\\
 \Input{Target's mass:  2.808921000497e+03}{}\\
 \Input{Projectile's mass:  1.876124078321e+03}{}\\
 \Input{First residual's mass: 939.565413016980301}{}\\
 \Input{First product's mass: 3747.70426580102}{}\\
  \Input{First step's Q value: -2.2246}{}\\
 \Input{Second product's mass: 938.782992507523659}{}\\
 \Input{Second residual's mass:  2.808921000497e+03}{}\\
 \Input{Second step's Q value: 0.0002728}{}\\
\vskip 5pt
 \Input{Legendre coefficients: n = 30}{}\\
 \Input{Interpolation: lin-lin}{}\\
 \Input{ Ein: 3.71:  n = 3}{}\\
  \Input{ \indent 1.0}{}\\
  \Input{ \indent 0.0}{}\\
  \Input{ \indent 0.0}{}\\
 \Input{ Ein: 3.9:  n = 3}{}\\
  \Input{ \indent 1.0}{}\\
  \Input{ \indent -0.14079}{}\\
  \Input{ \indent 0.026804}{}\\
    \Input{ $\cdots$}{}\\
  \Input{ Ein: 10.0:  n = 3}{}\\
  \Input{ \indent 1.0}{}\\
  \Input{ \indent 0.24417}{}\\
  \Input{ \indent 0.011232}{}\\
}


\documentclass[10pt]{article}

\usepackage[margin=1in]{geometry}

\newcommand{\exprlib}{\texttt{ExprLib}}
\newcommand{\code}[1]{\texttt{#1}}

\usepackage{parskip}

\usepackage{amssymb,amsmath,mathtools,nicefrac,esint,graphicx,microtype,subcaption,url,mhchem,hyperref}
\usepackage[mathscr]{euscript}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage[super]{nth}

\newcommand{\dder}[2]{\frac{\mathrm{d} #1}{\mathrm{d} #2}} % total derivative
\newcommand{\idder}[2]{\mathrm{d} #1/\mathrm{d} #2} % total derivative
\renewcommand{\v}[1]{\boldsymbol{#1}} % vector
\newcommand{\m}[1]{\left[\mathrm{#1}\right]} % matrix, upright with square brackets
\newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}} % partial derivative
\newcommand{\ipder}[2]{\partial #1/\partial #2} % inline partial derivative
\newcommand{\tr}{\intercal} % transpose
\newcommand{\const}[1]{\Big{|}_{#1}} % stuff that is held constant in a partial derivative

\usepackage{listings}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
backgroundcolor=\color{lbcolor},
    tabsize=2,
%   rulecolor=,
    language=[GNU]C++,
        basicstyle=\scriptsize,
        upquote=true,
        aboveskip={1.5\baselineskip},
        columns=fixed,
        showstringspaces=false,
        extendedchars=false,
        breaklines=true,
        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        frame=single,
        numbers=left,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        identifierstyle=\ttfamily,
        keywordstyle=\color[rgb]{0,0,1},
        commentstyle=\color[rgb]{0.026,0.112,0.095},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
        numberstyle=\color[rgb]{0.205, 0.142, 0.73},
%        \lstdefinestyle{C++}{language=C++,style=numbers}?.
}
\lstset{
    backgroundcolor=\color{lbcolor},
    tabsize=4,
  language=C++,
  captionpos=b,
  tabsize=3,
  frame=lines,
  numbers=left,
  numberstyle=\tiny,
  numbersep=5pt,
  breaklines=true,
  showstringspaces=false,
  basicstyle=\footnotesize,
%  identifierstyle=\color{magenta},
  keywordstyle=\color[rgb]{0,0,1},
  commentstyle=\color{DarkGreen},
  stringstyle=\color{red}
  }

\title{An \exprlib{} Example: a Lotka-Volterra Model of a Predator and Two Competing Prey Species}
\author{Mike Hansen}
\date{}

\begin{document}

\maketitle

This document demonstrates how to solve a system of ordinary differential equations (ODEs) with the block implicit dual time integration capability in the Expression Library, or \exprlib{}.
The ODEs considered here are a highly nonlinear model from ecology, of two competing prey species and a predator species.
We first discuss the system of ODEs, then the block-implicit dual time formulation and corresponding \exprlib{} code, and finally we demonstrate some results found with the python post-processing utility provided by the Expression Library.

\section{ODEs to Solve}
\subsection{Lotka-Volterra Model}
The Lotka-Volterra models of predator-prey and competitive non-predatory species are a simple model of complex ecological phenomena.
The equations are nonlinear and can present chaotic behavior.
Here we combine two non-predatory (prey) species that compete for resources with a predator species.
There can be selectivities in both the prey competition and predation model, which might be imagined as one prey species being `bulky' while the other is `evasive.'
The bulky species can out-produce and out-consume the evasive species and will dominate the landscape in absence of predation.
But the bulky species is harmed more by predation, and provides for a greater predator population.
If predation is too great, the evasive prey species will dominate, but the bulky prey species will dominate if predation is insufficient.

The governing equations for the prey species populations, $x_1$ and $x_2$, and predator population $y$ are
\begin{equation}
	\begin{aligned}
		\dder{x_1}{t} &= r_1x_1\left(1 - \frac{x_1+\alpha_{12}x_2}{K_1}\right) - \beta_1x_1y, \\
		\dder{x_2}{t} &= r_2x_2\left(1 - \frac{x_2+\alpha_{21}x_1}{K_2}\right) - \beta_2x_2y, \\
		\dder{y}{t}   &= (\eta_1x_1 + \eta_2x_2)y - \delta y.
	\end{aligned}
	\label{eqn: lv3}
\end{equation}
In the above expressions, $r_1,r_2>0$ are the intrinsic rates of growth of the prey species, while $K_1,K_2>0$ are the carrying capacities.
If $K_i\to\infty$ and there is no predation, then species $i$ would experience exponential growth, as its governing equation would reduce to $\idder{x_i}{t}=r_ix_i$ which admits the unstable solution of $x_i(t)=x_i(0)\exp(r_it)$.
If the carrying capacity is finite and there is no predation or inter-species competition, then each prey species will simply grow in logistic manner to its carrying capacity - thus $K_i$ is the maximum population of species $i$ in absence of competition or predation.

The $\alpha_{12}$ and $\alpha_{21}$ terms give rise to competition between prey for finite resources.
Observe that $x_i/K_i$ - the term that limits growth beyond $K_1$ - is replaced by $(x_i+\alpha_{ij}x_j)/K_i$.
This shows that $\alpha_{12}>0$ essentially limits the carrying capacity for species $1$ due to the consumption of resources by species $2$.
If $\alpha_{12}=\alpha_{21}=1$ then the species compete equally.
However if $\alpha_{12} = 4 = 1/\alpha_{21}$, for instance, then species $2$ effectively consumes four times the resources of species $1$.

The $\beta_1,\beta_2>0$ terms with $x_iy$ products represent loss of prey species due to predation.
The $\eta_1,\eta_2>0$ terms in the predator population equation are the gains in predator species due to predation.
Finally $\delta>0$ is the intrinsic rate of \emph{death} of predators in absence of prey.
Thus if there is no predation, the predator species goes extinct as $y\to0$ because $\idder{y}{t}=-\delta y$ has the stable solution $y(t)=y(0)\exp(-\delta t)$.

\subsection{Parameter Choices}
To choose parameters, we have two issues.
First, how to represent a `bulky' prey species and an `evasive' prey species with the given parameters in \eqref{eqn: lv3}.
And second, finding actual numbers that give interesting results.
Numbers were found by trial and error.

The distinction between the bulky and evasive prey species are as follows.
Species $1$ will be evasive, and $2$ bulky.
\begin{itemize}
	\item The evasive species has a lower intrinsic growth rate: $r_1<r_2$.
	\item The bulky species consumes more resources: $\alpha_{12}>1$, $\alpha_{21}=1/\alpha{12}<1$.
	\item Predation is more impactful on the bulky species: $\beta_2>\beta_1$, $\eta_2>\eta_1$.
	\item The evasive species has a higher carrying capacity: $K_1>K_2$.
\end{itemize}

As for exact numbers, the example parameter set and initial populations below leads to a stable equilibrium with nonzero populations of all three species.
\begin{equation}
	\text{stable nonzero equilibrium: }
	\begin{cases}
		& x_1(0) = x_2(0) = 100, y(0) = 10     , \\
		& r_1 = 1.0, K_1 = 200, \beta_1 = 0.01 , \\
		& r_2 = 4.0, K_2 = 100, \beta_2 = 0.1  , \\
		& \alpha_{12} = 1/\alpha_{21} = 4      , \\
		& \eta_1 = 0.01, \eta_2 = 0.2, \delta = 6.
	\end{cases}
\end{equation}

Another parameter set leads to extinction of the evasive species and stable, nonzero populations of the bulky prey and predator.
The only differences are the lesser impacts of predation on bulky species, with $\beta_2=0.05$ instead of $0.1$ and $\eta_2=0.1$ instead of $0.2$, in addition to the lower carrying capacity of evasive species, at $K_1=170$ instead of $200$.
\begin{equation}
	\text{extinction of evasive prey: }
	\begin{cases}
		& x_1(0) = x_2(0) = 100, y(0) = 10     , \\
		& r_1 = 1.0, K_1 = 170, \beta_1 = 0.01 , \\
		& r_2 = 4.0, K_2 = 100, \beta_2 = 0.05  , \\
		& \alpha_{12} = 1/\alpha_{21} = 4      , \\
		& \eta_1 = 0.01, \eta_2 = 0.1, \delta = 6.
	\end{cases}
\end{equation}

\subsection{Vector Form \& Jacobian Matrix}
Before solving a system of ODEs in \exprlib{} with the block-implicit method, it is wise to write the equations in vector form and understand the Jacobian matrix.
Given the solution vector $\v{U}(t)=[x_1(t),x_2(t),y(t)]^\tr$ with corresponding RHS vector $\v{R}$, the vector form of \eqref{eqn: lv3} is
\begin{equation}
	\dder{\v{U}}{t} = \v{R}(\v{U}) \quad\leftrightarrow\quad
	\dder{}{t}
	\begin{pmatrix}
		x_1 \\
		x_2 \\
		y
	\end{pmatrix}
	=
	\begin{pmatrix}
		R_1 \\
		R_2 \\
		R_3
	\end{pmatrix}
	=
	\begin{pmatrix}
		r_1x_1\left(1 - K_1^{-1}\left(x_1+\alpha_{12}x_2\right)\right) - \beta_1x_1y \\
		r_2x_2\left(1 - K_2^{-1}\left(x_2+\alpha_{21}x_1\right)\right) - \beta_2x_2y \\
		(\eta_1x_1 + \eta_2x_2)y - \delta y
	\end{pmatrix}.
\end{equation}

The Jacobian matrix is made up of partial derivatives of $\v{R}$ to $\v{U}$:
\begin{equation}
	\m{J} = \pder{\v{R}}{\v{U}} =
	\begin{bmatrix}
		\pder{R_1}{U_1}\const{u_2,u_3} & \pder{R_1}{U_2}\const{U_1,U_3} & \pder{R_1}{U_3}\const{U_1,U_2} \\
		\pder{R_2}{U_1}\const{u_2,u_3} & \pder{R_2}{U_2}\const{U_1,U_3} & \pder{R_2}{U_3}\const{U_1,U_2} \\
		\pder{R_3}{U_1}\const{u_2,u_3} & \pder{R_3}{U_2}\const{U_1,U_3} & \pder{R_3}{U_3}\const{U_1,U_2}
	\end{bmatrix}.
\end{equation}

For Equation \eqref{eqn: lv3} the derivatives with respect to a generic variable $\phi$ are as follows.
For \exprlib{} it is best to compute derivative with respect to a generic variable such as this, because it allows sensitivities to be propagated through the graph with the chain rule.
\begin{equation}
	\pder{R_1}{\phi} = r_1\left[\pder{x_1}{\phi}\left(1 - 2K_1^{-1}x_1\right) - \alpha_{12}K_1^{-1}\left(x_1\pder{x_2}{\phi}+x_2\pder{x_1}{\phi}\right)\right] - \beta_1\left(x_1\pder{y}{\phi}+y\pder{x_1}{\phi}\right),
	\label{eqn: dR1dphi}
\end{equation}
\begin{equation}
	\pder{R_2}{\phi} = r_2\left[\pder{x_2}{\phi}\left(1 - 2K_2^{-1}x_2\right) - \alpha_{21}K_2^{-1}\left(x_1\pder{x_2}{\phi}+x_2\pder{x_1}{\phi}\right)\right] - \beta_2\left(x_2\pder{y}{\phi}+y\pder{x_2}{\phi}\right),
	\label{eqn: dR2dphi}
\end{equation}
\begin{equation}
	\pder{R_3}{\phi} = \eta_1\left(x_1\pder{y}{\phi}+y\pder{x_1}{\phi}\right) + \eta_2\left(x_2\pder{y}{\phi}+y\pder{x_2}{\phi}\right) - \delta\pder{y}{\phi}.
	\label{eqn: dR3dphi}
\end{equation}

\section{Solving in \exprlib{}}

\subsection{RHS Expression and Sensitivities}
First we have to write an expression which computes the right-hand side (RHS) of the ODEs.
This is done in the \code{RhsThreeSpecies.h} file.
The \code{evaluate()} method is straightforward, and the \code{sensitivities(Tag)} method simply implements \eqref{eqn: dR1dphi}-\eqref{eqn: dR3dphi}.

\subsection{Initial Condition Setup}
To set up the initial conditions we use an \code{ExpressionTree} object.
In this case it is quite simple, as we make a tree and factory, populating the tree with constant fields with values of $x_1(0)$, $x_2(0)$, and $y(0)$.
\begin{lstlisting}
 Expr::ExpressionFactory icFactory;
 Expr::ExpressionTree icTree( icFactory, patch.id(), "tree: initial conditions" );

 typedef Expr::ConstantExpr<FieldT>::Builder ConstantT;
 icTree.insert_tree( icFactory.register_expression( new ConstantT( prey1Tag, prey1 ) ) );
 icTree.insert_tree( icFactory.register_expression( new ConstantT( prey2Tag, prey2 ) ) );
 icTree.insert_tree( icFactory.register_expression( new ConstantT( predTag, pred ) ) );
\end{lstlisting}

From here we simply execute the tree and write it to a GraphViz file for inspection if necessary.
\begin{lstlisting}
 icTree.register_fields( fml );
 icTree.bind_fields( fml );
 fml.allocate_fields( patch.field_info() );
 icTree.execute_tree();
 {
  std::ofstream f( "lotka-volterra-tree--initial-conditions.dot" );
  icTree.write_tree( f );
 }
\end{lstlisting}

\subsection{Integrator Setup}
To set up a dual time integrator you must build tags for the physical time step ($\Delta t$) and the dual time step ($\Delta\sigma$), and choose an order of accuracy in physical time.
Below we use a second order solution.
\begin{lstlisting}
 const Expr::Tag dtTag( "time step"     , Expr::STATE_NONE );
 const Expr::Tag dsTag( "dual time step", Expr::STATE_NONE );

 const int bdfOrder = 2;

 typedef Expr::DualTime::BlockImplicitBDFDualTimeIntegrator<FieldT> IntegratorT;
 boost::shared_ptr<IntegratorT> integrator = boost::make_shared<IntegratorT>(patch.id(),
                                                                             "integrator",
                                                                             dtTag,
                                                                             dsTag,
                                                                             bdfOrder );
\end{lstlisting}

Next we set the time step expressions to constants:
\begin{lstlisting}
 typedef Expr::ConstantExpr<so::SingleValueField>::Builder ConstantSingleValueT;
 integrator->set_physical_time_step_expression(new ConstantSingleValueT(dtTag, dtValue ));
 integrator->set_dual_time_step_expression    (new ConstantSingleValueT(dsTag, dsValue ));
\end{lstlisting}

\subsubsection{Building RHS Expressions}
To build the RHS expressions and any intermediates required to compute the $R_i$, we pull a factory off the integrator and register the expressions to it.
In this case we only have and RHS expression, but any other intermediates used to compute the RHS would be registered here.
\begin{lstlisting}
 typedef lv3::Rhs<FieldT>::Builder RhsT;
 Expr::ExpressionFactory& factory = integrator->factory();
 factory.register_expression( new RhsT( params, tagsE ) );
\end{lstlisting}

\subsubsection{Adding Variables}
Next we add the variables/equations to the integrator with the \code{add\_variable(\ldots)} method.
The first argument is the name of the integrated variable, the second is the RHS tag for the equation, and the final arguments are the indices of the variable and the RHS in the Jacobian matrix.
Any combination of indices may be specified so long as no two variable or RHS indices are shared (one index per variable, one per RHS) and that no index greater than $(\text{\# of equations}-1)$ is provided.
After adding all of the variables, the \code{done\_adding\_variables()} method must be called.
\begin{lstlisting}
 integrator->add_variable<FieldT>( tagsE.prey1Name   , tagsE.prey1RhsTag   , 0, 0 );
 integrator->add_variable<FieldT>( tagsE.prey2Name   , tagsE.prey2RhsTag   , 1, 1 );
 integrator->add_variable<FieldT>( tagsE.predatorName, tagsE.predatorRhsTag, 2, 2 );
 integrator->done_adding_variables();
\end{lstlisting}

\subsubsection{Jacobian Matrix}
In this case the Jacobian is formed directly through the sensitivities of the RHS expressions to the integrated solution variables.
\emph{After} we are done adding variables to the integrator, the Jacobian matrix is formed and specified.
The ASSEMBLER macro is used to declare the \code{jac} variable (pointer) to a dense matrix with elements of type \code{FieldT}.
After declaration, the elements are specified through the \code{Expr::matrix::sensitivity} method.
The \code{finalize()} method must be called on \code{jac}, and then \code{set\_jacobian(\ldots)} is called on the integrator.

A C++ lambda is used to make the Jacobian matrix assembler.
This is called each time we need to provide the Jacobian to a function such as \code{set\_jacobian} or an expression builder.
If a copy is not needed, then the lambda (namely the \code{auto jacobian = \ldots} and curly braces used to scope the lambda) may be discarded and the Jacobian itself may be provided to \code{set\_jacobian(jac)}.

\begin{lstlisting}
 auto jacobian = [&]()
 {
  ASSEMBLER( Expr::matrix::DenseSubMatrix, FieldT, jac )

  jac->element<FieldT>(0,0) = sensitivity( tagsE.prey1RhsTag   , tagsE.prey1StateTag    );
  jac->element<FieldT>(0,1) = sensitivity( tagsE.prey1RhsTag   , tagsE.prey2StateTag    );
  jac->element<FieldT>(0,2) = sensitivity( tagsE.prey1RhsTag   , tagsE.predatorStateTag );

  jac->element<FieldT>(1,0) = sensitivity( tagsE.prey2RhsTag   , tagsE.prey1StateTag    );
  jac->element<FieldT>(1,1) = sensitivity( tagsE.prey2RhsTag   , tagsE.prey2StateTag    );
  jac->element<FieldT>(1,2) = sensitivity( tagsE.prey2RhsTag   , tagsE.predatorStateTag );

  jac->element<FieldT>(2,0) = sensitivity( tagsE.predatorRhsTag, tagsE.prey1StateTag    );
  jac->element<FieldT>(2,1) = sensitivity( tagsE.predatorRhsTag, tagsE.prey2StateTag    );
  jac->element<FieldT>(2,2) = sensitivity( tagsE.predatorRhsTag, tagsE.predatorStateTag );

  jac->finalize();

  return jac;
 };
\end{lstlisting}

\subsubsection{Processing Extra Expressions}
To add an expression to the graph that is not needed to compute the RHS, for instance to visualize some complex function of the state variables, we need a special procedure of registering the expression.
In this case we are post processing eigenvalues of the Jacobian matrix, so we do the following.
\begin{lstlisting}
 typedef Expr::EigenvalueExpr<FieldT>::Builder EigT;
 integrator->get_tree().insert_tree(factory.register_expression(new EigT(...)));
\end{lstlisting}

\subsubsection{Finalizing and Running the Integration}
Once all expressions are registered and the Jacobian is set, we call the \code{prepare\_for\_integration(\ldots)} method on the integrator and execute the time and dual time loops as seen in the main executable.

\subsection{Data Output}
To write data to disc we first set up an output database and register fields to it for output.
\begin{lstlisting}
 Expr::FieldOutputDatabase db( fml, "lotka-volterra-output", true );

 db.request_field_output<FieldT>( tagsN.prey1StateTag    );
 db.request_field_output<FieldT>( tagsN.prey2StateTag    );
 db.request_field_output<FieldT>( tagsN.predatorStateTag );
 db.request_field_output<FieldT>( tagsE.eigenvalueTags[0] );
 db.request_field_output<FieldT>( tagsE.eigenvalueTags[1] );
 db.request_field_output<FieldT>( tagsE.eigenvalueTags[2] );
\end{lstlisting}

Then to write these fields at every physical time step we simply do the following, where \code{stepCount} is incremented every physical time step.
\begin{lstlisting}
 db.write_database( "step-" + boost::lexical_cast<std::string>( stepCount ) );
\end{lstlisting}

\subsection{Running the Executable}
When compiling \exprlib{}, be sure to set the cmake variable \texttt{ENABLE\_OUTPUT=ON}.
Compilation will builds an executable named \texttt{lotkavolterra} in the \code{\ldots/build/test/dualtime/ecology-example/} directory, in addition to the python scripts for post-processing results discussed below.
To run this executable and solve the system with default parameters, simply enter \code{./lotkavolterra}.
To see the command line options (the ODE parameters) enter \code{./lotkavolterra --help}.
This shows how one could change any number of the parameters, for example with \code{./lotkavolterra --r1=2 --K2=50}.

\section{Plotting the Results with Python}
The \code{DataLoader.py} file provides functions to load fields written by \exprlib{}, and in the \code{driver.py} file these are used to load data into vectors of the solution variables and eigenvalues over time.
Example plots of each species' populations and real parts of the eigenvalues of the Jacobian matrix are below.
\begin{figure}[htbp]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{figures/nonzero-equilibria.png}
		\caption{Populations and eigenvalues (real part only) of the Jacobian over time for the case in which all three species find nonzero equilibrium populations.}
	\end{center}
\end{figure}
\begin{figure}[htbp]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{figures/extinct-evasive.png}
		\caption{Populations and eigenvalues (real part only) of the Jacobian over time for the case in which evasive prey go extinct.}
	\end{center}
\end{figure}

\begin{figure}[htbp]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{figures/nonzero-spectrum.png}
		\caption{Eigenvalues trajectories on the complex plane for the case in which all three species find nonzero equilibrium populations.}
	\end{center}
\end{figure}
\begin{figure}[htbp]
	\begin{center}
		\includegraphics[width=0.8\textwidth]{figures/extinct-evasive-spectrum.png}
		\caption{Eigenvalues trajectories on the complex plane for the case in which evasive prey go extinct. Triangles indicate the initial condition and squares indicate the eigenvalues at the final time - the trajectories proceed in time \emph{from} the triangles \emph{to} the squares.}
	\end{center}
\end{figure}

\clearpage

\section{Debugging Matrix Assembly}
In developing implicit solution methods the Jacobian matrix is a common source of errors.
The \code{C++} file called \code{lotkaVolterraThreeSpeciesWithMatrixDebugging.cpp} demonstrates how a \code{MatrixExpression} can be used to debug matrix assembly in \code{ExprLib}.
This example only runs several time steps but it shows how to print Jacobian matrix values as time integration proceeds.
Note that (as described in the file) this requires a \code{DEBUG} build with threading off and \code{CUDA} off.

\end{document}


\chapter{Scalar Field Theory}

In this chapter, we study the interacting scalar field theory
\begin{equation}
\begin{aligned}
	\mathcal L &= \mathcal L_0 + \mathcal L_{\mathrm{int}}, \\
	\mathcal L_0 &=\frac{1}{2}\partial^\mu \phi \partial_\mu \phi -\frac{m^2}{2}\phi^2, \\
	\mathcal L_{\mathrm{int}} &= -\frac{g}{4!}\phi^4.
\end{aligned}
\end{equation}
As we have discussed, because of the interaction, the field and coefficients will be renormalized:
\begin{equation}
\begin{aligned}
	\phi &= \sqrt{Z_\phi} \phi_R, \\
	m &= \sqrt{Z_m} m_R, \\
	g &= Z_g g_R.
\end{aligned}
\end{equation}
The renormalized Lagrangian becomes:
\begin{equation}
	\mathcal{L}
	= Z_\phi \frac{1}{2} (\square\phi_R)^2 - Z_m Z_\phi \frac{m_R^2}{2} \phi_R^2 - Z_g Z_\phi^2 \frac{g_R}{4!}\phi_R^4.
\end{equation}
We can formally divided the renormalized theory into three parts: free theory part, interaction part, and the counter terms:
\begin{equation}
\begin{aligned}
	\mathcal L &= \mathcal L_0 + \mathcal L_{\mathrm{int}} + \mathcal L_{\mathrm{ct}}, \\
	\mathcal L_0 &= \frac{1}{2} (\square\phi_R)^2 - \frac{ m_R^2}{2} \phi_R^2, \\
	\mathcal L_{\mathrm{int}} &= -\frac{g_R}{4}\phi_R^4, \\
	\mathcal L_{\mathrm{ct}} &= A \frac{1}{2} (\square\phi_R)^2 - B\frac{ m_R^2}{2} \phi_R^2 - C \frac{g_R}{4!} \phi_R^4,
\end{aligned}
\end{equation}
where the coefficients in the counter terms are:
\begin{equation}
	A = Z_\phi-1, \quad
	B = Z_m Z_\phi - 1, \quad
	C = Z_g Z_\phi^2 - 1.
\end{equation}
The counter terms come from the renormalization factors, which can be formally infinity but are perturbatively of order $O(g)$, and thus are regarded as additional perturbations to the free theory.

\section{Perturbative Renormalization}

\subsection{First-order Correction to the Propagator}
Consider the first order perturbation to the propagator:
\begin{equation}
\begin{aligned}
	i \Delta^{(1)}(k)
	&= \includegraphics[align=c, width=0.4\linewidth]{pics/KG-3.png} \\
	&= i\Delta_0(k) \left[-\frac{g}{2}\int \frac{d^4 q}{(2\pi)^4} \frac{1}{q^2-m_R^2} + i(Ak^2-Bm_R^2) \right] i\Delta_0(k).
\end{aligned}
\end{equation}
Note that in the loop diagram, there is in total ($4 \times 3 = 12$) identical diagrams, and so gives the coefficients.
A simpler counter rule is that if the diagram has no symmetry, there is in total $4!$ identical diagrams and the denominator cancels out exactly, while for any remaining symmetries, the symmetry factor will remain in the denominator.
For the loop diagram we considered here, the symmetry factor is $2$.

Note that the second order correction to the Green's function has one ``ingoing'' leg and one ``outgoing'' leg, the core (or the amputated Green's function) in this case is called the \textit{self-energy} $i\Sigma(k^2)$:
\begin{equation}
	i\Sigma(k^2) = -\frac{g}{2}\int \frac{d^4 q}{(2\pi)^4} \frac{1}{q^4-m_R^2} + i(Ak^2-Bm_R^2).
\end{equation}
The integral is divergent, that when the counter terms come to rescue.
The divergent part of the integral can be absorbed into the coefficients, or be canceled by the counter terms.
In the following, we will see that the divergent integral
\begin{equation}
	I \equiv -\frac{g}{2} \int \frac{d^4 q}{(2\pi)^4} \frac{1}{q^4-m_R^2}
\end{equation}
can be regularized.
The regularization is typically controlled by a parameter which will recover the infinity when taking a specific limit.
The physical observable shall not depend explicitly on the regularization parameter, so the final result is free from divergent even if we take the limit.

\subsubsection{Regularization of the Divergent Integral}
Consider the integral
\begin{equation}
	I = -\frac{g}{2} \int\frac{d \omega}{2\pi} \int\frac{d^3 q}{(2\pi)^3} \frac{1}{\omega^2 - \bm q^2-m_R^2 -i\epsilon}
\end{equation}
Since the singularity locates at $\pm(\sqrt{\bm q^2+m_R^2}-i\epsilon)$, we can analytically change the integral of $\omega$ from real axis to the imaginary axis anti-clock-wisely, the result is equivalent to the substitution $\omega \rightarrow i\omega$.
The new integral is defined on the 4D Euclidean space:
\begin{equation}
\begin{aligned}
	I &= i \frac{g}{2} \int \frac{d^4 q}{(2\pi)^4} \frac{1}{q^2 + m_R^2} \\
	&= i\frac{g}{2} \frac{\Omega_4}{(2\pi)^4} \int_0^\infty dq \ \frac{q^3}{q^2 + m^2_R},
\end{aligned}
\end{equation}
where
\begin{equation}
	\Omega_d = \frac{2 \pi^{\frac{d}{2}}}{\Gamma\left(\frac{d}{2}\right)}
\end{equation}
is the $d$-dimensional spherical area.

Till now the integral is essentially the same and thus still divergent.
Now we are going to regularize the expression.
One most frequently used regularization scheme is the \textit{dimensional regularization}.
It take note of the fact that the divergence of the integral only happens at integer dimension.
When we put the field theory to ($d=4-\varepsilon$)-dimensional space, the integral becomes:
\begin{equation}
	I_\varepsilon = i\frac{g\tilde{\mu}^\varepsilon}{2}\frac{\Omega_{4-\varepsilon}}{(2\pi)^{4-\varepsilon}} \int_0^\infty dq\ \frac{q^{3-\varepsilon}}{q^2+m_R^2}.
\end{equation}
Note that we have introduced a mass scale $\tilde\mu$ to get the correct dimensionality.
The integral is now convergent.
The specific form the the integral is not important, but we are concerned about the expansion near $\varepsilon = 0$.
All the computation here can be done automatically (in \texttt{Mathematica}), and the result is
\begin{equation}
\begin{aligned}
	I_\varepsilon &= i\frac{g m_R^2}{32\pi^2} \left[\frac{2}{\varepsilon}+1+\log \left(\frac{4 \pi \tilde{\mu}^2 e^{-\gamma_E}}{m_R^2}\right)\right] + O(\varepsilon) \\
	&\equiv i\frac{g m_R^2}{32\pi^2} \left[\frac{2}{\varepsilon}+1+\log \left(\frac{\mu^2}{m_R^2}\right)\right] + O(\varepsilon),
\end{aligned}
\end{equation}
where $\gamma_E$ is the Euler constant.
We have seen that the integral is controlled by the parameter $\varepsilon$.
In the $\varepsilon \rightarrow 0$ limit, the integral is divergent.

\subsubsection{Renormalization Using the Counter Terms}
We are now going to renormalize the theory.
Note that in the self-energy definition
\begin{equation}
	\Sigma(k^2) = \frac{1}{i} I + A k^2-B^2 m_R^2,
\end{equation}
we have the freedom to choose the counter terms that cancel the infinity.
To the first order, the coefficients can be
\begin{equation}
	A = O(g^2), \quad
	B = \frac{g}{16\pi^2 \varepsilon} + O(g^2).
\end{equation}
The result is
\begin{equation}
	\Sigma(k^2) = \frac{g m_R^2}{16\pi^2} \log \left(\frac{\mu}{m_R}\right)
	+\frac{g m_R^2}{32\pi^2}+O(\varepsilon).
\end{equation}
The one-loop correction also leads to a infinite \textit{Dyson series}:
\begin{equation}
\begin{aligned}
	i\Delta(k) &= i\Delta_0(k) + i\Delta_0(k)\sum_{n=1}^\infty \left[i\Sigma(k^2)i\Delta_0(k)\right] \\
	&= \frac{i}{k^2 -m_R^2 + \Sigma(k^2)}.
\end{aligned}
\end{equation}

\subsubsection{Physical Observables}
The physical observable here is the rest mass $m_0$, which is experimentally measurable.
It equals to the pole of the propagator, i.e.,
\begin{equation}
	m_R^2 - \Sigma(m_0^2) = m_0^2.
\end{equation}
Depending on the mass scale $\mu$ we choose, the renormalized mass $m_R$ may or may not equal to the rest mass.
Specifically, we can choose mass scale so that $m_R = m_0$, that is
\begin{equation}
	\Sigma(m_0^2) = 0 \quad \Longrightarrow \quad
	\mu = e^{-\frac{1}{2}} m_0.
\end{equation}
To the first order, the self-energy has no momentum dependence, so there is no physical prediction.

\subsection{Second-order Correction to the Vertex}

Now consider the second order correction to the vertex function, which is the connected, amputated 4-point Green's function.
The perturbation correspond to 3 different Feynman diagrams together with a vertex counter term.
The explicit form is:
\begin{equation}
\begin{aligned}
	i\Gamma_4(k_1,k_2,k_3,k_4)
	&= \includegraphics[align=c, width=0.6\linewidth]{pics/KG-4.png}\\
	&= \frac{g_R^2}{2} \left[iF(s)+iF(t)+iF(u)\right] -iCg_R,
\end{aligned}
\end{equation}
where we have introduced three momentum parameters $s$, $t$, and $u$:
\begin{equation}
	s = (k_1+k_2)^2,\quad
	t = (k_1+k_3)^2,\quad
	u = (k_1+k_4)^2.
\end{equation}
The loop integrals for three channels are the same, denoted by
\begin{equation}
	iF(k^2) = \int \frac{d^4 q}{(2\pi)^4} \Delta_0(q) \Delta_0(q+k).
\end{equation}
The integral is again divergent.

\subsubsection{Regularization}
The denominator in the integral
\begin{equation}
	iF(k^2) = \int \frac{d^4 q}{(2\pi)^4} \frac{i}{q^2-m_R^2} \frac{i}{(q+k)^2-m_R^2}
\end{equation}
involves multiplication of two polynomial.
The expression can be simplified by the \textit{Feynman parametrization}, which is essentially the identity:
\begin{equation}
	\frac{1}{A_{1} \ldots A_{n}}=\int d F_{n}\left(x_{1} A_{1}+\ldots+x_{n} A_{n}\right)^{-n},
\end{equation}
where the integration measure over the Feynman parameters $x_{i}$ is
\begin{equation}
	\int d F_{n}=(n-1) ! \int_{0}^{1} d x_{1} \ldots d x_{n} \delta\left(x_{1}+\ldots+x_{n}-1\right).
\end{equation}
This measure is normalized so that $\int d F_{n} =1$.
The simplest case is
\begin{equation}
	\frac{1}{A B}=\int_{0}^{1} \frac{dx}{[A+(B-A) x]^{2}}
	=\int_{0}^{1} \frac{\delta(x+y-1)}{[x A+y B]^{2}} dx dy.
\end{equation}
Other useful identities are
\begin{equation}
\begin{aligned}
	\frac{1}{A B^{n}} &=\int_{0}^{1} dxdy\frac{\delta(x+y-1)n y^{n-1}}{[x A+y B]^{n+1}} , \\
	\frac{1}{A B C} &=\int_{0}^{1} dxdydz \frac{2\delta(x+y+z-1)}{[x A+y B+z C]^{3}} .
\end{aligned}
\end{equation}

Using the Feynman parameters, the integral is
\begin{equation}
	iF(k^2) = \frac{i\Omega_4}{(2\pi)^4} \int_0^1 dx \int dq\ \frac{q^{3}}{\left[q^2+m_R^2+x(1-x)k^2\right]^2}.
\end{equation}
Using the dimensional regularization, the integral is
\begin{equation}
	iF_\varepsilon(k^2) = \frac{i\tilde{\mu}^{\varepsilon}\Omega_{4-\varepsilon}}{(2\pi)^{4-\varepsilon}} \int_0^1 dx \int dq\ \frac{q^{3-\varepsilon}}{\left[q^2+m_R^2+x(1-x)k^2\right]^2}.
\end{equation}
Then we carry out the calculation, the result is:
\begin{equation}
\begin{aligned}
	F_\varepsilon(s) &= \frac{1}{8\pi^2\varepsilon} + \frac{1}{16\pi^2}\int_0^1 dx \ln \left(\frac{4\pi \tilde{\mu}^2 e^{-\gamma_E}}{D_{s,x}}\right) \\
	&= \frac{1}{8\pi^2\varepsilon} +\frac{1}{8\pi^2}\ln \left(\frac{\mu}{m_R}\right) - \frac{1}{16\pi^2}\int_0^1 dx \ln\left(\frac{D_{s,x}}{m_R^2}\right),
\end{aligned}
\end{equation}
where we have denote
\begin{equation}
	D_{k^2,x} \equiv m_R^2+x(1-x)k^2.
\end{equation}
Now sum up the contribution from all channel, the result looks like:
\begin{equation}
	\Gamma^{(2)}_4 = \frac{3 g_R^2}{16\pi^2}\left[\frac{1}{\varepsilon} + \ln\left(\frac{\mu}{m_R}\right)\right] - \frac{g_R^2}{32\pi^2}\int_0^1 dx \ln\left(\frac{D_{s,x}D_{t,x}D_{u,x}}{m_R^6}\right)- C g_R.
\end{equation}

\subsubsection{Renormalization and Physical Observables}

To absorb the divergence, we can choose the counter term coefficient as
\begin{equation}
	C = \frac{3g_R}{16\pi^2}.
\end{equation}
So, to the second order, the vertex function is:
\begin{equation}
	\Gamma_4(k_1,k_2,k_3,k_4) = -g_R + \frac{g_R^2}{32\pi^2}\int_0^1 dx \ln\left(\frac{\mu^6}{D_{s,x}D_{t,x}D_{u,x}}\right).
\end{equation}
The vertex function is directly related to the physical observables.
It can be measured in the scattering experiment or just just by the repulsive force it generated.
We can choose the scale $\mu_0$ so that
\begin{equation}
	\Gamma(m_R,m_R,m_R,m_R) = -g_R.
\end{equation}
Then the corrected vertex function gives the physical predictions, for example on the scattering amplitude for different $k_i$'s.

\subsection{Renormalization Group}
Now consider the RG equation for the one-loop correction.
The bare parameters are:
\begin{equation}
	g_0 = Z_g g\tilde{\mu}^{\epsilon},\
	m_0 = Z_m^{1/2} m,
\end{equation}
The RG conditions are:
\begin{eqnarray}
	\frac{d g_0}{d\ln \mu}
	&=& \left(\frac{3}{16\pi^2 \epsilon} + \frac{1}{g}\right)\frac{dg}{d\ln \mu} + \epsilon = 0, \\
	\frac{d m_0}{d\ln \mu}
	&=& \frac{1}{32\pi^2 \epsilon}\frac{dg}{d\ln \mu} + \frac{1}{m}\frac{dm}{d \ln \mu} = 0.
\end{eqnarray}
Consider the series expansion of beta function:
\begin{equation}
	\beta(g) = \frac{dg}{d\ln \mu} = \beta_1 g + \beta_2 g^2 +O(g^3).
\end{equation}
The beta function is
\begin{equation}
	\beta(g) = -\epsilon g + \frac{3g^2}{16\pi^2} + O(g^3).
\end{equation}
The anomalous dimension of mass is
\begin{equation}
	\gamma_m = \frac{1}{m}\frac{dm}{d \ln \mu} = \frac{g}{32\pi^2}+O(g^2)
\end{equation}

\section{Wilsonian Renormalization Group}
Now we consider the two-loop correction to the self energy
\begin{equation}
\begin{aligned}
	i\Sigma^{(2)}(k)
	&= -\frac{g_R^2}{3!} \int \frac{d^4 p_1}{(2\pi)^4}\frac{d^4 p_2}{(2\pi)^4} \frac{i}{p_1^2-m_R^2} \frac{i}{p_2^2-m_R^2} \frac{i}{(k-p_1-p_2)^2-m_R^2} \\
	&= i\frac{g_R^2}{3!} \int \frac{d^4 p_1}{(2\pi)^4}\frac{d^4 p_2}{(2\pi)^4} \int_0^1 dx dy dz\  \frac{2 \delta(1-x-y-z) }{D_{x,y,z}^3},
\end{aligned}
\end{equation}
where the denominator is a bilinear
\begin{equation}
	D = \left(\begin{array}{cc} p_1 & p_2 \end{array} \right)
		\left[\begin{array}{cc} x+z & z \\ z & y+z \end{array} \right]
		\left(\begin{array}{c} p_1 \\ p_2 \end{array} \right)
		- 2kz (p_1 + p_2) + k^2z - m_R^2.
\end{equation}

For a generic bilinear, we can simplify it by shifting the variable and diagonalized the quadratic coefficient matrix (which is always symmetric):
\begin{equation}
\begin{aligned}
	D &= \sum_{ij} A_{ij} p_i p_j + \sum_i B_i p_i - C \\
	&= \sum_{ij} A_{ij}p'_i p'_j - C - \frac{1}{4} \sum_{ij} B_i A^{-1}_{ij} B_j \\
	&= \sum_n a_n p''_n p''_n - C',
\end{aligned}
\end{equation}
We do not really need to carry out the diagonalization explicitly, but the above general form tells tells us that the integral can be transformed to
\begin{equation}
\begin{aligned}
	\frac{1}{D_{x,y,z}^3}
	&\rightarrow \frac{1}{(\det A)^2} \frac{1}{\left[p_1^2 + p_2^2 - C'\right]^3} \\
	&= \frac{1}{[xy+yz+zx]^2} \frac{1}{\left[ p_1^2+p_2^2 - \left(-\frac{xyz}{xy+yz+zx}k^2+m_R^2\right)\right]^3}
\end{aligned}
\end{equation}
The self energy is then
\begin{equation}
	\Sigma^{(2)}(k) = -\frac{g_R^2}{3!} \int_0^1 \frac{dF_3}{[xy+(x+y)z]^2} I(p_1^2,p_2^2),
\end{equation}
where
\begin{equation}
	I_\varepsilon(p_1^2,p_2^2) = \frac{\tilde{\mu}^{2\varepsilon}\Omega_d^2}{(2\pi)^{2d}} \int dp_1 dp_2 \frac{p_1^{d-1} p_2^{d-2}}{(p_1^2+p_2^2+C')^3}
\end{equation}

\subsection{Regularization and Renormalization}
The $\varepsilon$ expansion gives:
\begin{equation}
	I_\varepsilon(p_1^2,p_2^2) = -\frac{C'}{512 \pi^4 \varepsilon} + \text{finite part}.
\end{equation}
Back to the
\begin{equation}
	\frac{g_R^2}{3\cdot 2^{10} \pi^4 \varepsilon} \int_0^1 dF_3\ \frac{xyz}{(xy+yz+zx)^3} k^2
\end{equation}

\section{Spontaneously Symmetry Breaking}


In this chapter, we use the following steady-state case of single-phase porous media flow to
illustrate a popular method of model order reduction, namely, the proper orthogonal decomposition
method.
\section{MOR for steady-state problem}
\subsection{Model order reduction by POD}
\begin{itemize}
\item Model equations
\begin{equation} \label{single-phase}
\left\{
\begin{aligned}
- \nabla \cdot (\kappa \nabla p) & = 0  \qquad \text{in}~~ \Omega \\
\kappa \nabla p \cdot n &= f \qquad \text{on} ~~ \Gamma_\text{N} \\
p  &= 0 \qquad \text{on} ~~\Gamma_{\text{D}1} \\
p  &= 1 \qquad \text{on}~~ \Gamma_{\text{D}2}
\end{aligned}
\right.
\end{equation}
\begin{center}
\includegraphics[height=3.5cm]{figures/RBM/Darcy_domain.png}
\end{center}
\item In history matching, we need to solve the equations \eqref{single-phase} many times with different $\kappa$. Can we reuse the solution obtained in the process to reduce computational cost for the future?
\end{itemize}

\begin{figure}[!ht]
\begin{center}
\begin{tikzpicture}[xscale=1.2,yscale=1.2]
\tikzstyle{every node}=[font=\Large,scale=0.3]
\draw[-] (3,0) -- (5,0);
%\draw[-] (3,0.5) -- (5,0.5);
\draw[-] (3,1) -- (5,1);
%\draw[-] (3,1.5) -- (5,1.5);
\draw[-] (3,2) -- (5,2);

\draw[-] (3,0) -- (3,2);
%\draw[-] (3.5,0) -- (3.5,2);
\draw[-] (4,0) -- (4,2);
%\draw[-] (4.5,0) -- (4.5,2);
\draw[-] (5,0) -- (5,2);

\node at (4.00,1) {${\red {\tiny{(i,j)}}}$};
\node at (5.28,1) {${\red {\tiny{(i+1,j)}}}$};
\node at (5.18,2) {};%{${\red {0}}$}
\node at (5.18,0){};% {${\red {0}}$}
\node at (2.68,1) {${\red {\tiny{(i-1,j)}}}$};
\node at (2.78,2) {};%{${\red {0}}$}

\node at (2.78,0){};%{${\red {0}}$}

\node at (3.98, 2.1) {${\red {\tiny{(i,j+1)}}}$};
\node at (3.98, -0.1) {${\red {\tiny{(i,j-1)}}}$};

\fill(5,2) circle(0.8pt);
\fill(4,2) circle(0.8pt);
\fill(3,2) circle(0.8pt);
\fill(3,1) circle(0.8pt);
\fill(4,1) circle(0.8pt);
\fill(5,1) circle(0.8pt);
\fill(5,0) circle(0.8pt);
\fill(5,2) circle(0.8pt);
\fill(3,0) circle(0.8pt);
\fill(4,0) circle(0.8pt);
\end{tikzpicture}
\end{center}
\end{figure}

Five-point stencil finite difference scheme of the model \eqref{single-phase}
reads (if $\kappa \equiv 1$):
$$
\frac{4p_{i,j}-p_{i,j-1}-p_{i,j+1}-p_{i+1,j}-p_{i-1,j}}{h^2}=f_{i,j} \quad 1 \le i,j \le n;
$$
In practice, there are many details to look into, for example, complex geometry,
boundary conditions, general coefficient $\kappa$,  and mass conservation.
We can also use other discretization methods, like FVM, FEM, DG, and so on.

Let $d=n^2$ and $x_{\kappa}\in \mathbb R^d$ be the solution vector, i.e.,
$$
x_{\kappa, (i-1)n+j} := p_{i,j}, \qquad 1 \le i \le n,  1 \le j \le n.
$$
\begin{center}
\includegraphics[height=3.5cm]{figures/RBM/2Dto1Dordering.png}
\end{center}
In the end, we need to solve a linear system
\begin{equation}
A_{\kappa}x_{\kappa} = b_{\kappa},\quad  x_{\kappa}\in \mathbb{R}^d.
\end{equation}
We need to solve very large linear systems  and the computational cost is expensive.
Hence, we want to reuse the computed solutions to help obtaining solution for a new parameter.
The basic Idea is constructing a reduced model based on the available solutions of the original full model to get accurate approximations.

\paragraph{Obtain training data}
Pick $N$ samples $\kappa_\ell \in \mathcal{D}, \ell=1, 2, \ldots, N$, where $\mathcal{D}$ is a compact set for the parameters. For each $\kappa_\ell$, we solve for $x_\ell:=x_{\kappa_\ell}$ such that
$$
A_{\ell} \, x_\ell = b_{\ell}.
$$
Then we have
$$
X = \Big[ x_1,x_2, \ldots, x_N \Big] \in \mathbb{R}^{d\times N}.
$$
Denote $\bar{x} = \frac{1}{N}\sum_{\ell=1}^{N} x_{\ell}$ and
$$
\overline{X} = \Big[\bar{x},\bar{x}, \ldots, \bar{x} \Big] \in \mathbb{R}^{d\times N}.
$$
In practice, we may collect the already computed solutions as $X$.

\paragraph{POD: proper orthogonal decomposition}

\begin{itemize}\setlength{\itemsep}{10pt}
\item Eigen-decomposition: $$(X-\overline{X})(X-\overline{X})^T=W \Sigma W^T=\sum_{i=1}^d \sigma^2_i w_i w_i^T$$

where $W=[w_1,...,w_d]$, $W^TW=I$.
\item Eigenvalues of $(X-\overline{X})(X-\overline{X})^T$:
$$
\sigma_1^2 \ge \sigma_2^2 \ge \cdots \ge \sigma^2_{d'}\gg  \sigma^2_{d'+1}\ge \cdots\ge \sigma^2_d
$$
\item Obtain an orthogonal reduced basis
$$
\hat W=[w_1,w_2,\cdots,w_{d'}].
$$
\end{itemize}

\paragraph{Model reduction for steady-state problem}

\begin{itemize}\setlength{\itemsep}{10pt}

\item PCA: $y_\kappa=\hat{W}\hat{W}^T(x_\kappa-\bar{x})+\bar{x} = g \circ f \, (\hat x_{\kappa})$
\item Encoder $\hat{x}=f(x) = \hat{W}^T(x-\bar{x})$
\item Decoder $g(\hat{x})=W\hat{x}+\bar{x}$
\item Encode the original equation
$
f(A_\kappa x_\kappa)=f(b_\kappa) \Leftrightarrow \hat W^T A_\kappa x_\kappa=\hat W^Tb_\kappa
$
\item Approximate $x_\kappa\approx y_\kappa=\hat{W}\hat{W}^T(x_\kappa-\bar{x})+\bar{x}$
	$$\hat W^T A_\kappa y_\kappa=\hat W^Tb_\kappa,$$
	which is

		\begin{center}
		$
		\hat W^T A_\kappa \hat{W} (\hat{W}^T(x_\kappa-\bar{x}))=\hat W^T(b_\kappa - A_{\kappa}\bar{x})
		$
		\end{center}

\item Noting that $\hat{x_\kappa} = f(x_\kappa) = \hat{W}^T(x_\kappa-\bar{x})$. So we obtain that
		$$
		\hat W^T A_\kappa \hat{W} \hat{x_\kappa} =\hat W^T(b_\kappa - A_{\kappa}\bar{x})
		$$
	\end{itemize}

\paragraph{Workflow  of Model reduction for steady-state problem}

\begin{itemize}
\item Offline computation: obtain $\hat W$ and $\bar{x}$

\item Online computation: for a new parameter $\kappa \in \mathcal{D}$, solve the reduced model
\begin{equation}
\underbrace{\hat{W}^T A_\kappa \hat{W}}_{{\hat{A}_{\kappa}}} \hat{x_\kappa} =\underbrace{\hat W^T(b_\kappa - A_{\kappa}\bar{x})}_{\hat{b}_\kappa},
\end{equation}
namely
\begin{equation}
{\hat{A}_{\kappa}} \hat{x}_{\kappa} = \hat{b}_\kappa.
\end{equation}
Here $\hat{A}_{\kappa} \in  \mathbb{R}^{d'\times d'}$ so that the problem is reduced.
\item Apply the decoder to get the approximation solution in $x_{\kappa} \in \mathbb{R}^d$:
$$
x_{\kappa}= g(\hat{x}) = \hat W \,  \hat{x}_{\kappa} + \bar{x}.
$$
\end{itemize}

\subsection{Numerical results: steady-state case}
Consider the single-phase porous media flow:
\begin{equation}
\left\{
\begin{aligned}
- \nabla \cdot (\kappa \nabla p) & = 0  \qquad \text{in}~~ \Omega \\
\kappa \nabla p \cdot n &= f \qquad \text{on} ~~ \Gamma_\text{N} \\
p  &= 0 \qquad \text{on} ~~\Gamma_{\text{D}1} \\
p  &= 1 \qquad \text{on}~~ \Gamma_{\text{D}2}
\end{aligned}
\right.
\end{equation}
\begin{center}
\includegraphics[height=3.5cm]{figures/RBM/Darcy_domain.png}
\end{center}
By the POD, we only need three top singular values and basis functions.
	\begin{figure}[!htbp]
	\centering
	\includegraphics[width=.36\textwidth]{RBM/singularvalues_3_Darcy.eps}
	\includegraphics[width=.36\textwidth]{RBM/FirstB.png}\\
	\includegraphics[width=.36\textwidth]{RBM/SecondB.png}
	\includegraphics[width=.36\textwidth]{RBM/ThirdB.png}
	\caption{The test problem has three layers of permeability. The distribution of the largest 30 singular values and the basis functions corresponding to the top 3 singular values.}
\end{figure}

Next we approximate $p$ $(\kappa_1=1.55,\kappa_2=1.23,\kappa_3=0.48)$ by model order reduction (MOR).
\begin{figure}[!htbp]
	\centering
	\includegraphics[width=.32\textwidth]{RBM/ph_POD.png}
	\includegraphics[width=.32\textwidth]{RBM/ph_FEM.png}
	\includegraphics[width=.32\textwidth]{RBM/Error_3.png}
	\caption{Comparison of the solutions obtained by MOR and FEM.}
\end{figure}
The computational CPU time is compared as follows:
\begin{itemize}
	\item FEM: $4.56$s ($64 \times 64$ uniform grid)
	\item MOR online ($d'=3$): $0.0011$s; MOR offline: $327.8$s
\end{itemize}
The reduced model is obtained through offline computation and only one time computation is needed. Given a new $\kappa$, the computational time of using the reduced model is much less than it using FEM.

\paragraph{Efficiency of POD method}
\begin{center}
\includegraphics[width=0.75\textwidth]{RBM/PODtime.png}
\end{center}
From the above table, we have two observations:
\begin{itemize}
\item Offline step is usually costly
\item If POD works, it can save a lot of online time
\end{itemize}

\subsection{Summary of MOR}
The following figure summarizing the general ideas of MOR
\begin{center}
   \includegraphics[height=4.5cm]{figures/RBM/ROM_Ideas.png}
\end{center}
\begin{itemize}
\item DOF of the reduced model {\color{blue} $d' \ll d$} DOF of the full model
\item Offline computation (costly) can be done just once
\item Online computation (cheap) need to be repeated many times
\end{itemize}

Use POD Eigen-decompostion (or other methods such as greedy algorithm or CNN) to find $\hat{W}$, then do encoder and decoder as follows:

Encoder:
$$
A_{\kappa}x_{\kappa}=b_{\kappa}  \qquad \text{large system in $\mathbb{R}^d$}
$$
$$
\Downarrow f
$$
%
$$
{\hat{A}_{\kappa}} \hat{x}_{\kappa} = \hat{b}_\kappa \qquad \text{small system in $\mathbb{R}^{d'}$}
$$

Decoder:
$$
x_{\kappa}= g(\hat{x}_{\kappa}) = \hat W \,  \hat{x}_{\kappa} + \bar{x}.
$$

\section{MOR for time-dependent problem}
\subsection{Model order reduction by POD for time-dependent problem}
There are a lot of complex models in reservoir simulation. To simplify the presentation, we consider the single-phase porous media flow.
\begin{equation} \label{time-single}
\left\{
\begin{aligned}
\frac{\partial p}{\partial t} - \nabla \cdot (\kappa \nabla p) & = f, & &  [0,T] \times \Omega\\
\kappa \nabla p \cdot n & = 0, & &  [0,T] \times \Gamma_{\text{N}} \\
p & = p_\text{D}, & &  [0,T] \times \Gamma_\text{D} \\
p & = p_{0}, & & \{0\} \times \Omega
\end{aligned}
\right.
\end{equation}
We still FDM discretization for spatial dimension, then we have
\begin{equation}
\frac{\partial x(\kappa, t)}{\partial t}+A_{\kappa}x(\kappa,t)=b_{\kappa}(t).
\end{equation}

\paragraph{Obtain training data}
We generate data $X$ using all snapshots for all parameter samples:
\begin{itemize}
%
\item Pick sample parameters  $\kappa_\ell \in D, \ell=1, 2, \ldots, N$ as we have done for the steady state problem.
%
\item For each $\kappa_{\ell}$, obtain snapshots at time steps:
$$
t_m = m \Delta t,\quad  m =1, 2, \ldots, M:=\frac{T}{\Delta t}.
$$
%
\end{itemize}
For each $\kappa$, we solve
\begin{equation}
\frac{\partial x(\kappa, t)}{\partial t}+A_{\kappa} \, x(\kappa, t)=b_{\kappa}(t)
\end{equation}
using the backward Euler method
$$
\frac{x(\kappa, t_{m+1})-x(\kappa, t_m)}{\Delta t}+A_{\kappa}x(\kappa,t_{m+1})=b_{\kappa}(t_{m+1})
$$
to obtain
$$
x ^{m}(\kappa) :=x(\kappa,t_m), \quad m=1,2, \cdots, M \quad \Longrightarrow \quad X_{\kappa} = \Big[x^{1}(\kappa), \ldots, x^{M}(\kappa) \Big].
$$
Then we have
$$
X = \Big[ X_{\kappa_{1}}, \ldots, X_{\kappa_{N}} \Big]\in \mathbb{R}^{d\times NM}.
$$

\begin{center}
   \includegraphics[height=5cm]{figures/RBM/TrainingData.png}
\end{center}
We can now apply POD as in the steady-state case to
form $\hat W=[w_1, w_2, \ldots, w_{d'}]\in \mathbb{R}^{d\times d'}$ and we have $\hat W^T\hat W=I_{d'\times d'}$.

For each new parameter $\kappa\in D$, we only need to solve the reduced problem
\begin{equation}
\frac{\partial  \hat{x}(\kappa, t)}{\partial t}+ \hat{A}_\kappa \, \hat{x}(\kappa,t)  =\hat{b}_{\kappa}(t),
\end{equation}
by the backward Euler method with a new time step size $\Delta \hat t$ which maybe much bigger than $\Delta t$.
Namely
\begin{equation}
\frac{\hat{x}(\kappa, \hat t_{m+1})-\hat{x}(\kappa, \hat t_{m})}{\Delta \hat t}+\hat{A}_\kappa \, \hat{x}(\kappa, \hat t_{m+1})=\hat{b}_{\kappa}(\hat t_{m+1}).
\end{equation}
Then apply the decoder:
$$
\quad x(\kappa, \hat t_{m+1} )=\hat W \, \hat x(\kappa, \hat t_{m+1}) + \bar{x}.
$$

\subsection{Numerical results: time-dependent problem}
We still sample 8000 points for $\kappa$, namely, $N=8000$ and set $\Delta t = \frac{1}{256}$, namely, $M=256$, then form the training data $X \in \mathbb{R}^{d \times 2,048,000}$.
\begin{figure}[!htbp]
	\centering
	\includegraphics[width=.2\textwidth]{figures/POD/singularvalues_Time_Darcy.eps}
	\includegraphics[width=.2\textwidth]{figures/POD/1B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/2B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/3B.png}
	\\
	\includegraphics[width=.2\textwidth]{figures/POD/4B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/5B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/6B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/7B.png}
	\\
	\includegraphics[width=.2\textwidth]{figures/POD/8B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/9B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/10B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/11B.png}
	\\
	\includegraphics[width=.2\textwidth]{figures/POD/12B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/13B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/14B.png}
	\includegraphics[width=.2\textwidth]{figures/POD/15B.png}
	\caption{The basis functions corresponding to the top 15 singular values.}
\end{figure}

Then set $\Delta \hat t=\frac{1}{10}$ and approximate the solution $p$ when $\kappa_1=1.55,\kappa_2=1.23,\kappa_3=0.48$ by MOR.

\begin{center}
%\animategraphics[autoplay,loop,width=0.425\linewidth]{10}{figures/POD/rbm-time-}{0}{9}

\begin{figure}[!htbp]
	\includegraphics[width=.3\textwidth]{figures/POD/ph_POD_time.png}
	\includegraphics[width=.3\textwidth]{figures/POD/ph_FEM_time.png}
	\includegraphics[width=.3\textwidth]{figures/POD/Error_15.png}
	\caption{Comparison of the solutions obtained by MOR and FEM at $t=1$.}
\end{figure}

\end{center}

\subsection{More discussions on Model Order Reduction}
The process of MOR can summarized in the following figure:
 \begin{center}
     \includegraphics[height=7cm]{figures/RBM/RBM-methods.png}
 \end{center}
\begin{remark}
If number of parameters is limited, MOR is extremely powerful tool.  We find RBM is useful in many other applications, like FSI  and so on. In fact,  we can apply a greedy algorithm to further reduce offline cost.  For nonlinear problems (with respect to parameters), we can use Empirical Interpolation Method (EIM). However, is it will not work if we have a lot of parameters.
\end{remark}