Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Applications in Finance and Science

Diffusion and Percolation

Simulate diffusing particles and measure the diffusion coefficient, then explore percolation on a grid and find the critical threshold near 0.593.

Physics, chemistry and materials science use Monte Carlo to understand collective behavior built from simple random rules. Two classic examples are diffusion (how a drop of ink spreads) and percolation (how a liquid seeps through porous rock, or how a forest fire jumps from tree to tree). Both are easy to simulate and both show surprising large-scale patterns.

Diffusion: random walkers

Imagine many particles, each taking random steps independently. The details of one particle are unpredictable, but the cloud of particles spreads in a regular way. In two dimensions, if each coordinate gets a normal step with standard deviation ฯƒ per time step, then after n steps the mean squared distance from the start is

Text
โŸจRยฒโŸฉ = 2ฯƒยฒ ยท n

since each of the two coordinates contributes ฯƒยฒยทn. In physics the relation is written โŸจRยฒโŸฉ = 4ยทDยทt in two dimensions, where D is the diffusion coefficient (units of mยฒ/s) and t is time. Matching the two, a time step ฮ”t with step standard deviation ฯƒ corresponds to D = ฯƒยฒ / (2ยทฮ”t).

A key signature: mean squared displacement grows linearly in time, so a plot of โŸจRยฒโŸฉ against t is a straight line whose slope gives D.

Python Measure the diffusion coefficient from simulated particles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simulated curve hugs the theoretical straight line. The same trick, fitting the slope of mean squared displacement, is how physicists extract D from molecular dynamics simulations and from tracked particles under a microscope.

If particles are confined by walls, or pushed by a drift, the mean squared displacement stops being a straight line. Deviations from linear growth are a diagnostic: sub-linear growth suggests obstacles or trapping, super-linear growth suggests active transport.

Percolation: connectivity on a grid

Take a square grid. Make each cell open with probability p (a "hole" through which fluid can pass) and blocked otherwise, independently. Open cells that touch along an edge form clusters. The question: for what p does a cluster span the whole grid from top to bottom?

For small p the clusters are small islands and nothing spans. For large p an open path nearly always exists. Surprisingly, the change is sharp. For an infinitely large square lattice with this "site" rule, there is a critical probability p_c โ‰ˆ 0.5927. Below it, no infinite cluster; above it, an infinite cluster appears. On a finite grid the transition is smoothed but narrows as the grid grows.

We label clusters with scipy.ndimage.label, which assigns an integer to each connected group of open cells (using 4-neighbor connectivity by default), then check whether a label appears both in the top row and the bottom row.

Python Does a cluster span the grid?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fraction climbs from near 0 to near 1 in a narrow window around 0.59. Try size = 20 and size = 120 to see the window narrow as the grid grows.

Seeing the clusters

A picture makes the idea concrete. The plot below colors each cluster differently, just below and just above the threshold.

Python Clusters just below and above the threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At p = 0.50 many small, separate clusters; at p = 0.65 one giant cluster dominates, a single color sweeping across the whole grid.

Why percolation matters

Percolation is a model of phase transitions: a small change in a parameter causes a sudden qualitative change. It appears in:

  • Porous materials: whether oil or water can flow through rock.
  • Epidemics and networks: whether an infection reaches a large part of the population.
  • Electrical conduction: a composite conducts only once enough conducting particles connect.
  • Forest fires: whether a fire front can cross a region.

The threshold p_c depends on the lattice and the rule. For site percolation on the square lattice it is about 0.5927; for bond percolation on the same lattice it is exactly 1/2. Near p_c the cluster sizes obey power laws, one of the most studied examples of universality in physics.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In 2-D diffusion, mean squared displacement grows:

    <R^2> = 4Dt, a straight line in time; the typical distance grows like the square root of time.
  2. 2. If each coordinate step has standard deviation sigma and time step dt, the 2-D diffusion coefficient is:

    MSD after n steps is 2 sigma^2 n = 4 D t with t = n dt, so D = sigma^2 / (2 dt).
  3. 3. For site percolation on an infinite square lattice, the critical probability is about:

    p_c is approximately 0.5927 for site percolation; bond percolation on the square lattice has exactly 0.5.
  4. 4. As the grid gets larger, the transition from no spanning cluster to a spanning cluster:

    Finite-size effects blur the transition; the window shrinks as the grid grows.

Summary

  • Independent random steps produce diffusion: mean squared displacement grows linearly in time, with slope 4D in two dimensions.
  • Fit the slope of simulated MSD to estimate the diffusion coefficient D = ฯƒยฒ/(2ฮ”t).
  • Percolation asks when random open cells connect across a grid; labeling clusters with scipy.ndimage.label finds them.
  • On the square lattice the site-percolation threshold is about 0.5927, a sharp transition that sharpens with grid size.

Next chapter: Project: Scheduling Under Uncertainty