Guide · LAMMPS · 11 min read
How do I compute an RDF, MSD or diffusion coefficient in LAMMPS?
Checked against the docs · LAMMPS stable release 30 Sep 2026 and the examples/DIFFUSE README and inputs of the LAMMPS stable branch · Last checked 12 November 2026 · Spot a mistake? Tell us
Short answer
Use compute rdf with fix ave/time ... mode vector for g(r), and compute msd for the mean-squared displacement, whose slope against time is proportional to the diffusion coefficient. Both can run during the simulation or afterwards on a dump file with rerun; measure only after the run is equilibrated, and write unwrapped coordinates (xu yu zu) if you will compute the MSD from a dump.
What are the RDF, the MSD and the diffusion coefficient?
The radial distribution function, g(r), says how likely you are to find an atom at distance r from another, compared with atoms spread uniformly through the box. LAMMPS builds it as a histogram of pair distances, scaled by the count you would expect if the atoms were uniform. A g(r) of 1 means "as uniform"; peaks are shells of neighbors. Alongside it, LAMMPS gives the coordination number coord(r): the average number of neighbors within distance r.
The mean-squared displacement (MSD) is how far, on average, atoms have moved from where they started, squared: the sum of dx² + dy² + dz² over the atoms, divided by their number. LAMMPS counts atoms that cross the periodic boundaries as having moved on, not as jumping back into the box.
The diffusion coefficient D measures how fast atoms spread through the material. The manual gives two ways to it: the slope of the MSD against time is proportional to D, and so is the time integral of the velocity autocorrelation function (VACF), which compares each atom's velocity now with its velocity at the start.
When should I measure them?
After the run is equilibrated. Everything here is an average over atoms and time, and it describes the state you meant to simulate only once the system is in it. Discard the part of the run where the temperature, pressure and energy were still settling (How do I know my dynamics is equilibrated?).
Over long enough runs. The README of the manual's own diffusion example says its scripts are for illustration, with no guarantee that the systems are equilibrated or that the runs are long enough "to generate good statistics and highly accurate results". The same caution applies to yours.
Under a thermostat you have thought about. The manual's diffusion example equilibrates with fix langevin, then removes it with unfix before it measures the MSD under fix nve alone. The manual does not say which thermostats change a diffusion coefficient or by how much, so measure the way the example does, under fix nve, unless you have a reason not to. See How do I choose a thermostat and barostat?.
How do I compute the RDF?
Define the compute, then have fix ave/time average it and write it to a file. This pair is the manual's own example:
compute myRDF all rdf 50
fix 1 all ave/time 100 1 100 c_myRDF[*] file tmp.rdf mode vectorcompute myRDF all rdf 50 bins pair distances into 50 bins from 0 to the potential's cutoff. fix ave/time takes three numbers: use the values every Nevery steps, Nrepeat times, and output an average every Nfreq steps. With 100 1 100 there is no averaging; each block in tmp.rdf is one snapshot, every 100 steps. To average, raise Nrepeat: with 100 10 1000 (an example), each output averages 10 snapshots taken 100 steps apart. mode vector is needed because the RDF is an array, and c_myRDF[*] takes all its columns.
What is in the file. One row per bin: the bin's center in distance units, then a g(r) column and a coord(r) column for each pair of types you asked for.
For one pair of elements, name the types after the bin count. With oxygen as type 1 and hydrogen as type 2, compute 1 all rdf 200 1 1 1 2 2 2 gives g(r) for O–O, O–H and H–H (the manual's example compute 1 fluid rdf 500 1 1 1 2 2 1 2 2 shows the form). The first type in each pair is the central atom.
How far out it goes. By default only to the largest cutoff of your pair_style, because the RDF is built from the neighbor list. Two ways past it, both from the manual:
- the
cutoffkeyword, which costs extra neighbor lists and, beyond the force cutoff, needscomm_modify cutofflarge enough for ghost atoms out to the RDF cutoff plus the neighbor skin; - afterwards, with
rerunon a dump file andpair_style zerowith a longer cutoff, which computes no forces at all (below).
How do I compute the MSD and the diffusion coefficient?
During the run. The manual's 2D example measures the MSD and fits its slope as the run goes. Here are its lines, with what each does:
reset_timestep 0
compute msd all msd com yes
variable twopoint equal c_msd[4]/4/(step*dt+1.0e-6)
fix 9 all vector 10 c_msd[4]
variable fitslope equal slope(f_9)/4/(10*dt)
thermo_style custom step temp c_msd[4] v_twopoint v_fitslopecompute msdmeasures each atom's displacement from where it was when the compute was defined;com yessubtracts any drift of the group's center of mass first. Its fourth value,c_msd[4], is the total MSD in distance² units.reset_timestep 0makesstep*dtthe time since that reference.fix vector 10stores the MSD every 10 steps;slope()fits a straight line through the stored values by linear regression, with the points one unit apart, so dividing by10*dtturns the slope into MSD per unit time.v_twopointestimates D from the two end points;v_fitslopefrom the fitted line. The example's README says both "rapidly become roughly constant" for its run.
The script's own comment says the factor of 4 "is for 2d". In three dimensions the factor in common use is 6 (D = slope / 6), but the manual pages checked do not state it: they say only that the slope is proportional to D. Before you report a number, check the factor against the reference your group uses.
In what units. The MSD is in distance², the time in your units style's time: Ų and ps in metal units, so D comes out in Ų/ps (1 Ų/ps is 10⁻⁴ cm²/s); Ų and fs in real units, Ų/fs.
For one element. Make a group of its type and compute the MSD for that group alone (group takes type, as in the manual's group water type 3 4):
group oxygen type 1
compute msdO oxygen msd com yesFor a crystal, where atoms vibrate but do not diffuse, average yes measures displacements from each atom's average position instead of its starting one; the manual says this only makes sense when the atoms are not diffusing.
The other route: the VACF. compute vacf gives the velocity autocorrelation; fix vector stores it and the trap() function integrates it, as in the manual's in.vacf.2d example, whose comment says its factor of 0.5 is for 2D. The README notes that this gives a noisier value than the MSD for its example.
How do I compute them after the run, from a dump file?
Write the trajectory with what the analysis needs. For the MSD that means positions that are not folded back into the box: in dump custom, xu yu zu are coordinates "unwrapped" by each atom's image flags, which can lie far outside the box, while ix iy iz are the image flags themselves. Wrapped coordinates (x, xs) alone cannot tell a long jump from an atom that crossed the boundary. An example line:
dump traj all custom 1000 dump.lammpstrj id type xu yu zuRerun LAMMPS over it. rerun reads the snapshots one at a time and runs your computes on each, with no dynamics. The box must already exist (from read_data or read_restart), and charges and bonds come from that data file, since dump files do not hold them. For an RDF out to a longer distance, the manual's restrictions suggest pair_style zero with a longer cutoff:
# units, atom_style and read_data as in the original run
pair_style zero 10.0
pair_coeff * *
compute myRDF all rdf 200
fix 1 all ave/time 1000 1 1000 c_myRDF[*] file rdf.dat mode vector
rerun dump.lammpstrj dump x y z box yesdump x y z reads whichever position columns the file has (x, xs, xu or xsu, by their labels), and the file must contain atom IDs. The manual says the settings of an averaging fix such as fix ave/time must be consistent with the steps of the snapshots, or LAMMPS flags an error; here the dump example writes every 1000 steps, and the fix uses the values at those steps. The rerun page names compute msd as a use, but does not say the result matches the MSD of the run itself; compare a short stretch of both before you rely on the rerun.
Use several time origins for better statistics. The diffusion example measures from a single starting point, which lets LAMMPS estimate D during the run; its README says that for better statistics you typically want multiple time origins, post-processing the data from dump files. That is a calculation you do outside LAMMPS, in your own script.
How do I know the result is right?
For g(r):
- It should approach 1 at long distance in a uniform liquid or gas, by its definition.
- The manual says LAMMPS's definition is only appropriate for atoms uniformly distributed through the cell; for a slab, a cluster in vacuum or a single solute, g(r) is distorted, while coord(r) is still correct.
- In a molecular system, the default
special_bondssettings remove bonded pairs from the neighbor list and so from the RDF; the manual gives workarounds (tiny nonzero factors, or a rerun with differentspecial_bonds).
For the MSD and D:
- Plot the MSD against time. D comes from where it is a straight line. The manual's example fits the whole line; if the start of yours curves, fit only the straight part, and check that D does not move when you change where the fit starts.
- Compare the two-point estimate and the fitted slope, as the example does; they should agree once the MSD is linear.
- Repeat with a longer run, or a second run from different starting velocities, and see whether D moves.
- Check that the system did not drift as a whole:
com yesremoves the center-of-mass motion of the group.
What do people mix up about the RDF and MSD?
| Mix-up | What you would see | What to do |
|---|---|---|
| Measuring from step 0 of a run that was still settling | g(r) and D that mix the starting structure with the equilibrium one. | Equilibrate first, then define the computes (or set the dump range) for the production part only. |
fix ave/time without mode vector for an RDF | The default mode expects scalars, and the RDF is an array. | Add mode vector. |
Nrepeat of 1 and expecting an average | Each output is one noisy snapshot. | Raise Nrepeat so that Nrepeat × Nevery fits within Nfreq, or average the snapshots yourself. |
| An RDF cut off at the potential's cutoff | g(r) stops short, often near the first few shells for a short-ranged potential. | Use cutoff with comm_modify cutoff, or rerun with pair_style zero. |
| MSD from a dump of wrapped coordinates | Atoms that cross a boundary seem to jump back by a box length, so the MSD and D are wrong. | Dump xu yu zu, or ix iy iz with the positions. |
| Dividing the MSD slope by 4 in a 3D system | D too large by half. | 4 is for 2D in the manual's example; in 3D use 6. |
average yes for a liquid | An MSD that depends on how often it is computed. | Keep average yes for solids whose atoms do not diffuse. |
| Forgetting the time unit | D off by a factor of a thousand between metal (ps) and real (fs). | Divide by the time in your units style's unit, and say which in your write-up. |
| Continuing from a restart with a new compute ID | An MSD that is not continuous across the restart. | Use the same compute ID as the original run; the manual says this restores the reference positions. |
In Analyze, Graphs computes g(r) and the MSD from the run's loaded trajectory when you ask, with g(r) for each pair of elements, the MSD for each element and an estimate of the diffusion coefficient from the MSD's slope; check it against the method above before you report it. It works from the trajectory file the run already writes, so there is no compute to add to the script. Everything above works without it.