N-Body Simulation Checklist

Goals

FAQ

Learn the Processing library functions for displaying images, the Minim library functions for playing audio files, and familiarize yourself with the command line.

For help with reading data from an input file, review the program Students.kt. Compile and execute to make sure your system is configured properly. You can use studentsData.txt as input file.

For help with displaying images, review BouncingBall.kt. The Processing library has many functions for manipulating images, but for this assignment, you will need only those used in BouncingBall.kt. Here is a zipped gradle project BouncingBall.zip. Save and unzip it into your ~/mcs178 directory, then do cd ~/mcs178/BouncingBall, followed by gradle run to make sure your system is configured properly.

For help with playing music, review testMinim.kt. Here is a zipped gradle project PlayMusic.zip. Save and unzip it into your ~/mcs178 directory, then do cd ~/mcs178/PlayMusic, followed by gradle run to make sure your system is configured properly.

This program is more involved than those you wrote in previous projects, so you should budget more time accordingly. The best advice we can give you is to carefully test, debug, and re-test your code as you write it. Do not attempt to write the whole program at once—if you do, then you will have no idea where to find the errors when the program doesn't work. Proactive testing will save you enormous amounts of time in the long run.

Yes, or you will lose points.

The java.util.Scanner class documentation is here.

They are avaiable through these links: Processing and Minim.

See an example in BouncingBall.kt.

See an example in testMinim.kt.

Simulation

No, as indicated in the assignment, you must separate Step A (calculate all of the forces) from Step B(iii) (update the positions); otherwise, you will be computing the forces at time t using the velocities and positions of some of bodies at time t and others at time t + Δt.

It's the change in x-coordinate between two bodies (not between a body at time t and at time t + Δt).

Errors and Bugs

Did you take into consideration the fact that the numbers in the input file are in Cartesian Coordinate system, but the Processing library uses a different coordinate system?

Make sure that you get the sign right when you apply Newton's law of gravitation: (x[j] - x[i]) vs. (x[i] - x[j]). Note that Δx and Δy can be positive or negative so do not use kotlin.math.abs(). Do not consider changing the universal gravitational constant to patch your code!

Testing and Debugging

Inserting print statements is a good way to trace what your program is doing. Our input files were created with the following println() statements.

println(n)
println("%.2e".format(radius))
for (i in 0 until n) {
    println("%11.4e %11.4e %11.4e %11.4e %11.4e %12s"
        .format(px[i], py[i], vx[i], vy[i], mass[i], image[i]))
}

As indicated in the collaboration policy, you may not copy or adapt code (except from the course materials). Moreover, you need to cite any code that you copy or adapt (except for code provided with the assignment). So, you are free to copy or adapt this code fragment, with or without citation.

Enrichment

It's the fanfare to Also sprach Zarathustra by Richard Strauss. It was popularized as the key musical motif in Stanley Kubrick's 1968 film 2001: A Space Odyssey.

The leapfrog method is more stable for integrating Hamiltonian systems than conventional numerical methods like Euler's method or Runge–Kutta. The leapfrog method is symplectic, which means it preserves properties specific to Hamiltonian systems (conservation of linear and angular momentum, time-reversibility, and conservation of energy of the discrete Hamiltonian). In contrast, ordinary numerical methods become dissipative and exhibit qualitatively different long-term behavior. For example, the earth would slowly spiral into (or away from) the sun. For these reasons, symplectic methods are extremely popular for n-body calculations in practice.

Here's a more complete explanation of how you should interpret the variables. The classic Euler method updates the position uses the velocity at time t instead of using the updated velocity at time t + Δt. A better idea is to use the velocity at the midpoint t + Δt / 2. The leapfrog method does this in a clever way. It maintains the position and velocity one-half time step out of phase: At the beginning of an iteration, (px, py) represents the position at time t and (vx, vy) represents the velocity at time t − Δt / 2. Interpreting the position and velocity in this way, the updated position (px + Δt vx, py + Δt vy). uses the velocity at time t + Δt / 2. Almost magically, the only special care needed to deal with the half time-steps is to initialize the system's velocity at time t = −Δt / 2 (instead of t = 0.0), and you can assume that we have already done this for you. Note also that the acceleration is computed at time t so that when we update the velocity, we are using the acceleration at the midpoint of the interval under consideration.

Yes, Sundman and Wang developed global analytic solutions using convergent power series. However, the series converge so slowly that they are not useful in practice. See The Solution of the N-body Problem for a brief history of the problem.

N-body simulations play a crucial role in our understanding of the universe. Astrophysicists use it to study stellar dynamics at the galactic center, stellar dynamics in a globular cluster, colliding galaxies, and the formation of the structure of the Universe. The strongest evidence we have for the belief that there is a black hole in the center of the Milky Way comes from very accurate n-body simulations. Many of the problems that astrophysicists want to solve have millions or billions of particles. More sophisticated computational techniques are needed.

The same methods are also widely used in molecular dynamics, except that the heavenly bodies are replaced by atoms, gravity is replaced by some other force, and the leapfrog method is called Verlet's method. With van der Waals forces, the interaction energy may decay as 1/R 6 instead of an inverse square law. Occasionally, 3-way interactions must be taken into account, e.g., to account for the covalent bonds in diamond crystals.

Kepler's Orrery uses an n-body simulator to compose and play music.

Here's a wealth of information on n-body simulation.

Here are some interesting two-body systems. Here is beautiful 21-body system in a figure-8 —reproducing this system (in a data file in our format) will earn you a small amount of extra credit.