Scientific Prog in PP
Volume Number: 14
Issue Number: 5
Column Tag: Beginner's Corner
Scientific Programming in PowerPlant
by Michael McLaughlin, McLean, VA
PowerPlant provides a simple application shell with which we can do serious number
crunching, and still have a user-friendly, Macintosh interface
An Array of Excuses
"I can do everything I need in C.
Does that sound familiar? I hear it a lot, not just with reference to C but also to
Fortran and, once, to Basic! Scientists and engineers, in particular, often tend to view
a visit to the compiler much as they would a trip to the dentist -- a rueful necessity,
best done as quickly and painlessly as possible. The result, unfortunately, is seldom a
nice, friendly application with a pleasing graphical user interface (GUI) but, rather,
HelloWorld plus footnotes, usually accompanied by lots of global variables, predefined
arrays, and nary a pointer in sight.
The standard excuse is that they are in a hurry and, anyhow, C++ is useful only for
software that manipulates objects (whatever they are) and is totally inappropriate for
the sort of mathematical calisthenics that cause physicists, astronomers, and others to
chew up hours of clock cycles in search of a single number.
There are several reasons why computations can last for hours. They might involve a
large number of mini-problems, or they might address a single problem comprised of
many steps. In some cases, the solution could require iteration to convergence, and this
might take a while. The example described below contains all of these elements.
However, its size is small enough for demonstration without compromising the
intended objectives.
This article has three objectives: to demonstrate that C++ not only is appropriate but,
indeed, advantageous when it comes to writing mathematical software; to show that
PowerPlant is an easy way to develop Macintosh applications rapidly; finally, to
present a simple template, generally suitable for adaptation to CPU-intensive
applications.
A Scientific Problem
We begin with the Solar System at midnight (GMT) on the Fourth of July, 1976. We
are given the Cartesian coordinates for all of the planets and the Sun, along with some
additional data to be discussed below. The problem is to use Newton's Law of Gravitation
and his Second Law of motion to predict the configuration of the Solar System 365 days
later, keeping the system center-of-mass as a constant origin.
Given Newton's Laws, this task appears straightforward. It consists of a set of 27
second-order differential equations (three coordinates per planet), each of which has
the following form:
where q(n) is a coordinate of body[n], t is time, m(j) is the mass of body[j], r(ij) is
the radial distance between body[i] and body[j], and G is the gravitational constant.
Time is measured in days and distance in astronomical units (AU). (1 AU is,
approximately, the average distance between the Earth and the Sun.) This formulation
ignores several important factors, namely, Special and General Relativity,
nonsphericity of the planets, and a handful of asteroids that perturb the orbit of Mars
significantly, but these all are secondary effects, complicated though they are.
Solving this problem requires integrating all 27 differential equations over
successive time intervals, h, until the target time is reached. Here, we shall use
Numerov's method, in
where R is the error for this coordinate for interval k. Since F(q)(k+1) appears on
the right-hand side of this equation, even though q(k+1) is obviously unknown,
iteration to convergence will be needed.
With every iteration, the center-of-mass constraint determines the coordinates of the
Sun. For each coordinate, we compute
Numerov's method is not meant to achieve world-class accuracy. In fact, its most
redeeming quality is its brevity. This technique requires two very accurate starting
configurations as well as values for the masses of the 10 bodies (as is customary,
Earth and Moon are combined). These values are listed in the data file (init.dat)
accompanying the source code package available from ftp://ftp.mactech.com/src/.
And now, it's time to start coding.
Building the Application
What follows assumes familiarity with CodeWarrior and Constructor (or, at least,
ResEdit). Knowledge of C++ is not assumed although a passing acquaintance with its
syntax will be helpful. This example was compiled using CW Pro 2. Earlier versions
should require few, if any, changes.
Start by creating a new project, named SolarSys.ยต, from the Basic PowerPlant PPC
(or 68K) stationery. This ensures that it gets the requisite PP files and libraries. It
also means that almost all of the project headers will be precompiled. Change the
names of the project and target to SolarSys. Select Run to compile the PP files and to
ascertain that everything works.
The Main Routine
This default app does not do exactly what we want, so we shall have to modify it a little
bit. Open the file CPPStarterApp.h and replace CPPStarterApp with SolarSysApp
everywhere. Then, after the #include, add the line