The ink-drop experiment.
An ink drop spreads through molecular random walks.
The physical idea.
Think of the walkers as independent dye molecules released in a localized ink drop. This idealized model illustrates diffusion without fluid flow, convection or molecular interactions. Each molecule takes one independent step in one of four directions: left, right, up or down. The step length is a, measured in micrometers (µm), and the simulation time between steps is Δt = 1 second. After many steps, the predicted mean-square displacement is ⟨r²⟩ = 4Dt, where D = a²/(4Δt) is diffusivity (µm²/s), t is elapsed simulation time (s), and r is displacement from the starting point (µm). These illustrative values are not measured properties of a particular ink.
This example is based on my teaching code, demo2_many_walkers_diffusion.jl. I wrote the original code to show how random molecular motion leads to diffusion. The version on this page lets you follow the molecules and compare their mean-square displacement with the diffusion equation.
Try the simulation
Choose the number of molecules and the step length, then select Start. Each sweep moves every molecule once. Changing the number, step length or seed starts a new run. Playback speed changes how quickly you watch the simulation; it does not change the physical time between steps.
Where are the molecules?
Does the spreading agree with diffusion?
Recorded simulation
The original 100,000-molecule calculation and MP4 are retained here. The saved-state viewer advances in intervals of ten simulation steps; use the live simulation above to see every step and change parameters. The recorded calculation uses one unit of length and one unit of time per step.
Open the saved-state viewer and recorded movie
Precomputed NumPy results from the downloadable code; the browser scrubs saved states. Horizontal axis: x. Vertical axis: y. 100,000 walkers; 256 × 256 histogram bins from −128 to 128; color uses a square-root scale from probability 0 to 0.001 (values above 0.001 saturate).
Download measured diagnostics (CSV)Model and numerical choices.
This example moves every molecule once per sweep on an unbounded lattice. My original teaching demonstration selects molecules asynchronously in a periodic box. The displayed histogram window is not a boundary. Mean-square displacement uses unwrapped coordinates.
Questions explored in this example.
Use the live controls above to increase the number of molecules and compare fluctuations in mean-square displacement. Then double the step length and compare diffusivity. Download the source below to run larger calculations.
Our Python calculation, explained in steps.
The Python notebook uses NumPy and writes CSV snapshots and diagnostics into the output folder.
Download the Python notebook · Read the code in steps
If you would like a similar tutorial in Julia or C, email me with the lesson title and your intended use. I can share a Google Colab notebook with step-by-step explanations and worked examples.
Email: saswata_at_msme.iith.ac.in (replace _at_ with @).
Run the notebook cells in order for the explained calculation. The complete NumPy code is shown below for reading.
Read the complete NumPy code
"""Random-walk diffusion example based on my teaching code demo2_many_walkers_diffusion.jl.
Purpose: show how random molecular steps produce diffusion.
Run: python walkers.py [output_directory]. Requires NumPy.
Each walker takes one unit cardinal step per sweep on an unbounded 2D lattice.
My original teaching code uses asynchronous moves in a periodic box.
This example moves every molecule once per sweep on an unbounded lattice.
MSD uses unwrapped coordinates; theoretical MSD=steps, D=1/4.
"""
import sys
from pathlib import Path
import numpy as np
def run(output='walkers_output'):
out=Path(output);out.mkdir(parents=True,exist_ok=True)
rng=np.random.default_rng(7);n=100000;pos=np.zeros((n,2),dtype=int)
dirs=np.array([[1,0],[-1,0],[0,1],[0,-1]]);rows=[]
for step in range(401):
if step:
pos+=dirs[rng.integers(0,4,n)]
if step%10==0:
msd=np.mean(np.sum(pos*pos,axis=1));rows.append([step,msd,step])
hist,_,_=np.histogram2d(pos[:,0],pos[:,1],bins=256,range=[[-128,128],[-128,128]])
np.savetxt(out/f'density_{step:04d}.csv',hist/n,delimiter=',')
np.savetxt(out/'diagnostics.csv',rows,delimiter=',',header='steps,msd,theory',comments='')
assert abs(rows[-1][1]/400-1)<.04
print('Walker MSD at 400 steps',rows[-1][1],'theory 400')
if __name__=='__main__':run(*sys.argv[1:2])About these calculations.
We use these examples to explain numerical methods. We provide the parameters, boundary conditions and numerical checks with the results. These are teaching calculations, not predictions calibrated for a particular material.
← All worked examplesHow we develop our codes.
We start with a small calculation, check its result and then add the next part. These lessons follow the same sequence, with explanations and checks at each stage. Open the worked Python notebook lessons