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.
- What source code, a compiler and a program are, using LAMMPS as the example
- Where the Lennard-Jones formula lives in the LAMMPS source, line by line
- Which changes need compiling and which do not, checked with a live two-atom run
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.
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:
| What | Number |
|---|---|
| source files in all of LAMMPS (.cpp) | 1,860 |
| lines of C++ in all of LAMMPS (.cpp and .h) | 1.19 million |
| optional packages | 94 |
| core only (no packages): files and lines | 363 .cpp + 382 .h, 235,476 lines |
| our build: core + MOLECULE, KSPACE, RIGID | 451 files compiled |
| size of the program lmp | 11.6 MB |
| time to compile everything | 266 s (about 4½ minutes) on 2 processors |
| time to recompile after changing one file | 3.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):
| Code | Plain words | Maths |
|---|---|---|
* | multiply | × |
pow(sigma, 12.0) | σ to the power 12 | σ¹² |
[i][j] | for atom types i and j (here only 1 and 1) | |
lj1 … lj4 | four numbers | 48εσ¹², 24εσ⁶, 4εσ¹², 4εσ⁶ |
Then, for every pair of atoms closer than the cutoff, every step (lines 114–130):
| Line | Plain words | Maths |
|---|---|---|
rsq | the squared distance, computed just before: dx² + dy² + dz² | r² |
r2inv = 1.0 / rsq | one divided by r² | 1/r² |
r6inv = r2inv * r2inv * r2inv | (1/r²) three times | 1/r⁶ |
forcelj = … | the force times the distance | r·F(r) |
fpair = … * r2inv | divide 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:
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… | Where | Compile again? | Time |
|---|---|---|---|
| the distance between atoms, ε, σ, the cutoff, temperature, number of steps | the input script | no | run again, instant |
| what is printed, how often, which thermostat | the input script | no | run again, instant |
| the formula itself, for example r⁻¹² → r⁻⁹ | pair_lj_cut.cpp | yes | about 4 s for one file |
| a new kind of force or thermostat | a new .cpp file | yes | seconds 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.
Predict by hand
Input script and full log
What you just saw
- Every distance you tried gave the same numbers by hand and from LAMMPS: the four lines of section 3 are the whole calculation.
- At r = 1.0 the energy is exactly zero and the push is 24; at r = 21/6 = 1.122462 the force is almost zero (the bottom of the well); beyond 2.5 LAMMPS prints zero because of the cutoff.
- None of this needed compiling. Changing the 12 in r⁻¹² would.
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?
pair_coeff 1 1 1.0 1.0 2.5 to pair_coeff 1 1 2.0 1.0 2.5 in the log's input to check.3. Why does LAMMPS compute r2inv and never r itself?