def Align(self, coords1, coords2): from pygmin.mindist import rmsfit,aamindist potential = GMINPotential(GMIN) ca1 = CoordsAdapter(nrigid=coords1.size/6, coords = coords1) ca2 = CoordsAdapter(nrigid=coords2.size/6, coords = coords2) rot = rmsfit.findrotation_kabsch(ca2.posRigid, ca1.posRigid) ca2.posRigid[:] = np.dot(rot,ca2.posRigid.transpose()).transpose() for p in ca2.rotRigid: p[:] = rotations.mx2aa((np.dot(rot, rotations.aa2mx(p)))) print "before" print potential.getEnergy(coords1), potential.getEnergy(coords2) print ca2.rotRigid - ca1.rotRigid for p1,p2 in zip(ca1.rotRigid, ca2.rotRigid): p1[:],p2[:] = aamindist.aadistance(p1, p2) print "after" print potential.getEnergy(coords1), potential.getEnergy(coords2) print ca2.rotRigid - ca1.rotRigid path = InterpolatedPath(coords1, coords2, 40) print potential.getEnergy(path[0]) print "Interpolated energies" for x in InterpolatedPath(coords1, coords2, 40): print potential.getEnergy(x) print "done" return coords1, coords2
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=coords.size/6, coords = coords) # random rotation for angle-axis vectors for j in xrange(ca.nrigid): # choose bond to rotate around, index is first bead that changes index = np.random.randint(1, ca.nrigid) # determine backbone beads a1 = np.dot(rotations.aa2mx(ca.rotRigid[index-1]), np.array([1., 0., 0.])) a2 = np.dot(rotations.aa2mx(ca.rotRigid[index]), np.array([1., 0., 0.])) x1 = ca.posRigid[index-1] - 0.4*a1 # backbone bead x2 = ca.posRigid[index] - 0.4*a2 # backbone bead # get bond vector as axis of rotation + random magnitude p = x2 - x1 p /= np.linalg.norm(p) p *= np.random.random()*self.stepsize # convert random rotation to a matrix mx = rotations.aa2mx(p) # center of rotation is in middle of backbone bond center = 0.5*(x1 + x2) # apply rotation to positions and orientations for i in xrange(index, ca.nrigid): a = np.dot(rotations.aa2mx(ca.rotRigid[i]), np.array([1., 0., 0.])) ca.rotRigid[i] = rotations.rotate_aa(p, ca.rotRigid[i]) x = ca.posRigid[i] - 0.4*a ca.posRigid[i] = np.dot(mx, x - center) + center a = np.dot(rotations.aa2mx(ca.rotRigid[i]), np.array([1., 0., 0.])) ca.posRigid[i]+=0.4*a
def getEnergyGradient(self, coords): #return self.getEnergy(coords), self.NumericalDerivative(coords) E, grad = GMINPotential.getEnergyGradient(self, coords) ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) RMX = [rmdrvt(p, True) for p in ca.rotRigid] xback = [ x - 0.4 * np.dot(r[0], np.array([1., 0., 0.])) for x, r in zip(ca.posRigid, RMX) ] xbase = [ x - 0.4 * np.dot(r[0], np.array([1., 0., 0.])) for x, r in zip(ca.posRigid, RMX) ] gback = np.zeros_like(xback) Eangle = 0. v2 = xback[1] - xback[0] absv2 = np.linalg.norm(v2) v2 /= absv2 for i in xrange(1, ca.nrigid - 1): v1 = -v2 absv1 = absv2 v2 = xback[i + 1] - xback[i] absv2 = np.linalg.norm(v2) v2 /= absv2 v1v2 = np.dot(v1, v2) theta = np.arccos(v1v2) Eangle += 0.5 * self.k * (theta - self.theta0)**2 acos_prime = 1. / np.sqrt(1. - v1v2**2) s = self.k * (theta - self.theta0) * acos_prime gback[i - 1] += s * (-v2 / absv1 + v1v2 * v1 / absv1) gback[i] += s * (v1 / absv2 + v2 / absv1 - v1v2 * v1 / absv1 - v1v2 * v2 / absv2) gback[i + 1] += s * (-v1 / absv2 + v1v2 * v2 / absv2) Etorsion = 0 if self.use_torsion: for i in xrange(ca.nrigid - 3): r = xback[i:i + 4] theta = dihedral_angle(r) e_theta, g_theta = U_torsion_back_grad(theta) Etorsion += e_theta gback[i:i + 4] += g_theta * dihedral_gradient(r) cg = CoordsAdapter(nrigid=ca.nrigid, coords=grad) cg.posRigid += gback for i in xrange(ca.nrigid): x = -0.4 * np.array([1., 0., 0.]) R = RMX[i] cg.rotRigid[i][0] += np.dot(gback[i], np.dot(R[1], x)) cg.rotRigid[i][1] += np.dot(gback[i], np.dot(R[2], x)) cg.rotRigid[i][2] += np.dot(gback[i], np.dot(R[3], x)) return E + Eangle + Etorsion, grad
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=coords.size/6, coords = coords) # random displacement for positions ca.posRigid[:] = 2.*self.radius*(np.random.random(ca.posRigid.shape)-0.5) # random rotation for angle-axis vectors for rot in ca.rotRigid: rot[:] = rotations.random_aa()
def getEnergy(self, coords): E = GMINPotential.getEnergy(self, coords) ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) RMX = [rmdrvt(p, True) for p in ca.rotRigid] xback = np.array([ x - 0.4 * np.dot(r[0], np.array([1., 0., 0.])) for x, r in zip(ca.posRigid, RMX) ]) xbase = np.array([ x + 0.4 * np.dot(r[0], np.array([1., 0., 0.])) for x, r in zip(ca.posRigid, RMX) ]) Eangle = 0 v2 = xback[1] - xback[0] v2 /= np.linalg.norm(v2) for i in xrange(1, ca.nrigid - 1): v1 = -v2.copy() v2 = xback[i + 1] - xback[i] v2 /= np.linalg.norm(v2) theta = np.arccos(np.dot(v1, v2)) Eangle += 0.5 * self.k * (theta - self.theta0)**2 # add the torsion angle Etorsion = 0 if self.use_torsion: for i in xrange(ca.nrigid - 3): theta = dihedral_angle(xback[i:i + 4]) Etorsion += U_torsion_back(theta) return E + Eangle + Etorsion
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) # random displacement for positions ca.posRigid[:] += 2. * self.displace * ( np.random.random(ca.posRigid.shape) - 0.5) # random rotation for angle-axis vectors takestep.rotate(self.rotate, ca.rotRigid)
def export_xyz(fl, coords): ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) fl.write("%d\n\n" % (2 * ca.nrigid)) for i in xrange(ca.nrigid): a = np.dot(rotations.aa2mx(ca.rotRigid[i]), np.array([1., 0., 0.])) x_back = ca.posRigid[i] - 0.4 * a # backbone bead x_stack = ca.posRigid[i] + 0.4 * a fl.write("C %f %f %f\n" % (x_back[0], x_back[1], x_back[2])) fl.write("H %f %f %f\n" % (x_stack[0], x_stack[1], x_stack[2]))
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) # random displacement for positions #ca.posRigid[:] = 2.*self.radius*(np.random.random(ca.posRigid.shape)-0.5) # random rotation for angle-axis vectors for rot in ca.rotRigid: rot[:] = rotations.random_aa()
def map_to_aa(xyz): coords = np.zeros(6*13) ca = CoordsAdapter(nrigid=13, coords=coords) for i in xrange(13): ca.posRigid[i] = 0.5*(xyz[2*i] + xyz[2*i+1]) a1 = -(xyz[2*i] - xyz[2*i+1]) if(i<12): a3 = -(xyz[2*i+1] - xyz[2*i+3]) else: a3 = -(xyz[2*i-1] - xyz[2*i+1]) a2 = -np.cross(a1, a3) a3 = np.cross(a1, a2) a1/=np.linalg.norm(a1) a2/=np.linalg.norm(a2) a3/=np.linalg.norm(a3) # print np.dot(a1, a2), np.dot(a1, a3), np.dot(a2, a3) mx = np.array([a1, a2, a3]).transpose() p = rotations.mx2aa(mx) #print mx - rotations.aa2mx(p) # print a1-np.dot(rotations.aa2mx(p), np.array([1., 0., 0.])) ca.rotRigid[i] = p return coords
def map_to_aa(xyz): coords = np.zeros(6 * 13) ca = CoordsAdapter(nrigid=13, coords=coords) for i in xrange(13): ca.posRigid[i] = 0.5 * (xyz[2 * i] + xyz[2 * i + 1]) a1 = -(xyz[2 * i] - xyz[2 * i + 1]) if (i < 12): a3 = -(xyz[2 * i + 1] - xyz[2 * i + 3]) else: a3 = -(xyz[2 * i - 1] - xyz[2 * i + 1]) a2 = -np.cross(a1, a3) a3 = np.cross(a1, a2) a1 /= np.linalg.norm(a1) a2 /= np.linalg.norm(a2) a3 /= np.linalg.norm(a3) # print np.dot(a1, a2), np.dot(a1, a3), np.dot(a2, a3) mx = np.array([a1, a2, a3]).transpose() p = rotations.mx2aa(mx) #print mx - rotations.aa2mx(p) # print a1-np.dot(rotations.aa2mx(p), np.array([1., 0., 0.])) ca.rotRigid[i] = p return coords
def __init__(self, nrigid=0, natoms = 0): CoordsAdapter.__init__(nrigd=nrigid, natoms=natoms)
db=Database(db="oxdna.sqlite") path[0]=db.minima()[19].coords path[-1]=db.minima()[0].coords e1 = [] e2 = [] e1.append(pot.getEnergy(path[0])) e2.append(pot.getEnergy(path[0])) for i in xrange(1): for i in xrange(len(path)-1): e1.append(pot.getEnergy(path[i+1])) c1 = CoordsAdapter(nrigid=13, coords = path[i]) c2 = CoordsAdapter(nrigid=13, coords = path[i+1]) com1 = np.sum(c1.posRigid,axis=0) / float(13) com2 = np.sum(c1.posRigid,axis=0) / float(13) c1.posRigid-=com1 c2.posRigid-=com2 mx = findrotation_kabsch(c2.posRigid, c1.posRigid) #print mx c2.posRigid[:] = np.dot(mx, c2.posRigid.transpose()).transpose() for p in c2.rotRigid: p[:] = rotations.rotate_aa(p, rotations.mx2aa(mx)) e2.append(pot.getEnergy(path[i+1])) for p1,p2 in zip(c1.rotRigid, c2.rotRigid): n2 = p2/np.linalg.norm(p2)*2.*pi while True:
pickle.dump(path, open("interpolate.pickle", "w")) exit() db = Database(db="oxdna.sqlite") path[0] = db.minima()[19].coords path[-1] = db.minima()[0].coords e1 = [] e2 = [] e1.append(pot.getEnergy(path[0])) e2.append(pot.getEnergy(path[0])) for i in xrange(1): for i in xrange(len(path) - 1): e1.append(pot.getEnergy(path[i + 1])) c1 = CoordsAdapter(nrigid=13, coords=path[i]) c2 = CoordsAdapter(nrigid=13, coords=path[i + 1]) com1 = np.sum(c1.posRigid, axis=0) / float(13) com2 = np.sum(c1.posRigid, axis=0) / float(13) c1.posRigid -= com1 c2.posRigid -= com2 mx = findrotation_kabsch(c2.posRigid, c1.posRigid) #print mx c2.posRigid[:] = np.dot(mx, c2.posRigid.transpose()).transpose() for p in c2.rotRigid: p[:] = rotations.rotate_aa(p, rotations.mx2aa(mx)) e2.append(pot.getEnergy(path[i + 1])) for p1, p2 in zip(c1.rotRigid, c2.rotRigid): n2 = p2 / np.linalg.norm(p2) * 2. * pi