2007-05-01 07:37:43 +08:00
|
|
|
<HTML>
|
|
|
|
<CENTER><A HREF = "http://lammps.sandia.gov">LAMMPS WWW Site</A> - <A HREF = "Manual.html">LAMMPS Documentation</A> - <A HREF = "Section_commands.html#comm">LAMMPS Commands</A>
|
|
|
|
</CENTER>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<HR>
|
|
|
|
|
|
|
|
<H3>if command
|
|
|
|
</H3>
|
|
|
|
<P><B>Syntax:</B>
|
|
|
|
</P>
|
2010-11-19 01:28:36 +08:00
|
|
|
<PRE>if boolean then t1 t2 ... elif boolean f1 f2 ... elif boolean f1 f2 ... else e1 e2 ...
|
2007-05-01 07:37:43 +08:00
|
|
|
</PRE>
|
2010-11-19 01:28:36 +08:00
|
|
|
<UL><LI>boolean = a Boolean expression evaluated as TRUE or FALSE (see below)
|
2007-05-01 07:37:43 +08:00
|
|
|
<LI>then = required word
|
2010-01-20 04:18:34 +08:00
|
|
|
<LI>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
|
|
|
<LI>elif = optional word, can appear multiple times
|
|
|
|
<LI>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
|
|
|
<LI>else = optional argument
|
2010-11-19 01:28:36 +08:00
|
|
|
<LI>e1,e2,...,eN = one or more LAMMPS commands to execute if no condition is met, each enclosed in quotes (optional arguments)
|
2007-05-01 07:37:43 +08:00
|
|
|
</UL>
|
|
|
|
<P><B>Examples:</B>
|
|
|
|
</P>
|
2010-11-19 01:28:36 +08:00
|
|
|
<PRE>if "${steps} > 1000" then exit
|
|
|
|
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"
|
2007-05-01 07:37:43 +08:00
|
|
|
</PRE>
|
|
|
|
<P><B>Description:</B>
|
|
|
|
</P>
|
2010-01-20 04:04:41 +08:00
|
|
|
<P>This command provides an in-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 <A HREF = "variable.html">variable</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 witht 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>
|
2010-11-19 01:49:30 +08:00
|
|
|
<P>The syntax for Boolean expressions is described below.
|
2010-11-19 01:28:36 +08:00
|
|
|
</P>
|
|
|
|
<P>Each command (t1, f1, e1, etc) can be any valid LAMMPS input script
|
|
|
|
command. 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>
|
|
|
|
<P>IMPORTANT NOTE: If a command itself requires a quoted argument (e.g. a
|
|
|
|
<A HREF = "print.html">print</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 HREF = "Section_commands.html#3_2">this section</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.
|
2007-05-01 07:37:43 +08:00
|
|
|
</P>
|
2010-01-20 04:04:41 +08:00
|
|
|
<P>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
|
|
|
</P>
|
2010-11-19 01:28:36 +08:00
|
|
|
<PRE>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>
|
2010-11-19 01:28:36 +08:00
|
|
|
<P>Note that if one of the commands to execute is an invalid LAMMPS
|
|
|
|
command, such as "exit" in the first example above, then executing the
|
|
|
|
command will cause LAMMPS to halt.
|
2007-05-01 07:37:43 +08:00
|
|
|
</P>
|
2008-10-07 23:02:40 +08:00
|
|
|
<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 HREF = "variable.html">variable
|
2010-11-19 01:28:36 +08:00
|
|
|
delete</A> 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
|
|
|
</P>
|
|
|
|
<P>Here is an example of a double loop which uses the if and
|
|
|
|
<A HREF = "jump.html">jump</A> commands to break out of the inner loop when a
|
|
|
|
condition is met, then continues iterating thru the outer loop.
|
|
|
|
</P>
|
|
|
|
<PRE>label loopa
|
|
|
|
variable a loop 5
|
|
|
|
label loopb
|
|
|
|
variable b loop 5
|
|
|
|
print "A,B = $a,$b"
|
|
|
|
run 10000
|
2010-11-19 01:28:36 +08:00
|
|
|
if '$b > 2' then "print 'Jumping to another script'" "jump in.script break"
|
2008-10-07 23:02:40 +08:00
|
|
|
next b
|
|
|
|
jump in.script loopb
|
|
|
|
label break
|
|
|
|
variable b delete
|
|
|
|
</PRE>
|
|
|
|
<PRE>next a
|
|
|
|
jump in.script loopa
|
|
|
|
</PRE>
|
2010-11-19 01:28:36 +08:00
|
|
|
<HR>
|
|
|
|
|
|
|
|
<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>
|
2010-11-19 01:53:54 +08:00
|
|
|
<P>An expression is built out of numbers:
|
2010-11-19 01:28:36 +08:00
|
|
|
</P>
|
|
|
|
<PRE>0.2, 100, 1.0e20, -15.4, etc
|
|
|
|
</PRE>
|
|
|
|
<P>and Boolean operators:
|
|
|
|
</P>
|
2010-11-19 01:52:37 +08:00
|
|
|
<PRE>A == B, A != B, A < B, A <= B, A > B, A >= B, A && B, A || B
|
2010-11-19 01:28:36 +08:00
|
|
|
</PRE>
|
|
|
|
<P>Each A and B is a number or a variable reference like $a or ${abc},
|
|
|
|
or another Boolean expression.
|
|
|
|
</P>
|
|
|
|
<P>If a variable is used it must produce a number when evaluated and
|
2010-11-19 01:53:54 +08:00
|
|
|
substituted for in the expression, else an error will be generated.
|
2010-11-19 01:28:36 +08:00
|
|
|
</P>
|
|
|
|
<P>Expressions are evaluated left to right and have the usual C-style
|
|
|
|
precedence: the 4 relational operators "<", "<=", ">", and ">=" have
|
|
|
|
the highest precedence; those 4 relational operators before the
|
|
|
|
remaining two relational operators "==" and "!="; those two relational
|
|
|
|
operators before the logical AND operator "&&"; and the AND operator
|
|
|
|
"&&" before the logical OR operator "||". 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>The 6 relational operators return either a 1.0 or 0.0 depending on
|
|
|
|
whether the relationship between x and y is true or false. The
|
2010-11-19 01:49:30 +08:00
|
|
|
logical AND operator will return 1.0 if both its arguments are
|
2010-11-19 01:28:36 +08:00
|
|
|
non-zero, else return a 0.0. The logical OR operator will return 1.0
|
2010-11-19 01:49:30 +08:00
|
|
|
if either of its arguments is non-zero, else return a 0.0.
|
2010-11-19 01:28:36 +08:00
|
|
|
</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>
|
2007-05-01 07:37:43 +08:00
|
|
|
<P><B>Restrictions:</B> none
|
|
|
|
</P>
|
|
|
|
<P><B>Related commands:</B>
|
|
|
|
</P>
|
2010-01-20 04:04:41 +08:00
|
|
|
<P><A HREF = "variable.html">variable</A>, <A HREF = "print.html">print</A>
|
2007-05-01 07:37:43 +08:00
|
|
|
</P>
|
|
|
|
<P><B>Default:</B> none
|
|
|
|
</P>
|
|
|
|
</HTML>
|