示例#1
0
文件: dmagmin.py 项目: wwwtyro/PyGMIN
def quenchCrystal(coords, pot, **kwargs):
    ''' Special quench routine for crystals which makes sure that the final structure is a reduced cell '''
    coords, E, rms, calls = mylbfgs(coords, pot, **kwargs)
    #while(GMIN.reduceCell(coords)):
    if(GMIN.reduceCell(coords)):
        #print "Reduced cell, redo minimization"
        coords, E, rms, callsn = mylbfgs(coords, pot, **kwargs)
        calls+=callsn
    return coords, E, rms, calls
示例#2
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" 
示例#3
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"
示例#4
0
def test():
    pi = np.pi
    nspins = 6

    #phases = np.zeros(nspins)
    pot = XYModel(nspins, phi=np.pi / 8.)  #, phases=phases)

    angles = np.random.uniform(-pi / 4, pi / 4, nspins)
    print angles

    e = pot.getEnergy(angles)
    print e

    print "numerical gradient"
    ret = pot.getEnergyGradientNumerical(angles)
    print ret[1]
    print "analytical gradient"
    ret2 = pot.getEnergyGradient(angles)
    print ret2[1]
    print ret[0]
    print ret2[0]

    #try a quench
    from pygmin.optimize import mylbfgs
    ret = mylbfgs(angles, pot)

    print "quenched e = ", ret.energy
    print ret.coords

    test_basin_hopping(pot, angles)
示例#5
0
文件: xyspin1d.py 项目: js850/PyGMIN
def test():
    pi = np.pi
    nspins = 6
    
    #phases = np.zeros(nspins)
    pot = XYModel(nspins, phi = np.pi/8.) #, phases=phases)
        
    angles = np.random.uniform(-pi/4, pi/4, nspins)
    print angles

    e = pot.getEnergy(angles)
    print e
    
    print "numerical gradient"
    ret = pot.getEnergyGradientNumerical(angles)
    print ret[1]
    print "analytical gradient"
    ret2 = pot.getEnergyGradient(angles)
    print ret2[1]
    print ret[0]
    print ret2[0]
    
    
    #try a quench
    from pygmin.optimize import mylbfgs
    ret = mylbfgs(angles, pot)
    
    print "quenched e = ", ret.energy
    print ret.coords
    
    test_basin_hopping(pot, angles)
    def testBLJ(self):
        X1 = np.copy(self.X1)
        X2 = np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3)/2
        
        #run a quench so the structure is not crazy
        ret = mylbfgs(X2, self.pot)
        X2 = ret.coords

        self.runtest(X1, X2, MinPermDistCluster(measure=MeasureAtomicCluster(permlist=self.permlist)))
示例#7
0
文件: lj.py 项目: js850/PyGMIN
 def setUp(self):
     from pygmin.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 setUp(self):
     from pygmin.potentials.ljpshiftfast import LJpshift as BLJ
     
     self.natoms = 25
     self.ntypeA = int(self.natoms * .8)
     self.pot = BLJ(self.natoms, self.ntypeA)
     self.permlist = [range(self.ntypeA), range(self.ntypeA, self.natoms)]
     
     self.X1 = np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3)/2
     
     #run a quench so the structure is not crazy
     ret = mylbfgs(self.X1, self.pot)
     self.X1 = ret.coords
示例#9
0
    def __init__(self, coords, potential, takeStep, storage=None, event_after_step=[], \
            acceptTest=None,  \
            temperature=1.0, \
            quench = None, \
            confCheck = [], \
            outstream = sys.stdout,
            insert_rejected = False
            ):
        #########################################################################
        #initialize MonteCarlo base class
        #########################################################################
        MonteCarlo.__init__(self, coords, potential, takeStep, \
                            storage=storage, \
                            event_after_step=event_after_step, \
                            acceptTest=acceptTest,  \
                            temperature=temperature, \
                            confCheck = confCheck, \
                            outstream=outstream,store_initial=False)

        if quench is None:
            quench = lambda coords : mylbfgs(coords, self.potential)
        self.quench = quench
                
        #########################################################################
        #do initial quench
        #########################################################################
        self.markovE_old = self.markovE
        res = self.quench(self.coords)
        
        self.coords = res.coords
        self.markovE = res.energy
        self.rms = res.rms
        self.funcalls = res.nfev

        self.insert_rejected = insert_rejected
        
        if(self.storage):
            self.storage(self.markovE, self.coords)
        
        #print the initial quench
        self.acceptstep = True
        self.trial_energy = self.markovE
        self.printStep()
        
        self.result.energy = self.markovE
        self.result.coords = self.coords.copy()
示例#10
0
    def __init__(self, coords, potential, takeStep, storage=None, event_after_step=[], \
            acceptTest=None,  \
            temperature=1.0, \
            quench = None, \
            confCheck = [], \
            outstream = sys.stdout,
            insert_rejected = False
            ):
        #########################################################################
        #initialize MonteCarlo base class
        #########################################################################
        MonteCarlo.__init__(self, coords, potential, takeStep, \
                            storage=storage, \
                            event_after_step=event_after_step, \
                            acceptTest=acceptTest,  \
                            temperature=temperature, \
                            confCheck = confCheck, \
                            outstream=outstream,store_initial=False)

        if quench is None:
            quench = lambda coords: mylbfgs(coords, self.potential)
        self.quench = quench

        #########################################################################
        #do initial quench
        #########################################################################
        self.markovE_old = self.markovE
        res = self.quench(self.coords)

        self.coords = res.coords
        self.markovE = res.energy
        self.rms = res.rms
        self.funcalls = res.nfev

        self.insert_rejected = insert_rejected

        if (self.storage):
            self.storage(self.markovE, self.coords)

        #print the initial quench
        self.acceptstep = True
        self.trial_energy = self.markovE
        self.printStep()

        self.result.energy = self.markovE
        self.result.coords = self.coords.copy()
示例#11
0
def minima_from_ts(pot, xt, n=None, quench=None, **kwargs):
    """
    step off either side of a transition state and quench to find the minima
    
    Parameters
    ----------
    pot : potential object
    xt : array
        transition state coords
    stepmin : float
        initial guess for size of pushoff when stepping off the transition state
    stepmax : float
        maximum size of pushoff when stepping off the transition state
    gdiff : float
        criterion for choosing a step size.  Try to choose a stepsize so that:: 
        
            (gradpar - gradpar_ts) / gradpar_ts >= gdiff
        
        where gradpar and gradpar_ts is the parallel component of the gradient
        after the step and at the top of the transition state
    quenchRoutine : callable
        routine to use to do the quenching
    quenchParams : dict
        parameters to pass to quenchRoutine
    verbose : bool
    """
    # if no direction is given, choose random direction
    if n==None:
        # TODO: replace by better algorithm with uniform sampling
        n = np.random.random(xt.shape)-0.5
    
    if quench is None:
        quench = lambda coords : mylbfgs(coords, pot)        
    
    #x1 = xt - displace*n
    x1 = determinePushoff(pot, xt, n, **kwargs)
    x2 = determinePushoff(pot, xt, -n, **kwargs)
    #x2 = xt + displace*n
    #e1,g1 = getEnergyGradient(x1)
    #e2,g2 = getEnergyGradient(x2)
    #print np.dot(g1,g2)
    minimum1 = quench(x1)
    minimum2 = quench(x2)
    return minimum1, minimum2
示例#12
0
def test2():
    import pygmin.potentials.ljpshiftfast as ljpshiftfast
    import pygmin.potentials.ljpshift as ljpshift
    from pygmin.optimize import mylbfgs
    fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/coords1.quench"
    fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/test.coords"
    #fname = "out.coords"
    if False:
        coords = np.array(np.loadtxt(fname))
        coords = coords.reshape(-1)
        boxl = 11.05209
    else:
        natoms = 200
        coords = np.random.uniform(-1, 1, natoms * 3) * (natoms)**(1. / 3) / 2
        print "max, min coords", coords.max(), coords.min()
        boxl = 5

    natoms = len(coords) / 3
    ntypeA = int(natoms * 0.8)
    rcut = 2.5
    print "natoms", natoms, "ntypea", ntypeA

    blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)
    bljfast = ljpshiftfast.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 "energy difference", (epot - eblj)

    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 False:
        print "quenching"
        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 "max, min quenched coords", coords.max(), coords.min()

        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)
    elif True:
        print "quenching"
        ret2 = mylbfgs(coords, pot, iprint=-1)
        print "energy from quench2", ret2.energy
        print "max, min quenched coords", ret2.coords.max(), ret2.coords.min()

        print "ret2 evaluated in both potentials", pot.getEnergy(
            ret2.coords), blj.getEnergy(ret2.coords)
        print "and in blj fast                  ", bljfast.getEnergy(
            ret2.coords)
示例#13
0
    vec = rotations.vec_random()
    coords[i, :] = make2dVector(vec)
coords = np.reshape(coords, [nspins * 2])
#print np.shape(coords)
coordsinit = np.copy(coords)

#print "fields", pot.fields
print coords

e = pot.getEnergy(coords)
print "energy ", e

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

ret = mylbfgs(coords, pot)

print "quenched e = ", ret.energy, "funcalls", ret.nfev
print ret.coords

m = getm(ret[0])
print "magnetization after quench", m

#do basin hopping

from pygmin.basinhopping import BasinHopping
from pygmin.takestep.displace import RandomDisplacement
from pygmin.takestep.adaptive import AdaptiveStepsize
from pygmin.storage import savenlowest

takestep = RandomDisplacement(stepsize=np.pi / 4)
示例#14
0
 def get_minimizer(self, **kwargs):
     """return a function to minimize the structure"""
     pot = self.get_potential()
     kwargs = dict_copy_update(self.params["structural_quench_params"],
                               kwargs)
     return lambda coords: mylbfgs(coords, pot, **kwargs)
示例#15
0
文件: 2dRAHM.py 项目: js850/PyGMIN
    vec = rotations.vec_random()
    coords[i,:] = make2dVector(vec)
coords = np.reshape(coords, [nspins*2])
#print np.shape(coords)
coordsinit = np.copy(coords)

#print "fields", pot.fields
print coords


e = pot.getEnergy(coords)
print "energy ", e

print "try a quench"
from pygmin.optimize import mylbfgs
ret = mylbfgs(coords, pot)

print "quenched e = ", ret.energy, "funcalls", ret.nfev
print ret.coords

m = getm( ret[0] )
print "magnetization after quench", m


#do basin hopping

from pygmin.basinhopping import BasinHopping
from pygmin.takestep.displace import RandomDisplacement
from pygmin.takestep.adaptive import AdaptiveStepsize
from pygmin.storage import savenlowest
示例#16
0
    ntypeA = int(natoms*0.8)
    rcut = 2.5
    print "natoms", natoms, "ntypea", ntypeA
    
    bljslow = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)
    blj =              LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)
    
    ebljslow = bljslow.getEnergy(coords)
    print "blj energy slow", ebljslow
    eblj = blj.getEnergy(coords)
    print "blj energy     ", eblj
    blj.test_potential(coords)
    print ""
    
    print "partially quenching coords"
    ret1 = mylbfgs(coords, blj, iprint=-11, tol=1.)
    coords = ret1.coords
    ebljslow = bljslow.getEnergy(coords)
    print "blj energy slow", ebljslow
    eblj = blj.getEnergy(coords)
    print "blj energy     ", eblj
    blj.test_potential(coords, eps=1e-6)
    print ""


    
    print "quenching coords"
    ret1 = mylbfgs(coords, blj, iprint=-11)
    coords = ret1.coords
    ebljslow = bljslow.getEnergy(coords)
    print "blj energy slow", ebljslow
示例#17
0
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 pygmin.optimize import mylbfgs
    ret = mylbfgs(coords, pot)

    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)
示例#18
0
 def get_minimizer(self, **kwargs):
     """return a function to minimize the structure"""
     pot = self.get_potential()
     kwargs = dict_copy_update(self.params["structural_quench_params"], kwargs)        
     return lambda coords: mylbfgs(coords, pot, **kwargs)
示例#19
0
文件: benchmark.py 项目: js850/PyGMIN
import sys
import _lj
import _lbfgs
from pygmin.optimize import mylbfgs
N=100
natoms=[10, 13, 20, 30, 31, 38, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]

print "benchmarking lennard jones potential, %d atoms, %d calls", natoms, N
pot_old = LJ()
pot = _lj.LJ()

clbfgs = _lbfgs.LBFGS(pot)

res = open("results.txt", "w")

for na in natoms:
    t0 = time.time()
    for i in xrange(N):
        x = np.random.random(3*na) - 0.5
        ret = clbfgs.run(x)

    t1 = time.time()
    for i in xrange(N):
        x = np.random.random(3*na) - 0.5
        ret = mylbfgs(x, pot_old, tol=1e-5)
    res.write("%d %f %f\n"%(na, t1-t0, time.time()-t1))
    print "%d %f %f\n"%(na, t1-t0, time.time()-t1)

res.close()

示例#20
0
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 pygmin.optimize import mylbfgs
    ret = mylbfgs(coords, pot)
    
    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)
示例#21
0
def test2():
    import pygmin.potentials.ljpshiftfast as ljpshiftfast
    import pygmin.potentials.ljpshift as ljpshift
    from pygmin.optimize import mylbfgs
    fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/coords1.quench"
    fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/test.coords"
    #fname = "out.coords"
    if False:
        coords = np.array(np.loadtxt(fname))
        coords = coords.reshape(-1)
        boxl = 11.05209
    else:
        natoms = 200
        coords = np.random.uniform(-1,1,natoms*3)*(natoms)**(1./3)/2
        print "max, min coords", coords.max(), coords.min()
        boxl = 5

    natoms = len(coords) /3
    ntypeA = int(natoms*0.8)
    rcut = 2.5
    print "natoms", natoms, "ntypea", ntypeA
    
    blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl)
    bljfast = ljpshiftfast.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 "energy difference", (epot - eblj)
    
    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 False:
        print "quenching"
        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 "max, min quenched coords", coords.max(), coords.min()


        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)
    elif True:
        print "quenching"
        ret2 = mylbfgs(coords, pot, iprint=-1)
        print "energy from quench2", ret2.energy
        print "max, min quenched coords", ret2.coords.max(), ret2.coords.min()

        print "ret2 evaluated in both potentials", pot.getEnergy(ret2.coords), blj.getEnergy(ret2.coords)
        print "and in blj fast                  ", bljfast.getEnergy(ret2.coords)
示例#22
0
clbfgs = _lbfgs.LBFGS(pot)

t0 = time.time()
for i in xrange(100):
    x = 1. * (np.random.random(3 * natoms) - 0.5)
    ret = clbfgs.run(x)
    #print ret
    #print ret[0]
    #print pot.get_energy(ret[0])
    e, g = pot.get_energy_gradient(ret[0])
    print "C", np.linalg.norm(g)

t1 = time.time()
for i in xrange(100):
    x = 1. * np.random.random(3 * natoms)
    ret = mylbfgs(x, pot_old, tol=1e-5)
    print "PY:", np.linalg.norm(pot_old.getEnergyGradient(ret[0])[1])

print time.time() - t1, t1 - t0

t0 = time.time()
for i in xrange(N):
    e, g = pot_old.getEnergyGradient(x)
time_f2py = time.time() - t0
print "f2py potential", e, "time", time.time() - t0

t0 = time.time()
for i in xrange(N):
    e, g = pot.get_energy_gradient(x)
time_cython = time.time() - t0
print "cython potential", e, "time", time.time() - t0
示例#23
0
#phases = np.zeros(nspins)
pot = XYModel( dim = [L,L], phi = np.pi) #, phases=phases)


angles = np.random.uniform(-pi, pi, nspins)
print angles

e = pot.getEnergy(angles)
print "energy ", e



#try a quench
if False:
    from pygmin.optimize import mylbfgs
    ret = mylbfgs(angles, pot)
    
    print ret


#set up and run basin hopping

from pygmin.basinhopping import BasinHopping
from pygmin.takestep.displace import RandomDisplacement
from pygmin.takestep.adaptive import AdaptiveStepsize
from pygmin.storage import savenlowest

#should probably use a different take step routine  which takes into account
#the cyclical periodicity of angles
takestep = RandomDisplacement(stepsize = np.pi/4)
takestepa = AdaptiveStepsize(takestep, frequency = 20)
示例#24
0
L = 24
nspins = L**2

#phases = np.zeros(nspins)
pot = XYModel(dim=[L, L], phi=np.pi)  #, phases=phases)

angles = np.random.uniform(-pi, pi, nspins)
print angles

e = pot.getEnergy(angles)
print "energy ", e

#try a quench
if False:
    from pygmin.optimize import mylbfgs
    ret = mylbfgs(angles, pot)

    print ret

#set up and run basin hopping

from pygmin.basinhopping import BasinHopping
from pygmin.takestep.displace import RandomDisplacement
from pygmin.takestep.adaptive import AdaptiveStepsize
from pygmin.storage import savenlowest

#should probably use a different take step routine  which takes into account
#the cyclical periodicity of angles
takestep = RandomDisplacement(stepsize=np.pi / 4)
takestepa = AdaptiveStepsize(takestep, frequency=20)
storage = savenlowest.SaveN(500)