def setUp(self):
     natoms = 10
     minimiser = PeleMinimiser(lj.LJ())
     factory = ClusterFactory(natoms, minimiser)
     self.population = PopulationList(natoms, factory, max_size=5)
     while len(self.population) < self.population.max_size:
         self.population.append(factory.get_random_cluster())
Ejemplo n.º 2
0
def test():  # pragma: no cover
    from _orthogopt import orthogopt_slow, orthogopt

    natoms = 105
    for i in xrange(1):
        x = np.random.random(3 * natoms) * 5
        xx = x.reshape(-1, 3)
        com = xx.sum(0) / xx.shape[0]
        xx -= com
        v = np.random.random(3 * natoms)
        test1 = orthogopt_slow(v.copy(), x.copy())
        ozev = gramm_schmidt(zeroEV_cluster(x))

        orthogonalize(v, ozev)

        print np.linalg.norm(v - test1)
    exit()

    from pele.potentials import lj

    pot = lj.LJ()
    x = np.array([-1., 0., 0., 1., 0., 0., 0., 1., 1., 0., -1., -1.])
    x = np.random.random(x.shape)
    print x
    v = zeroEV_cluster(x)
    print np.dot(v[0], v[1]), np.dot(v[0], v[2]), np.dot(v[1], v[2])
    print np.dot(v[3], v[4]), np.dot(v[3], v[5]), np.dot(v[5], v[4])
    u = gramm_schmidt(zeroEV_cluster(x))
    for i in u:
        print (pot.getEnergy(x + 1e-4 * i) - pot.getEnergy(x)) / 1e-4, i
    print np.dot(u[3], u[4]), np.dot(u[3], u[5]), np.dot(u[5], u[4])
    print "########################"

    r = np.random.random(x.shape)
    print orthogopt(r.copy(), x.copy()) - orthogonalize(r.copy(), u)
Ejemplo n.º 3
0
 def test_replace(self):
     natoms=10
     minimiser=PeleMinimiser(lj.LJ())
     factory=ClusterFactory(natoms,minimiser)
     parent = factory.get_random_cluster()   
     mutator = MutateReplace()
     mutant = mutator.get_mutant(parent)
     self.assertLess(mutant.get_energy(), 0)
Ejemplo n.º 4
0
 def test_mutant(self):
     natoms = 10
     minimiser = PeleMinimiser(lj.LJ())
     factory = ClusterFactory(natoms, minimiser)
     parent_a = factory.get_random_cluster()
     parent_b = factory.get_random_cluster()
     crossover = DeavenHo()
     offspring = crossover.get_offspring(parent_a, parent_b)
     self.assertLess(offspring.get_energy(), 0)
Ejemplo n.º 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[:]

    def updateStep(self, acc, **kwargs):
        pass


natoms = 12

# random initial coordinates
coords = np.random.random(3 * natoms)
potential = lj.LJ()

step = TakeStepMonteCarlo(potential)

opt = bh.BasinHopping(coords, potential, takeStep=step)
opt.run(100)

# some visualization
try:
    import pele.utils.pymolwrapper as pym

    pym.start()
    pym.draw_spheres(opt.coords, "A", 1)
except:
    print("Could not draw using pymol, skipping this step")
Ejemplo n.º 6
0
############################################################
# Just quench 
############################################################
import numpy as np

import pele.potentials.lj as lj

natoms = 12

# random initial coordinates
coords=np.random.random(3*natoms)
pot = lj.LJ()

print pot.getEnergy(coords) 

a,b = pot.getEnergyGradient(coords) 
print type(a) 

from pele.optimize import lbfgs_scipy, cg  fire

# lbfgs 
ret = lbfgs_scipy( coords, pot, iprint=-1 , tol = 1e-3, nsteps=100) 
     # core dump! 

# cg  
#    ret = cg( coordsVec, pot.getEnergyGradient) 
    # runtime error -- ValueError: The truth value of an array with more than ...
    
# fire   
#ret = fire( coords, pot.getEnergyGradient, tol = 1e-3, nsteps=20000) 
# ValueError: The truth value of an array with more than ...
Ejemplo n.º 7
0
 def setUp(self):
     self.natoms = 3
     minimiser = PeleMinimiser(lj.LJ())
     coords = np.array(((0., 0., 0.1), (0.1, 0.1, 0.1), (0., 0.2, 0.2)))
     self.cluster = Cluster(self.natoms, coords, minimiser)
Ejemplo n.º 8
0
 def setUp(self):
     natoms = 3
     self.minimiser = PeleMinimiser(lj.LJ())
     coords = np.array(((0., 0., 0.1), (1., 1., 0.3), (0., 2., 0.2)))
     self.cluster = Cluster(natoms, coords, self.minimiser)
Ejemplo n.º 9
0
'''
A simple GA run. We use a 38 atom Lennard-Jones in this example.

The natoms argument defines the number of atoms in the cluster and is required for all GA searchs.
The minimiser object is also needed to perform the energy evaluations. This example uses a simple
Lennard-Jones minimiser. Other examples show how to set up minimisers for more complicated potentials.

@author: Mark Oakley
'''

from bcga.genetic_algorithm import GeneticAlgorithm
import pele.potentials.lj as lj
from bcga.pele_interface import PeleMinimiser

myga = GeneticAlgorithm(
    natoms=38,  # Number of atoms
    minimiser=PeleMinimiser(lj.LJ()))  #Energy minimisation method
myga.run()
Ejemplo n.º 10
0
 def setUp(self):
     natoms = 10
     minimiser = PeleMinimiser(lj.LJ())
     self.ga = GeneticAlgorithm(natoms, minimiser, max_generation=2)
 def setUp(self):
     natoms = 10
     minimiser = PeleMinimiser(lj.LJ())
     self.factory = ClusterFactory(natoms, minimiser)
Ejemplo n.º 12
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 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
Ejemplo n.º 13
0
'''
The batch GA is designed to be used in conjunction with a task arrays on a
scheduling system. The population of structures is stored in an SQL database
that can be accessed by several instances of the GA at the same time.

For this example, we use a simple Lennard-Jones potential. However, the batch
GA is designed to be most useful for very expensive energy calculations (e.g.
DFT).

Use the read_database script to view the database in a human-readable format.

@author: Mark Oakley
'''

from bcga.batch_genetic_algorithm import BatchGeneticAlgorithm
from bcga.pele_interface import PeleMinimiser
import pele.potentials.lj as lj

natoms = 38
minimiser=PeleMinimiser(lj.LJ())

myga = BatchGeneticAlgorithm(natoms,
                        minimiser,
                        remove_duplicates=True,
                        max_generation=20)

myga.run()