def setUpClass(cls): cls.bcc = CrystalStructure("Fe", bravais_basis="bcc", lattice_constants=[2.7]) cls.fcc = CrystalStructure("Fe", bravais_basis="fcc", lattice_constants=[3.6])
def test_get_distances_arbitrary_array(self): basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(3) neigh = basis.get_neighbors(cutoff_radius=3.5, num_neighbors=None) self.assertEqual( len(neigh.get_indices(np.random.random(3), num_neighbors=12)), 12) self.assertEqual( len( neigh.get_distances(np.random.random(3), num_neighbors=12, allow_ragged=True)), 12) self.assertLessEqual( len( neigh.get_vectors(np.random.random(3), num_neighbors=12, allow_ragged=True, cutoff_radius=3.5)), 12) self.assertTrue( neigh.get_vectors(np.random.random(( 2, 3)), num_neighbors=12).shape == (2, 12, 3)) neigh = basis.get_neighbors(num_neighbors=50) self.assertTrue( neigh.get_distances(np.random.random(3)).shape == (50, )) self.assertTrue( neigh.get_indices(np.random.random((2, 3))).shape == (2, 50)) self.assertTrue( neigh.get_vectors(np.random.random((2, 2, 3))).shape == (2, 2, 50, 3)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") _ = neigh.get_neighborhood(np.random.random(3), num_neighbors=51) self.assertEqual(len(w), 1) _ = neigh.get_distances(np.random.random(3), num_neighbors=51) self.assertEqual(len(w), 2)
def test_get_local_shells_ragged(self): structure = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) del structure[0] neigh = structure.get_neighbors(cutoff_radius=3.5, num_neighbors=None) self.assertEqual(np.sum(neigh.shells == -1), 12) self.assertEqual( np.sum(neigh.get_local_shells(cluster_by_distances=True) == -1), 12) self.assertEqual( np.sum(neigh.get_local_shells(cluster_by_vecs=True) == -1), 12) self.assertEqual( np.sum( neigh.get_local_shells(cluster_by_distances=True, cluster_by_vecs=True) == -1), 12) neigh.allow_ragged = True self.assertEqual(np.sum([len(s) == 11 for s in neigh.shells]), 12) self.assertEqual( np.sum([ len(s) == 11 for s in neigh.get_local_shells(cluster_by_distances=True) ]), 12) self.assertEqual( np.sum([ len(s) == 11 for s in neigh.get_local_shells(cluster_by_vecs=True) ]), 12) self.assertEqual( np.sum([ len(s) == 11 for s in neigh.get_local_shells(cluster_by_distances=True, cluster_by_vecs=True) ]), 12)
def test_get_global_shells(self): structure = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) neigh = structure.get_neighbors() self.assertTrue(np.array_equal(neigh.shells, neigh.get_global_shells())) structure += Atoms(elements='C', positions=[[0, 0, 0.5 * 4]]) neigh = structure.get_neighbors() self.assertFalse( np.array_equal(neigh.shells, neigh.get_global_shells())) structure = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) neigh = structure.get_neighbors() shells = neigh.get_global_shells() structure.positions += 0.01 * (np.random.random( (len(structure), 3)) - 0.5) structure.center_coordinates_in_unit_cell() neigh = structure.get_neighbors() self.assertTrue( np.array_equal( shells, neigh.get_global_shells(cluster_by_vecs=True, cluster_by_distances=True))) neigh.reset_clusters() self.assertTrue( np.array_equal(shells, neigh.get_global_shells(cluster_by_vecs=True))) self.assertFalse(np.array_equal(shells, neigh.get_global_shells()))
def test_getters(self): struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc') neigh = struct.get_neighbors() self.assertTrue(np.array_equal(neigh.distances, neigh.get_distances())) self.assertTrue(np.array_equal(neigh.vecs, neigh.get_vectors())) self.assertTrue(np.array_equal(neigh.indices, neigh.get_indices()))
def test_fe_small(self): struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc') neigh = struct.get_neighbors() self.assertAlmostEqual(neigh.vecs[neigh.shells == 1].sum(), 0) with self.assertRaises(ValueError): _ = struct.get_neighbors(num_neighbors=None)
def test_set_empty_states(self): atoms = CrystalStructure("Pt", BravaisBasis="fcc", a=3.98) self.job.structure = atoms self.job.set_empty_states(n_empty_states=10) self.assertEqual(self.job.input.incar["NBANDS"], 15) self.job.structure = atoms.repeat([3, 1, 1]) self.job.set_empty_states(n_empty_states=10) self.assertEqual(self.job.input.incar["NBANDS"], 25)
def test_get_bonds(self): basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(5) neigh = basis.get_neighbors(num_neighbors=20) bonds = neigh.get_bonds() self.assertTrue( np.array_equal(np.sort(bonds[0]['Al'][0]), np.sort(neigh.indices[0, neigh.shells[0] == 1])))
def test_get_layers_across_pbc(self): structure = CrystalStructure('Fe', bravais_basis='bcc', lattice_constants=2.8).repeat(2) layers = structure.analyse.get_layers() structure.cell[1, 0] += 0.01 structure.center_coordinates_in_unit_cell() self.assertEqual( len(np.unique(layers[structure.analyse.get_layers()[:, 0] == 0, 0])), 1)
def test_get_layers_with_strain(self): structure = CrystalStructure('Fe', bravais_basis='bcc', lattice_constants=2.8).repeat(2) layers = structure.analyse.get_layers().tolist() structure.apply_strain(0.1 * (np.random.random((3, 3)) - 0.5)) self.assertEqual( layers, structure.analyse.get_layers( planes=np.linalg.inv(structure.cell).T).tolist())
def test_al_large(self): struct = CrystalStructure(elements='Al', lattice_constants=4.04, bravais_basis='fcc').repeat(10) neigh = struct.get_neighbors() self.assertAlmostEqual( np.absolute(neigh.distances - np.linalg.norm(neigh.vecs, axis=-1)).max(), 0) self.assertAlmostEqual(neigh.vecs[neigh.shells == 1].sum(), 0) self.assertAlmostEqual(neigh.vecs[0, neigh.shells[0] == 1].sum(), 0)
def test_norm_order(self): a_0 = 2.8 basis = CrystalStructure("Fe", bravais_basis="bcc", lattice_constants=a_0).repeat(10) neigh = basis.get_neighbors(num_neighbors=None, norm_order=np.inf, cutoff_radius=a_0 + 0.01) self.assertEqual(len(neigh.indices[0]), 34) with self.assertRaises(ValueError): neigh.norm_order = 3
def test_al_small(self): struct = CrystalStructure(elements='Al', lattice_constants=4.04, bravais_basis='fcc') neigh = struct.get_neighbors() self.assertAlmostEqual( np.absolute(neigh.distances - np.linalg.norm(neigh.vecs, axis=-1)).max(), 0) self.assertAlmostEqual(neigh.vecs[neigh.shells == 1].sum(), 0) with self.assertRaises(ValueError): struct.get_neighbors(num_neighbors=0)
def test_get_shell_matrix(self): structure = CrystalStructure(elements='Fe', lattice_constants=2.83, bravais_basis='bcc').repeat(2) structure[0] = 'Ni' neigh = structure.get_neighbors(num_neighbors=8) mat = neigh.get_shell_matrix() self.assertEqual(mat[0].sum(), 8 * len(structure)) mat = neigh.get_shell_matrix(chemical_pair=['Fe', 'Ni']) self.assertEqual(mat[0].sum(), 16) mat = neigh.get_shell_matrix(chemical_pair=['Ni', 'Ni']) self.assertEqual(mat[0].sum(), 0)
def test_get_neighborhood_single(self): struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc') neigh = struct.get_neighborhood(np.random.random(3), cutoff_radius=3) distances = neigh.distances.copy() neigh.allow_ragged = False self.assertTrue(np.array_equal(neigh.distances, distances)) neigh.allow_ragged = True self.assertTrue(np.array_equal(neigh.distances, distances)) neigh.allow_ragged = False self.assertTrue(np.array_equal(neigh.distances, distances))
def test_getter_and_ragged(self): struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) del struct[0] neigh = struct.get_neighbors_by_distance(cutoff_radius=3, mode='filled') vecs = neigh.filled.vecs distances = neigh.filled.distances indices = neigh.filled.indices self.assertTrue(np.array_equal(neigh.distances, distances)) self.assertTrue(np.array_equal(neigh.indices, indices)) self.assertTrue(np.array_equal(neigh.vecs, vecs))
def test_getter_and_ragged(self): struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) del struct[0] neigh = struct.get_neighbors_by_distance(cutoff_radius=3) vecs = neigh.get_vectors(allow_ragged=False) distances = neigh.get_distances(allow_ragged=False) indices = neigh.get_indices(allow_ragged=False) neigh.allow_ragged = False self.assertTrue(np.array_equal(neigh.distances, distances)) self.assertTrue(np.array_equal(neigh.indices, indices)) self.assertTrue(np.array_equal(neigh.vecs, vecs))
def test_cluster_analysis(self): basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(10) neigh = basis.get_neighbors(num_neighbors=100) key, counts = neigh.cluster_analysis(id_list=[0, 1], return_cluster_sizes=True) self.assertTrue(np.array_equal(key[1], [0, 1])) self.assertEqual(counts[0], 2) key, counts = neigh.cluster_analysis(id_list=[0, int(len(basis) / 2)], return_cluster_sizes=True) self.assertTrue(np.array_equal(key[1], [0])) self.assertEqual(counts[0], 1)
def test_get_neighbors(self): struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc').repeat(10) cell = struct.cell.copy() cell += np.random.random((3, 3)) - 0.5 struct.positions += np.random.random((len(struct), 3)) - 0.5 struct.set_cell(cell, scale_atoms=True) neigh = struct.get_neighbors() self.assertAlmostEqual( np.absolute(neigh.distances - np.linalg.norm(neigh.vecs, axis=-1)).max(), 0) myself = np.ones_like(neigh.indices) myself = myself * np.arange(len(myself))[:, np.newaxis] dist = struct.get_distances(myself.flatten(), neigh.indices.flatten(), mic=True) self.assertAlmostEqual( np.absolute(dist - neigh.distances.flatten()).max(), 0) vecs = struct.get_distances(myself.flatten(), neigh.indices.flatten(), mic=True, vector=True) self.assertAlmostEqual( np.absolute(vecs - neigh.vecs.reshape(-1, 3)).max(), 0) dist = struct.get_scaled_positions() dist = dist[:, np.newaxis, :] - dist[np.newaxis, :, :] dist -= np.rint(dist) dist = np.einsum('nmi,ij->nmj', dist, struct.cell) dist = np.linalg.norm(dist, axis=-1).flatten() dist = dist[dist > 0] self.assertAlmostEqual(neigh.distances.min(), dist.min())
def test_run(self): job = self.project.create_job('HessianJob', "job_test") basis = CrystalStructure(element="Fe", bravais_basis="bcc", lattice_constant=2.85) basis.set_initial_magnetic_moments([2] * len(basis)) job.set_reference_structure(basis) phono = job.create_job("PhonopyJob", "phono") structure = phono.list_structures()[0] magmoms = structure.get_initial_magnetic_moments() self.assertAlmostEqual(sum(magmoms - 2), 0) rep = phono._phonopy_supercell_matrix().diagonal().astype(int) job._reference_structure.set_repeat(rep) job.structure.set_repeat(rep) job.set_force_constants(1)
def setUpClass(cls): state.update({ 'resource_paths': os.path.join(os.path.dirname(os.path.abspath(__file__)), "../static") }) cls.execution_path = os.path.dirname(os.path.abspath(__file__)) cls.project = Project(os.path.join(cls.execution_path, "test_vasp")) cls.job = cls.project.create_job("Vasp", "trial") cls.job_spin = cls.project.create_job("Vasp", "spin") cls.job_spin.structure = CrystalStructure("Fe", BravaisBasis="bcc", a=2.83) cls.job_spin.structure = cls.job_spin.structure.repeat(2) cls.job_spin.structure[2] = "Se" cls.job_spin.structure[3] = "O" cls.job_metadyn = cls.project.create_job("VaspMetadyn", "trial_metadyn") cls.job_complete = Vasp( project=ProjectHDFio(project=cls.project, file_name="vasp_complete"), job_name="vasp_complete", ) poscar_file = posixpath.join( cls.execution_path, "../static/vasp_test_files/full_job_sample/POSCAR") cls.job_complete.structure = read_atoms(poscar_file, species_from_potcar=True) poscar_file = posixpath.join( cls.execution_path, "../static/vasp_test_files/poscar_samples/POSCAR_metadyn") cls.job_metadyn.structure = read_atoms(poscar_file)
def setUpClass(cls): cls.file_location = os.path.dirname(os.path.abspath(__file__)) cls.project = Project(os.path.join(cls.file_location, "test_elast")) cls.basis = CrystalStructure(element="Fe", bravais_basis="bcc", lattice_constant=2.83) cls.project.remove_jobs_silently(recursive=True)
def test_get_voronoi_vertices(self): basis = CrystalStructure('Al', bravais_basis='fcc', lattice_constants=4) self.assertEqual(len(basis.analyse.get_voronoi_vertices()), 12) self.assertEqual( len(basis.analyse.get_voronoi_vertices(distance_threshold=2)), 1)
def setUp(self) -> None: self.job = ToyAtomisticJob( project=ProjectHDFio(project=self.project, file_name="test_job"), job_name="test_job", ) self.job.structure = CrystalStructure(element="Al", bravais_basis="fcc", lattice_constants=4).repeat(4) self.job.run()
def setUpClass(cls): cls.al_fcc = ase_to_pyiron(bulk("Al", cubic=True)) cls.fe_bcc = ase_to_pyiron(bulk("Fe", cubic=True)) cls.ti_hcp = ase_to_pyiron(bulk("Ti", orthorhombic=True)) cls.si_dia = ase_to_pyiron(bulk("Si", cubic=True)) cls.al_fcc_4 = CrystalStructure(element="Al", bravais_basis="fcc", lattice_constants=4 ).repeat(4)
def test_pyscal_centro_symmetry(self): basis = CrystalStructure('Fe', bravais_basis='bcc', lattice_constants=2.8) self.assertTrue( all([ np.isclose(v, 0.0) for v in basis.analyse.pyscal_centro_symmetry(num_neighbors=8) ]))
def test_get_neighborhood(self): struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc') struct.positions += 0.01 * ( 2 * np.random.random(struct.positions.shape) - 1) struct = struct.center_coordinates_in_unit_cell() positions = np.random.random((2, 3)).dot(struct.cell) neigh = struct.get_neighborhood(positions) positions = np.random.random((2, 3)).dot(struct.cell) new_neigh = neigh.get_neighborhood(positions) self.assertTrue( np.array_equal(new_neigh.distances, neigh.get_distances(positions))) self.assertTrue( np.array_equal(new_neigh.vecs, neigh.get_vectors(positions))) self.assertTrue( np.array_equal(new_neigh.indices, neigh.get_indices(positions)))
def test_set_structure(self): self.assertEqual(self.job.structure, None) atoms = CrystalStructure("Pt", BravaisBasis="fcc", a=3.98) self.job.structure = atoms self.assertEqual(self.job.structure, atoms) self.job.structure = None self.assertEqual(self.job.structure, None) self.job.structure = atoms self.assertEqual(self.job.structure, atoms)
def setUpClass(cls): cls.execution_path = os.path.dirname(os.path.abspath(__file__)) cls.project = Project(os.path.join(cls.execution_path, "test_job")) cls.job = AtomisticGenericJob( project=ProjectHDFio(project=cls.project, file_name="test_job"), job_name="test_job", ) cls.job.structure = CrystalStructure(element="Al", bravais_basis="fcc", lattice_constants=4).repeat(4)
def test_get_local_shells(self): structure = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2) neigh = structure.get_neighbors() shells = neigh.get_local_shells() structure.positions += 0.01 * (np.random.random( (len(structure), 3)) - 0.5) neigh = structure.get_neighbors() self.assertTrue( np.array_equal( shells, neigh.get_local_shells(cluster_by_vecs=True, cluster_by_distances=True))) neigh.reset_clusters() self.assertTrue( np.array_equal(shells, neigh.get_local_shells(cluster_by_vecs=True))) self.assertFalse(np.array_equal(shells, neigh.get_local_shells()))