Skip to main content
  1. Teaching/
  2. teaching/

·4 mins

1D Diffusion Simulator

Diffusion is one of the fundamental chemical processes which cause molecules to move. It is crucial to many (if not all) biological and chemical processes.

How fast a molecule travels depends on two things: how fast the molecule diffuses, and any biasing force that might be applied to it. If we think about this in terms of real gases, the diffusion speed is dependent on how heavy the molecules are (e.g. helium diffuses faster than sulfur hexafluoride), and the biasing force corresponds to things like wind or air movement that might carry the molecules along with them.

In this assignment, you will implement a very simple simulation of particle diffusion in one dimension.

Assignment

This assignment will be your first practice in learning how to use functions. While you could implement things using only top-level code like you have done previously, future assignments will rapidly get very difficult without knowing how to write functions (plus, this assignment will be graded on whether you implement the functions correctly or not).

First, write a function called diffusion_step, which will take in three arguments:

  • Current position of the particle (LaTeX: x_n)
  • Force applied to the particle (LaTeX: dV)
  • Speed of diffusion (LaTeX: \gamma)

All three of these arguments will be floating point numbers. You should name these arguments appropriately--note that while we have used mathematical symbols here, you should use whatever you are most comfortable with (and able to type on your keyboard). The function will return the next location of the particle, which is defined by the following equation:

LaTeX: x_{n+1} = x_n + dV + \gamma \xi_n

where LaTeX: \xi_n is a random gaussian number. You can obtain such a number by using random.gauss(0,1). You will need to put import random at the top of your file for this to work.

Once you have implemented diffusion_step, implement a function called diffusion_trace. It should take in three arguments: a force and diffusion speed like diffusion_step, and a number of steps to carry out, LaTeX: N.

diffusion_trace should move the particle N times, each time by calling diffusion_step to get how far and in which direction the particle moves, then by adding this to the particle's current position and storing it. After it has executed N steps, it should return the final position of the particle.

Finally, your program should ask the user for a force, diffusion speed, and number of steps to take. Then it should print out the final position of the particle. Put all of this code into a main() function. The last thing in your .py file should be calling the main() function at the top level.

Examples

Note: the exact numbers in this example are made-up. Since you are using randomness, it is normal for your program to generate different numbers each time.

> python Diffusion.py
Please enter a force: -2.0
Please enter a diffusion speed: 0.5
Please enter a number of steps: 1000
Final position is -2.7840296522
> python Diffusion.py
Please enter a force: 0.0
Please enter a diffusion speed: 1.0
Please enter a number of steps: 100000
Final position is 7.002896489

Insight

Run your program several times with the force much larger than the diffusion speed (using both positive and negative force values). Remember that this force corresponds to something like a wind or a fan blowing on the particle. Are the results consistent with what you might expect?

Run your program several times with a force of zero and a diffusion constant of 1. Compare how far the particle gets (on average) from the origin relative to the number of steps. Does the distance increase linearly? (That is, if you double the number of steps, does the particle tend to travel twice as far?) If not, does the particle move faster or slower than linear speeds?

Submission

Submit a single file named Diffusion1D.py (pay attention to capitalization!) on Canvas. Your file needs to compile and run. It should also have a header with the following information (this goes in your source file, not in the program output):

# File: Diffusion1D.py
# Student: 
# Course: Intro to Programming
# 
# Date:
# Description of Program:
# 
# With force >> diffusion, what happens to the particle?
# With a force of 0 and a diffusion coefficient of 1, does the
# particle diffuse linearly, superlinearly, or sublinearly?

The description should be a short (1-3 sentence) description of what the program does. Do not describe how it's written!

Include docstrings for each of your functions.

You may assume that your inputs are of the correct type, but you should check them for consistency. For example, a negative number of steps is clearly wrong, and you should print a message instead of trying to carry it out.