The equation set and solution procedure used by \maestro\ has evolved
over time.  In this chapter, we outline the algorithm currently
implemented in the code.  The latest published reference for \maestro
is the multilevel paper \cite{multilevel}.  In this description, we
make frequent reference to paper~I \cite{lowMach},
paper~II \cite{lowMach2}, paper~III \cite{lowMach3}, and
paper~IV \cite{lowMach4}.

%-----------------------------------------------------------------------------
% Equations Summary
%-----------------------------------------------------------------------------

\section{Summary of the \maestro\ Equation Set}

Here we summarize the equations solved by \maestro.  We refer the reader
to papers~I through IV and the multilevel paper for the derivation
and motivation of the equation set.  (Note: this `traditional' algorithm
uses Strang-splitting for the reactions.  An alternate implementation, using
spectral deferred corrections is outlined in Chapter~\ref{ch:sdc}.)

\subsection{Base State}

The stratified atmosphere is characterized by a one-dimensional
time-dependent base state, defined by a base state density, $\rho_0$,
and a base state pressure, $p_0$, in hydrostatic equilibrum:
\begin{equation}
\nabla p_0 = -\rho_0 |g| \er
\end{equation}

The gravitational acceleration, $g$ is either constant or a point-mass
with a $1/r^2$ dependence (see \S \ref{sec:planarinvsqgravity}) for plane-parallel geometries, or a monopole
constructed by integrating the base state density for spherical
geometries.

For the time-dependence, we will define a base state velocity, $w_0$,
which will adjust the base state from one hydrostatic equilibrum to
another in response to heating.

For convenience, we define a base state enthalphy, $h_0$, as needed
by laterally averaging the full enthalpy, $h$.

\subsection{Continuity}

Conservation of mass gives the same continuity equation we have with
compressible flow:
\begin{equation}
\frac{\partial \rho}{\partial t} + \nabla \cdot (\rho \Ub) = 0
\label{eq:flow:continuity}
\end{equation}

Additionally, we carry species around which can react.  The creation and destruction
of the species is described by their create rate, $\omegadot_k$, and the species
are defined by their mass fractions, $X_k \equiv \rho_k / \rho$, giving
\begin{equation}
\frac{\partial \rho X_k}{\partial t} + \nabla \cdot (\rho \Ub X_k) = \rho \omegadot_k
\label{eq:flow:rhoX}
\end{equation}
and
\begin{equation}
\sum_k X_k = 1
\end{equation}

The base state density evolution equation can be defined by laterally averaging the
full continuity equation, giving:
\begin{equation}
\frac{\partial\rho_0}{\partial t} = -\nabla\cdot(\rho_0 w_0 \eb_r),\label{eq:flow:base_density}
\end{equation}

Subtracting these two yields the evolution equation for the perturbational
density, $\rho^\prime \equiv \rho - \rho_0$:
\begin{equation}
\frac{\partial\rho'}{\partial t} = -\Ub\cdot\nabla\rho' -
  \rho'\nabla\cdot\Ub - \nabla\cdot\left(\rho_0\Ubt\right) \label{eq:flow:rhoprime}
\end{equation}

As first discussed in paper III and then refined in the multilevel paper, we capture
the changes that can occur due to significant convective
overturning by imposing the constraint that $\overline{\rho'}=0$
for all time.  This gives
\begin{equation}
\frac{\partial\overline{\rho'}}{\partial t} = -\nabla\cdot(\etarho\eb_r).
\end{equation}
where
\begin{equation}
\etarho = \overline{\left(\rho'\Ub\cdot\eb_r\right)}
\label{eq:flow:etarho}
\end{equation}
In practice, we correct the drift by simply setting $\rho_0 =
\overline{\rho}$ after the advective update of $\rho$.  However we still need to
explicitly compute $\etarho$ since it appears in other equations.

\subsection{Constraint}

The equation of state is cast into an elliptic constraint on the
velocity field by differentiating $p_0(\rho, s, X_k)$ along particle
paths, giving:
\begin{equation}
\nabla \cdot (\beta_0 \Ub) =
   \beta_0 \left ( S - \frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial t} \right )
\label{eq:U divergence}
\end{equation}
where $\beta_0$ is a density-like variable that carries background
 stratification, defined as
\begin{equation}
\beta_0(r,t) = \rho_0(0,t)\exp\left(\int_0^r\frac{1}{\gammabar p_0}\frac{\partial p_0}{\partial r'}dr'\right),
\end{equation}
and
\begin{equation}
S = -\sigma\sum_k\xi_k\omegadot_k + \frac{1}{\rho p_\rho}\sum_k p_{X_k}\omegadot_k + \sigma\Hnuc + \sigma\Hext + \frac{\sigma}{\rho} \nabla \cdot \kth \nabla T
\label{eq:flow:S}
\end{equation}
where $p_{X_k} \equiv \left. \partial p / \partial X_k
\right|_{\rho,T,X_{j,j\ne k}}$, $\xi_k \equiv \left. \partial h /
\partial X_k \right |_{p,T,X_{j,j\ne k}},
p_\rho \equiv \left. \partial p/\partial \rho \right |_{T, X_k}$, and
$\sigma \equiv p_T/(\rho c_p p_\rho)$, with
$p_T \equiv \left. \partial p / \partial T \right|_{\rho, X_k}$ and
$c_p \equiv \left.  \partial h / \partial T
\right|_{p,X_k}$ is the specific heat at constant pressure.   The last
term is only present if we are using thermal diffusion (\runparam{use\_thermal\_diffusion} {\tt = T}).  In this term, $\kth$ is the thermal conductivity.

In this constraint, $\gammabar$ is the lateral average of
$\Gamma_1 \equiv d\log p / d\log \rho |_s$.  Using the lateral average
here makes it possible to cast the constraint as a
divergence.  \cite{KP:2012} discuss the general case where we want to
keep the local variations of $\Gamma_1$ (and we explored this in paper
III).  We also look at this in \S~\ref{sec:flow:gamma1vary}.

\subsection{Momentum}

The compressible momentum equation (written in terms of velocity is):
\begin{equation}
\rho \frac{\partial \Ub}{\partial t} + \rho \Ub \cdot \nabla \Ub + \nabla p = -\rho |g| \er
\end{equation}
Subtracting off the base state, and defining the perturbational
pressure (sometimes called the dynamic pressure) as $\pi \equiv p - p_0$,
and perturbational density as $\rho' \equiv \rho - \rho_0$, we have:
\begin{equation}
\rho \frac{\partial \Ub}{\partial t} + \rho \Ub \cdot \nabla \Ub + \nabla \pi = -\rho' |g| \er
\end{equation}
or
\begin{equation}
\frac{\partial \Ub}{\partial t} + \Ub \cdot \nabla \Ub + \frac{1}{\rho} \nabla \pi =
   -\frac{\rho^\prime}{\rho} |g| \er
\end{equation}
This is the form of the momentum equation that we solved in papers
I--IV and in the multilevel paper.

Several authors \cite{KP:2012,VLBWZ:2013} explored the idea of energy
conservation in a low Mach number system and found that an additional
term (which can look like a buoyancy) is needed in the low Mach number
formulation, yielding:
\begin{equation}
\frac{\partial \Ub}{\partial t} + \Ub \cdot \nabla \Ub +
   \frac{\beta_0}{\rho} \nabla \left (\frac{p^\prime}{\beta_0} \right ) =
   -\frac{\rho^\prime}{\rho} |g| \er
\label{eq:flow:newmomentum}
\end{equation}
This is the form that we enforce in \maestro, and the choice is controlled
by \runparam{use\_alt\_energy\_fix}.

We decompose the full velocity field into a base state velocity,
$w_0$, that governs the base state dynamics, and a local velocity,
$\Ubt$, that governs the local dynamics, i.e.,
\begin{equation}
\Ub = w_0(r,t)\eb_r + \Ubt(\xb,t).
\end{equation}
with
$\overline{(\Ubt\cdot\eb_r)} = 0$ and
$w_0 = \overline{(\Ub\cdot\eb_r)}$---the motivation for this splitting was given in paper~II.
The velocity evolution equations are then
\begin{align}
\frac{\partial w_0}{\partial t} &= -w_0\frac{\partial w_0}{\partial
  r} - \frac{\beta_0}{\rho_0}\frac{\partial(\pi_0/\beta_0)}{\partial r},\label{eq:w0
  evolution}\\
%
\frac{\partial\Ubt}{\partial t} &= -(\Ubt + w_0\er)\cdot\nabla\Ubt
  - \left(\Ubt\cdot\eb_r\right)\frac{\partial w_0}{\partial r}\eb_r -
\frac{\beta_0}{\rho}\nabla\left(\frac{\pi}{\beta_0} \right) +
\frac{\beta_0}{\rho_0}\frac{\partial(\pi_0/\beta_0)}{\partial r}\eb_r -
\frac{\rho-\rho_0}{\rho}g\eb_r.\label{eq:flow:utildeupd}
\end{align}
where $\pi_0$ is the base state component of the perturbational pressure.
By laterally averaging to equation (\ref{eq:U divergence}),
we obtain a divergence constraint for $w_0$:
\begin{equation}
\nabla\cdot(\beta_0 w_0 \eb_r) =
    \beta_0\left(\Sbar - \frac{1}{\gammabar p_0}
           \frac{\partial p_0}{\partial t}\right).\label{eq:w0 divergence}
\end{equation}
The divergence constraint for $\Ubt$ can be found by subtracting
(\ref{eq:w0 divergence}) into (\ref{eq:U divergence}), resulting in
\begin{equation}
\nabla\cdot\left(\beta_0\Ubt\right) = \beta_0\left(S-\Sbar\right).\label{eq:utilde divergence}
\end{equation}

\subsection{Base State Expansion}

In practice, we calculate $w_0$ by integrating
the one-dimensional divergence constraint.  For a plane-parallel atmosphere, the
evolution is:
\begin{equation}
\label{eq:flow:dw0dr_planar}
\frac{\partial w_0}{\partial r} = \Sbar - \frac{1}{\gammabar p_0} \etarho g \enskip
\end{equation}

Then we define \MarginPar{this term needs the $\beta_0$'s as shown.  Also, is this really
the form, or should it be $\overline{(\beta_0/\rho_0) \partial (\pi_0/\beta_0)/\partial r}$?}
\begin{equation}
- \frac{\beta_0}{\rho_0} \frac{\partial (\pizero/\beta_0)}{\partial r} = \frac{\partial w_0}{\partial t} +
   w_0 \frac{\partial w_0}{\partial r} \enskip, \label{eq:pizero}
\end{equation}
once $w_0$ at the old and new times is known, and the advective term is computed explicitly.
Then we can include this for completeness in the update for $\ut.$

\subsection{Energy}

Finally, we add an equation for specific enthalpy evolution to our
system.  Strictly speaking this is not necessary to close the system,
but it becomes convenient at times to define the temperature.
\begin{equation}
\frac{\partial(\rho h)}{\partial t} =
   -\nabla\cdot(\rho h\Ub) + \frac{Dp_0}{Dt} + \rho\Hnuc + \rho\Hext,\label{eq:flow:enthalpy}
\end{equation}

We will often expand $Dp_0/Dt$ as
\begin{equation}
\frac{Dp_0}{Dt} = \psi + (\Ubt \cdot \er) \frac{\partial p_0}{\partial r}
\end{equation}
where we defined
\begin{equation}
\psi \equiv \frac{\partial p_0}{\partial t} + w_0 \frac{\partial p_0}{\partial r}
\end{equation}

When we are using thermal diffusion, there will be an additional term in
the enthalpy equation (see \S~\ref{sec:flow:diffusion}).

In paper~III, we showed that for a plane-parallel atmosphere with
constant gravity, $\psi = \etarho g$

At times, we will define a temperature equation by writing $h = h(T,p,X_k)$
and differentiating:
\begin{equation}
\label{eq:flow:temp}
\frac{DT}{Dt} = \frac{1}{\rho c_p} \left\{ \left(1 - \rho h_p\right) \left
  [ \psi + (\Ubt \cdotb \er) \frac{\partial p_0}{\partial r} \right ]
 - \sum_k \rho \xi_k {\omegadot}_k
 + \rho \Hnuc + \rho \Hext \right \} \enskip  .
\end{equation}

The base state evolution equations for density and enthalpy can be
found by averaging Eq~\ref{eq:flow:enthalpy}
over a layer of constant radius, resulting in
\begin{eqnarray}
\frac{\partial(\rho h)_0}{\partial t} &=& -\nabla\cdot\left[(\rho h)_0w_0\eb_r\right] +
  \psi + \overline{\rho \Hnuc} + \overline{\rho \Hext}. \label{eq:flow:enthalpy_base}
\end{eqnarray}
Subtracting it from the full enthalpy equation gives:
\begin{equation}
\frac{\partial(\rho h)'}{\partial t} = -\Ub\cdot\nabla(\rho h)' - (\rho h)'\nabla\cdot\Ub -
  \nabla\cdot\left[(\rho h)_0\Ubt\right] + \Ubt\cdot\nabla p_0
   + ( \rho\Hnuc - \overline{\rho \Hnuc}) + (\rho\Hext - \overline{\rho \Hext})
\label{eq:flow:rhohprime}
\end{equation}

%-----------------------------------------------------------------------------
% Time Advancement Algorithm
%-----------------------------------------------------------------------------

\section{Time Advancement Algorithm}\label{Sec:Time Advancement Algorithm}
Here is the current description of the algorithm, based on the
description in the multilevel paper.  The initialization has also been
included with more detail than was given in paper III.  The main
driver for a single step of the algorithm is \code{advance.f90}---refer
to that code to see the sequence of functions called to implement each
step.

\subsection{Definitions}
Below we define operations that will be referenced in
\S~\ref{sec:flow:singlestep}.

\begin{itemize}
\item {\bf React State}$[\rho^{\inp},(\rho h)^{\inp},X_k^{\inp},T^{\inp}, (\rho\Hext)^{\inp},
  p_0^{\inp}] \rightarrow [\rho^{\outp}, (\rho h)^{\outp}, X_k^{\outp},
  T^{\outp}, (\rho \omegadot_k)^{\outp}, (\rho\Hnuc)^{\outp}]$
  evolves the species and enthalpy due to reactions through
  $\Delta t/2$ according to:
\begin{equation}
\frac{dX_k}{dt} = \omegadot_k(\rho,X_k,T) ; \qquad
\frac{dT}{dt}   = \frac{1}{c_p} \left ( -\sum_k \xi_k  \omegadot_k  + \Hnuc \right ).
\end{equation}
Here the temperature equation comes from Eq.~\ref{eq:flow:temp} with $Dp_0/Dt = 0$ for
the burning part of the evolution.

Full details of the
solution procedure can be found in Paper III. We then define:
\begin{eqnarray}
(\rho\omegadot_k)^{\outp} &=& \frac{\rho^{\outp} ( X_k^{\outp} - X_k^{\inp})}{\dt/2}, \\
(\rho h)^{\outp} &=& (\rho h)^{\inp} + \frac{\dt}{2} (\rho\Hnuc)^{\outp} + \frac{\dt}{2} (\rho\Hext)^{\inp}.
\end{eqnarray}
where the enthalpy update includes external heat sources $(\rho\Hext)^{\inp}$.
As introduced in Paper IV, we update the temperature using $T^{\outp} =
T(\rho^\outp,h^\outp,X_k^\outp)$ for planar geometry or $T^{\outp} =
T(\rho^\outp,p_0^\inp,X_k^\outp)$ for spherical geometry, with this behavior
controlled by \runparam{use\_tfromp}.
Note that the density remains unchanged within {\bf React State}, i.e.,
$\rho^{\outp} = \rho^{\inp}$.

The source code for this operation can be found in \code{react\_state.f90}.

\item {\bf Advect Base Density}$[\rho_0^\inp,w_0^\inp] \rightarrow
  [\rho_0^\outp, \rho_0^{\outp,\nph}]$ is the process by which we
  update the base state density through $\dt$ in time.  We keep the
  time-centered edge states, $\rho_0^{\outp,\nph}$,
  since they are used later in discretization of $\etarho$ for planar problems.
\begin{description}
\item[planar:] We discretize equation (\ref{eq:flow:base_density}) to
  compute the new base state density,
\begin{equation}
\rho_{0,j}^{\outp} = \rho_{0,j}^{\inp} - \frac{\dt}{\dr} \left [ \left( \rho_0^{\outp,\nph} w_0^{\inp}\right)_{j+\myhalf} - \left( \rho_0^{\outp,\nph} w_0^{\inp}\right)_{j-\myhalf} \right ].
\end{equation}
  We compute the time-centered edge states, ${\rho_0}^{\outp,\nph}_{j\pm\myhalf}$,
  by discretizing an expanded form of equation (\ref{eq:flow:base_density}):
\begin{equation}
\frac{\partial \rho_0}{\partial t} + w_0 \frac{\partial \rho_0}{\partial r} = - \rho_0 \frac{\partial w_0}{\partial r},
\end{equation}
  where the right hand side is used as the force term.
\item[spherical:] The base state density update now includes the area factors in the
  divergences:
\begin{equation}
\rho_{0,j}^{\outp} = \rho_{0,j}^{\inp} - \frac{1}{r_j^2} \frac{\dt}{\dr} \left [ \left( r^2 \rho_0^{\outp,\nph} w_0^{\inp}\right)_{j+\myhalf} - \left( r^2 \rho_0^{\outp,\nph} w_0^{\inp}\right)_{j-\myhalf} \right].
\end{equation}
  In order to compute the time-centered edge states, an additional geometric
  term is added to the forcing, due to the spherical discretization of
  (\ref{eq:flow:base_density}):
\begin{equation}
\frac{\partial \rho_0}{\partial t} + w_0 \frac{\partial \rho_0}{\partial r} = - \rho_0 \frac{\partial w_0}{\partial r} - \frac{2 \rho_0 w_0}{r}.
\end{equation}
\end{description}

The source code for this operation can be found in \code{advect\_base.f90}.

\item {\bf Enforce HSE}$[p_0^{\inp},\rho_0^{\inp}]
  \rightarrow [p_0^{\outp}]$ has replaced {\bf Advect Base Pressure}
  from Paper III as the process by which we update the base state
  pressure.  Rather than discretizing the evolution equation for
  $p_0$, we enforce hydrostatic equilibrium directly, which is numerically simpler
  and analytically equivalent.  We first set
  $p_{0,j=0}^{\outp} = p_{0,j=0}^{\inp}$ and then update $p_0^\outp$ using:
\begin{equation}
p_{0,j+1}^{\outp} = p_{0,j}^{\outp} + \Delta r g_{j+\myhalf}\frac{\left(\rho_{0,j+1}^{\inp}+\rho_{0,j}^{\inp}\right)}{2},
\end{equation}
  where $g=g(\rho_0^\inp)$.  As soon as $\rho_{0,j}^\inp < \rho_{\rm cutoff}$, we set
  $p_{0,j+1}^\outp = p_{0,j}^\outp$ for all remaining values of $j$.
  Then we compare $p_{0,j_{\rm max}}^\outp$ with $p_{0,j_{\rm max}}^\inp$ and offset
  every element in $p_0^\outp$ so that $p_{0,j_{\rm max}}^\outp = p_{0,j_{\rm max}}^\inp$.
  We are effectively using the location where the $\rho_0^\inp$ drops below
  $\rho_{\rm cutoff}$ as the starting point for integration.

The source code for this operation can be found in \code{enforce\_HSE.f90}.

\item {\bf Advect Base Enthalpy}$[(\rho h)_0^\inp,w_0^\inp,\psi^\inp] \rightarrow [(\rho h)_0^\outp]$
  is the process by which we update the base state enthalpy through $\dt$ in time.
\begin{description}
\item[planar:] We discretize equation (\ref{eq:flow:enthalpy_base}), neglecting reaction
source terms, to compute the new base state enthalpy,
\begin{equation}
(\rho h)_{0,j}^{\outp} = (\rho h)_{0,j}^{\inp} - \frac{\dt}{\Delta r} \left\{ \left[ (\rho h)_0^{\nph} w_0^{\inp}\right]_{j+\myhalf} - \left[ (\rho h)_0^{\nph} w_0^{\inp}\right]_{j-\myhalf} \right\} + \dt\psi_j^{\inp}.
\end{equation}
  We compute the time-centered edge states, $(\rho h)_0^{\nph}$, by discretizing
  an expanded form of equation (\ref{eq:flow:enthalpy_base}):
\begin{equation}
\frac{\partial (\rho h)_0}{\partial t} + w_0 \frac{\partial (\rho h)_0}{\partial r} = -(\rho h)_0 \frac{\partial w_0}{\partial r} + \psi.
\end{equation}
\item[spherical:]  The base state enthalpy update now includes the area factors
  in the divergences:
\begin{eqnarray}
(\rho h)_{0,j}^{\outp} &=& (\rho h)_{0,j}^{\inp} \nonumber \\
&& - \frac{1}{r_j^2} \frac{\dt}{\dr} \left \{ \left[ r^2 (\rho h)_0^{\nph} w_0^{\inp}\right]_{j+\myhalf} - \left[ r^2 (\rho h)_0^{\nph} w_0^{\inp}\right]_{j-\myhalf} \right\} +\dt\psi^{\inp,\nph}.\nonumber\\
\end{eqnarray}
  In order to compute the time-centered edge states, an additional geometric
  term is added to the forcing, due to the spherical discretization of
  (\ref{eq:flow:enthalpy_base}):
\begin{equation}
\frac{\partial (\rho h)_0}{\partial t} + w_0 \frac{\partial (\rho h)_0}{\partial r} = -(\rho h)_0 \frac{\partial w_0}{\partial r} - \frac{2 (\rho h)_0 w_0}{r} + \psi.
\end{equation}
\end{description}

The source code for this operation can be found in \code{advect\_base.f90}.

\item {\bf Computing {\boldmath $w_0$}}\label{Sec:Computing w0}
Here we describe the process by which we compute $w_0$.  The arguments
are different for planar and spherical geometries.
\begin{description}

\item {\bf Compute} {\boldmath $w_0$} {\bf Planar}
$[\Sbar^{\inp},\gammabar^{\inp}, p_0^{\inp},\psi^{\inp}]\rightarrow [w_0^{\outp}]$:

In Paper III, we showed that $\psi=\etarho g$ for planar geometries,
and derived derived Eq.~\ref{eq:flow:dw0dr_planar} as an alternate
expression for Eq.~\ref{eq:w0 divergence}.  We discretize this as:
\begin{equation}
\frac{w_{0,j+\myhalf}^\outp-w_{0,j-\myhalf}^\outp}{\Delta r} = \left(\Sbar^{\inp} - \frac{1}{\gammabar^{\inp} p_0^{\inp}}\psi^{\inp}\right)_j,
\end{equation}
with $w_{0,-\myhalf}=0$.

\item {\bf Compute} {\boldmath $w_0$} {\bf Spherical}
  $[\Sbar^{\inp},\gammabar^{\inp},\rho_0^{\inp},p_0^{\inp},\etarho^{\inp}]
  \rightarrow[w_0^{\outp}]$:

We begin with equation (\ref{eq:w0 divergence}) written in spherical coordinates:
\begin{equation}
\frac{1}{r^2}\frac{\partial}{\partial r} \left (r^2 \beta_0 w_0 \right ) = \beta_0 \left ( \Sbar - \frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial t} \right ).
\end{equation}
We expand the spatial derivative and recall from Paper I that
\begin{equation}
\frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial r} = \frac{1}{\beta_0} \frac{\partial \beta_0}{\partial r},
\end{equation}
giving:
\begin{equation}
\frac{1}{r^2} \frac{\partial}{\partial r} \left (r^2 w_0 \right ) = \Sbar - \frac{1}{\gammabar p_0} \underbrace{\left( \frac{\partial p_0}{\partial t} + w_0 \frac{\partial p_0}{\partial r} \right)}_{\psi}.\label{eq:psi def}
\end{equation}
We solve this equation for $w_0$ as described in Appendix~B of the multilevel paper.

\end{description}

The source code for this operation can be found in \code{make\_w0.f90}.
\end{itemize}

\subsection{\label{sec:flow:singlestep} Single Step}

\begin{description}\itemsep 1em

%--------------------------------------------------------------------------
% STEP 0
%--------------------------------------------------------------------------

\item[Step 0.] {\em Initialization}

This step remains unchanged from Paper III.  See \S \ref{Sec:Initialization}
for details.  The initialization step only occurs at the beginning of the simulation.
The initial values for $\Ub^0, \rho^0, (\rho h)^0, X_k^0, T^0,
\rho_0^0, p_0^0$, and $\overline{\Gamma_1^0}$ are specified from the problem-dependent
initial conditions.  The initial time step, $\dt^0$, is computed as in
Paper III.  Finally, initial
values for $w_0^{-\myhalf}, \etarho^{-\myhalf}, \psi^{-\myhalf},
\pi^{-\myhalf}, S^0$, and $S^1$ come from a preliminary pass through
the algorithm.

%--------------------------------------------------------------------------
% STEP 1
%--------------------------------------------------------------------------
\item[Step 1.] {\em React the full state through the first time interval of $\dt / 2.$}

Call {\bf React State}$[\rho^n, (\rho h)^n, X_k^n, T^n, (\rho\Hext)^n, p_0^n]
 \rightarrow [\rho^{(1)},(\rho h)^{(1)},X_k^{(1)},T^{(1)},(\rho \omegadot_k)^{(1)},(\rho \Hnuc)^{(1)}]$.

%--------------------------------------------------------------------------
% STEP 2
%--------------------------------------------------------------------------

\item[Step 2.] {\em Compute the provisional time-centered expansion,
  $S^{\nph,\star\star}$, provisional base state velocity,
  $w_0^{\nph,\star}$, and provisional base state velocity forcing.}

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \Alph{enumi}}}

\item Compute $S^{\nph,\star\star}$.  We compute an estimate for the
  time-centered expansion term in the velocity divergence constraint,
  as given in Eq.~\ref{eq:flow:S}.  For the first time step ($n=0$),
  we set
\begin{equation}
S^{n+\myhalf,\star\star} = \frac{S^0 + S^1}{2},
\end{equation}
  where $S^1$ is found during initialization.  For other time steps
  $(n \ne 0)$, following \cite{SNe}, we extrapolate
  to the half-time using $S$ at the previous and current
  time levels
\begin{equation}
S^{\nph,\star\star} = S^n + \frac{\dt^n}{2} \frac{S^n - S^{n-1}}{\dt^{n-1}}.
\end{equation}
  Next, compute
\begin{equation}
\overline{S^{\nph,\star\star}} = {\mathrm{\bf Avg}} \left(S^{\nph,\star\star}\right).
\end{equation}

\item Compute $w_0^{\nph,\star}$.
\begin{description}
\item For planar geometry, call\\
{\bf Compute} {\boldmath $w_0$} {\bf Planar}$[\overline{S^{\nph,\star\star}},\overline{\Gamma_1^n},p_0^n,\psi^{n-\myhalf}] \rightarrow [w_0^{\nph,\star}]$.
\item For spherical geometry, call\\
{\bf Compute} {\boldmath $w_0$} {\bf Spherical}$[\overline{S^{\nph,\star\star}},\overline{\Gamma_1^n},\rho_0^n,p_0^n,\etarho^{n-\myhalf}] \rightarrow [w_0^{\nph,\star}]$.
\end{description}

\item Compute the provisional base state velocity forcing, using equation (38)
  from paper~III,
\begin{equation}
-\frac{\beta_0}{\rho_0} \frac{\partial (\pi_0/\beta_0)}{\partial r} = \frac{\partial w_0}{\partial t} + w_0 \frac{\partial w_0}{\partial r}, \label{eq:pizero}
\end{equation}
  with the following discretization:
\begin{equation}
\left ( \frac{\beta_0}{\rho_0} \frac{\partial (\pi_0/\beta_0)}{\partial r} \right )^{n,\star} = -\frac{w_0^{\nph,\star} - w_0^\nmh}{(\dt^n+\dt^{n-1})/2} - w_0^{n,\star} \left(\frac{\partial w_0}{\partial r}\right)^{n,\star},
\end{equation}
  where $w_0^{n,\star}$ and $(\partial w_0 / \partial r)^{n,\star}$ are defined as
\begin{eqnarray}
w_0^{n,\star} &=& \frac{\dt^{n} w_0^{\nmh} + \dt^{n-1} w_0^{\nph,\star}}{\dt^n+\dt^{n-1}}, \\
\left(\frac{\partial w_0}{\partial r}\right)^{n,\star} &=& \frac{1}{\dt^n+\dt^{n-1}}\left [ \dt^{n} \left(\frac{\partial w_0 }{ \partial r}\right)^{\nmh} + \dt^{n-1} \left(\frac{\partial w_0 }{ \partial r}\right)^{\nph,\star} \right ].\nonumber \\
\end{eqnarray}
  If $n=0$, we use $\dt^{-1} = \dt^0$.
\end{enumerate}

%--------------------------------------------------------------------------
% STEP 3
%--------------------------------------------------------------------------
\item[Step 3.] {\em Construct the provisional time-centered advective velocity on
edges, $\uadvone$.}

The local velocity field is described by Eq.~\ref{eq:flow:utildeupd}.
From this, we compute time-centered edge
velocities, $\uadvonedag$, using
$\Ub = \Ubt^n + w_0^{\nph,\star}$.  The $\dagger$ superscript refers to the
fact that the predicted velocity field does not satisfy the divergence
constraint.  We then construct $\uadvone$ from $\uadvonedag$
using a MAC projection, as described in detail in Appendix B of Paper III.
We note that $\uadvone$ satisfies the discrete version of
$\overline{(\uadvone\cdot\eb_r)}=0$ as well as
\begin{eqnarray}
\nabla \cdot \left(\beta_0^n \uadvone\right) &=& \beta_0^n \left(S^{\nph,\star\star} - \overline{S^{\nph,\star\star}}\right),\\
 \beta_0^n &=& \beta_0 \left(\rho_0^n, p_0^n, \overline{\Gamma_1^n}\right),
\end{eqnarray}
where $\beta_0$ is computed as described in Appendix C of Paper III (although
note that an alternate procedure that uses linear reconstruction of $g$ in
constructing $\beta_0$ is enabled with \runparam{use\_linear\_grav\_in\_beta}).

%--------------------------------------------------------------------------
% STEP 4
%--------------------------------------------------------------------------
\item[Step 4.] {\em Advect the base state and full state through a time interval of $\dt.$}

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \Alph{enumi}}}

\item Update $\rho_0$, saving the time-centered density at radial edges by calling

{\bf Advect Base Density}$[\rho_0^{n},w_0^{\nph,\star}] \rightarrow [\rho_0^{(2a),\star}, \rho_0^{\nph,\star,\pred}]$.

\item Update $(\rho X_k)$ using a discretized version of the species
continuity equation, Eq.~\ref{eq:flow:rhoX}, where we omit the
reaction terms, which were already accounted for in {\bf React State}:
\begin{equation}
\frac{\partial (\rho X_k)}{\partial t} + \nabla \cdot (\Ub \rho X_k) = 0 \enskip .
\label{eq:species}
\end{equation}
The update consists of two steps:

\begin{enumerate}
\renewcommand{\labelenumii}{{\bf \roman{enumii}}.}

\item Compute the time-centered species edge states, $(\rho X_k)^{\nph,\star,\pred}$,
  for the conservative update of $(\rho X_k)^{(1)}$.

  There are a variety of choices of quantities to predict to the
  edges (controlled by \runparam{species\_pred\_type}---see Chapter~\ref{ch:pert}).
  By default, we use the equations
\begin{eqnarray}
\frac{\partial\rho'}{\partial t} &=& -\Ub\cdot\nabla\rho' -
     \rho'\nabla\cdot\Ub - \nabla\cdot\left(\rho_0\Ubt\right),
     \label{eq:Perturbational Density}  \\
\frac{\partial X_k}{\partial t} &=& -\Ub\cdot\nabla X_k +
     \omegadot_k. \label{eq:Primitive Species}
\end{eqnarray}
  to
  predict $\rho^{'(1)} = \rho^{(1)} - \rho_0^n$ and
  $X_k^{(1)} = (\rho  X_k)^{(1)} / \rho^{(1)}$ to time-centered edges using
  $\Ub = \uadvone+w_0^{\nph,\star}\eb_r$, yielding $\rho^{'\nph,\star,\pred}$
  and $X_k^{\nph,\star,\pred}$.
  We convert the perturbational density to full state density using
\begin{equation}
\rho^{\nph,\star,\pred} = \rho^{'\nph,\star,\pred} + \frac{\rho_0^n + \rho_0^{(2a),\star}}{2},
\end{equation}
  where the base state density terms are mapped to Cartesian edges.
  Then,\\
  $(\rho X_k)^{\nph,\star,\pred} = \rho^{\nph,\star,\pred} \, X_k^{\nph,\star,\pred}$.

\item Evolve $(\rho X_k)^{(1)} \rightarrow (\rho X_k)^{(2),\star}$ using
\begin{eqnarray}
(\rho X_k)^{(2),\star} &=& (\rho X_k)^{(1)} \nonumber \\
&& - \dt \left\{ \nabla \cdot \left[ \left(\uadvone+w_0^{\nph,\star} \eb_r\right) (\rho X_k)^{\nph,\star,\pred} \right] \right\},\nonumber \\
\end{eqnarray}
\begin{equation}
\rho^{(2),\star} = \sum_k (\rho X_k)^{(2),\star},
\qquad
X_k^{(2),\star} = (\rho X_k)^{(2),\star} / \rho^{(2),\star}.
\end{equation}

\end{enumerate}

\item Define a radial edge-centered $\etarho^{\nph,\star}$ (Eq.~\ref{eq:flow:etarho}).

\begin{description}
\item For planar geometry, since $\etarho = \overline{\rho'(\Ub\cdot\eb_r)} = \overline{\rho(\Ub\cdot\eb_r)}-\overline{\rho_0(\Ub\cdot\eb_r}) = \overline{\rho(\Ub\cdot\eb_r)} - \rho_0w_0$,
\begin{eqnarray}
 \etarho^{\nph,\star} &=&  {\rm {\bf Avg}} \sum_k \left[ \left(\uadvone \cdot \eb_r + w_0^{\nph,\star}\right) (\rho X_k)^{\nph,\star,\pred} \right]\nonumber\\
&& - w_0^{\nph,\star} \rho_0^{\nph,\star,\pred},
\end{eqnarray}
\item For spherical geometry, first construct
$\etarho^{{\rm cart},\nph,\star} =
[\rho'(\Ub\cdot\eb_r)]^{\nph,\star}$ on Cartesian cell centers using:
\begin{eqnarray}
\etarho^{{\rm cart},\nph,\star} &=& \left[\left(\frac{\rho^{(1)}+\rho^{(2),\star}}{2}\right)-\left(\frac{\rho_0^n+\rho_0^{(2a),\star}}{2}\right)\right] \nonumber \\
&&\cdot \left( \uadvone \cdot \eb_r  + w_0^{\nph,\star}\right).
\end{eqnarray}
Then,
\begin{equation}
\etarho^{\nph,\star} = {\rm {\bf Avg}}\left(\etarho^{{\rm cart},\nph,\star}\right).
\end{equation}
This gives a radial cell-centered $\etarho^{\nph,\star}$.  To get
$\etarho^{\nph,\star}$ at radial edges, average the two neighboring
radial cell-centered values.
\end{description}

\item Correct $\rho_0$ by setting $\rho_0^{n+1,\star} =$ {\bf Avg}$(\rho^{(2),\star})$.

\item Update $p_0$ by calling
{\bf Enforce HSE}$[p_0^n,\rho_0^{n+1,\star}] \rightarrow [p_0^{n+1,\star}]$.

\item Compute $\psi^{\nph,\star}$.
\begin{description}
\item For planar geometry,
\begin{equation}
\psi_j^{\nph,\star} = \frac{1}{2} \left(\eta_{\rho,j-\myhalf}^{\nph,\star}
+ \eta_{\rho,j+\myhalf}^{\nph,\star}\right) g.
\end{equation}
\item For spherical geometry, first compute:
\begin{eqnarray}
\overline{\Gamma_1^{(1)}} &=& {\rm{\bf Avg}} \left[ \Gamma_1\left(\rho^{(1)}, p_0^{n}, X_k^{(1)}\right) \right]  , \\
\overline{\Gamma_1^{(2),\star}} &=& {\rm{\bf Avg}} \left[ \Gamma_1\left(\rho^{(2),\star}, p_0^{n+1,\star}, X_k^{(2),\star}\right) \right].
\end{eqnarray}
Then, define $\psi^{\nph,\star}$ using equation (\ref{eq:psi def})
\begin{eqnarray}
\psi_j^{\nph,\star}
&=& \left(\frac{\overline{\Gamma_1^{(1)}}+\overline{\Gamma_1^{(2),\star}}}{2}\right)_j
\left(\frac{p_0^n+p_0^{n+1,\star}}{2}\right)_j \nonumber \\
&& \left \{ \overline{S_j^{\nph,\star}} - \frac{1}{r_j^2} \left [ \left(r^2 w_0^{\nph,\star}\right)_{j+\myhalf} - \left(r^2 w_0^{\nph,\star}\right)_{j-\myhalf} \right ] \right \}.\nonumber \\
\end{eqnarray}
\end{description}

\item Update $(\rho h)_0$.  First, compute $(\rho h)_0^n = $ {\bf Avg}$[(\rho h)^{(1)}]$.
Then, call\\
{\bf Advect Base Enthalpy}$[(\rho h)_0^{n}, w_0^{\nph,\star}, \psi^{\nph,\star}] \rightarrow [(\rho h)_0^{n+1,\star}]$.

\item Update the enthalpy using a discretized version of the enthalpy
evolution equation (Eq.~\ref{eq:flow:enthalpy}), again omitting the reaction and heating terms
since we already accounted for
them in {\bf React State}.  This equation takes the form:
\begin{equation}
\frac{\partial (\rho h)}{\partial t}  = - \nabla \cdot (\Ub \rho h) + \psi + (\Ubt \cdot \eb_r) \frac{\partial p_0}{\partial r}.
\end{equation}
For spherical geometry, we solve the
analytically equivalent form,
\begin{equation}
\frac{\partial (\rho h)}{\partial t}  = - \nabla \cdot (\Ub \rho h) + \psi + \nabla \cdot (\Ubt p_0) - p_0 \nabla \cdot \Ubt,
\end{equation}
which experience has shown to minimize the drift from thermodynamic
equilibrium.  The update consists of two steps:

\begin{enumerate}
\renewcommand{\labelenumii}{{\bf \roman{enumii}}.}

\item Compute the time-centered enthalpy edge state, $(\rho h)^{\nph,\star,\pred},$
  for the conservative update of $(\rho h)^{(1)}$.  There are a
   variety of quantities that we can predict to the interfaces here
   (controlled by \runparam{enthalpy\_pred\_type}---see
   Chapter~\ref{ch:pert}).  For the default case, we use the
   perturbational enthalpy equation, Eq.~\ref{eq:flow:rhohprime}, neglecting reactions,
\begin{equation}
\frac{\partial(\rho h)'}{\partial t} = -\Ub\cdot\nabla(\rho h)' -
   (\rho h)'\nabla\cdot\Ub - \nabla\cdot\left[(\rho h)_0\Ubt\right] + \Ubt\cdot\nabla p_0
    \label{eq:Perturbational Enthalpy}.
\end{equation}
  to predict
  $(\rho h)' = (\rho h)^{(1)} - (\rho h)_0^n$ to time-centered edges,
  using $\Ub = \uadvone+w_0^{\nph,\star} \eb_r$,
  yielding $(\rho h)^{'\nph,\star,\pred}$.  We convert the perturbational
  enthalpy to a full state enthalpy using
\begin{equation}
(\rho h)^{\nph,\star,\pred} = (\rho h)^{'\nph,\star,\pred} + \frac{(\rho h)_0^n + (\rho h)_0^{n+1,\star}}{2}.
\end{equation}
  For planar geometry, we map $(\rho h)_0$ directly to Cartesian edges.
  In spherical geometry, our experience has shown that a slightly different
  approach leads to reduced discretization errors.  We first map
  $h_0 \equiv (\rho h)_0/\rho_0$ and $\rho_0$ to Cartesian edges separately,
  and then multiply these terms to get $(\rho h)_0$.

\item Evolve $(\rho h)^{(1)} \rightarrow (\rho h)^{(2),\star}$.
\begin{description}
\item For planar geometry,
\begin{eqnarray}
(\rho h)^{(2),\star}
&=& (\rho h)^{(1)} \nonumber \\
&&- \dt \left\{ \nabla \cdot \left[ \left(\uadvone+w_0^{\nph,\star} \eb_r\right) (\rho h)^{\nph,\star,\pred} \right] \right\} \nonumber \\
&& + \dt \left(\uadvone \cdot \eb_r\right) \left(\frac{\partial p_0}{\partial r} \right)^{n} + \dt \psi^{\nph,\star},
\end{eqnarray}

\item For spherical geometry,
\begin{eqnarray}
(\rho h)^{(2),\star}
&=& (\rho h)^{(1)} \nonumber \\
&&- \dt \left\{ \nabla \cdot \left[ \left(\uadvone+w_0^{\nph,\star} \eb_r\right) (\rho h)^{\nph,\star,\pred} \right] \right\} \nonumber \\
&& + \dt \left \{ \nabla \cdot \left (\uadvone p_0^{n} \right ) - p_0^{n} \nabla \cdot \uadvone \right \} \nonumber \\
&&+ \dt \psi^{\nph,\star},
\end{eqnarray}
\end{description}

\end{enumerate}

Then, for each Cartesian cell where $\rho^{(2),\star} < \rho_\mathrm{cutoff}$,
we recompute enthalpy using
\begin{equation}
(\rho h)^{(2),\star} = \rho^{(2),\star}h\left(\rho^{(2),\star},p_0^{n+1,\star},X_k^{(2),\star}\right).
\end{equation}
This behavior is controlled by \runparam{do\_eos\_h\_above\_cutoff}.

\item Update the temperature using the equation of state:
$T^{(2),\star} =
  T(\rho^{(2),\star}, h^{(2),\star}, X_k^{(2),\star})$ (planar geometry) or
$T^{(2),\star} =
  T(\rho^{(2),\star}, p_0^{n+1,\star}, X_k^{(2),\star})$ (spherical geometry).
\end{enumerate}
As before, this behavior is controlled by \runparam{use\_tfromp}.

%--------------------------------------------------------------------------
% STEP 5
%--------------------------------------------------------------------------
\item[Step 5.] {\em React the full state through a second time interval of $\dt / 2.$}

Call {\bf React State}$[ \rho^{(2),\star},(\rho h)^{(2),\star}, X_k^{(2),\star}, T^{(2),\star},
(\rho\Hext)^{(2),\star}, p_0^{n+1,\star}]$ \\
 ${}\hfill   \rightarrow [ \rho^{n+1,\star},(\rho h)^{n+1,\star}, X_k^{n+1,\star}, T^{n+1,\star}, (\rho \omegadot_k)^{(2),\star}, (\rho \Hnuc)^{(2),\star} ].$

%--------------------------------------------------------------------------
% STEP 6
%--------------------------------------------------------------------------
\item[Step 6.] {\em Compute the time-centered expansion, $S^{\nph,\star}$, base state
velocity, $w_0^{\nph}$, and base state velocity forcing.}

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \Alph{enumi}}}

\item Compute $S^{\nph,\star}$.  First, compute $S^{n+1,\star}$ with
\begin{equation}
S^{n+1,\star} =  -\sigma  \sum_k  \xi_k  (\omegadot_k)^{(2),\star}  +
   \frac{1}{\rho^{n+1,\star} p_\rho} \sum_k p_{X_k}  ({\omegadot}_k)^{(2),\star} +
   \sigma \Hnuc^{(2),\star} + \sigma \Hext^{(2),\star},
\end{equation}
  where $(\omegadot_k)^{(2),\star} = (\rho \omegadot_k)^{(2),\star} /
  \rho^{(2),\star}$ and the thermodynamic quantities are defined using
  $\rho^{n+1,\star}, X_k^{n+1,\star},$ and $T^{n+1,\star}$ as inputs to
  the equation of state.  If we are using diffusion then we would include the diffusion
  term in $S$.  Then, define
\begin{equation}
\overline{S^{\nph,\star}} = {\mathrm{\bf Avg}} (S^{\nph,\star}),
\qquad
 S^{\nph.\star} = \frac{S^n + S^{n+1,\star}}{2},
\end{equation}
\item Compute $w_0^{\nph}$.  First, define
\begin{equation}
\overline{\Gamma_1^{\nph,\star}} = \frac{\overline{\Gamma_1^n} + \overline{\Gamma_1^{n+1,\star}}}{2},
\quad
\rho_0^{\nph,\star} = \frac{\rho_0^{n} + \rho_0^{n+1,\star}}{2},
\quad
p_0^{\nph,\star} = \frac{p_0^{n} + p_0^{n+1,\star}}{2},
\end{equation}
  with
\begin{equation}
 \overline{\Gamma_1^{n+1,\star}} = {\rm{\bf Avg}} \left[ \Gamma_1\left(\rho^{n+1,\star}, p_0^{n+1,\star}, X_k^{n+1,\star}\right) \right].
\end{equation}
\begin{description}
\item For planar geometry, call\\
{\bf Compute} {\boldmath $w_0$} {\bf Planar}$[\overline{S^{\nph,\star}},\overline{\Gamma_1^{\nph,\star}},p_0^{\nph,\star},\psi^{\nph,\star}]\rightarrow [w_0^{\nph}]$.
\item For spherical geometry, call\\
{\bf Compute} {\boldmath $w_0$} {\bf Spherical}$[\overline{S^{\nph,\star}},\overline{\Gamma_1^{\nph,\star}},\rho_0^{\nph,\star},p_0^{\nph,\star},\etarho^{\nph,\star}]\rightarrow [w_0^{\nph}]$.
\end{description}

\item Compute the base state velocity forcing.  Rearrange equation (\ref{eq:pizero}),
\begin{equation}
\left ( \frac{\beta_0}{\rho_0} \frac{\partial (\pi_0/\beta_0)}{\partial r} \right )^n =
-\frac{w_0^{\nph} - w_0^\nmh}{\myhalf(\dt^n+\dt^{n-1})}
- w_0^n \left(\frac{\partial w_0}{\partial r}\right)^n,
\end{equation}
  where $w_0^{n}$ and $(\partial w_0 / \partial r)^{n}$ are defined as
\begin{eqnarray}
w_0^n &=& \frac{\dt^{n} w_0^{\nmh} + \dt^{n-1} w_0^{\nph}}{\dt^n+\dt^{n-1}}, \\
\left(\frac{\partial w_0}{\partial r}\right)^{n} &=& \frac{1}{\dt^n+\dt^{n-1} } \left [ \dt^{n} \left(\frac{\partial w_0 }{ \partial r}\right)^{\nmh} + \dt^{n-1} \left(\frac{\partial w_0 }{ \partial r}\right)^{\nph} \right ].\nonumber \\
\end{eqnarray}
  If $n=0$, we use $\dt^{-1} = \dt^0$.

\end{enumerate}

%--------------------------------------------------------------------------
% STEP 7
%--------------------------------------------------------------------------
\item[Step 7.] {\em Construct the time-centered advective velocity on edges, $\uadvtwo$.}

The procedure to construct $\uadvtwodag$ is identical to the procedure
for computing $\uadvonedag$ in {\bf Step 3}, but uses
the updated values $w_0^{\nph}$ and $\pi_0^n$ rather than $w_0^{\nph,\star}$
and $\pi_0^{n,\star}$.  We note that $\uadvtwo$ satisfies the discrete version of
$\overline{(\uadvtwo\cdot\eb_r)}=0$ as well as
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph,\star} \uadvtwo\right) =
\beta_0^{\nph,\star}\left(S^{\nph,\star} - \overline{S^{\nph,\star}}\right),
\end{equation}
\begin{equation}
\beta_0^{\nph,\star} = \frac{ \beta_0^n +  \beta_0^{n+1,\star} }{2};
\qquad
 \beta_0^{n+1,\star} = \beta_0 \left(\rho_0^{n+1,\star}, p_0^{n+1,\star}, \overline{\Gamma_1^{n+1,\star}}\right).
\end{equation}

%--------------------------------------------------------------------------
% STEP 8
%--------------------------------------------------------------------------
\item[Step 8.] {\em Advect the base state and full state through a time interval of $\dt.$}

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \Alph{enumi}}}

\item Update $\rho_0$, saving the time-centered density at radial edges by calling

{\bf Advect Base Density}$[\rho_0^{n},w_0^{\nph}] \rightarrow [\rho_0^{(2a)}, \rho_0^{\nph,\pred}]$.

\item Update $(\rho X_k)$.  This step is identical to {\bf Step 4B} except we use
  the updated values $w_0^{\nph}, \uadvtwo$, and $\rho_0^{(2a)}$ rather than
  $w_0^{\nph,\star}, \uadvone$, and $\rho_0^{(2a),\star}$.  In particular:

\begin{enumerate}
\renewcommand{\labelenumii}{{\bf \roman{enumii}}.}

\item Compute the time-centered species edge states, $(\rho X_k)^{\nph,\pred}$,
  for the conservative update of $(\rho X_k)^{(1)}$.  We use equations
  (\ref{eq:Perturbational Density}) and (\ref{eq:Primitive Species}) to
  predict $\rho^{'(1)} = \rho^{(1)} - \rho_0^n$ and
  $X_k^{(1)} = (\rho  X_k)^{(1)} / \rho^{(1)}$ to time-centered edges
  with $\Ub = \uadvtwo+w_0^{\nph} \eb_r$,
  yielding $\rho^{'\nph,\pred}$ and $X_k^{\nph,\pred}$.
  We convert the perturbational density to a full state density using
\begin{equation}
\rho^{\nph,\pred} = \rho^{'\nph,\pred} + \frac{\rho_0^n + \rho_0^{(2a)}}{2}.
\end{equation}
  Then, $(\rho X_k)^{\nph,\pred} = \rho^{\nph,\pred} \, X_k^{\nph,\pred}$.

\item Evolve $(\rho X_k)^{(1)} \rightarrow (\rho X_k)^{(2)}$ using
\begin{equation}
(\rho X_k)^{(2)} = (\rho X_k)^{(1)}
- \dt \left\{ \nabla \cdot \left[\left(\uadvtwo+w_0^{\nph} \eb_r\right)
(\rho X_k)^{\nph,\pred} \right] \right\},
\end{equation}
\begin{equation}
\rho^{(2)} = \sum_k (\rho X_k)^{(2)},
\qquad
X_k^{(2)} = (\rho X_k)^{(2)} / \rho^{(2)}.
\end{equation}

\end{enumerate}

\item Define a radial edge-centered $\etarho^{\nph}$.
\begin{description}
\item For planar geometry,
\begin{eqnarray}
 \etarho^{\nph} &=& {\rm {\bf Avg}} \sum_k \left [\left(\uadvtwo \cdot \eb_r + w_0^{\nph}\right) (\rho X_k)^{\nph,\pred} \right] \nonumber \\
&&- w_0^{\nph} \rho_0^{\nph,\pred},
\end{eqnarray}
\item For spherical geometry, first construct
$\etarho^{{\rm cart},\nph} = [\rho'(\Ub\cdot\eb_r)]^{\nph}$ on Cartesian
  cell centers using:
\begin{equation}
\etarho^{{\rm cart},\nph} = \left[\left(\frac{\rho^{(1)}+\rho^{(2)}}{2}\right)-\left(\frac{\rho_0^n+\rho_0^{(2a)}}{2}\right)\right] \left(\uadvtwo \cdot \eb_r + w_0^{\nph}\right).
\end{equation}
Then,
\begin{equation}
\etarho^{\nph} = {\rm {\bf Avg}}\left(\etarho^{{\rm cart},\nph}\right).
\end{equation}
This gives a radial cell-centered $\etarho^{\nph}$.  To get
$\etarho^{\nph}$ at radial edges, average the two neighboring
cell-centered values.
\end{description}

\item Correct $\rho_0$ by setting $\rho_0^{n+1} =$ {\bf Avg}$(\rho^{(2)})$.

\item Update $p_0$ by calling
{\bf Enforce HSE}$[p_0^n,\rho_0^{n+1}] \rightarrow [p_0^{n+1}]$.

\item Compute $\psi^{\nph}$.
\begin{description}
\item For planar geometry,
\begin{equation}
\psi_j^{\nph} = \frac{1}{2} \left(\eta_{\rho,j-\myhalf}^{\nph}
+ \eta_{\rho,j+\myhalf}^{\nph}\right) g.
\end{equation}
\item For spherical geometry, first compute:
\begin{equation}
\overline{\Gamma_1^{(2)}} = {\rm{\bf Avg}} \left[ \Gamma_1\left(\rho^{(2)}, p_0^{n+1},
X_k^{(2)}\right) \right].
\end{equation}
Then, define $\psi^{\nph}$ using equation (\ref{eq:psi def}):
\begin{eqnarray}
\psi_j^{\nph}
&=& \left(\frac{\overline{\Gamma_1^{(1)}}+\overline{\Gamma_1^{(2)}}}{2}\right)_j \left(\frac{p_0^n+p_0^{n+1}}{2}\right)_j \nonumber \\
&& \left \{ \overline{S_j^{\nph}} - \frac{1}{r_j^2} \left [ \left(r^2 w_0^{\nph}\right)_{j+\myhalf} - \left(r^2 w_0^{\nph}\right)_{j-\myhalf} \right ] \right \}.
\end{eqnarray}
\end{description}

\item Update $(\rho h)_0$ by calling
{\bf Advect Base Enthalpy}$[(\rho h)_0^n, w_0^{\nph}, \psi^{\nph}] \rightarrow [(\rho h)_0^{n+1}]$.

\item Update the enthalpy.  This step is identical to {\bf Step 4H} except we use
  the updated values $w_0^{\nph}, \uadvtwo, \rho_0^{n+1}, (\rho h)_0^{n+1}, p_0^{n+\myhalf}$,
  and $\psi^{n+\myhalf}$ rather than\\
  $w_0^{\nph,\star}, \uadvone, \rho_0^{n+1,\star}, (\rho h)_0^{n+1,\star}, p_0^n$,
  and $\psi^{n+\myhalf,\star}$.  In particular:

\begin{enumerate}
\renewcommand{\labelenumii}{{\bf \roman{enumii}}.}

\item Compute the time-centered enthalpy edge state, $(\rho h)^{\nph,\pred},$
  for the conservative update of $(\rho h)^{(1)}$.  We use equation
  (\ref{eq:Perturbational Enthalpy}) to predict
  $(\rho h)' = (\rho h)^{(1)} - (\rho h)_0^n$ to time-centered edges
  with $\Ub = \uadvtwo+w_0^{\nph} \eb_r$,
  yielding $(\rho h)^{'\nph,\pred}$.
  We convert the perturbational enthalpy to a full state enthalpy using
\begin{equation}
(\rho h)^{\nph,\pred} = (\rho h)^{'\nph,\pred} + \frac{(\rho h)_0^n + (\rho h)_0^{n+1}}{2}.
\end{equation}

\item Evolve $(\rho h)^{(1)} \rightarrow (\rho h)^{(2)}$.
\begin{description}
\item For planar geometry,
\begin{eqnarray}
(\rho h)^{(2)}
&=& (\rho h)^{(1)} - \dt \left\{ \nabla \cdot \left[ \left(\uadvtwo+w_0^{\nph} \eb_r\right)  (\rho h)^{\nph,\pred} \right] \right\} \nonumber \\
&& + \dt \left(\uadvtwo \cdot \eb_r\right) \left(\frac{\partial p_0}{\partial r} \right)^\nph + \dt \psi^{\nph},
\end{eqnarray}

\item For spherical geometry,
\begin{eqnarray}
(\rho h)^{(2)}
&=& (\rho h)^{(1)} - \dt \left\{ \nabla \cdot \left[ \left(\uadvtwo+w_0^{\nph} \eb_r\right)  (\rho h)^{\nph,\pred} \right] \right\} \nonumber \\
&& + \dt \left[ \nabla \cdot \left (\uadvtwo p_0^{\nph} \right ) - p_0^{\nph} \nabla \cdot \uadvtwo \right] + \dt \psi^{\nph},\nonumber \\
\end{eqnarray}
\end{description}
where $p_0^\nph$ is defined as $p_0^\nph = (p_0^n+p_0^{n+1})/2$.

\end{enumerate}

Then, for each Cartesian cell where $\rho^{(2)} < \rho_\mathrm{cutoff}$, we recompute enthalpy using
\begin{equation}
(\rho h)^{(2)} = \rho^{(2)}h\left(\rho^{(2)},p_0^{n+1},X_k^{(2)}\right).
\end{equation}

\item Update the temperature using the equation of state:
$T^{(2)} =
   T(\rho^{(2)}, h^{(2)}, X_k^{(2)})$ (planar geometry) or
$T^{(2)} =
   T(\rho^{(2)}, p_0^{n+1}, X_k^{(2)})$ (spherical geometry).
\end{enumerate}
Again, the actual inputs depend of \runparam{use\_tfromp}.

%--------------------------------------------------------------------------
% STEP 9
%--------------------------------------------------------------------------
\item[Step 9.] {\em React the full state through a second time interval of $\dt / 2.$}

Call {\bf React State}$[\rho^{(2)},(\rho h)^{(2)}, X_k^{(2)},T^{(2)}, (\rho\Hext)^{(2)}, p_0^{n+1}]$\\
\phantom{ }\hfill$\rightarrow [\rho^{n+1}, (\rho h)^{n+1}, X_k^{n+1}, T^{n+1}, (\rho \omegadot_k)^{(2)}, (\rho \Hnuc)^{(2)} ].$

%--------------------------------------------------------------------------
% STEP 10
%--------------------------------------------------------------------------
\item[Step 10.] {\em Define the new time expansion, $S^{n+1}$, and $\overline{\Gamma_1^{n+1}}$.}

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \Alph{enumi}}}
\item Define
\begin{equation}
  S^{n+1} =  -\sigma  \sum_k  \xi_k (\omegadot_k)^{(2)}  + \sigma \Hnuc^{(2)} +
  \frac{1}{\rho^{n+1} p_\rho} \sum_k p_{X_k}  ({\omegadot}_k)^{(2)}
   + \sigma \Hext^{(2)},
\end{equation}
where $(\omegadot_k)^{(2)} = (\rho \omegadot_k)^{(2)} / \rho^{(2)}$
and the thermodynamic quantities are defined using $\rho^{n+1}$,
$X_k^{n+1}$, and $T^{n+1}$ as inputs to the equation of state.
If we are doing thermal diffusion (\runparam{use\_thermal\_diffusion}{\tt = T})
then we also include the diffusive term in $S$.
Then, compute
\begin{equation}
\overline{S^{n+1}} = {\mathrm{\bf Avg}} (S^{n+1}).
\end{equation}

\item Define
\begin{equation}
\overline{\Gamma_1^{n+1}} = {\rm{\bf Avg}}\left[\Gamma_1\left(\rho^{n+1}, p_0^{n+1},
X_k^{n+1}\right) \right].
\end{equation}

\end{enumerate}

%--------------------------------------------------------------------------
% STEP 11
%--------------------------------------------------------------------------
\item[Step 11.] {\em Update the velocity}.

First, we compute the time-centered edge velocities, $\Ubt^{\nph,\pred}$.
Then, we define
\begin{equation}
\rho^\nph = \frac{\rho^n + \rho^{n+1}}{2}, \qquad \rho_0^\nph = \frac{\rho_0^n + \rho_0^{n+1}}{2}.
\end{equation}
We update the velocity field $\Ubt^n$ to $\Ubt^{n+1,\dagger}$ by discretizing
equation (\ref{eq:flow:utildeupd}) as
\begin{eqnarray}
\Ubt^{n+1,\dagger}
&=& \Ubt^n - \dt \left[\left(\uadvtwo+ w_0^{\nph} \eb_r\right) \cdot \nabla \Ubt^{\nph,\pred} \right] \nonumber \\
&&- \dt \left(\uadvtwo \cdot \eb_r\right)  \left(\frac{\partial w_0}{\partial r} \right)^\nph \eb_r \nonumber \\
&& + \dt \left[ - \frac{\beta_0^\nph}{\rho^\nph} \mathbf{G} \left ( \frac{\pi}{\beta_0}\right)^\nmh + \left(\frac{\beta_0}{\rho_0}\frac{\partial(\pi_0/\beta_0)}{\partial r}\right)^n \eb_r - \frac{\left(\rho^\nph-\rho_0^\nph\right)}{\rho^\nph} g^{\nph} \eb_r \right],\nonumber \\
\end{eqnarray}
where $\mathbf{G}$ approximates a cell-centered gradient from nodal
data.  Again, the $\dagger$ superscript refers
to the fact that the updated velocity does not satisfy the divergence
constraint.

Finally, we use an approximate nodal projection to define $\Ubt^{n+1}$
from $\Ubt^{n+1,\dagger},$  such that $\Ubt^{n+1}$ approximately
satisfies
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph} \Ubt^{n+1} \right)
= \beta_0^{\nph} \left(S^{n+1} - \overline{S^{n+1}} \right),
\end{equation}
where $\beta_0^{\nph}$ is defined as
\begin{equation}
\beta_0^{\nph} = \frac{\beta_0^n + \beta_0^{n+1}}{2}; \qquad
\beta_0^{n+1} = \beta \left(\rho_0^{n+1}, p_0^{n+1}, \overline{\Gamma_1^{n+1}}, g^{n+1}\right).
\end{equation}
As part of the projection we also define the new-time perturbational pressure,
$\pi^\nph.$  This projection necessarily differs from the MAC projection used in
{\bf Step 3} and {\bf Step 7} because the velocities in those steps are defined
on edges and $\Ubt^{n+1}$ is defined at cell centers, requiring different divergence
and gradient operators.  Details of the approximate projection are given in Paper III.

%--------------------------------------------------------------------------
% STEP 12
%--------------------------------------------------------------------------
\item[Step 12.] {\em Compute a new $\dt.$}

Compute $\dt$ for the next time step with the procedure described in
\S 3.4 of Paper III using $w_0$ as computed in {\bf Step 6} and $\Ubt^{n+1}$
as computed in {\bf Step 11}.

\end{description}

\noindent This completes one step of the algorithm.

\subsection{Volume Discrepancy Changes}

Chapter~\ref{ch:volume} describes the reasoning behind the volume discrepancy
term---a forcing term added to the constraint equation to bring us back to
the equation of state.  This addition of this term (enabled with \runparam{dpdt\_factor}{\tt = T}) modifies our
equation set in the following way:

\begin{itemize}
\item In {\bf Step 2B}, to compute $w_0$, we need to account for the volume discrepancy
term by first defining $p_{\rm EOS}^n = \overline{p(\rho,h,X_k)^n}$, and then using:
\begin{equation}
\frac{\partial w_0^{\nph,\star}}{\partial r} = \overline{S^{\nph,\star\star}} - \frac{1}{\overline{\Gamma_1^n}p_0^n}\psi^{\nmh} - \underbrace{\frac{f}{\overline{\Gamma_1^n}p_0^n}\left(\frac{p_0^n-\overline{p_{\rm EOS}^n}}{\Delta t}\right)}_{\delta\chi_{w_0}}.
\end{equation}
\item In {\bf Step 3}, the MAC projection should account for the volume discrepancy term:
\begin{equation}
\nabla \cdot \left(\beta_0^n \uadvone\right) =
\beta_0^n \left[ \left(S^{\nph,\star\star} - \overline{S^{\nph,\star\star}}\right)
+ \underbrace{\frac{f}{\gammabar^n p_0^n}
\left(\frac{p_{\rm EOS}^n - \overline{p_{\rm EOS}^n}}{\Delta t^n}\right)}_{\delta\chi}\right].
\end{equation}
\item In {\bf Step 6B}, to compute $w_0$, we need to account for the volume discrepancy
term by first defining $p_{\rm EOS}^{n+1,\star} = p(\rho,h,X_k)^{n+1,\star}$,
$\overline{\Gamma_1^{\nph,\star}} = (\overline{\Gamma_1^{n}}+\overline{\Gamma_1^{n+1,\star}})/2$,
and $p^{\nph,\star} = (p^{n}+p^{n+1,\star})/2$, and then using:
\begin{equation}
\frac{\partial w_0^{\nph}}{\partial r} = \overline{S^{\nph,\star}} - \frac{1}{\overline{\Gamma_1^{\nph,\star}}p_0^{\nph,\star}}\psi^{\nph,\star} - \frac{f}{\overline{\Gamma_1^{n+1,\star}}p_0^{n+1,\star}}\left(\frac{p_0^{n+1,\star}-\overline{p_{\rm EOS}^{n+1,\star}}}{\Delta t}\right) - \delta\chi_{w_0}
\end{equation}
\item In {\bf Step 7}, the MAC projection should account for the volume discrepancy term:
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph,\star} \uadvtwo\right) = \beta_0^{\nph,\star}\left[\left(S^{\nph,\star} - \overline{S^{\nph,\star}}\right) + \frac{f}{\overline{\Gamma_1^{n+1,\star}} p_0^{n+1,\star}} \left(\frac{p_{\rm EOS}^{n+1,\star} - \overline{p_{\rm EOS}^{n+1,\star}}}{\Delta t^n}\right) + \delta\chi\right],
\end{equation}
where $p(\rho,h,X_k)^{\nph,\star} = \left[p(\rho,h,X_k)^n +p(\rho,h,X_k)^{n+1,\star}\right]/2$.
\item In {\bf Step 11}, the approximate projection should account for the volume
discrepancy term:
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph} \Ubt^{n+1} \right)  = \beta_0^{\nph}\left\{  \left(S^{n+1} - \overline{S^{n+1}} \right)
+ \frac{f}{\overline{\Gamma_1^{n+1}} p_0^{n+1}}
\left[\frac{p(\rho,h,X_k)^{n+1} - \overline{p(\rho,h,X_k)^{n+1}}}{\Delta t^n}\right]\right\}.
\end{equation}
\end{itemize}

%-----------------------------------------------------------------------------
\subsection{\label{sec:flow:gamma1vary} $\Gamma_1$ Variation Changes}

The constraint we derive from requiring the pressure to be close to
the background hydrostatic pressure takes the form:
\begin{equation}
\nablab \cdotb \Ub + \frac{1}{\Gamma_1 p_0} \frac{Dp_0}{Dt} = S \enskip .
\end{equation}
The default \maestro\ algorithm replaces $\Gamma_1$ with $\gammabar$,
allowing us to write this as a divergence constraint.  In paper~III,
we explored the effects of localized variations in $\Gamma_1$ by
writing $\Gamma_1 = \gammabar + \delta \Gamma_1$.  This gives us:
\begin{equation}
\nablab \cdotb \Ub + \frac{1}{(\gammabar + \delta \Gamma_1) \; p_0}
  \Ub \cdotb \nablab p_0 = S - \frac{1}{(\gammabar + \delta \Gamma_1) \; p_0}
  \frac{\partial p_0}{\partial t} \enskip .
\end{equation}

Assuming that $\delta \Gamma_1 \ll \gammabar$, we then
have
\begin{equation}
\nablab \cdotb \Ub + \frac{1}{\gammabar p_0} \Ub \cdotb \nablab p_0
 \left [ 1 - \frac{\delta\Gamma_1}{\gammabar} + \frac{(\delta\Gamma_1)^2}{\gammabar^2} \right ]
= S - \frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial t}
 \left [ 1 - \frac{\delta\Gamma_1}{\gammabar} + \frac{(\delta\Gamma_1)^2}{\gammabar^2} \right ] \\
\end{equation}
Grouping by order of the correction, we have
\begin{equation}
\nablab \cdotb \Ub + \frac{1}{\gammabar p_0} \Ub \cdotb \nablab p_0
= S - \frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial t} +
\underbrace{\frac{\delta \Gamma_1}{\gammabar^2 p_0}
            \left [\frac{\partial p_0}{\partial t} + \Ub \cdotb \nablab p_0\right ]}_{\mbox{first order corrections}}
 -
  \underbrace{\frac{(\delta\Gamma_1)^2}{\gammabar^3 p_0}
            \left [ \frac{\partial p_0}{\partial t} + \Ub \cdotb \nablab p_0 \right ]}_{\mbox{second order corrections}} \enskip , \label{eq:gammafull}
\end{equation}

\subsubsection{Keeping to First Order in $\delta\Gamma_1$}
The base state evolution equation is the average of Eq.~\ref{eq:gammafull} over a layer
\begin{equation}
\nablab \cdot w_0 \er + \frac{1}{\gammabar p_0} w_0 \er \cdotb \nablab p_0 =
\Sbar - \frac{1}{\gammabar p_0} \frac{\partial p_0}{\partial t} + \overline{
  \left ( \frac{\delta \Gamma_1}{\gammabar^2 p_0} \Ubt \cdotb \nablab p_0
  \right ) } \enskip .
\end{equation}
where we see that the $[\delta \Gamma_1/(\Gamma_1^2 p_0)] \partial p_0/\partial t$ terms averages to zero, since the average of $\delta\Gamma_1$ term is zero.
Subtracting this from equation (\ref{eq:gammafull}), we have
\begin{equation}
\nablab \cdotb \Ubt + \frac{1}{\gammabar p_0} \Ubt \cdotb \nablab p_0 = S -
\Sbar + \frac{\delta \Gamma_1}{\gammabar^2 p_0} \left (\psi + \Ubt \cdotb
\nablab p_0 \right ) - \overline{ \left ( \frac{\delta \Gamma_1}{\gammabar^2 p_0} \Ubt
\cdotb \nablab p_0 \right ) } \enskip .
\end{equation}

These can be written more compactly as:
\begin{equation}
\frac{\partial w_0}{\partial r} = \Sbar -\frac{1}{\gammabar p_0}\psi +
\overline{ \left ( \frac{\delta \Gamma_1}{\gammabar^2 p_0} \Ubt \cdotb \nablab
  p_0 \right ) } \enskip , \label{eq:base_w0_with_dgamma1}
\end{equation}
for plane-parallel geometries (analogous to
Eq.~\ref{eq:flow:dw0dr_planar}), and
\begin{equation}
\nablab \cdotb (\beta_0 \Ubt) = \beta_0 \left [ S - \Sbar + \frac{\delta
    \Gamma_1}{\gammabar^2 p_0} \psi + \frac{\delta \Gamma_1}{\gammabar^2 p_0}
  \Ubt \cdotb \nablab p_0 - \overline{ \left ( \frac{\delta
      \Gamma_1}{\gammabar^2 p_0} \Ubt \cdotb \nablab p_0 \right ) } ~ \right ]
 \enskip ,  \label{eq:constraint_with_delta_gamma}
\end{equation}

This constraint is not in a form that can be projected.  To solve this
form, we need to use a lagged $\Ubt$ in the righthand side.

This change comes into \maestro\ in a variety of steps, summarized here.
To enable this portion of the algorithm, set \runparam{use\_delta\_gamma1\_term} {\tt = T}.

\begin{itemize}

\item In {\bf Step 3}, we are doing the ``predictor'' portion of the
\maestro\ algorithm, getting the MAC velocity that satisfies the constraint,
so we do not try to incorporate the $\delta \Gamma_1$ effect.  We set
all the $\delta \Gamma_1$ terms in
Eq.~\ref{eq:constraint_with_delta_gamma} to zero.

\item In {\bf Step 6}, we are computing the new time-centered source,
$S^{\nph,\star}$ and the base state velocity, $w_0^\nph$.  Now we can
incorporate the $\delta \Gamma_1$ effect.  First we construct:
\begin{equation}
\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0 \approx
   \frac{\Gamma_1^{n+1,\star} - \overline{\Gamma_1^{n+1,\star}}}
         {{\overline{\Gamma_1^{n+1,\star}}}^2} \frac{1}{p_0^n} \Ubt^n \cdotb \nablab p_0^n
\end{equation}
Then we call {\bf average} to construct the lateral average of this
\begin{equation}
\overline{\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0} =
   \mathbf{Avg} \left (\frac{\Gamma_1^{n+1,\star} - \overline{\Gamma_1^{n+1,\star}}}
         {{\overline{\Gamma_1^{n+1,\star}}}^2} \frac{1}{p_0^n} \Ubt^n \cdotb \nablab p_0^n \right )
\end{equation}

Since the average of this is needed in advancing $w_0$, we modify $\overline{S}$
to include this average:
\begin{equation}
\overline{S^{\nph,\star}} \leftarrow \overline{S^{\nph,\star}} +
   \overline{\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0}
\end{equation}

\item In {\bf Step 7}, we now include the $\delta \Gamma_1$ term in the righthand
side for the constraint by solving:
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph,\star} \uadvtwo\right) =
\beta_0^{\nph,\star}\left( S^{\nph,\star} - \overline{S^{\nph,\star}} +
   \frac{\Gamma_1^{n+1,\star} - \overline{\Gamma_1^{n+1,\star}}}
         {{\overline{\Gamma_1^{n+1,\star}}}^2} \frac{1}{p_0^n}
         (\psi^{\nph,\star} + \Ubt^n \cdotb \nablab p_0^n)
\right)
\end{equation}
We note that this includes the average of the correction term as shown
in Eq.~\ref{eq:constraint_with_delta_gamma} because we modified
$\bar{S}$ to include this already.

\item In {\bf Step 10}, we do a construction much like that done in {\bf Step 6},
but with the time-centerings of some of the quantities changed.
First we construct:
\begin{equation}
\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0 \approx
   \frac{\Gamma_1^{n+1} - \overline{\Gamma_1^{n+1}}}
         {{\overline{\Gamma_1^{n+1}}}^2} \frac{1}{p_0^{n+1}} \Ubt^n \cdotb \nablab p_0^{n+1}
\end{equation}
Then we call {\bf average} to construct the lateral average of this
\begin{equation}
\overline{\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0} =
   \mathbf{Avg} \left (\frac{\Gamma_1^{n+1} - \overline{\Gamma_1^{n+1}}}
         {{\overline{\Gamma_1^{n+1}}}^2} \frac{1}{p_0^{n+1}} \Ubt^n \cdotb \nablab p_0^{n+1} \right )
\end{equation}

Again we modify $\overline{S}$ to include this average:
\begin{equation}
\overline{S^{n+1}} \leftarrow \overline{S^{n+1}} +
   \overline{\frac{\delta \Gamma_1}{\Gamma_1^2 p_0} \Ubt \cdotb \nablab p_0}
\end{equation}

\item In {\bf Step 11}, we modify the source of the constraint to include the
$\delta \Gamma_1$ information.  In particular, we solve:
\begin{equation}
\nabla \cdot \left(\beta_0^{\nph} \Ubt^{n+1} \right)
= \beta_0^{\nph} \left(S^{n+1} - \overline{S^{n+1}} +
   \frac{\Gamma_1^{n+1} - \overline{\Gamma_1^{n+1}}}
         {{\overline{\Gamma_1^{n+1}}}^2} \frac{1}{p_0^{n+1}}
         (\psi^{\nph} + \Ubt^n \cdotb \nablab p_0^{n+1})
\right)
\end{equation}

\end{itemize}

%-----------------------------------------------------------------------------
\subsection{Thermal Diffusion Changes}

\label{sec:flow:diffusion}

Thermal diffusion was introduced in the XRB paper~\cite{xrb}.  This
introduces a new term to $S$ as well as the enthalpy equation.
Treating the enthalpy equation now requires a parabolic solve.  We describe
that process here.

Immediately after {\bf Step 4H}, diffuse the enthalpy through
a time interval of $\dt$.  First, define $(\rho h)^{(1a),\star} = (\rho h)^{(2),\star}$.
We recompute $(\rho h)^{(2),\star}$ to account for thermal diffusion.  Here we begin
with the enthalpy equation, but consider only the
diffusion terms,
\begin{equation}
\frac{\partial (\rho h)}{\partial t} = \nabla\cdot\kth\nabla T.
  \end{equation}
We can recast this as an enthalpy-diffusion equation by applying the
chain-rule to $h(p_0,T,X_k)$,
\begin{equation}
\nabla h = h_p \nabla p_0 + c_p \nabla T + \sum_k \xi_k \nabla X_k \enskip ,
\end{equation}
giving
\begin{equation}
  \frac{\partial (\rho h)}{\partial t}  =
 \nabla\cdot \frac{\kth}{c_p}\nabla h -
 \sum_k \nabla\cdot \frac{\xi_k \kth}{c_p}\nabla X_k -
 \nabla\cdot \frac{h_p \kth}{c_p}\nabla p_0.
  \end{equation}
Compute $\kth^{(1)}, c_p^{(1)}$, and $\xi_k^{(1)}$ from $\rho^{(1)}, T^{(1)}$, and $X_k^{(1)}$ as inputs to the equation of state.  The update is given by
\begin{eqnarray}
(\rho h)^{(2),\star} &=& (\rho h)^{(1a),\star} + \frac{\dt}{2}\nabla\cdot\left(\frac{\kth^{(1)}}{c_p^{(1)}}\nabla h^{(2),\star} + \frac{\kth^{(1)}}{c_p^{(1)}}\nabla h^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\sum_k\nabla\cdot\left(\frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(2),\star} + \frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\nabla\cdot\left(\frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n+1,\star} + \frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n}\right),
\end{eqnarray}
which is numerically implemented as a diffusion equation for $h^{(2),\star}$,
\begin{eqnarray}
\left(\rho^{(2),\star} - \frac{\dt}{2}\nabla\cdot\frac{\kth^{(1)}}{c_p^{(1)}}\nabla\right)h^{(2),\star} &=& (\rho h)^{(1a),\star} + \frac{\dt}{2}\nabla\cdot\frac{\kth^{(1)}}{c_p^{(1)}}\nabla h^{(1)}\nonumber\\
&&- \frac{\dt}{2}\sum_k\nabla\cdot\left(\frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(2),\star} + \frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\nabla\cdot\left(\frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n+1,\star} + \frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n}\right),
\end{eqnarray}

Immediately after {\bf Step 8H}, diffuse the enthalpy through a time interval of
$\dt$.  First, define $(\rho h)^{(1a)} = (\rho h)^{(2)}$.  We recompute $(\rho h)^{(2)}$ to
account for thermal diffusion.  Compute $\kth^{(2),\star}, c_p^{(2),\star}$, and
$\xi_k^{(2),\star}$, from $\rho^{(2),\star}, T^{(2),\star}$, and $X_k^{(2),\star}$ as inputs to
the equation of state.  The update is given by
\begin{eqnarray}
(\rho h)^{(2)} &=& (\rho h)^{(1a)} + \frac{\dt}{2}\nabla\cdot\left(\frac{\kth^{(2),\star}}{c_p^{(2),\star}}\nabla h^{(2)} + \frac{\kth^{(1)}}{c_p^{(1)}}\nabla h^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\sum_k\nabla\cdot\left(\frac{\xi_k^{(2),\star}\kth^{(2),\star}}{c_p^{(2),\star}}\nabla X_k^{(2)} + \frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\nabla\cdot\left(\frac{h_p^{(2),\star}\kth^{(2),\star}}{c_p^{(2),\star}}\nabla p_0^{n+1} + \frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n}\right),
\end{eqnarray}
which is numerically implemented as a diffusion equation for $h^{(2)}$.
\begin{eqnarray}
\left(\rho^{(2)} - \frac{\dt}{2}\nabla\cdot\frac{\kth^{(2),\star}}{c_p^{(2),\star}}\nabla\right)h^{(2)} &=& (\rho h)^{(1a)} + \frac{\dt}{2}\nabla\cdot\frac{\kth^{(1)}}{c_p^{(1)}}\nabla h^{(1)}\nonumber\\
&&- \frac{\dt}{2}\sum_k\nabla\cdot\left(\frac{\xi_k^{(2),\star}\kth^{(2),\star}}{c_p^{(2),\star}}\nabla X_k^{(2)} + \frac{\xi_k^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla X_k^{(1)}\right)\nonumber\\
&&- \frac{\dt}{2}\nabla\cdot\left(\frac{h_p^{(2),\star}\kth^{(2),\star}}{c_p^{(2),\star}}\nabla p_0^{n+1} + \frac{h_p^{(1)}\kth^{(1)}}{c_p^{(1)}}\nabla p_0^{n}\right),
\end{eqnarray}

%-----------------------------------------------------------------------------
% graphical flowcharts
%-----------------------------------------------------------------------------
\begin{figure}[tb]
\centering
\includegraphics[scale=0.65]{\flowfigpath/flowchart}
\caption[Graphical flowchart of \maestro]
  {\label{Fig:flowchart} A flowchart of the algorithm.  The
  thermodynamic state variables, base state variables, and local velocity are
  indicated in each step.  Red text indicates that quantity was
  updated during that step.  The predictor-corrector steps are
  outlined by the dotted box.  The blue text indicates state
  variables that are the same in {\bf Step 6} as they are in
  {\bf Step 2}, i.e., they are unchanged by the predictor steps.
  The diffusion steps (4a and 8a) are optional, depending on
  \runparam{use\_thermal\_diffusion}.}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[tb]
\centering
\includegraphics[scale=0.75]{\flowfigpath/flowchart_4_8}
\caption[Graphical flowchart of the density and enthalpy update steps]
{\label{Fig:flowchart48} A flowchart for {\bf Steps 4} and {\bf 8}.
  The thermodynamic state variables and base state variables are
  indicated in each step.  Red text indicates that quantity was
  updated during that step.  Note, for {\bf Step 4}, the updated
  quantities should also have a $\star$ superscript, e.g., {\bf Step
    8I} defines $T^{(2)}$ while {\bf Step 4I} defines $T^{(2),\star}$
  .}
\end{figure}

%-----------------------------------------------------------------------------
% Initialization
%-----------------------------------------------------------------------------

\section{Initialization}\label{Sec:Initialization}

\label{sec:flow:initialization}

We start each calculation with user-specified initial values for
$\rho$, $X_k$ and $T,$ as well as an initial background state.  In
order for the low Mach number assumption to hold, the initial data
must be thermodynamically consistent with the initial background
state.  In addition, the initial velocity field must satisfy an
initial approximation to the divergence constraint.  We use an iterative
procedure to compute both an initial right-hand-side for the
constraint equation and an initial velocity field that satisfies
the constraint.  The user specifies the number of iterations,
$N_{\rm iters}^{S},$ in this first step of the initialization procedure.

The initial perturbational pressure also needs to be determined for
use in {\bf Steps 3}, {\bf 7}, and {\bf 11}.
This is done through a second iterative procedure which follows the
time advancement algorithm as described in {\bf Steps 1-11} in
\S \ref{Sec:Time Advancement Algorithm}.
The user specifies the number of iterations,
$N_{\rm iters}^{\pi},$ in this second step of the initialization procedure.
The details for both iterations are given below.\\

%--------------------------------------------------------------------------
% STEP 0
%--------------------------------------------------------------------------
\noindent {\bf Step 0.} {\em Initialization}

First, we need to construct approximations to $S^0, w_0^{-\myhalf}, \Delta t^0$,
and $\Ub^0$.  Start with initial data $X_k^{\initp}, \rho^{\initp},$ $T^{\initp},$
an initial base state, and an initial guess for the velocity, $\Ub^{\initp}$.
Set $w_0^0 = 0$ as an initial approximation.  Use the equation of state to
determine $(\rho h)^{\initp}$.  Compute $\beta_0^0$ as a function of
the initial data.  The next part of the initialization process
proceeds as follows.

\begin{enumerate}
\renewcommand{\theenumi}{{\bf \alph{enumi}}}
\renewcommand{\labelenumii}{\roman{enumii}.}

\item {\em Initial Projection}: if \runparam{do\_initial\_projection} {\tt = T}, then we
   first project the velocity field with $\rho = 1$ and $\beta_0^0$.
   The initial projection does not see reactions
   or external heating, and thus we set $\dot\omega = \Hnuc = \Hext = 0$ in $S$.
   The reason for ignoring reactions and heating is that we need some kind of
   time scale over which to compute the effect of reactions, but we first
   need an estimate of the velocity
   field in order to derive the time step that will be used as a time scale.
   The elliptic equation we solve is
   \begin{equation}
   \nabla \cdot \beta_0^0 \nabla \phi = \underbrace{\beta_0^0(S - \Sbar)}_\mathrm{0~ except~ for~ diffusion} - \nabla \cdot (\beta_0^0 \Ub^{\initp})
   \end{equation}
   This $\phi$ is then used to correct the velocity field to obtain $\Ub^{0,0}$.
   If {\tt do\_initial\_projection = F}, set $\Ub^{0,0} = \Ub^{\initp}$.

\item {\em ``Divu'' iterations}: Next we do \runparam{init\_divu\_iter} iterations
  to project the velocity field using a constraint that sees reactions
  and external heating.
  The initial timestep estimate is provided by \code{firstdt} and
  $\Ub^{0,0}$, to allow us to compute the effect of reactions over $\Delta t/2$.

  {\bf Do} {$\nu = 1,...,N_{\rm iters}^{S}$.}
  \begin{enumerate}

  \item Estimate $\Delta t^\nu$ using $\Ub^{0,\nu-1}$ and $w_0^{\nu-1}.$

  \item {\bf React State}$[ \rho^{\initp},(\rho h)^{\initp}, X_k^{\initp}, T^{\initp},
(\rho^{\initp} \Hext), p_0^{\initp}] \rightarrow [\rho^{\outp}, (\rho h)^{\outp},
X_k^{\outp}, T^{\outp}, (\rho \omegadot_k)^{0,\nu} ].$

  \item Compute $S^{0,\nu}$ from equation (\ref{eq:defineS})
        using $(\rho \omegadot_k)^{0,\nu}$ and the initial data.

  \item Compute $\overline{S^{0,\nu}} = {\mathrm{\bf Avg}} (S^{0,\nu}).$

  \item Compute $w_0^{\nu}$ as in {\bf Step 2B} using $\overline{S^{0,\nu}}$ and $\psi=0$.

  \item Project $\Ub^{0,\nu-1}$ using $\beta_0^0$ and
        $(S^{0,\nu} - \overline{S^{0,\nu}})$ as the source term.
        This yields $\Ub^{0,\nu}.$  In this projection, again the density is
        set to 1, and the elliptic equation we solve is:

   \begin{equation}
   \nabla \cdot \beta_0^0 \nabla \phi = \beta_0 (S - \Sbar)- \nabla \cdot (\beta_0^0 \Ub^{0,\nu-1})
   \end{equation}

  \end{enumerate}

  {\bf End do.}

  Define $S^0 = S^{0,N_{\rm iters}^S}$, $w_0^{-\myhalf} = w_0^{N_{\rm iters}^S}$,
$\dt^0 = \Delta t^{N_{\rm iters}^S},$ and $\Ub^0 = \Ub^{0,N_{\rm iters}^S}.$

\end{enumerate}

Next, we need to construct approximations to $\etarho^{-\myhalf}, \psi^{-\myhalf}, S^1$,
and $\pi^{-\myhalf}$.  As initial approximations, set
$\etarho^{-\myhalf}=0, \psi^{-\myhalf}=0, S^{1,0}=S^0$, and $\pi^{-\myhalf}=0.$
\begin{enumerate}
\renewcommand{\theenumi}{{\bf \alph{enumi}}}
\renewcommand{\labelenumii}{\roman{enumii}.}
\addtocounter{enumi}{2}
\item {\it Pressure iterations}: Here we do \runparam{init\_iter} iterations to get an
  approximation for the lagged pressure:

  {\bf Do} {$\nu = 1,...,N_{\rm iters}^{\pi}$.}
  \begin{enumerate}
  \item Perform {\bf Steps 1-11} as described above, using
    $S^{\myhalf,\star\star} = (S^0 + S^{1,\nu-1})/2$ in {\bf Step 2} as described.
    The only other difference in the time advancement is that in {\bf Step 11}
    we define ${\bf V} = (\Ubt^{1,\star} - \Ubt^0)$ and solve
    \begin{equation}  L_\beta^\rho \phi = D \left ( \beta_0^{\myhalf} {\bf V} \right) - \beta_0^{\myhalf} \left[ \left(S^{1}-\overline{S^{1}}\right) - \left(S^{0}-\overline{S^{0}}\right) \right] \enskip .
    \end{equation}
    (The motivation for this form of the projection in the initial pressure iterations
    is discussed in \cite{almgren:bell:crutchfield}.)
      We discard the new velocity resulting from this, but keep the new
      value for $\pi^{\myhalf} = \pi^{-\myhalf} + (1 / \dt) \; \phi.$
      These steps also yield new scalar data at time $\dt,$ which
      we discard, and new values for $\etarho^{\myhalf}$ ({\bf Step 8C}),
      $\psi^{\myhalf}$ ({\bf Step 8F}),
      $S^{1,\nu}$ ({\bf Step 10A}), and $\pi^{\myhalf}$ ({\bf Step 11}), which we keep.
    \item Set $\pi^{-\myhalf} = \pi^{\myhalf}$, $\etarho^{-\myhalf} = \etarho^{\myhalf}$,
      and $\psi^{-\myhalf} = \psi^{\myhalf}$.
    \end{enumerate}

    {\bf End do.}

    Finally, we define $S^1 = S^{1,N_{\rm iters}^\pi}.$

  \end{enumerate}

The tolerances for these elliptic solves are described in \S~\ref{sec:mgtol}.

%-----------------------------------------------------------------------------
% changes
%-----------------------------------------------------------------------------

\section{Changes from Earlier Implementations}
\subsection{Changes Between Paper 3 and Paper 4}
\begin{enumerate}
\item We defined the mapping of data between a 1D radial array and the 3D Cartesian
grid for spherical problems (which we improve upon in the multilevel paper).
\item We update $T$ after the call to {\bf React State}.
\item We have created a \runparam{burning\_cutoff\_density}, where the burning does
not happen below this density.  It is presently set to \runparam{base\_cutoff\_density}.
\item Use corner coupling in advection.
\item We have an option, controlled by \runparam{use\_tfromp}, to update temperature
using $T=T(\rho,X_k,p_0)$ rather than $T=T(\rho,h,X_k)$.  The former completely
decouples enthalpy from our system.  For spherical problems, we use
{\tt use\_tfromp = TRUE}, for planar problems, we use {\tt use\_tfromp = FALSE}.
\item For spherical problems, we have changed the discretization of
$\Ubt\cdot\nabla p_0$ in the enthalpy update to
$\nabla\cdot(\Ubt p_0) - p_0\nabla\cdot\Ubt$.
\item In paper III we discretized the enthalpy evolution equation in
terms of $T$.  Since then we have discovered that
discretizing the enthalpy evolution in perturbational form, $(\rho h)'$,
leads to better numerical properties.  We use \runparam{enthalpy\_pred\_type}{\tt = 1}.
This is more like paper II.
\item We have turned off the evolution of $h$ above the atmosphere and instead
compute $h$ with the EOS using \runparam{do\_eos\_h\_above\_cutoff} {\tt = T}.
\end{enumerate}

\subsection{Changes Between Paper 4 and the Multilevel Paper}
See the multilevel paper for the latest.

\subsection{Changes Between the Multilevel Paper and Paper 5~\cite{wdconvect}}
\begin{enumerate}
\item Added rotation.
\end{enumerate}

\subsection{Changes Between Paper 5 and the XRB Paper}
\begin{enumerate}
\item We have added thermal diffusion, controlled by \runparam{use\_thermal\_diffusion},
\runparam{temp\_diffusion\_formulation}, and \runparam{thermal\_diffusion\_type}.
\item We added the volume discrepancy term to the velocity constraint equation,
controlled by the input parameter, \runparam{dpdt\_factor}.
\item For certain problems, we need to set \runparam{do\_eos\_h\_above\_cutoff} {\tt = F}
to prevent large, unphysical velocities from appearing near the edge of the star.
\end{enumerate}

\subsection{Changes Since the XRB Paper}

\begin{enumerate}

\item We switched to the new form of the momentum equation to
Eq.~\ref{eq:flow:newmomentum} to conserve the low-Mach number form of
energy.

\item We changed the form of the volume discrepancy term to get better
agreement between the two temperatures.

\end{enumerate}

%-----------------------------------------------------------------------------
% Future Considerations
%-----------------------------------------------------------------------------

\section{Future Considerations}

\begin{itemize}
\item Should we use a predictor-corrector for updating the full-state density?
Specifically, after calling {\bf Correct Base}, should we do a full-state density
advance and {\bf Correct Base} using the more accurate estimate of $\rho_0^{n+1}$?
\item We are still exploring the effects of \runparam{use\_tfromp}  {\tt = F} for spherical
problems.  We would eventually like to run in this mode, but $T=T(\rho,X_k,p_0)$
and $T=T(\rho,h,X_k)$ drift away from each other more than we would like.  Our
attempts at incorporating a \runparam{dpdt\_factor} for spherical problems have not
been successful.
\end{itemize}


% !TEX root = main.tex

%-------------------------------------------------
\section{Location-scale models}

We seek to identify classes of parameters, in particular \emph{location parameters} and \emph{scale parameters}.

We can think of a parameter as a \emph{function} of the CDF (or PDF/PMF) of a random variable: we call these \emph{functionals}\footnote{The term \textit{functional} is a generic term used for a function that maps functions to scalar values.}.
%For example, the mean of $X$ can be expressed by the \emph{mean functional},
%\[
%T(F) = \int_{-\infty}^{\infty} xf(x)\,dx.
%\]
%Likewise, the median of $X$ can be expressed as the \emph{median functional},
%\[
%T(F) = F_X^{-1}(1/2).
%\]
For example,
\[\begin{array}{lll}
T(F_X) & = \displaystyle\int_{-\infty}^{\infty} xf_X(x)\,dx	& \qquad\text{is the \emph{mean functional},} \\[2ex]
T(F_X) & = F_X^{-1}(1/2)	& \qquad\text{is the \emph{median functional}.}
\end{array}
\]

% remark: induced estimators
\begin{remark}
The empirical CDF $\hat{F}_X$ is itself a CDF so we can apply the functional $T$ to $\hat{F}_X$:
\bit
\it $T(\hat{F}_X)$ is called the \emph{induced estimator} of $T(F_X)$.
\eit
For example,
\bit
\it if $T$ is the mean functional, $T(\hat{F}_X)$ is the sample mean;
\it if $T$ is the median functional, $T(\hat{F}_X)$ is the sample median.
\eit
\end{remark}

% defn: location and scale functionals
\begin{definition}
\ben
\it $T$ is said to be a \emph{location functional} if
\[
T(F_{a+bX}) = a + bT(F_X) \quad\text{for all $a,b\in\R$.}
\]
\it $T$ is said to be a \emph{scale functional} if
\[
T(F_{a+bX}) = bT(F_X) \quad\text{for all $b>0$.}
\]
\een
\end{definition}

% example: mean and standard deviation
\begin{example}
\ben
\it Show that the mean is a location functional.
\it Show that the standard deviation is a scale functional.
\een
\begin{solution}
Let $Y=a+bX$. By the linearity of expectation, the mean functional $T(F_X) = \expe(X)$ sastifies
\[
T(F_{a+bX})= \expe(a+bX) = a+b\expe(X) = a + bT(F_X),
\]
and the standard deviation functional $T(F_X) = \sqrt{\var(X)}$ sastifies
\[
T(F_{a+bX}) = \sqrt{\var(a+bX)} = \sqrt{b^2\var(X)} = b\sqrt{\var(X)} = bT(F_X)
\]
as required.
\end{solution}
\end{example}

%% remark: induced estimators
%\begin{remark}
%Consider the empirical CDF of $X$,
%\[
%\hat{F}_n(x) = \frac{1}{n}\sum_{i=1}^n I(X_i\leq x)
%\]
%
%Because $\hat{F}_n$ is itself a CDF, we can apply the functional $T$ to $\hat{F}_n)$.
%\bit
%\it $T(\hat{F}_n)$ is called the \emph{induced estimator} of $T(F)$.
%\it
%
%For example,
%\bit
%\it if $T(F)$ is the mean functional, $T(\hat{F}_n)$ is the sample mean;
%\it if $T(F)$ is the median functional, $T(\hat{F}_n)$ is the sample median.
%\eit
%\end{remark}
%-----------------------------
%\subsection{Location-scale models}
%
%Let $X$ be a random variable, let $T(F_X)$ be a location functional and consider the random variable $Z = X - T(F_X)$. Because $T$ is a location functional, $T(F_Z)=0$ and the PDF of $X$ can be written as
%\[
%f_X(x) = f_Z(x-T(F_X)).
%\]
%
%% location model
%\begin{definition}[Location models]
%A statistical model for the distribution of $X$ is called a \emph{location model} with location functional $\theta_X = T(F_X)$ if
%\[
%X = T(F_X) + Z
%\]
%where $Z$ is a random variable with $T(F_Z)=0$.
%\end{definition}
%
%Simpler version:
%\begin{definition}
%Let $\mathcal{M}=\{F(x;\theta):\theta\in\Theta\}$ be a statistical model for the distribution of $X$.
%\ben
%\it $\mathcal{M}$ is called a \emph{location model} if for every $F\in\mathcal{M}$ and $a\in\R$, the CDF $G(x)=F(a+x)$ also belongs to $\mathcal{M}$.
%\it $\mathcal{M}$ is called a \emph{scale model} if for every $F\in\mathcal{M}$ and $b\in\R$ with $b>0$, the CDF $G(x)=F(bx)$ also belongs to $\mathcal{M}$..
%\it $\mathcal{M}$ is called a \emph{location-scale model} if for every $F\in\mathcal{M}$ and $a,b\in\R$ with $b>0$, the CDF $G(x)=F(a+bx)$ also belong $\mathcal{M}$.
%\een
%\end{definition}

% location and location-scale models
\begin{definition}
A statistical model $\mathcal{M}$ is called a
%\ben
%\it \emph{location model} if for every $F\in\mathcal{M}$ and $a\in\R$, the CDF $G(x)=F(a+x)$ also belongs to $\mathcal{M}$.
%\it \emph{location-scale model} if for every $F\in\mathcal{M}$ and $a,b\in\R$ with $b>0$, the CDF $G(x)=F(a+bx)$ also belongs $\mathcal{M}$.
%\een
\ben
\it \emph{location model} if $F(a+x)\in\mathcal{M}$ whenever $F\in\mathcal{M}$ and $a\in\R$.
\it \emph{location-scale model} if $F(a+bx)\in\mathcal{M}$ whenever $F\in\mathcal{M}$ and $a,b\in\R$ with $b>0$.
\een
\end{definition}

\begin{example}
Show that the family of uniform distributions $\mathcal{M}=\left\{\displaystyle\frac{x-L}{R-L}:L,R\in\R, L<R\right\}$ is a location-scale model.
\begin{solution}
For $Y=a+bX$ where $X\sim\text{Uniform}[L,R]$,
\[
F_Y(y) = \prob(Y\leq y) = \prob\left(X\leq\frac{y-a}{b}\right)
	= \frac{(y-a/b)-L}{R-L}
	= \frac{y - (a+bL)}{(a+bR)-(a+bL)}.
\]
Hence $Y\sim\text{Uniform}[a+bL, a+bR]$ so $F_Y\in\mathcal{M}$.
\end{solution}
\end{example}

%\begin{example}
%Show that the family of normal distributions $\mathcal{M}=\{f(x;\mu,\sigma^2):\mu\in\R,\sigma^2\in\R^+\}$ is a location-scale model.
%\begin{solution}
%For $Y=a+bX$ where $X\sim N(\mu,\sigma^2)$,
%\[
%Y \sim N(a+b\mu, b^2\sigma^2)
%\]
%so $F_Y\in\mathcal{M}$.
%\end{solution}
%\end{example}

%-----------------------------
\subsection{Q-Q plots}
Let $\mathcal{M}$ be a location-scale model and let $Z$ be a random variable whose CDF $F_Z\in\mathcal{M}$ is known. Suppose we have another random variable $X$ whose CDF $F_X$ is unknown, and that we wish to test whether $F_X\in\mathcal{M}$.

%Let $Z$ be a random variable whose CDF $F_Z$ is known, and suppose that $F_Z\in\mathcal{M}$ where $\mathcal{M}$ is a location-scale model. Suppose we have another random variable $X$ whose CDF $F_X$ is unknown, and that we wish to test whether or not $F_X\in\mathcal{M}$.

Because $\mathcal{M}$ is a location-scale model, if $F_X\in\mathcal{M}$ then
\[
X = a + bZ %\quad\text{for $a,b\in\R$ with $b>0$,}
\]
where $a$ and $b>0$ are unknown parameters. Since $b>0$ the CDF of $X$ satisfies
\[
F_X(x) = \prob(X\leq x) = \prob(a+bZ\leq y) = \prob\left(Z\leq\frac{x-a}{b}\right) = F_Z\left(\frac{x-a}{b}\right).
\]

Let $x_p$ and $z_p$ denote the $p$th quantiles of $X$ and $Z$ respectively (where $0<p<1$). Then
\bit
\it $z_p = F_Z^{-1}(p)$ is known
\it $x_p = a + bF_Z^{-1}(p)$ is unknown (because $a$ and $b$ are unknown).
\eit
If $F_X\in\mathcal{M}$ we have the linear relationship
\[
x_p = a + b z_p.
\]

%\bigskip
The quantiles $x_p$ of $F_X$ are unknown, but they can be estimated by order statistics.
\bit
%\it Let $X_1,X_2,\ldots,X_n$ be a random sample from the distribution of $X$.
%\it Let $X_{(1)},X_{(2)},\ldots,X_{(n)}$ be the order statistics of $X_1,X_2,\ldots,X_n$.
%\it Let $X_1,X_2,\ldots,X_n$ be a random sample from the distribution of $X$.
\it Let $X_{(1)},X_{(2)},\ldots,X_{(n)}$ be the order statistics of a random sample from the distribution of $X$.
\it $X_{(k)}$ is a point estimator of the quantile $x_{p_k}$ where $p_k=k/(n+1)$.
\eit

\begin{definition}
A plot of the order statistics $X_{(k)}$ against the quantiles $z_{p_k}$ is called a \emph{Quantile-Quantile plot} (Q-Q plot).
\end{definition}

\bit
\it If $F_X\in\mathcal{M}$, the plot should be approximately linear.
\it The parameters $a$ and $b$ can be estimated by the intercept and gradient respectively.
\eit

% example: Q-Q plots
\begin{example}
To test whether a random sample $X_1,X_2,\ldots,X_n$ is from a Normal distribution, we plot the order statistics $X_{(k)}$ against the values $z_{p_k} = \Phi^{-1}\big[k/(n+1)\big]$ for $k=1,2,\ldots,n$ where $\Phi$ is the CDF of $N(0,1)$. If the points lie on (or near) a straight line, we might conclude that the $X_i$ are normally distributed (see Figure~\ref{fig:qq}).
%\begin{minipage}{\linewidth}
%\begin{minipage}{0.45\linewidth}
%\resizebox{\linewidth}{!}{\includegraphics{nqqplot-normal}}
%\end{minipage}
%\hfill
%\begin{minipage}{0.45\linewidth}
%\resizebox{\linewidth}{!}{\includegraphics{nqqplot-uniform}}
%\end{minipage}
%\end{minipage}
\begin{figure}[ht]
\centering
\begin{tabular}{cc}
	\begin{subfigure}{0.45\textwidth}
	\resizebox{\linewidth}{!}{\includegraphics{nqqplot-normal}}
	\end{subfigure}
&
	\begin{subfigure}{0.45\textwidth}
	\resizebox{\linewidth}{!}{\includegraphics{nqqplot-uniform}}
	\end{subfigure}
\end{tabular}
\caption{Q-Q plots for testing normality.\label{fig:qq}}
\end{figure}

\end{example}

% exercise (symmetric distributions)
\begin{exercise}
\begin{questions}
\question
Let $X$ be a random variable, let $F_X$ be its CDF and suppose that its distribution is symmetric about $a$.  Using the fact that $X-a$ and $-(X-a)$ have the same distribution, show that any location functional satisfies $T(F_X)=a$.
\begin{answer}
Because $T$ is a location functional,
\[
T(F_{X-a}) = T(F_X)-a \qquad\text{and}\qquad T(F_{-(X-a)}) = -T(F_X)+a.
\]
Because $X-a$ and $-(X-a)$ have the same distribution, these are equal so $T(F_X)=a$, as required.
\end{answer}

\question %median and IQR
\begin{parts}
\part % median
Show that the median is a location functional.
\begin{answer}
Let $Y=a+bX$. Then
\[
F_{a+bX}(y) = \begin{cases}
	F_X\left(\frac{y-a}{b}\right)		& \text{ if $b>0$,} \\
	1 - F_X\left(\frac{y-a}{b}\right)	& \text{ if $b<0$.}
\end{cases}
\]
Let $T(F_X)=F_X^{-1}(1/2)$ be the median functional. Then $F_X\big[T(F_X)\big] = 1/2$ so for $b>0$,
\[
F_{a+bX}\big[a+bT(F_X)\big] = F_X\left[\frac{a+bT(F_X) - a}{b}\right] = F_X\big[T(F_X)\big] = 1/2,
\]
and for $b<0$,
\[
F_{a+bX}\big[a+bT(F_X)\big] = 1 - F_X\left[\frac{a+bT(F_X) - a}{b}\right] = 1 - F_X\big[T(F_X)\big] = 1 - 1/2 = 1/2.
\]
In either case we have
\[
T(F_{a+bX}) = F_{a+bX}^{-1}(1/2) = a+bT(F_X),
\]
so the median is a location functional, as required.
\end{answer}

\part % IQR
Show that the inter-quartile range is a scale functional.
\begin{answer}
Let $L(F_X) = F_X^{-1}(1/4)$ and $U(F_X)=F_X^{-1}(3/4)$ be the lower-quartile and upper-quartile functionals respectively. Then
\[
F_X\big[L(F_X)\big] = 1/4 \quad\text{and}\quad F_X\big[U(F_X)\big] = 3/4.
\]
For $b>0$,
\begin{align*}
F_{a+bX}\big[a+bL(F_X)\big]
	& = F_X\left[\frac{a+bL(F_X) - a}{b}\right] = F_X\big[L(F_X)\big] = 1/4,\\
F_{a+bX}\big[a+bU(F_X)\big]
	& = F_X\left[\frac{a+bU(F_X) - a}{b}\right] = F_X\big[U(F_X)\big] = 3/4,\\
\end{align*}
Thus $L(F_{a+bX}) = F_{a+bX}^{-1}(1/4) = a+bL(F_X)$ and $U(F_{a+bX}) = F_{a+bX}^{-1}(3/4) = a+bU(F_X)$.

\bigskip
The inter-quartile range functional is $T(F_X)=U(F_X)-L(F_X)$, so
\[
T(F_{a+bX})
	= F_{a+bX}^{-1}(3/4) - F_{a+bX}^{-1}(1/4)
	= \big[a+bU(F_X)\big] - \big[a + bL(F_X)\big]
	= b\big[U(F_X)-L(F_X)\big]
	= bT(F_X).
\]
Thus the inter-quartile range is a scale functional, as required.
\end{answer}
\end{parts}
\end{questions}
\end{exercise}


\input{../common/common.tex}

\title{Math notes - Schr\"oder-Bernstein Theorem}
\author{Uwe Hoffmann}
\hypersetup{colorlinks, pdftitle={Math notes - Schr\"oder-Bernstein Theorem}}

\begin{document}

\setcounter{chapter}{1}
\section*{Schr\"oder-Bernstein Theorem}

\newthought{Bijections} from one-to-one functions are the topic\footnote{Exercise 1.5.11 on page 32 from \bibentry{abbott15}.} in this note. The problem statement is known as the Schr\"oder-Bernstein Theorem. \index{Bernstein}\index{Schr\"oder}

\vspace{10 mm}
\begin{problem}
Let $f:X \to Y$ and $g:Y \to X$ be one-to-one functions. Then there exists a bijection $h:X \to Y$.
\end{problem}

\begin{marginfigure}[1.0in]
\begin{tikzpicture}

\node[] (lX) at (0, 4) {X};
\node[] (lY) at (4, 4) {Y};
\node[] (lA) at (-0.4, 1.5) {A};
\node[] (lfA) at (4.6, 1.5) {f(A)};

\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (d\x) at (0,\x) {};
}
\node[fit=(d1) (d2) (d3),ellipse,draw,minimum width=1.5cm] {};
\node[fit=(d2) (d3),ellipse,draw,minimum width=1cm] {};

\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (c\x) at (4,\x) {};
}
\node[fit=(c1) (c2) (c3),ellipse,draw,minimum width=2cm] {};
\node[fit=(c2) (c3),ellipse,draw,minimum width=1cm] {};

\draw[-latex] (d3) -- node[above] {f} (c3);
\draw[-latex] (d2) -- node[above] {f} (c2);
\draw[-latex] (c1) -- node[below] {g} (d2);
\end{tikzpicture}
\caption{$A$ violates $P(A)$}
\label{fig:intersectNotEmpty}
\end{marginfigure}

The given functions are one-to-one, so for subsets $f(X)$ and $g(Y)$ they are already bijections. This leads to the idea of partitioning $X$ and $Y$ such that we can compose a bijection $h$ piece-wise from $f$ and $g^{-1}$ using the partitions. In particular given a subset $A \subseteq X$, we consider the sets $A$, $X \setminus A$, $f(A)$, $Y \setminus f(A)$ and $g(Y \setminus f(A))$. We want subsets $A \subseteq X$, such that $A \cap g(Y \setminus f(A)) = \emptyset$, as shown in figure \ref{fig:intersectEmpty}. Let's define this as property $P$:
$$
\forall A \subseteq X: P(A) \Leftrightarrow A \cap g(Y \setminus f(A)) = \emptyset
$$

\begin{marginfigure}[1.0in]
\begin{tikzpicture}

\node[] (lX) at (0, 4) {X};
\node[] (lY) at (4, 4) {Y};
\node[] (lA) at (-0.4, 1.5) {A};
\node[] (lfA) at (4.6, 1.5) {f(A)};

\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (d\x) at (0,\x) {};
}
\node[fit=(d1) (d2) (d3),ellipse,draw,minimum width=1.5cm] {};
\node[fit=(d2) (d3),ellipse,draw,minimum width=1cm] {};

\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (c\x) at (4,\x) {};
}
\node[fit=(c1) (c2) (c3),ellipse,draw,minimum width=2cm] {};
\node[fit=(c2) (c3),ellipse,draw,minimum width=1cm] {};

\draw[-latex] (d3) -- node[above] {f} (c3);
\draw[-latex] (d2) -- node[above] {f} (c2);
\draw[-latex] (c1) -- node[below] {g} (d1);
\end{tikzpicture}
\caption{$A$ satisfies $P(A)$}
\label{fig:intersectEmpty}
\end{marginfigure}

If we have a subset $A \subseteq X$ that satisfies $P(A)$, then we can define the bijection $h$:
$$
h(x) = \begin{cases}
         f(x) & : x \in A\\
         g^{-1}(x) & : x \in g(Y \setminus f(A))
      \end{cases}
$$

The domain of $h$ is $A \cup g(Y \setminus f(A))$, which is not necessarily equal to $X$, so we are not done yet. Our goal therefore is to find a subset $A \subseteq X$ that satisfies $P(A)$ and for which $A \cup g(Y \setminus f(A)) = X$.

Let
$$
\varLambda = \{A \subseteq X: P(A)\}
$$
be the set of all subsets of $X$ that satisfy property $P$ and let $\bar{A}$ be the union of all such subsets
$$
\bar{A} = \bigcup_{A \in \varLambda} A
$$

\begin{lem}\label{biggestSubset}
$\bar{A}$ is the biggest subset of $X$ that satisfies $P$.
\end{lem}

\begin{proof}
First we show that $\bar{A}$ satisfies $P$. Assume
$$
\exists y \in Y \setminus f(\bar{A}) \text{ with } g(y) \in \bar{A}
$$
Then there exists a set $A \in \varLambda$ with $g(y) \in A$ \sidenote{Because $\bar{A} = \bigcup_{A \in \varLambda}$.}.
$A \subseteq \bar{A}$, so $f(A) \subseteq f(\bar{A})$. Therefore $Y \setminus f(\bar{A}) \subseteq Y \setminus f(A)$, so $y \in Y \setminus f(A)$. But this contradicts $A$ satisfying property $P$, so no such $y$ exists. It follows that $\bar{A}$ satisfies $P$ too.

Assume there is a set $A'$ that satisfies $P$ and that is bigger than $\bar{A}$, so $\bar{A} \subseteq A'$. But $A' \in \varLambda$ and $\bar{A} = \bigcup_{A \in \varLambda}$, so $A' \subseteq \bar{A}$. That means $A'=\bar{A}$.
\end{proof}

With $\bar{A}$ we can define the partitions $X=\bar{A} \oplus (X \setminus \bar{A})$ and $Y=f(\bar{A}) \oplus (Y \setminus f(\bar{A}))$.

\begin{lem}
$$
g(Y \setminus f(\bar{A})) = X \setminus \bar{A}
$$
\end{lem}

\begin{proof}
Because $\bar{A}$ satisfies $P$, we already know that
$$
g(Y \setminus f(\bar{A})) \subseteq X \setminus \bar{A}
$$
Now assume
$$
\exists x \in X \setminus \bar{A} \text{ such that } \forall y \in Y \setminus f(\bar{A}): g(y) \neq x
$$
But then $\bar{A} \cup \{x\}$ satisfies $P$
\sidenote{
We have
$$
Y \setminus f(\bar{A} \cup \{x\}) \subseteq Y \setminus f(\bar{A})
$$
so
$$
\forall y \in Y \setminus f(\bar{A} \cup \{x\}): g(y) \not\in \bar{A} \cup \{x\}
$$
} and is bigger than $\bar{A}$. This contradicts lemma \ref{biggestSubset}. So no such $x$ exists and the lemma is proven.
\end{proof}

We can now define the bjection $h: X \to Y$ with
$$
h(x) = \begin{cases}
         f(x) & : x \in \bar{A}\\
         g^{-1}(x) & : x \in X \setminus \bar{A}
      \end{cases}
$$
which solves the problem in this section. \sidenote{The solution uses a nifty proof strategy: maximize a mathematical structure so that its ``complement'' has no choice but to satisfy a certain property, ie not satisfying the property would contradict the maximality.}

\bibliographystyle{plainnat}
\bibliography{../common/math}

\end{document}