Esempio n. 1
0
    def test_connectivity_array(self):
        vc = VoronoiConnectivity(self.get_structure("LiFePO4"))
        ca = vc.connectivity_array
        expected = np.array([0, 1.96338392, 0, 0.04594495])
        self.assertTrue(np.allclose(ca[15, :4, ca.shape[2] // 2], expected))

        expected = np.array([0, 0, 0])
        self.assertTrue(np.allclose(ca[1, -3:, 51], expected))

        site = vc.get_sitej(27, 51)
        self.assertEqual(site.specie, Element("O"))
        expected = np.array([-0.29158, 0.74889, 0.95684])
        self.assertTrue(np.allclose(site.frac_coords, expected))
Esempio n. 2
0
    def test_connectivity_array(self):
        vc = VoronoiConnectivity(self.get_structure("LiFePO4"))
        ca = vc.connectivity_array
        expected = np.array([0, 1.96338392, 0, 0.04594495])
        self.assertTrue(np.allclose(ca[15, :4, ca.shape[2] // 2], expected))

        expected = np.array([0, 0, 0])
        self.assertTrue(np.allclose(ca[1, -3:, 51], expected))

        site = vc.get_sitej(27, 51)
        self.assertEqual(site.specie, Element('O'))
        expected = np.array([-0.29158, 0.74889, 0.95684])
        self.assertTrue(np.allclose(site.frac_coords, expected))
Esempio n. 3
0
    def test_connectivity_array(self):
        vc = VoronoiConnectivity(self.get_structure("LiFePO4"))
        ca = vc.connectivity_array
        np.set_printoptions(threshold=np.NAN, linewidth=np.NAN, suppress=np.NAN)

        expected = np.array([0, 1.96338392, 0, 0.04594495])
        self.assertTrue(np.allclose(ca[15, :4, ca.shape[2] // 2], expected))

        expected = np.array([0, 0, 0])
        self.assertTrue(np.allclose(ca[1, -3:, 51], expected))

        site = vc.get_sitej(27, 51)
        self.assertEqual(site.specie, Element('O'))
        expected = np.array([-0.29158, 0.74889, 0.95684])
        self.assertTrue(np.allclose(site.frac_coords, expected))
Esempio n. 4
0
 def test_connectivity_array(self):
     vc = VoronoiConnectivity(self.s)
     ca = vc.connectivity_array
     np.set_printoptions(threshold = np.NAN, linewidth = np.NAN, suppress = np.NAN)
     
     expected = np.array([0, 1.96338392, 0, 0.04594495])
     self.assertTrue(np.allclose(ca[15, :4, ca.shape[2] // 2], expected))
     
     expected = np.array([0, 2.04147512, 2.12118181])
     self.assertTrue(np.allclose(ca[1, -3:, 51], expected))
     
     site = vc.get_sitej(27, 51)
     self.assertEqual(site.specie, Element('O'))
     expected = np.array([-0.04316, 0.25111, 0.29158])
     self.assertTrue(np.allclose(site.frac_coords, expected))
Esempio n. 5
0
    def voronoi_Nonperiodic(self, structure):
        """
        VoronoiConnectivity().get_connections()

        input:
        structure - (Structure)
        cutoff - (float)

        return:
        A list of site pairs that are Voronoi Neighbours,
        along with their real-space distances.

        The shape of a list is N×3, where N is the number of pairs.
        e.g. [[0, 1, 3.9710929317605546], [0, 2, 3.971092931760555],
              [0, 3, 3.971092931760555], ...]
        """
        n_atoms = len(structure)
        connections = VoronoiConnectivity(structure, cutoff=self.cutoff).get_connections()
        adjacency_matrix = np.zeros((n_atoms, n_atoms))

        # compute adjacency matrix
        for n in range(len(connections)):
            site_i = connections[n][0]
            site_j = connections[n][1]
            adjacency_matrix[site_i, site_j] += 1

        # compute squared distance matrix
        squared_distance_matrix = structure.distance_matrix ** 2

        warnings.filterwarnings("ignore") # ignore RuntimeWarning (division by zero)
        reciprocal_squared_distance_matrix = squared_distance_matrix ** -1
        reciprocal_squared_distance_matrix[
                            reciprocal_squared_distance_matrix == np.inf] = 0

        return (adjacency_matrix, reciprocal_squared_distance_matrix)
    def test_connectivity_array(self):
        vc = VoronoiConnectivity(self.s)
        ca = vc.connectivity_array
        np.set_printoptions(threshold=np.NAN,
                            linewidth=np.NAN,
                            suppress=np.NAN)

        expected = np.array([0, 1.96338392, 0, 0.04594495])
        self.assertTrue(np.allclose(ca[15, :4, ca.shape[2] // 2], expected))

        expected = np.array([0, 2.04147512, 2.12118181])
        self.assertTrue(np.allclose(ca[1, -3:, 51], expected))

        site = vc.get_sitej(27, 51)
        self.assertEqual(site.specie, Element('O'))
        expected = np.array([-0.04316, 0.25111, 0.29158])
        self.assertTrue(np.allclose(site.frac_coords, expected))
Esempio n. 7
0
    def feas(self, image):
        
        # Returns pymatgen structure from ASE Atoms.
        try:
            image = AseAtomsAdaptor.get_structure(image)
        except:
            image = image
        
        # atom feature vector
        atom_fea = np.array([self.dict_atom_fea[i] 
                             for i in image.atomic_numbers])

        # VoronoiConnectivity for bond feature vector
        VC = VoronoiConnectivity(image)
        
        all_nbrs = []
        
        # add connectivity for neighbor atoms distances
        conn = VC.connectivity_array
        
        for ii in range(0, conn.shape[0]):
            curnbr = []
            for jj in range(0, conn.shape[1]):
                for kk in range(0, conn.shape[2]):
                    if conn[ii][jj][kk] != 0:
                        curnbr.append([ii, conn[ii][jj][kk]
                                       / np.max(conn[ii]), jj])
                    else:
                        curnbr.append([ii, 0.0, jj])
            all_nbrs.append(np.array(curnbr))
            
        all_nbrs = [sorted(nbrs, key=lambda x: x[1], reverse=True) 
                    for nbrs in all_nbrs]
        
        nbr_fea_idx = np.array([list(map(lambda x: x[2],
                                         nbr[:self.max_num_nbr]))
                                         for nbr in all_nbrs], dtype=np.int64)
        
        nbr_fea = np.array([list(map(lambda x: x[1], nbr[:self.max_num_nbr]))
                            for nbr in all_nbrs])

        nbr_fea = np.exp(-(nbr_fea[..., np.newaxis] - self.filter)**2
                         / self.step**2)
        
        return atom_fea, nbr_fea, nbr_fea_idx
Esempio n. 8
0
    def __getitem__(self, idx):
        cif_id, target = self.id_prop_data[idx]
        crystal = Structure.from_file(
            os.path.join(self.root_dir, cif_id + '.cif'))
        atom_fea = np.vstack([
            self.ari.get_atom_fea(crystal[i].specie.number)
            for i in range(len(crystal))
        ])
        atom_fea = torch.Tensor(atom_fea)
        #         all_nbrs = crystal.get_all_neighbors(self.radius, include_index=True)

        VC = VoronoiConnectivity(crystal)
        all_nbrs = []
        # add connectivity for neighbor atoms distances
        conn = VC.connectivity_array
        for ii in range(0, conn.shape[0]):
            curnbr = []
            for jj in range(0, conn.shape[1]):
                for kk in range(0, conn.shape[2]):
                    if conn[ii][jj][kk] != 0:
                        curnbr.append(
                            [ii, conn[ii][jj][kk] / np.max(conn[ii]), jj])
                        #curnbr.append([ii, conn[ii][jj][kk], jj])
                    else:
                        curnbr.append([ii, 0.0, jj])
            all_nbrs.append(np.array(curnbr))
        all_nbrs = [
            sorted(nbrs, key=lambda x: x[1], reverse=True) for nbrs in all_nbrs
        ]

        nbr_fea_idx = np.array([
            list(map(lambda x: x[2], nbr[:self.max_num_nbr]))
            for nbr in all_nbrs
        ])
        nbr_fea = np.array([
            list(map(lambda x: x[1], nbr[:self.max_num_nbr]))
            for nbr in all_nbrs
        ])
        nbr_fea = self.gdf.expand(nbr_fea)
        atom_fea = torch.Tensor(atom_fea)
        nbr_fea = torch.Tensor(nbr_fea)
        nbr_fea_idx = torch.LongTensor(nbr_fea_idx)
        target = torch.Tensor([float(target)])
        return (atom_fea, nbr_fea, nbr_fea_idx), target, cif_id
Esempio n. 9
0
    def __getitem__(self, idx):
        atoms = copy.deepcopy(self.atoms_list[idx])
        crystal = AseAtomsAdaptor.get_structure(atoms)
        atoms_initial_config = copy.deepcopy(
            self.atoms_list_initial_config[idx])
        crystal_initial_config = AseAtomsAdaptor.get_structure(
            atoms_initial_config)

        #Stack the features from atom_init
        atom_fea = np.vstack([
            self.ari.get_atom_fea(crystal[i].specie.number)
            for i in range(len(crystal))
        ])

        # If use_tag=True, then add the tag as an atom feature
        if self.use_tag:
            atom_fea = np.hstack([atom_fea, atoms.get_tags().reshape((-1, 1))])

        # If use_fixed_info=True, then add whether the atom is fixed by ASE constraint to the features
        if self.use_fixed_info:
            fix_loc, = np.where([
                type(constraint) == FixAtoms
                for constraint in atoms.constraints
            ])
            fix_atoms_indices = set(
                atoms.constraints[fix_loc[0]].get_indices())
            fixed_atoms = np.array(
                [i in fix_atoms_indices for i in range(len(atoms))]).reshape(
                    (-1, 1))
            atom_fea = np.hstack([atom_fea, fixed_atoms])

        # If use_voronoi, then use the voronoi connectivity from pymatgen to determine neighbors and distances
        if self.use_voronoi:

            #Get the connectivity array for the initial and final structure
            VC = VoronoiConnectivity(crystal)
            VC_initial_config = VoronoiConnectivity(crystal_initial_config)
            conn = copy.deepcopy(VC.connectivity_array)
            conn_initial_config = copy.deepcopy(
                VC_initial_config.connectivity_array)

            #Iterate through each atom, find it's neighbors, and add their distances
            all_nbrs = []

            # Loop over central atom
            for ii in range(0, conn.shape[0]):
                curnbr = []

                #Loop over neighbor atoms
                for jj in range(0, conn.shape[1]):

                    #Loop over each possible PBC image for the chosen image
                    for kk in range(0, conn.shape[2]):
                        # Only add as a neighbor if the atom is not the currently selected center one and there is connectivity
                        # to that image
                        if jj is not kk and conn[ii][jj][kk] != 0:

                            #Add the neighbor strength depending on train_geometry base
                            if self.train_geometry == 'initial':
                                curnbr.append([
                                    ii, conn_initial_config[ii][jj][kk] /
                                    np.max(conn_initial_config[ii]), jj
                                ])
                            elif self.train_geometry == 'final':
                                curnbr.append([
                                    ii, conn[ii][jj][kk] / np.max(conn[ii]), jj
                                ])
                            elif self.train_geometry == 'final-adsorbate':
                                #In order for this to work, each adsorbate atom should be set to tag==1 in the atoms object
                                if (atoms.get_tags()[ii] == 1
                                        or atoms.get_tags()[jj] == 1):
                                    if conn[ii][jj][kk] / np.max(
                                            conn[ii]) > 0.3:
                                        curnbr.append([ii, 1.0, jj])
                                    else:
                                        curnbr.append([ii, 0.0, jj])
                                else:
                                    curnbr.append([
                                        ii, conn_initial_config[ii][jj][kk] /
                                        np.max(conn_initial_config[ii]), jj
                                    ])
                            else:
                                curnbr.append([
                                    ii, conn[ii][jj][kk] / np.max(conn[ii]), jj
                                ])
                        else:
                            curnbr.append([ii, 0.0, jj])
                all_nbrs.append(np.array(curnbr))

            # If use_distance=True, then add the distance to an adsorbate (tag=1) as a feature
            if self.use_distance:
                atom_fea = np.hstack(
                    [atom_fea,
                     distance_to_adsorbate_feature(atoms, VC)])

            #Find the strongest neighbors for each atom
            all_nbrs = np.array(all_nbrs)
            all_nbrs = [
                sorted(nbrs, key=lambda x: x[1], reverse=True)
                for nbrs in all_nbrs
            ]
            nbr_fea_idx = np.array([
                list(map(lambda x: x[2], nbr[:self.max_num_nbr]))
                for nbr in all_nbrs
            ])
            nbr_fea = np.array([
                list(map(lambda x: x[1], nbr[:self.max_num_nbr]))
                for nbr in all_nbrs
            ])

            #expand distance one-hot encoding with GDF
            nbr_fea = self.gdf.expand(nbr_fea)
        else:
            all_nbrs = crystal.get_all_neighbors(self.radius,
                                                 include_index=True)
            all_nbrs = [sorted(nbrs, key=lambda x: x[1]) for nbrs in all_nbrs]
            nbr_fea_idx, nbr_fea = [], []
            for nbr in all_nbrs:
                if len(nbr) < self.max_num_nbr:
                    nbr_fea_idx.append(
                        list(map(lambda x: x[2], nbr)) + [0] *
                        (self.max_num_nbr - len(nbr)))
                    nbr_fea.append(
                        list(map(lambda x: x[1], nbr)) + [self.radius + 1.] *
                        (self.max_num_nbr - len(nbr)))
                else:
                    nbr_fea_idx.append(
                        list(map(lambda x: x[2], nbr[:self.max_num_nbr])))
                    nbr_fea.append(
                        list(map(lambda x: x[1], nbr[:self.max_num_nbr])))
            nbr_fea_idx, nbr_fea = np.array(nbr_fea_idx), np.array(nbr_fea)
            nbr_fea = self.gdf.expand(nbr_fea)

        try:
            nbr_fea = torch.Tensor(nbr_fea)
        except RuntimeError:
            print(nbr_fea)

        nbr_fea_idx = torch.LongTensor(nbr_fea_idx)
        atom_fea = torch.Tensor(atom_fea)

        return (atom_fea, nbr_fea, nbr_fea_idx)
Voronoi_Analyzer = VoronoiAnalyzer()
anay = Voronoi_Analyzer.analyze(s1, n=0)
strucs = [s1, s2]
anays = Voronoi_Analyzer.analyze_structures(strucs)
print("Voronoi_Analyzer.analyze(s1,n=0):", anay)
#plt = Voronoi_Analyzer.plot_vor_analysis(anays)
relax = RelaxationAnalyzer(s1, s2)
volume_change = relax.get_percentage_volume_change()
lattice_parameter_changes = relax.get_percentage_lattice_parameter_changes()
print('initial volume:', s1.volume)
print('final volume:', s2.volume)
print("percent_volume_change:", volume_change)
print("percent_lattice_change:", lattice_parameter_changes)
bond_dist = relax.get_percentage_bond_dist_changes(max_radius=6)
print("percent_bond_distance_change:", bond_dist)
connec = VoronoiConnectivity(s2)
#print("connec.get_connections():",connec.get_connections())

#A_222_dopant = [0]
A_222_dopant_neighbour = [[69, 73], [68, 72], [63, 65], [62, 64], [64, 65],
                          [62, 63], [72, 73], [68, 69]]
A_222_dopant_neighbour_MA_I_a = [[69, 73], [68, 72], [59, 61], [58, 60],
                                 [60, 61], [58, 59], [68, 69], [72, 73]]
A_222_dopant_neighbour_MA_I_b = [[67, 71], [66, 70], [63, 65], [62, 64],
                                 [70, 71], [66, 67], [62, 63], [64, 65]]
A_222_dopant_neighbour_MA_I_c = [[69, 73], [68, 72], [62, 64], [63, 65],
                                 [72, 73], [68, 69], [64, 65], [62, 63]]

###Calculate dopant bond length
dopant_bond_a = 0
for i in range(2):
Esempio n. 11
0
def list_maximum_connectivity(list_struc):
    return [
        VoronoiConnectivity(structure, cutoff=10).max_connectivity
        for structure in list_struc
    ]
Esempio n. 12
0
def list_Voronoi_Neighbors(list_struc):
    return [
        VoronoiConnectivity(structure, cutoff=10).get_connections()
        for structure in list_struc
    ]
Esempio n. 13
0
def list_the_solid_angle_of_polygon_between_atomi_and_imagej_of_atomj(
        list_struc):
    return [
        VoronoiConnectivity(structure, cutoff=10).connectivity_array
        for structure in list_struc
    ]
coordinate_sites = Voronoi.get_coordinated_sites(1)
Voronoi_Analyzer = VoronoiAnalyzer()
anay = Voronoi_Analyzer.analyze(s1,n=0)
strucs = [s1,s2]
anays = Voronoi_Analyzer.analyze_structures(strucs)
print("Voronoi_Analyzer.analyze(s1,n=0):",anay)
#plt = Voronoi_Analyzer.plot_vor_analysis(anays)
relax = RelaxationAnalyzer(s1,s2)
volume_change = relax.get_percentage_volume_change()
lattice_parameter_changes = relax.get_percentage_lattice_parameter_changes()
print('initial volume:',s1.volume)
print('final volume:',s2.volume)
print("percent_volume_change:",volume_change)
print("percent_lattice_change:",lattice_parameter_changes)
bond_dist = relax.get_percentage_bond_dist_changes()
connec = VoronoiConnectivity(s2)

B_233_dopant = [0]
B_233_dopant_neighbour = [144,153,162,165,180,181]
B_233_dopant_neighbour_Pb = [117,111,114,109,110]
B_233_dopant_neighbour_Pb_I =[[144,153,171,174,189,190],[147,156,165,168,183,184],[150,159,162,168,186,187],[145,154,163,166,181,182],[146,155,164,167,180,182]]
B_233 = zip(B_233_dopant_neighbour_Pb,B_233_dopant_neighbour_Pb_I)


### To determine the dopant and its neighbour I pair and bond distance 
comb1 = []
for i in B_233_dopant:
    for j in B_233_dopant_neighbour:
        pair = []
        pair.append(i)
        pair.append(j)