CS181 - Fall 2006
Problem Set 2 -- Simulating particles

This is a two-part programming assignment. The first part is due at 11:30PM on Tuesday, October 10 (right before fall break), and the second is due at 11:30PM on Tuesday, October 17 (right at the end of fall break). The goal is to get some experience writing MPI code, and to get a sense of different issues that come up in simulations.

You should submit everything by making it available on a web page and emailing me a pointer. When I get to work on October 11 and October 18 I will print out your writeups for the two parts.

Scientific codes which simulate a physical process often depend on having some kind of model of the physical process, discretizing that model so that it can be simulated on a computer, then implementing it correctly and efficiently. When running on parallel machines, the implementation issues can become quite complex. In this assignment you'll get to see several aspects of the overall process.

The physical process we will consider is that of particles moving subject to various forces.

• particles.c
• util.h
• Makefile

• ### Part I: Drifting particles

We would like to simulate the motion of several particles under the effect of Newton's law (F=ma), and a current. Each particle has a mass, as well as an initial position and velocity.

If you look at particles.c, you'll see that it takes multiple time steps and approximates the new location and velocity of each particle at each time step using Euler's method. If you look to see how the code is parallelized, you'll find that it's rather trivial . . . each processor updates the location of its set of particles. The only communication is in distributing the particles' initial location and velocity, and in computing the new time step (question to consider: why isn't the time step a constant?).

If you compile the code using the Makefile and then run it as follows:

```% mpirun -np 1 ./particles
```
you should see a nice display showing the movement of the particles. (To get the display you may need to 'ssh -X' into a different machine from the one you're on.) If you run it again on two processors:
```% mpirun -np 2 ./particles
```
the overall picture should look similar, but now in two colors (each processor plots its particles in a different color).

To do:

• In general, when a visual display isn't possible, people use numbers to get a sense of what's happening. Modify the code so that processor 0 prints the current time, the current delta-time, and the root-mean-square (square root of the average of the velocities squared) of the velocity of all the particles after each step. (This is version 1 of the code.) Do a simulation with 10 particles for 5 seconds using the initialization values in the original code. Save the output so that you can turn it in with the writeup.
• How do you test code such as this to see if it really simulates the physical process described? In other words, what starting configurations would you test for the particles and what behaviour would you expect to see? Test out some of these cases and describe what happens.
• Remove (comment out) the display code, change the number of particles and/or duration, and do some timing runs. Turn in a plot and an explanation/description of what you tried and what you observe.
• Do at least one of the following; the result is version 2 of the code.
• improve the output (text or graphics) to improve the look and/or to make it easier to verify whether the simulation is accurate.
• Euler is not considered a particularly accurate method. Improve the accuracy of the simulation by changing the method to the 2nd-order Runge-Kutta (aka midpoint) method. See here and here (under "Integration Methods") for details on the Euler and midpoint methods.
• motivate and make some other improvement.
• To submit:

Turn in commented code for both version 1 and version 2 (described above), together with a Makefile and any supporting files (eg, header files), and a writeup addressing the items above.

### Part II: Attracting particles

Now we're going to modify the simulation. In this case not only do the particles move according to Newton's law and a current, but they also attract each other with a force that's similar to gravity in that it's also proportional to the inverse square of the distance between them. Note that every particle is affected by every other particle, although it's affected more by those that are closer.

We'll use the following formula for the force that two particles feel towards each other (in the x and y directions):

```   F_attract_x = dist_x(part_1,part_2) * (G*mass_1*mass_2 / dist(part_1,part_2))
F_attract_y = dist_y(part_1,part_2) * (G*mass_1*mass_2 / dist(part_1,part_2))
```
For the purposes of this problem, we'll define the constant G to be .01.

To do:

• Write MPI code that performs this simulation.

Note that, just as in the previous case, you'll be taking small time steps and updating the position and velocity of each particle at each time step. You may use any method to calculate the update.

• To submit:

Turn in the commented code as well as a writeup explaining:

• How you parallelized the code (this is the most important part).
• How you verified that the code worked.
• At least one speedup plot.
• More sophisticated parallelization techniques that you would have liked to have tried.