示例#1
0
 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[:]
示例#2
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()
示例#3
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()
示例#4
0
    def __init__(self, coords, potential, takeStep, storage=None, event_after_step=[], \
            acceptTest=None,  \
            temperature=1.0, \
            quenchRoutine = defaults.quenchRoutine, \
            quenchParameters = defaults.quenchParams, \
            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)

        self.quenchRoutine = quenchRoutine
        self.quenchParameters = quenchParameters
        
        #########################################################################
        #do initial quench
        #########################################################################
        self.markovE_old = self.markovE
        res = \
            self.quenchRoutine(self.coords, self.potential.getEnergyGradient, **self.quenchParameters)
        newcoords, Equench, self.rms, self.funcalls = res[:4]
        self.coords = newcoords
        self.markovE = Equench

        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()
示例#5
0
 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[:]
示例#6
0
#   c = np.reshape()

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 pygmin.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 pygmin.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

示例#8
0
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.getEnergyGradient)
    coords = ret[0]
    
    
    Tlist = getTemps(Tmin, Tmax, nreplicas)
    replicas = []
    ostreams = []
    histograms = []
    takesteplist = []
    radius = 2.5
    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 pygmin.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
示例#9
0
文件: LJ_MC.py 项目: wwwtyro/PyGMIN
natoms = 40

coords = np.random.rand(natoms*3)


lj = LJ() 

ret = quench(coords, lj.getEnergyGradient)
coords = ret[0]

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)
示例#10
0
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 pygmin.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