\documentclass[a4paper,12pt]{article}
\usepackage{cdblatex}
\usepackage{hyperref}
\usepackage{geometry}
\usepackage{summary}
\usepackage{config}

\geometry{a4paper,landscape,margin=2cm}
% \geometry{a4paper,portrait,margin=2cm}
\hypersetup{colorlinks=true}% use false for journals and paper
\numberwithin{equation}{section}% requires amsmath

\begin{document}

% =================================================================================================
\section*{Notes}

The convention for the curvature used in these notes conforms to that of Misner-Thorne-Wheeler
(MTW, eq. 11.12) , namely
\begin{align*}
   V^{a}{}_{;bc} - V^{a}{}_{;cb} = - R^{a}{}_{dbc} V^{d}
\end{align*}

Also, note the following shorthand for mixed covariant derivatives
\begin{align*}
   \nabla_a\left(\nabla_b\right) &= \nabla_{ab}\\
   \nabla_a\left(\nabla_b\left(\nabla_c\right)\right) &= \nabla_{abc}\\
   \nabla_a\left(\nabla_b\left(\nabla_c\left(\nabla_d\right)\right)\right) &= \nabla_{abcd}
\end{align*}
and so on.

See for example the Python function {\tts combine_nabla} in {\tts cadabra/dRabcd.tex}.

In terms of $\nabla$ the above MTW definition of $R^{a}{}_{bcd}$ can written as
\begin{align*}
   \left(\nabla_{cb}-\nabla_{bc}\right) V^{a} = - R^{a}{}_{dbc} V^{d}
\end{align*}

% -------------------------------------------------------------------------------------------------
\subsection*{Symmetrisation}
\input{lib/dGamma.cdbtex}

In the following pages there will be frequent constructions of the form
\begin{dgroup*}
   \begin{dmath*}  3 A^b A^c\Gamma^a{}_{d(b,c)} = \cdb{scaled1.002} \end{dmath*}
   \begin{dmath*}  6 A^b A^c A^e \Gamma^a{}_{d(b,ce)} = \cdb{scaled2.002} \end{dmath*}
   \begin{dmath*} 15 A^b A^c A^e A^f \Gamma^a{}_{d(b,cef)} = \cdb{scaled3.002} \end{dmath*}
\end{dgroup*}
The vector $A^{a}$ has no special meaning. Its purpose is to indicate that the
associciated tensor is symmetric over a selection of its indices. If the $A^{a}$ were not included
then the right hand side would either need to be spelt out in full or some other device would
be needed to denote the symmetries. The symmetrisation brackets are included on the left hand
side though they are redundant (in the presence of the $A^{a}$).

\clearpage

% =================================================================================================
\section*{The metric in RNC}
\input{lib/metric.cdbtex}

\begin{dgroup*}
   \begin{dmath*} g_{a b}(x) = \cdb{Metric.601}+\BigO{\eps^6} \end{dmath*}
\end{dgroup*}

% =================================================================================================
\section*{Curvature expansion of the metric}
\begin{align*}
     g_{a b}(x) =
     \ngab{0}_{a b}
   + \ngab{2}_{a b}
   + \ngab{3}_{a b}
   + \ngab{4}_{a b}
   + \ngab{5}_{a b}+\BigO{\eps^6}
\end{align*}
\begin{dgroup*}
   \begin{dmath*}     \ngab{0}_{a b} = \cdb{scaled0.601} \end{dmath*}
   \begin{dmath*}   3 \ngab{2}_{a b} = \cdb{scaled2.601} \end{dmath*}
   \begin{dmath*}   6 \ngab{3}_{a b} = \cdb{scaled3.601} \end{dmath*}
   \begin{dmath*} 180 \ngab{4}_{a b} = \cdb{scaled4.601} \end{dmath*}
   \begin{dmath*}  90 \ngab{5}_{a b} = \cdb{scaled5.601} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The inverse metric in RNC}
\input{lib/metric-inv.cdbtex}

\begin{dgroup*}
   \begin{dmath*} g^{a b}(x) = \cdb{Metric.601}+\BigO{\eps^6} \end{dmath*}
\end{dgroup*}

% =================================================================================================
\section*{Curvature expansion of the inverse metric}
\begin{align*}
     g^{a b}(x) =
     \ngab{0}^{a b}
   + \ngab{2}^{a b}
   + \ngab{3}^{a b}
   + \ngab{4}^{a b}
   + \ngab{5}^{a b}+\BigO{\eps^6}
\end{align*}
\begin{dgroup*}
   \begin{dmath*}    \ngab{0}^{a b} = \cdb{scaled0.601} \end{dmath*}
   \begin{dmath*}  3 \ngab{2}^{a b} = \cdb{scaled2.601} \end{dmath*}
   \begin{dmath*}  6 \ngab{3}^{a b} = \cdb{scaled3.601} \end{dmath*}
   \begin{dmath*} 60 \ngab{4}^{a b} = \cdb{scaled4.601} \end{dmath*}
   \begin{dmath*} 90 \ngab{5}^{a b} = \cdb{scaled5.601} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The metric determinant in RNC}
\input{lib/detg2.cdbtex}

\begin{dgroup*}
   \Dmath*{-\det g(x) = \cdb{Ndetg.701}+\BigO{\eps^6}}
\end{dgroup*}

% =================================================================================================
\section*{The metric Jacobian in RNC}

\begin{dgroup*}
   \Dmath*{\sqrt{-\det g(x)} = \cdb{sqrtNdetg.801}+\BigO{\eps^6}}
\end{dgroup*}

% =================================================================================================
\section*{The log of detg in RNC}

\begin{dgroup*}
   \Dmath*{\log\left(-\det g(x)\right) = \cdb{logNdetg.901}+\BigO{\eps^6}}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The connection in RNC}
\input{lib/connection.cdbtex}

\begin{dgroup*}
   \begin{dmath*} A^a A^b \Gamma^{d}_{a b} = \cdb{Gamma.301} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} 360 A^a A^b \Gamma^{d}_{a b} = \cdb{Scaled.301} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Curvature expansion of the connection}
\begin{align*}
     A^a A^b \Gamma^{d}_{a b} =
     A^a A^b \nGamma{2}^{d}{}_{a b}
   + A^a A^b \nGamma{3}^{d}{}_{a b}
   + A^a A^b \nGamma{4}^{d}{}_{a b}
   + A^a A^b \nGamma{5}^{d}{}_{a b}+\BigO{\eps^6}
\end{align*}
\begin{dgroup*}
   \begin{dmath*}   3 A^a A^b \nGamma{2}^{d}_{a b} = \cdb{scaled2.301} \end{dmath*}
   \begin{dmath*}  12 A^a A^b \nGamma{3}^{d}_{a b} = \cdb{scaled3.301} \end{dmath*}
   \begin{dmath*} 360 A^a A^b \nGamma{4}^{d}_{a b} = \cdb{scaled4.301} \end{dmath*}
   \begin{dmath*} 180 A^a A^b \nGamma{5}^{d}_{a b} = \cdb{scaled5.301} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Symmetrised partial derivatives of the connection}
\input{lib/dGamma.cdbtex}

\begin{dgroup*}
   \begin{dmath*}   3 A^b A^c\Gamma^a{}_{d(b,c)} = \cdb{scaled1.002} \end{dmath*}
   \begin{dmath*}   6 A^b A^c A^e \Gamma^a{}_{d(b,ce)} = \cdb{scaled2.002} \end{dmath*}
   \begin{dmath*}  15 A^b A^c A^e A^f \Gamma^a{}_{d(b,cef)} = \cdb{scaled3.002} \end{dmath*}
   \begin{dmath*}   9 A^b A^c A^e A^f A^g \Gamma^a{}_{d(b,cefg)} = \cdb{scaled4.002} \end{dmath*}
   \begin{dmath*} 252 A^b A^c A^e A^f A^g A^h\Gamma^a{}_{d(b,cefgh)} = \cdb{scaled5.002} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Symmetrised partial derivatives of $R^a{}_{bcd}$}
\input{lib/dRabcd.cdbtex}

\begin{dgroup*}
   \begin{dmath*}    A^c A^d A^e R^a{}_{cdb,e} = \cdb{scaled1.601} \end{dmath*}
   \begin{dmath*}    A^c A^d A^e A^{f} R^a{}_{cdb,ef} = \cdb{scaled2.601} \end{dmath*}
   \begin{dmath*} -2 A^c A^d A^e A^{f} A^{g} R^a{}_{cdb,efg} = \cdb{scaled3.601} \end{dmath*}
   \begin{dmath*} -5 A^c A^d A^e A^{f} A^{g} A^{h} R^a{}_{cdb,efgh} = \cdb{scaled4.601} \end{dmath*}
   \begin{dmath*} -3 A^c A^d A^e A^{f} A^{g} A^{h} A^{i}R^a{}_{cdb,efghi} = \cdb{scaled5.601} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The generalised connection in RNC}
\input{lib/genGamma.cdbtex}

\begin{dgroup*}
   \begin{dmath*} A^b A^c \Gamma^{a}_{b c} = \cdb{genGamma0.000} \end{dmath*}
   \begin{dmath*} A^b A^c A^d \Gamma^{a}_{b c d} = \cdb{genGamma1.000} \end{dmath*}
   \begin{dmath*} A^b A^c A^d A^e \Gamma^{a}_{b c d e} = \cdb{genGamma2.000} \end{dmath*}
   \begin{dmath*} A^b A^c A^d A^e A^f \Gamma^{a}_{b c d e f} = \cdb{genGamma3.000} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The generalised connection in RNC}

This is the same as the previous page but with a small change in the format to avoid fractions.

\begin{dgroup*}
   \begin{dmath*} 360 A^b A^c \Gamma^{a}_{b c} = \cdb{scaledGamma0.001} \end{dmath*}
   \begin{dmath*} 360 A^b A^c A^d \Gamma^{a}_{b c d} = \cdb{scaledGamma1.001} \end{dmath*}
   \begin{dmath*}  90 A^b A^c A^d A^e \Gamma^{a}_{b c d e} = \cdb{scaledGamma2.001} \end{dmath*}
   \begin{dmath*}   3 A^b A^c A^d A^e A^f \Gamma^{a}_{b c d e f} = \cdb{scaledGamma3.001} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Convert from generic (x) to local RNC coords (y)}
\input{lib/gen2rnc.cdbtex}

\begin{align*}
   y^a = \ny{0}^{a} + \ny{1}^{a} + \ny{2}^{a} + \ny{3}^{a} + \ny{4}^{a}
\end{align*}

\begin{dgroup*}
   \begin{dmath*}     \ny{0}^{a} = \cdb{scaled1.002} \end{dmath*}
   \begin{dmath*}   2 \ny{1}^{a} = \cdb{scaled2.002} \end{dmath*}
   \begin{dmath*}   6 \ny{2}^{a} = \cdb{scaled3.002} \end{dmath*}
   \begin{dmath*}  24 \ny{3}^{a} = \cdb{scaled4.002} \end{dmath*}
   \begin{dmath*} 360 \ny{4}^{a} = \cdb{scaled5.002} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{The geodesic ivp}
\input{lib/geodesic-ivp.cdbtex}

\begin{align*}
   x^{a}(s) = x^{a}
            + s {\dot{x}^a}
            + \frac{s^2}{2!} {\dot{x}^b} {\dot{x}^c} A^{a}_{bc}
            + \frac{s^3}{3!} {\dot{x}^b} {\dot{x}^c} {\dot{x}^d} A^{a}_{bcd}
            + \frac{s^4}{4!} {\dot{x}^b} {\dot{x}^c} {\dot{x}^d} {\dot{x}^e} A^{a}_{bcde}
            + \frac{s^5}{5!} {\dot{x}^b} {\dot{x}^c} {\dot{x}^d} {\dot{x}^e} {\dot{x}^f} A^{a}_{bcdef}
            + \dotsb
\end{align*}
\begin{dgroup*}
   \begin{dmath*} 360 A^{a}_{bc} = \cdb{sterm2.002} \end{dmath*}
   \begin{dmath*} 360 A^{a}_{bcd} = \cdb{sterm3.002} \end{dmath*}
   \begin{dmath*}  90 A^{a}_{bcde} = \cdb{sterm4.002} \end{dmath*}
   \begin{dmath*}   3 A^{a}_{bcdef} = \cdb{sterm5.002} \end{dmath*}
\end{dgroup*}
\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $R$}
\input{lib/geodesic-bvp.cdbtex}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.601} + \BigO{s^3,\eps^3} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + \BigO{s^3,\eps^3}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \BigO{\eps^3} \end{dmath*}
   \begin{dmath*} -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla R$}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.602} + \BigO{s^4,\eps^4} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + \BigO{s^4,\eps^4}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \BigO{\eps^4} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \BigO{\eps^4} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
\end{dgroup*}

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla^2 R$}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.603} + \BigO{s^5,\eps^5} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + (s-s^4) x^{a}_4
                     + \BigO{s^5,\eps^5}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \nx{4}^{a}_2 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
   \begin{dmath*} -720 \nx{4}^{a}_2 = \cdb{Rterm24.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \nx{4}^{a}_3 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
   \begin{dmath*}  -720 \nx{4}^{a}_3 = \cdb{Rterm34.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_4 = \nx{4}^{a}_4 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}  -180 \nx{4}^{a}_4 = \cdb{Rterm44.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla^3 R$}

The geodesic that connects the points with RNC coordinates $x^a$ and $x^a+Dx^a$ is described, for $0\le s\le 1$, by
%
% \begin{dgroup*}
%    \begin{dmath*} x^{a}(s) = \cdb{bvp.604} + \BigO{s^6,\eps^6} \end{dmath*} % too big for pdfLaTeX
% \end{dgroup*}
%
\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + (s-s^4) x^{a}_4
                     + (s-s^5) x^{a}_5
                     + \BigO{s^6,\eps^6}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \nx{4}^{a}_2 + \nx{5}^{a}_2 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
   \begin{dmath*} -720 \nx{4}^{a}_2 = \cdb{Rterm24.102} \end{dmath*}
   \begin{dmath*} -360 \nx{5}^{a}_2 = \cdb{Rterm25.102} \end{dmath*}
\end{dgroup*}

\clearpage

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \nx{4}^{a}_3 + \nx{5}^{a}_3 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
   \begin{dmath*}  -720 \nx{4}^{a}_3 = \cdb{Rterm34.102} \end{dmath*}
   \begin{dmath*} -1080 \nx{5}^{a}_3 = \cdb{Rterm35.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_4 = \nx{4}^{a}_4 + \nx{5}^{a}_4 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}  -180 \nx{4}^{a}_4 = \cdb{Rterm44.102} \end{dmath*}
   \begin{dmath*} -2160 \nx{5}^{a}_4 = \cdb{Rterm45.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_5 = \nx{5}^{a}_5 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*} -360 \nx{5}^{a}_5 = \cdb{Rterm55.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic arc-length}
\input{lib/geodesic-lsq.cdbtex}

\begin{dgroup*}[spread=5pt]
   % LCB: which of these is correct?
   % \begin{dmath*} \left(\Delta s\right)^2 = \cdb{lsq.301} + \BigO{\eps^6,Dx^6} \end{dmath*}
   % \begin{dmath*} \left(\Delta s\right)^2 = \cdb{lsq.301} + \BigO{\eps^6,Dx^7} \end{dmath*}
   \begin{dmath*} \left(\Delta s\right)^2 = \cdb{lsq.301} + \BigO{\eps^6} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic arc-length curvature expansion}

\begin{align*}
   % LCB: which of these is correct?
   % \left(\Delta s\right)^2 = \nD{0} + \nD{2} + \nD{3} + \nD{4} + \nD{5} + \BigO{\eps^6,Dx^6}
   % \left(\Delta s\right)^2 = \nD{0} + \nD{2} + \nD{3} + \nD{4} + \nD{5} + \BigO{\eps^6,Dx^7}
   \left(\Delta s\right)^2 = \nD{0} + \nD{2} + \nD{3} + \nD{4} + \nD{5} + \BigO{\eps^6}
\end{align*}

\begin{dgroup*}[spread=5pt]
   \begin{dmath*}      \nD{0} = \cdb{scaled0.301} \end{dmath*}
   \begin{dmath*}    3 \nD{2} = \cdb{scaled2.301} \end{dmath*}
   \begin{dmath*}   12 \nD{3} = \cdb{scaled3.301} \end{dmath*}
   \begin{dmath*}  360 \nD{4} = \cdb{scaled4.301} \end{dmath*}
   \begin{dmath*} 1080 \nD{5} = \cdb{scaled5.301} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Tranformation between two RNC frames}
\input{lib/rnc2rnc.cdbtex}

\begin{align*}
   y^{a} = \ny{0}^{a} + \ny{2}^{a} + \ny{3}^{a} + \ny{4}^{a} + \ny{5}^{a} + \BigO{\eps^6}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} \ny{0}^{a} = Dx^{a} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \ny{2}^{a} = \ny{2}^{a}_1 \end{dmath*}
   \begin{dmath*}   3 \ny{2}^{a}_1 = \cdb{xDxterm12.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \ny{3}^{a} = \ny{3}^{a}_1 + \ny{3}^{a}_2 \end{dmath*}
   \begin{dmath*} -12 \ny{3}^{a}_1 = \cdb{xDxterm13.102} \end{dmath*}
   \begin{dmath*} -24 \ny{3}^{a}_2 = \cdb{xDxterm22.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \ny{4}^{a} = \ny{4}^{a}_1 + \ny{4}^{a}_2 + \ny{4}^{a}_3 \end{dmath*}
   \begin{dmath*} -180 \ny{4}^{a}_1 = \cdb{xDxterm14.102} \end{dmath*}
   \begin{dmath*} -720 \ny{4}^{a}_2 = \cdb{xDxterm23.102} \end{dmath*}
   \begin{dmath*} -720 \ny{4}^{a}_3 = \cdb{xDxterm32.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \ny{5}^{a} = \ny{5}^{a}_1 + \ny{5}^{a}_2 + \ny{5}^{a}_3 + \ny{5}^{a}_4 \end{dmath*}
   \begin{dmath*}  -360 \ny{5}^{a}_1 = \cdb{xDxterm15.102} \end{dmath*}
   \begin{dmath*} -2160 \ny{5}^{a}_2 = \cdb{xDxterm24.102} \end{dmath*}
   \begin{dmath*} -1080 \ny{5}^{a}_3 = \cdb{xDxterm33.102} \end{dmath*}
   \begin{dmath*}  -360 \ny{5}^{a}_4 = \cdb{xDxterm42.102} \end{dmath*}
\end{dgroup*}

\end{document}


\begin{intro}
  In this section, we develop error estimates of the following type
  \begin{verse}
    If the size of mesh cells converges to zero, then the difference
    between the true solution and the finite element solution
    converges to zero with a certain order.
  \end{verse}
  They are thus a prediction, that the solutions actually converge,
  and they measure the asymptotic convergence rate. They do contain
  unknown constants, such that they are no prediction of the error on
  a given mesh.

  The theory in this chapter is about bilinear forms which are bounded
  and elliptic on a subspace $V \subset H^1(\domain)$ determined by
  boundary conditions. We will choose $V = H^1_0(\domain)$, even if
  more general boundary conditions are possible. It is a good approach
  to think of the Dirichlet problem for the Laplacian, even if we
  allow for somewhat more general equations.
\end{intro}

\subsection{Approximation of Sobolev spaces by finite elements}

\begin{Lemma*}{poincare}{Poincaré inequality}
  Let $\domain$ be a bounded Lipschitz domain. For any function
  $u\in H^1(\domain)$ define
  \begin{gather}
    \overline u = \frac1{\abs{\domain}} \int_\domain u(\vx) \dvx,
  \end{gather}
  where $\abs{\domain}$ denotes the measure of $\domain$. There exists
  a constant $c$ depending on the domain only, such that each of the
  following inequalities hold:
  \begin{align}
    \norm{u-\overline u}_{L^2(\domain)} &\le c \norm{\nabla u}_{L^2(\domain)}\\
    \norm{u}_{L^2(\domain)}^2 &\le c \Bigl(\norm{\nabla u}_{L^2(\domain)}^2 + \overline u^2\Bigr)
  \end{align}
\end{Lemma*}

\begin{proof}
  The proof exceeds the tools we have developed in this class. The
  proof in \cite[Section 7.8]{GilbargTrudinger98} seems elementary and
  direct, but is technical and requires star-shaped domains. The proof
  in \cite[Section 5.8.1]{Evans98} is more elegant, but it uses
  compact embedding and is indirect, such that the constant cannot be
  determined.
\end{proof}

\begin{Lemma*}{bramble-hilbert}{Bramble-Hilbert}
  Let $\cell\subset \R^d$ be a domain with Lipschitz boundary and let
  $s(.)$ be a bounded sublinear functional on $H^{k+1}(\cell)$ with
  the property
  \begin{gather}
    s(p) = 0 \qquad\forall p\in \P_k.
  \end{gather}
  Then, there exists a constant $c$ only dependent on $\cell$ such that
  \begin{gather}
    \abs{s(v)} \le c \snorm{v}_{k+1,\cell}.
  \end{gather}
\end{Lemma*}

\begin{proof}
  Since $s(\cdot)$ is sublinear and vanishes on $\P_k$, we have for
  $v\in H^{k+1}(\cell)$:
  \begin{gather}
    \label{eq:bramble-hilbert-1}
    \abs{s(v)} \le \abs{s(v+p)} + \abs{s(p)} = \abs{s(v+p)}
    \qquad\forall p\in\P_k.
  \end{gather}
  We will construct a polynomial, such that
  \begin{gather}
    \label{eq:bramble-hilbert-2}
    \overline{\d^\alpha(v+p)}
    = \frac1{\abs{\cell}}\int_\cell \d^\alpha (v+p) \dx = 0
    \qquad\forall \abs{\alpha} \le k,
  \end{gather}
  that is, the sum $v+p$ and all its derivatives up to order $k$ are
  mean-value free. Thus, by recursive application of Poincaré
  inequality, we get for $\abs{\alpha}\le k$
  \begin{alignat*}2
    \norm{v+p}_{L^2(\cell)}^2
    &\le c \left[ \norm{\nabla(v+p)}_{L^2(\cell)}^2 + \overline{v+p}^2\right]
      && \le c \snorm{v+p}_{1;\cell}^2\\
    \norm{\d^\alpha(v+p)}_{L^2(\cell)}^2,
    &\le c \left[ \norm{\nabla\d^\alpha(v+p)}_{L^2(\cell)}^2
      + \overline{\d^\alpha(v+p)}^2\right]
      && \le c \snorm{v+p}_{\abs{\alpha}+1;\cell}^2,
  \end{alignat*}
  such that $\norm{v+p}_{k+1;\cell} \le \snorm{v+p}_{k+1;\cell}$.
  Furthermore, since $p\in\P_k$
  \begin{gather*}
    \norm{\d^\alpha(v+p)}_{L^2(\cell)} = \norm{\d^\alpha v}_{L^2(\cell)}
    \qquad\forall \abs{\alpha} = k+1.
  \end{gather*}
  Combining with~\eqref{eq:bramble-hilbert-1}, we obtain
  \begin{gather*}
    \abs{s(v)} \le c \snorm{v+p}_{k+1;\cell} \le c \snorm{v}_{k+1;\cell}.
  \end{gather*}
  It remains to construct the polynomial $p$ with the desired
  properties. To this end, we note that for two multi-indices $\alpha$
  and $\beta$ holds that $\d^\alpha \vx^\beta =0$ as soon as
  $\alpha_i>\beta_i$ for some index $i$. Let
  \begin{gather*}
    p(\vx) = \sum_{\abs{\beta}\le k} a_\beta \vx^\beta.
  \end{gather*}
  Then, for any $\abs{\alpha} = k$ we get
  \begin{gather*}
    \d^\alpha p(\vx) = \alpha! \,\delta_{\alpha\beta},
  \end{gather*}
  where $\alpha! = \alpha_1!\alpha_2!\dots\alpha_d!$. Thus, we can use
  condition~\eqref{eq:bramble-hilbert-2} to fix the coefficients
  $a_\beta$ to
  \begin{gather*}
    a_\beta = \frac1{\beta!\,\abs{\cell}} \int_\cell \d^\beta v\dx
    \qquad \abs{\beta} = k.
  \end{gather*}
  Thus, we have decomposed $p = \tilde p_k + p_{k-1}$, where
  $\tilde p_k$ is known and $p_{k-1}\in \P_{k-1}$. Thus, we can repeat
  the process determining the coefficients of highest order in
  $p_{k-1}$,
  \begin{gather*}
    a_\beta = \frac1{\beta!\,\abs{\cell}} \int_\cell \d^\beta (v-\tilde p_k)\dx
    \qquad \abs{\beta} = k-1.
  \end{gather*}
  Recursion down to $k=0$ yields the polynomial $p$ with the desired property.
\end{proof}

\begin{Corollary}{b-h-projector}
  Let $\Pi\colon H^{k+1}(\cell) \to \P_k$ be a continuous, linear
  projector. For any $m\le k$ there exists a constant $c$ such that
  \begin{gather}
    \norm{u-\Pi u}_{m,\cell} \le c \snorm{u}_{k+1,\cell}.
  \end{gather}
\end{Corollary}

\begin{Definition}{mesh-family}
  Let $\{\mesh_h\}$ for $h>0$ be a family of meshes parametrized by
  the parameter
  \begin{gather}
    h = \max_{\cell\in\mesh_h} h_\cell,
  \end{gather}
  where $h_\cell$ is the characteristic length from the discussion of
  mappings, for instance the diameter of $\cell$. Such a family is
  called \define{shape regular}, if the constants $M_\cell$,
  $m_\cell$, $d_\cell$, and $D_\cell$ in the scaling lemma
  can be chosen independent of the cell $\cell\in\mesh_h$ and of
  $h>0$. The family is called \define{quasi-uniform}, if in addition
  there is a positive constant independent of $h$ such that
  \begin{gather}
    h \le c \min_{\cell\in\mesh_h} h_\cell.
  \end{gather}
\end{Definition}

\begin{Definition}{nodal-interpolation}
  Let $V$ be a function space on $\domain$, and let
  $V_h = V_{\mesh_h}$ be a finite element space on the mesh $\mesh_h$
  on $\domain$ with node functionals $\nodal_i$ and dual basis $p_i$,
  where $i=1,\dots,n_h$. We define the \define{nodal interpolation}
  operator by
  \begin{gather}
    \begin{split}
      I_h\colon V&\to V_h\\
      v &\mapsto \sum \nodal_i(v)p_i.
    \end{split}
  \end{gather}
\end{Definition}

\begin{Lemma}{nodal-interpolation}
  The nodal interpolation operator $I_h$ is a projector. It is
  continuous on $H^2(\domain)$ if the dimension is $d=2,3$ and the
  node functionals are defined as Lagrange interpolation.
\end{Lemma}

\begin{Definition}{broken-sobolev-norm}
  On a mesh $\mesh_h$, we define the \define{broken Sobolev norm} and
  seminorm by
  \begin{gather}
    \begin{split}
      \norm{u}_{k;h}^2 &= \sum_{\cell\in\mesh_h} \norm{u}_{k;\cell}^2\\
      \snorm{u}_{k;h}^2 &= \sum_{\cell\in\mesh_h} \snorm{u}_{k;\cell}^2
    \end{split}
  \end{gather}
\end{Definition}

\begin{Theorem}{fe-interpolation}
  Let $\{\mesh_h\}$ be a shape regular family of meshes.
  Let the finite element spaces $V_h = V_{\mesh_h}$. Let the nodal
  interpolation operator $I_h$ be surjective onto $\P_k$ on every cell
  $\cell\in\mesh_h$ and continuous on $H^{k+1}(\domain)$. Then, there
  is a constant $c$ such that for any $u\in H^{k+1}(\domain)$ and
  $m\le k+1$ there holds
  \begin{gather}
    \label{eq:fe-interpolation}
    \norm{u-I_h u}_{m;h} \le c h^{k+1-m} \snorm{u}_{k+1;h}.
  \end{gather}
\end{Theorem}

\begin{proof}
  We have by definition
  \begin{gather*}
    \snorm{u-I_h u}_{m;h} = \sum_{\cell\in\mesh_h} \snorm{u-I_h u}_{m;\cell}.
  \end{gather*}
  Using the scaling lemma, we get
  \begin{gather*}
    \snorm{u-I_h u}_{m;\cell}
    \le c h_\cell^{\nicefrac d2-m} \snorm{\refu - \widehat{I_hu}}.
  \end{gather*}
  On the reference cell, we use the Bramble-Hilbert lemma, more
  precisely, \slideref{Corollary}{b-h-projector} to obtain
  \begin{gather*}
    \snorm{\refu - \widehat{I_hu}} \le c \snorm{\refu}_{k+1;\refcell}.
  \end{gather*}
  Scaling back yields
  \begin{gather*}
    \snorm{\refu}_{k+1;\refcell}
    \le c h_\cell^{k+1-\nicefrac d2} \snorm{u}_{k+1;\cell}.
  \end{gather*}
  Combining, we obtain
  \begin{gather*}
    \snorm{u-I_h u}_{m;\cell} \le c h_\cell^{k+1-\nicefrac d2 - m +
      \nicefrac d2}\snorm{u}_{k+1;\cell}.
  \end{gather*}
  Summing up and pulling the maximum of $h_\cell^{k+1-m}$ out of the
  sum yields the result for $h_\cell \le 1$.
\end{proof}

\begin{remark}
  Strictly speaking, we have only proven the result for $h_T \le
  1$.
  But then, if $\diam\domain = 1$, this condition is always
  true. Therefore, by rescaling the domain before computing, the
  estimate holds in general.
\end{remark}

\begin{Corollary}{fe-approximation}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $V = H^1_0(\domain)$ and let the finite element space $V_h$ be
  defined on a shape-regular family of meshes $\{\mesh_h\}$, such that
  the interpolation estimate~\eqref{eq:fe-interpolation} holds. If
  furthermore the solution $u\in H^{k+1}(\domain)$, the error of the
  finite element solution $u_h\in V_h \subset V$ admits the estimate
  \begin{gather}
    \norm{u-u_h}_{1;h} \le c h^{k} \snorm{u}_{k+1;h}.
  \end{gather}
\end{Corollary}

\begin{intro}
  For 2nd order elliptic problems, we have now derived estimates of
  the $H^1$-norm of the error under the assumption that the solution
  exhibits further regularity. Now, let us drop this assumptionfor an
  assumption on the boundary condition only, to obtain a qualitative
  convergence result.
\end{intro}

\begin{Theorem}{fe-convergence}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $V = H^1_0(\domain)$ and let the finite element space $V_h$ be
  defined on a shape-regular family of meshes $\{\mesh_h\}$, such that
  the interpolation estimate~\eqref{eq:fe-interpolation} holds. Let
  $u\in V$ and $u_h\in V_h$ be solutions to the exact and the finite
  element versions of a 2nd order boundary value problem. Then,
  \begin{gather}
    \lim_{h\searrow 0} \norm{u-u_h}_{1;\domain} = 0.
  \end{gather}
\end{Theorem}

\subsection{Estimates of stronger norms}

\begin{intro}
  So far, we have seen error estimates in a ``natural norm'' defined
  as a norm such that the Lax-Milgram lemma holds for a given bilinear
  form $a(.,.)$. In this subsection, we now consider the question of
  estimates in stronger norms, such that the bilinear form is not
  elliptic with respect to this norm.
\end{intro}

\begin{Definition}{stronger-norm}
  Let $\norm{\cdot}_X$ and $\norm{\cdot}_Y$ be norms on a vector space
  $V$. We call $\norm{\cdot}_X$ a \define{stronger norm} than
  $\norm{\cdot}_Y$, if there is a constant $c$ such that for all
  $v\in V$:
  \begin{gather}
    \norm{v}_Y \le c \norm{v}_X.
  \end{gather}
  In this case, $\norm{\cdot}_Y$ is called the \define{weaker
    norm}. If a converse inequality holds, the norms are called
  \define{equivalent}.
\end{Definition}

\begin{example}
  For a bounded domain $\domain$, the norms $\norm{\cdot}_{k+1}$ and
  $\norm{\cdot}_{k}$ are both defined on $V = H^1_0(\domain)$. By the
  Sobolev embedding theorem, there is a constant $c$ such that for any
  $v\in V$
  \begin{gather*}
    \norm{v}_{k} \le c \norm{v}_{k+1}
  \end{gather*}
\end{example}

\begin{Lemma*}{inverse-estimate}{Inverse estimate}
  Let $\cell$ be a mesh cell of size $h_\cell$. Then, there is a
  constant only depending on $k$ and the constants of the scaling
  lemma, such that for every $u\in \P_k$ there holds
  \begin{gather}
    \norm{u}_{1;\cell} \le c h_T^{-1} \norm{u}_{0;\cell}.
  \end{gather}
\end{Lemma*}

\begin{Theorem}{h2-error}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $V\subset H^1(\domain)$ and let $\{\mesh_h\}$ be a family of
  quasi-uniform meshes with finite element spaces $V_h \subset V$
  containing the space $\P_k$ with $k\ge 2$ on each mesh cell. If
  furthermore the solution $u\in H^{k+1}(\domain)$, the error between
  the exact and the finite element solution to a uniquely solvable
  elliptic boundary value problem, admits the estimate
  \begin{gather}
    \norm{u-u_h}_{2;h} \le c h^{k-1} \snorm{u}_{k+1;h}.
  \end{gather}
\end{Theorem}

\begin{proof}
  We cannot apply Céa's lemma directly, since the bilinear form is not
  elliptic with respect to the broken $H^2$-inner product on the space
  $H^1(\domain)$. On the other hand, the error is not polynomial, such
  that we cannot apply the inverse estimate to it. Instead, we use
  triangle inequality
  \begin{gather*}
    \norm{u-u_h}_{2;h} \le \norm{u-I_h u}_{2;h} + \norm{I_h u-u_h}_{2;h},
  \end{gather*}
  and observe, that we already have the desired estimate for the first
  term. For the second, we estimate by inverse estimate on each cell
  \begin{gather*}
    \min_{\cell\in\mesh_h} h_T \norm{I_h u-u_h}_{2;h}
    \norm{I_hu-u_h}_{1} \le c \norm{I_h u-u_h}_{1}^2,
  \end{gather*}
  and
  \begin{align*}
    \norm{I_h u-u_h}_{1}^2
    & \le \frac c\gamma a(I_h u-u_h, I_h u-u_h)
    \\& = \frac c\gamma a(I_h u-u, I_h u-u_h)
    \\& \le \frac{cM}{\gamma}\norm{I_h u-u}_{1} \norm{I_h u-u_h}_{1}
    \\& \le C h^{k} \snorm{u}_{k+1;h} \norm{I_h u-u_h}_{1},
  \end{align*}
  where we have used Galerkin orthogonality and the interpolation
  estimate. Combining the two and using the quasi-uniformity, we
  obtain the result of the theorem.
\end{proof}

\subsection{Estimates of weaker norms and linear functionals}

\begin{intro}
  Deriving optimal error estimates in the $L^2$-norm cannot be
  achieved by the same technique as used for the $H^2$-norm, as the
  following simple argument shows: using triangle inequality, we obtain
  \begin{gather*}
    \norm{u-u_h}_{L^2} \le \norm{u-I_h u}_{L^2} + \norm{I_h u-u_h}_{L^2},
  \end{gather*}
  we obtain that the first term is of order $h^{k+1}$. Thus, for an
  optimal error estimate, we require that the second term be of order
  $h^{k+1}$ as well. We need a replacement of the inverse estimate,
  which gains an order of $h$ instead of loosing it. This is Poincaré
  inequality, but it requires an almost mean-value free function. And
  since $I_h u$ is not the interpolant of $u_h$, we cannot guarantee a
  small mean value of the difference on each mesh cell.

  To the rescue comes a ``duality argument'' known as Aubin-Nitsche
  trick, which we introduce now.
\end{intro}

\begin{Definition}{dual-problem}
  Let the weak form of a boundary value problem on the domain
  $\domain$ be defined as: find $u\in V$ such that
  \begin{gather*}
    a(u,v) = f(v) \qquad\forall v\in V.
  \end{gather*}
  Then, the \define{dual problem}, also called \define{adjoint
    problem}, with right hand side $g\in V^*$ is: find $u^* \in V$ such
  that
  \begin{gather*}
    a(v,u^*) = g(v) \qquad\forall v\in V.
  \end{gather*}
\end{Definition}

\begin{Lemma}{poisson-dual}
  The adjoint problem of the Dirichlet boundary value problem for
  Poisson's equation is equal to the dual problem, that is, for
  $u\in H^1_0(\domain)$, the two statements
  \begin{xalignat*}2
    a(u,v) &= f(v) &\forall v&\in V,\\
    a(v,u) &= f(v) &\forall v&\in V,
  \end{xalignat*}
  are equivalent.
\end{Lemma}

\begin{Assumption*}{elliptic-regularity}{Elliptic regularity}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $H^1_0(\domain)$. We say that a boundary value problem has
  \define{elliptic regularity}, if for any $g\in L^2(\domain)$ the
  solution $u$ is in $H^2(\domain)$. In other words, there is a
  constant $c$ independent of $g$, such that
  \begin{gather}
    \norm{u}_{2;\domain} \le c \norm{g}_{0;\domain}.
  \end{gather}
\end{Assumption*}

\begin{example}
  By \slideref{Remark}{classical-convex}, a second order PDE with
  coefficients $a_{ij}\in C^{0,1}(\overline\domain)$ and
  $b_i, c\in L^\infty(\domain)$ has elliptic regularity. The same
  holds by integration by parts for ints adjoint.

  The same boundary value problem does not have elliptic regularity,
  if the domain has a nonconvex corner, since we have corner
  singularity functions which are not in $H^2(\domain)$, and we can
  construct a right hand side $g\in L^2(\domain)$, which produces such
  singularities.
\end{example}

\begin{Theorem}{fe-l2}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $V = H^1_0(\domain)$ and let the finite element space $V_h$ be
  defined on a shape-regular family of meshes $\{\mesh_h\}$, such that
  the interpolation estimate~\eqref{eq:fe-interpolation} holds. If
  furthermore the solution $u\in H^{k+1}(\domain)$ and the dual
  problem has \putindex{elliptic regularity}, the error of the finite
  element solution $u_h\in V_h \subset V$ admits the estimate
  \begin{gather}
    \norm{u-u_h}_{0} \le c h^{k+1} \snorm{u}_{k+1;h}.
  \end{gather}
\end{Theorem}

\begin{Corollary}{fe-functional}
  Under the assumptions of \slideref{Theorem}{fe-l2}, let $J(.)$ be a
  bounded linear functional on $L^2(\domain)$. Then,
  \begin{gather}
    \abs{J(u)-J(u_h)}_{0} \le c h^{k+1} \snorm{u}_{k+1;h}.
  \end{gather}
\end{Corollary}

\subsection{Green's function and maximum norm estimates}

\begin{intro}
  In this section, we will introduce the basic concepts needed for
  pointwise error estimation. They heavily rely on Green's function,
  which is useful for a general understanding of the solution
  structure as well.
\end{intro}

\begin{Definition}{greens-function}
  For a differential equation $Lu = f$ in $\domain$ with boundary
  conditions $u=0$ on the boundary $\d\domain$, we define
  \define{Green's function} $G(\vy,\vx)$ associated to the point
  $\vy\in\domain$ as solution to the problem
  \begin{gather}
    \begin{aligned}
      L G(\vy,\vx) &= \delta(\vx-\vy)&\qquad\forall \vx&\in\domain,\\
      G(\vy,\vx) &= 0&\qquad\forall \vx&\in\d\domain.
    \end{aligned}
  \end{gather}
\end{Definition}

\begin{Theorem}{greens-function-rd}
  Green's function for Poisson's equation on the whole space
  $\domain=\R^d$ is
  \begin{gather}
    G(\vy,\vx) =
    \begin{cases}
      - \frac1{2\pi} \log \abs{\vx-\vy} & d=2\\
      \frac1{d(d-2)\abs{B_1(0)}} \frac1{\abs{\vx-\vy}^{d-2}} & d\ge3.
    \end{cases}
  \end{gather}
\end{Theorem}

\begin{proof}
  See~\cite[Section 2.2]{Evans98}.
\end{proof}

\begin{Lemma}{greens-function-domain}
  Let $\domain\subset \R^d$ be a bounded domain. Then, Green's
  function associated to a point $\vy\in\domain$ for a linear
  differential operator $L$ on $\domain$ is obtained as the sum
  \begin{gather}
    G(\vy,\vx) = G_\infty(\vy,\vx) - G_0(\vy,\vx),
  \end{gather}
  where $G_\infty(\vy,\vx)$ is Green's function for the whole space
  and $G_0(\vy,\vx)$ solves $LG_0(\vy,\vx)=0$ with boundary values
  $G_\infty(\vy,\vx)$. If the domain is convex, then,
  $G_0(\vy,.)\in H^2(\domain)$ for any interior point $\vy$.
\end{Lemma}

\begin{Theorem}{greens-function-representation}
  Let $f\in C(\overline\domain)$ and let $\domain$ be such that the
  solution to
  \begin{gather}
    \begin{aligned}
      Lu &= f &\qquad\text{in }&\domain,\\
      u &= 0 &\text{on }&\d\domain,
    \end{aligned}
  \end{gather}
  is in $C^2(\overline\domain)$. Then,
  \begin{gather}
    u(\vx) = \int_\domain f(\vy)G(\vy,\vx)\dvy.
  \end{gather}
\end{Theorem}

\begin{proof}
  See \cite[Section 2.2]{Evans98}
\end{proof}

\begin{Theorem}{linfty-error}
  Let $a(.,.)$ be a bounded and elliptic bilinear form on
  $V = H^1_0(\domain)$ on a bounded, convex domain $\domain\subset \R^2$. Let the
  finite element space $V_h$ be defined on a quasi-uniform family of
  meshes $\{\mesh_h\}$ with local spaces $\P_1$ or $\Q_1$. If
  furthermore the solution $u\in W^{2,\infty}(\domain)$, the error of
  the finite element solution $u_h\in V_h \subset V$ admits the
  estimate
  \begin{gather}
    \norm{u-u_h}_{\infty} \le c h^{2} (1+\abs{\log h}) \snorm{u}_{2,\infty}.
  \end{gather}
\end{Theorem}

\begin{proof}
  The proof relies on solving a dual problem for the error in a single
  point. The solution is Green's function for the adjoint equation,
  which is very irregular at the point of interest. Then, complicated
  analysis is needed to generate useful approximation estimates in
  spite of the singularity. Details can be found
  in~\cite{SchatzWahlbin77,RannacherScott82}.
\end{proof}

\begin{remark}
  This estimate extends to $\domain\in\R^d$ for $d\ge 3$ and to higher
  polynomial degrees \emph{without} the logarithmic factor. The
  regularity assumptions are quite high then.
\end{remark}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


% $Id: 785a41386b8ef8716fb66a73bb45d2dfa3eaee4f $

\section{Equational Reasoning with Rigid Unit Superposition}
\label{sec:super}

There are many ways of integrating equational reasoning in tableau
methods~\cite{DB75,LS02,BR15,DV96}. Because our prover does not rely on clausal
forms, but on arbitrary formulas with quantifiers occurring deep inside
branches, we deal with rigid variables, i.e. variables that can be instantiated
only once. The problem we want to solve, rigid E-unification modulo rewrite
rules, is the following. Assume a set of equations $E$, containing rigid
variables, a rewrite system $\mathcal{RE}$, and target terms $s$ and $t$. We
want a substitution $\sigma$ such that
$\bigwedge_{e \in E} e\sigma \vdash s\sigma =_\mathcal{E} t\sigma$. Such a
substitution is a solution to the rigid E-unification problem.

We propose here an approach based on superposition with rigid variables, as in
previous work by Degtyarev and Voronkov~\cite{DV96} and earlier work on rigid
paramodulation~\cite{DAP00}, but with significant differences. First, in order
to avoid constraint solving, we do not use basic superposition nor
constraints. Second, we introduce a merging rule, which factors together
intermediate (dis)equations that are alpha-equivalent: with multiple instances
of some of the quantified formulas (amplification), it becomes important not to
duplicate work. In this aspect, our calculus is quite close to labeled unit
superposition~\cite{KS10} when using sets as labels. Third, unlike rigid
paramodulation, we use a term ordering to orient the equations.

\subsection{Preliminary Definitions}

We write $ \clauseWithSubst{ s \approx t }{ \Sigma}$ (resp.
$\clauseWithSubst{ s \not\approx t }{ \Sigma}$), the unit clause that contains
exactly one equation (resp.~disequation) under hypothesis $\Sigma$ (which is a
set of substitutions). We write $\clauseWithSubst{\emptyset}{\Sigma}$ for the
empty clause under hypothesis $\Sigma$. We define $\renameVars{e}$, where $e$
is a (dis)equation, as follows: let $\sigma$ map every rigid variable of $e$ to
a fresh non-rigid variable, then
$\renameVars{e} = \clauseWithSubst{ e\sigma }{ \{ \sigma \} }$. For example,
$\renameVars{p(X)\approx a}$ is $\clauseWithSubst{ p(Y)\approx a}{ \{ X\mapsto
Y\} }$. The E-unification problem $E \vdash s\approx t$ can be solved by proving
$\clauseWithSubst{\emptyset }{ \Sigma}$ from
$\{ \renameVars{e} \}_{ e \in E } \cup \{ \renameVars{ s \not\approx t } \}$,
where $\Sigma$ contains the solutions. The meaning of $s \approx t | \Sigma$ is
that for every $\sigma \in \Sigma$, $s \approx t$ is provable using the
substitution $\sigma$ for the metavariables.

As can be noticed, we keep a set of substitutions, rather than unit clauses
paired with individual substitutions, in order to avoid duplicating the work for
alpha-equivalent clauses. Indeed, because of amplification, many instances of a
given (dis)equation might be present in a branch of the tableau. It would be
inefficient to repeat the same inference steps with each variant of the axioms.
Because we apply $\renameVars{e}$ on every initial $e$, clauses do not share any
variable, except in their attached sets of substitutions.

To perform an inference step between two unit (dis)equations, we merge their
sets of substitutions. Merging $\Sigma$ and $\Sigma'$, intuitively, means
computing $\{ \textsf{merge}(\sigma,\sigma') ~|~ \sigma \in \Sigma, \sigma'\in
\Sigma' \}$ for every pair $(\sigma,\sigma')$ of compatible substitutions. For
example, the resolution step between $p(x,x)| \{ X \mapsto a \}$ and
$\lnot p(y,b)| \{ X \mapsto y \}$ is not possible, because the result would need
to map $X$ to $a$ and $b$, which is impossible because $X$ is rigid.
Compatibility relies on a partial ordering $\leq$, such that
$\sigma \leq \sigma'$ means that $\sigma$ is less general than $\sigma'$.

Considering a substitution as a function from variables to terms, we define the
domain of a substitution $\sigma$ as the set of variables that have a
non-trivial binding in $\sigma$.\footnote{A trivial binding maps a variable to
itself.} The co-domain of a substitution is the set of variables occurring in
terms in the image of the domain of the substitution. In the following, we will
consider idempotent substitutions, i.e. substitutions for which the domain and
co-domain have an empty intersection.

The composition of two substitutions $\sigma$ and $\sigma'$, denoted by
$\sigma \circ \sigma'$, is said to be well-defined if and only if the domains of
$\sigma$ and $\sigma'$ have no intersection. In this case,
$\sigma \circ \sigma' \triangleq \left\{ x \mapsto (x\sigma)\sigma' | x \in
\text{domain}(\sigma) \right\}$. This definition extends to sets of
substitutions: $\Sigma \circ \sigma' \triangleq \left\{ \sigma \circ \sigma' |
\sigma \in \Sigma \right\}$. We then have $\sigma \leq \sigma'$ if and only if
$\exists \sigma''.~ \sigma \circ \sigma'' = \sigma'$. This notion also extends
to sets of substitutions: $\smash{ \Sigma \leq \Sigma' }$ if and only if
$\smash{ \forall \sigma' \in \Sigma'.~ \exists \sigma \in \Sigma. \sigma \leq
\sigma' }$. The merging of two substitutions $\sigma \uparrow \sigma'$ is the
supremum of $\{\sigma,\sigma'\}$ for the order $\leq$, if it exists, or $\bot$
otherwise. The merging of sets of substitutions is
$\Sigma \uparrow \Sigma' \triangleq \left\{ \sigma \uparrow \sigma' ~|~ \sigma
\in \Sigma, \sigma' \in \Sigma' \right., \sigma \uparrow \sigma' \not= \bot
\}$. An inference rule is said to be successful if the merging of the premises'
substitution sets is non-empty.

\subsection{Inference System}

In Fig.~\ref{fig:unit-sup-rules}, we present the rules for unit superposition
with rigid variables. We adopt notations and names from Schulz's paper on
E~\cite{SS02}. A single bar denotes an inference, i.e. we add the result to the
saturation set, whereas a double bar is a simplification in which the premises
are replaced by the conclusion(s). The relation $\prec$ is a reduction ordering,
used to orient equations and restrict inferences, thus pruning the search space.
Typically, $\prec$ is one of RPO or KBO. The rules of
Fig.~\ref{fig:unit-sup-rules} work as described below:

\begin{description}
\item[ER] is equality resolution, where a disequation
$\clauseWithSubst{s \not\approx t}\Sigma$ is solved by syntactically unifying
$s$ and $t$ with $\sigma$, if $\sigma$ is compatible with $\Sigma$.
\item[SN] is superposition into negative literals. A subterm of $u$ is rewritten
using $s \approx t$ after unifying it with $s$ by $\sigma$. The rewriting is
done only if $s\sigma \not\preceq t\sigma$, a sufficient (but not necessary)
condition for a ground instance of $s\sigma \approx t\sigma$ to be oriented
left-to-right.
\item[SP] is similar to SN, but superposes into a positive literal.
\item[TD1] deletes trivial equations that will never contribute to a proof.
\item[TD2] deletes clauses with an empty set of substitutions. In practice, we
only apply a rule if the conclusion is labeled with a non-empty set of
substitutions.
\item[ME] merges two alpha-equivalent clauses into a single clause, by merging
the sets of substitutions. This rule is very important in practice, to prevent
the search space from exploding due to the duplicates of most formulas.
Superposition deals with this explosion by removing duplicates using
subsumption, but in our context subsumption is not complete because rigid
variables are only proxy for ground terms: even if $C\sigma \subseteq D$, the
one ground instance of $C$ might not be compatible with the ground instance of
$D$.
\item[ES] is a restricted form of equality subsumption. The active equation
$\clauseWithSubst{ s\approx t}\Sigma $ can be used to delete another clause, as
in E~\cite{SS02}. However, ES only works if $s$ and $t$ are syntactically equal
to the corresponding subterms in the subsumed clause $C$. Otherwise, there is no
guarantee that further instantiations will not make $s\approx t$ incompatible
with $C$. Moreover, $C$ needs not be entirely removed. Only its substitutions
that are compatible with $\Sigma$ are subsumed.
\item[RP] is rewriting of positive clauses, which only works for syntactical
equality, not matching.
\item[RN] is the same as RP but for rewriting negative clauses.
\end{description}

Rule \textbf{SN} (resp. \textbf{SP}) generates as many equations (resp.
disequations) as there are in the set
$(\Sigma \circ \sigma'') \uparrow (\Sigma' \circ \sigma'')$ because all
substitutions may not always be merged. For instance, given
$f(x) = t | \{ \{ X_1 \mapsto x \}, \{ X_2 \mapsto x \} \}$ and
$f(a) = v | \{ \{ X_1 \mapsto a \} \} \}$, we have to derive two distinct
non-mergeable equations $(t = v)\{ x \mapsto a \} | \{ \{ X_1 \mapsto a \} \}$
and $(t = v)\{ x \mapsto a \} | \{ \{ X_1 \mapsto a; X_2 \mapsto a \} \}$.

\begin{figure}[htb]
\begin{center}
% ER
\AXC{$s \not\approx t |\Sigma$}
\LL{ER}
\RL{if $\sigma = \text{mgu}(s, t)$}
\UIC{$\emptyset | \Sigma \circ \sigma $}\DP\\[12pt]

% SN
\AXC{$s \approx t | \Sigma$}
\AXC{$u \not\approx v | \Sigma'$}
\LL{SN}
\BIC{$\sigma''(u[p \leftarrow t] \not\approx v) | \sigma'''$}\DP
$\text{if} \left\{ \begin{array}{l@{\quad}l}
\sigma'' = \text{mgu}(u_{|p}, s) & u_{|p} \not\in V\\
\sigma''(s) \not\preceq \sigma''(t) & \sigma''(u) \not\preceq \sigma''(v)\\
\multicolumn{2}{l}{
\sigma''' \in (\Sigma \circ \sigma'') \uparrow (\Sigma' \circ \sigma'')}
\end{array}\right.$\\[12pt]

% SP
\AXC{$s \approx t | \Sigma$}
\AXC{$u \approx v | \Sigma'$}
\LL{SP}
\BIC{$\sigma''(u[p \leftarrow t] \approx v) | \sigma'''$}\DP
$\text{if} \left\{ \begin{array}{l@{\quad}l}
\sigma'' = \text{mgu}(u_{|p}, s) & u_{|p} \not\in V\\
\sigma''(s) \not\preceq \sigma''(t) & \sigma''(u) \not\preceq \sigma''(v)\\
\multicolumn{2}{l}{
\sigma''' \in (\Sigma \circ \sigma'') \uparrow (\Sigma' \circ \sigma'')}
\end{array}\right.$\\[12pt]

\mbox{
% TD1
\AXC{$s \approx s | \Sigma $}
\LL{TD1}
\doubleLine{}
\UIC{$\top$}\DP

% TD2
\AXC{$s \mathrel{R} t | \emptyset$}
\LL{TD2}
\RL{$ R \in \{ \approx, \not\approx \} $}
\doubleLine{}
\UIC{$\top$}
\DP}\\[12pt]

% ME
\AXC{$\rho(u) \approx \rho(v) | \Sigma$}
\AXC{$u \approx v | \Sigma'$}
\LL{ME}
\RL{$\rho \text{ is a variable renaming}$}
\doubleLine{}
\BIC{$\rho(u) \approx \rho(v) | \Sigma \cup (\Sigma' \circ \rho)$}\DP\\[12pt]

% ES
\AXC{$s \approx t | \Sigma$}
\AXC{$u[p \leftarrow s] \approx u[p \leftarrow t] | \Sigma' \cup \Sigma''$}
\LL{ES}
\RL{$
\text{if} \left\{ \begin{array}{l}
\Sigma'' \not= \emptyset\\
\Sigma \leq \Sigma''
\end{array}\right.$}
\doubleLine{}
\BIC{$s\approx t | \Sigma \qquad u[p\leftarrow s] \approx u[p \leftarrow t] |
\Sigma'$}\DP\\[12pt]

% RP
\AXC{$s \approx t | \Sigma$}
\AXC{$u \approx v | \Sigma'$}
\LL{RP}
\doubleLine{}
\BIC{$s \approx t | \Sigma$ \qquad $u[p \leftarrow t] \approx v | \Sigma'$}\DP
$\text{if} \left\{\begin{array}{l}
u_{|p} = s\\
s \succ t\\
\Sigma \leq \Sigma'\\
u \not\succeq v ~ \text{or} ~ p \neq \lambda\\
\end{array}\right.$\\[12pt]

% RN
\AXC{$s \approx t | \Sigma$}
\AXC{$u \not\approx v | \Sigma'$}
\LL{RN}
\doubleLine{}
\BIC{$s \approx t | \Sigma$ \qquad $u[p \leftarrow t] \not\approx v |
\Sigma'$}\DP
$\text{if} \left\{\begin{array}{l}
u_{|p} = s\\
s \succ t\\
\Sigma \leq \Sigma'\\
\end{array}\right.$
\caption{The Set of Rules for Unit Rigid Superposition}
\label{fig:unit-sup-rules}
\end{center}
\end{figure}

\subsection{Rewriting}

Rewrite rules can be integrated to the rigid unit superposition easily. In fact,
a rewrite rule $l\rew{}r$ can be expressed as an equality with a hypothesis set
consisting of a single trivial substitution
$s\approx{}t|\{\text{identity}\}$. Since the trivial substitution is compatible
with every substitution, it will never prevent any inference, thus allowing us
to use the unit clause as many times as needed to rewrite terms without
accumulating constraints, particularly using the rules RP and RN, whose side
conditions are always verified by rewrite rules. Rigid unit superposition
therefore provides an algorithm for rigid E-unification modulo rewrite rules.

\subsection{Main Loop}

Our objective with rigid E-unification is to attempt to close a branch of the
tableau prover (i.e. a set of Boolean literals set to true). To do so, all
equational or atomic literals are added to a set of unit clauses to process,
with a label $\Sigma \triangleq \{ \emptyset \}$. Then, the given-clause
algorithm is applied to try and saturate the set. Assuming a fair strategy, this
will eventually find a solution (i.e. derive
$\clauseWithSubst{\emptyset}{\Sigma}$) if there exists one. We refer the
interested reader to~\cite{SS02} for more details.

Because the whole branch is managed by a single given-clause saturation loop, we
look for all solutions susceptible to close the branch at the same time.
Moreover, this technique is amenable to incrementality, i.e. every time a
(dis)equation is decided by the SAT solver, we could add it to the saturation
set and perform a (limited) number of steps of the given-clause algorithm.

\subsection{Example}

To illustrate the calculus, we detail a refutation of the following set of
clauses stemming from set theory, where pair, fst, and snd are the constructor
and destructors of tuples, $f$ a function on tuples, and $X$ a rigid variable:

\[\begin{array}{rcl}
\text{pair}(\text{fst}(x), \text{snd}(x))) &\rew& x\\
\text{fst}(a) &\approx& \text{fst}(b)\\
p(a) &\not\approx& p(\text{pair}(\text{fst}(b), X))\\
\end{array}\]

Because the problem is purely equational, the tableau structure is trivial, and
all the work is done by the rigid superposition procedure as shown in
Fig.~\ref{fig:unit-sup-proof-example}.

\begin{figure}[t]
\begin{center}
\begin{tabular}{clc}
1 & axiom & $\text{pair}(\text{fst}(x), \text{snd}(x))) \rew x$\\

2 & axiom & $\text{fst}(a) = \text{fst}(b)$\\

3 & axiom & $p(a) \not= p(\text{pair}(\text{fst}(b), X))$\\

4 & \renameVarsSymb(1) &
$\clauseWithSubst
{ \text{pair}(\text{fst}(x), \text{snd}(x)) \approx x }
{ \{ \} }$\\

5 & \renameVarsSymb(2) &
$\clauseWithSubst
{ \text{fst}(a) \approx \text{fst}(b) }
{ \{ \} }$\\

6 & \renameVarsSymb(3) &
$\clauseWithSubst
{ f(a) \not\approx f(\text{pair}(\text{fst}(b), y)) }
{ \{ \mapVar{X}y \} }$\\

\midrule

7 & RN(5,6) &
$\clauseWithSubst
{ f(a) \not\approx f(\text{pair}(\text{fst}(a), y)) }
{ \{ \mapVar{X}y \} }$\\

8 & SN(4,7) &
$\clauseWithSubst
{ f(a) \not\approx f(a) }
{ \{ \mapVar{X}{\text{snd}(a)} \} }$\\

9 & ER(8) &
$\clauseWithSubst
{ \emptyset }
{ \{ \mapVar{X}{\text{snd}(a)} \} }$
\end{tabular}
\caption{Proof of a Set Theory Problem}
\label{fig:unit-sup-proof-example}
\end{center}
\end{figure}


\documentclass[a4paper,12pt]{article}
\usepackage[left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath,amssymb,amsthm,algorithm,algorithmic,graphicx,yhmath,url,enumitem,lscape,mathtools}
\usepackage{wrapfig,subfigure}

\newcounter{problem}
\newenvironment{problem}{\refstepcounter{problem} \noindent {\bf Problem \arabic{problem}}}{\newpage}
\newenvironment{solution}{\vspace{0.3cm} \par \noindent {\bf Solution}}{}
\newenvironment{verification}{\vspace{0.3cm} \par \noindent {\bf Verification}}{}
\newenvironment{hint}{\vspace{0.3cm} \par {\bf Hint:}}{}

\newcounter{remark}
\newenvironment{remark}{\refstepcounter{remark} \vspace{0.3cm} \par \noindent {\bf Remark \arabic{remark}}}{\vspace{0.3cm}}
\newcounter{lesson}
\newenvironment{lesson}{\refstepcounter{lesson} \vspace{0.3cm} \par \noindent {\bf Lesson \arabic{lesson}}}{\vspace{0.3cm}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Rn}{\mathbb{R}^n}
\newcommand{\Rnn}{\mathbb{R}^{n \times n}}
\newcommand{\bes}{\begin{equation*}}
\newcommand{\ees}{\end{equation*}}
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\newcommand{\eps}{\epsilon}
\newcommand{\fl}{\text{fl}}

\begin{document}

\title{5DV005, Fall 2018, Lab session 5}
\author{Carl Christian Kjelgaard Mikkelsen}

\maketitle
\tableofcontents

\section{The time and the place}
The lab session will take place on
\begin{center}
Wednesday, December 5th, 2018, (kl. 13.00-16.00), Room MA416-426.
\end{center}

\section{Introduction}

This week's problems do not require much in terms of programming. However, however careful inspection and analysis of the output is required. Use any excess time to complete problems from previous weeks.

\section{The problems}

\begin{problem} Consider the problem of computing the derivative $f'(x)$ using the finite difference approximation
  \bes
  D_1(f,x,h) = \frac{f(x+h)-f(x)}{h}
  \ees
  Execute the script {\tt rdifmwe1} and examine output in detail:
  \begin{enumerate}
  \item Determine the value of $k$ where the computed value of Richardson's fraction has executed an illegal jump.
  \item Determine the range of $k$ values for which the computed value of Richardson's fraction convergences monotonically to $2^p$ for a suitable value of $p$.
  \item Determine the range of $k$ values for which the computed value of Richardson's fraction converges to $2^p$ at the correct rate.
  \item Determine the range of $k$ values where the error estimates become more and more accurate.
  \item How is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
    \end{enumerate}
  \end{problem}

  \begin{problem} Copy {\tt rdifmwe1.m} into {\tt /work/l5p2.m} and adapt it to the problem of computing $f'(2)$ where
    \bes
    f(x) = e^x \sin(x)
    \ees
    Do \textit{not} include the derivative when you call {\tt rdif}.
    \begin{enumerate}
    \item Verify that the computed value of Richardson's fraction appears to converge towards $2^p$ for a suitable value of $p$ as $h$ tends to zero.
    \item Find the last value of $k$, where the computed value of Richardson's fraction behaved exactly as predicted for the exact value of Richardson's fraction.
    \item Include the exact derivative when you call {\tt rdif}. Find the value of $k$ where the accuracy of Richardson's error estimate is maximal.
    \item How is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
      \end{enumerate}
 \end{problem}

\begin{problem} Consider the problem of computing the derivative $f'(x)$ using the finite difference approximation
  \bes
  D_2(f,x,h) = \frac{f(x+h)-f(x-h)}{2h}
  \ees
  Execute the script {\tt rdifmwe2} and examine output in detail:
  \begin{enumerate}
  \item Determine the value of $k$ where the computed value of Richardson's fraction has executed an illegal jump.
  \item Determine the range of $k$ values for which the computed value of Richardson's fraction convergences monotonically to $2^p$ for a suitable value of $p$.
  \item Determine the range of $k$ values for which the computed value of RichRichardson's fraction converges to $2^p$ at the correct rate.
  \item Determine the range of $k$ values where the error estimates become more and more accurate.
  \item Is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
    \end{enumerate}
\end{problem}

 \begin{problem} Copy {\tt rdifmwe2.m} into {\tt /work/l5p4.m} and adapt it to the problem of computing $f'(2)$ where
    \bes
    f(x) = e^x \sin(x).
    \ees
    Do \textit{not} include the derivative when you call {\tt rdif} initially.
    \begin{enumerate}
    \item Verify that the computed value of Richardson's fraction appears to converge towards $2^p$ for a suitable value of $p$ as $h$ tends to zero.
    \item Find the last value of $k$, where the computed value of Richardson's fraction behaved exactly as predicted for the exact value of Richardson's fraction.
    \item Include the exact derivative when you call {\tt rdif}. Find the value of $k$ where the accuracy of Richardson's error estimate is maximal.
    \item Is the behavior of Richardson's fraction related to the quality of Richardson's error estimate?
    \end{enumerate}
 \end{problem}

\bibliographystyle{plain}
  \bibliography{../../../lecture-notes/refs}

\end{document}


% !TeX root = ../smc-report.tex
% !TeX encoding = UTF-8
% !TeX spellcheck = en_GB

\section*{LINFA: Smart drugs restocking}

  \subsection*{Smart drugs restocking}

    The pharmaceutical logistic chain is a very complex system, as it includes several actors and critical points (such as elevated drug costs, need for transport at monitored temperature, chance of expiration of goods, stock management, irregularity of demands, several possible logistic approaches, etc), which render the problem hard to optimize without the proper tools for decision support. In this context, possible unavailability could lead to critical situations, sometime even catastrophic, as it wouldn't be possible any more to guarantee the correct execution of one or more healthcare protocol, thus affecting the patients' health. Furthermore, orders are typically carried out on a daily basis and in a manual fashion, without the support of any decision support system. All these reasons makes restocking schedule hard, thus leading to unproductive stocks and higher stocking costs.

    In this scenario fits the project \textit{LINFA} (i.e. \textit{Logistica INtelligent del FArmaco}, or \textit{Smart Drug Logistic}, from Italian), that aims to develop an IT system for support to processes of drugs logistic management, in the context of healthcare or local companies. LINFA aims to increase efficiency, effectiveness and predictability of the process of drugs and medical devices restocking, within healthcare structures, through methods of predictive analysis and optimization, advanced logistic techniques and tracking features through the use of RFID technologies or the integration of healthcare and administrative information stream.

    For sake of simplicity, the following assumptions will be made in order to build a model:

    \begin{itemize}
      \item a single ward is present;
      \item the ward has a fixed number of beds (40) and a fixed maximum storage capacity (40);
      \item patients are indistinguishable;
      \item there is only one type of drug;
      \item patients can arrive through emergencies (i.e. according to a random variable) or scheduled examinations (i.e. according to a fixed constant);
      \item each day, patients can leave the ward with a certain probability;
      \item each day, patients can consume up to 3 units of drug;
      \item for each missing drug, an urgent order is issued, which arrives immediately;
      \item restock orders are issued at the end of each day and arrive immediately;
      \item the drug can be restocked in quantities of 0 (i.e. no order), 10, 20, 30 or 40.
    \end{itemize}

    The model in Code \ref{lst:ward} models a generic ward with the above specifications and three days of lookahead. The model is intended to characterise the evolution of the ward for the three subsequent days in order to support decision to the restock order of the current day, so it starts at the end of the current day (i.e. after patients arrival/discharge and drug consumption, but before the restock order). The arrival of patients and the consumption of drugs have been modelled through binomial distributions, as well as drug restock order choices, with different success probabilities. Sojourn time inside the ward for each patient is instead modelled through a geometric distribution.

    \begin{center}
      \lstinputlisting[language=prism, caption={PRISM code of the probabilistic model of a hospital ward.}, label={lst:ward}]{code/ward_dtmc.sm}
    \end{center}

    \question{Look at the model in Code \ref{lst:ward} and describe how it works.\\}
    \answer{
      The first thing that can be notice about the model is that it is defined as a DTMC. This is due the fact that the model is fully probabilistic (i.e. there is no non-determinism) and that the time is divided into discrete steps.

      In the first part of the code, before the beginning of the main module (lines 3-19), constants and probabilities are defined. Distributions for patients arrival through emergency, drug consumption and drug restock are defined as binomial distribution with different success probabilities (lines 8-11). Upper bounds of such distributions are defined as $4$ for emergency arrivals (\prism{maxER}), $3$ for drug consumption (\prism{maxConsume}) and $4$ for drug reorder choices. Arrivals of scheduled patients for the three subsequent days of lookahead are defined as fixed variables (lines 17-19). Also, cost weights are defined for the order cost of a single drug unit, the cost for urgent drug reorders and the stocking cost for keeping each drug unit in stock in the right conditions each day (lines 13-15).

      The main module \prism{hospitalWard} firstly defines several internal variables (lines 37-42), such as the number of hospitalised patients \prism{n}, the number of current drugs in stock \prism{stockDrugs}, the current day \prism{day}, or support variables, such as \prism{s}, \prism{tmp} or \prism{tmp2}. In particular \prism{s} is used to describe the internal state for each day of the ward.

      States from \prism{s=0} to \prism{s=3} (lines 31-46) are used to model the drug order for the current day and for the first and second following days. The module starts at the end of the current day (i.e. after patients arrival/discharge and drug consumption, but before the restock order) in order to include as much real information as possible from the real ward when deciding how to reorder. Lines 34-37 are used to chose probabilistically which kind of order to issue, according to the probability \prism{probOrder} defined. Lines 39-43 are then used to execute the corresponding order, effectively increasing the stocked drugs. Also, each order has a label associated, that will be used in the cost reward computation. After the order choice has been made, the \prism{day} variable is incremented and the \prism{tmp} support variable is set to the number of patients currently hospitalised, \prism{n}.

      States from \prism{s=10} to \prism{s=12} are used to model the arrival and discharge of patients in the ward. State \prism{s=10} (lines 49-52) of the ward is used to cycle through all the currently hospitalised patients to decide which of them will leave the ward, with probability \prism{probExit}. In this scenario the \prism{tmp} variable is used to cycle through all the patients. State \prism{s=11} (lines 55-57) is used to model the arrival of scheduled patients, the exact number of which is indicated by three constants. State \prism{s=12} (lines 60-63) instead is used to model the arrival of new patients through emergencies, modelled through a binomial distribution with success probability \prism{probER} defined.

      State \prism{s=20} is used to model drug consumption by the currently hospitalised patients. A binomial distribution with success probability \prism{probConsume} is used and a cycle is implemented through the use of the \prism{tmp} (lines 66-69). On top of that, for each patient, if after the drug consumption some drugs result to be missing (i.e. some drug was needed but there wasn't enough in stock), then the special action with label \prism{missingDrugs} is executed (line 71), which is needed to compute the correct cost reward. When drugs have been administered to all the patients (line 73) the module will then either start again (line 76) or stop definitely (line 77) in case the three following days have been completely modelled.

      Lastly, the only reward defined, \prism{totalCost}, is used to cumulate the total cost coming from different sources (lines 85-95). In particular, every time the consumption of drug is finished for a certain day, the remaining drugs in stock produce a cost determined by the weight \prism{costStorage} (line 86). Each time one or more drugs result missing during administration, and therefore an urgent order has to be issued, each missing drug produce a cost defined by the weight \prism{costUrgentOrder} (line 88). Regular orders also produce a cost, in particular a cost of $1$ for each drug ordered (lines 90-94).
    }

    \question{
      Add the following property, representing the expected \prism{totalCost} at the end of the three subsequent days:

      \lstinputlisting[language=prism, numbers=none]{code/totalCost.pctl}

      Then, create a new experiment to evaluate the best reorder strategy for wards that have different probabilities of drug consumption. For this experiment, set the probability constants \prism{probExit} and \prism{probER} to $0.5$ and $0.3$ respectively.
    }
    \answer{
      The results of the experiment, that plots the expected \prism{totalCost} after the three days for different consumption and reorder probabilities, are shown in Figure \ref{fig:totalCost_probOrder_probConsume_plot}.

    	\begin{figure}[h!]
    		\begin{center}
    			\includegraphics[scale=0.45]{totalCost_probOrder_probConsume_plot.eps}
    		\end{center}
    		\caption{Experiment plot of the expected total cost after the three subsequent days as \prism{probOrder} varies for different values of \prism{probConsume}.}
    		\label{fig:totalCost_probOrder_probConsume_plot}
    	\end{figure}

      As expected, lower values of \prism{probOrder} result in a higher expected \prism{totalCost} after three days for higher probabilities of consumption. This is clearly due the fact that higher consumption probabilities means a higher demand of drugs, which otherwise will deplete soon, therefore policies that tend to restock big quantities of drugs are advised in these situations. These curves, however, are not monotone, as they seem to cross around $0.6$, which seems to be also the minimum for most curves. After this point, which seems to be the best policy to be taken on average, the situation changes, meaning that higher probabilities of reordering are favoured in scenarios with higher probabilities of consumption. It is also worth noticing that the curve relative to \prism{probConsume}$=0$ is the only strictly increasing, due to the fact that if no drugs are ever consumed (i.e. never required), then reordering drugs is always just a cost and never an investment. So, as expected, low values of \prism{probConsume} requires also low values of \prism{probOrder} and viceversa. A good strategy, on average, would be to reorder with probability between $0.5$ and $0.6$.
    }

    \question{Perform now an experiment similar to the previous one, this time studying the best reorder strategy for wards that have different average sojourn times (i.e. probability of patient discharge). Set instead the value of \prism{probConsume} to $0.2$}
    \answer{
      The results of the experiment are shown in Figure \ref{fig:totalCost_probOrder_probExit_probConsume-0-2_plot}.

    	\begin{figure}[h!]
    		\begin{center}
    			\includegraphics[scale=0.45]{totalCost_probOrder_probExit_probConsume-0-2_plot.eps}
    		\end{center}
    		\caption{Experiment plot of the expected total cost after the three subsequent days as \prism{probOrder} varies for different values of \prism{probExit}, with \prism{probConsume}$=0.2$.}
    		\label{fig:totalCost_probOrder_probExit_probConsume-0-2_plot}
    	\end{figure}

      The results show that, as expected, lower values of \prism{probExit} (i.e. patients stay longer, on average, in the ward) require higher values of \prism{probOrder}, while higher values of the discharge probability require a smaller reorder probability. As seen in the previous experiment, the curves cross, this time between $0.3$ and $0.35$. In this scenario, if the probability of patients discharge is unknown, the safest strategy would be with a value of \prism{probOrder} between $0.25$ and $0.3$.
    }

    \question{Repeat the previous experiment, this time with \prism{probConsume}$=0.8$. Which conclusions can you draw comparing the results of the two experiments?}
    \answer{
      The results of the experiment are shown in Figure \ref{fig:totalCost_probOrder_probExit_probConsume-0-8_plot}.

    	\begin{figure}[h!]
    		\begin{center}
    			\includegraphics[scale=0.45]{totalCost_probOrder_probExit_probConsume-0-8_plot.eps}
    		\end{center}
    		\caption{Experiment plot of the expected total cost after the three subsequent days as \prism{probOrder} varies for different values of \prism{probExit}, with \prism{probConsume}$=0.8$.}
    		\label{fig:totalCost_probOrder_probExit_probConsume-0-8_plot}
    	\end{figure}

      The results of this last experiment are very similar to the results of the previous one, meaning that lower values of \prism{probExit} favour higher values of \prism{probOrder} and viceversa. This time, instead, the inversion point of the curves results shifted to the right, around $0.6$. This shift is due the fact that when drugs are consumed more frequently, ordering more drugs keeps being the optimal choice even if patients leave the hospital quickly, because even in the short time they spend in the ward, they are expected to consume high quantities of drugs. Also, the range of the expected \prism{totalCost} varies greatly between the two experiments: in the previous one the maximum was $636$, while for this was is $2543$. This can simply be explained by the fact that if more drugs are consumed on average, the simple cost of having to restock more drug units makes the total cost skyrocket.
    }

  \dashedrule

  \subsection*{Adding non-determinism}
    One of the most useful features of PRISM is the optimal strategy extraction of an MDP model. \textit{MDPs} (\textit{Markov Decision Process}) are probabilistic models that also include non-deterministic choices, in order to model transitions between states with no probabilistic information attached to them. A strategy, for an MDP, is a set of specific actions for all the non-deterministic choices that resolve the non-determinism of the model, turning it into a fully probabilistic model. Among all the possible strategies, the optimal one is the strategy that minimises/maximises a certain reward function.

    \question{Modify the model in Code \ref{lst:ward} in order to make the drug reorder choice non-deterministic.}
    \answer{
      Changing the reorder choice in Code \ref{lst:ward} from a probabilistic choice to a non-deterministic one is quite straightforward. The first thing do is, clearly, to change the keyword \prism{dtmc} in line 1 to \prism{mdp}, so that the model is correctly built as an MDP. Then, it's simply necessary to replace the lines 31-46 in Code \ref{lst:ward} with the following code:

      \lstinputlisting[language=prism, numbers=none]{code/ward_mdp_update.sm}

      The above code allows all the reorder choices to be enabled at the same time with no probability information attached to them, thus making the reorder choice non-deterministic. Also, the constant \prism{probOrder} (line 11) and the variable \prism{drugsToOrder} (line 28) can be removed from the code, as they are not used any more.
    }

    \question{
      Save the new MDP model as \bash{ward\_mdp.sm}, setting \prism{probExit}, \prism{probER} and \prism{probConsume} to be $0.5$, $0.3$ and $0.6$ respectively. Then execute the following command to \href{http://www.prismmodelchecker.org/manual/RunningPRISM/Adversaries}{extract the optimal adversary}, states and labels:

      \lstinputlisting[numbers=none]{code/extractadv.bat}

      Following the PRISM manual, determine which is the optimal strategy for the current day reorder with the initial setting of \prism{stockDrugs}$=10$ and \prism{n}$=10$.
    }
    \answer{
      According to the manual, once the suggested command have been executed, the file \bash{adv.lab} indicates which is the ID of the initial state, in this case $370$. By exploring, then, the \bash{adv.tra} file, the following line can be found:

      \lstinputlisting[numbers=none]{code/adv_tra_example.tra}

      The above line indicates that, when in the state with ID $370$ (i.e. the initial state, which in this case represents the scenario with 10 patients and 10 stocked drugs), the best strategy to follow, in order to minimise on average the total cost after three days, is to order 10 units, as indicated by the label of the action \prism{order10}. The above line indicates also that, by taking this action, the model will move to the state with ID $2191$, which, according to the file \bash{adv.sta}, corresponds to the scenario with 10 patients, 20 stocked drugs and variable \prism{s}$=1$, as expected.
    }

    \question{Perform now an experiment, either manually or through a custom script, varying the initial state for both \prism{n} and \prism{stockDrugs}. Show and comment the results.}
    \answer{
      The results of the experiment are shown in Figure \ref{fig:sd_n_plot}.

    	\begin{figure}[h!]
    		\begin{center}
    			\includegraphics[scale=1]{sd_n_plot.pdf}
    		\end{center}
    		\caption{Experiment plot of the optimal reorder strategy as \prism{stockDrugs} varies for different values of \prism{n}.}
    		\label{fig:sd_n_plot}
    	\end{figure}

    	From Figure \ref{fig:sd_n_plot} it can be seen that, as the value of \prism{stockDrugs} increases, the suggested optimal reorder amount decreases, as with higher amounts of drugs already in stock, the need for even more drugs gets weaker and weaker. When the number of hospitalised patients \prism{n}, instead, is increased, the suggested optimal amount results also increased, because, as expected, with more patients in the hospital more drug consumptions are expected, thus requiring a fuller stock. In Figure \ref{fig:sd_n_plot} the curves \prism{n}$=30$ and \prism{n}$=40$ overlaps.

    	The experiment have been conducted through a simple custom script, used to speed up the experimentation\footnote{The script source code can be found at the following link:\\ \url{https://github.com/oddlord/uni/tree/master/phd/courses/smc/experiments}.}.
    }

    \question{Perform a similar experiment, this time varying \prism{stockDrugs} and \prism{costStorage}, while keeping the initial value of \prism{n} fixed to $20$.}
    \answer{
      The results of the experiment are shown in Figure \ref{fig:sd_costStorage_0-40_plot}.

    	\begin{figure}[h!]
    		\begin{center}
    			\includegraphics[scale=1]{sd_costStorage_0-40_plot.pdf}
    		\end{center}
    		\caption{Experiment plot of the optimal reorder strategy as \prism{stockDrugs} varies for different values of \prism{costStorage}.}
    		\label{fig:sd_costStorage_0-40_plot}
    	\end{figure}

      The results in Figure \ref{fig:sd_costStorage_0-40_plot} show that, similarly to the results of the previous experiment, the suggested optimal reorder amount decreases as the amount of drugs in stock increases. Also, it can be observed how, with the increase of the storage cost for each drug for each day, the suggested amount decreases: this is due the fact that, when the cost is low (or even zero) the system tends to suggest to completely refill the storage, in order to cope even with extreme emergency situation; on the other hand, if the storage cost weights more, PRISM is forced to find a more suitable trade-off between the storage cost and the actual need for drugs. An interesting detail is also that the gap between two subsequent curves seems to get smaller and smaller as \prism{costStorage} increases: this remarks the fact that, as \prism{costStorage} increases, the suggested optimal reorder amount tends to a limit, representing the best trade-off between the need to store as less drugs as possible (due to the high storage cost) and the need to also store as much drugs as possible (in order to avoid urgent orders).
    }


\documentclass[letterpaper]{article}
\usepackage{amsmath}
\usepackage{fullpage}
\usepackage{natbib}
\usepackage{textcomp}
\usepackage{color}
\definecolor{darkblue}{rgb}{0.0,0.0,0.9}
\usepackage[colorlinks=true,urlcolor=darkblue,linkcolor=black,citecolor=darkblue]{hyperref}

\newcommand{\deriv}[2]{\frac{\mathrm{d}#1}{\mathrm{d}#2}}

\begin{document}
\newpage
\pagestyle{plain}
\setcounter{page}{1}

\begin{center}
\huge{DJLES: Dubreil-Jacotin-Long Equation Solver}\\[1em]
\large{Feb 8, 2019}\\
Michael Dunphy
\end{center}

\section{Introduction}
DJLES is a MATLAB/Octave package that finds a mode-one solution to the DJL equation. \cite{StastnaLamb2002} describe the problem setup, weakly nonlinear theory, fully nonlinear theory, and iterative solution procedure. DJLES was inspired by \verb+soliw+, a DJL solving C package resulting from the work of~\cite{StastnaLamb2002}.

DJLES differs from \verb+soliw+ by being MATLAB based, which avoids the need to compile C code and manage the resulting raw binary files. When used with MATLAB, it also takes advantage of the built in parallelisation of FFTs which accelerates solution on multi-core computers. Lastly, it easily employs the resolution refinement approach of~\cite{DunphySubichStastna2011}, a feature not available in \verb+soliw+.

If you find this code useful, please cite~\cite{DunphySubichStastna2011} as it will encourage the author to maintain/improve DJLES. Bug reports, fixes/improvements and additional case files are welcome; please contact \href{mailto:mdunphy@uwaterloo.ca}{mdunphy@uwaterloo.ca} for inclusion in a future version.

DJLES has benefited from contributions from Marek Stastna, Kevin Lamb, Chris Subich, Derek Steinmoeller, Jorge Magalh\~aes and Jos\'e da Silva.

The remainder of this document is a user guide for the code.

\section{Quickstart}
Download the code and run one of the case files in either MATLAB or Octave. The case files take less than one minute to run on a recent desktop computer, and may need 2-3 minutes on an older system. Upon completion the code will produce a plot of the wave.

\section{Usage}
Typical usage is to set problem parameters, then call \verb+djles_refine_solution+ to solve the DJL equation, which returns the isopycnal displacement field $\eta$ and wave speed $c$ in MATLAB variables \verb+eta+ and \verb+c+, respectively. The required problem parameters are:
\begin{itemize}
 \item $L$ and $H$ -- Domain length and depth (m)
 \item $N_x$ and $N_z$ -- Number of grid points in $x$ and $z$
 \item $\bar{\rho}(z)$ and $\deriv{}{z} \bar{\rho}(z)$
       -- Background density profile and first derivative
       \begin{itemize}
        \item If using full density, also provide reference density parameter \verb+rho0+.
        \item If using non-dimensional density, DJLES will assume \verb+rho0=1+.
       \end{itemize}
 \item $A$ -- Target available potential energy (per unit in $y$)
       \begin{itemize}
        \item If using full density, units are kg m/s$^2$.
        \item If using non-dimensional density, units are m$^4$/s$^2$.
       \end{itemize}
 \item $U_{bg}(z)$, $\deriv{}{z} U_{bg}(z)$, and $\deriv{^2}{z^2} U_{bg}(z)$
       -- Background velocity profile (m/s) and first two derivatives
\end{itemize}
If a background velocity is not desired, set it to zero (see test cases).
Once the solution is complete, \verb+djles_diagnostics+ computes the associated velocity fields, vorticity, etc., \verb+djles_pressure+ computes the pressure, and \verb+djles_plot+ produces a simple plot of these fields.

During the first call to \verb+djles_refine_solution+, we use weakly nonlinear theory to find an initial guess for the solution, which is then iterated to convergence. Subsequent calls to \verb+djles_refine_solution+ use the previous fully nonlinear solution as the initial guess. This allows for the strategy of successively adjusting parameters (resolution, wave APE, etc) to ease/accelerate finding a solution. The various test cases demonstrate several solution scenarios.

\subsection{Tuning knobs}
There are a few tuning knobs. Set them in the case file to override the default.
\begin{itemize}
 \item \verb+min_iterate+ specifies the minimum number of iterations that the solver will take, default is 10.
 \item \verb+max_iterate+ specifies the maximum number of iterations that the solver will take, default is 2000. If you use a very small \verb+epsilon+ or \verb+relax+ you may need to increase this.
 \item \verb+NL+ is the number of Legendre points to use for the Gauss quadrature used for the APE integral. Values below 5 are probably too small, values in 15--25 are quite good, and values higher than 25 are likely unnecessary. Default is 20.
 \item \verb+relax+ is the underrelaxation factor, $0 < \verb+relax+ <= 1$.
        The value is the fraction of the new iteration, $\eta^{k*}$ to retain
        after performing an iteration, that is,
        $\eta^k = \eta^{k-1} + \verb+relax+(\eta^{k*} - \eta^{k-1})$.
        The default of 0.5 works well in most cases, and a value of 1 disables underrelaxation.
        Using a small value can help stabilise the solver at the expense of needing more iterations.
 \item \verb+g+ is the gravitational acceleration constant, default value is 9.81 m/s$^2$.
 \item \verb+verbose+ controls the verbosity of status updates. A value of 0 disables printing, a value $\ge 1$ enables printing updates after each iteration, and a value $\ge 2$ enables printing of timing data upon completion. Default is 1.
\item \verb+epsilon+ controls the stop condition, which is the relative difference between successive iterations. Default $1\times10^{-4}$. Smaller values will require more solver iterations.
\end{itemize}

\subsection{Miscellaneous}

A successful solve will have a relative error of roughly \verb+epsilon+/\verb+relax+ decimal places in $\eta$. Using a small value of \verb+relax+ should be accompanied with a reduction in \verb+epsilon+.

If the solver fails to converge,
\begin{enumerate}
 \item Ensure that your density profile $\rho(z)$ is stably stratified.
 \item Run the solver with a smaller underrelaxation factor. A value of 0.1 or even 0.01 may suppress oscillations and stabilise the solver.
 \item Ensure that your density profile (and first derivative) is smooth.
 \item Ensure that the domain width $L$ is wide enough to contain the wave. The boundary conditions for the problem are $\eta=0$ on all boundaries; if your domain is too narrow it will be enforcing that condition at the wrong location.
\end{enumerate}

\subsection{Compatibility notes}
\begin{itemize}
 \item Tested with MATLAB R2013a (Linux 64-bit and Windows), R2012a (OSX 10.6.8).
 \item Tested with Octave v3.8.1, v4.0.1 and v4.4.1 (Linux 64-bit).
 \item The initial guess part uses \verb+polyeig.m+, and Octave added it in v3.8.0, so it's unlikely to work with Octave older than v3.8.0.
 \item MATLAB R2014a prints warnings about deprecating \verb+ppval+, you can suppress the warnings with \\ \verb+warning('off','MATLAB:interp1:ppGriddedInterpolant');+
\end{itemize}

\subsection{Numerical Grid}
The domain is the rectangular region $[0,L] \times [-H,0]$ and is discretized into $N_x\times N_z$ grid boxes. The boundary conditions for the problem are zero, and we use the cell centres for the coordinates, that is,
\begin{align}
x_i^c &= \frac{\Delta x}{2} + i \Delta x, \quad i \in 0,1,2,\ldots,N_x-1,\\
z_j^c &= -H + \frac{\Delta z}{2} + j \Delta z, \quad j \in 0,1,2,\ldots,N_z-1,
\end{align}
where $(\Delta x, \Delta z) = (L/N_x, H/N_z)$. The grid shifting routine \verb+djles_shift_grid.m+ shifts the data from the cell centres to cell edges,
\begin{align}
x_i^e &= i \Delta x, \quad i \in 0,1,2,\ldots,N_x\\
z_j^e &= -H + j \Delta z, \quad j  \in 0,1,2,\ldots,N_z.
\end{align}
The array sizes are $N_x+1 \times N_z+1$ on the cell edges grid.

\section{Description of M-files}
The code consists of a handful of functions, scripts, and test cases.

\subsection{Scripts}
The scripts are
\begin{verbatim}
Filename                  - Description
-----------------------------------------------------------------------------------------
djles_common.m            - Sets default parameters and generates grid and wavenumbers.
djles_diagnostics.m       - Computes wave velocities, vorticity, etc from eta and c.
djles_initial_guess.m     - Used by djles_refine_solution.m to get an initial guess for
                            eta & c from WNL theory if an initial guess is not provided.
djles_plot.m              - Plots the solution and some diagnostics.
djles_pressure.m          - Computes the non-hydrostatic pressure and residuals in the
                            governing equations. Uses results from djles_diagnostics.m.
djles_refine_solution.m   - This is the main m-file that each case files calls. It
                            performs the iterative procedure to find a wave solution.
\end{verbatim}

\subsection{Helper Functions}
The helper functions are
\begin{verbatim}
djles_change_resolution.m - Changes the resolution of the solution.
djles_compute_apedens.m   - Computes the available potential energy density.
djles_diffmatrix.m        - Generates a finite difference differentiation matrix for use
                            in the initial guess procedure.
djles_extend.m            - Extends the input data using specified symmetry.
djles_gradient.m          - Computes the gradient of input data.
djles_quadweights.m       - Returns the 1-dimensional interior grid quadrature weights.
djles_residual.m          - Computes the residual in the DJL equation.
djles_shift_grid.m        - Shifts the input data from cell centres to cell endpoints.
djles_sinequadrature.m    - Computes the interior grid sine quadrature weights for
                            area integrals.
djles_wavelength.m        - Computes an estimate of the wavelength.
\end{verbatim}

\subsection{Test cases}
Included are eight test cases that demonstrate how to use DJLES. A brief description of each follows.

\subsubsection{Small APE}
Test case: \verb"case_small_ape.m" \\\\
This test case demonstrates the parameter regime of no background current, a smooth pycnocline, and small wave APE.
The initial guess obtained from weakly nonlinear theory will be ``close'' to the fully nonlinear DJL solution, so the solver readily converges.
Once we find the low resolution wave (32$\times$32), we increase the resolution to 512$\times$256 and iterate to convergence.
The low resolution solution is a very good initial guess for the high resolution problem, so the high resolution wave is also readily solved.

\subsubsection{Large APE}
Test case: \verb"case_large_ape.m" \\\\
This test case demonstrates the parameter regime of no background current, a smooth pycnocline, and large wave APE.
The initial guess from weakly nonlinear theory will be rather poor.
The solution strategy we use is to find a wave with a small APE, and supply that wave as the initial guess to find a wave with a larger APE.
Applying this several times yields the final wave with a large APE.
In this case we start at one percent of the target APE and raise the APE in five steps.
Lastly, we increase the resolution once we reach the target APE.
The result is a broad flat crested wave.

\subsubsection{Background current}
Test case: \verb"case_ubg.m" \\\\
We use the same successive solution strategy here by solving without a background current first,
and then raise the background current strength in increments until we reach the full background current.
We increase epsilon for the intermediate solutions to accelerate the process as we only need them to be of ``initial guess'' quality.
Lastly, we reduce epsilon, and increase the resolution in two increments.

\subsubsection{Sharp pycnocline}
Test case: \verb"case_sharp_pycnocline.m" \\\\
Obtaining a solution with a sharp pycnocline can be difficult for the solver to find directly, and the successive solution strategy works here as well.
We begin with a low-resolution wide-pycnocline that is readily solved and begin successively reducing the pycnocline thickness.
As we sharpen the pycnocline, we also increase the resolution such that the pycnocline is not under-resolved.

\subsubsection{Synthetic data}
Test case: \verb"case_synthetic_data.m" \\\\
This case matches the parameters of the background current case, except that we use synthetic mooring data as the background profiles.
We sample the analytic functions at 25 evenly spaced points and construct continuous profiles using linear interpolating polynomials.
Second order differentiation provides the gradients, which are linearly interpolated in the same fashion.
The \verb+linear+ and \verb+pchip+ interpolation methods preserve monotonocity which is important to ensure the interpolated density profile is stable.
The \verb+spline+ method does not preserve monotonicity so be careful if you use it.
As in \verb"case_ubg.m", we raise the background velocity in four successive steps, followed by three resolution refinements.

\subsubsection{Lake Erie data}
Test case: \verb"case_lakeerie.m" \&  \verb"case_lakeerie.txt" \\\\
Here we use time-averaged temperature profile data collected from Lake Erie (K.~Lamb 2014, pers.~comm.).
The text file contains two columns, depth (m) and temperature (\textdegree C), and we convert the temperature to density with a linear equation of state.
There is no surface value so we extrapolate to find a value for $z=0$.
We construct the density and density gradient in the same fashion as in the synthetic data case, and use no background current.
We solve the wave at low resolution and then raise the resolution in two steps.

\subsubsection{Pineda et al. (2015)}
Test case: \verb"case_pineda.m" \&  \verb"case_pineda_cast1.txt" \\\\
This case is intended as a typical shallow continental shelf case where we reproduce the DJL solutions shown in~\cite{PinedaEtAl2015}.
The text file contains two columns, depth (m) and density (kg/m$^3$), and is an approximate reproduction (courtesy Jorge Magalh\~aes and Jos\'e da Silva) of the smoothed curve of Fig 9a.
We use the full density and set the reference density \verb+rho0+ as the maximum value, and construct the density and first derivative functions as in the synthetic data case.

The first part of the script finds the wave showcased in Figure 11.
After raising the target APE in a few steps at low resolution and large epsilon, we apply several steps of resolution refinement with epsilon reductions.
The relative residual of the final wave is order $4\times 10^{-7}$, and matches~\cite{PinedaEtAl2015} to three significant figures in wave height ($-14.1176$ m vs. $-14.1$ m) and two significant figures in phase speed (0.585978 m/s vs. 0.585 m/s).

The second part of the script reproduces the solid curves of Figure 10.
We sweep over a range of target APE values and record the wave height, phase speed, and velocity and pressure one metre above the bottom.
Each wave is reused as the initial guess for the next APE value as the sweep progresses.

Lastly, we note there is a difference in amplitude between the pressure reported in~\cite{PinedaEtAl2015} and that computed by DJLES (Figures 10c and 11c).
We compute residuals by substituting the wave fields into the Boussinesq equations moving with the wave (see bottom part of \verb+djles_pressure.m+), and find that the residuals are appropriately small, which indicates that the pressure computed by DJLES is correct.

\subsubsection{Amazon River mouth data}
Test case: \verb"case_amazon.m" \&  \verb"case_amazon.txt" \\\\
This case is intended to find the maximum wave amplitude for internal solitary waves on the continental shelf near the mouth of the Amazon River.
The included text file contains three columns: depth (m), density (kg/m$^3$) and velocity (m/s) projected along the wave direction of propagation (data courtesy of Jorge Magalh\~aes and Jos\'e da Silva).

The density profile has inversions, so we make ad-hoc adjustments to ensure that the first derivative is always negative (and thus $N^2(z)$ is always positive).
Much like the other cases, we start with a small amplitude at low resolution, and incrementally raise the values until we reach a high resolution wave of 35.3 m amplitude.
This wave is near its amplitude limit; further increases to the APE will expand the wave as a broad flat crested wave and a larger domain is needed to calculate such a wave.

% References
\bibliographystyle{kbib}
\bibliography{djles}

\end{document}


\section{Description of datasets}
\label{sec:datasets}

Most of the VQA datasets have strong biases. This allow models to learn strategies without reasoning about the visual input~\cite{Santoro2017ASN}.
The CLEVR dataset~\cite{johnson2017clevr} was developed to address those issues and come back to the core challenge of visual QA which is testing reasoning abilities.
CLEVR contains images of 3D-rendered objects; each image comes with a number of highly compositional questions that fall into different categories.
Those categories fall into 5 classes of tasks: Exist, Count, Compare Integer, Query Attribute and Compare Attribute.
The CLEVR dataset consists of:
\begin{itemize}
\item 	A training set of 70k images and 700k questions,
\item	A validation set of 15k images and 150k questions,
\item	A test  set of 15k images and 150k questions about objects,
\item	Answers, scene graphs and functional programs for all train and val images and questions.
\end{itemize}
Each object present in the scene, aside of position, is characterized by a set of four attributes:
\begin{itemize}
\item 2 sizes: large, small,
\item 3 shapes: square, cylinder, sphere,
\item 2 material types: rubber, metal,
\item 8 color types: gray, blue, brown, yellow, red, green, purple, cyan,
\end{itemize}
resulting in 96 unique combinations.

Along with CLEVR, the authors~\cite{johnson2017clevr} introduced  CLEVR-CoGenT (Compositional Generalization Test, CoGenT in short), with a goal of evaluating how well the models can generalize, learn relations and compositional concepts.
This dataset is generated in the same way as CLEVR with two additional conditions.
As shown in \tableref{tab:cogent_conditions}, in Condition A all cubes are gray, blue, brown, or yellow, whereas all cylinders are red, green, purple, or cyan; in Condition B cubes and cylinders swap color palettes.
For both conditions spheres can be any colors.

\begin{table}[b!]
	\centering
	\begin{tabular}{cccc}
		\toprule
		Dataset        & Cubes              & Cylinders &  Spheres         \\
		\midrule
		CLEVR   &  any color &  any color        &    any color    \\
		%\midrule
		CLEVR CoGenT A & gray / blue / brown / yellow  & red / green / purple / cyan       &    any color  \\
		CLEVR CoGenT B  & red / green / purple / cyan &   gray / blue / brown / yellow       &      any color  \\
		\bottomrule
	\end{tabular}
	\caption{Colors/shapes combinations present in CLEVR, CoGenT-A and CoGenT-B datasets.}
	\label{tab:cogent_conditions}
\end{table}

The CoGenT dataset contains:
\begin{itemize}
\item	Training set of 70,000 images and 699,960 questions in Condition A,
\item	Validation set of 15,000 images and 149,991 questions in Condition A,
\item	Test set of 15,000 images and 149,980 questions in Condition A (without answers),
\item	Validation set of 15,000 images and 150,000 questions in Condition B,
\item	Test set of 15,000 images and 149,992 questions in Condition B (without answers),
\item	Scene graphs and functional programs for all training/validation images/questions.
\end{itemize}