\section{Example Proof for Monoid Laws}
\label{sec:example}
In this section we use equational reasoning (see section \ref{sec:equationalreasoning}) to prove the left identity law of the monoid type class (see section \ref{sec:monoid}) for a new type.
The following example is based on a blog post of Gabriel Gonzales \cite{gonzales14}. We simplified the types of a plugin and extended the example with an additional plugin implementation.

Suppose we want to build a plugin system. A plugin in our example is an \verb|IO| action that takes a \verb|Char| value and does some work with it (e.g. log to a file, potentially with side effects). Hence, a plugin is of type \verb|Char -> IO ()|. Listing \ref{lst:logtoplugin} shows the definition of the plugin \verb|logto|. It writes the first argument of type \verb|Char| to a file named \verb|log.txt|.

\begin{lstlisting}[caption={Definition of a plugin that writes a character to a file.}, label={lst:logtoplugin}]
import System.IO
logto :: Char -> IO ()
logto c  = do
  handle <- openFile "log.txt" WriteMode
  hPutChar handle c
\end{lstlisting}

Listing \ref{lst:applyingaSinglePlugin} shows an application of the \verb|logto| plugin. We read a character \verb|c| from the standard input and call \verb|logto c|.

\begin{lstlisting}[caption={Calling the {\ttfamily logto} IO action in main}, label={lst:applyingaSinglePlugin}]
main = do
    c <- getChar
    logto c
\end{lstlisting}

Listing \ref{lst:print2stdout} shows an additional plugin that prints the character to the standard output.

\begin{lstlisting}[caption={Definition of the plugin {\ttfamily print2stdout}}, label={lst:print2stdout}]
print2stdout :: Char -> IO ()
print2stdout c = putChar c
\end{lstlisting}
In order to extend the program from listing \ref{lst:applyingaSinglePlugin}, we want to be able to compose several plugins.
Listing \ref{lst:composition} gives an example of the composition of the plugins \verb|logto| and \verb|printstdout|. We apply the \verb|mappend| function from the \verb|Monoid| type class to \verb|logto| and \verb|print2stdout|. Both arguments are of type \verb|Char -> IO ()|. The return value is also of type \verb|Char -> IO ()|. Listing \ref{lst:composition} only works if \verb|mappend| is implemented for the type \verb|Char -> IO ()|. We will give an implementation later in this section.

\begin{lstlisting}[caption={Composition of two plugins}, label={lst:composition}]
composedPlugin :: Char -> IO ()
composedPlugin  = logto `mappend` print2stdout
\end{lstlisting}

In addition, we demand that the order in which we evaluate the plugins must not matter. The plugins have to work independently of each other. For example, the behavior of the plugins \verb|composed1| and \verb|composed2| from listing \ref{lst:evaluateorder} should be the same (\verb|donothing| is another plugin of type \verb|Char -> IO ()|).

\begin{lstlisting}[caption={The order in which we evaluate the plugins does not matter}, label={lst:evaluateorder}]
composed1 = logto `mappend` (print2stdout `mappend` donothing)
composed2 = (logto `mappend` print2stdout) `mappend` donothing
\end{lstlisting}

The monoid laws state that \verb|mappend| must be associative. Hence, if we can prove that our implementation of \verb|mappend| satisfies the monoid laws, we can combine them, using the monoid function \verb|mappend| and we are able to add plugins without concerning about the order of evaluation. In addition, they are easier to use because they will behave as expected.

The plugins are of type \verb|Char -> IO ()|. Instead of writing a specialized instance for \verb|Char -> IO ()|, we use the general implementation of section \ref{sec:monoid}.
The instance implementation is repeated for convenience:

\begin{lstlisting}[caption={Monoid instance},label={lst:monoidinstance2}]
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}

instance (Applicative f, Monoid a) => Monoid (f a) where
  mempty = pure mempty
  mappend = liftA2 mappend
\end{lstlisting}
The generalization has the advantage of not having that to prove the type class law only once for all types that match the general type declaration. The process of verifying a program is cumbersome and time consuming. Hence, the generalization of proofs is desirable.

We will verify that the definition \ref{lst:monoidinstance2} satisfies the left identity law of the \verb|Monoid| type class in two steps. First, we describe, why the instance implementation of listing \ref{lst:monoidinstance2} makes the type \verb|Char -> IO ()| part of the \verb|Monoid| type class. Then we prove that the left identity law from section \ref{sec:monoid} holds for the \verb|Monoid| instance implementation of listing \ref{lst:monoidinstance2}. We will assume that the instance implementation of listing \ref{lst:monoidinstance2} is a \verb|Monoid| in the first step.

\subsection{Generalization of the plugin type }
\label{sec:generalization}

In order to use the instance implementation of listing \ref{lst:monoidinstance2}, the type \verb|(->) Char| (that is the type of a Haskell function) has to be part of the type class \verb|Applicative| and the type \verb|IO ()| has to be part of the type class \verb|Monoid|. \verb|IO ()| is a \verb|Monoid| if the type \verb|IO| is an \verb|Applicative| and \verb|()| is a \verb|Monoid|.

Here is an overview of all the steps of the argumentation:
\begin{enumerate}
\item Show that \verb|Char -> IO ()| is a \verb|Monoid|.
\item Show that \verb|(->) Char| is an \verb|Applicative| and \verb|IO ()| is a \verb|Monoid|.
\item Show that \verb|IO| is an \verb|Applicative| and \verb|()| is a \verb|Monoid|.
\end{enumerate}

We show the required properties in reversed order.

\begin{etaremune}
\item The standard library provides a \verb|Monoid| instance for \verb|()| \cite{monoid} and an \verb|Applicative| instance for \verb|IO| \cite{control.applicative}. All type class instances of the standard library obey the corresponding type class laws \cite{Lipovaca}.

\item If the implementation from listing \ref{lst:monoidinstance2} satisfies the monoid laws then \verb|IO ()| is a \verb|Monoid| because \verb|IO| is part of the \verb|Applicative| type class \cite{control.applicative} and \verb|()| is a \verb|Monoid|. The type \verb|(->) r| (that's the type of Haskell functions) is a part of the \verb|Applicative| type class \cite{control.applicative}.

\item
If the implementation from listing \ref{lst:monoidinstance2} satisfies the monoid laws then \verb|Char -> IO ()| is a \verb|Monoid| because \verb|(->) Char| is an \verb|Applicative| and \verb|IO ()| is a \verb|Monoid|. The compiler will use the instance implementation for the \verb|Monoid| type class from listing \ref{lst:monoidinstance2} for the type \verb|Char -> IO ()| because it matches the type
\begin{verbatim}
(Applicative f, Monoid b) => Monoid (f b)
\end{verbatim}

\end{etaremune}

Notice that we rely heavily on the assumption that listing \ref{lst:monoidinstance2} satisfies the monoid laws. The next section will prove that the implementation is correct.

\subsection{Proof}
\label{sec:exampleproof}

In this section we will show that the implementation in listing \ref{lst:monoidinstance2} satisfies the left identity law of the \verb|Monoid| type class (see section \ref{sec:monoid}).
The left identity law demands that:
\begin{verbatim}
mappend mempty x = x
\end{verbatim}

We use equational reasoning to show that the left-hand side is equal to \verb|x|. First we use the definitions of \verb|mappend| and \verb|mempty| of listing \ref{lst:monoidinstance2} to substitute the left-hand side. Furthermore we look up the definition of \verb|liftA2| in the source code \cite{control.applicative} to evaluate the expression.
Listing \ref{lst:lifta2} shows the definition of \verb|liftA2|.

\begin{lstlisting}[caption={Function definition of {\ttfamily liftA2}},label={lst:lifta2}]
liftA2 f x y = (pure f <*> x) <*> y
\end{lstlisting}

\begin{verbatim}
mappend mempty x                    -- apply def. mappend
= liftA2 mappend mempty x           -- apply def. mempty
= liftA2 mappend (pure mempty) x    -- apply def. of liftA2
= (pure mappend <*> pure mempty) <*> x
\end{verbatim}

To resolve this expression further, we use the laws of the \verb|Applicative| type class described in the appendix \ref{sec:applicatives}.
One law of the \verb|Applicative| type class states:
\begin{verbatim}
pure f <*> pure x = pure (f x)
\end{verbatim}
We can use this property to substitute the left-hand side. Next we write \verb|mappend mempty| as lambda function \verb|\a -> mappend mempty a| and use the monoid law
\begin{verbatim}
mappend mempty x = x
\end{verbatim}
to simplify the expression. In the last step we use the first applicative law
\begin{verbatim}
pure id <*> v = v
\end{verbatim}
 to rewrite the expression as \verb|x|.
\begin{verbatim}
(pure mappend <*> pure mempty) <*> x    -- 3. applicative law
= pure (mappend mempty) <*> x           -- transform to lambda
= pure (\a -> mappend mempty a) <*> x   -- 1. monoid law
= pure (\a -> a) <*> x                  -- a -> a = id
= pure id <*> x                         -- 1. applicative law
= x
\end{verbatim}

That completes the proof.

The example demonstrated several ideas:
\begin{itemize}
\item Type classes allow us to generalize definitions. A proof for the generalization is valid for all specializations.
\item To prove a type class law we can use equational reasoning.
\item Type class laws (or properties) allow to prove further properties.
\end{itemize}


\section{Conclusion}
We are interested in answering a basic question of how to forecast a time series variable of interest when there is uncertainly about parameter instability. Specifically, which model should be used for forecasting: the break model or the stable one? If uncertainty is strong and we decide to combine forecasts from two models, what is the optimal rule in terms of some information criterion about assigning weights? Built upon Hansen's Mallows' model averaging method, we propose using the cross-validation criterion to combine forecasting models. In the literature of model selection, CV is shown to be robust to heteroscedasticity than other information criteria, such as, AIC, BIC and Mallows'. Without assuming conditional heteroscedasticity, we show that CV model averaging is approximately equivalent to Mallows' model averaging. But in many empirical applications related to macroeconomic time series or financial time series, researchers usually can not avoid explicitly dealing with heteroscedasticity for analysis and forecast. This motivates our generalization of the model averaging method by allowing for conditional heteroscedasticity.

Researchers have found that in many applications, equally weighted forecasts perform better than other complex combination methods. This forecast combination puzzle has cast double on the use of complicated model averaging methods. Both CV and Cp weights are easy to compute and do not rely on direct weight estimation as in the Granger-Ramanathan forecast combination. This feature should be appealing to practitioners and professional forecasters because simplicity can help reduce the excess noise introduced by using complex weighting methods. This may help explain why our methods forecast better than equal weight in both controlled simulation and in forecasting U.S. and Taiwan quarterly GDP growth rates out-of-sample.


\section{2D DP}

%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest common subsequence}
  \begin{exampleblock}{LCS: longest common subsequence (Problem 7.5)}
	\[
	  X = X_{1} \cdots X_{m} \quad Y = Y_{1} \cdots Y_{n}
	\]

	\begin{enumerate}[(1)]
      \item Find (the length of) an LCS of $X$ and $Y$
    \end{enumerate}
  \end{exampleblock}

  \begin{align*}
	X &= \langle A,\textcolor{blue}{B},\textcolor{blue}{C},\textcolor{blue}{B},D,\textcolor{blue}{A},B \rangle  \\
	Y &= \langle \textcolor{blue}{B},D,\textcolor{blue}{C},A,\textcolor{blue}{B},\textcolor{blue}{A} \rangle \\
	Z &= \langle B,C,B,A \rangle
  \end{align*}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest common subsequence}
  \begin{description}
	\item[Subproblem:] $L[i,j]$: the length of an LCS of $X[1 \cdots i]$ and $Y[1 \cdots j]$
	\item[Goal:] $L[m,n]$
	  \pause
	\item[Make choice:] Is $X_{i} = Y_{j}$?
	\item[Recurrence:] \textcolor{red}{(Proof!)}
	  \begin{displaymath}
		L[i,j] = \left\{ \begin{array}{ll}
		  L[i-1, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
		  \max \set{L[i-1,j], L[i,j-1]} & \textrm{if $X_{i} \neq Y_{j}$}
		\end{array} \right.
	  \end{displaymath}
	  \pause
	\item[Init:]
	  \begin{align*}
		L[0,j] &= 0, \; 0 \le j \le n \\
		L[i,0] &= 0, \; 0 \le i \le m
	  \end{align*}
	\item[Time:] $\Theta(mn)$
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest common subsequence}
  \begin{exampleblock}{Longest common subsequence (Problem 7.5)}
	\[
	  X = X_{1} \cdots X_{m} \quad Y = Y_{1} \cdots Y_{n}
	\]

	\begin{enumerate}[(1)]
	  \setcounter{enumi}{1}
	  \item Allowing repetition of $X$
	  \item Allowing repetition $\le k$ of $X$
	\end{enumerate}
  \end{exampleblock}

  \pause
  \begin{displaymath}
	L[i,j] = \left\{ \begin{array}{ll}
	  L[\textcolor{red}{i}, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
	  \max \set{L[i-1,j], L[i,j-1]} & \textrm{if $X_{i} \neq Y_{j}$}
	\end{array} \right.
  \end{displaymath}

  \pause
  \[
	X \implies X^{(k)} \triangleq X_{1}^{(k)} \cdots X_{m}^{(k)}
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest common substring}
  \centerline{What about longest common \emph{substring}?}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Shortest common supersequence}
  \begin{exampleblock}{Shortest common supersequence (Problem 7.6)}
	\[
	  X = X_{1} \cdots X_{m} \quad Y = Y_{1} \cdots Y_{n}
	\]

	\begin{itemize}
	  \item Find (the length of) a shortest common subsequence of $X$ and $Y$
	\end{itemize}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Shortest common supersequence}
  \begin{description}
	\item[Subproblem:] $L[i,j]$: the length of an SCS of $X[1 \cdots i]$ and $Y[1 \cdots j]$
	\item[Goal:] $L[m,n]$
	  \pause
	\item[Make choice:] Is $X_{i} = Y_{j}$?
	\item[Recurrence:]
	  \begin{displaymath}
		L[i,j] = \left\{ \begin{array}{ll}
		  L[i-1, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
		  \min \set{L[i-1,j] + 1, L[i,j-1] + 1} & \textrm{if $X_{i} \neq Y_{j}$}
		\end{array} \right.
	  \end{displaymath}
	  \pause
	\item[Init:]
	  \begin{align*}
		L[0,j] &= j, \; 0 \le j \le n \\
		L[i,0] &= i, \; 0 \le i \le m
	  \end{align*}
  \end{description}

  \pause
  \begin{alertblock}{Remark}
	\[
	  \max(m,n) \le L(m,n) \le m+n
	\]
  \end{alertblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Shortest common supersequence}
  \[
	X[1 \dots m] \qquad Y[1 \dots n]
  \]

  \begin{align*}
	r &= |\text{LCS}(X,Y)| \\
	t &= |\text{SCS}(X,Y)|
  \end{align*}

  \pause
  \[
	X \cap Y = \emptyset \implies r = 0 \quad t = m + n
  \]

  \pause
  \[
	X \prec Y \implies r = m \quad t = n
  \]

  \pause
  \[
	r + t = m + n
  \]
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String shuffling}
  \begin{exampleblock}{String shuffling (Problem 7.7)}
	\begin{itemize}
	  \item Strings $X[1 \dots m], Y[1 \dots n], Z[1 \dots k]$
	  \item Is $X \oplus Y = Z$?
	\end{itemize}

	\begin{enumerate}[(1)]
	  \item Reduced to LCS:
		\[
		  (Z' \triangleq Z \setminus \text{LCS}(X, Z)) = Y
		\]
	  \item $O(mn)$
	  \item Minimum \# deleted characters to ensure $X \oplus Y = Z$ in $O(mnk)$
	\end{enumerate}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String shuffling}
  \[
	(Z' \triangleq Z \setminus \text{LCS}(X, Z)) = Y
  \]

  \vspace{0.60cm}
  \begin{columns}
	\column{0.50\textwidth}
	  \[
		\text{``YES''} \implies X \oplus Y = Z
	  \]

	  \uncover<2->{
		\begin{align*}
		  X &= \textcolor{red}{A}C \\
		  Y &= CB \\
		  Z &= CB\textcolor{red}{A}
		\end{align*}
	  }
	\column{0.50\textwidth}
	  \[
		\text{``NO''} \implies X \oplus Y \neq Z
	  \]

	  \uncover<3->{
		\begin{align*}
		  X &= \textcolor{red}{A} \\
		  Y &= AB \\
		  Z &= \textcolor{red}{A}BA
		\end{align*}
	  }
  \end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String shuffling}
  \begin{description}
	\item[Subproblem:] $S[i,j]$: Is $X[1 \dots i] \oplus Y[1 \dots j] = Z[1 \dots r \triangleq i + j]$?
	\item[Goal:] $S[m,n]$
	  \pause
	\item[Make choice:] Is $Z[r] = X[i] \lor Z[r] = Y[j]$?
	\item[Recurrence:]
	  \begin{align*}
		S[i,j] = &(Z[r] = X[i] \land S[i-1,j]) \lor \\
			&(Z[r] = Y[j] \land S[i,j-1])
	  \end{align*}
	  \pause
	\item[Init:]
	  \begin{align*}
		S[0,j] &= (Z = Y), \; \forall 0 \le j \le n  \\
		S[i,0] &= (Z = X), \; \forall 0 \le i \le m
	  \end{align*}
	\item[Time:] $O(mn)$
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String shuffling}
  \begin{description}
	\item[Subproblem:] $D[i,j,r]$: minimum \# deleted characters to ensure $X[1 \dots i] \oplus Y[1 \dots j] = Z[1 \dots r]$
	\item[Goal:] $D[m,n,k]$
	  \pause
	\item[Make choice:] Is $Z[r] = X[i] \lor Z[r] = Y[j]$?
	\item[Recurrence:]
	  \[
		D[i,j,r] = \min \left\{ \begin{array}{ll}
		  D[i-1, j, r-1] & \text{if } Z[r] = X[i] \\
		  D[i, j-1, r-1] & \text{if } Z[r] = Y[j] \\
		  1 + D[i-1, j, r] & \\
		  1 + D[i, j-1, r] & \\
		  1 + D[i, j, r-1] & \\
		\end{array} \right.
	  \]
	  \pause
	\item[Init:]
	  \begin{align*}
		D[0,j,r] &= j + r - 2|\text{LCS}(Y[1 \dots j], Z[1 \dots r])| \\
		D[i,0,r] &=	i + r - 2|\text{LCS}(X[1 \dots i], Z[1 \dots r])| \\
		D[i,j,0] &= i + j
	  \end{align*}
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest contiguous substring both forward and backward}
  \begin{exampleblock}{Longest contiguous substring both forward and backward (Problem 7.8)}
    \begin{itemize}
      \item String $T[1 \cdots n]$
      \item Find a longest contiguous substring (LCS) both forward and backward
    \end{itemize}
  \end{exampleblock}

  \begin{center}
	d\textcolor{blue}{ynam}icprogramming\textcolor{blue}{many}times
  \end{center}

  \begin{itemize}
	\item Subproblem $L[i]$: the length of an LCS in $T[1 \cdots i]$
	\item Subproblem $L[i,j]$: the length of an LCS in $T[i \cdots j]$
  \end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest contiguous substring both forward and backward}
  \begin{description}
	\item[Subproblem:] $L[i,j]$: the length of an LCS \textcolor{red}{starting with $T_{i}$ and ending with $T_{j}$}
	\item[Goal:] $\max\limits_{1 \le i \le j \le n} L[i,j]$
	  \pause
	\item[Make choice:] Is $T_{i} = T_{j}$?
	\item[Recurrence:]
	  \begin{displaymath}
		L[i,j] = \left\{ \begin{array}{ll}
		  0 & \textrm{if $T_{i} \neq T_{j}$}  \\
		  L[i+1,j-1] + 1 & \textrm{if $T_{i} = T_{j}$}
		\end{array} \right.
	  \end{displaymath}
	  \pause
	\item[Init:]
	  \begin{align*}
		L[i,i] &= 0, \; 0 \le i \le n  \\
		L[i,i+1] &= \left\{ \begin{array}{ll}
		  1 & \text{if } T_{i} = T_{i+1}  \\
		  0 & \text{if } T_{i} \neq T_{i+1}
		  \end{array} \right.
	  \end{align*}
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Longest contiguous substring both forward and backward}
  Code: three ways of filling the table
  \fignocaption{width = 0.50\textwidth}{figs/three-ways-filling-table.png}

  \begin{algorithmic}
	\ForAll{$d \gets 2 \dots n-1$}
	  \ForAll{$i \gets 1 \dots n-d$}
	    \State $j \gets i + d$
		\State $\dots$
	  \EndFor
    \EndFor

	\State \Return $\max\limits_{1 \le i \le j \le n} L[i,j]$
  \end{algorithmic}

  % \begin{center}
  %     \begin{verbatim}
  %       for d = 2 to n-1
  %         for i = 1 to n-d
  %           j = i + d
  %           ...
  %       return max_{1 <= i <= j <= n} L[i,j]
  %    \end{verbatim}
  % \end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Longest palindrome subsequence}
  \begin{exampleblock}{Longest palindrome subsequence (Problem 7.10)}
	\begin{enumerate}[(1)]
      \item Find (the length of) a longest palindrome subsequence of $S[1 \cdots n]$
    \end{enumerate}
  \end{exampleblock}

  \begin{description}
	\item[Subproblem:] $L[i,j]$: the length of an LSP of $S[i \cdots j]$
	\item[Goal:] $L[1,n]$
	  \pause
	\item[Make choice:] Is $S[i] = S[j]$?
	\item[Recurrence:]
	  \begin{displaymath}
		L[i,j] = \left\{ \begin{array}{ll}
		  L[i+1, j-1] + 2 & \text{if } S[i] = S[j]  \\
		  \max\set{L[i+1,j], L[i,j-1]} & \text{if } S[i] \neq S[j]
		\end{array} \right.
	  \end{displaymath}
	  \pause
	\item[Init:]
	  \begin{align*}
		L[i,i] &= 1, \; \forall 1 \le i \le n  \\
		\textcolor{red}{L[i,i+1]} &= 2, \; \text{if } S[i] = S[i+1], \; \forall 1 \le i \le n-1
	  \end{align*}
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Palindrome splitting}
  \begin{exampleblock}{Palindrome splitting (Problem 7.10)}
	\begin{enumerate}[(1)]
	  \setcounter{enumi}{1}
	  \item Split a string $S[1 \dots n]$ into minimum number of palindromes (\# cuts)
    \end{enumerate}
  \end{exampleblock}

  \begin{description}
	\item[Subproblem:] $C[i,j]$: minimum number of cuts for string $S[i \dots j]$
	\item[Goal:] $C[1,n] + 1$
	  \pause
	\item[Make choice:] Where is the first cut?
	\item[Recurrence:]
	  \[
		C[i,j] = \left\{ \begin{array}{ll}
		  0	\; \text{ if } S[i \dots j] \text{ is a palindrome} & \\
		  \min\limits_{i+1 \le k \le j-1} C[i,k-1] + 1 + C[k,j] & \text{ o.w.}
		\end{array} \right.
	  \]
	  \pause
	\item[Init:] $C[i,i] = 0$
	\item[Time:] $O(n^3)$
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Palindrome splitting}
  \begin{exampleblock}{Palindrome splitting (Problem 7.10)}
	\begin{enumerate}[(1)]
	  \setcounter{enumi}{1}
	  \item Split a string $S[1 \dots n]$ into minimum number of palindromes
    \end{enumerate}
  \end{exampleblock}

  \begin{description}
	\item[Subproblem:] $P[i]$: minimum number of palindromes for $S[1 \cdots i]$
	\item[Goal:] $P[n]$
	  \pause
	\item[Make choice:] Where does the last palindrome start from?
	\item[Recurrence:]
	  \[
		P[i] = \min_{\begin{subarray}{c}
			1 \le k \le i \\
			S[k \dots i] \text{ is a palindrome}
		  \end{subarray}} P[k-1] + 1
	  \]
	  \pause
	\item[Init:] $P[0] = 1$
	\item[Time:] $O(n^3)$ \emph{vs.} $O(n^2)$
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String splitting}
  \begin{exampleblock}{String splitting (Problem 7.11)}
    \begin{itemize}
      \item Split a string $S$ into many pieces
      \item Cost $|S| = n \implies n$
      \item Given locations of $m$ cuts: $\textcolor{gray}{C_{0}}, C_{1}, \cdots, C_{m}, \textcolor{gray}{C_{m+1}}$
      \item Find the minimum cost of splitting $S$ into $m+1$ pieces $S_{0} \cdots S_{m}$
    \end{itemize}
  \end{exampleblock}
\end{frame}
%%%%%%%%%%%%%%%%%%%%
\begin{frame}{String splitting}
  \begin{description}
	\item[Subproblem:] $C[i,j]$: the minimum cost of splitting substring $S_{i} \cdots S_{j-1}$ using cuts $C_{i+1} \cdots C_{j-1}$
	\item[Goal:] $C[0,m+1]$
	  \pause
	\item[Make choice:] What is the first cut in $C_{i+1} \cdots C_{j-1}$?
	\item[Recurrence:]
	  \[
		C[i,j] = \min_{i < k < j} \left(C[i,k] + C[k,j] + l(S_{i} \cdots S_{j-1}) \right)
	  \]
	  \pause
	\item[Init:] $C[i, i+1] = 0$
  \end{description}
\end{frame}
%%%%%%%%%%%%%%%%%%%%


\section{Ensemble Methods}

% ===
\subsection{Bagging
\enskip\normalfont\sffamily (\textbf{B}ootstrap \textbf{agg}regation)}

\begin{enumerate}
    \item Draw $M$ bootstrap sets $Z'_1, \ldots, Z'_M$
    \item Train $M$ base models $b^{(1)}, \ldots, b^{(M)}$
    \item \textbf{Aggregate:}
        $\bbar^{(M)}(\bm x) =
        \left\{
        \begin{array}{@{}l@{\enskip}l@{}}
            \frac1M \sum_{t\leq M} b^{(t)}(\bm x)           & \text{regr.} \\
            \mathrm{sign}\paren{ \sum_t b^{(t)}(\bm x) }    & \text{class.}
        \end{array}\right.$
\end{enumerate}

\emph{Why it works:}
Small \textit{variance} (weak learners),
small \textit{covariance} (almost indep. since $Z'_i \neq Z'_j$).
\\\vspace{-2pt}For finite range $y$ and large enough $M$:
\\\enskip
$\E*[\substack{Y\mid X\\ Z, Z'}]{(y - \bbar^{(M)}(\bm x))^2}
\leq \E*[\substack{Y\mid X\\ Z, Z'}]{(y - b(\bm x))^2}$

\emph{Random Forest:}\enskip
At each splitting step, u.a.r. choose $m$ of $p$ features and split only one (best) feature.
$\to$ reduce \textit{correlation} between trees.

\textbf{Validation:}\enskip
\textit{Out-of-bag error} $\to$ validate each $\bm x_i$ with trees that didn't use it for training.

% ===
\subsection{Boosting}

\textit{Sequentially} train weak learners on all data, but \rotatebox[origin=c]{45}{$\to$}weight of misclass. samples (\rotatebox[origin=c]{-45}{$\!\to$}bias).

% \emph{FSAM {\normalfont\sffamily (forward stagewise additive modeling)}:}

\emph{AdaBoost:}\enskip
Stat. learning (\textit{forward stagewise additive modeling}) with \textbf{exp. loss}, trains max-margin ($= y_i \bbar(\bm x_i)$), self-avg. and interpolating (\rotatebox[origin=c]{-45}{$\!\to$}overfitting) classifiers.

\begin{highlightbox}
    \textit{\scriptsize [Init]:} $\bbar^{(0)} \leftarrow 0$,\enskip $w_i \leftarrow 1/n \:\forall i\leq n$ \\
    for $t = 1 \ldots M$:
    \begin{itemize}[leftmargin=25pt]
        \item[\textit{\scriptsize [Train]:}]
            $b^{(t)} = \arg\min_b \mathcal L^w(b) \color{gray} = \sum_i w_i \mathbb I \{b(\bm x_i) \neq y_i\}$
        \item[\textit{\scriptsize [Eval]:}]
            $\mathrm{err}_t = \mathcal L^w(b^{(t)})$
        \item[\textit{\scriptsize [Aggr]:}]
            $\bbar^{(t)} = \bbar^{(t-1)} + \alpha_t b^{(t)}$;\enskip
            $\alpha_t = \frac12 \log(\frac{1}{\mathrm{err}_t} - 1)$
        \vspace{-2pt}
        \item[\textit{\scriptsize [Reweight]:}]
            $w_i = w_i \cdot \exp(\alpha_t \mathbb I\brace{b^{(t)}(\bm x_i) \neq y_i})$
            {\small normalize!}
    \end{itemize}
    Return $\bbar^{(M)}(\bm x) = \mathrm{sign}(\sum_t \alpha_t b^{(t)}(\bm x))$
\end{highlightbox}

% ===


\documentclass[parskip=half]{scrartcl}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[citestyle=authoryear-icomp,bibstyle=authoryear]{biblatex}
\usepackage{booktabs}
\usepackage[style=british]{csquotes}
\usepackage{fontspec}
\usepackage{mathtools}
\usepackage{ntheorem}
\usepackage{siunitx}

\usepackage{tikz}
\usetikzlibrary{angles,calc,intersections,quotes}

\usepackage[p,osf]{cochineal}
\usepackage[cochineal,vvarbb]{newtxmath}
\usepackage[cal=boondoxupr,calscaled=1.0]{mathalfa}

\newfontfamily\dispfamily{Cabin}[Scale=0.95]
\addtokomafont{disposition}{\dispfamily}

\MakeAutoQuote{«}{»}
\DeclareMathOperator{\Res}{Res}

\addbibresource{cable.bib}

\newcommand{\Int}[2]{\int_{#1}^{#2}\!}
\newcommand{\D}{\mathop{}\!d}

\newcommand{\cZ}{\mathcal{Z}}
\newcommand{\cC}{\mathcal{C}}

\theoremstyle{nonumberplain}
\newtheorem{lemma*}{Lemma}
\newenvironment{proof}{\textbf{Proof.}}{\hfill$\square$}

\title{Analytic solutions to the cable equation}

\author{Sam Yates}
%\date{July 29, 2019}

\sisetup{mode=text}

\begin{document}

% Work-around for siunitx vs newtxmath bug
\ExplSyntaxOn
    \cs_undefine:N \c__siunitx_minus_tl
    \tl_const:Nn \c__siunitx_minus_tl { - }
\ExplSyntaxOff

\maketitle

\section{The cable equation}

The cable equation describes the evolution of the potential
on a long, thin, conducting cable in a conducting medium, separated
by a leaky dielectric. It assumes that the behaviour
can be modelled entirely as a one dimensional problem,
in terms of the linear conductivity of the cable $\sigma$,
and the conductance $g$ and capacitance $c$ per unit length of the
dielectric. The potential $v(x, t)$ then satisfies
\begin{equation}
    (\sigma v')' = c \dot v + g v.
\end{equation}
where $f'$ denotes the derivative of $f$ with respect to the first
variable, and $\dot{f}$ the derivative with respect to the second.
The quantities $\sigma$, $g$, and $c$ are functions of position,
and will depend on the electrical and geometrical properties of
the system.

For a cable of radius $r(x)$ with constant bulk resistivity
$R_L$, areal capacitance $C_M$ and areal surface resistivity
$R_M$, these parameters are given by
\begin{align}
    \sigma(x) &= \pi r(x)^2 / R_L, \\
    g(x) &= 2 \pi r(x) \sqrt{1 + r'(x)^2} / R_M, \\
    c(x) &= 2 \pi r(x) \sqrt{1 + r'(x)^2} C_M.
\end{align}
Letting $\theta(x) = \arctan r'(x)$, the cable equation becomes
\begin{equation}
    \label{eq:constelec}
    \frac{R_M}{2 R_L}\left(
        2\sin\theta\cdot v' + r \cos\theta\cdot v''
    \right) =
    R_M C_M \dot v + v.
\end{equation}

\section{Analytic solutions for $v(x, t)$}

Two important special cases for the cable equation are the cylinder,
with $r(x)$ constant and $\theta(x)=0$, and the conical frustum,
with $\theta(x)$ constant and $r(x)=x\tan\theta$.

Given constant electrical properties, as in \eqref{eq:constelec},
a zero voltage at $t=0$, and Neumann boundary conditions, what
is $v(x, t)$? The Rallpack 1 model \autocite{bhalla1992} is
an example of such a passive cable model, and is discussed in
Appendix \ref{ap:rallpack}.

\subsection{The cylinder}

Suppose the cable lies on the interval $[0, B]$, with initial
voltage zero, zero current
at $x=0$, and fixed injected current $I$ at $x=B$. The boundary
conditions are then $v(x,0) = 0$, $v'(0, t) = 0$, and $v'(B, t) = -E$,
where $-E = I\cdot R_L/\pi r^2$.

A change of variables gives
\begin{equation}
    v(x, t) = -\lambda E\cdot u(x/\lambda, t/\tau),
\end{equation}
where
\begin{equation}
    \lambda = \sqrt{\frac{R_M r}{2 R_L}}, \quad \tau = R_M C_M, \quad b = B/\lambda,
\end{equation}
and $u$ satisfies
\begin{equation}
    \begin{aligned}
        u''(x, t) &= \dot u(x, t) + u(x, t),\\
        u(x, 0) &= 0,\\
        u'(0, t) &= 0,\\
        u'(b, t) &= 1.
    \end{aligned}
    \label{eq:ucyl}
\end{equation}

The corresponding time-invariant problem has solution $\hat u(x)$ where
\begin{equation}
    \hat u'' = \hat u,\quad \hat u'(0) = 0,\quad \hat u'(b) = 1,
\end{equation}
giving
\begin{equation}
    \hat u(x) = \frac{\cosh x}{\sinh b}.
\end{equation}

Consider separable transient solutions $u(x,t)-\hat u(x)$, with zero derivative
at $x=0$ and $x=b$, of the form $\psi(x)\eta(t)$. From \eqref{eq:ucyl},
$\eta(t)=e^{-\lambda t}$ for some $\lambda$ and $\psi$ satisfies
\begin{gather}
    \label{eq:efncyl}
    \psi'' + (\lambda-1)\psi = 0,\\
    \label{eq:efncylbc}
    \quad \psi'(0)=\psi'(b)=0.
\end{gather}
This is a regular Sturm-Liouville problem
\autocite[see e.g.][Theorem~2.1, p.~146]{reid1980} with discrete non-negative eigenvalues
$\lambda_k-1$ and corresponding orthogonal eigenfunctions $\psi_k$,
\begin{equation}
    \langle \psi_k, \psi_{k'} \rangle =
    \Int{0}{b} \psi_k(x)\psi_{k'}(x) \D x =0\quad\text{if $k\neq k'$}.
\end{equation}

The least eigenvalue corresponds to $\lambda_0 = 1$ with eigenfunction
$\psi_0(x)=1$.

For $\lambda>1$, \eqref{eq:efncyl} gives
$\psi(x) = c_1\cos\sqrt{\lambda-1}x + c_2\sin\sqrt{\lambda-1}x$. From $\psi'(0)=0$,
we have $c_2=0$. From $\psi'(b)=1$, it follows $c_1=0$ or $\sqrt{\lambda-1}b = k\pi$
for some positive integer $k$. Consequently we have eigenvalues and eigenfunctions,
\begin{gather}
    \label{eq:lkcyl}
    \lambda_k = 1+\left(\frac{k\pi}{b}\right)^{\mathrlap{2}},\\
    \psi_k(x) = \cos \frac{k\pi x}{b}.
\end{gather}
for integers $k\geq 0$, including the constant $\lambda=1$ case.

The time-dependent solution $u(x, t)$ then is
\begin{equation}
    u(x,t) = \hat u(x) - \sum_{k=0}^{\infty} a_k e^{-\lambda_k t}\psi_k(x),
\end{equation}
with $a_k$ determined by the initial conditions,
\begin{equation}
    0 = \langle u(x,0), \psi_k(x) \rangle
    = \langle \hat u(x), \psi_k(x) \rangle
    - a_k\langle \psi_k(x), \psi_k(x)\rangle,
\end{equation}
with
\begin{align}
    \langle \hat u(x), \psi_k(x) \rangle
    = \frac{1}{\sinh b}\Int{0}{b}\cosh x\cos\frac{k\pi}{b}x \D x
    = (-1)^k\lambda_k^{-1},
    \intertext{and}
    \langle \psi_k(x), \psi_k(x) \rangle
    = \Int{0}{b}\cos^2 \frac{k\pi}{b}x \D x
    =
    \begin{cases}
        b & \text{if $k=0$,}\\
        \frac{b}{2} & \text{otherwise.}\\
    \end{cases}
\end{align}
Consequently,
\begin{equation}
    \label{eq:cylseries}
    u(x,t) = \frac{\cosh x}{\sinh b} - \frac{1}{b}e^{-t} -
    \frac{2}{b}\sum_{k=1}^{\infty} (-1)^k \lambda_k^{-1} e^{-\lambda_k t} \cos\frac{k\pi x}{b},
\end{equation}
with $\lambda_k$ given in \eqref{eq:lkcyl}. A discussion on the residuals in this series can
be found in Appendix~\ref{ap:cylcomp}.

Note that the time derivative $\dot u(x,t)$ can be expressed in terms of Jacobi theta functions:
\begin{equation}
    \begin{aligned}
        \dot u(x,t) &= \frac{1}{b}e^{-t}\left(1+2\sum_{k=1}^{\infty} (-1)^ke^{-\frac{\pi^2 k^2 t}{b}}\cos\frac{k\pi x}{b}\right)\\
        &= \frac{1}{b}e^{-t}\theta_4\left(\frac{\pi x}{2 b}\middle| \frac{i\pi t}{b^2}\right).
    \end{aligned}
\end{equation}

\subsection{The tapered cable}

Consider the cable as a conical frustum on the interval $[A, B]$ with radius
$r(x) = x \tan\theta$ and boundary conditions $v'(A, t) = 0$, $v'(B, t) = -E$,
and $v(x, 0) = 0$ (see Figure \ref{fig:tapered}). A fixed injecting current $I$
at $x=B$ corresponds to $-E=I\cdot R_L/\pi r(B)^2$. Reparameterizing gives
\begin{equation}
    v(x, t) = -\lambda E\cdot u(x/\lambda, t/\tau),
\end{equation}
where
\begin{equation}
    \lambda = \frac{R_M\sin\theta}{2 R_L}, \quad \tau = R_M C_M, \quad a = A/\lambda, \quad b = B/\lambda,
\end{equation}
and $u$ satisfies
\begin{equation}
    \label{eq:conu}
    \begin{aligned}
        x u''(x, t) + 2 u'(x, t) &= \dot u(x, t) + u(x, t),\\
        u(x, 0) &= 0,\\
        u'(a, t) &= 0,\\
        u'(b, t) &= 1.
    \end{aligned}
\end{equation}

\begin{figure}[tbh]
    \begin{tikzpicture}[scale=0.8]
        \coordinate (origin) at (0,0);
        \coordinate (b0) at (15,0);
        \coordinate (b1) at ($ (b0) + (0,1.8) $);
        \coordinate (a0) at (5,0);

        \path [name path=tangent] (origin) -- (b1);
        \path [name path=avert] (a0) -- +(0,2);
        \path [name intersections={of=tangent and avert, by=a1}];

        \coordinate (a-1) at ($ (a1)!2!(a0) $);
        \coordinate (b-1) at ($ (b1)!2!(b0) $);

        \draw [name path=axis, dashed] (origin) -- (b0);

        \draw let \p1 = (a1), \n2 = {0.2*\y1} in
            (a1) arc (90:270:\n2 and \y1);

        \draw let \p1 = (a1), \n2 = {0.2*\y1} in
            [densely dotted] (a-1) arc (-90:90:\n2 and \y1);

        \draw let \p1 = (b1), \n2 = {0.2*\y1} in
            (b0) ellipse (\n2 and \y1);

        \draw (a1) -- (b1) (a-1) -- (b-1);
        \draw [dotted] (origin) -- (a1) (origin) -- (a-1);

        \pic [draw, "$\theta$", angle radius = 15ex, angle eccentricity=1.2] {angle = b0--origin--b1};

        \coordinate (lright) at ($ (b1)!2!(b0) - (0,40pt) $);
        \coordinate (lleft) at ($ (origin) + (lright) - (b0) $);

        \path (lleft) node (x0label) { $x=0$ };
        \path (lright) node (xblabel) { $x=B =b\lambda $ };
        \path ($ (lleft)!(a0)!(lright) $) node (xalabel) { $x=A =a\lambda$ };

        \draw [dotted] ($ (a-1)-(0,2ex) $) -- ($ (xalabel)+(0,2ex) $);
        \draw [dotted] ($ (b-1)-(0,2ex) $) -- ($ (xblabel)+(0,2ex) $);
        \draw [dotted] ($ (origin)-(0,2ex) $) -- ($ (x0label)+(0,2ex) $);
    \end{tikzpicture}
    \caption{Tapered cable coordinates.}
    \label{fig:tapered}
\end{figure}

Proceeding similarly to the cylinder problem, the solution can be presented as a sum of
a time-invariant solution $\hat u$ and a series of transients that are solutions to
a Sturm--Liouville problem.

The time-invariant problem is
\begin{equation}
    \begin{gathered}
        x\hat u''(x) + 2\hat u'(x) - \hat u(x) = 0,\\
        \hat u'(a) = 0,\quad \hat u'(b) = 1.
    \end{gathered}
\end{equation}

Multiplying by $x$ gives a differential equation of the form
\begin{equation}
    \label{eq:conicmu}
    x^2 g''(x) + 2x g'(x) + \mu x g(x) = 0
\end{equation}
for some $\mu$. This has solutions in terms of Bessel functions.
If $\cC_\nu$ is a solution to the Bessel equation
\begin{equation}
    z^2 f''(z) + z f'(z) - (z^2 - \nu^2) f(z) = 0,
\end{equation}
then $g(x) = x^{-\frac{1}{2}}\cC_1(\alpha x^\frac{1}{2})$ satisfies
\begin{equation}
    \label{eq:besg}
    x^2 g''(x) + 2x g'(x) + \frac{\alpha^2}{4} x g(x) = 0.
\end{equation}
Similarly, if $\cZ_\nu$ is a solution to the modified Bessel equation
\begin{equation}
    z^2 f''(z) + z f'(z) - (z^2 + \nu^2) f(z) = 0,
\end{equation}
then $g(x) = x^{-\frac{1}{2}}\cZ_1(\alpha x^\frac{1}{2})$ satisfies
\begin{equation}
    \label{eq:modbesg}
    x^2 g''(x) + 2x g'(x) - \frac{\alpha^2}{4} x g(x) = 0.
\end{equation}

The time-invariant solution $\hat u(x)$ satisfies \eqref{eq:modbesg} with $\alpha=2$, and so
can be written as
\begin{equation}
    \hat u(x) = \frac{1}{\sqrt{x}} \left(c_1 I_1(2\sqrt{x}) - c_2 K_1(2\sqrt{x})\right),
\end{equation}
with derivative
\begin{equation}
    \hat u'(x) = \frac{1}{x} \left(c_1 I_2(2\sqrt{x}) + c_2 K_2(2\sqrt{x})\right).
\end{equation}
Solving for the boundary conditions $\hat u'(a)=0$, $\hat u'(b)=1$ then determines the coefficients
$c_1$ and $c_2$, giving
\begin{equation}
    \label{eq:conuhat}
    \hat u(x) =
    \frac{b}{\sqrt{x}}\cdot
    \frac{K_2(2\sqrt{a})I_1(2\sqrt{x}) + I_2(2\sqrt{a})K_1(2\sqrt{x})}
    {K_2(2\sqrt{a})I_2(2\sqrt{b})-I_2(2\sqrt{a})K_2(2\sqrt{b})}.
\end{equation}

Separable transient solutions $u(x,t)-\hat u(x)$ will be of the form $e^{-\lambda t}\psi(x)$,
where $\psi(x)$ satisfies
\begin{gather}
    \label{eq:conpsi1}
    x\psi''(x) + 2\psi'(x) + (\lambda-1)\psi(x) = 0\\
    \label{eq:conpsi2}
    \psi'(a) = \psi'(b) = 0.
\end{gather}
Multiplying \eqref{eq:conpsi1} by $x$ gives
\begin{equation}
    \label{eq:conpsils}
    x^2\psi''(x) + 2x\psi'(x) + (\lambda-1)x \psi(x) =
    \frac{d}{dx}(x^2\psi'(x)) + (\lambda-1)x \psi(x) = 0,
\end{equation}
which together with \eqref{eq:conpsi2} forms a regular Sturm--Liouville problem with
non-negative eigenvalues $\lambda_k-1$ and corresponding orthogonal eigenfunctions
$\psi_k(x)$ with respect to the weight function $\rho(x) = x$.

For $\lambda_0=1$, the eigenfunction is $\psi_0(x)=1$. For $\lambda_k>0$,
equation \eqref{eq:conpsils} is of the form \eqref{eq:besg}, with $\alpha=2\sqrt{\lambda_k-1}$.
Writing $\omega_k$ for $\sqrt{\lambda_k-1}$, we have
\begin{equation}
    \psi_k(x) = \frac{1}{\sqrt{x}} \left(c_1^{(k)} J_1(2\omega_k \sqrt{x})) + c_2^{(k)} Y_1(2\omega_k \sqrt{x})\right),
\end{equation}
with derivative
\begin{equation}
    \psi_k'(x) = - \frac{\omega_k}{x} \left(c_1^{(k)} J_2(2\omega_k\sqrt{x})) + c_2^{(k)} Y_2(2\omega_k\sqrt{x})\right).
\end{equation}

The boundary conditions $\psi_k'(a) = \psi_k'(b) = 0$ imply
\begin{gather}
    f_2\left(\sqrt{\tfrac{b}{a}}, 2\omega_k\sqrt{a}\right) = 0,
    \intertext{where $f_\nu$ is the cross-product Bessel function}
    f_\nu(q, x) = J_\nu(qx)Y_\nu(x)-J_\nu(x)Y_\nu(qx).
\end{gather}
The eigenvalues for $k>0$ are then $\lambda_k=1+\omega_k^2$ with
\begin{equation}
    \label{eq:conomegak}
    \omega_k =  \frac{\chi_{2, k}\left(\sqrt{\scriptstyle \frac{b}{a}}\right)}{2\sqrt{a}}.
\end{equation}
where $\chi_{\nu, k}(q)$ is the $k$th positive root of $f_\nu(q, x)=0$. These solutions
can be computed efficiently via a Newton--Raphson scheme \autocite{sorolla2013}.
$c_1^{(k)}$ and $c_2^{(k)}$ are then determined up to scale; for example, we can
set
\begin{equation}
    c_1^{(k)} = J_2(2\omega_k\sqrt{x})^{-1},\qquad
    c_2^{(k)} = -Y_2(2\omega_k\sqrt{x})^{-1},
\end{equation}
giving
\begin{equation}
    \label{eq:conpsik}
    \psi_k(x) =\frac{1}{\sqrt{x}}\left(
    \frac{J_1(2\omega_k\sqrt{x})}{J_2(2\omega_k\sqrt{a})}
    -
    \frac{Y_1(2\omega_k\sqrt{x})}{Y_2(2\omega_k\sqrt{a})}
    \right).
\end{equation}

The complete solution then will be
\begin{equation}
    \label{eq:conuxt}
    u(x,t) = \hat u(x) - \sum_{k=0}^{\infty} a_k e^{-\lambda t} \psi_k(x),
\end{equation}
where the $a_k$ will be determined by the initial conditions.

As the $\psi_k$ are
eigenfunctions of the Sturm-Liouville problem (\ref{eq:conpsils}, \ref{eq:conpsi2}), they
are orthogonal with respect to the inner product with weight function $\rho(x)=x$,
\begin{equation}
    \langle f, g \rangle = \Int{a}{b} xf(x)g(x) \D x.
\end{equation}
Taking the inner product of $\langle u(x,0), \psi_k(x)\rangle$ then gives for each $k$,
\begin{equation}
    a_k = \frac{\Int{a}{b} x \hat u(x)\psi_k(x) \D x}{\Int{a}{b} x \psi_k(x)^2 \D x}.
\end{equation}

For $k=0$,
\begin{equation}
    \begin{aligned}
        a_0 &= \frac{1}{b-a} \cdot \Int{a}{b} x \hat u(x) \D x\\
        &= \frac{1}{b-a} \cdot \left. x^2\hat u'(x)\right|_a^b\\
        &= \frac{b^2}{b-a}.
    \end{aligned}
\end{equation}
(See Appendix \ref{ap:conid} for the derivation of this and the following integrals.)

For $k>0$,
\begin{equation}
    \begin{aligned}
        \Int{a}{b} x \hat u(x)\psi_k(x) \D x
        &= \frac{1}{1+\omega_k^2} \cdot \left. (x^2\hat u'(x)\psi_k(x)-x^2\hat u(x)\psi'_k(x))\right|_a^b\\
        &= \lambda_k^{-1} b^2\psi_k(b),
    \end{aligned}
\end{equation}
and
\begin{equation}
    \begin{aligned}
        \Int{a}{b} x \psi_k(x)^2 \D x
        &= \left.
        x^2\psi_k(x)^2 + \frac{x^2}{\omega_k^2}\psi_k(x)\psi'_k(x)
        + \frac{x^3}{\omega_k^2}\psi'_k(x)^2
        \right|_a^b\\
        &= b^2\psi_k(b)^2 - a^2\psi_k(a)^2,
    \end{aligned}
\end{equation}
giving
\begin{equation}
    a_k = \frac{1}{\lambda_k}\cdot \frac{b^2\psi_k(b)}{b^2\psi_k(b)^2-a^2\psi_k(a)^2}.
\end{equation}

This gives the time-dependent solution
\begin{equation}
    u(x,t) = \hat u(x) - \frac{b^2}{b-a}e^{-t} -
    \sum_{k=1}^{\infty} \lambda_k^{-1} e^{-\lambda_k t}
    \frac{b^2\psi_k(b)\psi_k(x)}{b^2\psi_k(b)^2-a^2\psi_k(a)^2}
\end{equation}
where $\hat u$ is given by \eqref{eq:conuhat}, $\psi_k$ is given by \eqref{eq:conpsik},
and $\lambda_k = 1 + \omega_k^2$ is given by \eqref{eq:conomegak}.

\section{Approximating the gradient}

In a finite volume discretization there
will be a computation of the approximation to the gradient
$v'(x)$ as a linear combination of two (or potentially more)
values of the discrete approximation of the voltage for points
near $x$.

If the coefficients in this approximation are constant in
time, they cannot account for source terms or values of the
voltage outside the points in question. The most faithful
such approximation should then reproduce the exact gradient $v'(x)$
in the source-free steady state form of the cable equation,
\begin{equation}
    (\sigma v')' = 0.
\end{equation}

The voltage $v$ is then determined by its values at
any two distinct points $a$ and $b$,
\begin{equation}
    \frac{v(x) - v(a)}{v(b) - v(a)} =
    \frac{\displaystyle \Int{a}{x} \sigma(z)^{-1} \D z}
         {\displaystyle \Int{a}{b} \sigma(z)^{-1} \D z},
\end{equation}
and correspondingly, the gradient is
\begin{equation}
    \sigma(x) v'(x) =
    \frac{v(b) - v(a)}
         {\displaystyle\Int{a}{b} \sigma(z)^{-1} \D z}.
\end{equation}
If $x$ is a point of discontinuity in $\sigma$,
the flux $\sigma(x)v'(x)$ is nonetheless well defined.

For a finite volume approximation, however, the available
estimates may correspond instead to surface area-weighted means
over control volumes. Consider two adjacent control volumes
on $[a, m]$ and $[m, b]$, with mean voltages $\bar v_a$ and
$\bar v_b$ given in terms of a weight function $w(x)$:
\begin{align}
    \bar v_a &= w_a^{-1} \Int{a}{m} w(x)v(x) \D x,\\
    \bar v_b &= w_b^{-1} \Int{m}{b} w(x)v(x) \D x,
\end{align}
with normalizing constants $w_a$ and $w_b$. With $\sigma v' = \kappa$
constant,
\begin{equation}
    \begin{aligned}
        \bar v_a
        &= w_a^{-1} \Int{a}{m} w(x) \left( v(m) + \kappa \Int{m}{x} \sigma(y)^{-1} \D y \right) \D x \\
        &= v(m) + \kappa \cdot w_a^{-1} \Int{a}{m} w(x) \Int{m}{x} \sigma(y)^{-1} \D y \D x,
    \end{aligned}
\end{equation}
and similarly,
\begin{equation}
    \begin{aligned}
        \bar v_b
        &= v(m) + \kappa \cdot w_b^{-1} \Int{m}{b} w(x) \Int{m}{x} \sigma(y)^{-1} \D y \D x,
    \end{aligned}
\end{equation}
Taking the difference then gives a solution for $\kappa$,
\begin{equation}
    \kappa = (\bar v_b - \bar v_a) \cdot \left[
        w_b^{-1}\!\Int{m}{b} w(x)\! \Int{m}{x} \sigma(y)^{-1} \D y \D x\,-\,
        w_a^{-1}\!\Int{a}{m} w(x)\! \Int{m}{x} \sigma(y)^{-1} \D y \D x
        \right]^{\mathrlap{-1}}.
\end{equation}

\newpage
\appendix
\section{The Rallpack 1 model}
\label{ap:rallpack}

The Rallpack suite \autocite{bhalla1992} is a set of three models for the
validation and benchmarking of cable-based neuron simulators. The first of
these comprises a cylindrical passive cable of length $L$ and diameter $d$,
with a constant current applied at $x=0$. The membrane resistive current
density is given by $J = R_M^{-1}(v-E)$, where $E$ is a fixed reversal potential.
The values of the electrial and geometric parameters are given in
Table~\ref{tbl:rallpack1}.

The membrane potential can then be expressed as
\begin{equation}
    v(x, t) = E - \lambda I \cdot u(\frac{b-x}{\lambda}, \frac{t}{\tau}),
\end{equation}
where
\begin{equation}
    \lambda = \sqrt{\frac{R_M d}{4 R_L}},\quad
    \tau = R_M C_M,\quad
    b = L/\lambda,
\end{equation}
and $u(x,t)$ is given by \eqref{eq:cylseries}.

\begin{table}[htb]
    \centering
    \begin{tabular}{lll}
        \toprule
	Parameter & {} & {Value} \\
        \midrule
	Cable diameter                    & $d$    & \SI{1.0}{\um} \\
	Cable length                      & $L$    & \SI{1.0}{\mm} \\
	Bulk (axial) resistivity          & $R_L$  & \SI{1.0}{\ohm\m} \\
	Membrane resistivity              & $R_M$  & \SI{4.0}{\ohm\m\squared} \\
	Membrane specific capacitance     & $C_M$  & \SI{0.01}{\F\per\m\squared} \\
	Membrane reversal potential       & $E_M$  & \SI{-65.0}{\mV} \\
	Injected current                  & $I$    & \SI{0.1}{\nA} \\
        \bottomrule
    \end{tabular}
    \caption{Rallpack 1 parameters}
    \label{tbl:rallpack1}
\end{table}

\section{Series computation for the voltage on the uniform cylinder}
\label{ap:cylcomp}

How many terms in the series \eqref{eq:cylseries} need to be computed for a given tolerance?
We obtain bounds on the remainders in the series as follows.

Let $u_n(x,t)$ be the partial sum up to $k=n-1$ in \eqref{eq:cylseries}, and $r_n(x,t) = u(x,t) - u_n(x,t)$
be the remainder,
\begin{equation}
    r_n(x,t) = -\frac{2}{b}\sum_{k=n}^{\infty} (-1)^k\lambda_k^{-1}e^{-\lambda_k t}\cos\frac{k\pi x}{b}.
\end{equation}
Then $|r_n(x,t)|\leq R_n(t)$ for all $x$, with
\begin{equation}
    R_n(t) = \frac{2}{b}\sum_{k=n}^{\infty} \lambda_k^{-1}e^{-\lambda_k t}\\
    = \frac{2t}{b}\sum_{k=n}^{\infty} l(k)^{-1}e^{-l(k)}\\
\end{equation}
where
\[
    l(z) = \left(1+\left(\frac{z\pi}{b}\right)^2\right)\cdot t.
\]
The summand is monotonically decreasing in $k$, so
\begin{equation}
    \label{eq:rnigamma}
    R_n(t)
    \leq \frac{2t}{b}\Int{n}{\infty} l(k)^{-1}e^{-l(k)}\D k
    = \frac{\sqrt{t}}{b} \Int{\lambda_n t}{\infty} e^{-z}z^{-\frac{3}{2}} \D z.
\end{equation}

For $a>0$, $s>0$, integrating by parts gives
$\displaystyle \Int{a}{\infty} e^{-z}z^{-s}\D z < e^{-a}a^{-s}$,
and so
\begin{equation}
    R_n(t) < \frac{1}{b t}\lambda_n^{-3/2}e^{-\lambda_n t}.
\end{equation}

\section{Identities and integrals for tapered cable eigenfunctions}
\label{ap:conid}

\newcommand{\Fp}{\smash{F^{\mathrlap{\prime}}}\mkern-2.0mu}
\newcommand{\Gp}{\smash{G^{\mathrlap{\prime}}}\mkern-2.0mu}

Let
\begin{equation}
    \begin{aligned}
        F_k(z) &= z^{-\frac{k}{2}}\cZ_k(2z^{1/2}),&
        G_k(z) &= z^{-\frac{k}{2}}\cC_k(2\omega z^{1/2}),
    \end{aligned}
\end{equation}
where $C_k$ and $Z_k$ are solutions to the Bessel and modified Bessel equations
respectively,
\begin{equation}
    \begin{aligned}
        \cZ_k(z) &= c_1 I_k(z) + c_2 e^{i\pi k} K_k(z),&
        \cC_k(z) &= c_3 J_k(z) + c_4 Y_k(z)
    \end{aligned}
\end{equation}
for some constants $c_1$, $c_2$, $c_3$, $c_4$.

$F_k$ and $G_k$ are solutions of the differential equations
\begin{gather}
    z f''(z) + (k+1) f'(z) - z = 0\\
    \intertext{and}
    z g''(z) + (k+1) g'(z) + \omega^2 z = 0
\end{gather}
respectively, following the identity (2) of \autocite[p.~512]{lommel1879}.

Applying the derivative and recurrence relations for Bessel functions
\autocite[\S 10.6, \S10.29]{nistdlmf} then gives the following identities,
\begin{align}
    \label{eq:fgrel}
    \Fp_k(z) &= F_{k+1}(z), & z \Fp_k(z) &= -k F_k(z) + F_{k-1}(z),\\
    \Gp_k(z) &= -\omega G_{k+1}(z), & z \Gp_k(z) &= -k G_k(z) + \omega G_{k-1}(z).
\end{align}
When $k$ is an integer, these imply
\begin{equation}
    \begin{aligned}
        F_{-k}(z) &= z^k F_k(z),\\
        G_{-k}(z) &= (-z)^k G_k(z),
    \end{aligned}
    \qquad \text{for $k\in\mathbb{Z}$}.
\end{equation}

In the following, derivatives will be respect to $z$, and the argument $z$ to
$F_k$ and $G_k$ will be omitted where it is unambiguous.

\subsection*{The integral $\Int{}{} z F_1 G_1 \D z$}

The relations \eqref{eq:fgrel} give the identities
\begin{equation}
    \begin{aligned}
        \label{eq:fgrel2}
        (z^kF_k)' &= z^{k-1}F_{k-1}\\
        (z^kG_k)' &= \omega z^{k-1}G_{k-1}.
    \end{aligned}
\end{equation}
Consequently
\begin{equation}
    \begin{aligned}
        (z^2 F_2 G_1)' &= z F_1 G_1 + z^2 F_2 \Gp_1 = z F_1 G_1 - \omega z^2 F_2 G_2 \\
        (z^2 F_1 G_2)' &= z F_1 G_1 + z^2 \Fp_1 G_2 = z F_1 G_1 + z^2 F_2 G_2.
    \end{aligned}
\end{equation}
Cancelling the $F_2G_2$ term gives
\begin{equation}
    (z^2 F_2 G_1 + \omega z^2 F_1 G_2)' =
    (1+\omega^2) z F_1 G_1,
\end{equation}
and so
\begin{equation}
    (1+\omega^2) \Int{}{} z F_1 G_1 dz = z^2 F_2 G_1 + \omega z^2 F_1 G_2 = z^2 \Fp_1 G_1 - z^2 F_1 \Gp_1.
\end{equation}

\subsection*{The integral $\Int{}{} z F_1 \D z$}

As observed in \eqref{eq:fgrel2}, $(z^2 F_2)' = z F_1$, and so
\begin{equation}
    \Int{}{} z F_1 dz = z^2 F_2 = z^2 \Fp_1.
\end{equation}

\subsection*{The integral $\Int{}{} z G_1^2 \D z$}

Following the technique of \autocite[p.533]{lommel1879},
\begin{equation}
    \begin{aligned}
        (z^a G_k G_{m+1})'
        &= a z^{a-1} G_k G_{m+1} + z^a(\Gp_k G_{m+1} + G_k \Gp_{m+1})\\
        &= a z^{a-1} G_k G_{m+1} - \omega z^a G_{k+1}G_{m+1} + z^{a-1} G_k(\omega G_m - (m+1) G_{m+1})\\
        &= (a-m-1) z^{a-1} G_k G_{m+1} + \omega z^{a-1} (zG_{k+1}G_{m+1} - G_k G_m).
    \end{aligned}
\end{equation}
The last term is symmetric in $k$ and $m$, so
\begin{equation}
    (z^a G_k G_{m+1} - z^a G_m G_{k+1})' =
    (a-m-1) z^{a-1} G_k G_{m+1} - (a-k-1) z^{a-1} G_m G_{k+1}.
\end{equation}
Letting $a=k+1$ then gives,
\begin{equation}
    (z^{k+1} G_k G_{m+1} - z^{k+1} G_{k+1} G_m)' = (k-m) z^k G_k G_{m+1},
\end{equation}
and in particular,
\begin{equation}
    \label{eq:intzg1sq}
    \Int{}{} z G_1^2 \D z = z^2 G_1^2 - z^2 G_0 G_2.
\end{equation}

The result \eqref{eq:intzg1sq} can be expressed in terms of just $G_1$ and $\Gp_1$ by
application of the recurrence relationships:
\begin{equation}
    \label{eq:intzg1sqbis}
    \Int{}{} z G_1^2 \D z = z^2 G_1^2 + \frac{z^2}{\omega^2} G_1 \Gp_1 + \frac{z^3}{\omega^2} G^{\prime 2}_1.
\end{equation}

\printbibliography
\end{document}


\subsubsection{Repeated Linear Factors}
\noindent
If $Q(x)$ has repeated roots, it factors into
\begin{equation*}
	Q(x) = R(x)(x-a)^k\text{, }k \geq 2\text{ and }R(a) \neq 0
\end{equation*}
When making the common denominator for each repeated root of multiplicity $k$, we do
\begin{equation*}
	\frac{P(x)}{R(x)(x-a)^k} = \left(\text{Decomposition of }R(x)\right) + \frac{A_1}{x-a}+\ldots+\frac{A_k}{(x-a)^k}
\end{equation*}
You would then multiply each side by the denominator like in the linear factors case and solve for the coefficients. The only additional difficulty is that you might have to use previous results or solve a system of linear equations to get some of the constants.

\ifodd\includeBackgroundReviewExamples\input{./backgroundReview/algebraPreCalc/repeatedLinearFactors_example.tex}\fi


\documentclass[12pt]{article}
\usepackage{amssymb,latexsym,amsmath}
%\usepackage{epsfig}
%\usepackage{graphicx}
%\DeclareGraphicsExtensions{.pdf, .jpg, .tif, .eps, .jpg}

\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse % we are not running PDFLaTeX
\else
\pdfoutput=1 % we are running PDFLaTeX
\pdftrue
\fi
\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi
\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .tif}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi

\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = -0.5 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2 in
\parindent = 0.0 in

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\khat}{\hat{\mathbf k}}
\newcommand{\uv}{\mathbf u}
%\newcommand{\omegav}{\mathbf \omega}
\newcommand{\omegav}{\mathbf w}
\newcommand{\w}{\mathbf w}
\newcommand{\grad}{\nabla}
\newcommand{\curl}{\grad \times}

\DeclareMathOperator{\Span}{span}

\title{The Boussinesq equations and Coordinate scalings in the
  LANL-Sandia DNS code} \author{Mark Taylor, Beth Wingate, Susan Kurien}

\begin{document}
\maketitle

\section{Scaling of the Domain}

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=4.in]{box}
\caption{ }
\label{F:box}
\end{center}
\end{figure}

We would like to solve the NS equations in the box pictured in
Fig.~\ref{F:box}.  The equations are
\[
\frac{ \partial  \uv }{\partial t}  + (\curl \uv + f \khat) \times \uv +
\grad \pi + N \theta \khat = \nu \Delta \uv
\]
\[
\grad \cdot \uv = 0
\]
\[
\frac{ \partial  \theta }{\partial t}  + \uv \cdot \grad \theta -
N \uv \cdot \khat = \kappa \Delta \theta
\]
The Schmidt number is $\nu / \kappa$.

We refer to the coordinate system in the box in Fig.~\ref{F:box} as
the primted coordinate system, and we now introduce a
change of variables, into the {\em non-primed} coordinate
system.  In the non-primed coordinate system, the domain because a
unit cube:
\[
x = x', \qquad y=y', \qquad  z = \frac{z'}{h},
\]
The only change to our code is that:
\[
\frac{\partial}{\partial z'} = \frac{1}{h} \frac{\partial}{\partial z}
\]
And thus
\[
\grad =
\begin{pmatrix} \dfrac{\partial}{\partial x} \\[5mm]
                \dfrac{\partial}{\partial y} \\[5mm]
                \dfrac{1}{h} \dfrac{\partial}{\partial z}
\end{pmatrix}
 \qquad
\Delta  = \left(\frac{\partial}{\partial x}\right)^2 +
           \left(\frac{\partial}{\partial y}\right)^2 +
           \left(\frac{1}{h}\frac{\partial}{\partial z}\right)^2
\]
and
\[
\grad \cdot \uv = \frac{\partial u_1 }{\partial x} +
\frac{\partial u_2 }{\partial y} +
\frac{1}{h} \frac{\partial u_3 }{\partial z}
\]
and  vorticity $\omegav$ is given by
\begin{equation}
\label{E:vor}
\omegav = \curl \uv =
\begin{pmatrix}
\dfrac{\partial u_3}{\partial y} - \dfrac{1}{h} \dfrac{\partial u_2}{\partial z}  \\[5mm]
\dfrac{1}{h} \dfrac{\partial u_1}{\partial z} -  \dfrac{\partial u_3}{\partial x}  \\[5mm]
\dfrac{\partial u_2}{\partial x} - \dfrac{\partial u_1}{\partial y}
\end{pmatrix}
\end{equation}

The divergence free condition on $\uv$ is solved by the choice
of $\pi$.  This involves,
for some vector field ${\mathbf v}$,
solving an equation of the form
\[
\Delta \pi = \grad \cdot {\mathbf v}
\]
The remaining equations we write in component form:
\[
\begin{pmatrix} u_1 \\
                u_2 \\
                u_3 \\
\end{pmatrix}_t
  + (\omegav + f \khat) \times
\begin{pmatrix} u_1 \\
                u_2 \\
                u_3 \\
\end{pmatrix}
+ \grad \pi +
\begin{pmatrix} 0 \\
                0 \\
                N \theta \\
\end{pmatrix}
= \nu
\begin{pmatrix} \dfrac{\partial^2 u_1}{\partial x^2} +
                \dfrac{\partial^2 u_1}{\partial y^2} +
                \dfrac{1}{h^2}\dfrac{\partial^2 u_1}{\partial z^2}   \\[5mm]
                \dfrac{\partial^2 u_2}{\partial x^2} +
                \dfrac{\partial^2 u_2}{\partial y^2} +
                \dfrac{1}{h^2}\dfrac{\partial^2 u_2}{\partial z^2}   \\[5mm]
                \dfrac{\partial^2 u_3}{\partial x^2} +
                \dfrac{\partial^2 u_3}{\partial y^2} +
                \dfrac{1}{h^2}\dfrac{\partial^2 u_3}{\partial z^2}
\end{pmatrix}
\]
\[
\frac{ \partial  \theta }{\partial t}  + \uv \cdot \grad \theta -
N u_3  = \kappa \left(
                \dfrac{\partial^2 \theta}{\partial x^2} +
                \dfrac{\partial^2 \theta}{\partial y^2} +
                \dfrac{1}{h^2}\dfrac{\partial^2 \theta}{\partial z^2}
\right)
\]

Thus the only changes needed for our parallel DNS code are is that
all derivatives in $z$ need to be scaled by $h$.

\section{Hyper Viscosity}
Consider our scaled hyper viscosity term
\[
\frac{ \partial  \uv }{\partial t} =
\sqrt{E(k_\textrm{max})} \,  k_\textrm{max}^{-2\eta + 1.5}  \Delta^\eta \uv
\]
We first verify that the units are correct.  The units of
energy are $m^2/s^2$, so the units of the energy in a single
spherical shell $E(k)$ is $m^3/s^2$.  Thus we have
\[
\frac{m}{s^2} = \frac{m^{1.5}}{s} \,\frac{1}{m^{-2\eta+1.5}}
                          \,\frac{1}{m^{2\eta}}
                           \, \frac{m}{s}
\]
In the frequency domain,
\[
\frac{ \partial  u_3 }{\partial t} =
\sqrt{E(k_\textrm{max})} \,  k_\textrm{max}^{-2\eta + 1.5}
( (2\pi l)^2 + (2\pi m)^2 + (2\pi n/h)^2)^\eta u_3
\]

\section{Resolution Condition}

The assumptions made in the hydrostatic equations implicitly assume
(acording to Leslie) that
\[
 \max{ k_h } << \min{k_z}
\]
which works out to (assuming 2/3 dealiasing)
\[
   \frac{N_x}{3} <<  \frac{1}{h}
\]

\section{Computing Scalars}
Integrals per unit volume are approximated by the sum
\[
<f,g> = \frac{1}{h} \iiint  f g  \, dx' \, dy' \, dz' =   \iiint  f g \, dx \, dy \, dz
=\frac{1}{N_x N_y N_z} \sum  f  g
\]
We also have that
\[
\frac{1}{N_x N_y N_z} \sum  f  g = \sum \hat{f} \hat{g}
\]
so integrals can be computed in Fourier or grid space.

All other scalars (kinetic energy, helicity, various dissipation
terms) are computed in physical coordinates.  So for example
\[
k = <\uv,\uv> = <u_1,u_1> + <u_2,u_2> +  <u_3,u_3>
\]
and the helicity
\[
  h  = w_1 u_1  + w_2 u_2 + w_3 u_3
\]
As in example, using integer wave numbers $(l,m,n)$ (defined in the
next section), we have
\[
< \omegav , \Delta \uv> = \sum (2 \pi)^2 (l^2 + m^2 + (n/h)^2)
\, \uv \cdot \omegav
\]

\section{Computing 3D Energy Spectra (in a spherical shell)}
If we choose to discretize our domain so that the
maximum wave number (in the primed coordinate system) is
the same in all diretions, then
\[
\Delta x' = \Delta y' = \Delta z'
\]
Or
\[
N_x  = N_y = N_z/h
\]
where $N_x$ is the number of grid points in the $x$ direction.
The highest wave number is given by:
\[
k_x = \pi N_x, \qquad k_y = \pi N_y, \qquad k_z = \pi N_z/h
\]
(Note that $\pi$ appears in the wave numbers since our domain is
of length 1 in the $x$ and $y$ diretions.)  The wave number spacing
is given by
\[
\Delta k_x = 2 \pi, \qquad  \Delta k_y = 2 \pi, \qquad  \Delta k_z = 2 \pi / h
\]
In our DNS code, we index our arrays of Fourier coefficients
with integers $(l,m,n)$, with $l=0 \dots N_x/2$,
$m=0 \dots N_y/2$ and
$n=0 \dots N_z/2$.  The wave number spacing is given by
given by
\[
k_x = l \Delta k_x,, \qquad  k_y = m \Delta k_y, \qquad  k_z = n  \Delta k_z
\]
We index the arrays containing power spectra
such as $E(k)$ with integer wave number $k$, represents a shell or anulus
of some chosen thickness centered around the wave number associated
with $k$.

The total energy is
\[
E  = \sum_{l,m,n}  |  {\hat u_1}(l,m,n) |^2 +
 |  {\hat u_2}(l,m,n) |^2 +
 |  {\hat u_3}(l,m,n) |^2
\]

The standard energy spectrum is now, for integers $k$:
\[
E(k)  = \sum_{l,m,n}  |  {\hat u_1}(l,m,n) |^2 +
 |  {\hat u_2}(l,m,n) |^2 +
 |  {\hat u_3}(l,m,n) |^2
\]
where the sum is taken over all integers $(l,m,n)$ such that
\[
(\Delta k_z)^2 (k-1/2)^2 \le (\Delta k_x l)^2 + (\Delta k_y m)^2 + (\Delta k_z n)^2 <  (\Delta k_z)^2 (k+1/2)^2
\]
where we have chosen to use a shell thickness of $\Delta k_z$.
Scaling out the $2\pi$, we get
\[
(k-1/2)^2 \le  h^2 \left( l^2 + m^2 + (n/h)^2 \right) < (k+1/2)^2
\]

\section{Computing 2D Energy Spectra (in an annulus)}
We also compute a 2D spectra $E(k_h,k_z)$ by summing squares of Fourier
coefficients over annular regions in
$x$ and $y$ of radius $k_h$.
where
\[
k_h = \sqrt{(k_x^2 + k_y^2)} = \sqrt{(2\pi l)^2 + (2\pi m)^2}.
\]
From this quantity, we can get $E(k_h,0)$, which
represents the energy spectrum of the 2D field
obtained by averaging the original 3D field  in the direction of rotation,
and then summing the energy within spherical shells (an annulus for 2D fields).
We are also interested in
\begin{equation}
E(k_h) = \sum_{k_z=0}^{\pi N_z/h} E(k_h,k_z)
\label{E:SPECb}
\end{equation}
which represents
the energy spectrum of the full 3D field, but summed over
the annular region between concentric cylinders
(whose axis are in the $z$ direction).  As with the spherical
shell spectrum, Eq.~\ref{E:SPECb} has the property that
it partitions the energy into a set of discrete wave numbers, so
that the total energy
\[
E = \sum_{k_h} E(k_h)
\]

The 2D shell (annulus) thickness is now
determined by the horizontal wavenumber spacings. In our code,
we use an integer index $k$ defined by $k_h = k \Delta k_x$ and
define
\begin{eqnarray*}
E(k,j) = \sum_{l,m}  |  {\hat u_1}(l,m,n) |^2 +
 |  {\hat u_2}(l,m,n) |^2 +
 |  {\hat u_3}(l,m,n) |^2
\end{eqnarray*}
where the sum is taken over fixed $n$ and all integers $(l,m)$ such that
\[
(\Delta k_h)^2 (j-1/2)^2 \le (\Delta k_x l)^2 + (\Delta k_y m)^2  <  (\Delta k_h)^2 (j+1/2)^2
\]
and with $k_x=k \Delta k_x$ for integers $k$.
we have chosen to use a shell thickness of $\Delta k_h =\Delta k_x = \Delta
k_y = 2\pi$. Scaling out the $2\pi$, we get
\[
(j-1/2)^2 \le  l^2 +  m^2 < (j+1/2)^2.
\]

\section{Running the Model for the Boussinesq equations}

\noindent
This section discusses important elements of the inputfile and how to
build the executable.

When you've chosen scalar type = 4 in the gridsetup script (see
below), you need to add a line to the input file right after the line
for the smagorinsky coefficient,
\noindent
\begin{verbatim}
.
.
.
107.08        ! Bous. parameter  Fr = .21/bous=107.08
0e0           ! alpha (for NS-alpha model).  alpha>1 is in units of delx
.0            ! smagorinsky
1             ! max number of schmidt numbers for passive scalars
1. 4          ! schmidt number = nu/kappa, ratio of viscous coeffs,type of scalar
0             ! compute_structure_functions
.
.
.
\end{verbatim}

Note that the type in this file must match the type in the gridsetup
command. Also note that if you want to the code to compute the
two-point correlations of potential vorticity then you need to set the
value of \texttt{compute structure functions} to 1.

\noindent
To set up the model you need to use the following:

\begin{description}

\item[1)] Example to set the FORTRAN arrays sizes before compiling:
  ($32^3$ simulation on 8 processors, prognostic variables: $u,v,w$
  and $\theta$). Note the 4 on the end, which
  indicates the type of scalar equations for the code to use \\

  \begin{verbatim}
      ./gridsetup.py 1 1 8 32 32 32 2 2 2 0 0 0 4
  \end{verbatim}

  \noindent
  This one sets up the problem for $128^3$ on 32 processors
  like this:

  \begin{verbatim}
   ./gridsetup.py 1 1 32 128 128 128 2 2 2 0 0 0 4
  \end{verbatim}

\item[2)] Then one makes the Boussinesq equations by
  typing\\

  \begin{verbatim}
  gmake dnsb
  \end{verbatim}
this  is identical to the dns code, except that it compiles in
the Boussinesq diagnostics (aspect\_diag.F90)
instead of the usual turbulence
diagnostics (turb\_diag.F90).
\end{description}

\noindent

\section{Comparing with Smith \& Waleffe (SW99 and SW02)}

\subsection{Scalings and definitions}

\subsubsection*{$\underline{\text{Wave numbers}}$}
Wave numbers are dimensional and should be computed $k_f = 2 \pi
k_f^m$, where the superscript $m$ stands for Mark.

\subsubsection*{$\underline{\text{Rossby Number}}$}
SW02 computes this Rossby number,
\begin{equation}
  Ro = \frac{(k_f^2 \epsilon_f)^{1/3}}{\text{fcor}},
\end{equation}
where each number in the equation is computed as,
\begin{description}
   \item[1)] $k_f = 2 \pi k_f^m$
   \item[2)] $\epsilon_f$ is computed as an average through the run using
     scalars.m. It can also be found for each time in the output file
     if one looks at the $d/dt$ line and hunts for the f parameter (for
     the forcing).
  \item[3)] fcor is an input to the code. There is some question as to
    whether we should divide by $fcor/2$ or $fcor$, see SW02 Eq 3.2

\end{description}

\subsubsection*{$\underline{\text{Froude number}}$}

Modifications to the dns code to accomodate this is to include the
addition of a parameter {\bf{bous}} which multiplies the vertical
velocity in the  density anomoly equation. The equations we actually
solve in the code are,
We would like to solve the NS equations in the box pictured in
Fig.~\ref{F:box}.  The equations are
\[
\frac{ \partial  \uv }{\partial t}  + (\curl \uv + \text{fcor} ~\khat) \times \uv
+
\grad \pi + \text{bous}~ \theta \khat = \nu \Delta \uv
\]
\[
\grad \cdot \uv = 0
\]
\[
\frac{ \partial  \theta }{\partial t}  + \uv \cdot \grad \theta -
\text{bous} ~\uv \cdot \khat = \kappa \Delta \theta.
\]
Therefore to compute a consistent Froude number,
\begin{equation}
Fr = \frac{(\epsilon_f k_f^2)^{1/3}}{\text{bous}}.
\end{equation}
Basically, $\text{bous}$ in the code is the is the same as $N$ in SW02 eq
2.1-2.3.

\subsubsection*{$\underline{\text{Time Scale}}$}
To be consistent with SM multiply the time in the output file by the
time scale from SM, $t_{sw02} = (\epsilon_f (2 \pi k_f)^2)^{1/3}$

\subsection{Test of rotation only, $\text{bous} = 0$ (no stratification)}

Compare figure (\ref{fig:SW99Fig3}) and figure (r16ketime).

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{SW99Fig3}
\caption{From Smith and Waleffe 1999}
\label{fig:SW99Fig3}
\end{center}
\end{figure}
\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{r16ketime}
\caption{From sandia/lanl dns code for different kinds of hypeviscosity}
\label{fig:r16ketime}
\end{center}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{SW99Fig4}
\caption{From Smith and Waleffe 1999}
\label{fig:SW99Fig4}
\end{center}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{SW99Fig10}
\caption{From Smith and Waleffe 1999}
\label{fig:SW99Fig10}
\end{center}
\end{figure}

\subsection{Test of stratification only, $fcor=0$ (no rotation) }

Compare figure (\ref{fig:SW02Fig2}) to figure (\ref{fig:t900})

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{SW02Fig2}
\caption{From Smith and Waleffe 2002}
\label{fig:SW02Fig2}
\end{center}
\end{figure}
\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=4.in]{t900}
\caption{From the sandia/lanl dns code for 3 different types of hyperviscosity.}
\label{fig:t900}
\end{center}
\end{figure}

First test, $f=0, N\ne0$. Choose $bous=107.08$ with hyperviscosity. Then
\begin{equation}
Fr = \frac{\left((.5)(2 \pi 24)^{2}\right)^{1/3}}{107.08}= .21
\end{equation}
Forcing is:
\begin{verbatim}
sto_high_24.
\end{verbatim}
A plot of the spectrum is in Figure \ref{fig:norot} and should be
compared to SW02's Figure 2.
\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{frp21}
\caption{Spectrum from $200^3$ simulation of LANL/Sandia DNS code with
  no rotation and $Fr=.21$. This is Smith and Waleffe 2002 Figure 3.}
\label{fig:norot}
\end{center}
\end{figure}

\subsection{Test of nontrivial rotation and stratification $N\ne0, f\ne0$ - }

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=6.in]{SW02Fig4}
\caption{From Smith and Waleffe 2002}
\label{fig:SW02Fig4}
\end{center}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[angle=-90,width=4.in]{SW02Fig5}
\caption{From Smith and Waleffe 2002}
\label{fig:SW02Fig5}
\end{center}
\end{figure}

\begin{eqnarray}
Fr &=& \frac{\left((.5)(2 \pi 24)^{2}\right)^{1/3}}{107.08}= .21 \\
Ro &=& \frac{\left((.5)(2 \pi 24)^{2}\right)^{1/3}}{1.07077} = 21
\end{eqnarray}
This means $\text{bous}=108.07$ and $\text{fcor} =1.07077 $ . We try
to produce the plots in Smith and Waleffe Figure 4 and 5.

Forcing is
\begin{verbatim}
sto_high_24.
\end{verbatim}

\section{Test of projection routines in unit aspect ratio.  March 4, 2009, SK}

The projection of the boussinesq data onto the Craya-Herring normal
modes is in spectrum.F90 in the compute\_project\_ch subroutine. This
routine is called from analysis\_spectral.F90 and decomposes a given
snapshot (gridspace) into its two wave and one vortical mode (SW02) in fourier space and computes the power spectra of those modes.

All our test cases were run to compare with the unit aspect ratio
cases in SW02. We match the resolution ($128^3$), Ro, Fr and forcing
wavenumber $k_f = 24$. Our energy input rate $\epsilon_f = 0.5$.

\subsection{Case SW02: $128^3$ gridpoints, $N/f \rightarrow \infty$, $Fr = 0.21$, $Ro \rightarrow \infty$}
Figure \ref{Froude_time} shows the time evolution of the kinetic energy of the flow with the same nondimensional time and energy units used in SW02 namely:
$$T = t(\varepsilon_f k_f^2)^{1/3}$$
$$energy = E(\varepsilon_f/k_f)^{-2/3}$$
This case was run upto T = 1088 in these units. This figure should be compared with Fig. 1 in SW02 for the $Fr = 0.21$, $k_f = 24$ case.
\begin{figure}[ht]
\centering
  \includegraphics[scale = 0.5]{Comp_SW02_fig1_Nfinf_kf24}
\caption{Evolution in time of the kinetic energy. To be compared with $Fr = 0.21$, $k_f = 24$ curve in Figure 1 of SW02.\label{Froude_time}}
\end{figure}

The figure \ref{Froude_spec} shows the total and projected spectra for
this case at T=1088, to be compared with SW02, Fig. 2, plotted at T = 900.
The total and $k_h=0$ agree very well qualitatively.
\begin{figure}[ht]
\centering
  \includegraphics[scale = 0.7]{Comp_SW02_fig2}
  \caption{Total energy and the projected wave, vortical and $k_h=0$
    spectral distributions at T = 690. To be compared with Figure 2 of
    SW02. \label{Froude_spec}}
\end{figure}

\subsection{Case SW02: $128^3$ gridpoints, $N/f = 100$, $Fr = 0.21$, $Ro = 21$}
Figure \ref{NF100_time} shows the time evolution of the kinetic energy of the flow with the same non-dimensional time and energy units used in SW02 namely:
$$T = t(\varepsilon_f k_f^2)^{1/3}$$
$$energy = E(\varepsilon_f/k_f)^{-2/3}$$
This case was run upto T = 1151 in these units. Note that SW02
runs this case upto T=2200 (not shown in their Fig. 4) and plots their
spectra at T=2200. Comparing Fig. \ref{NF100_time} and the $N/f = 100$
curve of SW02 Fig. 4 upto time T = 800 (the maximum time-scale in
SW02) we notice slight differences which can probably be attributed to
the slight difference in our peak forcing wavenumbers (ours is
$k_f=24$, vs. SW02 $k_f=20$) and the fact that our $\epsilon_f \simeq
0.5$ whereas SW02 fixed theirs at $\epsilon_f = 1$. But the overall
shape of the curve, the time at which it starts to grow after an
initial equilibrium ($\approx T = 200$) is the same.
\begin{figure}[ht]
\centering
  \includegraphics[scale = 0.5]{Comp_SW02_fig4_Nf100}
\caption{Evolution in time of the kinetic energy. To be compared with $N/f = 100$ curve in Figure 4 of SW02.\label{NF100_time}}
\end{figure}

The energy spectra of the total energy as well as the wave, vortical
and $k_h=0$ VSHF modes is given in Fig. \ref{NF100_spec}. This is
computed at our latest time of $T=1151$, to be compared with the SW02
Fig. 5 computed at T = 2200. The differences can probably be
attributed both to the later time of SW02 and the slight differences
in our forcing wavenumber, energy input rate $\epsilon_f$. But the
qualitative agreement is good. The $k_h = 0$ VSHF modes carry most of
the energy in the peak low-wavenumbers. An unexplained difference is
the 'peaky' nature of the low-mode energy distribution ($5< k < 10$)
in SW02 Fig. 5. This does not appear in our runs. We show in addition
the wave and vortical mode spectra which SW02 does not show. We do not
show the potential energy spectrum which SW02 does show.
\begin{figure}[ht]
\centering
  \includegraphics[scale = 0.7]{Comp_SW02_fig5}
\caption{Total energy and the projected wave, vortical and $k_h=0$ spectral distributions at T = 1151. To be compared with Figure 5 of SW02. \label{NF100_spec}}
\end{figure}

\subsection{Case SW02: $128^3$ gridpoints, $N/f = 10$, $Fr = 0.21$, $Ro = 2.1$}
Figure \ref{NF10_time} shows the time evolution of the kinetic energy
of the flow with the same non-linear time and energy units used in
SW02. This case was run upto about T = 690 in those units. Note that SW02
runs this case upto T=605. Comparing Fig. \ref{NF10_time} and the $N/f = 10$
curve of SW02 Fig. 4 upto time T = 605 (the maximum time-scale in
SW02) we notice slight differences which can probably be attributed to
the fact that our $\epsilon_f \simeq
0.5$ whereas SW02 fixed theirs at $\epsilon_f = 1$. But the overall
shape of the curve, the time at which it starts to grow after an
initial equilibrium ($\approx T = 200$) is the same.
\begin{figure}[ht]
\centering
  \includegraphics[scale = 0.5]{Comp_SW02_fig4_Nf10}
\caption{Evolution in time of the kinetic energy (NOT the total energy as it says in the title field). To be compared with $N/f = 10$ curve in Fig. 4 of SW02. \label{NF10_time}}
\end{figure}

The energy spectra of the total energy for this run as well as the
wave, vortical and $k_h=0$ VSHF modes is given in Fig.
\ref{NF10_spec}. This is computed at our latest time of $T=690$, to be
compared with the SW02 Fig. 9 computed at T = 605. The differences can
probably be attributed both to the slightly earlier time of SW02 and
the slight differences in our energy input rate $\epsilon_f$. The
qualitative agreement is good. An unexplained difference is the
'peaky' nature of the low-mode energy distribution ($10< k < 15$) in
SW02 Fig. 9, This does not appear in our runs although we do have a
smooth peaking of the energy in the same wavenumber range.  The $k_h =
0$ (VSHF) modes carry most of the energy in the peak low-wavenumbers.
The PV (vortical) mode spectra also agree well in that they do not
contribute to the most energetic low wave numvers (corresponding to
the peak in the $k_h=0$ spectrum but they make the dominant
contribution to the low wavenumbers $k < 8$ but the energy in these
low modes is an order of magnitude less than the peak in $10< k <15$.
We also show in addition the wave modes which SW02 does not show. We
do not show the potential energy spectrum which SW02 does show.
\begin{figure}[ht]
\centering
 \includegraphics[scale = 0.7]{Comp_SW02_fig9}
 \caption{Total energy and the projected wave, vortical and $k_h=0$
   spectral distributions computed at $T= 690$. To be compared with
   Fig. 9 of SW02. \label{NF10_spec}}
\end{figure}

\section{Test of projection routines and non-unit aspect ratio. July 9, 2009. SK}
We are trying to recover Figure 1 (dashed line) and Figure 2 in
Remmel, Sukhatme and Smith, 2009.  Since our domain parameters our
different from those in Remmel et al 2009, we had to define the
appropriate scales as follows:

\subsection{Scales}
\begin{table}[ht]
\begin{tabular}{lll}
\hline\\
Parameter & RSS09 & Sandia-LANL code\\
\hline\\
aspect ratio $A$ & 0.2 & 0.2\\
dimension ($L_x \times L_y\times L_z$) & $10\pi \times 10\pi \times 2\pi $ & $1  \times 1 \times A$ \\
resolution ($N_x \times N_y \times N_z$) & $80 \times 400 \times 400$ & $80 \times 400 \times 400$ \\
$\Delta k_x \times \Delta k_y \times \Delta k_z = \frac{2\pi}{L_x} \times \frac{2\pi}{L_y} \times \frac{2\pi}{L_z}$ &
$0.2 \times 0.2 \times 1$ & $2\pi \times 2\pi \times 10\pi$  \\
energy input rate $\epsilon_f$ & 1 & 1 \\
smallest 3D $k$-shell &  $\Delta k = \Delta k_z = 1$ & $\Delta k = \Delta k_z = 10\pi$\\
forcing shell $k_f = 4$ & $k_f = 4\times \Delta k_z = 4$ & $k_f = 4 \times \Delta k_z = 4\times 10\pi$\\
damped modes (fat shells) & $k \leq 2.5$ & $k \leq 2.5\times 10\pi $ \\
\hline
\end{tabular}
\end{table}

With the above definitions, the various scales and $Ro$ and $Fr$ are
defined for each of the two simulations:
\begin{itemize}
\item typical velocity based on energy input rate and forcing scale
  (which by choice is the same as the {\it vertical scale}) $U =
  ({\epsilon_f}{H})^{1/3} = \displaystyle(\frac{\epsilon_f}{k_f})^{1/3}$
\item typical vertical length scale $H = \displaystyle\frac{1}{k_f}$;
\item typical horizontal length scale $L = \displaystyle\frac{H}{A} = \displaystyle\frac{1}{A k_f}$;
\item $Ro = \displaystyle\frac{U}{Lf} = 0.05 \implies f = \displaystyle\frac{U}{L Ro} = \displaystyle\frac{A \epsilon_f^{1/3}  k_f^{2/3} } {Ro}$;
\item $Fr = \displaystyle\frac{U}{HN} = 0.05 \implies N = \displaystyle\frac{U}{H Fr} = \displaystyle\frac{\epsilon_f^{1/3}k_f^{2/3}}{Fr}$;
\item Nonlinear time $\tau = (\epsilon_f k_f^2)^{(-1/3)}$. We will use subscript $RSS$ or $S-L$ to denote the RSS09 or Sandia-LANL timescales respectively.
\item Energy scale ${\cal E} = (\epsilon_f /k_f)^{(2/3)}$. Again the corresponding subscripts will be used.
\end{itemize}

RSS09 forces the eigenmodes randomly at each time-step. Sandia-LANL
code forces $u,v,w$ and $\theta$ randomly at each time-step. These
should be equivalent.

Remmel et al have a uniform drag added to the rhs of the equations,
$-0.1 b$ where $b$ is any
of $u,v,w,\theta$, for all modes $k < 2.5$. In order to choose the
corresponding factor for the Sandia-LANL simulation we note that the
drag coefficient $d_r = 0.1$ has units of $T^{-1}$. Assuming the
non-dimensional product $\tau d_r$, should be the same in each of our
simulations we can determine $d_r$ for the Sandia-LANL code.

In the RSS09 simulation, $\tau d_r \simeq 25$, and
therefore for the Sandia-LANL code the corresponding
\begin{itemize}
\item $d_r = 25/\tau = 1$.
\end{itemize}
In order to get RSS09's dimensional time, we rescale our time by
$\tau_{RSS}/\tau_{S-L}$. In order to get RSS09's dimensional energy,
we rescale our energy by ${\cal E}_{RSS}/{\cal E}_{S-L}$.

\subsection{Results}
Unless otherwise stated, all the scalings are as defined above for the
Sandia-LANL domain convention. The input file is:

\begin{verbatim}

8            ! input file type
1             ! file I/O header type   1-5
ns_uvw       ! equations
zero        ! initial condition:  KH-blob, KH-anal, iso12
0           ! init condition subtype
sto_high_t4  ! forcing:  none, iso, iso12, iso12w, iso23w
2.0 1       ! forcing parameters (not used for iso12, n.n=f^2, n width of gaussian)
value        ! viscosity type  (value, smallest, kediff, Re)
0e-5        ! viscosity coefficient.  (300)
hyper16_imp   ! hyper16      ! hyper16_imp ! hyper vis
2.500         ! hyper vis value
hypo0        ! hypo vis
1          ! hypo vis value
100          ! fcor  rotation
0.2        ! Z sscaling  (aspect ratio)
500.0            ! Bous. parameter
0e0           ! alpha (for NS-alpha model).  alpha>1 is in units of delx
.0            ! smagorinsky
1             ! max number of schmidt numbers for passive scalars
1. 4            ! schmidt number = nu/kappa, ratio of viscous coeffs, type
0            ! compute_structure functions (set to 1 to compute strfns)
0            ! compute pdf functions
fft-dealias  ! derivative method  (fft, fft-dealias, fft-sphere, 4th)
periodic     ! x bc  (periodic, no-slip)
periodic     ! y bc
periodic     ! z bc
5          ! time to run
1.5          ! adv cfl  (0 = disabled, use min_dt) .8
0.25           ! vis cfl  (0 = disabled, use min_dt) .01
0.000010             ! min_dt
.1             ! max_dt
0.0          ! restart_dt  (0 = restart output disabled)
.1          ! diag_dt   (.scalars)
.1            ! model_dt   (.spec,.scalars-bous, .bisostr, .isostr)
-50          ! screen_dt (0 = disables, +ve = timesteps, -ve = time)
.2          ! output_dt  (field data, 0 = output disabled, except for custom out times)
0             ! also output the vorticity
0           ! n_output_custom  number of custom output times
00.00       ! custom output times, one per line

\end{verbatim}

Figure \ref{Fig1_H} is to be compared with RSS09 Fig. 1.
\begin{figure}[ht]
\centering
\includegraphics[scale = .7]{RSS09_Fig1_UH}
\caption{The time has been rescaled by $\tau_{RSS}/\tau_{S-L}$ and the energy has been rescaled by ${\cal E}_{RSS}/{\cal E}_{S-L}$. \label{Fig1_H}}
\end{figure}

Figure \ref{Fig2_H} is to be compared with RSS09 Fig. 2.
\begin{figure}[ht]
\centering
\includegraphics[scale = .7]{RSS09_Fig2_UH}
\caption{Total, wave and vortical energy spectra at rescaled time = 30.\label{Fig2_H}}
\end{figure}

\section{Bousinesq scalars we're interested in computing}

We'd like to compute the following (dimensional) quantities integrated
around in the volume:

\begin{itemize}
\item[1.] Total energy and total energy dissipation

\begin{equation}
\frac{\partial}{\partial t} \frac{1}{2} \int_V
         \left[  (u^2 + v^2 + w^2) +   \theta^2\right]
= \frac{1}{2}\int_V \left[  \nu \nabla^2 (u^2 + v^2 + w^2)
                     + \kappa \nabla^2 \theta^2
                         \right]
\end{equation}

\item[2.] Potential energy, $\frac{1}{2} \theta^2$

\item[3.] Kinetic energy, $\frac{1}{2} ( u^2 + v^2 + w^2 )$

\item[4.] Potential vorticity (q) and its production-dissipation which
  should globally integrate to zero I think.
  \begin{equation}
  \frac{\partial}{\partial t} \int_V
           \left[ q \right]
  = \int_V \left[ \nu (\nabla^2 \mathbf\omega) \cdot \mathbf{\nabla}
    (\bar{\theta})
                       + \kappa (\nabla^2 \mathbf{\nabla} \theta) \cdot
                       (\mathbf\omega
                       + \text{fcor} ~{\bf{\hat{z}}} ))
                           \right]
  \end{equation}

  Here $\mathbf = \mathbf\nabla \times \mathbf u$ is the vorticity and
  $\bar\theta = \theta_o -\text{bous}~ z + \theta(x,y,z)$. Also, $q =
  (\mathbf\omega + \text{fcor}~\mathbf{\hat{z}}) \cdot \mathbf\nabla
  \bar\theta.$

\item[5.] Potential enstrophy $Q = q^2/2$ and its
  production-dissipation. This is the important production-dissipation
  rate we need for our two-point correlation.

\item[6.] Potential vorticity production-dissipation

\item[7.] Potential enstrophy
  \begin{equation}
    \frac{\partial}{\partial t} \int_V   \left[ Q \right]
    = \int_V \left[ \nu  ~q (~\nabla^2 ~\mathbf\omega) \cdot \mathbf{\nabla}
      (\bar{\theta})
      + \kappa  ~q ~\nabla^2 (\mathbf{\nabla} \theta) \cdot
      (\mathbf\omega
      + \text{fcor} ~{\bf{\hat{z}}} ))
    \right]
  \end{equation}

\item[8.] two-point correlation (coming)

\end{itemize}

\section{Comparing SW to KSW}
In the two-point correlation paper the equations we use are scaled
differently than than we use in Mark's code (which are the same
equations used in SW). To make comparisions with our results in the
two-point correlation paper we have to keep in mind the following
scalings. Note, these are relationships between {\sl{dimensional}}
quantities, not nondimensional quantities.
\begin{gather}
\tilde\rho = (\frac{b \rho_o}{g})^{1/2} ~\theta\\
(\frac{g}{\rho_o b})^{1/2}~\rho = (\frac{g \rho_o}{b}) - \text{bous}
~z + \theta = \bar\theta \\
N = \text{bous} = (\frac{g b}{\rho_o})^{1/2}\\
pe_{\text{ksw}} = \frac{1}{2} \theta^2 = \frac{g}{\rho_o b} \tilde\rho^2\\
q_{\text{ksw}} = (\frac{g}{b \rho_o})^{1/2} q_{\text{SW}}
\end{gather}

\end{document}