LAMMPS WWW Site - LAMMPS Documentation - LAMMPS Commands
variable command Syntax:
variable name style args ...
? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
name = name of variable to define style
= delete or index or loop or world or universe or uloop or string or format or getenv or file or atomfile or equal or atom delete = no args
index args = one or more strings loop args = N
N = integer size of loop, loop from 1 to N inclusive loop args = N pad
N = integer size of loop, loop from 1 to N inclusive
pad = all values will be same length, e.g. 001, 002, ..., 100 loop args = N1 N2
N1,N2 = loop from N1 to N2 inclusive loop args = N1 N2 pad
N1,N2 = loop from N1 to N2 inclusive
pad = all values will be same length, e.g. 050, 051, ..., 100 world args = one string for each partition of processors universe args = one or more strings uloop args = N
N = integer size of loop uloop args = N pad
N = integer size of loop
pad = all values will be same length, e.g. 001, 002, ..., 100 string arg = one string format args = vname fstr
vname = name of equal-style variable to evaluate fstr = C-style format string getenv arg = one string file arg = filename
atomfile arg = filename
equal or atom args = one formula containing numbers, thermo keywords, math operations, group functions, atom values and vectors, compute/fix/variable references numbers = 0.0, 100, -5.4, 2.8e-4, etc constants = PI
thermo keywords = vol, ke, press, etc from thermo_style math operators = (), -x, x+y, x-y, x*y, x/y, x^y,
x==y, x!=y, x<=\ math functions = sqrt(x), exp(x), ln(x), log(x), abs(x),
sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), atan2(y,x), random(x,y,z), normal(x,y,z), ceil(x), floor(x), round(x)
ramp(x,y), stagger(x,y), logfreq(x,y,z), stride(x,y,z), vdisplace(x,y), swiggle(x,y,z), cwiggle(x,y,z)
group functions = count(group), mass(group), charge(group), xcm(group,dim), vcm(group,dim), fcm(group,dim),
? ? ? ? ? ? ? ? ? ? ? ? ? ? bound(group,xmin), gyration(group), ke(group), angmom(group,dim), torque(group,dim), inertia(group,dimdim), omega(group,dim)
region functions = count(group,region), mass(group,region), charge(group,region), xcm(group,dim,region), vcm(group,dim,region), fcm(group,dim,region), bound(group,xmin,region), gyration(group,region), ke(group,reigon), angmom(group,dim,region), torque(group,dim,region), inertia(group,dimdim,region), omega(group,dim,region)
special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x)
atom value = id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i] atom vector = id, mass, type, mol, x, y, z, vx, vy, vz, fx, fy, fz compute references = c_ID, c_ID[i], c_ID[i][j] fix references = f_ID, f_ID[i], f_ID[i][j] variable references = v_name, v_name[i]
Examples:
variable x index run1 run2 run3 run4 run5 run6 run7 run8 variable LoopVar loop $n variable beta equal temp/3.0 variable b1 equal x[234]+0.5*vol variable b1 equal \variable b equal xcm(mol1,x)/2.0 variable b equal c_myTemp variable b atom x*y/vol variable foo string myfile variable f file values.txt
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 pad variable str format x %.6g variable x delete Description:
This command assigns one or more strings to a variable name for evaluation later in the input script or during a simulation.
Variables can thus be useful in several contexts. A variable can be defined and then referenced elsewhere in an input script to become part of a new input command. For variable styles that store multiple strings, the next command can be used to increment which string is assigned to the variable. Variables of style equal store a formula which when evaluated produces a single numeric value which can be output either directly (see the print, fix print, and run every commands) or as part of thermodynamic output (see the thermo_style command), or used as input to an averaging fix (see the fix ave/time command). Variables of style atom store a formula which when evaluated produces one numeric value per atom which can be output to a dump file (see the dump custom command) or used as input to an averaging fix (see the fix ave/spatial and fix ave/atom commands). Variables of style atomfile can be used anywhere in an input script that atom-style variables are used; they get their per-atom values from a file rather than from a formula.
In the discussion that follows, the \command. This name can only contain alphanumeric characters and underscores. The \subsequent arguments. The \the 2nd example, or it can be a formula as in the 3rd example. The \
evaluation of the string. Note that the same string can generate different values when it is evaluated at different times during a simulation.
IMPORTANT NOTE: When the input script line is encountered that defines a variable of style equal or atom that contains a formula, the formula is NOT immediately evaluated and the result stored. See the discussion below about \Evaluation of Variables\format style variable since it evaluates another variable when it is invoked.
IMPORTANT NOTE: When a variable command is encountered in the input script and the variable name has already been specified, the command is ignored. This means variables can NOT be re-defined in an input script (with 2 exceptions, read further). This is to allow an input 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 index variable setting in the input script.
There are two exceptions to this rule. First, variables of style string, getenv, equal and atom ARE redefined each time the command is encountered. This allows these style of variables to be redefined multiple times in an input script. In a loop, this means the formula associated with an equal or atom style variable can change if it contains a substitution for another variable, e.g. $x or v_x.
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. The delete style does the same thing.
This section of the manual explains how occurrences of a variable name in an input script line are replaced by the
variable's string. The variable name can be referenced as $x if the name \name \
As described below, for variable styles index, loop, file, universe, and uloop, which string is assigned to a variable can be incremented via the next command. When there are no more strings to assign, the variable is exhausted and a flag is set that causes the next jump command encountered in the input script to be 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 style 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 \
if \next a
jump in.script loop label break variable a delete
This section describes how various variable styles are defined and what they store. Many of the styles store one or more strings. Note that a single string can contain spaces (multiple words), if it is enclosed in quotes in the variable command. When the variable is substituted for in another input script command, its returned string will then be interpreted as multiple arguments in the expanded command.
For the index style, one or more strings are specified. Initially, the 1st string is assigned to the variable. Each time
a next command is used with the variable name, the next string is assigned. All processors assign the same string to the variable.
Index style variables with a single string value can also be set by using the command-line switch -var; see this section for details.
The loop style is identical to the index style except that the strings are the integers from 1 to N inclusive, if only one argument N is specified. This allows generation of a long list of runs (e.g. 1000) without having to list N strings in the input script. Initially, the string \next command is used with the variable name, the next string (\loop style can also be specified with two arguments N1 and N2. In this case the loop runs from N1 to N2 inclusive, and the string N1 is initially assigned to the variable. N1 <= N2 and N2 >= 0 is required.
For the world style, one or more strings are specified. There must be one string for each processor partition or \See this section of the manual for information on running LAMMPS with multiple partitions via the \
command-line switch. This variable command assigns one string to each world. All processors in the world are assigned the same string. The next command cannot be used with equal style variables, since there is only one value per world. This style of variable is useful when you wish to run different simulations on different partitions, or when performing a parallel tempering simulation (see the temper command), to assign different temperatures to different partitions. For the universe style, one or more strings are specified. There must be at least as many strings as there are processor partitions or \this page for information on running LAMMPS with multiple partitions via the \command-line switch. This variable command initially assigns one string to each world. When a next command is encountered using this variable, the first processor partition to encounter it, is assigned the next available string. This continues until all the variable strings are consumed. Thus, this command can be used to run 50 simulations on 8
processor partitions. The simulations will be run one after the other on whatever partition becomes available, until they are all finished. Universe style variables are incremented using the files \\
The uloop style is identical to the universe style except that the strings are the integers from 1 to N. This allows generation of long list of runs (e.g. 1000) without having to list N strings in the input script.
For the string style, a single string is assigned to the variable. The only difference between this and using the index style with a single string is that a variable with string style can be redefined. E.g. by another command later in the input script, or if the script is read again in a loop.
For the format style, an equal-style variable is specified along with a C-style format string, e.g. \must be appropriate for formatting a double-precision floating-point value. This allows an equal-style variable to be formatted specifically for output as a string, e.g. by the print command, if the default format \precision.
For the getenv style, a single string is assigned to the variable which should be the name of an environment variable. When the variable is evaluated, it returns the value of the environment variable, or an empty string if it not defined. This style of variable can be used to adapt the behavior of LAMMPS input scripts via environment variable settings, or to retrieve information that has been previously stored with the shell putenv command. Note that because environment variable settings are stored by the operating systems, they persist beyond a clear command.
For the file style, a filename is provided which contains a list of strings to assign to the variable, one per line. The strings can be numeric values if desired. See the discussion of the next() function below for equal-style variables, which will convert the string of a file-style variable into a numeric value in a formula.
When a file-style variable is defined, the file is opened and the string on the first line is read and stored with the variable. This means the variable can then be evaluated as many times as desired and will return that string. There are two ways to cause the next string from the file to be read: use the next command or the next() function in an equal- or atom-style variable, as discussed below.
The rules for formatting the file are as follows. A comment character \with the comment character is stripped. Blank lines are skipped. The first \space, is the \
For the atomfile style, a filename is provided which contains one or more sets of values, to assign on a per-atom basis to the variable. The format of the file is described below.
When an atomfile-style variable is defined, the file is opened and the first set of per-atom values are read and stored with the variable. This means the variable can then be evaluated as many times as desired and will return those values. There are two ways to cause the next set of per-atom values from the file to be read: use the next command or the next() function in an atom-style variable, as discussed below.
The rules for formatting the file are as follows. Each time a set of per-atom values is read, a non-blank line is searched for in the file. A comment character \Blank lines are skipped. The first \lines to immediately follow. N can be be the total number of atoms in the system, or only a subset. The next N lines have the following format ID value
where ID is an atom ID and value is the per-atom numeric value that will be assigned to that atom. IDs can be listed in any order.
IMPORTANT NOTE: Every time a set of per-atom lines is read, the value for all atoms is first set to 0.0. Thus values for atoms whose ID does not appear in the set, will remain 0.0.
For the equal and atom styles, a single string is specified which represents a formula that will be evaluated afresh each time the variable is used. If you want spaces in the string, enclose it in double quotes so the parser will treat it as a single argument. For equal style variables the formula computes a scalar quantity, which becomes the value of the variable whenever it is evaluated. For atom style variables the formula computes one quantity for each atom whenever it is evaluated.
Note that equal and atom variables can produce different values at different stages of the input script or at different
times during a run. For example, if an equal variable is used in a fix printcommand, different values could be printed each timestep it was invoked. If you want a variable to be evaluated immediately, so that the result is stored by the variable instead of the string, see the section below on \
The next command cannot be used with equal or atom style variables, since there is only one string.
The formula for an equal or atom variable can contain a variety of quantities. The syntax for each kind of quantity is
simple, but multiple quantities can be nested and combined in various ways to build up formulas of arbitrary complexity. For example, this is a valid (though strange) variable formula: variable x equal \
Specifically, an formula can contain numbers, thermo keywords, math operators, math functions, group functions, region functions, atom values, atom vectors, compute references, fix references, and references to other variables.
Number Constant Thermo keywords Math operators 0.2, 100, 1.0e20, -15.4, etc PI vol, pe, ebond, etc (), -x, x+y, x-y, x*y, x/y, x^y, x==y, x!=y, x<=\sqrt(x), exp(x), ln(x), log(x), abs(x), sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), atan2(y,x), Math functions random(x,y,z), normal(x,y,z), ceil(x), floor(x), round(x), ramp(x,y), stagger(x,y), logfreq(x,y,z), stride(x,y,z), vdisplace(x,y), swiggle(x,y,z), cwiggle(x,y,z) Group functions count(ID), mass(ID), charge(ID), xcm(ID,dim), vcm(ID,dim), fcm(ID,dim), bound(ID,dir), gyration(ID), ke(ID), angmom(ID,dim), torque(ID,dim), inertia(ID,dimdim), omega(ID,dim) count(ID,IDR), mass(ID,IDR), charge(ID,IDR), xcm(ID,dim,IDR), vcm(ID,dim,IDR), fcm(ID,dim,IDR), bound(ID,dir,IDR), gyration(ID,IDR), ke(ID,IDR), Region functions