LAMMPS from scratch · 20 min

Git: seeing exactly what changed

Git keeps every version of LAMMPS and shows any change as a diff: the lines removed in red, the lines added in green. You learn to read one, then follow a real experiment: one character of the LAMMPS source changed, recompiled, run, matched by hand, and undone.

1 · Understand: git remembers every version

LAMMPS is developed with git, a program that stores every version of every file. Two uses matter for this course: you download an exact version (so your line numbers match ours), and after you edit the code, git shows exactly what you changed and can undo it. Every code change in Part E will be shown this way.

CommandWhat it does
git clone -b stable_22Jul2025_update6 --depth 1 https://github.com/lammps/lammps.gitdownload one exact version (page 0.4)
git statuslist the files you have changed
git diffshow every changed line
git diff --statjust count the changes per file
git restore src/pair_lj_cut.cppundo your changes to one file
git stash / git stash popput all changes aside / bring them back

2 · A real change, read as a diff

We changed one character in src/pair_lj_cut.cpp: the 4.0 in line 511 became 2.0. This is what git reported, copied from our terminal:

$ git status Not currently on any branch. Changes not staged for commit: modified: src/pair_lj_cut.cpp $ git diff --stat src/pair_lj_cut.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/pair_lj_cut.cpp b/src/pair_lj_cut.cpp index 55279f6..7e4a443 100644 --- a/src/pair_lj_cut.cpp +++ b/src/pair_lj_cut.cpp @@ -508,7 +508,7 @@ double PairLJCut::init_one(int i, int j) 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); + lj3[i][j] = 2.0 * epsilon[i][j] * pow(sigma[i][j], 12.0); lj4[i][j] = 4.0 * epsilon[i][j] * pow(sigma[i][j], 6.0); if (offset_flag && (cut[i][j] > 0.0)) {
Line of the diffMeaning
--- a/…, +++ b/…a is the file before, b the file after
@@ -508,7 +508,7 @@this piece starts at line 508 and shows 7 lines, before and after
double PairLJCut::init_one(…)the function the change is in (a helpful extra)
lines starting with a spaceunchanged, shown for context
− redremoved
+ greenadded: a changed line appears as one removal and one addition
"Not currently on any branch"normal: you downloaded a release tag, not a line of development

Notice the "Not currently on any branch" line: it is not an error, only a reminder that you are on a fixed release.

3 · Predict by hand, then observe the real run

lj3 is used only in the energy line (page 0.1, line 129). The force lines use lj1 and lj2, which did not change. So before running anything we can predict:

energy per atom: ½ Σ n (lj3 r⁻¹² − lj4 r⁻⁶) = ½(2 x² S6 − 4 x S3) = x² S6 − 2x S3 = 1.54016742 − 2 × 4.92685145 = −8.3135355 (was −6.7733681) TotEng = −8.31353548 + 4.49887500 = −3.8146605 Press = −3.7033504 unchanged: it comes from the forces Temp unchanged at every step: same forces, so the atoms move exactly the same

We recompiled (16 s: two files, pair_lj_cut.cpp and lammps.cpp, which records the git state in the version information) and ran melt. Real output, original and changed:

StepTempE_pair originalE_pair changedPress (both)
03−6.7733681−8.3135355−3.7033504
501.6842865−4.8082494−8.43989555.5666131
1001.6712577−4.7875609−8.44018655.6613913
1501.6444751−4.7471034−8.44543075.8614211
2001.6471542−4.7509053−8.45612675.8805431
2501.6645597−4.7774327−8.45368145.7526089

Every prediction holds: step 0 matches to all digits, and Temp and Press are identical at every step. The change made the reported energy wrong without changing the motion at all. That is a warning for Part E: energy and force are written in separate lines, and a code change must keep them consistent (F = −dE/dr), or the program will run happily and print nonsense.

$ git restore src/pair_lj_cut.cpp undo $ make -j 2 13 s $ ./lmp -in in.melt 0 3 -6.7733681 0 -2.2744931 -3.7033504 back to the original

4 · Your own edit: see the diff and the prediction

Change the four numbers of lines 509–512. The page writes the diff git would show, and predicts the melt step-0 line by hand. To run your edit for real you would compile it (page 0.4); the run above shows that such predictions hold.

Your turn

1. Change only lj1 from 48.0 to 24.0. What happens to E_pair, Press and the motion?

2. A diff begins with @@ -116,1 +116,1 @@. What does it tell you?

3. You edited three files and want to start again from the release. Which one command undoes everything?