\lab{Lorenz Equations}{Lorenz Equations}
\label{lab:lorenz}

\objective{Investigate the behavior of a system that exhibits chaotic behavior.
Demonstrate methods for visualizing the evolution of a system.}

Chaos is everywhere.
It can crop up in unexpected places and in remarkably simple systems, and a great deal of work has been done to describe the behavior of chaotic systems.
One primary characteristic of chaos is that small changes in initial conditions result in large changes over time in the solution curves.

\section*{The Lorenz System}
One of the earlier examples of chaotic behavior was discovered by Edward Lorenz.
In 1963, while working to study atmospheric dynamics he derived the simple system of equations
\begin{align*}
\frac{\partial x}{\partial t} &= \sigma \left(y - x\right) \\
\frac{\partial y}{\partial t} &= \rho x - y - x z \\
\frac{\partial z}{\partial t} &= x y - \beta z
\end{align*}
where $\sigma$, $\rho$, and $\beta$ are all constants.
After deriving these equations, he plotted the solutions and observed some unexpected behavior.
For appropriately chosen values of $\sigma$, $\rho$, and $\beta$, the solutions did not tend toward any steady fixed points, nor did the system permit any stable cycles.
The solutions did not tend off toward infinity either.
With further work, he began the study of what was called a strange attractor.
This system, though relatively simple, exhibits chaotic behavior.

\begin{problem}
\label{prob:lorenz_basic}
Use Mayavi's \li{plot3d} function to plot the trajectories of several points in the Lorenz system.
Use $\sigma = 10$, $\beta = \frac{8}{3}$, and $\rho = 28$.
Choose random initial values between $-15$ and $15$.
The result should look something like Figure \ref{fig:lorenz_plot}.
\end{problem}

\begin{figure}
\includegraphics[width=\textwidth]{lorenz_plot.png}
\caption{Approximate solutions to the Lorenz equations for several random starting points.}
\label{fig:lorenz_plot}
\end{figure}

\section*{Animation in Mayavi}
Here we will take a brief diversion into some tools for plotting that will help us to visualize the evolution of systems like the one we are studying here.
Both Matplotlib and Mayavi allow for some kind of visualization.
% As of this writing, Mayavi's support for 3D plotting is much more robust than the plotting in Matplotlib, so we will encourage students to use it instead of Matplotlib.
Here we will work primarily with the animation functions in Mayavi, though similar functionality is available in Matplotlib.

\subsection*{Setting Data}
Said simply, most things you plot in Mayavi, allow you to change their data.
For things you plot using the basic built in plotting functions in the \li{mlab} api this can be done using the \li{set} and \li{reset} methods of the \li{mlab_source} attribute of the object created by the plotting function.
For example, the following short script will plot the curve $(t, \cos(t), 0)$, in spite of the fact that we originally plot the data corresponding to the curve $(t, \sin(t), 0)$.
\begin{lstlisting}
import numpy as np
from mayavi import mlab

x = np.linspace(- 2 * np.pi, 2 * np.pi)
y = np.sin(x)
z = np.zeros_like(x)
# Plot the first curve.
curve = mlab.plot3d(x, y, z)
# Change the y values.
curve.mlab_source.set(y=np.cos(x))
# Show the new curve.
mlab.show()
\end{lstlisting}

We can use this functionality in conjunction with some function decorators included in Mayavi to make a plot that continually evolves.
For example, we can continuously shift the phase of a curve like the one above using something like this:
\begin{lstlisting}
from mayavi import mlab
import numpy as np

def animate_sine(resolution=101, step=1, delay=20):
    # Compute the initial values for the curve.
    # Leave off the last point so we can update by rolling the entries of the array from
    # the end to the beginning.
    x = np.linspace(0, 4 * np.pi, resolution)[:-1]
    # Make the surface object and the initial plot.
    c = mlab.plot3d(x, np.sin(x), np.zeros_like(x), line_width=.2)
    # Use decorators to call the update the plot
    # periodically with a given time delay.
    # 'animate' is a generator that updates
    # the plot each time it is called.
    # The show decorator takes care of showing the figure.
    @mlab.show
    @mlab.animate(delay=delay)
    def animate():
        # Get 'y' back from the surface object.
        y = c.mlab_source.y
        # Update the plot at each iteration of this loop.
        while True:
            y = np.roll(y, step)
            c.mlab_source.set(y=y)
            yield
    # Run the animation on the figure.
    animate()
# Run the full animation.
animate_sine()
\end{lstlisting}

\begin{figure}
\begin{subfigure}{.49\textwidth}
\includegraphics[width=\textwidth]{harmonic1.png}
\end{subfigure}
\begin{subfigure}{.49\textwidth}
\includegraphics[width=\textwidth]{harmonic2.png}
\end{subfigure}
\caption{Simple surfaces we can animate with Mayavi.}
\label{fig:harmonic_animations}
\end{figure}

The \li{set} method can also be used on 3D surfaces.
The following two examples show how this is done.
The surfaces they animate are shown in Figure \ref{fig:harmonic_animations}.
\begin{lstlisting}
from mayavi import mlab
import numpy as np

def animate_harmonic(resolution=51, delay=25):
    # Make the initial data for the surface.
    x = np.linspace(0, np.pi, resolution)
    y = np.linspace(0, np.pi, resolution)
    x, y = np.meshgrid(x, y, copy=False)
    z = np.sin(x) * np.sin(y)
    # Plot the surface.
    # For now use zeros as the z values.
    # It will use the scalars values to select colors,
    # so we'll have it match the colors to the z values now.
    c = mlab.mesh(x, y, np.zeros_like(z), scalars=z)
    # Animate it by changing the 'z' values.
    @mlab.show
    @mlab.animate(delay=delay)
    def animate():
        # We'll have it oscillate between its current value
        # and the negative of its current value.
        # We'll have scale range from values of 0 to 2 * np.pi.
        scale = 0.
        while True:
            # Update the scale
            scale += .05
            # Cycle back toward 0 if necessary.
            if scale > 2 * np.pi:
                scale -= 2 * np.pi
            # Update the plot
            c.mlab_source.set(z = np.sin(scale) * z)
            yield
    # Run the animation on the figure.
    animate()
# Run the full animation.
animate_harmonic()
\end{lstlisting}

Here is an example that uses this same approach to plot a more generic oscillating surface.
\begin{lstlisting}
import numpy as np
from mayavi import mlab

def oscillate(x, y, z, delay=20):
    @mlab.show
    @mlab.animate(delay=delay)
    def animate(x, y, z):
        # Make the initial plot.
        surface = mlab.mesh(x, y, np.zeros_like(z), scalars=z)
        # Use this variable to scale it at each step in the animation.
        scale = 0.
        while True:
            scale += .05
            if scale > 2 * np.pi:
                scale -= 2 * np.pi
            # Update the 'z' values for the surface.
            surface.mlab_source.set(z = np.sin(scale) * z)
            yield
    # Run the animation
    animate(x, y, z)

# Here's another fun example.
# Construct the data for the plot.
x = np.linspace(0, np.pi)
y = np.linspace(0, np.pi)
x, y = np.meshgrid(x, y, copy=False)
z = np.sin(2 * x) * np.sin(2 * y)
# Run the animation.
oscillate(x, y, z)
\end{lstlisting}

\subsection*{Resetting Data}
The \li{set} method we have shown thus far is useful when we are changing the values of the data used in a plot, but it does not work when we need to change the \emph{shape} of the arrays involved as well.
Some times it is necessary to change the shapes of the arrays used for the plot.
To do this we must use the \li{reset} method.
Here is an example where we use the \li{reset} method to trace out a helix curve like the one shown in Figure \ref{fig:helix_animation}
\begin{lstlisting}
from mayavi import mlab
import numpy as np

def trace_helix(resolution=401, delay=10, step=1):
    z = np.linspace(0, 2, resolution)
    x = np.cos(4 * np.pi * z)
    y = np.sin(4 * np.pi * z)
    # Make a line to start from.
    # Note that the 'x', 'y', and 'z' coordinates for the curve must be contained in arrays or lists.
    # Only passing the coordinates of the first point will not work.
    # Notice how we are passing in the color.
    # The color is expected to be a tuple (not a list or array) representing RGB values for the desired color.
    c = mlab.plot3d(x[:1], y[:1], z[:1], line_width=.2, color=(1, 0, 0))
    # Set the camera position to a good angle for this plot.
    mlab.gcf().scene.camera.position = [3.95632052, 3.95626431, 4.95668558]
    mlab.gcf().scene.camera.focal_point = [2.27987766e-04, 1.71780586e-04, 1.00059305]
    mlab.gcf().scene.camera.clipping_range = [3.25443332, 11.39746923]
    @mlab.show
    @mlab.animate(delay=delay)
    def animate():
        scale = 0.
        for i in xrange(2 + step, z.size, step):
            # Reset the 'x', 'y', and 'z' coordinates for the graph.
            c.mlab_source.reset(x=x[:i], y=y[:i], z=z[:i])
            yield
    animate()
trace_helix()
\end{lstlisting}

\begin{figure}
\includegraphics[width=\textwidth]{helix.png}
\caption{A simple curve we can animate using Mayavi.}
\label{fig:helix_animation}
\end{figure}

If you want to reset the zoom at each update so that the view updates to match the plot you can do the following
\begin{lstlisting}
from mayavi import mlab
import numpy as np

def trace_helix(resolution=401, delay=10, step=1):
    z = np.linspace(0, 2, resolution)
    x = np.cos(4 * np.pi * z)
    y = np.sin(4 * np.pi * z)
    c = mlab.plot3d(x[:1], y[:1], z[:1], line_width=.2, color=(1, 0, 0))
    @mlab.show
    @mlab.animate(delay=delay)
    def animate():
        scale = 0.
        for i in xrange(2 + step, z.size, step):
            c.mlab_source.reset(x=x[:i], y=y[:i], z=z[:i])
            # Reset the zoom at each iteration.
            mlab.gcf().scene.reset_zoom()
            yield
    animate()
trace_helix()
\end{lstlisting}

If you would like to find the proper configuration for the camera there are several different ways to do it.
One is to plot the full figure, and access the camera \li{scene}, \li{focal_point}, \li{clipping_range}, \li{viewing_angle}, and \li{view_up} attributes, save their values and use them to configure the plot as shown in the first example involving the helix.

You can also use the Mayavi pipeline window.
You can open this window by clicking the button at the top-right of the window where your plot appears.
If you click the red record button, move your plot to the position you want it to have, then stop the recording, you will be able to get the desired camera positioning.

\begin{problem}
\label{prob:lorenz_animation}
Write a Python function that animates the Lorenz system using Mayavi.
Have it accept a number of trajectories to plot, a final time value, a resolution for the plot, a stepping number (how many new points to include at each update of the plot), and a time delay to use between iterations.
Generate your starting points the same way you did in problem \ref{prob:lorenz_basic}.
Use different colors for each trajectory.
\end{problem}

We will now use animations to demonstrate that the Lorenz system is very sensitive to changes in the initial conditions.

\begin{problem}
\label{prob:lorenz_tol_sensitivity}
Write another Python function that produces a similar animation as the one in Problem \ref{prob:lorenz_animation}.
Use one initial guess, but solve the ODE system using the arguments \li{atol=1E-14} and \li{rtol=1E-12}, and then \li{atol=1E-15} and \li{rtol=1E-13} when you call \li{scipy.odeint}.
Have your function accept a final time value, a resolution for the curve, a stepping number, and a time delay to use between iterations.
What happens as you let your solution curves evolve over time?
Try running the simulation for longer periods of time.
% The solution curves should stay together for a while, then separate.
\end{problem}

\begin{problem}
Write another animation that plots a single solution set and another solution set with slightly perturbed initial conditions.
We will perturb the initial conditions by the smallest representable floating point value.
If \li{x0} is our first initial condition, let the second set of initial conditions, \li{x1}, be \li{x1 = y0 * (1. + 2.22E-16)}.
What happens as you let your solution curves evolve over time?
Try running the simulation for longer periods of time.
% The solution curves should stay together for a while, then separate.
\end{problem}

\section*{Lyapunov Exponents}
The Lyapunov exponent of a dynamical system is one measure of how chaotic a system is.
While there are more conditions for a system to be considered chaotic, one of the primary indicators of a chaotic system is \emph{extreme sensitivity to initial conditions}.
Strictly speaking, this is saying that a chaotic system is poorly conditioned.
Usually, in dynamical systems, the sensitivity to changes in initial conditions depends exponentially on the time the system is allowed to evolve.
If $\delta(t)$ represents the difference between two solution curves, when $\delta(t)$ is small, the following approximation holds.
\[\|\delta(t)\| \sim \|\delta(0)\| e^{\lambda t}\]
where $\lambda$ is a constant called the Lyapunov exponent.
For the Lorenz system, expirimentally it can be verified that $\lambda \approx .9$.

\begin{figure}
\includegraphics[width=\textwidth]{lyapunov_plot.pdf}
\caption{A semilog plot of the separation between two solutions to the Lorenz equations together with a fitted line that gives a rough estimate of the Lyapunov exponent of the system.}
\label{fig:lyapunov_exponent}
\end{figure}

\begin{problem}
Get a crude estimate of the lyapunov exponent for the Lorenz system.
Write a Python function that, finds an initial point on the strange attractor, runs the simulation to a given time $t$, and produces a semilog plot of the norm of the difference between the two solution curves.
Also have it plot an exponential line fitted to match the curve (this will be linear on the semilog plot).
Have it return a rough estimate of the Lyapunov exponent.
The output should be something like Figure \ref{fig:lyapunov_exponent}.

Note: In order to get a good estimate of the Lyapunov exponent, your initial guess should already lie on the strange attractor.
You can get a value on the attractor by running the system for a while to find a good initial guess.

Hint: To find the fitting line, take the logarithm of the norms of the differences, compute a linear fit, then take the exponential function of the resulting line.
The Lyapunov exponent will be approximately equal to the slope found by the linear regression.
\end{problem}


%!TEX root = ceres-solver.tex
\chapter{Powell's Function}
\label{chapter:tutorial:powell}
Consider now a slightly more complicated example -- the minimization of Powell's function. Let $x = \left[x_1, x_2, x_3, x_4 \right]$ and
\begin{align}
   f_1(x) &= x_1 + 10*x_2 \\
   f_2(x) &= \sqrt{5} * (x_3 - x_4)\\
   f_3(x) &= (x_2 - 2*x_3)^2\\
   f_4(x) &= \sqrt{10} * (x_1 - x_4)^2\\
	F(x) & = \left[f_1(x),\ f_2(x),\ f_3(x),\ f_4(x) \right]
\end{align}
$F(x)$ is a function of four parameters, and has four residuals. Now,
one way to solve this problem would be to define four
\texttt{CostFunction} objects that compute the residual and Jacobians. \eg the following code shows the implementation for $f_4(x)$.
\begin{minted}[mathescape]{c++}
class F4 : public ceres::SizedCostFunction<1, 4> {
 public:
  virtual ~F4() {}
  virtual bool Evaluate(double const* const* parameters,
                        double* residuals,
                        double** jacobians) const {
    double x1 = parameters[0][0];
    double x4 = parameters[1][0];
    // $f_4 = \sqrt{10} * (x_1 - x_4)^2$
    residuals[0] = sqrt(10.0) * (x1 - x4) * (x1 - x4)
    if (jacobians != NULL) {
      jacobians[0][0] = 2.0 * sqrt(10.0) * (x1 - x4);   // $\partial_{x_1}f_1(x)$
      jacobians[0][1] = 0.0;                            // $\partial_{x_2}f_1(x)$
      jacobians[0][2] = 0.0;                            // $\partial_{x_3}f_1(x)$
      jacobians[0][3] = -2.0 * sqrt(10.0) * (x1 - x4);  // $\partial_{x_4}f_1(x)$
    }
    return true;
  }
};
\end{minted}

But this can get painful very quickly, especially for residuals involving complicated multi-variate terms. Ceres provides two ways around this problem. Numeric and automatic symbolic differentiation.

\section{Automatic Differentiation}
\label{sec:tutorial:autodiff}
With its automatic differentiation support, Ceres allows you to define templated objects/functors that will compute the residual and it takes care of computing the Jacobians as needed and filling the \texttt{jacobians} arrays with them. For example, for $f_4(x)$ we define
\begin{minted}[frame=lines,mathescape]{c++}
class F4 {
 public:
  template <typename T> bool operator()(const T* const x1,
                                        const T* const x4,
                                        T* residual) const {
    // $f_4 = \sqrt{10} * (x_1 - x_4)^2$
    residual[0] = T(sqrt(10.0)) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
    return true;
  }
};
\end{minted}

The important thing to note here is that \texttt{operator()} is a
templated method, which assumes that all its inputs and outputs are of
some type \texttt{T}. The reason for using templates here is because Ceres will call \texttt{F4::operator<T>()}, with $\texttt{T=double}$ when just the residual is needed, and with a special type $T=\texttt{Jet}$ when the Jacobians are needed.

Note also that the parameters are not packed
into a single array, they are instead passed as separate arguments to
\texttt{operator()}. Similarly we can define classes \texttt{F1,F2}
and \texttt{F4}.  Then let us consider the construction and solution of the problem. For brevity we only describe the relevant bits of code~\footnote{The full source code for this example can be found in \texttt{examples/powell.cc}.}
\begin{minted}[mathescape]{c++}
double x1 =  3.0; double x2 = -1.0; double x3 =  0.0; double x4 =  1.0;
// Add residual terms to the problem using the using the autodiff
// wrapper to get the derivatives automatically.
problem.AddResidualBlock(
  new ceres::AutoDiffCostFunction<F1, 1, 1, 1>(new F1), NULL, &x1, &x2);
problem.AddResidualBlock(
  new ceres::AutoDiffCostFunction<F2, 1, 1, 1>(new F2), NULL, &x3, &x4);
problem.AddResidualBlock(
  new ceres::AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL, &x2, &x3)
problem.AddResidualBlock(
  new ceres::AutoDiffCostFunction<F4, 1, 1, 1>(new F4), NULL, &x1, &x4);
\end{minted}
A few things are worth noting in the code above. First, the object
being added to the \texttt{Problem} is an
\texttt{AutoDiffCostFunction} with \texttt{F1}, \texttt{F2}, \texttt{F3} and \texttt{F4} as template parameters. Second, each \texttt{ResidualBlock} only depends on the two parameters that the corresponding residual object depends on and not on all four parameters.

Compiling and running \texttt{powell.cc} gives us:
\begin{minted}{bash}
Initial x1 = 3, x2 = -1, x3 = 0, x4 = 1
   0: f: 1.075000e+02 d: 0.00e+00 g: 1.55e+02 h: 0.00e+00 rho: 0.00e+00 mu: 1.00e-04 li:  0
   1: f: 5.036190e+00 d: 1.02e+02 g: 2.00e+01 h: 2.16e+00 rho: 9.53e-01 mu: 3.33e-05 li:  1
   2: f: 3.148168e-01 d: 4.72e+00 g: 2.50e+00 h: 6.23e-01 rho: 9.37e-01 mu: 1.11e-05 li:  1
   3: f: 1.967760e-02 d: 2.95e-01 g: 3.13e-01 h: 3.08e-01 rho: 9.37e-01 mu: 3.70e-06 li:  1
   4: f: 1.229900e-03 d: 1.84e-02 g: 3.91e-02 h: 1.54e-01 rho: 9.37e-01 mu: 1.23e-06 li:  1
   5: f: 7.687123e-05 d: 1.15e-03 g: 4.89e-03 h: 7.69e-02 rho: 9.37e-01 mu: 4.12e-07 li:  1
   6: f: 4.804625e-06 d: 7.21e-05 g: 6.11e-04 h: 3.85e-02 rho: 9.37e-01 mu: 1.37e-07 li:  1
   7: f: 3.003028e-07 d: 4.50e-06 g: 7.64e-05 h: 1.92e-02 rho: 9.37e-01 mu: 4.57e-08 li:  1
   8: f: 1.877006e-08 d: 2.82e-07 g: 9.54e-06 h: 9.62e-03 rho: 9.37e-01 mu: 1.52e-08 li:  1
   9: f: 1.173223e-09 d: 1.76e-08 g: 1.19e-06 h: 4.81e-03 rho: 9.37e-01 mu: 5.08e-09 li:  1
  10: f: 7.333425e-11 d: 1.10e-09 g: 1.49e-07 h: 2.40e-03 rho: 9.37e-01 mu: 1.69e-09 li:  1
  11: f: 4.584044e-12 d: 6.88e-11 g: 1.86e-08 h: 1.20e-03 rho: 9.37e-01 mu: 5.65e-10 li:  1
Ceres Solver Report: Iterations: 12, Initial cost: 1.075000e+02, \
Final cost: 2.865573e-13, Termination: GRADIENT_TOLERANCE.
Final x1 = 0.000583994, x2 = -5.83994e-05, x3 = 9.55401e-05, x4 = 9.55401e-05
\end{minted}
It is easy to see that the  optimal solution to this problem is at $x_1=0, x_2=0, x_3=0, x_4=0$ with an objective function value of $0$. In 10 iterations, Ceres finds a solution with an objective function value of $4\times 10^{-12}$.

\section{Numeric Differentiation}
If a templated implementation is not possible then a \texttt{NumericDiffCostFunction} object can be used. The user defines a \texttt{CostFunction} object whose \texttt{Evaluate} method is only computes the residuals. A wrapper object \texttt{NumericDiffCostFunction} then uses it to compute the residuals and the Jacobian using finite differencing.  \texttt{examples/quadratic\_numeric\_diff.cc} shows a numerically differentiated implementation of \texttt{examples/quadratic.cc}.

We recommend that if possible,  automatic differentiation should be used. The use of
C++ templates makes automatic differentiation extremely efficient,
whereas numeric differentiation can be quite expensive, prone to
numeric errors and leads to slower convergence.


\chapter{Equations governing the motion of a fluid}\label{c3}
\section{Material integrals in a moving fluid}\label{c3s1}
\begin{itemize}
\item We prove that
\begin{equation}\label{c3s1e1}
\td{\tau^\ast}{t} = (\dive\vec{u})\tau^\ast,
\end{equation}
where
\begin{equation}\label{c3s1e2}
\tau^\ast = \lim_{\delta\tau(t_0) \rightarrow 0}\frac{\delta\tau(t)}{\delta\tau(t_0)}
\end{equation}
We begin with the definition of the first derivative,
\[
\td{\tau^\ast}{t} = \lim_{h \rightarrow 0}\frac{\tau(t + h) - \tau(t)}{h}
\]
Using \eqref{c3s1e2},
\[
\td{\tau^\ast}{t} = \lim_{h \rightarrow 0}\frac{1}{h}\left(\lim_{\delta\tau(t_0) \rightarrow 0}\frac{\delta\tau(t+h)}{\delta\tau(t_0)} -
\lim_{\delta\tau(t_0) \rightarrow 0}\frac{\delta\tau(t)}{\delta\tau(t_0)}\right)
\]
or,
\[
\td{\tau^\ast}{t} = \lim_{h \rightarrow 0}\frac{1}{h}\lim_{\delta\tau(t_0) \rightarrow 0}\frac{\delta\tau(t+h) - \delta\tau(t)}{\delta\tau(t_0)},
\]
But
\[
\delta\tau(t+h) - \delta\tau(t) = h\td{\delta\tau}{t},
\]
which from equation (3.1.1) of the book is
\[
\delta\tau(t+h) - \delta\tau(t) = h\dive\vec{u}\delta\tau(t)
\]
so that
\[
\td{\tau^\ast}{t} = \lim_{h \rightarrow 0}\frac{1}{h}\lim_{\delta\tau(t_0) \rightarrow 0}\frac{h\dive\vec{u}\delta\tau(t)}{\delta\tau(t_0)} =
\lim_{h \rightarrow 0}\lim_{\delta\tau(t_0) \rightarrow 0}\frac{\dive\vec{u}\delta\tau(t)}{\delta\tau(t_0)}
\]
Since the function no longer depends on $h$, the limit as $h \rightarrow 0$ is ineffective and
\[
\td{\tau^\ast}{t} = \dive\vec{u}\lim_{\delta\tau(t_0) \rightarrow 0}\frac{\delta\tau(t)}{\delta\tau(t_0)} = (\dive\vec{u})\tau^\ast
\]

\item We will now argue that, if $\delta\vec{l}$ is a material line element, then
\begin{equation}\label{c3s1e3}
\td{\delta\vec{l}}{t} = \delta\vec{l}\cdot\grad\vec{u} + o(|\delta\vec{l}|)
\end{equation}
A material line element's length changes only if the velocity of the fluid at its either ends is not the same. Let $\vec{u}_0$ and $\vec{u}_1$ be the velocities of the two ends. Then, for
a small element, $\vec{u}_1 = \vec{u}_0 + \delta\vec{l}\cdot\grad\vec{u} + o(|\delta\vec{l}|)$ so that the difference in velocities is $\delta\vec{l}\cdot\grad\vec{u} +
o(|\delta\vec{l}|)$.

\item The volume of a cylindrical volume element with end faces of area $\delta\vec{S}$ and generator $\delta\vec{l}$ is $\delta\tau = \delta\vec{S}\cdot\delta\vec{l}=
\delta S_i\delta l_i$.
Since we have
\[
\lim_{\delta\tau \rightarrow 0}\frac{1}{\delta\tau}\td{\delta\tau}{t} = \dive\vec{u},
\]
we can as well write
\begin{equation}\label{c3s1e4}
\td{\delta\tau}{t} = \dive\vec{u}\delta\tau + o(\delta\tau)
\end{equation}
or
\[
\td{\delta{l}_i}{t}\delta S_i + \delta{l}_i\td{\delta{S}_i}{t} = \pdt{u_j}{x_j}\delta S_i\delta l_i + o(\delta\tau)
\]
Using \eqref{c3s1e3} in Cartesian tensor form,
\[
\delta{l}_j\pdt{u_i}{x_j}\delta S_i + \delta{l}_i\td{\delta{S}_i}{t} = \pdt{u_j}{x_j}\delta S_i\delta l_i + o(\delta\tau)
\]
Interchanging $i$ and $j$ in the first term,
\[
\delta{l}_i\pdt{u_j}{x_i}\delta S_j + \delta{l}_i\td{\delta{S}_i}{t} = \pdt{u_j}{x_j}\delta S_i\delta l_i + o(\delta\tau)
\]
or
\[
\delta{l}_i\left(\pdt{u_j}{x_i}\delta S_j + \td{\delta{S}_i}{t} - \pdt{u_j}{x_j}\delta S_i\right) = o(\delta\tau)
\]
If this were to be true in general then,
\begin{equation}\label{c3s1e5}
\td{\delta{S}_i}{t} = \pdt{u_j}{x_j}\delta S_i - \pdt{u_j}{x_i}\delta S_j + o(|\delta\vec{S}|)
\end{equation}
We can use the above equation to obtain an expression for,
\begin{eqnarray}
\frac{d}{dt}(\rho\delta S_i) &=& \rho\td{\delta S_i}{t} + \delta S_i\td{\rho}{t} \nonumber \\
 &=& \rho\left(\pdt{u_j}{x_j}\delta S_i - \pdt{u_j}{x_i}\delta S_j + o(|\delta\vec{S}|)\right) + \nonumber \\
 & & \delta S_i\left(-\rho\pdt{u_j}{x_j}\right) \nonumber \\
 &=& -\rho\delta S_j \pdt{u_j}{x_i} + o(|\delta\vec{S}|) \label{c3s1e6}
\end{eqnarray}

\item From \eqref{c3s1e3},
\[
2\delta\vec{l}\cdot\td{\delta\vec{l}}{t} = 2\delta\vec{l}\cdot\grad\vec{u}\cdot\delta\vec{l} + o(|\delta\vec{l}|^2)
\]
or,
\[
2\delta{l}\td{\delta{l}}{t} = 2\delta{l}m_i\pdt{u_j}{x_i}\delta{l}m_j + o(|\delta\vec{l}|^2)
\]
or,
\begin{equation}\label{c3s1e7}
\frac{1}{\delta{l}}\td{\delta{l}}{t} = m_im_j\pdt{u_j}{x_i} + o(|\delta\vec{l}|),
\end{equation}
where we wrote $\delta\vec{l} = \delta{l}\vec{m}$, $\vec{m}$ being a unit vector along the material line element. Similarly, from \eqref{c3s1e6},
\begin{eqnarray}
\delta S_i \frac{d}{dt}(\rho\delta S_i) &=& -\rho\delta S_j \pdt{u_j}{x_i}\delta S_i + o(|\delta\vec{S}|^2) \nonumber \\
\delta S \frac{d}{dt}(\rho\delta S) &=& -\rho(\delta S)^2 n_j\pdt{u_j}{x_i}n_i + o(|\delta\vec{S}|^2) \nonumber \\
\frac{1}{\rho\delta S}\frac{d}{dt}(\rho\delta S) &=& -n_in_j\pdt{u_j}{x_i} + o(|\delta\vec{S}|), \label{c3s1e7a}
\end{eqnarray}
where we wrote $\delta\vec{S} = \delta{S}\vec{n}$, $\vec{n}$ being a unit vector normal to the material area element.

\item Let us consider the change of a line integral over a material element. Let
\[
I = \int_P^Q \theta d\vec{l},
\]
where $\theta$ is an intensive property of the fluid, dependent only on $\vec{x}$ and $t$. We first express the integral as a Riemann sum,
\[
\int_P^Q \theta d\vec{l} = \lim_{\epsilon \rightarrow 0}\sum_n \theta_n \delta\vec{l}_n,
\]
where $\epsilon$ is the size of the largest sub-interval. Thus,
\begin{eqnarray*}
\frac{d}{dt}\int_P^Q \theta d\vec{l} &=& \frac{d}{dt}\lim_{\epsilon \rightarrow 0}\sum_n \theta_n \delta\vec{l}_n \\
 &=& \lim_{\epsilon \rightarrow 0}\sum_n \frac{D}{Dt}\left(\theta_n \delta\vec{l}_n \right) \\
 &=& \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n}\delta\vec{l}_n + \theta_n\md{\delta\vec{l}_n} \right)
\end{eqnarray*}
For a material element, the material derivative is same as total derivative so that
\[
\frac{d}{dt}\int_P^Q \theta d\vec{l} = \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n}\delta\vec{l}_n + \theta_n\td{\delta\vec{l}_n}{t} \right)
\]
Using \eqref{c3s1e3},
\[
\frac{d}{dt}\int_P^Q \theta d\vec{l} = \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n}\delta\vec{l}_n + \theta_n\delta\vec{l}\cdot\grad\vec{u} + \theta_no(|\delta\vec{l}|) \right)
\]
or, since $o(|\delta\vec{l}|) < \epsilon$
\[
\frac{d}{dt}\int_P^Q \theta d\vec{l} = \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n}\delta\vec{l}_n + \theta_n\delta\vec{l}\cdot\grad\vec{u}\right)
\]
or
\begin{equation}\label{c3s1e8}
\frac{d}{dt}\int_P^Q \theta d\vec{l} = \int_P^Q \md{\theta}d\vec{l}_n + \int_P^Q \theta d\vec{l}\cdot\grad\vec{u}
\end{equation}

\item Now consider the surface integral over a material element,
\[
I = \int \theta \un dS = \int \theta dS_i
\]
Writing the integral as a Riemann sum
\[
I = \lim_{\epsilon \rightarrow 0} \sum_n \theta_n \delta S_i^{(n)},
\]
where $\epsilon$ is the size of the largest area interval and $\delta S_i^{(n)}$ is the $n$th area element. Thus,
\begin{eqnarray*}
\td{I}{t} &=& \frac{d}{dt}\lim_{\epsilon \rightarrow 0} \sum_n \theta_n \delta S_i^{(n)} \\
 &=& \lim_{\epsilon \rightarrow 0} \sum_n \frac{D}{Dt}\left(\theta_n \delta S_i^{(n)}\right) \\
 &=& \lim_{\epsilon \rightarrow 0} \sum_n \left(\md{\theta_n}\delta S_i^{(n)} + \theta_n \md{\delta S_i^{(n)}}\right)
\end{eqnarray*}
For a material element, the material derivative is same as total derivative so that
\[
\td{I}{t} = \lim_{\epsilon \rightarrow 0} \sum_n \left(\md{\theta_n}\delta S_i^{(n)} + \theta_n \td{\delta S_i^{(n)}}{t}\right)
\]
Using \eqref{c3s1e5},
\[
\td{I}{t} =
\lim_{\epsilon \rightarrow 0} \sum_n \left[\md{\theta_n}\delta S_i^{(n)} + \theta_n\left(\pdt{u_j}{x_j}\delta S_i^{(n)} - \pdt{u_j}{x_i}\delta S_j^{(n)} + o(|\delta\vec{S}|)\right)\right]
\]
Since $o(|\delta\vec{S}|) < \epsilon$,
\[
\td{I}{t} = \lim_{\epsilon \rightarrow 0} \sum_n \left[\md{\theta_n}\delta S_i^{(n)} + \theta_n\left(\pdt{u_j}{x_j}\delta S_i^{(n)} - \pdt{u_j}{x_i}\delta S_j^{(n)}\right)\right]
\]
or
\begin{equation}\label{c3s1e9}
\frac{d}{dt}\int \theta dS_i = \int\md{\theta}dS_i + \int\theta\pdt{u_j}{x_j}dS_i - \int\theta\pdt{u_j}{x_i}dS_j
\end{equation}

\item An integral over a material volume element is
\[
I = \int\theta d\tau,
\]
Expressed as a Riemann sum,
\[
I = \lim_{\epsilon \rightarrow 0}\sum_n \theta_n \delta \tau_n,
\]
where $\epsilon$ is the size of the largest volume interval so that
\begin{eqnarray*}
\td{I}{t} &=& \frac{d}{dt}\lim_{\epsilon \rightarrow 0}\sum_n \theta_n \delta \tau_n \\
 &=& \lim_{\epsilon \rightarrow 0}\sum_n \frac{D}{Dt}\left(\theta_n \delta \tau_n\right) \\
 &=& \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n} \delta \tau_n + \theta_n\md{\delta \tau_n}\right)
\end{eqnarray*}
For a material element, the material derivative is same as total derivative so that
\[
\td{I}{t} = \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n} \delta \tau_n + \theta_n\td{\delta \tau_n}{t}\right)
\]
Using \eqref{c3s1e1},
\[
\td{I}{t} = \lim_{\epsilon \rightarrow 0}\sum_n \left(\md{\theta_n} \delta \tau_n + \theta_n\dive\vec{u}\delta \tau_n\right)
\]
or
\begin{equation}\label{c3s1e10}
\frac{d}{dt}\int\theta d\tau = \int\md{\theta} d\tau + \int\theta\dive\vec{u} d\tau
\end{equation}

\item A useful form of \eqref{c3s1e10} is
\begin{eqnarray*}
\frac{d}{dt}\int\theta\rho d\tau &=& \int\left(\md{(\theta\rho)} + \theta\rho\dive{u}\right)d\tau \\
 &=& \int\left(\md{\theta}\rho + \theta\md{\rho} + \theta\rho\dive{u}\right)d\tau \\
\end{eqnarray*}
From the equation of mass conservation,
\[
\md{\rho} = -\rho\dive{u}
\]
so that
\begin{equation}\label{c3s1e11}
\frac{d}{dt}\int\theta\rho d\tau = \int\rho\md{\theta}d\tau
\end{equation}

\item Several conservation laws can be put in the form
\[
\frac{d}{dt}\int\theta\rho d\tau = \int Q d\tau
\]
Using \eqref{c3s1e11}, the left hand side becomes,
\[
\int\rho\md{\theta}d\tau = \int Qd\tau,
\]
which can be true only if
\begin{equation}\label{c3s1e12}
\rho\md{\theta} = Q
\end{equation}

\item While deriving \eqref{c3s1e12} we travelled with the material element. We can get to the same conclusion if we, instead, focus on a fixed volume in space. If $V$ is such a volume
with surface area $A$ then
\[
\frac{d}{dt}\int\rho\theta dV
\]
is the rate of change of the quantity $\rho\theta$ contained in $V$. The change can be either because of a flux through the boundary or a source (or a sink) in $V$. If $\un$ is the
outward normal,
\[
\frac{d}{dt}\int\rho\theta dV = -\int\theta\rho\vec{u}\cdot\un dA + \int QdV
\]
Since the volume of integration is fixed, we can interchange the order of differentiation and integration on the left hand side to get
\[
\int\frac{\partial}{\partial t} (\theta\rho) dV = -\int\theta\rho\vec{u}\cdot\un dA + \int QdV
\]
Using the divergence theorem on the first term on the right,
\begin{equation}\label{c3s1e13}
\int\frac{\partial}{\partial t} (\theta\rho) dV = -\int\dive(\theta\rho\vec{u})dV + \int QdV
\end{equation}
which immediately leads to the differential form,
\begin{equation}\label{c3s1e14}
\frac{\partial}{\partial t} (\theta\rho) dV = -\dive(\theta\rho\vec{u}) + Q
\end{equation}
\end{itemize}

\section{Equations of motion}\label{c3s2}
\begin{itemize}
\item We note that the relation between time derivative of a vector $\vec{A}$ in a fixed and a rotating frame of reference is
\begin{equation}\label{c3s2e1}
\left(\td{\vec{A}}{t}\right)_f = \left(\td{\vec{A}}{t}\right)_r + \vec{\Omega} \vp \vec{A},
\end{equation}
where the subscripts $f$ and $r$ mean \enquote*{fixed} and \enquote*{rotating}. $\vec{\Omega}$ is the angular velocity of the rotating frame of reference with respect to the fixed
frame of reference. An immediate consequence of this equation is
\begin{equation}\label{c3s2e2}
\left(\td{\vec{\Omega}}{t}\right)_f = \left(\td{\vec{\Omega}}{t}\right)_r
\end{equation}

\item If $\vec{A} = \vec{x}$,
\[
\left(\td{\vec{x}}{t}\right)_f = \left(\td{\vec{x}}{t}\right)_r + \vec{\Omega} \vp \vec{x},
\]
Differentiating once more
\[
\left(\frac{d^2\vec{x}}{dt^2}\right)_f = \left[\frac{d}{dt}\left(\td{\vec{x}}{t}\right)_r\right]_f + \left(\td{\vec{\Omega}}{t}\right)_f\vp\vec{x} +
\vec{\Omega}\vp\left(\td{\vec{x}}{t}\right)_f
\]
or
\[
\left(\td{\vec{x}}{t}\right)_f = \left(\frac{d^2\vec{x}}{dt^2}\right)_r + \vec{\Omega}\vp\left(\td{\vec{x}}{t}\right)_r + \left(\td{\vec{\Omega}}{t}\right)_f \vp \vec{x} +
\vec{\Omega}\vp\left[\left(\td{\vec{x}}{t}\right)_r + \vec{\Omega} \vp \vec{x}\right]
\]
or
\begin{equation}\label{c3s2e3}
\left(\frac{d^2\vec{x}}{dt^2}\right)_f = \left(\frac{d^2\vec{x}}{dt^2}\right)_r + \left(\td{\vec{\Omega}}{t}\right)_r \vp \vec{x} + 2\vec{\Omega}\vp\left(\td{\vec{x}}{t}\right)_r
+ \vec{\Omega}\vp(\vec{\Omega} \vp \vec{x})
\end{equation}
where we used \eqref{c3s2e2}.

\item The momentum density of a fluid element is $\rho\vec{u}$. The rate of change of this quantity is
\[
\frac{d}{dt}\int\rho\vec{u}d\tau,
\]
which using \eqref{c3s1e11} can be written as
\[
\int \rho\md{\vec{u}}d\tau
\]
If $\vec{F}$ is the body force per unit volume and $\sigma_{ij}$ is the surface force
per unit area. then the total force is
\[
\int\rho F_i d\tau + \int\sigma_{ij}n_j dS = \int\rho F_i d\tau + \int\pdt{\sigma_{ij}}{x_j} d\tau
\]
By Newton's second law, this quantity is equal to the total force acting on the fluid element, so that
\begin{equation}\label{c3s2e4}
\int \rho\md{u_i}d\tau = \int\rho F_i d\tau + \int\pdt{\sigma_{ij}}{x_j} d\tau,
\end{equation}
which, in differential form is,
\begin{equation}\label{c3s2e5}
\rho\md{u_i} = \rho F_i + \pdt{\sigma_{ij}}{x_j},
\end{equation}

\item We can get an equation of motion even by considering a fixed volume $V$ of the fluid. The rate of change of momentum of fluid in $V$ is
\[
\frac{d}{dt}\int\rho u_i dV,
\]
Since the volume of integration is fixed, we can as well write it as
\[
\int\frac{\partial}{\partial t}(\rho u_i)dV
\]
The flux of momentum through the surface $A$ of the volume is
\[
-\int\rho u_i u_j n_j dA
\]
while the \enquote*{source terms} are
\[
\int\rho F_i dV + \int\sigma_{ij}n_j dA
\]
so that the equation of motion is
\begin{equation}\label{c3s2e6}
\int\frac{\partial}{\partial t}(\rho u_i)dV = -\int\rho u_i u_j n_j dA + \int\rho F_i dV + \int\sigma_{ij}n_j dA
\end{equation}
Using divergence theorem for the last term on the right hand side,
\begin{equation}\label{c3s2e7}
\int\frac{\partial}{\partial t}(\rho u_i)dV = -\int\rho u_i u_j n_j dA + \int\rho F_i dV + \int\pdt{\sigma_{ij}}{x_j} dV
\end{equation}

\item If $\vec{F}$ is a conservative force so that $\rho\vec{F} = -\grad(\rho\Psi)$, for a potential function $\Psi$, the second term on the right hand side of \eqref{c3s2e6} can be
written as
\[
\int\rho F_i dV = -\int\pdt{\rho\Psi}{x_i}dV = -\int\rho\Psi n_i dA
\]
so that
\[
\int\frac{\partial}{\partial t}(\rho u_i)dV = -\int\rho u_i u_j n_j dA + \int\left(-\rho\Psi n_i + \sigma_{ij}n_j\right) dA
\]
If the motion is steady, the integrand on the left hand side is zero and
\begin{equation}\label{c3s2e8}
\int\rho u_i u_j n_j dA = \int\left(-\rho\Psi n_i + \sigma_{ij}n_j\right) dA
\end{equation}
Equation \eqref{c3s2e8} says that \emph{under steady state} the convective flux of momentum out of the surface $A$ bounding $V$ is balanced by the resultant contact force exerted at
the boundary by the surrounding medium and the resultnt force at the boundary arising from the stress system equivalent to the body force. The loss of momemtum is exactly compensated by
the two forces.
\end{itemize}

\section{The expression for the stress tensor}\label{c3s3}
\begin{itemize}
\item The average value of the normal component of stress on a surface element at $\vec{x}$, over all directions of $\un$ is
\[
\langle\sigma\rangle = \frac{1}{4\pi}\int n_i\sigma_{ij}n_j d\Omega(\un)
\]
Since we are changing only the directions of the normals, staying at the same point $\vec{x}$, we can pull out $\sigma_{ij}$ out of the integral,
\[
\langle\sigma\rangle = \frac{1}{4\pi}\sigma_{ij}\int n_i n_j d\Omega(\un)
\]
Using \eqref{c3sae1},
\begin{equation}\label{c3s3e1}
\langle\sigma\rangle = \frac{1}{4\pi}\sigma_{ij}\frac{4\pi}{3}\delta_{ij} = \frac{1}{3}\sigma_{ii}
\end{equation}

\item For any tensor $\{\sigma_{ij}\}$, the quantity $\sigma_{ii}$ is an invariant under rotations. Therefore, from \eqref{c3s3e1}, $\langle\sigma\rangle$ is an invariant quantity. If
the fluid were static, $\sigma_{ij} = -p\delta_{ij}$ and hence $\langle\sigma\rangle = -p$. Thus, the analog of pressure, in general flow conditions is $-\langle\sigma\rangle$.

\item Taking the analogy in the reverse direction, we \emph{define} the pressure at a point in a moving fluid as
\begin{equation}\label{c3s3e2}
p = -\frac{\sigma_{ii}}{3}
\end{equation}

\item The pressure so defined is \emph{not} the same as the thermodynamic pressure. The latter quantity is defined for equilibrium conditions while the former, being a purely mechanical
quantity, is defined for fluids in motion. It is only when the fluid is at rest does the mechanical pressure defined by \eqref{c3s3e2} become identical with thermodynamic pressure.

\item We write the stress tensor as a sum of an isotropic tensor and a \enquote*{deviatoric} tensor. Thus,
\[
\sigma_{ij} = -p\delta_{ij} + d_{ij}
\]
The deviatoric part, $d_{ij}$ is assumed to be proportional to the local velocity gradient. The most general linear relationship between the two tensors $d_{ij}$ and $\grad\vec{u}$ is
\[
d_{ij} = A_{ijkl}\pdt{u_k}{x_l}
\]
Since we wrote
\[
\pdt{u_k}{x_l} = \frac{1}{2}\left(\pdt{u_k}{x_l} + \pdt{u_l}{x_k}\right) + \frac{1}{2}\left(\pdt{u_k}{x_l} - \pdt{u_l}{x_k}\right) = e_{kl} + \xi_{kl}
\]
and we further noticed that $\xi_{kl}$ is an anti-symmetric tensor so that it can be expressed in terms of a single vector $\vec{\omega}$ as
\[
\xi_{kl} = -\frac{1}{2}\epsilon_{klm}\omega_m
\]
and hence
\[
d_{ij} = A_{ijkl}e_{kl} - \frac{1}{2}A_{ijkl}\epsilon_{klm}\omega_m
\]
The tensor $A_{ijkl}$ is a characteristic of the fluid alone. If the fluid is isotropic then so is $A_{ijkl}$. Now, an isotropic tensor of fourth order can be written, following
\eqref{mr12e18}, as
\[
A_{ijkl} = \mu\delta_{ik}\delta_{jl} + \mu^\op\delta_{il}\delta_{jk} + \mu^\tp\delta_{ij}\delta_{kl},
\]
where $\mu, \mu^\op$ and $\mu^\tp$ are scalars. Since the stress tensor is symmetric, $\mu = \mu^\op$ so that
\[
A_{ijkl} = \mu(\delta_{ik}\delta_{jl} + \delta_{il}\delta_{jk}) + \mu^\tp\delta_{ij}\delta_{kl},
\]
Therefore,
\begin{eqnarray*}
d_{ij} &=& A_{ijkl}e_{kl} - \frac{1}{2}A_{ijkl}\epsilon_{klm}\omega_m \\
 &=& \mu(\delta_{ik}\delta_{jl} + \delta_{il}\delta_{jk})e_{kl} + \mu^\tp\delta_{ij}\delta_{kl}e_{kl} - \\
 & & \frac{\mu}{2}\left(\delta_{ik}\delta_{jl} + \delta_{il}\delta_{jk}\right)\epsilon_{klm}\omega_m - \frac{\mu^\tp\delta_{ij}\delta_{kl}}{2}\epsilon_{klm}\omega_m \\
 &=& 2\mu e_{ij} + \mu^\tp e_{kk} \delta_{ij} - \frac{\mu}{2}(\epsilon_{ijm} + \epsilon_{jim})\omega_m - \frac{\mu^\tp}{2}\delta_{ij}\epsilon_{kkm}\omega_m
\end{eqnarray*}
Now, $\epsilon_{ijm} + \epsilon_{jim} = 0$ and $\epsilon_{kkm} = 0$, so that
\begin{equation}\label{c3s3e3}
d_{ij} = 2\mu e_{ij} + \mu^\tp\Delta\delta_{ij}
\end{equation}

\item Since the deviatoric tensor $d_{ij}$ does not contribute to the normal stress, $d_{ii} = 0$. From \eqref{c2s3e3},
\[
d_{ii} = 2\mu e_{ii} + \mu^\tp \Delta \delta_{ii} = 2\mu\Delta + 3\mu^\tp\Delta = \Delta(2\mu + 3\mu^\tp)
\]
$d_{ii} = 0$ implies
\[
\mu^\tp = -\frac{2}{3}\mu,
\]
so that \eqref{c3s3e3} becomes,
\begin{equation}\label{c3s3e4}
d_{ij} = 2\mu\left(e_{ij} -\frac{1}{3}\Delta\delta_{ij}\right)
\end{equation}

\item Molecular relaxation times involving mass transport are usually of the order of $10^{-9}s$\cite{bagchi2012molecular}. Time scale corresponding to macroscopic velocity gradient is
reciprocal of $|\grad\vec{u}|$. Thus, only when $|\grad\vec{u}|$ is of $O(10^9)$ does molecular motion affect the continuum variables. That is the reason why the linear relationship
between $d_{ij}$ and $\grad\vec{u}$ is valid for a very large range of gradients.

\item Using \eqref{c3s3e4} in the expression for the stress tensor, we get
\begin{equation}\label{c3s3e5}
\sigma_{ij} = -p\delta_{ij} + 2\mu\left(e_{ij} - \frac{1}{3}\Delta\delta_{ij}\right)
\end{equation}
Putting it in the equation of motion \eqref{c3s2e5},
\begin{equation}\label{c3s3e5a}
\rho\md{u_i} = \rho F_i - \pdt{p}{x_i} + \frac{\partial}{\partial x_j}\left[2\mu\left(e_{ij} - \frac{1}{3}\Delta\delta_{ij}\right)\right],
\end{equation}
Since
\[
e_{ij} = \frac{1}{2}\left(\pdt{u_i}{x_j} + \frac{u_j}{x_i}\right),
\]
the divergence of rate-of-strain tensor is
\[
\frac{1}{2}\left[\frac{\partial^2 u_i}{\partial x_j \partial x_j} + \frac{\partial}{\partial x_i}\left(\pdt{u_j}{x_j}\right)\right] =
\frac{1}{2}\left(\nabla^2 u_i + \pdt{\Delta}{x_i}\right)
\]
Therefore, the equation of motion is
\begin{equation}\label{c3s3e6}
\rho\md{u_i} = \rho F_i - \pdt{p}{x_i} + \mu\left(\nabla^2 u_i + \frac{1}{3}\pdt{\Delta}{x_i}\right)
\end{equation}
This is the Navier-Stokes equation. If the fluid is incompressible, it is simplified to
\[
\rho\md{u_i} = \rho F_i - \pdt{p}{x_i} + \mu\nabla^2 u_i
\]
and be expressed in Gibbs notation as
\begin{equation}\label{c3s3e7}
\rho\md{\vec{u}} = \rho\vec{F} - \grad p + \mu\nabla^2\vec{u}
\end{equation}

\item Equation (1.3.2) of the book defined the vector $\vec\Sigma(\un)$ as the force exerted by the fluid on the side of the surface element to which $\un$ points, on the fluid on the
side which $\un$ points away from. Further, equation (1.3.5) of the book defined the stress tensor as $\Sigma_j = \sigma_{ij}n_j$. $\sigma_{ij}$ is the $i$ component of the force per
unit area exerted on a plane surface whose normal is in the $j$ direction. If we consider a simple shearing motion with velocity $(U(y), 0, 0)$, the only non-zero components of the
deviatoric stress tensor, in an incompressible fluid, are
\[
d_{12} = d_{21} = \mu\td{U}{y}
\]
$d_{12}$ is the $x$ component of the force per unit area on a plane surface element whose normal points in the positive $y$ direction. Further, it is the force exerted by the fluid on
the side to which the normal points on the fluid on the side which $\un$ points away from. Therefore, if we consider an area element in the $xz$ plane, then the fluid in the upper part
($y > 0$) exerts a force in the positive $x$ direction. Clearly, the force is in a direction to erase the velocity difference between a layer of fluid in the $xz$ plane and the one just
above it. This can happen only if $\mu > 0$.

\item Consider an interface separating two media. Referring to figure (1.9.4) of the book, if $t_i$ denotes the tangent to the interface then the tangential stress in upper medium is
$t_i\sigma^\tp_{ij}n_j$. Continuity of tangential stress implies
\begin{equation}\label{c3s3e8}
t_i\sigma^\tp_{ij}n_j = t_i\sigma^\op_{ij}n_j
\end{equation}
Using \eqref{c1s7e7} for the normal components
\begin{equation}\label{c3s3e9}
n_i\sigma^\op_{ij}n_j - n_i\sigma^\tp_{ij}n_j = \gamma\left(\frac{1}{R_1} + \frac{1}{R_2}\right)
\end{equation}
\end{itemize}

\subsection{Exercise}
\begin{enumerate}
\item Continuing to refer to figure (1.9.4) of the book, if the upper medium is a gas, then the continuity of tangential stress, given by \eqref{c3s3e8} becomes
\[
0 = t_i\sigma^\op_{ij}n_j
\]
Thus, if there is a material line element normal to the interface, then there is no tangential force acting on it. Therefore, it will continue to be in the normal direction.

(Note that $\sigma_{ij}$ is the force per unit area in the $i$ direction on a small material area element, normal to which points in the $j$ direction. Further, it is the force by the
fluid in which the normal points, on the fluid on the side which the normal points away from. If we choose a coordinate system such that the interface lies in the $xy$ plane and the $z$
axis points upwards then $\sigma_{12}n_2$ is the force in the $x$ direction and $\sigma_{12}n_1$ is the force in the $y$ direction. To get their magnitudes, we just need to take a
dot product with a unit vector in that direction. Thus, the magnitude of the two forces in $xy$ plane are $n_1\sigma_{12}n_2$ and $n_2\sigma_{12}n_1$.)
\end{enumerate}

\section{Changes in the internal energy of a fluid in motion}\label{c3s4}
\begin{itemize}
\item A material element of a fluid in motion is not a thermodynamic system in equilibrium. Therefore, it is not right to assign thermodynamic variables to it without close examination.
Of the thermodynamical variables, density $\rho$ can be defined as a local mass per unit volume even if the material element is not in equilibrium. The first law of thermodynamics is
applicable even to systems not in equilibrium. Since the heat added to the element $\dbar Q$ and the work done on it $\dbar W$ are experimental quantities those can be observed, so is
their sum $dU$. Therefore, even internal energy can be defined for a material element.

\item If the fluid is homogeneous, then we can use the instantenous values of $\rho$ and $U$ to define other thermodynamic quantities. For instance, we can define $T$ using the equation
of state.

\item Consider a fluid element of volume $\tau$ and surface area $S$. The volume forces $F_i$ do a work on it at the rate
\[
\int u_i F_i d\tau
\]
while the surface forces $\sigma_{ij}$ do work at the rate
\[
\int u_i\sigma_{ij}n_j dS = \int\pdt{u_i\sigma_{ij}}{x_j} d\tau
\]
Thus, the rate at which all forces do work on the fluid is
\begin{eqnarray*}
 &=& \int u_i F_i d\tau + \int\pdt{u_i\sigma_{ij}}{x_j} d\tau \\
 &=& \int u_i F_i d\tau + \int\pdt{\sigma_{ij}}{x_j}u_i d\tau + \int\pdt{u_i}{x_j}\sigma_{ij}d\tau
\end{eqnarray*}
The second term above arises because of a variation in stress across the fluid element.It leads to change in the element's kinetic energy. The third term above arises because of a
variation in velocity across the fluid element. It causes the element to deform without a change in the element's kinetic energy. The work done in deformation shows as an increase in
internal energy of the element. The three integrals can be written as one with the integrand,
\[
u_iF_i + \pdt{\sigma_{ij}}{x_j}u_i + \pdt{u_i}{x_j}\sigma_{ij} = u_i\left(F_i + \pdt{\sigma_{ij}}{x_j}\right) + \pdt{u_i}{x_j}\sigma_{ij}
\]
Using \eqref{c3s2e5} the terms in the bracket can be combined to get
\begin{equation}\label{c3s4e1}
\rho u_i\md{u_i} + \pdt{u_i}{x_j}\sigma_{ij}
\end{equation}
The integrand, written above, can be interpreted as a the rate at which work is being done on a fluid element per unit volume. To get the rate per unit mass, we divide it be $\rho$. Thus,
the rate at which work is done by volume and surface forces per unit mass is
\begin{equation}\label{c3s4e2}
u_i\md{u_i} + \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} = \frac{1}{2}\md{(u_iu_i)} + \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j}
\end{equation}
The form on the right hand side makes it clear that the first term represents the change in kinetic energy while the second term represents the change in internal energy.

\item The rate at which heat enters a fluid element due to temperature gradient is
\[
\int k\grad T\cdot\un dS = \int\dive(k\grad T)d\tau
\]
Thus the rate, per unit mass, of addition of heat to the element is
\begin{equation}\label{c3s4e3}
\frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}

\item If $u$ is the internal energy per unit mass, then by first law of thermodynamics, the rate of its change per unit mass is
\begin{equation}\label{c3s4e3a}
\md{u} = \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
or
\[
\md{u} = \frac{1}{2}\left(\frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} + \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j}\right) + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
We can always interchange the indices in the second term in the bracket, it is a scalar after all, to get
\[
\md{u} = \frac{1}{2}\left(\frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} + \frac{\sigma_{ji}}{\rho}\pdt{u_j}{x_i}\right) + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
But the stress tensor is symmetric, so that
\[
\md{u} = \frac{1}{2}\left(\frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} + \frac{\sigma_{ij}}{\rho}\pdt{u_j}{x_i}\right) + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\begin{equation}\label{c3s4e4}
\md{u} = \frac{\sigma_{ij}}{\rho}e_{ij} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
Using \eqref{c3s3e5},
\[
\md{u} = \frac{e_{ij}}{\rho}\left[-p\delta_{ij} + 2\mu\left(e_{ij} -\frac{1}{3}\Delta\delta_{ij}\right)\right] + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\[
\md{u} = \left[-p\frac{e_{ii}}{\rho} + 2\frac{\mu}{\rho}\left(e_{ij}e_{ij} -\frac{1}{3}\Delta e_{ii}\right)\right] + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
Putting $e_{ii} = \Delta$,
\begin{equation}\label{c3s4e5}
\md{u} = -p\frac{\Delta}{\rho} + 2\frac{\mu}{\rho}\left(e_{ij}e_{ij} -\frac{1}{3}\Delta^2\right) + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}

\item We will now show that
\[
-p\Delta + 2\mu\left(e_{ij}e_{ij} - \frac{\Delta^2}{3}\right) = -p\delta_{ij}\left(\frac{\Delta\delta_{ij}}{3}\right) +
2\mu\left(e_{ij} - \frac{\Delta\delta_{ij}}{3}\right)\left(e_{ij} - \frac{\Delta\delta_{ij}}{3}\right)
\]
\begin{eqnarray*}
\text{RHS} &=& -p\delta_{ij}\left(\frac{\Delta\delta_{ij}}{3}\right) + 2\mu\left(e_{ij} - \frac{\Delta\delta_{ij}}{3}\right)\left(e_{ij} - \frac{\Delta\delta_{ij}}{3}\right) \\
 &=& -p\Delta\frac{\delta_{ij}\delta_{ij}}{3} + 2\mu\left(e_{ij}e_{ij} - 2e_{ij}\frac{\Delta\delta_{ij}}{3} + \frac{\Delta^2\delta_{ij}\delta_{ij}}{9}\right) \\
 &=& -p\Delta + 2\mu\left(e_{ij}e_{ij} - \frac{2}{3}\Delta e_{ii} + \frac{\Delta^2}{3}\right) \\
 &=& -p\Delta + 2\mu\left(e_{ij}e_{ij} - \frac{1}{3}\Delta^2\right) \\
 &=& \text{LHS}
\end{eqnarray*}

\item In \eqref{c3s4e5}, the first term on the right hand side involves only the isotropic part of stress and rate-of-strain while the second term has only the corresponding
deviatoric parts. Further, the second term is non-negative. It represents the heating of the element due to frictional forces. It is a quantity of interest and therefore deserves
a separate symbol
\begin{equation}\label{c3s4e6}
\Phi = 2\frac{\mu}{\rho}\left(e_{ij}e_{ij} -\frac{1}{3}\Delta^2\right)
\end{equation}

\item We have so far argued that the usual definitions of $\rho$ and $U$ can be used in non-equilibrium states of the fluid element. We denote the pressure obtained from using
instantaneous values of $\rho$ and $U$ in equilibrium equations of state by $p_e$. Recall that $p$, as used in the equations above, is \emph{defined} to be the average stress at a
point. In general $p$ and $p_e$ are not the same when the fluid is in motion, although they are identical in a static fluid.

\item Since the difference between $p$ and $p_e$ is solely due to fluid's motion, we assume that $p - p_e$ depends only on the local velocity gradient. We further assume that the
dependence is linear so that
\[
p - p_e = B_{ij}\pdt{u_i}{x_j} = B_{ij}e_{ij} - \frac{1}{2}B_{ij}\epsilon_{ijk}\omega_k
\]
If the fluid is isotropic, we can expect $B_{ij}$ to be an isotropic tensor. From section \ref{mr12}, an isotropic sencond rank tensor is a scalar multiple of Kronecker delta. Therefore,
let
\begin{equation}\label{c3s4e7}
B_{ij} = -\kappa\delta_{ij}
\end{equation}
so that
\begin{equation}\label{c3s4e8}
p - p_e = -\kappa\Delta
\end{equation}
The constant $\kappa$ is usually called the bulk viscosity of the fluid.

\item Since $p$ and $p_e$ are identical in a static fluid, we can surmise that $p - p_e$ is due to deviatoric part of the stress. If the fluid contracts during the motion, that is its
local $\Delta$ is negative, $p$ will be greater than $p_e$. Therefore, we can write $p - p_e = -\kappa\Delta$, assuming that $\kappa > 0$. If the fluid expands during the motion, then
its local $\Delta$ is positive and $p$ will end up being smaller than $p_e$. Therefore, once again $p - p_e = -\kappa\Delta$ is a valid relation if $\kappa > 0$.

\item We can now write the first term on the right hand side of \eqref{c3s4e5} as
\begin{equation}\label{c3s4e9}
-p\frac{\Delta}{\rho} = -p_e\frac{\Delta}{\rho} + \kappa\frac{\Delta^2}{\rho}
\end{equation}
The term
\[
-p_e\frac{\Delta}{\rho}
\]
is contribution by the reversible effects of equilibrium pressure while the term
\[
\kappa\frac{\Delta^2}{\rho} \ge 0
\]
is the dissipative effect of bulk viscosity. Although the second term is usually much smaller than the first one on the right hand side of \eqref{c3s4e9}, being a positive quantity,
periodic pressure variations over a long enough time can make it significant. That is why, it is important in transmission of sound waves in fluids.

\item We will now derive equation (3.4.11) of the book. Starting from \eqref{c3s4e5} and \eqref{c3s4e6},
\[
\md{u} = -p\frac{\Delta}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
From \eqref{c3s4e9},
\[
\md{u} = -p_e\frac{\Delta}{\rho} + \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
We can write the mass conservation equation as
\[
\Delta = -\frac{1}{\rho}\md{\rho}
\]
so that
\[
\md{u} = \frac{p_e}{\rho^2}\md{\rho} + \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\[
\md{u} = -p_e\frac{D}{Dt}\left(\frac{1}{\rho}\right) + \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or, since volume per unit mass $v = 1/\rho$,
\[
\md{u} = -p_e\md{v} + \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\[
\md{u} + p_e\md{v} = \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\begin{equation}\label{c3s4e9a}
T\md{s} = \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
where we have used the first part of equation (1.5.20) of the book, namely, $Tds = du + p_edv$. If we also use the second part, that is $Tds = c_pdT - \beta v T dp_e$, we get
\[
c_p\md{T} - \beta v T \md{p_e} = \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
If we were to use $\rho$ throughout,
\begin{equation}\label{c3s4e10}
c_p\md{T} - \frac{\beta T}{\rho}\md{p_e} = \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
\end{itemize}

\section{Bernoulli's theorem}\label{c3s5}
\begin{itemize}
\item We will derive equation (3.5.1) of the book. To do so, we begin with the equation of motion \eqref{c3s2e5}
\[
\rho\md{u_i} = \rho F_i + \pdt{\sigma_{ij}}{x_j}
\]
which is same as
\[
u_i\md{u_i} = u_iF_i + \frac{u_i}{\rho}\pdt{\sigma_{ij}}{x_j}
\]
Now,
\[
\frac{\partial}{\partial x_j} (u_i\sigma_{ij}) = u_i\pdt{\sigma_{ij}}{x_j} + \sigma_{ij}\pdt{u_i}{x_j}
\]
so that
\[
u_i\md{u_i} = u_iF_i + \frac{1}{\rho}\pdt{(u_i\sigma_{ij})}{x_j} - \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j}
\]
or
\begin{equation}\label{c3s5e1}
\frac{D}{Dt}\left(\frac{u_iu_i}{2}\right) = u_iF_i + \frac{1}{\rho}\pdt{(u_i\sigma_{ij})}{x_j} - \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j}
\end{equation}
From \eqref{c3s4e3a}
\[
\md{u} = \frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
so that
\[
-\frac{\sigma_{ij}}{\rho}\pdt{u_i}{x_j} = -\md{u} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
Putting this in \eqref{c3s5e1}, we get
\[
\frac{D}{Dt}\left(\frac{u_iu_i}{2}\right) = u_iF_i + \frac{1}{\rho}\pdt{(u_i\sigma_{ij})}{x_j} -\md{u} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
or
\begin{equation}\label{c3s5e2}
\frac{D}{Dt}\left(u + \frac{u_iu_i}{2}\right) = u_iF_i + \frac{1}{\rho}\pdt{(u_i\sigma_{ij})}{x_j} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}

\item If $\vec{F}$ is a conservative force with a potential function $\Psi$,
\[
u_iF_i = -u_i\pdt{\Psi}{x_i} = -\md{\Psi} + \pdt{\Psi}{t}
\]
If $\Psi$ is also independent of time,
\[
u_iF_i = -\md{\Psi}
\]
Putting it in \eqref{c3s5e2}, we get
\begin{equation}\label{c3s5e3}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \Psi\right) = \frac{1}{\rho}\pdt{(u_i\sigma_{ij})}{x_j} + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
From \eqref{c3s3e5}
\[
\sigma_{ij} = -p\delta_{ij} + 2\mu\left(e_{ij} -\frac{1}{3}\Delta\delta_{ij}\right)
\]
so that
\[
u_i\sigma_{ij} = -pu_j + 2\mu\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)
\]
and hence,
\begin{eqnarray*}
\pdt{(u_i\sigma_{ij})}{x_j} &=& -\pdt{(pu_j)}{x_j} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right) \\
 &=& -p\pdt{u_j}{x_j} - u_j\pdt{p}{x_j} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right) \\
 &=& -p\pdt{u_j}{x_j} - \md{p} + \pdt{p}{t} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)
\end{eqnarray*}
and hence, from \eqref{c3s5e3},
\begin{eqnarray*}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \Psi\right) &=&
\frac{1}{\rho}\left[-p\pdt{u_j}{x_j} - \md{p} + \pdt{p}{t} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)\right]\\
 & & + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{eqnarray*}
Using mass conservation equation,
\[
\pdt{u_j}{x_j} = -\frac{1}{\rho}\md{\rho}
\]
so that
\begin{eqnarray*}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \Psi\right) &=&
\frac{p}{\rho^2}\md{\rho} - \frac{1}{\rho}\md{p} + \frac{1}{\rho}\left[\pdt{p}{t} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)\right] \\
 & & + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{eqnarray*}
Now,
\[
\frac{p}{\rho^2}\md{\rho} - \frac{1}{\rho}\md{p} = -p\frac{D}{Dt}\left(\frac{1}{\rho}\right) - \frac{1}{\rho}\md{p} = -\frac{D}{Dt}\left(\frac{p}{\rho}\right),
\]
so that
\begin{eqnarray*}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \Psi\right) &=&
-\frac{D}{Dt}\left(\frac{p}{\rho}\right) + \frac{1}{\rho}\left[\pdt{p}{t} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)\right] \\
 & & + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{eqnarray*}
or
\begin{eqnarray*}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \frac{p}{\rho} + \Psi\right) &=&
\frac{1}{\rho}\left[\pdt{p}{t} + 2\mu\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right)\right] \\
 & & + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{eqnarray*}
If the pressure is steady,
\begin{equation}\label{c3s5e4}
\frac{D}{Dt}\left(\frac{u_iu_i}{2} + u + \frac{p}{\rho} + \Psi\right) = \frac{2\mu}{\rho}\frac{\partial}{\partial x_j}\left(u_ie_{ij} -\frac{\Delta}{3} u_j\right) +
\frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
If the fluid is frictioness, $\mu = 0$ and it is non-conducting, $k = 0$ so that
\begin{equation}\label{c3s5e5}
\md{\mathcal{H}} = 0,
\end{equation}
where the function $\mathcal{H}$ is defined as
\begin{equation}\label{c3s5e6}
\mathcal{H} = \frac{u_iu_i}{2} + u + \frac{p}{\rho} + \Psi
\end{equation}
If we denote the speed of a fluid parcel by $q$,
\begin{equation}\label{c3s5e7}
\mathcal{H} = \frac{q^2}{2} + u + \frac{p}{\rho} + \Psi
\end{equation}

\item Since $Tds = du + pdv$, $T\grad S = \grad u + p\grad{v}$ or
\[
T\grad S = \grad u + p\grad\left(\frac{1}{\rho}\right) = \grad u + \grad\left(\frac{p}{\rho}\right) - \frac{1}{\rho}\grad p,
\]
or
\begin{equation}\label{c3s5e8}
T\grad S + \frac{1}{\rho}\grad p = \grad\left(u + \frac{p}{\rho}\right)
\end{equation}
From \eqref{c3s5e7}
\[
\grad\mathcal{H} = \grad\left(\frac{q^2}{2} + \Psi\right) + \grad\left(u + \frac{p}{\rho}\right)
\]
From \eqref{c3s5e8}, we get
\begin{equation}\label{c3s5e9}
\grad\mathcal{H} = T\grad S + \grad\left(\frac{q^2}{2} + \Psi\right) + \frac{1}{\rho}\grad p
\end{equation}

\item For a steady flow of a frictionless fluid, the equation of motion \eqref{c3s3e7} becomes
\[
\rho\vec{u}\cdot\grad\vec{u} = -\rho\grad\Psi - \grad p
\]
Since
\[
\grad(\vec{u}\cdot\vec{u}) = \grad{q^2} = 2\vec{u}\cdot\grad\vec{u} + 2\vec{u}\vp\curl\vec{u},
\]
we have
\[
\grad\left(\frac{q^2}{2}\right) = \vec{u}\cdot\grad\vec{u} + \vec{u}\vp\vec{\omega}
\]
or
\[
\rho\grad\left(\frac{q^2}{2}\right) - \rho\vec{u}\vp\vec{\omega} = -\rho\grad\Psi - \grad p
\]
or
\[
\grad\left(\frac{q^2}{2} + \psi\right)  + \frac{1}{\rho}\grad p = \vec{u}\vp\vec{\omega}
\]
Putting this relation in \eqref{c3s5e9}, we get
\begin{equation}\label{c3s5e10}
\grad\mathcal{H} = T\grad S + \vec{u}\vp\vec{\omega}
\end{equation}

\item From the relation, $Tds = du + pdv$, we get
\[
Tds = du + pd\left(\frac{1}{\rho}\right) = du + d\left(\frac{p}{\rho}\right) - \frac{1}{\rho}dp
\]
For an isentropic (adiabatic) process,
\[
\frac{1}{\rho}dp = d\left(u + \frac{p}{\rho}\right)
\]
If
\[
c^2 = \left(\pdt{p}{\rho}\right)_S,
\]
we have
\[
dp = c^2d\rho
\]
or
\[
\frac{1}{\rho}dp = \frac{c^2}{\rho}d\rho
\]
so that
\[
\frac{c^2}{\rho}d\rho = d\left(u + \frac{p}{\rho}\right)
\]
or
\begin{equation}\label{c3s5e11}
u + \frac{1}{\rho} = \int\frac{c^2}{\rho}d\rho
\end{equation}
or
\begin{equation}\label{c3s5e12}
\frac{D}{Dt}\left(u + \frac{1}{\rho}\right) = \frac{D}{Dt}\int\frac{c^2}{\rho}d\rho
\end{equation}
Using \eqref{c3s5e11} in the definition \eqref{c3s5e7} we get
\begin{equation}\label{c3s5e13}
\mathcal{H} = \frac{q^2}{2} + \int\frac{c^2}{\rho}d\rho + \Psi
\end{equation}

\item We will show that
\begin{equation}\label{c3s5e14}
-\vec{\Omega}\vp(\vec{\Omega}\vp\vec{x}) = \frac{1}{2}\grad(\vec{\Omega}\vp\vec{x})^2
\end{equation}
Starting from the right hand side,
\begin{equation}\label{c3s5e15}
\frac{1}{2}\grad(\vec{\Omega}\vp\vec{x})^2 = (\vec{\Omega}\vp\vec{x})\cdot\grad(\vec{\Omega}\vp\vec{x}) + (\vec{\Omega}\vp\vec{x})\vp\curl(\vec{\Omega}\vp\vec{x})
\end{equation}
Since $\vec{\Omega}$ is a constant,
\[
\curl(\vec{\Omega}\vp\vec{x}) = \vec{\Omega}\dive\vec{x} - \vec{\Omega}\cdot\grad\vec{x} = 3\vec{\Omega} - \vec{\Omega} = 2\vec{\Omega}
\]
so that
\begin{equation}\label{c3s5e16}
(\vec{\Omega}\vp\vec{x})\vp\curl(\vec{\Omega}\vp\vec{x}) = 2(\vec{\Omega}\vp\vec{x})\vp\vec{\Omega} = -2\vec{\Omega}\vp(\vec{\Omega}\vp\vec{x})
\end{equation}
Now consider
\begin{eqnarray*}
(\vec{\Omega}\vp\vec{x})\cdot\grad(\vec{\Omega}\vp\vec{x}) &=&
\left(\epsilon_{ijk}\uvec{i}\Omega_j x_k \cdot \uvec{l}\frac{\partial}{\partial x_l}\right)\left(\epsilon_{pqr}\uvec{p}\Omega_q x_r\right) \\
 &=& \epsilon_{ijk}\Omega_j x_k\frac{\partial}{\partial x_i}\left(\epsilon_{pqr}\uvec{p}\Omega_q x_r\right) \\
 &=& \epsilon_{ijk}\Omega_j x_k \epsilon_{pqr}\uvec{p}\Omega_q \delta_{ir} \\
 &=& \epsilon_{ijk}\Omega_j x_k \epsilon_{pqi}\uvec{p}\Omega_q \\
 &=& \epsilon_{ijk}\epsilon_{pqi} \Omega_j\Omega_q x_k\uvec{p} \\
 &=& \epsilon_{ijk}\epsilon_{ipq} \Omega_j\Omega_q x_k\uvec{p} \\
 &=& (\delta_{jp}\delta_{kq} - \delta_{jq}\delta_{kp})\Omega_j\Omega_q x_k\uvec{p} \\
 &=& x_k\Omega_k \Omega_j\uvec{j} - \Omega_j\Omega_j x_k\uvec{k} \\
 &=& (\vec{x}\cdot\vec{\Omega})\vec{\Omega} - \Omega^2\vec{x}
\end{eqnarray*}
Thus,
\begin{equation}\label{c3s5e17}
(\vec{\Omega}\vp\vec{x})\cdot\grad(\vec{\Omega}\vp\vec{x}) = \vec{\Omega}\vp(\vec{\Omega}\vp\vec{x})
\end{equation}
Putting \eqref{c3s5e16} and \eqref{c3s5e17} on the right hand side of \eqref{c3s5e15},
\[
\frac{1}{2}\grad(\vec{\Omega}\vp\vec{x})^2 = -\vec{\Omega}\vp(\vec{\Omega}\vp\vec{x})
\]
\end{itemize}

\section{The complete set of governing equations}\label{c3s6}
\begin{itemize}
\item Flow of a Newtonian fluid is described by
\begin{enumerate}
\item Mass conservation, \eqref{c2s2e1a},
\begin{equation}\label{c3s6e1}
\frac{1}{\rho}\md{\rho} + \dive\vec{u} = 0
\end{equation}
\item Momentum balance, \eqref{c3s3e5a}
\begin{equation}\label{c3s6e2}
\rho\md{u_i} = \rho F_i - \pdt{p}{x_i} + \frac{\partial}{\partial x_j}\left[2\mu\left(e_{ij} - \frac{1}{3}\Delta\delta_{ij}\right)\right]
\end{equation}
\item Energy balance, \eqref{c3s4e9a} and \eqref{c3s4e10}
\[
T\md{s} = c_p\md{T} - \frac{\beta T}{\rho}\md{p_e} = \kappa\frac{\Delta^2}{\rho} + \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\]
If we ignore effects of expansion damping, we can drop the first term on the right hand side and replace $p_e$ with $p$ to get
\begin{equation}\label{c3s6e3}
T\md{s} = c_p\md{T} - \frac{\beta T}{\rho}\md{p} = \Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)
\end{equation}
\end{enumerate}
The six quantities $\rho, \vec{u}, p$ and $T$ are the unknowns in these equations. Since these are only five equations, we need one more, namely the equation of state of the form
\begin{equation}\label{c3s6e4}
f(p, \rho, T) = 0
\end{equation}
to be able to solve for all the unknowns. The material parameters $\mu$ and $k$ are given functions of $\rho$ and $T$.
\end{itemize}

\subsection{Isentropic flows}
\begin{itemize}
\item Recall that an isentropic flow is the one for which entropy of a fluid element does not change throughout the course of its motion while a homentropic flow is the one for which
entropy per unit mass $s$ is same throughout the fluid.

\item If we set the molecular transport coefficients, $\mu$ and $k$ to zero, equation \eqref{c3s6e3} gives,
\begin{equation}\label{c3s6e5}
c_p\md{T} - \frac{\beta T}{\rho}\md{p}
\end{equation}
Solving this equation gives $T$ as a function of $p$. Coupled with the equation of state, $f(p, \rho, T) = 0$ gives $\rho$ as a function of $p$. Now
\[
\md{s} = 0
\]
implies that $s$ is a constant. To indicate that $\rho$ is a function of $p$ at constant entropy, we write
\begin{equation}\label{c3s6e6}
\rho = \rho(p, s)
\end{equation}
If the flow were homentropic, we would have written $\rho = \rho(p)$. For an isentropic flow, at a fixed entropy of a fluid element, \eqref{c3s6e6} gives
\begin{eqnarray*}
\pdt{\rho}{t} &=& \pdt{\rho}{p}\pdt{p}{t} \\
\grad{\rho} &=& \pdt{\rho}{p}\grad p
\end{eqnarray*}
therefore,
\begin{equation}\label{c3s6e7}
\md{\rho} = \pdt{\rho}{p}\md{p}
\end{equation}
and hence equation \eqref{c3s6e1} for mass conservation becomes
\begin{equation}\label{c3s6e8}
\frac{1}{\rho c^2}\md{p} + \dive\vec{u} = 0
\end{equation}
where we have used the relation
\begin{equation}\label{c3s6e9}
c^2 = \left(\pdt{p}{\rho}\right)_s
\end{equation}
Putting $\mu = 0$ also simplifies the momentum balance equation \eqref{c3s6e2} to
\begin{equation}\label{c3s6e10}
\rho\md{\vec{u}} = \rho\vec{F} - \grad p
\end{equation}

\item Consider a fluid element at rest. Under the equilibrium condition, the pressure gradient and the body force balance each other so that $\rho_0\vec{F} = \grad p_0$, where the
subscript $0$ indicates equilibrium values. Now let the fluid be slightly perturbed so that the pressure becomes $p = p_0 + p_1$ and as a result the density becomes $\rho = \rho_0 +
\rho_1$. Here, the subscript $1$ indicates the perturbed values. Now,
\[
\frac{1}{\rho} = \frac{1}{\rho_0 + \rho_1} = \frac{1}{\rho_0}\left(\frac{1}{1 + \rho_1/\rho_0}\right) = \frac{1}{\rho_0}\left(1 - \frac{\rho_1}{\rho_0}\right),
\]
up to first order in $\rho_1$. Further,
\[
\md{p} = \md{p_0} + \md{p_1},
\]
so that
\[
\frac{1}{\rho c^2}\md{p} = \frac{1}{\rho_0 c^2}\left(1 - \frac{\rho_1}{\rho_0}\right)\left(\md{p_0} + \md{p_1}\right)
= \frac{1}{\rho_0 c^2}\left(\md{p_0} + \md{p_1} - \frac{\rho_1}{\rho_0}\md{p_0}\right),
\]
up to first order terms in perturbed quantities. Under equilibrium conditions,
\[
\md{p_0} = \pdt{p_0}{t} = 0,
\]
because the equilibrium velocity $\vec{u}_0 = 0$. Therefore,
\[
\frac{1}{\rho c^2}\md{p} = \frac{1}{\rho_0 c^2}\md{p_1} = \frac{1}{\rho_0 c^2}\pdt{p_1}{t}
\]
Similarly, $\dive\vec{u} = \dive\vec{u}_0 + \dive\vec{u}_1 = \dive\vec{u}_1$ so that the equation of mass conservation \eqref{c3s6e8} becomes,
\begin{equation}\label{c3s6e11}
\frac{1}{\rho_0 c^2}\pdt{p_1}{t} + \dive\vec{u}_1 = 0
\end{equation}
Now,
\begin{eqnarray*}
\md{\vec{u}} &=& \md{\vec{u}_0} + \md{\vec{u}_1} \\
 &=& \md{\vec{u}_1} \\
 &=& \pdt{\vec{u_1}}{t} + \vec{u}\cdot\grad{\vec{u_1}} \\
 &=& \pdt{\vec{u}_1}{t},
\end{eqnarray*}
up to first order terms in $\vec{u}_1$. Therefore, the momentum balance equation \eqref{c3s6e10} becomes
\[
\left(\rho_0 + \rho_1\right)\pdt{\vec{u}_1}{t} = (\rho_0 + \rho_1)\vec{F} - \grad p_0 - \grad p_1
\]
or
\begin{equation}\label{c3s6e12}
\rho_0\pdt{\vec{u}_1}{t} = \rho_1\vec{F} - \grad p_1
\end{equation}
where we used the equilibrium condition $\rho_0\vec{F} = \grad p_0$. Differentiating \eqref{c3s6e11} with respect to $t$ and substituting \eqref{c3s6e12} in the result gives
\[
\frac{1}{c^2}\pdt{p_1}{t} = \nabla^2 p_1 - \dive(\rho_1\vec{F})
\]
Now,
\[
\dive(\rho_1\vec{F}) = \rho_1\dive\vec{F} + \vec{F}\cdot\grad\rho_1 = \rho_1\dive\vec{F} + \vec{F}\cdot\left(\pdt{\rho_1}{p_1}\grad p_1\right) =
\rho_1\dive\vec{F} + \frac{\vec{F}\cdot\grad p_1}{c^2}
\]
so that
\begin{equation}\label{c3s6e13}
\frac{1}{c^2}\pdt{p_1}{t} = \nabla^2 p_1 - \rho_1\dive\vec{F} - \frac{\vec{F}\cdot\grad p_1}{c^2}
\end{equation}
If the only external field is gravity, $\vec{F} = \vec{g}$ so that $\dive{\vec{g}} = 0$ and hence,
\[
\frac{1}{c^2}\pdt{p_1}{t} = \nabla^2 p_1 - \frac{\vec{g}\cdot\grad p_1}{c^2}
\]
For air under normal pressure, the velocity of sound $c \approx 330$ $ms^{-1}$ and hence $g/c^2 \approx 9 \times 10^{-5}$. Thus, the second term on the right hand side is negligibly
small and
\begin{equation}\label{c3e6e14}
\frac{1}{c^2}\pdt{p_1}{t} = \nabla^2 p_1
\end{equation}
which is the equation of sound waves in fluids.
\end{itemize}

\section{Incompressible flows}\label{c3s7}
\begin{itemize}
\item Let the velocity $\vec{u}$ be such that it varies appreciably only over distances comparable to $L$. In other words, variation in $\vec{u}$ over distances small compared to $L$ is
negligible. Further, let the variation of $\vec{u}$ in space or time be of a magnitude comparable to $U$. Then, the velocity field is approximately solenoidal if
\[
|\dive\vec{u}| \ll \frac{U}{L}
\]
or if
\begin{equation}\label{c3s7e1}
\Big|\frac{1}{\rho}\md{\rho}\Big| \ll \frac{U}{L}
\end{equation}

\item If we choose $\rho$ and $s$ as independent variables, then we can express $p = p(\rho, s)$. In that case,
\[
dp = \pdt{p}{\rho}d\rho + \pdt{p}{s}ds
\]
and hence
\[
\md{p} = \pdt{p}{\rho}\md{\rho} + \pdt{p}{s}\md{s}
\]
or, writing in the usual thermodynamic convention,
\begin{equation}\label{c3s7e2}
\md{p} = c^2\md{\rho} + \left(\pdt{p}{s}\right)_\rho\md{s}
\end{equation}
Using this equation, we can write
\[
\md{\rho} = \frac{1}{c^2}\md{p} - \frac{1}{c^2}\left(\pdt{p}{s}\right)_\rho\md{s}
\]
and hence the condition for incompressibility \eqref{c3e6e14} can be written as
\begin{equation}\label{c3s7e3}
\Big|\frac{1}{\rho c^2}\md{p} - \frac{1}{\rho c^2}\left(\pdt{p}{s}\right)_\rho\md{s}\Big| \ll \frac{U}{L}
\end{equation}
This relation is valid if each of the two terms on the left hand side have a magnitude small when compared with $U/L$. We will examine the two terms separately.

\item For the moment, assume that the flow is isentropic, in which case, the equation of motion \eqref{c3s6e10} can be written as
\[
\grad p = \rho\vec{F} - \rho\md{\vec{u}}
\]
so that
\[
\vec{u}\cdot\grad p = \rho\vec{u}\cdot\vec{F} - \rho\vec{u}\cdot\md{\vec{u}} = \rho\vec{u}\cdot\vec{F} - \rho\frac{D}{Dt}\left(\frac{q^2}{2}\right)
\]
and
\[
\md{p} = \pdt{p}{t} + \vec{u}\cdot\grad p = \pdt{p}{t} + \rho\vec{u}\cdot\vec{F} - \rho\frac{D}{Dt}\left(\frac{q^2}{2}\right)
\]
due to which the first term on the left hand side of \eqref{c3s7e3} becomes
\[
\frac{1}{\rho c^2}\md{p} = \frac{1}{\rho c^2}\pdt{p}{t} + \frac{1}{c^2}\vec{u}\cdot\vec{F} - \frac{1}{c^2}\frac{D}{Dt}\left(\frac{q^2}{2}\right)
\]
and its smallness means
\begin{equation}\label{c3s7e4}
\Big|\frac{1}{\rho c^2}\pdt{p}{t} + \frac{\vec{u}\cdot\vec{F}}{c^2} - \frac{1}{2c^2}\md{q^2} \Big| \ll \frac{U}{L}
\end{equation}
\begin{enumerate}
\item Smallness of
\[
\Big|\frac{1}{2c^2}\md{q^2} \Big|
\]
means that the ratio
\[
\frac{U^2}{c^2} \ll 1 \Rightarrow \frac{U}{c} \ll 1
\]
The dimensionless quantity $U/c$ is called Mach number. If the Mach number of a flow is very small, then other terms on the left hand side of \eqref{c3s7e1} and \eqref{c3s7e2} also
being small, the fluid can be considered effectively incompressible.

\item Dimension of pressure is same as $\rho q^2$. If $U$ is the typical variation in velocity, $L$ is the typical length scale and $n$ the dominant frequency, $Ln$ too has dimensions
of velocity and $ULn$ has dimensions of $q^2$. Thus the pressure change is of the order of $\rho UL n$ and rate of change of pressure is of the order of $\rho UL n^2$. Therefore,
\[
\Big|\frac{1}{\rho c^2}\pdt{p}{t}\Big| \ll \frac{U}{L} \Rightarrow \Big|\frac{1}{\rho c^2}\rho UL n^2 \Big| \ll \frac{U}{L}
\]
which is same as
\begin{equation}\label{c3s7e5}
\frac{L^2n^2}{c^2} \ll 1
\end{equation}
This condition will be obviously violated if $L$ is of the order of the wavelength of a sound wave and $n$ is of the order of its frequency. Thus, compressibility cannot be ignored if
the flow is caused due to passage of sound.

\item Smallness of
\[
\Big|\frac{\vec{u}\cdot\vec{F}}{c^2}\Big| \ll \frac{U}{L}
\]
in the case of gravitational fields means
\[
\frac{gL}{c^2} \ll 1
\]
Now for air, $c^2 = \gamma p/\rho$ (refer to \href{https://en.wikipedia.org/wiki/Speed_of_sound#Speed_of_sound_in_ideal_gases_and_air}{Wikipedia}) so that the smallness of the second
term on the left hand side of \eqref{c3s7e2} is equivalent to
\[
\frac{\rho g L}{\gamma p} \ll 1,
\]
where $\gamma$ is the adiabatic ratio. For air,
\[
\frac{p}{\rho g} \approx 8 \text{ km }
\]
(refer to p. 20 of the book) so that the relation
\[
\frac{\rho g L}{\gamma p} \ll 1,
\]
is valid for if $L$ is much lesser than $\gamma \time 8 = 11.2$ kilometer, which means always at the scale of the lab.
\end{enumerate}

\item We will now consider the term
\[
T_2 = \Big|\frac{1}{\rho c^2}\left(\pdt{p}{s}\right)_\rho\md{s}\Big|
\]
From (3.6.19),
\[
\frac{1}{\rho c^2}\left(\pdt{p}{s}\right)_\rho = \frac{\beta T}{c_p}
\]
so that
\[
T_2 = \Big|\frac{\beta T}{c_p}\md{s}\Big| = \Big|\frac{\beta}{c_p}T\md{s}\Big|
\]
From \eqref{c3s6e3},
\[
T_2 = \Big|\frac{\beta}{c_p}\left[\Phi + \frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right)\right]\Big|
\]
Now,
\[
\frac{\beta}{c_p}\Phi = \frac{\beta}{c_p}\frac{\mu}{\rho}\frac{U^2}{L^2}
\]
and
\[
\frac{\beta}{c_p}\frac{1}{\rho}\frac{\partial}{\partial x_i}\left(k\pdt{T}{x_i}\right) = \beta\frac{k}{\rho c_p}\frac{\theta}{L^2}
\]
Thus $T_2 \ll U/L$ is possible is
\begin{eqnarray}
\frac{\beta U}{c_p}\frac{\mu}{\rho L} &\ll& 1 \label{c3s7e7} \\
\frac{\beta\theta\kappa}{LU} &\ll& 1 \label{c3s7e8},
\end{eqnarray}
where
\[
\kappa = \frac{k}{\rho c_p}
\]
is the thermometric conductivity.
\end{itemize}


\input{../../UCLHeader.tex}
\input{../../UCLCommands.tex}
\begin{document}
\title{Error Correction Lecture 2 }
\author{with Dan Browne}
\maketitle
\tableofcontents

\section{Correcting Errors of the Shor Code}
Recall that the concatenated Shor code has the following initialisation circuit.

\begin{figure}[!ht]
  \caption{Shor code initialisation diagram.}
  \centering
    \includegraphics[width=\textwidth]{Shor_Code_Initialisation.jpg}
\end{figure}

For an error represented by the operator
\beq
X_1 = XII III III
\eeq
we get
\beq
X_1 \ket{0}_L = \left( \ket{100} + \ket{011} \right) \left( \ket{000} + \ket{111} \right)^{\otimes 2}
\eeq
\beq
X_1 \ket{1}_L = \left( \ket{100} - \ket{011} \right) \left( \ket{000} - \ket{111} \right)^{\otimes 2}
\eeq
This error is detected by the $Z_1$ and $Z_2$ parity measurements. In fact, we can write down the following table for error corrections,
\begin{tabular}{ccc}
$Z_1Z_2$ & $Z_4Z_5$ & $Z_7Z_8$ \\
$Z_2 Z_3$ & $Z_5 Z_6 $ & $Z_8 Z_9$ \\
$(Z_3Z_1)$ & $(Z_4Z_6)$ & $(Z_7 Z_9)$
\end{tabular}
We have written the last row in brackets because just as before, it can be constructed from the other two rows and is thus superfluous. Note however that we cannot correct for two bitflip errors. The correction would introduce a factor of $-1$ on the $\ket{1}_L$ state.

However, we know that the operator $X_1 X_2 X_3 = \bar{Z}$. This error is undetectable. As a consequence, we cannot detect any errors that happen on adjacent bits.

In general, we want
\beq
\mbox{error} \cdot \mbox{error correction} = \identity
\eeq

\section{Phase errors}
Let us introduce the error $Z_1$. It has the following effect:
\beq
Z_1 \ket{0}_L = \left( \ket{000} - \ket{111} \right) \left( \ket{000} + \ket{111} \right)^{\otimes 2}
\eeq
\beq
Z_1 \ket{1}_L = \left( \ket{000} + \ket{111} \right) \left( \ket{000} - \ket{111} \right)^{\otimes 2}
\eeq
We can detect this error with the operator $XXXIIIIII$. It has the following effect:
\beq
XXX \left( \ket{000} \pm \ket{111} \right) = \pm \left( \ket{000} \pm \ket{111} \right)
\eeq
which enables us to see where the error has introduced a sign error. We get a negative eigenvalue for the error $XXX = \bar{Z}$. But there is a problem. Since this error is a logical operator, its detection would allow us to distinguish between $\ket{0}_L$ and $\ket{1}_L$. We can't allow this, since it would collapse any superposition. So instead, we measure $X_1 X_2 X_3 X_4 X_5 X_6$. That is, measure two out of three registers. This will check the parity between the first three and the middle three states. So we get the error detection measurements
\begin{tabular}{c}
$X_1 X_2 X_3 X_4 X_5 X_6$ \\
$X_1 X_2 X_3 X_7X_8 X_9$ \\
$(X_4 X_5 X_6 X_7 X_8 X_9)$
\end{tabular}
This can detect a single $Z$ error on every single qubit. We find the following syndrome table, using the three measurements above
\begin{tabular} {ccc}
$Z_1$ & $Z_2$ $Z_3$ \\ \hline
-- & -- & -- \\
-- & -- & -- \\
+ & + & +
\end{tabular}
But notice that the syndrome looks the same for all three measurements! This means that we cannot distinguish between them. Say we detect $Z_2$, but we then implement the correction $Z_1$. Notice, however that the two commute. So we have
\beq
Z_2 \cdot Z_1 \ket{0}_L = \ket{0}_L
\eeq
\beq
Z_2\cdot Z_1 \ket{1}_L = \ket{1}_L
\eeq
So it works! This is the same as for the error detection operators for single bitflip errors.

This phenomenon is called \textbf{degeneracy}. We have both non-degenerate and degenerate code.

A non-degenerate code means that every possible correctable error has a unique syndrome. This is the case for all classical codes.

It is, understandably, easier to prove things for non-degenerate codes. Thus, some quantum questions are still completely open systems.

\section{Slight Generalisation}
We shall here look at the properties of Pauli operators and their tensor products. We shall use them extensively throughout the course,
and most likely all of their properties.

The Pauli operators are $I, X, Y, Z$. We include the identity $I$ since some of the properties include it.

The Pauli operators have the following operators.
\begin{itemize}
\item \textbf{All unitary} we find that for every Pauli operator,
\beq
U^{-1} = U^\dagger = U
\eeq
\item \textbf{All Hermitian} we find
\beq
U^\dagger = U
\eeq
\item  \textbf{All are self-inverse} that is,
\beq
U = U^{-1}
\eeq
\item \textbf{Recursive property} we can write
\beq
Y = i XZ
\eeq
Then,
\beq
Y \ket{\psi} = i XZ \ket{\psi}
\eeq
so that a $Y$-error can be written in terms of a bitflip and phase error. The $i$ is a global phase which we can ignore.
\item \textbf{Commute or anti-commute} All Pauli matrices anti commute. That is
\beq
\{U_i, U_j\} = 0 \mbox{ if } i \neq j
\eeq
\item \textbf{All are traceless} except for $I$.
\item \textbf{Tensor products of Pauli matrices commute} we find that
\beq
[X\otimes X, Z \otimes Z] = 0
\eeq

\end{itemize}
The last property means that we can always correct for a combination of errors. It means that the following statements are equivalent:
\beq
X_E Z_E X_C Z_C
\eeq
\beq
X_E X_C Z_E Z_C
\eeq
where $E$ stands for error and $C$ stands for correction. That is, it doesn't matter in which order we detect and correct the errors since all the operators anti-commute, which just introduces a global phase.

\section{Arbitrary errors}
Claim: We can correct for arbitrary, unitary errors. Any operator which is a tensor product of Paulis can be written $O_XO_Z$ where $O_X$ contains $X$ and $I$, and $O_Z$ contains $Z$ and $I$.
For example, we can derive all possible $Y$ errors,
\beq
YZIX = i(XIIX)(ZZII)
\eeq
In fact, the Pauli operators form a basis for $2^N \times 2^N$ matrices that live in $\mathbb{C}^{2^N\times 2^N}$. Any $2\times2$ matrix can be written as
\beq
O = a I + bX + cY + dZ
\eeq
To check whether they actually form a basis, we can make use the Hilbert-Schmidt inner product.
\beq
\braket{A,B} = \trace{AB}
\eeq
which can be thought of as an orthogonality condition but for matrices. If you find a set of $D$ operators with zero Hilbert-Schmidt product where $D$ is also the dimension of the space where they live, you have a basis.

\section{Measuring Pauli's}
We can device a circuit for measuring the Pauli operators. For a number of $n$ qubits, we can imagine a projector
\beq
P = P_1 \otimes P_2 \otimes P_3 \otimes \ldots \otimes P_n
\eeq
This circuit would simply look like projectors acting on the individual qubits.

We know that all eigenvalues of $X,Y,Z$ are $\pm 1$. If we have
\beq
P \ket{\psi} = \ket{\psi}
\eeq
we measure a $+1$, and if we have
\beq
P\ket{\psi} = - \ket{\psi}
\eeq
we measure $-1$.

\end{document}

Error Correction Lecture 24 Feb 2016.tex


\subsection{Treatment data}

\subsubsection{Recap}

With multilevel data with fixed coefficients we have:

\(y_{ij}=\mathbf x_{ij}\theta +m_j + \epsilon_{ij}\)

We can estimate \(m_j\) using fixed effects or similar methods.

\subsubsection{Treatment data}

If the data is grouped by whether an entity was treated then will have:

\begin{itemize}
\item \(y_{i0}\) - the outcome if the entity was not treated
\item \(y_{i1}\) - the outcome if the entity was treated
\end{itemize}

However we only observe \(y_i\) and \(D_i\).

\(y_i=y_{i0}+D_i(y_{i1}-y_{10})\)


\section{Appendix}
In the body of the text, we explain in detail how the scientific understanding of the geological processes that generated the data led to the development of the statistical model. Here in the appendix, we define the statistical models directly without the scientific motivation for clarity of model definition.

\subsection{Top-down mixing model}

In the top-down mixing model, observations are made on both the child sediments and parent sediments. The $n_y$ observed age measurements for the child sediment are given by the vector $\mathbf{y} = (y_1, \ldots, y_{n_y})'$ and are reported with an observed analytic measurement standard deviation $\boldsymbol{\sigma}_y = (\sigma_{y1}, \ldots, \sigma_{yn_y})'$ associated with each observation. Assuming a normal distribution for the measurement process, the latent true ages are defined as $\tilde{\mathbf{y}} = (\tilde{y}_1, \ldots, \tilde{y}_{n_y})'$ and are modeled by
\begin{align*}
\mathbf{y} | \tilde{\mathbf{y}}, \boldsymbol{\sigma}_y^2 & \sim \operatorname{N} (\mathbf{y} | \tilde{\mathbf{y}}, \operatorname{diag} ( \boldsymbol{\sigma}_y^2 ) ),
\end{align*}
where $N(\mathbf{x} | \boldsymbol{\mu}, \boldsymbol{\Sigma})$ is a multivariate normal distribution with data vector $\mathbf{x}$, mean vector $\boldsymbol{\mu}$, and covariance matrix $\boldsymbol{\Sigma}$. The notation $\operatorname{diag} (\boldsymbol{\sigma^2})$ represents a diagonal covariance matrix with $i,i$th element $\sigma^2_i$ and off diagonal elements all equal to 0.

Likewise, the $b = 1, \ldots, B$ parent observations are each comprised of $n_b$ observations and are given by the vector $\mathbf{z}_b = (z_{b1}, \ldots z_{bn_b})'$  and are reported with an observed analytic measurement standard deviation $\boldsymbol{\sigma}_b = (\sigma_{b1}, \ldots, \sigma_{bn_b})'$ associated with each observation. Assuming a normal distribution for the measurement process, the latent true ages are defined as $\tilde{\mathbf{z}}_b = (\tilde{z}_{b1}, \ldots, \tilde{z}_{bn_b})'$ and are modeled by
\begin{align*}
\mathbf{z}_b | \tilde{\mathbf{z}}_b, \boldsymbol{\sigma}_{b}^2 & \sim \operatorname{N} (\mathbf{z}_b | \tilde{\mathbf{z}}_b, \operatorname{diag} ( \boldsymbol{\sigma}_{b}^2 ) )
\end{align*}

The latent parent age distributions for the $b = 1, \ldots, B$ parents are modeled using a finite mixture of $K$ Gaussian distributions
\begin{align*}
\tilde{\mathbf{z}}_b | \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \boldsymbol{p}_{b} & \sim \prod_{i=1}^{n_b} \sum_{k=1}^K p_{bk} \operatorname{N} \left( \tilde{z}_{ib} \middle| \mu_{k}, \sigma^2_{k} \right)
\end{align*}
where $\boldsymbol{\mu} = (\mu_1, \ldots, \mu_K)'$ and $\boldsymbol{\sigma}^2 = (\sigma^2_1, \ldots, \sigma^2_K)'$ are the mean and variance of the mixture distributions (which are shared across each of the parents) and $\mathbf{p}_{b} = (p_{b1}, \ldots, p_{bK})'$ are mixture weights where for $k = 1, \ldots, K,$ $p_{bk} > 0$ and $\sum_{k=1}^K p_{bk} = 1$. For $k = 1, \ldots, K$, the mixture distribution means are assigned independent, vague priors $N(\mu_\mu = 150 \mbox{ Myr}, \sigma^2_\mu = 150^2 \mbox{ Myr}^2)$ where Myr represents a million years. To ensure the mixing distributions are relatively concentrated with respect to geologic time, the mixing kernel standard deviations are assigned independent truncated half-Cauchy priors $\sigma_k \sim \operatorname{Cauchy}^+(0, 25 \mbox{ Myr})I\{0 < \sigma_k < 50 \mbox{ Myr}\}$, which enforces the mixing distribution scales (which represent geologic mineral formation events) to be small relative to the range of dates from about 0 to 300 Myr.

For each parent $b = 1, \ldots, B$, the mixing probabilities $\mathbf{p}_b$ are modeled by introducing $k = 1, \ldots, K-1$ independent and identically distributed random variables $\tilde{p}_{bk} \sim Beta(1, \alpha_b)$ random variables and transforming the $\tilde{p}_{bk}$s by
\begin{align*}
p_{b k} & = \begin{cases}
\tilde{p}_{b1} & \mbox{for } k = 1,\\
 \tilde{p}_{bk} \prod_{s=1}^{k-1} (1 - \tilde{p}_{bs}) & \mbox{for } k=2, \ldots, K-1, \\
\prod_{s=1}^{k-1} (1 - p_{bs}) & \mbox{for } k = K.
\end{cases}
\end{align*}
which induces a finite approximation to the stick-breaking representation of a Dirichlet process so long as $K$ is chosen large enough (Section 3 \citet{ishwaran2001gibbs} and \citet{ishwaran2002exact}). For $b = 1, \ldots, B$, $\alpha_b$ is assigned a $gamma(1, 1)$ prior and the vector $\boldsymbol{\alpha} = (\alpha_1, \ldots, \alpha_B)'$.

Combining the parent distributions, the unobserved, latent ages are modeled using the finite mixture of mixtures

\begin{align*}
\tilde{\mathbf{y}} | \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \{\boldsymbol{p}_b \}_{b=1}^B, \boldsymbol{\phi} & \sim \prod_{i=1}^{n_y} \sum_{b=1}^B \phi_b \sum_{k=1}^K p_{bk} \operatorname{N} \left( \tilde{y}_i \middle| \mu_{b}, \sigma_{b}^2 \right),
\end{align*}
where the notation $\{\mathbf{p}_b\}_{b=1}^{B}$ denotes the set of parameters $\{ \mathbf{p}_1, \ldots, \mathbf{p}_B\}$. The parameter $\boldsymbol{\phi} = (\phi_1, \ldots, \phi_B)'$ models the proportion of the child sediment $\phi_b$ that comes from parent $b$ where $\phi_b > 0$ and $\sum_{b=1}^B \phi_b = 1$. The mixing proportion $\boldsymbol{\phi}$ is a assigned a $Dirichlet(\alpha_\phi \mathbf{1})$ prior where $\mathbf{1}$ is a vector of ones of length $B$ and $\alpha_\phi$ is assigned a $gamma(1, 1)$ prior.

All combined, the top-down mixing model posterior is

\begin{align*}
& \left[ \mathbf{\tilde{y}}, \{ \mathbf{\tilde{z}}_b \}_{b=1}^B, \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \{ \boldsymbol{p}_b \}_{b=1}^B, \boldsymbol{\phi}, \alpha_\phi, \boldsymbol{\alpha} \middle| \mathbf{y}, \boldsymbol{\sigma}_y^2, \{ \mathbf{z}_b\}_{b=1}^B, \{\boldsymbol{\sigma}^2_b \}_{b=1}^{B} \right] \propto \\
& \hspace{3cm} \left[\mathbf{y} \middle| \mathbf{\tilde{y}}, \boldsymbol{\sigma}_y^2 \right]
\prod_{b=1}^B \left[\mathbf{z}_b \middle| \mathbf{\tilde{z}}_b, \boldsymbol{\sigma}_b^2 \right] \times \\
& \hspace{3cm}
\left[ \mathbf{\tilde{y}} \middle| \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \{ \boldsymbol{p}_b \}_{b=1}^B, \boldsymbol{\phi} \right]
\prod_{b=1}^B
\left[ \mathbf{\tilde{z}}_b \middle| \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \boldsymbol{p}_b \right] \times\\
& \hspace{3cm}
\left[ \boldsymbol{\mu} \right]
\left[ \boldsymbol{\sigma}^2 \right]
\left[ \boldsymbol{\phi} | \alpha_{\phi} \right]
\left[ \alpha_{\phi} \right]
\left( \prod_{b=1}^B \left[ \boldsymbol{p}_b \middle| \alpha_b \right]
\left[ \alpha_b \right]
 \right),
\end{align*}
where each line on the right-hand side of the proportional symbol is the data, process, and prior model, respectively.

\subsection{Bottom-up unmixing model}

In the bottom-up unmixing model, observations are made on $d = 1, \ldots, D$ child sediments whereas the parent sediments are unobserved. For each of the $d = 1, \ldots, D$ children, the $n_d$ observed age measurements are given by the vector $\mathbf{y}_d = (y_{d1}, \ldots, y_{dn_d})'$ and are reported with an observed analytic measurement standard deviation $\boldsymbol{\sigma}_d = (\sigma_{d1}, \ldots, \sigma_{dn_d})'$ associated with each observation. Assuming a normal distribution for the measurement process, the latent true ages are defined as $\tilde{\mathbf{y}}_d = (\tilde{y}_{d1}, \ldots, \tilde{y}_{dn_d})'$ and are modeled by
\begin{align*}
\mathbf{y}_d | \tilde{\mathbf{y}}_d, \boldsymbol{\sigma}_d^2 & \sim \operatorname{N} (\mathbf{y}_d | \tilde{\mathbf{y}}_d, \operatorname{diag} ( \boldsymbol{\sigma}_d^2 ) ),
\end{align*}

As none of the parent ages are observed, the latent parent age distributions for the $b = 1, \ldots, B$ parents are represented as a finite mixture of $K$ Gaussian distributions
\begin{align*}
\sum_{k=1}^K p_{bk} \operatorname{N} \left( \mu_{k}, \sigma^2_{k} \right)
\end{align*}
where $\boldsymbol{\mu} = (\mu_1, \ldots, \mu_K)'$ and $\boldsymbol{\sigma}^2 = (\sigma^2_1, \ldots, \sigma^2_K)'$ are the mean and variance of the mixture distributions (which are shared across each of the parents) and $\mathbf{p}_{b} = (p_{b1}, \ldots, p_{bK})'$ are mixture weights where for $k = 1, \ldots, K,$ $p_{bk} > 0$ and $\sum_{k=1}^K p_{bk} = 1$. For $k = 1, \ldots, K$, the mixture distribution means are assigned independent, vague priors $N(\mu_\mu = 150 \mbox{ Myr}, \sigma^2_\mu = 150^2 \mbox{ Myr}^2)$ where Myr represents a million years. To ensure the mixing distributions are relatively concentrated with respect to geologic time, the mixing kernel standard deviations are assigned independent truncated half-Cauchy priors $\sigma_k \sim \operatorname{Cauchy}^+(0, 25 \mbox{ Myr})I\{0 < \sigma_k < 50 \mbox{ Myr}\}$, which enforces the mixing distribution scales (which represent geologic mineral formation events) to be small relative to the range of dates from about 0 to 300 Myr.

Because none of the parent ages are observed, the parent distributions are estimated entirely using child sediment observations. Assuming a fixed and known number of parents $B$, the bottom-up process model for the $d$th child is

\begin{align*}
\tilde{\mathbf{y}}_{d} | \boldsymbol{\mu}, \boldsymbol{\sigma^2}, \{ \boldsymbol{p}_b \}_{b=1}^B, \boldsymbol{\phi}_d & \sim \prod_{i1=}^{n_d} \sum_{b=1}^B \phi_{db} \sum_{k=1}^K p_{bk} \operatorname{N}(\tilde{y}_{id} | \mu_{k}, \sigma^2_{k}),
\end{align*}
where the $B$-dimensional vector of mixture proportions $\boldsymbol{\phi}_d = \left( \phi_{d1}, \ldots, \phi_{dB} \right)'$ models the proportion of the $d$th child sediment that can be attributed to each of the $B$ parents where for $b = 1, \ldots, B$, $\phi_{db}>0$ and $\sum_{b=1}^B \phi_{db} = 1$.
For each of the $d = 1, \ldots, D$ children, the mixing proportions $\boldsymbol{\phi}_d$ are a assigned independent $Dirichlet(\alpha_{d} \mathbf{1})$ prior where $\mathbf{1}$ is a vector of ones of length $B$ and each $\alpha_{d}$ is assigned a $gamma(1, 1)$ prior. The priors for $\boldsymbol{\mu}$, $\boldsymbol{\sigma^2}$, and $\{\mathbf{p}_{b}\}_{b=1}^B$ (and their respective hyperparameters $\boldsymbol{\alpha} = (\alpha_1, \ldots, \alpha_B)'$) are the same as in the top-down mixing model.

Thus, the bottom-up unmixing model posterior distribution is

\begin{align*}
& \left[ \{ \tilde{\mathbf{y}}_d \}_{d=1}^D, \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \{ \boldsymbol{p}_b \}_{b=1}^B, \{ \boldsymbol{\phi}_d \}_{d=1}^D, \boldsymbol{\alpha}_{\phi}, \boldsymbol{\alpha} \middle| \{ \mathbf{y}_d\}_{d=1}^D, \{\boldsymbol{\sigma}^2_{d} \}_{d=1}^D \right] \propto \\
& \hspace{3cm} \prod_{d=1}^D \left[\mathbf{y}_d \middle| \mathbf{\tilde{y}}_d, \boldsymbol{\sigma}_{d}^2 \right] \times \\
& \hspace{3cm}
\prod_{d=1}^D \left[ \tilde{\mathbf{y}}_d \middle| \boldsymbol{\mu}, \boldsymbol{\sigma}^2, \boldsymbol{\phi}_d, \{ \boldsymbol{p}_b \}_{b=1}^B \right] \times \\
& \hspace{3cm}
\left[ \boldsymbol{\mu}\right]
\left[ \boldsymbol{\sigma}^2 \right]
\left( \prod_{b=1}^B \left[ \boldsymbol{p}_b \middle| \alpha_b \right]
\left[ \alpha_b \right] \right)
\left( \prod_{d=1}^D \left[ \boldsymbol{\phi}_d | \alpha_{d} \right]
\left[ \alpha_{d} \right] \right),
\end{align*}
where each line on the right-hand side of the proportional symbol is the data, process, and prior model, respectively.