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
Forceobject. TheForce‘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
CustomCVForceobject, passing an algebraic expression to the constructor that defines the potential energy. Then calladdCollectiveVariable()to define collective variables andaddGlobalParameter()to define global parameters. The values of global parameters may be modified during a simulation by callingContext::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 aContextby 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 aTabulatedFunctionobject. That function can then appear in the expression.Methods
CustomCVForceCreate a CustomCVForce.~CustomCVForcegetNumCollectiveVariablesGet the number of collective variables that the interaction depends on. getNumGlobalParametersGet the number of global parameters that the interaction depends on. getNumEnergyParameterDerivativesGet the number of global parameters with respect to which the derivative of the energy should be computed. getNumTabulatedFunctionsGet the number of tabulated functions that have been defined. getEnergyFunctionGet the algebraic expression that gives the energy of the system setEnergyFunctionSet the algebraic expression that gives the energy of the system addCollectiveVariableAdd a collective variable that the force may depend on. getCollectiveVariableNameGet the name of a collective variable. getCollectiveVariableGet a writable reference to the Forceobject that computes a collective variable.getCollectiveVariableGet a const reference to the Forceobject that computes a collective variable.addGlobalParameterAdd a new global parameter that the interaction may depend on. getGlobalParameterNameGet the name of a global parameter. setGlobalParameterNameSet the name of a global parameter. getGlobalParameterDefaultValueGet the default value of a global parameter. setGlobalParameterDefaultValueSet the default value of a global parameter. addEnergyParameterDerivativeRequest that this Forcecompute the derivative of its energy with respect to a global parameter.getEnergyParameterDerivativeNameGet the name of a global parameter with respect to which this Forceshould compute the derivative of the energy.addTabulatedFunctionAdd a tabulated function that may appear in the energy expression. getTabulatedFunctionGet a const reference to a tabulated function that may appear in the energy expression. getTabulatedFunctionGet a reference to a tabulated function that may appear in the energy expression. getTabulatedFunctionNameGet the name of a tabulated function that may appear in the energy expression. getCollectiveVariableValuesGet the current values of the collective variables in a Context.getInnerContextGet the inner Contextused for evaluating collective variables.usesPeriodicBoundaryConditionsReturns whether or not this force makes use of periodic boundary conditions. -
CustomCVForce(const std::string &energy)¶ Create a
CustomCVForce().Parameters: - 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
Forceobject, which should have been created on the heap with the “new” operator. TheCustomCVForcetakes over ownership of it, and deletes theForcewhen theCustomCVForceitself is deleted.Parameters: - name – the name of the collective variable, as it will appear in the energy expression
- variable – the collective variable, represented by a
Forceobject. The value of the variable is the energy computed by theForce.
Returns: the index within the Forceof the variable that was added
-
const std::string &
getCollectiveVariableName(int index) const¶ Get the name of a collective variable.
Parameters: - 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
Forceobject that computes a collective variable.Parameters: - index – the index of the collective variable to get
Returns: the Forceobject
-
const Force &
getCollectiveVariable(int index) const¶ Get a const reference to the
Forceobject that computes a collective variable.Parameters: - index – the index of the collective variable to get
Returns: the Forceobject
-
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.Parameters: - 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.
Parameters: - 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.
Parameters: - 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.
Parameters: - 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.
Parameters: - 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
Forcecompute the derivative of its energy with respect to a global parameter. The parameter must have already been added withaddGlobalParameter().Parameters: - 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
Forceshould compute the derivative of the energy.Parameters: - index – the index of the parameter derivative, between 0 and
getNumEnergyParameterDerivatives()
Returns: the parameter name - index – the index of the parameter derivative, between 0 and
-
int
addTabulatedFunction(const std::string &name, TabulatedFunction *function)¶ Add a tabulated function that may appear in the energy expression.
Parameters: - name – the name of the function as it appears in expressions
- function – a
TabulatedFunctionobject defining the function. TheTabulatedFunctionshould have been created on the heap with the “new” operator. TheForcetakes over ownership of it, and deletes it when theForceitself is deleted.
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.
Parameters: - index – the index of the function to get
Returns: the TabulatedFunctionobject defining the function
-
TabulatedFunction &
getTabulatedFunction(int index)¶ Get a reference to a tabulated function that may appear in the energy expression.
Parameters: - index – the index of the function to get
Returns: the TabulatedFunctionobject defining the function
-
const std::string &
getTabulatedFunctionName(int index) const¶ Get the name of a tabulated function that may appear in the energy expression.
Parameters: - 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.Parameters: - context – the
Contextfor which to get the values - values – [out] the values of the collective variables are computed and stored into this
- context – the
-
Context &
getInnerContext(Context &context)¶ Get the inner
Contextused for evaluating collective variables.When you create a
Contextfor aSystemthat contains aCustomCVForce, internally it creates a newSystem, adds the Forces that define the CVs to it, creates a newContextfor thatSystem, 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 internalContext. 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 innerContextto it. This method returns a reference to it.Parameters: - context – the
Contextcontaining theCustomCVForce
Returns: the inner Contextused to evaluate the collective variables- context – the
-
bool
usesPeriodicBoundaryConditions() const¶ Returns whether or not this force makes use of periodic boundary conditions.
Returns: true if force uses PBC and false otherwise
-