def distance_squared_grad(self, com1, p1, com2, p2): """ calculate spring force between 2 sites Parameters ---------- com1: center of mass of 1st site p1: angle axis vector of 1st site com2: center of mass of 2nd site p2: angle axis vector of 2nd site sitetype: AASiteType, optional amgle axis site type with mass and moment of inertia tensor returns: tuple spring cart, spring rot """ return sitedist_grad(self.get_smallest_rij(com1, com2), p1, p2, self.S, self.W, self.cog)
def distance_squared_grad(self, com1, p1, com2, p2): ''' calculate spring force between 2 sites Parameters ---------- com1: center of mass of 1st site p1: angle axis vector of 1st site com2: center of mass of 2nd site p2: angle axis vector of 2nd site sitetype: AASiteType, optional amgle axis site type with mass and moment of inertia tensor returns: tuple spring cart, spring rot ''' return _aadist.sitedist_grad(self.get_smallest_rij(com1, com2), p1, p2, self.S, self.W, self.cog) R1, R11, R12, R13 = rotMatDeriv(p1, True) R2 = rotations.aa2mx(p2) dR = R2 - R1 dR = dR g_M = -2.*self.W*(com2-com1) # dR_kl S_lm dR_km g_P = np.zeros(3) g_P[0] = -2.*np.trace(np.dot(R11, np.dot(self.S, dR.transpose()))) g_P[1] = -2.*np.trace(np.dot(R12, np.dot(self.S, dR.transpose()))) g_P[2] = -2.*np.trace(np.dot(R13, np.dot(self.S, dR.transpose()))) g_M -= 2.*self.W * np.dot(dR, self.cog) g_P[0] -= 2.*self.W * np.dot((com2-com1), np.dot(R11, self.cog)) g_P[1] -= 2.*self.W * np.dot((com2-com1), np.dot(R12, self.cog)) g_P[2] -= 2.*self.W * np.dot((com2-com1), np.dot(R13, self.cog)) return g_M, g_P
def test(): # pragma: no cover natoms = 3 x = np.random.random([natoms, 3]) * 5 masses = [1., 1., 16.] # np.random.random(natoms) print masses x -= np.average(x, axis=0, weights=masses) cog = np.average(x, axis=0) S = np.zeros([3, 3]) for i in xrange(3): for j in xrange(3): S[i][j] = np.sum(x[:, i] * x[:, j]) site = AASiteType(M=natoms, S=S, W=natoms, cog=cog) X1 = 10.1 * np.random.random(3) X2 = 10.1 * np.random.random(3) p1 = rotations.random_aa() p2 = rotations.random_aa() R1 = rotations.aa2mx(p1) R2 = rotations.aa2mx(p2) x1 = np.dot(R1, x.transpose()).transpose() + X1 x2 = np.dot(R2, x.transpose()).transpose() + X2 import _aadist print "site representation:", np.sum((x1 - x2) ** 2) print "distance function: ", site.distance_squared(X1, p1, X2, p2) print "fortran function: ", _aadist.sitedist(X2 - X1, p1, p2, site.S, site.W, cog) import time t0 = time.time() for i in xrange(1000): site.distance_squared(X1, p1, X2, p2) t1 = time.time() print "time python", t1 - t0 for i in xrange(1000): sitedist(X2 - X1, p1, p2, site.S, site.W, cog) # _aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran", t2 - t1 # for i in xrange(1000/20): # #_aadist.sitedist(X1, p1, X2, p2, site.S, site.W, cog) # _aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran acc", t2 - t1 print site.distance_squared_grad(X1, p1, X2, p2) g_M = np.zeros(3) g_P = np.zeros(3) for i in xrange(3): eps = 1e-6 delta = np.zeros(3) delta[i] = eps g_M[i] = (site.distance_squared(X1 + delta, p1, X2, p2) - site.distance_squared(X1, p1, X2, p2)) / eps g_P[i] = (site.distance_squared(X1, p1 + delta, X2, p2) - site.distance_squared(X1, p1, X2, p2)) / eps print g_M, g_P xx = site.distance_squared_grad(X1, p1, X2, p2) print g_M / xx[0], g_P / xx[1] print _aadist.sitedist_grad(X2 - X1, p1, p2, site.S, site.W, cog)
for i in xrange(1000): _aadist.sitedist(X2 - X1, p1, p2, site.S, site.W, cog) #_aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran", t2-t1 # for i in xrange(1000/20): # #_aadist.sitedist(X1, p1, X2, p2, site.S, site.W, cog) # _aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran acc", t2-t1 print site.distance_squared_grad(X1, p1, X2, p2) g_M = np.zeros(3) g_P = np.zeros(3) for i in xrange(3): eps = 1e-6 delta = np.zeros(3) delta[i] = eps g_M[i] = (site.distance_squared(X1+delta, p1, X2, p2) - site.distance_squared(X1, p1, X2, p2))/eps g_P[i] = (site.distance_squared(X1, p1+delta, X2, p2) - site.distance_squared(X1, p1, X2, p2))/eps print g_M, g_P xx = site.distance_squared_grad(X1, p1, X2, p2) print g_M/xx[0], g_P/xx[1] print _aadist.sitedist_grad(X2 - X1, p1, p2, site.S, site.W, cog) # print _aadist.sitedist_grad(com1, p1, com2, p2, self.S, self.W, self.cog)
def test(): # pragma: no cover natoms = 3 x = np.random.random([natoms, 3]) * 5 masses = [1., 1., 16.] # np.random.random(natoms) print masses x -= np.average(x, axis=0, weights=masses) cog = np.average(x, axis=0) S = np.zeros([3, 3]) for i in xrange(3): for j in xrange(3): S[i][j] = np.sum(x[:, i] * x[:, j]) site = AASiteType(M=natoms, S=S, W=natoms, cog=cog) X1 = 10.1 * np.random.random(3) X2 = 10.1 * np.random.random(3) p1 = rotations.random_aa() p2 = rotations.random_aa() R1 = rotations.aa2mx(p1) R2 = rotations.aa2mx(p2) x1 = np.dot(R1, x.transpose()).transpose() + X1 x2 = np.dot(R2, x.transpose()).transpose() + X2 import _aadist print "site representation:", np.sum((x1 - x2)**2) print "distance function: ", site.distance_squared(X1, p1, X2, p2) print "fortran function: ", _aadist.sitedist(X2 - X1, p1, p2, site.S, site.W, cog) import time t0 = time.time() for i in xrange(1000): site.distance_squared(X1, p1, X2, p2) t1 = time.time() print "time python", t1 - t0 for i in xrange(1000): sitedist(X2 - X1, p1, p2, site.S, site.W, cog) # _aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran", t2 - t1 # for i in xrange(1000/20): # #_aadist.sitedist(X1, p1, X2, p2, site.S, site.W, cog) # _aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time() print "time fortran acc", t2 - t1 print site.distance_squared_grad(X1, p1, X2, p2) g_M = np.zeros(3) g_P = np.zeros(3) for i in xrange(3): eps = 1e-6 delta = np.zeros(3) delta[i] = eps g_M[i] = (site.distance_squared(X1 + delta, p1, X2, p2) - site.distance_squared(X1, p1, X2, p2)) / eps g_P[i] = (site.distance_squared(X1, p1 + delta, X2, p2) - site.distance_squared(X1, p1, X2, p2)) / eps print g_M, g_P xx = site.distance_squared_grad(X1, p1, X2, p2) print g_M / xx[0], g_P / xx[1] print _aadist.sitedist_grad(X2 - X1, p1, p2, site.S, site.W, cog)