\chapter{Model equations}\label{chap:model_equations}

\section{How to navigate this chapter}

This chapter covers the fluid and associated field equations modelled by {\fluidity}.

\begin{center}
\begin{tabular}{lccc}
\hline
problem & underlying equations & boundary conditions & other considerations\\
\hline
scalar advection & \ref{sec:MP-AdvdifEqn-eqns}, \ref{sec:MP-AdvdifEqn-adv} & \ref{sec:bc_scalar_dirichlet}, \ref{sec:bc_scalar_neumann} & × \\
scalar advection-diffusion & \ref{sec:MP-AdvdifEqn-eqns}, \ref{sec:MP-AdvdifEqn-diff} & \ref{sec:bc_scalar_dirichlet}, \ref{sec:bc_scalar_neumann}, \ref{sec:bc_scalar_robin} × & ×\\
momentum equation & \ref{sec:MP-MomEqn} & \ref{sec:bc_vector_dirichlet}, \ref{sec:bc_vector_stress}, \ref{sec:bc_vector_traction}, \ref{sec:FS} & \ref{sec:eqn_extensions} \\
\hline
\end{tabular}
\end{center}

The material covered in this chapter is dealt with in great detail in \citet{batchelor1967} and \citet{landau}. \cite{cushman1994} is also a useful reference.

\section{Advection--Diffusion equation}\label{sec:MP-AdvdifEqn}
\index{advection-diffusion equation}

\subsection{General equation}\label{sec:MP-AdvdifEqn-eqns}

The general form the equation that governs the evolution of a scalar field $c$
(\eg passive tracer, species concentration, temperature, salinity) is
\begin{equation}\label{eq:general_scalar_eqn}
\ppt{c} + \nabla\cdot(\bmu c) = \nabla\cdot(\kaptens\nabla c) - \sigma c + F,
\end{equation}
where $\bmu=(u,v,w)^{T}$ is the velocity vector, $\kaptens$ is the diffusivity (tensor), $\sigma$ is an absorption coefficient ($-\sigma c$ is sometimes termed Rayleigh or linear friction) and $F$ represents any source or reaction terms.

\subsubsection{Advection}\label{sec:MP-AdvdifEqn-adv}
The advection term in \eqref{eq:general_scalar_eqn}, given by
\begin{equation}\label{eq:scalar_advection}
\nabla\cdot(\bmu c) = \bmu\cdot\nabla c + (\nabla\cdot\bmu)c,
\end{equation}
expresses the transport of the scalar quantity $c$ in the flow field $\bmu$. Note that for an incompressible flow $\nabla\cdot\bmu=0$ (see section \ref{sec:equation_of_state}) resulting in the second term on the right hand side of \eqref{eq:scalar_advection} dropping out. However, there may be numerical
reasons why the discrete velocity field is not exactly divergence free, in which case this term may
be included in the discretisation. {\fluidity} deals with the advection term in the form
\begin{equation}\label{eq:fluidity_scalar_advection}
\nabla\cdot(\bmu c) + (\beta-1)(\nabla\cdot\bmu)c,
\end{equation}
so that $\beta=1$ corresponds to the conservative form of the equation and $\beta=0$ to the non-conservative.

\subsubsection{Diffusion}\label{sec:MP-AdvdifEqn-diff}
The diffusion term in \eqref{eq:general_scalar_eqn}, given by
\begin{equation}\label{eq:scalar_diffusion}
\nabla\cdot(\kaptens\nabla c),
\end{equation}
represents the mixing of $c$ and may be due to
molecular mixing of individual particles via Brownian motion, or mixing via large scale (in comparison to the
molecular scale) motion in the flow. For many applications \eqref{eq:scalar_diffusion} can be written in simpler forms. Often, diffusion is isotropic giving $\kaptens = \mathrm{diag}(\kappa,\kappa,\kappa)$ and thus the diffusion term
may be written as
\begin{equation}\label{eq:scalar_isotropic_diffusion}
\nabla\cdot(\kaptens\nabla c) = \kappa\nabla\cdot\nabla c = \kappa\nabla^2 c = \kappa\Delta c.
\end{equation}
In domains with high aspect ratio dynamics one often uses a smaller value of diffusivity in the `thin'
direction. For example, in the atmosphere or ocean we may choose a horizontal (eddy) diffusivity $\kappa_H$ and a
vertical (eddy) diffusivity $\kappa_V$ so that $\kaptens = \mathrm{diag}(\kappa_H,\kappa_H,\kappa_V)$ with $\kappa_V < \kappa_H$.
In this case the diffusion term may be written as
\begin{equation}\label{eq:scalar_anisotropic_diffusion}
\nabla\cdot(\kaptens\nabla c) = \kappa_H \left(\pptt[x]{c} + \pptt[y]{c}\right) + \kappa_V \pptt[z]{c}.
\end{equation}
Note that the second-order terms defined above are often termed Laplacian diffusion.

\subsubsection{Absorption, reaction and source terms}\label{sec:MP-AdvdifEqn-abs}
\index{absorption term}
\index{source term}
\index{reaction term}

The absorption term in \eqref{eq:general_scalar_eqn}
\begin{equation}\label{eq:scalar_absorption}
-\sigma c,
\end{equation}
has the effect of decreasing the magnitude of $c$ (note the minus sign and the fact
that $\sigma$ would typically be positive). It is sometimes termed Rayleigh friction.

The remaining term in \eqref{eq:general_scalar_eqn}
\begin{equation}\label{eq:scalar_source}
F = \sum_i F_i,
\end{equation}
can encompasses a number of source and reaction terms. Those terms where $F_i$ are
a given function of time, location or a-priori known fields are termed sources (and sometime
sinks if they are negative). Those terms which are also functions of other prognostic fields
are termed reactions and are common when dealing with chemistry or biology.

\subsection{Scalar boundary conditions} \label{sec:BCs}

\index{boundary conditions!scalar}

To form a well-posed system upon which to attempt a numerical discretisation, the
set of equations (discussed above) describing the behaviour of the
system must be supplemented with appropriate boundary conditions.

\subsubsection{Dirichlet condition for a scalar field}\label{sec:bc_scalar_dirichlet}
\index{boundary conditions!Dirichlet}
For a scalar field, $c$ say, a Dirichlet condition on the boundary
$\partial\Omega$ takes the form
\begin{equation*}
c=\tilde{c},\quad \textrm{on}\quad \partial\Omega.
\end{equation*}

\subsubsection{Neumann condition for a scalar field}\label{sec:bc_scalar_neumann}
\index{boundary conditions!Neumann}

Taking the weak form (applying Green's theorem to the diffusion term) of the advection-diffusion equation \eqref{eq:general_scalar_eqn} leads to a surface integral of the form
\begin{equation*}
\int_{\partial\Omega} \phi (\kaptens\nabla c)\cdot\bmn \;d\Gamma,
\end{equation*}
where $\phi$ is a test function (see section \ref{chap:numerical_discretisation}).
The Neumann condition is specified by assigning a value to $(\kaptens\nabla c)\cdot\bmn$, \eg
\begin{equation*}
(\kaptens\nabla c)\cdot\bmn = q,\quad \textrm{on}\quad \partial\Omega,
\end{equation*}
and substituting in this surface integral to the discretised equation. Note that $q$ is often termed a flux.

\subsubsection{Robin condition for a scalar field}\label{sec:bc_scalar_robin}
\index{boundary conditions!Neumann}

Taking the weak form (applying Green's theorem to the diffusion term) of the advection-diffusion equation \eqref{eq:general_scalar_eqn} leads to a surface integral of the form
\begin{equation*}
\int_{\partial\Omega} \phi (\kaptens\nabla c)\cdot\bmn \;d\Gamma,
\end{equation*}
where $\phi$ is a test function (see section \ref{chap:numerical_discretisation}).
The Robin condition is specified by relating the surface diffusive flux $(\kaptens\nabla c)\cdot\bmn$ to an ambient field value $c_{a}$, with an associated surface transfer coefficient $h$. This is given by the relationship
\begin{equation*}
-(\kaptens\nabla c)\cdot\bmn = h (c - c_{a}),\quad \textrm{on}\quad \partial\Omega,
\end{equation*}
where in general $h$ and $c_{a}$ can vary spatially and temporally. This is substituted into the discretised equation to give a surface absorption term given by $hc$ and a surface source given by $-hc_{a}$.

\section{Fluid equations}\label{sec:MP-MomEqn}

\index{conservation!equation}
\index{momentum equation}

A starting point for describing the physics of a continuum are the conservation equations. Fluid volumes deform in time as the fluid moves. If $\theta(\bmx,t)$ is the density of some quantity (\eg Temperature) associated with the fluid, the time evolution of that quantity in a fluid volume $V(t)$ is
\begin{equation}\label{RTT}
 \ddt{}\left[\int_{V(t)}\theta(\bmx,t)\right]=
 \int_{V(t)}\left(\DDt{\theta}+\theta\nabla\cdot\bmu\right),
\end{equation}
which is the Reynolds' Transport theorem. In \eqref{RTT} $\bmx=(x,y,z)^T$ and $\bmu=(u,v,w)^T$ are three-dimensional position and velocity vectors respectively and
\begin{equation}\label{MatDiv}
 \DDt{}\equiv\frac{\partial}{\partial{t}}+\bmu\cdot\nabla,
\end{equation}
is the \textit{material derivative} (NB. it has many other commonly-used names including the total and Lagrangian derivative). \index{Reynolds Transport theorem}

\subsection{Mass conservation}
\index{conservation!mass}
Substituting $\theta=\rho$ in to \eqref{RTT} and noting that matter is neither created nor destroyed gives that the \lhs\ of \eqref{RTT} is zero. Then, as the volume $V(t)$ is arbitrary, it is seen that the mass density satisfies
\begin{equation}\label{mass_conservation}
 \DDt{\rho}=-\rho\nabla\cdot\bmu,
\end{equation}
or equivalently
\begin{equation}\label{mass_conservation_2}
 \frac{\partial\rho}{\partial{t}}+\nabla\cdot(\rho\bmu)=0.
\end{equation}
The quantity $\rho\bmu$ is called the \textit{mass flux} or \textit{momentum} and \eqref{mass_conservation_2} is termed the \textit{equation of continuity}.

\subsection{Momentum conservation}
\index{conservation!momentum}
The momentum associated with a unit volume of fluid is given by $\rho\bmu$. Initially, consider that the fluid is \textit{ideal}, that is, viscosity and conductivity are assumed to be unimportant. Then, the rate of change of momentum is given by
\begin{equation}\label{mom_cons_1}
 \frac{\partial}{\partial{t}}(\rho\bmu)=\rho\frac{\partial\bmu}{\partial{t}}+\frac{\partial\rho}{\partial{t}}\bmu.
\end{equation}
Using the equation of continuity \eqref{mass_conservation_2} and Euler's
equation \citep{batchelor1967} (the force equation for an inviscid fluid) in the form
\begin{equation}\label{mom_cons_2}
 \frac{\partial\bmu}{\partial{t}}=-\bmu\cdot\nabla\bmu-\frac{1}{\rho}\nabla{p},
\end{equation}
gives
\begin{equation}\label{mom_cons_3}
 \frac{\partial}{\partial{t}}(\rho\bmu)=-\nabla{p}-\nabla\cdot(\rho\bmu\bmu),
\end{equation}
where $\bmu\bmu$ is a tensor which represents the dyadic product of vectors which, using index notation, can be written as ${u_{i}u_{j}}$. Writing $\tensor{\Pi}=p\mathbf{I}+\rho\bmu\bmu$, \eqref{mom_cons_3} can be written as
\begin{equation}\label{mom_cons_4}
 \frac{\partial}{\partial{t}}(\rho\bmu)+\nabla\cdot\tensor{\Pi}=0,
\end{equation}
where $\tensor{\Pi}$ is clearly a symmetric tensor and is termed the \textit{momentum flux density tensor}.

The effects of viscosity on the motion of a fluid are now considered. To express the equations of motion governing a viscous fluid, some additional terms are required. The equation of continuity (conservation of mass) is equally valid for any viscous as well as inviscid fluid. However, Euler's equation \eqref{mom_cons_2} and hence \eqref{mom_cons_4} require modification.

By adding $-\tautens$ to the previously introduced \textit{momentum flux density tensor}, $\tensor{\Pi}$, so that

\begin{equation}
 \tensor{\Pi}=p\mathbf{I}+\rho\bmu\bmu-\tautens=-\sigtens+\rho\bmu\bmu,
\end{equation}

where $\sigtens=-p\mathbf{I}+\tautens$, the viscous transfer of momentum in the fluid can be taken into account. $\sigtens$ is called the stress tensor and gives the part of the momentum flux which is not due to direct transfer of momentum with the mass of the fluid. $\tautens$ is named the deviatoric or viscous stress tensor. These tensors and the forms which they may take are discussed in more detail in section \ref{sec:equation_of_state}. Thus, the most general form of the momentum equation of a compressible viscous fluid may be written as

\begin{equation}\label{viscous_fluids_1}
 \rho\left(\frac{\partial\bmu}{\partial{t}}+\bmu\cdot\nabla\bmu\right)=-\nabla\cdot\sigtens+\rho\bmF,
\end{equation}

where $\bmF$ is a volume force per unit mass (\eg gravity, astronomical forcing).

% Note: Something needs to be added about the difference between the two forms outlined below within Fluidity i.e. a reference
% to somewhere in chapter 3 that goes over this.

\subsubsection{Compressible equations in conservative form}\label{sec:compressible_conservative}
Using the conservation laws outlined above the following point-wise PDE system governing the motion of a compressible fluid is obtained

\begin{subeqnarray}
\frac{\pp\rho}{\pp t} + \nabla\cdot(\rho\bmu) &=& 0,\slabel{conmass}\\
\frac{\pp}{\pp t}(\rho\bmu) + \nabla\cdot(\rho\bmu\bmu-\sigtens) &=& \rho\bmF,\slabel{conmom}\\
\frac{\pp}{\pp t}(\rho \tote) + \nabla\cdot(\rho E\bmu - \sigtens\bmu +
\bmq) &=& \rho\bmF\cdot\bmu,\slabel{conenergy}
\label{conservativesystem}
\end{subeqnarray}
where $\tote\equiv\inte+\modu^2/2$ is the total specific energy (in which $\inte$ is the internal energy). \eqref{conmass} is exactly the conservative form of the continuity equation given in \eqref{mass_conservation_2}, \eqref{conmom} is equivalent to equation \eqref{viscous_fluids_1} and \eqref{conenergy} is obtained from making the substitutions $w=\inte+p/\rho$ and $\tote\equiv\inte+\modu^2/2$ in \eqref{viscous_fluids_1}.

\subsubsection{Compressible equations in non-conservative form}\label{sec:compressible_nonconservative}
Expanding terms in \eqref{conservativesystem} yields the
non-conservative form of the compressible equations\footnote{\eqref{nonconmass}
is trivial to obtain. \eqref{nonconmom} makes use of \eqref{conmass}
and the divergence of the dyadic product, given by
\begin{equation*}
\nabla\cdot(\bmu\bmu) = \bmu\cdot\nabla\bmu + \bmu\nabla\cdot\bmu,
\end{equation*}
along with \eqref{MatDiv}. \eqref{nonconenergy} makes use of both \eqref{nonconmass} and \eqref{nonconmom} and
note that substituting for $\tote\equiv\inte+\modu^2/2$ results in the cancellation of kinetic energy terms.}
\begin{subeqnarray}\label{nonconform}
\DDt{\rho} + \rho\nabla\cdot\bmu &=& 0,\slabel{nonconmass}\\
\rho\DDt{\bmu} -\nabla\cdot\sigtens &=& \rho\bmF,\slabel{nonconmom}\\
\rho\DDt{\inte} - \sigtens\cdot\nabla\bmu + \nabla\cdot\bmq &=&
0.\slabel{nonconenergy} \label{nonconservativesystem}
\end{subeqnarray}
Note that, provided the fields (\eg density and pressure) vary smoothly, that is, the fields are differentiable functions, equations \eqref{conservativesystem} and \eqref{nonconservativesystem} are identical.

\subsection{Equations of state \& constitutive relations}
\label{sec:equation_of_state}
\index{equation of state}

Closure of the conservation equations requires an additional equation describing how the stress tensor is related to density, temperature and any other state variables of relevance. In general, this relationship is dependent of the physical and chemical properties of the material in the domain; hence, this relationship is known as the material model (note that in multi-material simulations a different material model can and must be specified for each material).

It is convenient to separate the full stress tensor $\sigtens$ into an isotropic part, the pressure $p$, and a deviatoric part $\tautens$.  With the convention that compressive stress is negative, the stress tensor is given by $\sigtens =-p \bmI + \tautens$, where $\bmI$ is the identity matrix. If the stress tensor is separated in this way, the material model comprises two parts: an equation of state\footnote{Equation of state settings in \fluidity\ are described in section~\ref{sec:ConfigEOS}} $f(p,\rho,T,\ldots)=0$ relating density to pressure, temperature, etc., and a constitutive relationship\footnote{constitutive relationship settings in \fluidity\ are described in section (to be written)} $g(\tautens,\bmu)=0$.

\subsubsection{Newtonian fluids}
\index{stress}
\index{strain}

Two important classes of fluids are: (i) Newtonian fluids, where deviatoric strain rate $\left(\etens\right)$ is linearly proportional to deviatoric stress ($\tautens$); and (ii) non-Newtonian fluids, where deviatoric strain rate is non-linearly proportional to the deviatoric stress. At present \fluidity\ is only configured to deal with Newtonian fluids.

For a Newtonian fluid the relation between deviatoric stress and deviatoric strain rate can be expressed as:

\begin{equation}
\tautens = 2\mu \etens + \lambda(\nabla\cdot\bmu)\bmI,
\end{equation}
where
\begin{equation}
 \etens = \frac{1}{2}(\nabla\bmu + (\nabla\bmu)^T),
\end{equation}
is the
deviatoric strain rate tensor, and $\bmI$ is the identity matrix. $\mu$ and
$\lambda$ are the two coefficients of viscosity. Physical arguments
yield the so-called Stokes' relationship $3\lambda+2\mu=0$, and
hence:
\begin{equation}
\tautens = 2\mu(\etens - (\nabla\cdot\bmu)\bmI/3),
\end{equation}
where $\mu$ is the molecular viscosity. See \cite{batchelor1967} for further
details.

\subsubsection{Equation of state for incompressible flow}
\label{sec:IncompressibleFlow}
\index{equation of state}

If a material is \emph{perfectly} incompressible its density cannot change;
in other words, the material density is independent of pressure and
temperature, giving $\rho = \rho_0$, where $\rho_0$ is the reference
density. Note that a flow may contain multiple incompressible materials of
different densities, in which case $\rho^k=\rho_0^k$ applies for each
individual material (indexed with the superscript $k$).

All real materials are compressible to some extent so that changes in
pressure and temperature cause changes in density.  However, in many
physical circumstances such changes in material density are sufficiently
small that the assumption of incompressible flow is still valid. If $U/L$ is
the order of magnitude of the spatial variation in the velocity field, then
the flow field can be considered incompressible if the relative rate of
change of density with time is much less than the spatial variation in
velocity; \ie if $\frac{1}{\rho}\DDt{\rho}\ll U/L$ then \cite[][p.167]{batchelor1967}.

\begin{equation}\label{eq:divfree}
\nabla\cdot\mathbf{u}\approx 0.
\end{equation}

The term incompressible flow is used to describe any such situation where
changes in the density of a parcel of material are negligible.  Not all
parcels in the flow need have the same density; the only requirement is that
the density of each parcel remains unchanged.  For example, in the ocean
where salt content and temperature change with depth, the density of
adjacent parcels changes but any one parcel has a constant density
\cite{panton2006}.  In such cases it is often important to account for
changes in buoyancy caused by the dependence of density on pressure,
temperature and composition $C$ (see, for example,
section~\ref{sec:boussinesq_approximation}).  If the change in $\rho,p,T,C$
about a reference state $\rho_0,p_0,T_0,C_0$ is small, the dependence of
density on each state variable can be assumed to be linear.  In this case, a
general equation of state takes the form
\index{equation of state!linear}
\begin{equation}
\rho = \rho_0(1 - \alpha(T-T_0) + \beta(C-C_0) + \gamma(p-p_0)),
\end{equation}
where $\alpha$ is the thermal expansion coefficient:
\begin{equation*}
\alpha = -\frac{1}{\rho}\frac{\pp\rho}{\pp T},
\end{equation*}
$\beta$ is a general compositional contraction coefficient
\begin{equation}
\beta = \frac{1}{\rho}\frac{\pp\rho}{\pp C},
\end{equation}
and $\gamma$ is the isothermal compressibility
\begin{equation}
\gamma = \frac{1}{\rho}\frac{\pp\rho}{\pp p}.
\end{equation}

For ocean modelling applications the most important compositional variation is salinity $S$ (the volume fraction of salt) and the compressibility of water $\gamma$ is so small that the pressure dependence can be neglected, giving the simple linear equation of state
\begin{equation}
\rho = \rho_0(1 - \alpha(T-T_0) + \beta(S-S_0)),
\end{equation}
where $\beta$ is the saline contraction coefficient (not to be confused with the beta plane parameters defined in the section \ref{sec:coriolis}.)

\subsubsection{Pade equation of state for ocean modelling}\label{sec:PadeDescription}
\index{equation of state!Pade approximation}

Within \fluidity it is also possible to relate the density of sea water to the in situ temperature and salinity through the Pad{\'e} approximation to the equation of state. This approximation offers an accurate and computationally efficient method for calculating the density of seawater and is described by \citet{mcdougall2003}.

% The Pade approx may not actually be coded for the in situ temp, this needs to be checked.

\subsection{Forms of the viscous term in the momentum equation} \label{sec:ViscosityTerms}

If the modelled fluid is incompressible, or has a homogeneous viscosity, it is possible to simplify the stress term in the momentum equation. The complete form of this term, termed the '\emph{stress form}', is:
\begin{equation}
\nabla\cdot\sigtens = \nabla\cdot\left(-p \bmI + \tautens\right) = - \nabla p +
\nabla\cdot\mu\left(\nabla\bmu + (\nabla\bmu)^T - \frac{2}{3}(\nabla\cdot\bmu)\bmI \right)
\end{equation}

If the flow is incompressible, $\nabla\cdot\bmu=0$. It is now possible to simplify the above equation into the '\emph{partial stress form}':
\begin{equation}
\nabla\cdot\sigtens = - \nabla p +
\nabla\cdot\mu\left(\nabla\bmu + (\nabla\bmu)^T\right)
\end{equation}

If the flow is incompressible, and the viscosity is homogeneous:
\begin{equation}
\nabla\cdot\mu\left(\nabla\bmu + (\nabla\bmu)^T\right) = \frac{\partial}{\partial x_j} \mu \left(\frac{\partial \bmu_i}{\partial x_j} + \frac{\partial \bmu_j}{\partial x_i}\right) = \mu \left(\frac{\partial}{\partial x_j}\frac{\partial \bmu_i}{\partial x_j} + \frac{\partial}{\partial x_j}\frac{\partial \bmu_j}{\partial x_i}\right) = \mu \left(\frac{\partial}{\partial x_j}\frac{\partial \bmu_i}{\partial x_j}\right) = \nabla\cdot\mu\nabla\bmu
\end{equation}
the stress tensor can then be further simplified to obtain the '\emph{tensor form}':
\begin{equation}
\nabla\cdot\sigtens = - \nabla p + \nabla\cdot\mu\nabla\bmu
\end{equation}

The appropriate form of the stress tensor must be selected in the options tree as described in \ref{sec:VelocityOptions}.

\subsection{Momentum boundary conditions} \label{sec:BCs-mom}

\index{boundary conditions!Momentum}

As with the scalar equations discussed previously in section \ref{sec:MP-AdvdifEqn}, any well posed problem requires appropriate boundary conditions for the momentum equation. Possible momentum boundary conditions are discussed below.

\subsubsection{Prescribed Dirichlet condition for momentum --- no-slip as a special case}\label{sec:bc_vector_dirichlet}
\index{boundary conditions!Dirichlet}
\index{momentum equation}

This condition for momentum is set by simply prescribing all three components of
velocity. For example, we might specify an inflow boundary where the normal component
of velocity is non-zero, but the two tangential directions are zero. A special case is
where all three components are zero and this is referred to as no-slip.

\subsubsection{Prescribed stress condition for momentum --- free-stress as a special case}\label{sec:bc_vector_stress}
\index{boundary conditions!prescribed stress}
\index{traction force}
\index{momentum equation}
\index{free stress}

As with the scalar equation (see section \ref{sec:bc_scalar_neumann}), applying Green's theorem to the stress term and the pressure
gradient in \eqref{mtm} results in a surface integral of the form
\begin{equation}\label{StressBC}
\tautens\cdot\bmn - p\bmn = \bmT,\quad \textrm{on}\quad \partial\Omega,
\end{equation}
where $\bmT$ is an applied `traction' force (actually a force per unit area or stress, it becomes
a force when the surface integral in the weak form is performed). An example of this might be were we set the vertical
component to zero (in the presence of a free surface) and impose the two tangential directions
(\eg a wind stress).

\index{boundary conditions!free stress}
The free-stress condition is the case where we take $\bmT\equiv\vec{0}$.

\subsubsection{Traction boundary condition for momentum --- free-slip as a special case}\label{sec:bc_vector_traction}
\index{boundary conditions!traction}
\index{momentum equation}
\index{free slip}
In this case the normal component of velocity can be prescribed (\eg inflow or
no-flow ($g=0$) through the boundary)
\begin{equation}
\bmu\cdot\bmn = g,\quad \textrm{on}\quad \partial\Omega.
\label{normal_flow_condition}
\end{equation}
The remaining two degrees of freedom are imposed by taking the
tangential component of \eqref{StressBC} and specifying the tangential component
of the force $\bmT_{\tau}$, \ie
\begin{equation*}
\bmtau\cdot(\tautens\cdot\bmn - p\bmn) = \bmtau\cdot\tautens\cdot\bmn = \bmT_{\tau},\quad \textrm{on}\quad \partial\Omega,
\end{equation*}
An example of this might be where a rigid lid is used (so normal component is zero)
and the tangential components are a prescribed wind stress (in which case we take
the two tangential directions to correspond to the available stress or wind velocity
information, \ie east-west and north-south) or bottom drag. The term
\emph{free slip}, is often used for the case
where the tangential components of stress are set to zero.

\subsubsection{Free surface boundary condition}\label{sec:FS}
\index{free surface}
\index{kinematic boundary condition}
In the case of a free surface, the normal component of the velocity is related
to the movement of the free surface through the \emph{kinematic boundary
condition}. The free surface height $\eta\equiv\eta(x,y,t)$ is measured from
the initial state of the fluid. The kinematic boundary condition is derived by
stating that a fluid particle following the flow at the free surface, should
remain at the free surface. In other words, its trajectory is described by
$t\mapsto (x(t), y(t),\eta(x(t),y(t),t))$. Its vertical velocity, $w$, should
therefore satisfy:
\begin{equation} \label{eq:kinematicbc1}
  \frac{D\eta}{Dt}=\bmu_H \cdot\nabla_H \eta +
  \frac{\partial\eta}{\partial t} = w \quad \textrm{on}\quad \Omega
\end{equation}
where $\nabla_H\equiv(\partial/\partial x,\partial/\partial y)^T$, and
$\bmu_H$ is the horizontal component of $\bmu$.
Using the fact that the normal vector $\vec n$ at the free surface is
$(-\frac{\partial \eta}{\partial x},-\frac{\partial \eta}{\partial y}, 1)^T/
\|(-\frac{\partial \eta}{\partial x},-\frac{\partial \eta}{\partial y}, 1)\|$,
this can be reformulated to
\begin{equation}\label{freesurf3}
\frac{\partial \eta}{\partial t}=\frac{\bmu \cdot \vec n}{\vec n \cdot \vec k},
\end{equation}
where $\vec k=(0,0,1)$ is the vertical normal vector. Note that in spherical
geometries $\vec k$ is the radial unit vector. Since this condition does not
restrict the normal velocity, it simply
prescribes what the free surface movement is, we still need a stress condition
in all directions, similar to equation $\eqref{StressBC}$. The tangential
components can be either free, $\bmT_\tau=0$, or given by a wind stress. For the
normal component, we get:
\begin{equation}
  \bmn\cdot(\tautens\cdot\bmn - p\bmn) = \bmn\cdot\tautens\cdot\bmn - p =
  -\patm \quad \textrm{on}\quad \partial\Omega.
  \label{NormalStressBC}
\end{equation}
In ocean applications, the deviatoric part of the normal stress condition is
usually neglected and a simple Dirichlet pressure boundary condition $p=\patm$
is applied instead.

\subsubsection{Wetting and drying}\label{sec:WD}
\index{free surface!Wetting and drying}
If wetting and drying occurs, the free surface boundary condition \ref{freesurf3} needs to be changed to ensure that the water level $\eta$ does not sink below the bathymetry level $b$:

\begin{equation}\label{eq:wd1}
\frac{\partial \max(\eta, b+d_0)}{\partial t}=\frac{\bmu \cdot \vec n}{\vec n \cdot \vec k},
\end{equation}
where $d_0$ is a threshold defining the minimum water depth.

The details of the wetting and drying algorithm are described in \cite{Funke2011}.

\section{Extensions, assumptions and derived equation sets}\label{sec:eqn_extensions}

In certain scenarios it is desirable to simplify the equations of sections \ref{sec:MP-AdvdifEqn} and \ref {sec:MP-MomEqn} according to various approximations. Such approximations can drastically reduce the complexity of the system under consideration while maintaining much of the important physics. In this section we consider approximate forms and assumptions of the conservation equations that are appropriate to different problems.

\subsection{Equations in a moving reference frame}\label{sec:coriolis}
\index{Coriolis}
Newton's second law holds in a fixed inertial reference frame, \ie
fixed with respect to the distant stars.
Examples of systems which one may wish to study with boundaries moving
with respect to this fixed inertial frame include translating and spinning tanks
and the rotating Earth. For these systems it is often convenient to rewrite the
underlying equations within the moving frame. Extra terms then need to be considered
which account for the fact that the acceleration of a fluid parcel relative to the
moving reference frame is different to the acceleration with respect to the fixed
inertial frame, and the latter is the one which allows us to invoke Newton's Laws.
For useful discussions see \citep{batchelor1967,cushman1994,gill1982}.

\begin{figure}\label{fig:rotating_frame}
\centering
\includegraphics[width=8.0cm]{misc_images/coordinates.pdf}
\caption{Schematic of coordinates in a frame rotating with a sphere. The rotation is about a vector
pointing from South to North pole. A point on the surface of the sphere $\bmx$ and its perpendicular
distance from the axes of rotation $\bmx_{\perp}$ are shown. The latitude of $\bmx$ is given by $\phi$
and the unit vectors $\bmi$, $\bmj$ and $\bmk$ represent a local coordinate axes at a point $\bmx$ in the rotating
frame: $\bmi$ points Eastwards, $\bmj$ points Northwards and $\bmk$ points in the radial outwards direction.}
\end{figure}

It is possible to form the equations with respect to a moving reference frame as long as the
additional force or acceleration term is included. In the case of a rotating frame that is
just by replacing the material derivative in the momentum equation by
\begin{equation}
\DDt{\bmu} + 2\bmOmega\times\bmu,
\end{equation}
where $\bmOmega$ is the angular velocity vector of the rotating
system.

Consider the Earth with a rotation vector in an inertial reference frame given by
\begin{equation}\label{eq:on_sphere_rotation}
\bmOmega=(0,0,\Omega)^T.
\end{equation}
In a local rotating frame of reference where the
$x$-axis is oriented Eastwards,
the $y$-axis is oriented Northwards and the $z$-axis is the local upwards direction,
the Earth's rotation vector is expressed as
\begin{equation*}
\bmOmega = \Omega \cos \phi\; {\bf j} + \Omega \sin \phi\; {\bf k} \equiv \Omega (0,\cos\phi,\sin\phi)^T,
\end{equation*}
where $\phi$ is the latitude.
The acceleration terms in the three momentum equations now have the form
{\setlength\arraycolsep{2pt}
\begin{eqnarray*}
&&\DDt{u} {\color{red}+} {\color{red}2\Omega\cos\phi\; w} - 2\Omega\sin\phi\; v,\\
&&\DDt{v} + 2\Omega\sin\phi\; u,\\
&&\DDt{w} {\color{red}-} {\color{red}2\Omega\cos\phi\; v}.
\end{eqnarray*}}

\subsubsection{The `traditional' approximation}
\index{traditional approximation}
Define the Coriolis and reciprocal Coriolis parameters \citep{cushman1994} respectively by
\begin{equation}\label{eq:coriolis_parameters}
f=2\Omega\sin\phi,\quad f^*=2\Omega\cos\phi.
\end{equation}
Due to dimensional considerations it is is usual to
drop the $f^*$ term and hence simply assume that
\begin{equation}\label{eq:f_omega}
\bmOmega=(0,0,f/2)^T,
\end{equation}
in the local frame of reference, \ie only to consider the locally vertical
component of the rotation vector.
This approximation, when taken along with the assumption of hydrostatic balance in the
vertical constitutes what is generally known as the traditional approximation in geophysical fluid
dynamics.

\subsubsection{The $f$-plane and $\beta$-plane approximations}
\index{Coriolis!f-plane@$f$-plane}
\index{Coriolis!b-plane@$\beta$-plane}
If the Coriolis parameter $f$ is approximated by a constant value:
\begin{equation}\label{eq:f-plane}
f=f_0,
\end{equation}
this is termed the $f$-plane approximation,
where $f_0 = 2\Omega\sin\phi_0$ at a latitude $\phi_0$.
This is obviously only an applicable approximation in a domain of interest
that does not have large extent in latitude.

For slightly larger domains a more accurate approximation is to use
\begin{equation}\label{eq:beta-plane}
f = f_0 + \beta y,
\end{equation}
where $y$ is the local coordinate in the Northwards direction.
Taking $\phi = \phi_0 + y/R_E$ and expanding \eqref{eq:coriolis_parameters}
in a Taylor series yields
\begin{equation*}
f = f_0 +2\Omega\cos\phi_0\frac{y}{R_E}+\ldots,\quad \beta = \frac{2\Omega}{R_E}\cos\phi_0,
\end{equation*}
where $R_E\approx\unit[6378]{km}$. Typical values of these terms are:
\begin{equation*}
\Omega = \frac{2\pi}{24\times 60\times 60} = 7.2722\times 10^{-5}\rads[],
\end{equation*}
(NB. a sidereal day should be used to give a more accurate value of $7.2921\times 10^{-5}\rads[]$)
and
\begin{center}\begin{small}
\begin{tabular}{c|ccc}
  &  $\phi_0 = 30$ & $\phi_0=45$ & $\phi_0 = 60$ \\  \hline
 $f_0$  & 7.2722e-05 & 1.0284e-04 & 1.2596e-04 \\
 $\beta$  & 1.9750e-11  &  1.6124e-11  &  1.1402e-11 \\
\end{tabular}\end{small}
\end{center}

\subsection{Linear Momentum}
\index{linear momentum}

Newton's second law states that the sum of forces applied to a body is equal to the time derivative of linear momentum of the body,
\begin{equation}\label{Newtons2nd}
 \sum \vec f = \d(m\vec u)/\d t.
\end{equation}
Applying this law to a control volume\footnote{The concept of a control volume and how they are defined within fluidity is discussed more in section \ref{ControlVolumeAdvection}} of fluid and making use of equation \ref{RTT} leads to the linear momentum equation for a fluid which can be written as
\begin{equation}\label{LinMom}
 \frac{\partial}{\partial t}\int_{V}\rho\vec u \d V = -\int_{S} \vec u \rho \vec u\cdot\vec n \d A+\int_{V}\vec F\rho \d V
                                                      +\int_{S} \sigtens\cdot\vec n \d A,
\end{equation}
where $V$ is the control volume, $S$ is the surface of the control volume and $\vec n$ is the unit normal to the surface of the control volume and $\vec F$ is a volume force per unit mass. Physically, \ref{LinMom} states that the sum of all forces applied on the control volume is equal to the sum of the rate of change of momentum inside the control volume and the net flux of momentum through the control surface. More details regarding the derivation and properties of this equation can be found in \citet{batchelor1967}.

\subsection{The Boussinesq approximation} \label{sec:boussinesq_approximation}
\index{density!reference}
\index{Boussinesq!approximation}

Under certain conditions, one is able to assume that density does not vary greatly about a mean reference density, that is, the density at a position $\vec x$ can be written as
\begin{equation}
\rho(\bmx,t) = \rho_0 + \rho'(\bmx,t),
\end{equation}
where $\rho'\ll\rho_0$. Such an approximation, namely, the Boussinesq
approximation, involves two steps. The first makes use of \eqref{eq:divfree} ---
mass conservation thus becomes volume conservation and sound waves are filtered.
The second part of the Boussinesq approximation follows by replacing $\rho$ by
$\rho_0$ in all terms of \eqref{viscous_fluids_1}, except where density is
multiplied by gravity (\ie in the buoyancy term where full density must be retained --- these are the density variations that drive natural convection). This yields
\begin{equation}
\rho_0\DDt{\bmu} -\nabla\cdot\sigtens = \rho \bmg + \rho_0\bmF,
\end{equation}
where the gravity term has been separated explicitly from the forcing term $\bmF$ and $\bmg$ is the gravitational vector (e.g. $\bmg = -g\bmk$ in planar problems when gravity points in the negative $z$ direction and $\bmg = -g\vec r$ on the sphere).

\subsubsection{Buoyancy and hydrostacy}\label{sec:buoyancy_hydrostacy}
\index{density!reference}
\index{pressure!perturbation}
In absence of viscosity the vertical momentum equation can be written as:
\begin{equation}\label{eq:vertmom}
\DDt[t]{w} = -\ppx[z]{p} + \rho g.
\end{equation}
If the fluid is in a state of rest, the left hand-side is zero, giving:
\begin{equation}\label{eq:hydrostatic_balance}
\ppx[z]{p} = -\rho g
\end{equation}
This is known as hydrostatic balance. If the pressure $p_{\text{top}}$ at the top of the domain
is given, e.g., an atmospheric pressure or a given ice load,
the hydrostatic pressure can be computed as
\begin{equation} \label{eq:hydrostatic_pressure}
  p(x,y,z) = p_{\text{top}}(x,y) + \int_{z=z}^{\eta(x,y)} \rho g dz,
\end{equation}
where $z=\eta(x,y)$ denotes the location of the top of the domain, e.g. a free
surface. Here, we assume a coordinate system with $x$ and $y$ in the horizontal
and $z$ in the vertical direction. The integral represents the weight of the
water above a point at a given height $z$. For a constant density this simplifies to:
\begin{equation}
  p(x,y,z) = p_{\text{top}}(x,y) + \rho g \left(\eta(x,y)-z\right).
\end{equation}

If we consider the general case, that is not in hydrostatic balance and where
the density is not constant, but we have a small perturbation density
$\rho'$, we may still write:
\begin{equation}
  p(x,y,z) = p_0(z) + p'(x,y,z) = -\rho_0 g z + p'(x,y,z).
\end{equation}
Here, we have separated the pressure $p_0=-\rho_0 g z$, due to the weight of a fluid of reference density $\rho_0$ below a reference level at $z=0$ near the top, from a perturbation pressure $p'$. This
perturbation pressure still contains the hydrostatic pressure due to the
perturbation density, and non-hydrostatic parts of the pressure. In the
case of a free surface at $z=\eta$, the additional weight of $\rho g\eta$, that
is not taken into account in $p_0$ is also included in $p'$.

By construction, the pressure gradient of $p_0$ exactly balances $\rho_0\bmg$,
the reference density part of the gravity term
\begin{equation}
  \rho\bmg=\rho_0\bmg+\rho'\bmg
\end{equation}
We can therefore simply replace $p$ by $p'$ in the pressure gradient term, if we
subtract $\rho_0\bmg$ from the gravity term, to obtain a buoyancy term
$\rho'\bmg$.

\subsubsection{Combining pressure and free surface}
\index{free surface}
In many models with a free surface, the $\rho_0 g\eta$ part of pressure (known
in ocean models as barotropic pressure), is subtracted from the pressure as
well,
and treated separately. Since this part still depends on the horizontal
coordinates, its 3D gradient does not simply balance with the buoyancy, and a
horizontal gradient $\rho_0g\nabla_{\text{H}}\eta$ remains, which is added as
an extra term in the momentum equation in such models.
In \fluidity{}
however, we keep this part in the perturbation pressure and it is therefore
solved for in conjunction with the non-hydrostatic parts of pressure.

In this approach, care has to be taken when applying boundary conditions. If a
pressure boundary condition of $p=\patm$ is enforced at $z=\eta$, then the
boundary condition for the perturbation changes to:
\begin{equation}
  p'=p+\rho_0 z=\patm+\rho_0 g\eta
  \quad \textrm{on}\quad \partial\Omega.\label{freesurfacepressure}
\end{equation}
If a normal stress condition is applied, see \eqref{NormalStressBC}, we similarly get:
\begin{equation}
  -\bmn\cdot\tautens\cdot\bmn + p' = \patm + \rho_0 g\eta
  \quad \textrm{on}\quad \partial\Omega.
  \label{normalstressfreesurface}
\end{equation}

\subsubsection{The non-hydrostatic Boussinesq equations}\label{sec:typical_ICOM_equations}
\index{Boussinesq!equations}
\index{momentum equation}
\index{continuity equation}

Applying the approximations outlined in section \ref{sec:boussinesq_approximation} to (\ref{viscous_fluids_1}) and (\ref{mass_conservation_2}), along with scalar transport equations for salinity and temperature (see (\ref{eq:general_scalar_eqn})) and an appropriate equation of state (see section \ref{sec:equation_of_state}), the three-dimensional non-hydrostatic Boussinesq equations can be written as

\begin{subeqnarray}
\frac{\pp\bmu}{\pp t} + \bmu\cdot\nabla \bmu + 2 \bmOmega \times \bmu
&=& - \nabla p' + \rho' \bmg + \nabla\cdot \tautens + \bmF,
\slabel{mtm}\\
\nabla\cdot {\bmu}&=&0,\slabel{conty}\\
\frac{\pp T}{\pp t} + \bmu\cdot\nabla  T  &=&
\nabla . \left ( \kaptens_T  \nabla T\right),\slabel{heat}\\
\frac{\pp S}{\pp t} + \bmu\cdot\nabla  S  &=&
\nabla . \left ( \kaptens_S  \nabla S\right),\slabel{salt}\\
f(p,\rho,T,\ldots)&=&0,\slabel{state}
\label{boussinesq}
\end{subeqnarray}

where $p'$ is the perturbation pressure (see section \ref{sec:buoyancy_hydrostacy}),
$\rho=\rho_0+\rho'$ where $\rho'(=(\rho-\rho_0)/\rho_0)$ is the perturbation density,
$T$ is the temperature, $S$ is salinity, and
$\tautens,\kaptens_T,\kaptens_S$ are the viscosity, thermal diffusivity and saline
diffusivity tensors respectively.
The rotation vector is $\bmOmega$, and $\bmF$ contains additional source terms such as the astronomical tidal forcing. A discussion
regarding the validity of the Boussinesq approximation is given in \cite{Gray1976}

\subsection{Supplementary boundary conditions and body forces}

\subsubsection{Bulk parameterisations for oceans}
\index{boundary conditions!bulk parameterisations}

In order to simulate real-world ocean scenarios, realistic boundary conditions for the momentum, freshwater and heat fluxes
must be applied to the upper ocean surface. \fluidity can apply such boundary conditions in the form of the bulk formulae of \citet{large2004},
COARE 3.0 \citep{fairall2003} and \citet{kara2005} in combination with the ERA-40 reanalysis data \citep{Uppala2005}.

Three surface kinematic fluxes calculated: heat -- $\langle w\theta \rangle$,
salt -- $\langle ws \rangle$, and momentum -- $\langle wu \rangle$ and $\langle wv \rangle$,
which can be related to the surface fluxes of heat $Q$, the
freshwater $F$, and the momentum $\overrightarrow\tau=\left(\tau_u,\tau_v\right)$, via:
\begin{align}
\langle w\theta \rangle &= Q\left(\rho C_p \right)^{-1} \\
\langle ws \rangle &= F\left(\rho^{-1}S_0\right) \\
\left(\langle wu \rangle, \langle wv \rangle\right) &=
\overrightarrow{\tau}\rho^{-1} =
\left(\tau_u,\tau_v\right)\rho^{-1}
\end{align}
where $\rho$ is the ocean density, $C_p$ is the heat capacity (4000 Jk$S^{-1}$K$^{-1}$)
and $S_0$ is a reference ocean salinity, which is the current sea surface salinity.
These fluxes are then applied as upper-surface Neumann boundary conditions on
the appropriate fields.

\subsubsection{Co-oscillating boundary tides}
\index{boundary conditions!boundary tides}
\label{sec:boundary_tide}

Boundary tides can be applied to open ocean domain boundaries through setting a Dirichlet condition on the non-hydrostatic part of the pressure. Co-oscillating tides are forced as cosine waves of specified phase and amplitude along designated boundaries:
\begin{equation}
h=\sum_{i}A_{i}\cos(\sigma_{i} t -\phi_{i})
+\sum_{j}A_{j}\cos(\sigma_{j} t -\phi_{j})
+\sum_{k}A_{k}\cos(\sigma_{k} t -\phi_{k}),
\label{eq:co-oscillating-tide}
\end{equation}
where $h$ is the free surface height (m), $A$ is the amplitude of the tidal
constituent (m), $t$ is the time (s), $\phi$ is the phase of the tidal constituent
(radians) \citep{Wells2008}.

The nature of the co-oscillating tide can take the form of either one fixed cosine wave of constant amplitude
and phase applied across the entire length of the boundary, or it can be variable as delimited via an
interpolation of different amplitudes and phases at a series of points spread along the boundary \citep{Wells2008}.
Within \fluidity, co-oscillating boundary tides can be applied through \eg the FES2004 data set (see \ref{sec:tides_in_the_med}).

\subsubsection{Astronomical tides}\label{sec:AST}
\index{body forces!astronomical tides}
\label{astronomical}

Astronomical forcing can also be applied to a fluid as a body force\footnote{Note that use of the word body force in this context should not be confused with the application of a body force through the options tree discussed later in \ref{chap:configuration}. The astronomical forcing is applied separately.}.
The astronomical tidal potential is calculated at each node of the finite element mesh using the
multi-constituent equilibrium theory of tides equation:
  \begin{eqnarray}
\eta_{eq}\left(\lambda,\theta,t\right)&=&\sin^{2}\theta\sum_{i}A_{i}\cos\left(\sigma_{i}t+\chi_{i}+2\lambda\right)\nonumber\\
&+&\sin2\theta\sum_{j}A_{j}\cos\left(\sigma_{j}t+\chi_{j}+\lambda\right)\\
&+&\left(3\sin^{2}\theta-2\right)\sum_{k}A_{k}\cos\left(\sigma_{k}t+\chi_{k}\right)\nonumber,
\label{eq:multi-constituent-eq_theory}
  \end{eqnarray}
where $\eta_{eq}$ is the equilibrium tidal potential (m), $\lambda$ is the east longitude (radians), $\theta$ is the colatitude
$[(\pi/2)-{\text{latitude}}]$, $\chi$ is the astronomical argument (radians), $\sigma$
is the frequency of the tidal constituent (s$^{\text{-1}}$), $t$ is universal standard
time (s) and $A$ is the equilibrium amplitude of the tidal constituent (m). Subscript
$i$ represents the semi-diurnal constituents (e.g. M$_{\text{2}}$), subscript $j$
the diurnal constituents (\eg K$_{\text{1}}$) and subscript $k$ the long period
constituents (\eg M$_{\text{f}}$; \citealp{Wells2008}).
The overall forcing
is applied as the product of the gradient of the resulting equilibrium tidal potential and the acceleration due
to gravity (g) \citep{Mellor1996, Kantha2000, Wells2007}.
The multi-constituent equilibrium theory of tides is flexible in that it enables astronomical tides to be forced as
individual constituents
(e.g. M$_{2}$ or S$_{2}$) or as a combination of different constituents (e.g. M$_{2}$ + S$_{2}$) \citep{Wells2008}.

As there is no interest in calculating the tide for an exact date, the astronomical argument
is typically excluded from the multi-constituent theory of tides equation for ICOM applications meaning that all satellites
start at 0$^{\circ}$ latitude \citep{Wells2008}.

The astronomical tidal potential can be modified to account for the deformation of the solid Earth
(the body tide) if desired. The multi-constituent equilibrium theory of tides equation includes the effects of the
solid Earth deformation, adding this to the overall free surface height.
This is fine
when validating model results against measurements that record the overall elevation of the
Earth's oceans (e.g. satellite altimeter readings and surface tide gauges). If however, the model
is validated against measurements that do not include the effects of the Earth's body tide
such as with pelagic pressure gauges, then a correction to the equilibrium tidal potential
is required. This can be applied as:
\begin{equation}
\eta=(1+k-h)\eta_{eq},
\label{eq:body-tide}
\end{equation}
where $\eta$ is the corrected tidal potential, $\eta_{eq}$ is the uncorrected equilibrium tidal potential
and $k$ (0.3) and $h$ (0.61) are Love numbers (after \citealp{Love1909}).
Both $k$ and $h$ are dimensionless measures of the elastic behaviour of the solid Earth. $k$ accounts for the enhancement to the
Earth's gravitational potential brought about by the re-distribution of the Earth's mass whereas $h$
is a correction for the physical distortion to the Earth's surface \citep{Pugh1987}.

The exact values of $k$ and $h$ vary for different tidal constituents and the numbers shown are given
for the semi-diurnal M$_{\text{2}}$ constituent. Typical variations to $k$ and $h$ are less than 0.01 which
corresponds to a $<$1\% change to the equilibrium tidal potential.
These errors are sufficiently small that the stated values are a suitable approximation to
use in body tide corrections to the majority of tidal constituents.

\subsection{Shallow water equations}
\index{shallow water equations}
In many oceanographic simulations, the horizontal lengths scales are much larger
than the vertical. It can be shown that this leads to small vertical velocities,
and in particular small vertical accelerations. Thus, in the vertical momentum
equation \eqref{eq:vertmom} the left hand-side can be neglected entirely. This
means we assume the total pressure is given by \eqref{eq:hydrostatic_pressure},
the so-called hydrostatic or shallow water approximation.

Although shallow water flows are nearly horizontal, they may still exhibit a
three-dimensional structure, due to e.g. bottom friction. In many applications
however, these 3D effects are not important and it is sufficient to only study
the depth-averaged flow velocities and free surface elevations
\citep{vreugdenhil1994}. The depth-integrated continuity equation can be derived
by working out the horizontal divergence of the horizontal velocities
integrated, vertically, from the bottom $z=b$ to the free surface $z=\eta$:
\begin{equation}
\begin{split}
  \nabla_H \cdot \int_{z=b}^{z=\eta} \bmu_H &=
  \bmu_H\big|_{z=\eta}\cdot\nabla_H \eta
  -\bmu_H\big|_{z=b}\cdot\nabla_H b + \int_{z=b}^{z=\eta} \nabla_H\cdot\bmu_H \\
  &=
  \bmu_H\big|_{z=\eta}\cdot\nabla_H \eta
  -\bmu_H\big|_{z=b}\cdot\nabla_H b
  + \int_{z=b}^{z=\eta} \nabla\cdot\bmu-\frac{\partial w}{\partial z} \\
  &=
  \left[\bmu_H\cdot\nabla_H \eta + w\right]_{z=\eta}
  -\left[\bmu_H\cdot\nabla_H b + w\right]_{z=b} \\
  &=-\frac{\partial\eta}{\partial t} + 0
  \label{eq:swe1_derivation}
\end{split}
\end{equation}
Here, we've made use of the divergence-freeness of the flow, and, in the last
step, the kinematic boundary condition \eqref{eq:kinematicbc1} at the free
surface and no-normal-flow condition at the bottom.

After defining the total water depth $h=\eta-b$, and the depth-averaged
velocity:
\begin{equation}
  \bar\bmu = \frac{\int_{z=b}^{z=\eta} \bmu_H}{h},
\end{equation}
the depth-averaged shallow water equations (in non-conservative form) are given
by:
\begin{subequations} \label{eq:swe}
\begin{align}
  \frac{\partial\eta}{\partial t} + \nabla\cdot\left(h\bar\bmu\right) &=0,
  \label{eq:swe1} \\
  \frac{\partial\bar\bmu}{\partial t} + \bar\bmu\cdot\nabla\bar\bmu
  + g\nabla\eta + C_D \frac{\|\bar\bmu\|\bar\bmu}{h} &=0. \label{eq:swe2}
\end{align}
\end{subequations}
Note that because this is now a purely two-dimensional equation, we may drop the
subscript $H$ from the gradient operator $\nabla$. The continuity equation
\eqref{eq:swe1} follows directly from \eqref{eq:swe1_derivation}.
For the derivation of the depth-averaged momentum equation \eqref{eq:swe2} see
e.g. \citet{vreugdenhil1994}. The last term in that equation is a quadratic drag term, with
dimensionless coefficient $C_D$, that represents the bottom friction.

\subsection{Multi-material simulations}
\index{multi-material flow}
The ability to differentiate between regions with distinct material properties is of fundamental importance in the modelling of many physical systems. Two different approaches exist for achieving this: the multi-material approach and the multiphase approach. The multi-material approach is implemented within \fluidity, and the multiphase approach (discussed in the next subsection) is currently under development.
In situations where the model can resolve physical mixing of immiscible materials, or where there is no mixing, only one velocity field (and hence one momentum equation) is required to describe the flow of all materials. The \emph{multi-material} approach, considers all materials to be immiscible materials separated by a sharp interface.

In a multi-material approach, the various forms of the conservation equations can be solved for multiple material flows if the point-wise mass density $\rho$ in the equations is defined as the bulk density at each point.  If the flow comprises $n$ materials and the volume fraction of the $i^{th}$ material is denoted $\phi_i$ then the bulk density is given by:
\begin{equation}
\rho = \sum_{i=1}^n \phi_i\rho_i
\end{equation}
where $\rho_i$ is the density of each material.  For incompressible materials $\rho_i = \rho_{i0}$; for materials whose density is defined by an equation of state (see section~\ref{sec:equation_of_state}) $\rho_i = f(p,T,S,\ldots)$.  Conservation of mass at each point also requires that
\begin{equation}
\sum_{i=1}^n \phi_{i} = 1.
\end{equation}

In an $n$-material problem, the multi-material approach requires that $n-1$ advection equations are solved, to describe the transport of the volume fraction of all but one of the materials.  The volume fraction of the remaining material can be derived from the other volume fractions by
\begin{equation}\label{diagnosticvolfrac}
\phi_{n} = 1 - \sum_{i=1}^{n-1}\phi_{i}.
\end{equation}
The transport of the $i^{th}$ volume fraction is given by
\begin{equation}
\ppt{\phi_i} + \bmu\cdot\nabla\phi_i = 0,
\end{equation}
where the volume fraction field at time zero must be specified.

\subsection{Multiphase simulations}
\label{sec:multiphase_equations}
\index{multiphase flow}
Multiphase flows are defined by \cite{prosperettiEtAl2007} to be flows in which two or more phases of matter (solid, liquid, gas, etc) are simultaneously present and are allowed to inter-penetrate. Simple examples include the flow of a fizzy drink which is composed of a liquid and a finite number of gas bubbles, the transportation of solid sediment particles in a river, and the flow of blood cells around the human body.

Further to the above definition, each phase is classed as either \textit{continuous} or \textit{dispersed}, where a continuous phase is a connected liquid or gas substance in which dispersed phases (comprising a finite number of solid particles, liquid droplets and/or gas bubbles) may be immersed \citep{croweEtAl1998}.

To enable the mixing and inter-penetration of phases, a separate velocity field (and hence a separate momentum equation) is assigned to each one and solved for. Extra terms are then included to account for inter-phase interactions. Furthermore, the model currently assumes no mass transfer between phases, incompressible flow, and a common pressure field $p$ so that only one continuity equation is used. The form of the governing equations depends on whether we are dealing with incompressible or compressible flow.

\subsubsection{Incompressible flow}
For an incompressible multiphase flow, the continuity equation and momentum equation for phase $i$ (based on the derivation in \cite{ishii1975}, written in non-conservative form) are:
\begin{equation}
\sum_{i=1}^N\nabla\cdot\left(\alpha_i\mathbf{u}_i\right) = 0,
\end{equation}
\begin{equation}
\alpha_i\rho_i\frac{\partial \mathbf{u}_i}{\partial t} +
\alpha_i\rho_i\mathbf{u}_i\cdot\nabla\mathbf{u}_i =
-\alpha_i\nabla p + \alpha_i\rho_i\mathbf{g} +
\nabla\cdot\left(\alpha_i\tautens_i\right) +
\mathbf{F}_i,
\end{equation}
where $\mathbf{u}_i$, $\rho_i$, $\tautens_i$ and $\alpha_i$ are the velocity, density, viscous stress tensor and volume fraction of phase $i$ respectively, and $\mathbf{F}_i$ represents the forces imposed on phase $i$ by the other $N-1$ phases. Details of momentum transfer terms are given in Chapter \ref{chap:configuration}. For more information about the incompressible multiphase flow model implemented in Fluidity, see the paper by \cite{Jacobs_etal_2013}.

\subsubsection{Compressible flow}
Fluidity currently only supports the case where the continuous phase is compressible. All other phases (the dispersed phases) are incompressible. The momentum equation is the same as the one given above, but the continuity equation becomes:
\begin{equation}
\alpha_1\frac{\partial\rho_1}{\partial t} + \nabla\cdot\left(\alpha_1\rho_1\mathbf{u}_1\right) + \rho_1\left(\sum_{i=2}^N\nabla\cdot\left(\alpha_i\mathbf{u}_i\right)\right) = 0,
\end{equation}
where the compressible phase has an index of $i=1$ here.

An internal energy equation may also need to be solved depending on the equation of state used for the compressible phase. This is given by:
\begin{equation}
\alpha_i\rho_i\frac{\partial e_i}{\partial t} + \alpha_i\rho_i\mathbf{u}_i\cdot\nabla e_i = -\alpha_i p\nabla\cdot\mathbf{u}_i + \nabla\cdot\left(\alpha_i\frac{k_i}{C_{v,i}}\nabla e_i\right) + Q_i
\end{equation}
where $e_i$ is the internal energy, $k_i$ is the effective conductivity, $C_{v,i}$ is the specific heat (at constant volume), and $Q_i$ is an inter-phase energy transfer term described in Chapter \ref{chap:configuration}. Note that, in Fluidity, the pressure term is always neglected in the (incompressible) particle phase's internal energy equation.


COMPARE THIS TO THE GROUP-WISE MODEL Chapter

Emphasize that covariates are important.  There's no reason to ignore
them just because this was common in traditional tests.

Difference between experimental work and field or observational work.

Title of Fisher's book: ``for experimental workers''

\subsubsection{Differences Between Means of Two Groups: the Two-Sample t-test}

The one-way \ANOVA\ approach works when comparing the means among
groups.  It can be applied to any number of groups: 2 groups, 3
groups, 4 groups, and so on.

You might be surprised to hear that there is a special test, called
the \newword{two-sample t-test} for testing whether the means are
different between two groups.  I'll illustrate it using the question
of whether the current population survey data give
significant evidence that wages are different between men and women
--- two groups.

\index{P}{summary@\texttt{summary}!for linear models}
\index{P}{Hypothesis Testing!summary@\texttt{summary}!for linear models}

Using the modeling approach, the calculations are:
\begin{Schunk}
\begin{Sinput}
> cps = fetchData("cps.csv")
> mod3 = lm( wage ~ sex, data=cps)
> summary(mod3)
\end{Sinput}
\begin{Soutput}
...
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    7.879      0.322   24.50  < 2e-16 ***
sexM           2.116      0.437    4.84  1.7e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 5.03 on 532 degrees of freedom
Multiple R-squared: 0.0422,	Adjusted R-squared: 0.0404
F-statistic: 23.4 on 1 and 532 DF,  p-value: 1.7e-06
\end{Soutput}
\end{Schunk}
The p-value is very small, justifying rejection of the null
hypothesis.  (This conclusion is subject to some legitimate
criticism, since no attempt has been made to adjust for covariates
such as \VN{age} or the level of education.  With a modeling approach,
you can add these covariates to the model.  But the t-test and one-way
\ANOVA\ approaches don't allow this to be done.  That's sufficient
reason not to use them.  For the purpose here, to demonstrate how to
perform a t-test, I'll continue on without claiming that the use of
the method is justified in anything but a formal sense.)

The streamlined form of the calculation produces the same F and p-value:
\begin{Schunk}
\begin{Sinput}
> anova(mod3)
\end{Sinput}
\begin{Soutput}
Analysis of Variance Table

Response: wage
           Df Sum Sq Mean Sq F value  Pr(>F)
sex         1    594     594    23.4 1.7e-06 ***
Residuals 532  13483      25
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{Soutput}
\end{Schunk}

Now to do this in the form of a t-test.  The command uses the familiar
modeling language to identify the response variable (\VN{wage)} and
the explanatory grouping variable (\VN{sex}).
\begin{Schunk}
\begin{Sinput}
> t.test( wage ~ sex, data=cps, var.equal=TRUE)
\end{Sinput}
\begin{Soutput}
	Two Sample t-test

data:  wage by sex
t = -4.84, df = 532, p-value = 1.703e-06
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.97 -1.26
sample estimates:
mean in group F mean in group M
           7.88            9.99
\end{Soutput}
\end{Schunk}
Rather than computing F, the t-test computes a statistic called the t
value.  The relationship between the two is simple: $t^2 = F$.  So,
the t of $-4.84$ corresponds to F $=23.4$.
Similarly, in the t-test there is a quantity called the ``degrees of
freedom.''  This corresponds to the degrees of freedom in the
denominator in the F test.  The p-values from the t-test and the F
test are the same!

\subsubsection{Technical Note: The Unequal Variance t-test}
\label{sec:unequal-variance-t-test}
\index{C}{t-test!unequal variance}

Actually, the above t-test is not the one that many people would
perform in this situation.  The full name of the above test is the
\newword{equal-variance t-test}.
I include this section mainly for the
reader who has been told about the
\newworddef{unequal-variance t-test}{unequal variance t-test}.
The issue of whether to use an equal-variance test or an unequal
variance test is one of the places where the approach taken in this
book rubs against conventional pedagogical practice.

The term {\bf equal variance} refers to an extension of the null
hypothesis: rather than the null hypothesis being that the population
means of the response variable are the same in the two groups, the
null is expanded to say also that the population variances are equal.
The named argument \code{var.equal=TRUE} is what specifies this
additional assumption to the software.

If you suspend the assumption that the group variances are equal, the
appropriate t-test test takes on another form, the \newword{unequal
  variance t-test}.  Here it is:
\begin{Schunk}
\begin{Sinput}
> t.test( wage ~ sex, data=cps)
\end{Sinput}
\begin{Soutput}
	Welch Two Sample t-test

data:  wage by sex
t = -4.89, df = 531, p-value = 1.369e-06
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.97 -1.27
sample estimates:
mean in group F mean in group M
           7.88            9.99
\end{Soutput}
\end{Schunk}
The results of the unequal variance t-test are somewhat different from
those of \ANOVA\ or the equal variance t-test. To summarize these
differences:

\bigskip
\centerline{\begin{tabular}{lccc}
 & & equal var. & unequal var.  \\
 &  F-test & t-test & t-test \\\hline
D.F.& 532 & 532 & 530.5 \\
t & & $-4.84$ & $-4.885$  \\
t$^2$ or F & 23.4 & 23.4 & 23.86 \\
p-value & $1.7\times10^{-6}$  & $1.7\times10^{-6}$ & $1.369\times10^{-6}$\\
\end{tabular}}
\bigskip
The values provided by the equal and unequal variance t-tests
are different but very close.

\index{C}{t-test!and adjustment}
\index{C}{adjustment!and t-test}
\index{C}{equal-variance t-test}
\index{C}{unequal variance t-test}

Which test to use?  Many people will argue that the unequal variance
t-test is the appropriate test to use when you don't have a good
reason to assume that the variances of the two groups are different.
This is reasonable enough if one is trying to choose between the
equal-variance and the unequal-variance t-test.  But there is an
``elephant in the room,'' an aspect of the situation that dominates
things but that, for some reason, people don't talk about.  This
elephant is the covariates --- the other explanatory variables
that you should be adjusting for.  The t-test approach doesn't provide
any capability for such adjustment.  Indeed, if you try to add another
explanatory variable to the t-test command, you get an error message:
\begin{Schunk}
\begin{Sinput}
> t.test( wage ~ sex + educ, data=cps)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in t.test.formula(wage ~ sex + educ, data = cps) :
  'formula' missing or incorrect
\end{Soutput}
\end{Schunk}
The same applies to trying to do a t-test with a quantitative
explanatory variable:
\begin{Schunk}
\begin{Sinput}
> t.test( wage ~ educ, data=cps)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
Error in t.test.formula(wage ~ educ, data = cps) :
  grouping factor must have exactly 2 levels
\end{Soutput}
\end{Schunk}

\index{C}{ANOVA!covariates}
\index{C}{covariate!ANOVA}

The \ANOVA\ approach easily allows the addition of covariates to the
model.

If you find yourself in a situation where there are no covariates and
you are comparing two groups with potentially unequal variances, it
makes sense to use the unequal variance t-test.  I regard it as a
special-purpose method that should be used only in such special
situations.  As it happens, the results you get with the
equal-variance t-test and the unequal-variance t-test will be very
close ... unless the variances of the two groups are hugely different.
And, if the variances are indeed so different, you should be asking
yourself deeper questions, such as ``Why am I interested in the group
mean in the first place?  Isn't the difference in variance telling me
something, too?''

\subsubsection{The Grand Mean: the One-Sample t-test}
\index{C}{t-test!one-sample}
\index{C}{t-test!computing}
\index{C}{one-sample t-test}

Occasionally, the question to be asked of a variable is very simple:
Is there evidence that the overall mean is different from some specified
value.  For example, suppose the government had published figures
saying that the mean wage was \$9.25 per hour
at the time the current population survey
data were collected.  You're interested to know whether the CPS data
are consistent with this claim.

The one-sample t-test is designed to deal with this situation.
\begin{Schunk}
\begin{Sinput}
> t.test( cps$wage, mu=9.25)
\end{Sinput}
\begin{Soutput}
	One Sample t-test

data:  cps$wage
t = -1.02, df = 533, p-value = 0.3101
alternative hypothesis: true mean is not equal to 9.25
95 percent confidence interval:
 8.59 9.46
sample estimates:
mean of x
     9.02
\end{Soutput}
\end{Schunk}
The named argument \code{mu=9.25} specifies the population value of
the mean under the null hypothesis.  In this case, the p-value is
large, $0.3101$, so there is no reason to reject the null hypothesis.

The one-sample t-test is equivalent to the F test applied to a model
where the only explanatory model term is the intercept.  As always,
$t^2 = F$, so the effective F value from the t-test is $-1.016^2 = 1.032$
\begin{Schunk}
\begin{Sinput}
> mod4 = lm( wage - 9.25 ~ 1, data=cps)
> anova(mod4)
\end{Sinput}
\begin{Soutput}
Analysis of Variance Table

Response: wage - 9.25
           Df Sum Sq Mean Sq F value Pr(>F)
Residuals 533  14077    26.4
\end{Soutput}
\end{Schunk}
>
The results are identical to the one-sample t-test. (There is no
unequal-variance, one-sample t-test for the simple reason that in the
one-sample t-test there are no groups to have difference variances!)

Notice that in forming the model,
the null hypothesis parameter, $9.25$ has been subtracted
from the response variable in the modeling statement.  This is
essential.  Otherwise, the test will be whether the sample mean is
consistent with a population value of zero.  Formally, that's a
possible null hypothesis, but it makes no sense in this situation.

One important use for the one-sample t-test occurs in situations where
measurements have been made in pairs, for example, before-and-after
measurements.  When used this way, the one-sample t-test is called a
\newword{paired t-test}.  The idea is to test the difference within
each pair to see if it is non-zero.  As you'll see in the next
chapter, \ANOVA\ can also be configured to perform such paired tests
and, in fact, is able to generalize them to multiple measurements and
to adjust for covariates.
\index{C}{t-test!paired}


% !TeX root = ../Thermostats.tex
As mentioned earlier in section \ref{cemd} we need an algorithm to artificially keep our system at a desired temperature. Such algorithms are usually called thermostats. Since the beginning of molecular dynamics simulations, various types of thermostats have been proposed. Many rely on random numbers and try to imitate the random collisions between particles of the system and the heat path, while others take on a more deterministic approach. In general, thermostats can be divided into four broad categories:
\begin{itemize}
\item \textbf{Stochastic methods:} A system variable (temperature or velocity) is sampled from a desired probability distribution (e.g. Andersen)
\item \textbf{Strong-coupling methods:} A system variable is constrained to the desired value (e.g. Gaussian)
\item \textbf{Weak-coupling methods:} A system variable is driven towards the desired value (e.g. Berendsen)
\item \textbf{Extended system dynamics:} Additional degrees of freedom are added to the system to get the desired phase space density (e.g. Nosé-Hoover)
\end{itemize}
In this project we try to compare some of the most common thermostats, which will be described in detail in the following sections.

\subsection{Gaussian}\label{th:gaussian}
This thermostat rescales the velocities of the particles to match the kinetic energy of the particles to the expected value $K_0$ (derived from the Boltzmann distribution at the desired temperature T). This is achieved by calculating the current kinetic energy $K$ of the system and rescaling the velocities $v$ according to the following formula:
%: temperaturberechnung in unserer simulation irgendwo definieren und hier referenzieren.
\begin{equation}
v'  = v\cdot \sqrt{\frac{K_0}{K}}\label{eq:gauss}
\end{equation}
One should note here that this approach produces a constant kinetic energy, and thus corresponds to an isokinetic rather than to a canonical ensemble. However, many properties of statistical systems become independent of the ensemble as $N$ goes to infinity, so the choice of ensemble becomes irrelevant.
Furthermore, the scaling leads to discontinuous velocities. A different formulation of this thermostat is based on the Gaussian principle of least constraint. Here, the equations of motion are altered by adding a friction term in order to keep the temperature fixed. The new equations of motion now have the following form:
\begin{align*}
&  \dot{q} = v && \dot{v} = - \frac{F}{m} - \zeta v /numberthis
\end{align*}
The friction constant $\zeta$ is determined via Lagrangian multipliers in such a way that the total kinetic energy is constant, i.e.
\begin{equation}
\sum_i \frac{m_i v_i^2}{2} = \frac{N_f k T}{2}
\end{equation}
and
\begin{equation}
\sum_i m_i v_i \dot{v}_i = 0
\end{equation}
which leads to
\begin{equation}
\zeta = \frac{\sum_i v_i F_i}{\sum_i m_i v_i^2}
\end{equation}
In a simulation, this version of the Gaussian thermostat is easier implemented in conjunction with a leapfrog integration algorithm. Since the only difference between velocity verlet and leapfrog integration is the time at which the velocity is evaluated, we used this implementation for the Gaussian thermostat.
\input{./sections/patrick.tex}
\subsection{Berendsen}
This thermostat proposed by Berendsen et al. \cite{Berendsen1984} supplements the Hamiltonian of the system with an additional first order equation where the difference between the kinetic energy at a given time $K$ and its target value $K_0$ (modulo the change rate $\tau$) drive the change in kinetic energy.
\begin{equation}
{dK} = \frac{(K_0-K)dt}{\tau} \label{eq:berendsen}%die formel sieht ziemlich blöd aus finde ich grad - hat jmd eine idee für eine bessere konvention bzgl wunsch und aktueller temperatur?
\end{equation}
The velocities are then scaled to fit these kinetic energies at every time step (as in \eqref{eq:gauss}).

One drawback of this thermostat is that in general it does not sample from a well defined ensemble \cite{Morishita2000}. Furthermore, it lacks a conserved quantity \cite{Bussi2007} For these reasons, similar considerations as for the Gaussian thermostat (\ref{th:gaussian}) apply.
\subsection{Bussi--Donadio--Parrinello}
Bussi et al.\cite{Bussi2007} improved Berendsen's method by switching the target value from equation \eqref{eq:gauss} to a changing, time dependent stochastic variable and adjusting the temperature over several time steps as Berendsen does (via eq. \eqref{eq:berendsen}).

Therefore the auxiliary dynamics can be expressed via the following formula:
\begin{equation}
dK = (K_0- K) \frac{dt}{\tau} + \underbrace{2\sqrt{\frac{K_0\cdot K}{N_f}}\frac{dW}{\sqrt{\tau}}}_{\text{stochastic term}}
\end{equation}
where $dW$ corresponds to a Wiener noise --- a random number drawn from a normal distribution with variance $\sqrt{dt}$ --- and $N_f$ is the number of degrees of freedom.
Without the stochastic term the thermostat is equal to the Berendsen thermostat -- with very small $\tau$ it's a stochastic Gaussian thermostat (sampling kinetic energies from the desired distribution and instantly rescaling velocities accordingly at every time step). For very large $\tau$ the thermostat does not affect the system. If the system is far from equilibrium the `Berendsen- part' (deterministic part) dominates the behaviour and the system reaches equilibrium quickly. Upon reaching equilibrium it samples from the proper canonical ensemble with the desired fluctuations.

For this thermostat the choice of the stochastic term is somewhat arbitrary as it only influences the speed of equilibration. In our implementation we chose the term proposed in the paper \cite{Bussi2007}.
\subsection{Nosé--Hoover}
In contrast to the previously introduced thermostats, which rely on stochastic changes of the velocities of the particles to control temperature, Nosé devised a deterministic thermostat by altering the Hamiltonian of the system ~\cite{Nose2002}. His approach was then further improved by Hoover, who eliminated the need for time scaling ~\cite{Hoover1985}.
In the original method proposed by Nosé an additional degree of freedom $s$ is introduced, which serves as a scaling factor for the velocities and can be thought of as the external heat bath of the system.
\begin{equation}
v_i = s\cdot \dot{r_i}
\end{equation}
Two additional terms associated with $s$ are added to the Hamiltonian of the system, which then reads as follows
\begin{equation}
\mathcal{H}_{Nose} = \sum_i \frac{p_i^2}{2m_i s^2} + \phi (r) + \frac{p_s^2}{2Q} + (N_f+1)kT\ln(s)
\end{equation}
where $\phi(r)$ denotes the classical potential energy of the system, $N_f$ the degrees of freedom, $Q$ is a free choice of parameter corresponding to the time scale of the fluctuations in kinetic energy and $p_s = Q\dot{s}$ is the conjugate momentum to $s$. The potential energy term $(N_f+1)kT\ln{s}$ is chosen such that the right canonical ensemble distribution is obtained in a simulation and the kinetic energy term $\frac{p_s}{2Q}$ is added to get a dynamic equation for the propagation of $s$.
The equations of motion in the modified system can be obtained via the Hamiltonian equations
\begin{align*}
        & \dot{p} = - \frac{\partial \mathcal{H}}{\partial q} &&  \dot{q} = \frac{\partial \mathcal{H}}{\partial p}
        && \dot{p_s} = - \frac{\partial \mathcal{H}}{\partial s} && \dot{s} = \frac{\partial \mathcal{H}}{\partial p_s} \numberthis
\end{align*}

and thus read

\begin{align*}
& \dot{p} = F && \dot{q} = \frac{p}{m s^2} \\
& \dot{p_s} = \sum_i \frac{p_i^2}{m s^3} - (N_f+1)\frac{kT}{s} &&  \dot{s} = \frac{p_s}{Q} \numberthis
\end{align*}

These coupled equations can be simplified by reducing the time scale by $s$ - i.e. $dt_{old} = s dt_{new} $, which results in

\begin{align*}
& \dot{p} = s F && \dot{q} = \frac{p}{m s} \\
& \dot{p_s} = \sum_i \frac{p_i^2}{m s^2} - (N_f+1)\cdot kT &&  \dot{s} = \frac{s p_s}{Q} \numberthis
\end{align*}

Hoover was able to further simplify the approach by eliminating the variable $s$ and rewriting the equations in terms of $q$, $\dot{q}$ and $\ddot{q}$

\begin{equation}
\ddot{q} = \frac{\dot{p}}{m s} - \frac{p}{m s^2}\dot{s} = \frac{F}{m} - \frac{p_s}{Q}\dot{q} \equiv \frac{F}{m} - \zeta \dot{q}
\end{equation}

The variable $\zeta = \frac{p_s}{Q}$ is introduced to highlight the analogy between this equation of motion and that of a Newtonian motion with a friction term. It evolves in time as

\begin{equation}
\dot{\zeta} = \left[\sum_i m_i \dot{q}^2_i - (N_f+1)kT\right]/Q
\end{equation}

Since the damping parameter $\zeta$ is not determined instantaneously, as is the case with e.g. a Gaussian thermostat, but via an equation of motion, the Nosé-Hoover method describes an integral thermostat and therefore leads to both smooth as well as time-reversible and deterministic paths in phase space.

\subsection{Nose-Hoover Chains} \label{NHC}
While the Nosé-Hoover thermostat generates the right canonical distributions for large, ergodic systems, it often fails if the system has more than one constant of motion - for example if the total momentum is conserved, as is the case for systems without external forces. This is due to the fact that the limiting distribution of the Nośe-Hoover thermostat has a gaussian dependence not only on the momenta $p_i$ but also on the conjugate momentum $p_s$ of the 'heat bath', but there is no driving force to ensure the correct fluctuations of $p_s$. Martyna and Klein proposed a method to solve this problem, now known as Nosé-Hoover chains \cite{Martyna1992}. The idea is to thermostat $p_s$ as well as its thermostat, etc., forming a chain of thermostats, which can be extended to arbitrary length. For a total of $M$ thermostats, the extended equations of motions take the following form:

\begin{align*}
& \dot{q}_i = \frac{p_i}{m_i} && \dot{p}_i = F_i - p_i \frac{p_{s_1}}{Q_1} \\
& \dot{s}_j = \frac{p_{s_j}}{Q_j} \\
& \dot{p}_{s_1} = \left[ \sum_{i=1}^N \frac{p^2_i}{m_i} - (N_f+1)kT\right] - p_{s_1}\frac{p_{s_2}}{Q_2} \\
& \dot{p}_{s_j} = \left[ \frac{p^2_{s_{j-1}}}{Q_{j-1}} - kT \right] - p_{s_j}\frac{p_{s_{j+1}}}{Q_{j+1}} \\
& \dot{p}_{s_M} = \left[ \frac{p^2_{s_{M-1}}}{Q_{M-1}} - kT\right] \numberthis
\end{align*}

As with the original Nosé-Hoover method, the 'masses' $Q_j$ of the thermostats determine the strength of the coupling between successive thermostats and have to be chosen carefully to get good results. However, the choice of mass gets less critical the more thermostats are used in the chain. If the system under investigation has a typical frequency $\omega$, a good choice of masses is $Q_1 = \frac{qkT}{\omega}$ and $Q_j = \frac{kT}{\omega}$, which also gives the thermostats an average 'frequency' of $\omega$ \cite{Martyna1992}.

\subsubsection{A short note on the implementation of the Nosé-Hoover thermostats}
In the equations of motion of both the Nosé-Hoover thermostat as well as the Nosé-Hoover chains second derivatives of the position coordinates depend on the first derivative, which leads to a problem with the standard velocity-verlet integration scheme used with the other thermostats. One possibility to overcome this problem is the use of an iterative scheme - such as a predictor-corrector method. However, such an approach would lead to the loss of time-reversibility, which is one of the major advantages of the Nośe-Hoover thermostat. For this project we used the explicit time-reversible integrators developed by Martyna \textit{et al.} via the Liouville formalism and a clever use of the Trotter formulas \cite{Martyna1996}. For a detailed explanation as well as an example of how to implement this method for a chain of length two, we refer the reader to pages 535-540 of `Understanding Molecular Simulation' by Frenkel and Smit \cite{FrenkelSmit2002}.


\chapter{Laplace Transforms}
\noindent
Laplace transforms allow us to change differential equations into algebraic equations. We can then solve the algebraic equation and "undo" the Laplace transform to find the solution to our differential equation.

% Definition
\input{./laplaceTransforms/definition/definition.tex}
% Derivations
\input{./laplaceTransforms/derivations/derivations.tex}
% Inverse Laplace
\input{./laplaceTransforms/inverseLaplace/inverseLaplace.tex}
% Solving Differential Equations
\input{./laplaceTransforms/solvingEquations/solvingEquations.tex}


\documentclass{article}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, latexsym}

\usepackage{pgfplots}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{nicefrac}
\usepackage{placeins}
\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}
\usetikzlibrary{shapes,positioning,intersections,quotes}
\usetikzlibrary{arrows.meta,
                bending,
                intersections,
                quotes,
                shapes.geometric}

\definecolor{darkgreen}{rgb}{0.0, 0.6, 0.0}
\definecolor{darkred}{rgb}{0.7, 0.0, 0.0}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\title{Week 15}
\begin{document}
\pagenumbering{gobble}
\maketitle
\newpage
\pagenumbering{arabic}

\section*{Anomaly detection}

\begin{itemize}
  \item We can assess whether data points are anomalous by using the dataset as a baseline.
  \item if $p(x_{test}) < \epsilon \quad$, then flag this as an anomaly
  \item if $p(x_{test}) \geq \epsilon \quad$, then this is OK
  \item $\epsilon$ is a threshold probability number that we determine based on how certain we need/want to be.
\end{itemize}

\includegraphics[width=0.5\textwidth]{resources/anomaly}

\section*{Applications}
\begin{itemize}
  \item Fraud detection

        \begin{itemize}
          \item Users have activities connected with them, such as the amount of time spent online, the location of login, and the frequency with which they spend money.
          \item Using this information, we can create a model of what regular users do.
          \item What is the probability of "normal" behavior?
          \item Send atypical users' data through the model to identify them. Make a note of everything that appears unusual. Block cards/transactions automatically.
        \end{itemize}

  \item Manufacturing

        \begin{itemize}
          \item Aircraft engine example.

        \end{itemize}

  \item Monitoring computers in data center

        \begin{itemize}
          \item If you have many machines in a cluster (x1 = memory use, x2 = number of disk accesses/sec, x3 = CPU load).
          \item When you notice an anomalous machine, it is likely that it is soon to fail.
          \item Consider replacing parts of it.
        \end{itemize}
\end{itemize}

\section*{The Gaussian distribution}

\begin{itemize}
  \item $\mu$ is mean.
  \item $\sigma^2$ is variance and $\sigma$ is a standard deviation.
  \item probability of x, parameterized by the mean and variance:
\end{itemize}

$$p(x; \mu; \sigma^2) = \frac{1}{\sqrt{2\pi\sigma}}exp(-\frac{(x-\mu)^2}{2\sigma^2})$$

~\\

\includegraphics[width=0.5\textwidth]{resources/gaussian}

\begin{itemize}
  \item Assume we have a data collection of m examples.
  \item Given that each example is a real number, we plot the data on the x axis.
  \item Given the dataset can you estimate the distribution?
\end{itemize}

\includegraphics[width=0.6\textwidth]{resources/data_fit}

Seems like a good fit - data suggests a higher likelihood of being in the center and a lower likelihood of being further out.

\newpage
\section*{Anomaly detection}

\FloatBarrier
\begin{algorithm}
\caption{Anomaly detection}\label{euclid}
\begin{algorithmic}[1]
\State Choose features $x_i$ that you think might be indicative of anomalous examples.
\State Fit parameters $\mu_1, ..., \mu_n, \sigma_1^2, ..., \sigma^n$

$$\mu_j = \frac{1}{m} \sum_{i=1}^mx_j^{(i)}$$
$$\sigma_j^2 = \frac{1}{m} \sum_{i=1}^m(x_j^{(i)}-\mu_j)^2$$

\State Given new example x, compute p(x):

$$p(x)= \prod_{j=1}^n \frac{1}{\sqrt{2\pi\sigma_j}}exp(-\frac{(x_j-\mu_j)^2}{2\sigma_j^2})$$

\end{algorithmic}
\end{algorithm}
\FloatBarrier

\section*{Developing and evaluating and anomaly detection system}

\begin{itemize}
  \item You have some labeled data.

        \begin{itemize}
          \item $y=0$ for engines which were non-anomalous.
          \item $y=1$ for engines which were anomalous.
        \end{itemize}

  \item Training set is the collection of normal examples.
  \item Next define:

        \begin{itemize}
          \item Cross validation set.
          \item Test set.
          \item For both assume you can include a few examples which have anomalous examples.
        \end{itemize}

  \item In our example we have:

        \begin{itemize}
          \item 10000 good engines.
          \item 50 flawed engines.
        \end{itemize}

  \item Split into:

        \begin{itemize}
          \item Training set: 6000 good engines (y = 0).
          \item CV set: 2000 good engines, 10 anomalous.
          \item Test set: 2000 good engines, 10 anomalous.
        \end{itemize}

\end{itemize}

~\\
What's a good metric to use for evaluation?

\begin{itemize}
  \item Compute fraction of true positives/false positive/false negative/true negative.
  \item Compute precision/recall.
  \item Compute F1-score.
\end{itemize}

\section*{Multivariate Gaussian distribution}
It is a somewhat different approach that can occasionally discover anomalies that normal Gaussian distribution anomaly detection fails to detect.

\begin{itemize}
  \item Assume you can fit a Gaussian distribution to CPU load and memory use.
  \item Assume we have an example in the test set that appears to be an anomaly (e.g. x1 = 0.4, x2 = 1.5).
  \item Here memory use is high and CPU load is low (if we plot x1 vs. x2 our green example looks miles away from the others).
  \item The problem is that if we look at each characteristic individually, they may fall inside acceptable bounds - the difficulty is that we know we shouldn't obtain those types of numbers together, but they're both okay individually.
\end{itemize}

\includegraphics[width=\textwidth]{resources/mult_gauss}

~\\
What are the parameters for this new model?
\begin{itemize}
  \item $\mu$ which is an n dimensional vector (where n is number of features)
  \item $\Sigma$ which is an [n x n] matrix - the covariance matrix
\end{itemize}

$$p(x; \mu; \Sigma) = \frac{1}{(2\pi)^{n/2}|\Sigma|^{1/2}}exp(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x-\mu))$$

~\\\\
\includegraphics[width=\textwidth]{resources/cov_matrix_sigma}

~\\
Very tall thin distribution, shows a strong positive correlation.

\subsection*{Gaussian model - summary}

\begin{itemize}
  \item Probably used more often.
  \item There is a need to manually create features to capture anomalies where x1 and x2 take unusual combinations of values.
  \item So need to make extra features and might not be obvious what they should be.
  \item Much cheaper computationally.
  \item Scales much better to very large feature vectors.
  \item Works well even with a small training set e.g. 50, 100.
\end{itemize}

\subsection*{Multivariate gaussian model - summary}

\begin{itemize}
  \item Used less frequently.
  \item Can capture feature correlation.
  \item So no need to create extra values.
  \item Less computationally efficient.
  \item Needs for m > n  i.e. number of examples must be greater than number of features.
\end{itemize}

\end{document}