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.
Not surprisingly, the -body code leapfrog2.C is more than
twice as long as the code leapfrog1.C which was hardwired for
. 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
, 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
, then
.
Since
, we have
.