%-------------------------------------------------------------------------------

% This file is part of Code_Saturne, a general-purpose CFD tool.
%
% Copyright (C) 1998-2013 EDF S.A.
%
% This program is free software; you can redistribute it and/or modify it under
% the terms of the GNU General Public License as published by the Free Software
% Foundation; either version 2 of the License, or (at your option) any later
% version.
%
% This program is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
% FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
% details.
%
% You should have received a copy of the GNU General Public License along with
% this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
% Street, Fifth Floor, Boston, MA 02110-1301, USA.

%-------------------------------------------------------------------------------

%-------------------------------------------------------------------------------
\section{Time discretisation of a transport equation}

At first, the physical properties of the flow are computed (density,
viscosity, specific heat \emph{etc.}): indeed, they may depend upon the variables
(such as the temperature for example).

The time scheme is a $\theta$-scheme:
\begin{equation}
\left\{%
\begin{array}{ll}
\theta = 1 & \text{for an implicit first order Euler scheme}, \\
\theta = \frac{1}{2} & \text{for second order Crank-Nicolson scheme}.
\end{array}
\right.
\end{equation}

For the second order scheme, the time step is assumed to be constant.

If required, the equations for the turbulent variables are solved (turbulent
kinetic energy and dissipation or Reynolds stresses and dissipation), using
a $\theta$-scheme again. For the $k-\varepsilon$ model, an additional step
is carried out to couple the source terms. For the Reynolds stress model,
the variables (turbulent stresses and dissipation) are solved sequentially,
without coupling.

Next, the equations for the \emph{scalars} (enthalpy, temperature, tracers,
concentrations, mass fractions...) are solved, also with a $\theta$-scheme.

Finally, all the variables are updated and another time step may start.

The general equation for advection (valid for the velocity components, the
turbulent variables and the scalars) is re-written as follows in a condensed
form; the mass equation ($\frac{\partial \rho } {\partial t}+ \dive(\rho
\vect{u}) = \Gamma$ see Equation (\ref{eq:mass})) has been used to split the time derivative:
%
\begin{equation}\label{eq:transport_eq_scalar}
\rho \der{\varia}{t} + \grad \varia \cdot \left(\rho \vect{u} \right) 
- \dive \left(\rho \vect{u} \right) \varia
- \dive \left(K \grad \varia \right) = S_{i}\left(\Phi, \, \varphi\right) \varia + S_{e}\left(\Phi,\varphi \right) 
+ \Gamma \left( \varia - \varia^{in} \right).
\end{equation}
In Equation~\ref{eq:transport_eq_scalar}, $\Phi$ represents the physical properties such as $(\rho,K,\mu_{t},...)$,
$\varphi$ represents the variables of the problem such as $(\vect{u},k,\epsilon,...)$,
$S_{i}\left(\Phi, \, \varphi \right) \varia$ is the linear part of the source terms
and 
$S_{e} \left(\Phi, \, \varphi \right)$  includes all other source terms.

Therefore, four different time steppings are used, they all define the time at which the quantities are evaluated:
%
\begin{enumerate}[ label=\roman{*}/, ref=(\roman{*})]
\item $\theta$ is the time stepping applied to the variable $\varia^{n+\theta} \equiv \theta \varia^{n+1} + \left( 1 - \theta \right) \varia^n$,
\item $\theta_\Phi$ is the time stepping applied to the physical properties,
\item $\theta_F$ is the time stepping applied to the mass flux,
\item $\theta_S$ is the time stepping applied to the source terms,
\end{enumerate}
%

If $\theta=1/2$, or if an extrapolation is used, the time step $\Delta t$ is
constant in time and uniform in space.

\subsection{Physical properties $\Phi$}

The physical properties of the flow (density, viscosity, specific heat...)
are:

\begin{itemize}
\item either explicit, defined at the time step $n$.
\item or extrapolated at $n+\theta _{\Phi }$ using the Adam-Bashforth
time scheme (in this case, the time step is assumed to be constant).
\end{itemize}

Under a more general form, this reads:
\begin{equation}
 \Phi^{n+\theta_{\Phi}} \equiv(1+\theta_{\Phi})\,\Phi^{n}- \theta_{\Phi}\,\Phi^{n-1},
\end{equation}

\begin{equation}
\left\{%
\begin{array}{ll}
\theta_{\Phi} = 0 & \text{standard explicit formulation} ,\\
\theta_{\Phi} = 1/2 & \text{second order extrapolation at } n+1/2, \\
\theta_{\Phi} = 1 & \text{first order extrapolation at } n+1.
\end{array}
\right.
\end{equation}

\subsection{Mass flux}

For the mass flux, three time schemes are available. The mass flux may be:
%
\begin{itemize}
\item explicit, taken at time step $n$ for the momentum equations and
updated with its value at time step $n+1$ for the equations for turbulence
and scalars (standard scheme).

\item explicit, taken at time step $n$ for the momentum equations and
also for the equations for turbulence and scalars.

\item taken at $n+\theta_{F}$ (second order if $\theta_{F}=1/2$). To
solve the momentum equations, $\left(\rho\,\vect{u}\right)^{n-2+\theta_{F}}$ and 
$\left(\rho\,\vect{u}\right)^{n-1+\theta_{F}}$ are known. Hence, the value at 
$n+\theta_{F}$ is obtained as a result of the following extrapolation:
\begin{equation}
\left(\rho\vect{u}\right)^{n+\theta_{F}}
= 2 \left(\rho \vect{u}\right)^{n-1+\theta_{F}} 
- \left(\rho \vect{u}\right)^{n-2+\theta_{F}}.
\end{equation}
At the end of this phase (after the pressure correction step), 
$\left(\rho\,\vect{u}\right)^{n+1}$ is known and the following interpolation is used to
determine the mass flux at $n+\theta_{F}$ that will be adopted for the
equations for turbulence and scalars:
\begin{equation}
\left(\rho\,\vect{u}\right)^{n+\theta_{F}}= \dfrac{1}{2-\theta_{F}}
   \left(\rho \vect{u}\right)^{n+1}
+\frac{1-\theta_{F}}{2-\theta_{F}} \left(\rho \vect{u}\right)^{n-1+\theta_{F}}.
\end{equation}
\end{itemize}

\subsection{Source terms}

As for the physical properties, the \textbf{explicit} source terms are:

\begin{itemize}
\item explicit:
\begin{equation}
\left[ S_{e} \left(\Phi , \, \varphi \right) \right]^{n}=S_{e}\left(\Phi ^{n+\theta_{\Phi }}, \, \varphi^{n} \right),
\end{equation}

\item extrapolated at $n+\theta _{S}$ using the Adams-Bashforth scheme:
\begin{equation}
\left[ S_{e} \left(\Phi , \, \varphi \right) \right]^{n+\theta _{S}}=\left(1+\theta _{S}\right)\,
S_{e}\left(\Phi^{n}, \, \varphi^{n}\right)-\theta _{S}\,S_{e} \left(\Phi ^{n-1} , \,\varphi ^{n-1}\right) .
\end{equation}
\end{itemize}

By default, to be consistent and preserve the order of convergence in time,
the implicit source terms are discretized with the same scheme as that is used
for convection-diffusion of the unknown considered, \emph{i.e.} taken at 
$n+\theta $:
\begin{equation}
\left[ S_{i} \left(\Phi , \, \varphi \right) \varia \right]^{n+\theta }
= S_{i}(\Phi^{n+\theta _{\Phi}} , \, \varphi^{n}) \,\left[ \theta \varia^{n+1}+\left(1-\theta \right)\varia^{n} \right].
\end{equation}

\begin{remark}
The \textbf{implicit} source terms taken also at $n+\theta $ for $\theta
_{S}\neq 0$, while for $\theta _{S}=0$, the implicit source terms are taken
at $n+1$ , this to enhance stability.
\end{remark}

\subsection{General time discretized form}

For the sake of clarity, it is assumed hereafter that, unless otherwise
explicitly stated, the mass flux is taken at $n+\theta_F$ and the physical
properties are taken at $n+\theta_\Phi$, with $\theta_F$ and $\theta_\Phi$
dependent upon the specific schemes selected for the mass flux and the
physical properties respectively and all $\theta$s are denoted by $\theta$.

Under a general form, the discrete counterpart of Equation~(\ref{eq:transport_eq_scalar}) at
$n+\theta$ reads:
\begin{equation}
\displaystyle \dfrac{\rho}{\Delta t} \left(\varia^{n+1}-\varia^{n}\right)
+ \grad\varia^{n+\theta} \cdot \left(\rho \vect{u} \right)
- \dive \left( K \grad \varia^{n+\theta} \right) =
\left[ S_{i}(\Phi, \, \varphi) \varia \right]^{n+\theta} + \left[S_{e}(\Phi, \, \varphi) \right]^{n+\theta} .
\end{equation}

Using the standard $\theta$-scheme $\varia^{n+\theta}=\theta \varia^{n+1}+\left(1-\theta \right)
\varia^{n}$, the equation reads:
\begin{equation}
\begin{array}{r @{\,} c @{\,} l}
\displaystyle \dfrac{\rho}{\Delta t} \left( \varia^{n+1}-\varia^{n} \right)
+ \theta \left[ 
\grad \varia^{n+1} \cdot \left( \rho \vect{u} \right) 
- \dive \left( K \grad \varia^{n+1} \right)   \right]
&=& 
- \left(1-\theta\right) \left[ 
\grad \varia^{n} \cdot \left( \rho \vect{u} \right) 
- \dive \left( K \grad \varia^{n}\right) 
\right]
 \\
&+& S_{i}\left(\Phi , \,\varphi^n\right)\left[\theta \varia^{n+1}+ \left(1-\theta \right) \varia^{n}\right] 
+ \left[ S_{e}(\Phi, \, \varphi)\right]^{n+\theta}.
\end{array}
\end{equation}

For numerical reasons, the system is solved in an iterative and incremental
manner, with the help of the series $\delta \varia^{n+1}_{k+1}=\varia^{n+1}_{k+1}-\varia^{n+1}_{k}$ 
(with, by definition, $\varia^{n+1}_0=\varia^{n}$).
More theoretical details of such an iterative process are given in \S \ref{sec:algebrea_iterative_process}.

%-------------------------------------------------------------------------------
\section{Pressure-based velocity-pressure solver}
The aim of this section is to describe how Navier Stokes equations are solved for an incompressible 
or weakly compressible (dilatable or Low Mach algorithm) combined with a 
implicit Euler time stepping or a second order Crank Nicolson time stepping.

The set of equations to be solved is
\begin{equation}\label{eq:time_stepping_navier_stokes}
\left\{\begin{array}{l}
\displaystyle \rho \der{\vect{u}}{t} + \gradt \, \vect{u} \cdot \left( \rho\vect{u}  \right)
=\divv(\tens{\sigma}) - \divv(\rho \tens{R}) + \vect{ST}_u - \tens{K}\,\vect{u} + \rho \vect{g} + \Gamma \left( \vect{u}^{in} - \vect{u} \right), \\
\dfrac{\partial \rho}{\partial t} + \dive\left( \rho \vect{u} \right) = \Gamma ,
\end{array}\right.
\end{equation}
%
where $\rho$ is the density field, $\vect{u}$ is the velocity field to be solved,
$[\,\vect{ST}_u-\tens{K}\,\vect{u} + \rho \vect{g} \,]$  are sources terms (note that $\tens{K}$ is expected to be a positive definite tensor),
 $\tens{\sigma}$ is the stress tensor, composed of the viscous tensor $\tens{\tau}$ and of the pressure field as follows
 %
\begin{equation}
\left\{\begin{array}{r c l}
\tens{\sigma} &=& \tens{\tau} - P\tens{Id} , \\%FIXME +Rij
\tens{\tau} & =& 2 \mu  \tens{S} +   \left( \kappa - \dfrac{2}{3}\mu \right)   \trace \left( \tens{S} \right) \tens{1} ,\\
\tens{S} &=& \dfrac{1}{2} \left( \gradt \, \vect{u}+\transpose{\gradt \, \vect{u}} \right) .
\end{array}\right.
\end{equation}
 where  $\mu$ is the dynamic molecular viscosity
 $\kappa$ the volume viscosity (also called the second viscosity, usually neglected in the code, excepted for compressible flows).
$\tens{S}$ is called the strain rate and  $\Gamma$ is an eventual mass source term.

%-------------------------------------------------------------------------------
\subsection{Segregated solver: SIMPLEC}
A fractional step scheme is used to solve the mass and momentum equations
(see Chorin \cite{Chorin:1968}). 

The first step (predictor step) provides predicted velocity
components: they are determined in a coupled way solving a $3 \ncell \times 3 \ncell $ system. 
The mass equation is taken into account during the second step
(corrector step): a pressure Poisson equation is solved and the mass fluxes
at the cell faces are updated.

\subsubsection{Prediction step}
In this section, a predicted velocity field $ \widetilde{\vect{u}} $ is obtained by solving 
the momentum equation of \eqref{eq:time_stepping_navier_stokes}

\begin{equation}
\begin{array}{rcl}
\displaystyle \rho \der{ \vect {u} } { t} 
+
\underbrace {
\gradt \,  \underline{u} \cdot \left( \rho\vect{u} \right)
}_{
\text{convection}
}%end underbrace
&= & \underbrace{
\divv \left(\mu\ \gradt \, \vect{u} \right)
}_{
\text{diffusion}
}%end underbrace
 + 
\underbrace{
\divv \left( \mu \transpose{\gradt \, \vect{u}} \right)
}_{
\text{transpose of the velocity gradient term}
}% end underbrace
\underbrace{ 
-  \frac{2}{3} \grad \left( \mu\ \trace \left( \gradt \, \vect{u}  \right) \right)
}_{
\text{secondary viscosity}
}% end underbrace
\\
 &-& \grad \left( P \right)
 - \divv \left( \rho \tens{R} \right)
 + \left( \rho -\rho_0 \right) \vect{g} \\
&\displaystyle +&
\underbrace{
\Gamma \left( \vect{u}^{in}-\vect{u} \right)
}_{
\text{Mass source term}
}%end underbrace
- 
\underbrace{
\rho \tens{K} \, \vect{u}
}_{
\text{Head loss}
}%end underbrace
 + 
\underbrace{ 
\vect{ST}_{u}^{exp}+ \tens{ST}_{u}^{imp}\, \vect{u}
}_{
\text{user source terms}
}%end underbrace
\end{array}
\end{equation}

For more details, see \S~ \ref{ap:navstv} and \ref{ap:predvv}. 
\subsubsection{Correction step}

For more details, see \S~ \ref{ap:navstv} and \ref{ap:resopv}. 

%-------------------------------------------------------------------------------
%\subsection{Pseudo-coupled solver}

%\section{Steady algorithm}

%\section{Unsteady algorithm}
