LAMMPS from scratch · 20 min
Finding your way in the code
LAMMPS is over a million lines of C++, but you never read it from the top. Four routes take you from what you see (an error message, a command, a thermo column) straight to the line that made it. You follow one of them from the Temp column to the eight lines that compute it, then recompute Temp yourself from the 4000 velocities LAMMPS dumps, and change the one setting that makes the naive formula right.
- Four routes into the code: the error location, the naming rule, the thermo keyword, grep
- Trace the Temp column to compute_temp.cpp
- Recompute T = Σ m v² / (dof · k_B) from 4000 velocities and match LAMMPS to 15 digits
- Why dof is 3N − 3, and what changes when you set it to 3N
1 · Understand: four routes in
| You have | Route | Example |
|---|---|---|
| an error or a warning | read the end of the message: every ERROR and WARNING carries the file and line that raised it (src/error.cpp:139, src/error.cpp:228) | ERROR: Illegal fix nvt command: missing argument(s) (src/fix_nh.cpp:61) |
| a command in the script | the naming rule of page B.1 | fix 1 all nve → src/fix_nve.cpp |
| a column in the thermo output | find the keyword in src/thermo.cpp, follow it to the compute it reads | Temp → compute_temp.cpp (section 2) |
| a word and nothing else | grep (page 0.2) or git grep | grep -rn "Lost atoms" src/ → src/thermo.cpp:526 |
An editor with "go to definition" (VS Code with the C/C++ extension, opened on the src folder) then jumps from any name to where it is defined. You only ever read the few lines on the path.
2 · Observe: from the Temp column to the formula
Six stops, about 20 lines read. In LJ units mvv2e = 1 and boltz = 1 (page A.4), so the code says:
T = Σ m (vx² + vy² + vz²) / dof, dof = 3N − 3
The −3: velocity … mom yes (page A.6) and fix nve keep the total momentum at zero, so 3 of the 3N velocity components are not free. Most textbooks write 3N.
3 · Match: Temp from 4000 velocities
Run melt for 100 steps, dump every velocity with 17 digits, and apply the formula you just found. Thermo is printed with 15 digits so the two can be compared.
4 · Modify: change one number in the code's path
extra_dof is set in src/compute.cpp:85, but you do not need to recompile to change it: compute_modify thermo_temp extra/dof 0 sets it from the script (src/compute.cpp:149). Tick the box above and run again. Predict first:
- Temp becomes Σ m v² / 12000 instead of / 11997: smaller by the factor 11997/12000.
- Press does not change. src/compute_pressure.cpp:256 uses
dof · boltz · T, which is Σ m v² whatever dof is. - TotEng does not change: the kinetic energy is ½ Σ m v², with no dof in it.
Reading the code told you which numbers depend on the setting and which do not, before running anything.
Your turn
1. Where does LAMMPS decide to stop with "Lost atoms"? Use route 4.
grep -rn "Lost atoms" src/ gives src/thermo.cpp:526, inside Thermo::lost_check() (line 495): it adds up the atoms on all processors every time thermo prints and compares with the number at the start. That is also why "Lost atoms" appears only at a thermo step.2. Follow the Press column the same way. Which file, and what is added to the kinetic part?
thermo_press (output.cpp:64) → src/compute_pressure.cpp:256: P = (dof·k_B·T + virial_xx + virial_yy + virial_zz) / (3V) · nktv2p. The virial is Σ r·F over pairs, summed by the pair style (page C.5).3. For 2 atoms, what Temp does LAMMPS print if each moves with speed 1 in opposite directions (m = 1, LJ units)?