<spanid="index-0"></span><h1>if command<aclass="headerlink"href="#if-command"title="Permalink to this headline">¶</a></h1>
<divclass="section"id="syntax">
<h2>Syntax<aclass="headerlink"href="#syntax"title="Permalink to this headline">¶</a></h2>
<divclass="highlight-python"><divclass="highlight"><pre>if boolean then t1 t2 ... elif boolean f1 f2 ... elif boolean f1 f2 ... else e1 e2 ...
</pre></div>
</div>
<ulclass="simple">
<li>boolean = a Boolean expression evaluated as TRUE or FALSE (see below)</li>
<li>then = required word</li>
<li>t1,t2,...,tN = one or more LAMMPS commands to execute if condition is met, each enclosed in quotes</li>
<li>elif = optional word, can appear multiple times</li>
<li>f1,f2,...,fN = one or more LAMMPS commands to execute if elif condition is met, each enclosed in quotes (optional arguments)</li>
<li>else = optional argument</li>
<li>e1,e2,...,eN = one or more LAMMPS commands to execute if no condition is met, each enclosed in quotes (optional arguments)</li>
</ul>
</div>
<divclass="section"id="examples">
<h2>Examples<aclass="headerlink"href="#examples"title="Permalink to this headline">¶</a></h2>
<divclass="highlight-python"><divclass="highlight"><pre>if "${steps}> 1000" then quit
if "${myString} == a10" then quit
if "$x <= $y" then "print X is smaller = $x" else "print Y is smaller = $y"
if "(${eng}> 0.0) || ($n < 1000)" then &
"timestep 0.005"&
elif $n<10000 &
"timestep 0.01"&
else &
"timestep 0.02"&
"print 'Max step reached'"
if "${eng}>${eng_previous}" then "jump file1" else "jump file2"
</pre></div>
</div>
</div>
<divclass="section"id="description">
<h2>Description<aclass="headerlink"href="#description"title="Permalink to this headline">¶</a></h2>
<p>This command provides an if-then-else capability within an input
script. A Boolean expression is evaluted and the result is TRUE or
FALSE. Note that as in the examples above, the expression can contain
variables, as defined by the <aclass="reference internal"href="variable.html"><em>variable</em></a> command, which
will be evaluated as part of the expression. Thus a user-defined
formula that reflects the current state of the simulation can be used
to issue one or more new commands.</p>
<p>If the result of the Boolean expression is TRUE, then one or more
commands (t1, t2, ..., tN) are executed. If it is FALSE, then Boolean
expressions associated with successive elif keywords are evaluated
until one is found to be true, in which case its commands (f1, f2,
..., fN) are executed. If no Boolean expression is TRUE, then the
commands associated with the else keyword, namely (e1, e2, ..., eN),
are executed. The elif and else keywords and their associated
commands are optional. If they aren’t specified and the initial
Boolean expression is FALSE, then no commands are executed.</p>
<p>The syntax for Boolean expressions is described below.</p>
<p>Each command (t1, f1, e1, etc) can be any valid LAMMPS input script
command, except an <aclass="reference internal"href="include.html"><em>include</em></a> command, which is not
allowed. If the command is more than one word, it must enclosed in
quotes, so it will be treated as a single argument, as in the examples
above.</p>
<divclass="admonition warning">
<pclass="first admonition-title">Warning</p>
<pclass="last">If a command itself requires a quoted argument (e.g. a
<aclass="reference internal"href="print.html"><em>print</em></a> command), then double and single quotes can be used
and nested in the usual manner, as in the examples above and below.
See <aclass="reference internal"href="Section_commands.html#cmd-2"><span>Section_commands 2</span></a> of the manual for
more details on using quotes in arguments. Only one of level of
nesting is allowed, but that should be sufficient for most use cases.</p>
</div>
<p>Note that by using the line continuation character “&”, the if command
can be spread across many lines, though it is still a single command:</p>
<divclass="highlight-python"><divclass="highlight"><pre>if "$a <$b" then &
"print 'Minimum value = $a'"&
"run 1000"&
else &
'print "Minimum value = $b"'&
"minimize 0.001 0.001 1000 10000"
</pre></div>
</div>
<p>Note that if one of the commands to execute is <aclass="reference internal"href="quit.html"><em>quit</em></a>, as in
the first example above, then executing the command will cause LAMMPS
to halt.</p>
<p>Note that by jumping to a label in the same input script, the if
command can be used to break out of a loop. See the <aclass="reference internal"href="variable.html"><em>variable delete</em></a> command for info on how to delete the associated
loop variable, so that it can be re-used later in the input script.</p>
<p>Here is an example of a loop which checks every 1000 steps if the
system temperature has reached a certain value, and if so, breaks out
of the loop to finish the run. Note that any variable could be
checked, so long as it is current on the timestep when the run
completes. As explained on the <aclass="reference internal"href="variable.html"><em>variable</em></a> doc page,
this can be insured by includig the variable in thermodynamic output.</p>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.