Computational Physics, 1B, Term 2

Worked solution for PLANET example

| README | Planet.cc | gnu | gnu10 | gnu11 | output/sun | Planet2.cc | gnu20 | gnu21 | output/gnu0 |

Planet.cc is a worked solution for the task:
Write code that implements the Leapfrog method. Get it going for one initial condition, such as x0 = (1.5,2), v0 = (0.4,0).

Every few iterations it prints out
t, x1, x2, v1, v2, T, V, T+V
where T is the kinetic energy and V is the potential energy. When it's printed all those outputs to a file, you can plot things from the file using gnuplot.
Directories: The first C++ program, Planet.cc, will put its output in a directory called output (provided you first create that directory and then run the program following the instructions written in the comments at the top of Planet.cc). The "gnu" files (gnu | gnu10 | gnu11) go in the upper directory.


gnu is a simple gnuplot file for plotting the output of Planet.cc. It displays the positions and the velocities in a single graph.




gnu10 and gnu11 are a fancier pair of gnuplot files for making a movie from the output of Planet.cc. gnu10 sets things up, then calls gnu11, which does the plot and calls itself recursively.





The file sun contains the coordinates of the sun


example1

Planet2.cc is a much fancier solution for the task. It can handle either of two force laws (inverse square and Hooke law); and while it is running, it can print out gnuplot commands to stdout, so if you pipe its output into gnuplot, it makes a live movie. It can be modified so that it performs an Euler simulation immediately after a Leapfrog one. These extra features are not very elegant.
Directories:
For the second program (Planet2.cc), "output/gnu0" should go in the output directory. The output of the program is written to files in the "/tmp" folder, and commands for gnuplot are written to standard output (stdout). See the head of the cc file for instructions.



David MacKay
Last modified: Mon Sep 24 16:06:61 2012