Ejemplo n.º 1
0
 def get_period_dis(self, p1, p2):
     """
     在考虑周期性的情况下,计算两点之间的距离
     :param p1: 分数坐标,例如[0.5,0.5,0.5]
     :param p2: 分数坐标
     :return:  两点之间的距离,考虑周期性
     """
     temp_site1 = PeriodicSite('Ar', p1, self.__struc.lattice)
     temp_site2 = PeriodicSite('Ar', p2, self.__struc.lattice)
     dis = temp_site1.distance(temp_site2)
     return dis
Ejemplo n.º 2
0
def rediscovery_kdTree(stru, migrate, vorosites, threshold = 0.5):
    recover_labels = []
    recover_state = {}
    migrate_mindis = {}
    
    migrate_pos_frac = np.around(np.array([site.frac_coords for site in stru.sites if migrate in site._atom_site_label], ndmin=2), 3)
    # print(migrate_pos_frac)
    migrate_pos_frac %= 1.0
    migrate_pos_frac %= 1.0
    # print(migrate_pos_frac)
    # migrate_pos = [site.coords for site in stru.sites if migrate in site._atom_site_label]
    # print(migrate_pos)
    labels = [site._atom_site_label for site in stru.sites if migrate in site._atom_site_label]

    points = np.around(np.array(vorosites[0] + vorosites[1] + vorosites[2], ndmin=2), 3)
    # print(points)
    points %= 1.0
    points %= 1.0
    # print(points)
    # print(len(points))
    vorositesKdTree = cKDTree(points)
    min_dis,min_ids = vorositesKdTree.query(migrate_pos_frac,k=1)
    # print(labels)
    # print(min_dis)

    for idx in range(len(min_ids)):
        if labels[idx] in recover_labels:
            continue
        tmp_site1 = PeriodicSite("Ar", migrate_pos_frac[idx], stru.lattice)
        tmp_site2 = PeriodicSite("Ar", points[min_ids[idx]], stru.lattice)
        pts_len = [len(vorosites[0]), len(vorosites[1]), len(vorosites[2])]
        pt_tag = get_point_tag(min_ids[idx], pts_len)
        migrate_mindis[str(labels[idx])] = (pt_tag, tmp_site1.distance(tmp_site2))
        if tmp_site1.distance(tmp_site2) <= threshold:
            recover_state[str(labels[idx])] = pt_tag
            recover_labels.append(labels[idx])
        else:
            recover_state[str(labels[idx])] = None

    recover_rate = len(recover_labels) / len(np.unique(labels))
    return recover_rate, recover_state, migrate_mindis
Ejemplo n.º 3
0
def rediscovery(migrate,vorosites,stru):
    labels = []
    recover_labels = []
    recover_state = {}
    true_recover_dis = {}
    #点类型,分别表示间隙、瓶颈、面心
    points_type = ["It","Bn","Fc"]


    for k in range(len(stru.sites)):
        site = stru.sites[k]
        label = site._atom_site_label
        if migrate not in label:
            continue
        #labels记录所有的label
        if label not in labels:
            labels.append(label)
        #recover_labels记录已恢复的label
        if label in recover_labels:
            continue

        for pts_idx, pts in enumerate (vorosites):
            cp_tag = np.ones((len(pts), ), dtype=int)
            for pt_idx, pt in enumerate (pts):
                if cp_tag[pt_idx] != -1:
                    print("mobile:",site,"label",label)
                    print("void:",pt)
                    #以Ar作为临时当前空隙的表示符号
                    tmp_site = PeriodicSite("Ar",pt,stru.lattice)
                    print(site.distance(tmp_site))

                    if site.distance(tmp_site) < 0.5:
                        #当某空隙位已与结构中的晶格位配对时,将该空隙位以及与该空隙位0.25A半径范围内的所有空隙位移除,后续不再判断。
                        recover_labels.append(label)

                        true_recover_dis[str(label)] = (points_type[pts_idx]+str(pt_idx),site.distance(tmp_site))
                        cp_tag[pt_idx] = -1
                        
                        for pt_idx2, pt2 in enumerate (pts):
                            tmp_site2 = PeriodicSite("Ar",list(pt2),stru.lattice)
                            if tmp_site.distance(tmp_site2) < 0.25:
                                cp_tag[pt_idx2] = -1
                        break

    #统计当前结构的恢复率
    recover_rate = len(recover_labels)/len(labels)
    for la in labels:
        if la in recover_labels:
            recover_state[str(la)] = True
        else:
            recover_state[str(la)] = False

    return recover_rate, recover_state, true_recover_dis
Ejemplo n.º 4
0
 def _supercell_with_defect(self, scaling_matrix, defect_site):
     sc = self._structure.copy()
     sc.make_supercell(scaling_matrix)
     oldf_coords = defect_site.frac_coords
     coords = defect_site.lattice.get_cartesian_coords(oldf_coords)
     newf_coords = sc.lattice.get_fractional_coords(coords)
     sc_defect_site = PeriodicSite(
         defect_site.species_and_occu, newf_coords, sc.lattice, properties=defect_site.properties
     )
     for i in range(len(sc.sites)):
         # if sc_defect_site == sc.sites[i]:
         if sc_defect_site.distance(sc.sites[i]) < 1e-3:
             del sc[i]
             return sc
     raise ValueError("Something wrong if reached here")
Ejemplo n.º 5
0
 def _supercell_with_defect(self, scaling_matrix, defect_site):
     sc = self._structure.copy()
     sc.make_supercell(scaling_matrix)
     oldf_coords = defect_site.frac_coords
     coords = defect_site.lattice.get_cartesian_coords(oldf_coords)
     newf_coords = sc.lattice.get_fractional_coords(coords)
     sc_defect_site = PeriodicSite(defect_site.species_and_occu,
                                   newf_coords,
                                   sc.lattice,
                                   properties=defect_site.properties)
     for i in range(len(sc.sites)):
         #if sc_defect_site == sc.sites[i]:
         if sc_defect_site.distance(sc.sites[i]) < 1e-3:
             del sc[i]
             return sc
     raise ValueError('Something wrong if reached here')
Ejemplo n.º 6
0
class PeriodicSiteTest(PymatgenTest):
    def setUp(self):
        self.lattice = Lattice.cubic(10.0)
        self.si = Element("Si")
        self.site = PeriodicSite("Fe", [0.25, 0.35, 0.45], self.lattice)
        self.site2 = PeriodicSite({"Si": 0.5}, [0, 0, 0], self.lattice)
        self.assertEqual(self.site2.species_and_occu,
                         Composition({Element('Si'): 0.5}),
                         "Inconsistent site created!")
        self.propertied_site = PeriodicSite(Specie("Fe", 2),
                                            [0.25, 0.35, 0.45],
                                            self.lattice,
                                            properties={
                                                'magmom': 5.1,
                                                'charge': 4.2
                                            })
        self.dummy_site = PeriodicSite("X", [0, 0, 0], self.lattice)

    def test_properties(self):
        """
        Test the properties for a site
        """
        self.assertEqual(self.site.a, 0.25)
        self.assertEqual(self.site.b, 0.35)
        self.assertEqual(self.site.c, 0.45)
        self.assertEqual(self.site.x, 2.5)
        self.assertEqual(self.site.y, 3.5)
        self.assertEqual(self.site.z, 4.5)
        self.assertTrue(self.site.is_ordered)
        self.assertFalse(self.site2.is_ordered)
        self.assertEqual(self.propertied_site.magmom, 5.1)
        self.assertEqual(self.propertied_site.charge, 4.2)

    def test_distance(self):
        other_site = PeriodicSite("Fe", np.array([0, 0, 0]), self.lattice)
        self.assertAlmostEqual(self.site.distance(other_site), 6.22494979899,
                               5)

    def test_distance_from_point(self):
        self.assertNotAlmostEqual(
            self.site.distance_from_point([0.1, 0.1, 0.1]), 6.22494979899, 5)
        self.assertAlmostEqual(self.site.distance_from_point([0.1, 0.1, 0.1]),
                               6.0564015718906887, 5)

    def test_distance_and_image(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        (distance, image) = self.site.distance_and_image(other_site)
        self.assertAlmostEqual(distance, 6.22494979899, 5)
        self.assertTrue(([-1, -1, -1] == image).all())
        (distance, image) = self.site.distance_and_image(other_site, [1, 0, 0])
        self.assertAlmostEqual(distance, 19.461500456028563, 5)
        # Test that old and new distance algo give the same ans for
        # "standard lattices"
        lattice = Lattice(np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertAlmostEqual(
            get_distance_and_image_old(site1, site2)[0],
            site1.distance_and_image(site2)[0])
        lattice = Lattice.from_parameters(1, 0.01, 1, 10, 10, 10)
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertTrue(
            get_distance_and_image_old(site1, site2)[0] >
            site1.distance_and_image(site2)[0])
        site2 = PeriodicSite("Fe", np.random.rand(3), lattice)
        (dist_old, jimage_old) = get_distance_and_image_old(site1, site2)
        (dist_new, jimage_new) = site1.distance_and_image(site2)
        self.assertTrue(dist_old - dist_new > -1e-8,
                        "New distance algo should give smaller answers!")
        self.assertFalse((abs(dist_old - dist_new) < 1e-8) ^
                         (jimage_old == jimage_new).all(),
                         "If old dist == new dist, images must be the same!")
        latt = Lattice.from_parameters(3.0, 4.0, 10.0, 3.0, 1.0, 2.0)
        site = PeriodicSite("Fe", [0.1, 0.1, 0.1], latt)
        site2 = PeriodicSite("Fe", [0.99, 0.99, 0.99], latt)
        (dist, img) = site.distance_and_image(site2)
        self.assertAlmostEqual(dist, 1.1304420998572722)
        self.assertEqual(list(img), [0, -1, -1])

    def test_is_periodic_image(self):
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]), self.lattice)
        self.assertTrue(self.site.is_periodic_image(other),
                        "This other site should be a periodic image.")
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        self.assertFalse(self.site.is_periodic_image(other),
                         "This other site should not be a periodic image.")
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]),
                             Lattice.rhombohedral(2, 60))
        self.assertFalse(self.site.is_periodic_image(other),
                         "Different lattices should not be periodic images.")

    def test_equality(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        self.assertTrue(self.site.__eq__(self.site))
        self.assertFalse(other_site.__eq__(self.site))
        self.assertFalse(self.site.__ne__(self.site))
        self.assertTrue(other_site.__ne__(self.site))

    def test_as_from_dict(self):
        d = self.site2.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site, self.site2)
        self.assertNotEqual(site, self.site)
        d = self.propertied_site.as_dict()
        site3 = PeriodicSite({"Si": 0.5, "Fe": 0.5}, [0, 0, 0], self.lattice)
        d = site3.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species_and_occu, site3.species_and_occu)

        d = self.dummy_site.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species_and_occu,
                         self.dummy_site.species_and_occu)

    def test_to_unit_cell(self):
        site = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        site = site.to_unit_cell
        val = [0.25, 0.35, 0.46]
        self.assertArrayAlmostEqual(site.frac_coords, val)
Ejemplo n.º 7
0
def rediscovery_byRad_kdTree_onlyVertex(stru, migrate, vorosites, vororad, threshold = 0.5):
    recover_labels = []
    recover_state = {}
    migrate_mindis = {}

    # print(stru)
    # print("the len of stru.sites:", len(stru.sites))
    migrate_pos_frac = np.around(np.array([site.frac_coords for site in stru.sites if migrate in site._atom_site_label], ndmin=2), 3)
    migrate_pos_frac %= 1.0
    migrate_pos_frac %= 1.0
    expand_pos_frac = migrate_pos_frac
    # expand the migrant sites to 3*3*3
    for a in range(-1, 2):
        for b in range(-1, 2):
            for c in range(-1, 2):
                if a==b==c==0:
                    continue
                else:
                    expand_pos_frac = np.concatenate((expand_pos_frac,migrate_pos_frac+np.array([a,b,c])),axis=0)

    # print(migrate_pos_frac)
    # migrate_pos_frac %= 1.0
    # migrate_pos_frac %= 1.0
    # print(migrate_pos_frac)
    # migrate_pos = [site.coords for site in stru.sites if migrate in site._atom_site_label]
    # print(migrate_pos)
    migrate_labels = [site._atom_site_label for site in stru.sites if migrate in site._atom_site_label]
    
    points = np.around(np.array(vorosites[0], ndmin=2), 3)
    points_rad = np.array(vororad[0])
    
    points %= 1.0
    points %= 1.0
    # print(points)
    # print(len(points))
    vorositesKdTree = cKDTree(points)
    min_dis,min_ids = vorositesKdTree.query(migrate_pos_frac,k=1)
    # print(labels)
    # print(min_dis)

    pts_len = [len(vorosites[0])]
    for idx in range(len(migrate_labels)):
        if migrate_labels[idx] in recover_labels:
            continue
        tmp_site1 = PeriodicSite("Ar", migrate_pos_frac[idx], stru.lattice)
        tmp_site2 = PeriodicSite("Ar", points[min_ids[idx]], stru.lattice)
        
        pt_tag = get_point_tag_onlyVertex(min_ids[idx], pts_len)
        pt_rad = points_rad[min_ids[idx]]
        dis_st1_st2 = tmp_site1.distance(tmp_site2)
        migrate_mindis[str(migrate_labels[idx])] = (pt_tag, pt_rad, dis_st1_st2)
        
        # if dis_st1_st2 <= threshold or dis_st1_st2 <= pt_rad:
        if dis_st1_st2 <= threshold:
            recover_state[str(migrate_labels[idx])] = pt_tag
            recover_labels.append(migrate_labels[idx])
        else:
            recover_state[str(migrate_labels[idx])] = None

    recover_rate = len(recover_labels) / len(np.unique(migrate_labels))
    return recover_rate, recover_state, migrate_mindis
Ejemplo n.º 8
0
class PeriodicSiteTest(PymatgenTest):

    def setUp(self):
        self.lattice = Lattice.cubic(10.0)
        self.si = Element("Si")
        self.site = PeriodicSite("Fe", [0.25, 0.35, 0.45],
                                 self.lattice)
        self.site2 = PeriodicSite({"Si": 0.5}, [0, 0, 0], self.lattice)
        self.assertEqual(self.site2.species_and_occu,
                         Composition({Element('Si'): 0.5}),
                         "Inconsistent site created!")
        self.propertied_site = PeriodicSite(Specie("Fe", 2),
                                            [0.25, 0.35, 0.45],
                                            self.lattice,
                                            properties={'magmom': 5.1,
                                                        'charge': 4.2})
        self.dummy_site = PeriodicSite("X", [0, 0, 0], self.lattice)

    def test_properties(self):
        """
        Test the properties for a site
        """
        self.assertEqual(self.site.a, 0.25)
        self.assertEqual(self.site.b, 0.35)
        self.assertEqual(self.site.c, 0.45)
        self.assertEqual(self.site.x, 2.5)
        self.assertEqual(self.site.y, 3.5)
        self.assertEqual(self.site.z, 4.5)
        self.assertTrue(self.site.is_ordered)
        self.assertFalse(self.site2.is_ordered)
        self.assertEqual(self.propertied_site.magmom, 5.1)
        self.assertEqual(self.propertied_site.charge, 4.2)

    def test_distance(self):
        other_site = PeriodicSite("Fe", np.array([0, 0, 0]), self.lattice)
        self.assertAlmostEqual(self.site.distance(other_site), 6.22494979899,
                                5)

    def test_distance_from_point(self):
        self.assertNotAlmostEqual(self.site.distance_from_point([0.1, 0.1,
                                                                 0.1]),
                                  6.22494979899, 5)
        self.assertAlmostEqual(self.site.distance_from_point([0.1, 0.1, 0.1]),
                               6.0564015718906887, 5)

    def test_distance_and_image(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        (distance, image) = self.site.distance_and_image(other_site)
        self.assertAlmostEqual(distance, 6.22494979899, 5)
        self.assertTrue(([-1, -1, -1] == image).all())
        (distance, image) = self.site.distance_and_image(other_site, [1, 0, 0])
        self.assertAlmostEqual(distance, 19.461500456028563, 5)
        # Test that old and new distance algo give the same ans for
        # "standard lattices"
        lattice = Lattice(np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertAlmostEqual(get_distance_and_image_old(site1, site2)[0],
                               site1.distance_and_image(site2)[0])
        lattice = Lattice.from_parameters(1, 0.01, 1, 10, 10, 10)
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertTrue(get_distance_and_image_old(site1, site2)[0] >
                        site1.distance_and_image(site2)[0])
        site2 = PeriodicSite("Fe", np.random.rand(3), lattice)
        (dist_old, jimage_old) = get_distance_and_image_old(site1, site2)
        (dist_new, jimage_new) = site1.distance_and_image(site2)
        self.assertTrue(dist_old - dist_new > -1e-8,
                        "New distance algo should give smaller answers!")
        self.assertFalse((abs(dist_old - dist_new) < 1e-8) ^
                         (jimage_old == jimage_new).all(),
                         "If old dist == new dist, images must be the same!")
        latt = Lattice.from_parameters(3.0, 3.1, 10.0, 2.96, 2.0, 1.0)
        site = PeriodicSite("Fe", [0.1, 0.1, 0.1], latt)
        site2 = PeriodicSite("Fe", [0.99, 0.99, 0.99], latt)
        (dist, img) = site.distance_and_image(site2)
        self.assertAlmostEqual(dist, 0.15495358379511573)
        self.assertEqual(list(img), [-11, 6, 0])

    def test_is_periodic_image(self):
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]), self.lattice)
        self.assertTrue(self.site.is_periodic_image(other),
                        "This other site should be a periodic image.")
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        self.assertFalse(self.site.is_periodic_image(other),
                         "This other site should not be a periodic image.")
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]),
                             Lattice.rhombohedral(2, 60))
        self.assertFalse(self.site.is_periodic_image(other),
                         "Different lattices should not be periodic images.")

    def test_equality(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        self.assertTrue(self.site.__eq__(self.site))
        self.assertFalse(other_site.__eq__(self.site))
        self.assertFalse(self.site.__ne__(self.site))
        self.assertTrue(other_site.__ne__(self.site))

    def test_as_from_dict(self):
        d = self.site2.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site, self.site2)
        self.assertNotEqual(site, self.site)
        d = self.propertied_site.as_dict()
        site3 = PeriodicSite({"Si": 0.5, "Fe": 0.5}, [0, 0, 0], self.lattice)
        d = site3.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species_and_occu, site3.species_and_occu)

        d = self.dummy_site.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species_and_occu, self.dummy_site.species_and_occu)


    def test_to_unit_cell(self):
        site = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        site = site.to_unit_cell
        val = [0.25, 0.35, 0.46]
        self.assertArrayAlmostEqual(site.frac_coords, val)
Ejemplo n.º 9
0
class PeriodicSiteTest(PymatgenTest):
    def setUp(self):
        self.lattice = Lattice.cubic(10.0)
        self.si = Element("Si")
        self.site = PeriodicSite("Fe", [0.25, 0.35, 0.45], self.lattice)
        self.site2 = PeriodicSite({"Si": 0.5}, [0, 0, 0], self.lattice)
        self.assertEqual(
            self.site2.species,
            Composition({Element("Si"): 0.5}),
            "Inconsistent site created!",
        )
        self.propertied_site = PeriodicSite(
            Species("Fe", 2),
            [0.25, 0.35, 0.45],
            self.lattice,
            properties={"magmom": 5.1, "charge": 4.2},
        )
        self.dummy_site = PeriodicSite("X", [0, 0, 0], self.lattice)

    def test_properties(self):
        """
        Test the properties for a site
        """
        self.assertEqual(self.site.a, 0.25)
        self.assertEqual(self.site.b, 0.35)
        self.assertEqual(self.site.c, 0.45)
        self.assertEqual(self.site.x, 2.5)
        self.assertEqual(self.site.y, 3.5)
        self.assertEqual(self.site.z, 4.5)
        self.assertTrue(self.site.is_ordered)
        self.assertFalse(self.site2.is_ordered)
        self.assertEqual(self.propertied_site.properties["magmom"], 5.1)
        self.assertEqual(self.propertied_site.properties["charge"], 4.2)

    def test_distance(self):
        other_site = PeriodicSite("Fe", np.array([0, 0, 0]), self.lattice)
        self.assertAlmostEqual(self.site.distance(other_site), 6.22494979899, 5)

    def test_distance_from_point(self):
        self.assertNotAlmostEqual(self.site.distance_from_point([0.1, 0.1, 0.1]), 6.22494979899, 5)
        self.assertAlmostEqual(self.site.distance_from_point([0.1, 0.1, 0.1]), 6.0564015718906887, 5)

    def test_distance_and_image(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        (distance, image) = self.site.distance_and_image(other_site)
        self.assertAlmostEqual(distance, 6.22494979899, 5)
        self.assertTrue(([-1, -1, -1] == image).all())
        (distance, image) = self.site.distance_and_image(other_site, [1, 0, 0])
        self.assertAlmostEqual(distance, 19.461500456028563, 5)
        # Test that old and new distance algo give the same ans for
        # "standard lattices"
        lattice = Lattice(np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertAlmostEqual(
            get_distance_and_image_old(site1, site2)[0],
            site1.distance_and_image(site2)[0],
        )
        lattice = Lattice.from_parameters(1, 0.01, 1, 10, 10, 10)
        site1 = PeriodicSite("Fe", np.array([0.01, 0.02, 0.03]), lattice)
        site2 = PeriodicSite("Fe", np.array([0.99, 0.98, 0.97]), lattice)
        self.assertTrue(get_distance_and_image_old(site1, site2)[0] > site1.distance_and_image(site2)[0])
        site2 = PeriodicSite("Fe", np.random.rand(3), lattice)
        (dist_old, jimage_old) = get_distance_and_image_old(site1, site2)
        (dist_new, jimage_new) = site1.distance_and_image(site2)
        self.assertTrue(
            dist_old - dist_new > -1e-8,
            "New distance algo should give smaller answers!",
        )
        self.assertFalse(
            (abs(dist_old - dist_new) < 1e-8) ^ (jimage_old == jimage_new).all(),
            "If old dist == new dist, images must be the same!",
        )
        latt = Lattice.from_parameters(3.0, 3.1, 10.0, 2.96, 2.0, 1.0)
        site = PeriodicSite("Fe", [0.1, 0.1, 0.1], latt)
        site2 = PeriodicSite("Fe", [0.99, 0.99, 0.99], latt)
        (dist, img) = site.distance_and_image(site2)
        self.assertAlmostEqual(dist, 0.15495358379511573)
        self.assertEqual(list(img), [-11, 6, 0])

    def test_is_periodic_image(self):
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]), self.lattice)
        self.assertTrue(
            self.site.is_periodic_image(other),
            "This other site should be a periodic image.",
        )
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        self.assertFalse(
            self.site.is_periodic_image(other),
            "This other site should not be a periodic image.",
        )
        other = PeriodicSite("Fe", np.array([1.25, 2.35, 4.45]), Lattice.rhombohedral(2, 60))
        self.assertFalse(
            self.site.is_periodic_image(other),
            "Different lattices should not be periodic images.",
        )

    def test_equality(self):
        other_site = PeriodicSite("Fe", np.array([1, 1, 1]), self.lattice)
        self.assertTrue(self.site.__eq__(self.site))
        self.assertFalse(other_site.__eq__(self.site))
        self.assertFalse(self.site.__ne__(self.site))
        self.assertTrue(other_site.__ne__(self.site))

    def test_as_from_dict(self):
        d = self.site2.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site, self.site2)
        self.assertNotEqual(site, self.site)
        d = self.propertied_site.as_dict()
        site3 = PeriodicSite({"Si": 0.5, "Fe": 0.5}, [0, 0, 0], self.lattice)
        d = site3.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species, site3.species)

        d = self.dummy_site.as_dict()
        site = PeriodicSite.from_dict(d)
        self.assertEqual(site.species, self.dummy_site.species)

    def test_to_unit_cell(self):
        site = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), self.lattice)
        site.to_unit_cell(in_place=True)
        val = [0.25, 0.35, 0.46]
        self.assertArrayAlmostEqual(site.frac_coords, val)

        lattice_pbc = Lattice(self.lattice.matrix, pbc=(True, True, False))
        site = PeriodicSite("Fe", np.array([1.25, 2.35, 4.46]), lattice_pbc)
        site.to_unit_cell(in_place=True)
        val = [0.25, 0.35, 4.46]
        self.assertArrayAlmostEqual(site.frac_coords, val)

    def test_setters(self):
        site = self.propertied_site
        site.species = "Cu"
        self.assertEqual(site.species, Composition("Cu"))
        site.x = 1.25
        site.y = 1.35
        self.assertEqual(site.coords[0], 1.25)
        self.assertEqual(site.coords[1], 1.35)
        self.assertEqual(site.a, 0.125)
        self.assertEqual(site.b, 0.135)
        site.lattice = Lattice.cubic(100)
        self.assertEqual(site.x, 12.5)

        def set_bad_species():
            site.species = {"Cu": 0.5, "Gd": 0.6}

        self.assertRaises(ValueError, set_bad_species)

        site.frac_coords = [0, 0, 0.1]
        self.assertArrayAlmostEqual(site.coords, [0, 0, 10])
        site.coords = [1.5, 3.25, 5]
        self.assertArrayAlmostEqual(site.frac_coords, [0.015, 0.0325, 0.05])

    def test_repr(self):
        self.assertEqual(
            self.propertied_site.__repr__(),
            "PeriodicSite: Fe2+ (2.5000, 3.5000, 4.5000) [0.2500, 0.3500, 0.4500]",
        )
Ejemplo n.º 10
0
def get_dis(struc, p1, p2):
    tmp_siteA = PeriodicSite("Ar", p1, struc.lattice)
    tmp_siteB = PeriodicSite("Ar", p2, struc.lattice)
    return tmp_siteA.distance(tmp_siteB)