示例#1
0
文件: _mylbfgs.py 项目: js850/PyGMIN
def runtest(X, pot, natoms = 100, iprint=-1):
    from _lbfgs_py import PrintEvent
    tol = 1e-5
    maxstep = 0.005

    Xinit = np.copy(X)
    e, g = pot.getEnergyGradient(X)
    print "energy", e
    
    lbfgs = LBFGS(X, pot, maxstep = 0.1, nsteps=10000, tol=tol,
                  iprint=iprint, H0=2.)
    printevent = PrintEvent( "debugout.xyz")
    lbfgs.attachEvent(printevent)
    
    ret = lbfgs.run()
    print ret
    
    print ""
    print "now do the same with scipy lbfgs"
    from pygmin.optimize import lbfgs_scipy as quench
    ret = quench(Xinit, pot, tol = tol)
    print ret
    #print ret[1], ret[2], ret[3]    
    
    if False:
        print "now do the same with scipy bfgs"
        from pygmin.optimize import bfgs as oldbfgs
        ret = oldbfgs(Xinit, pot, tol = tol)
        print ret
    
    if False:
        print "now do the same with gradient + linesearch"
        import _bfgs
        gpl = _bfgs.GradientPlusLinesearch(Xinit, pot, maxstep = 0.1)  
        ret = gpl.run(1000, tol = 1e-6)
        print ret
            
    if False:
        print "calling from wrapper function"
        from pygmin.optimize import lbfgs_py
        ret = lbfgs_py(Xinit, pot, tol = tol)
        print ret
        
    if True:
        print ""
        print "now do the same with lbfgs_py"
        from pygmin.optimize import lbfgs_py
        ret = lbfgs_py(Xinit, pot, tol = tol)
        print ret



    try:
        import pygmin.utils.pymolwrapper as pym
        pym.start()
        for n, coords in enumerate(printevent.coordslist):
            coords=coords.reshape(natoms, 3)
            pym.draw_spheres(coords, "A", n)
    except ImportError:
        print "error loading pymol"
示例#2
0
def test(natoms = 40, boxl=4.):
    import pygmin.potentials.ljpshiftfast as ljpshift
    import pygmin.defaults as defaults
    from pygmin.utils.neighbor_list import makeBLJNeighborListPot
    ntypeA = int(natoms*0.8)
    ntypeB = natoms - ntypeA
    rcut = 2.5
    freezelist = range(ntypeA/2) + range(ntypeA,ntypeA+ntypeB/2)
    nfrozen = len(freezelist)
    print "nfrozen", nfrozen
    coords = np.random.uniform(-1,1,natoms*3)*(natoms)**(1./3)/2
    
    
    NLblj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)
    blj = FreezePot(NLblj, freezelist)

    pot = makeBLJNeighborListPotFreeze(natoms, freezelist, ntypeA=ntypeA, rcut=rcut, boxl=boxl)
    #pot = FreezePot(NLpot, freezelist)
    

    
    eblj = blj.getEnergy(coords)
    print "blj energy", eblj
    
    epot = pot.getEnergy(coords)
    print "mcpot energy", epot
    
    print "difference", (epot - eblj)/eblj
    
    ret1 = defaults.quenchRoutine(coords, blj.getEnergyGradient, iprint=-11)
    np.savetxt("out.coords", ret1[0])
    print "energy from quench1", ret1[1]
    ret2 = defaults.quenchRoutine(coords, pot.getEnergyGradient, iprint=-1)
    print "energy from quench2", ret2[1]
    
    print "ret1 evaluated in both potentials", pot.getEnergy(ret1[0]), blj.getEnergy(ret1[0])
    print "ret2 evaluated in both potentials", pot.getEnergy(ret2[0]), blj.getEnergy(ret2[0])
    
    coords = ret1[0]
    e1, g1 = blj.getEnergyGradient(coords)
    e2, g2 = pot.getEnergyGradient(coords)
    print "energy difference from getEnergyGradient", (e2 - e1)
    print "largest gradient difference", np.max(np.abs(g2-g1))
    print "rms gradients", np.linalg.norm(g1)/np.sqrt(len(g1)), np.linalg.norm(g2)/np.sqrt(len(g1))

    if True:
        for subpot in pot.pot.potentials:
            nl = subpot
            print "number of times neighbor list was remade:", nl.buildcount, "out of", nl.count
    
    if False:
        try: 
            import pygmin.utils.pymolwrapper as pym
            pym.start()
            pym.draw_spheres(np.reshape(coords,[-1,3]), "A", 1)
            pym.draw_spheres(np.reshape(ret1[0],[-1,3]), "A", 2)
            pym.draw_spheres(np.reshape(ret2[0],[-1,3]), "A", 3)
        except ImportError:
            print "Could not draw using pymol, skipping this step" 
示例#3
0
def runtest(X, pot, natoms=100, iprint=-1):
    from lbfgs_py import PrintEvent
    tol = 1e-5
    maxstep = 0.005

    Xinit = np.copy(X)
    e, g = pot.getEnergyGradient(X)
    print "energy", e

    lbfgs = LBFGS(X, pot, maxstep=0.1)
    printevent = PrintEvent("debugout.xyz")
    #lbfgs.attachEvent(printevent)

    ret = lbfgs.run(10000, tol=tol, iprint=iprint)
    print "done", ret[1], ret[2], ret[3], ret[5]

    print ""
    print "now do the same with scipy lbfgs"
    from pygmin.optimize import lbfgs_scipy as quench
    ret = quench(Xinit, pot.getEnergyGradient, tol=tol)
    print ret[1], ret[2], ret[3]

    if False:
        print "now do the same with scipy bfgs"
        from pygmin.optimize import bfgs as oldbfgs
        ret = oldbfgs(Xinit, pot.getEnergyGradient, tol=tol)
        print ret[1], ret[2], ret[3]

    if False:
        print "now do the same with gradient + linesearch"
        import bfgs
        gpl = bfgs.GradientPlusLinesearch(Xinit, pot, maxstep=0.1)
        ret = gpl.run(1000, tol=1e-6)
        print ret[1], ret[2], ret[3]

    if True:
        print ""
        print "calling from wrapper function"
        from pygmin.optimize import mylbfgs as quench
        ret = quench(Xinit, pot.getEnergyGradient, tol=tol)
        print ret[1], ret[2], ret[3]

    if True:
        print ""
        print "now do the same with lbfgs_py"
        from pygmin.optimize import lbfgs_py
        ret = lbfgs_py(Xinit, pot.getEnergyGradient, tol=tol)
        print ret[1], ret[2], ret[3]

    if False:
        import pygmin.utils.pymolwrapper as pym
        pym.start()
        for n, coords in enumerate(printevent.coordslist):
            coords = coords.reshape(natoms, 3)
            pym.draw_spheres(coords, "A", n)
示例#4
0
def test(natoms = 40, boxl=None):
    import pygmin.potentials.ljpshiftfast as ljpshift
    from pygmin.optimize import mylbfgs
    ntypeA = int(natoms*0.8)
    rcut = 2.5
    coords = np.random.uniform(-1,1,natoms*3)*(natoms)**(1./3)/2
    
    blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)

    pot = makeBLJNeighborListPot(natoms, ntypeA=ntypeA, rcut=rcut, boxl=boxl)
    

    
    eblj = blj.getEnergy(coords)
    print "blj energy", eblj
    
    epot = pot.getEnergy(coords)
    print "mcpot energy", epot
    
    print "difference", (epot - eblj)/eblj
    
    ret1 = mylbfgs(coords, blj, iprint=-11)
    np.savetxt("out.coords", ret1.coords)
    print "energy from quench1", ret1.energy
    ret2 = mylbfgs(coords, pot, iprint=-1)
    print "energy from quench2", ret2.energy
    
    print "ret1 evaluated in both potentials", pot.getEnergy(ret1.coords), blj.getEnergy(ret1.coords)
    print "ret2 evaluated in both potentials", pot.getEnergy(ret2.coords), blj.getEnergy(ret2.coords)
    
    coords = ret1.coords
    e1, g1 = blj.getEnergyGradient(coords)
    e2, g2 = pot.getEnergyGradient(coords)
    print "energy difference from getEnergyGradient", (e2 - e1)
    print "largest gradient difference", np.max(np.abs(g2-g1))
    print "rms gradients", np.linalg.norm(g1)/np.sqrt(len(g1)), np.linalg.norm(g2)/np.sqrt(len(g1))

    
    for subpot in pot.potentials:
        nl = subpot.neighborList
        print "number of times neighbor list was remade", nl.buildcount, "out of", nl.count
    
    if False:
        try: 
            import pygmin.utils.pymolwrapper as pym
            pym.start()
            pym.draw_spheres(np.reshape(coords,[-1,3]), "A", 1)
            pym.draw_spheres(np.reshape(ret1.coords,[-1,3]), "A", 2)
            pym.draw_spheres(np.reshape(ret2.coords,[-1,3]), "A", 3)
        except ImportError:
            print "Could not draw using pymol, skipping this step" 
示例#5
0
def test(natoms=40, boxl=None):
    import pygmin.potentials.ljpshiftfast as ljpshift
    from pygmin.optimize import mylbfgs
    ntypeA = int(natoms * 0.8)
    rcut = 2.5
    coords = np.random.uniform(-1, 1, natoms * 3) * (natoms)**(1. / 3) / 2

    blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)

    pot = makeBLJNeighborListPot(natoms, ntypeA=ntypeA, rcut=rcut, boxl=boxl)

    eblj = blj.getEnergy(coords)
    print "blj energy", eblj

    epot = pot.getEnergy(coords)
    print "mcpot energy", epot

    print "difference", (epot - eblj) / eblj

    ret1 = mylbfgs(coords, blj, iprint=-11)
    np.savetxt("out.coords", ret1.coords)
    print "energy from quench1", ret1.energy
    ret2 = mylbfgs(coords, pot, iprint=-1)
    print "energy from quench2", ret2.energy

    print "ret1 evaluated in both potentials", pot.getEnergy(
        ret1.coords), blj.getEnergy(ret1.coords)
    print "ret2 evaluated in both potentials", pot.getEnergy(
        ret2.coords), blj.getEnergy(ret2.coords)

    coords = ret1.coords
    e1, g1 = blj.getEnergyGradient(coords)
    e2, g2 = pot.getEnergyGradient(coords)
    print "energy difference from getEnergyGradient", (e2 - e1)
    print "largest gradient difference", np.max(np.abs(g2 - g1))
    print "rms gradients", np.linalg.norm(g1) / np.sqrt(
        len(g1)), np.linalg.norm(g2) / np.sqrt(len(g1))

    for subpot in pot.potentials:
        nl = subpot.neighborList
        print "number of times neighbor list was remade", nl.buildcount, "out of", nl.count

    if False:
        try:
            import pygmin.utils.pymolwrapper as pym
            pym.start()
            pym.draw_spheres(np.reshape(coords, [-1, 3]), "A", 1)
            pym.draw_spheres(np.reshape(ret1.coords, [-1, 3]), "A", 2)
            pym.draw_spheres(np.reshape(ret2.coords, [-1, 3]), "A", 3)
        except ImportError:
            print "Could not draw using pymol, skipping this step"
示例#6
0
                        self.mcstep,
                        temperature=self.T,
                        outstream=None)
        mc.run(self.nsteps)
        coords[:] = mc.coords[:]

    def updateStep(self, acc, **kwargs):
        pass


natoms = 12

# random initial coordinates
coords = np.random.random(3 * natoms)
potential = lj.LJ()

reseed = TakeStepMonteCarlo(potential, T=100, nsteps=1000)
takestep = displace.RandomDisplacement(stepsize=0.5)

stepGroup = group.Reseeding(takestep, reseed, maxnoimprove=20)

opt = bh.BasinHopping(coords, potential, takeStep=stepGroup)
opt.run(100)

# some visualization
try:
    import pygmin.utils.pymolwrapper as pym
    pym.start()
    pym.draw_spheres(opt.coords, "A", 1)
except:
    print "Could not draw using pymol, skipping this step"
示例#7
0
文件: 1_basic.py 项目: js850/PyGMIN
# -*- coding: iso-8859-1 -*-
############################################################
#Example 1: Simple basin hopping
############################################################
import numpy as np

import pygmin.potentials.lj as lj
import pygmin.basinhopping as bh
from pygmin.takestep import displace

natoms = 12

# random initial coordinates
coords=np.random.random(3*natoms)
potential = lj.LJ()

step = displace.RandomDisplacement(stepsize=0.5)

opt = bh.BasinHopping(coords, potential, takeStep=step)
opt.run(100)

# some visualization
try: 
    import pygmin.utils.pymolwrapper as pym
    pym.start()
    pym.draw_spheres(opt.coords, "A", 1)
except:
    print "Could not draw using pymol, skipping this step"