\chapter{Coupling of Neuronal Model and Collective Dynamics}
    \section{Collective Behavior Experiments}
    The mechanisms that underly the dynamics of collective behavior as we observe it in many species like birds or fish have been of great interest.
    Theoretical work that has been developed to explain the observed behavior often relies on agent-based models with local interaction rules (\cite{Couzin2002}, \cite{Vicsek1995}).
    Recently, \cite{Strandburg-Peshkin2013} investigated the nature of these local interactions by performing an experiment with a fish school of 70 fish where a part of the group has been trained to move to a stimulus so that they could function as leaders of the group from which the information about the stimulus would spread during the experiment.
    By comparing different models to explain the spread of information in the group, which was approximated by the measured response times of the individuals, they found that a model that was based on visual input was supported the most by the observed data.\\
    In another experiment, \cite{Rosenthal2015} found that visual input also plays a crucial role in the spread of startle responses in a fish school.
    For this, they analyzed spontaneously occurring startle events in a fish school of roughly 150 golden shiners and looked at the factors that were most predictive of a fish to startle after the initial startle event.
    The two most predictive features that they found were the logarithmic distance to visible neighbors and the rank of subtended angular area of the initially startling fish on the retina of the focal fish \citep{Rosenthal2015}.
    Note, that due to the elongated body shape of the fish and the location of the eyes on the side, the subtended angular area is different from the distance alone because for example, a neighboring fish that is in front of the focal fish might be closer but it will cover a smaller part of the visual field than compared to a neighboring fish on the side.
    In the case of several neighbors, occlusions will also influence the ranking of the subtended angular areas.
    Based on these features \cite{Rosenthal2015} constructed an interaction network of the fish school and found that the local weighted clustering coefficient at the moment of an initial startle is informative of the size of the following cascade of startles.\\
    The first aspect of the startle behavior of the fish school that we were interested in was the frequency of spontaneously occurring startles.
    In the experiment of \cite{Rosenthal2015} the total number of 138 such startle events was acquired from recordings of 5 different groups over 53 minutes each so that the startling frequency of the group is around $8\cdot 10^{-3}$ startles per second and around $5\cdot 10^{-5}$ for an individual fish.
    A similar experiment that was done with 40 instead of 150 golden shiners found that the spontaneous startling frequency is around $5\cdot 10^{-4}$ to $1\cdot 10^{-3}$ for a single fish (Matt Grobis, Couzin Lab, private communication) and thus an order of magnitude higher than in the experiment of \cite{Rosenthal2015}.
    A possible explanation for the lower startling frequency in the bigger fish school might be a decreased responsiveness that is caused by the presence of other fish as it has been found in Guppies \citep{Fischer2015}.
    \section{Collective Behavior Model}\label{swarm_methods}
    In this section we will describe the model for the collective behavior, the integration of the neuronal model for the startle response initiation, the behavioral measurements and give an overview of the implementation.
	The model describes the individual motion of an agent by a stochastic differential equation that includes Brownian motion as described in \cite{Romanczuk2012a} and a continuous version of social interaction as described in \cite{Couzin2002}.
	We assume a set of $N$ interacting agents ($i= 1,\dots, N$).
	The dynamics of each agent in 2D is described by the following equations of motion:

	\begin{equation}
		\frac{d \vec{r}_i}{dt}=\vec{v}_i(t)
		\label{eq:r_diff}
	\end{equation}

	\begin{equation}
		\vec{v}_i(t) = {s_i\cos(\varphi_i(t)) \choose s_i\sin(\varphi_i(t)) }
		\label{eq:v_def}
	\end{equation}

	\begin{equation}
		\frac{d s_i}{dt} = \alpha\left( \mu_{s} - s_i\right) + F_{i, s} + \eta_{i, s}
		\label{eq:s_diff}
	\end{equation}

	\begin{equation}
		\frac{d \varphi_i}{dt} = \frac{1}{s_i + c_{s}}\left( F_{i,\varphi} + \eta_{i,\varphi} \right)
		\label{eq:phi_diff}
	\end{equation}

	Here $\vec{r}_i$ is the Cartesian position vector and $\vec{v}_i$ is the velocity vector, both of the focal agent.
	In the absence of social forces the speed $s_i$ of agent $i$ is relaxing towards the mean value $\mu_{s}$, which is the same for all agents, while we add the Gaussian white noise term $\eta_{i, s}$ and the relaxation time is determined by $\alpha$.
	The influence of the total effective social force is calculated by the projection onto the normalized velocity vector:

	\begin{equation}
		F_{i, s}=\vec{F}_i \cdot \vec{u}_{v, i} = \vec{F}_i { \cos\varphi_i \choose \sin\varphi_i }
		\label{eq:s_force}
	\end{equation}

	The change of direction $\varphi$ is mostly determined by $\vec{F}_{i,\varphi}$, the projection of the total effective social force onto the orthogonal of the normalized velocity vector and added Gaussian white noise represented by $\eta_{i,\varphi}$.

	\begin{equation}
		F_{i,\varphi}=\vec{F}_i \cdot \vec{u}_{\varphi,i} = \vec{F}_i {-\sin\varphi_i \choose \cos\varphi_i }
		\label{eq:phi_force}
	\end{equation}

	Additionally, we introduce the term $1/(s_i + c_s)$ to decrease the amount of direction change for high speeds where $c_s$ is meant to prevent numerical instabilities for very small $s_i$ values.
	The stochastic differential equations for the direction of motion of individual agents can be solved by a simple Euler-Maruyama method:

	\begin{equation}
		\varphi_i(t+1) = \varphi_i(t) + \frac{1}{s_i}\left( F_{i,\varphi}(t)\Delta t + \sqrt{2 \sigma_{\varphi}\Delta t} \text{ GRN(t)}\right)
		\label{eq:phi_update}
	\end{equation}

	\begin{equation}
		s_i(t+1) = s_i(t) + (\alpha (\mu_s - s_i(t)) + F_{i, s}(t))\Delta t + \sqrt{2 \sigma_{s} \Delta t} \text{ GRN(t)}
		\label{eq:s_update}
	\end{equation}

	\begin{equation}
		\vec{r}(t+1) = \vec{r}(t) + {s_i\cos(\varphi_i(t)) \choose s_i\sin(\varphi_i(t)) }
		\label{eq:r_update}
	\end{equation}
	Here "GRN(t)" is a Gaussian Random Number drawn from the standard normal distribution and $\sigma_\varphi$ and $\sigma_s$ are the standard deviations of direction and speed noise respectively.
	The total effective social force is a sum of three components:
	\begin{equation}
		\vec{F}_i=\vec{F}_{i,rep}+\vec{F}_{i,alg}+\vec{F}_{i,att}
		\label{eq:forces}
	\end{equation}
	Attraction:
	\begin{equation}
		\vec{F}_{i,att}= \frac{1}{n_{i,att}}\sum_{j \in Neigh} \mu_{att}S_{att}({r}_{ji}) \hat{r}_{ji}
		\label{eq:att}
	\end{equation}
	Repulsion:
	\begin{equation}
		\vec{F}_{i,rep}=\frac{1}{n_{i,rep}}\sum_{j \in Neigh} -\mu_{rep}S_{rep}({r}_{ji}) \hat{r}_{ji}
		\label{eq:rep}
	\end{equation}
	Alignment:
	\begin{equation}
		\vec{F}_{i,alg}=\frac{1}{n_{i,alg}}\sum_{j \in Neigh} \mu_{alg}S_{alg}({r}_{ji}) (\vec{v}_j-\vec{v}_i)
		\label{eq:alg}
	\end{equation}
	with $\vec{r}_{ji} = \vec{r}_j - \vec{r}_i$, $r_{ji} = |\vec{r}_{ji}|$ and $\hat r = \vec{r}/|r|$.
	Each component is computed by the normalized sum over the forces of the single interaction pairs with all agents of the neighborhood of the focal agent.
	For the metric interaction the neighborhood consists of all other agents and for the topological interaction the neighborhood consists of the Voronoi neighbors (for explanation, see Introduction).
	Note that the interaction range of the metric interaction is implemented by the weighting functions $S_X(r)$ that are sigmoid functions of distance, which go from 1 to 0, with the transition point at $r_{X,0}$ and steepness $a_{X}$:
	\begin{equation}
	S_X(r)=\frac{1}{2}\left(\tanh(-a_{X}(r-r_{X,0})+1\right)
	\label{eq:sigm}
	\end{equation}
	The strength of the different interactions is set by a constant $\mu_X$.
	For the normalization we use the number of effective neighbors $n_{i,X}$ which we define as the number of neighbors for topological interaction and for metric interaction we take the sum of the values of $S_X(r)$ so that $n_{i,X}$ will be approximately the number of agents within the interaction range:
	\begin{equation}
	n_{i,X} = \left\{\begin{array}{lc}
	|\{j, j \in Neigh\}|, & \text{for topological interaction}\\
	\sum_{j \in Neigh} S_X(r_{ji}), & \text{for metric interaction}
	\end{array}\right.
	\label{eq:neigh}
	\end{equation}\\
	Note, that due to numerical precision the numerical value returned by $S_X(r)$ is equal to zero for analytical values that are smaller than double precision which is in the order of $10^{-16}$.
	To avoid division by zero, we apply the normalization only for agents whose numerical value of $n_{i,X}$ is not zero.
	This also means that the ranges for the social forces are effectively longer since the normalization will rescale all non-zero values returned by $S_X(r)$ to the order of one.
	For the simulation of the fish school we define the basic time unit as seconds and the basic distance unit as one body length (BL) of a fish.
	We use a square arena of size $L$ with periodic boundaries so that the effective topology of the arena is a torus.
	\subsection{Coupling with neuronal model}
	For each agent we use the full neuronal model as described by equations \ref{eq:inhib} to \ref{eq:thrs} to initiate startle responses during the simulation.
    The resting activity of the inhibitory population $\rho_{0}$ of each agent was set to the mode of the fitted log-normal distribution from the previous chapter which was approximately $20.6$ in our case.
    For all other neuronal parameters we took the values that were either fixed or fitted in the previous chapter (see Table \ref{tab:neuroparams}).\\
    The last part that we need to define for the coupling is the visual input.
    In the previous chapter we had an abstract situation with only one stimulus but in the collective behavior simulations the agents are possibly surrounded by many neighbors.
    We considered three different methods to determine the effective visual input: max, k-nearest neighbors (KNN) and k-nearest mean deviate (KMD).
    For all methods we first computed the visual angle of each neighbor of the focal agent by assuming that the shape of an agent is a sphere with a diameter of one body length.
    Then, for the max method we simply took the maximal visual angle, i.e. the visual angle of the nearest neighbor.
    For the KNN method, we calculated the mean visual angle of the k nearest neighbors where k was fixed to the value 3.
    Finally, the KMD method is a combination of the former two methods where we subtracted the mean of the k nearest neighbors from the maximal visual angle.\\
    In the simulation these computations are performed at every time step to calculate the effective visual input for each agent.
    If this leads to a firing of the model M-cell of an agent we initiate a stereotypical startle response by replacing the total social force with a preset startle force:
	\begin{equation}
		\vec{F}_i =  {\mu_{st} \cos(\varphi_{st}) \choose \mu_{st} \sin(\varphi_{st})}.
		\label{eq:startle_force}
	\end{equation}
    Here, the amplitude $\mu_{st}$ was chosen to resemble experimentally measured accelerations and the escape direction $\varphi_{st}$ was drawn from the uniform distribution of the half circle that is pointing away from the direction of the mean distance vector to all agents, i.e. away from the group.
	\subsection{Behavioral Measurements}
	In order to analyze the behavior of the simulated fish school we measure 1) polarization, the degree to which the swimming directions are ordered, 2) cohesion, the degree to which the group stays together and 3) startling frequency, the average frequency of startles in the group.
	Other possible measurements that were not used due to limited scope of the project are the number and size of startle cascades where one startle response triggers a wave of subsequent startles and the mobility of the group, measured by the average traveled distance of the group.
	The group polarization is defined as the mean of the normalized velocity vectors for a given point in time:
	\begin{equation}
		p_{group}(t) = \frac{1}{N} \left| \sum_{i=1}^{N} \vec{v}_i(t) \right|
		\label{eq:pol}
	\end{equation}
	We define the cohesion in three different ways:
	\begin{equation}
		coh_{group}(t) = \left\{\begin{array}{lc}
		\frac{1}{N} \sum\limits_{i=1}^{N} \min\limits_{j \in Neigh} r_{ji}(t) , & \text{mean nearest neighbour distance}\\[12pt]
		\frac{1}{N} \sum\limits_{i=1}^{N}\frac{1}{N-1} \sum\limits_{j=1, j\neq i}^{N} r_{ji}(t), & \text{mean inter-individuell distance}\\[12pt]
		A(Conv(\{\vec{r}_i(t), i \in N \})), & \text{area of convex hull}
		\end{array}\right.
		\label{eq:coh}
	\end{equation}
	Here $A(Conv())$ denotes the area of the Convex Hull of the given set of points, i.e. the Cartesian positions of the agents at time $t$.
	If not noted otherwise, we used the mean nearest neighbor distance as the cohesion measure.
	The startling frequency is calculated for a full simulation run and simply the average number of startles per second.
	\subsection{Implementation}
	This project is based on an already existing implementation for the collective behavior with a different mechanism for the startle response initiation.
    All code can be found on the Github repository at \href{https://github.com/awakenting/master-thesis}{https://github.com/awakenting/master-thesis}.
	The model was implemented in Python 3.6 with the three main classes AgentData, BaseParams and AgentParams.
	An instance of AgentData contains all of the information about the agents that can change during the simulation or that can be different between individual agents and is thus the main data structure.
	The classes BaseParams and AgentParams contain fixed parameters of the simulated environment and global properties of the agents.\\
	In the main simulation function, for each time step, we first reset the social forces of each agent to zero, then we calculate the new social forces according to equations \ref{eq:att} to \ref{eq:alg}.
	Next, we update the inhibitory population activity and the membrane potential of all agents.
	For agents whose membrane potential reached the threshold we set the total force to the startle force as described by equation \ref{eq:startle_force} and for all others it is the sum of the three social forces as described by equation \ref{eq:forces}.
	Using the updated force the coordinates of the agents are finally updated according to equations \ref{eq:phi_update} to \ref{eq:r_update}.
    \section{Numerical experiments}
    In order to analyze the collective behavior model and its coupling with the neuronal model we chose most of the parameters such that the group behavior is comparable to the experiments mentioned earlier in this chapter.
    We fixed the number of agents to 40 and used a square arena with a side length of 40 BLs as in the experiments by Matt Grobis (Couzin Lab, private communication).
    Group behavior was simulated for a simulation time of 500 seconds.
    Most of the parameters of the collective model were fixed to values that are known to produce schooling behavior.
    Two parameters that we varied systematically were the mean speed of the agents $\mu_{s}$ and the noise on the direction change $\sigma_{\varphi}$ (referred to as "direction noise" in the following) as they allowed us to look at fish schools in different states in terms of polarization and cohesion.\\
    Our first measure of interest was the startling frequency.
    In Figure \ref{fig:swarm_heatmaps}, we see that except for the smallest mean speed, the startling frequency increases with increasing direction noise.
    There are also small increases in startling frequency for higher mean speed values but they are relatively low if compared to the effect of direction noise.
    The average nearest-neighbor distance shows the same pattern as the startling frequency with the highest values, i.e. the least cohesion, for highest speed as well as direction noise.
    In contrast, the mean polarization of the school over time is generally close to the possible maximal value and goes below $0.2$ only for the smallest speed and higher direction noise.
    Hence, it seems that a higher startling frequency leads to higher average nearest-neighbor distances.
    This is not true for the polarization where we find similar startling frequencies in the right-most columns of the heatmaps in Figure \ref{fig:swarm_heatmaps} but the mean polarization values range from 0.5 to 0.93.\\
    \begin{figure}[H]
    \begin{center}
    \includegraphics[width=\textwidth]{looming_swarm_fixed_rhonull_int_type_matrix_vis_method_knn_mean_deviate_new.pdf}
    \end{center}
    \caption{\textbf{Startle frequency dependency on collective properties.} Collective behavior results for metric interaction and with KNN-Mean-Deviate as visual integration method (see Section \ref{swarm_methods} for explanation). \textbf{A} Above a mean speed of 0.5 $BL/s$ the startle frequency only depends on the noise on the swimming direction of the individuals. It goes from no startles for very low noise to two startles per second for the highest noise that we analyzed here. \textbf{B} The average Nearest-Neighbor distance increases with noise on the swimming direction similar to the startle frequency but also increases with the mean speed in the range of higher noise values. \textbf{C} The polarization of the collective increases with the mean speed and decreases for higher noise on the swimming direction.}
    \label{fig:swarm_heatmaps}
    \end{figure}
    While we only looked at the mean values over time in Figure \ref{fig:swarm_heatmaps}, in Figure \ref{fig:swarm_over_time} we show how the different measures develop over time.
    For the case with medium direction noise level (left column in Figure \ref{fig:swarm_over_time}) we can clearly see how short periods with many startles affect the polarization as well as cohesion (see e.g. the time point at about 130 s).
    In the right column in Figure \ref{fig:swarm_over_time}, the high direction noise leads to fluctuations of higher amplitude for the polarization and cohesion.
    Additionally, the signals seem to undergo low-frequency oscillations with period lengths of around 60 s to 80 s which we also see for the lower direction noise but less pronounced.\\
    Next, we were interested in the spatial characteristics of single startle events.
    We restricted this analysis to simulations with a mean speed of 1.125 BL/s which is the second row from the bottom in the heatmaps in Figure \ref{fig:swarm_heatmaps}, also indicated by the orange box.
    Unfortunately, the simulations for a direction noise of 0.06 did not result in enough startle events so that we cannot properly analyze the results for this direction noise level.\\
    In the left column of Figure \ref{fig:swarm_startle_stats} we show the position of an agent at the time it startled.
    The coordinate system is build so that the center of mass of the school is at the origin and the mean movement direction of the school points upwards along the y-axis.
    At first glance, the startle positions are distributed similar to the positions of agents that did not startle although the startle positions tend to be more at the center of the school.
    This can be seen more clearly in the right column of Figure \ref{fig:swarm_startle_stats} where we show a histogram of the distance to the center of mass for startles and non-startles.
    Both histograms largely overlap but the startle distances are shifted by a small amount towards the center.
    The direction noise level does not change this pattern.\\
    In the last step of the spatial analysis, we measured the "frontness" of the startling agent by computing the mean movement direction of the group and projecting the position of the startling agent on the vector that originates from the center of mass of the group and points to the mean movement direction.
    The distribution of the frontness values for agents that do not startle (middle column in Figure \ref{fig:swarm_startle_stats}) is symmetric and Gaussian-shaped with the mean value at zero.
    The frontness values of the startling agents have a similar shaped distribution for a direction noise level of 0.1 but the distribution is more narrow which again indicates that the startle positions are closer to the center of the group.
    With increasing direction noise the distribution of startle frontness values becomes more skewed towards positive values, meaning that at high direction noise agents are more likely to startle when they are slightly more in the front than the average agent.\\
    We continue with a comparison of the different methods to determine the visual input of an agent.
    We focused so far on the KMD method where the visual input consists of the difference between the maximal visual angle and the mean of visual angles of the three nearest neighbors (as we fixed k=3).
    If we instead use the mean visual angle of the three nearest neighbors (KMEAN method) as the visual input the most salient difference is an increase of the startling frequency of about one order of magnitude (Figure \ref{fig:swarm_comparison}, left column, first row vs. second row. Note, that we used different colorbar scales in order to make the structure within the heatmaps visible).
    For the KMEAN method we also have startle activity for all combinations of the two parameters mean speed and direction noise which is not the case for the lowest direction noise in the KMD method.
    The effect of the direction noise is here dependent on the mean speed.
    While at higher speeds the startling frequency increases with higher direction noise as we observe it for KMD, at the lowest speed the startling frequency decreases for higher direction noise.
    The overall higher startling frequency is accompanied by nearest-neighbor distances that are up to twice as much as for the KMD method.
    Interestingly, the relationship between startling frequency and average nearest-neighbor distance is reversed for KMEAN because we find the highest nearest-neighbor distance for the lowest startling frequency.
    The polarization follows the same pattern as for KMD with a decrease of polarization for lower speed and for higher direction noise although the polarization does not decrease as much as in the KMD method.\\
    The differences between the MAX and KMD methods are in general similar to those just described for KMEAN.
    The startling frequencies are again much higher and even two to three times as high as for KMEAN.
    In contrast to KMEAN, the startling frequency always increases with increasing mean speed and increasing direction noise.
    Similar to KMEAN, the lowest startling frequency corresponds to the highest mean nearest-neighbor distance but in the case of MAX this point lies at the lowest mean speed and lowest direction noise.
    Finally, the polarization shows the same pattern as for KMD and KMEAN but it does not decrease as much so that the lowest value is at 0.56.
    \begin{figure}[H]
    \begin{center}
    \includegraphics[width=\textwidth]{looming_swarm_over_time.pdf}
    \end{center}
    \caption{\textbf{Collective behavior properties over time for two examples.} The startling frequency, polarization and average nearest-neighbor distance are shown over time from two collective behavior simulations with metric interaction and with KNN-Mean-Deviate as visual integration method (see Section \ref{swarm_methods} for explanation). \textbf{A} For a medium swimming direction noise the polarization (top row) approaches a stable state 50 s after the simulation started. After many startle events in the initial time period the become more rare and come in short waves where many agents startle (second and third row). The group cohesion fluctuates around a value of 2 BL for the average nearest-neighbor distance. \textbf{B} For higher direction noise the polarization of the group shows high amplitude fluctuations around a mean value of 0.5. There are more startle events that seem to occur irregularly over time. The average nearest-neighbor distance shows similar to the polarization bigger fluctuations.}
    \label{fig:swarm_over_time}
    \end{figure}

    \begin{figure}[H]
    \begin{center}
    \includegraphics[width=\textwidth]{looming_swarm_startle_stats.pdf}
    \end{center}
    \caption{\textbf{School position at startle events.} Spatial properties of startle events for metric interaction and with KNN-Mean-Deviate as visual integration method (see Section \ref{swarm_methods} for explanation). Each row corresponds to a different direction noise value, starting with the smallest value for which the startle frequency was not zero at the top. The first columns shows for each startle event the relative position of the startling agent (green) and of all other agents (blue). The origin of the plot corresponds to the center of mass of the group and the mean direction in which the group is swimming is upwards along the y-axis. The second column shows the distribution of "frontness" values which is the the projection onto the vector that starts at the center of mass and points in the mean swimming direction of the school. The third column shows the distribution of the euclidean distance to the center of mass.}
    \label{fig:swarm_startle_stats}
    \end{figure}

    \begin{figure}[H]
    \begin{center}
    \includegraphics[width=\textwidth]{looming_swarm_fixed_rhonull_int_type_matrix_comparison_new.pdf}
    \end{center}
    \caption{\textbf{Comparison of different visual input methods.} Collective behavior results for metric interaction and different visual integration methods (see Section \ref{swarm_methods} for explanation). The same measurements as in Figure \ref{fig:swarm_heatmaps} are shown. \textbf{A} The same heatmaps as in Figure \ref{fig:swarm_heatmaps}, shown again here for better comparison. \textbf{B} The startle frequency, cohesion and polarization for the KMEAN method. The general patterns with respect to effects of mean speed and direction noise are similar to KMD although the startling frequency is much higher and the average nearest-neighbor distance is almost twice as big. \textbf{C} As for KMEAN, the general pattern is similar to KMD although the startling frequency is much higher for the MAX method.}
    \label{fig:swarm_comparison}
    \end{figure}


\vssub
\subsection{~Nesting} \label{sub:num_nest}
\conthead{\ws}{H. L. Tolman}

\noindent
Traditionally, wave models only consider one-way nesting, with boundary data
from low-resolution grids being provided to high-resolution grids. This
approach has always been available in \ws, and is discussed in
\para\ref{sub:one_way}. In model version 3.14, a multi-grid wave model
driver was introduced, considering full two-way nesting between grids. This
approach is discussed in \para\ref{sub:two_way}. The illustrations below
consider regular grids, but the principles discussed are applicable to
curvilinear and triangular grids too.

% -------------------------------------------------------------
\vssub
\subsubsection{~Traditional one-way nesting} \label{sub:one_way}
\vssub

\input{num/fig_nest1.tex}

The conventional wave model program {\file ww3\_shel} considers a single wave
model grid. This program includes options to transfer boundary conditions from
large-scale runs to small-scale runs. Each run can simultaneously accept one
data set with boundary conditions, and generate up to 9 data sets with
boundary conditions. To assure conservation of wave energy with incompatible
depths and currents, the boundary data consists of energy spectra
$F(\sigma,\theta)$. The data file consists of spectra at grid points of the
generating run, and information needed to interpolate spectra at the requested
boundary points. The size of the transfer files is thus minimized if the input
points for a small-scale run are located on grid lines in the large-scale
run. When used as input, the spectra are interpolated in space and time for
every global time step $\Delta t_g$, using a linear interpolation of spectral
components.

The numerical approach for including boundary data in a wave model is
illustrated in Fig.~\ref{fig:nest1}. Active boundary points are assigned in
the grid to separate sea points from land points or from otherwise deactivated
grid points. Between the active boundary points and sea points, a local
boundary scheme is applied (typically first order). In the internal sea points
of the model, the selected propagation scheme is used.

Practical aspects of the conventional one-way nesting approach are
discussed in more detail in Appendix~\ref{app:nest}.

% -------------------------------------------------------------
\vssub
\subsubsection{~Two-way nesting} \label{sub:two_way}
\vssub

Model version 3.14 includes an option to use the multi-grid or mosaic approach
to wave modeling with the program {\file ww3\_multi} \citep{tol:Vict06a,
  tol:MMAB07b, tol:OMOD08b}. In this program, an arbitrary number of grids
with arbitrary resolutions is considered, with data exchange between grids at
each relevant model time step. The grids are given a rank number, where lower
rank corresponds to lower resolution, and equal rank corresponds to similar
resolution (but not necessarily equal resolution).  Three types of data
transfer between grids are considered:

\begin{itemize}
\item Transfer of data from lower to higher rank grids.
\item Transfer of data from higher to lower rank grids.
\item Transfer of data between grids with equal rank.
\end{itemize}

Data transfer from lower to higher ranked grids is accomplished by providing
boundary data to the higher ranked grid, as in the traditional one-way nesting
approach described in the previous section and in Fig.~\ref{fig:nest1}.

\input{num/fig_nest2}

When this approach is combined with data transfer from higher to lower rank, a
full two-way nesting approach is established. In {\file ww3\_multi} the data
at the lower ranked grids is reconciled with the data at the higher ranked
grids after the higher ranked grids have `caught up' in time with the lower
ranked grids.  Considering that the resolution of the lower ranked grid by
definition is lower that the resolution of the higher ranked grid, a natural
way to estimate the wave energy in the lower ranked grid $E_{l,i}$ from energy
in the higher ranked grid $E_{h,j}$ is

\begin{equation}
E_{l,i} = \sum w_{i,j} E_{h,j} \:\:\: , \label{eq:nest_hg1}
\end{equation}

\noindent
where $i$ and $j$ are grid counters in the two grids, and where $w_{i,j}$ are
averaging weights. The weights can be defined consistent with conservation of
wave energy as the surface of the grid box $j$ in the higher ranked grid that
covers the grid box $i$ in the lower ranked grid, normalized with the surface
of the lower ranked grid box $i$. This is illustrated in Fig.~\ref{fig:nest2}.
To avoid circular reconciliation, grid points in the lower ranked grid that
contribute to the boundary data in the higher ranked grid are not updated in
this manner.

\input{num/fig_nest3}

Overlapping grids with similar rank cannot use the above two-way nesting
technique to consistently exchange data. Instead, all such grids are
propagated one time step, after which the grids are reconciled as is
illustrated in Fig.~\ref{fig:nest3}. For grid 1 ($\circ$ in
Fig.~\ref{fig:nest3}) two areas can be distinguished. In area C, the influence
of the boundary has propagated into the grid since the last
reconciliation. The actual depth of penetration depends on the stencil width
of the numerical scheme, and the number of propagation time steps.  In areas A
and B, information from the boundary has not yet penetrated, and this area can
be considered as the `interior' of grid 1.  Similarly, area A represents the
boundary penetration depth for grid 2 ($\bullet$ in Fig.~\ref{fig:nest3})
whereas B and C represent the interior of grid 2.  A simple and consistent
reconciliation between grid 1 and 2 uses data from grid 1 exclusively in area
A (interpolating data from grid 1 to grid points in grid 2 as necessary), and
uses data from grid 2 exclusively in area C. In area B, where interior parts
of both grids overlap, a consistent solution can be found by using weighted
averages from both grids. Note that this approach is easily extended to
multiple overlapping grids.

Note that for explicit numerical propagation schemes and overlapping grids with
identical resolution and coinciding grid points, solutions for overlapping
grids and the compatible single grid can be identical, as long as the overlap
areas are sufficiently wide.

\vspace{\baselineskip}
\noindent
The two-way nesting techniques in {\file ww3\_multi} are largely automated.
Each grid is prepared individually, with its own preferred time stepping
information. Locations where each grid expects to get boundary data from lower
ranked grids are marked
as in the one-way nesting approach. All other bookkeeping needed to implement
the two-way nesting techniques are automated, although some iterations may be
needed to assure that all input boundary points defined in each grid can be
provided with boundary data from other grids in the multi-grid application.
Alternatively, each grid can obtain data from an external data file as in the
traditional nesting approach. In the present implementation, each grid has to
obtain all boundary data from a single file, or from other grids in the
multi-grid application, but cannot receive data from file and grids
simultaneously. Details on the management algorithm developed to run all grid
simultaneously can be found in \citet[section 3.4]{tol:MMAB07b} and
\cite{tol:OMOD08b}, and will not be reproduced here.

Note that the grids used in {\file ww3\_multi} do not need to have the same
spectral discretization. Spectra are converted on the fly in {\file
ww3\_multi}. Details on the numerical techniques used for this approach can be
found in \citet[section 3.5.5]{tol:MMAB07b}.
Grid generation for multiple grids in such an approach can be cumbersome, and
consistency between grids is required for consistent model results. For this
reason automated grid generation utilities have been developed by
\cite{tol:MMAB07a, tol:OMOD08a}.


\input source_header.tex

\newtheorem{example}{Example}[section]

\newtheorem{exercise}{Exercise}[section]

\newtheorem{excursion}{Excursion}[section]

\newtheorem{definition}{Definition}[section]

\newtheorem{theorem}{Theorem}[section]

\newtheorem{lemma}{Lemma}[section]

\newtheorem{corollary}{Corollary}[section]

\newtheorem{proposition}{Proposition}[section]

\newenvironment{proof}{%
\noindent
\textbf{Proof:}
}%
{
\hfill \qed\\
\medskip
}

\newcommand{\qed}{$\Box$}

\newcommand{\Rule}[2]{\genfrac{}{}{0.7pt}{}{{\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{3mm}\fbox{$#1$}}}{{\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{3mm}\fbox{$#2$}}}}

\newcommand{\Rulee}[3]{\genfrac{}{}{0.7pt}{}{{\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{3mm}\fbox{$#1$}}}{{\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{3mm}\fbox{$#2$}}}[#3]}

\newcommand{\under}{|}
\newcommand{\eval}{\to}
\newcommand{\transition}{\rightrightarrows_s}
\newcommand{\translate}{\twoheadrightarrow}
\newcommand{\translateaux}{\hookrightarrow}
\newcommand{\TruE}{\textbf{\texttt{true}}}
\newcommand{\FalsE}{\textbf{\texttt{false}}}

\begin{document}
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	\docheader{2021}{Source}{\S 1 Infinite Loop Detection}{Joey Chen}
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Introduction}
In this report we detail the full specification and method behind the Infinite Loop Detector.

We will first begin by introducing the notion of a {\it State Transition System} and use it to define and derive results on infinite loops.

Next, we will derive a denotational semantics for {\it Source} programs where we will be able to extract Symbolic information about the programs, which can then be used to build the state transition system.

The sets will then be used by the {\it analyzer} to prove the existence of an infinite loop, using theorems in the first section.

\section{Objectives}
Our main goal for this project is to create a tool to detect infinite loops for students in the NUS module CS1101S, where students learn basic programming skills and CS fundamentals.

Due to Turing's theorem, it is impossible to create a program that detects every instance of non-termination. To circumvent this issue, we can make use of the fact that programs students write for this module are fairly simplistic.

Typical programs students write that encounter infinite loops tend to have a fairly simple control-flow structure that lends itself smoothly to analysis.
We conducted a small survey and curated a list of common infinite loops. You can find them here: https://tinyurl.com/source-infloops

Since this tool is meant to aid in students' learning, we will prioritize having no false positives. To ensure this, every detected infinite loop will implicitly contain a proof that the program will not terminate. More details in section 3 below.

Another desirable would be to provide helpful error messages to the students, so that they are able to quickly debug and learn from their mistakes.
\section{Preliminaries (Math)}

\subsection{Definitions}
We will adapt the notation in \textbf{[CS02]}, but modify it slightly to suit our purposes.

A {\it state transition system} $\Sigma=<Names,Var : Names\to V_{name}, T>$  consists of the following components:

\begin{itemize}
  \item $Names$, a finite set of function names, e.g. 'fac', or 'fib', etc. We define a special name $*$ called the {\it termination symbol}, to denote termination.

  \item $Var$, a function which takes as input a function name, and returns a set of global variables used in the function, and variables used as arguments to the function. A {\it state} $\sigma_{name}$ is an assignment to all variables in $V_{name}$. An {\it assertion} is a first-order formula over a set of variables.

  \item $T$, a finite set of {\it transitions}
\end {itemize}

\noindent
A transition is a tuple $<f,g,q,p>$, where:

\begin{itemize}
  \item $f \in Names$ is the function name of the caller function.
  \item $g \in Names \cup \{*\}$ is either the function name of the callee function, or a special {\it terminate symbol}.
  \item $q$ is an assertion over $Var(f)$ representing a sufficient condition for the transition to be executed.
  \item $p$ is an assertion over $Var(f) \cup primed(Var(g))$, denoting the relation between the variables in the caller and the callee. We define a special set function, $primed$ that adds primes to all variable names to avoid any name clashes, i.e. $primed: x \mapsto x'$
\end{itemize}

A {\it program path} is a nonempty sequence $(<name_1, \sigma_1>,<name_2, \sigma_2>,...)$ of pairs containing a function name $name_i$ and a corresponding state in $Var(name_i)$, such that:

\noindent
For each adjacent pair $<name_i,\sigma_i>$ and $<name_{i+1},\sigma_{i+1}>$, there exists $\tau=<f,g,q,p> \in T$ and $name_i = f,\ name_{i+1} = g, \sigma_i \models q, (\sigma_i,\sigma_{i+1}) \models p$.

\noindent
The only exception is when $g$ is the termination symbol, then the sequence ends.

A {\it computation} is a triple $<\Sigma, start, \Omega>$ where

\begin{itemize}
  \item $\Sigma$ is a state transition system
  \item $start$ is one of the names in $\Sigma$
  \item $\Omega$ is a state of $Var(start)$. $start$ and $\Omega$ represent the first function call in the computation.
\end{itemize}

\begin{example}
  Consider the function fib:
\begin{lstlisting}
function fib(x) {
  if(x===0 || x===1) {
    return 1;
  } else {
    return fib(x-1) + fib(x-2);
  }
}
\end{lstlisting}

The corresponding state transition system for fib will be:
$\Sigma := < Names, Var : Names \to V_{name}, T>$

where
\[
Names = \{fib\}
\]
\[
Var(fib) = \{x\}
\]
\[
\begin{aligned}
T = \{ &< fib, fib, (x>1), (x’=x-1) >, \\
   & < fib, fib, (x>1), (x’=x-2) >, \\
   & < fib, fib, (x<0), (x’=x-1) >, \\
   & < fib, fib, (x<0), (x’=x-2) >, \\
   & < fib, *, x=0, \top >, \\
   & < fib, *, x=1, \top > \\
   & \} \\
\end{aligned}
\]

A program path will be:

\[(< fib, x=4 >, < fib, x=2 >, < *, \top >)\]
\[(< fib, x=4 >, < fib, x=3 >, < fib, x=1 >, < *, \top >)\]

Note that different paths may start with the same pair. An infinite loop happens if any one of
the paths are infinite.

\end{example}

We will use the following theorem to motivate our definition of termination and infinite loops.

\begin{theorem}
  The length of a program path is finite iff it contains the terminal symbol *.
\end{theorem}

\begin{proof}

  finite $\Leftarrow$ contains $*$: by definition, once $*$ is encountered, the sequence ends.

  finite $\Rightarrow$ contains $*$: consider otherwise, i.e. $*$ is not encountered, then let $n$ be the length of the sequence since it is finite.
  Now, let $<f_n, \sigma_n>$ be the $n^{th}$, i.e. last element of the sequence. Then by hypothesis, $f_n$ is not $*$ and by definition, there will be another element $<f_{n+1}, \sigma_{n+1}>$ in the program path, so the length of the program path is greater than n, which is a contradiction.
\end{proof}

We say a program path {\it terminates} if it contains the termination symbol $*$.
A computation {\it terminates} if all the possible program paths starting with $<start,\Omega>$ terminate.
A computation is said to go into an {\it infinite loop} if it does not terminate.

We will prove that a computation goes into an infinite loop by constructing a program path that does not have the termination symbol.

\subsection{Immediate Results}
From these definitions, we can already describe two very common classes of infinite loops: forgetting the base case and forgetting to update the state.

\begin{theorem}
  If for some $f$, $\forall <f,g,q,p> \in T,\ f = g$ then any program path containing $f$ does not terminate.
\end{theorem}

This corresponds to the case where there is a missing base case.

\begin{proof}
  For some program path $<g_1,\sigma_1>, <g_2,\sigma_2>,..., <f,\sigma_i>$, clearly $*$ is not present, and the path has not terminated yet.
  By the hypothesis, the next call in the path will be $<f,\sigma_{i+1}>$. By induction, we can construct an infinitely long program path.
\end{proof}

\begin{example}
  Consider the function fac:
\begin{lstlisting}
function fac(x) {
  return x*fac(x-1);
}
\end{lstlisting}

The corresponding transition set is
\[
  T = \{ < fac, fac, \top, (x’=x-1) > \}
\]

Using the previous theorem, any program path will be infinite, i.e.

\[(<fac, x=1>, <fac, x=0>, <fac, x=-1>, ...)\]

\end{example}

\begin{theorem}
  If for some $f$, $\exists <f,g,q,p> \in T,\exists \sigma s.t.\ f=g, \sigma\models q, (\sigma,primed(\sigma))\models p$, then any comptation containing $<f,\sigma>$ in one of its paths does not terminate.
\end{theorem}

This corresponds to the case where the state is not updated between function calls.

\begin{proof}
  For some program path $<g_1,\sigma_1>, <g_2,\sigma_2>,..., <f,\sigma>$, clearly $*$ is not present, and the path has not terminated yet.
  By the hypothesis, we can construct a program path which will contain $<f,\sigma>$ as the next call. By induction, we can construct an infinitely long program path.
\end{proof}

\begin{example}
  Consider the function fac:
\begin{lstlisting}
function fac(x) {
  if(x===0) {
    return 1;
  } else {
    return x*fac(x);
  }
}
\end{lstlisting}

The corresponding transition set is
\[
 \begin{aligned}
T = \{ &< fac, fac, (x>1), (x’=x) >, \\
   & < fac, fac, (x<1), (x’=x) >, \\
   & < fac, *, x=0, \top >, \\
   & \} \\
\end{aligned}
\]

Using the previous theorem, we can construct the infinite program path:

\[(<fac, x=1>, <fac, x=1>, <fac, x=1>, ...)\]

\end{example}

\subsection{Countdown Functions}
From an observation of simple programs in introductory program courses, most of the control flow statements are limited to inequalities involving an integer variable and constant.

Integer inequalities involving only one variable are also very easy to manipulate algebraically, and every inequality can be written in the form $x < c$ where $x$ is an integer variable and $c$ is an integer constant. For example $x\geq 1$ is equivalent to $-x < -2$

A common pattern in introductory programming exercises is managing control flow with something we call a 'countdown variable'. The example below will better illustrate this point.

%\lstset{language=Javascript}
\begin{lstlisting}
function fac(x) {
  if(x===0) {
    return 1;
  } else {
    return fac(x-1);
  }
}
\end{lstlisting}

In this code snippet, we notice 3 things:
\begin{enumerate}
  \item A single integer value $x$ is used for control flow, depending on whether $x=0$ or $x\neq 0$.
  \item One branch of the $if$ statement terminates the program, while the other calls $fac$ recursively, but with its argument $x-1$.
  \item If $x<0$, the program will go into an infinite loop.
\end{enumerate}

We can generalize this to include functions where control flow is dependent only on an inequality involving a single integer variable and an integer constant, and this variable is only incremented/decremented by another integer constant in each recursive function call.

For convenience we define $T_{name}=\{<f,g,q,p>\in T~|~f=name\}$

\begin{theorem}
  If for some $f$, $x \in Var(f)$, $T_f = \{<f,f,x\neq c,x'=x-d>,<f,*,x=c, >\}$, where $c, d \in \mathbb{Z}, d > 0$, then any program path containing $<f, x < c >$ will not terminate.
\end{theorem}

\begin{proof}
   For some program path $<g_1,\sigma_1>, <g_2,\sigma_2>,..., <f,x < c>$, clearly $*$ is not present, and the path has not terminated yet.
   let $c_1$ be the value of x, and $c_{i+1} = c_i - d$. Then, since $c_1 < c$, and $c_{i+1} < c$, $\forall i c_i < c$.
   Then using the first transition in the transition set, we can construct an infinitely long program path $<g_1, \sigma_1>, ..., <f, x=c_1 >, <f, x=c_2> ...$.

\end{proof}

\begin{corollary}
  If for some $f$, $x \in Var(f)$, $T_f = \{<f,f,x\neq c,x'=x+d>,<f,*,x=c, >\}$, where $c, d \in \mathbb{Z}, d > 0$, then any program path containing $<f, x > c >$ will not terminate.
\end{corollary}

\begin{proof}
  simply repeat the previous proof, replacing $-$ with $+$, and $<$ with $>$.
\end{proof}

\section{Implementation}

Now that we have a mathematical foundation for our analysis of infinite loops, we only need to link it to the syntax of Source programs.
Our goal will be to first obtain the function transition set of the program, then test it for non-termination.

The implementation will consist of 3 stages: the symbolic executor, the serializer and the analyzer.

The symbolic executor will convert the Source AST into another tree-like structure as an intermediate value, which will then be used by the serializer to build the function transition set.

The analyzer will then use the function transition sets to produce domains where the functions will enter an infinite loop, using the theorems above.

The process goes something like this:

$S_4 \to_{Symbolic Executor} Symbol \to_{Serializer} T(f) \to_{Analyzer} D $

\subsection{Symbolic Execution}

The symbolic executor will in essence, summarize the AST into a tree of symbols which contain algebraic data about the program.

\subsubsection{Symbols}
We will introduce a few symbols that we use below.

\begin{itemize}
\item A {\it Literal Value symbol} will be used to represent literal integer or boolean values. These symbols will be used mainly for intermediate computation and evaluation.

We will denote this symbol by $LV(x)$ where $x$ is a literal value, for example $LV(4)$ represents the number 4.

\item The {\it Number symbol} will be used to represent algebraic expressions involving the sum of an integer variable and a constant, for example $x+1$.

  We will denote number symbols by $N(x,c,sign)$ where $x$ is the name of the variable, $c$ is the constant value, and $sign$ is the sign of $x$ ($1$ for positive or $-1$ for negative).
  e.g. we will denote $x+1$ by $N(x,1,1)$.

\item The {\it Inequality symbol} will be used to represent equalities or inequalities between an integer variable and a constant, for example $x<20$.
  We will denote inequality symbols by $Ineq(x,a,direction)$, where $x$ is the name of the variable, $a$ is the constant on the right hand side, and $direction$ is $-1$ for $<$, $0$ for $=$, and $1$ for $>$.

\item A {\it Logical symbol} will be used to represent conjunctions or disjunctions between boolean symbols.
  We will denote logical symbols by $L(B_1,B_2,type)$ where $B_1$ and $B_2$ are the boolean symbols, and $type$ is either $conjunction$ or $disjunction$.
  An example is $L(Ineq(x,-1,-1),Ineq(x,1,1),conjunction)$ for $x<-1 \land x>1$.

\item A {\it Boolean symbol} is an inequality symbol or a logical symbol.

\item The {\it Branch symbol} will be used when processing if statements, and will contain a logical symbol for the condition, and 2 symbols for the consequent and alternate statements respectively.
  We will denote branch symbols by $Br(B,Sym_1,Sym_2)$ where $B$ is a boolean or skip symbol, and $Sym_1$ and $Sym_2$ are arbitrary symbols.

\item The {\it Sequence symbol} will be used to denote a list of symbols executed consecutively.
  We will denote it with $Seq(Sym_1,...,Sym_n)$ where $Sym_1,...,Sym_n$ are arbitrary symbols.

\item A {\it Function symbol} will be used to denote a function call and its arguments.
  We will denote it by $F(f,x_1,...,x_n)$ where $f$ is the name of the function, and $x_1,...,x_n$ are its parameters.

\item The {\it Termination symbol} will be used to denote that the program will terminate.
  Similarly to section 2, we will use $*$ for the termination symbol.
\end{itemize}

Since we are only interested in the control-flow structure of the program, we will introduce a {\it skip} symbol to denote that we do not care about the result of evaluating the statement. For expressions we are unable to analyze, we will also use the skip symbol to denote that we are ignoring this expression.

If the skip symbol is used in the construction of another symbol, we will return another skip symbol to denote that the construction does not make sense. Two exceptions are the branch and sequence symbols. To those familiar with functional programming, the skip symbol is similar to the {\it Optional} or {\it Maybe} monads used in languages such as Scala and Haskell.

We summarize the symbols below.

\begin{center}
\begin{tabular}{|l|l|} \hline
Symbol & Explanation\\ \hline
\textbf{Num} & The algebraic expression $x+sign\cdot c$\\
\textbf{Inequality} & The set $\{x|sign\cdot x<sign\cdot c\}$ \\
\textbf{LV} & literal values\\
\textbf{Logical}  & Conjunction or disjunction of $B_1$ and $B_2$ respectively depending on $type$  \\
\textbf{BoolSym} & Boolean Symbols from $\textbf{Logical + Inequality}$\\
\textbf{Branch}  & Symbol for conditional expressions \\
\textbf{Sequence} & A sequence of symbols \\
\textbf{Function} & Function symbol \\
\textbf{Terminate} & Termination symbol \\
  \textbf{Skip} & Symbolizes we ignore this expression \\
  \hline
\end{tabular}
\end{center}

For brevity when the values inside the symbols are not used we will omit them
(e.g. $F$ for $F(f,Sym_1,...Sym_n)$

\subsubsection{Store}
To keep track of variable declarations, we will introduce the notion of a {\it store}. Note that this is a simplified version of the store used in the CS4215 notes.

A Store $\Sigma$ will be used to store symbols,
indexed by identifiers (i.e. strings). We will define an operation, $\Sigma[l\leftarrow v]$,
which denotes a store that works like $\Sigma$, except that $\Sigma(l) = v$

\subsubsection{Symbolic Executor}

\begin{center}
\begin{tabular}{|l|ll|} \hline
Semantic Domain & Definition & Explanation\\ \hline
\textbf{Num} & \textbf{Id * Int * Bool} & Number Symbol\\
\textbf{Inequality}  & \textbf{Id * Int * Bool} & Inequality symbol\\
\textbf{LV}  & \textbf{Int + Bool} & literal values\\
\textbf{Logical}  & \textbf{BoolSym * BoolSym * Bool} & Logical symbol\\
\textbf{BoolSym}  & \textbf{Inequality + Logical} & Boolean Symbols\\
\textbf{Branch}  & \textbf{BoolSym * Sym * Sym} & Branch symbol \\
\textbf{Sequence}  & \textbf{Sym * ... * Sym}  & Sequence symbols \\
\textbf{Function}   & \textbf{Id * Sym * ... * Sym} & Function symbol \\
\textbf{Terminate}   & $\{*\}$ & Termination symbol \\
\textbf{Skip}   & $ \{Skip\}$ & Skip symbol \\
\textbf{Symbol} & \textbf{Num + LV + BoolSym + } & \\
                & \textbf{Branch + Sequence + Function +} & \\
                & \textbf{Termination + Skip}  & Set of all possible symbols \\
\textbf{Store} & $Id \to Symbol$ & store of symbols \\
  \hline
\end{tabular}
\end{center}

The semantic function
$\cdot \under \cdot \eval \cdot$
is defined as a three-argument relation:
\[
\cdot \under \cdot \eval \cdot :
 \textbf{Store} \boldsymbol{*} \textbf{Source \S 1} \rightarrow \textbf{Symbol} \boldsymbol{*} \textbf{Store}
\]

Most of the time, we will not be using the store, so we will use abbreviated symantic function where $\Sigma$ is implicit (TODO??)
\[
\cdot \eval \cdot :
\textbf{Source \S 1} \rightarrow \textbf{Symbol}
\]
\[
E \eval Sym := \Sigma \under E \eval (Sym, \Sigma)
\]

\noindent
When we encounter literal values, we simply wrap them with the $LV$ tag.

\[
\Rule{}{\TruE \eval LV(true)} \hfill
\Rule{}{\FalsE \eval LV(false)} \hfill
\Rule{}{n \eval LV(n)}
\]

\noindent
For identifiers, we scan through the store and return the corresponding symbol, if found.
\[
\Rule{}{
\Sigma \under x \eval (\Sigma(x),\Sigma)}
\]

\noindent
For constant declarations, we simply update the store.

\[
\Rule{
E \eval Sym}{
\Sigma \under \texttt{const} x\ \texttt{=}\ E\ \texttt{;} \eval (v,\Sigma[x \leftarrow Sym])}
\]

\noindent
For sequences of statements.

\[
\Rule{
\Sigma \under E_1 \eval (Sym_1,\Sigma^{(1)}) \qquad ... \qquad \Sigma^{(n-1)} \under E_1 \eval (Sym_n,\Sigma^{(n)})}{
\Sigma \under E_1 \texttt{;}\ ...\ \texttt{;} E_n \texttt{;} \eval (Seq(Sym_1,\ ...\ ,Sym_n),\Sigma^{(n)})}
\]

\noindent
For blocks we revert changes to the store once we exit.

\[
\Rule{
\Sigma \under E \eval (Sym,\Sigma')}{
\Sigma \under {E} \eval (Sym,\Sigma)}
\]

\noindent
For if statements and conditional operators, we wrap the branch test, consequent and alternative in their respective locations in a new branch symbol.

\[
\Rule{E_1 \eval L_1 \qquad E_2 \eval Sym_1 \qquad E_3 \eval Sym_1}
      {if(E_1)\{E_2\}~else~\{E_3\} \eval Br(L_1,Sym_1,Sym_2)}
\]
\[
\Rule{E_1 \eval L_1 \qquad E_2 \eval Sym_1 \qquad E_3 \eval Sym_1}
      {E_1?E_2:E_3 \eval Br(L_1,Sym_1,Sym_2)}
\]

\noindent
Similarly for call statements, we simply wrap the function name and its arguments in a new function symbol.

\[
\Rule{E_1 \eval Sym_1, E_2 \eval Sym_2 ... E_n \eval Sym_n}
{f(E_1,E_2,...,E_n) \eval F(f,Sym_1,Sym_2,...Sym_n)}
\text{where f is an identifier for some function}
\]

\noindent
For return statements, we want to replace the symbol with a termination symbol if the expression does not include any function calls.
To do this, we define a function, $terminal : Symbol \to Bool$ as follows.
\[
  terminal(x) =
  \begin{cases}
    \FalsE, &\quad x \in Function \\
    terminal(x_1)\land ... \land terminal(x_n) &\quad x = Seq(x_1,...,x_n) \\
    terminal(x_1)\land terminal(x_2) &\quad x = Br(B,x_1,x_2) \\
    \TruE &\quad otherwise \\
  \end{cases}
\]

\[
\Rule{E \eval Sym}
      {return~E \eval Sym}
      terminal(Sym)
    \]
    \[
\Rule{E \eval Sym}
      {return~E \eval *}
\neg terminal(Sym)
\]

\noindent
For unary operators, we carry out the respective operations on the literal value, or symbols.

\[
\Rule{E \eval LV(x)}
     {!E \eval LV(\neg x)} \hfill
\Rule{E \eval LV(x)}
     {-E \eval LV(-x)}
\]

\[
\Rule{E \eval Bool}
     {-E \eval BoolNegate(Bool)} \hfill
\Rule{E \eval Num(x,c,positive)}
     {-E \eval Num(x,-c,negative)}
\]

\noindent
Binary operators are more cumbersome as there are many cases to consider. We only allow addition and subtraction between a number symbol and literal value, or between two literal values.

\[
\Rule{E_1 \eval LV(a) \qquad E_2 \eval LV(b)}
      {E_1+E_2 \eval LV(a+b)} \hfill
\Rule{E_1 \eval LV(a) \qquad E_2 \eval LV(b)}
      {E_1-E_2 \eval LV(a-b)}
\]
\[
\Rule{E_1 \eval N(x,c,sign) \qquad E_2 \eval LV(a)}
      {E_1+E_2 \eval N(x,c+a,sign)} \hfill
\Rule{E_1 \eval LV(a) \qquad E_2 \eval N(x,c,sign)}
      {E_1+E_2 \eval N(x,c+a,sign)}
\]
\[
\Rule{E_1 \eval N(x,c,sign) \qquad E_2 \eval LV(a)}
      {E_1-E_2 \eval N(x,c-a,sign)} \hfill
\Rule{E_1 \eval LV(a) \qquad E_2 \eval N(x,c,sign)}
      {E_1-E_2 \eval N(x,a-c,-sign)}
\]

\noindent
For inequality operators, we only allow inequalities between number symbols and literal values.

\[
\Rule{E_1 \eval N(x,c,sign) \qquad E_2 \eval LV(a)}
     {E_1<E_2 \eval Ineq(x,a-c,sign)} \hfill
\Rule{E_1 \eval LV(a) \qquad E_2 \eval N(x,c,sign)}
     {E_1<E_2 \eval Ineq(x,a-c,-sign)}
\]

\noindent
We can make use of the previous definitions to define the rest of the inequalitites more succintly.
For convenience, we define the function \(neg : BoolSym \to BoolSym\) to allow us to negate integer inequalities.

\[
  neg(sym) =
  \begin{cases}
    Ineq(x,c,-sign), &\quad sym = Ineq(x,c,sign), sign\neq 0 \\
    L(Ineq(x,c,-1),Ineq(x,c,1),conjunction), &\quad sym = Ineq(x,c,sign), sign = 0 \\
    L(neg(S_1),neg(S_2),disjunction), &\quad sym = L(S_1,S_2,conjunction)\\
    L(neg(S_1),neg(S_2),conjunction), &\quad sym = L(S_1,S_2,disjunction)\\
  \end{cases}
\]

\[
\Rule{E_1 < E_2 \eval Ineq(x,a,-sign)}
      {E_1 > E_2 \eval Ineq(x,a,sign)} \hfill
\Rule{E_1 < E_2 \eval Ineq(x,a+1,sign)}
      {E_1 <= E_2 \eval Ineq(x,a,-sign)}
\]

\[
\Rule{E_1 < E_2 \eval Ineq(x,a-1,sign)}
     {E_1 >= E_2 \eval Ineq(x,a,-sign)} \hfill
\Rule{E_1 < E_2 \eval Ineq(x,a,sign)}
     {E_1 === E_2 \eval Ineq(x,a,0)} \hfill
\]

\[
\Rule{E_1 < E_2 \eval Ineq(x,a,sign)}
     {E_1 !== E_2 \eval Neg(Ineq(x,a,0))}
\]

\noindent
For logical symbols, we have to take into account short-circuiting. We do this by explicitly creating a branch symbol with the desired behaviour.

\[
\Rule{E_1 \eval B_1 \qquad E_2 \eval B_2}
      {E_1\ \&\&\ E_2 \eval Br(B_1,B_2,LV(false))} \hfill
\Rule{E_1 \eval B_1 \qquad E_2 \eval B_2}
      {E_1\ ||\ E_2 \eval Br(B_1,LV(true),B_2)}
\]

\noindent
Lastly, we catch all other statements which do not fall under any of the previously defined rules under the following rule which simply produces a skip symbol.

\[
\Rule{E}
     {E \eval Skip}
\text{E does not have a rule}
\]

\begin{example}
  Consider the function fac:
\begin{lstlisting}
function fac(x) {
  if(x===0) {
    return 1;
  } else {
    return x*fac(x-1);
  }
}
\end{lstlisting}

\noindent
The symbolic executor will produce the following symbol tree:
\begin{lstlisting}
Seq(
  Branch(
    Ineq(x,0),
    Seq(*),
    Seq(
      F(fac,N(x,-1))
    )
  )
)
\end{lstlisting}

\end{example}

\subsection{Serializer}
After the symbolic executor is run, we will obtain a tree of symbols.
We need one more step to turn this tree into a transition set before we can analyze it.

\noindent
We define a new function
\[
\cdot \mid \cdot \mapsto \cdot: Id\boldsymbol{*} Sym\to T
\]
inductively as follows:

\[
\Rule{}
{Sym \mapsto_f \{<f,*,\top, >\}}
terminal(Sym)
\]
\[
\Rule{}
     {F(g,N(x_1,c,sign),N(x_2,c,sign),...) \mapsto_f \{<f,g,true,p>\}}
p = "sign\cdot x_1=c\land sign\cdot x_2=c \land ..."
\]

\[
\Rule{E_1 \mapsto_f \{<f,g_1,q_1,p_1>,...\} \quad  E_2 \mapsto_f \{<f,g_{n+1},q_{n+1},p_{n+1}>,...\}}
     {Br(C,E_1,E_2) \mapsto_f \{<f,g_1,q_1\land C,p_1>,...\} \cup \{<f,g_{n+1},q_{n+1}\land\neg C,p_{n+1}>,...\}}
\]
\[
\Rule{E_1 \mapsto_f S_1 \quad ... \quad E_n \mapsto_f S_n}
     {Seq(E_1,...,E_n) \mapsto_f S_1 \cup ... \cup S_n}
\]

In practice, it will be convenient to re-use our symbols in our representation of transition sets,
and we will do just that. Let $t = < F_1, F_2, B >$ be a representation of a transition, where

\begin{itemize}
\item $F_1$ is the function symbol of the caller function.
\item $F_2$ is the function symbol of the callee function, or the termination symbol.
\item $B$ is either a boolean symbol, or a null value.
\end{itemize}

One can see that this representation is isomorphic to our previous definition of a transition,
$<f, g, q, p>$ as $f$ and $g$ can be inferred from $F_1$ and $F_2$ respectively,
$q$ can be inferred from $B$, with the null value meaning $q = \top$
$p$ can be inferred by comparing the arguments in $F_1$ and $F_2$.

\begin{example}
  Consider the output from the previous example:
\begin{lstlisting}
Seq(
  Branch(
    Ineq(x,0),
    Seq(*),
    Seq(
      F(fac,N(x,-1))
    )
  )
)
\end{lstlisting}

  \noindent
  The Serializer will produce the following:
\begin{lstlisting}
[
  { caller: F(fac, N(x, 0)),
    callee: F(fac, N(x, -1)),
    condition: Ineq(x, 0, -1)
  },
  { caller: F(fac, N(x, 0)),
    callee: F(fac, N(x, -1)),
    condition: Ineq(x, 0, 1)
  },
  { caller: F(fac, N(x, 0)),
    callee: *,
    condition: Ineq(x, 0, 0)
  },
]
\end{lstlisting}

  \noindent
  Which corresponds to the transition set:
\[
\begin{aligned}
  T = \{ & < fac, fac, x > 0, (x’=x-1) > \\
 & < fac, fac, x < 0, (x’=x-1) > \\
 & < fac, *, x = 0, \top > \\
\}
\end{aligned}
\]

\end{example}

\subsection{Analyzer}
Now we are ready to use the transition sets to detect infinite loops.
The idea is to:
\begin{enumerate}
\item Get a list of computations that result in an infinite loop.
\item Extract the start symbol from that computation.
\item Add an additional branch at the start of the original function which throws an error if the conditions in the start symbol are satisfied.
\end{enumerate}

To do this, we will apply our theorems in section 3 to the transition set, and modify the AST of the program accordingly. We illustrate with an example:

\begin{example}
  For the factorial function,
\begin{lstlisting}
function fac(x) {
  if(x===0) {
    return 1;
  } else {
    return x*fac(x);
  }
}
\end{lstlisting}

  \noindent
  We will insert a line to throw an error.
\begin{lstlisting}
function fac(x) {
  if(x<0){error ("infinite loop...")}else{}
  if(x===0) {
    return 1;
  } else {
    return x*fac(x);
  }
}
\end{lstlisting}
\noindent
Since we know exactly which theorem we used in proving the infinite loop, we can write
descriptive error messages for the student.

\end{example}

Note that since we are editing the program at the AST level, we can freely change the SourceLocation of the nodes we add, so that the error message produced points to the line of the desired function call.

\subsection{Proof of finiteness}
We have one more problem we need to address before we can use our infinite loop detector, that is,
whether the infinite loop detector itself will run into an infinite loop!

\begin{theorem}

\end{theorem}
The symbolic executor will terminate in finite time for finite inputs of code.

\begin{proof}
\noindent
Firstly, note that for every rule in the symbolic executor being evaluated on statement $E$, the program will either
\begin{enumerate}
\item returns a symbol immediately, or
\item recursively call the symbolic executor again, but on code that has length strictly less than statement $E$.
\end{enumerate}

If we assume that the symbolic executor goes into an infinite loop, by observation 2 above, this would imply
that we started with an infinitely long input of code, which is a contradiction.

\end{proof}

Proof that the serializer is finite can be obtained using a similar argument as the above theorem.

The Analyzer also only takes time linear to the length of the transition sets,
since the theorems we currently have only require a linear scan through the transition sets.

\section{Further work}
Currently, our work is limited by
\begin{enumerate}
\item \textbf{Theory}: Since the focus for this project was on Source 1, we did not develop any theory
  which would work for Source 2 and beyond. One idea for Source 2 would be to model lists by using
  their lengths as size functions using the {\it size change principle} in \textbf{[LJBA01]}.
\item \textbf{Lack of theorems}: The theorems in section 3 only cover a very small subset of infinite loops,
  and does not cover several commonly used patterns such as mutal recursion.
\item \textbf{Symbolic Executor is not expressive enough}: The symbolic executor is not able to turn many
  expressions into symbols. For instance, multiplication is not supported at all. Being able to
  represent more programs symbolically will allow us to prove more theorems about infinite loops on them.
  There is still a need to keep the symbolic executor simple though, so a delicate balance needs to be kept.
\item \textbf{Theorems in analyzer are hard to implement}: Manipulating transitions sets as they are
  represented in Typescript records gets quite cumbersome as the complexity of the theorems increase, and
  this will lead to many bugs.
\end{enumerate}

Although there is much room for improvement, we would like to emphasize that this tool is meant to
be an aid to students, and should not become a hinderence to them. Therefore maintaining
performance and a 0 false positive rate should still be prioritized over new features.

\subsection{More theorems}
Another commonly encountered pattern is mutual recursion.

\begin{example}

\end{example}

We will briefly state an approach on how to detect infinite loops in the above example.
Note that a typical computation for the above goes as follows:
\[(<even, x=2>, <odd, x=1>, <even, x=0>, *)\]
We will only encounter an infinite loop for negative values of x, similarly to the factorial function:
\[(<even, x=-1>, <odd, x=-2>, <even, x=-3>, ...)\]
The idea on how to handle this is to split the computation up into 2 sequences, one for each function, i.e.
\begin{enumerate}
\item \[(<even, x=-1>, <even, x=-3>, ...)\]
\item \[(<odd, x=-2>, <odd, x=-4>, ...)\]
\end{enumerate}
Notice that if we can prove that neither of the sequences terminate, the original sequence will also not
terminate (since it will not contain the * symbol).
We can also prove the non-termination of each sequence using the theorem on countdown functions.

However, implementing this involves a lot of work and book keeping, and unfortunately, we did not have
time to complete this before the project deadline. The next section elaborates on how this can be improved.

\subsection{Alternative implementation of analyzer}
As mentioned previously, manually manipulating the transition sets is very cumbersome and will lead to bugs.
It may be fruitful to use another programming language to implement the theorem checking, or even
a domain specific language.

As a proof of concept, here's a quick (simplified) implementation of some of the above theorems (including
mutual recursion) in Prolog.

\begin{lstlisting}
% online demo: https://swish.swi-prolog.org/p/source-infloops.pl
:- use_module(library(clpfd)).

transition(even, term, X, _) :- X #= 0.
transition(even, odd, X, Y) :- X #\= 0, Y #= X - 1.

transition(odd, term, X, _) :- X #= 0.
transition(odd, even, X, Y) :- X #\= 0, Y #= X - 1.

transition(fac, term, X, _) :- X #= 0.
transition(fac, fac, X, Y) :- X #\= 0, Y #= X - 1.

transition(f, f, X, Y) :- Y #= X-1.

hasBaseCase(X) :- transition(X,Y,_,_), X\=Y.
noBaseCase(X) :- \+ (hasBaseCase(X)).

infCountdown(X, Y) :- transition(X, term, C1,
                      transition(X, X, Z, 0), Z #> C1, Y #< C1
                      \+ (transition(X, term, Y, _)).

infCountdown(X, Y) :- transition(X, term, C1, _),
                      transition(X, X, Z, 0), Z #< C1, Y #> C1,
                      \+ (transition(X, term, Y, _)).

nStepTransition(1, X, Y, Z, W) :- transition(X, Y, Z, W).
nStepTransition(N, X, Y, Z, W) :- N \= 1, M is N-1,
                                  nStepTransition(M, X, A, Z, B),
                                  transition(A, Y, B, W).

infCountdown2(X, Y) :- transition(X, term, C1, _),
                       nStepTransition(2, X, X, Z, 0), Z #> C1, Y #< C1,
                       \+ (transition(X, term, Y, _)),
                       \+ nStepTransition(2, X, term, Y, 0).

% ?- noBaseCase(f).           % true
% ?- infCountdown(fac, X).    % X in inf .. -1
% ?- infCountdown2(even, X).  % X in inf .. -1
\end{lstlisting}

As shown above, constraint logic programming finds a good fit in this project.

\subsection{Imperative programming}
Another improvement will be to provide support for Source 3. This is very difficult
as having impure functions will complicate the theory.

\section{References}
\textbf{[CS02]} Michael C\'olon and Henny Sipma. Practical methods for proving pro-gram termination.  In {\it Proceedings  of  the  14th  International  Confer-ence on Computer Aided Verification}, CAV 02, page 442454, Berlin,Heidelberg, 2002. Springer-Verlag.

\textbf{[LJBA01]}  Chin  Soon  Lee,  Neil  D.  Jones,  and  Amir  M.  Ben-Amram.    The size-change  principle  for  program  termination. {\it SIGPLAN  Not.},36(3):8192, January 2001.

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\marginpar{Thursday\\ 2020-8-27, \\ compiled \\ \today}

We can compute this integral if we know the distribution of the velocity moduli \(f(v)\). The angular integral is nontrivial: the single-velocity cross section has an angular dependence.

The calculation is then tricky, the full result is relatively recent, going back only to the 1980s. We will not discuss it here.
We are able to reduce the CSK to a single integral in \(\dd{v}\).

Now, let us give an explicit expression for the velocity distribution \(f(v)\): since the electrons are relativistic we need to use a \textbf{relativistic Maxwellian}, also called a Jüttner distribution, which looks like
%
\begin{align}
f(v) = \frac{\gamma^{5} \exp(- \gamma / \Theta )}{4 \pi c^3 \Theta K_2 (1/\Theta )}
\qquad \text{where} \qquad
\Theta = \frac{k_B T}{mc^2}
\,,
\end{align}
%
where \(\gamma \) is the Lorentz gamma factor of the electron, while \(K_2 \) is the modified Bessel function of order 2.
We will then find a cross section depending on the rescaled temperature \(\Theta \).
The integral over \(\dd{v}\) can then be evaluated numerically.

What we find is that the cross section is strongly peaked, both in energy (temperature, \(\Theta \)) and in angle (\(\xi = \cos \theta \)).

We can then compute the total absorption coefficient, by integrating across all possible outgoing photon energies and angles:
%
\begin{align}
\sigma (\epsilon ; \Theta ) &= \int \dd{\Omega }' \dd{\epsilon }' \sigma (\epsilon \to \epsilon ', \xi ; \Theta ) \\
&= \frac{n \sigma_T}{2 \epsilon K_2 (1 / \Theta )}
\int_0^{\infty } \dd{z} \sigma_0 (\epsilon z) \exp(- \frac{z + 1/z}{2 \Theta })
\,,
\end{align}
%
where \(\sigma_0 \) is a known function. This integral is quite easy to compute numerically.
What we find is quite similar to the drop of the Klein-Nishina cross section, for high temperatures the drop happens at lower energies and is smoother.

\subsubsection{Energy exchange rate}

We want to see how much energy is transferred from photons to electrons and vice versa. We start off by considering the energy exchanged by a single electron and many photons.

The electron will be moving with a velocity \(v\), and we consider the photon flux coming from a direction range \(\dd{\Omega }\). We also consider the distribution across photon energies, so that we have the number flux of photons \(\dd{f} = (I/\epsilon ) \dd{\epsilon } \dd{\Omega }\), where \(I\) is the specific intensity.
With this, we can compute the number of scatterings per unit time, solid angle of emission \(\dd{\Omega }' \) and emission energy \(\dd{\epsilon }'\) as
%
\begin{align}
\dd{N_s} = (\sigma \dd{\Omega }' \dd{\epsilon }') \dd{f}
\,.
\end{align}

Then, the energy change per unit time will be \((\epsilon' - \epsilon ) \dd{N_s}\).
This is all differential: in order to get the full energy change we need to integrate. For a thermal photon distribution we will have
%
\begin{align}
\dot{E}_s = \int \dd{\Omega } \dd{\epsilon } \dd{\Omega }' \dd{\epsilon }'
(\epsilon ' - \epsilon ) \frac{I}{\epsilon } \sigma (\epsilon \to \epsilon ' , \xi ; \Theta )
\,.
\end{align}

Now, we can see that this depends on the number of scatterings, the electron and the radiation temperatures.
Let us assume that the distribution of the photons is Planckian as well, with a temperature \(T_r\), and define \(\Theta _r = k_B T_r / m c^2\).

Then, we can find that for small \(\Theta _r\) and small \(\Theta \) we have
%
\begin{align}
\frac{\dot{E}_s}{E_s} = \qty(4 \Theta - \Theta _r) \qty(1 + \frac{5}{2} \Theta - 21 \frac{\zeta (5)}{\zeta (4)} \Theta _r + \dots)
\,,
\end{align}
%
where \(\zeta \) is  the Riemann zeta function.

\todo[inline]{The 4 is \emph{inside} the parenthesis, right? at thermal equilibrium photons have mean energy \(4 k_B T_e\)\dots See eg \cite[eq.\ 7.36]{rybickiRadiativeProcessesAstrophysics1979}, but I've seen the result often cited elsewhere as well.}

The lowest order contribution is then, for an isotropic photon distribution (not only a blackbody),
%
\begin{align}
\frac{\dot{E}_s}{E_s} = 4 \Theta - \frac{\expval{\epsilon^2}}{\expval{\epsilon }}
\,,
\end{align}
%
so if the photons are colder than \(4 k_B T_e\), where \(T_e\) is the temperature of the electrons, they gain energy.

What happens if a photon \textbf{scatters off the electron distribution many times}?

Let us assume we have an isotropic electron distribution with number density \(n\), and that the scattering is isotropic as well.
We shall use the cross section for Thomson scattering, \(\sigma _T\); this scattering is not isotropic but it has forward-backward symmetry, which is enough for our considerations.

The photon mean free path is then \(\lambda _s = 1 / (n \sigma _T)\), while the mean time between scatterings is \(t_s = \lambda _s / c\).

The mean number of times the photon scatters in travelling a length \(L\) is something we already discussed, and the final result was
%
\begin{align}
N_s = \frac{L^2}{\lambda _s^2} = \qty(n \sigma _T L)^2 = \tau _s^2
\,,
\end{align}
%
where \(\tau _s\) is the average scattering optical depth, if \(\tau _s \gg 1\), and \(N_s \sim \tau _s\) if \(\tau _s\) was \(\ll 1\). Then, \(N_s \sim \max (\tau _s, \tau _s^2)\).

This is useful in order to define the Compton \(y\) parameter: as long as the photon energy \(\epsilon \) is much smaller than \(4 k_B T_e\), we have

\todo[inline]{At least, it looks to me like we need to make that assumption: the photon cannot thermalize further than that temperature}
%
\begin{align}
y = \expval{\frac{\Delta \epsilon }{\epsilon }} N_s \approx \frac{4 k_B T}{mc^2 } \max(\tau _s , \tau _s^2)
\,,
\end{align}
%
where \(\expval{\Delta \epsilon / \epsilon }\) is the mean fractional energy change per scattering, which as we saw earlier was \(4 \Theta \) at lowest order for a nonrelativistic thermal distribution in the Thomson  limit.
This number \(y \) tells us how efficient Comptonization is: by how much does the photon's energy change as it travels through the medium?

If this \(y\) comes out to be much smaller than 1, then we expect to see little effect on the primary spectrum (of the star, for instance) while if \(y \geq 1\)  there will be large spectral deformations.
We can have small \(y\) both if the energy change \(\Delta \epsilon / \epsilon \) is small, or if the medium is optically thin: \(\tau _s\) is small.

\chapter{Spectral modification in plasmas}

\section{The Kompaneets equation}

This is the tool we use in order to understand what kind of spectral modifications we expect to see for radiation passing through a cloud in which repeated scatterings may occur.
It was developed in the 1950s by a Russian physicist working on nuclear weapons.

It is basically a way to rewrite the \textbf{Boltzmann equation}, which is the equation describing the evolution of a photon distribution undergoing scatterings with electrons:
%
\begin{align}
\pdv{n}{t} = c \int \dd[3]{p} \dd{\Omega } \dv{\sigma }{\Omega }
\qty[f(\vec{p}') n(\epsilon ') (1 + n(\epsilon )) - f(\vec{p}) n(\epsilon ) (1 + n(\epsilon '))]
\,,
\end{align}
%
where \(f(\vec{p})\) is the phase space density of electrons, while \(n(\epsilon )\) is the isotropic phase space density of photons.
The reason why there is a \(1 + n(\epsilon )\) factor is to account for \emph{stimulated scattering}, that is, since photons obey Bose-Einstein statistics the probability of photons being scattered from energy \(\epsilon '\) to \(\epsilon \) is enhanced by a factor \(1 + n (\epsilon )\) since they have a tendency to occupy the same quantum state.

This equation describes the scattering back and forth of an electron with momentum \(\vec{p}'\) and a photon with energy \(\epsilon '\) into an electron with momentum \(\vec{p}\) and a photon with energy \(\epsilon \); the electron phase space distribution is considered to be  fixed.

We will assume that the energy of the photons is much smaller than \(mc^2\), so \(\epsilon \ll 1\), and that the electrons are nonrelativistic. We can then approximate
%
\begin{align}
\frac{\epsilon'}{\epsilon } = \frac{1 - \beta \cos \theta }{1 - \beta \cos \theta ' + \epsilon (1 - \cos \theta ) / \gamma } \approx 1 + \beta \cos \theta ' - \beta \cos \theta = 1 + \vec{\beta} \cdot (\vec{\Omega}' - \vec{\Omega})
\,.
\end{align}

Conservation of 3-momentum and energy gives us
%
\begin{align}
\vec{p}' = \vec{p} + \epsilon \vec{\Omega} - \epsilon ' \vec{\Omega}'
\qquad \text{and} \qquad
E' = E + \epsilon - \epsilon '
\,,
\end{align}
%
while if the electrons are thermal as well as nonrelativistic their distribution will be
%
\begin{align}
f(E) = n (2 \pi m k_B T)^{-3/2} \exp(- \frac{E}{\Theta })
\,.
\end{align}

Note that all the energies are in units of \(mc^2\), where \(m\) is the electron mass, and the momenta are in units of \(mc\).

We further assume that the energy change of the photon is small compared to the mean electron energy,
%
\begin{align}
\Delta = \frac{\epsilon ' - \epsilon }{\Theta } = \frac{\epsilon \vec{\beta} \cdot (\vec{\Omega}' - \vec{\Omega})}{\Theta } \ll 1
\,,
\end{align}
%
where we used the relation between the photon energies before and after the scattering which follows from the Lorentz transformation.

\end{document}


\section{Conclusion}
\label{sec:conclusion}

The introduction of variation using fitness selection and crossover allows for quicker convergence and greater accuracy of certain neural networks. In our tests, we observed cases where little to no improvement was seen and we observed cases where error could be reduced  as much as 20\%. This seems to indicate dependence on initial condition and on specific network initializations and randomizations performed at the beginning of each workflow. In all, we have clearly presented a more efficient workflow for refining optimal parameterizations for neural networks compared to random retraining. Therefore, we have shown this methods viability to increase accuracy and reduce computation necessary for accurately training a RNN.


    \filetitle{schur}{Compute and store triangular representation of VAR}{VAR/schur}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
V = schur(V)
\end{verbatim}

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

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{V} {[} VAR {]} - VAR object.
\end{itemize}

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

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{V} {[} VAR {]} - VAR object with the triangular representation
  matrices re-calculated.
\end{itemize}

\paragraph{Description}\label{description}

In most cases, you don't have to run the function \texttt{schur} as it
is called from within \texttt{estimate} immediately after a new
parameterisation is created.

\paragraph{Example}\label{example}