\documentclass[10pt]{article}
\input{articlepackage}
\usepackage[labelfont=bf]{caption}
\usepackage[margin=0.8in]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\small\textsc{Atmospheric Flux}}
\chead{}
\rhead{}
\lfoot{}
\cfoot{\textit{\thepage}}
\rfoot{}

\newcommand{\plotdir}[1]{/mnt/james/allMyStuff/BoostedDM/Python/plots/#1}
%\renewcommand{\plotdir}[1]{/Users/james/allMyStuff/BoostedDM/Python/plots/#1}
\renewcommand*{\thefootnote}{\fnsymbol{footnote}}
\setcounter{footnote}{1}

\begin{document}
\hrule
\vspace{1pt}
\hrule
\begin{center}
\large\textsc{\color{darkblue}\textbf{Calculating the Atmospheric Flux}}
\vspace{5pt}

\footnotesize\textit{\textbf{J.B.G. Alvey}, Theoretical Particle Physics and Cosmology, King's College London\footnote{\href{mailto:james.alvey@kcl.ac.uk}{james.alvey@kcl.ac.uk}}}
\end{center}
\begin{abstract}
\noindent Set of notes discussing the calculations involved in obtaining the atmospheric dark matter flux $\textrm{d}\phi_\chi/\textrm{d}T_\chi$ due to $pN \rightarrow \pi_0 \rightarrow \gamma\chi\chi$ interactions in the Earth's atmosphere.
\end{abstract}
\hrule
\tableofcontents
\vspace{5pt}
\hrule
\vspace{1pt}
\hrule
\vspace{10pt}
\renewcommand{\thefootnote}{\tiny\textbf{\arabic{section}.\arabic{footnote}}}
%\begin{multicols*}{2}
\section{Suppression of the Incident Proton Flux}
If we assume there is an initial proton flux, as measured for example by the AMS experiment, at the top of the atmosphere, then interactions with nitrogen molecules in the air will lead to a suppression at heights below this initial value. This will depend on the total inelastic cross section between the protons and the nitrogen molecules. The numerical value of this cross section, $\sigma_{pN}^{\textrm{\small inel}}$ is plotted in Figure \ref{fig:pnsigma}. We see from this that the cross section is approximately constant across the energy range where the AMS flux is parameterised (approximately $10^1 - 10^3 \, \textrm{GeV}$). As such, in what follows, we take th simplifying assumption that $\sigma_{pN}^{\textrm{\small inel}}$ is constant, with a value of $255 \, \textrm{mb}$. Now, denote the proton flux as a function of proton energy and height as $\textrm{d}\phi_p/\textrm{d}T_p(T_p, h)$. Evaluated at $h_{\textrm{\small max}} \simeq 180 \, \textrm{km}$, this is equal to the AMS flux discussed above. Letting the number density of nitrogen molecules in the atmosphere be $n_N(h)$, which is shown in Figure \ref{fig:airdensity}, the flux satisfies;
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{pNsigma.pdf}}
        \caption{The total $pN$ cross section as a function of incident proton energy.}
        \label{fig:pnsigma}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{air_density.pdf}}
        \caption{The number density of nitrogen molecules in the Earth's atmosphere as a function of height.}
        \label{fig:airdensity}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{suppression.pdf}}
        \caption{The suppression function $Y(h)$ as a function of $h$.}
        \label{fig:supp}
    \end{subfigure}
    \caption{Plots of the $pN$ cross section and the air density.}
    \label{fig:pnair}
  \end{figure}
\end{center}
\begin{equation}
  \label{eq:sup}
  \frac{\ud}{\ud h}\frac{\ud \phi_p(T_p, h)}{\ud T_p} = \sigma_{pN}^{\textrm{\small inel}}n_N(h)\frac{\ud \phi_p(T_p, h)}{\ud T_p}
\end{equation}
With the simplifying assumption regarding the cross section we see that this is now separable. If we let;
\begin{equation}
  \frac{\ud \phi_p(T_p, h)}{\ud T_p} := Y(h)\frac{\ud\phi_p(T_p, h_{\textrm{\small max}})}{\ud T_p}
\end{equation}
In other words, we introduce a suppression factor $Y(h)$ normalised such that $Y(h_{\textrm{\small max}} = 180\,\textrm{km}) = 1$. Substituting this into \eqref{eq:sup}, we find the following equation for $Y(h)$,
\begin{equation}
  \frac{\ud Y(h)}{\ud h} = \sigma_{pN}^{\textrm{\small inel}} n_N(h) Y(h)
\end{equation}
Integrating and using the initial conditions on $Y(h)$ we find the solution;
\begin{equation}
  Y(h) = \exp\left(-\sigma_{pN}^{\textrm{\small inel}}\int_{h}^{h_{\textrm{\small max}}}{\upd{\tilde{h}} n_N(\tilde{h})}\right)
\end{equation}
We can integrate this numerically using the parameterisation for the number density, the results are shown in Figure \ref{fig:supp}. We note in particular that it is approximately constant above $h = 20 \, \textrm{km}$, below which is decreases rapidly. \textbf{Something to consider is whether the function} $Y(h)$ \textbf{should depend on angle} $\theta$.
\section{The Resultant Dark Matter Flux}
Now we have an expression for the attenuation of the proton flux as a function of height, we need to calculate the dark matter flux $\ud \phi_\chi/\ud T_\chi$. If we let the radius of the Earth be $R_E = 6378.1 \, \textrm{km}$, then the differential flux can be calculated via;
\begin{multline}
  \frac{\ud \phi_\chi}{\ud T_\chi} = \int_{0}^{H}{(R_E + h)^2\upd{h}\int_0^{2\pi}{\upd{\phi} \int_{-1}^{+1}{\upd{\cos\theta}\frac{n_N(h)Y(h)}{2\pi \ell(R_E + h, \theta)^2}}}\times \int_{T_p^{\textrm{\tiny min}}}^{T_p^{\textrm{\tiny max}}}}{\upd{T_p} \sigma_{pN\rightarrow \chi\chi}(T_p)f(T_p, T_\chi)\frac{\ud \phi_p(T_p, h_{\textrm{\small max}})}{\ud T_p}}
\end{multline}
There are a couple of things to notice about this formula. Firstly, the geometry is shown in Figure \ref{fig:geometry}, with the angles $\theta$ and $\phi$ as illustrated. It has been assumed that the Earth is transparent to dark matter in this case. With these definitions, the function $\ell(r, \theta)$ is given by;
\begin{equation}
  \ell^2(r, \theta) = 2 R_E r (1 - \cos\theta) + (r - R_E)^2
\end{equation}
Secondly, we note that the simplification that the $pN$ cross section is independent of energy ensures that the prefactor before the multiplication sign is just a constant geometrical factor, which we denote $F_p$. This means that the Monte Carlo techniques used to evaluate the second integral can be performed independently. From a notational perspective, $f(T_p, T_\chi)$ accounts for the multiplicity of the dark matter particles produced at each energy $T_\chi$ for a given proton energy $T_p$. It should have units of inverse energy. Finally, $H$ is some reference height up to which the integration is performed which should be determined.
\subsection{The Geometrical Factor}
We evaluate the geometrical factor in the case that $Y(h)$ does not depend on the angle. In this case, the integral becomes;
\begin{align*}
  F_g(h_1, h_2) &= \int_{h_1}^{h_2}{\upd{h} (R_E + h)^2 \int_{0}^{2\pi}{\upd{\phi} \int_{-1}^{+1}{\upd{\cos\theta}n_N(h)Y(h)\frac{1}{2\pi \ell^2(R_E + h, \theta)}  } } } \\
  &= 2\pi\int_{h_1}^{h_2}{\upd{h} (R_E + h)^2 \int_{-1}^{+1}{\upd{\cos\theta}n_N(h)Y(h)\frac{1}{2\pi \left(2R_E (R_E + h)(1 - \cos\theta) + h^2\right)}  } } \\
  &= \int_{h_1}^{h_2}{\upd{h} (R_E + h)^2 n_N(h)Y(h) \int_{-1}^{+1}{\upd{\cos\theta}\frac{1}{ \left(2R_E (R_E + h)(1 - \cos\theta) + h^2\right)}  } }
\end{align*}
Using the fact that $2R_E(R_E + h) + h^2 \geq 2R_E(R_E + h) > 0$, we can do the angular integration analytically to obtain;
\begin{equation}
  F_g(h_1, h_2) = \int_{h_1}^{h_2}{\upd{h} \frac{R_E + h}{2R_E} n_N(h)Y(h) \log\left(\frac{4R_E(R_E + h) + h^2}{h^2}\right)}
\end{equation}
The results of doing this integral and the corresponding atmospheric flux are given in Figure \ref{fig:atmosdmflux}.
\begin{center}
  \begin{figure}[h]
    \centering
    \includegraphics[width=0.7\textwidth]{\plotdir{atmos_dm_flux_pospelov.pdf}}
    \caption{The resulting dark matter flux, not accounting for any attenuation after it is produced.}
    \label{fig:atmosdmflux}
  \end{figure}
\end{center}
\section{Full Derivation including Attenuation}
Ultimately the goal is to calculate the dark matter flux a depth $h_d$ below the surface of the Earth, where the detector is located. For Xenon 1T, this depth is approximately $6\,\textrm{km}$. To do so, we first compute the flux of dark matter particles produced $\textrm{cm}^{-3}\,\textrm{s}^{-1}\,\textrm{GeV}^{-1}$ at a position $(h, \theta, \phi)$, as labelled in Figure \ref{fig:dmatt}. Furthermore, we assume that the flux produced at this point recieves an attentuation due to nucleon interactions given by $Y_d(h, \theta, \phi)$. In other words, there is a position dependent attenuation that depends on the line of sight density to the detector. The total dark matter flux at the detector is then an integral over all the possible production points;
\begin{equation}
  \frac{\ud \phi^{\textrm{\tiny det}}_\chi}{\ud T_\chi}[\textrm{cm}^{-2}\,\textrm{s}^{-1}\,\textrm{GeV}^{-1}] = \int_{0}^{h_{\textrm{\tiny max}}}{\upd{h} (R_E + h)^2 \int_{0}^{2\pi}{\upd{\phi} \int_{-1}^{+1}{\upd{\cos\theta} \frac{1}{\ell^2_d(h, \theta)} Y_d(h, \theta, \phi) \frac{\ud \phi_\chi}{\ud\cos\theta \, \ud \phi \, \ud h \, \ud T_\chi}  }  }  }
\end{equation}
where $\ud \phi_\chi/\ud\cos\theta \,\ud \phi\, \ud h \,\ud T_\chi \,[\textrm{cm}^{-3}\,\textrm{s}^{-1}\,\textrm{sr}^{-1}\,\textrm{GeV}^{-1}]$ is the flux of dark matter particles produced at a position $(h, \theta, \phi)$. Furthermore, $l_d(h, \theta)$ denotes the line of sight distance between the production point and the detector.  It is given by;
\begin{equation}
  \ell^2_d(h, \theta) = (R_E + h)^2 + (R_E - h_d)^2 - 2(R_E + h)(R_E - h_d)\cos\theta
\end{equation}
\begin{center}
  \begin{figure}[h]
    \centering
    \includegraphics[width=0.4\textwidth]{\plotdir{nE.pdf}}
    \caption{The number density within the Earth assuming the average mass per nucleon is $11.8871 m_p$.}
    \label{fig:patt2}
  \end{figure}
\end{center}
\subsection{The Supression Factor $Y_d(h, \theta, \phi)$}
If a flux $\ud \phi_\chi/\ud T_\chi$ propagates along a line of sight from $(h, \theta, \phi)$ to the detector, and the cross-section $\sigma_{\chi N}$ is independent of energy, then we should do the following integral,
\begin{equation}
  \label{eq:yd}
  Y_d(h, \theta, \phi) = \exp\left(-\sigma_{\chi N} \int_{\mathcal{C}}{\upd{\ell} n}\right)
\end{equation}
where $n$ is the number density, a plot of which is shown in Figure \ref{fig:nE}. If we parametrise the path as in \ref{fig:dmatt} then the suppression factor is given by;
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{dmattenuation.pdf}
        \caption{The geometry of the detector location at a depth $h_d$ below the Earth's surface.}
        \label{fig:dmatt}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{pattenuation.pdf}
        \caption{The geometry for the proton attenuation as a function of height.}
        \label{fig:patt}
    \end{subfigure}
    \caption{Geometries for the attenuation of the dark matter and the proton fluxes}
    \label{fig:pnair}
  \end{figure}
\end{center}
\begin{equation}
  \label{eq:dmsup}
  Y_d(h, \theta, \phi) = \exp\left(-\sigma_{\chi N} \int_{0}^{\ell_d(h, \theta)}{\upd{z}n\left(r(z) - R_E\right)}\right)
\end{equation}
The choice of radial co-ordinate is in line with our definition of the nitrogen density using $h = 0$ as the surface of the Earth. To find $r(z)$, we use;
\begin{equation}
  \frac{\ell_d(h, \theta)}{\sin\theta} = \frac{R_E + h}{\sin\alpha} \Rightarrow \sin\alpha = \frac{(R_E + h)\sin\theta}{\ell_d(h, \theta)}
\end{equation}
Then we can calculate;
\begin{equation}
  r(z, h, \theta)^2 = (R_E - h_d)^2 + z^2 - \textrm{sign}\left[(R_E - h_d) - (R_E + h)\cos\theta\right]2(R_E - h_d)z\left(1 - \frac{(R_E + h)^2\sin^2\theta}{\ell^2_d(h, \theta)}\right)^{1/2}
\end{equation}
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{rexamples.pdf}}
        \caption{The function $r(z, h, \theta)$ illustrating the expected behaviour for different values of $\theta$.}
        \label{fig:rexample}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{Ydcontour.pdf}}
        \caption{A contour plot of $Y_d(h, \theta, \sigma_\chi^{\textrm{SI}} = 10^{-32}\,\textrm{cm}^2)$.}
        \label{fig:Ydcontour}
    \end{subfigure}
    \caption{The behaviour of $Y_d(h, \theta, \sigma_\chi^{\textrm{SI}})$ and $r(z, h, \theta)$ as a function of $h$ and $\theta$.}
    \label{fig:Yd}
  \end{figure}
\end{center}
where the $\pm$ arises due to the angle $\alpha$ being acute or obtuse. In the former case, $\cos\alpha$ will be positive, whilst in the latter case it will be negative. This is shown in Figure \ref{fig:rexample}. From this we can also derive the line of sight distance only through the Earth by noticing that this distance is given by $z_{\star}$ where $r(z_\star, h, \theta) = R_E$. There will be two such solutions, one positive, and one negative. We should take the positive one. This satisfies;
\begin{equation}
R_E^2 = (R_E - h_d)^2 + z_\star^2 - \textrm{sign}\left((R_E - h_d) - (R_E + h)\cos\theta\right)\cdot 2(R_E - h_d)z_\star\left(1 - \frac{(R_E + h)^2 \sin^2\theta}{\ell_d^2(h, \theta)}\right)^{1/2}
\end{equation}
Solving for $z_\star$, we find;
\begin{equation}
  z_\star = \frac{1}{2}\left(b(h, \theta) + \sqrt{b^2(h, \theta) + 4\left(R_E^2 - (R_E - h_d)^2\right)}\right)
\end{equation}
where,
\begin{equation}
  b(h, \theta) = \textrm{sign}\left((R_E - h_d) - (R_E + h)\cos\theta\right)\cdot 2(R_E - h_d)\left(1 - \frac{(R_E + h)^2 \sin^2\theta}{\ell_d^2(h, \theta)}\right)^{1/2}
\end{equation}
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{zstarexample.pdf}}
        \caption{The function $z_\star(h, \theta)$ illustrating the expected behaviour for different values of $\theta$.}
        \label{fig:zexample}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{zstarcontour.pdf}}
        \caption{A contour plot of $Y_d(h, \theta, \sigma_\chi^{\textrm{SI}} = 10^{-32}\,\textrm{cm}^2)$.}
        \label{fig:zstarcontour}
    \end{subfigure}
    \caption{The behaviour of $z_\star(h, \theta)$ as a function of $h$ and $\theta$.}
    \label{fig:zstar}
  \end{figure}
\end{center}
The results are shown in Figure \ref{fig:zstar} for a detector at depth 1.4 km. If we assume a distinction between the core and the mantle, then we also need to calculate how much of this path is within the core, with radius $r_c$. For a given height $h$, the line of sight will intersect the core only if;
\begin{equation}
\theta > \theta_{\star}(h) = \arccos\left(\frac{r_c}{R_E - h_d}\right) + \arccos\left(\frac{r_c}{R_E + h}\right)
\end{equation}
Now, if $\theta$ is indeed larger than this value, then we can calculate the two line of sight distances $z_{\textrm{core}}$ and $z_{\textrm{mantle}}$. This can be done by solving for $z$ when $r(z, h, \theta) = r_c$. For $\theta > \theta_{\star}(h)$ this will give two solutions, the difference of which is $z_{\textrm{core}}$;
\begin{equation}
  z_{\textrm{core}}(h, \theta) = \sqrt{b(h, \theta)^2 - 4\left((R_E - h_d)^2 - r_c^2\right)}
\end{equation}
This can then be used to calculate $z_{\textrm{mantle}}(h, \theta)$. Taking into account the cutoff on $\theta$ due to the finite extent of the core, we find;
\begin{equation}
  z_{\textrm{core}}(h, \theta) = \begin{cases} 0 & \text{if } \theta \leq \theta_{\star}(h) \\ \sqrt{b(h, \theta)^2 - 4\left((R_E - h_d)^2 - r_c^2\right)} & \text{if } \theta > \theta_{\star}(h)\end{cases}, \qquad z_{\textrm{mantle}}(h, \theta) = z_{\star}(h, \theta) - z_{\textrm{core}}(h, \theta)
\end{equation}
We can then substitute these definitions into \eqref{eq:dmsup} to find the suppression factor $Y_d(h, \theta)$. In order to actually calculate this, we need to parametrise the number density of the Earth. We also need to understand what $\sigma_{\chi N}$ represents in \eqref{eq:yd}. From the Pospelov paper, we have the expression;
\begin{equation}
  \sigma_{\chi N} = \sigma_\chi^{\textrm{SI}} A^2 \left(\frac{m_N(m_\chi + m_p)}{m_p(m_\chi + m_N)}\right)^2
\end{equation}
The mass range of dark matter we are interested in means that $m_\chi \ll m_p, m_N$ so we can approximate this by;
\begin{equation}
  \sigma_{\chi N} = \sigma_{\chi}^{\textrm{SI}}A^2
\end{equation}
The number quoted is $A^2 = 165.025$ is a weighted sum of the fractions of elements in the Earth's crust and mantle. A similar weighted average gives a nuclear mass of $11.8871 m_p$. This will be used to convert the density in units $\textrm{g}\,\textrm{cm}^{-3}$ to a number density in units $\textrm{cm}^{-3}$. The results for a cross section of $\sigma_chi^{\textrm{SI}} = 10^{-32}\,\textrm{cm}^2$ are shown in Figure \ref{fig:Ydcontour}.
\subsection{Attenuation of the Proton Flux}
It remains to calculate the factor $\ud \phi_\chi / \ud \cos\theta \, \ud \phi \,\ud h \, \ud T_\chi$. This is given by;
\begin{equation}
  \frac{\ud \phi_\chi (h, \theta, \phi)}{\ud\cos\theta \, \ud \phi \, \ud h} = n_N(h) \int_{T_p^{\textrm{\tiny min}}}^{T_p^{\textrm{\tiny max}}}{\upd{T_p}\frac{1}{\Omega(T_p)}\frac{\ud\phi_p(T_p; h, \theta, \phi)}{\ud T_p}\frac{\ud \sigma_{pN \rightarrow \chi\chi}(T_p)}{\ud T_\chi}}
\end{equation}
where $\Omega(T_p)$ is the opening solid angle for the boosted dark matter pointing towards the detector, as in Figure \ref{fig:patt}. We see that we need to determine the proton flux as a function of $h$ and $\theta$. Note that we are interested only in the proton flux that will lead to dark matter that actually hits the detector. This is in contrast to the expressions in the simplified case, where the opening angle was assumed to be $2\pi$. Now, the flux that leads to dark matter travelling towards the detector can be written as;
\begin{equation}
  \frac{\ud \phi_p(h, \theta)}{\ud T_p} = Y_p(h, \theta)\frac{\ud \phi_p(h_{\textrm{\tiny max}})}{\ud T_p}
\end{equation}
Here, $Y_p(h, \theta)$ denotes the average suppression that accounts for the propagation of protons from all points in the solid angle $2\Omega$ as in Figure \ref{fig:patt}. If we let this region of solid angle be denoted $\mathcal{A}$, then;
\begin{equation}
  Y_p(h, \theta) = \frac{\int_{\mathcal{A}}{\upd{\tilde{\Omega}} \exp\left(-\sigma_{pN}^{\textrm{\tiny inel.}} \int_{\mathcal{C}}{n\,\ud l}\right)   }}{\int_{\mathcal{A}}{\upd{\tilde{\Omega}}}}
\end{equation}
where the line of sight integration along $\mathcal{C}$ depends on the path from the edge of the atmosphere to the point $(h, \theta, \phi)$. For small $\Omega$, this is approximately given by;
\begin{equation}
  Y_p(h, \theta) \simeq \exp\left(-\sigma_{pN}^{\textrm{\tiny inel.}} \int_{\mathcal{C^\star}}{n_N \, \ud l}\right)
\end{equation}
where now $\mathcal{C}^\star$ is the line of sight between the edge of the atmosphere and the detector, through the point $(h, \theta, \phi)$. This is illustrated in Figure \ref{fig:patt2}. With these definitions, we have;
\begin{equation}
    Y_p(h, \theta) = \exp\left(-\sigma_{pN}^{\textrm{\tiny inel.}}\int_{0}^{\ell_p(h, \theta)}{\upd{\tilde{z}}n_N\left(r_p(\tilde{z}) - R_E\right)}\right)
\end{equation}
Now we need to find $\ell_p(h, \theta)$ and $r_p(\tilde{z}, h, \theta)$. We let $\theta_2$ be the angle between the detector, the point $(h, \theta, \phi)$ and the centre of the Earth, and the angle $\alpha_1$ be the one between the centre of the Earth, the point at $h_{\textrm{\tiny max}}$ and the detector. Then we find;
\begin{center}
  \begin{figure}[h]
    \centering
    \includegraphics[width=0.6\textwidth]{pattenuation2.pdf}
    \caption{The geometry of the integration over the proton path.}
    \label{fig:patt2}
  \end{figure}
\end{center}
\begin{equation}
  r^2_p(\tilde{z}, h, \theta) = (R_E + h_{\textrm{\tiny max}})^2 + \tilde{z}^2 - 2(R_E + h_{\textrm{\tiny max}})\tilde{z}\cos\alpha_1
\end{equation}
Using the sine rule, we have;
\begin{equation}
    \frac{\ell_d(h, \theta)}{\sin\theta} = \frac{R_E - h_d}{\sin\theta_2} \,\, \Rightarrow \,\, \sin\theta_2 = \frac{(R_E - h_d)\sin\theta}{\ell_d(h, \theta)}
\end{equation}
So,
\begin{equation}
  \frac{R_E + h_{\textrm{\tiny max}}}{\sin(\pi - \theta_2)} = \frac{R_E + h}{\sin\alpha_1} \,\,\Rightarrow\,\, \sin\alpha_1 = -\frac{(R_E + h)(R_E - h_d)\sin\theta}{(R_E + h_{\textrm{\tiny max}})\ell_d(h, \theta)}
\end{equation}
This in turn implies that;
\begin{equation}
  \cos\alpha_1 = \left(1 - \frac{(R_E + h)^2(R_E - h_d)^2\sin^2\theta}{(R_E + h_{\textrm{\tiny max}})^2\ell^2_d(h, \theta)}\right)^{1/2}
\end{equation}
Then we can write down $r_p(\tilde{z}, h, \theta)$ as;
\begin{equation}
  r^2_p(\tilde{z}, h, \theta) = (R_E + h_{\textrm{\tiny max}})^2 + \tilde{z}^2 - 2(R_E + h_{\textrm{\tiny max}})\tilde{z}\left(1 - \frac{(R_E + h)^2(R_E - h_d)^2\sin^2\theta}{(R_E + h_{\textrm{\tiny max}})^2\ell^2_d(h, \theta)}\right)^{1/2}
\end{equation}
Note in this case that we do not have the same issues with positive and negative $\cos\alpha_1$ as one can check that it does not change sign and is initially positive for $\theta = 0$. Now to find $\ell_p(h, \theta)$, we note that $\tilde{z} = R_E + h$ when $r_p(\tilde{z}) = \ell_p(h, \theta)$. So we can solve the quadratic in $\ell_p(h, \theta)$ given by;
\begin{equation}
  (R_E + h)^2 = (R_E + h_{\textrm{\tiny max}})^2 + \ell^2_p(h, \theta) - 2(R_E + h_{\textrm{\tiny max}})\ell_p(h, \theta)\left(1 - \frac{(R_E + h)^2(R_E - h_d)^2\sin^2\theta}{(R_E + h_{\textrm{\tiny max}})^2\ell^2_d(h, \theta)}\right)^{1/2}
\end{equation}
There will be two solutions to this equation, one of will be the distance we are looking for, the other will be the distance to the point the other side of the detector throught the line of sight. Clearly this latter distance is larger, so we should take the smaller of the two solutions. Solving the quadratic and taking the negative square root, we find;
\begin{equation}
  \ell_p(h, \theta) = (R_E + h_{\textrm{\tiny max}})\left(1 - \frac{(R_E + h)^2(R_E - h_d)^2 \sin^2\theta}{(R_E + h_{\textrm{\tiny max}})^2 \ell^2_d(h, \theta)}\right)^{1/2} - (R_E + h)\left(1 - \frac{(R_E - h_d)^2 \sin^2\theta}{\ell^2_d(h, \theta)}\right)^{1/2}
\end{equation}
The behaviour of $\ell_p(h, \theta)$ as a function of $h$ and $\theta$ is shown in Figures \ref{fig:lpcontour} and \ref{fig:lpexample}
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{lpcontour.pdf}}
        \caption{A contour plot illustrating the behaviour of $\ell_p(h, \theta)$ as a function of $h$ and $\theta$.}
        \label{fig:lpcontour}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{lpexample.pdf}}
        \caption{The function $\ell_p(h, \theta)$ for a selection of values of $\theta$.}
        \label{fig:lpexample}
    \end{subfigure}
    \caption{The behaviour of $\ell_p(h, \theta)$ as a function of $h$ and $\theta$.}
    \label{fig:lp}
  \end{figure}
\end{center}
We also present in Figure \ref{fig:Yp} the results for $Y_p(h, \theta)$. We see that indeed we reproduce the suppression factor found in earlier sections for $\theta = 0$, but that for other values of $\theta$, the non-trivial geometry leads to a different scale height as the protons propagate through the atmosphere.
\begin{center}
  \begin{figure}[h]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{Ypcontour.pdf}}
        \caption{A contour plot illustrating the behaviour of $Y_p(h, \theta)$ as a function of $h$ and $\theta$.}
        \label{fig:Ypcontour}
    \end{subfigure}%
    \hfill
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{\plotdir{Ypexample.pdf}}
        \caption{The function $Y_p(h, \theta)$ for a selection of values of $\theta$.}
        \label{fig:Ypexample}
    \end{subfigure}
    \caption{The behaviour of $Y_p(h, \theta)$ as a function of $h$ and $\theta$. We do not show $h$ all the way up to the top of the atmosphere, since the curve remains very close to $1$ indicating that there is little suppression above these heights.}
    \label{fig:Yp}
  \end{figure}
\end{center}
\subsection{Summary and Final Expression}
We can now bring all the expressions together. We find, including the attenutation effects for both the dark matter and the proton flux, the dark matter flux at a detector $h_d \, \textrm{m}$ below the surface of the Earth is given by;
\begin{multline}
  \frac{\ud \phi^{\textrm{\tiny det}}_\chi(\sigma^{\textrm{SI}}_{\chi})}{\ud T_\chi} = \int_0^{h_{\textrm{\tiny max}}}{\upd{h}(R_E + h)^2 \int_{0}^{2\pi}{\upd{\phi} \int_{-1}^{+1}{\upd{\cos\theta} \frac{Y_d(h, \theta; \sigma^{\textrm{SI}}_{\chi})Y_p(h, \theta)}{\ell_d^2(h, \theta)} n_N(h)  }  }  } \\ \times \int_{T_p^{\textrm{\tiny min}}}^{T_p^{\textrm{\tiny max}}}{\upd{T_p}\frac{1}{\Omega(T_p)}\frac{\ud\phi_p(h_{\textrm{\tiny max}})}{\ud T_p}\frac{\ud \sigma_{pN \rightarrow \chi\chi}}{\ud T_\chi}   }
\end{multline}
where the various factors are as defined in the previous sections. Now, as in the simplified case where the proton flux was only incident from above and there was no attenuation, the second part of the integral is really independent of the geometry, so we can parametrise the flux via;
\begin{equation}
  \frac{\ud \phi^{\textrm{\tiny det}}_\chi(\sigma^{\textrm{SI}}_{\chi})}{\ud T_\chi} = G_{\textrm{\tiny det}}(\sigma^{\textrm{SI}}_{\chi}) \int_{T_p^{\textrm{\tiny min}}}^{T_p^{\textrm{\tiny max}}}{\upd{T_p}\frac{1}{\Omega(T_p)}\frac{\ud\phi_p(h_{\textrm{\tiny max}})}{\ud T_p}\frac{\ud \sigma_{pN \rightarrow \chi\chi}}{\ud T_\chi}   }
\end{equation}
where now the geometrical factor is given by;
\begin{equation}
  G_{\textrm{\tiny det}}(\sigma^{\textrm{SI}}_{\chi}) = \int_0^{h_{\textrm{\tiny max}}}{\upd{h}(R_E + h)^2 \int_{0}^{2\pi}{\upd{\phi} \int_{-1}^{+1}{\upd{\cos\theta} \frac{Y_d(h, \theta; \sigma^{\textrm{SI}}_{\chi})Y_p(h, \theta)}{\ell_d^2(h, \theta)} n_N(h)  }  }  }
\end{equation}
%\end{multicols*}
\end{document}


\chapter{Applications of the Derivative}

\input{./applications_derivative/extreme_values.tex}
\input{./applications_derivative/mvt.tex}
\input{./applications_derivative/first_second_derivative_test.tex}
\input{./applications_derivative/modeling_optimization.tex}
\input{./applications_derivative/related_rates.tex}
\input{./applications_derivative/linearization_newtons_method.tex}


\input{preamble}

\begin{document}
    \section{Subjects}
    \begin{itemize}
        \item Different distance measures between evolutionary trees
        \item Computing distance between trees
    \end{itemize}

    \section{Notes}

    \subsection{Phylogenetic trees}
    There is strong evidence that all life on earth is decended from a single
    common ancestor. Over the course of at least 3.8 billion years, that life
    form has changed and split itself into new and independent lineages. The
    evolutionary relationships among these species is referred to as their
    phylogeny, phylogenetic reconstruction is concerned with inferring the
    phylogeny of groups of organisms.

    We call these groups \textit{taxa} (or singular taxon), the splitting of
    lineages is called \textit{speciation} (think of splitting it into
    different species). Usually speciation happens if one population is split
    into two that can no longer interbreed (e.g. if a river splits them apart).
    We can use methods of phylogenetic to contemplate over the tree of life, or
    simply to inferr the phylogeny of different populations within a species.

    In a rooted phylogenetic tree $T$, the root node $r$ corresponds to the
    last common ancestor of all species in $T$, we then call a path from the
    root to a leaf an \textit{evolutionary path}. If an equal amount of change
    occurs on every evolutionary path (i.e. each species have the same amount
    of ``change'') then the evolutionary change occur in a more-or-less
    clocklike fashion and we then say that this tree satisfies the
    \textit{molecular clock hypothesis}, we can then assign a time $t(v)$ to
    every internal node $v$ in the tree and a length of $t(v)-t(w)$ to an edge
    $(v,w)$ in the tree.

    Every extant (still alive) species corresponds to time $0$, and a
    speciation event (internal node $v$) occured in the tree $t(v)$ time ago.
    We then have that the length of an edge represents the amount of time that
    lies between two speciation events.

    Furthermore, the last common ancestor (root) lived $t(r)$ and all
    evolutionary paths have the same length $t(r)$ where the length of a path
    is the sum of the lengths of all edges along the path.

    Once the \textit{molecular clock hypotheses} was widely accepted, but has
    since been disproven so now time instead refers to the expected amount of
    evolution.

    \subsubsection{Distance methods}
    Distance methods construct a phylogenetic tree from a distance matrix that
    contains the evolutionary distances between all pairs of taxa (groups of
    organisms). If we ignore edge weights, we speak of the topology (shape) of
    a tree, and it turns out that we have methods that \textit{provably} find
    the correct tree if the distance matrix is ultrametric, and some that works
    if the distance matrix is additive. However the data we can record is
    usually approximation of the true (additive) data, and thus we can't rely
    on the distance matrix being additive. However, it turns out that the
    methods can still find the correct \textit{topology} of the tree under
    certain criteria.

    If we just have the topology, then we must assign weights to the edges of
    the tree that best fit the data, which can be done with the least squares
    methods.

    \subsubsection{Basic definitions}
    Phylogenies are usually represented as binary trees, because generially
    speciation happens when one lineage splits into two independent lineages.
    This is not entirely correct, sometimes horizontal gene-transfer happens
    and hybrid speciation but it is rare. So for simplicity we just look deal
    with phylogenetic trees, however let's not insist they have to be binary
    for the moment:

    Let $S=\{s_1,\dots,s_n\}$ be a set of taxa. A phylogenetic tree on $S$ is a
    triple $T=(V,E,\alpha)$ where:
    \begin{itemize}
        \item $V$ is the set of nodes, $E$ is the set of undirected edges
        \item $(V,E)$ is a an acyclic connected graph, in which there might be
        a distinguished root node of degree $\geq 2$ and all other internal
        nodes have a degree $\geq 3$. So either rooted or unrooted. We will
        denote the set of leaves by $V_L$ and the set of internal nodes by $V_I$
        \item $\alpha$ is a bijection $\alpha: S \rightarrow V_L$ between the
        set of taxa and the set of leaves
    \end{itemize}
    An edge $(v,w) \in E$ is an external edge if either $v$ or $w$ is a leaf.
    Otherwise it is an internal edge.

    We haven't included edge-weights here, they will be introduced later.

    \subsection{Distance between tree-topologies}
    Distance between trees is about measuring how far they are from each other,
    that is, how different are they. There are a number of different ways that
    have been proposed to this end, we will start by looking at the measures
    that compute the distance between the topologies of trees.

    \subsubsection{The symmetric difference}
    If we ignore the edge weights, then each edge can be seen as a branch which
    divides the species into a partition with two sets. If we make a list of
    the partitions each tree implies (e.g. $AB|CD$ and $A|B$ and $C|D$)  we
    will then simply count how many partitions are in one list which is not in
    the other and that will be the symmetric difference.

    The symmetric distance is very easy to compute, but is very sensitive to
    all differences between trees. Therefore a tree which has partial
    similarities, might still score the maximum possible distance.

    This can also be named the Robinson-Fould distance

    \subsubsection{The quartets distance}
    The \textit{quartets distance} is more sensitive to partial similarities of
    structures between trees. A quartet is four named species in an unrooted
    tree. Quartet topology is the topology of the quartet induced by the tree.

    The quartet distance is then simply the number of quartets that don't have
    the same topology in the two trees. Computing this distance can be done in
    \bigO{n^2}. It has been shown that it can be done in \bigO{n(\ln n)^2}

    \subsubsection{Nearest-Neighbour interchange distance}
    Simply, compute the minimum number of nearest-neighbour interchange
    re-arrangements that are needed to go from one tree to the other. Does not
    have teh same issue with tree-distance symmetric distance, however it is
    NP-Complete.

    \subsubsection{Computing RF-Distance}
    There are various ways to compute the Robinson Fouls distance (also called
    the symmetric difference). The \bigO{n} algorithm proposed by Day. So here
    come's Day's algorithm for computing the RF-Distance:
    \begin{itemize}
        \item Root both trees by the same leaf
        \item Do a DFS numbering of $T_1$
        \item Assign the same numbers to $T_2$
        \item Find all the ``valid'' intervals in $T_1$ and $T_2$
        \item Count the number of intervals that don't exist in both $T_1$ and
        $T_2$
    \end{itemize}

    \subsection{Distance between trees}
    Now, there are some distance-measures that take the branch-weights into
    account.

    The Robinson Foulds distance can be taken into account by, for each edge,
    compute the difference of the weights. If an edge exists in one and not the
    other (i.e. the case where we would nomrally count one up) we add the
    complete weight of the edge.

    \subsubsection{Computing the Quartet distance}
    The triplet and quartet distance are very close, I will focus on the
    Quartet distance.

    First let's look at some of the cases, the Quartet distance counts the
    number of quartets where the two trees disagree. To this end there are
    ``resolved'' and ``unresolved'' cases, the unresolved case, is when all the
    leaves are connected through a single node (all same parent).

    If both $T_1$ and $T_2$ deems the quartet to be unresolved, then they agree
    if either one or the other think it's unresolved then they disagree. If
    they both think it's resolved then they agree if they have the same
    topology. So we count the number of times they disagree. The number of
    quartets is $\begin{pmatrix}
    n\\
    4
    \end{pmatrix}$.
    \\
    \\
    Let's just look at binary trees, if we are dealing with binary trees, then
    the unresolved case is not possible, thus we just look at the quartets
    where the topologies differ.

    A dynamic programming approach, conceptually we simply say that each edge
    is oriented both ways, we then say that $F_1 \xrightarrow{e_1}(F_2, F_3)$
    in $T_1$, \textit{claims} all quartets, $ij|kl$ with $i,j \in F_1$ and
    either $ k \in F_2$ and $l \in F_3$ or vice versa, where $F_1$ is the tree
    behind the edge and $F_2, F_3$ is the two subtrees in front of the edge.
    Every shared quartet is counted twice, so we divide by two. Giving us the
    formula:

    \begin{equation*}
        Count(e_1,e_2) = \begin{pmatrix}
        |F_1 \cap G_1|\\
        2
        \end{pmatrix}
        (|F_2 \cap G_2| \cdot |F_3 \cap G_3| + |F_2 \cap G_3| \cdot |F_3 \cap
        G_2|)
    \end{equation*}
    \begin{equation*}
        d_{quartet}=\begin{pmatrix}
        n\\
        4
        \end{pmatrix}
        - \frac{1}{2} \sum_{e_1,e_2} Count(e_1, e_2)
    \end{equation*}

     and then we count the how many of these edges $ij
    \rightarrow kl$ are claimed by both trees. This can be done in \bigO{n^2}.

    There is another way that is based on tree coloring that can do it in
    \bigO{n \log n}
\end{document}


% Created By: Greg Futia
% Created On:
% Last Edited: Sept. 14th, 2015

%%%%%%%%%%%%%%%%%%%%%%% preamble %%%%%%%%%%%%%%%%%%%%%%%%%%%
%\documentclass[journal, draftcls]{IEEEtran}  % double space
\documentclass[journal]{IEEEtran}
%\usepackage{fixltx2e}
\usepackage{amssymb, amsmath, subfig, color, fancybox}
\usepackage[]{graphicx}
%\usepackage{stfloats}
\usepackage{epstopdf}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}

%\DeclareMathSizes{10}{8}{*}{*}
%\displaystyle 8

\usepackage{url}  % helps hyperlinks
% Configure the linking of references and citations
% hypertex for stright dvi, pdftex for stright PDf
\usepackage[hypertex, colorlinks=true, linkcolor=red, citecolor=blue, urlcolor=blue, linktoc=page]{hyperref}
\usepackage[noadjust]{cite}   % for Bibtex - needs to be after hyperref
%\usepackage[dvipdfm]{media9}

\usepackage{ae} %%for Computer Modern fonts; % fixes fonts not being found in yap

%%%%%%%%%%%%%%%%%%%%%%% begin %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

%%%%%%%%%%%%%%%%%% title page information %%%%%%%%%%%%%%%%%%
\title{Regressed Equations Day 9}

%\author{Gregory L. Futia$^*$,~\IEEEmembership{Member,~IEEE,} Lubna Qamar, Kian Behbakht, and Emily A. Gibson
%\author{Gregory L. Futia, Lubna Qamar, Kian Behbakht, and Emily A. Gibson
%\thanks{Manuscript received \today; revised TBD}
%\IEEEcompsocitemizethanks{\IEEEcompsocthanksitem $^*$ G. Futia and E. Gibson are with the Department of Bioengineering, University of Colorado Denver | Anschutz Medical Campus, Aurora, Co. 80045. (e-mail: gregory.futia@ucdenver.edu)
%\IEEEcompsocthanksitem K. Behbakht and L. Qamar are with the Department Obstetrics and Gynecology, Division of Gynecologic Oncology University of Colorado Denver | Anschutz Medical Campus, Aurora, Co. 80045. (e-mail: kian.behbakt@ucdenver.edu)}%
%}

\newcommand{\EQN}[0]{Eq. }
\newcommand{\FIG}[0]{Fig. }
\newcommand{\SEC}[0]{Sec. }
\newcommand{\TAB}[0]{Table }
\newcommand{\APX}[0]{Appendix }
\newcommand{\note}[1]{{\bf[#1]}}
\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\newcommand{\tablefont}[1]{\fontsize{#1}{#1}\selectfont}
\newcommand{\UL}[0]{$\rm \mu L$}
\newcommand{\UM}[0]{$\rm \mu m$}

% \homepage{http:...} %% author's URL, if desired

%%%%%%%%%%%%%%%%%%% abstract and OCIS codes %%%%%%%%%%%%%%%%
%% [use \begin{abstract*}...\end{abstract*} if exempt from copyright]

\maketitle

%\begin{abstract}

%\end{abstract}

%\ocis{(000.0000) General.} % REPLACE WITH CORRECT OCIS CODES FOR YOUR ARTICLE

%%%%%%%%%%%%%%%%%%%%%%%%%%  body  %%%%%%%%%%%%%%%%%%%%%%%%%%

%\subsection{Multivariate Analysis and Regression}

The regressions trained with all day 9 data.

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg1_{all}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG1_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg2_{Sigma}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG2_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{"Reg3_{DAPI + CD45 + PanCK}.tex"} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG3_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{"Reg4_{DAPI + Bodipy + CD45}.tex"} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG5_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{"Reg5_{DAPI + CD45}.tex"} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG5_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{"Reg6_{DAPI + PanCK}.tex"} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{"Reg7_{DAPI + Bodipy}.tex"} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg8_{DAPI}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg9_{Bodipy}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg10_{CD45}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

\begin{gather}
%\tablefont{9pt}
\begin{aligned}
\input{Reg11_{PanCK}.tex} , % this file is generated in matlab.. this ensures that the metrics are up to date
\label{Eqn: REG6_DNA_BD}
\end{aligned}
\end{gather}

%%%%%%%%%%%%%%%%%%%%%%% References %%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{thebibliography}{1}
%\bibitem{gallo99} K. Gallo and G. Assanto, ``All-optical diode based on second-harmonic generation in an asymmetric waveguide,'' \josab {\bf 16,} 267--269 (1999).

%\bibliographystyle{IEEEtran}
%\bibliography{references_bib}
%
%\begin{figure}[h]
%    \begin{centering}
%    \subfloat{
%    \includegraphics[]{Graphics/RegressionRoc.eps}
%    }
%    \subfloat{
%    \includegraphics[]{Graphics/DistFitGauss.eps}
%    }
%    \caption[]{The dashed red and blue lines are the fitted gaussians used to create the fitted ROC curve of \FIG \ref{fig: Regression ROC Curves}. Clearly they don't fit the distributions that well but when the curves represent the distribution better the ROC curve runs significantly higher than what it should be.
%    \label{fig: Regression ROC Curves Fit Guass}}
%    \end{centering}
%\end{figure}
%
%
%\begin{figure}[h]
%    \begin{centering}
%    \subfloat{
%    \includegraphics[]{Graphics/RegressionRocGaussLin.eps}
%    }
%    \subfloat{
%    \includegraphics[]{Graphics/DistFitGaussLin.eps}
%    }
%    \caption[]{Here is doing a linear fit to the Gaussian. The fit curves look good but the ROC curve is way better than that of the origional data. You can see the roll off is steeper than the origional data particularly on the log scale.
%    \label{fig: Regression ROC Curves Fit GuassLin}}
%    \end{centering}
%\end{figure}
%
%\begin{figure}[h]
%    \begin{centering}
%    \subfloat{
%    \includegraphics[]{Graphics/RegressionRocLorentz.eps}
%    }
%    \subfloat{
%    \includegraphics[]{Graphics/DistFitLorentz.eps}
%    }
%    \caption[]{Here is fitting the curves to a Lorentzian function instead of a Gaussian. The lorentzian has must slower roll off meaning the tails of the distributions don't go to zero (nearly) as rapdily.
%    \label{fig: Regression ROC Curves Fit Lorentz}}
%    \end{centering}
%\end{figure}

%\end{thebibliography}

%\begin{IEEEbiography}{Gregory L. Futia}
%earned his M.S. degree in Electrical Engineering from Colorado State University in 2011 working with Dr. Bartels with thesis on theory and implementation of spatial frequency modulated imaging. He earned is B.S. degree in Electrical Engineering from Purdue University in 2007.
%
%Since 2011, he has been a PhD student in Bioengineering at the University of Colorado | Anschutz Medical Campus. His research is focused on the exploration of new biomarkers for CTCs. He is particularly interested in optical methods for CTC detection and isolation.
%\end{IEEEbiography}
%
%
%\begin{IEEEbiography}{Lubna Qamar}
%
%\end{IEEEbiography}
%
%\begin{IEEEbiography}{Kian Behbakht}
%is a processor of gynecologic Oncology and Reproductive Science, adjunct professor in Pharmacology, as well as a practicing clinical gynecologic oncologist at the University of Colorado Denver | Anschutz Medical Campus and University of Colorado Hospital.
%
%He is interested in the pathogenesis of ovarian cancer and the epidemiology of cervical cancer. His clinical interests are endoscopic and fertility preserving procedures in gynecologic cancer.
%\end{IEEEbiography}
%
%\begin{IEEEbiography}{Emily A. Gibson}
%is a assistant professor of Bioengineering at the University of Colorado Denver | Anschutz Medical Campus. She earned her PhD in physics from the the University of Colorado Boulder in 2007 with research optical generation of soft x-rays.
%
%One area of her research is the development of new diagnostic tools by integrating microfluidic devices with laser spectroscopy for high throughput analysis and sorting biological samples. Her research is focused on applying optical techniques to improve early cancer detection and treatment of diabetes, among other medical applications.
%\end{IEEEbiography}

\end{document}


The Functional Point approach is a technique that allows to evaluate the effort
needed for the design and implementation of a project. We have used this technique
to evaluate the application dimension basing on the functionalities of the
application itself. The functionalities list has been obtained from the RASD document
and for each one of them the realization complexity has been evaluated.
The functionalities has been groped in:
\begin{itemize}
\item Internal Logic File: it represents a set of homogeneous data handled by the system
\item External Interface File: it represents a set of homogeneous data used
by the application but handled by external application
\item External Input: elementary operation that allows input of data in the system
\item External Output: elementary operation that creates a bit stream towards
the outside of the application
\item External Inquiry: elementary operation that involves input and output operations
\end{itemize}

\newpage

The following table outline the number of Functional Point based on functionality
and relative complexity:

\begin{table}[h]
	\centering
	\begin{tabular}{| l | l | l | l |}
		\hline
		\multirow{2}{*}{\textbf{Function Type}} & \multicolumn{3}{c|}{\textbf{Complexity}} \\
		\cline{2-4}
		& Simple & Medium & Complex  \\
		\hline
		Internal Logic File & 7 & 10 & 15 \\ \hline
		External Interface File & 5 & 7 & 10 \\ \hline
		External Input & 3 & 4 & 6 \\ \hline
		External Output & 4 & 5 & 7 \\ \hline
		External Inquiry & 3 & 4 & 6 \\ \hline

	\end{tabular}
\end{table}

\section{Internal Logic File}
Internal Logic Files are homogeneous sets of data used and managed by the application.\\
Summary of the calculated weights:
\begin{equation*}
	\begin{aligned}
		&	FP_{ILF}
		& & = 5 \cdot w_{Simple,ILF} + 3 \cdot w_{Medium,ILF} + 1 \cdot w_{Complex,ILF}\\
		&&& = 5 \cdot 7 + 3 \cdot 10 + 1 \cdot 15\\
		&&& = 80\\
	\end{aligned}
\end{equation*}
In details:
\begin{itemize}
	\fp{Ride}{Ride data. Frequent modifications, frequent insertions, frequent readings.}{1}{w_{Medium,ILF}}
  \fp{User}{Personal Registered Passenger data. Infrequent modifications, but frequent insertions and readings.}{1}{w_{Medium,ILF}}
  \fp{Car}{Car information. Infrequent insertion and modification, frequent reading}{1}{w_{Medium,ILF}}
	\fp{Customer Service}{Customer help request. Very infrequent modifications, insertions and readings.}{1}{w_{Simple,ILF}}
	\fp{Payment}{Data of the ended rent. Infrequent modification and reading, very frequent insertion}{1}{w_{Complex,ILF}}
	\fp{Payment Method}{User's payment data, Frequent readings, infrequent modification and insertion}{1}{w_{Simple,ILF}}
	\fp{Parking}{Parking data, frequent reading, infrequent insertion and modifications}{1}{w_{Simple,ILF}}
	\fp{Service Station}{Service station data. frequent reading, infrequent insertion and modifications}{1}{w_{Simple,ILF}}
	\fp{GPS Data}{GPS Data, Database Server side. Very frequent insertions and readings, but very infrequent modifications.}{1}{w_{Simple,ILF}}
\end{itemize}
%
\section{External Interface File}
External Interface Files are homogeneous sets of data used by the application but generated and maintained by other applications.\\
Summary of the calculated weights:
\begin{equation*}
	\begin{aligned}
		&	FP_{ELF}
		& & = 2 \cdot w_{Simple,ELF}\\
		&&& = 2 \cdot 5\\
		&&& = 10\\
	\end{aligned}
\end{equation*}
\newpage{}
In details:
\begin{itemize}
\fp{Google Maps API}{Google Maps API related data about Travel Time and Addresses. Very frequent readings.}{1}{w_{Simple,ELF}}
\fp{Paypal API}{Paypal API request related to user's payement. Frequent insertion}{1}{w_{Simple,ELF}}
\end{itemize}
%
\section{External Input}
External Inputs are elementary operations to elaborate data coming from the external environment.\\
Summary of the calculated weights:
\begin{equation*}
	\begin{aligned}
		&	FP_{EI}
		& & = 5 \cdot w_{Simple,EI} + 2 \cdot w_{Medium,EI} + 2 \cdot w_{Complex,EI}\\
		&&& = 5 \cdot 3 + 2 \cdot 4 + 2 \cdot 6\\
		&&& = 35\\
	\end{aligned}
\end{equation*}
In details:
\begin{itemize}
	\fp{Login}{This operation requires a simple effort. In fact it has to perform few steps in order to conclude the procedure.}{1}{w_{Simple,EI}}
	\fp{Logout}{This operation requires a simple effort. In fact it has to perform few steps in order to conclude the procedure.}{1}{w_{Simple,EI}}
	\fp{Registration}{This operation requires a simple effort. In fact it has to perform few steps in order to conclude the procedure.}{1}{w_{Simple,EI}}
	\fp{Handle Personal Profile}{This operation requires a simple effort. In fact it has to perform few steps in order to conclude the procedure.}{1}{w_{Simple,EI}}
	\fp{Car Interaction}{This operation requires a medium effort. In fact it is very frequent.}{1}{w_{Medium,EI}}
	\fp{Car Reservation}{This operation requires a complex effort. In fact it has to perform many elementary steps in order to handle it}{1}{w_{Complex,EI}}
	\fp{Problem}{This operation requires a medium effort}{1}{w_{Medium,EI}}
  \fp{Rent}{This operation requires a simple effort.}{1}{w_{Simple,EI}}
  \fp{End of Rent}{This operation requires a complex effort. In fact it has to perform many elementary steps in order to handle it}{1}{w_{Complex,EI}}
\end{itemize}
%
\section{External Output}
External Outputs are elementary operations that generate data for the external environment, and they usually include the elaboration of data from logic files.\\
Summary of the calculated weights:
	\begin{equation*}
		\begin{aligned}
		&	FP_{EO}
		& & = 4 \cdot w_{Simple,EO}\\
		&&& = 4 \cdot 4\\
		&&& = 16\\
	\end{aligned}
\end{equation*}
In details:
\begin{itemize}
	\fp{User Notification}{The result of the end of the rent must be send to the user who handle it}{1}{w_{Simple,EO}}
	\fp{Email Confirmation}{The result of this operation must be sent to the user that registers}{1}{w_{Simple,EO}}
	\fp{Maintenance Notification}{The result of the maintenance call must be sent to the specific user.}{1}{w_{Simple,EO}}
	\fp{Reservation}{Informations about the reservation must be sent to the related user}{1}{w_{Simple,EO}}
\end{itemize}
%
\section{External Inquiry}
External Inquiries are elementary operations that involve input and output, without significant elaboration of data from logic files.\\
Summary of the calculated weights:
\begin{equation*}
	\begin{aligned}
		&	FP_{EIQ}
		& & = 2 \cdot w_{Medium,EIQ}\\
		&&& = 2 \cdot 4\\
		&&& = 8\\
	\end{aligned}
\end{equation*}
In details:
\begin{itemize}
	\fp{Manager car status}{In order to perform these operations the system has to retrieve, send and render simple data.}{1}{w_{Medium,EIQ}}
	\fp{Manager user status}{In order to perform these operations the system has to retrieve, send and render simple data.}{1}{w_{Medium,EIQ}}
\end{itemize}
%
\section{Summary}
All the calculated $FP_{i}$ sums up to $FP$, which is the total Function Points value:
\begin{equation*}
	\begin{aligned}
		&	FP
		& & = FP_{ILF} + FP_{ELF} + FP_{EI} + FP_{EO} + FP_{EIQ}\\
		&&& = 80 + 10 + 35 + 16 + 8\\
		&&& = 149\
	\end{aligned}
\end{equation*}
The total $FP$ value is then multiplied by a constant factor $k_{i,j}$ that depends on the programming language $i$ used to develop the software and the company gearing ratio $j$.\par
The gearing ratio is the level of a company's debt related to its equity capital, usually expressed in percentage form.
Gearing is a measure of a company's financial leverage and shows the extent to which its operations are funded by lenders versus shareholders.\par
This final calculation gives us the number of SLOC $n_{SLOC}$ estimated for \PowerEnJoy{}:
\begin{equation*}
	\begin{aligned}
		&   n_{SLOC}
		& & = FP \cdot k_{Java, Avg}\\
		&&& = 149 \cdot 53\\
		&&& = 7897 \text{ SLOC}
	\end{aligned}
\end{equation*}
%


    \filetitle{filter}{Re-estimate the factors by Kalman filtering the data taking FAVAR coefficients as given}{FAVAR/filter}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
[A,D,CC,F,U,E] = filter(A,D,Range,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{A} {[} FAVAR {]} - Estimated FAVAR object.
\item
  \texttt{D} {[} struct \textbar{} tseries {]} - Input database or
  tseries object with the FAVAR observables.
\item
  \texttt{Range} {[} numeric {]} - Filter date range.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{A} {[} FAVAR {]} - FAVAR object.
\item
  \texttt{D} {[} struct {]} - Output database or tseries object with the
  FAVAR observables.
\item
  \texttt{CC} {[} struct \textbar{} tseries {]} - Re-estimated common
  components in the observables.
\item
  \texttt{F} {[} tseries {]} - Re-estimated common factors.
\item
  \texttt{U} {[} tseries {]} - Re-estimated idiosyncratic residuals.
\item
  \texttt{E} {[} tseries {]} - Re-estimated structural residuals.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\item
  \texttt{'cross='} {[} \emph{\texttt{true}} \textbar{} \texttt{false}
  \textbar{} numeric {]} - Run the filter with the off-diagonal elements
  in the covariance matrix of idiosyncratic residuals; if false all
  cross-covariances are reset to zero; if a number between zero and one,
  all cross-covariances are multiplied by that number.
\item
  \texttt{'invFunc='} {[} \emph{\texttt{'auto'}} \textbar{}
  function\_handle {]} - Inversion method for the FMSE matrices.
\item
  \texttt{'meanOnly='} {[} \texttt{true} \textbar{}
  \emph{\texttt{false}} {]} - Return only mean data, i.e.~point
  estimates.
\item
  \texttt{'persist='} {[} \texttt{true} \textbar{} \emph{\texttt{false}}
  {]} - If \texttt{filter} or \texttt{forecast} is used with
  \texttt{'persist='} set to true for the first time, the forecast MSE
  matrices and their inverses will be stored; subsequent calls of the
  \texttt{filter} or \texttt{forecast} functions will re-use these
  matrices until \texttt{filter} or \texttt{forecast} is called.
\item
  \texttt{'output='} {[} \emph{\texttt{'auto'}} \textbar{}
  \texttt{'dbase'} \textbar{} \texttt{'tseries'} {]} - Format of output
  data.
\item
  \texttt{'tolerance='} {[} numeric \textbar{} \emph{\texttt{0}} {]} -
  Numerical tolerance under which two FMSE matrices computed in two
  consecutive periods will be treated as equal and their inversions will
  be re-used, not re-computed.
\end{itemize}

\paragraph{Description}\label{description}

It is the user's responsibility to make sure that \texttt{filter} and
\texttt{forecast} called with \texttt{'persist='} set to true are valid,
i.e.~that the previously computed FMSE matrices can be really re-used in
the current run.

\paragraph{Example}\label{example}


\author{Nicholas A. Lines}
\date{2022-02-08}
\title{Tables to accompany CHANCE article submission}
\documentclass{article}
\usepackage[a4paper,margin=1in,landscape]{geometry}
\usepackage{tabularx}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{hyperref}
\hypersetup{
 pdfauthor={Nicholas Lines, Data Science Research},
 pdftitle={Tables},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 25.3.1 (Org mode 9.3.7)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Tables}

\setcounter{table}{1}

\begin{longtable}{rrrrrrrr}
\caption{Correlation of seven popular readability formulae across the Blog Authorship Corpus.}
\label{Table 2}\\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Lensear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endfirsthead
\caption[]{Correlation of seven popular readability formulae across the Blog Authorship Corpus.} \\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Lensear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
Dale-Chall     &        1.00 &  0.93 &          0.13 &    0.95 &           0.93 &   -0.94 &            0.93 \\
ARI            &        0.93 &  1.00 &          0.11 &    1.00 &           1.00 &   -0.99 &            1.00 \\
Coleman-Liau   &        0.13 &  0.11 &          1.00 &    0.09 &           0.09 &   -0.20 &            0.11 \\
Spache         &        0.95 &  1.00 &          0.09 &    1.00 &           0.99 &   -0.99 &            1.00 \\
Lensear Write  &        0.93 &  1.00 &          0.09 &    0.99 &           1.00 &   -0.99 &            1.00 \\
Flesch        &       -0.94 & -0.99 &         -0.20 &   -0.99 &          -0.99 &    1.00 &           -0.99 \\
Flesch-Kincaid &        0.93 &  1.00 &          0.11 &    1.00 &           1.00 &   -0.99 &            1.00 \\
\end{longtable}

\pagebreak

\begin{longtable}{rrrrrrrrr}
\caption{An example of grading inconsistencies between popular readability formulae. Three sample texts are provided, beginning with the All the World's a Stage monologue uttered by Jacques in As You Like It, Act II Scene VII. The second and third texts are the encyclopedia entries on William Shakespeare taken from the Simplified English and standard Wikipedia, respectively. The grades shown are the maximum grade predicted for each text by each formula. Grade 13 is considered college-level, and Grade 17 graduate level, etc.}
\label{Table 3}\\
\toprule
  Text Sample &  Dale-Chall &  ARI &  Coleman-Liau &  Spache &  Linsear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endfirsthead
\caption[]{An example of grading inconsistencies between popular readability formulae. Three sample texts are provided, beginning with the All the World's a Stage monologue uttered by Jacques in As You Like It, Act II Scene VII. The second and third texts are the encyclopedia entries on William Shakespeare taken from the Simplified English and standard Wikipedia, respectively. The grades shown are the maximum grade predicted for each text by each formula. Grade 13 is considered college-level, and Grade 17 graduate level, etc.} \\
\toprule
  Text Sample &  Dale-Chall &  ARI &  Coleman-Liau &  Spache &  Linsear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
  Shakespeare &          12 &   14 &            10 &       8 &             17 &       9 &              12 \\
  Wiki Simple &          13 &   11 &            11 &       7 &             11 &      12 &               9 \\
Wiki Standard &          14 &   14 &            13 &       8 &             17 &      13 &              13 \\
\end{longtable}

\pagebreak

\begin{longtable}{rrrrrrrr}
\caption{Correlation of author age and sex (Female=0, Male=1) with readability formulae values in the Blog Authorship Corpus. The Sex row represents point-biserial correlation, and the Age row represents Pearson correlation.}
\label{Table 4}\\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Lensear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endfirsthead
\caption[]{Correlation of author age and sex (Female=0, Male=1) with readability formulae values in the Blog Authorship Corpus. The Sex row represents point-biserial correlation, and the Age row  represents Pearson correlation.} \\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Linsear Write &  Flesch&  Flesch-Kincaid \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
Sex &  0.03 &  0.01 &  0.21 &  0.01 &  0.01 & -0.03 &  0.01 \\
Age & -0.08 & -0.04 &  0.26 & -0.06 & -0.05 & 0.02  &  -0.04 \\
\end{longtable}

\pagebreak

\begin{longtable}{lrrrrrrr}
\caption{Point-biserial correlation of sex (Female=0, Male=1) to blog readability scores for three self-declared vocation groups in the BAC. Rather than explaining away the relationship between sex and readability, grouping bloggers by vocation shows some professions exhibit more extreme sex bias in readability than others. Similar results are found for Pearson correlation of age to readability in these groups.}
\label{Table 5}\\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Lensear Write &  Flesch &  Flesch-Kincaid \\
\midrule
\endfirsthead
\caption[]{Point-biserial correlation of sex (Female=0, Male=1) to blog readability scores for three self-declared vocation groups in the BAC. Rather than explaining away the relationship between sex and readability, grouping bloggers by vocation shows some professions exhibit more extreme sex bias in readability than others. Similar results are found for Pearson correlation of age to readability in these groups.} \\
\toprule
{} &  Dale-Chall &   ARI &  Coleman-Liau &  Spache &  Lensear Write &  Flesch &  Flesch-Kincaid \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
Accounting         &        0.03 & -0.01 &          0.40 &   -0.01 &          -0.01 &   -0.04 &           -0.00 \\
Investment Banking &        0.17 & -0.07 &          0.64 &   -0.05 &          -0.10 &   -0.08 &           -0.07 \\
Maritime           &        0.33 &  0.31 &          0.52 &    0.30 &           0.25 &   -0.41 &            0.31 \\
\end{longtable}

\end{document}


\subsection{Convenient}
This section decribes all the functions in the file ``ptwXY\_convenient.c''.

\subsubsection{ptwXY\_getXArray}
This function returns, as an \highlight{ptwXPoints}, the list of x values in \highlight{ptwXY} instance. The returned object is allocated
by \highlight{ptwXY\_getXArray} and must be freed by the user.
\setargumentNameLengths{ptwXY}
\CallingC{ptwXPoints *ptwXY\_getXArray(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
Returns NULL if an error occured.

\subsubsection{ptwXY\_ysMappedToXs}
This function returns the y-values of \highlight{ptwXY} at the points defined by the values of \highlight{Xs}.
\setargumentNameLengths{offset}
\CallingC{ptwXPoints *ptwXY\_ysMappedToXs(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{ptwXPoints *Xs,}
    \addArgument{int64\_t *offset );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{Xs}{The list of x-values to evaluate \highlight{ptwXY} at.}
    \argumentBox{offset}{The index in \highlight{ptwXY} of the first point greater than or equal to the first point of \highlight{Xs}.}
Returns NULL if an error occured.

\subsubsection{ptwXY\_dullEdges}
This function insures that the y-values at the end-points of  \highlight{ptwXY} are 0. This can be usefull for making
sure two \highlight{ptwXYPoints} instances have mutual domains.
\setargumentNameLengths{positiveXOnly}
\CallingC{nfu\_status ptwXY\_dullEdges(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{double lowerEps,}
    \addArgument{double upperEps,}
    \addArgument{int positiveXOnly );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{lowerEps}{The amount to adjust the first points.}
    \argumentBox{upperEps}{The amount to adjust the last points.}
    \argumentBox{positiveXOnly}{The next point in the linked list.}
The description here will mainly focuses on the dulling of the low point of ptwXY, the upper point's dulling is similar.
Let $\epsilon_l =$ lowerEps, $x_0$ and $y_0$ be the first point of ptwXY and $x_1$ and $y_1$ be the second point of ptwXY.
Also, if $x_0 \ne 0$ then let $\Delta x = |\epsilon_l| x_0$ otherwise let $\Delta x = |\epsilon_l|$.
Then, the points around $x_0$ are modified only if lowerEps $\ne 0$ and $y_0 \ne 0$.
The dulling of the lower edge can have one of the four outcomes listed here,
\begin{eqnarray}
                        & x_0, 0    & \hskip .5 in x_p, y_p            \hskip .5 in x_1, y_1 \hskip .5 in {\rm outcome \ 1} \nonumber \\
                        & x_0, 0    & \hskip .5 in \hphantom{x_p, y_p} \hskip .5 in x_1, y_1 \hskip .5 in {\rm outcome \ 2} \nonumber \\
    x_m, 0 \hskip .5 in & x_0, y'_0 & \hskip .5 in x_p, y_p            \hskip .5 in x_1, y_1 \hskip .5 in {\rm outcome \ 3} \nonumber \\
    x_m, 0 \hskip .5 in & x_0, y'_0 & \hskip .5 in \hphantom{x_p, y_p} \hskip .5 in x_1, y_1 \hskip .5 in {\rm outcome \ 4} \nonumber
\end{eqnarray}
In all outcomes, the lower point now has $y = 0$.
The point is added at $x_p = x_0 + \Delta x$ with $y = f(x_p)$ only if $x_0 + 2 \Delta x < x_2$.
If the point at $x_m = x_0 - \Delta x$ is not added, then $y_0$ is set to 0 as shown in outcomes 1 and 2.
The point $x_m$ is not added if $\epsilon_l > 0$, or positiveXOnly is true and $x_m < 0$ and $x_0 \ge 0$.

The dulling of the upper edge can have one of the four outcomes listed here,
\begin{eqnarray}
    x_{k-1}, y_{k-1} \hskip .5 in x_m, y_m \hskip .5 in            & x_k, 0 & \hskip .5 in  \hphantom{x_p, 0} \hskip .5 in {\rm outcome \ 1} \nonumber \\
    x_{k-1}, y_{k-1} \hskip .5 in \hphantom{x_m, y_m} \hskip .5 in & x_k, 0 & \hskip .5 in  \hphantom{x_p, 0} \hskip .5 in {\rm outcome \ 2} \nonumber \\
    x_{k-1}, y_{k-1} \hskip .5 in x_m, y_m \hskip .5 in            & x_k, y_k & \hskip .5 in x_p, 0 \hskip .5 in {\rm outcome \ 3} \nonumber \\
    x_{k-1}, y_{k-1} \hskip .5 in \hphantom{x_m, y_m} \hskip .5 in & x_k, y_k & \hskip .5 in x_p, 0 \hskip .5 in {\rm outcome \ 4} \nonumber
\end{eqnarray}
where $k$ is the index of the last point.

\subsubsection{ptwXY\_mergeClosePoints}
Removes and/or moves points so that no two consecutive points are too close to others.
\setargumentNameLengths{epsilon}
\CallingC{fnu\_status ptwXY\_mergeClosePoints(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{double epsilon);}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{epsilon}{The minimum relative spacing desired.}
Points are removed and/or moved so the $x_{i+1} - x_i \le \highlight{epsilon} \times ( x_i + x_{i+1} ) / 2$.

\subsubsection{ptwXY\_intersectionWith\_ptwX}
This function returns an \highlight{ptwXYPoints} instance whose x-values are the intersection of \highlight{ptwXY}'s and
\highlight{ptwX}'s x-values. The domains of \highlight{ptwXY} and \highlight{ptwX} do not have to be mutual.
\setargumentNameLengths{ptwXY}
\CallingC{ptwXY\_intersectionWith\_ptwX(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{ptwXPoints *ptwX );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{ptwX}{A pointer to the \highlight{ptwXPoints} object.}

\subsubsection{ptwXY\_areDomainsMutual}
This function returns \highlight{nfu\_Okay} if \highlight{ptwXY1} and \highlight{ptwXY2} are mutual.
\CallingC{fnu\_status ptwXY\_areDomainsMutual(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY1,}
    \addArgument{ptwXYPoints *ptwXY2 );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY1}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{ptwXY2}{A pointer to a \highlight{ptwXYPoints} object.}
If one or both of \highlight{ptwXY1} and \highlight{ptwXY2} are empty, \highlight{nfu\_empty} is returned.
If one or both of \highlight{ptwXY1} and \highlight{ptwXY2} has only one point, \highlight{nfu\_tooFewPoints} is returned.
If the domains are not mutual, \highlight{nfu\_domainsNotMutual} is returned.

\subsubsection{ptwXY\_tweakDomainsToMutualify}
If a small tweak of one or more end points will make the domains of \highlight{ptwXY1} and \highlight{ptwXY2} mutual,
then the tweak is made. A small tweak is defined as
\begin{equation}
    | x2 - x1 | \le {\rm Epsilon} ( \ | x1 | + | x2 | \ )
\end{equation}
where $x1$ is one endpoint of \highlight{ptwXY1}, $x2$ is the corresponding endpoint of \highlight{ptwXY2} and
\begin{equation}
    {\rm Epsilon} = {\rm fabs}( \ {\rm epsilon} \ ) + {\rm fabs}( \ {\rm epsilonFactor} * {\rm DBL\_EPSILON} \ )
\end{equation}
\setargumentNameLengths{positiveXOnly1}
\CallingC{fnu\_status ptwXY\_tweakDomainsToMutualify(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY1,}
    \addArgument{ptwXYPoints *ptwXY2,}
    \addArgument{int epsilonFactor,}
    \addArgument{double epsilon );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY1}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{ptwXY2}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{epsilonFactor}{Relative comparision value.}
    \argumentBox{epsilon}{Relative comparision value in units of DBL\_EPSILON.}

\subsubsection{ptwXY\_mutualifyDomains}
If possible and needed, this function mutualifies the domains of \highlight{ptwXY1} and \highlight{ptwXY2} by calling
\highlight{ptwXY\_dullEdges} on one or both of \highlight{ptwXY1} and \highlight{ptwXY2} as needed.
\setargumentNameLengths{positiveXOnly1}
\CallingC{fnu\_status ptwXY\_mutualifyDomains(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY1,}
    \addArgument{double lowerEps1,}
    \addArgument{double upperEps1,}
    \addArgument{int positiveXOnly1,}
    \addArgument{ptwXYPoints *ptwXY2,}
    \addArgument{double lowerEps2,}
    \addArgument{double upperEps2,}
    \addArgument{int positiveXOnly2 );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY1}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{lowerEps1}{If needed the value of \highlight{lowerEps} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY1}.}
    \argumentBox{upperEps1}{If needed the value of \highlight{upperEps} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY1}.}
    \argumentBox{positiveXOnly1}{The value of \highlight{positiveXOnly} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY1}.}
    \argumentBox{ptwXY2}{A pointer to a \highlight{ptwXYPoints} object.}
    \argumentBox{lowerEps2}{If needed the value of \highlight{lowerEps} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY2}.}
    \argumentBox{upperEps2}{If needed the value of \highlight{upperEps} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY2}.}
    \argumentBox{positiveXOnly2}{The value of \highlight{positiveXOnly} passed to \highlight{ptwXY\_dullEdges} when dulling \highlight{ptwXY2}.}

\subsubsection{ptwXY\_copyToC\_XY}
This function copies the points from index \highlight{index1} inclusive to \highlight{index2} exclusive of \highlight{ptwXY}
into the address pointed to by \highlight{xys}.
\setargumentNameLengths{allocatedSize}
\CallingC{fnu\_status ptwXY\_copyToC\_XY(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{int64\_t index1,}
    \addArgument{int64\_t index2,}
    \addArgument{int64\_t allocatedSize,}
    \addArgument{int64\_t numberOfPoints,}
    \addArgument{double *xys );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{index1}{The lower index.}
    \argumentBox{index2}{The upper index.}
    \argumentBox{allocatedSize}{The size of the space allocated for xys in pairs of C-double.}
    \argumentBox{numberOfPoints}{The number of (x,y) points filled into *xys.}
    \argumentBox{xys}{A pointer to the space to write the data.}
    \vskip 0.05 in \noindent
The size of \highlight{xys} must be at least 2 $\times$ sizeof( double ) $\times$ \highlight{allocatedSize} bytes.
The values of \highlight{index1} and \highlight{index2} are ajusted as follows. If \highlight{index1} is less than 0, it is set to 0. Then
if \highlight{index2} is less than \highlight{index1}, it is set to \highlight{index1}. Finally,
if \highlight{index2} is greater than the length of \highlight{ptwXY}, it is set to the length of \highlight{ptwXY}.
If \highlight{allocatedSize} is less than the number of points to be copied (i.e., \highlight{index2} - \highlight{index1}
after \highlight{index1} and \highlight{index2} are adjusted) then \highlight{nfu\_insufficientMemory} is returned;

\subsubsection{ptwXY\_valuesToC\_XsAndYs}
This function copies the points of \highlight{ptwXY} to \highlight{xs} and \highlight{ys}. Memory for \highlight{xs} and \highlight{ys}
are allocated and the callers is responsible for free-ing their memory.
\setargumentNameLengths{ptwXY}
\CallingC{fnu\_status ptwXY\_valuesToC\_XsAndYs(}{statusMessageReporting *smr,
    \addArgument{ptwXYPoints *ptwXY,}
    \addArgument{double **xs,}
    \addArgument{double **ys );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{ptwXY}{A pointer to the \highlight{ptwXYPoints} object.}
    \argumentBox{xs}{The x-values of \highlight{ptwXY}}
    \argumentBox{ys}{The y-values of \highlight{ptwXY}}

\setargumentNameLengths{interpolation}
\subsubsection{ptwXY\_valueTo\_ptwXY}
This function creates a \highlight{ptwXYPoints} instance with the two points (x1,y), (x2,y) where x1 $<$ x2.
\CallingC{ptwXYPoints *ptwXY\_valueTo\_ptwXY(}{statusMessageReporting *smr,
    \addArgument{double x1,}
    \addArgument{double x2,}
    \addArgument{double y );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{x1}{x value for the lower point.}
    \argumentBox{x2}{x value for the upper point.}
    \argumentBox{y}{y value for both points.}
    \vskip 0.05 in \noindent
If an error occurs, NULL is returned.

\subsubsection{ptwXY\_createGaussianCenteredSigma1}
This function returns a \highlight{ptwXYPoints} instance of the simple Gaussian $y(x) = \exp( -x^2 / 2 )$.
\setargumentNameLengths{status}
\CallingC{ptwXYPoints *ptwXY\_createGaussianCenteredSigma1(}{statusMessageReporting *smr,
    \addArgument{double accuracy );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{accuracy}{The returned points are accurate to accuracy.}
    \vskip 0.05 in \noindent
The domain ranges from $-\sqrt{ 2 \log( {\rm yMin} )}$ to $\sqrt{ 2 \log( {\rm yMin} )}$ where yMin = $10^{-10}$.

\subsubsection{ptwXY\_createGaussian}
This function returns a \highlight{ptwXYPoints} instance of the Gaussian $y(x) = a \exp( -( x - c )^2 / ( 2 s ) )$.
In the equation $a = $ \highlight{amplitude}, $c = $ \highlight{xCenter} and $s = $ \highlight{sigma}. This function calls
\highlight{ptwXY\_create\-GaussianCenteredSigma1} and then scales the x and y values.
\setargumentNameLengths{amplitude}
\CallingC{ptwXYPoints *ptwXY\_createGaussian(}{statusMessageReporting *smr,
    \addArgument{double accuracy,}
    \addArgument{double xCenter,}
    \addArgument{double sigma,}
    \addArgument{double amplitude,}
    \addArgument{double domainMin,}
    \addArgument{double domainMax,}
    \addArgument{double dullEps );}}
    \argumentBox{smr}{The \highlight{statusMessageReporting} instance to record errors.}
    \argumentBox{accuracy}{The returned points are accurate to accuracy.}
    \argumentBox{xCenter}{The center of the Gaussian.}
    \argumentBox{sigma}{The width of the Gaussian.}
    \argumentBox{amplitude}{The amplitude of the Gaussian.}
    \argumentBox{domainMin}{The lower domain of the returned Gaussian.}
    \argumentBox{domainMax}{The upper lower domain of the returned Gaussian.}
    \argumentBox{dullEps}{Currently not implemented.}
    \vskip 0.05 in \noindent


\documentclass{article}
\usepackage[a4paper, total={6in, 8in}]{geometry}

\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{mathtools, nccmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{graphicx}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
  showstringspaces=false,
  commentstyle=\color{red},
  keywordstyle=\color{blue}
}
\title{CFD Lab Group E Project Documentation}
\date{July 2021}
\author{
  Yunus Can Cakir\\
  \text{Technical University of Munich}
  \and
  Markus Mühlhäußer\\
  \text{Technical University of Munich}
  \and
   Sheng Chia-Yu\\
  \text{Technical University of Munich}
}
\begin{document}
\maketitle

\section{Introduction}
In this CFD project, our main goal was to implement and investigate different turbulent models. Aside from this, we wanted to explore different solvers, parallelization of our code on the GPU with different APIs, preconditioning, adaptive grid refinement and more. Our CFD solver comprises of 3 types of simulations: a CPU, a Vulkan and a CUDA simulation. It also supports MPI on the CPU path. During the course of our project we have implemented K-epsilon, K-omega and K-omega SST turbulent models.

\section{Code}
Our code diverges from the given skeleton in the sense that we have a single Solver class and depending on the simulation type we have CPUSolver, VulkanSolver or CudaSolver classes. In the Utilities.hpp file, you can change the precision between double and float under the "Real" type. Inside the Simulation.cpp file, the solver loop runs. The solver loop is divided into 3 sub methods that is implemented by each subclass. These are namely,
\begin{lstlisting}[language=C++,frame=single]
void initialize()
\end{lstlisting}
This method handles all the initialization specific to chosen path. In Vulkan, this method handles preconditioner calculation, buffer allocation, descriptor binding etc. In CUDA, similarly this method handles preconditioner calculation and buffer allocation.
\begin{lstlisting}[language=C++,frame=single]
void solve_pre_pressure(Real &dt)
\end{lstlisting}
That handles the computations up to the pressure solving step. These include dt calculation, F and G calculation, setting up boundary values and other GPU or CPU specific tasks.
\begin{lstlisting}[language=C++,frame=single]
void solve_pressure(Real &res, uint32_t &it)
\end{lstlisting}
This method handles the pressure solving step. Depending on the configuration, either an SOR solver or a PCG solver is employed on the GPU or CPU.
\begin{lstlisting}[language=C++,frame=single]
void solve_post_pressure()
\end{lstlisting}
This method handles velocity calculation, turbulent viscosity, k/$\epsilon$ or $\omega$ calculation and copying the field values to the CPU for VTK output, if the GPU path is selected.

\subsection{Running the code}
The code has been tested on both Linux and Windows. For Linux you can build the code via CMake.
\begin{lstlisting}[language=bash,frame=single]
mkdir build
cd build
cmake ..
make
\end{lstlisting}
Make sure you enable CUDA or Vulkan support from the CMake settings.
\begin{itemize}
    \item Vulkan : cmake .. -DUSE\_VULKAN=ON
    \item CUDA : cmake .. -DUSE\_CUDA=ON
\end{itemize}
Make sure you delete you CMake cache if you change these settings midway through the build. \newline
Also note that the Vulkan code is only tested and designed on a RTX 3060 GPU, therefore there may be problems on lower-end hardware.
\subsection{Scene configuration}
In addition to the usual options from the previous exercises, we have other options.
\begin{itemize}
    \item model
    \begin{itemize}
        \item 0 $\rightarrow$ Turbulence off(default)
        \item 1 $\rightarrow$  K-epsilon model
        \item 2 $\rightarrow$  K-omega model
        \item 3 $\rightarrow$  K-omega SST model
    \end{itemize}
    \item solver
    \begin{itemize}
        \item 0 $\rightarrow$ SOR(default)
        \item 1 $\rightarrow$ PCG
    \end{itemize}
    \item simulation
    \begin{itemize}
        \item 0 $\rightarrow$ CPU(default)
        \item 1 $\rightarrow$ Cuda
        \item 2 $\rightarrow$ Vulkan
    \end{itemize}
    \item preconditioner
    \begin{itemize}
        \item -1 $\rightarrow$ off(default)
        \item 0 $\rightarrow$ AINV
        \item 1 $\rightarrow$ SSOR
        \item 2 $\rightarrow$ Jacobi preconditioner
    \end{itemize}
    \item refine
    \begin{itemize}
        \item Number (Grid has the refinement level $2^N$
    \end{itemize}
\end{itemize}

Note that following combinations are not supported:
\begin{itemize}
    \item PCG - MPI
    \item Cuda - MPI
    \item Vulkan - MPI
\end{itemize}

\section{Turbulent Models}
In our code we are using RANS-based turbulence modelds. Specifically we are using eddy viscosity models that models the turbulence via a turbulent viscosity term.

\subsection{K-epsilon Model}
In this model, we model the turbulent viscosity as
\begin{equation}
\mu_{t}=\rho C_{\mu} \frac{k^{2}}{\epsilon}
\end{equation}
Where k is the kinetic energy and $\epsilon$ is the turbulent dissipation rate. \newline
We calculate these quantities via the equations:
\begin{equation}
\frac{\partial(\rho k)}{\partial t}+\frac{\partial\left(\rho k u_{i}\right)}{\partial x_{i}}=\frac{\partial}{\partial x_{j}}\left[\frac{\mu_{t}}{\sigma_{k}} \frac{\partial k}{\partial x_{j}}\right]+2 \mu_{t} E_{i j} E_{i j}-\rho \varepsilon
\end{equation}
\begin{equation}
\frac{\partial(\rho \varepsilon)}{\partial t}+\frac{\partial\left(\rho \varepsilon u_{i}\right)}{\partial x_{i}}=\frac{\partial}{\partial x_{j}}\left[\frac{\mu_{t}}{\sigma_{\varepsilon}} \frac{\partial \varepsilon}{\partial x_{j}}\right]+C_{1 \varepsilon} \frac{\varepsilon}{k} 2 \mu_{t} E_{i j} E_{i j}-C_{2 \varepsilon} \rho \frac{\varepsilon^{2}}{k}
\end{equation}

Although the K-epsilon model is fairly accurate away from the wall, it is problematic near the wall due to high shear stress, which results in higher $\mu$ along the walls.

\subsection{K-omega model}
Just like the K-epsilon model, the K-omega model is also a two equation model. Here, we reformulate the $\omega$ such that:
\begin{equation}
\omega=\frac{\epsilon}{C_{\mu} k} , C_{\mu} = 0.09
\end{equation}

Then we transport the quantities K and $\omega$ as usual.

Compared to K-epsilon model, the K-omega model has much better behaviour near walls, however one still wants the accuracy the K-epsilon model shows away from the walls. Which brings us to another widely used turbulence model.

\subsection{K-omega SST model}

In this model, we "blend" between K-omega and K-epsilon via a constant and limit the turbulent viscosity in the process. In the process, the only difference with the K-omega model is the limitation of the viscosity and the extra term in the $\omega$ transport. Namely,
\begin{equation}
\nu_{T}=\frac{a_{1} k}{\max \left(a_{1} \omega, S F_{2}\right)}
\end{equation}

\begin{equation}
\frac{\partial \omega}{\partial t}+U_{j} \frac{\partial \omega}{\partial x_{j}}=\alpha S^{2}-\beta \omega^{2}+\frac{\partial}{\partial x_{j}}\left[\left(\nu+\sigma_{\omega} \nu_{T}\right) \frac{\partial \omega}{\partial x_{j}}\right]+2\left(1-F_{1}\right) \sigma_{\omega 2} \frac{1}{\omega} \frac{\partial k}{\partial x_{i}} \frac{\partial \omega}{\partial x_{i}}
\end{equation}

Via constants $F_1$ and $F_2$. These are empirically derived constants fitted to a tanh function.

In our observations, we found that SST model is in between the K-epsilon and K-omega model, as suggested by the literature.

\begin{figure}
\centering
  \centering
  \includegraphics[width=.6\linewidth]{komega.png}
  \caption{K-omega model on Karman Vortex Street}
  \label{fig:sub1}
  \centering
  \includegraphics[width=.6\linewidth]{sst.png}
  \caption{K-omega SST model on Karman Vortex Street}
  \label{fig:sub2}
\label{fig:test}
\end{figure}

\section{Pressure Solvers and Preconditioners}
Aside from the usual SOR solver, we have added a PCG solver for the CPU, Vulkan and the CUDA path. For reference in the CPU path, we've used a modified version of Robert Bridson's PCG code which also provides a Modified Incomplete Cholesky preconditioner. In the GPU paths, we've implemented our own PCG solver and experimented with different preconditioners. For the preconditioners in the GPU branches, we wanted SPAI preconditioners for fast evaluation on the GPU, otherwise we'd have to solve 2 triangular systems which is a costly operation.

\subsection{Jacobi Preconditioner}
This is the simplest type of preconditioner. Since in our case the Laplacian matrix has non-zero diagonals, we simply use $P_{ij} = \frac{1}{D_{ij}}$ where D is the diagonal part of the pressure matrix.

\subsection{SSOR Preconditioner}

This preconditioner \cite{helfenstein} is defined by
\begin{equation}
M=K K^{T}
\end{equation}
where
\begin{equation}
K=\frac{1}{\sqrt{2-\omega}}(\bar{D}+L) \bar{D}^{-1 / 2}
\end{equation}
in which $0 <  \omega < 2$ and $\bar{D} = \frac{1}{\omega}D$
In the code, we precompute this matrix from the pressure matrix, then in the CG algorithm, the only extra cost is a sparse matrix vector multiplication.

\subsection{Approximate Inverse Preconditioner Variant}
This preconditioner \cite{labutin} approximates the inverse by first choosing an initial preconditioner, in which case we've chosen a Jacobi preconditioner, then approximating
the inverse via Neumann series. Assuming we have $D_0$ as an initial approximation of $A^{-1}$ and a condition

\begin{equation}
\left\|R_{0}\right\| \leq k<1, \quad R_{0}=I-A D_{0}
\end{equation}
Then,
\begin{equation}
\begin{aligned}
A^{-1} &=\left(D_{0} D_{0}^{-1}\right) A^{-1}=D_{0}\left(D_{0}^{-1} A^{-1}\right) \\
&=D_{0}\left(A D_{0}\right)^{-1}=D_{0}\left(I-R_{0}\right)^{-1} \\
&=D_{0}\left(I+R_{0}+R_{0}^{2}+R_{0}^{3}+\ldots\right)
\end{aligned}
\end{equation}

where $R_n = I - AD_n$

This way, we can selectively approximate a finer preconditioner. We have stopped at $D_1$. As an extra measure, we also interpolate it via a naive Jacobi preconditioner.

Please note that since we are merely approximating a sparse approximate inverse, it isn't guaranteed to exist and more often than not, we need to choose different preconditioners depending on the scene. On some scenes, like the "Fluid Trap", none of the preconditioners work.

\section{GPU Solvers}

\subsection{Vulkan}

For our Vulkan solver, we wanted to focus on performance. To maximize the performance we fully solve the simulation on the GPU. Our code supports both double precision and single precision operations. However, for GPU execution the single precision path is recommended. To reduce the CPU overhead, we record all the command buffers beforehand, then we only submit those command buffers during the simulation. To avoid divergent code while handling boundary conditions, we instead encode all the BCs into a sparse matrix then do a sparse matrix multiplication.

For the sparse matrix representation, we use a CSR(Compressed Sparse Row) format. A pseudocode below describes a basic SpMV calculation.
\begin{algorithm}
    \caption{An algorithm for sparse matrix vector calculation}
    \begin{algorithmic}
        \If {row $<$ size}
            \State $s\gets 0$
            \For{j from $rowstart[row]$ to $rowstart[row+1]$}
                \State $s\gets s + umatrix[j] * u[colidx[j]]$
            \EndFor
        \EndIf
    \end{algorithmic}
\end{algorithm}

During the PCG algorithm, we also do a sparse matrix vector multiplication. There, we use a diagonal storage format(DIA) which is better suited for compute intensive GPU code.
\subsection{CUDA}

CUDA calculations are similar to Vulkan, except some parts of the code, like the calculation of $dt$ happens on the CPU.

\section{Grid Refinement}
Creation of detailed pgm files can be tedious, but especially when using turbulence models a finer grid can be beneficial. Therefore a refine parameter can be set to computationally create a finer grid in the powers of 2.
To accurately compute the k-epsilon model near solid walls, a finer mesh size is needed. To save computation power a method for creating an adaptive grid was developed, but is yet unfinished  and therefore on a seperate project branch.

\begin{thebibliography}{6}
\bibitem{helfenstein}
R. Helfenstein, J. Koko. 2012. Parallel preconditioned conjugate gradient algorithm on GPU.
\bibitem{labutin}
I. Labutin, I. Surodina. 1996. Algorithm for Sparse Approximate Inverse Preconditioners in the Conjugate Gradient Method.

\end{thebibliography}

\end{document}


\documentclass[11pt]{article}

\usepackage{fullpage}
\usepackage{rotating}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{fancyhdr}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{bm}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{caption2}
\usepackage{subfigure}
\usepackage{float}
\usepackage{extpfeil}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{remark}[subsection]{Remark}

%%
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}

%% define new symbols
\def\bx{\bm{x}}
\def\bb{\bm{b}}
\def\ba{\bm{a}}
\def\bc{\bm{c}}
\def\bf{\bm{f}}
\def\by{\bm{y}}
\def\bu{\bm{u}}
\def\bv{\bm{v}}
\def\BW{\bm{W}}
\def\BA{\bm{A}}
\def\bz{\bm{z}}
\def\BZ{\bm{Z}}
\def\BH{\bm{H}}
\def\BL{\bm{L}}
\def\BU{\bm{U}}
\def\BV{\bm{V}}
\def\BB{\bm{B}}
\def\BC{\bm{C}}
\def\BD{\bm{D}}
\def\BE{\bm{E}}
\def\BW{\bm{W}}
\def\BQ{\bm{Q}}
\def\BG{\bm{G}}
\def\BA{\bm{A}}
\def\BX{\bm{X}}
\def\BY{\bm{Y}}
\def\BQ{\bm{Q}}
\def\BI{\bm{I}}
\def\BR{\bm{R}}

%% define new brackets
\def\la{\left\langle}
\def\ra{\right\rangle}
\def\ln{\left\|}
\def\rn{\right\|}
\def\lb{\left(}
\def\rb{\right)}
\def\lsb{\left[}
\def\rsb{\right]}
\def\lcb{\left\{}
\def\rcb{\right\}}

%%
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}

%%
\title{Homework II}
\author{Name: Shao Yanjun, Number: 19307110036}

\begin{document}
\maketitle

%------------------------------------
\begin{abstract}
This is Daniel's homework of  "Statistical Learning".
\end{abstract}
%-------------------------------------
%=====================

\paragraph{(1)}The OLS is $\hat{\beta}=\left(\mathrm{X}^{\prime} \mathrm{X}\right)^{-1} \mathrm{X}^{\prime} \mathrm{Y}$. And the likelihood function of $\beta$ is,

\begin{align*}
	L(\beta, \sigma)=\frac{1}{(2 \pi \sigma)^{n/2}} \exp \left(-\frac{1}{2 \sigma^{2}} \varepsilon^{\top} \varepsilon\right)
\end{align*}

Since $-(\mathrm{Y}-\mathrm{X} \beta)(\mathrm{Y}-\mathrm{X} \beta) \leqslant 0, \quad \forall \mathrm{Y}-\mathrm{X} \beta \in \mathbb{R}^{n}$,
the exponential term will be minimized by $\mathrm{Y}=\mathrm{X} \beta$.

Therefore, MLE of $\beta$ can also be $\hat{\beta}=(\mathrm{X}^\prime \mathrm{X})^{-1} \mathrm{X}^{\prime} \mathrm{Y}$

\paragraph{(2)} Let any linear combination of $\beta$ be $c^{\prime} \beta=c_{0} \beta_{0}+c_{1} \beta_{1}+\cdots+\operatorname{c_n} \beta_{n}$. For OLS estimator. $c^{\prime} \hat{\beta}=c^{\prime}(\mathrm{Z}^\prime \mathrm{Z})^{-1} \mathrm{Z}^{\prime} \mathrm{Y}$. Mark $a=\mathrm{Z}\left(\mathrm{Z}^\prime\mathrm{Z}\right)^{-1} c$. And for any unbiased estimator $d^{\prime} \mathrm{Y}$, such that $E(d^{\prime} \mathrm{Y})=d^{\prime} \mathrm{Z} \beta=c^{\prime} \beta$, we must have $\mathrm{Z}^{\prime} d=c$.

Calculate Variance,
\begin{align*}
	\operatorname{Var}\left(d^{\prime} \mathrm{Y}\right)&=\sigma^{2} d^{\prime} d=\sigma^{2}(a+(d-\alpha))^{\prime}(a+(d-a))\\
	&=\sigma^{2}\left( a^{\prime} a+2(d-a)^{\prime} a+(d-a)^{\prime}(d-a) \right)
\end{align*}

And notice that,
\begin{align*}
	(d-a)^{\prime} a
	&=\left(d-\mathrm{Z}\left(\mathrm{Z}^{\prime}\right)^{-1} c\right)^{\prime}\left(\mathrm{Z}\left(\mathrm{Z}^{\prime}\mathrm{Z}\right)^{-1} c\right)\\
	&=c^{\prime}\left(\left(\mathrm{Z}^{\prime} \mathrm{Z}\right)^{-1}-(\mathrm{Z}^\prime \mathrm{Z})^{-1}\right) c=0
\end{align*}

Therefore, $\operatorname{Var}\left(d^{\prime} \mathrm{Y}\right) \geqslant \sigma^{2} a^{\prime} a=\operatorname{Var}\left(c^{\prime} \hat{\beta}\right)$
\paragraph{(3)} $E\left(n-\hat{r}^{\hat{2}}\right)=E(\hat{z}(y-\hat{1})=E(\varepsilon(y+y))$
$\left.=E\left(y^{\prime} y\right)-E\left(\hat{y}^{\prime} \hat{y}\right)=\sum_{i=1}^{n} E\left(y_{i}^{2}\right)-E\left(\hat{y}_{i}^{2}\right)+n\left(E_{c} y_{i}\right)\right)^{2}-n\left(E\left(\hat{y}_{i}\right)^{2}\right.$
$=\sum_{i=1}^{n} \operatorname{Var}\left(Y_{i}\right)-\operatorname{Var}\left(\hat{Y}_{i}\right)$
$=n \sigma^{2}-r \sigma^{2}=(n-r) \sigma^{2}$
$\because \hat{Y}_{r}=e_{i}^{\prime} \hat{Y}=e^{\prime} Z\left(z^{\prime} z^{-1}\right)^{\prime} Y \sim N\left(\beta_{i},(H)^{-1} \cdot \sigma^{2}\right), \sum_{i=1}^{n}(H)_{i i}^{-1}=\operatorname{tr}\left(H^{-1}\right)=r$
$\therefore E\left(\hat{\sigma}^{2}\right)=\sigma^{2}$
%-------------------------------------
%=====================
\end{document}


\chapter{Calculation of optical properties - \code{mie}}
\label{sec:optprop}

\index{Mie}

{\sl libRadtran} includes the tool \code{mie} to calculate optical
properties of spherical particles. Two different efficient and well
tested Mie codes are implemented: The
one by \citet{wiscombe80a} and the one by \citet{bohren1998}. Scattering phase matrices and
corresponding Legendre polynomials can currently only be calculated
using the code by \citet{wiscombe80a}.

\section{Basic usage}

\subsection{Running \code{mie}}

Mie scattering calculations are performed for a specified
wavelength interval. The \code{mie} program reads input from standard input,
and outputs to standard output or to a file. If
\code{output\_user netcdf} is specified \code{mie} generates a file
that can be used for radiative transfer calculations with
\code{uvspec}.

The \code{mie} tool is normally invoked in the following
way:

\begin{Verbatim}[fontsize=\footnotesize]
  mie < input_file > output_file
\end{Verbatim}

\strong{Warning:} Please note the error checking on input variables
is very scarce at the moment. Hence, if you provide erroneous input,
the outcome is unpredictable.

\subsection{The \code{mie} input file}

The \code{mie} input file consists of single line entries, each making
up a complete input to the mie program. First on the line comes
the parameter name, followed by one or more parameter values. The
parameter name and the parameter values are seperated by white space.

Filenames are entered without any surrounding single or double quotes.

Comments are introduced by a \code{\#}. Blank lines are ignored.

\subsection{Model output}

The standard output (stdout) of the mie program is one line for each
wavelength and each effective radius.
The format of the output line is
\begin{Verbatim}[fontsize=\footnotesize, frame=single, samepage=true]
  lambda refrac_real refrac_imag qext omega gg spike pmom
\end{Verbatim}
The keywords here are the same as in input option
\code{output\_user}. The Legendre polynomials \code{pmom} are only
caclulated if the option \code{nmom} is specified.

If \code{output\_user netcdf} is specified the output is written to a
netcdf file in the format that is required by \code{uvspec}.

\section{Examples}

\subsection{Calculation for one particle}

The following example shows a Mie calculation for a single spherical
particel with a radius of 200~$\mu$m. The refractive index is
specified by the user. The calculation is performed for wavelengths
from 280 to 5000~nm in 5~nm steps.
\efile{../examples/MIE_1.INP}

\subsection{Calculation for a size distribution}

Not all cloud droplets are of one specific size. The cloud droplet
size spectrum may be represented for instance by a gamma
distribution. Gamma distributions can easily be specified using the
option \codeidx{distribution} \code{gamma} as
demonstrated in the following example:
\efile{../examples/MIE_2.INP}
The refractive index of water is taken for this calculation. In order
to generate input for \code{uvspec} Legendre polynomials and from
those the phase matrices need to be calculated. The option
\codeidx{nmom} specifies how many Legendre polynomials shall be
computed. If the selected number is too small for an accurate
representation of the phase matrix, a warning is given. If
\code{output\_user netcdf} is specified the corresponding phase
matrices are calculated from the Legendre moments. The scattering
angle grid is optimized so that the phase matrix is sampled as
accurate as possible. The option \code{nthetamax} can be used to set
an upper limit of scattering angle grid points to be used.
This example generates a netcdf file which can directly be used in
\code{uvspec} with the options \codeidx{wc\_properties} or
\codeidx{ic\_properties}.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: