Simulation Physics

Isaac Newton laid down 300 years ago the rule by which you can calculate precisely the future behaviour of any classical (non quantum) system of objects. The rule stated mathematically looks like this:

a = F / m

That is, the acceleration a of an object is in the direction of the total force F on the object, and is equal in size to the strength of the force divided by the mass m of the object. In the case of this box, the objects in question are the 16 spring junctions (or atoms). Each atom has a certain fixed mass and is subject to three kinds of forces: (1) a constant force due to gravity, (2) the forces exerted by the springs (or chemical bonds) that join the atom to the other atoms, and (3) the force exerted by the walls whenever the atom comes close to them.

The force of gravity is expressed mathematically by:

F_gravity = - m G

That is, this force is constant and has a strength of m times G, the constant you adjust with the slider in the simulation. The minus sign means the force is directed always downwards.

The force of each spring is stated mathematically by Hooke's Law:

F_spring = - K (d - L)

where d is the distance between the two atoms the spring connects, and L is the length of the spring when it is neither stretched nor compressed. L has a certain fixed value. d, however, varies during the course of the simulation as atoms come closer and move farther apart. K is the strength of the spring, which you adjust with a slider. The spring force is negative when the spring is stretched (d > L) and positive when the spring is compressed (d < L), so that the spring pulls the atoms toward one another when it is stretched and pushes them away from one another when it is compressed. Much like chemical bonds do.

The force due to the walls is given by:

F_wall =  - K d  if d < 0
       =      0  if d > 0

where d is the distance of the atom from a wall. That is, the wall is a Hooke's Law spring pushing the atom away whenever the atom is inside the "bouncy" surface of the wall, and the wall exerts no force otherwise.

The total force on each atom is the force of gravity plus the forces from all the springs attached to it plus the force from the wall, if any. The fact that each atom has several forces on it is what gives the simulation its complexity -- an atom can be pushed or pulled in several directions simultaneously. This is also the main reason we need a computer: we have altogether 16 atoms and 42 springs, giving a total of 64 "equations of motion" to solve, with an average of 9 terms in each, to find how the system evolves in time. Blech.

Once we know the total force on an atom we can use Newton's equation to calculate the acceleration. Now the acceleration is by definition the change in velocity per unit time. Supposing we know the velocity at some time t1 and want to know it at some later time t2. If t2 is close enough to t1, then for all intents and purposes the acceleration is constant and the total change in velocity is given by multiplying the acceleration (the change in velocity per unit time) by the time interval (how long the acceleration acts):

v2 = v1 + a (t2 - t1)

Similarly the velocity is the change in position per unit time, so if we know the position r1 at time t1 we can find the position r2 at time t2 by the same trick, assuming the velocity does not change very much over the time interval:

r2 = r1 + v (t2 - t1)

(A better trick, if you are familiar with calculus, and what we actually do in the simulation, is to formally "integrate" the first equation above to get the exact result:

r2 = r1 + v1 (t2-t1) + a (t2-t1)(t2-t1) / 2

This equation does not require the velocity to be constant over the time interval. Furthermore, we can extract some subtle improvements by rearranging the order in which we calculate new velocities and positions. The art of doing this is still a frontier of research. The actual algorithm used in this simulation is called the velocity Verlet algorithm.)

An outline of the entire program for the simulation is:

  1. Establish initial positions and velocities for all the degrees of freedom. In this case that means setting the initial positions of all the atoms to result in the box being at the top of the screen, setting all the springs to be unstretched, and setting all the velocities of the atoms to zero.

  2. Calculate the total force on each atom. Initially, since the springs are unstretched, only gravity will act. After the first collision, when the springs start wiggling, then the spring forces will contribute. During the collisions the wall force acts as well.

  3. Use Newton's equation to calculate the resulting acceleration of each atom.

  4. Calculate from the accelerations the velocities at some tiny time interval later.

  5. Calculate from the velocities the positions at some tiny time interval later.

  6. Return to step 2.

We continue this calculation process for as long as we want the simulation to run. Every so often (60 milliseconds, to be exact) we draw a new picture on the screen.

The key to accuracy in the simulation is letting the time interval for each cycle be as small as possible, so that the changes in acceleration which we are ignoring during each time interval are as small as possible. The key to speed in the simulation is making the time interval as large as possible, so that the computer need not do so many calculations per unit time. Clearly a balance must be struck. Within the confines of an interpreted language like Java, the balance must tilt towards speed. With a compiled language like C or Fortran the balance can be tilted towards accuracy, and simulation can give you quite precise results.

Used in the latter way this process of "computer simulation" is widely used by modern scientists to study the behaviour of complex systems composed of many degrees of freedom. Simulations are made, for example, of enzyme molecules and nucleic acid (DNA and RNA) molecules in water, following each of the several tens of thousands of atoms in these systems over a time span of a nanosecond or so (a nanosecond is a billionth of a second) to understand the microscopic motions responsible for the chemical reactions that cause mutation and cancer, infectious disease and drug action, cell growth and physiological maturation, and so on.


Back to the Instructions