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_occupy_lattice(self): basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) basis_O = CrystalStructure("O", bravais_basis="fcc", lattice_constant=4.2) basis_O.scaled_positions += [0., 0., 0.5] basis = basis_Mg + basis_O basis.center_coordinates_in_unit_cell() orig_basis = basis.copy() self.assertEqual(basis.get_chemical_formula(), "Mg4O4") Mg_indices = basis.select_index("Mg") O_indices = basis.select_index("O") basis.occupy_lattice(Na=Mg_indices) self.assertEqual(basis.get_chemical_formula(), "Na4O4") basis.occupy_lattice(Cl=O_indices) self.assertEqual(basis.get_chemical_formula(), "Cl4Na4") self.assertTrue(np.array_equal(basis.select_index("Na"), Mg_indices)) self.assertTrue(np.array_equal(basis.select_index("Cl"), O_indices)) orig_basis.set_repeat([2, 2, 2]) Mg_indices = orig_basis.select_index("Mg") O_indices = orig_basis.select_index("O") orig_basis.occupy_lattice(Cl=O_indices, Na=Mg_indices) self.assertEqual(orig_basis.get_chemical_formula(), "Cl32Na32") orig_basis.occupy_lattice(H=O_indices[0]) self.assertEqual(orig_basis.get_chemical_formula(), "Cl31HNa32")
def setUp(self): self.bcc = CrystalStructure("Fe", bravais_basis='bcc', lattice_constants=[2.7]) self.fcc = CrystalStructure("Fe", bravais_basis='fcc', lattice_constants=[3.6])
def test__getitem__(self): self.assertEqual(self.CO2[0].symbol, 'C') self.assertEqual(self.C3[2].position.tolist(), [0, 2, 0]) self.assertTrue((self.C3[1:].positions == np.array([[0, 0, 2], [0, 2, 0]])).all()) short_basis = self.CO2[0] self.assertIsInstance(short_basis, Atom) short_basis = self.CO2[[0]] self.assertIsInstance(short_basis, Atoms) self.assertEqual(short_basis.indices[0], 0) self.assertEqual(len(short_basis.species), 1) short_basis = self.CO2[[2]] self.assertIsInstance(short_basis, Atoms) self.assertEqual(short_basis.indices[0], 0) self.assertEqual(len(short_basis.species), 1) basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) basis_O = CrystalStructure("O", bravais_basis="fcc", lattice_constant=4.2) basis_O.positions += [0., 0., 0.5] basis = basis_Mg + basis_O basis.center_coordinates_in_unit_cell() basis.set_repeat([3, 3, 3]) mg_indices = basis.select_index("Mg") o_indices = basis.select_index("O") basis_new = basis[mg_indices] + basis[o_indices] self.assertEqual(len(basis_new._tag_list), len(basis[mg_indices]) + len(basis[o_indices])) self.assertEqual(basis_new.get_spacegroup()["Number"], 225)
def test_get_scaled_positions(self): basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) self.assertTrue( np.array_equal(basis_Mg.scaled_positions, basis_Mg.get_scaled_positions()))
def setUpClass(cls): cls.file_location = os.path.dirname(os.path.abspath(__file__)) cls.project = Project(os.path.join(cls.file_location, "test_murnaghan")) cls.basis = CrystalStructure( element="Fe", bravais_basis="fcc", lattice_constant=3.5 ) cls.project.remove_jobs_silently(recursive=True)
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) 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 setUpClass(cls): cls.file_location = os.path.dirname(os.path.abspath(__file__)) cls.project = Project( os.path.join(cls.file_location, "testing_murnaghan_non_modal")) cls.basis = CrystalStructure(element="Fe", bravais_basis="bcc", lattice_constant=2.8) cls.project.remove_jobs(recursive=True)
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_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_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 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 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_parent_index(self): basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) basis_O = CrystalStructure("O", bravais_basis="fcc", lattice_constant=4.2) basis_O.positions += [0., 0., 0.5] basis = basis_Mg + basis_O basis.center_coordinates_in_unit_cell() basis.set_repeat([2, 2, 2]) o_indices = basis.select_index("O") pse = PeriodicTable() pse.add_element("O", "O_up", spin="up") o_up = pse.element("O_up") basis[o_indices] = o_up self.assertTrue(np.array_equal(o_indices, basis.select_index(o_up))) self.assertEqual(len(basis.select_index("O")), 0) self.assertTrue( np.array_equal(o_indices, basis.select_parent_index("O")))
def test_run(self): job = self.project.create_job( self.project.job_type.AtomisticExampleJob, "job_test") basis = CrystalStructure(element="Fe", bravais_basis="bcc", lattice_constant=2.83) basis.set_initial_magnetic_moments([2, 2]) job.structure = basis phono = self.project.create_job("PhonopyJob", "phono") phono.ref_job = job structure = phono.list_structures()[0] magmoms = structure.get_initial_magnetic_moments() self.assertAlmostEqual(sum(magmoms - 2), 0)
def test_repeat(self): basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) basis_O = CrystalStructure("O", bravais_basis="fcc", lattice_constant=4.2) basis_O.scaled_positions += [0., 0., 0.5] basis = basis_Mg + basis_O basis.center_coordinates_in_unit_cell() basis.add_tag(selective_dynamics=[True, True, True]) basis.selective_dynamics[basis.select_index("O")] = [ False, False, False ] len_before = len(basis) sel_dyn_before = np.array(basis.selective_dynamics.list()) self.assertTrue( np.alltrue( np.logical_not( np.alltrue(sel_dyn_before[basis.select_index("O")], axis=1)))) self.assertTrue( np.alltrue( np.alltrue(sel_dyn_before[basis.select_index("Mg")], axis=1))) basis.set_repeat([3, 3, 2]) sel_dyn_after = np.array(basis.selective_dynamics.list()) len_after = len(basis) self.assertEqual(basis.get_spacegroup()["Number"], 225) self.assertEqual(len_before * 18, len_after) self.assertEqual(len(sel_dyn_before) * 18, len(sel_dyn_after)) self.assertTrue( np.alltrue( np.logical_not( np.alltrue(sel_dyn_after[basis.select_index("O")], axis=1)))) self.assertTrue( np.alltrue( np.alltrue(sel_dyn_after[basis.select_index("Mg")], axis=1)))
def create_structure(element, bravais_basis, lattice_constant): """ Args: element: bravais_basis: lattice_constant: Returns: """ return CrystalStructure(element=element, bravais_basis=bravais_basis, lattice_constants=[lattice_constant])
def test_Chemical_Element_to_and_from_hdf_with_None_Parent(self): pr = Project( os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_periodic_table")) basis = CrystalStructure(element="Ni", bravais_basis="fcc", lattice_constant=3.7) ham = pr.create_job(pr.job_type.Lammps, "lammps_test_2") test_ham = pr.create_job(pr.job_type.Lammps, "lammps_test_2") ham.structure = basis ham.to_hdf() test_ham.from_hdf() self.assertEqual(test_ham["input/structure/species"][0], ham["input/structure/species"][0]) ham.remove()
def test_Chemical_Element_to_and_from_hdf(self): ni_up = self.pse.add_element("Ni", "Ni_up", spin="up") pr = Project( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_periodic_table')) basis = CrystalStructure(element=ni_up, bravais_basis='fcc', lattice_constant=3.7) ham = pr.create_job(pr.job_type.Lammps, 'lammps_test_1') test_ham = pr.create_job(pr.job_type.Lammps, 'lammps_test_1') ham.structure = basis ham.to_hdf() test_ham.from_hdf() self.assertEqual(test_ham['input/structure/species'][0], ham['input/structure/species'][0]) ham.remove()
def test_write_magmoms(self): magmom = np.arange(8.) magmom_ncl = np.zeros([8, 3]) magmom_ncl[:, 0] = magmom / 2 magmom_ncl[:, 1] = magmom magmom_ncl[:, 2] = magmom ** 2 magmom_str = "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0" magmom_ncl_str =\ "0.0 0.0 0.0 0.5 1.0 1.0 1.0 2.0 4.0 1.5 3.0 9.0 " \ "2.0 4.0 16.0 2.5 5.0 25.0 3.0 6.0 36.0 3.5 7.0 49.0" self.job.structure = CrystalStructure("Fe", BravaisBasis="bcc", a=2.83) self.job.structure = self.job.structure.repeat(2) self.job.structure.set_initial_magnetic_moments(magmom) self.job.input.incar["ISPIN"] = 1 self.job.write_magmoms() self.assertIsNone(self.job.input.incar["MAGMOM"]) self.assertEqual(self.job.input.incar["ISPIN"], 1) del self.job.input.incar["ISPIN"] self.job.write_magmoms() self.assertEqual(self.job.input.incar["ISPIN"], 2) self.assertEqual(self.job.input.incar["MAGMOM"], magmom_str) del self.job.input.incar["MAGMOM"] self.job.structure.set_initial_magnetic_moments(magmom_ncl) self.job.set_spin_constraint(lamb=1.0, rwigs_dict={"Fe": 2.5}, direction=True, norm=True) self.job.write_magmoms() self.assertEqual(self.job.input.incar["LNONCOLLINEAR"], True) self.assertEqual(self.job.input.incar["MAGMOM"], magmom_ncl_str) self.assertEqual(self.job.input.incar["M_CONSTR"], magmom_ncl_str) del self.job.input.incar["MAGMOM"] del self.job.input.incar["M_CONSTR"] del self.job.input.incar["LNONCOLLINEAR"] del self.job.input.incar["RWIGS"] self.assertRaises(ValueError, self.job.write_magmoms) self.job.input.incar["RWIGS"] = "2.5" del self.job.input.incar["LAMBDA"] self.assertRaises(ValueError, self.job.write_magmoms)
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 create_structure(element, bravais_basis, lattice_constant): """ Create a crystal structure using pyiron's native crystal structure generator Args: element (str): Element name bravais_basis (str): Basis type lattice_constant (float/list): Lattice constants Returns: pyiron.atomistics.structure.atoms.Atoms: The required crystal structure """ return CrystalStructure( element=element, bravais_basis=bravais_basis, lattice_constants=[lattice_constant], )
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()) struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc').repeat(10) struct.pbc = False cell = struct.cell.copy() cell += np.random.random((3,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 = np.linalg.norm(struct.positions[myself]-struct.positions[neigh.indices], axis=-1) self.assertAlmostEqual(np.absolute(dist-neigh.distances).max(), 0) struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc').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) struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc') neigh = struct.get_neighbors() self.assertAlmostEqual(neigh.vecs[neigh.shells==1].sum(), 0) struct = CrystalStructure(elements='Al', lattice_constants=4.04, bravais_basis='bcc').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) struct = CrystalStructure(elements='Al', lattice_constants=4.04, bravais_basis='bcc') 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_run(self): try: # Only test if the machine has access to sqsgenerator -- at time of writing Windows doesn't job = self.project.create_job( 'SQSJob', "job_test" ) structure = CrystalStructure("Al", bravais_basis="fcc", lattice_constant=4) job.structure = structure with self.assertRaises(ValueError): job.validate_ready_to_run() job.input.mole_fractions = dict() structure[0] = 'Sc' job.structure = structure job.validate_ready_to_run() job.input.mole_fractions = dict() mole_fractions = {'Al': 0.5, 'Sc': 0.5} job.input.mole_fractions = mole_fractions.copy() job.structure = structure job.validate_ready_to_run() self.assertAlmostEqual(job.input.mole_fractions['Al'], mole_fractions['Al']) except ImportError: pass
def setUpClass(cls): 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 test_get_nelect(self): atoms = CrystalStructure("Pt", BravaisBasis="fcc", a=3.98) self.job.structure = atoms self.assertEqual(self.job.get_nelect(), 10)
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__add__(self): cell = np.eye(3) * 10.0 basis_0 = Atoms(["O"], scaled_positions=[[0.5, 0.5, 0.5]], cell=cell) basis_1 = Atoms(["H"], scaled_positions=[[0.75, 0.75, 0.75]], cell=cell) basis_2 = Atoms(["H"], scaled_positions=[[0.25, 0.25, 0.25]], cell=cell) basis_3 = Atoms(["H", "O", "N"], scaled_positions=[[0.35, 0.35, 0.35], [0., 0., 0.], [0., 0., 0.1]], cell=cell) pse = PeriodicTable() pse.add_element("O", "O_up", spin="up") o_up = pse.element("O_up") basis_4 = Atoms([o_up], scaled_positions=[[0.27, 0.27, 0.27]], cell=np.eye(3) * 20.0) b = basis_0 + basis_1 self.assertEqual(b.get_chemical_formula(), "HO") b = basis_0 + basis_1 + basis_2 self.assertEqual(b.get_chemical_formula(), "H2O") b += basis_2 self.assertEqual(b.get_chemical_formula(), "H3O") b = basis_0 + basis_1 + basis_2 + basis_3 self.assertEqual(b.get_chemical_formula(), "H3NO2") self.assertTrue( np.array_equal(b.scaled_positions[b.select_index("N")], [[0., 0., 0.1]])) self.assertTrue( np.allclose( b.scaled_positions[b.select_index("H")], [[0.75, 0.75, 0.75], [0.25, 0.25, 0.25], [0.35, 0.35, 0.35]])) self.assertTrue( np.allclose(b.scaled_positions[b.select_index("O")], [[0.5, 0.5, 0.5], [0., 0., 0.]])) b.set_repeat([2, 2, 2]) self.assertEqual(b.get_chemical_formula(), "H24N8O16") b += basis_4 self.assertEqual(b.get_chemical_formula(), "H24N8O16O_up") self.assertTrue( np.allclose(b.scaled_positions[b.select_index(o_up)], [[0.27, 0.27, 0.27]])) COX = self.C2 + Atom("O", position=[0, 0, -2]) COX += Atom("O", position=[0, 0, -4]) COX += COX n_objects = len(set(COX.get_species_objects())) n_species = len(set(COX.get_chemical_elements())) self.assertEqual(n_objects, n_species) self.assertEqual(n_objects, 2) self.assertEqual(n_species, 2) basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2) basis_O = CrystalStructure("O", bravais_basis="fcc", lattice_constant=4.2) # basis_O.set_relative() basis_O.scaled_positions += [0., 0., 0.5] basis = basis_Mg + basis_O self.assertEqual(len(basis._tag_list), len(basis_Mg._tag_list) + len(basis_O._tag_list)) basis.center_coordinates_in_unit_cell() self.assertEqual(basis.get_spacegroup()["Number"], 225)