Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_positions(self):
basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2)
self.assertTrue(np.array_equal(basis_Mg.positions, basis_Mg.get_positions()))
def test_apply_strain(self):
basis_Fe = CrystalStructure("Fe", bravais_basis="bcc", lattice_constants=2.85)
with self.assertRaises(ValueError):
basis_Fe.apply_strain(-2)
basis_new = basis_Fe.apply_strain(0.01, return_box=True)
self.assertAlmostEqual(basis_new.cell[0,0], 2.85*1.01)
self.assertAlmostEqual(basis_new.positions[1,0], 0.5*2.85*1.01)
self.assertAlmostEqual(basis_Fe.cell[0,0], 2.85)
basis_Fe.apply_strain(0.01)
self.assertAlmostEqual(basis_Fe.cell[0,0], 2.85*1.01)
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.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_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.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_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_apply_strain(self):
basis_Fe = CrystalStructure("Fe", bravais_basis="bcc", lattice_constants=2.85)
with self.assertRaises(ValueError):
basis_Fe.apply_strain(-2)
basis_new = basis_Fe.apply_strain(0.01, return_box=True)
self.assertAlmostEqual(basis_new.cell[0,0], 2.85*1.01)
self.assertAlmostEqual(basis_new.positions[1,0], 0.5*2.85*1.01)
self.assertAlmostEqual(basis_Fe.cell[0,0], 2.85)
basis_Fe.apply_strain(0.01)
self.assertAlmostEqual(basis_Fe.cell[0,0], 2.85*1.01)
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.set_scaled_positions(basis_O.get_scaled_positions() + [0.0, 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))
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)
# cls.project.remove_jobs(recursive=True)
def test_get_scaled_positions(self):
basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2)
basis_Mg.set_cell(basis_Mg.cell+0.1 * np.random.random((3, 3)))
basis_Mg = basis_Mg.center_coordinates_in_unit_cell()
self.assertTrue(
np.allclose(
np.dot(np.linalg.inv(basis_Mg.cell).T, basis_Mg.positions.T).T,
basis_Mg.get_scaled_positions(),
)
lat_0[:] = "V"
self.assertEqual(lat_0.get_chemical_formula(), "V108")
lat_0[[1, 3, 5]] = "Mg" # direct occupation
self.assertEqual(lat_0.get_chemical_formula(), "Mg3V105")
# lat_0[[0]] = 'V' # vacancy (note: do not delete atom)
lat_1 = lat_0.copy()
lat_1.set_scaled_positions(1 / 4 + lat_1.get_scaled_positions())
lat_1[:] = "V"
self.assertEqual(lat_1.get_chemical_formula(), "V108")
lat_1[[1, 4, 9]] = "H"
lat_1[[2, 5, 8]] = "C"
self.assertEqual(lat_1.get_chemical_formula(), "C3H3V102")
lat_1.set_scaled_positions(1 / 4 + lat_1.get_scaled_positions())
lat_1[:] = "V" # vacancies
self.assertEqual(lat_1.get_chemical_formula(), "V108")
basis_Mg = CrystalStructure("Mg", bravais_basis="fcc", lattice_constant=4.2)
basis_Mg.set_repeat(3)
basis_Mg[:-3] = "Al"
self.assertEqual(basis_Mg.get_chemical_formula(), 'Al105Mg3')
basis_Mg[4:-len(basis_Mg)+7] = "C"
self.assertEqual(basis_Mg.get_chemical_formula(), 'Al102C3Mg3')
basis_Mg[4:] = "C"
self.assertEqual(basis_Mg.get_chemical_formula(), 'Al4C104')
basis_Mg[:] = "Mg"
self.assertEqual(basis_Mg.get_chemical_formula(), 'Mg108')
basis_Mg[::2] = "Al"
self.assertEqual(basis_Mg.get_chemical_formula(), 'Al54Mg54')
struct = CrystalStructure("Al", bravais_basis="fcc", lattice_constant=4.2, bravais_lattice="cubic")
struct[0] = 'Mg'
self.assertEqual(struct.get_chemical_formula(), 'Al3Mg')
struct[1] = 'Cu'
self.assertEqual(struct.get_chemical_formula(), 'Al2CuMg')