CustomCVForce¶
- class openmm.openmm.CustomCVForce(*args)¶
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, 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 a TabulatedFunction object. That function can then appear in the expression.
- __init__(self, energy) → CustomCVForce¶
- __init__(self, other) → CustomCVForce
Create a CustomCVForce.
- Parameters
energy (string) – an algebraic expression giving the energy of the system as a function of the collective variables and global parameters
Methods
__init__
(-> CustomCVForce)Create a CustomCVForce.
addCollectiveVariable
(self, name, variable)Add a collective variable that the force may depend on.
addEnergyParameterDerivative
(self, name)Request that this Force compute the derivative of its energy with respect to a global parameter.
addGlobalParameter
(self, name, defaultValue)Add a new global parameter that the interaction may depend on.
addTabulatedFunction
(self, name, function)Add a tabulated function that may appear in the energy expression.
getCollectiveVariable
(-> Force)Get a const reference to the Force object that computes a collective variable.
getCollectiveVariableName
(self, index)Get the name of a collective variable.
getCollectiveVariableValues
(self, context)Get the current values of the collective variables in a Context.
getEnergyFunction
(self)Get the algebraic expression that gives the energy of the system
getEnergyParameterDerivativeName
(self, index)Get the name of a global parameter with respect to which this Force should compute the derivative of the energy.
getForceGroup
(self)Get the force group this Force belongs to.
getGlobalParameterDefaultValue
(self, index)Get the default value of a global parameter.
getGlobalParameterName
(self, index)Get the name of a global parameter.
getInnerContext
(self, context)Get the inner Context used for evaluating collective variables.
getName
(self)Get the name of this Force.
Get the number of collective variables that the interaction depends on.
Get the number of global parameters with respect to which the derivative of the energy should be computed.
getNumGlobalParameters
(self)Get the number of global parameters that the interaction depends on.
getNumTabulatedFunctions
(self)Get the number of tabulated functions that have been defined.
getTabulatedFunction
(-> TabulatedFunction)Get a reference to a tabulated function that may appear in the energy expression.
getTabulatedFunctionName
(self, index)Get the name of a tabulated function that may appear in the energy expression.
setEnergyFunction
(self, energy)Set the algebraic expression that gives the energy of the system
setForceGroup
(self, group)Set the force group this Force belongs to.
setGlobalParameterDefaultValue
(self, index, …)Set the default value of a global parameter.
setGlobalParameterName
(self, index, name)Set the name of a global parameter.
setName
(self, name)Set the name of this Force.
updateParametersInContext
(self, context)Update the tabulated function parameters in a Context to match those stored in this Force object.
Returns whether or not this force makes use of periodic boundary conditions.
Attributes
The membership flag
- property thisown¶
The membership flag
- getNumCollectiveVariables(self) → int¶
Get the number of collective variables that the interaction depends on.
- getNumGlobalParameters(self) → int¶
Get the number of global parameters that the interaction depends on.
- getNumEnergyParameterDerivatives(self) → int¶
Get the number of global parameters with respect to which the derivative of the energy should be computed.
- getNumTabulatedFunctions(self) → int¶
Get the number of tabulated functions that have been defined.
- getEnergyFunction(self) → std::string const &¶
Get the algebraic expression that gives the energy of the system
- setEnergyFunction(self, energy)¶
Set the algebraic expression that gives the energy of the system
- addCollectiveVariable(self, name, variable) → int¶
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.
- Parameters
name (string) – the name of the collective variable, as it will appear in the energy expression
variable (Force *) – 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
- Return type
int
- getCollectiveVariableName(self, index) → std::string const &¶
Get the name of a collective variable.
- Parameters
index (int) – the index of the collective variable for which to get the name
- Returns
the variable name
- Return type
string
- getCollectiveVariable(self, index) → Force¶
- getCollectiveVariable(self, index) → Force
Get a const reference to the Force object that computes a collective variable.
- Parameters
index (int) – the index of the collective variable to get
- Returns
the Force object
- Return type
- addGlobalParameter(self, name, defaultValue) → int¶
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 (string) – the name of the parameter
defaultValue (double) – the default value of the parameter
- Returns
the index of the parameter that was added
- Return type
int
- getGlobalParameterName(self, index) → std::string const &¶
Get the name of a global parameter.
- Parameters
index (int) – the index of the parameter for which to get the name
- Returns
the parameter name
- Return type
string
- setGlobalParameterName(self, index, name)¶
Set the name of a global parameter.
- Parameters
index (int) – the index of the parameter for which to set the name
name (string) – the name of the parameter
- getGlobalParameterDefaultValue(self, index) → double¶
Get the default value of a global parameter.
- Parameters
index (int) – the index of the parameter for which to get the default value
- Returns
the parameter default value
- Return type
double
- setGlobalParameterDefaultValue(self, index, defaultValue)¶
Set the default value of a global parameter.
- Parameters
index (int) – the index of the parameter for which to set the default value
defaultValue (double) – the default value of the parameter
- addEnergyParameterDerivative(self, 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().
- Parameters
name (string) – the name of the parameter
- getEnergyParameterDerivativeName(self, index) → std::string const &¶
Get the name of a global parameter with respect to which this Force should compute the derivative of the energy.
- Parameters
index (int) – the index of the parameter derivative, between 0 and getNumEnergyParameterDerivatives()
- Returns
the parameter name
- Return type
string
- addTabulatedFunction(self, name, function) → int¶
Add a tabulated function that may appear in the energy expression.
- Parameters
name (string) – the name of the function as it appears in expressions
function (TabulatedFunction *) – a TabulatedFunction object defining the function. The TabulatedFunction should have been created on the heap with the “new” operator. The Force takes over ownership of it, and deletes it when the Force itself is deleted.
- Returns
the index of the function that was added
- Return type
int
- getTabulatedFunction(self, index) → TabulatedFunction¶
- getTabulatedFunction(self, index) → TabulatedFunction
Get a reference to a tabulated function that may appear in the energy expression.
- Parameters
index (int) – the index of the function to get
- Returns
the TabulatedFunction object defining the function
- Return type
- getTabulatedFunctionName(self, index) → std::string const &¶
Get the name of a tabulated function that may appear in the energy expression.
- Parameters
index (int) – the index of the function to get
- Returns
the name of the function as it appears in expressions
- Return type
string
- getCollectiveVariableValues(self, context)¶
Get the current values of the collective variables in a Context.
- Parameters
context (Context) – the Context for which to get the values
- Returns
values – the values of the collective variables are computed and stored into this
- Return type
vector< double >
- getInnerContext(self, 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.
- updateParametersInContext(self, 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.
- usesPeriodicBoundaryConditions(self) → bool¶
Returns whether or not this force makes use of periodic boundary conditions.
- Returns
true if force uses PBC and false otherwise
- Return type
bool
- getForceGroup(self) → int¶
Get the force group this Force belongs to.
- getName(self) → std::string const &¶
Get the name of this Force. This is an arbitrary, user modifiable identifier. By default it equals the class name, but you can change it to anything useful.
- setForceGroup(self, group)¶
Set the force group this Force belongs to.
- Parameters
group (int) – the group index. Legal values are between 0 and 31 (inclusive).
- setName(self, name)¶
Set the name of this Force. This is an arbitrary, user modifiable identifier. By default it equals the class name, but you can change it to anything useful.