2015-07-30 22:53:28 +08:00
<!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" >
2015-12-21 23:20:41 +08:00
< title > if command — LAMMPS documentation< / title >
2015-07-30 22:53:28 +08:00
< 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" / >
2015-12-21 23:20:41 +08:00
< link rel = "top" title = "LAMMPS documentation" href = "index.html" / >
2015-07-30 22:53:28 +08:00
< 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 & 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 & 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 >
< / 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 > » < / 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< a class = "headerlink" href = "#if-command" title = "Permalink to this headline" > ¶< / a > < / h1 >
< div class = "section" id = "syntax" >
< h2 > Syntax< a class = "headerlink" href = "#syntax" title = "Permalink to this headline" > ¶< / a > < / h2 >
< div class = "highlight-python" > < div class = "highlight" > < pre > if boolean then t1 t2 ... elif boolean f1 f2 ... elif boolean f1 f2 ... else e1 e2 ...
< / 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< a class = "headerlink" href = "#examples" title = "Permalink to this headline" > ¶< / a > < / h2 >
< div class = "highlight-python" > < div class = "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 >
< div class = "section" id = "description" >
< h2 > Description< a class = "headerlink" href = "#description" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > This command provides an if-then-else capability within an input
2010-11-19 01:28:36 +08:00
script. A Boolean expression is evaluted and the result is TRUE or
FALSE. Note that as in the examples above, the expression can contain
2015-07-30 22:53:28 +08:00
variables, as defined by the < a class = "reference internal" href = "variable.html" > < em > variable< / em > < / a > command, which
2010-11-19 01:28:36 +08:00
will be evaluated as part of the expression. Thus a user-defined
formula that reflects the current state of the simulation can be used
2015-07-30 22:53:28 +08:00
to issue one or more new commands.< / p >
< p > If the result of the Boolean expression is TRUE, then one or more
2010-11-19 01:28:36 +08:00
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
2013-05-08 07:41:55 +08:00
commands associated with the else keyword, namely (e1, e2, ..., eN),
2010-11-19 01:28:36 +08:00
are executed. The elif and else keywords and their associated
2015-07-30 22:53:28 +08:00
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 < a class = "reference internal" href = "include.html" > < em > include< / em > < / a > command, which is not
2013-06-18 22:05:06 +08:00
allowed. If the command is more than one word, it must enclosed in
2010-11-19 01:28:36 +08:00
quotes, so it will be treated as a single argument, as in the examples
2015-07-30 22:53:28 +08:00
above.< / p >
2015-12-11 01:23:56 +08:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
2015-07-30 22:53:28 +08:00
< p class = "last" > If a command itself requires a quoted argument (e.g. a
< a class = "reference internal" href = "print.html" > < em > print< / em > < / a > command), then double and single quotes can be used
2010-11-19 01:28:36 +08:00
and nested in the usual manner, as in the examples above and below.
2015-07-30 22:53:28 +08:00
See < a class = "reference internal" href = "Section_commands.html#cmd-2" > < span > Section_commands 2< / span > < / a > of the manual for
2011-12-14 04:43:36 +08:00
more details on using quotes in arguments. Only one of level of
2015-07-30 22:53:28 +08:00
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 >
< div class = "highlight-python" > < div class = "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 < a class = "reference internal" href = "quit.html" > < em > quit< / em > < / a > , as in
2014-02-01 08:22:12 +08:00
the first example above, then executing the command will cause LAMMPS
2015-07-30 22:53:28 +08:00
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" > < 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
2014-03-18 06:52:40 +08:00
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
2015-07-30 22:53:28 +08:00
completes. As explained on the < a class = "reference internal" href = "variable.html" > < em > variable< / em > < / a > doc page,
this can be insured by includig the variable in thermodynamic output.< / p >
< div class = "highlight-python" > < div class = "highlight" > < pre > variable myTemp equal temp
2014-03-18 06:52:40 +08:00
label loop
variable a loop 1000
run 1000
2015-07-30 22:53:28 +08:00
if " ${myTemp} < 300.0" then " jump SELF break"
2014-03-18 06:52:40 +08:00
next a
jump SELF loop
label break
2015-07-30 22:53:28 +08:00
print " ALL DONE"
< / pre > < / div >
< / div >
< p > Here is an example of a double loop which uses the if and
< a class = "reference internal" href = "jump.html" > < em > jump< / em > < / 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-python" > < div class = "highlight" > < pre > label loopa
2008-10-07 23:02:40 +08:00
variable a loop 5
2015-07-30 22:53:28 +08:00
label loopb
2008-10-07 23:02:40 +08:00
variable b loop 5
2015-07-30 22:53:28 +08:00
print " A,B = $a,$b"
2008-10-07 23:02:40 +08:00
run 10000
2015-07-30 22:53:28 +08:00
if " $b > 2" then " jump SELF break"
next b
jump in.script loopb
label break
2014-03-18 06:52:40 +08:00
variable b delete
2015-07-30 22:53:28 +08:00
next a
jump SELF loopa
< / pre > < / div >
< / div >
< hr class = "docutils" / >
< p > The Boolean expressions for the if and elif keywords have a C-like
2010-11-19 01:28:36 +08:00
syntax. Note that each expression is a single argument within the if
command. Thus if you want to include spaces in the expression for
2015-07-30 22:53:28 +08:00
clarity, you must enclose the entire expression in quotes.< / p >
< p > An expression is built out of numbers (which start with a digit or
2014-02-06 05:27:34 +08:00
period or minus sign) or strings (which start with a letter and can
2015-07-30 22:53:28 +08:00
contain alphanumeric characters or underscores):< / p >
< div class = "highlight-python" > < div class = "highlight" > < pre > < 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-python" > < div class = "highlight" > < pre > A == B, A != B, A < B, A < = B, A > B, A > = B, A & & 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" > < em > equal-style variable< / em > < / a > . Or it can produce a string,
like an < a class = "reference internal" href = "variable.html" > < em > index-style variable< / em > < / a > . For an individual
2014-02-06 05:27:34 +08:00
Boolean operator, A and B must both be numbers or must both be
2015-07-30 22:53:28 +08:00
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 ” !” has the highest
precedence, the 4 relational operators “ < ” , “ < =” , “ > ” , and “ > =” are
next; the two remaining relational operators “ ==” and ” !=” are next;
then the logical AND operator “ & & ” ; and finally the logical OR
operator “ ||” has the lowest precedence. Parenthesis can be used to
2010-11-19 01:28:36 +08:00
group one or more portions of an expression and/or enforce a different
2015-07-30 22:53:28 +08:00
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
2014-02-06 05:27:34 +08:00
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
2015-07-30 22:53:28 +08:00
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,
2014-02-06 05:27:34 +08:00
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
2010-11-19 01:49:30 +08:00
logical AND operator will return 1.0 if both its arguments are
2010-11-19 02:30:31 +08:00
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
2014-02-06 05:27:34 +08:00
returns 0.0. The 3 logical operators can only be used to operate on
2015-07-30 22:53:28 +08:00
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< a class = "headerlink" href = "#restrictions" title = "Permalink to this headline" > ¶< / a > < / h2 >
< blockquote >
< div > none< / div > < / blockquote >
< / div >
< div class = "section" id = "related-commands" >
< h2 > Related commands< a class = "headerlink" href = "#related-commands" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > < a class = "reference internal" href = "variable.html" > < em > variable< / em > < / a > , < a class = "reference internal" href = "print.html" > < em > print< / em > < / a > < / p >
< p > < strong > Default:< / strong > none< / p >
< / div >
< / div >
< / div >
< / div >
< footer >
< hr / >
< div role = "contentinfo" >
< p >
2015-12-11 01:23:56 +08:00
© Copyright 2013 Sandia Corporation.
2015-07-30 22:53:28 +08:00
< / 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:'./',
2015-12-21 23:20:41 +08:00
VERSION:'',
2015-07-30 22:53:28 +08:00
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 >