This project displays the common natural phenomenon expressed
by the inverse-square law. Essentially what this shows us
is that, for many types of acting forces (e.g. gravity or
magnetism), the strength of the force varies inversely with
the square of the distance between the two bodies the force
acts upon. In this case, the formula used is the standard
formula for the Law of Gravitational Attraction: 
(m1 * m2 * G) / (r^2).

This is an 'n-body' model, where we have a certain
number of small particles, and one large acting mass
(the mouse pointer). The force is entirely one-way:
the large mass remains unaffected by the smaller particles
around it. (Note that this is purely for purposes of
simulation. In the real world, a force such as gravity acts
on all bodies around it.)

Gravity is the best example of such a force. You can watch
the particles form elliptic orbits around the mouse pointer,
or watch them slingshot around it, similar to how a comet
streaks past our sun. Think of the individual turtles as
planets or other solar bodies, and see how they react to
various masses that move or remain stationary.

First select the number of particles with the NUMBER slider.
Then press the SETUP button to scatter them across the

The MASS slider sets the value of the mass of the
acting force. (Thus, it also determines at what distances
the particles can safely orbit at, before they get sucked in
by an overwhelming force.)

The TRACE-MODE switch, when set to 1, has each turtle mark
out its position every time-tick. Thus you can see the
ellipses and parabolas formed by different particles'

When the sliders have been set to desireable levels, press
the GO button to begin the simulation. Move the mouse to
where you wish it to begin, and click the mouse button. This
will start the turtles moving. If you wish to stop the
simulation (say, to change the value of MASS), press and
hold the mouse button until the particles stop moving and
the GO button shuts off. You may then re-press GO at your
leisure- again, it will take a mouse click to start
everything moving once GO has been pressed.

Finally, the CLEAR button will return the background to
black, effectively clearing the screen of all traces if
TRACE-MODE has been set on.

The most important thing to observe is the behavior of the
particles. Notice that, as the particles have no initial
velocity of their own, a single, motionless acting mass will
just pull them all in. Even a very slight mass (MASS
set to a small value) will pull in all those particles it
can reach. (Due to limited precision beyond a certain
point, the motive-force on a particle can become zero.)

Move the mouse around- watch what happens if you move it
quickly or slowly. Jiggle it around in a single place,
or let it sit still. Although most of the particles
will ultimately become absorbed by the mass, observe what
patterns the others fall into. (Set TRACE-MODE on to watch
this explicitly; 'on' meaning that the switch is set to 1.)

When you examine the procedure window, take note that the
standard turtle primitives 'seth', 'fd 1', etc... aren't
used here. Everything is done directly to the x-coordinates
and y-coordinates of the turtles.

There are a few other parameters, set in the code, that
affect the performance of 'gravitation'. First, each
particle has a maximum velocity it can attain (obviously
separated into its x and y components)- this is kept as the
global variable 'max-v'. Also, the force acting upon each
turtle is multiplied by a constant, 'g' (another global
variable). Both these parameters keep the simulation from
going too quickly, and in no way affect the realistic
qualities of the model. Feel free to play with their values,
set in the procedure 'setup'. (Of course, the default value
of g for the 'gravitation' project, 0.5,  is much higher
than the value used in Newtonian Mechanics, 6.67e-11.

Initial conditions are very important for a model such as
this one. Try changing how the particles are placed during 
the 'setup' procedure.

Make sure to watch how different values of the MASS slider
impact the model.

Let the particles begin with a constant velocity, or give
them all a random velocity. You could add a slider that
would let the user set the velocities, and thus be able to
compare the effects of different speeds. Or try giving
each particle a variable mass, which directly affects
the strength of the acting force upon it.

'gravitation' assumes the force to be an attractive
force, i.e. the particles tend to approach it. However,
it should be a relatively easy change to make this into
a repulsive force. Try setting up each particle on top of
the repulsive force, giving each a different mass, and
observe what happens.

When a particle moves off of the edge of the screen, it
doesn't re-appear by wrapping onto the other side (as in 
most other StarLogo models). 'gravitation' creates
the illusion of a plane of infinite size, to better model
the behavior of the particles. Notice that, in trace-mode,
you can see most of the ellipse a particle stamps out, even
though the particle periodically shoots off the screen. This
is done through a combination of the basic turtle primitive
'ht' (mnemonic for 'hide-turtle'), keeping every turtle's
xcor and ycor as special turtle variables 'xc' and 'yc', and
a re-write of the 'towards-nowrap' and 'distance-nowrap'

Due to the large numbers that are sometimes involved in the
calculations here, 'gravitation' keeps track of the
largest such number it has to deal with. This is kept as a
variable, 'max-int', and is set to 32767 (this is 2 to the
15th power, minus one). Although observer variables are
essentially unbounded in size, turtle and patch variables
are limited to a 16-bit representation. (For you computer
scientists, the most-significant bit is obviously kept as
the sign-bit, and StarLogo also reserves another 16 bits for
floating point calculations. Hence the smallest fraction
StarLogo can work with is 1/65536.)

Notice that all of the code within the procedure 
'move-particles' is kept to turtle instructions. 
Almost all the variables are turtle-variables, even the
position of the acting-mass. Although it seems like it
would make more sense to keep that value as a global
variable, it is actually faster for StarLogo to keep it
inside each turtle. 'Turtle code' runs the fastest when
kept free of outside distractions (such as the calling of
an observer procedure). Even just one line of code could
cause a significant slow-down. In fact, such a slowdown
occurs already due to the presence of the code in 'go' that
handles the clicking of the mouse button. This little bit of
checking cuts the speed of 'gravitation' by about half.