First Project: A Simple N-Body Simulation

In this assignment, you are to write a program that simulates n-bodies in two dimensional space. Each body is characterized by a mass, a position, and a velocity.

The simulation proceeds by updating the position of each body followed by updating the velocity of each body. To simplify the simulation, we will assume that the gravitational constant, G, is 1. Further, each time step for the simulation is a single unit of time.

Input

The input will consist of an integer, N, that specifies the number of bodies in the simulation; N 5-tuples that specify the mass, initial position, and initial velocity for each of the bodies; and an integer that specifies the number of time steps in the simulation. As an example, the following input specifies 2 bodies. The first body has a mass of 2.5, an initial position of (3.1,4.2) and an initial velocity of (-2,4). The second body has a mass of 12.4, an initial position of (0,0), and an initial velocity of (5,2). Finally, the simulation is to run for 20 time steps.
2
2.5 3.1 4.2 -2 4
12.4
0 0
5 2
20
You may assume that the input is correct.

Output

When the simulation is complete, you should print the position for each body (you may also print the mass and/or velocity). The bodies should be printed in the order in which they were read in.

Theory

This simulation is fairly easy to code using vector notation. Using vector notation, the position and velocity for each body is given as a vector. Updating the position of a body (at the start of each time step) is accomplished by adding (using vector addition) the current velocity to the current position:
 new_position = old_position + velocity

Updating the velocity for a body is a bit more complicated. First, you need to sum the accelerations (i.e, gravity) contributed by the all of the other bodies. Second, you need to convert this acceleration to a velocity. Third, you need to combine this velocity with the current velocity of the body.

For two bodies, A and B, the acceleration that B contributes to A is given by the following formula:

         pos(B) - pos(A)       mass(B)      
accel =  --------------- * -----------------
         |pos(A)-pos(B)|   |pos(A)-pos(B)|^2

Because each step of the simulation represents a unit time, the velocity due to the acceleration contributed by all of the other bodies is the same as the acceleration.

Given the current velocity of a body and the velocity contributed by the other bodies, the new velocity for the body is the (vector) sum of these velocities.

Error Conditions

In writing this simulation, you may ignore the possibility that two objects will collide between time steps. If any two bodies are in the same place when you are computing the accelerations, you will get a divide by zero error. You should check for this error and stop the simulation with an appropriate error message should two bodies be in the same place.

Grading

Grading on this program will be based on correctness, internal documentation (comments), and the organization of your code (i.e., the use of class definitions).

Vector Operations

This assignment uses a number of vector operations.

Vector addition

<a, b> + <c, d> = <a+c, b+d>

Vector subtraction

<a, b> - <c, d> = <a-c, b-d>

Multiplication by a Scalar

s * <a, b> = <s*a, s*b>

Magnitude

|<a, b>| = sqrt( a*a + b*b )

Barney Maccabe
Last modified: Mon Apr 7 13:36:45 MDT