LAMMPS from scratch · 15 min

Source code, compiling, running

LAMMPS is text written by people (the source code) that a compiler turns into a program your computer can run. You find the exact lines where LAMMPS computes the Lennard-Jones energy, see why changing the input needs no compiling but changing the formula does, and check a two-atom run by hand.

1 · Understand: three things, one path

Every program you use, LAMMPS included, starts as source code: ordinary text files written by people. A computer cannot run that text directly. A second program, the compiler, translates it once into machine instructions and saves the result as the program (also called the executable). For LAMMPS the program is a single file named lmp.

source codepair_lj_cut.cpp, … compileronce, minutes program lmpruns many times input scriptread while running logresults change the source → compile again · change the input → just run again

A kitchen is a good picture. The source code is a recipe book; the compiler is the training that turns a new cook into someone who knows every recipe by heart; the program is that trained cook; the input script is today's order; the log is the meal. Changing today's order is instant. Changing a recipe means training the cook again.

2 · LAMMPS in numbers

We downloaded LAMMPS (stable release 22 Jul 2025, update 6) and compiled it on a small 2-processor cloud computer. These are the real counts:

WhatNumber
source files in all of LAMMPS (.cpp)1,860
lines of C++ in all of LAMMPS (.cpp and .h)1.19 million
optional packages94
core only (no packages): files and lines363 .cpp + 382 .h, 235,476 lines
our build: core + MOLECULE, KSPACE, RIGID451 files compiled
size of the program lmp11.6 MB
time to compile everything266 s (about 4½ minutes) on 2 processors
time to recompile after changing one file3.8 s: one file compiled, then the pieces joined again (13–16 s in a git download, where a second file that records the git state is rebuilt too: page 0.6)

The last two rows matter most. Compiling everything takes minutes, but after changing one file the compiler only redoes that file and joins the pieces again, which takes seconds. That is why changing LAMMPS is practical.

3 · Where the Lennard-Jones formula lives

The force between Lennard-Jones atoms is computed in one file, src/pair_lj_cut.cpp. You do not need to know C++ to read the lines that matter: they are the formula of Lesson 1.2 written with a few abbreviations.

First, four numbers are prepared once (lines 509–512):

lj1[i][j] = 48.0 * epsilon[i][j] * pow(sigma[i][j], 12.0); lj2[i][j] = 24.0 * epsilon[i][j] * pow(sigma[i][j], 6.0); lj3[i][j] = 4.0 * epsilon[i][j] * pow(sigma[i][j], 12.0); lj4[i][j] = 4.0 * epsilon[i][j] * pow(sigma[i][j], 6.0);
CodePlain wordsMaths
*multiply×
pow(sigma, 12.0)σ to the power 12σ¹²
[i][j]for atom types i and j (here only 1 and 1)
lj1 … lj4four numbers48εσ¹², 24εσ⁶, 4εσ¹², 4εσ⁶

Then, for every pair of atoms closer than the cutoff, every step (lines 114–130):

r2inv = 1.0 / rsq; r6inv = r2inv * r2inv * r2inv; forcelj = r6inv * (lj1[itype][jtype] * r6inv - lj2[itype][jtype]); fpair = factor_lj * forcelj * r2inv; … evdwl = r6inv * (lj3[itype][jtype] * r6inv - lj4[itype][jtype]) - offset[itype][jtype];
LinePlain wordsMaths
rsqthe squared distance, computed just before: dx² + dy² + dz²r²
r2inv = 1.0 / rsqone divided by r²1/r²
r6inv = r2inv * r2inv * r2inv(1/r²) three times1/r⁶
forcelj = …the force times the distancer·F(r)
fpair = … * r2invdivide by r² once more (factor_lj is 1 here)F(r)/r
evdwl = …the pair energy (offset is 0 unless shifted)E(r)

Put the four numbers in, with ε = σ = 1:

evdwl = (1/r⁶) × (4 × (1/r⁶) − 4) = 4(r⁻¹² − r⁻⁶) the LJ energy forcelj = (1/r⁶) × (48 × (1/r⁶) − 24) = 48r⁻¹² − 24r⁻⁶ = r × F(r) fpair = forcelj / r² = F(r)/r x-force on atom i = delx × fpair = F(r) × (delx/r) (delx = x_i − x_j)

LAMMPS never takes a square root here: it works with r² and 1/r² because that is faster. offset is zero unless the energy is shifted (pair_modify shift yes), and factor_lj is 1 except for bonded neighbours in molecules.

4 · Two kinds of change

You want to change…WhereCompile again?Time
the distance between atoms, ε, σ, the cutoff, temperature, number of stepsthe input scriptnorun again, instant
what is printed, how often, which thermostatthe input scriptnorun again, instant
the formula itself, for example r⁻¹² → r⁻⁹pair_lj_cut.cppyesabout 4 s for one file
a new kind of force or thermostata new .cpp fileyesseconds to minutes

The program reads the input script while it runs, so input changes need nothing else. The formula, however, is fixed inside the program by the compiler: to change it you change the source and compile again. Part E of this section does exactly that.

5 · Try it: two atoms, live

LAMMPS runs in your browser. Change the distance, predict by hand, run, compare. This is an input change, so no compiling is needed.

Try:

Predict by hand

Input script and full log

What you just saw

Check every number in Python

Your turn

1. Which of these need LAMMPS to be compiled again: (a) a smaller time step, (b) printing the pressure every 10 steps, (c) a new way of computing temperature, (d) adding the MANYBODY package?

2. With ε = 2 and σ = 1, what are lj3 and lj4, and what energy does LAMMPS print at r = 1.1?

3. Why does LAMMPS compute r2inv and never r itself?