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.

1 · Understand: four routes in

You haveRouteExample
an error or a warningread 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 scriptthe naming rule of page B.1fix 1 all nve → src/fix_nve.cpp
a column in the thermo outputfind the keyword in src/thermo.cpp, follow it to the compute it readsTemp → compute_temp.cpp (section 2)
a word and nothing elsegrep (page 0.2) or git grepgrep -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:

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.

2. Follow the Press column the same way. Which file, and what is added to the kinetic part?

3. For 2 atoms, what Temp does LAMMPS print if each moves with speed 1 in opposite directions (m = 1, LJ units)?