def test_sitedist(self): drij = np.random.uniform(-1,1,3) p1 = vec_random() * 0.5 p2 = vec_random() * 0.5 W = 1.3 S = np.random.uniform(-1,1,[3,3]) cog = np.random.uniform(-1,1,3) dist = sitedist(drij, p1, p2, S, W, cog) dist2 = _sitedist(drij, p1, p2, S, W, cog) self.assertAlmostEqual(dist, dist2, places=4)
def test1(self): drij = np.random.uniform(-1,1,3) p1 = vec_random() * 0.5 p2 = vec_random() * 0.5 W = 1.3 S = np.random.uniform(-1,1,[3,3]) cog = np.random.uniform(-1,1,3) g_M, g_P = sitedist_grad(drij, p1, p2, S, W, cog) g_Mp, g_Pp = _sitedist_grad(drij, p1, p2, S, W, cog) self.assert_array_almost_equal(g_M, g_Mp, places=4) self.assert_array_almost_equal(g_P, g_Pp, places=4)
def get_random_configuration(self): nspins = self.nspins if self.one_frozen: nspins -= 1 coords = np.zeros([nspins, 2]) for i in range(nspins): vec = vec_random() coords[i,:] = hs.make2dVector(vec) return coords.reshape(-1)
def test_potential_constrained(): L=4 nspins=L*L pot = HeisenbergModelConstraint( dim = [L,L], field_disorder = 1.) #, phases=phases) coords = np.zeros([nspins, 3]) for i in range(nspins): coords[i,:] = rotations.vec_random() coords = coords.reshape(-1) pot.test_potential(coords)
def test_big_small(self): P = vec_random() P1 = P * 1.1e-6 P2 = P * 0.9e-6 # print P1.dot(P1) > 1e-12, P2.dot(P2) > 1e-12 rm, drm1, drm2, drm3 = rmdrvt(P1, True) rmp, drm1p, drm2p, drm3p = rmdrvt(P2, True) # print rm # print rmp self.assert_array_almost_equal(rm, rmp, places=4) self.assert_array_almost_equal(drm1, drm1p, places=4) self.assert_array_almost_equal(drm2, drm2p, places=4) self.assert_array_almost_equal(drm3, drm3p, places=4)
def test_potential(): L=4 nspins=L*L pot = HeisenbergModel( dim = [L,L], field_disorder = 1.) #, phases=phases) coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i,:] = make2dVector(vec) coords = np.reshape(coords, [nspins*2]) coords[1] = 2.*np.pi - coords[1] pot.test_potential(coords)
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=coords.size / 6, coords=coords) backbone = np.zeros(3) # random rotation for angle-axis vectors for pos, rot in zip(ca.posRigid, ca.rotRigid): backbone += rotations.vec_random() * 0.7525 # choose a random rotation rot[:] = rotations.random_aa() # calcualte center of base from backgone a1 = np.dot(rotations.aa2mx(rot), np.array([1., 0., 0.])) pos[:] = backbone + 0.4 * a1
def __init__(self, dim=[4, 4], field_disorder=1., fields=None): self.dim = copy(dim) self.nspins = np.prod(dim) self.G = nx.grid_graph(dim, periodic=True) self.fields = np.zeros([self.nspins, 3]) self.indices = dict() nodes = sorted(self.G.nodes()) for i, node in enumerate(nodes): self.indices[node] = i if fields is None: self.fields[i,:] = rotations.vec_random() * np.sqrt(field_disorder) else: self.fields[i,:] = fields[i,:]
def takeStep(self, coords, **kwargs): # easy access to coordinates ca = CoordsAdapter(nrigid=old_div(coords.size, 6), coords=coords) backbone = np.zeros(3) # random rotation for angle-axis vectors for pos, rot in zip(ca.posRigid, ca.rotRigid): backbone += rotations.vec_random() * 0.7525 # choose a random rotation rot[:] = rotations.random_aa() # calcualte center of base from backgone a1 = np.dot(rotations.aa2mx(rot), np.array([1., 0., 0.])) pos[:] = backbone + 0.4 * a1
def __init__(self, dim=None, field_disorder=1., fields=None): if dim is None: dim = [4, 4] self.dim = copy(dim) self.nspins = np.prod(dim) self.G = nx.grid_graph(dim, periodic=True) self.fields = np.zeros([self.nspins, 3]) self.indices = dict() nodes = sorted(self.G.nodes()) for i, node in enumerate(nodes): self.indices[node] = i if fields is None: self.fields[i, :] = rotations.vec_random() * field_disorder else: self.fields[i, :] = fields[i, :]
def __init__(self, dim = [4, 4], field_disorder = 1.): """ dim is an array giving the dimensions of the lattice phi is the magnitude of the randomness in the fields """ self.dim = copy(dim) self.nspins = np.prod(dim) self.G = nx.grid_graph(dim, periodic=True) self.fields = np.zeros([self.nspins, 3]) self.indices = dict() i = 0 for node in self.G.nodes(): self.indices[node] = i self.fields[i,:] = rotations.vec_random() * \ field_disorder#np.random.uniform(0, field_disorder, [3]) i += 1
def test_small_theta(self): P = vec_random() * np.random.uniform(0,1e-7) print P self.check(P, True)
def test1(self): P = vec_random() * np.random.uniform(1e-5,1) self.check(P, True)
def test(): pi = np.pi L = 8 nspins = L**2 #phases = np.zeros(nspins) pot = HeisenbergModel( dim = [L,L], field_disorder = 1.) #, phases=phases) coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i,:] = make2dVector(vec) coords = np.reshape(coords, [nspins*2]) if False: normfields = np.copy(pot.fields) for i in range(nspins): normfields[i,:] /= np.linalg.norm(normfields[i,:]) coords = coords3ToCoords2( np.reshape(normfields, [nspins*3] ) ) coords = np.reshape(coords, nspins*2) #print np.shape(coords) coordsinit = np.copy(coords) #print "fields", pot.fields print coords if False: coords3 = coords2ToCoords3(coords) coords2 = coords3ToCoords2(coords3) print np.reshape(coords, [nspins,2]) print coords2 coords3new = coords2ToCoords3(coords2) print coords3 print coords3new e = pot.getEnergy(coords) print "energy ", e if False: print "numerical gradient" ret = pot.getEnergyGradientNumerical(coords) print ret[1] if True: print "analytical gradient" ret2 = pot.getEnergyGradient(coords) print ret2[1] print ret[0] print ret2[0] print "ratio" print ret2[1] / ret[1] print "inverse sin" print 1./sin(coords) print cos(coords) print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot, iprint=1) print "quenched e = ", ret.energy, "funcalls", ret.nfev print ret.coords with open("out.spins", "w") as fout: s = coords2ToCoords3( ret.coords ) h = pot.fields c = coords2ToCoords3( coordsinit ) for node in pot.G.nodes(): i = pot.indices[node] fout.write( "%g %g %g %g %g %g %g %g %g %g %g\n" % (node[0], node[1], \ s[i,0], s[i,1], s[i,2], h[i,0], h[i,1], h[i,2], c[i,0], c[i,1], c[i,2] ) ) coords3 = coords2ToCoords3( ret.coords ) m = np.linalg.norm( coords3.sum(0) ) / nspins print "magnetization after quench", m test_basin_hopping(pot, coords)
h = pot.fields for node in pot.G.nodes(): i = pot.indices[node] fout.write("%g %g %g %g %g %g %g %g\n" % (node[0], node[1], s[i, 0], s[i, 1], s[i, 2], h[i, 0], h[i, 1], h[i, 2] )) pi = np.pi L = 4 nspins = L ** 2 pot = HeisenbergModelRA(dim=[L, L], field_disorder=2.) coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i, :] = make2dVector(vec) coords = np.reshape(coords, [nspins * 2]) coordsinit = np.copy(coords) print coords e = pot.getEnergy(coords) print "energy ", e print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot) print "quenched e = ", ret.energy, "funcalls", ret.nfev
for node in pot.G.nodes(): i = pot.indices[node] fout.write("%g %g %g %g %g %g %g %g\n" % (node[0], node[1], s[i, 0], s[i, 1], s[i, 2], h[i, 0], h[i, 1], h[i, 2])) pi = np.pi L = 4 nspins = L**2 pot = HeisenbergModelRA(dim=[L, L], field_disorder=2.) coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i, :] = make2dVector(vec) coords = np.reshape(coords, [nspins * 2]) coordsinit = np.copy(coords) print coords e = pot.getEnergy(coords) print "energy ", e print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot) print "quenched e = ", ret.energy, "funcalls", ret.nfev