next up previous contents
Next: 5.2 Coordinates in the Up: 5. Exploring with a Previous: 5. Exploring with a

5.1 A More General Leapfrog

For simplicity, Alice et al. decided to continue to embed the initial conditions at the beginning of their code, leaving until later the task of writing a more general input routine. It contains initial conditions corresponding to three particles moving equidistantly on a circle. Here is the code they produced. We will look at the different parts in turn in the following sections.



\begin{Code}[leapfrog2.C]
\small\verbatiminput{chap5/leapfrog2.C} \end{Code}

Not surprisingly, the $N$-body code leapfrog2.C is more than twice as long as the code leapfrog1.C which was hardwired for $N=2$. The general lay-out is similar, so we will quickly walk through it, pointing out the salient differences.

One new feature is the expression const in front of the declaration of the two variables m and pi. In C++ this means that you cannot change the value, once you have assigned it. If you would try to change the value of a const variable, the compiler would issue an error message. But apart from making code maintenance safer by preventing someone from changing a value that should not be changed by mistake, it also makes the code more easy to read, since it tells the human reader that there is a reason to keep a particular value constant.

In our case, we have chosen to give all particles the same mass, and we do not want particles to lose or gain mass, and therefore we declare and initialize the variable storing the shared mass value as const double m = 1. The variable pi stands for the mathematical constant $\pi = 3.14159\dots$, and is therefore declared as const double pi = 2 * asin(1). The actual value is obtained from the arc sine function asin(), which is defined as the inverse of the sine function: when $y = \sin(x)$, then $x=\arcsin(y)$. Since $\sin(\pi/2)=1$, we have $pi = 2 \arcsin(1)$.


next up previous contents
Next: 5.2 Coordinates in the Up: 5. Exploring with a Previous: 5. Exploring with a
The Art of Computational Science
2004/01/25