\documentclass[twoside]{article}[12pt]

% include packages; if you don't like the big default margins, 
% then set the geometry BEFORE including the votutorial-style
\usepackage[a4paper, top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}

%\usepackage{latex-votut/votutorial}
\usepackage[gavo]{latex-votut/votutorial}
%\usepackage[eurovo]{latex-votut/votutorial}
%\usepackage[ivoa]{latex-votut/votutorial}
%\usepackage[customlogo]{latex-votut/votutorial}
%\usepackage{ngerman}
\usepackage[utf8]{inputenc}

% for syntax highlighting:
\usepackage{listings}
\usepackage{textcomp}



% more tool-commands:
\newcommand{\CosmoSim}{%
\link{CosmoSim}{https://www.cosmosim.org/}%
}
\newcommand{\Gnuplot}{%
\link{Gnuplot}{http://www.gnuplot.info/}%
}

% image directory for screenshots, further icons:
\def\dir{images}

% overwrite some defaults:
% -- icons
\renewcommand{\icon}[1]{%
  % parameter: file name
  \raisebox{-1ex}{\includegraphics[scale=\iconscale]{\icondir/#1}}%
}

\fancyhead[LE,RO]{\leftmark}


% -- styles
\renewcommand{\menu}[1]{{\color{darkblue}  \tt $\,$#1$\,$}}
%\renewcommand{\url}[1]{ \href{#1}{#1} } --- that's the default anyway
%\newcommand{\menu}[1]{\textcolor{darkgreen}{ \sffamily{#1}}}


% -- adjust listings style for syntax highlighting
\definecolor{sqlkey}{HTML}{770088}
\definecolor{sqlnum}{HTML}{116644}
\definecolor{sqltable}{HTML}{0055aa}
\definecolor{sqlstring}{HTML}{aa1111}
\definecolor{sqlcomment}{HTML}{aa5500}

\lstdefinestyle{daiquiri}{%
   	frame=single,%
  	rulecolor=\color{sqlborder},%
   	backgroundcolor=\color{sqlbg},%
   	%aboveskip=0pt,%
   	%belowskip=0pt,%
	basewidth=0.5em,%
	lineskip=-0.01pt,%
   	showspaces=false,
   	showtabs=false,
   	upquote=true,
	basicstyle={\normalsize \tt \color{darkgrey}},%
	keywordstyle=\color{sqlkey},% for language specific keywords
	keywordstyle={[2]\color{sqltable}},% for table and column names, manually enclose with ` in LaTeX-source
	keywordstyle={[3]\color{sqlnum}},% for numbers in LaTeX-source
	stringstyle=\color{sqlstring},
	commentstyle=\color{sqlcomment},
}

\lstdefinelanguage{SQL}{%
	morekeywords={SELECT, FROM, WHERE, ORDER, BY, LIMIT, TOP, LIKE, AS, COUNT, GROUP BY, FLOOR, AND, OR, BETWEEN},%
	morekeywords={[3],1,2,3,4,5,6,7,8,9,0},% ... not working, coloring numbers is not supported ...
	sensitive=false,%
	breaklines=true, % wrap lines, if not fitting
	numbers=none, % no line numbers
	morecomment=[l]{--},%
	morestring=[s]{'}{'},%
	moredelim=[is][keywordstyle2]{`}{`},%
	%moredelim=*[s][\itshape]{/*}{*/}
}


\lstset{style=daiquiri, language=SQL}


% environment for script files
\newenvironment{script}{%
 \renewcommand{\baselinestretch}{1.05}
 \normalsize
 \tt
 \setlength{\FrameSep}{100pt}
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \fcolorbox{sqlborder}{sqlbg}{\color{darkgrey} ##1}\hskip-\fboxsep
     % There is no \@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage
   }}%
 {\par\unskip\endMakeFramed}





% maketitle options
\title{Introduction to simulation databases:\\[1ex]Density fields and dark matter halos}
\author{Kristin Riebe\thanks{Leibniz-Insitut f\"{u}r Astrophysik Potsdam (AIP); Email: kriebe@aip.de}, GAVO}
\date{June 27, 2016}
\titlepicture{\dir/titlepicture2}

\def\abstractname{}

\begin{document}
\maketitle

%{\color{red} \logooptions} 

\begin{abstract}
\vspace{-5ex}

Simulation databases like the Millennium database or \CosmoSim\ contain data sets from 
computer simulations of the universe. Via a web interface, users can access these tables. The data contain properties of dark matter halos forming in such simulations, their merging history, 
density information as well as the simulation particles themselves.
 
The simulations we use in this tutorial do not contain any gas dynamics, only gravitational forces are at work. 
Such simulations can be used to study the formation of the large scale structure in the universe: where are clumps forming, where are filaments?
Are there big voids (empty regions) in between? Where are the dark matter halos located, in which galaxies 
may have formed, and how did they evolve?

In this tutorial, we will extract slices of the density field from a simulation, get dark matter halos for the same region, 
and view the particle distribution and merger history for one halo.\\

\textbf{Requirements:}
\begin{itemize}
\item website \CosmoSim\ (\url{https://www.cosmosim.org/})
\item \Topcat\ (\url{http://www.star.bris.ac.uk/~mbt/topcat/})\\
alternatively: \Gnuplot\ (\url{http://www.gnuplot.info/})

\end{itemize}

\textbf{Further material:}
\begin{itemize}
\item CosmoSim documentation\\
 \url{http://www.cosmosim.org/cms/documentation/}
\item Demo video and more extensive simulations-tutorial at\\\mbox{\url{http://www.cosmosim.org/cms/documentation/demos-and-tutorials/}}
\end{itemize}

\end{abstract}

%\usedsoftware{\CosmoSim\ Webseite, \Topcat}

\newpage

\section{Density field}\label{sec:density}
We will start with extracting a slice of the density field in the $x$-$y$-plane from the database. 
See Appendix \ref{app:density} for more background information.

\subsection{Get the data}

\begin{itemize}
	\item Open the \CosmoSim\ website \mbox{\url{www.cosmosim.org}} and login with your username and password.\\
	If you do not have an account yet, please \link{register}{http://www.cosmosim.org/auth/registration/register}. Registration is free and open to anyone.
	\item Go to the \key{Query Form} in the top menu (\url{http://www.cosmosim.org/query}).\\
	Queries for the data are written in SQL, directly into the white text area, and then sent to the database. 

	\vspace{2.ex}
	
    \begin{minipage}[c]{0.57\linewidth}
    
	\item Click on \key{Examples} above the text area on the right side. Choose \menu{Slice of density field at redshift 0} and double-click. The SQL expression for getting the data from the database is automatically inserted:
   	\begin{lstlisting}
SELECT ix, iy, iz, dens 
FROM `Bolshoi`.`Dens256` 
WHERE snapnum=416 AND iz = 94 
ORDER BY ix, iy
	\end{lstlisting}
	
	\item Enter a name for your result table in the line below the main text area, e.g. \entry{density\_416}.
	
    \end{minipage}
    \hfill
    \begin{minipage}[c]{0.38\linewidth}
        \includegraphics[width=\linewidth]{\dir/Screenshot-CosmoSim-QueryForm-Example}
    \end{minipage}
    
	\item Click the \key{Submit new SQL Query} button and confirm all additional messages with Submit/OK.

	\item Your job now appears in the \key{Jobs list} on the left side. The small icon next to the result name indicates if your job is running (triangle) or has already finished (check mark). Click on the name to see the details. 
	
	\item Once your job is finished (which may take a while), click the job name again, then choose the tab \key{Results Table}. This shows the data you retrieved from the database. 
	
\end{itemize}



\subsection{View the density field with Topcat}

\begin{itemize}
\begin{minipage}[c]{0.67\linewidth}
\item \key{Start Topcat}, a tool for viewing database tables and (simple) visualisations. If you don't have it on your computer yet, make sure that Java is installed and download the latest version from the \link{Topcat website}{http://www.star.bris.ac.uk/~mbt/topcat/\#install}. 
You can also start it directly by clicking this link: \\
\mbox{\url{http://www.star.bris.ac.uk/~mbt/topcat/topcat-full.jnlp}}.

\item Switch back to the \key{web browser} with the CosmoSim query page. When your query is finished, click on the job name and switch to the \key{SAMP} tab. Click \key{Register with a local SAMP Hub} at the bottom. Follow the instructions on the web page to allow a security exception, until you see the possible SAMP connections listed on the page. 

\item Click \key{Send table to topcat} to directly interact with Topcat. 

\item If SAMP does not work for some reason, then switch to the \menu{Download}-tab, choose Comma-Separated Values and click on \menu{Download table}. The file will be prepared on the server. Click on the new link to download and save the file. In Topcat, open the file: \menu{File} \goto\ \menu{Load table}, select \menu{Format}: \entry{CSV} and use \menu{System Browser} to select your file.

\item In \key{Topcat}, select your table and choose \key{Graphics} \goto\ \key{Plane Plot} \icon{topcat-planeplot}. This opens a new window. 

\end{minipage}
\hfill    
\begin{minipage}[c]{0.3\linewidth}
   \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-dens256-aux}\\
\end{minipage}




\item In the \key{Position} tab, choose following axes: \menu{X}: \entry{ix}, \menu{Y}: \entry{iy}.

\item Select the \key{Form} tab next to \menu{Position} and \menu{Subsets}. Increase the size of your window (click and drag with left mouse button) or switch to full screen mode in order to see all the possible options.

\item At \menu{Shading} choose the \menu{Mode}: \entry{aux}; and for \menu{Aux}: \entry{dens+1}. This adds density coloring to your dots, but currently your image looks nearly black.

        
\begin{minipage}[c]{0.66\linewidth}

\item Select the \menu{Aux Axis} from the left menu to adjust its properties. Change here the \menu{Scaling} to \entry{log} instead of linear. This will apply a logarithmic scaling for the colors. Since the logarithm is undefined for negative values, we had added +1 to \texttt{dens} for shifting the values above 0 (i.e. from overdensities to relative densities, see Appendix \ref{app:density}). The web like structure should be visible now. 


\item At \menu{Aux Shader} choose a color table you like, e.g. \entry{Cold} for blue shades.

\item Switch to the \menu{Range} tab to fix the range of your density values:\\
\menu{Minimum Aux:} \entry{0.01}, \menu{Maximum Aux:} \entry{100} 

\end{minipage}
\hfill    
\begin{minipage}[c]{0.3\linewidth}
   
    \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-dens256-aux2}\\
    
    \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-dens256-aux3-2}
\end{minipage}

\end{itemize}


\subsection{More density fields}
\begin{itemize}
\item Go back to the CosmoSim website, \key{Query interface}. Click \menu{SQL query} above the job list on the right side to start a new query and select again the same example query. Change now the snapnum inside the query from 416 to \key{snapnum=36}. This changes the snapshot number, i.e. the timestep for which the density field is retrieved. 416 corresponds to today; snapnum=36 is $\approx$ 500 million years after the Big Bang for this simulation. Choose a different name for the results, e.g. \entry{density\_36} and \key{submit your query}. 

\begin{minipage}[c]{0.66\linewidth}

\item Make sure that \key{Topcat is still open} and follow the same steps as before to \key{send your results to Topcat}. 

\item In Topcat, the new table should appear in the main window. Go to the \key{Plane Plot} window we prepared in the previous section. Add the new data to the same plot window by clicking \menu{Layers} \goto\ \menu{Add Positional Control}\icon{topcat-addnewpositionalplotcontrol}.
\item Choose your new table, and again enter \entry{ix} and \entry{iy} for the positions. Make the same adjustments as in the previous section, i.e. at \menu{Form} tab add \key{Aux}, \entry{dens+1}. 
\item Tick the checkmark next to the table name on/off to switch between the different density fields. Can you see how the early density fluctuations have evolved into more condensed structures?


\end{minipage}
\hfill    
\begin{minipage}[c]{0.3\linewidth}
\includegraphics[width=\linewidth]{\dir/Screenshot-topcat-density36}\\
\end{minipage}

\item \textbf{[Extra]} Repeat these steps again for more snapnums, see the table at \url{http://www.cosmosim.org/cms/news/density-fields-for-bolshoi-updated/} for an overview of all available values. Experiment also with different slices, e.g. choose a different value for \sql{iz} or fix \sql{ix} instead.

\item \textbf{[Extra]} You can also compare with the density field calculated using a 512$^3$ grid. Just change the table name in your SQL query to \entry{Bolshoi.Dens512}.

\item  \textbf{[Extra]} Compare with the density field of a simulation with a larger volume: MDR1 with a 1000 Mpc/$h$ box. For this simulation, we only have the density field at redshift z=0, i.e. today, at the end of the simulation, available. Here's an examle query for a slice of this simulation:
   	\begin{lstlisting}
SELECT ix, iy, iz, dens FROM `MDR1`.`Dens512` WHERE iz = 447
ORDER BY ix, iy
	\end{lstlisting}


\end{itemize}

%\pagebreak[2]
\newpage

\section{Dark matter halos}
In this section we retrieve dark matter halos from the Friends-Of-Friends (FOF) halo catalogue. In such halos, galaxies like the Milky Way and galaxy clusters can form.
See the documentation at CosmoSim on Simulations and \link{Halofinders}{http://www.cosmosim.org/cms/simulations/halo-finders/} for more information. 
  
  
\subsection{Dark matter halos for the slice}\label{sec:densityhalos}
\begin{itemize}
\item Go to the \key{Query Form} at the CosmoSim webpage and enter following query:
\begin{lstlisting}
SELECT fofId, x, y, z, mass, size FROM `Bolshoi`.`FOF` 
WHERE snapnum=416 AND floor(iz/4.) = 94
\end{lstlisting}
We need to divide \texttt{iz} by 4, since the grid index for the FOF catalogue is based on a 1024$^3$ grid.

\item Enter a \key{result name} and \key{Submit}. When the job is finished, click on it in the \key{Jobs list} and switch to the \key{Results Table} tab. 


\vspace{1.5ex}

\begin{minipage}[c]{0.66\linewidth}

\item \key{Send the table to Topcat} via SAMP. If necessary, click again \menu{Register with a local SAMP Hub} beforehand and confirm the security exception. Make sure that Topcat is still open beforehand.

\item Go to Topcat's \key{Plane Plot} from the previous section and show only the density plot for snapnum=416 (remove checkmark for all other plots).
\key{Add the new data set} to the same plot window by clicking \menu{Layers} \goto\ \menu{Add Position Control} \icon{topcat-addnewpositionalplotcontrol}.

\item Select the most recent table and adjust the \key{coordinates}:
 \menu{X}: \entry{x}, \menu{Y}: \entry{y}.
 This plots the positions of the dark matter halos on top of the density field.
 
\item Switch to the \key{Subsets} tab and choose a good contrasting color, e.g. yellow.

\item Switch to the \key{Form} tab, click the icon \key{Add new Size Form} \icon{topcat-addnewsizeform}. Enter at \menu{Size}: \entry{mass}.
This scales the dot's size proportional to their mass. Uncheck \menu{Mark} in order to have only the sized markers visible. 

\item Move the \key{Scale} slider for the sized markers to adjust their size.
\end{minipage}
\hfill    
\begin{minipage}[c]{0.3\linewidth}
\includegraphics[width=\linewidth]{\dir/Screenshot-topcat-dens-fofhalos}\\
\end{minipage}


\item The halos should lie on top of the bright regions in the density field, but they look slightly shifted and scaled. 
The reason lies in the density field positions: There we plotted only the ix, iy grid cell index, which corresponds to the lower left corner of each grid cell. 
In order to get the position at the center, we need to add +0.5 to each coordinate and to scale them down from 256 grid cells to 250 Mpc$/h$. 
Thus, select the \key{density} data in the left menu and \key{adjust the positions} to:\\
\menu{X}: \entry{(ix+0.5)*250./256}, \\
\menu{Y}: \entry{(iy+0.5)*250./256}.

\item \textbf{[Extra]} Scale the dots by their \entry{size} (radius) instead of \entry{mass} by changing the column at \menu{Size}. The differences in size are not as strong as differences in mass (mass-factor $\approx$ size-factor$^3$).

\end{itemize}


\newpage

\subsection{3D distribution of halos}\label{sec:3dhalos}
 
\begin{itemize}



	\item Go to the \key{Query interface} at the CosmoSim webpage and enter following new SQL query:

	\begin{minipage}[c]{0.66\linewidth}

	\begin{lstlisting}
SELECT x, y, z, mass, size FROM `Bolshoi`.`FOF`
WHERE snapnum=416 AND mass >= 1.e10
AND sprng_dbl(111) < 0.01
ORDER BY mass DESC
	\end{lstlisting}

	 This randomly selects 1\% of all halos above the given mass threshold and sorts them by mass. 
The query retrieves $\sim$ 3 MB of data. The results will look even nicer, if you change the fraction from 0.01 to 0.1; but this 
may slow down Topcat's 3D view substantially. If you already have performance problems with Topcat or intend to view the results with Gnuplot only, 
add \entry{LIMIT 2000} at the end of the query for limiting your results to 2000 rows.

\item Enter a \key{result name} and \key{Submit}. When the job is finished, send the table to \key{Topcat}.

\item In Topcat's main window, select the new table, then choose \key{Graphics} \goto\ \key{Cube Plot} \icon{topcat-cubeplot}. This opens a new window for 3D plotting.

\item Choose for \key{positions}: \menu{X}: \entry{x}, \menu{Y}: \entry{y}, \menu{Z}: \entry{z}.


\end{minipage}
\hfill    
\begin{minipage}[c]{0.3\linewidth}
\includegraphics[width=\linewidth]{\dir/Screenshot-topcat-fof3d}\\
\end{minipage}

\item Switch to the \key{Form} tab. Increase the window size for better visibility. Select \menu{Shading, Mode}: \entry{density}.


\item Scroll down to \menu{Global Style} and set the dot \key{Size} to \entry{0} instead of the default 1. 
Use the left mouse button to rotate the 3D plot. More help on navigation is
    available by clicking the small question mark at the bottom left of the window.
One can see that the large scale structure is reflected in the halo distribution. 


\end{itemize}

 

\subsection{Particles of a halo}\label{sec:haloparticles}
\begin{itemize}
\item Go to the \key{Query interface} at the CosmoSim webpage and enter following query:

    \begin{minipage}[c]{0.66\linewidth} 

\begin{lstlisting}
SELECT p.x,p.y,p.z 
FROM 
  `MDR1`.`Particles85` AS p,
  (SELECT particleId from `MDR1`.`FOFParticles` 
   WHERE fofId = 85000001014) AS fp 
WHERE fp.particleId = p.particleId
\end{lstlisting}
	
	\item[] This query selects all particles and their positions for the halo with \texttt{fofId = 85000001014} from the MDR1 simulation, at redshift 0 (snapnum=85 for this simulation).
    You can change the digits at the end of this \texttt{fofId} to select a different halo.

	
	\item Enter a \key{result name} and \key{Submit}. When the query is finished, send the table to \key{Topcat}.

    \item In Topcat's main window choose \key{Graphics} \goto\ \key{Cube Plot} \icon{topcat-cubeplot}.
    
    \item Choose following \key{axes}:
    \menu{X}: \entry{p.x}, \menu{Y}: \entry{p.y} and \menu{Z}: \entry{p.z}
    
    \item Color the particles by projected density: choose the \key{Form}-tab, select at \menu{Shading} the \menu{Mode}: \entry{density}. Select a nice colormap, e.g. \entry{Pastel}. Choose \menu{Shader Flip} and \menu{Scaling}: \entry{Log} to get pretty results.

    \end{minipage}
    \hfill
    \begin{minipage}[c]{0.30\linewidth}
        \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-particles2}
    \end{minipage}

  	\item Use the left mouse button to rotate the plot. If you look close enough, you can even detect small lumps of higher density. These small subhalos are remnants of halos that were trapped in the gravitational potential of this halo during the simulation.

\end{itemize}


\newpage

\subsection{Mass growth of a halo}\label{sec:massgrowth}
Dark matter halos grow with time, because they accrete loose material ("smooth accretion") as well as other dark matter halos.
This can be detected when plotting the mass growth of a halo with time.
%A plot of the mass growth can give first hints, whether the merging history of a halo was rather quite and smooth or violent with many major mergers.


\begin{itemize}

\item Go to the \key{Query interface} at the CosmoSim webpage and enter following query:

\begin{minipage}[c]{0.66\linewidth}

\begin{lstlisting}
SELECT p.fofTreeId, p.treeSnapnum, p.mass, p.size
FROM `MDR1`.`FOFMtree` AS p, 
(SELECT fofTreeId, mainLeafId FROM `MDR1`.`FOFMtree` 
  WHERE fofId = 85000001014) AS m
WHERE p.fofTreeId BETWEEN m.fofTreeId AND m.mainLeafId
ORDER BY p.treeSnapnum
\end{lstlisting}

\item[] This query selects all direct progenitors of the given halo (so-called \emph{main branch}). Adjust the \texttt{fofId}, if you want to get the mass growth of a different halo.

\item Enter a \key{result name} and \key{Submit}. Wait until it is finished and send the table to \key{Topcat}.

\item In \key{Topcat}, choose \key{Graphics} \goto\ \key{Plane Plot} \icon{topcat-planeplot}.
\item In the \key{Position} tab, choose following axes: \menu{X}: \entry{p.treeSnapnum}, \menu{Y}: \entry{p.mass}.
	The \texttt{treeSnapnum} is the time axis here: 0 is a snapshot closer to the beginning and 39 at the end of the simulation.



    \end{minipage}
    \hfill
    \begin{minipage}[c]{0.30\linewidth}
        \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-massgrowth}
    \end{minipage}

\item Switch to the \key{Form}-tab and add a new line form \icon{topcat-addnewlineform} for connecting the points. 
Steep increases in the mass from one timestep to another indicate the accretion of a lot of mass at once, i.e. merging events with other dark matter halos.


\end{itemize}
   

\subsection{Merger tree: progenitors of a halo}\label{sec:mergertree}
Let us now find all the halos which have merged into one dark matter halo at the end of the simulation.

\begin{itemize}


	\item Go to the \key{Query interface} at the CosmoSim webpage and enter following query (again adjust the \texttt{fofId}):

    \begin{minipage}[c]{0.66\linewidth} 

\begin{lstlisting}
SELECT p.fofTreeId, p.treeSnapnum, p.x, p.y, p.z, p.size
FROM `MDR1`.`FOFMtree` AS p, 
(SELECT fofTreeId, lastProgId FROM `MDR1`.`FOFMtree` 
  WHERE fofId = 85000001014) AS m
WHERE p.fofTreeId BETWEEN m.fofTreeId AND m.lastProgId
ORDER BY p.treeSnapnum
\end{lstlisting}
    
	\item Enter a \key{result name} and \key{Submit}. When the query is finished, send the table to \key{Topcat}.

    \item In Topcat, go to \key{Graphics} \goto\ \key{Cube Plot} \icon{topcat-cubeplot}. 
    \item Select following \key{axes}:
    \menu{X}: \entry{p.x}, \menu{Y}: \entry{p.y} and \menu{Z}: \entry{p.z}.
    
    \item Switch to the \key{Form}-tab and add a new size form \icon{topcat-addnewsizeform}. Set the \menu{Size} to \entry{p.size} for scaling the dots with the average halo radius.
    
    \item Apply more color by selecting at \menu{Shading}, the \menu{Mode}: \entry{aux} and entering \menu{Aux}: \entry{treeSnapnum}. Adjust the \menu{Opaque limit} to your liking.

    \end{minipage} 
    \hfill 
    \begin{minipage}[c]{0.3\linewidth} 
        \includegraphics[width=\linewidth]{\dir/Screenshot-topcat-mtree2}
    \end{minipage}

    
    \item Now halos from the beginning of the simulation are colored black-purple, more recent progenitors have yellow-red colors. 
    This nicely shows how the progenitor halos have merged with each other at earlier times to form the final halo (red) at the center. 
    


\end{itemize}




\newpage

\appendix
\section{Density field and grid cells}\label{app:density}

The density was calculated by dividing the simulation box in grid cells of equal size. For each cell, the number of simulation particles 
was counted and used to calculate the density: the more particles there are inside, the higher is the density\footnote{This is only half of the truth: in fact, a smoothing function was applied: each particle also contributes to adjacent cells.}.\\



\begin{minipage}[c]{0.45\linewidth}
    \vspace{5ex}
    \includegraphics[width=0.8\linewidth]{\dir/densitygrid.png}\\
\end{minipage}
\hfill
\begin{minipage}[c]{0.5\linewidth}
	\begin{tabular}{c|c|c|c|c}
	ix & iy & particles & relative density & overdensity\\\hline
	0 & 0 & 1 & 0.5 & $-0.5$ \\
	1 & 0 & 1 & 0.5 & $-0.5$ \\
	2 & 0 & 2 & 1.0 & $\hphantom{+}0.0$ \\
	0 & 1 & 2 & 1.0 & $\hphantom{+}0.0$ \\
	1 & 1 & 7 & 3.5 & $\hphantom{+}2.5$ \\
	2 & 1 & 1 & 0.5 & $-0.5$ \\
	0 & 2 & 0 & 0.0 & $-1.0$ \\
	1 & 2 & 3 & 1.5 & $\hphantom{+}0.5$ \\
	2 & 2 & 1 & 0.5 & $-0.5$ \\
	
	\end{tabular}
\end{minipage}
\emph{\small Simplified density estimation: count the particles in each cell, determine the relative density by dividing through the mean number of particles per cell (18 particles/9 cells = 2) and subtract 1 to get the overdensity (above the mean). Such overdensities along with the corresponding indizes (ix, iy, iz) can be stored in database tables.}\\

For the Bolshoi simulation at CosmoSim, the density was calculated on a grid with 256$^3$ cells. The simulation box has a side length\footnote{$h$ is the Hubble constant divided by 100. It can be ignored here and just considered as part of the unit.} of 250 Mpc/$h$, which gives for each of our grid cells a side length of $250/256$ Mpc/$h \approx 0.977$ Mpc/$h$. 


\section{Plotting with Gnuplot}
In case there is no Java/Topcat available on your computer, you can also visualize the data using \Gnuplot\ (\url{http://www.gnuplot.info/}). Download the necessary executable (Windows) or tar-ball (Linux) and install it on your computer. Gnuplot can be used via a command line or by loading a file which contains the commands, a so-called \emph{script}. Here we provide the commands to plot the data from the exercises in this tutorial. 
\begin{itemize}
\item Download the result table of your SQL query via the \key{Download}-tab at the CosmoSim website. Choose CSV format (columns separated with comma), generate the file and click on the link. Save the data to a directory of your choice. 
\item Put the Gnuplot commands of one of the sections below into a pure text-file (on Windows use e.g. Notepad for this). Replace the file name at the plot/splot command by your own data file name and save your script-file as a text file, e.g. \entry{density.plt}.
\item Start the Gnuplot command line: in Linux type \entry{gnuplot} in your terminal; in Windows double-click the corresponding exe-file. Change your local directory to the script-directory (if necessary). Then load the script with \entry{load 'density.plt'} and view the result.
\end{itemize}

\subsection{Density plots}
\begin{itemize}
\item Density slice, section \ref{sec:density}, 
\texttt{density.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/density.plt}

\item Density slice with dark matter halos, section \ref{sec:densityhalos}, \texttt{densityhalos.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/density.plt}
\end{itemize}

\subsection{Halo plots}
\begin{itemize}
\item Plotting the result from the query of section \ref{sec:3dhalos}, random selection of halos, won't look as nice as with Topcat, since 
Gnuplot does not calculate projected densities on the fly. You can still look a selection of the most massive halos in 3D, \texttt{halos3d.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/halos3d.plt}

\item Halo particles, section \ref{sec:haloparticles}, \texttt{haloparticles.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/haloparticles.plt}


\item Mass growth, section \ref{sec:massgrowth}, \texttt{massgrowth.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/halomassgrowth.plt}

\item Merger tree, section \ref{sec:mergertree}, \texttt{halomergertree.plt}:
\lstinputlisting[language=Gnuplot, firstline=1]{data/halomergertree.plt}

\end{itemize}

\end{document}
