Programming Assignment Checklist: N-Body Simulation

Goals

Frequently Asked Questions

What's the easiest way to copy the nbody directory from the COS 126 ftp site to my computer?

DrJava doesn't let me redirect standard input. What should I do instead? Use the command-line. Here are instructions for configuring your system to use the command-line: [ Windows · Mac · Linux ]

What does the following error mean?

class file has wrong version 49.0, should be 48.0
You are running Java 1.4, not 1.5. You need 1.5 to use StdIn and StdDraw. Please follow the command-line instructions.

How do I plot a picture using StdDraw? The command StdDraw.picture(x, y, filename) plots the image in the given filename (either JPEG, GIF, or PNG format) on the canvas, centered on (x, y).

Do I need to write my own functions (static methods)? It's not required. If you want to modularize your program, here are two good ones.

// return Euclidean distance between (x1, y1) and (x2, y2)
public static double distance(double x1, double y1, double x2, double y2)

// return the magnitude of the gravitational force between two bodies
// of mass m1 and m2 that are distance r apart
public static double force(double m1, double m2, double r)

My computer doesn't display the animation smoothly. Is there anything I can do? Use StdDraw.show(30) to turn on the animation mode of standard draw. Call it once at the end of each time step, not after each each drawing command.

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.setXscale() and StdDraw.setYscale() to change the coordinate system to use the physics coordinates instead of the unit box.

My planets don't move. Make sure that you are using a large enough value of Δt (we specify 25000, but you might want a smaller one when debugging).

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. 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: (x[j] - x[i]) vs. (x[i] - x[j]). Note that Δx and Δy can be positive or negative so do not use Math.abs(). 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 (not exponentiation).

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

When I compile NBody.java, it says "cannot resolve symbol StdDraw." Any thoughts? Be sure you have either StdDraw.java or StdDraw.class in the current directory.

How do I submit the extra credit? Submit the files as usual using Moodle. Document what you did in your readme.txt file.

How do I change my directory to the H: drive from the Windows Command Prompt? Type H: at the command prompt. Then cd to the appropriate directory.

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-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 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