2007-05-01 07:37:43 +08:00
|
|
|
"LAMMPS WWW Site"_lws - "LAMMPS Documentation"_ld - "LAMMPS Commands"_lc :c
|
|
|
|
|
|
|
|
:link(lws,http://lammps.sandia.gov)
|
|
|
|
:link(ld,Manual.html)
|
|
|
|
:link(lc,Section_commands.html#comm)
|
|
|
|
|
|
|
|
:line
|
|
|
|
|
|
|
|
if command :h3
|
|
|
|
|
|
|
|
[Syntax:]
|
|
|
|
|
2010-11-19 01:28:36 +08:00
|
|
|
if boolean then t1 t2 ... elif boolean f1 f2 ... elif boolean f1 f2 ... else e1 e2 ... :pre
|
2007-05-01 07:37:43 +08:00
|
|
|
|
2010-11-19 01:28:36 +08:00
|
|
|
boolean = a Boolean expression evaluated as TRUE or FALSE (see below)
|
2007-05-01 07:37:43 +08:00
|
|
|
then = required word
|
2010-01-20 04:18:34 +08:00
|
|
|
t1,t2,...,tN = one or more LAMMPS commands to execute if condition is met, each enclosed in quotes
|
2010-11-19 01:28:36 +08:00
|
|
|
elif = optional word, can appear multiple times
|
|
|
|
f1,f2,...,fN = one or more LAMMPS commands to execute if elif condition is met, each enclosed in quotes (optional arguments)
|
2010-01-20 04:04:41 +08:00
|
|
|
else = optional argument
|
2010-11-19 01:28:36 +08:00
|
|
|
e1,e2,...,eN = one or more LAMMPS commands to execute if no condition is met, each enclosed in quotes (optional arguments) :ul
|
2007-05-01 07:37:43 +08:00
|
|
|
|
|
|
|
[Examples:]
|
|
|
|
|
2012-01-28 08:14:10 +08:00
|
|
|
if "$\{steps\} > 1000" then quit
|
2014-02-06 05:27:34 +08:00
|
|
|
if "$\{myString\} == a10" then quit
|
2010-11-19 01:28:36 +08:00
|
|
|
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
|
2007-05-01 07:37:43 +08:00
|
|
|
|
|
|
|
[Description:]
|
|
|
|
|
2013-05-08 07:41:55 +08:00
|
|
|
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
|
|
|
|
variables, as defined by the "variable"_variable.html 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.
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
commands are optional. If they aren't specified and the initial
|
|
|
|
Boolean expression is FALSE, then no commands are executed.
|
|
|
|
|
|
|
|
The syntax for Boolean expressions is described below.
|
|
|
|
|
|
|
|
Each command (t1, f1, e1, etc) can be any valid LAMMPS input script
|
2013-06-18 22:05:06 +08:00
|
|
|
command, except an "include"_include.html command, which is not
|
|
|
|
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
|
|
|
|
above.
|
|
|
|
|
|
|
|
IMPORTANT NOTE: If a command itself requires a quoted argument (e.g. a
|
|
|
|
"print"_print.html command), then double and single quotes can be used
|
|
|
|
and nested in the usual manner, as in the examples above and below.
|
2011-12-14 04:43:36 +08:00
|
|
|
See "Section_commands 2"_Section_commands.html#cmd_2 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.
|
2007-05-01 07:37:43 +08:00
|
|
|
|
2010-01-20 04:04:41 +08:00
|
|
|
Note that by using the line continuation character "&", the if command
|
2010-11-19 01:28:36 +08:00
|
|
|
can be spread across many lines, though it is still a single command:
|
2010-01-20 04:04:41 +08:00
|
|
|
|
2010-11-19 01:28:36 +08:00
|
|
|
if "$a < $b" then &
|
|
|
|
"print 'Minimum value = $a'" &
|
2010-01-20 04:04:41 +08:00
|
|
|
"run 1000" &
|
|
|
|
else &
|
2010-11-19 01:28:36 +08:00
|
|
|
'print "Minimum value = $b"' &
|
2010-01-20 04:04:41 +08:00
|
|
|
"minimize 0.001 0.001 1000 10000" :pre
|
|
|
|
|
2014-02-01 08:22:12 +08:00
|
|
|
Note that if one of the commands to execute is "quit"_quit.html, as in
|
|
|
|
the first example above, then executing the command will cause LAMMPS
|
|
|
|
to halt.
|
2007-05-01 07:37:43 +08:00
|
|
|
|
2008-10-07 23:02:40 +08:00
|
|
|
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 "variable
|
2010-11-19 01:28:36 +08:00
|
|
|
delete"_variable.html command for info on how to delete the associated
|
|
|
|
loop variable, so that it can be re-used later in the input script.
|
2008-10-07 23:02:40 +08:00
|
|
|
|
2014-03-18 06:52:40 +08:00
|
|
|
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 "variable"_variable.html doc page,
|
|
|
|
this can be insured by includig the variable in thermodynamic output.
|
|
|
|
|
|
|
|
variable myTemp equal temp
|
|
|
|
label loop
|
|
|
|
variable a loop 1000
|
|
|
|
run 1000
|
|
|
|
if "$\{myTemp\} < 300.0" then "jump SELF break"
|
|
|
|
next a
|
|
|
|
jump SELF loop
|
|
|
|
label break
|
|
|
|
print "ALL DONE" :pre
|
|
|
|
|
2008-10-07 23:02:40 +08:00
|
|
|
Here is an example of a double loop which uses the if and
|
|
|
|
"jump"_jump.html commands to break out of the inner loop when a
|
|
|
|
condition is met, then continues iterating thru the outer loop.
|
|
|
|
|
|
|
|
label loopa
|
|
|
|
variable a loop 5
|
|
|
|
label loopb
|
|
|
|
variable b loop 5
|
|
|
|
print "A,B = $a,$b"
|
|
|
|
run 10000
|
2014-03-18 06:52:40 +08:00
|
|
|
if "$b > 2" then "jump SELF break"
|
2008-10-07 23:02:40 +08:00
|
|
|
next b
|
|
|
|
jump in.script loopb
|
|
|
|
label break
|
2014-03-18 06:52:40 +08:00
|
|
|
variable b delete
|
2008-10-07 23:02:40 +08:00
|
|
|
next a
|
2014-03-18 06:52:40 +08:00
|
|
|
jump SELF loopa :pre
|
2008-10-07 23:02:40 +08:00
|
|
|
|
2010-11-19 01:28:36 +08:00
|
|
|
:line
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-02-06 05:27:34 +08:00
|
|
|
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):
|
2010-11-19 01:28:36 +08:00
|
|
|
|
2014-02-06 05:27:34 +08:00
|
|
|
0.2, 100, 1.0e20, -15.4, etc
|
|
|
|
InP, myString, a123, ab_23_cd, etc :pre
|
2010-11-19 01:28:36 +08:00
|
|
|
|
|
|
|
and Boolean operators:
|
|
|
|
|
2010-11-19 02:30:31 +08:00
|
|
|
A == B, A != B, A < B, A <= B, A > B, A >= B, A && B, A || B, !A :pre
|
2010-11-19 01:28:36 +08:00
|
|
|
|
2014-02-06 05:27:34 +08:00
|
|
|
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.
|
2010-11-19 01:28:36 +08:00
|
|
|
|
2014-02-06 05:27:34 +08:00
|
|
|
If a variable is used it can produce a number when evaluated, like an
|
|
|
|
"equal-style variable"_variable.html. Or it can produce a string,
|
|
|
|
like an "index-style variable"_variable.html. 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.
|
2010-11-19 01:28:36 +08:00
|
|
|
|
|
|
|
Expressions are evaluated left to right and have the usual C-style
|
2010-11-19 02:30:31 +08:00
|
|
|
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
|
|
|
|
order of evaluation than what would occur with the default precedence.
|
|
|
|
|
2014-02-06 05:27:34 +08:00
|
|
|
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().
|
|
|
|
|
|
|
|
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
|
2010-11-19 01:28:36 +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
|
|
|
|
numbers, not on strings.
|
2010-11-19 01:28:36 +08:00
|
|
|
|
|
|
|
The overall Boolean expression produces a TRUE result if the result is
|
|
|
|
non-zero. If the result is zero, the expression result is FALSE.
|
|
|
|
|
2011-03-18 23:09:16 +08:00
|
|
|
:line
|
|
|
|
|
2007-05-01 07:37:43 +08:00
|
|
|
[Restrictions:] none
|
|
|
|
|
|
|
|
[Related commands:]
|
|
|
|
|
2010-01-20 04:04:41 +08:00
|
|
|
"variable"_variable.html, "print"_print.html
|
2007-05-01 07:37:43 +08:00
|
|
|
|
|
|
|
[Default:] none
|