def test1(self): system = LJCluster(6) ss0 = 1. displace = RandomDisplacement(stepsize=ss0) ts = AdaptiveStepsize(displace, interval=10) bh = system.get_basinhopping(takestep=ts) bh.run(9) self.assertAlmostEqual(displace.stepsize, ss0, 10) bh.run(2) self.assertNotAlmostEqual(displace.stepsize, ss0, 1)
def initialize_system(): system = LJCluster(natoms) x0 = np.random.random(natoms * 3) #.reshape(natoms,3) db = system.create_database() step = RandomDisplacement(stepsize=1.0) bh = system.get_basinhopping(database=db, takestep=step, coords=x0, temperature=10.0) return system, db, bh
def get_takestep(self, **kwargs): """return the takestep object for use in basinhopping, etc. """ d = dict(self.params.takestep) d.update(kwargs) kwargs = d try: stepsize = kwargs.pop("stepsize") except KeyError: stepsize = 0.6 takeStep = RandomDisplacement(stepsize=stepsize) return takeStep
def build_lj_nested_sampling(system, db): from pele.takestep import RandomDisplacement nreplicas = 20 mciter = 10000 nminima = 10000 minima = db.minima() if len(minima) > nminima: minima = minima[:nminima] takestep = RandomDisplacement(stepsize=0.5) accept_tests = system.get_config_tests() ns = NestedSamplingBS(system, nreplicas, takestep, minima, mciter=mciter, accept_tests=accept_tests) return ns
def bh_no_system_class(): import numpy as np from pele.potentials import LJ natoms = 17 potential = LJ() x0 = np.random.uniform(-1, 1, 3*natoms) from pele.takestep import RandomDisplacement, AdaptiveStepsizeTemperature displace = RandomDisplacement() adaptive_displacement = AdaptiveStepsizeTemperature(displace) from pele.storage import Database database = Database("lj17.sqlite") from pele.basinhopping import BasinHopping bh = BasinHopping(x0, potential, adaptive_displacement, storage=database.minimum_adder) bh.run(10) for m in database.minima(): print m.energy
def get_takestep(self, **kwargs): """return the takestep object for use in basinhopping, etc. Notes ----- default is random displacement with adaptive step size and adaptive temperature See Also -------- pele.takestep """ kwargs = dict_copy_update(self.params["takestep"], kwargs) try: stepsize = kwargs.pop("stepsize") except KeyError: stepsize = 0.6 takeStep = RandomDisplacement(stepsize=stepsize) tsAdaptive = AdaptiveStepsizeTemperature(takeStep, **kwargs) return tsAdaptive
def get_takestep(self, **kwargs): """return the takestep object for use in basinhopping, etc. default is random displacement with adaptive step size adaptive temperature See Also -------- pele.takestep """ if self.phi_disorder > 0.01: return super(XYModlelSystem, self).get_takestep(**kwargs) # if no disorder, turn off adaptive step and temperature. from pele.takestep import RandomDisplacement kwargs = dict(self.params["takestep"].items() + kwargs.items()) try: stepsize = kwargs.pop("stepsize") except KeyError: stepsize = 2. * np.pi takeStep = RandomDisplacement(stepsize=stepsize) return takeStep
def run_basinhopping(ts, system, db, x): if x == None: x = 0.1 * np.random.random() + 0.003 E0 = -44.5 step = RandomDisplacement(stepsize=x) bh = system.get_basinhopping(database=db, takestep=step, coords=x0, temperature=1.0) t = time.time() while True: bh.run(1) eps = np.abs(bh.trial_energy / E0 - 1.) print "\n\n", bh.trial_energy, bh.takeStep.stepsize, "\n\n" if eps < 0.02: print "Finished\n\n", bh.trial_energy, bh.takeStep.stepsize print bh.trial_energy break dt = time.time() - t #ts.points = (ssize,dt) ts.points = np.concatenate((ts.points, [[x, dt]]))
def test_BH(self, db, nsteps): self.potential = self.get_potential() from pele.takestep import RandomDisplacement, AdaptiveStepsizeTemperature takeStepRnd = RandomDisplacement(stepsize=2) tsAdaptive = AdaptiveStepsizeTemperature(takeStepRnd, interval=10, verbose=True) self.params.basinhopping["temperature"] = 10.0 # todo - how do you save N lowest? bh = self.get_basinhopping(database=db, takestep=takeStepRnd) bh = self.get_basinhopping(database=db, takestep=tsAdaptive) print 'Running BH .. ' bh.run(nsteps) print "Number of minima found = ", len(db.minima()) min0 = db.minima()[0] print "lowest minimum found has energy = ", min0.energy
def __init__(self, pot, T=10., nsteps=100, stepsize=0.1): self.potential = pot self.T = T self.nsteps = nsteps self.mcstep = RandomDisplacement(stepsize=stepsize)
""" 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 __future__ import print_function from pele.systems import LJCluster from pele.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("")
def runptmc(nsteps_tot=100000): natoms = 31 nreplicas = 4 Tmin = 0.2 Tmax = 0.4 nsteps_equil = 10000 nsteps_tot = 100000 histiprint = nsteps_tot / 10 exchange_frq = 100 * nreplicas coords = np.random.random(3 * natoms) #quench the coords so we start from a reasonable location mypot = lj.LJ() ret = quench(coords, mypot) coords = ret.coords Tlist = getTemps(Tmin, Tmax, nreplicas) replicas = [] ostreams = [] histograms = [] takesteplist = [] radius = 2.5 # create all the replicas which will be passed to PTMC for i in range(nreplicas): T = Tlist[i] potential = lj.LJ() takestep = RandomDisplacement(stepsize=0.01) adaptive = AdaptiveStepsize(takestep, last_step=nsteps_equil) takesteplist.append(adaptive) file = "mcout." + str(i + 1) ostream = open(file, "w") hist = EnergyHistogram(-134., 10., 1000) histograms.append(hist) event_after_step = [hist] radiustest = SphericalContainer(radius) accept_tests = [radiustest] mc = MonteCarlo(coords, potential, takeStep=takestep, temperature=T, \ outstream=ostream, event_after_step = event_after_step, \ confCheck = accept_tests) mc.histogram = hist #for convienence mc.printfrq = 1 replicas.append(mc) #is it possible to pickle a mc object? #cp = copy.deepcopy(replicas[0]) #import pickle #with open("mc.pickle", "w") as fout: #pickle.dump(takesteplist[0], fout) #attach an event to print xyz coords from pele.printing.print_atoms_xyz import PrintEvent printxyzlist = [] for n, rep in enumerate(replicas): outf = "dumpstruct.%d.xyz" % (n + 1) printxyz = PrintEvent(outf, frq=500) printxyzlist.append(printxyz) rep.addEventAfterStep(printxyz) #attach an event to print histograms for n, rep in enumerate(replicas): outf = "hist.%d" % (n + 1) histprint = PrintHistogram(outf, rep.histogram, histiprint) rep.addEventAfterStep(histprint) ptmc = PTMC(replicas) ptmc.use_independent_exchange = True ptmc.exchange_frq = exchange_frq ptmc.run(nsteps_tot) #do production run #fix the step sizes #for takestep in takesteplist: # takestep.useFixedStep() #ptmc.run(30000) if False: #this doesn't work print "final energies" for rep in ptmc.replicas: print rep.temperature, rep.markovE for rep in ptmc.replicas_par: print rep.mcsys.markovE for k in range(nreplicas): e, T = ptmc.getRepEnergyT(k) print T, e if False: #this doesn't work print "histograms" for i, hist in enumerate(histograms): fname = "hist." + str(i) print fname with open(fname, "w") as fout: for (e, visits) in hist: fout.write("%g %d\n" % (e, visits)) ptmc.end() #close the open threads
def takeStep(self, coords, **kwargs): self.count += 1 RandomDisplacement.takeStep(self, coords, **kwargs)
def run_nested_sampling_lj(system, nreplicas=300, mciter=1000, target_ratio=0.7, label="test", minima=None, use_compiled=True, nproc=1, triv_paral=True, minprob=1, maxiter=1e100, **kwargs): takestep = RandomDisplacement(stepsize=0.07) accept_tests = system.get_config_tests() if type(system) is LJClusterNew: if use_compiled: mc_runner = MonteCarloCompiled(system.radius) # import pickle # with open("testpickle", "w") as pout: # pickle.dump(mc_runner, pout) else: mc_runner = MonteCarloChain(system.get_potential(), takestep, accept_tests=accept_tests) print "using the compiled MC = ", use_compiled elif type(system) is HarParticle: mc_runner = HarRunner(system) print "using HarRunner" elif type(system) is IsingSystem: mc_runner = IsingRunnerC(system) print "using IsingRunner" else: raise TypeError('system type is not known') print "using", nproc, "processors" if minima is not None: assert (len(minima) > 0) print "using", len(minima), "minima" ns = NestedSamplingBS(system, nreplicas, mc_runner, minima, mciter=mciter, target_ratio=target_ratio, stepsize=0.07, nproc=nproc, triv_paral=triv_paral, minprob=minprob, **kwargs) else: ns = NestedSampling(system, nreplicas, mc_runner, mciter=mciter, target_ratio=target_ratio, stepsize=0.07, nproc=nproc, triv_paral=triv_paral, **kwargs) etol = 0.01 ns = run_nested_sampling(ns, label, etol, maxiter=maxiter) return ns
import numpy as np from src.runmc import mc_cython from lj_run import LJClusterNew, MonteCarloCompiled from nested_sampling import MonteCarloChain from pele.takestep import RandomDisplacement from pele.utils.xyz import write_xyz system = LJClusterNew(31) x = system.get_random_configuration() with open("test.xyz", "w") as fout: write_xyz(fout, x) mciter = 100000 stepsize = .01 Emax = 1e20 radius = 2.5 mcc = MonteCarloCompiled(system, radius) mcc(x.copy(), mciter, stepsize, Emax) print mcc.naccept, mcc.nsteps, mcc.energy takestep = RandomDisplacement(stepsize=stepsize) mc = MonteCarloChain(system.get_potential(), x.copy(), takestep, Emax, system.get_config_tests()) for i in xrange(mciter): mc.step() print mc.naccept, mc.nsteps, mc.energy
def takeStep(self, coords, **kwargs): self.count += 1 RandomDisplacement.takeStep(self, coords, **kwargs)
def get_takestep(self, **kwargs): """return the takestep object for use in basinhopping, etc.""" return RandomDisplacement(stepsize=self.scale)