CS21B Lab 9: Simulating Diffusion

Due 11:59pm Tuesday, April 12, 2011

Run update21b to create the cs21b/labs/09 directory. Then cd into your cs21b/labs/09 directory and create the python program for the lab in this directory (handin21b looks for your solutions here).


You will use object-oriented programming and the graphics library to create a visual simulation of diffusion. The simulation will begin with two types of particles each concentrated in one half of the graphics window. Through random motion the particles will eventually become uniformly distributed throughout the graphics window, as shown below.

Create your solution incrementally. First define and test a class for representing the particles. Next define and test a class for simulating diffusion.

1. Define the Particle class

Define the Particle class in the file particle.py. You will need to define the following methods within the Particle class:

2. Test the Particle class

Once you are satisfied that the Particle class is working properly, move on to the next step.

3. Define the Diffusion class

In the file diffusion.py define the following methods within the Diffusion class:

4. Test the Diffusion class

Optional extension

If you'd like to simulate diffusion across a membrane, you can add a series of rectangle objects down the center of the window containing a number of gaps larger that the diameter of the particles. You'll have to figure out a way to make the particles bounce off the membrane if they contact it.


Once you are satisfied with your program, hand it in by typing handin21b in a terminal window.