The magazine of the Melbourne PC User Group

Dancing Stars - Part 5
Random Cluster of Stars  
Ken Holmes

Continued from PC Update for April 2002

The formation of a spiral galaxy would be quite similar to the formation of a planetary system such as that around the sun, although on a vastly different scale. Both originate in a gas cloud with a reasonably large angular momentum; local higher density regions tend to contract into bodies of various sizes with various velocities. These experience close encounters with each other and, statistically, the group tends to flatten to an oblate spheroid and, with enough angular momentum, into a disc with a central concentration. During encounters, single bodies and small groups suffer greater modification to their orbits than do the larger ones. Around our galaxy are clusters with perhaps 100,000 or 1 million stars, which are orbitting the centre of the galaxy but, because of their total mass, have resisted being deflected into the disc whilst the galaxy was flattening. They are composed of old stars and have settled into what are called "global clusters", implying that the group does not have, within it, enough total angular momentum to cause its own flattening.


Such a group we may try to study with our simple program. In Listing 5, we use the random number generator to position 30 stars in a cubic space with random position and random velocity. Anticipating that, in a developed cluster, the stars near the centre would tend to have higher velocities and, when tossed to the edge, they would slow down, we have allotted velocity components inversely proportional to distance from the centre. To simplify, we have given all stars a mass of 1 and we then subject them to our core code loop.

Keep Them On the Screen

With such a small number of stars allotted random velocities, the mean velocity of the group is unlikely to be zero and it may move out of our viewing area. More significantly, we see a process akin to "evaporation" as individual stars experience encounters which give them high velocity and toss them out of the group; as they leave the group in one direction with high momentum, the conservation of momentum decrees that the group will be pushed in the opposite direction. Accordingly, we periodically apply some code to determine the mean x/y/z position components of the stars remaining within a certain distance and adjust every star's position by the equal and opposite amount to keep the central group in our sights. While we are about it, we also do the same with velocity to reduce its migratory tendency. This does not affect the gravitational motions as we are, in effect, simply moving our vantage point and velocity to keep up with the central group. The number of stars "remaining" is printed in the top left corner.

Caveats

For starters, we won't be too adamant about interpretting the results of this first program, but it will serve to indicate the approach one might take to do a serious study. With a desktop computer processor's speed, we will compromise on time interval and number of stars to get something happening in a reasonable time. QuickBasic does not produce an efficient program; in a trial, I found that an identical program written in a DOS version of Turbo C++ operated more than 20 times faster. The researchers use dedicated supercomputers with highly optimised program code and, of course, nothing else, such as a multitasking operating system, stealing resources. They can consider many more stars of different sizes, and even planets, and they can reduce the time interval to try to make close encounter calculations more realistic. Bear in mind that with n stars there are n(n-1) gravitational interactions so, no matter how many resources they have, they can't match nature acting on myriad stars over time scales of hundreds of millions of years with gravity acting continuously, or, in effect, with infinitely small time intervals.


Figure 10


What Do We See?

In Figure 10, the upper region is a stereo view of the paths of the stars over 640 loops of the code. The centre is a mess but using the mirror will show the orbits of stars tossed out of the group, temporarily or permanently. We use 15 colours of the VGA palette (twice) to distinguish the paths and velocities of our 30 individual stars; alternatively, you may clear the screen after each 2 plots (by uncommenting line 47 - the '2' may be upped for a longer path and slower twinkle) and plot them in white (by substituting lines 81 and 82 for lines 79 and 80) to see bright white stars moving in 3D and twinkling against the black background; this is quite effective.

Along the bottom, not in stereo, are plotted the velocities of each star to show how you might extract any information you might wish to examine to assess the reality of the data the program is producing. What these indicate is that the peak velocities, which occur during encounters within a few pixels, are about 1 pixel per time unit. This is a long way from giving a reasonable outcome from the encounter so the results are really nonsense. Thus, we cannot say whether the tendency to "evaporate" even approaches the reality for 30 stars. Intuitively, one might feel that larger group are harder to escape from. It is such feelings that lead people to seek more resources, to reduce time interval and to extend running time to days or weeks. Also, if plotting the picture absorbs too much time (or no one is watching) the statistical data might be saved periodically in files for later analysis. 
'----------------------------------------------------------

'Listing 5        'Global cluster, 30 stars of mass 1. g = 1

DEFINT I-K

DIM x(30), y(30), z(30), c(30) 'position components & colour

DIM vx(30), vy(30), vz(30)              'velocity components

DIM dsq(30, 30), a(30, 30)  'distance sqrd & accel. - i to j

DIM ax(30, 30), ay(30, 30), az(30, 30)   'x/y/z accel. comps

DIM atx(30), aty(30), atz(30)   'x/y/z comps of total accel.

SCREEN 12: WINDOW (-320, 0)-(319, 479)   'VGA screen 640x480

eyexr = 80: eyexl = -80: eyey = 480: eyez = -600   'eye posn

ns = 30: g = 1                     'number of stars, gravity

xc = 160: yc = 340: zc = 160         'mean position of stars

RANDOMIZE TIMER      'Random number generator seeded by time

FOR i = 1 TO ns       'allocate colour/pos./vel. of 30 stars

 c(i) = i MOD 15 + 1: x(i) = RND * 160 + 80

 y(i) = RND * 160 + 260: z(i) = RND * 160 + 80

 'ie. positions star within xc+-80, yc+-80, zc+-80

 dx = x(i) - xc: dy = y(i) - yc: dz = z(i) - zc

 d = SQR(dx * dx + dy * dy + dz * dz)  'distance from centre

 IF d < 20 THEN d = 20               'avoid dividing by zero

 mv = 3 / d: vx(i) = (RND - .5) * mv  'mv/2 is max vel comp.

 vy(i) = (RND - .5) * mv: vz(i) = (RND - .5) * mv

 'ie. each velocity component is between -mv/2 and +mv/2

NEXT i

 DO WHILE n < 1000         'Main program loops until any key

   'clear screen, centralise stars, zero the total momentum

  CLS : LINE (0, 150)-(0, 479): LINE (-320, 150)-(320, 150)

  tx = 0: tvx = 0: ty = 0: tvy = 0: tz = 0: tvz = 0  'totals

  nr = 0  'to count number remaining within 150 pixels range

  FOR i = 1 TO ns

   dx = x(i) - xc: dy = y(i) - yc: dz = z(i) - zc

   di = SQR(dx * dx + dy * dy + dz * dz)  'dist. from centre

   IF di < 150 THEN                   'ignore those escaping

    tx = tx + x(i): tvx = tvx + vx(i)    'total dist. & vel.

    ty = ty + y(i): tvy = tvy + vy(i)  '... x/y/z components

    tz = tz + z(i): tvz = tvz + vz(i): nr = nr + 1 'incr. nr

   END IF

   LINE (di - 320, 0)-(di - 320, 10), c(i)   'plot distances

  NEXT i: PRINT nr; "stars left"   'print no. still in range

   'get average posn & vel. "creep" of those remaining

  tx = tx / nr - xc: ty = ty / nr - yc: tz = tz / nr - zc

  tvx = tvx / nr: tvy = tvy / nr: tvz = tvz / nr

  FOR i = 1 TO ns  'adjust position and momentum of 30 stars

   x(i) = x(i) - tx: y(i) = y(i) - ty: z(i) = z(i) - tz

   vx(i) = vx(i) - tvx: vy(i) = vy(i) - tvy: vz(i) = vz(i) - tvz

  NEXT i

  FOR n = 1 TO 640  'calculate 640 time intervals

   'IF n MOD 2 = 0 THEN CLS

   FOR i = 1 TO ns - 1: FOR j = 2 TO ns'each with all others

    IF i < j THEN            'i > j case is negative of this

     xd = x(j) - x(i): yd = y(j) - y(i): zd = z(j) - z(i)

     dsq(i, j) = xd * xd + yd * yd + zd * zd  'distance sqrd

     IF dsq(i, j) < 4 THEN dsq(i, j) = 4'stop excessive acc.

     d = SQR(dsq(i, j))                            'distance

     a(i, j) = g / dsq(i, j)            'grav. accel. i to j

     ax(i, j) = a(i, j) * xd / d       'x comps of i to j...

     ax(j, i) = -ax(i, j)                  '...and of j to i

     ay(i, j) = a(i, j) * yd / d       'y comps of i to j...

     ay(j, i) = -ay(i, j)                  '...and of j to i

     az(i, j) = a(i, j) * zd / d       'z comps of i to j...

     az(j, i) = -az(i, j)                  '...and of j to i

    END IF

   NEXT j: NEXT i

   FOR i = 1 TO ns: atx(i) = 0: aty(i) = 0: atz(i) = 0

    FOR j = 1 TO ns        'add x & y acc. due to all others

     IF i <> j THEN atx(i) = atx(i) + ax(i, j)     'cumul. x

     IF i <> j THEN aty(i) = aty(i) + ay(i, j)     'cumul. y

     IF i <> j THEN atz(i) = atz(i) + az(i, j)     'cumul. z

    NEXT j

    vx(i) = vx(i) + atx(i)               'change x vel. of i

    x(i) = x(i) + vx(i)                  'change x pos. of i

    vy(i) = vy(i) + aty(i)               'change y vel. of i

    y(i) = y(i) + vy(i)                  'change y pos. of i

    vz(i) = vz(i) + atz(i)               'change z vel. of i

    z(i) = z(i) + vz(i)                  'change z pos. of i

    zf = eyez / (eyez - z(i))      'z factor for perspective

    xr = eyexr + (x(i) - eyexr) * zf 'screen x for right eye

    xl = eyexl + (x(i) - eyexl) * zf  'screen x for left eye

    yb = eyey + (y(i) - eyey) * zf   'screen y for both eyes

    IF xr > 0 THEN PSET (xr, yb), c(i)  'mark right eye posn

    IF xl > 0 THEN PSET (-xl, yb), c(i)'mark left - mirrored

    'IF xr > 0 THEN PSET (xr, yb), 15  'white right eye posn

    'IF xl > 0 THEN PSET (-xl, yb), 15'white left - mirrored

    v = SQR(vx(i) * vx(i) + vy(i) * vy(i) + vz(i) * vz(i))

    PSET (-320 + n, v * 100), c(i)  'plot velocity - vel. of

   NEXT i                '... 1 plots 100 pixels from bottom

   k$ = INKEY$                      'check for any key to...

   IF k$ > "" THEN n = 2000   'exit FOR n..NEXT n & DO..LOOP

  NEXT n

 LOOP



Click to download DSTARS5.ZIP for source listing

and executable version of this program.

Next Month

We will look at a similar program in Borland Turbo C++ for those more interested in C language or those who may wish to investigate more ambitious programs. 

Note: This series consists of Parts 1 to 6 in PC Update issues from December 2001 to June 2002.  Click to download DSTARS.ZIP file (225 KB) which contains all text, source and executable copies of programs used in this series.

Reprinted from the May 2002 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]