LAMMPS from scratch · 20 min

How LAMMPS reads one line

Before LAMMPS does anything with a line, it joins continued lines, cuts off comments, replaces variables with their values and splits the result into words. You follow these six steps in the real source, run any line through a line reader that predicts each step, and check the prediction against LAMMPS.

1 · Understand: six steps for every line

StepWhat happensWhere in input.cpp
1. readtake the next line of the fileInput::file(), line 196
2. joina line ending in & is joined to the next (the & becomes a space)line 205: "if line ends in continuation char '&'"
3. stripeverything from # on is a comment, unless the # is inside quoteslines 444–472
4. substitute$x, ${name} and $(formula) are replaced by values, except inside quotes (a command such as print substitutes its quoted text itself, later)line 488: substitute(copy,…)
5. splitcut into words at spaces; text in quotes stays one word (quotes removed)nextword(), line 525
6. runthe first word is the command; LAMMPS looks it up and runs it with the other wordsexecute_command(), line 764

The comment step in the real code, translated:

// strip a # comment by replacing it with 0 // do not treat a # inside single/double/triple quotes as a comment while (*ptr) { walk along the line, one character at a time if (*ptr == '#') { found a # outside quotes: *ptr = '\0'; end the line here break; } if (*ptr == '\'') { … } a quote: jump to the matching quote

Step 6 is a long list of names (if (mycmd == "clear") … else if (mycmd == "units") …), followed by a lookup table for commands such as run and velocity. A name in neither gives the error "Unknown command".

WriteMeansExample (rc = 2.0)
$xthe variable with the one-letter name x$n → 10
${name}the variable called name; an equal-style formula is printed with 15 digits${rc} → 2
$(formula)work out the formula now, print with up to 20 digits$(v_rc+0.5) → 2.5
$(formula:%.4f)the same, with your own format$(v_rc/3:%.4f) → 0.6667

2 · Modify and observe: the line reader

Pick an example or type your own line and variables. The page predicts every step; "Check with LAMMPS" asks the real LAMMPS to print the substituted line.

    3 · Match: variables change nothing when the values are the same

    Here is melt with the temperature and the cutoff written as variables: T = 2*1.5 and the cutoff $(v_rc+0.5) with rc = 2.0. Step 4 turns them into 3 and 2.5, so the hand prediction for step 0 is exactly the original line.

    variable T equal 2*1.5 variable rc equal 2.0 … velocity all create ${T} 87287 loop geom → create 3 87287 pair_style lj/cut $(v_rc+0.5) → lj/cut 2.5 pair_coeff 1 1 1.0 1.0 $(v_rc+0.5) # same cutoff → 1 1 1.0 1.0 2.5 predicted step 0: E_pair −6.7733681 TotEng −2.2744931 Press −3.7033504

    Your turn

    1. What does LAMMPS make of pair_coeff 1 1 1.0 1.0 2.5#cutoff?

    2. With variable third equal 1/3, why does $(v_third) print 0.33333333333333298176 but $(1/3) prints 0.33333333333333331483?

    3. You write variable rc equal 2.5 and then pair_style lj/cut $rc. What goes wrong?