Programming Assignment Checklist: N-Body Simulation

Goals

Frequently Asked Questions

The submission system says my program exceeds the 80 character limit, but I don't see which line it's on. Can you help? Program WidthChecker.java prints out all lines from standard input longer than a command line input N. Please note that all tabs are converted to 4 spaces during assignment submission.

My computer it too slow to display the animation smoothly. Is there anything I can do? Here are a few suggestions. First, be sure that you are only calling StdDraw.pause once at the end of each time step, instead of after each StdDraw.spot command. You might also try increasing the delay parameter in StdDraw.pause to prevent hoarding system resources.

Can I combine Steps 1, 2, and 3 into one massive loop? No! This will simultaneously screw up the physics and make your code harder to understand and debug.

I draw the planets, but nothing appears on the screen. Why? Use StdDraw.setScale to change the coordinate system to use the physics coordinates instead of the screen ones. Also, be sure that you call StdDraw.pause at the end of each time step.

I'm confused about all of the Δt / 2 terms. Do I need to worry about them to get the physics right? No! The update formulas for velocity and position already take this into account.

What should I use for the initial velocity in the leapfrog method? Use the value from the input file. As a technicality, the leapfrog method should be initialized with the velocity at time t = -Δt / 2, so we'll assume this is the value in the input file. In real codes, special care must be made to deal with this.

I'm a physicist. Why should I use the leapfrog method instead of the formula I derived in high school? 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. You asked!

My planets repel each other. Why don't they attract each other? Make sure that you get the sign right when you apply Newton's law of gravitation. Note that Δx and Δy can be positive or negative. Do not consider changing the universal gravitational constant G to patch your code!

How should I compute x2? The simplest way is x*x. In Java, the ^ operator means XOR (instead of exponentiation).

When I compile NBody.java, it says "cannot resolve symbol StdDraw." Any thoughts? Be sure you have StdIn.class, StdDraw.class, and Draw.class in the current directory. Also make sure you are running Java 1.4.x and not some older version.

Input, Output, and Testing

Input. Copy the nbody directory from the COS 126 ftp site to your computer. This includes the image files and many sample data files with interesting universes.

Compilation.  Your program must be named NBody.java. The capitalization is important. Compile your program with:

javac NBody.java
Note that even if you had the .java files of StdDraw and StdIn instead of the .class files, it's not actually necessary to javac StdDraw.java or StdIn.java since this happens automatically when you compile NBody.java.

Execution.  To redirect standard input from a file, execute your program with:

java NBody < planets.txt

Submission

readme.txt. Use the following readme file template and answer any questions.

Submission.  Submit NBody.java. Don't forget to hit the "Run Script" button on the submission system to test that it compiles cleanly.

Possible Progress Steps

These are purely suggestions for how you might make progress. You do not have to follow these steps. Warning: this program is more involved than the previous ones, and you should budget more time accordingly. The best advice we can give you is to carefully test, debug, and re-rest 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 error if the program doesn't work. We promise that proactive testing will save you enormous amounts of time in the long run. Trust us! Also, if you get stumped or frustrated on some portion of the assignment, you should not hesitate to consult a preceptor.

Enrichment


COS 126 Assignments
Kevin Wayne