Computational Physics, 1B, Term 2

Worked solution for PLANET example USING CLASSES

| README | planet.cc | gnu | sun | all.zip |

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

It is written in advanced C++, going beyond what's expected in the 1B course. It defines classes (see page 61 of the first tutorial) for particles. It uses a structure called xyvect to hold 2-dimensional vectors. This structure definition defines how to do arithmetic with these objects, thus permitting the simulation code to contain elegant statements such as
    x += v * dt;
and
    v += a * dt;
Such human-friendly statements are not possible in many older languages such as C.
In the class approach to the particle, all the functions associated with the particle are put within the definition of the particle.
The program writes the trajectory to stdout, so the intended usage is something like:
 planet > tmp
The printout contains
t, x1, x2, v1, v2, r, theta, J, 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.
For example
gnuplot
load 'gnu'
 


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

[an error occurred while processing this directive]

The file sun contains the coordinates of the sun
[an error occurred while processing this directive]
[an error occurred while processing this directive]
example


David MacKay
Last modified: Wed Nov 14 10:11:32 2007