示例#1
0
    def minimize(self):
        #shape(visits2d) is now (nqbins, nebins, nreps)
        #we need it to be (nreps, nqbins*nebins)
        #first reorder indices
        nreps = self.nrep
        nebins = self.nebins
        nqbins = self.nqbins
        nbins = self.nebins * self.nqbins
        #visits = np.zeros([nreps, nebins, nqbins], np.integer)
        reduced_energy = np.zeros([nreps, nebins, nqbins])
#        for k in range(self.nrep):
#            for j in range(self.nqbins):
#                for i in range(self.nebins):
#                    #visits[k,i,j] = self.visits2d[i,j,k]
#                    reduced_energy[k,i,j] = self.binenergy[i] / (self.Tlist[k]*self.k_B)
        for j in range(self.nqbins):
            reduced_energy[:,:,j] = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis]*self.k_B)
                    
        visits = self.visits2d
        visits = np.reshape( visits, [nreps, nbins ]) 
        reduced_energy = np.reshape( reduced_energy, [nreps, nbins])           
        self.logP = np.where( visits != 0, np.log( visits.astype(float) ), 0 )

        
        from wham_potential import WhamPotential
        whampot = WhamPotential( self.logP, reduced_energy )
        
        nvar = nbins + nreps
        X = np.random.rand(nvar)
        print "initial energy", whampot.getEnergy(X)
        try: 
            from pygmin.optimize import mylbfgs as quench
            ret = quench(X, whampot, iprint=10, maxstep = 1e4)
        except ImportError:
            from pygmin.optimize import lbfgs_scipy as quench
            ret = quench(X, whampot)            

        print "quenched energy", ret.energy
        
        global_min = False
        if global_min:
            from pygmin.basinhopping import BasinHopping
            from pygmin.takestep.displace import RandomDisplacement
            takestep = RandomDisplacement(stepsize=10.)
            takestep.useAdaptiveStep()
            takestep.adaptive_class.f = 2.
            bh = BasinHopping(X, whampot, takestep)
            bh.run(1000)
        
        
        #self.logn_Eq = zeros([nebins,nqbins], float64)
        X = ret.coords
        self.logn_Eq = X[nreps:]
        self.w_i_final = X[:nreps]
        
        self.logn_Eq = np.reshape(self.logn_Eq, [nebins, nqbins])
        self.logn_Eq = np.where( self.visits2d.sum(0) == 0, self.LOGMIN, self.logn_Eq )
示例#2
0
 def globalMinimization(self):
     """
     in experimentation i've never been able to find more than
     one minimum
     """
     nreps = self.nrep
     nbins = self.nebins
     visitsT = (self.visits1d)
     #print "min vis", np.min(visitsT)
     self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
     #print "minlogp", np.min(self.logP)
     self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
     
     self.whampot = WhamPotential(self.logP, self.reduced_energy)
     
     X = np.random.rand( nreps + nbins )
     E = self.whampot.getEnergy(X)
     print "energy", E 
     
     print "quenching"
     from pygmin.optimize import lbfgs_scipy as quench
     ret = quench(X, self.whampot)
     print "quench energy", ret.energy
     
     from pygmin.basinhopping import BasinHopping
     from pygmin.takestep.displace import RandomDisplacement
     takestep = RandomDisplacement(stepsize=10)
     takestep.useAdaptiveStep()
     takestep.adaptive_class.f = 1.5 #i have no idea what a good stepsize should be
     bh = BasinHopping(X, self.whampot, takestep )
     
     import matplotlib.pyplot as plt
     for i in range(10):
         bh.run(2000)
         self.logn_E = bh.coords[nreps:]
         cvdata = self.calc_Cv(400)
         plt.plot(cvdata[:,0], cvdata[:,5], '-')
     plt.show()
         
         
     
     X = bh.coords
     self.logn_E = X[nreps:]
     self.w_i_final = X[:nreps]
示例#3
0
def test_basin_hopping(pot, angles):
    from pygmin.basinhopping import BasinHopping
    from pygmin.takestep.displace import RandomDisplacement
    from pygmin.takestep.adaptive import AdaptiveStepsize

    takestep = RandomDisplacement(stepsize=np.pi / 4)
    takestepa = AdaptiveStepsize(takestep, frequency=20)

    bh = BasinHopping(angles, pot, takestepa, temperature=1.01)
    bh.run(20)
示例#4
0
    def globalMinimization(self):
        """
        in experimentation i've never been able to find more than
        one minimum
        """
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        print "energy", E

        print "quenching"
        from pygmin.optimize import lbfgs_scipy as quench
        ret = quench(X, self.whampot)
        print "quench energy", ret.energy

        from pygmin.basinhopping import BasinHopping
        from pygmin.takestep.displace import RandomDisplacement
        takestep = RandomDisplacement(stepsize=10)
        takestep.useAdaptiveStep()
        takestep.adaptive_class.f = 1.5  #i have no idea what a good stepsize should be
        bh = BasinHopping(X, self.whampot, takestep)

        import matplotlib.pyplot as plt
        for i in range(10):
            bh.run(2000)
            self.logn_E = bh.coords[nreps:]
            cvdata = self.calc_Cv(400)
            plt.plot(cvdata[:, 0], cvdata[:, 5], '-')
        plt.show()

        X = bh.coords
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]
示例#5
0
def getInitialCoords(natoms, pot):
    from pygmin.basinhopping import BasinHopping
    from pygmin.takestep.displace import RandomDisplacement
    takestep = RandomDisplacement(0.3)
    X = np.random.uniform(-1, 1, natoms * 3) * (1. * natoms)**(1. / 3) * .1
    bh = BasinHopping(X, pot, takestep)
    bh.run(30)

    X = bh.coords
    return X
示例#6
0
def getSetOfMinLJ(natoms = 11): #for testing purposes
    from pygmin.potentials.lj import LJ
    pot = LJ()
    coords = np.random.uniform(-1,1,natoms*3)
    from pygmin.basinhopping import BasinHopping
    from pygmin.takestep.displace import RandomDisplacement
    from pygmin.takestep.adaptive import AdaptiveStepsize
    from pygmin.storage.savenlowest import SaveN
    saveit = SaveN(10)
    takestep1 = RandomDisplacement()
    takestep = AdaptiveStepsize(takestep1, frequency=15)
    bh = BasinHopping(coords, pot, takestep, storage=saveit, outstream=None)
    bh.run(100)
    return pot, saveit
def guesstsLJ():
    from pygmin.potentials.lj import LJ
    pot = LJ()
    natoms = 9
    coords = np.random.uniform(-1, 1, natoms * 3)
    from pygmin.basinhopping import BasinHopping
    from pygmin.takestep.displace import RandomDisplacement
    from pygmin.takestep.adaptive import AdaptiveStepsize
    from pygmin.storage.savenlowest import SaveN
    saveit = SaveN(10)
    takestep1 = RandomDisplacement()
    takestep = AdaptiveStepsize(takestep1, frequency=15)
    bh = BasinHopping(coords, pot, takestep, storage=saveit, outstream=None)
    bh.run(100)
    coords1 = saveit.data[0].coords
    coords2 = saveit.data[1].coords

    return guessts(coords1, coords2, pot)
示例#8
0
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)
takestepa = AdaptiveStepsize(takestep, frequency=10)
storage = savenlowest.SaveN(20)

bh = BasinHopping(coords, pot, takestepa, temperature=1.01, storage=storage)
bh.run(200)

print "lowest structures fount:"
with open("out.spins", "w") as fout:
    for min in storage.data:
        m = getm(min.coords)
        print "energy", min.energy, "magnetization", m
        fout.write("energy %g magnetization %g\n" % (min.energy, m))
        printspins(fout, pot, min.coords)
        fout.write("\n\n")
"""