Package ffx.openmm

Class CustomExternalForce

java.lang.Object
ffx.openmm.Force
ffx.openmm.CustomExternalForce
Direct Known Subclasses:
RestrainPositionsForce

public class CustomExternalForce extends Force
This class implements an "external" force on particles. The force may be applied to any subset of the particles in the System. The force on each particle is specified by an arbitrary algebraic expression, which may depend on the current position of the particle as well as on arbitrary global and per-particle parameters.

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.

  • Constructor Details

    • CustomExternalForce

      public CustomExternalForce(String energy)
      Create a CustomExternalForce.
      Parameters:
      energy - The energy expression for the force.
  • Method Details

    • addGlobalParameter

      public int addGlobalParameter(String name, double defaultValue)
      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

      public int addParticle(int index, DoubleArray particleParameters)
      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

      public int addPerParticleParameter(String name)
      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.
      Specified by:
      destroy in class Force
    • getEnergyFunction

      public String 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

      public String getGlobalParameterName(int index)
      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

      public void getParticleParameters(int index, IntBuffer 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).
    • 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

      public String getPerParticleParameterName(int index)
      Get the name of a per-particle parameter.
      Parameters:
      index - The index of the parameter.
      Returns:
      The name of the parameter.
    • setEnergyFunction

      public void setEnergyFunction(String energy)
      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

      public void setGlobalParameterName(int index, String name)
      Set the name of a global parameter.
      Parameters:
      index - The index of the parameter.
      name - The name of the parameter.
    • setParticleParameters

      public void setParticleParameters(int index, int particle, DoubleArray particleParameters)
      Set the parameters for a particle.
      Parameters:
      index - The index of the particle.
      particle - The index of the particle.
      particleParameters - The particle parameters.
    • setPerParticleParameterName

      public void setPerParticleParameterName(int index, String name)
      Set the name of a per-particle parameter.
      Parameters:
      index - The index of the parameter.
      name - The name of the parameter.
    • updateParametersInContext

      public void updateParametersInContext(Context context)
      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 class Force
      Returns:
      True if the force uses periodic boundary conditions.