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. 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
CustomCVForce
object, 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 aContext
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, atan2, 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 aTabulatedFunction
object. That function can then appear in the expression.Methods
Create a
CustomCVForce
.Get the number of collective variables that the interaction depends on.
Get the number of global parameters that the interaction depends on.
Get the number of global parameters with respect to which the derivative of the energy should be computed.
Get the number of tabulated functions that have been defined.
Get the algebraic expression that gives the energy of the system
Set the algebraic expression that gives the energy of the system
Add a collective variable that the force may depend on.
Get the name of a collective variable.
Get a writable reference to the
Force
object that computes a collective variable.Get a const reference to the
Force
object that computes a collective variable.Add a new global parameter that the interaction may depend on.
Get the name of a global parameter.
Set the name of a global parameter.
Get the default value of a global parameter.
Set the default value of a global parameter.
Request that this
Force
compute the derivative of its energy with respect to a global parameter.Get the name of a global parameter with respect to which this
Force
should compute the derivative of the energy.Add a tabulated function that may appear in the energy expression.
Get a const reference to a tabulated function that may appear in the energy expression.
Get a reference to a tabulated function that may appear in the energy expression.
Get the name of a tabulated function that may appear in the energy expression.
Get the current values of the collective variables in a
Context
.Get the inner
Context
used for evaluating collective variables.Update the tabulated function parameters in a
Context
to match those stored in thisForce
object.Returns 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
Force
object, which should have been created on the heap with the “new” operator. TheCustomCVForce
takes over ownership of it, and deletes theForce
when theCustomCVForce
itself 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
Force
object. The value of the variable is the energy computed by theForce
.
- 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.
- 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
Force
object that computes a collective variable.- Parameters
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.- Parameters
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
.- 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
Force
compute 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
Force
should compute the derivative of the energy.- Parameters
index – the index of the parameter derivative, between 0 and
getNumEnergyParameterDerivatives()
- Returns
the parameter name
-
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
TabulatedFunction
object defining the function. TheTabulatedFunction
should have been created on the heap with the “new” operator. TheForce
takes over ownership of it, and deletes it when theForce
itself 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
TabulatedFunction
object 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
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.
- 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
Context
for which to get the valuesvalues – [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 aSystem
that contains aCustomCVForce
, internally it creates a newSystem
, adds the Forces that define the CVs to it, creates a newContext
for 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 callupdateParametersInContext()
on it, you need to pass that innerContext
to it. This method returns a reference to it.- Parameters
context – the
Context
containing theCustomCVForce
- 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 thisForce
object. This method provides an efficient method to update certain parameters in an existingContext
without needing to reinitialize it. Simply call getTabulatedFunction(index).setFunctionParameters() to modify this object’s parameters, then callupdateParametersInContext()
to copy them over to theContext
.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 theContext
.
-
bool
usesPeriodicBoundaryConditions
() const¶ Returns whether or not this force makes use of periodic boundary conditions.
- Returns
true if force uses PBC and false otherwise
-