Пример #1
0
def main():
    # test class
    natoms = 3
    coords = np.random.uniform(-1, 1, natoms * 3) * 2

    lj = ATLJ(Z=1.0)

    E = lj.getEnergy(coords)
    print "E", E
    E, V = lj.getEnergyGradient(coords)
    print "E", E
    print "V"
    print V

    print "try a quench"
    from pygmin.optimize import mylbfgs as quench

    ret = quench(coords, lj.getEnergyGradient, iprint=-1)
    # quench( coords, lj.getEnergyGradientNumerical, iprint=1 )
    print "energy ", ret[1]
    print "rms gradient", ret[2]
    print "number of function calls", ret[3]

    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz

    coords = ret[0]

    printlist = []
    for i in range(100):
        coords = np.random.uniform(-1, 1, natoms * 3) * 2
        # coords = np.array([0,0,1., 0,0,0, 0,0,2])
        # coords[6:] += np.random.uniform(-1,1,3)*0.1
        ret = quench(coords, lj.getEnergyGradient, iprint=-1)
        coords = ret[0]
        X = np.reshape(coords, [natoms, 3])
        com = X.sum(0) / natoms
        X[:, :] -= com[np.newaxis, :]
        printlist.append(np.reshape(X, natoms * 3))

    with open("out.xyz", "w") as fout:
        for coords in printlist:
            printxyz(fout, coords)
Пример #2
0
def main():
    #test class
    natoms = 3
    coords = np.random.uniform(-1, 1, natoms * 3) * 2

    lj = ATLJ(Z=1.)

    E = lj.getEnergy(coords)
    print "E", E
    E, V = lj.getEnergyGradient(coords)
    print "E", E
    print "V"
    print V

    print "try a quench"
    from pygmin.optimize import mylbfgs as quench
    ret = quench(coords, lj, iprint=-1)
    #quench( coords, lj.getEnergyGradientNumerical, iprint=1 )
    print "energy ", ret.energy
    print "rms gradient", ret.rms
    print "number of function calls", ret.nfev

    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    coords = ret.coords

    printlist = []
    for i in range(100):
        coords = np.random.uniform(-1, 1, natoms * 3) * 2
        #coords = np.array([0,0,1., 0,0,0, 0,0,2])
        #coords[6:] += np.random.uniform(-1,1,3)*0.1
        ret = quench(coords, lj.getEnergyGradient, iprint=-1)
        coords = ret.coords
        X = np.reshape(coords, [natoms, 3])
        com = X.sum(0) / natoms
        X[:, :] -= com[np.newaxis, :]
        printlist.append(np.reshape(X, natoms * 3))

    with open("out.xyz", "w") as fout:
        for coords in printlist:
            printxyz(fout, coords)
Пример #3
0
def test_soft_sphere(natoms = 9):
    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)

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

    printlist = []
    printlist.append((coords.copy(), "intial coords"))
    
    #test a quench with default lbfgs
    from pygmin.optimize import lbfgs_scipy as quench
    coords, E, rms, funcalls = quench(coords, pot.getEnergyGradient, iprint=-1)
    printlist.append((coords.copy(), "intial coords"))
    print "energy post quench", pot.getEnergy(coords)

    fname = "out.xyz"
    print "saving coordinates to", fname
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    with open(fname, "w") as fout:
        for xyz,line2 in printlist:
            xyz = putInBox(xyz, boxl)
            printxyz(fout, xyz, line2=line2) 
            
    
    from scipy.optimize import check_grad
    res = check_grad(pot.getEnergy, pot.getGradient, coords)
    print "testing gradient (should be small)", res
Пример #4
0
def test_soft_sphere(natoms=9):
    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)

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

    printlist = []
    printlist.append((coords.copy(), "intial coords"))

    #test a quench with default lbfgs
    from pygmin.optimize import mylbfgs as quench
    res = quench(coords, pot, iprint=-1)
    printlist.append((res.coords.copy(), "intial coords"))
    print "energy post quench", pot.getEnergy(coords)

    fname = "out.xyz"
    print "saving coordinates to", fname
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    with open(fname, "w") as fout:
        for xyz, line2 in printlist:
            xyz = putInBox(xyz, boxl)
            printxyz(fout, xyz, line2=line2)

    from scipy.optimize import check_grad
    res = check_grad(pot.getEnergy, pot.getGradient, coords)
    print "testing gradient (should be small)", res
Пример #5
0
def test_molecule():
    otp = setupLWOTP()

    xyz = otp.getxyz()
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    import sys
    #with open("out.xyz", "w") as fout:
    printxyz(sys.stdout, xyz)

    aa = np.array([.2, .3, .4])
    for xyz, aanew in otp.getSymmetries(np.zeros(3), aa):
        printxyz(sys.stdout, xyz, line2="symmetry")
        xyz = otp.getxyz(aa=aanew)
        printxyz(sys.stdout, xyz, line2="symmetry from returned aa")
Пример #6
0
def test_molecule():
    otp = setupLWOTP()

    xyz = otp.getxyz()
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    import sys
    #with open("out.xyz", "w") as fout:
    printxyz(sys.stdout, xyz)
    
    aa = np.array([.2, .3, .4])
    for xyz, aanew in otp.getSymmetries( np.zeros(3), aa):
        printxyz(sys.stdout, xyz, line2="symmetry")
        xyz = otp.getxyz(aa = aanew)
        printxyz(sys.stdout, xyz, line2="symmetry from returned aa")
Пример #7
0
 def __call__(self, coords, **kwargs):
     from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz 
     printxyz(self.fout, coords)
     self.coordslist.append( coords.copy() )
Пример #8
0
coords = np.zeros(2 * 3 * nmol, np.float64)
coords[0:nmol *
       3] = np.random.uniform(-1, 1, [nmol * 3]) * 1.3 * (nsites)**(1. / 3)
for i in range(nmol):
    k = nmol * 3 + 3 * i
    coords[k:k + 3] = rot.random_aa()

#set up the takestep routine
from pygmin.potentials.rigid_bodies.take_step import RBTakeStep
takestep = RBTakeStep()

#set up the class to save lowest energy structures
from pygmin.storage.savenlowest import SaveN
saveit = SaveN(100)

#set up basinhopping
from pygmin.basinhopping import BasinHopping
bh = BasinHopping(coords, mysys, takestep, storage=saveit.insert)

#run basin hopping
bh.run(40)

#print the saved coords
fname = "otp.xyz"
print "saving xyz coords to", fname
from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
with open(fname, "w") as fout:
    for minimum in saveit.data:
        xyz = mysys.getxyz(minimum.coords)
        printxyz(fout, xyz, atom_type=["N", "O", "O"])
Пример #9
0
def testpot1():
    import itertools
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    pot, coords, coords1, coords2 = guesstsLJ()
    coordsinit = np.copy(coords)
    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)
    print ""
    

    
    
    from pygmin.printing.print_atoms_xyz import PrintEvent

    #print ret
    
    with open("out.xyz", "w") as fout:
        e = pot.getEnergy(coords1)
        print "energy of minima 1", e
        printxyz(fout, coords1, line2=str(e))
        e, grad = pot.getEnergyGradient(coordsinit)
        print "energy of NEB guess for the transition state", e, "rms grad", \
            np.linalg.norm(grad) / np.sqrt(float(len(coords))/3.)
        printxyz(fout, coordsinit, line2=str(e))
        e = pot.getEnergy(coords2)
        print "energy of minima 2", e
        printxyz(fout, coords2, line2=str(e))
        
        #mess up coords a bit
        coords += np.random.uniform(-1,1,len(coords))*0.05
        e = pot.getEnergy(coords)
        printxyz(fout, coords, line2=str(e))

        defaults.quenchParams["iprint"] = 1
        #defaults.lowestEigenvectorQuenchParams["iprint"] = 1
        defaults.tsSearchParams["iprint"] = 1
        
        printevent = PrintEvent(fout)
        print ""
        print "starting the transition state search"
        ret = findTransitionState(coords, pot, event=printevent, iprint=-1)
        print ret
        #coords, eval, evec, e, grad, rms = ret
        e = pot.getEnergy(ret.coords)
        printxyz(fout, coords2, line2=str(e))

    print "finished searching for transition state"
    print "energy", e
    print "rms grad", ret.rms
    print "eigenvalue", ret.eigenval
    
    if False:
        print "now try the same search with the dimer method"
        from pygmin.NEB.dimer import findTransitionState as dimerfindTS
        coords = coordsinit.copy()
        tau = np.random.uniform(-1,1,len(coords))
        tau /= np.linalg.norm(tau)
        ret = dimerfindTS(coords, pot, tau )
        enew, grad = pot.getEnergyGradient(ret.coords)
        print "energy", enew
        print "rms grad", np.linalg.norm(grad) / np.sqrt(float(len(ret.coords))/3.)
Пример #10
0
    coords[k : k + 3] = rot.random_aa()


#set up the takestep routine
from  pygmin.potentials.rigid_bodies.take_step import RBTakeStep
takestep = RBTakeStep()

#set up the class to save lowest energy structures
from pygmin.storage.savenlowest import SaveN
saveit = SaveN(100)

#set up basinhopping
from  pygmin.basinhopping import BasinHopping
bh = BasinHopping(coords, mysys, takestep, storage=saveit.insert )

#run basin hopping
bh.run(40)



#print the saved coords
fname = "otp.xyz"
print "saving xyz coords to", fname
from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
with open(fname, "w") as fout:
    for minimum in saveit.data:
        xyz = mysys.getxyz(minimum.coords)
        printxyz( fout, xyz, atom_type=["N", "O", "O"])


Пример #11
0
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 pygmin.optimize import lbfgs_ase as quench

coords, E, rms, funcalls = quench(coords, pot.getEnergyGradient, iprint=1)
printlist.append((coords.copy(), "intial coords"))
print "energy post quench", pot.getEnergy(coords)


from scipy.optimize import check_grad

res = check_grad(pot.getEnergy, pot.getGradient, coords)
print "testing gradient (should be small)", res


fname = "out.xyz"
print "saving coordinates to", fname
from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz

with open(fname, "w") as fout:
    for xyz, line2 in printlist:
        # xyz = putInBox(xyz, boxl)
        printxyz(fout, xyz, line2=line2)
Пример #12
0
 def printwrapper(self, E, coords, accepted):
     if self.count % self.printfrq == 0: 
         self.center(coords)
         printxyz(self.fout, coords, line2=str(E))
     self.count += 1
Пример #13
0
def test_sandbox(nmol=6):
    import copy
    from pygmin.potentials.lj import LJ

    #define the molecule types.
    #here use only one type, LWOTP
    otp = molecule.setupLWOTP()

    # define the interaction matrix for the system.
    # for LWOTP there is only one atom type, so this is trivial
    lj = LJ()
    interaction_matrix = [[lj]]

    #set up a list of molecules
    mols = [otp for i in range(nmol)]

    #set up the RBSandbox object
    mysys = RBSandbox(mols, interaction_matrix)
    nsites = mysys.nsites

    #get an initial set of coordinates
    comcoords = np.random.uniform(-1, 1, [nmol * 3]) * 1.3 * (nsites)**(1. / 3)
    aacoords = np.array([copy.copy(rot.random_aa()) for i in range(nmol)])
    aacoords = aacoords.reshape(3 * nmol)
    coords = np.zeros(2 * 3 * nmol, np.float64)
    coords[0:3 * nmol] = comcoords[:]
    coords[3 * nmol:2 * 3 * nmol] = aacoords[:]
    print "lencoords, len aacoords", len(coords), len(aacoords), len(comcoords)

    #print "xyz coords", mysys.transformToXYZ(coords)

    #save the initial set of coords
    printlist = []
    xyz = mysys.getxyz(coords)
    printlist.append((xyz.copy(), "initial"))

    #calculate the initial energy
    Einit = mysys.getEnergy(coords)
    print "initial energy", Einit

    #test the gradient
    numericV = mysys.NumericalDerivative(coords, 1e-10)
    numericV = numericV.copy()
    print "numeric V", numericV

    E, V = mysys.getEnergyGradient(coords)
    print "energy from gradient", E, "difference", E - Einit
    #print "analytic gradient", V
    maxgrad_relative = np.max(np.abs(V - numericV) / np.abs(V))
    maxgraddiff = np.max(np.abs(V - numericV))
    print "max error in gradient", maxgraddiff, "max relative", maxgrad_relative

    #do a quench to make sure everything is working well
    from pygmin.optimize import lbfgs_scipy
    coords, E, rms, funcalls = lbfgs_scipy(coords,
                                           mysys.getEnergyGradient,
                                           iprint=-1)
    print "postquench E", E, "rms", rms, "funtion calls", funcalls
    xyz = mysys.getxyz(coords)
    printlist.append((xyz.copy(), "post quench"))

    #print the saved coords
    fname = "otp.xyz"
    print "saving xyz coords to", fname
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    with open(fname, "w") as fout:
        for xyz, line2 in printlist:
            printxyz(fout, xyz, line2=line2, atom_type=["N", "O", "O"])

    test_symmetries(coords, mysys)
Пример #14
0
def test_sandbox(nmol = 6):
    import copy
    from pygmin.potentials.lj import LJ

    #define the molecule types.
    #here use only one type, LWOTP
    otp = molecule.setupLWOTP()


    # define the interaction matrix for the system.
    # for LWOTP there is only one atom type, so this is trivial
    lj = LJ()
    interaction_matrix = [[lj]]


    #set up a list of molecules
    mols = [otp for i in range(nmol)]

    #set up the RBSandbox object
    mysys = RBSandbox(mols, interaction_matrix)
    nsites = mysys.nsites
    

    #get an initial set of coordinates
    comcoords = np.random.uniform(-1,1,[nmol*3]) * 1.3*(nsites)**(1./3)
    aacoords = np.array( [copy.copy(rot.random_aa()) for i in range(nmol)] )
    aacoords = aacoords.reshape(3*nmol)
    coords = np.zeros(2*3*nmol, np.float64)
    coords[0:3*nmol] = comcoords[:]
    coords[3*nmol:2*3*nmol] = aacoords[:]
    print "lencoords, len aacoords", len (coords), len(aacoords), len(comcoords)

    #print "xyz coords", mysys.transformToXYZ(coords)

    #save the initial set of coords
    printlist = []
    xyz = mysys.getxyz( coords )
    printlist.append( (xyz.copy(), "initial"))

    #calculate the initial energy
    Einit = mysys.getEnergy(coords)
    print "initial energy", Einit

    #test the gradient
    numericV = mysys.NumericalDerivative(coords, 1e-10)
    numericV = numericV.copy()
    print "numeric V", numericV

    E, V = mysys.getEnergyGradient(coords)
    print "energy from gradient", E, "difference", E - Einit
    #print "analytic gradient", V
    maxgrad_relative = np.max(np.abs(V-numericV)/np.abs(V))
    maxgraddiff = np.max(np.abs(V - numericV))
    print "max error in gradient", maxgraddiff, "max relative", maxgrad_relative

    #do a quench to make sure everything is working well
    from pygmin.optimize import lbfgs_scipy
    coords, E, rms, funcalls = lbfgs_scipy(coords, mysys.getEnergyGradient, iprint=-1)
    print "postquench E", E, "rms", rms, "funtion calls", funcalls
    xyz = mysys.getxyz(coords )
    printlist.append( (xyz.copy(), "post quench"))


    #print the saved coords
    fname = "otp.xyz"
    print "saving xyz coords to", fname
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    with open(fname, "w") as fout:
        for xyz, line2 in printlist:
            printxyz( fout, xyz, line2=line2, atom_type=["N", "O", "O"])
            
    
    test_symmetries(coords, mysys)
Пример #15
0
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 pygmin.optimize import lbfgs_ase as quench

coords, E, rms, funcalls = quench(coords, pot.getEnergyGradient, iprint=1)
printlist.append((coords.copy(), "intial coords"))
print "energy post quench", pot.getEnergy(coords)

from scipy.optimize import check_grad

res = check_grad(pot.getEnergy, pot.getGradient, coords)
print "testing gradient (should be small)", res

fname = "out.xyz"
print "saving coordinates to", fname
from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
with open(fname, "w") as fout:
    for xyz, line2 in printlist:
        #xyz = putInBox(xyz, boxl)
        printxyz(fout, xyz, line2=line2)
Пример #16
0
 def __call__(self, coords, **kwargs):
     from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz 
     printxyz(self.fout, coords)
     self.coordslist.append( coords.copy() )
Пример #17
0
 def printwrapper(self, E, coords, accepted):
     if self.count % self.printfrq == 0:
         self.center(coords)
         printxyz(self.fout, coords, line2=str(E))
     self.count += 1
Пример #18
0
def testpot1():
    import itertools
    from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz
    pot, coords, coords1, coords2 = guesstsLJ()
    coordsinit = np.copy(coords)
    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)
    print ""

    from pygmin.printing.print_atoms_xyz import PrintEvent

    #print ret

    with open("out.xyz", "w") as fout:
        e = pot.getEnergy(coords1)
        print "energy of minima 1", e
        printxyz(fout, coords1, line2=str(e))
        e, grad = pot.getEnergyGradient(coordsinit)
        print "energy of NEB guess for the transition state", e, "rms grad", \
            np.linalg.norm(grad) / np.sqrt(float(len(coords))/3.)
        printxyz(fout, coordsinit, line2=str(e))
        e = pot.getEnergy(coords2)
        print "energy of minima 2", e
        printxyz(fout, coords2, line2=str(e))

        #mess up coords a bit
        coords += np.random.uniform(-1, 1, len(coords)) * 0.05
        e = pot.getEnergy(coords)
        printxyz(fout, coords, line2=str(e))

        printevent = PrintEvent(fout)
        print ""
        print "starting the transition state search"
        ret = findTransitionState(coords, pot, iprint=-1)
        print ret
        #coords, eval, evec, e, grad, rms = ret
        e = pot.getEnergy(ret.coords)
        printxyz(fout, coords2, line2=str(e))

    print "finished searching for transition state"
    print "energy", e
    print "rms grad", ret.rms
    print "eigenvalue", ret.eigenval

    if False:
        print "now try the same search with the dimer method"
        from pygmin.NEB.dimer import findTransitionState as dimerfindTS
        coords = coordsinit.copy()
        tau = np.random.uniform(-1, 1, len(coords))
        tau /= np.linalg.norm(tau)
        ret = dimerfindTS(coords, pot, tau)
        enew, grad = pot.getEnergyGradient(ret.coords)
        print "energy", enew
        print "rms grad", np.linalg.norm(grad) / np.sqrt(
            float(len(ret.coords)) / 3.)