% --- [ Combined Results ] -----------------------------------------------------

\subsection{Combined Results}
\label{sec:combined_results}

The \textit{combined} control flow recovery results of the Hammock method, the Interval method, and for comparison the theoretical optimum when recovering \textit{2-way conditionals}, \textit{n-way conditionals}, \textit{pre-test loops} and \textit{post-test loops} from the combined test programs of Coreutils and SQLite are presented in figure \ref{fig:total_results_combined}.

The \textbf{Hammock method} correctly recovered $42.16\%$ of the control flow primitives present in the test programs (\textit{true positives}). On average, for every $10$ control flow primitives of the original source code, the Hammock method recovered $3.5$ control flow primitives that were \textit{not} present of the original source code (\textit{false positives}).

The \textbf{Interval method} correctly recovered $74.28\%$ of the control flow primitives present in the test programs (\textit{true positives}). On average, for every $10$ control flow primitives of the original source code, the Interval method recovered $5.9$ control flow primitives that were \textit{not} present of the original source code (\textit{false positives}).

\begin{figure}[htbp]
	\centering
	\includegraphics[width=\textwidth]{inc/5_results/results_combined.png}
	\caption{Comparison of control flow recovery results for each method, with the combined results of recovering \textit{2-way conditionals}, \textit{n-way conditionals}, \textit{pre-test loops} and \textit{post-test loops}. The data is based on the combined test programs of Coreutils and SQLite.}
	\label{fig:total_results_combined}
\end{figure}


\section*{Week 8: Shareholders' Equity and Earnings Per Share}

\subsection*{Contributed Capital: Commong Stock}

Common stock is divided into Par Value and Additional paid-in capital (APIC). Par value is the stated value on the face of the security.  APIC is the difference between capital raised and par value. \\
Underwriting expenses:
\begin{itemize}[noitemsep,topsep=0pt]
	\item If $ Cash = Par + APIC \implies $  were subtracted from APIC.
	\item If $ Cash < Par + APIC \implies $ capitalized. An asset is created such that $Cash + Capitalized Costs = Par + APIC$
\end{itemize}

\subsection*{Earnings Per Share}

$Basic EPS = \frac{NIavailableToCommonShareholders}{Weighted Average Shares Outstanding}$ \\

$Numerator = NI - NI attributable to noncontrolling interest - Preferred dividends $ \\

$Diluted EPS = \frac{NIavailableToCommonShareholders+Addbacks}{Weighted Average Shares Outstanding+Conversions}$ \\

Addbacks in the numerator include dividends paid to preferred shareholders and after-tax
interest paid to convertible debt holders.
Diluted shares include convertible instruments and stock option exercises. Assume all
convertible instruments convert to common stock. Stock option exercises require more
calculations.


\chapter{Training procedure}

\textbf{by Bastian Boll} \\

The central problem of training our models lies in the structure of the labels. For each interval, we try to predict a vector in $(0,1)^{1500}$. Each component of the vector is the probability that a specific word is contained in the interval. Naturally, most entries in the vector labels are 0 because most words are not contained in any single given interval. Trying to predict the exact label vector is not a good approach, because for a good acoustic model, many words are similar. Suppose, a given interval contains the single word \emph{we}. A good acoustic model should predict \emph{we} with high probability, but it should also give nonzero probability to \emph{she}, \emph{he}, \emph{me} or other similar sounding words. If we choose a loss function which penalizes these outputs, such as a simple mean squared error loss, it will not work well in practice. In fact, because of this effect, our models resorted to predict near zero probability for every word. We can of course mitigate this problem by interpreting the model as a single- or multiclass classifier for the few words with nonzero probability and use standard loss functions (softmax cross entropy and sigmoid cross entropy respectively). However, there is still potentially some merit in constructing a custom loss function for this specific usecase of predicting multiple classes with given label probability.

\section{Finding a good loss function}

\textbf{by Bastian Boll} \\

As outlined previously, we have multiple possible loss functions to choose from, each corresponding to a particular interpretation of the problem.

\paragraph{Mean squared error} is a very bad idea in practice. The resulting models extract the wrong information from the data: instead of finding the few nonzero probabilities, they set every predicted probability close to zero. Upon closer examination, while all predicted probabilities are close to zero (between 0.001 and 0.003), we can still see that prior proabilities for each word were also somewhat extracted from the data as very frequent words like \emph{the} consistently get higher probabilities assigned to them.

\paragraph{Softmax cross entropy} is the standard loss function for single class classification. In order to apply this to our problem, we need to label each interval with exactly one word. This presents some compromise for two reasons. Because the dataset is not precise, we may actually be providing wrong labels to model during training. This should not be a large problem because the intervals are large enough that this problem is not expected to occur very frequently. The second problem lies in the fact, that we need to throw away available information because one interval can easily contain multiple words.

\paragraph{Sigmoid cross entropy} is the standard loss function for multi class classification. By using multiple labels, we are presented with similar compromises. We now label each interval with multiple words, if the respective word probability is above a given threshold. This mitigates the first problem of losing label information in single class classification.

\paragraph{Custom loss function construction} presents the possibility to try and address both problems with the classification approaches. The main idea of our custom construction is to prioritize the model giving a high probability prediction for the word with the largest label probability in the interval. We measure this as a squared distance between the maximum label probability and the respective predicted probability. We want to also penalize a high average predicted probability. In pratice, the latter amounts to a regularization term which is scaled by a hand optimized factor between $0.3$ and $0.5$.
This still allows for multiple guesses as to which word is most likely, but it incentivises fewer large probabilities.

\chapter{Results}
\label{results}

\textbf{by Bastian Boll} \\

We use tensorflow for GPU accellerated training of our models. Final loss values for every combination of model an loss function are summarized in the following table.
\FloatBarrier
\begin{table}[ht]
	\label{tab:training_results}
	\centering
	\begin{tabular}{ccc}
		\toprule
		Model & Loss function & Final loss\\
		\midrule
		conv\_lstm  & softmax & 6.6850 \\
		conv\_lstm  & custom  & 0.0218 \\
		conv\_lstm  & sigmoid & 0.6922 \\
		deep\_conv  & softmax & 6.7470 \\
		deep\_conv  & custom  & 0.0227 \\
		deep\_conv  & sigmoid & 0.6931 \\
		dense\_conv & softmax & 6.7700 \\
		dense\_conv & custom  & 0.0310 \\
		dense\_conv & sigmoid & 0.7000 \\
		\bottomrule
	\end{tabular}
	\caption{Final loss values obtained in training.}
\end{table}
\FloatBarrier

In order to quantify the prediction result accuracy, we perform the optimization procedure for a talk that was not part of the training set with the predicted probabilities and measure the sum of squared differences between the optimized time points for each word and the respective time points computed from the real subtitle. Doing this for all models and loss functions as well as for optimal labels reveals multiple key insights.

\begin{enumerate}
	\item The optimization process works well for computing time alignment with optimal labels. This is further validated by listening to a demonstration in which the audio is played with each word superimposed at the computed time.
	\item All models helped to increase the accuracy of the alignment from the baseline initial guess (linear interpolation over the whole talk with subtracted silence).
	\item The required optimization time is on the order of minutes and is very inconsistent between different model predictions.
\end{enumerate}

We can also observe, that some predictions lead the optimizer to reach local minima which are not the optimal time points. By construction, this should not occur for very good predictions, but an improvement is still achieved.\\
Listening to the audio, we can see that the initial guess is already close to being sufficient for subtitle generation. The optimizer has a very slow asymptotic conververgence towards the real time points, even for perfect labels.

% However, a high single digit percentage improvement over the initial guess (as measured by the squared distance outlined above) is sufficient for most scenarios.

The following table outlines the results for optimization.

\FloatBarrier
\begin{table}[ht]
	\centering
	\begin{tabular}{ccccc}
		\toprule
		Model & Loss function & Optimizer Steps & Time (sec) & Improvement (\%)\\
		\midrule
		optimal labels &  & 262 & 144.87 & 76.11 \\
		conv\_lstm  & softmax  & 391 & 186.98 & 2.17 \\
		conv\_lstm  & custom   & 105 & 61.56 & 2.73 \\
		conv\_lstm  & sigmoid  & 144 & 83.32 & 6.07 \\
		deep\_conv  & softmax  & 807 & 464.8 & 5.66 \\
		deep\_conv  & custom   & 286 & 167.81 & 7.44 \\
		deep\_conv  & sigmoid  & 19 & 8.42 & 8.92 \\
		dense\_conv & softmax  & 903 & 525.08 & 8.62 \\
		dense\_conv & custom   & 130 & 76.86 & 10.81 \\
		dense\_conv & sigmoid  & 955 & 514.32 & 11.59 \\
		\bottomrule
	\end{tabular}
	\caption{Percentage improvement over initial guess as measured by sum of squared differences to optimal time points.}
\end{table}
\FloatBarrier

% The results show, that the sigmoid cross entropy loss function is best suited to optimize for this particular measure. We can also see that the achieved results do not seem to improve with convolutional network depth. The more shallow convolutional network is able to provide sufficient results for subtitle generation if trained with a sigmoid cross entropy loss function.


\section{Market Microstructure}

Market microstructure is the study of the trading mechanics that are in place
in real markets. It describes why trading occurs, the rules of trading
and the protocols to form trades. \citep[p. 3-4]{Has07}
Understanding market microstructure is crucial in designing an artificial market
as without realistic trading setup the representativeness of the model may end
up to be poor and have limited practicality. The goal of an artificial market often
is to recreate phenomena observed in real markets thus the link to the real world
should be maintained. In this section, the auction theory is briefly introduced and
then relevant distinctions among market microstructures are discussed.
Lastly, the common price phenomena, called stylized facts, are presented. Stylized facts
are often used in validating the artificial models. As the literature on decentralized
markets, as what over-the-counter (OTC) trading is about, is scarce the focus of this thesis is
on centralized markets. Centralized markets have an exchange in which the trading takes place
whereas in decentralized markets the buyers and the sellers seek out each other.

% TODO: Maybe extend with behavioural aspects?

\subsection{Double Auction}
% Call market (Itayose, Sealed-bid) vs CDA (Zaraba)

The theoretical foundation of the microstructure in financial markets lies in double auctions.
Double auction market is a special type of auction in which there
are multiple buyers and sellers which both can
produce quotes and are treated symmetrically \citep*{Kle99}. Buyers
make offers to buy, bid, the traded asset while sellers make offers
to sell, ask, the traded asset \citep*{Moc15}. There are some variations
between double auction markets. They generally differ in when the order matching
process takes place and how the orders are matched.
%In some implementations, such as
%\citet*{God93}'s, a trade is formed only when a buyer and a seller
%each agree on the exact trade price but typically the matching of crossing orders is automated.

% \subsubsection{Order Matching in Double Auction}

Typically, the order matching
is a continuous process occurring after the arrival of each new order but
in some markets this process takes place after a specified time interval
has passed, for example at the end of a trading day. \citep{boer05}
In auction literature, there are various terms used to describe essentially
the same mechanics. \citet{boer05} used term
\textit{continuous session market} for the former and \textit{call market}
for the latter whereas \citet{ASt05} called them \textit{Itayose method}
and \textit{Zaraba method} respectively. \citet{Moc15} used term \textit{continuous
double auction} (CDA) for the former and \textit{periodic double auction} to describe
the same mechanics of call markets as which they described is an extension of
a type of auction called sealed-bid. In sealed-bid auctions, the bid and ask offers
are submitted once and then the order matching is executed once. In this thesis,
the terms \textit{continuous double auction} and \textit{call markets} are chosen to describe
these types of markets as they are the most prevalent in the literature.

In call market, the orders are cumulated for a specified time period and the
market is cleared with a single price whereas in continuous market it is done
whenever there are orders that can be matched, and the price is derived from these
orders. \citep{boer05}
Many real-world stock markets are formed as a hybrid of call and continuous
double auction markets. The price discovery at the beginning and at the end of a
trading day is conducted with call market mechanics and the trading during the
typical trading hours is conducted using a continuous environment \citep{NasdaqClosing05}.
For example, Nasdaq's opening and closing call auctions, called opening and closing crosses,
create the official open and close prices of the market and in between the crosses
trading takes place in a continuous double auction environment \citep{NasdaqCrosses}.
% More about Sealed-bid matching (supply-demand) and CDA matching

\subsection{Execution Systems}
% Quote-driven vs Order-driven
The execution system of a market determines how the orders are matched.
The execution systems are typically divided into two types:
quote-driven and order-driven execution systems. In a quote-driven market, also known
as intermediate markets, the investors trade with prices provided
by dealers or intermediaries who are part of the market organization.
In an order-driven market, also called as limit order market,
the prices are formed according to the orders submitted by the
investors themselves and trades are formed in a centralized marketplace
via automatic order matching or with the help of market makers.
\citep{Baru17} According to \citet{MALINOVA2013104},
the liquidity and price efficiency are generally higher in order-driven
markets. They also found out that smaller orders get more favourable prices in
quote-driven markets due to smaller bid-ask spread
but larger orders benefit from order-driven markets because of the lower
execution costs. Quote-driven markets are also less transparent compared
to order-driven markets. Real markets, however, are most often a combination
of the two \citep{boer05} but there has been a trend to move from quote-driven
markets to order-driven markets \citep{MALINOVA2013104}.

As simulating intermediate market participants increases complexity
of the model, artificial markets with realistic quote-driven execution are rare.
However, some artificial markets, such as \citet{SantaFe99}'s, do have some of
the elements of quote-driven markets in a sense that the prices are driven by
the sizes of the orders and not the prices offered by the investors or traders.
Most of the artificial markets found in the ASM literature are order-driven.
% Challenges of quote-driven (clearing is nontrivial)

\subsection{Price Formation}
% Order book, matching and clearing
% Why we do not go deeper to quote-driven:
Price formation is a direct product of the execution system in place in a market
\citep{boer05}. In order-driven markets, price formation is a direct result of the orders
in the limit order book and the matching algorithm in place whereas in quote-driven markets it
is dependent on how the intermediaries view the current and future
balance between supply and demand of the traded asset. The equilibrium price of a
quote-driven market is driven by the sizes of the orders and in order-driven markets
it is also driven by the prices submitted with the orders. \citep{MALINOVA2013104}
As quote-driven markets' price formation is somewhat a black box due to the
dependency on the views and intentions of the intermediaries, it was not considered
as the execution system for the model built in this thesis. On the other hand,
the price formation of an order-driven market is dependent only on the orders in the
market and the predefined supply-demand matching algorithm in place thus its price
formation can be described with a greater level of generalization.

% Limit order book and terms
The limit order book is a core component in the price formation in an order-driven market.
Limit order book stores the active limit orders which are simply orders that include the quantity and
the limit price stated by the trader. The limit price is the maximum price, in case of a bid, or the minimum
price, in case of an ask, that the order can be cleared with. In other words,
bids (asks) cannot be cleared with a price higher (lower) than the limit price stated
in the order. There are also other types of orders, such as market and
stop-loss orders, but these can be seen as derivatives from limit orders:
for example, a bid (an ask) market order is essentially a limit order
with an extremely high (low) limit price so that it does get matched
during the clearing process as long as there are opposing limit orders in the
order book. \citep{lob13} %TODO Supervisor: Voiko taman esittaa esimerkiksi illustrative diagram muodossa?

% Basics of limit order book, supply and demand etc.
A simple limit order book is presented in ~\ref{tbl:orderbook}. This table shows the
quantity of asset bid and asked per each price level: for example, with a price of 5.00
there are 100 unit bid and 200 unit asked. There are, however, more bids and asks that
accept this price. Those traders that have bid 5.01 and those that have asked 4.99 would obviously also accept
this price. Therefore, it is more informatic to calculate the cumulative sum of the bids and reverse cumulative sum of
the asks quantities and come up with total quantities of bids and asks per price level that can be matched.
This order book with cumulative quantities is visualized in figure ~\ref{fig:lob_visual}. Cumulative bids
are renamed as demand and cumulative asks as supply as these terms are more representative in this context.
As there should not be clearable orders in continuous double auction's order book, except very temporarily,
this example represents more of the state of a limit order book in a call market. Order book
in a continuous double auction is discussed in a moment.

\input{tables/order_book.tex}% table.tex > \mytable
\begin{figure}[H]
    \begin{center}
        \includegraphics{plots/orderbook_visualized.png}
        \caption{Visualized order book}
        \label{fig:lob_visual}
    \end{center}
\end{figure}

% Price formation in sealed bid/call market
Call markets typically clear at equilibrium price which is the point where
supply and demand cross. To get the equilibrium price, first the maximum clearable quantity
is calculated per price level. This quantity is simply the elementwise
minimum from the supply and the demand arrays. Then the quantity in equilibrium
can be determined by taking the maximum from the maximum clearable quantities
per price level and equilibrium price is simply the price where the maximum clearable quantity
equals the equilibrium quantity. Pseudocode of such an algorithm is illustrated in
algorithm ~\ref{alg:lob_equil}. After the equilibrium price and quantity have been determined, the
order matching takes place. The asks are sorted in ascending order using price and bids descending order
and the orders are filled in this order until the sum of filled bids and the sum of filled asks
each equals the equilibrium quantity. It should be noted that not all the bids and the asks with a limit price
equal to the equilibrium price may be filled, as is the case in the example illustrated in figure ~\ref{fig:lob_visual}.
Also, it is possible that supply and demand cross in a range of prices meaning that equilibrium price is
a range of prices. In such a situation it is common to take the mean of these prices and consider that
as the clearing price.

\begin{algorithm}[H]
    \SetAlgoLined
    \DontPrintSemicolon

    bids = sort\_ascending(bids, by price)\;
    asks = sort\_descending(asks, by price)\;
    \;
    demand = cumulative\_sum(bids, using quantity)\;
    supply = cumulative\_sum(asks, using quantity)\;
    \;
    maximum clearable per price = elementwise\_min\_per\_price(demand, supply)\;
    \;
    equilibrium quantity = max(maximum clearable per price)\;
    equilibrium price = price where (maximum clearable per price == equilibrium quantity)\;
    \KwResult{Equilibrium price and quantity}
    \caption{Pseudo algorithm for finding market equilibrium}
    \label{alg:lob_equil}
\end{algorithm}

In a continuous double auction, the matching process is slightly simpler in computation
sense as there is no need to find a common price for many orders. Only the latest arrived
order needs to be cleared with opposing orders or checked whether it can be cleared. A continuous
double auction does not accumulate crossing orders in a similar fashion as call markets do.
In principle, the same algorithm used in a call market could be used also in a continuous double auction
but typically the surplus distribution is different. Unlike in call markets where the surplus is often
split among the crossing orders, in a continuous double auction the surplus goes to the trader who made the
latest order. In other words, the trade prices are drawn from the orders that were already in the limit
order book. This sort of matching could be conducted by looping and clearing the opposing side of the
order book until either there are no more crossing orders with the newly arrived order, or the new
order is already cleared completely. In case of the newly arrived order is not completely cleared after
depleting all possible crossing orders, the order is inserted to the order book and the clearing process
ends. If there were no crossing orders, to begin with, the newly arrived is put to the order book immediately.
This algorithm is illustrated in algorithm ~\ref{alg:lob_cont}. For further reading, an extensive and
thorough overview of the mechanics of limit order book markets is provided by \citet{lob13}. A detailed
explanation about the rules of trading and matching engine of Nasdaq Nordic market model is documented in
\citet{NasdaqModel}.

\begin{algorithm}[H]
    \SetAlgoLined
    \DontPrintSemicolon
    \uIf{new order is bid}{
        asks = get\_asks(order book)\;
        price of new order = get\_price(new order)\;

        \While{(min\_price(asks) $\geq$ price of new order) \\
        \hskip\algorithmicindent\& (get\_quantity(new order) > 0)}{
            crossing orders = filter(asks, where price $\geq$ get\_price(new order))\;
            opposite order = get\_next(crossing orders)\;
            clear(new order, opposite order)\;
        }
    }
    \uElseIf{new order is ask}{
        bids = get\_bids(order book)\;
        price of new order = get\_price(new order)\;

        \While{(min\_price(bids) $\leq$ price of new order) \\
        \hskip\algorithmicindent \& (get\_quantity(new order) > 0)}{
            crossing orders = filter(bids, where price $\leq$ get\_price(new order))\;
            opposite order = get\_next(crossing orders)\;
            clear(new order, opposite order)\;
        }
    }
    \uIf{get\_quantity(new order) > 0}{
        insert\_to\_orderbook(new order)\;
    }

    \caption{Pseudo algorithm for clearing continuous double auction}
    \label{alg:lob_cont}
\end{algorithm}

\subsection{Price Dynamics}
% Stylized facts
The price dynamics in stock markets are known to be chaotic and difficult to
analyze. The underlying elements that play a role in the price dynamics
are constantly evolving and they remain relatively poorly understood.
However, there are some characteristics of price behaviour that are supported
with enough empirical evidence to be considered as properties of price in
financial markets. These statistical phenomena, called stylized facts in
econometrics, have been observed in extensive amount of studies across
different assets, markets and time periods \citep{Shakeel18}.
% List of Stylized facts (Cont R. (2001) & Gould et al. (2013))
According to \citet{StylizedFacts01} and \citet{lob13}, price related stylized
facts include:

\begin{enumerate}
    \item Fat-tailed distribution of returns: distribution of asset returns has fatter tails than normal distribution meaning that they have positive excess kurtosis.
          In the figure  ~\ref{fig:sp_fat_tails} distributions of daily rate of returns of S\&P 500 returns are shown in a ten year period.
    \par
    \begin{minipage}{\linewidth}
        \centering
        \includegraphics[width=10cm]{plots/S&P500_fat_tails.png}
        \captionof{figure}{Fat-tailed of returns of S\&P 500 from 1.1.2010 to 1.1.2020}
        \label{fig:sp_fat_tails}
    \end{minipage}
    \item Lack of autocorrelation: autocorrelation of returns in financial markets have been shown to be statistically insignificant, except in the very short term.
          Previous returns have no predictive power over the future returns of a stock.
          Figure ~\ref{fig:sp_autocorr} illustrates the autocorrelation of daily returns of S\&P 500 in a ten year period.
          As seen in the figure, the autocorrelations of all of the lags lie inside the blue area meaning that all of the lags
          are not statistically significant from zero.
    \par
    \begin{minipage}{\linewidth}
        \centering
        \includegraphics[width=10cm]{plots/S&P500_autocorr.png}
        \captionof{figure}{Autocorrelation in S\&P 500 from 1.1.2010 to 1.1.2020}
        \label{fig:sp_autocorr}
    \end{minipage}
    \item Volatility clusters: volatility has been measured to have positive autocorrelation.
          Additional exceptional price movements tend to follow big price movements forming clusters of volatility throughout time.
          As can be observed from the figure ~\ref{fig:sp_volaclusters}, there are volatility clusters for up to ten weeks in
          S\&P 500 as the autocorrelation of volatility is positive up to that point.
          The figure illustrates the autocorrelation of weekly daily returns in S\&P 500.
    \par
    \begin{minipage}{\linewidth}
        \centering
        \includegraphics[width=10cm]{plots/S&P500_vola_autocorr.png}
        \captionof{figure}{Volatility clustering in S\&P 500 from 1.1.2010 to 1.1.2020}
        \label{fig:sp_volaclusters}
    \end{minipage}
\end{enumerate}

% TODO Supervisor: add either one subplot of the previous or add more description to the title (10 year period etc.)

% Something about theories of these things (explanation for the stylized facts)
% file:///D:/Opinnot/Master's%20Thesis/literature/real%20markets/price%20dynamics/TheoriesExplainingStockPriceBehavior%20(1).pdf

All of the figures above utilize daily adjusted close prices for S\&P 500 stock index.
As has been done in the literature of artificial markets, the
representativeness of the ASM model built in this thesis is validated also
using these stylized facts.


\section{Audio Transmission Measurement}
\label{sect:Trans}
This second major category of AVNA measurements consists of generating a sine wave and applying this to a network. The network must have at least two sets of connections that act as the input and output. The transmission measurement determines the complex input and output voltages and then computes the ratio of the voltage magnitudes along with the difference in phase.
%
\subsection{Description}
\label{subsect:TDescr}
\textbf{Complex Transmission - }The concept of voltage gain  is common.
This is directly applied here when we take the ratio of the magnitude of the output voltage to that of the input voltage. That measure does not involve the phase of the signals and is the same as measuring the input and output voltages of the network terminals and dividing the result with a calculator.

The "complex" or "vector" part of a gain measurement results from making phase measurements in addition to magnitude measurements. With the AVNA, we determine how much phase difference there is between the input and output sine waves.
This can be important for equipment such as  filters, control systems, or communications networks. In part, it also allows a complete mathematical model of linear circuits to be constructed.  This is a valuable tool for design of circuits.

It is worth noting that some of the common Radio Frequency concepts such as scattering parameters and reflection coefficients are also applicable at low frequencies.  Complex transmission (and impedance) measurements support these descriptors.  We won't attempt to cover all the possibilities here, but it would be an interesting topic for someone to explore.

\subsection{Instructions}
\label{subsect:TInstr}
\textbf{Single Frequency Measurements - }This procedure parallels the impedance measurements of the previous section. We will cover the essence of a transmission measurement using the touch screen. Everything covered here, and more, can be done via the USB-Serial link, as is described in the "AVNA1 Serial Interface" section.

When the AVNA1 is powered up, as shown in  Figure \ref{AVNA_000-label},  you have a choice of four Audio Test Instruments along with Service and Calibration functions.  As was the case for impedance measurements, we again select "AVNA" as our instrument.  The next screen, Figure  \ref{AVNA_001-label}, applies to both impedance and transmission measurements and allows us to select single or swept frequency measurements as well as to change the source resistance between 50 and 5000 Ohms.  First, check if the reference resistance is set to 50 Ohms and that the input resistance switch has the 50-Ohm termination resistor on.  This sets us up for a common 50-Ohm in and out transmission measurement.

To continue with the sample transmission measurement, we will select "\textsf{Single Freq}".  At this point we select the frequency of measurement using the "\textsf{Freq Down}" and "\textsf{Freq Up}"  buttons.  We will suppose our frequency of interest is 5000 Hz and we will use the buttons to set that.  Now, tap on "\textsf{Meas T}" to be ready for transmission measurements.   At this point, the AVNA will warn us that a Cal is needed as seen on the next screen, Figure  \ref{AVNA_043-label}.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.75]{./images/AVNA_043.pdf}
\caption{AVNA Transmission showing need for calibration.}
\label{AVNA_043-label}
\end{center}
\end{figure}
%
We will proceed to Cal by placing a wire between the hot \q{Z} and \q{T} terminals and tapping "\textsf{Cal}" at the bottom.   There is a brief delay, a brief message and then the almost blank screen seen here.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.75]{./images/AVNA_044.pdf}
\caption{Transmission measurement after calibration.}
\label{AVNA_044-label}
\end{center}
\end{figure}
%
This means the transmission path is calibrated at this one frequency and ready to use.  Before removing the calibration wire, tap on the "\textsf{Meas T}" button to confirm that the Gain is 0.0 dB and the phase is 0.0 degrees for the direct connection.  The screen should look very much like the following, Figure \ref{AVNA_033-label}; if not, the problem should be tracked down.  We can redo the "\textsf{Cal}" if needed.  Otherwise, it is time to connect up a circuit and measure it.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.75]{./images/AVNA_033.pdf}
\caption{Single frequency Transmission measurement with the calibration jumper still in place.}
\label{AVNA_033-label}
\end{center}
\end{figure}
%
As an example, I used a series combination of a 10 Ohm resistor and a 0.22 $\mu$F capacitor.  This time, for a transmission measurement, the two parts will be connected between the hot \q{Z} and \q{T} terminals. This replaces the wire used for calibration.  This is not a particularly interesting circuit to test, but it illustrates the measurement.  For single frequency measurements, the screen will update about once a second.  This is useful for testing multiple components, or when making adjustments.  Here is the result of my measurement for this RC network.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.75]{./images/AVNA_047.pdf}
\caption{AVNA Transmission measurement for a single frequency.}
\label{AVNA_047-label}
\end{center}
\end{figure}
%
For the measurements just made, the source and termination impedances were both 50 Ohms. But, we have several other impedance options available that can be useful.  We can use the BNC connector instead of the terminals, the source impedance will be much lower, typically less than an Ohm.  Also if we move the switch to not use the 50 Ohm input termination, the measuring input impedance will be 1.0 megohm in parallel with about 25 pF.  This is a common setup for low frequency testing,  It is also compatible with many oscilloscope probes.  Remember that if we change any of the source or termination impedances, we need to do a new calibration.  Also, changing either of these two reference resistances will change the transmission measurement values, sometimes by a large amount.

\textbf{Swept Frequency Measurements - } As we did for impedance measurements, we can utilize a built-in set of frequencies to measure transmission amplitude and phase.  The same 13 frequencies are available covering 10 to 40,000 Hz.  Under Serial Control, this same measurement can be made at an arbitrary set of frequencies (see "AVNA1 Serial Interface" for details.).

The procedure parallels that of Single Frequency measurements, and so we won't repeat every detail.  Please look back to the previous section of Single Frequency Measurements for additional information.  We start from the main AVNA screen, Figure \ref{AVNA_001-label}, where we can again select the source resistance and then select "\textsf{Sweep}".  At his point we can choose "\textsf{Meas T}" from the bottom menus.  Again, we will have a bold message reminding us to do a "\textsf{Cal}".  You connect a wire or short test clip between the hot terminals and tap on "\textsf{Cal}".  After  about 10 seconds the middle of the screen should be blank. Then the circuit to be measured should be connected in place of the calibration wire.  Tapping on "\textsf{Single T Sweep}" and waiting about 8 seconds should produce a similar screen to Figure  \ref{AVNA_037-label} seen here.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.75]{./images/AVNA_037.pdf}
\caption{AVNA Swept frequency Transmission measurement screen.}
\label{AVNA_037-label}
\end{center}
\end{figure}
%
All 13 measurements have been made, but only 7 are displayed on the screen.  Use the bottom buttons, "\textsf{Disp Frq Up}" and "\textsf{Disp Frq Down}" to display the range of interest.  When you change the circuit and want to re- sweep, again tap "\textsf{Single T Sweep}" and wait a few seconds for the new results.  You can also go back to re calibrate at any time by disconnecting the network and connecting a through jumper wire and tapping "\textsf{Cal}".

%
\subsection{Discussion}
\label{subsect:TDiscus}
The calibration procedure measures the voltage magnitude and phase with the signal source connected to the voltmeter.  This is saved and used for subsequent  circuit transmission measurements.  For those, the ratio of the new voltage magnitude to the calibrate magnitude is used to find the relative transmission magnitude.  Similarly, the calibrate phase is subtracted from the new measurement phase.  (If the calibrate jumper wire is still in place, this gives a relative transmission magnitude of 1.000 and a measurement phase of 0.000 degrees.)

Averaging is built into these measurements to decrease the effects of noise.  This increases the accuracy at higher amounts of attenuation.  It is inherent that lower frequencies take longer amounts of time to average.  For our lowest frequency of 10 Hz, each cycle requires 0.1 seconds.  The averaging algorithm used requires integral numbers of cycles, and the time adds up.

\textbf{Transmission Delay - }In future revisions there will be a formal measurement of group delay added to the AVNA.  Meanwhile, if the delay measurement is desired, this can created externally by using a pair of phase measurements at two nearby frequencies.  The group delay is found from these by
\begin{equation}
\tau=(-1/360)(\phi_1-\phi_2)/(f_1-f_2)
\end{equation}
where $\phi_1$ and $\phi_2$ are the two phase measurements in degrees, and $f_1$ and $f_2$ are the two frequencies in Hz.  The resulting delay $\tau$ is in seconds.


% To compile single chapters put a % symbol in front of "\comment" and "}%end of comment" below
%    and take off the % symbol from "\end{document" at the bottom line. Undo before compiling
%    the complete thesis
%%Note: You can only use \section command, you are not allowed, per TTU Graduate School, use
%%\subsection command for ghigher level subheadings. At most level 2 subheadings are allowed.

\chapter{Experiments and Results}
\label{Experiments and Results}

\section[Simulation Overview]{Testing Overview}

Two machine learning approaches are utilized in Chapter \ref{DASP Device Classification Chapter} to determine the performance and applicability of DASP originated features for URE characterization and classification.  A feature vector approach using LDA and k-NN learning algorithms was applied to the 1-D statistical feature vectors derived from DASP images, as described in Section \ref{Statistical Feature Extraction}, while a CNN deep learning algorithm was applied directly to the DASP images to perform image recognition based learning. Figure \ref{fig:dasp_lda_knn_process_flow} provides a detailed diagram of the DASP feature vector processing flow from URE signal capture to the LDA and k-NN learning algorithms.

\begin{figure}[tb]
	\includegraphics[width=\textwidth]{./misc_graphics/dasp_lda_knn_process_flow.jpg}
	\centering
	\caption{Flow diagram of the DASP processing flow from URE signal capture through the LDA and k-NN learning process.}
	\label{fig:dasp_lda_knn_process_flow}
\end{figure}

Figure \ref{fig:dasp_lda_knn_process_flow} outlines the DASP processing flow to generate feature vectors for LDA and k-NN testing and training.   All URE signal captures were initially processed through the DASP transforms with up to four images extracted from each DASP transform, the raw scaled image, the scatter peak detector image, the LoG edge detector image, and the radon line detector image.  Statistical feature vectors were then extracted from the DASP images by first segmenting the image and then performing row and column summing to obtain statistical sample vectors.  The statistical measurements were then concatenated in to a 1-D feature vector per sample and normalized by their z-score across all samples.  The normalized feature vectors were evaluated through the LDA and k-NN learning processes.

Figure \ref{fig:dasp_cnn_process_flow} provides an overview of the DASP processing flow for the CNN learning process.  Instead of image segmentation and feature vector extraction, the CNN learner only requires an image input for learning.  Similar to the feature vector processing flow, the CNN process generates up to four images for each DASP transform, the raw scaled image, the scatter peak detector image, the LoG edge detector image, and the radon line detector image.  The resulting DASP images were saved as individual Tag Image File Format (TIFF) files for cataloging, labeling, training, and testing with the CNN learner.

\begin{figure}[tb]
	\includegraphics[width=\textwidth]{./misc_graphics/dasp_cnn_process_flow.jpg}
	\centering
	\caption{Flow diagram of the DASP processing flow from URE signal capture through the CNN learning process.}
	\label{fig:dasp_cnn_process_flow}
\end{figure}

\section[DASP Processes and Transforms]{DASP Processes and Transforms}

Because no \emph{a priori} knowledge of the test devices' URE characteristics were taken in to account in the DASP evaluation process, the DASP algorithms, transforms, and parameter settings were selected heuristically to include as much of the URE spectrum that could be reasonably achieved given a $2$MS/s collection system and a one second capture time per device sample.  Table \ref{tab:dasp_config_parameters} provides a listing of the DASP algorithms and transforms used in the feature and image generation processes, along with their respective parameter settings.  All DASP algorithms were evaluated, including the SCAP auto-covariance vector, $\bf{S}_F$, while it should also be noted that not all combinations of DASP algorithms and image processing transforms were utilized.  In addition, the SCAP auto-covariance vector was not utilized by the CNN learner because it does not result in a 2-D image.

\begin{table}[tb]
	\caption{Table of all DASP algorithms and image transforms used for image generation and feature extraction.}
	\centering
		\begin{tabular}{cc|c}
		\hline
		DASP Algorithm & Image Transform & Configuration Parameters\\
		\hline
    CMASP & Array & $f_c = 10000$\\
		CMASP & Edge Array & $Bc = 9500$ \\
		CMASP & Radon Array & $f_m = 1000$ \\
		& & $B_m = 950$ \\
		\hline
		FASP & Array & $N = 1000$ \\
		\hline
		HASP-F & Array & $f_c = 1000$ \\
		HASP-F & Edge Array & $B = 1990$ \\
		HASP-F & Radon Array & \\
		\hline
		HASP-D & Array & $f_c = 1000$ \\
		& & $B = 1990$ \\
		\hline
		MASP (Low) & Array & $N = 10000$\\
		MASP (Low) & Edge Array & $p = 0.02$ \\
		MASP (Low) & Radon Array & \\
		MASP (Low) & Scatter Plot &\\
		\hline
		MASP (High) & Array & $N = 1000$ \\
		MASP (High) & Edge Array & $p = 0.02$ \\
		MASP (High) & Radon Array & \\
		MASP (High) & Scatter Plot & \\
		\hline
		SCAP & Array & $f_c = 1000$ \\
		SCAP & Cross-Covariance Vector & $B = 1990$ \\
    \hline
		\end{tabular}
	\label{tab:dasp_config_parameters}
\end{table}

Table \ref{tab:dasp_config_parameters} provides the configuration parameters for each DASP algorithm, based upon their specific input requirements.  Two sets of MASP images were generated with different settings to characterize low and high frequency modulations, with the higher frequency MASP algorithm trading off modulation frequency resolution.  The Edge and Radon Arrays were not applied to the FASP, HASP-D, and SCAP images because the additional benefit was not obvious given the multitude of curved lines present in the HASP-D and SCAP images and the large number of vertical lines in a typical FASP image.

\section[Statistical Feature Extraction]{Statistical Feature Extraction}

Statistical features were extracted from the whole and $3 \times 3$ segmented DASP images in Table \ref{tab:dasp_config_parameters} using the whole image, the summed row vector, and the summed column vector.  The extracted features for each image segment were concatenated in to a single feature vector
\begin{equation}
	\textit{\textbf{X}}_s= [\sigma^{2}_{\omega},  \gamma_{\omega},  \kappa_{\omega}, \sigma^{2}_{\rho},  \gamma_{\rho},  \kappa_{\rho}, \sigma^{2}_{\gamma},  \gamma_{\gamma},  \kappa_{\gamma}]
	\label{eq:featureeq}
\end{equation}
where $\omega$ are the whole segment features, $\rho$ are summed row features, and $\gamma$ are summed column features, resulting in $9$ features for each of the DASP image segments.  The SCAP auto-covariance vector only provides a single vector for feature extraction and therefore only yielded three features, $[\sigma^{2},  \gamma,  \kappa]$.  Finally, the z-score for each feature was calculated across all data sets resulting in a normalized feature set that was utilized for subsequent training and classification analysis.  The image segment feature vectors, $\textit{\textbf{X}}_s$, were then concatenated to form a full feature vector
\begin{equation}
	\textit{\textbf{X}}= [\textit{\textbf{X}}_1, \textit{\textbf{X}}_2, \ldots, \textit{\textbf{X}}_9, \textit{\textbf{X}}_{10}]
	\label{eq:featureeq_cat}
\end{equation}
for each DASP image, where $\textit{\textbf{X}}_1$ is the top image segment and $\textit{\textbf{X}}_2$ through $\textit{\textbf{X}}_{10}$ are the $9$ image segments comprised of the $3 \times 3$ grid segmentation.  The concatenation of the image segment feature vectors resulted in a total of $9 \times 10 = 90$ features per DASP image, except for the SCAP auto-covariance feature vector which only returned three features.

\section[DASP Image Store]{DASP Image Store}

The MATLAB\textsuperscript \textregistered ~~Convolutional Neural Network implementation, including supporting tools such as the \textit{imageDataStore} command, was designed to seamlessly catalog, label, train, and test on image files.  To simplify the CNN learning process, each DASP image was saved as a \textit{.tif} file using the process in Algorithm \ref{alg:tiffimalg}.  The TIFF image generation algorithm is identical to the image scaling algorithm, Algorithm \ref{alg:imscalealg}, with the addition of the $16$bit scaling, image resizing, and file saving steps.  Each TIFF file was stored in its own directory structure within a labeled folder for class indexing.

\begin{algorithm}
	\caption{TIFF Image Generation Algorithm} \label{alg:tiffimalg}
	\scriptsize
	\begin{algorithmic}[1]
		\Require~~
		\Statex $\mathbf{I}$ - Input Image
		\Statex $\mathbf{I_b}$ - Background Image
		\Ensure~~
		\Statex $\mathbf{I_s}$ - Saved TIFF Image
		\Statex
		\State $\mathbf{I_s} \gets \mathbf{I} - \mathbf{I_b}$
		\State $\mathbf{I_s} \leq 0 \gets 0$
		\State $\mathbf{I_s}  \gets $ STANDARD DEVIATION FILTER of $\mathbf{I_s}$
		\State $\mathbf{I_s} \gets \log{\mathbf{I_s}}$
		\State $\mathbf{I_s} \gets 2^{16} \times \frac{\mathbf{I_s} - \min{\mathbf{I_s}}}{\max{\mathbf{I_s}} - \min{\mathbf{I_s}}}$
		\State RESIZE $\mathbf{I_s}$ to $500 \times 500$
		\State SAVE $\mathbf{I_s}$ as $16$bit TIFF Image file
	\end{algorithmic}
\end{algorithm}


In this section, we evaluate our framework using different configurations of the
illustrative application. All the experiments are conducted on a \up{GNU}/Linux
machine equipped with an Intel Core i7 2.66~\up{GH}z and 8~\up{GB} of \up{RAM}.
All the configuration files used in the experiments are available online at
\cite{eslab2014b}.

Let us first elaborate on the default configuration of our setup, which is
subsequently adjusted according to the purpose of each particular experiment. We
consider a 45-nm technological process. The effective channel length is assumed
to have a nominal value of 17.5~nm \cite{ptm} and a standard deviation of
2.25~nm where the global and local variations are equally weighted. The
correlation matrix of the uncertain parameters is computed based on
\eref{bayes-correlation} where the length-scale parameters $\ell_\SE$ and
$\ell_\OU$ are set to half the size of the square die. In the model order
reduction described in \xref{probability-transformation}, the threshold
parameter $\eta$ is set to 0.99, which preserves 99\% of the variance of the
data. The floorplan of the platform is constructed in such a way that the
processing elements form a regular grid. The dynamic power profiles involved in
the experiments are based on simulations of applications that are randomly
generated via \up{TGFF} \cite{dick1998}. The time step \dt of power and
temperature profiles is set to 1~ms, which is also the time step of the
recurrence in \eref{chaos-recurrence}.

The construction of \ac{PC} expansions and integration grids follows the
exposition given in \sref{chaos-construction}. The tuning parameter $\gamma$ in
\eref{chaos-anisotropic-weight} is set to zero, which turns the anisotropic
index set in \eref{index-total-order-anisotropic} into the isotropic one in
\eref{index-total-order-isotropic}. Anisotropy will be exploited in a later
section.

In the following, we focus on the assessment of temperature profiles. Note,
however, that the results for temperature allow one to draw reasonable
conclusions about the performance of the proposed framework with respect to
power, since power is an intermediate step toward temperature, and any accuracy
problems with respect to power are expected to propagate to temperature.

Additionally, it is worth noting that, since the temperature-driven studies
discussed in \sref{chaos-past}, namely \cite{huang2009a, juan2011, juan2012,
lee2013}, work under the static steady-state assumption (the work in
\cite{juan2011} is also limited to the maximum temperature, and the one in
\cite{huang2009a} does not model the power-temperature interplay), a one-to-one
comparison with the proposed technique is not possible.

For purposes of comparison, we employ \ac{MC} sampling, which is introduced in
\sref{past}. This approach samples the quantity of interest directly, meaning
that there is no intermediate representation involved in these calculations.
There is no model order reduction applied prior to direct sampling, which
preserves the whole variance of the problem at hand, and the system in
\eref{temperature-model-original} is solved using traditional techniques, namely
the fourth- and fifth-order Runge--Kutta formulae (the Dormand--Prince method)
\cite{press2007}.

\subsection{Approximation Accuracy}

The first set of experiments aims to investigate the accuracy of our framework
with respect to direct sampling. At this point, it is important to realize that
the true distributions of temperature are unknown, and both the \ac{PC} and
\ac{MC} approaches introduce errors. These errors decrease as the level \lc of
\ac{PC} expansions and the number of samples \no of \ac{MC} sampling increase.
Hence, instead of postulating that the \ac{MC} technique with a certain number
of samples is the solution that we should achieve, we vary both \lc and \no and
monitor the corresponding difference between the results produced by the two
alternatives.

We also study the impact of the correlation structure between the local random
variables $\set{\u_{\local, i}}_{i = 1}^\np$; recall
\sref{chaos-transient-application}. More specifically, apart from \lc and \no,
we change the balance between the two correlation functions shown in
\eref{bayes-correlation}, that is, the squared-exponential kernel $k_\SE$ and
the Ornstein--Uhlenbeck kernel $k_\OU$, which is controlled by the weight
coefficient $w \in [0, 1]$.

The \ac{PC} and \ac{MC} methods are compared using three error metrics. The
first two are the \acp{NRMSE} of the expectation and variance of temperature
profiles. The third metric is the mean of the \acp{NRMSE} of the empirical
\acp{PDF} of temperature constructed for all processing elements at all time
steps. The metrics are denoted by \error{\expectation}, \error{\variance}, and
\error{f}, respectively. The first two are straightforward to interpret, and
they are calculated using the analytical expressions in \eref{chaos-moments}.
The third one is a strong indicator of the quality of the distributions
estimated by our framework, and it is computed by sampling the constructed
\ac{PC} expansions. In contrast to direct sampling, this sampling incurs
negligible overhead, as noted in \sref{chaos-processing}.

\inputtable{chaos-transient-accuracy}
The studied values for \lc, \no, and $w$ are the sets $\set{i}_{i = 1}^7$,
$\set{10^i}_{i = 2}^5$, and $\set{0, 0.5, 1}$, respectively. The three variants
of $w$ correspond to the total dominance of $k_\OU$ ($w = 0$), perfect balance
between $k_\SE$ and $k_\OU$ ($w = 0.5$), and total dominance of $k_\SE$ ($w =
1$). A comparison for a quad-core architecture with a dynamic power profile of
$\ns = 10^2$ steps is given in \tref{chaos-transient-accuracy-0},
\tref{chaos-transient-accuracy-0-5}, and \tref{chaos-transient-accuracy-1},
which correspond to $w = 0$, $w = 0.5$, and $w = 1$, respectively. Each table
contains three subtables: the left one is for \error{\expectation}, the middle
one for \error{\variance}, and the right one for \error{f}, which results in
nine subtables in total.

The columns of the tables that correspond to high values of \no can be used to
assess the accuracy of the constructed \ac{PC} expansions; likewise, the rows
that correspond to high values of \lc can be used to gauge the sufficiency of
the number of \ac{MC} samples. One can immediately note that, in all the
subtables, all the error metrics tend to decrease from the top-left corners (low
values of \lc and \no) to the bottom-right corners (high values of \lc and \no),
which suggests that the \ac{PC} and \ac{MC} methods converge. There are a few
outliers associated with low expansion levels and the random nature of sampling.
For instance, \error{\variance} increases from 66.13 to 66.7 and \error{f} from
1.59 to 1.62 when \no makes a transition from \power{10}{4} to \power{10}{5} in
\tref{chaos-transient-accuracy-0-5}. However, the main trend is still clear.

For clarity of the discussions below, we focus primarily on one of the three
tables, namely \tref{chaos-transient-accuracy-0-5}. The conclusions drawn with
respect to \tref{chaos-transient-accuracy-0-5} will be generalized to the other
two tables at the end of this subsection.

\inputfigure{chaos-application-density}
First, we assess the accuracy of our technique and thus pay particular attention
to the columns of \tref{chaos-transient-accuracy-0-5} corresponding to high
values of \no. It can be seen that the error of the expectation is small even
when $\lc = 1$. Concretely, it is bounded by 0.6\%; see \error{\expectation} for
$\lc \geq 1$ and $\no \geq 10^4$. The error of the variance starts at 66.7\% for
the first-level \ac{PC} expansions and drops significantly to 5.71\% and below
for the fourth level and higher; see \error{\variance} for $\lc \geq 4$ and $\no
= 10^5$. The error of the \ac{PDF} allows us to conclude that the \acp{PDF}
computed by means of \ac{PC} expansions starting from the third level closely
follow those estimated by the \ac{MC} technique with a large number of samples.
The observed difference in \tref{chaos-transient-accuracy-0-5} is bounded by
1.83\%; see \error{f} for $\lc \geq 3$ and $\no \geq 10^4$.

In order to give a better sense of the proximity of the two methods,
\fref{chaos-application-density} shows the \acp{PDF} computed using our
framework with $\lc = 4$ (the solid lines) along with those calculated by the
\ac{MC} approach with $\no = 10^4$ (the dashed lines) at time 50~ms. It can be
seen that the \acp{PDF} closely match each other. Note that this example
concerns one particular time step. Such curves are readily available for the
other steps of the analyzed time span as well.

Next, we investigate the convergence of the \ac{MC} technique and consequently
watch the rows of \tref{chaos-transient-accuracy-0-5} that correspond to \ac{PC}
expansions of high levels. Similarly to the previous observations, even for low
values of \no, the error of the expectation estimated by direct sampling is
relatively small. Specifically, this error is bounded by 1.19\%; see
\error{\expectation} for $\lc \geq 4$ and $\no = 10^2$. At the same time, the
case with $\no = 10^2$ has high error rates in terms of the variance and
\ac{PDF}: they are above 38\% and around 4\%, respectively; see
\error{\variance} and \error{f} for $\lc \geq 4$ and $\no = 10^2$. The results
in the cases with $\no \geq 10^3$ are reasonably more accurate, and the one with
$\no = 10^4$ appears to be sufficiently adequate.

The above conclusions drawn with respect to the results reported in
\tref{chaos-transient-accuracy-0-5} ($w = 0.5$) are directly applicable to those
reported in \tref{chaos-transient-accuracy-0} ($w = 0$) and
\tref{chaos-transient-accuracy-1} ($w = 1$). The only difference is that the
average error rates are lower when either of the two correlation kernels shown
in \eref{bayes-correlation} dominates. In particular, according to
\error{\variance}, the case with $w = 1$, which corresponds to $k_\SE$ and is
reported in \tref{chaos-transient-accuracy-1}, stands out as the least
challenging.

Guided by the above observations, we conclude that our framework delivers
sufficiently accurate results starting from $\lc = 4$, and that the \ac{MC}
estimates can be considered to be sufficiently reliable starting from $\no =
10^4$.

\subsection{Computational Speed}

The second set of experiments is to measure the speed of our framework with
respect to direct sampling. To this end, we keep \lc and \no fixed and equal to
4 and \power{10}{4}, respectively. The latter value is also similar to those
used in the literature \cite{ghanta2006, bhardwaj2008, huang2009a, shen2009,
xiang2010, juan2012, lee2013} and consistent with the theoretical results on the
accuracy of \ac{MC} sampling presented in \cite{diaz-emparanza2002}. Analogously
to the previous subsection, we report the results obtained for different values
of the weight coefficient $w$, which impacts the number of the independent
variables $\vz: \Omega \to \real^\nz$ preserved after the model order reduction
described in \xref{probability-transformation}.

\inputtable{chaos-transient-speed}
First, we vary the number of processing elements \np, which directly affects the
dimensionality of the uncertain parameters $\vu: \Omega \to \real^\nu$; recall
\sref{chaos-transient-application}. The results, including the dimensionality
\nz of \vz, are given in \tref{chaos-transient-speed-elements} where $\np \in
\set{2^i}_{i = 1}^5$ and $\ns = 10^3$. It can be seen that the variation
patterns inherent in the fabrication process \cite{cheng2011} offer significant
potential for model order reduction: \nz is observed to be at most 12, whereas
the maximum number without reduction is 33 (1 global variable and 32 local ones
corresponding to the case with $\np = 32$). The amount of reduction also depends
on the floorplan, which is illustrated by the decrease in \nz when \np increases
from 16 to 32 for $w = 1$. To elaborate, one floorplan is a four-by-four grid, a
square, while the other an eight-by-four grid, a rectangle. Since both are
fitted into square dies, the former is spread across the whole area, whereas the
latter is concentrated along the middle line; the rest is due to the
peculiarities of $k_\SE$.

On average, the $k_\OU$ kernel ($w = 0$) requires the smallest number of
variables, while the combination of the $k_\SE$ and $k_\OU$ kernels ($w = 0.5$)
requires the largest. This means that, in the latter case, more variables should
be preserved in order to retain 99\% of the variance. Consequently, the case
with $w = 0.5$ is the most demanding in terms of computation time. Note that,
since the results reported in the previous subsection correspond to the case
with $\np = 4$, \nz is two, five, and five in \tref{chaos-transient-accuracy-0},
\tref{chaos-transient-accuracy-0-5}, and \tref{chaos-transient-accuracy-1},
respectively.

At this point, it is important to realize the following. First, since the curse
of dimensionality arguably constitutes the major concern of \ac{PC} expansions,
the applicability of our framework depends primarily on how this curse manifests
itself with regard to the problem at hand, that is, on the dimensionality \nz of
\vz. Second, since \vz is a result of a procedure that depends on many factors,
the relationship between \vu and \vz is not straightforward, which is also
illustrated in the previous paragraphs. Thus, \nu can be misleading when
reasoning about the applicability of our technique; \nz is well suited for this
purpose.

Another observation with respect to \tref{chaos-transient-speed-elements} is the
shallow slope of the execution time of the \ac{MC} technique, which illustrates
the well-known fact that the workload per sample is independent of the number of
stochastic dimensions. On the other hand, the rows with $\nz \geq 10$ hint at
the curse of dimensionality, which \ac{PC} expansions suffer from. However, even
in high dimensions, the proposed framework significantly outperforms direct
sampling. For instance, in order to analyze a dynamic power profile with
\power{10}{3} steps of a platform with 32 processing elements, the \ac{MC}
approach requires more than 40 hours, whereas our framework takes less than two
minutes; see the case with $w = 0.5$.

Next, we investigate the scaling properties of the proposed framework with
respect to the duration of the analyzed time span, which is directly
proportional to the number of time steps \ns covered by power and temperature
profiles. The results for a quad-core platform are given in
\tref{chaos-transient-speed-steps}. Due to the long execution times demonstrated
by the \ac{MC} approach, its statistics for high values of \ns are extrapolated
based on a smaller number of samples $\no < 10^4$. Similarly to
\tref{chaos-transient-speed-elements}, we observe a certain dependency of the
constructed expansions on the dimensionality \nz, which is two for $w = 0$ and
five for $w = 0.5$ and $w = 1$; see \tref{chaos-transient-speed-elements} for
$\np = 4$. It can be seen in \tref{chaos-transient-speed-steps} that the
computation times of both techniques grow linearly with respect to \ns, which is
expected. However, our framework displays a superior performance, being up to
five orders of magnitude faster than the \ac{MC} alternative.

It is worth noting that the observed speedups are due to two major factors.
First, \ac{PC} expansions are generally superior to \ac{MC} sampling when the
curse of dimensionality is suppressed \cite{eldred2008, xiu2010}, which we
accomplish by model order reduction and efficient integration schemes. The
second reason is the particular technique used in the framework for solving the
temperature model and constructing \ac{PC} expansions in a stepwise manner shown
in \eref{chaos-recurrence}.


\section{\src{dyn_divdamp}}

\subsection{Description}

Kernel \src{dyn_divdamp} is taken from the original subroutine
\src{OPRT3D_divdamp} in \NICAM.
%
This subroutine is originally defined, as you can see in that name, in
\src{mod_oprt3d}.
%
Subroutine \src{OPRT3D_divdamp} calculates the gradient of divergence of
the vector $\{v_x, v_y, v_z\}$.
%
If this vector represents the velocity vector, this term is called
``divergence damping term''.

\subsection{Discretization and code}

Argument lists and local variables definition part of this subroutine is
as follows.

\begin{LstF90}[name=divdamp]
subroutine OPRT3D_divdamp( &
     ddivdx,    ddivdx_pl,    &
     ddivdy,    ddivdy_pl,    &
     ddivdz,    ddivdz_pl,    &
     rhogvx,    rhogvx_pl,    &
     rhogvy,    rhogvy_pl,    &
     rhogvz,    rhogvz_pl,    &
     rhogw,     rhogw_pl,     &
     coef_intp, coef_intp_pl, &
     coef_diff, coef_diff_pl  )
  implicit none

  real(RP), intent(out) :: ddivdx      (ADM_gall   ,ADM_kall,ADM_lall   ) ! tendency
  real(RP), intent(out) :: ddivdx_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(out) :: ddivdy      (ADM_gall   ,ADM_kall,ADM_lall   )
  real(RP), intent(out) :: ddivdy_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(out) :: ddivdz      (ADM_gall   ,ADM_kall,ADM_lall   )
  real(RP), intent(out) :: ddivdz_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(in)  :: rhogvx      (ADM_gall   ,ADM_kall,ADM_lall   ) ! rho*vx { gam2 x G^1/2 }
  real(RP), intent(in)  :: rhogvx_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(in)  :: rhogvy      (ADM_gall   ,ADM_kall,ADM_lall   ) ! rho*vy { gam2 x G^1/2 }
  real(RP), intent(in)  :: rhogvy_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(in)  :: rhogvz      (ADM_gall   ,ADM_kall,ADM_lall   ) ! rho*vz { gam2 x G^1/2 }
  real(RP), intent(in)  :: rhogvz_pl   (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(in)  :: rhogw       (ADM_gall   ,ADM_kall,ADM_lall   ) ! rho*w  { gam2 x G^1/2 }
  real(RP), intent(in)  :: rhogw_pl    (ADM_gall_pl,ADM_kall,ADM_lall_pl)
  real(RP), intent(in)  :: coef_intp   (ADM_gall   ,1:3,ADM_nxyz,TI:TJ,ADM_lall   )
  real(RP), intent(in)  :: coef_intp_pl(ADM_gall_pl,1:3,ADM_nxyz,      ADM_lall_pl)
  real(RP), intent(in)  :: coef_diff   (ADM_gall,1:6        ,ADM_nxyz,ADM_lall   )
  real(RP), intent(in)  :: coef_diff_pl(         1:ADM_vlink,ADM_nxyz,ADM_lall_pl)

  real(RP) :: sclt   (ADM_gall   ,TI:TJ) ! scalar on the hexagon vertex
  real(RP) :: sclt_pl(ADM_gall_pl)
  real(RP) :: sclt_rhogw
  real(RP) :: sclt_rhogw_pl

  real(RP) :: rhogvx_vm   (ADM_gall   )                      ! rho*vx / vertical metrics
  real(RP) :: rhogvx_vm_pl(ADM_gall_pl)
  real(RP) :: rhogvy_vm   (ADM_gall   )                      ! rho*vy / vertical metrics
  real(RP) :: rhogvy_vm_pl(ADM_gall_pl)
  real(RP) :: rhogvz_vm   (ADM_gall   )                      ! rho*vz / vertical metrics
  real(RP) :: rhogvz_vm_pl(ADM_gall_pl)
  real(RP) :: rhogw_vm    (ADM_gall,   ADM_kall,ADM_lall   ) ! rho*w  / vertical metrics
  real(RP) :: rhogw_vm_pl (ADM_gall_pl,ADM_kall,ADM_lall_pl)

  integer  :: gmin, gmax, iall, gall, kall, kmin, kmax, lall, gminm1

  integer  :: ij
  integer  :: ip1j, ijp1, ip1jp1
  integer  :: im1j, ijm1, im1jm1

  integer  :: g, k, l, n, v
  !---------------------------------------------------------------------------
\end{LstF90}

Here \src{ddivdx}, \src{ddivdy}, \src{ddivdz} are calculated x, y, z component
of the gradient of divergence, respectively.
%
And these with \src{_pl} are those for the pole region.
%
\src{rhogvx}, \src{rhogvy}, \src{rhogvz}, and \src{rhogw} are
$G^{1/2} \gamma^2  \times \rho v_x$,
$G^{1/2} \gamma^2  \times \rho v_y$,
$G^{1/2} \gamma^2  \times \rho v_z$, and
$G^{1/2} \gamma^2  \times \rho w$, respectively,
where $\{v_x, v_y, v_z\}$ are the wind vector of horizontal wind component in 3-D Cartesian
coordinates, $w$ is vertical wind,
and $G^{1/2}$ and $\gamma$ are the metrics comes from the
terrain-following coordinate described in \autoref{s:vert_coord}
%
Other arguments \src{coef_intp}, \src{coeff_diff} and those with
\src{_pl} are various coefficients for finite difference calculation,
the same with those in \src{dyn_diffusion}.

Local variable \src{sclt}, \src{sclt_pl} are the scalar value on the
gravitational cente of triangles, i.e. the vertices of the hexagonal control volume.
%
\src{rhogvx_vm} etc. are \src{rhogvx} divided by the vertical metrics.

The first part of this subroutine is as follows.

\begin{LstF90}[name=divdamp, firstnumber=last]
  call DEBUG_rapstart('OPRT3D_divdamp')

  gmin = (ADM_gmin-1)*ADM_gall_1d + ADM_gmin
  gmax = (ADM_gmax-1)*ADM_gall_1d + ADM_gmax
  iall = ADM_gall_1d
  gall = ADM_gall
  kall = ADM_kall
  kmin = ADM_kmin
  kmax = ADM_kmax
  lall = ADM_lall

  gminm1 = (ADM_gmin-1-1)*ADM_gall_1d + ADM_gmin-1

  !$omp parallel default(none),private(g,k,l), &
  !$omp shared(gall,kmin,kmax,lall,rhogw_vm,rhogvx,rhogvy,rhogvz,rhogw,VMTR_C2WfactGz,VMTR_RGSQRTH,VMTR_RGAMH)
  do l = 1, lall
     !$omp do
     do k = kmin+1, kmax
     do g = 1, gall
        rhogw_vm(g,k,l) = ( VMTR_C2WfactGz(g,k,1,l) * rhogvx(g,k  ,l) &
                          + VMTR_C2WfactGz(g,k,2,l) * rhogvx(g,k-1,l) &
                          + VMTR_C2WfactGz(g,k,3,l) * rhogvy(g,k  ,l) &
                          + VMTR_C2WfactGz(g,k,4,l) * rhogvy(g,k-1,l) &
                          + VMTR_C2WfactGz(g,k,5,l) * rhogvz(g,k  ,l) &
                          + VMTR_C2WfactGz(g,k,6,l) * rhogvz(g,k-1,l) &
                          ) * VMTR_RGAMH(g,k,l)                       & ! horizontal contribution
                        + rhogw(g,k,l) * VMTR_RGSQRTH(g,k,l)            ! vertical   contribution
     enddo
     enddo
     !$omp end do nowait

!OCL XFILL
     !$omp do
     do g = 1, gall
        rhogw_vm(g,kmin  ,l) = 0.0_RP
        rhogw_vm(g,kmax+1,l) = 0.0_RP
     enddo
     !$omp end do
  enddo
  !$omp end parallel

\end{LstF90}

This part calculates \src{rhogw_vm} from 3 components of $\rho \bm{v}$
and $\rho w$ (with the metrics).
The values are located at the triangluar points in horizonatal direction,
and at the half-integer levels in vertical direction.
%
Coefficients prefixed with \src{VMTR_} are defined and pre-calculated in
module \src{mod_vmtr} in original \NICAM.
%
When kernelize this subroutine, these definition are moved to
\src{mod_misc}, and read from the input data file.

Second part is pretty long as follows.

\begin{LstF90}[name=divdamp,firstnumber=last]
  !$omp parallel default(none),private(g,k,l,ij,ip1j,ip1jp1,ijp1,im1j,ijm1,im1jm1,sclt_rhogw), &
  !$omp shared(ADM_have_sgp,gminm1,gmin,gmax,gall,kmin,kmax,lall,iall,ddivdx,ddivdy,ddivdz,rhogvx,rhogvy,rhogvz, &
  !$omp        rhogvx_vm,rhogvy_vm,rhogvz_vm,rhogw_vm,sclt,coef_intp,coef_diff,GRD_rdgz,VMTR_RGAM)
  do l = 1, lall
     do k = kmin, kmax
!OCL XFILL
        !$omp do
        do g = 1, gall
           rhogvx_vm(g) = rhogvx(g,k,l) * VMTR_RGAM(g,k,l)
           rhogvy_vm(g) = rhogvy(g,k,l) * VMTR_RGAM(g,k,l)
           rhogvz_vm(g) = rhogvz(g,k,l) * VMTR_RGAM(g,k,l)
        enddo
        !$omp end do

        !$omp do
        do g = gminm1, gmax
           ij     = g
           ip1j   = g + 1
           ip1jp1 = g + iall + 1
           ijp1   = g + iall

           sclt_rhogw = ( ( rhogw_vm(ij,k+1,l) + rhogw_vm(ip1j,k+1,l) + rhogw_vm(ip1jp1,k+1,l) ) &
                        - ( rhogw_vm(ij,k  ,l) + rhogw_vm(ip1j,k  ,l) + rhogw_vm(ip1jp1,k  ,l) ) &
                        ) / 3.0_RP * GRD_rdgz(k)

           sclt(g,TI) = coef_intp(g,1,XDIR,TI,l) * rhogvx_vm(ij    ) &
                      + coef_intp(g,2,XDIR,TI,l) * rhogvx_vm(ip1j  ) &
                      + coef_intp(g,3,XDIR,TI,l) * rhogvx_vm(ip1jp1) &
                      + coef_intp(g,1,YDIR,TI,l) * rhogvy_vm(ij    ) &
                      + coef_intp(g,2,YDIR,TI,l) * rhogvy_vm(ip1j  ) &
                      + coef_intp(g,3,YDIR,TI,l) * rhogvy_vm(ip1jp1) &
                      + coef_intp(g,1,ZDIR,TI,l) * rhogvz_vm(ij    ) &
                      + coef_intp(g,2,ZDIR,TI,l) * rhogvz_vm(ip1j  ) &
                      + coef_intp(g,3,ZDIR,TI,l) * rhogvz_vm(ip1jp1) &
                      + sclt_rhogw
        enddo
        !$omp end do nowait

        !$omp do
        do g = gminm1, gmax
           ij     = g
           ip1j   = g + 1
           ip1jp1 = g + iall + 1
           ijp1   = g + iall

           sclt_rhogw = ( ( rhogw_vm(ij,k+1,l) + rhogw_vm(ip1jp1,k+1,l) + rhogw_vm(ijp1,k+1,l) ) &
                        - ( rhogw_vm(ij,k  ,l) + rhogw_vm(ip1jp1,k  ,l) + rhogw_vm(ijp1,k  ,l) ) &
                        ) / 3.0_RP * GRD_rdgz(k)

           sclt(g,TJ) = coef_intp(g,1,XDIR,TJ,l) * rhogvx_vm(ij    ) &
                      + coef_intp(g,2,XDIR,TJ,l) * rhogvx_vm(ip1jp1) &
                      + coef_intp(g,3,XDIR,TJ,l) * rhogvx_vm(ijp1  ) &
                      + coef_intp(g,1,YDIR,TJ,l) * rhogvy_vm(ij    ) &
                      + coef_intp(g,2,YDIR,TJ,l) * rhogvy_vm(ip1jp1) &
                      + coef_intp(g,3,YDIR,TJ,l) * rhogvy_vm(ijp1  ) &
                      + coef_intp(g,1,ZDIR,TJ,l) * rhogvz_vm(ij    ) &
                      + coef_intp(g,2,ZDIR,TJ,l) * rhogvz_vm(ip1jp1) &
                      + coef_intp(g,3,ZDIR,TJ,l) * rhogvz_vm(ijp1  ) &
                      + sclt_rhogw
        enddo
        !$omp end do

        if ( ADM_have_sgp(l) ) then ! pentagon
           !$omp master
           sclt(gminm1,TI) = sclt(gminm1+1,TJ)
           !$omp end master
        endif

!OCL XFILL
        !$omp do
        do g = 1, gmin-1
           ddivdx(g,k,l) = 0.0_RP
           ddivdy(g,k,l) = 0.0_RP
           ddivdz(g,k,l) = 0.0_RP
        enddo
        !$omp end do nowait

        !$omp do
        do g = gmin, gmax
           ij     = g
           im1j   = g - 1
           im1jm1 = g - iall - 1
           ijm1   = g - iall

           ddivdx(g,k,l) = ( coef_diff(g,1,XDIR,l) * ( sclt(ij,    TI) + sclt(ij,    TJ) ) &
                           + coef_diff(g,2,XDIR,l) * ( sclt(ij,    TJ) + sclt(im1j,  TI) ) &
                           + coef_diff(g,3,XDIR,l) * ( sclt(im1j,  TI) + sclt(im1jm1,TJ) ) &
                           + coef_diff(g,4,XDIR,l) * ( sclt(im1jm1,TJ) + sclt(im1jm1,TI) ) &
                           + coef_diff(g,5,XDIR,l) * ( sclt(im1jm1,TI) + sclt(ijm1  ,TJ) ) &
                           + coef_diff(g,6,XDIR,l) * ( sclt(ijm1,  TJ) + sclt(ij,    TI) ) )
        enddo
        !$omp end do nowait

        !$omp do
        do g = gmin, gmax
           ij     = g
           im1j   = g - 1
           im1jm1 = g - iall - 1
           ijm1   = g - iall

           ddivdy(g,k,l) = ( coef_diff(g,1,YDIR,l) * ( sclt(ij,    TI) + sclt(ij,    TJ) ) &
                           + coef_diff(g,2,YDIR,l) * ( sclt(ij,    TJ) + sclt(im1j,  TI) ) &
                           + coef_diff(g,3,YDIR,l) * ( sclt(im1j,  TI) + sclt(im1jm1,TJ) ) &
                           + coef_diff(g,4,YDIR,l) * ( sclt(im1jm1,TJ) + sclt(im1jm1,TI) ) &
                           + coef_diff(g,5,YDIR,l) * ( sclt(im1jm1,TI) + sclt(ijm1  ,TJ) ) &
                           + coef_diff(g,6,YDIR,l) * ( sclt(ijm1,  TJ) + sclt(ij,    TI) ) )
        enddo
        !$omp end do nowait

        !$omp do
        do g = gmin, gmax
           ij     = g
           im1j   = g - 1
           im1jm1 = g - iall - 1
           ijm1   = g - iall

           ddivdz(g,k,l) = ( coef_diff(g,1,ZDIR,l) * ( sclt(ij,    TI) + sclt(ij,    TJ) ) &
                           + coef_diff(g,2,ZDIR,l) * ( sclt(ij,    TJ) + sclt(im1j,  TI) ) &
                           + coef_diff(g,3,ZDIR,l) * ( sclt(im1j,  TI) + sclt(im1jm1,TJ) ) &
                           + coef_diff(g,4,ZDIR,l) * ( sclt(im1jm1,TJ) + sclt(im1jm1,TI) ) &
                           + coef_diff(g,5,ZDIR,l) * ( sclt(im1jm1,TI) + sclt(ijm1  ,TJ) ) &
                           + coef_diff(g,6,ZDIR,l) * ( sclt(ijm1,  TJ) + sclt(ij,    TI) ) )
        enddo
        !$omp end do nowait

!OCL XFILL
        !$omp do
        do g = gmax+1, gall
           ddivdx(g,k,l) = 0.0_RP
           ddivdy(g,k,l) = 0.0_RP
           ddivdz(g,k,l) = 0.0_RP
        enddo
        !$omp end do
     enddo ! loop k

!OCL XFILL
     !$omp do
     do g = 1, gall
        ddivdx(g,kmin-1,l) = 0.0_RP
        ddivdy(g,kmin-1,l) = 0.0_RP
        ddivdz(g,kmin-1,l) = 0.0_RP
        ddivdx(g,kmax+1,l) = 0.0_RP
        ddivdy(g,kmax+1,l) = 0.0_RP
        ddivdz(g,kmax+1,l) = 0.0_RP
     enddo
     !$omp end do
  enddo ! loop l
  !$omp end parallel

\end{LstF90}

In the outer most \src{l}-loop(l.98) and \src{k}-loop(l.99),
\src{sclt} at the triangular point \src{TI}(l.120) and \src{TJ}(l.144)
are calculated separately by interpolation.
%
and finally,
in the \src{g}-loop begins at l.173,
desired \src{ddivdx}(l.179), \src{ddivdy}(l.195), and
\src{ddivdz}(l.211) are calculated.

The final part is for the pole region, doing almost the same thing
as the normal region described above.

\begin{LstF90}[name=divdamp,firstnumber=last]
  if ( ADM_have_pl ) then
     n = ADM_gslf_pl

     do l = 1, ADM_lall_pl
        do k = ADM_kmin+1, ADM_kmax
        do g = 1, ADM_gall_pl
           rhogw_vm_pl(g,k,l) = ( VMTR_C2WfactGz_pl(g,k,1,l) * rhogvx_pl(g,k  ,l) &
                                + VMTR_C2WfactGz_pl(g,k,2,l) * rhogvx_pl(g,k-1,l) &
                                + VMTR_C2WfactGz_pl(g,k,3,l) * rhogvy_pl(g,k  ,l) &
                                + VMTR_C2WfactGz_pl(g,k,4,l) * rhogvy_pl(g,k-1,l) &
                                + VMTR_C2WfactGz_pl(g,k,5,l) * rhogvz_pl(g,k  ,l) &
                                + VMTR_C2WfactGz_pl(g,k,6,l) * rhogvz_pl(g,k-1,l) &
                                ) * VMTR_RGAMH_pl(g,k,l)                          & ! horizontal contribution
                              + rhogw_pl(g,k,l) * VMTR_RGSQRTH_pl(g,k,l)            ! vertical   contribution
        enddo
        enddo

        do g = 1, ADM_gall_pl
           rhogw_vm_pl(g,ADM_kmin  ,l) = 0.0_RP
           rhogw_vm_pl(g,ADM_kmax+1,l) = 0.0_RP
        enddo
     enddo

     do l = 1, ADM_lall_pl
        do k = ADM_kmin, ADM_kmax
           do v = 1, ADM_gall_pl
              rhogvx_vm_pl(v) = rhogvx_pl(v,k,l) * VMTR_RGAM_pl(v,k,l)
              rhogvy_vm_pl(v) = rhogvy_pl(v,k,l) * VMTR_RGAM_pl(v,k,l)
              rhogvz_vm_pl(v) = rhogvz_pl(v,k,l) * VMTR_RGAM_pl(v,k,l)
           enddo

           do v = ADM_gmin_pl, ADM_gmax_pl
              ij   = v
              ijp1 = v + 1
              if( ijp1 == ADM_gmax_pl+1 ) ijp1 = ADM_gmin_pl

              sclt_rhogw_pl = ( ( rhogw_vm_pl(n,k+1,l) + rhogw_vm_pl(ij,k+1,l) + rhogw_vm_pl(ijp1,k+1,l) ) &
                              - ( rhogw_vm_pl(n,k  ,l) + rhogw_vm_pl(ij,k  ,l) + rhogw_vm_pl(ijp1,k  ,l) ) &
                              ) / 3.0_RP * GRD_rdgz(k)

              sclt_pl(ij) = coef_intp_pl(v,1,XDIR,l) * rhogvx_vm_pl(n   ) &
                          + coef_intp_pl(v,2,XDIR,l) * rhogvx_vm_pl(ij  ) &
                          + coef_intp_pl(v,3,XDIR,l) * rhogvx_vm_pl(ijp1) &
                          + coef_intp_pl(v,1,YDIR,l) * rhogvy_vm_pl(n   ) &
                          + coef_intp_pl(v,2,YDIR,l) * rhogvy_vm_pl(ij  ) &
                          + coef_intp_pl(v,3,YDIR,l) * rhogvy_vm_pl(ijp1) &
                          + coef_intp_pl(v,1,ZDIR,l) * rhogvz_vm_pl(n   ) &
                          + coef_intp_pl(v,2,ZDIR,l) * rhogvz_vm_pl(ij  ) &
                          + coef_intp_pl(v,3,ZDIR,l) * rhogvz_vm_pl(ijp1) &
                          + sclt_rhogw_pl
           enddo

           ddivdx_pl(:,k,l) = 0.0_RP
           ddivdy_pl(:,k,l) = 0.0_RP
           ddivdz_pl(:,k,l) = 0.0_RP

           do v = ADM_gmin_pl, ADM_gmax_pl
              ij   = v
              ijm1 = v - 1
              if( ijm1 == ADM_gmin_pl-1 ) ijm1 = ADM_gmax_pl ! cyclic condition

              ddivdx_pl(n,k,l) = ddivdx_pl(n,k,l) + coef_diff_pl(v-1,XDIR,l) * ( sclt_pl(ijm1) + sclt_pl(ij) )
              ddivdy_pl(n,k,l) = ddivdy_pl(n,k,l) + coef_diff_pl(v-1,YDIR,l) * ( sclt_pl(ijm1) + sclt_pl(ij) )
              ddivdz_pl(n,k,l) = ddivdz_pl(n,k,l) + coef_diff_pl(v-1,ZDIR,l) * ( sclt_pl(ijm1) + sclt_pl(ij) )
           enddo
        enddo

        ddivdx_pl(:,ADM_kmin-1,l) = 0.0_RP
        ddivdx_pl(:,ADM_kmax+1,l) = 0.0_RP
        ddivdy_pl(:,ADM_kmin-1,l) = 0.0_RP
        ddivdy_pl(:,ADM_kmax+1,l) = 0.0_RP
        ddivdz_pl(:,ADM_kmin-1,l) = 0.0_RP
        ddivdz_pl(:,ADM_kmax+1,l) = 0.0_RP
     enddo
  else
     ddivdx_pl(:,:,:) = 0.0_RP
     ddivdy_pl(:,:,:) = 0.0_RP
     ddivdz_pl(:,:,:) = 0.0_RP
  endif

  call DEBUG_rapend('OPRT3D_divdamp')

  return
end subroutine OPRT3D_divdamp
\end{LstF90}

\subsection{Input data and result}

Max/min/sum of input/output data of the kernel subroutine are output as
a log.
%
Below is an example of \src{$IAB_SYS=Ubuntu-gnu-ompi} case.

\begin{LstLog}
 ### Input ###
 +check[check_ddivdx    ] max=  3.0131744015374420E-11,min= -5.2988229203876260E-11,sum= -1.6975403236890680E-11
 +check[check_ddivdx_pl ] max=  2.6717761683260969E-11,min= -5.3219224048058505E-11,sum=  4.8275913249408648E-11
 +check[check_ddivdy    ] max=  2.8051192990274010E-11,min= -2.3223214708700599E-11,sum= -4.5483269668124264E-11
 +check[check_ddivdy_pl ] max=  4.4686835079822753E-11,min= -4.1286060234480353E-11,sum=  6.8817604250940581E-11
 +check[check_ddivdz    ] max=  3.4380463226247279E-11,min= -1.3010875841849350E-11,sum= -7.2052908719075937E-11
 +check[check_ddivdz_pl ] max=  3.2276474319270210E-13,min= -8.5477300588035169E-14,sum=  3.9437219768394675E-12
 +check[rhogvx          ] max=  2.2205470252374623E-01,min= -2.0636880346843767E-01,sum= -1.7381945003843336E+02
 +check[rhogvx_pl       ] max=  1.4368235944041274E-01,min= -1.9062928892442133E-01,sum= -5.6937904377485706E+00
 +check[rhogvy          ] max=  2.4120815977222165E-01,min= -2.5907366818620919E-01,sum=  1.7927398128296358E+02
 +check[rhogvy_pl       ] max=  8.8938663283927272E-02,min= -9.6299249362234787E-02,sum= -9.3645167747781997E-03
 +check[rhogvz          ] max=  2.0899984486710360E-01,min= -1.9461784957198358E-01,sum=  2.9192900535497301E+02
 +check[rhogvz_pl       ] max=  1.2715227826262342E-03,min= -6.6561175383069663E-04,sum=  2.2146129568583188E-02
 +check[rhogw           ] max=  1.2675708942695349E-03,min= -4.2678569215437437E-03,sum= -2.6525715444058587E-02
 +check[rhogw_pl        ] max=  9.9481836269239257E-04,min= -4.2678569215437437E-03,sum= -8.3801034534239580E-02
 ### Output ###
 +check[ddivdx          ] max=  3.0131744015374420E-11,min= -5.2988229203876260E-11,sum= -1.6975403236890680E-11
 +check[ddivdx_pl       ] max=  2.6717761683260969E-11,min= -5.3219224048058505E-11,sum=  4.8275913249408648E-11
 +check[ddivdy          ] max=  2.8051192990274010E-11,min= -2.3223214708700599E-11,sum= -4.5483269668124264E-11
 +check[ddivdy_pl       ] max=  4.4686835079822753E-11,min= -4.1286060234480353E-11,sum=  6.8817604250940581E-11
 +check[ddivdz          ] max=  3.4380463226247279E-11,min= -1.3010875841849350E-11,sum= -7.2052908719075937E-11
 +check[ddivdz_pl       ] max=  3.2276474319270210E-13,min= -8.5477300588035169E-14,sum=  3.9437219768394675E-12
 ### Validation : point-by-point diff ###
 +check[check_ddivdx    ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[check_ddivdx_pl ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[check_ddivdy    ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[check_ddivdy_pl ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[check_ddivdz    ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 +check[check_ddivdz_pl ] max=  0.0000000000000000E+00,min=  0.0000000000000000E+00,sum=  0.0000000000000000E+00
 *** Finish kernel
\end{LstLog}

Check the lines below \src{``Validation : point-by-point diff''} line,
that shows difference between calculated output array and
pre-calculated reference array.
These should be zero or enough small to be acceptable.

There are sample output log files in \file{reference/}
in each kernel program directory, for reference purpose.

\subsection{Sample of perfomance result}

Here's an example of the performance result part of the log output.
Below is an example executed with the machine environment described in \autoref{s:measuring_env}.
%
Note that in this program kernel part is iterated one time.

\begin{LstLog}
 *** Computational Time Report
 *** ID=001 : MAIN_dyn_divdamp                 T=     0.028 N=      1
 *** ID=002 : OPRT3D_divdamp                   T=     0.028 N=      1
\end{LstLog}


\section{Hyperbolic functions}


\documentclass[12pt, oneside]{article}

\usepackage{float}
\usepackage{lineno}
\usepackage{color, amssymb, amsmath, amsthm, verbatim, wasysym}
\usepackage{natbib}
\usepackage{epsfig}
\usepackage[mathscr]{eucal}
\usepackage{mathrsfs}
\usepackage{appendix}
\raggedbottom
\usepackage[left=0.8in,right=0.8in,top=0.8in,bottom=0.9in,centering]{geometry}

% Uncomment to show references.
%\usepackage[notcite,notref]{showkeys}

% To make really wide hats that cover everything:
\usepackage{scalerel}
\usepackage{stackengine}
\setstackEOL{\#}
\stackMath
\def\hatgap{1pt}
\def\subdown{-0.2pt}
\newcommand\reallywidehat[2][]{%
\renewcommand\stackalignment{l}%
\stackon[\hatgap]{#2}{%
\stretchto{%
    \scalerel*[\widthof{$#2$}]{\kern-.6pt\bigwedge\kern-.6pt}%
    {\rule[-\textheight/2]{1ex}{\textheight}}%WIDTH-LIMITED BIG WEDGE
}{0.6ex}% THIS SQUEEZES THE WEDGE TO 0.5ex HEIGHT
_{\smash{\belowbaseline[\subdown]{\scriptstyle#1}}}%
}}

% For a 'strut' that creates space above underbraces
\newcommand*\mystrut[1]{\vrule width0pt height0pt depth#1\relax}

% Punctuation
\newcommand{\com}{\, ,}
\newcommand{\per}{\, .}

% A nice 'definition'
\newcommand{\defn}{\ensuremath{\stackrel{\mathrm{def}}{=}}}

% Use \bar to over line solo symbols
\newcommand{\av}[1]{\left \langle{#1} \right \rangle}
\newcommand{\avbg}[1]{\overline{#1}}
\newcommand{\avbgg}[1]{\overline{#1}}
\newcommand{\hav}[1]{\widehat{#1}}

% Begin and end equations
\newcommand{\beq}{\begin{equation}}
\newcommand{\eeq}{\end{equation}}

% Vector calculus operators
\newcommand{\p}{\partial}
\newcommand{\bnabla}{\boldsymbol \nabla}
\newcommand{\pnabla}{\boldsymbol \nabla_{\! \! \perp}}
\newcommand{\hnabla}{\bnabla_{\! \! h}}
\newcommand{\bnablad}{\bnabla_{\! \! \alpha}}
\newcommand{\bcdot}{\boldsymbol \cdot}
\newcommand{\hlap}{\triangle_h}
\newcommand{\lap}{\triangle}
\newcommand{\grad}{\bnabla}
\newcommand{\curl}{\bnabla \!\times\!}
\newcommand{\diver}{\bnabla \bcdot }
\newcommand{\cross}{\times}

% Bold symbolds
\newcommand{\bu}{\boldsymbol u}
\newcommand{\buh}{\boldsymbol u_h}
\newcommand{\bx}{\boldsymbol x}
\newcommand{\ba}{\boldsymbol{a}}
\newcommand{\bk}{\boldsymbol{k}}
\newcommand{\bh}{\boldsymbol{h}}
\newcommand{\bm}{\boldsymbol{m}}
\newcommand{\bn}{\boldsymbol{\hat n}}
\newcommand{\bxh}{\hspace{0.1em} \boldsymbol{\hat x}}
\newcommand{\byh}{\hspace{0.1em}\boldsymbol{\hat y}}
\newcommand{\bzh}{\hspace{0.1em}\boldsymbol{\hat z}}
\newcommand{\bnh}{\hspace{0.1em}\boldsymbol{\hat n}}
\newcommand{\bomega}{\boldsymbol \omega}
\newcommand{\bOmega}{\boldsymbol \Omega}
\newcommand{\bxi}{\ensuremath {\boldsymbol {\xi}}}
\newcommand{\bXi}{\ensuremath {\boldsymbol {\Xi}}}
\newcommand{\bU}{\boldsymbol{U}}
\newcommand{\bX}{\boldsymbol{X}}

% Greek abbrevs
\newcommand{\ep}{\epsilon}
\newcommand{\om}{\omega}
\newcommand{\kap}{\kappa}

% Roman characters
\newcommand{\ee}{\mathrm{e}}
\newcommand{\ii}{\mathrm{i}}
\newcommand{\cc}{\mathrm{cc}}
\newcommand{\dd}{{\rm d}}
\newcommand{\id}{{\, \rm d}}
\newcommand{\DD}{{\rm D}}
\newcommand{\J}{\mathrm{J}}
\renewcommand{\L}{\mathrm{L}}

% Non-dimensional numbers
\newcommand{\Ri}{Ri}
\newcommand{\Ro}{Ro}
\newcommand{\Bu}{Bu}
\newcommand{\Pe}{Pe}

% Material derivative
\newcommand{\Dt}[1]{\mathrm{D}_t #1}

% Small in-line fractions
\newcommand{\half}{\tfrac{1}{2}}

% Bold 'F' for 'forcing'
\newcommand{\bff}{\boldsymbol{F}}
\newcommand{\fh}{\breve f}

% Operators
\newcommand{\friction}{\mathrm{F}}
\newcommand{\mixing}{\mathrm{M}}
\newcommand{\wave}{\mathrm{E}}
\newcommand{\disp}{\mathrm{D}}

\newcommand{\mode}{\phi}
\newcommand{\psih}{\hat \psi}
\newcommand{\qh}{\hat q}

\begin{document}

\title{\vspace{-4ex} Reduced equations for weakly nonlinear Boussinesq flows}
\author{Greg}
\date{} \maketitle \vspace{-4ex}

\section{The quasi-geostrophic equation}

Quasi-geostrophic flow is a small amplitude, slowly-evolving, hydrostatic and small-aspect-ratio mode of motion in rotating stratified flow. Its evolution is much slower than a rotation period due to small departures from a linear, static balance betwen velocity and pressure. The evolution of quasi-geostrophic flow is governed by the advection of quasi-geostrophic potential vorticity, which is related to its advecting streamfunction through the elliptic equation
\beq
q = \Big ( \underbrace{\mystrut{2ex} \p_x^2 + \p_y^2}_{\defn \hlap} + \underbrace{\mystrut{2ex} \p_z \frac{f_0^2}{N^2} \p_z}_{\defn \L} \Big ) \psi \com
\label{pv}
\eeq
With the rotation rate $f = f_0 + \beta y$, quasi-geostrophic potential vorticity solves the advection equation
\beq
q_t + \J \left ( \psi, q \right ) + \beta \psi_x = \friction \left ( \hlap \psi \right ) + \mixing \left ( \L \psi \right ) \per
\label{qg}
\eeq
A projection of both \eqref{pv} and \eqref{qg} onto the vertical modes $\phi_{nz}$ yields
\beq
q_n = \left ( \hlap - \lambda_n^{-2} \right ) \psi_n \com \qquad \text{and} \qquad q_{nt} + \beta \psi_{nx} + \int \phi_{nz} \J \left ( \psi, q \right ) \id z = \friction_n \left ( \hlap \psi_n \right) - \lambda_n^{-2} \mixing_n \left ( \psi_n \right ) \per
\eeq
It is not true in general that the vertical mode projection commutes with the operators $\friction$ and $\mixing$, but we assume this here for simplicity.

\subsection{Two-layer quasi-geostrophic flow with tracers}

The two-layer equations for potential vorticities $q_1,q_2$, and streamfunctions $\psi_1, \psi_2$, in layers 1 and 2 consist of a potential vorticity conservation equation for each layer,
\begin{align}
q_{1t} + \J \left ( \psi_1, q_1 \right ) + \beta \psi_{1x} &= \friction \left ( q_1 \right ) \com \\
q_{2t} + \J \left ( \psi_2, q_2 \right ) + \beta \psi_{2x} &= - r \hlap \psi_2 +  \friction \left ( q_2 \right ) \com
\end{align}
where typically the layerwise friction operator is an isotropic viscous-like term of the form
\beq
\friction = (-1)^{n/2-1} \nu \hlap^{n/2} \com
\eeq
The potential vorticity in each layer depends both on the layerwise relative vorticity, as well as the `shear' $\psi_1-\psi_2$, according to
\begin{align}
q_1 &= \hlap \psi_1 - F_1 \left ( \psi_1 - \psi_2 \right ) \label{psi1} \\
q_2 &= \hlap \psi_2 + F_2 \left ( \psi_1 - \psi_2 \right ) \per \label{psi2}
\end{align}
The Fourier transform of \eqref{psi1} and \eqref{psi2} can be rearranged into
\beq
 \left [ \, \begin{matrix}
\psih_1 \\
\psih_2
\end{matrix} \, \right ] =
- \frac{1}{\text{det} M_2} \left [ \, \begin{matrix}
 K^2 + F_2 &  F_1 \\
F_2 & K^2 + F_1
\end{matrix} \, \right ]  \left [ \, \begin{matrix}
\qh_1 \\
\qh_2
\end{matrix} \, \right ] \com
\eeq
where $M_2$ is a matrix and it's determinant $\text{det} M_2$ is
\beq
\text{det} M_2 = K^2 \left ( K^2 + F_1 + F_2 \right ) \per
\eeq

We also include layerwise tracer distributsion $c_1$ and $c_2$, whose conservation is governed by
\begin{align}
c_{1t} + \J \left ( \psi_2, c_1 \right ) &= \kappa \left ( c_1 - c_2 \right ) + \mixing c_1 \com \\
c_{2t} + \J \left ( \psi_2, c_2 \right ) &= \kappa \left ( c_2 - c_1 \right ) + \mixing c_2 \com
\end{align}
$\kappa$ is the vertical diffusion coefficient.

\subsection{$n$-layer quasi-geostrophic flow}

\section{The YBJ equation}

The two prognostic variables in the YBJ equation are wave field amplitude $A$ and quasi-geostrophic potential vorticity $q$. The wave field amplitude represents the complexified horizontal velocity via
\beq
\tilde u + \ii \tilde v = \ee^{- \ii f_0 t} \L A \com
\eeq
and solves the YBJ equation
\beq
\L A_t + \frac{\ii f_0}{2} \hlap A +  \J \left ( \psi, \L A \right ) + \L A \left ( \tfrac{\ii}{2} \hlap \psi + \beta y \right ) = \friction \left ( \L A \right ) \per
\label{ybj}
\eeq

\subsection{In $x,y$ with barotropic $\psi(x, y, t)$}

When $\psi = \psi(x, y, t)$ is barotropic and does not depend on $z$, the YBJ equation \eqref{ybj} can be projected onto vertical modes to yield
\beq
A_{nt} - \frac{\ii f_0 \lambda_n^2}{2} \hlap A_n + \J \left ( \psi, A_n \right ) + A_n \left ( \tfrac{\ii}{2} \hlap \psi + \beta y \right ) = \friction_{n} \left ( A_n \right ) \per
\eeq
The quasi-geostrophic streamfunction $\psi$ obeys the ordinary two-dimensional turbulence equation,
\beq
\hlap \psi_t + \J \left ( \psi, \hlap \psi \right ) = \friction \left ( \hlap \psi \right ) \per
\eeq

\subsection{In $y, z$ with stationary $\psi(y, z)$}

When $\psi(y, z)$ does not depend on $y$ and therefore does not evolve, and $N$ is constant, the YBJ equation reduces to
\beq
A_{zzt} + \frac{\ii N^2}{2 f_0} A_{yy} + \left ( \tfrac{\ii}{2} \psi_{yy} + \beta y \right ) A_{zz} = \friction \left ( A_{zz} \right ) \per
\eeq
Because $q_t = 0$ when $\psi_x = q_x = 0$, the PV equation is solved automatically and $\psi$ does not evolve.

\section{The hydrostatic wave equation}

The hydrostatic wave equation for a wave field with frequency $\sigma$ is
\beq
\begin{split}
& \wave A_{t} + \J \left ( \psi , \wave A \right ) + \ii \alpha \sigma \disp A  + \J \left ( A, \disp \psi \right )  \\
& \qquad - \tfrac{2 \ii \sigma}{f_0^2} \Big [  \J \left ( \psi_x , \ii \sigma A_x - f_0 A_y \right ) +  \J \left ( \psi_y , \ii \sigma A_y + f_0 A_x \right ) \Big ] \\
& \qquad \qquad + \tfrac{ \ii \sigma}{f_0}  \left [ \hnabla \bcdot \left ( \disp \psi \hnabla A \right ) - \disp \left ( \tfrac{\alpha f_0^2}{N^2} \psi_z A_z \right ) + \p_z \left ( \tfrac{\alpha f_0^2}{N^2} \psi_z \disp A \right ) \right ]  =0 \per
\end{split}
\label{internalTideEqnRemodeling}
\eeq
where the operators $\wave$ and $\disp$ are defined
\beq
\wave \defn \frac{\alpha}{2} \left [ \hlap + \left (4  + 3 \alpha \right ) \L \right ] \qquad \text{and} \qquad \disp \defn \hlap - \alpha \L \per
\eeq

\subsection{In $x,y$ with barotropic $\psi(x, y, t)$}

\beq
\begin{split}
& \wave_n A_{nt} + \ii \alpha \sigma \disp_n A_n +  \J \left ( \psi, \wave_n A_n \right ) + \J \left ( A_n, \hlap \psi \right ) + \tfrac{\ii \sigma}{f_0} \hnabla \bcdot \left ( \hlap \psi \hnabla A_n \right ) \\
&  \qquad - \tfrac{2 \ii \sigma}{f_0^2} \left [ \J \left ( \psi_x, \ii \sigma A_{nx} - f_0 A_{ny} \right ) + \J \left ( \psi_y, \ii \sigma A_{ny} + f_0 A_{nx} \right )  \right ] = \friction_n \left ( A_n \right ) \com
\end{split}
\label{modeWiseWaveEqn}
\eeq
where $\nu_A$ is the hyperviscosity applied to $A_n$, and the mode-wise operators $\wave_n$ and $\disp_n$ are
\beq
\wave_n = \frac{\alpha}{2} \Big [ \hlap - \lambda_n^{-2} \left ( 4 + 3 \alpha \right ) \Big ] \qquad \text{and} \qquad \disp_n = \hlap + \alpha \lambda_n^{-2} \per
\eeq
Equation \eqref{modeWiseWaveEqn} describes the horizontal propagation of a mode-$n$ wave field with amplitude $A_n(x,y,t)$ through two-dimensional turbulence with streamfunction $\psi$.  The arbitrary stratification profile $N(z)$ enters \eqref{modeWiseWaveEqn} via the eigenvalue $\lambda_n^{-2}$ determined by \eqref{modalEigenproblem}.

\subsection{In $y, z$ with stationary $\psi(y, z)$}

\appendix

\section{The vertical mode decomposition}
\label{verticalModeProjection}

The hydrostatic vertical modes $\mode_n(z)$ solve the eigenproblem
\beq
\frac{f_0^2}{N^2} \mode_{nzz} + \lambda_n^{-2} \mode_n = 0 \com \qquad \text{with} \qquad \mode_n = 0 \quad \text{at} \quad z = -H, 0 \per
\label{modalEigenproblem}
\eeq
Note that the derivative $h_{nz}$ satisfies $h_{nz} = - \lambda_n^2 \L h_{nz}$.  The amplitudes of certain quantities are determined by their weighted projection onto $\mode_n$ or its derivative $\mode_{nz}$. For example,
\beq
u_n \defn \int_{-H}^0 \Phi \, \mode_{nz} \id z \com \qquad b_n \defn \int_{-H}^0 b \, \mode_n \id z  \qquad \text{and} \qquad w_n \defn \int_{-H}^0 \frac{N^2}{\lambda_n^2 f_0^2} \, w \, \mode_n \id z  \per
\label{modezDef}
\eeq
The proper choice depends on the equation set and chosen boundary conditions.

Some trickiness is associated with the friction operator $\friction$. We want to assume, for example, that we can define an operator $\friction_n$ such that
\beq
\friction_{nu} (u_n) \approx \int_{-H}^0 \phi_{nz} \friction_u(u) \id z \com
\label{approximateFriction}
\eeq
for example. This is not always possible, however.  Notice that if $\friction = \nu \lap$, then
\beq
\int_{-H}^0 \phi_{nz} \nu \lap u \id z = \nu \hlap u_n - \frac{1}{(f_0 \lambda_n)^2} \int_{-H}^0 N^2 \phi_n u_{z} \id z \com
\eeq
and different modes are therefore coupled by the rightmost term. When $N$ is constant, however, this becomes
\beq
\int_{-H}^0 \phi_{nz} \nu \lap u \id z = \nu \hlap u_n + \nu \left ( \tfrac{n \pi}{H} \right )^2 u_n \com
\eeq
and therefore $\friction_n = \nu \left ( \hlap + \left ( \tfrac{n \pi}{H} \right )^2 \right )$. A similar conundrum is associated with the diffusion operator $\mixing$.

\section{The non-hydrostatic Boussinesq equations}

The rotating Boussinesq equations are
\begin{align}
\Dt{\bu} + 2 \bOmega \times \bu - b \bzh + \bnabla p &= \friction \bu \com \label{mom} \\
\Dt{b} + w N^2 &= \mixing b \com \label{buoy} \\
\bnabla \bcdot \bu &= 0 \com \label{cont}
\end{align}
where $\Dt \defn \p_t + \bu \bcdot \bnabla$ is the material derivative,
\beq
2 \bOmega \defn \underbrace{2 \Omega \cos \phi}_{\defn \breve f} \byh + \underbrace{2 \Omega \sin \phi}_{\defn f} \bzh \com
\eeq
is the axis around which the Earth rotates, and the $\friction$ and $\mixing$ are operators that represent dissipative frictional processes and diffusive mixing processes, respectively. If dissipation and diffusion are due to isotropic molecular processes, then $\friction = \nu \lap$ and $\mixing = \kap \lap$, where $\lap = \p_x^2 + \p_y^2 + \p_z^2$ is the three-dimensional Laplacian.

\section{`Wave operator form' of the Boussinesq equations}
\label{waveOperatorForm}

The component-wise rotating inviscid Boussinesq equations are
\begin{align}
u_t - f v + \fh w + p_x &= - \bu \bcdot \bnabla u \com \label{xmom} \\
v_t + f u + p_y &= - \bu \bcdot \bnabla v \com \label{ymom} \\
w_t - \fh u - b + p_z &= - \bu \bcdot \bnabla w \com \label{zmom} \\
b_t + w N^2 &= - \bu \bcdot \bnabla b \com \label{buoyComp} \\
\bnabla \bcdot \bu &= 0 \label{contComp} \per
\end{align}
We first form the `oscillation equation' with the combination $\p_t \eqref{zmom} + \eqref{buoyComp}$:
\beq
\left ( \p_t^2 + N^2 \right ) w - \fh u_t + p_{zt} = - \p_t \left ( \bu \bcdot \bnabla w \right ) - \bu \bcdot \bnabla b \per
\label{oscillation}
\eeq
The 'divergence equation' follows from $- \p_x \eqref{xmom}- \p_y \eqref{ymom}$ and using $u_x + v_y = - w_z$,
\beq
w_{zt} + f \omega - u f_y  -  \fh w_x - \hlap p =  \p_x \big ( \bu \bcdot \bnabla u \big ) + \p_y \left ( \bu \bcdot \bnabla v \right ) \per
\label{divergence}
\eeq
The vertical vorticity equation is obtained from $\p_x \eqref{ymom} - \p_y \eqref{xmom}$,
\beq
\omega_t - f w_z + v f_y = - \pnabla \bcdot \left ( \bu \bcdot \bnabla \right ) \bu \per
\label{vorticity}
\eeq
Yes! In the penultimate step we calculate $\p_z \p_t \eqref{divergence} - f \p_z \eqref{vorticity}$, yielding
\beq
\Big [  \left ( \p_t^2 + f^2 \right ) \p_z^2 - \fh \p_x \p_z \p_t \Big ] w - \hlap p_{zt} = f_y \p_z \left ( u_t + f v \right ) + \p_z \left ( \p_t \bnabla + f \pnabla \right ) \bcdot \left ( \bu \bcdot \bnabla \right ) \buh \per
\label{divVort}
\eeq
Finally, the combination $\hlap \eqref{oscillation} + \eqref{divVort}$ yields the wave operator form,
\beq
\begin{split}
\Big [&  \lap \p_t^2 + f^2 \p_z + N^2 \hlap \Big ] w = \fh \p_t \left ( \hlap u + w_{xz} \right ) + u_t \fh_{yy} + 2 u_{yt} \fh_{y}  + f_y \p_z \left ( u_t + f v \right ) \\
& \qquad  + \p_z \left ( \p_t \bnabla + f \pnabla \right ) \bcdot \left ( \bu \bcdot \bnabla \right ) \bu - \hlap \left ( \bu \bcdot \bnabla b \right ) - \lap \p_t \left ( \bu \bcdot \bnabla w \right ) \per
\end{split}
\label{waveOperatorEqn}
\eeq

\bibliographystyle{jfm}
\bibliography{refs}

\end{document}