3 Enabling and controlling the Colvars module in NAMD

Here, we document the syntax of the commands and parameters used to set up and use the Colvars module in NAMD Phillips et al. (2020). One of these parameters is the configuration file or the configuration text for the module itself, whose syntax is described in 3.4 and in the following sections.

3.1 Units in the Colvars module

The “internal units” of the Colvars module are the units in which values are expressed in the configuration file, and in which collective variable values, energies, etc. are expressed in the output and colvars trajectory files. Generally the Colvars module uses internally the same units as its back-end MD engine, with the exception of VMD, where different unit sets are supported to allow for easy setup, visualization and analysis of Colvars simulations performed with any simulation engine.

Note that angles are expressed in degrees, and derived quantities such as force constants are based on degrees as well. Some colvar components have default values, expressed in Ångström (Å) in this documentation. They are converted to the current length unit, if different from Å. Atomic coordinates read from XYZ files (and PDB files where applicable) are expected to be expressed in Ångström, no matter what unit system is in use by the back-end (NAMD) or the Colvars Module. They are converted internally to the current length unit as needed. Note that force constants in harmonic and harmonicWalls biases (6.8) are rescaled according to the width parameter of colvars, so that they are formally in energy units, although if width is given its default value of 1.0, force constants are effectively expressed in kcal/mol/(colvar unit)2.

To avoid errors due to reading configuration files written in a different unit system, it can be specified within the input:

  • •

    units — Unit system to be used
    [ string, context: global ]

    A string defining the units to be used internally by Colvars. In NAMD the only supported value is NAMD’s native units: real (Å, kcal/mol).

3.2 NAMD parameters

To enable a Colvars-based calculation, the colvars on command must be added to the NAMD script. Two optional commands, colvarsConfig and colvarsInput can be used to define the module’s configuration or continue a previous simulation. Because these are static parameters, it is typically more convenient to use the cv command in the rest of the NAMD script.

  • •

    colvars — Enable the Colvars module
    Default: off [ boolean, context: NAMD configuration file ]

    If this flag is on, the Colvars module within NAMD is enabled.

  • •

    colvarsConfig — Configuration file for the collective variables
    [ UNIX filename, context: NAMD configuration file ]

    Name of the Colvars configuration file (3.4, 3.5 and following sections). This file can also be provided by the Tcl command cv configfile. Alternatively, the contents of the file (as opposed to the file itself) can be given as a string argument to the command cv config.

  • •

    colvarsInput — Input state file for the collective variables
    [ UNIX filename, context: NAMD configuration file ]

    Keyword used to specify the input state file’s name (3.6). If file name contains “.colvars.state”, Colvars will strip any further suffix following this string. If not, the suffix wil be appended. When intending to load an input state file after startup, e.g. within a Tcl script section, the cv load command should be used instead.

3.3 Using the scripting interface to control the Colvars module

After the first initialization of the Colvars module, the internal state of Colvars objects may be queried or modified in a NAMD script:

cv <method> arg1 arg2 ...
where <method> is the name of a specific procedure and arg1, arg2, …are its required and/or optional arguments.

In the remainder of this section, the most frequently used commands of the Colvars scripting interface are discussed and exemplified. For a full list of scripting commands available, see section 7.

3.3.1 Setting up the Colvars module

If the NAMD configuration parameter colvars is on, the cv Tcl command can be used anywhere in the NAMD script, and will be invoked as soon as NAMD begins processing Tcl commands.

To define new collective variables and/or biases for immediate use in the current session, configuration can be loaded from an external configuration file:

cv configfile "colvars-file.in"
This can in principle be called at any time, if only flags internal to Colvars are being modified. In practice, when new atoms or any new atomic properties (e.g. total forces) are being requested, initialization steps will be required that are not carried out during a simulation. Therefore, it is generally good practice in a simulation to change the Colvars configuration outside the scope between segments of the same computation.

To load the configuration directly from a string the “config” method may be used:

cv config "keyword { ... }"
This method is particularly useful to dynamically define the Colvars configuration within a NAMD script. For example, when running an ensemble of umbrella sampling simulations in NAMD, it may be convenient to use an identical script and define the specific window through environment variables.

In the following example, the environment variable “SLURM_ARRAY_TASK_ID” is set in a Slurm array job to increasing values starting at 0. The value of this variable is used to define the window’s numeric index, and the umbrella restraint center beginning at 2.00 for the first window, and increasing in increments of 0.25 for all other windows:

cv configfile colvar-definition.in
set window $env(SLURM_ARRAY_TASK_ID)
cv config "harmonic {
  name us_${window}
  colvars xi
  centers [expr 2.0 + 0.25 * ${window}]
  ...
}"

3.3.2 Using the Colvars version in scripts

The vast majority of the syntax in Colvars is backward-compatible, adding keywords when new features are introduced. However, when using multiple versions simultaneously it may be useful to test within the script whether the version is recent enough to support the desired feature. The “version” can be used to get the Colvars version for this use:

if { [cv version] >= "2020-02-25" } {
  cv config "(use a recent feature)"
}

3.3.3 Loading and saving the Colvars state and other information

After a configuration is fully defined, the “load” method may be used to load a state file from a previous simulation that contains e.g. data from history-dependent biases), to either continue that simulation or analyze its results:

cv load "<oldjob>.colvars.state"
or more simply using the prefix of the state file itself:
cv load "<oldjob>"
Anything following the “.colvars.state” suffix will be stripped.

The latter version is particularly convenient in combination with the NAMD reinitatoms command, for example:

reinitatoms <oldjob>
cv load <oldjob>
The step number contained by the loaded file will be used internally by Colvars to control time-dependent biases, unless firstTimestep is issued, in which case that value will be used.

When the system’s topology is changed during simulation via the structure command (e.g. in constant-pH simulations), it is generally best to reset and re-initialize the module from scratch before loading the corresponding snapshot:

structure newsystem.psf
reinitatoms <snapshot>
cv reset
cv configfile <new_config>
cv load <snapshot>

The “save” method, analogous to “load”, allows to save all restart information to a state file. This is normally not required during a simulation if colvarsRestartFrequency is defined (either directly or indirectly by the NAMD restart frequency). Because not only a state file (used to continue simulations) but also other data files (used to analyze the trajectory) are written, it is generally recommended to call the save method using a prefix, rather than a complete file name:

cv save "<job>"

3.3.4 Accessing atomic data

For computational efficiency the Colvars module keeps internal copies of the numeric IDs, masses, charges, positions, and optionally total forces of the atoms requested for a Colvars computation. At each simulation step, up-to-date versions of these properties are copied from the central memory of NAMD into the internal memory of the Colvars module. In a post-processing workflow or outside a simulation (e.g. when using VMD), this copy can be carried out as part of the update method:

cv update  
which also performs the (re-)computation of all variables and biases defined in Colvars.

For example, the current sequence of numeric IDs of the atoms processed by Colvars can be obtained as:

cv getatomids  
and their current positions as:
cv getatompositions  
This may prove useful to test the correctness of the coordinates passed to Colvars, particularly in regard to periodic boundary conditions (5.3). There is currently no mechanism to modify the above fields via the scripting interface, but such capability will be added in the future.

A special case are collective variables that are based on centers of mass, or volumetric maps (see e.g. mapTotal). These are often computed by NAMD itself, and any atoms that are used only for such variables will not be included in the lists above.

While running a simulation, or when setting one up in VMD, it is possible to examine all the forces that were last applied by Colvars to the atoms, or are about to be applied:

cv getatomappliedforces  
where the length and order of this sequence matches that provided by the getatomids method. A simpler way of testing the stability of a Colvars configuration before or during a simulation makes use of aggregated data, such as the energy:
cv getenergy  
the root-mean-square of the Colvars applied forces:
cv getatomappliedforcesrms  
or the maximum norm of the applied forces:
cv getatomappliedforcesmax  
which can be matched to a specific atom via its numeric ID obtained as:
cv getatomappliedforcesmaxid  

See 7.1 for a complete list of scripting commands used to manage atomic data and runtime parameters of the Colvars module.

3.3.5 Managing collective variables

After one or more collective variables are defined, they can be accessed with the following syntax.

cv colvar "<name>" <method> arg1 arg2 ...
where “<name>” is the name of the variable.

For example, to recompute the collective variable “xi” after a change in its parameters, the following command can be used:

cv colvar "xi" update 
This ordinarily is not needed during a simulation run, where all variables are recomputed at every step (along with biasing forces acting on them). However, when analyzing an existing trajectory, e.g. in VMD, a call to update is generally required.

While in all typical cases all configuration of the variables is done with the “config” or “configfile” methods, a limited set of changes can be enacted at runtime using:

cv colvar "<name>" modifycvcs arg1 arg2 ...
where each argument is a string passed to the function or functions that are used to compute the variable, and are called colvar components, or CVCs (4.1). For example, a variable “DeltaZ” made of a single “distanceZ” component can be made periodic with a period equal to the unit cell dimension along the Z-axis:
cv colvar "DeltaZ" modifycvcs "period ${Lz}"
Please note that this option is currently limited to changing the values of the polynomial superposition parameters componentCoeff, or of the componentExp to update on the fly, of period, wrapAround or forceNoPBC options for components where it is relevant.

If the variable is computed using many components, it is possible to selectively turn some of them on or off:

cv colvar "<name>" cvcflags <flags>
where “<flags>” is a list of 0/1 values, one per component. This is useful for example when script-based path collective variables in Cartesian coordinates (4.12.3) are used, to minimize computational cost by disabling the computation of terms that are very close to zero.

Important: None of the changes enacted by the “modifycvcs” or “cvcflags” methods will be saved to state files, and will be lost when restarting a simulation, deleting the corresponding collective variable, or resetting the module with the “reset” method.

3.3.6 Applying and analyzing forces on collective variables

As soon as a colvar “xi” and its associated biasing potentials are up to date (i.e. during a MD run, or after the respective “update” methods have been called), the force applied onto the colvar is known and may be accessed through the getappliedforce method:

cv colvar "xi" getappliedforce 
See also the use of the outputAppliedForce option to have this force be saved to file during a simulation.

Aside from the biasing methods already implemented within Colvars (6) this force may be incremented ad hoc, for example as part of a custom restraint implemented by scriptedColvarForces:

cv colvar "xi" addforce <force>
where “<force>” is a scalar or a vector (depending on the type of variable “xi”). If addforce is used during a simulation step, the force added by will be communicated to the corresponding atoms as soon as Colvars communicates all forces to NAMD.

For certain types of variable, the force applied directly on a colvar may be combined with those acting indirectly on it via the interatomic force field, making up the total force. When the outputTotalForce keyword is enabled, or when a biasing method that makes explicit use of the total force is enabled, the total force may be obtained as:

cv colvar "xi" gettotalforce 
Note that not all types of variable support total-force computation, and the value of the total force may not be available immediately within the same simulation step: see the documentation of outputTotalForce for more details.

See 7.2 for a complete list of scripting commands used to manage collective variables.

3.3.7 Managing collective variable biases

Because biases depend only upon data internal to the Colvars module (i.e. they do not need atomic coordinates from NAMD), it is generally easy to create them or update their configuration at any time. For example, given the most current value of the variable “xi”, an already-defined harmonic restraint on it named “h_xi” can be updated as:

cv bias "h_xi" update 
During a running simulation this step is not needed, because an automatic update of each bias is already carried out.

Another circumstance when “update” may be called e.g. as part of the function invoked by scriptedColvarForces, it is executed before any biasing forces are applied to the variables, thus allowing to modify them. This use of “update” is often used e.g. in the definition of custom bias-exchange algorithms as part of the NAMD script. Because a restraint is a relatively light-weight object, the easiest way to change the configuration of an existing bias is deleting it and re-creating it:

# Delete the restraint "harmonic_xi"
cv bias harmonic_xi delete
# Re-define it, but using an updated restraint center
cv config "harmonic {
  name harmonic_xi
  centers ${new_center}
  ...
}"
# Now update it (based on the current value of "xi")
cv bias harmonic_xi update
It is also possible to make the change subject to a condition on the energy of the new bias:
...
cv bias harmonic_xi update
if { [cv bias harmonic_xi energy] < ${E_accept} } {
    ...
}

3.3.8 Loading and saving the state of individual biases

Some types of bias are history-dependent, and the magnitude of their forces depends not only on the values of their corresponding variables, but also on previous simulation history. It is thus useful to load information from a state file that contains information specifically for one bias only, for example:

cv bias "metadynamics1" load "old.colvars.state"
or alternatively, using the prefix of the file instead of its full name:
cv bias "metadynamics1" load "old"
A corresponding “save” function is also available:
cv bias "metadynamics1" save "new"
Please note that the file above must contain only the state information for that particular bias: loading a state file for the whole module is not allowed.

This pair of functions is also used internally by Colvars to implement e.g. multiple-walker metadynamics (6.6.7), but they can be called from a scripted function to implement alternative coupling schemes.

See 7.3 for a complete list of scripting commands used to manage biases.

3.4 Configuration syntax used by the Colvars module

Configuration for the Colvars module is passed using an external file, or inlined as a string in a NAMD script using the Tcl command cv config "...". Configuration lines follow the format “keyword value” or “keyword { ... }”, where the keyword and its value must be separated by one or more space characters. The following formatting rules apply:

  • •

    Keywords are case-insensitive; for example, upperBoundary is the same as upperboundary and UPPERBOUNDARY); note that their string values are however still case-sensitive (e.g. names of variables, file names).

  • •

    A long value, or a list of multiple values, can be distributed across multiple lines by using curly braces, “{” and “}”: the opening brace “{” must occur on the same line as the keyword, following at least one space character; the closing brace “}” may be at any position after that; any keywords following the closing brace on the same line are not valid (they should appear instead on a different line).

  • •

    Many keywords are nested, and are only meaningful within the specific context of another keyword; for example, the keyword name is available inside the block of the keyword colvar {...}, but not outside of it; for every keyword documented in the following, the “parent” keyword that defines such context is also indicated.

  • •

    If a keyword requiring a boolean value (yes|on|true or no|off|false) is provided without an explicit value, it defaults to ‘yes|on|true’; for example, ‘outputAppliedForce’ may be used as shorthand for ‘outputAppliedForce on’.

  • •

    The hash character “#” indicates a comment: all text in the same line following this character will be ignored.

  • •

    Outside of comments, only ASCII characters are allowed for defining keywords, and the only white-space characters supported are spaces, tabs and newlines: a warning will be printed upon detection of non-ASCII characters in a configuration line, which include both characters that are visibly “special”, as well as those with a very similar appearance to ASCII ones (for instance, non-breaking spaces); common ways to identify/remove non-ASCII characters are using the Emacs text editor, or using LC_ALL=C vi.

  • •

    Tcl syntax is generally not available inside a Colvars configuration file/string, but it is possible to use Tcl variables or bracket expansion of commands when configuration is passed via the command cv config "..."; for example, this is particularly useful when combined with NAMD’s parameter introspection, e.g. cv config "colvarsTrajFrequency [DCDFreq]" allows synchronizing the output frequencies of NAMD and Colvars trajectory files. .

3.5 Global keywords

The following keywords are available in the global context of the Colvars configuration, i.e. they are not nested inside other keywords:

  • •

    colvarsTrajFrequency — Colvar value trajectory frequency
    Default: 100 [ positive integer, context: global ]

    The values of each colvar (and of other related quantities, if requested) are written to the file outputName.colvars.traj every these many steps throughout the simulation. If the value is 0, such trajectory file is not written. For optimization the output is buffered, and synchronized with the disk only when the restart file is being written.

  • •

    colvarsRestartFrequency — Colvar module restart frequency
    Default: NAMD parameter restartFreq [ positive integer, context: global ]

    When this value is non-zero, a state file suitable for restarting will be written every these many steps. Additionally, any other output files produced by Colvars are written as well (except the trajectory file, which is written every colvarsTrajFrequency steps). It is generally a good idea to leave this parameter at its default value, unless needed for special cases or to disable automatic writing of output files altogether. If neeed, writing of all output files can still be triggered at any time via the save scripting function.

  • •

    indexFile — Index file for atom selection (GROMACS “ndx” format)
    [ UNIX filename, context: global ]

    This option reads an index file (usually with a .ndx extension) as produced by the make_ndx tool of GROMACS. This keyword may be repeated to load multiple index files. A group with the same name may appear multiple times, as long as it contains the same indices in identical order each time: an error is raised otherwise. The names of index groups contained in this file can then be used to define atom groups with the indexGroup keyword. Other supported methods to select atoms are described in 5.

  • •

    smp — Whether SMP parallelism should be used
    Default: on [ boolean, context: global ]

    If this flag is enabled (default), SMP parallelism over threads will be used to compute variables and biases, provided that this is supported by the NAMD build in use.

  • •

    sourceTclFile — Tcl file to be sourced at startup
    [ UNIX filename, context: global ]

    This option causes the provided Tcl script file to be loaded at startup, e.g. to initialize the Tcl callback procedures for scripted variables or biases.

3.6 Input state file

Several of the sampling methods implemented in Colvars are time- or history-dependent, i.e. they work by accumulating data as a simulation progresses, and use these data to determine their biasing forces. If the simulation engine uses a checkpoint or restart file (as GROMACS and LAMMPS do), any data needed by Colvars are embedded into that file. Otherwise, a dedicated state file can be loaded into Colvars directly.

When a dedicated Colvars state file is used, it may be in either one of two formats:

  • •

    Formatted, i.e. “text” format, which takes more space and is slower to to load/save but is also portable across different platforms and even different simulation engines (save for changes in physical units). This format is used by default, unless explicitly requested otherwise.

  • •

    Unformatted, i.e. “binary” format, which is both space-efficient and quick to load/save, but requires that the same NAMD build was used to write the file and that the Colvars configuration remains the same. This format is supported by Colvars versions starting 2023-09-25. Colvars state files can be written in binary format by setting the environment variable “COLVARS_BINARY_RESTART” to 1.

In either format, the state file contains accumulated data as well as the step number at the end of the run. The step number read from a state file overrides any value that NAMD provides, and will be incremented if the simulation proceeds. This means that the step number used internally by Colvars may not always match the step number reported by NAMD. This is particularly inmportant in NAMD, which represents step numbers as a 32-bit integers that overflows after ∼ 2 billion steps, effectively negating the usefulness of the firstTimeStep keyword. However, step numbers are implemented correctly in the Colvars state file.

3.6.1 Restarting in NAMD.

When the initial configuration is provided using the NAMD keyword colvarsConfig, the colvarsInput NAMD keyword can be used to give the name of a state file, which will be read immediately at startup. On the other hand, for loading an input state file after startup, the Tcl command cv load (see 3.3 and 3.3.3) is also available. Note that the two approaches are not interchangeable: because the colvarsConfig and colvarsInput keywords are processed at NAMD startup, their effect precedes any command included in the Tcl-scripted portion of a NAMD simulation script, including cv load.

To ensure that the Colvars configuration is always loaded before an input state file is read, one should either:

  • •

    use the keywords colvarsConfig and colvarsInput if the Colvars configuration never changes during the NAMD run, or

  • •

    use the Tcl commands cv config (or cv configfile) and cv load to manage Colvars from the Tcl-scripted portion of a NAMD input script.

The colvarsInput keyword and the cv load command both support loading Colvars state files in either binary or text formats.

3.6.2 Changing configuration upon restarting.

In some cases, it is useful to modify the configuration of variables or biases between consecutive runs, for example by adding or removing a restraint. Some special provisions will happen in that case. When a state file is loaded, no information is available about any newly added variable or bias, which will thus remain uninitialized until the first compute step. Conversely, any information that the state file may contain about variables or biases that are no longer defined will be silently ignored. Please note that these checks are performed based only on the names of variables and biases: it is your responsibility to ensure that these names have consistent definitions between runs.

The flexibility just described carries some limitations: namely, it is only supported when reading text-format Colvars state files. Instead, restarting from binary files after a configuration change will trigger an error. It is also important to remind that when switching to a different build of NAMD, the binary format may change slightly, even if the release version is the same.

To work around the potential issues just described, a text-format Colvars state file should be loaded. This is the default in NAMD unless the “COLVARS_BINARY_RESTART” is set to 1, and this information is only provided here for troubleshooting purposes.

3.7 Output files

When the output prefix outputName is defined, the following output files are written during a simulation run:

  • •

    A state file, named outputName.colvars.state, which is written at the end of the specified run, and can also be written at any time with the scripting command save (3.3.3). This file is in plain text format by default, regardless of the value of binaryOutput of the NAMD coordinate and velocity files, or in binary format if the environment variable COLVARS_BINARY_RESTART is set to a non-zero integer. The state file can be used to continue a simulation: unless its contents are embedded in the checkpoint file of the MD engine itself (currently, GROMACS and LAMMPS support this), instructions for loading the Colvars state file will be required in the simulation script (see 3.6).

  • •

    If the parameter colvarsRestartFrequency is larger than zero and the restartName prefix is defined, a restart file is written every that many steps: this file is fully equivalent to the final state file. The name of this file is restartName.colvars.state.

  • •

    If the parameter colvarsTrajFrequency is greater than 0 (default value: 100 steps), a trajectory file, named outputName.colvars.traj, is written during the simulation. Unlike a state file, this file is not needed to restart a simulation, but can be used for post-processing and analysis. The format of this file is described in sec. 3.8.6.

  • •

    Additionally, certain features, when enabled, can emit output files with a specific purpose: for example, potentials of mean force (PMFs) can be written to file to be analyzed or plotted. These files are described in the respective sections, but as a general rule they all use names beginning with the outputName prefix. Like the trajectory file, these additional files are needed only for analyzing a simulation’s results, but not to continue it.

Lastly, the total energy of all biases or restraints applied to the colvars appears under the NAMD standard output, under the MISC column.

3.8 File formats

This section summarizes the file formats of various files that Colvars may be reading or writing.

3.8.1 Configuration and state files.

Configuration files are text files that are generally read as input by NAMD, and may be optionally inlined in a NAMD script (see 3.3.1). Starting from version 2017-02-01, changes in newline encodings are handled transparently, i.e. it is possible to typeset a configuration file in Windows (CR-LF newlines) and then use it with Linux or macOS (LF-only newlines).

Formatted state files, although not written manually, follow otherwise the same text format as configuration files. Binary state files can only be read by the Colvars code itself.

3.8.2 Index (NDX) files

For atom selections that cannot be specified only by using internal Colvars keywords, external index files may also be used following the NDX format used in GROMACS:

[ group_1_name ]
  i1  i2  i3  i4  ...
  ...             ...  iN
[ group_2_name ]
  ...
where i1 through iN are 1-based indices. Each group name may not contain spaces or tabs: otherwise, a parsing error will be raised.

Multiple index files may be provided to Colvars, each using the keyword indexFile. Two index files may contain groups with the same names, however these must also represent identical atom selections, i.e. the same sequence of indices including order.

Other than with GROMACS, an index group may also be generated from the VMD command-line interface, using the helper function write_index_group provided in the colvartools folder:

source colvartools/write_index_group.tcl
set sel [atomselect top "resname XXX and not hydrogen"]
write_index_group indexfile.ndx $sel "Ligand"

3.8.3 XYZ coordinate files

XYZ coordinate files are text files with the extension “.xyz”. They are read by the Colvars module using an internal reader, and expect the following format:

N
Comment line
E1 x1 y1 z1
E2 x2 y2 z2
…
EN xN yN zN

where N is the number of atomic coordinates in the file and Ei is the chemical element of the i-th atom. Because Ei is not used in Colvars, any string that does not contain tabs or spaces is acceptable.

Note: all XYZ coordinates are assumed to be expressed in Å units.

An XYZ file may contain either one of the following scenarios:

  1. (i)

    The file contains as many coordinates as the atoms that they are being read for: all coordinates will be read from the file following the same order as the atoms appear in the selection generated using the keywords listed in section 5. (Note that the order is guaranteed only if a single type of selection keyword is used one or more times, and not guaranteed when different types of selection keywords are used.)

  2. (ii)

    The file contains more coordinates than needed, and it is assumed to contain coordinates for the entire system: only coordinates that match the numeric indices of the selected atoms are read, in order of increasing number.

XYZ-file coordinates are read directly by Colvars and stored internally as double-precision floating point numbers.

3.8.4 PDB coordinate files

PDB coordinate files are read by the Colvars module using existing functionality in NAMD, and therefore follow the same format. The values of the atomic coordinates and other fields, such as occupancy or temperature factors, are then communicated to Colvars by NAMD.

PDB files may be used either as one of the available mechanisms to select atoms (see the atomsFile keyword), or more frequently to read reference coordinates for least-squares fit alignment (see the refPositionsFile keyword).

To select atoms via the atomsFile keyword, the option atomsFile is required, and atoms are selected based on either one of the following cases.

  1. (i)

    All atoms with a non-zero value of the corresponding column are selected.

  2. (ii)

    If and only if atomsColValue is provided, only atoms with a value of atomsCol equaling atomsColValue are selected. This can be useful to reuse the same PDB file for generating multiple selections.

To read coordinates via the refPositionsFile keyword, there are four possible scenarios.

  1. (i)

    The file contains as many coordinates as the atoms that they are being read for: all coordinates will be read from the file following the same order as the atoms appear in the selection generated using the keywords listed in section 5. (Note that the order is guaranteed only if a single type of selection keyword is used one or more times, and not guaranteed when different types of selection keywords are used.)

  2. (ii)

    The file contains more coordinates than needed, and it is assumed to contain coordinates for the entire system: only coordinates that match the numeric indices of the selected atoms are read, in order of increasing number.

  3. (iii)

    If the corresponding refPositionsCol keyword is specified, only positions with a non-zero value of the column refPositionsCol are read. This is particularly useful when all atoms were selected via atomsFile and atomsCol, which can then be the same as refPositionsCol for loading coordinates.

  4. (iv)

    This is a special case of the previous point: if both refPositionsCol and refPositionsColValue are specified, only atoms for which the refPositionsCol column as the value refPositionsColValue are read.

Due to the fixed-precision PDB format, it is not recommended to use PDB files to read coordinates when precision is a concern, and the XYZ format (see 3.8.2) is recommended instead.

3.8.5 Grid files: multicolumn text format

Many simulation methods and analysis tools write files that contain functions of the collective variables tabulated on a grid (e.g. potentials of mean force or multidimensional histograms) for the purpose of analyzing results. Such files are produced by ABF (6.3), metadynamics (6.6), multidimensional histograms (6.13), as well as any restraint with optional thermodynamic integration support (6.2).

In some cases, these files may also be read as input of a new simulation. Suitable input files for this purpose are typically generated as output files of previous simulations, or directly by the user in the specific case of ensemble-biased metadynamics (6.6.5). This section explains the “multicolumn” format used by these files. For a multidimensional function f⁢(ξ1,ξ2,…) the multicolumn grid format is defined as follows:

# Ncv
# 𝚖𝚒𝚗⁢(ξ1) 𝚠𝚒𝚍𝚝𝚑⁢(ξ1) 𝚗𝚙𝚘𝚒𝚗𝚝𝚜⁢(ξ1) 𝚙𝚎𝚛𝚒𝚘𝚍𝚒𝚌⁢(ξ1)
# 𝚖𝚒𝚗⁢(ξ2) 𝚠𝚒𝚍𝚝𝚑⁢(ξ2) 𝚗𝚙𝚘𝚒𝚗𝚝𝚜⁢(ξ2) 𝚙𝚎𝚛𝚒𝚘𝚍𝚒𝚌⁢(ξ2)
# … … … …
# 𝚖𝚒𝚗⁢(ξNcv) 𝚠𝚒𝚍𝚝𝚑⁢(ξNcv) 𝚗𝚙𝚘𝚒𝚗𝚝𝚜⁢(ξNcv) 𝚙𝚎𝚛𝚒𝚘𝚍𝚒𝚌⁢(ξNcv)
ξ11 ξ21 … ξNcv1 f(ξ11, ξ21, …, ξNcv1)
ξ11 ξ21 … ξNcv2 f(ξ11, ξ21, …, ξNcv2)
… … … … …

Lines beginning with the character “#” are the header of the file. Ncv is the number of collective variables sampled by the grid. For each variable ξi, 𝚖𝚒𝚗⁢(ξi) is the lowest value sampled by the grid (i.e. the left-most boundary of the grid along ξi), 𝚠𝚒𝚍𝚝𝚑⁢(ξi) is the width of each grid step along ξi, 𝚗𝚙𝚘𝚒𝚗𝚝𝚜⁢(ξi) is the number of points and 𝚙𝚎𝚛𝚒𝚘𝚍𝚒𝚌⁢(ξi) is a flag whose value is 1 or 0 depending on whether the grid is periodic along ξi. In most situations:

  • •

    𝚖𝚒𝚗⁢(ξi) is given by the lowerBoundary keyword of the variable ξi;

  • •

    𝚠𝚒𝚍𝚝𝚑⁢(ξi) is given by the width keyword;

  • •

    𝚗𝚙𝚘𝚒𝚗𝚝𝚜⁢(ξi) is calculated from the two above numbers and the upperBoundary keyword;

  • •

    𝚙𝚎𝚛𝚒𝚘𝚍𝚒𝚌⁢(ξi) is set to 1 if and only if ξi is periodic and the grids’ boundaries cover its period.

How the grid’s boundaries affect the sequence of points depends on how the contents of the file were computed. In many cases, such as histograms and PMFs computed by metadynamics (6.6.5), the values of ξi in the first few columns correspond to the midpoints of the corresponding bins, i.e. ξ11=𝚖𝚒𝚗⁢(ξi)+𝚠𝚒𝚍𝚝𝚑⁢(ξi)/2. However, there is a slightly different format in PMF files computed by ABF (6.3) or other biases that use thermodynamic integration (6.2). In these cases, it is free-energy gradients that are accumulated on an (npoints)-long grid along each variable ξ: after these gradients are integrated, the resulting PMF is discretized on a slightly larger grid with (npoints+1) points along ξ (unless the interval is periodic). Therefore, the grid’s outer edges extend by 𝚠𝚒𝚍𝚝𝚑⁢(ξi)/2 above and below the specified boundaries, so that for instance 𝚖𝚒𝚗⁢(ξi) in the header appears to be shifted back by 𝚠𝚒𝚍𝚝𝚑⁢(ξi)/2 compared to what would be expected. Please keep this difference in mind when comparing PMFs computed by different methods.

After the header, the rest of the file contains values of the tabulated function f⁢(ξ1,ξ2,…,ξNcv), one for each line. The first Ncv columns contain values of ξ1, ξ2, …ξNcv and the last column contains the value of the function f. Points are sorted in ascending order with the fastest-changing values at the right (“C-style” order). Each sweep of the right-most variable ξNcv is terminated by an empty line. For two dimensional grid files, this allows quick visualization by programs such as GNUplot.

Example 1: multicolumn text file for a one-dimensional histogram with lowerBoundary = 15, upperBoundary = 48 and width = 0.1.

# 1
# 15 0.1 330 0
15.05 6.14012e-07
15.15 7.47644e-07
… …
47.85 1.65944e-06
47.95 1.46712e-06

Example 2: multicolumn text file for a two-dimensional histogram of two dihedral angles (periodic interval with 6∘ bins):

# 2
# -180.0 6.0 30 1
# -180.0 6.0 30 1
-177.0 -177.0 8.97117e-06
-177.0 -171.0 1.53525e-06
… … …
-177.0 177.0 2.442956-06
-171.0 -177.0 2.04702e-05
… … …

3.8.6 Output trajectory files

The Colvars trajectory file (with a suffix .colvars.traj) is a plain text file (scientific notation with 14-digit precision) whose columns represent quantities such as colvar values, applied forces, or individual restraints’ energies. Under most scenarios, plotting or analyzing this file is straightforward: for example, the following contains a variable “A” and the energy of a restraint “r⁢A”:

#       step   A                     E_rA
           0    1.42467449615693e+01  6.30982865292123e+02
         100    1.42282559728026e+01  6.20640585041317e+02
…

Occasionally, if the Colvars configuration is changed mid-run certain quantities may be added or removed, changing the column layout. Labels in comment lines can assist in such cases: for example, consider the trajectory above with the addition of a second variable, “B”, after 10,000 steps:

#       step   A                     E_rA
           0    1.42467449615693e+01  6.30982865292123e+02
         100    1.42282559728026e+01  6.20640585041317e+02
…
#       step   A                     B                     E_rA              
       10000    1.38136915830229e+01  9.99574098859265e-01  4.11184644791030e+02
       10100    1.36437184346326e+01  9.99574091957314e-01  3.37726286543895e+02

Analyzing the above file with standard tools is possible, but laborious: a convenience script is provided for this and related purposes. It may be used either as a command-line tool or as a Python module:

>>> from plot_colvars_traj import Colvars_traj
>>> traj = Colvars_traj('test.colvars.traj')
>>> print(traj['A'].steps, traj['A'].values)
[    0   100  ...  10000 10100] [14.246745 14.228256 ... 13.813692 13.643718]
>>> print(traj['B'].steps, traj['B'].values)
[10000 10100] [0.999574  0.9995741]