CustomCVForce¶

class OpenMM::CustomCVForce

This class supports energy functions that depend on collective variables. To use it, you define a set of collective variables (scalar valued functions that depend on the particle positions), and an algebraic expression for the energy as a function of the collective variables. The expression also may involve tabulated functions, and may depend on arbitrary global parameters.

Each collective variable is defined by a Force object. The Force‘s potential energy is computed, and that becomes the value of the variable. This provides enormous flexibility in defining collective variables, especially by using custom forces. Anything that can be computed as a potential function can also be used as a collective variable.

To use this class, create a CustomCVForce object, passing an algebraic expression to the constructor that defines the potential energy. Then call addCollectiveVariable() to define collective variables and addGlobalParameter() to define global parameters. The values of global parameters may be modified during a simulation by calling Context::setParameter().

This class also has the ability to compute derivatives of the potential energy with respect to global parameters. Call addEnergyParameterDerivative() to request that the derivative with respect to a particular parameter be computed. You can then query its value in a Context by calling getState() on it.

Expressions may involve the operators + (add), - (subtract), * (multiply), / (divide), and ^ (power), and the following functions: sqrt, exp, log, sin, cos, sec, csc, tan, cot, asin, acos, atan, sinh, cosh, tanh, erf, erfc, min, max, abs, floor, ceil, step, delta, select. All trigonometric functions are defined in radians, and log is the natural logarithm. step(x) = 0 if x is less than 0, 1 otherwise. delta(x) = 1 if x is 0, 0 otherwise. select(x,y,z) = z if x = 0, y otherwise.

In addition, you can call addTabulatedFunction() to define a new function based on tabulated values. You specify the function by creating a TabulatedFunction object. That function can then appear in the expression.

Methods

 CustomCVForce Create a CustomCVForce. ~CustomCVForce getNumCollectiveVariables Get the number of collective variables that the interaction depends on. getNumGlobalParameters Get the number of global parameters that the interaction depends on. getNumEnergyParameterDerivatives Get the number of global parameters with respect to which the derivative of the energy should be computed. getNumTabulatedFunctions Get the number of tabulated functions that have been defined. getEnergyFunction Get the algebraic expression that gives the energy of the system setEnergyFunction Set the algebraic expression that gives the energy of the system addCollectiveVariable Add a collective variable that the force may depend on. getCollectiveVariableName Get the name of a collective variable. getCollectiveVariable Get a writable reference to the Force object that computes a collective variable. getCollectiveVariable Get a const reference to the Force object that computes a collective variable. addGlobalParameter Add a new global parameter that the interaction may depend on. getGlobalParameterName Get the name of a global parameter. setGlobalParameterName Set the name of a global parameter. getGlobalParameterDefaultValue Get the default value of a global parameter. setGlobalParameterDefaultValue Set the default value of a global parameter. addEnergyParameterDerivative Request that this Force compute the derivative of its energy with respect to a global parameter. getEnergyParameterDerivativeName Get the name of a global parameter with respect to which this Force should compute the derivative of the energy. addTabulatedFunction Add a tabulated function that may appear in the energy expression. getTabulatedFunction Get a const reference to a tabulated function that may appear in the energy expression. getTabulatedFunction Get a reference to a tabulated function that may appear in the energy expression. getTabulatedFunctionName Get the name of a tabulated function that may appear in the energy expression. getCollectiveVariableValues Get the current values of the collective variables in a Context. getInnerContext Get the inner Context used for evaluating collective variables. updateParametersInContext Update the tabulated function parameters in a Context to match those stored in this Force object. usesPeriodicBoundaryConditions Returns whether or not this force makes use of periodic boundary conditions.
CustomCVForce(const std::string &energy)

Create a CustomCVForce().

• energy – an algebraic expression giving the energy of the system as a function of the collective variables and global parameters
~CustomCVForce()
int getNumCollectiveVariables() const

Get the number of collective variables that the interaction depends on.

int getNumGlobalParameters() const

Get the number of global parameters that the interaction depends on.

int getNumEnergyParameterDerivatives() const

Get the number of global parameters with respect to which the derivative of the energy should be computed.

int getNumTabulatedFunctions() const

Get the number of tabulated functions that have been defined.

const std::string &getEnergyFunction() const

Get the algebraic expression that gives the energy of the system

void setEnergyFunction(const std::string &energy)

Set the algebraic expression that gives the energy of the system

int addCollectiveVariable(const std::string &name, Force *variable)

Add a collective variable that the force may depend on. The collective variable is represented by a Force object, which should have been created on the heap with the “new” operator. The CustomCVForce takes over ownership of it, and deletes the Force when the CustomCVForce itself is deleted.

• name – the name of the collective variable, as it will appear in the energy expression
• variable – the collective variable, represented by a Force object. The value of the variable is the energy computed by the Force.
Returns: the index within the Force of the variable that was added
const std::string &getCollectiveVariableName(int index) const

Get the name of a collective variable.

• index – the index of the collective variable for which to get the name
Returns: the variable name
Force &getCollectiveVariable(int index)

Get a writable reference to the Force object that computes a collective variable.

• index – the index of the collective variable to get
Returns: the Force object
const Force &getCollectiveVariable(int index) const

Get a const reference to the Force object that computes a collective variable.

• index – the index of the collective variable to get
Returns: the Force object
int addGlobalParameter(const std::string &name, double defaultValue)

Add a new global parameter that the interaction may depend on. The default value provided to this method is the initial value of the parameter in newly created Contexts. You can change the value at any time by calling setParameter() on the Context.

• name – the name of the parameter
• defaultValue – the default value of the parameter
Returns: the index of the parameter that was added
const std::string &getGlobalParameterName(int index) const

Get the name of a global parameter.

• index – the index of the parameter for which to get the name
Returns: the parameter name
void setGlobalParameterName(int index, const std::string &name)

Set the name of a global parameter.

• index – the index of the parameter for which to set the name
• name – the name of the parameter
double getGlobalParameterDefaultValue(int index) const

Get the default value of a global parameter.

• index – the index of the parameter for which to get the default value
Returns: the parameter default value
void setGlobalParameterDefaultValue(int index, double defaultValue)

Set the default value of a global parameter.

• index – the index of the parameter for which to set the default value
• defaultValue – the default value of the parameter
void addEnergyParameterDerivative(const std::string &name)

Request that this Force compute the derivative of its energy with respect to a global parameter. The parameter must have already been added with addGlobalParameter().

• name – the name of the parameter
const std::string &getEnergyParameterDerivativeName(int index) const

Get the name of a global parameter with respect to which this Force should compute the derivative of the energy.

Returns: the parameter name
int addTabulatedFunction(const std::string &name, TabulatedFunction *function)

Add a tabulated function that may appear in the energy expression.

Returns: the index of the function that was added
const TabulatedFunction &getTabulatedFunction(int index) const

Get a const reference to a tabulated function that may appear in the energy expression.

• index – the index of the function to get
Returns: the TabulatedFunction object defining the function
TabulatedFunction &getTabulatedFunction(int index)

Get a reference to a tabulated function that may appear in the energy expression.

• index – the index of the function to get
Returns: the TabulatedFunction object defining the function
const std::string &getTabulatedFunctionName(int index) const

Get the name of a tabulated function that may appear in the energy expression.

• index – the index of the function to get
Returns: the name of the function as it appears in expressions
void getCollectiveVariableValues(Context &context, std::vector<double> &values)

Get the current values of the collective variables in a Context.

• context – the Context for which to get the values
• values – [out] the values of the collective variables are computed and stored into this
Context &getInnerContext(Context &context)

Get the inner Context used for evaluating collective variables.

When you create a Context for a System that contains a CustomCVForce, internally it creates a new System, adds the Forces that define the CVs to it, creates a new Context for that System, and uses it to evaluate the variables. In most cases you can ignore all of this. It is just an implementation detail. However, there are a few cases where you need to directly access that internal Context. For example, if you want to modify one of the Forces that defines a collective variable and call updateParametersInContext() on it, you need to pass that inner Context to it. This method returns a reference to it.

Returns: the inner Context used to evaluate the collective variables
void updateParametersInContext(Context &context)

Update the tabulated function parameters in a Context to match those stored in this Force object. This method provides an efficient method to update certain parameters in an existing Context without needing to reinitialize it. Simply call getTabulatedFunction(index).setFunctionParameters() to modify this object’s parameters, then call updateParametersInContext() to copy them over to the Context.

This method is very limited. The only information it updates is the parameters of tabulated functions. All other aspects of the Force (the energy expression, the set of collective variables, etc.) are unaffected and can only be changed by reinitializing the Context.

bool usesPeriodicBoundaryConditions() const

Returns whether or not this force makes use of periodic boundary conditions.

Returns: true if force uses PBC and false otherwise