next up previous contents
Next: 9.3 Testing, testing, . Up: 9. Setting up a Previous: 9.1 A Model for

9.2 Implementation: a Sphere in Cold Start

After a lengthy session, our friends came up with a code, tentatively called sphere1a.C. Having had the experience of writing the nbody_sh1.C code, it was not to heard to write the bulk of the code, with the main driver, the output routine, and so on. The only really new ingredients were the random number generator and the function sphere that gave all particles their initial conditions.

Alice:
Well, I think we did a nice job. We're beginning to get good at this!

Bob:
Yes, it's a lot less daunting that I had thought. Once you have one moderately complex code written and tested and documented, it gets a lot easier to write the next one. You can simply start with what you already have, and make a variation.

Carol:
You mentioned documentation. We have written comments here and there, and at the head of each function, but perhaps we should get into the habit of providing a little more information, as an introduction, or a form of primer or manual, besides the code comments themselves?

Bob:
By our guest!

Alice:
Yes, I think you should do that, Carol. After all, you are supposed to have learned all that good stuff in your classes!

Carol:
So much for making a good suggestion, only to get saddled with the work. Oh well, I actually like summarizing what I've done, once I'm happy with it, so let me give it a try. I'll keep it short, though, for now. Here goes.

We list here the full code for sphere.C, split up into its functions. First the information at the head of the file:



\begin{Code}[sphere1a.C: summary]
\small\verbatiminput{chap9/sphere1a.C.1_summary} \end{Code}

This comment block is followed by the #include directives and other general definitions, and then the declarations of all functions.



\begin{Code}[sphere1a.C: premain]
\small\verbatiminput{chap9/sphere1a.C.2_premain} \end{Code}

In the main function, the reader is asked to provide a value for the desired number of particles, and optionally a seed for the random number generator. When you want to do a number of independent runs, it is better not to specify such a seed, since then each run will receive a different random seed. If then you want to rerun a particular run, you can specify the seed that happened to be used in that run. You can find the value of the seed used at the beginning of the output of each run, so even if you did not specify one, you will still be able to run any run again.

Note that in reality there is no such thing as a truly random number; instead we use a pseudo-random number, generated by an algorithm designed to generate numbers that are (hopefully) random-look-alike enough for our purpose. As a technical detail, two runs will only get a different random seed as long as they start more than one second apart, since the seed is constructed from the unix clock, read off in seconds.



\begin{Code}[sphere1a.C: main]
\small\verbatiminput{chap9/sphere1a.C.3_main} \end{Code}

The following function is similar to what we used in the previous chapter, for nbody_sh1.C.



\begin{Code}[sphere1a.C: read\_options]
\small\verbatiminput{chap9/sphere1a.C.read_options} \end{Code}

Here is a pseudo-random number generator, which we took from the old Unix example, one which returns a real number between 0 and 1.



\begin{Code}[sphere1a.C: randunit]
\small\verbatiminput{chap9/sphere1a.C.randunit} \end{Code}

This more general version of a pseudo-random number generator allows specification of the upper and lower bounds of the interval.



\begin{Code}[sphere1a.C: randinter]
\small\verbatiminput{chap9/sphere1a.C.randinter} \end{Code}

The following function does all the work, places all $N$ particles homogeneously distributed in a sphere with radius 1.



\begin{Code}[sphere1a.C: sphere]
\small\verbatiminput{chap9/sphere1a.C.sphere} \end{Code}

The resulting $N$-body system is then ready for output.



\begin{Code}[sphere1a.C: put\_snapshot]
\small\verbatiminput{chap9/sphere1a.C.put_snapshot} \end{Code}


next up previous contents
Next: 9.3 Testing, testing, . Up: 9. Setting up a Previous: 9.1 A Model for
The Art of Computational Science
2004/01/25