def test(): from pygmin.systems import LJCluster natoms = 13 system = LJCluster(natoms) db = system.create_database() # get some minima bh = system.get_basinhopping(database=db, outstream=None) bh.run(100) manager = ConnectManager(db, clust_min=2) for i in range(4): min1, min2 = manager.get_connect_job(strategy="random") print "connecting", min1._id, min2._id connect = system.get_double_ended_connect(min1, min2, db, verbosity=0) connect.connect() print "\n\ntesting untrap" for i in range(10): min1, min2 = manager.get_connect_job(strategy="untrap") print min1._id, min2._id print "\n\ntesting combine" for i in range(5): min1, min2 = manager.get_connect_job(strategy="combine") print min1._id, min2._id print "\n\ntesting gmin" for i in range(5): min1, min2 = manager.get_connect_job(strategy="gmin") print min1._id, min2._id
class TestBasinhopping(unittest.TestCase): def setUp(self): natoms = 6 self.system = LJCluster(natoms) self.e1 = -12.7120622568 self.e2 = -12.302927529580728 def assertEnergy(self, e): self.assertTrue( abs(e-self.e1) < 1e-4 or abs(e-self.e2) < 1e-4) def test_create_basinhopping(self): bh = self.system.get_basinhopping(outstream=None) bh.run(3) self.assertEnergy(bh.result.energy) self.assertEnergy(bh.markovE) def test_takestep(self): takestep = self.system.get_takestep(stepsize=1.) bh = self.system.get_basinhopping(outstream=None, takestep=takestep) bh.run(3) self.assertEnergy(bh.result.energy) self.assertEnergy(bh.markovE)
def get_database(natoms=13, nconn=5): """create a database for a lennard jones system fill it with minima from a basinhopping run, then connect some of those minima using DoubleEndedConnect """ ljsys = LJCluster(natoms) db = ljsys.create_database() bh = ljsys.get_basinhopping(database=db, outstream=None) while (len(db.minima()) < nconn + 1): bh.run(100) minima = list(db.minima()) m1 = minima[0] for m2 in minima[1:nconn + 1]: connect = ljsys.get_double_ended_connect(m1, m2, db) connect.connect() return db
def get_database(natoms=13, nconn=5): """create a database for a lennard jones system fill it with minima from a basinhopping run, then connect some of those minima using DoubleEndedConnect """ ljsys = LJCluster(natoms) db = ljsys.create_database() bh = ljsys.get_basinhopping(database=db, outstream=None) while len(db.minima()) < nconn + 1: bh.run(100) minima = list(db.minima()) m1 = minima[0] for m2 in minima[1 : nconn + 1]: connect = ljsys.get_double_ended_connect(m1, m2, db) connect.connect() return db
""" Example 2: reading coords from file """ import numpy as np from pygmin.systems import LJCluster natoms = 12 niter = 100 system = LJCluster(natoms) coords = np.loadtxt('coords') coords = coords.reshape(-1) db = system.create_database() bh = system.get_basinhopping(database=db) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima()[0].energy print ""
from pygmin.systems import LJCluster import numpy as np import scipy.sparse import scikits.sparse.cholmod as cholmod import time import pygmin.transition_states as ts from pygmin.utils.hessian import get_sorted_eig natoms = 1000 system = LJCluster(natoms) #system.params.structural_quench_params.debug = True #system.params.structural_quench_params.iprint = 100 db = system.create_database() bh = system.get_basinhopping(db) bh.run(1) m = db.minima()[0] coords = m.coords potential = system.get_potential() energy, gradient, hessian = potential.getEnergyGradientHessian(coords) dummy_vec = ts.gramm_schmidt(ts.zeroEV_cluster(coords)) shifted_hess = hessian.copy() for i in range(6): shifted_hess += np.outer(dummy_vec[i], dummy_vec[i]) shifted_eval, shifted_evec = get_sorted_eig(shifted_hess)
def takeStep(self, coords, **kwargs): #make a new monte carlo class mc = MonteCarlo(coords, self.potential, self.mcstep, temperature=self.T, outstream=None) mc.run(self.nsteps) coords[:] = mc.coords[:] def updateStep(self, acc, **kwargs): pass natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() # create takestep routine manually potential = system.get_potential() step = TakeStepMonteCarlo(potential) bh = system.get_basinhopping(database=db, takestep=step) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima( )[0].energy print ""
# create a database to store the minimum in db = system.create_database() # note: this creates a database in memory, if you want to save the results # you would use # db = system.create_database("lj.sqlite") minimum1 = db.addMinimum(newenergy, newcoords) # get a second random minimized configuration and add it to the database ret = system.get_random_minimized_configuration() print "a second minimum has energy", ret[1] e2, coords2 = ret[1], ret[0] minimum2 = db.addMinimum(e2, coords2) # do a basinhopping run to find the global minimum and build up the database of minima bh = system.get_basinhopping(database=db, outstream=None) niter = 20 bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima( )[0].energy # print the energies of all the minima found print "the minima in the database have energies" for minimum in db.minima(): print " ", minimum.energy # find the minimum distance (a.k.a. mindist) between the two lowest minima m1, m2 = db.minima()[:2] mindist = system.get_mindist() dist, coords1, coords2 = mindist(m1.coords, m2.coords) print "the minimum distance between the two lowest minima is", dist
from pygmin.systems import LJCluster from pygmin.utils.disconnectivity_graph import DisconnectivityGraph, database2graph natoms = 13 system = LJCluster(natoms) use_existing_database = False if use_existing_database: db = system.create_database("lj13.sqlite", createdb=False) else: # build a small database using basinhopping print "building a small database using basinhopping" "" db = system.create_database() bh = system.get_basinhopping(database=db, outstream=None) bh.run(20) print "starting with a database of", len(db.minima()), "minima" # turn of status printing for the connect run # first use the logging module to turn off the status messages logger = logging.getLogger("pygmin.connect") logger.setLevel("WARNING") # connect all minima to the lowest minimum print "now connecting all the minima to the lowest energy minimum" m1 = db.minima()[0] for m2 in db.minima()[1:]: print " connecting minima with id's", m1._id, m2._id
else: faccept = float(ndiff_accept) / ndiff if faccept > self.target_new_min_accept_prob: driver.acceptTest.temperature *= self.Tfactor else: driver.acceptTest.temperature /= self.Tfactor if self.verbose: print " temperature is now %.4g ratio %.4g target %.4g" % (driver.acceptTest.temperature, faccept, self.target_new_min_accept_prob) if __name__ == "__main__": import numpy as np from pygmin.takestep import displace from pygmin.systems import LJCluster #from pygmin.takestep import adaptive natoms = 38 sys = LJCluster(natoms=38) # random initial coordinates coords = sys.get_random_configuration() takeStep = displace.RandomDisplacement( stepsize=0.4 ) tsAdaptive = AdaptiveStepsizeTemperature(takeStep, interval=300, verbose=True) db = sys.create_database() opt = sys.get_basinhopping(database=db, takestep=tsAdaptive, coords=coords) opt.printfrq = 50 opt.run(5000)
""" Example 4: modify the parameters of the adaptive stepsize note that the system class uses adaptive stepsize by default, so the previous examples also use adaptive stepsize """ from pygmin.systems import LJCluster from pygmin.takestep import RandomDisplacement, AdaptiveStepsizeTemperature natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() # create takestep routine manually step = RandomDisplacement(stepsize=0.3) # wrap it in the class which will adaptively improve the stepsize wrapped_step = AdaptiveStepsizeTemperature(step, interval=30) bh = system.get_basinhopping(database=db, takestep=wrapped_step) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima( )[0].energy print ""
from OpenGL.GLUT import glutInit import sys import pylab as pl app = QtGui.QApplication(sys.argv) from pygmin.systems import LJCluster pl.ion() natoms = 13 system = LJCluster(natoms) system.params.double_ended_connect.local_connect_params.NEBparams.iter_density = 5. dbname = "lj%dtest.db" % (natoms,) db = system.create_database(dbname) #get some minima if False: bh = system.get_basinhopping(database=db) bh.run(10) minima = db.minima() else: x1, e1 = system.get_random_minimized_configuration()[:2] x2, e2 = system.get_random_minimized_configuration()[:2] min1 = db.addMinimum(e1, x1) min2 = db.addMinimum(e2, x2) minima = [min1, min2] # connect some of the minima nmax = min(3, len(minima)) m1 = minima[0] for m2 in minima[1:nmax]: connect = system.get_double_ended_connect(m1, m2, db)
self.nsteps = nsteps self.mcstep = RandomDisplacement(stepsize=stepsize) def takeStep(self, coords, **kwargs): #make a new monte carlo class mc = MonteCarlo(coords, self.potential, self.mcstep, temperature = self.T, outstream=None) mc.run( self.nsteps ) coords[:] = mc.coords[:] def updateStep(self, acc, **kwargs): pass natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() # create takestep routine manually potential = system.get_potential() step = TakeStepMonteCarlo(potential) bh = system.get_basinhopping(database=db, takestep=step) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima()[0].energy print ""
""" Example 4: modify the parameters of the adaptive stepsize note that the system class uses adaptive stepsize by default, so the previous examples also use adaptive stepsize """ from pygmin.systems import LJCluster from pygmin.takestep import RandomDisplacement, AdaptiveStepsizeTemperature natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() # create takestep routine manually step = RandomDisplacement(stepsize=0.3) # wrap it in the class which will adaptively improve the stepsize wrapped_step = AdaptiveStepsizeTemperature(step, interval=30) bh = system.get_basinhopping(database=db, takestep=wrapped_step) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima()[0].energy print ""