def test_apply_transformation(self): t = TranslateSitesTransformation([0], [0.1, 0.2, 0.3]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertTrue(np.allclose(s[0].frac_coords, [0, 0, 0]))
def test_apply_transformation(self): t = TranslateSitesTransformation([0], [0.1, 0.2, 0.3]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertTrue(np.allclose(s[0].frac_coords, [0, 0, 0])) str(t)
def translate_sites(self, lattice): s = Structure(lattice, [self.central_subsite.specie] + [site.specie for site in self.peripheral_subsites], [self.central_subsite.site.coords] + [site.site.coords for site in self.peripheral_subsites], coords_are_cartesian=True) trans = TranslateSitesTransformation(range(len(s)), -(lattice.get_fractional_coords(s[0].coords))) new_s = trans.apply_transformation(s) trans2 = TranslateSitesTransformation(range(len(s)), (-0.5, -0.5, -0.5)) new_s = trans2.apply_transformation(Structure.from_sites(new_s.sites, to_unit_cell=True)) self.peripheral_subsites = [SubStructureSite.from_coords_and_specie(site.coords, site.specie) for site in new_s.sites[1:]] self.central_subsite = SubStructureSite.from_coords_and_specie(new_s[0].coords, new_s[0].specie)
def test_apply_transformation_site_by_site(self): t = TranslateSitesTransformation([0, 1], [[0.1, 0.2, 0.3], [-0.075, -0.075, -0.075]]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s[1].frac_coords, [0.3, 0.3, 0.3])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertAlmostEqual(s[0].distance_and_image_from_frac_coords([0, 0, 0])[0], 0) self.assertArrayAlmostEqual(s[1].frac_coords, [0.375, 0.375, 0.375])
def test_apply_transformation(self): t = TranslateSitesTransformation([0, 1], [0.1, 0.2, 0.3]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s[1].frac_coords, [0.475, 0.575, 0.675])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertAlmostEqual(s[0].distance_and_image_from_frac_coords([0, 0, 0])[0], 0) self.assertTrue(np.allclose(s[1].frac_coords, [0.375, 0.375, 0.375]))
def test_apply_transformation_site_by_site(self): t = TranslateSitesTransformation( [0, 1], [[0.1, 0.2, 0.3], [-0.075, -0.075, -0.075]]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s[1].frac_coords, [0.3, 0.3, 0.3])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertTrue(np.allclose(s[0].frac_coords, [0, 0, 0])) self.assertTrue(np.allclose(s[1].frac_coords, [0.375, 0.375, 0.375])) str(t)
def test_apply_transformation_site_by_site(self): t = TranslateSitesTransformation([0, 1], [[0.1, 0.2, 0.3], [-0.075, -0.075, -0.075]]) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s[1].frac_coords, [0.3, 0.3, 0.3])) inv_t = t.inverse s = inv_t.apply_transformation(s) self.assertTrue(np.allclose(s[0].frac_coords, [0, 0, 0])) self.assertTrue(np.allclose(s[1].frac_coords, [0.375, 0.375, 0.375])) str(t)
def test_to_from_dict(self): d1 = TranslateSitesTransformation([0], [0.1, 0.2, 0.3]).as_dict() d2 = TranslateSitesTransformation([0, 1], [[0.1, 0.2, 0.3], [-0.075, -0.075, -0.075]]).as_dict() t1 = TranslateSitesTransformation.from_dict(d1) t2 = TranslateSitesTransformation.from_dict(d2) s1 = t1.apply_transformation(self.struct) s2 = t2.apply_transformation(self.struct) self.assertTrue(np.allclose(s1[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s2[0].frac_coords, [0.1, 0.2, 0.3])) self.assertTrue(np.allclose(s2[1].frac_coords, [0.3, 0.3, 0.3])) str(t1) str(t2)
def move_all_atoms(structure, disp_ion): """ Create structures with displaced ions """ yield 'calc_00_0', structure atoms = [i for i in range(len(structure))] for atom in atoms: for direction in range(3): displacement = [0.] * 3 displacement[direction] = disp_ion for orientation in ['+', '-']: transform = TranslateSitesTransformation( [atom], displacement, vector_in_frac_coords=True) structure_displaced = transform.apply_transformation(structure) yield 'calc_%02d_%s%d' % (atom + 1, orientation, direction + 1), structure_displaced displacement[direction] = -displacement[direction]
def planar_structure_normalization(structure): ''' This function does the following: 1. check whether the structure is planar using coordniates standard deviation 2. move the planar layer to the center of c-direction Args: structure: pymatgen structure Return: a boolean whether the structure is planar tranformed pymatgen structure ''' tol = 1E-3 # tolerance to check whether the structure is planar is_planar = True coords = structure.frac_coords ts = TransformedStructure(structure, []) if np.std(coords[:,2]) < tol : center_translate = 0.5 - coords[:,2].mean() elif np.std(coords[:,0]) < tol : ts.append_transformation(SupercellTransformation([[0,0,1],[0,1,0],[1,0,0]])) ts.append_transformation(RotationTransformation([0,1,0], 90)) center_translate = 0.5 - coords[:,0].mean() elif np.std(coords[:,1]) < tol : ts.append_transformation(SupercellTransformation([[1,0,0],[0,0,1],[0,1,0]])) ts.append_transformation(RotationTransformation([1,0,0], 90)) center_translate = 0.5 - coords[:,1].mean() else : is_planar = False transformed_structure = None if is_planar: ts.append_transformation(TranslateSitesTransformation( list(range(len(structure))), [0,0,center_translate])) # Use pymatgen 2019.7.2, ts.structures[-1] may change in a newer version transformed_structure = ts.structures[-1] return is_planar, transformed_structure
def test_to_from_dict(self): d = TranslateSitesTransformation([0], [0.1, 0.2, 0.3]).to_dict t = TranslateSitesTransformation.from_dict(d) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) str(t)
def test_to_from_dict(self): d = TranslateSitesTransformation([0], [0.1, 0.2, 0.3]).as_dict() t = TranslateSitesTransformation.from_dict(d) s = t.apply_transformation(self.struct) self.assertTrue(np.allclose(s[0].frac_coords, [0.1, 0.2, 0.3])) str(t)