Class CustomExternalForce
- Direct Known Subclasses:
RestrainPositionsForce
To use this class, create a CustomExternalForce object, passing an algebraic expression to the constructor that defines the potential energy of each affected particle. The expression may depend on the particle's x, y, and z coordinates, as well as on any parameters you choose. Then call addPerParticleParameter() to define per-particle parameters, and addGlobalParameter() to define global parameters. The values of per-particle parameters are specified as part of the system definition, while values of global parameters may be modified during a simulation by calling Context::setParameter(). Finally, call addParticle() once for each particle that should be affected by the force. After a particle has been added, you can modify its parameters by calling setParticleParameters(). This will have no effect on Contexts that already exist unless you call updateParametersInContext().
As an example, the following code creates a CustomExternalForce that attracts each particle to a target position (x0, y0, z0) via a harmonic potential:
CustomExternalForce* force = new CustomExternalForce("k*((x-x0)^2+(y-y0)^2+(z-z0)^2)");
This force depends on four parameters: the spring constant k and equilibrium coordinates x0, y0, and z0. The following code defines these parameters:
force->addGlobalParameter("k", 100.0);
force->addPerParticleParameter("x0");
force->addPerParticleParameter("y0");
force->addPerParticleParameter("z0");
Special care is needed in systems that use periodic boundary conditions. In that case, each particle really represents an infinite set of particles repeating through space. The variables x, y, and z contain the coordinates of one of those periodic copies, but there is no guarantee about which. It might even change from one time step to the next. You can handle this situation by using the function periodicdistance(x1, y1, z1, x2, y2, z2), which returns the minimum distance between periodic copies of the points (x1, y1, z1) and (x2, y2, z2). For example, the force given above would be rewritten as
CustomExternalForce* force = new CustomExternalForce("k*periodicdistance(x, y, z, x0, y0, z0)^2");
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.
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
addGlobalParameter
(String name, double defaultValue) Add a global parameter that the interaction may depend on.int
addParticle
(int index, DoubleArray particleParameters) Add a particle to the force.int
Add a per-particle parameter that the interaction may depend on.void
destroy()
Destroy the force.Get the energy expression for the force.double
getGlobalParameterDefaultValue
(int index) Get the default value of a global parameter.getGlobalParameterName
(int index) Get the name of a global parameter.int
Get the number of global parameters.int
Get the number of particles.int
Get the number of per-particle parameters.void
getParticleParameters
(int index, com.sun.jna.ptr.IntByReference particle, DoubleArray particleParameters) Get the parameters for a particle.void
getParticleParameters
(int index, IntBuffer particle, DoubleArray particleParameters) Get the parameters for a particle.getPerParticleParameterName
(int index) Get the name of a per-particle parameter.void
setEnergyFunction
(String energy) Set the energy expression for the force.void
setGlobalParameterDefaultValue
(int index, double defaultValue) Set the default value of a global parameter.void
setGlobalParameterName
(int index, String name) Set the name of a global parameter.void
setParticleParameters
(int index, int particle, DoubleArray particleParameters) Set the parameters for a particle.void
setPerParticleParameterName
(int index, String name) Set the name of a per-particle parameter.void
updateParametersInContext
(Context context) Update the parameters in the context.boolean
Check if the force uses periodic boundary conditions.Methods inherited from class ffx.openmm.Force
getForceGroup, getForceIndex, getName, getPointer, setForceGroup, setForceIndex, setName
-
Constructor Details
-
CustomExternalForce
Create a CustomExternalForce.- Parameters:
energy
- The energy expression for the force.
-
-
Method Details
-
addGlobalParameter
Add a global parameter that the interaction may depend on.- Parameters:
name
- The name of the parameter.defaultValue
- The default value of the parameter.- Returns:
- The index of the parameter that was added.
-
addParticle
Add a particle to the force.- Parameters:
index
- The particle index.particleParameters
- The particle parameters.- Returns:
- The index of the particle that was added.
-
addPerParticleParameter
Add a per-particle parameter that the interaction may depend on.- Parameters:
name
- The name of the parameter.- Returns:
- The index of the parameter that was added.
-
destroy
public void destroy()Destroy the force. -
getEnergyFunction
Get the energy expression for the force.- Returns:
- The energy expression for the force.
-
getGlobalParameterDefaultValue
public double getGlobalParameterDefaultValue(int index) Get the default value of a global parameter.- Parameters:
index
- The index of the parameter.- Returns:
- The default value of the parameter.
-
getGlobalParameterName
Get the name of a global parameter.- Parameters:
index
- The index of the parameter.- Returns:
- The name of the parameter.
-
getNumGlobalParameters
public int getNumGlobalParameters()Get the number of global parameters.- Returns:
- The number of global parameters.
-
getNumParticles
public int getNumParticles()Get the number of particles.- Returns:
- The number of particles.
-
getNumPerParticleParameters
public int getNumPerParticleParameters()Get the number of per-particle parameters.- Returns:
- The number of per-particle parameters.
-
getParticleParameters
Get the parameters for a particle.- Parameters:
index
- The index of the particle.particle
- The index of the particle (output).particleParameters
- The particle parameters (output).
-
getParticleParameters
public void getParticleParameters(int index, com.sun.jna.ptr.IntByReference particle, DoubleArray particleParameters) Get the parameters for a particle.- Parameters:
index
- The index of the particle.particle
- The index of the particle (output).particleParameters
- The particle parameters (output).
-
getPerParticleParameterName
Get the name of a per-particle parameter.- Parameters:
index
- The index of the parameter.- Returns:
- The name of the parameter.
-
setEnergyFunction
Set the energy expression for the force.- Parameters:
energy
- The energy expression for the force.
-
setGlobalParameterDefaultValue
public void setGlobalParameterDefaultValue(int index, double defaultValue) Set the default value of a global parameter.- Parameters:
index
- The index of the parameter.defaultValue
- The default value of the parameter.
-
setGlobalParameterName
Set the name of a global parameter.- Parameters:
index
- The index of the parameter.name
- The name of the parameter.
-
setParticleParameters
Set the parameters for a particle.- Parameters:
index
- The index of the particle.particle
- The index of the particle.particleParameters
- The particle parameters.
-
setPerParticleParameterName
Set the name of a per-particle parameter.- Parameters:
index
- The index of the parameter.name
- The name of the parameter.
-
updateParametersInContext
Update the parameters in the context.- Parameters:
context
- The context to update.
-
usesPeriodicBoundaryConditions
public boolean usesPeriodicBoundaryConditions()Check if the force uses periodic boundary conditions.- Overrides:
usesPeriodicBoundaryConditions
in classForce
- Returns:
- True if the force uses periodic boundary conditions.
-