| pyPot |
index |
PyPot: base class used by potential terms written in Python.
to write a potential term in Python, define a class which derives from
pyPot.PyPot, and make sure to call the base class' constructor.
Then, the new class should define two methods:
calcEnergy, and calcEnergyAndDerivList(derivList). The constructor is
PyPot(self,name,defaultSimulation=None). If simulation is omitted, it
defaults to simulation.currentSimulation().
Note that the derivList argument contains the sum of gradients up to
the position of the current term in a potList, so this should be
added to, and not cleared, or wholesale scaled in any user-defined
energy term. The derivList argument is indexed by atom, as seen below.
Alternatively, a method named calcEnergyAndDerivs(indexDerivList) can be
defined, where the indexing is instead done via atom index in the PyPot's
simulation.
Here is a simple example of a potential term for a single bond:
from pyPot import PyPot ; from vec3 import norm, Vec3
class BondPot(PyPot):
''' example class to evaluate energy, derivs of a single bond
'''
def __init__(self,name,atom1,atom2,length,forcec=1):
''' constructor - force constant is optional.'''
PyPot.__init__(self,name) #first call base class constructor
self.a1 = atom1 ; self.a2 = atom2
self.length = length; self.forcec = forcec
return
def calcEnergy(self):
self.q1 = self.a1.pos() ; self.q2 = self.a2.pos()
self.dist = norm(self.q1-self.q2)
return 0.5 * self.scale() * self.forcec * (self.dist-self.length)**2
def calcEnergyAndDerivList(self,derivs):
energy = self.calcEnergy()
deriv1 = Vec3(map(lambda x,y:x*y,
[self.forcec * (self.dist-self.length) / self.dist]*3 ,
( self.q1[0]-self.q2[0],
self.q1[1]-self.q2[1],
self.q1[2]-self.q2[2] )))
derivs[self.a1] += self.scale() * deriv1
derivs[self.a2] -= self.scale() * deriv1
return energy
pass
# The term can be instantiated like:
p = BondPot('bond',AtomSel('resid 1 and name C')[0],
AtomSel('resid 1 and name O')[0], length=1.5)
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 4.0.2
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
| Classes | ||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||
| Functions | ||
| ||