N-Body Simulation

Start: 03/31/2020
Due: 04/17/2020, by the beginning of class
Collaboration: individual
Important Links: gradle howto, style guidelines, Checklist, nbody.zip, gradesheet

This assignment was developed by Robert Sedgewick and Kevin Wayne, Princeton University. Original assignment was in Java using their stdlib library.


Goal

Write a Kotlin program using the Processing and Minim libraries 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 program’s input is a text file that contains the information for a particular universe (in SI units, using the Cartesian Coordinate system).

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 you have unzipped the provided file nbody.zip into your ~/mcs178 directory, and your shell’s current directory is ~/mcs178/nbody. See zip how-to.

To compile your program using gradle, type the following in your terminal application (linux terminal or OS X Terminal):

$ gradle compileKotlin

To execute your program using gradle, and use worlds/planets.txt as input file, type:

$ gradle run --args=worlds/planets.txt

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 one possible way to evolve the positions and velocities of the particles.

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.

Finishing touches

The first finishing touch is to play the theme to 2001: A Space Odyssey using the Minim library and the audio file src/resources/2001.wav. The second is to find a free starfield image and learn how to use it in your own project. The image should be interesting and bigger than the given starfield.jpg. In fact, its size should be as close to your computer display size as possible.

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.