示例#1
0
class LJTest(unittest.TestCase):
    def setUp(self):
        self.natoms = 10
        self.coords = np.random.uniform(-1,1.,3*self.natoms) * self.natoms**(-1./3)
        self.pot = LJ()
        self.E = self.pot.getEnergy(self.coords)
        self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)
        
        import itertools
        self.ilist = [] #np.zeros([self.natoms*(self.natoms-1)/2, 2])
        k = 0
        for i in range(self.natoms):
            for j in range(i):
                k += 1
                self.ilist.append( [i,j] )
        self.ilist = np.array(self.ilist) 
        #print self.ilist
    
    def test_grad_e(self):
        self.assertAlmostEqual(self.E, self.Egrad, 7)
    def test_lists_e(self):
        e = self.pot.getEnergyList(self.coords, self.ilist)
        self.assertAlmostEqual(self.E, e, 7)
    def test_lists_eg(self):
        e, g = self.pot.getEnergyGradientList(self.coords, self.ilist)
        self.assertAlmostEqual(self.E, e, 7)
        gdiffmax = np.max(np.abs( g-self.grad )) / np.max(np.abs(self.grad))
        self.assertLess(gdiffmax, 1e-7)
示例#2
0
class LJTest(unittest.TestCase):
    def setUp(self):
        self.natoms = 10
        self.coords = np.random.uniform(-1, 1., 3 * self.natoms) * self.natoms ** (-1. / 3)
        self.pot = LJ()
        self.E = self.pot.getEnergy(self.coords)
        self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)

        self.ilist = []  # np.zeros([self.natoms*(self.natoms-1)/2, 2])
        k = 0
        for i in range(self.natoms):
            for j in range(i):
                k += 1
                self.ilist.append([i, j])
        self.ilist = np.array(self.ilist)
        # print self.ilist

    def test_grad_e(self):
        self.assertAlmostEqual(self.E, self.Egrad, 7)

    def test_lists_e(self):
        e = self.pot.getEnergyList(self.coords, self.ilist)
        self.assertAlmostEqual(self.E, e, 7)

    def test_lists_eg(self):
        e, g = self.pot.getEnergyGradientList(self.coords, self.ilist)
        self.assertAlmostEqual(self.E, e, 7)
        gdiffmax = np.max(np.abs(g - self.grad)) / np.max(np.abs(self.grad))
        self.assertLess(gdiffmax, 1e-7)
示例#3
0
    def setUp(self):
        from pele.optimize import mylbfgs

        self.natoms = 10
        self.coords = np.random.uniform(-1, 1., 3 * self.natoms) * self.natoms ** (-1. / 3)
        self.pot = LJ()
        ret = mylbfgs(self.coords, self.pot, tol=2.)
        self.coords = ret.coords
        self.E = self.pot.getEnergy(self.coords)
        self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)
示例#4
0
    def setUp(self):
        self.natoms = 10
        self.coords = np.random.uniform(-1, 1., 3 * self.natoms) * self.natoms ** (-1. / 3)
        self.pot = LJ()
        self.E = self.pot.getEnergy(self.coords)
        self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)

        self.ilist = []  # np.zeros([self.natoms*(self.natoms-1)/2, 2])
        k = 0
        for i in range(self.natoms):
            for j in range(i):
                k += 1
                self.ilist.append([i, j])
        self.ilist = np.array(self.ilist)
示例#5
0
 def setUp(self):
     from pele.optimize import mylbfgs
     self.natoms = 10
     self.coords = np.random.uniform(-1,1.,3*self.natoms) * self.natoms**(-1./3)
     self.pot = LJ()
     ret = mylbfgs(self.coords, self.pot, tol=2.)
     self.coords = ret.coords
     self.E = self.pot.getEnergy(self.coords)
     self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)
示例#6
0
文件: _bfgs.py 项目: borislavujo/pele
def test():
    natoms = 100
    tol = 1e-6
    
    from pele.potentials.lj import LJ
    pot = LJ()
    
    X = getInitialCoords(natoms, pot)
    X += np.random.uniform(-1,1,[3*natoms]) * 0.3
    
    #do some steepest descent steps so we don't start with a crazy structure
    #from optimize.quench import _steepest_descent as steepestDescent
    #ret = steepestDescent(X, pot.getEnergyGradient, iprint = 1, dx = 1e-4, nsteps = 100, gtol = 1e-3, maxstep = .5)
    #X = ret[0]
    #print X

    
    Xinit = np.copy(X)
    e, g = pot.getEnergyGradient(X)
    print "energy", e
    
    lbfgs = BFGS(X, pot, maxstep = 0.1)
    
    ret = lbfgs.run(100, tol = tol, iprint=1)
    print "done", ret[1], ret[2], ret[3]
    
    print "now do the same with scipy lbfgs"
    from pele.optimize import lbfgs_scipy as quench
    ret = quench(Xinit, pot.getEnergyGradient, tol = tol)
    print ret[1], ret[2], ret[3]    
    
    print "now do the same with scipy bfgs"
    from pele.optimize import bfgs as oldbfgs
    ret = oldbfgs(Xinit, pot.getEnergyGradient, tol = tol)
    print ret[1], ret[2], ret[3]    
    
    print "now do the same with old gradient + linesearch"
    gpl = GradientPlusLinesearch(Xinit, pot, maxstep = 0.1)  
    ret = gpl.run(100, tol = 1e-6)
    print ret[1], ret[2], ret[3]    
示例#7
0
def test_LJ(natoms=12, **kwargs):  # pragma: no cover
    from pele.potentials.lj import LJ
    from pele.optimize import mylbfgs
    import pele.utils.rotations as rot
    from pele.mindist.permutational_alignment import permuteArray
    import random

    quench = mylbfgs
    lj = LJ()
    X1 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3)
    # quench X1
    ret = quench(X1, lj)
    X1 = ret.coords
    X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3)
    # make X2 a rotation of X1
    print("testing with", natoms,
          "atoms, with X2 a rotated and permuted isomer of X1")
    aa = rot.random_aa()
    rot_mx = rot.aa2mx(aa)
    for j in range(natoms):
        i = 3 * j
        X2[i:i + 3] = np.dot(rot_mx, X1[i:i + 3])
    perm = list(range(natoms))
    random.shuffle(perm)
    print(perm)
    X2 = permuteArray(X2, perm)

    import copy
    X1i = copy.copy(X1)
    X2i = copy.copy(X2)

    print("******************************")
    print("testing normal LJ  ISOMER")
    print("******************************")
    test(X1, X2, lj, **kwargs)

    print("******************************")
    print("testing normal LJ  non isomer")
    print("******************************")
    X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3)
    ret = quench(X2, lj)
    X2 = ret.coords

    Y = X1.reshape([-1, 3])
    Y += np.random.random(3)
    X1[:] = Y.flatten()

    test(X1, X2, lj, **kwargs)

    distinit = np.linalg.norm(X1 - X2)
    print("distinit", distinit)
示例#8
0
class TestLJAfterQuench(unittest.TestCase):
    """do the tests after a short quench so that the energies are not crazy large
    """

    def setUp(self):
        from pele.optimize import mylbfgs

        self.natoms = 10
        self.coords = np.random.uniform(-1, 1., 3 * self.natoms) * self.natoms ** (-1. / 3)
        self.pot = LJ()
        ret = mylbfgs(self.coords, self.pot, tol=2.)
        self.coords = ret.coords
        self.E = self.pot.getEnergy(self.coords)
        self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)

    def test_gradient(self):
        e0 = self.pot.getEnergy(self.coords)
        e, g, hess = self.pot.getEnergyGradientHessian(self.coords)
        gnum = self.pot.NumericalDerivative(self.coords)
        maxg = np.max(np.abs(g))
        maxgdiff = np.max(np.abs(g - gnum))
        self.assertAlmostEqual(e0, e, 5)
        self.assertLess(maxgdiff / maxg, 1e-5)

    def test_hessian(self):
        e, g, hess = self.pot.getEnergyGradientHessian(self.coords)
        nhess = self.pot.NumericalHessian(self.coords, eps=1e-8)
        # print "hess", hess
        #        print "numerical", nhess
        #        diff = hess - nhess
        maxhess = np.max(np.abs(hess))
        maxdiff = np.max(np.abs(hess - nhess))
        #        print "diff", hess[:2,:2] - nhess[:2,:2]
        #        print maxhess, maxdiff, maxdiff / maxhess
        #        print "diff", (hess[:2,:2] - nhess[:2,:2])/maxhess
        #        print "diff", (hess - nhess)/maxhess
        #        print np.max(np.abs((hess-nhess)/nhess))
        self.assertLess(maxdiff / maxhess, 1e-5)
示例#9
0
"""
an example for finding the minimum distance and best alignment
between two lennard jones clusters
"""
from __future__ import print_function
from builtins import range
import numpy as np

from pele.potentials.lj import LJ
from pele.optimize import lbfgs_py
from pele.mindist import MinPermDistAtomicCluster

pot = LJ()

natoms = 40

# get two random quenched structures to compare
coords1 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5
coords2 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5
ret1 = lbfgs_py(coords1, pot)
ret2 = lbfgs_py(coords2, pot)
coords1 = ret1.coords
coords2 = ret2.coords

# all the atoms are permutable
permlist = [list(range(natoms))]

mindist = MinPermDistAtomicCluster(niter=100, permlist=permlist, verbose=False)
dist, newcoords1, newcoords2 = mindist(coords1, coords2)

print("")
示例#10
0
import numpy as np
#from potentials.soft_sphere import SoftSphere, putInBox
from pele.potentials.lj import LJ as SoftSphere

np.random.seed(0)

natoms = 120
rho = 1.6
boxl = 1.
meandiam = boxl / (float(natoms)/rho)**(1./3)
print "mean diameter", meandiam 

#set up potential
#diams = np.array([meandiam for i in range(natoms)]) #make them all the same
#pot = SoftSphere(diams = diams)
pot = SoftSphere()


#initial coordinates
coords = np.random.uniform(-1,1,[natoms*3]) * (natoms)**(1./3)
E = pot.getEnergy(coords)
print "initial energy", E 

printlist = [] #list of coordinates saved for printing
printlist.append((coords.copy(), "intial coords"))



#test a quench with default lbfgs
#from optimize.quench import quench
from pele.optimize import lbfgs_ase as quench
示例#11
0
def testpot1():
    from pele.potentials.lj import LJ
    import itertools
    pot = LJ()
    a = 1.12 #2.**(1./6.)
    theta = 60./360*np.pi
    coords = [ 0., 0., 0., \
              -a, 0., 0., \
              -a/2, a*np.cos(theta), 0., \
              -a/2, -a*np.cos(theta), 0.1 \
              ]
    natoms = len(coords)/3
    c = np.reshape(coords, [-1,3])
    for i, j in itertools.combinations(range(natoms), 2):
        r = np.linalg.norm(c[i,:] - c[j,:])
        print i, j, r 
    
    e, g = pot.getEnergyGradient(coords)
    print "initial E", e
    print "initial G", g, np.linalg.norm(g)

    eigpot = LowestEigPot(coords, pot)
    vec = np.random.rand(len(coords))
    e, g = eigpot.getEnergyGradient(vec)
    print "eigenvalue", e 
    print "eigenvector", g
    
    if True:
        e, g, hess = pot.getEnergyGradientHessian(coords)
        print "shape hess", np.shape(hess)
        print "hessian", hess
        u, v = np.linalg.eig(hess)
        print "max imag value", np.max(np.abs(u.imag))
        print "max imag vector", np.max(np.abs(v.imag))
        u = u.real
        v = v.real
        print "eigenvalues", u
        for i in range(len(u)):
            print "eigenvalue", u[i], "eigenvector", v[:,i]
        #find minimum eigenvalue, vector
        imin = 0
        umin = 10.
        for i in range(len(u)):
            if np.abs(u[i]) < 1e-10: continue
            if u[i] < umin:
                umin = u[i]
                imin = i
        print "lowest eigenvalue ", umin, imin
        print "lowest eigenvector", v[:,imin]

    
    from pele.optimize import lbfgs_py as quench
    ret = quench(vec, eigpot.getEnergyGradient, iprint=10, tol = 1e-5, maxstep = 1e-3, \
                 rel_energy = True)
    print ret
    
    print "lowest eigenvalue "
    print umin, imin
    print "lowest eigenvector"
    print v[:,imin]
    print "now the estimate"
    print ret[1]
    print ret[0]