Lesson 6.1 · 14 min

Radial distribution function

The radial distribution function g(r) tells you how likely you are to find a neighbour at distance r, compared with a random (ideal-gas) arrangement. It is the first thing to compute from any trajectory: it shows the structure, gives coordination numbers and can be compared directly with X-ray and neutron scattering.

The idea

Sit on one atom and ask: how many neighbours are at distance r? Count the atoms in a thin spherical shell between r and r + dr, and divide by the number you would expect if the atoms were scattered at random with the same density ρ (an ideal gas). That ratio is the radial distribution function:

g(r) = (neighbours in the shell) / (ρ · 4πr² dr)

Average over every atom as the centre and over many frames, and g(r) becomes smooth.

Reading it (code tab, Part 2; Lennard-Jones liquid near its triple point):

Gas, liquid, solid. A dilute gas has one broad peak, close to e−u(r)/kT. A liquid has a few decaying peaks. A crystal has sharp peaks at the lattice distances that never die out (Part 3).

Coordination number. Integrating ρ g(r) 4πr² up to the first minimum counts the neighbours in the first shell: about 12 for the LJ liquid and exactly 12 for fcc. For battery electrolytes this is how you count how many anion oxygens or solvent molecules surround a Li⁺ ion, using the Li–O g(r).

Potential of mean force. w(r) = −kT ln g(r) is the effective free energy between two atoms, averaged over everything else (Part 4). It differs from the bare pair potential because of the surrounding atoms.

Experiments. X-ray and neutron scattering measure the structure factor S(k), which is a Fourier transform of g(r) − 1. That makes g(r) one of the most direct checks of a force field.

Key idea. g(r) = local density at distance r / average density. Zero inside the core, peaks for the neighbour shells, 1 far away. Integrate the first peak to get coordination numbers.

The algorithm

choose dr (about 0.01-0.02 σ, or 0.002 nm); bins up to r_max ≤ L/2
h[:] = 0
for each saved frame:
    for each pair i < j:
        r = |minimum image of (x_i - x_j)|
        if r < r_max: h[floor(r / dr)] += 2        # the pair counts for both atoms
for each bin k (centre r_k):
    shell = 4/3 π ((r_k + dr/2)³ - (r_k - dr/2)³)
    g[k] = h[k] / (n_frames · N · ρ · shell)
n_1 = Σ_{r_k < r_min} 4π ρ r_k² g[k] dr               # coordination number
  1. Never go beyond half the box length: past L/2 the minimum image no longer sees every neighbour.
  2. For two species A and B, count only A–B pairs and divide by NA ρB; the coordination number of B around A uses ρB.
  3. Equilibrate first, and use uncorrelated frames (every 10–100 steps). The first peak converges fast; the tail and dilute species need many frames.
  4. Tools: GROMACS gmx rdf, LAMMPS compute rdf + fix ave/time, VMD, MDAnalysis InterRDF, and TRAVIS.