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.
- The six steps between a line of text and a command being run, in input.cpp
- The rules for &, #, $x, ${name} and $(formula), including a 15-digit trap
- Use variables in the melt script and confirm by hand that nothing changes
1 · Understand: six steps for every line
| Step | What happens | Where in input.cpp |
|---|---|---|
| 1. read | take the next line of the file | Input::file(), line 196 |
| 2. join | a line ending in & is joined to the next (the & becomes a space) | line 205: "if line ends in continuation char '&'" |
| 3. strip | everything from # on is a comment, unless the # is inside quotes | lines 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. split | cut into words at spaces; text in quotes stays one word (quotes removed) | nextword(), line 525 |
| 6. run | the first word is the command; LAMMPS looks it up and runs it with the other words | execute_command(), line 764 |
The comment step in the real code, translated:
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".
| Write | Means | Example (rc = 2.0) |
|---|---|---|
$x | the 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.
Your turn
1. What does LAMMPS make of pair_coeff 1 1 1.0 1.0 2.5#cutoff?
pair_coeff 1 1 1.0 1.0 2.5. Try it in the line reader.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?
$rc means the one-letter variable r followed by the letter c. If r is not defined LAMMPS stops with an error; if it is, you get a wrong number. Names longer than one letter need braces: ${rc}.