"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:]

if value1 operator value2 then command1 else command2 :pre

value1 = 1st value
operator = "<" or "<=" or ">" or ">=" or "==" or "!="
value2 = 2nd value
then = required word
command1 = command to execute if condition is met
else = optional word
command2 = command to execute if condition is not met (optional argument) :ul

[Examples:]

if $\{steps\} > 1000 then exit
if $x <= $y then "print X is smaller = $x" else "print Y is smaller = $y"
if $\{eng\} > 0.0 then "timestep 0.005"
if $\{eng\} > $\{eng_previous\} then "jump file1" else "jump file2" :pre

[Description:]

This command provides an in-then-else test capability within an input
script.  Two values are numerically compared to each other and the
result is TRUE or FALSE.  Note that as in the examples above, either
of the values can be variables, as defined by the
"variable"_variable.html command, so that when they are evaluated when
substituted for in the if command, a user-defined computation will be
performed which can depend on the current state of the simulation.

If the result of the if test is TRUE, then command1 is executed.  This
can be any valid LAMMPS input script command.  If the command is more
than 1 word, it should be enclosed in double quotes, so that it will
be treated as a single argument, as in the examples above.

The if command can contain an optional "else" clause.  If it does and
the result of the if test is FALSE, then command2 is executed.

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
delete"_variable.html for info on how to delete the associated loop
variable, so that it can be re-used later in the input script.

Note that if either command1 or command2 is a bogus LAMMPS command,
such as "exit" in the first example, then executing the command will
cause LAMMPS to halt.

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
  if	    $b > 2 then "jump in.script break"
  next	    b
  jump	    in.script loopb
label	    break
variable    b delete :pre
next	    a
jump	    in.script loopa :pre

[Restrictions:] none

[Related commands:]

"variable"_variable.html

[Default:] none
