def takeStep(self, coords, **kwargs): # ake 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 __init__(self, coords, potential, takeStep, storage=None, event_after_step=None, acceptTest=None, temperature=1.0, quench=None, confCheck=None, 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.result.nfev += res.nfev 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()
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()
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[:]
natoms = 40 coords = np.random.rand(natoms*3) lj = LJ() ret = quench(coords, lj) coords = ret.coords takestep = TakeStep(stepsize=0.1 ) #do equilibration steps, adjusting stepsize tsadapt = AdaptiveStepsize(takestep) mc = MonteCarlo(coords, lj, tsadapt, temperature = 0.5) equilout = open("equilibration","w") #mc.outstream = equilout mc.setPrinting(equilout, 1) mc.run(10000) #fix stepsize and do production run mc.takeStep = takestep mcout = open("mc.out", "w") mc.setPrinting(mcout, 10) #print coords from time to time xyzout = open("out.xyz", "w") printevent = PrintEvent(xyzout, 300) mc.addEventAfterStep(printevent.printwrapper) mc.run(10000)
#set up the step taking routine #Normal basin hopping takes each step from the quenched coords. This modified step taking routine takes a step from the #last accepted coords, not from the quenched coords from pele.take_step.random_displacement import takeStep takestep = takeStep(stepsize=.01) #pass a function which rejects the step if the system leaved the inital basin. import do_quenching dostuff = do_quenching.DoQuenching(pot, coords, quench=quench) accept_test_list = [dostuff.acceptReject] #set up basin hopping from pele.mc import MonteCarlo temperature = 1.0 event_after_step = [] mc = MonteCarlo(coords, pot, takestep, \ event_after_step = event_after_step, \ acceptTests = accept_test_list, temperature = temperature) #run basin hopping mc.run(200) print mc.naccepted, "steps accepted out of", mc.stepnum print "quench: ", dostuff.nrejected, "steps rejected out of", dostuff.ntot
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 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