diff -Naur lammps-8Oct08/doc/Section_start.html lammps-9Oct08/doc/Section_start.html --- lammps-8Oct08/doc/Section_start.html 2008-05-27 08:06:53.000000000 -0600 +++ lammps-9Oct08/doc/Section_start.html 2008-10-06 17:07:25.000000000 -0600 @@ -579,12 +579,13 @@ single character (referenced as $x in the input script) or a full string (referenced as ${abc}). The value can be any string. Using this command-line option is equivalent to putting the line "variable -name index value" at the beginning of the input script. Defining a -variable as a command-line argument overrides any setting for the same -variable in the input script, since variables cannot be re-defined. -See the variable command for more info on defining -variables and this section for more info -on using variables in input scripts. +name index value" at the beginning of the input script. Defining an +index variable as a command-line argument overrides any setting for +the same index variable in the input script, since index variables +cannot be re-defined. See the variable command for +more info on defining index and other kinds of variables and this +section for more info on using variables in +input scripts.
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 -Naur lammps-8Oct08/doc/if.txt lammps-9Oct08/doc/if.txt --- lammps-8Oct08/doc/if.txt 2007-06-20 06:56:17.000000000 -0600 +++ lammps-9Oct08/doc/if.txt 2008-10-07 09:02:40.000000000 -0600 @@ -45,10 +45,33 @@ 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 -Naur lammps-8Oct08/doc/jump.html lammps-9Oct08/doc/jump.html --- lammps-8Oct08/doc/jump.html 2008-03-18 15:09:15.000000000 -0600 +++ lammps-9Oct08/doc/jump.html 2008-10-07 09:02:40.000000000 -0600 @@ -27,9 +27,9 @@
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 @@
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 -Naur lammps-8Oct08/doc/jump.txt lammps-9Oct08/doc/jump.txt --- lammps-8Oct08/doc/jump.txt 2008-03-18 15:09:15.000000000 -0600 +++ lammps-9Oct08/doc/jump.txt 2008-10-07 09:02:40.000000000 -0600 @@ -24,9 +24,9 @@ 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 @@ 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 -Naur lammps-8Oct08/doc/next.html lammps-9Oct08/doc/next.html --- lammps-8Oct08/doc/next.html 2007-06-20 06:56:17.000000000 -0600 +++ lammps-9Oct08/doc/next.html 2008-10-07 09:02:40.000000000 -0600 @@ -48,7 +48,9 @@
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 @@ 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 -Naur lammps-8Oct08/doc/next.txt lammps-9Oct08/doc/next.txt --- lammps-8Oct08/doc/next.txt 2007-06-20 06:56:17.000000000 -0600 +++ lammps-9Oct08/doc/next.txt 2008-10-07 09:02:40.000000000 -0600 @@ -45,7 +45,9 @@ 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 @@ 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 -Naur lammps-8Oct08/doc/variable.html lammps-9Oct08/doc/variable.html --- lammps-8Oct08/doc/variable.html 2008-05-15 14:32:10.000000000 -0600 +++ lammps-9Oct08/doc/variable.html 2008-10-07 09:02:40.000000000 -0600 @@ -17,9 +17,10 @@
index args = one or more strings
+ delete = no args
+ index args = one or more strings
loop args = N = integer size of loop
world args = one string for each partition of processors
universe args = one or more strings
@@ -40,7 +41,7 @@
vx[], vy[], vz[], fx[], fy[], fz[]
compute references = c_ID, c_ID[2], c_ID[N], c_ID[N][2], c_ID[], c_ID[][2]
fix references = f_ID, f_ID[2], f_ID[N], f_ID[N][2], f_ID[], f_ID[][2]
- other variables = v_abc, v_abc[N], v_abc[]
+ variable references = v_abc, v_abc[N], v_abc[]
Description:
@@ -97,20 +99,20 @@ script to be processed multiple times without resetting the variables; see the jump or include commands. It also means that using the command-line switch -var -will override a corresponding variable setting in the input script. +will override a corresponding index variable setting in the input +script.There are two exceptions to this rule. First, variables of style equal and atom ARE redefined each time the command is encountered. This allows them to be reset, when their formulas contain a substitution for another variable, e.g. $x. This can be useful in a -loop. This also means an equal-style variable will re-define a -command-line switch -var setting, so an index-style variable should -be used for such settings instead, as in bench/in.lj. +loop.
Second, as described below, if a variable is iterated on to the end of its list of strings via the next command, it is removed from the list of active variables, and is thus available to be -re-defined in a subsequent variable command. +re-defined in a subsequent variable command. The delete option does +the same thing.
As explained above, an exhausted variable can be re-used in an input +script. The delete option also removes the variable, the same as if +it were exhausted, allowing it to be redefined later in the input +script or when the input script is looped over. This can be useful +when breaking out of a loop via the if and jump +commands before the variable would become exhausted. For example, +
+label loop +variable a loop 5 +print "A = $a" +if $a > 2 then "jump in.script break" +next a +jump in.script loop +label break +variable a delete +
For the index style, one or more strings are specified. Initially, @@ -298,13 +316,13 @@
Fix references access one or more quantities calculated by a -fix. The ID in the reference should be replaced by -the actual ID of the fix defined elsewhere in the input script. -See the doc pages for individual computes to see which ones calculate -global versus per-atom quantities. If the compute reference contains -empty brackets, then per-atom values calculated by the compute are -accessed. Otherwise a single value (global or per-atom) calculated by -the compute is accessed. +fix. The ID in the reference should be replaced by the +actual ID of the fix defined elsewhere in the input script. See the +doc pages for individual computes to see which ones calculate global +versus per-atom quantities. If the fix reference contains empty +brackets, then per-atom values calculated by the fix are accessed. +Otherwise a single value (global or per-atom) calculated by the +fix is accessed.
Note that some fixes only generate quantities on certain timesteps. If a variable attempts to access the fix on non-allowed timesteps, an @@ -338,27 +356,30 @@
then LAMMPS will run for a while when the print statement is invoked!
-Another way to reference a variable in a formula is using the $x form -instead of v_x. There is a subtle difference between the two -references that has to do with when the evaluation of the included -variable is done. -
-Using a $x, the value of the include variable is substituted for -immediately when the line is read from the input script, just as it -would be in other input script command. This could be the desired -behavior if a static value is desired. Or it could be the desired -behavior for an equal-style variable if the variable command appears -in a loop (see the jump and next commands), -since the substitution will be performed anew each time thru the loop -as the command is re-read. Note that if the variable formula is -enclosed in double quotes, this prevents variable substitution and -thus an error will be generated when the variable formula is -evaluated. -
-Using a v_x, the value of the included variable will not be accessed -until the variable formula is evaluated. Thus the value may change -each time the evaluation is performed. This may also be desired -behavior. +
It is useful to understand the distinction between referencing a +variable in a formula using the $x form instead of v_x. There is a +subtle difference between the two references that has to do with when +the evaluation of the included variable is done. +
+Referencing the variable as $x, the value of the include variable is +substituted for immediately when the line is read from the input +script, just as it would be in other input script command. +
+This could be the desired behavior if a static value is desired. Or +it could be the desired behavior for an equal-style variable if the +variable command appears in a loop (see the jump and +next commands), since the substitution will be performed +anew each time thru the loop as the command is re-read. Note that if +the variable formula is enclosed in double quotes, this prevents +variable substitution and thus an error will be generated when the +variable formula is evaluated. +
+Referencing the variable as v_x, the value of the included variable +will not be accessed until the variable formula is evaluated. Thus +the value may change each time the evaluation is performed. This may +also be desired behavior.
As an example, if the current simulation box volume is 1000.0, then these lines: diff -Naur lammps-8Oct08/doc/variable.txt lammps-9Oct08/doc/variable.txt --- lammps-8Oct08/doc/variable.txt 2008-05-15 14:32:10.000000000 -0600 +++ lammps-9Oct08/doc/variable.txt 2008-10-07 09:02:40.000000000 -0600 @@ -13,7 +13,8 @@ variable name style args ... :pre name = name of variable to define :ulb,l -style = {index} or {loop} or {world} or {universe} or {uloop} or {equal} or {atom} :l +style = {delete} or {index} or {loop} or {world} or {universe} or {uloop} or {equal} or {atom} :l + {delete} = no args {index} args = one or more strings {loop} args = N = integer size of loop {world} args = one string for each partition of processors @@ -35,7 +36,7 @@ vx\[\], vy\[\], vz\[\], fx\[\], fy\[\], fz\[\] compute references = c_ID, c_ID\[2\], c_ID\[N\], c_ID\[N\]\[2\], c_ID\[\], c_ID\[\]\[2\] fix references = f_ID, f_ID\[2\], f_ID\[N\], f_ID\[N\]\[2\], f_ID\[\], f_ID\[\]\[2\] - other variables = v_abc, v_abc\[N\], v_abc\[\] :pre + variable references = v_abc, v_abc\[N\], v_abc\[\] :pre :ule [Examples:] @@ -47,10 +48,11 @@ variable b1 equal "x\[234\] + 0.5*vol" variable b equal xcm(mol1,x)/2.0 variable b equal c_myTemp -variable b atom x[]*y[]/vol +variable b atom x\[\]*y\[\]/vol variable temp world 300.0 310.0 320.0 $\{Tfinal\} variable x universe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -variable x uloop 15 :pre +variable x uloop 15 +variable x delete :pre [Description:] @@ -91,20 +93,20 @@ script to be processed multiple times without resetting the variables; see the "jump"_jump.html or "include"_include.html commands. It also means that using the "command-line switch"_Section_start.html#2_6 -var -will override a corresponding variable setting in the input script. +will override a corresponding index variable setting in the input +script. There are two exceptions to this rule. First, variables of style {equal} and {atom} ARE redefined each time the command is encountered. This allows them to be reset, when their formulas contain a substitution for another variable, e.g. $x. This can be useful in a -loop. This also means an {equal}-style variable will re-define a -command-line switch -var setting, so an {index}-style variable should -be used for such settings instead, as in bench/in.lj. +loop. Second, as described below, if a variable is iterated on to the end of its list of strings via the "next"_next.html command, it is removed from the list of active variables, and is thus available to be -re-defined in a subsequent variable command. +re-defined in a subsequent variable command. The {delete} option does +the same thing. :line @@ -122,6 +124,22 @@ skipped. This enables the construction of simple loops in the input script that are iterated over and then exited from. +As explained above, an exhausted variable can be re-used in an input +script. The {delete} option also removes the variable, the same as if +it were exhausted, allowing it to be redefined later in the input +script or when the input script is looped over. This can be useful +when breaking out of a loop via the "if"_if.html and "jump"_jump.html +commands before the variable would become exhausted. For example, + +label loop +variable a loop 5 +print "A = $a" +if $a > 2 then "jump in.script break" +next a +jump in.script loop +label break +variable a delete :pre + :line For the {index} style, one or more strings are specified. Initially, @@ -291,13 +309,13 @@ c_ID\[\]\[M\]: per-atom vector component from a per-atom compute :tb(s=:) Fix references access one or more quantities calculated by a -"fix"_fix.html. The ID in the reference should be replaced by -the actual ID of the fix defined elsewhere in the input script. -See the doc pages for individual computes to see which ones calculate -global versus per-atom quantities. If the compute reference contains -empty brackets, then per-atom values calculated by the compute are -accessed. Otherwise a single value (global or per-atom) calculated by -the compute is accessed. +"fix"_fix.html. The ID in the reference should be replaced by the +actual ID of the fix defined elsewhere in the input script. See the +doc pages for individual computes to see which ones calculate global +versus per-atom quantities. If the fix reference contains empty +brackets, then per-atom values calculated by the fix are accessed. +Otherwise a single value (global or per-atom) calculated by the +fix is accessed. Note that some fixes only generate quantities on certain timesteps. If a variable attempts to access the fix on non-allowed timesteps, an @@ -329,27 +347,30 @@ then LAMMPS will run for a while when the print statement is invoked! -Another way to reference a variable in a formula is using the $x form -instead of v_x. There is a subtle difference between the two -references that has to do with when the evaluation of the included -variable is done. - -Using a $x, the value of the include variable is substituted for -immediately when the line is read from the input script, just as it -would be in other input script command. This could be the desired -behavior if a static value is desired. Or it could be the desired -behavior for an equal-style variable if the variable command appears -in a loop (see the "jump"_jump.html and "next"_next.html commands), -since the substitution will be performed anew each time thru the loop -as the command is re-read. Note that if the variable formula is -enclosed in double quotes, this prevents variable substitution and -thus an error will be generated when the variable formula is -evaluated. - -Using a v_x, the value of the included variable will not be accessed -until the variable formula is evaluated. Thus the value may change -each time the evaluation is performed. This may also be desired -behavior. +:line + +It is useful to understand the distinction between referencing a +variable in a formula using the $x form instead of v_x. There is a +subtle difference between the two references that has to do with when +the evaluation of the included variable is done. + +Referencing the variable as $x, the value of the include variable is +substituted for immediately when the line is read from the input +script, just as it would be in other input script command. + +This could be the desired behavior if a static value is desired. Or +it could be the desired behavior for an equal-style variable if the +variable command appears in a loop (see the "jump"_jump.html and +"next"_next.html commands), since the substitution will be performed +anew each time thru the loop as the command is re-read. Note that if +the variable formula is enclosed in double quotes, this prevents +variable substitution and thus an error will be generated when the +variable formula is evaluated. + +Referencing the variable as v_x, the value of the included variable +will not be accessed until the variable formula is evaluated. Thus +the value may change each time the evaluation is performed. This may +also be desired behavior. As an example, if the current simulation box volume is 1000.0, then these lines: diff -Naur lammps-8Oct08/src/variable.cpp lammps-9Oct08/src/variable.cpp --- lammps-8Oct08/src/variable.cpp 2008-07-08 13:45:01.000000000 -0600 +++ lammps-9Oct08/src/variable.cpp 2008-10-07 08:44:33.000000000 -0600 @@ -89,30 +89,23 @@ void Variable::set(int narg, char **arg) { - if (narg < 3) error->all("Illegal variable command"); + if (narg < 2) error->all("Illegal variable command"); - // if var already exists, just skip, except EQUAL and ATOM vars + // DELETE + // doesn't matter if variable no longer exists - if (find(arg[0]) >= 0 && - strcmp(arg[1],"equal") != 0 && strcmp(arg[1],"atom") != 0) return; - - // make space for new variable - - if (nvar == maxvar) { - maxvar += VARDELTA; - names = (char **) - memory->srealloc(names,maxvar*sizeof(char *),"var:names"); - style = (int *) memory->srealloc(style,maxvar*sizeof(int),"var:style"); - num = (int *) memory->srealloc(num,maxvar*sizeof(int),"var:num"); - index = (int *) memory->srealloc(index,maxvar*sizeof(int),"var:index"); - data = (char ***) - memory->srealloc(data,maxvar*sizeof(char **),"var:data"); - } + if (strcmp(arg[1],"delete") == 0) { + if (narg != 2) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) remove(find(arg[0])); + return; // INDEX // num = listed args, index = 1st value, data = copied args - if (strcmp(arg[1],"index") == 0) { + } else if (strcmp(arg[1],"index") == 0) { + if (narg < 3) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) return; + if (nvar == maxvar) extend(); style[nvar] = INDEX; num[nvar] = narg - 2; index[nvar] = 0; @@ -124,6 +117,8 @@ } else if (strcmp(arg[1],"loop") == 0) { if (narg != 3) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) return; + if (nvar == maxvar) extend(); style[nvar] = LOOP; num[nvar] = atoi(arg[2]); index[nvar] = 0; @@ -142,6 +137,7 @@ error->all("Cannot redefine variable as a different style"); remove(find(arg[0])); } + if (nvar == maxvar) extend(); style[nvar] = EQUAL; num[nvar] = 2; index[nvar] = 0; @@ -154,6 +150,9 @@ // error check that num = # of worlds in universe } else if (strcmp(arg[1],"world") == 0) { + if (narg < 3) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) return; + if (nvar == maxvar) extend(); style[nvar] = WORLD; num[nvar] = narg - 2; if (num[nvar] != universe->nworlds) @@ -171,12 +170,17 @@ } else if (strcmp(arg[1],"universe") == 0 || strcmp(arg[1],"uloop") == 0) { if (strcmp(arg[1],"universe") == 0) { + if (narg < 3) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) return; + if (nvar == maxvar) extend(); style[nvar] = UNIVERSE; num[nvar] = narg - 2; data[nvar] = new char*[num[nvar]]; copy(num[nvar],&arg[2],data[nvar]); } else { if (narg != 3) error->all("Illegal variable command"); + if (find(arg[0]) >= 0) return; + if (nvar == maxvar) extend(); style[nvar] = ULOOP; num[nvar] = atoi(arg[2]); data[nvar] = new char*[num[nvar]]; @@ -221,6 +225,7 @@ error->all("Cannot redefine variable as a different style"); remove(find(arg[0])); } + if (nvar == maxvar) extend(); style[nvar] = ATOM; num[nvar] = 1; index[nvar] = 0; @@ -482,6 +487,22 @@ } /* ---------------------------------------------------------------------- + make space in arrays for new variable +------------------------------------------------------------------------- */ + +void Variable::extend() +{ + maxvar += VARDELTA; + names = (char **) + memory->srealloc(names,maxvar*sizeof(char *),"var:names"); + style = (int *) memory->srealloc(style,maxvar*sizeof(int),"var:style"); + num = (int *) memory->srealloc(num,maxvar*sizeof(int),"var:num"); + index = (int *) memory->srealloc(index,maxvar*sizeof(int),"var:index"); + data = (char ***) + memory->srealloc(data,maxvar*sizeof(char **),"var:data"); +} + +/* ---------------------------------------------------------------------- copy narg strings from **from to **to ------------------------------------------------------------------------- */ diff -Naur lammps-8Oct08/src/variable.h lammps-9Oct08/src/variable.h --- lammps-8Oct08/src/variable.h 2008-01-08 16:13:53.000000000 -0700 +++ lammps-9Oct08/src/variable.h 2008-10-07 08:44:33.000000000 -0600 @@ -51,9 +51,10 @@ Tree *left,*right; }; + void remove(int); + void extend(); void copy(int, char **, char **); double evaluate(char *, Tree **); - void remove(int); double eval_tree(Tree *, int); void free_tree(Tree *); int find_matching_paren(char *, int, char *&);