LAMMPS WWW Site - LAMMPS Documentation - LAMMPS Commands

variable command

Syntax:

variable name style args ... 

Examples:

variable x index run1 run2 run3 run4 run5 run6 run7 run8
variable LoopVar loop 20
variable beta equal div(temp,3.0)
variable b1 equal add(x[234],mult(0.5,lx))
variable b equal xcm(mol1,x)
variable temp world 300.0 310.0 320.0 330.0
variable x universe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

Description:

This command assigns one or more values to a variable name so that the variable can be used in subsequent input script commands. The "name" of the variable is an arbitrary string. Each "value" is a string which could be text or numbers, as in the examples above. As explained in this section, occurrences of the variable name in an input script line are replaced by the variable's value. The variable name can be referenced in the input script as $x if the name "x" is a single character, or as ${LoopVar} if the name "LoopVar" is one or more characters.

As described below, for variable styles index, loop, and universe, the value assigned to a variable can be incremented via the next command. When there are no more values 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 exited from.

When a variable command is encountered for a variable that has already been specified, the command is skipped. This is the case for all variable styles except equal, so that equal-style variable names can be re-used and re-defined anytime. Skipping allows you to loop over the same input script many times without re-defining your variables. When a variable is exhausted via the next command, it is then available to be re-defined in a subsequent variable 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 can also be set (with a single value) 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. Initially, the string "1" is assigned to the variable. Each time a next command is used with the variable name, the next string ("2", "3", etc) is assigned. All processors assign the same string to the variable.

For the equal style, a single string is specified which represents an equation that will be evaluated afresh each time the variable is used. Thus the variable can take on different values at different stages of the input script. For example, if the variable is used in a fix print command, it could print different values each timestep it was invoked. The next command cannot be used with equal-style variables, since there is only one value. Note that, as with any other input script command, it is feasible to use another variable in the equal variable's string, e.g. variable y equal mult($x,2). However, $x will be replaced immediately by it's current value when the command is first parsed, not each time that $y is substituted for.

The syntax of the equation assigned to equal variables is simple. It can contain "functions", "vectors", "keywords", or "numbers" in any combination.

For the group functions, ID is a group-ID, dim is 'x' or 'y' or 'z', and dir is one of 6 strings: "xmin", "xmax", "ymin", "ymax", "zmin", or "zmax". The group functions mass() and charge() are the total mass and charge of the group of atoms. Xcm() and vcm() return components of the position and velocity of the center of mass of the group. Bound() returns the min/max of a particular coordinate for all atoms in the group. Gyration() computes the radius-of-gyration of the group of atoms. See the fix gyration command for the formula.

Keywords have restrictions on when they can be assigned to variables. For example, keywords that compute thermodynamic quantites can only be invoked after the first simulation has begun. A warning is issued if thermodyanmic keywords are invoked on timesteps when thermodynamic information is not being printed to the screen, since the values assigned to the variable may be out-of-date.

The variable equal equation can also be nested in that function arguments can be functions, vectors, keywords, or numbers. For example, this is a valid equation:

variable x equal div(add(pe,ke),pow(vol,div(1,3))) 

For the world style, one or more strings are specified. There must be one string for each processor partition or "world". See this section of the manual for information on running LAMMPS with multiple partitions via the "-partition" 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 "worlds". See this page for information on running LAMMPS with multiple partitions via the "-partition" 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 value. This continues until all the variable values 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 "tmp.lammps.variable" and "tmp.lammps.variable.lock" which you will see in your directory during such a LAMMPS run.

If a variable command is encountered when the variable has already been defined, the command is ignored. Thss allows an input script with a variable command to be processed multiple times; see the jump or include commands. It also means that the use of the command-line switch -var will override a corresponding variable setting in the input script.

Restrictions:

The use of atom vectors in equal style variables requires the atom style to use a global mapping in order to look up the vector indices. Only atom styles with molecular information create global maps.

Related commands:

next, jump, include, temper, fix print, print

Default: none