Lattice gas
The lattice gas is a model of a fluid in which each cell of a lattice is either empty or holds one particle, and each pair of particles in neighbouring cells has an interaction energy \(-\epsilon\), where \(\epsilon > 0\), so that the particles attract each other. Here, the lattice is a square of \(L \times L\) cells with periodic boundary conditions (its left and right edges are joined, and so are its top and bottom, so that every cell has four neighbours), and it holds \(N\) particles. The energy of a configuration is
where \(n_i\) is 1 if cell \(i\) is occupied and 0 if it is empty, and the sum is over all pairs of neighbouring cells.
With the substitution \(n_i = (1 + s_i)/2\), this energy becomes that of the Ising model of a magnet, with spins \(s_i = \pm 1\) and coupling constant \(J = \epsilon/4\), plus terms that are constant when \(N\) is fixed. Onsager's exact solution of the two-dimensional Ising model [Onsager, Physical Review 65, 117 (1944), doi:10.1103/PhysRev.65.117] therefore gives the critical temperature of the lattice gas:
Below \(T_\mathrm{c}\), a lattice gas whose density (the fraction of cells that are occupied, \(N/L^2\)) is not too close to 0 or 1 separates into two coexisting phases: a dense liquid and a dilute gas. Above \(T_\mathrm{c}\), it is a single, uniform phase.
Kawasaki dynamics
At temperature \(T\), the probability of a configuration with energy \(E\) is proportional to its Boltzmann factor, \(\exp(-E/k_\mathrm{B}T)\). A Markov chain Monte Carlo simulation samples configurations with this probability distribution by making a sequence of random changes, each of which is accepted or rejected with a probability that depends on the change in energy. In Kawasaki dynamics [Kawasaki, Physical Review 145, 224 (1966), doi:10.1103/PhysRev.145.224], each change moves a particle into an empty neighbouring cell, which conserves the number of particles; in Glauber dynamics, by contrast, a single cell changes between empty and occupied. Each step of the simulation is as follows:
- Choose a cell at random, and one of its four neighbours at random.
- If one of the two cells holds a particle and the other is empty, move the particle into the empty cell with the acceptance probability \(p\) defined below; otherwise, do nothing.
If the particle's cell has \(n_1\) occupied neighbours and the empty cell has \(n_2\), including the particle's cell, the move changes the energy by
because the particle loses \(n_1\) bonds and gains \(n_2 - 1\). Since \(0 \le n_1 \le 3\) and \(1 \le n_2 \le 4\), \(\Delta E\) takes one of the seven values \(-3\epsilon, -2\epsilon, \ldots, 3\epsilon\).
The ratio of the Boltzmann factors of the new and old configurations is \(q = \exp(-\Delta E/k_\mathrm{B}T)\), and the acceptance probability is
The reverse move, which returns the particle to its original cell, has the ratio \(1/q\) and therefore the acceptance probability \(1/(1 + q)\), so the forward and reverse acceptance probabilities are in the ratio \(q\). A move and its reverse are proposed equally often, because a pair of neighbouring cells is equally likely to be chosen whichever of them holds the particle. The simulation therefore satisfies detailed balance: in the Boltzmann distribution, each move occurs as often as its reverse. This choice of \(p\) is Barker's acceptance probability [Barker, Australian Journal of Physics 18, 119 (1965), doi:10.1071/PH650119]; the Metropolis acceptance probability, \(\min(1, q)\), also satisfies detailed balance. With \(\epsilon = 0\), every move has \(p = 1/2\), and the particles perform random walks (as in the post A very simple 2-D diffusion model) in which no two particles can occupy the same cell.
Time in the simulation is measured in sweeps: one sweep is \(L^2\) steps, in which each cell is chosen once, on average, as the first of the pair.
Code
The program below simulates a lattice of \(50 \times 50\) cells holding 1,000 particles (a density of 0.4), with energies in units of \(\epsilon\) and temperatures in units of \(\epsilon/k_\mathrm{B}\). It animates the same random starting configuration side by side at \(T = 0.4\) (about \(0.7\,T_\mathrm{c}\)) and \(T = 0.8\) (about \(1.4\,T_\mathrm{c}\)), and shows the energy per particle, \(E/N\), above each lattice. The function kawasaki_sweeps calculates the acceptance probabilities for the seven values of \(\Delta E\) once, and generates the random numbers for all of its steps in three calls to NumPy's random number generator, which is much faster than generating them one step at a time.
"""A two-dimensional lattice gas simulated with Kawasaki dynamics.
The same random starting configuration is animated side by side at a
temperature below the critical temperature and at one above it.
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# Number of cells along each side of the lattice, and number of particles.
L, NPARTICLES = 50, 1000
# Temperatures, in units of eps / kB, where -eps is the energy of a pair of
# particles in neighbouring cells. The critical temperature is 0.567.
TEMPERATURES = (0.4, 0.8)
# Sweeps (L * L attempted moves) between frames, and the number of frames.
SWEEPS_PER_FRAME, NFRAMES = 20, 101
SEED = 42
# Row and column offsets to the four nearest neighbours of a cell.
NEIGHBOURS = ((-1, 0), (1, 0), (0, -1), (0, 1))
def random_lattice(rng):
"""Return an L x L lattice with NPARTICLES particles in random cells."""
lattice = np.zeros(L * L, dtype=int)
lattice[rng.choice(L * L, NPARTICLES, replace=False)] = 1
return lattice.reshape(L, L)
def occupied_neighbours(lattice, i, j):
"""Return the number of particles in the four cells next to cell (i, j)."""
return (lattice[(i - 1) % L, j] + lattice[(i + 1) % L, j]
+ lattice[i, (j - 1) % L] + lattice[i, (j + 1) % L])
def energy_per_particle(lattice):
"""Return the energy per particle, in units of eps."""
# Count each neighbouring pair once, from its upper or left-hand cell.
npairs = (np.sum(lattice * np.roll(lattice, -1, axis=0))
+ np.sum(lattice * np.roll(lattice, -1, axis=1)))
return -npairs / NPARTICLES
def kawasaki_sweeps(lattice, T, nsweeps, rng):
"""Attempt nsweeps * L**2 moves on lattice, in place, at temperature T."""
# The acceptance probability for each possible energy change,
# dE = -3, -2, ..., 3 (in units of eps).
p_accept = 1 / (1 + np.exp(np.arange(-3, 4) / T))
# For each step: a cell, one of its neighbours and a number in [0, 1).
nsteps = nsweeps * L * L
cells = rng.integers(L, size=(nsteps, 2)).tolist()
directions = rng.integers(4, size=nsteps).tolist()
rands = rng.random(nsteps).tolist()
for (i1, j1), d, r in zip(cells, directions, rands):
di, dj = NEIGHBOURS[d]
i2, j2 = (i1 + di) % L, (j1 + dj) % L
if lattice[i1, j1] == lattice[i2, j2]:
# Both cells are empty or both are occupied: nothing can move.
continue
if lattice[i2, j2]:
# Relabel the cells so that the particle is in cell 1.
i1, j1, i2, j2 = i2, j2, i1, j1
# The particle loses its bonds to the neighbours of cell 1 and gains
# bonds to the neighbours of cell 2, except cell 1, which it leaves.
n1 = occupied_neighbours(lattice, i1, j1)
n2 = occupied_neighbours(lattice, i2, j2)
dE = n1 - n2 + 1
if r < p_accept[dE + 3]:
lattice[i1, j1], lattice[i2, j2] = 0, 1
def animate(cmap="binary"):
"""Return an animation of the lattice gas at each of TEMPERATURES."""
rng = np.random.default_rng(SEED)
start = random_lattice(rng)
lattices = [start.copy() for _ in TEMPERATURES]
fig, axes = plt.subplots(1, len(TEMPERATURES), figsize=(9, 5.2),
layout="constrained")
images = []
for ax, lattice in zip(axes, lattices):
images.append(ax.imshow(lattice, cmap=cmap, vmin=0, vmax=1))
ax.set_xticks([])
ax.set_yticks([])
def update(frame):
if frame > 0:
for lattice, T in zip(lattices, TEMPERATURES):
kawasaki_sweeps(lattice, T, SWEEPS_PER_FRAME, rng)
for ax, image, lattice, T in zip(axes, images, lattices, TEMPERATURES):
image.set_data(lattice)
E = energy_per_particle(lattice)
ax.set_title(f"$T = {T}$, $E/N = {E:.2f}$")
fig.suptitle(f"{frame * SWEEPS_PER_FRAME} sweeps")
return FuncAnimation(fig, update, frames=NFRAMES, interval=100,
repeat=False)
if __name__ == "__main__":
anim = animate()
plt.show()
To save the animation as a GIF image instead of displaying it, replace plt.show() with anim.save("kawasaki.gif", writer="pillow", fps=10).
Results
At \(T = 0.4\), the particles gather into clusters within 20 sweeps, which lowers the energy per particle from −0.79 to −1.27. The clusters then merge into larger domains of liquid, separated by gas, and \(E/N\) falls more slowly, to −1.63 after 2,000 sweeps. The domains grow slowly, because particles can only hop from cell to cell, through the gas or along the edges of the domains. The energy is lowest when the liquid forms a single band across the lattice, which has the shortest possible boundary: a band of 20 complete rows has \(E/N = -1.95\). Moving particles into empty cells anywhere on the lattice, not only neighbouring ones, would also conserve their number and sample the same Boltzmann distribution, and would separate the phases sooner, but it would not model the motion of the particles.
At \(T = 0.8\), small clusters form and break up continually, and the particles remain spread over the whole lattice: after the first 100 sweeps, \(E/N\) fluctuates between −1.28 and −1.14.