Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 · Projects and Practice

Project: A Hard-Disk Gas and the Maxwell-Boltzmann Distribution

Simulate a two-dimensional gas of hard disks from a non-equilibrium start, watch the speeds relax to the 2D Maxwell-Boltzmann (Rayleigh) distribution and track Boltzmann's H function.

Why does a gas settle into the Maxwell-Boltzmann distribution? The derivation in chapter 5 assumed equilibrium. In this project you start with a deliberately non-thermal gas, in which every particle has the same speed, let elastic collisions do their work, and watch the distribution of speeds change. You will compare it with the exact two-dimensional equilibrium law and follow Boltzmann's H function as it decreases.

The model

N identical hard disks of radius r and mass m = 1 move in a square box of side 1 with perfectly reflecting walls. Disks do not interact except by elastic collisions: when two disks touch, their velocity components along the line joining their centers are exchanged, and the perpendicular components are unchanged. Total kinetic energy and momentum are conserved in each collision.

In two dimensions, the equilibrium velocity distribution is a Gaussian in each component, so the speed has the Rayleigh distribution:

Text
F(v) = (v / σ²) exp( -v² / (2σ²) ),       σ² = kT/m = ⟨v²⟩/2

(In 2D the average kinetic energy per particle is kT, because there are two quadratic terms, so ⟨v²⟩ = 2kT/m.) The initial state has all speeds equal to 1, which is a delta function and far from Rayleigh. Equilibrium should be reached after each disk has collided a few times.

This is a simplified, time-stepped simulation: it checks for overlaps every small step dt rather than finding exact collision times. With a small dt and a low packing fraction it conserves energy to a fraction of a percent and is entirely adequate for the distribution of speeds. Research codes use event-driven molecular dynamics to be exact.

Step 1: simulate and compare with the Rayleigh law

The code places 196 disks on a 14 × 14 grid with random directions and unit speeds, advances 3000 steps and gathers speeds from the second half of the run. The area fraction is N π r² = 196 π (0.012)² ≈ 0.089, a dilute gas.

Python Relaxation of speeds to the Rayleigh distribution

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 statistic ⟨v⁴⟩/⟨v²⟩² is a sensitive fingerprint: it equals 1 for the initial state (all speeds equal) and exactly 2 for a Rayleigh distribution. The simulation should give a value close to 2 (within statistical error), the mean speed matches the Rayleigh prediction, and the histogram shows the characteristic shape, with a tail reaching speeds well above the initial value of 1. In the run above ⟨v⁴⟩/⟨v²⟩² comes out near 1.93. Energy is conserved to within a small drift caused by the time-stepping approximation.

Step 2: the H function

Boltzmann defined H(t) = ∫ f(v,t) ln f(v,t) d²v, a measure that decreases as a gas approaches equilibrium (it is minus the entropy up to constants) and stays constant afterward apart from fluctuations. With speeds binned into a histogram f with bin width Δv, a 1D estimator of the same idea is H = Σ f ln f Δv. At fixed total energy, H should fall from a very large value (a delta function has f ln f huge) to the Rayleigh value.

Python Boltzmann's H function falls and then fluctuates

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.

H starts at a positive value (about 1.8 in this run) because all speeds fall in a single bin, then drops quickly to about -0.5 and afterward hovers near the equilibrium value of about -0.6 (the binned estimator is slightly biased by the finite number of disks) with small fluctuations (a finite gas does not satisfy the H-theorem exactly; it holds on average). This is the microscopic content of the second law: starting from a special state, almost all later states are close to the equilibrium distribution.

Extensions

  1. Increase the packing fraction (larger r) and see how the Rayleigh law still holds while the pressure deviates from the ideal-gas value NkT/A.
  2. Start with two groups of disks at different speeds and watch them mix to a single temperature.
  3. Compute the pressure from the momentum transferred to the walls and compare with P A = N kT.
  4. Measure the mean free path as a function of density and compare with the kinetic-theory estimate for dilute gases.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which distribution describes the speeds in a 2D gas at equilibrium?

    Each velocity component is Gaussian; the speed of a 2D Gaussian vector follows the Rayleigh law.
  2. 2. In the hard-disk simulation, an elastic collision between equal masses does what to the velocity components?

    That conserves momentum and kinetic energy for equal masses.
  3. 3. What does the ratio ⟨v⁴⟩/⟨v²⟩² equal for a Rayleigh distribution?

    For 2D Gaussian velocities, ⟨v⁴⟩ = 8σ⁴ and ⟨v²⟩² = 4σ⁴.
  4. 4. What does the H function do as a gas approaches equilibrium?

    H is minus the entropy up to constants, and a finite gas fluctuates slightly about equilibrium.

Summary

  • A gas started with a delta function in speed relaxes to the equilibrium distribution within a few collisions per particle.
  • In 2D the equilibrium speed law is the Rayleigh distribution; ⟨v⁴⟩/⟨v²⟩² = 2 distinguishes it from the initial state.
  • Boltzmann's H falls and then fluctuates around its equilibrium value: the microscopic picture of irreversibility.
  • Time-stepped collision handling is approximate; event-driven simulation is the exact method.

You have now followed statistical mechanics from counting states to simulating phase transitions and gases.

Next: Back to the Statistical Mechanics overview