How To/Suggestions


####Possible Plan We suggest you implement the algorithm for positions in the following steps.

  1. Read in the data file planets.txt using the Scanner provided by the method arguments and store the information in six arrays. Use Scanner's next() method to get the next word, nextInt() to get the next integer, and nextDouble() to get the next real number.</p>

    Each input file contains the information for a particular universe. The first value is an integer N which represents the number of bodies. The second value is a real number R which represents the radius of the universe. Finally, there are N rows, and each row contains 6 values. The first two values are the x- and y-coordinates of the initial position; the second two values are the x- and y-components of the initial velocity; the next value is the mass; the last value is a String that is the name of an image file used to display the body. As an example, the input file planets.txt contains data for the inner planets of our solar system (in SI units):

    5
    2.50e11
    1.496e11 0.000e00 0.000e00 2.980e04 5.974e24 earth.gif
    2.279e11 0.000e00 0.000e00 2.410e04 6.419e23 mars.gif
    5.790e10 0.000e00 0.000e00 4.790e04 3.302e23 mercury.gif
    0.000e00 0.000e00 0.000e00 0.000e00 1.989e30 sun.gif
    1.082e11 0.000e00 0.000e00 3.500e04 4.869e24 venus.gif

    Let px[i], py[i], vx[i], vy[i], and mass[i] be real numbers which store the current position (x and y coordinates), velocity (x and y components), and mass of planet i. Let image[i] be a string which represents the filename of the image used to display planet i. Keep in mind that some submissions have comments at the end - be sure you read only N lines of input instead of reading until the end of the file.

    Do not even think of continuing until you have checked that you read in the data correctly. To test, print the information back out using System.out.println().

  2. Plot the background starfield.jpg image. Note that StdDraw.picture(x, y, file) centers the picture on (x, y). Use StdDraw.setXscale(-R, R) and StdDraw.setYscale(-R, R) to set the boundaries of the simulation. Test that it works. Now, write a loop to plot the N bodies following your Scanner code from step 1. If all goes correctly, you should see the four stationary planets and the sun. Now, go and test it on another data file. This bit of code forms the central basis for your simulation. In the next few steps, you will add code around it and before it to simulate movement and forces.

  3. Write a loop to calculate the new velocity and position for each body using timeStep (This code goes before the plotting code you wrote in the previous step). Since we haven't yet incorporated gravity, assume the acceleration acting on each planet is zero.

  4. Add an outer loop to repeat the previous two steps for the duration of totalTime.

    To create the illusion of movement, we need to plot each body at its current position using the StdDraw.picture method and display this sequence of snapshots (or frames) in rapid succession. So, after each time step:

    1. draw the background image starfield.jpg,
    2. redraw all the bodies in their new positions, and
    3. control the animation speed using StdDraw.show().

    Test your animation. You should now see the four planets moving off the screen in a straight line, with constant velocity. Test it on another data file to be sure.

    </li>
  5. Now, calculate the net force acting on each body. You will need two additional arrays fx[i] and fy[i] to store the net force acting on planet i. First, complete the distance() and force() methods. Next, within positions(), write a loop to initialize all the net forces to 0.0. Then write two nested for loops to calculate the net force exerted by planet j on planet i. (Note: refer to the Equations page for the math.) Add these values to fx[i] and fy[i], but skip the case when i equals j. Once you have these values computed, you can use them to compute the acceleration (instead of assuming it is zero). Test your program on several data files.

  6. Your method should finish by returning a 2D array of doubles containing the x and y-positions of the *n* bodies. When you snarf the project, the method positions() comes with commented instructions on how to return the array in the format the grader expects.

  7. When you've finished, you can test if your program works correctly by running it on planets.txt. If you are using the default values time = 10,000,000 and Δt = 25,000, the simulation should show four planets orbiting the sun and stop in this position:

    result

    and should produce [this animation](http://www.cs.duke.edu/courses/cps100e/spring10/assign/nbody/nbody-planets.mov).

  8. To make the viewing experience more enjoyable, uncomment the commented line in start() to play the theme to 2001: A Space Odyssey using StdAudio and the file 2001.mid. You can also have the program instead play the theme to Superman using superman.mid.
  9. </ol>

    Your analysis

    In your README.txt, you will answer two questions about the simulation of the bodies described in planets.txt.
    1. What is the final position of the planets after 1,000,000 seconds with a timestep (i.e. Δt) of 25,000?
    2. For what values of timeStep (e.g., Δt > T), does the simulation no longer behave correctly? That is, the planets planets no longer follow their orbits around the Sun. Explain how you arrived at your answer.