def distance_squared(self, com1, p1, com2, p2): ''' distance measure between 2 angle axis bodies of same type 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 angle axis site type with mass and moment of inertia tensor returns: distance squared ''' return _aadist.sitedist(self.get_smallest_rij(com1, com2), p1, p2, self.S, self.W, self.cog) R1 = rotations.aa2mx(p1) R2 = rotations.aa2mx(p2) dR = R2 - R1 dR = dR d_M = self.W*np.sum((com2-com1)**2) # dR_kl S_lm dR_km d_P = np.trace(np.dot(dR, np.dot(self.S, dR.transpose()))) d_mix = 2.*self.W * np.dot((com2-com1), np.dot(dR, self.cog)) return d_M + d_P + d_mix
def distance_squared(self, com1, p1, com2, p2): """ distance measure between 2 angle axis bodies of same type 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 angle axis site type with mass and moment of inertia tensor returns: distance squared """ return sitedist(self.get_smallest_rij(com1, com2), p1, 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)
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) coords1 = np.random.random(120) coords2 = np.random.random(120) 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): _aadist.sitedist(X2 - X1, p1, p2, site.S, site.W, cog) #_aadist.aadist(coords1, coords2, site.S, site.W, cog) t2 = time.time()
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)