Computational Physics P223/P229B

Assignment #2

The goal of this assignment is to implement some building blocks to simulate a simple physical system: a single classical particle in a box. This assigment must be completed before class on Tue 21 Jan and mailed to the instructor with the Subject line "CompPhys Assignment 2".

[1] Download the files for this assignment

Create a new directory that you will use for this assignment. Download the following files into this directory:
In addition, you will need your AboutMe.java that you customized for the last assignment: copy it into your directory for this assignment along with any personal image file that you refer to in your "getPhoto" method.

[2] Compile and run with the skeleton classes

Compile the 3 classes you downloaded (Particle2D, CollisionManager, Wall2D) as well as your AboutMe class. Next, run ParticleProgram from the JAR file. Refer to Assignment1 if you are not sure how to complete these steps.

Running the program should produce a window that looks something like this:

Skeleton program window

The red ball represents the classical particle that you will simulate. Experiment with the "Run", "Pause" and "Restart" buttons: these control the time evolution of your model. At the moment, their only visible effect will be to change the model time displayed in the bottom right corner. The slider lets you speed up or slow down model time relative to real time by a factor of 10%-200%.

Visit the "Config" tab. You should see something like this:

Configuration tab

At the moment, these 2 sliders will not have any visible effect. But as you start to implement the misssing pieces in your skeleton programs, you will find them useful for debugging. Finally check that the "About" tab shows the information from your customized "AboutMe" class.

[3] Edit the Particle2D class

In order to get the particle moving in the simulation model, you must implement the "doUpdate" method of Particle2D to update the particle's postion (xPosition,yPosition) and velocity (xVelocity,yVelocity) variables, given the components of a force (xForce, yForce) being applied for an interval (timeStep). You can assume that the time interval is short enough that a linear solution is OK. The units of distance are pixels and the units of time are seconds.

Your implementation will probably include lines like this:

    xVelocity= ...fill this in... ;
    yVelocity= ...fill this in... ;
    xPosition= ...fill this in... ;
    yPosition= ...fill this in... ;

Once this is working, your particle should start travelling diagonally down to the right and leave the window after about 6 seconds of elapsed model time. You will see a thin red line that traces out the path the particle has followed. Your window should look something like this after about 6 seconds of model time:

Model without bounce

If you are having trouble, a simple debugging technique you might find useful is to slow down the model update rate from the default 10 Hz to 1 Hz (on the "Config" tab), and then add some text output that is printed for each update. For example:

    System.out.println("The new velocity is " + xVelocity + "(x) , " + yVelocity + "(y)");

When you have uniform motion working correctly, turn on some gravity at the "Config" tab to generate a non-zero yForce and check the results.

Complete your editing of Particle2D.java by implementing the "getXMomentum", "getYMomentum" and "applyImpulse" methods, following the directions in the comments. You will not be able to check that these operate correctly until you complete the next part of the assignment, but at least check that they compile correctly.

[4] Edit the CollisionManager class

In order to get the particle to bounce off the walls of its container, you will now implement two methods of the CollisionManager class. Both of these methods have the same name (nextCollisionTime) but take different arguments: this is legal in most Object-Oriented (OO) languages and is actually a powerful feature usually known as "overloading".

Start by implementing the version of "nextCollisionTime" that takes single Particle2D and Wall2D objects as inputs, according to the specifications in the comments. Refer to the Wall2D.java that you downloaded earlier for information on using Wall2D objects. Hint: you will probably find the getXNorm( ) and getYNorm() methods useful.

Next implement the other version of "nextCollisionTime" that takes a set of particles and walls as inputs. This version should loop over (particle,wall) pairs and then delegate most of its work to the first version, eg, with method calls like:

    Particle2D particle = ... ;
    Wall2D wall = ... ;
    ...
    double when= nextCollisionTime(particle,wall);

The sets of particles and walls are provided as "Iterators". Refer to this page for general documetation on using the Java Iterator class. Iterators are a common OO technique for traversing the contents of a "container class" (a LinkedList in this case). In addition to returning the next collision time (if any), this method must remember the (particle,wall) pair that are responsible for the next collision: store these in the variables bouncedParticle and bouncedWall. If you forget to do this, you will get an error message when you run your program:

    Missing references to particle and/or wall for last collision

Once you are correctly calculating collision times, your program should look something like this when it runs:

Model with bounce

The hollow circles represent the locations where the particle makes contact with a wall (according to your implementation of the CollisionManager) and therefore receives an impulse "kick". Remember to double-check your Particle2D "getXMomentum", "getYMomentum" and "applyImpulse" methods now if your program is not working correctly. The debugging technique suggested above will also be helpful here.

Both of these implementations can potentially be quite inefficient. But, in general (and in this case also), you should concentrate on a clear and correct implementation. Optimization can come later, when and where it is needed.

Assessment Standards

Your edited Particle2D.java and CollisionManager.java will be assessed according to the following standards (with roughly equal weight):
Email your edited Particle2D.java and CollisionManager.java files (as attachments) to dkirkby@uci.edu with the Subject line "CompPhys Assignment 2" before next week's class (9:30am on Tue 21 Jan).

This page is maintained by D. Kirkby for the UCI Computational Physics (P223/P229B) course.