Page MenuHomec4science

if.html
No OneTemporary

File Metadata

Created
Mon, Dec 23, 08:59
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>if command &mdash; LAMMPS documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/sphinxcontrib-images/LightBox2/lightbox2/css/lightbox.css" type="text/css" />
<link rel="top" title="LAMMPS documentation" href="index.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="Manual.html" class="icon icon-home"> LAMMPS
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="Section_intro.html">1. Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_start.html">2. Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_commands.html">3. Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_packages.html">4. Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_accelerate.html">5. Accelerating LAMMPS performance</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_howto.html">6. How-to discussions</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_example.html">7. Example problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_perf.html">8. Performance &amp; scalability</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_tools.html">9. Additional tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_modify.html">10. Modifying &amp; extending LAMMPS</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_python.html">11. Python interface to LAMMPS</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_errors.html">12. Errors</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_history.html">13. Future and history</a></li>
</ul>
</div>
&nbsp;
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="Manual.html">LAMMPS</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="Manual.html">Docs</a> &raquo;</li>
<li>if command</li>
<li class="wy-breadcrumbs-aside">
<a href="http://lammps.sandia.gov">Website</a>
<a href="Section_commands.html#comm">Commands</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="if-command">
<span id="index-0"></span><h1>if command</h1>
<div class="section" id="syntax">
<h2>Syntax</h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">boolean</span> <span class="n">then</span> <span class="n">t1</span> <span class="n">t2</span> <span class="o">...</span> <span class="k">elif</span> <span class="n">boolean</span> <span class="n">f1</span> <span class="n">f2</span> <span class="o">...</span> <span class="k">elif</span> <span class="n">boolean</span> <span class="n">f1</span> <span class="n">f2</span> <span class="o">...</span> <span class="k">else</span> <span class="n">e1</span> <span class="n">e2</span> <span class="o">...</span>
</pre></div>
</div>
<ul class="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>
<div class="section" id="examples">
<h2>Examples</h2>
<div class="highlight-default"><div class="highlight"><pre><span></span>if &quot;${steps} &gt; 1000&quot; then quit
if &quot;${myString} == a10&quot; then quit
if &quot;$x &lt;= $y&quot; then &quot;print X is smaller = $x&quot; else &quot;print Y is smaller = $y&quot;
if &quot;(${eng} &gt; 0.0) || ($n &lt; 1000)&quot; then &amp;
&quot;timestep 0.005&quot; &amp;
elif $n&lt;10000 &amp;
&quot;timestep 0.01&quot; &amp;
else &amp;
&quot;timestep 0.02&quot; &amp;
&quot;print &#39;Max step reached&#39;&quot;
if &quot;${eng} &gt; ${eng_previous}&quot; then &quot;jump file1&quot; else &quot;jump file2&quot;
</pre></div>
</div>
</div>
<div class="section" id="description">
<h2>Description</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 <a class="reference internal" href="variable.html"><span class="doc">variable</span></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&#8217;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 <a class="reference internal" href="include.html"><span class="doc">include</span></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>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If a command itself requires a quoted argument (e.g. a
<a class="reference internal" href="print.html"><span class="doc">print</span></a> command), then double and single quotes can be used
and nested in the usual manner, as in the examples above and below.
See <a class="reference internal" href="Section_commands.html#cmd-2"><span class="std std-ref">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 &#8220;&amp;&#8221;, the if command
can be spread across many lines, though it is still a single command:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="s2">&quot;$a &lt; $b&quot;</span> <span class="n">then</span> <span class="o">&amp;</span>
<span class="s2">&quot;print &#39;Minimum value = $a&#39;&quot;</span> <span class="o">&amp;</span>
<span class="s2">&quot;run 1000&quot;</span> <span class="o">&amp;</span>
<span class="k">else</span> <span class="o">&amp;</span>
<span class="s1">&#39;print &quot;Minimum value = $b&quot;&#39;</span> <span class="o">&amp;</span>
<span class="s2">&quot;minimize 0.001 0.001 1000 10000&quot;</span>
</pre></div>
</div>
<p>Note that if one of the commands to execute is <a class="reference internal" href="quit.html"><span class="doc">quit</span></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 <a class="reference internal" href="variable.html"><span class="doc">variable delete</span></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 <a class="reference internal" href="variable.html"><span class="doc">variable</span></a> doc page,
this can be insured by includig the variable in thermodynamic output.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">variable</span> <span class="n">myTemp</span> <span class="n">equal</span> <span class="n">temp</span>
<span class="n">label</span> <span class="n">loop</span>
<span class="n">variable</span> <span class="n">a</span> <span class="n">loop</span> <span class="mi">1000</span>
<span class="n">run</span> <span class="mi">1000</span>
<span class="k">if</span> <span class="s2">&quot;$</span><span class="si">{myTemp}</span><span class="s2"> &lt; 300.0&quot;</span> <span class="n">then</span> <span class="s2">&quot;jump SELF break&quot;</span>
<span class="nb">next</span> <span class="n">a</span>
<span class="n">jump</span> <span class="n">SELF</span> <span class="n">loop</span>
<span class="n">label</span> <span class="k">break</span>
<span class="nb">print</span> <span class="s2">&quot;ALL DONE&quot;</span>
</pre></div>
</div>
<p>Here is an example of a double loop which uses the if and
<a class="reference internal" href="jump.html"><span class="doc">jump</span></a> commands to break out of the inner loop when a
condition is met, then continues iterating thru the outer loop.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">label</span> <span class="n">loopa</span>
<span class="n">variable</span> <span class="n">a</span> <span class="n">loop</span> <span class="mi">5</span>
<span class="n">label</span> <span class="n">loopb</span>
<span class="n">variable</span> <span class="n">b</span> <span class="n">loop</span> <span class="mi">5</span>
<span class="nb">print</span> <span class="s2">&quot;A,B = $a,$b&quot;</span>
<span class="n">run</span> <span class="mi">10000</span>
<span class="k">if</span> <span class="s2">&quot;$b &gt; 2&quot;</span> <span class="n">then</span> <span class="s2">&quot;jump SELF break&quot;</span>
<span class="nb">next</span> <span class="n">b</span>
<span class="n">jump</span> <span class="ow">in</span><span class="o">.</span><span class="n">script</span> <span class="n">loopb</span>
<span class="n">label</span> <span class="k">break</span>
<span class="n">variable</span> <span class="n">b</span> <span class="n">delete</span>
<span class="nb">next</span> <span class="n">a</span>
<span class="n">jump</span> <span class="n">SELF</span> <span class="n">loopa</span>
</pre></div>
</div>
<hr class="docutils" />
<p>The Boolean expressions for the if and elif keywords have a C-like
syntax. Note that each expression is a single argument within the if
command. Thus if you want to include spaces in the expression for
clarity, you must enclose the entire expression in quotes.</p>
<p>An expression is built out of numbers (which start with a digit or
period or minus sign) or strings (which start with a letter and can
contain alphanumeric characters or underscores):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="mf">0.2</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.0e20</span><span class="p">,</span> <span class="o">-</span><span class="mf">15.4</span><span class="p">,</span> <span class="n">etc</span>
<span class="n">InP</span><span class="p">,</span> <span class="n">myString</span><span class="p">,</span> <span class="n">a123</span><span class="p">,</span> <span class="n">ab_23_cd</span><span class="p">,</span> <span class="n">etc</span>
</pre></div>
</div>
<p>and Boolean operators:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>A == B, A != B, A &lt; B, A &lt;= B, A &gt; B, A &gt;= B, A &amp;&amp; B, A || B, !A
</pre></div>
</div>
<p>Each A and B is a number or string or a variable reference like $a or
${abc}, or A or B can be another Boolean expression.</p>
<p>If a variable is used it can produce a number when evaluated, like an
<a class="reference internal" href="variable.html"><span class="doc">equal-style variable</span></a>. Or it can produce a string,
like an <a class="reference internal" href="variable.html"><span class="doc">index-style variable</span></a>. For an individual
Boolean operator, A and B must both be numbers or must both be
strings. You cannot compare a number to a string.</p>
<p>Expressions are evaluated left to right and have the usual C-style
precedence: the unary logical NOT operator &#8221;!&#8221; has the highest
precedence, the 4 relational operators &#8220;&lt;&#8221;, &#8220;&lt;=&#8221;, &#8220;&gt;&#8221;, and &#8220;&gt;=&#8221; are
next; the two remaining relational operators &#8220;==&#8221; and &#8221;!=&#8221; are next;
then the logical AND operator &#8220;&amp;&amp;&#8221;; and finally the logical OR
operator &#8220;||&#8221; has the lowest precedence. Parenthesis can be used to
group one or more portions of an expression and/or enforce a different
order of evaluation than what would occur with the default precedence.</p>
<p>When the 6 relational operators (first 6 in list above) compare 2
numbers, they return either a 1.0 or 0.0 depending on whether the
relationship between A and B is TRUE or FALSE. When the 6 relational
operators compare 2 strings, they also return a 1.0 or 0.0 for TRUE or
FALSE, but the comparison is done by the C function strcmp().</p>
<p>When the 3 logical operators (last 3 in list above) compare 2 numbers,
they also return either a 1.0 or 0.0 depending on whether the
relationship between A and B is TRUE or FALSE (or just A). The
logical AND operator will return 1.0 if both its arguments are
non-zero, else it returns 0.0. The logical OR operator will return
1.0 if either of its arguments is non-zero, else it returns 0.0. The
logical NOT operator returns 1.0 if its argument is 0.0, else it
returns 0.0. The 3 logical operators can only be used to operate on
numbers, not on strings.</p>
<p>The overall Boolean expression produces a TRUE result if the result is
non-zero. If the result is zero, the expression result is FALSE.</p>
</div>
<hr class="docutils" />
<div class="section" id="restrictions">
<h2>Restrictions</h2>
<blockquote>
<div>none</div></blockquote>
</div>
<div class="section" id="related-commands">
<h2>Related commands</h2>
<p><a class="reference internal" href="variable.html"><span class="doc">variable</span></a>, <a class="reference internal" href="print.html"><span class="doc">print</span></a></p>
<p><strong>Default:</strong> none</p>
</div>
</div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2013 Sandia Corporation.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="_static/sphinxcontrib-images/LightBox2/lightbox2/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="_static/sphinxcontrib-images/LightBox2/lightbox2/js/lightbox.min.js"></script>
<script type="text/javascript" src="_static/sphinxcontrib-images/LightBox2/lightbox2-customize/jquery-noconflict.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>

Event Timeline