N-Body Simulation

Start: 10/9/2018
Due: 10/19/2018, by the beginning of class
Collaboration: individual
Important Links: style guidelines, Checklist, nbody.zip, stdlib.jar, gradesheet

This assignment was developed by Robert Sedgewick and Kevin Wayne, Princeton University. Original language was Java.


Goal

Write a program to simulate the motion of n particles in the plane, mutually affected by gravitational forces, and animate the results. Such methods are widely used in cosmology, semiconductors, and fluid dynamics to study complex physical systems. Scientists also apply the same techniques to other pairwise interactions including Coulombic, Biot–Savart, and van der Waals.

Context

In 1687, Isaac Newton formulated the principles governing the motion of two particles under the influence of their mutual gravitational attraction in his famous Principia. However, Newton was unable to solve the problem for three particles. Indeed, in general, solutions to systems of three or more particles must be approximated via numerical simulations.

Program specification

Write a Kotlin program Nbody.kt that

Input format

The input format is a text file that contains the information for a particular universe (in SI units).

As an example, planets.txt contains real data from part of our Solar System. Input Format

Getting started

Before you begin coding, do the following.

Compiling and executing your program

I’m assuming

  1. You have set up your environment variable CLASSPATH to include your current directory and path name to the file stdlib.jar, and also alias kotlinc and kotlin. See CLASSPATH how-to.
  2. You have unzipped the provided file nbody.zip into your current directory. See zip how-to.

To compile your program from the command line, type the following in your terminal application (linux terminal, SWL bash, or OS X Terminal):

$ kotlinc Nbody.kt

To execute your program from the command line, and redirecting from the file planets.txt to standard input, type:

$ kotlin NbodyKt 157788000.0 25000.0 < nbody/planets.txt

(I’m assuming throughout the writeup that you have unzipped the given nbody.zip into the directory where you are working on the source code for this project.) After the animation stops, your program must output the final state of the universe in the same format as the input, e.g.:

5
2.50e11
 1.4925e+11 -1.0467e+10  2.0872e+03  2.9723e+04  5.9740e+24    earth.gif
-1.1055e+11 -1.9868e+11  2.1060e+04 -1.1827e+04  6.4190e+23     mars.gif
-1.1708e+10 -5.7384e+10  4.6276e+04 -9.9541e+03  3.3020e+23  mercury.gif
 2.1709e+05  3.0029e+07  4.5087e-02  5.1823e-02  1.9890e+30      sun.gif
 6.9283e+10  8.2658e+10 -2.6894e+04  2.2585e+04  4.8690e+24    venus.gif

Simulating the universe: the physics

We review the equations governing the motion of the particles, according to Newton's laws of motion and gravitation. Don't worry if your physics is a bit rusty; all of the necessary formulas are included below. We'll assume for now that the position (px, py) and velocity (vx, vy) of each particle is known. In order to model the dynamics of the system, we must know the net force exerted on each particle.

Simulating the universe: the numerics

We use the leapfrog finite difference approximation scheme to numerically integrate the above equations: this is the basis for most astrophysical simulations of gravitational systems. In the leapfrog scheme, we discretize time, and update the time variable t in increments of the time quantum Δt (measured in seconds). We maintain the position (px, py) and velocity (vx, vy) of each particle at each time step. The steps below illustrate how to evolve the positions and velocities of the particles.

  1. Calculate its acceleration (ax, ay) at time t using the net force computed in Step A and Newton's second law of motion: ax = Fx / m, ay = Fy / m.
  2. Calculate its new velocity (vx, vy) at the next time step by using the acceleration computed in (i) and the velocity from the old time step: Assuming the acceleration remains constant in this interval, the new velocity is (vx + Δt ax, vy + Δt ay).
  3. Calculate its new position (px, py) at time t + Δt by using the velocity computed in (ii) and its old position at time t: Assuming the velocity remains constant in this interval, the new position is (px + Δt vx, py + Δt vy).

Do not interleave steps A and B(iii); otherwise, you will be computing the forces at time t using the positions of some of the particles at time t and others at time t + Δt. The simulation is more accurate when Δt is very small, but this comes at the price of more computation.

Creating an animation

Draw each particle at its current position to standard drawing, and repeat this process at each time step until the designated stopping time. By displaying this sequence of snapshots (or frames) in rapid succession, you will create the illusion of movement. After each time step, draw the background image starfield.jpg; redraw all the particles in their new positions; and control the animation speed (about 40 frames per second looks good).

You will call several methods from the StdDraw library.

Optional finishing touch

For a finishing touch, play the theme to 2001: A Space Odyssey using the StdAudio library and the audio file 2001.wav. It's a one-liner using the method StdAudio.play().

Challenge for the bored

There are limitless opportunities for additional excitement and discovery here. Create an alternate universe (using the same input format). Try adding other features, such as supporting elastic or inelastic collisions. Or, make the simulation three-dimensional by doing calculations for x-, y-, and z-coordinates, then using the z-coordinate to vary the sizes of the planets. Add a rocket ship that launches from one planet and has to land on another. Allow the rocket ship to exert force with the consumption of fuel.

Gradesheet

We will use this gradesheet when grading your work.

Submission

Zip up your Nbody.kt and the completed readme.txt files and submit via Moodle. See zip how-to for how to zip up files.