diff --git a/doc/if.html b/doc/if.html index 424473a05a..819c8f9c4c 100644 --- a/doc/if.html +++ b/doc/if.html @@ -48,10 +48,34 @@ 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 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 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 ++
next a +jump in.script loopa +
Restrictions: none
Related commands: diff --git a/doc/if.txt b/doc/if.txt index d71ae0d87a..fdd4ae9179 100644 --- a/doc/if.txt +++ b/doc/if.txt @@ -45,10 +45,33 @@ 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:] diff --git a/doc/jump.html b/doc/jump.html index f449a183a4..98d93fb147 100644 --- a/doc/jump.html +++ b/doc/jump.html @@ -27,9 +27,9 @@ jump in.run2 runloop
This command closes the current input script file, opens the file with the specified name, and begins reading LAMMPS commands from that file. -The original file is not returned to, although by using multiple jump -commands it is possible to chain from file to file or back to the -original file. +Unlike the include command, the original file is not +returned to, although by using multiple jump commands it is possible +to chain from file to file or back to the original file.
Optionally, if a 2nd argument is used, it is treated as a label and the new file is scanned (without executing commands) until the label @@ -61,6 +61,25 @@ simulation.
variable f world script.1 script.2 script.3 script.4 jump $f+
Here is an example of a double loop which uses the if and +jump 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 ++
next a +jump in.script loopa +
Restrictions:
If you jump to a file and it does not contain the specified label, diff --git a/doc/jump.txt b/doc/jump.txt index e0c63a1be4..74efd6f695 100644 --- a/doc/jump.txt +++ b/doc/jump.txt @@ -24,9 +24,9 @@ jump in.run2 runloop :pre This command closes the current input script file, opens the file with the specified name, and begins reading LAMMPS commands from that file. -The original file is not returned to, although by using multiple jump -commands it is possible to chain from file to file or back to the -original file. +Unlike the "include"_include.html command, the original file is not +returned to, although by using multiple jump commands it is possible +to chain from file to file or back to the original file. Optionally, if a 2nd argument is used, it is treated as a label and the new file is scanned (without executing commands) until the label @@ -58,6 +58,24 @@ mpirun -np 40 lmp_ibm -partition 4x10 -in in.file :pre variable f world script.1 script.2 script.3 script.4 jump $f :pre +Here is an example of a double loop which uses the "if"_if.html and +jump 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:] If you jump to a file and it does not contain the specified label, diff --git a/doc/next.html b/doc/next.html index 620d73b265..12bf89db4d 100644 --- a/doc/next.html +++ b/doc/next.html @@ -48,7 +48,9 @@ value from their respective lists.
When any of the variables in the next command has no more values, a flag is set that causes the input script to skip the next jump command encountered. This enables a loop containing -a next command to exit. +a next command to exit. As explained in the variable +command, the variable that has exhausted its values is also deleted. +This allows it to be used and re-defined later in the input script.
When the next command is used with index- or loop-style variables, the next value is assigned to the variable for all processors. When @@ -87,17 +89,36 @@ finished. For example, this script will run 15 simulations in a double loop.
variable i loop 3 -variable j loop 5 -clear -... -read_data data.polymer.$i$j -print Running simulation $i.$j -run 10000 -next j -jump in.script + variable j loop 5 + clear + ... + read_data data.polymer.$i$j + print Running simulation $i.$j + run 10000 + next j + jump in.script next i jump in.script+
Here is an example of a double loop which uses the if and +jump 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 ++
next a +jump in.script loopa +
Restrictions: none
Related commands: diff --git a/doc/next.txt b/doc/next.txt index 2c7b66acfa..ef0a81164f 100644 --- a/doc/next.txt +++ b/doc/next.txt @@ -45,7 +45,9 @@ value from their respective lists. When any of the variables in the next command has no more values, a flag is set that causes the input script to skip the next "jump"_jump.html command encountered. This enables a loop containing -a next command to exit. +a next command to exit. As explained in the "variable"_variable.html +command, the variable that has exhausted its values is also deleted. +This allows it to be used and re-defined later in the input script. When the next command is used with {index}- or {loop}-style variables, the next value is assigned to the variable for all processors. When @@ -84,17 +86,35 @@ Jump and next commands can also be nested to enable multi-level loops. For example, this script will run 15 simulations in a double loop. variable i loop 3 -variable j loop 5 -clear -... -read_data data.polymer.$i$j -print Running simulation $i.$j -run 10000 -next j -jump in.script + variable j loop 5 + clear + ... + read_data data.polymer.$i$j + print Running simulation $i.$j + run 10000 + next j + jump in.script next i jump in.script :pre +Here is an example of a double loop which uses the "if"_if.html 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:] diff --git a/doc/variable.html b/doc/variable.html index 82369b4ebd..448f2d9f33 100644 --- a/doc/variable.html +++ b/doc/variable.html @@ -140,9 +140,9 @@ commands before the variable would become exhausted. For example,
label loop variable a loop 5 print "A = $a" -if $a > 2 then "jump in.test break" +if $a > 2 then "jump in.script break" next a -jump in.test loop +jump in.script loop label break variable a deletediff --git a/doc/variable.txt b/doc/variable.txt index bb19c00d5b..dfeaa006ac 100644 --- a/doc/variable.txt +++ b/doc/variable.txt @@ -134,9 +134,9 @@ commands before the variable would become exhausted. For example, label loop variable a loop 5 print "A = $a" -if $a > 2 then "jump in.test break" +if $a > 2 then "jump in.script break" next a -jump in.test loop +jump in.script loop label break variable a delete :pre