def get_offspring(self, cluster_a, cluster_b): '''Generate an offspring structure from two parent structures.''' #Prepare clusters for cluster in [cluster_a, cluster_b]: cluster.centre() cluster.rotate_random() cluster.sort_z() #Choose cutting plane cut = np.random.randint(1, cluster_a.natoms) #Make new cluster coords = np.empty(shape=(cluster_a.natoms, 3)) atom_types = [] for i in range(0, cut): coords[i] = cluster_a.get_coords(i) atom_types.append(cluster_a.atom_types[i]) for i in range(cut, cluster_a.natoms): coords[i] = cluster_b.get_coords(i) atom_types.append(cluster_b.atom_types[i]) composition = get_composition(cluster_a.atom_types) atom_types = fix_composition(composition, atom_types) offspring = Cluster(cluster_a.natoms, coords, cluster_a.minimiser, atom_types=atom_types, labels=cluster_a.labels) offspring.quenched = False offspring.sort_type() cluster_a.sort_type() cluster_b.sort_type() return offspring
def get_offspring(self,cluster_a,cluster_b): '''Generate an offspring structure from two parent structures.''' #Prepare clusters for cluster in [cluster_a,cluster_b]: cluster.centre() cluster.rotate_random() cluster.sort_z() #Choose cutting plane cut=np.random.randint(1,cluster_a.natoms) #Make new cluster coords=np.empty(shape=(cluster_a.natoms,3)) atom_types=[] for i in range(0,cut): coords[i]=cluster_a.get_coords(i) atom_types.append(cluster_a.atom_types[i]) for i in range(cut,cluster_a.natoms): coords[i]=cluster_b.get_coords(i) atom_types.append(cluster_b.atom_types[i]) composition = get_composition(cluster_a.atom_types) atom_types=fix_composition(composition,atom_types) offspring=Cluster(cluster_a.natoms, coords, cluster_a.minimiser, atom_types=atom_types, labels=cluster_a.labels) offspring.quenched=False offspring.sort_type() cluster_a.sort_type() cluster_b.sort_type() return offspring
def read_db(self,minimum): '''Read structure from Pele minimum''' cluster=Cluster(self.natoms, np.reshape(minimum.coords,(-1,3)), self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.energy=minimum.energy cluster.quenched=True return cluster
def get_random_cluster(self): '''Return a cluster with random coordinates''' coords=(np.random.rand(self.natoms,3) -0.5) * 1.4 * float(self.natoms)**(1./3.) cluster = Cluster(self.natoms, coords, self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.quenched=False return cluster
def read_db(self, minimum): '''Read structure from Pele minimum''' cluster = Cluster(self.natoms, np.reshape(minimum.coords, (-1, 3)), self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.energy = minimum.energy cluster.quenched = True return cluster
class ClusterTest(unittest.TestCase): def setUp(self): self.natoms=3 minimiser=PeleMinimiser(lj.LJ()) coords=np.array(((0.,0.,0.1), (1.,1.,0.3), (0.,2.,0.2))) self.cluster = Cluster(self.natoms,coords,minimiser) def test_z_sort(self): self.cluster.sort_z() for i in range(1,self.natoms): self.assertGreater(self.cluster.get_coords(i)[2], self.cluster.get_coords(i-1)[2]) def test_get_energy(self): self.assertAlmostEquals(self.cluster.get_energy(),-3.) def test_centre(self): self.assertEquals(self.cluster.centre()[1],-1.) def test_get_atom(self): self.assertEquals(self.cluster.get_coords(0)[0],0.0) self.assertEquals(self.cluster.get_coords(0)[1],0.0) self.assertEquals(self.cluster.get_coords(0)[2],0.1) def test_get_label(self): self.assertEquals(self.cluster.get_label(2),"X")
class ClusterTest(unittest.TestCase): def setUp(self): self.natoms = 3 minimiser = PeleMinimiser(lj.LJ()) coords = np.array(((0., 0., 0.1), (1., 1., 0.3), (0., 2., 0.2))) self.cluster = Cluster(self.natoms, coords, minimiser) def test_z_sort(self): self.cluster.sort_z() for i in range(1, self.natoms): self.assertGreater( self.cluster.get_coords(i)[2], self.cluster.get_coords(i - 1)[2]) def test_get_energy(self): self.assertAlmostEquals(self.cluster.get_energy(), -3.) def test_centre(self): self.assertEquals(self.cluster.centre()[1], -1.) def test_get_atom(self): self.assertEquals(self.cluster.get_coords(0)[0], 0.0) self.assertEquals(self.cluster.get_coords(0)[1], 0.0) self.assertEquals(self.cluster.get_coords(0)[2], 0.1) def test_get_label(self): self.assertEquals(self.cluster.get_label(2), "X")
def setUp(self): self.natoms = 3 minimiser = PeleMinimiser(BLJCut(3, 1)) coords = np.array(((0., 0., 0.1), (1., 1., 0.3), (0., 2., 0.2))) types = [0, 1, 0] labels = ["X", "Y"] self.cluster = Cluster(self.natoms, coords, minimiser, atom_types=types, labels=labels)
def get_random_cluster(self): '''Return a cluster with random coordinates''' coords = (np.random.rand(self.natoms, 3) - 0.5) * 1.4 * float( self.natoms)**(1. / 3.) cluster = Cluster(self.natoms, coords, self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.quenched = False return cluster
class OverlapTest(unittest.TestCase): 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) def test_cutoff_default(self): self.cluster.fix_overlaps() self.assertGreaterEqual(min_dist(self.cluster._coords), 1.0) def test_cutoff_2(self): self.cluster.fix_overlaps(2.0) self.assertGreaterEqual(min_dist(self.cluster._coords), 2.0)
def get_mutant(self, cluster): cluster.sort_type() atom_types = list(cluster.atom_types) swap0 = np.random.randint(0, get_composition(atom_types)[0]) swap1 = np.random.randint( get_composition(atom_types)[0], cluster.natoms) atom_types[swap0] = 1 atom_types[swap1] = 0 mutant = Cluster(cluster.natoms, np.array(cluster._coords), cluster.minimiser, atom_types=atom_types, labels=cluster.labels) mutant.sort_type() return mutant
def read_xyz(self,xyz_file): '''Read structure from xyz file.''' natoms=int(xyz_file.readline()) coords=np.empty(shape=(natoms,3)) energy=float(xyz_file.readline().split()[1]) for i in range(0,natoms): coords[i]=xyz_file.readline().split()[1:4] cluster=Cluster(natoms, coords, self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.energy=energy cluster.quenched=True return cluster
def read_xyz(self, xyz_file): '''Read structure from xyz file.''' natoms = int(xyz_file.readline()) coords = np.empty(shape=(natoms, 3)) energy = float(xyz_file.readline().split()[1]) for i in range(0, natoms): coords[i] = xyz_file.readline().split()[1:4] cluster = Cluster(natoms, coords, self.minimiser, atom_types=get_atom_types(self.composition), labels=self.labels) cluster.energy = energy cluster.quenched = True return cluster
def get_mutant(self,cluster): cluster.sort_type() atom_types=list(cluster.atom_types) swap0=np.random.randint(0, get_composition(atom_types)[0]) swap1=np.random.randint(get_composition(atom_types)[0], cluster.natoms) atom_types[swap0]=1 atom_types[swap1]=0 mutant=Cluster(cluster.natoms, np.array(cluster._coords), cluster.minimiser, atom_types=atom_types, labels=cluster.labels) mutant.sort_type() return mutant
class OverlapTest(unittest.TestCase): 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) def test_cutoff_default(self): self.cluster.fix_overlaps() self.assertGreaterEqual(min_dist(self.cluster._coords), 1.0) def test_cutoff_2(self): self.cluster.fix_overlaps(2.0) self.assertGreaterEqual(min_dist(self.cluster._coords), 2.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)
def get_mutant(self, cluster): coords = (np.random.rand(cluster.natoms, 3) - 0.5) * 1.4 * float( cluster.natoms)**(1. / 3.) mutant = Cluster(cluster.natoms, coords, cluster.minimiser, atom_types=list(cluster.atom_types), labels=cluster.labels) return mutant
def setUp(self): self.natoms=3 minimiser=PeleMinimiser(BLJCut(3,1)) coords=np.array(((0.,0.,0.1), (1.,1.,0.3), (0.,2.,0.2))) types=[0,1,0] labels=["X","Y"] self.cluster = Cluster(self.natoms,coords,minimiser, atom_types=types,labels=labels)
class BinaryClusterTest(unittest.TestCase): def setUp(self): self.natoms=3 minimiser=PeleMinimiser(BLJCut(3,1)) coords=np.array(((0.,0.,0.1), (1.,1.,0.3), (0.,2.,0.2))) types=[0,1,0] labels=["X","Y"] self.cluster = Cluster(self.natoms,coords,minimiser, atom_types=types,labels=labels) def test_labels(self): self.assertEquals(self.cluster.get_label(0),"X") self.assertEquals(self.cluster.get_label(1),"Y") self.assertEquals(self.cluster.get_label(2),"X") def test_sort_labels(self): self.cluster.sort_z() self.assertEquals(self.cluster.get_label(0),"X") self.assertEquals(self.cluster.get_label(1),"X") self.assertEquals(self.cluster.get_label(2),"Y")
class BinaryClusterTest(unittest.TestCase): def setUp(self): self.natoms = 3 minimiser = PeleMinimiser(BLJCut(3, 1)) coords = np.array(((0., 0., 0.1), (1., 1., 0.3), (0., 2., 0.2))) types = [0, 1, 0] labels = ["X", "Y"] self.cluster = Cluster(self.natoms, coords, minimiser, atom_types=types, labels=labels) def test_labels(self): self.assertEquals(self.cluster.get_label(0), "X") self.assertEquals(self.cluster.get_label(1), "Y") self.assertEquals(self.cluster.get_label(2), "X") def test_sort_labels(self): self.cluster.sort_z() self.assertEquals(self.cluster.get_label(0), "X") self.assertEquals(self.cluster.get_label(1), "X") self.assertEquals(self.cluster.get_label(2), "Y")
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)
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)