def complete_ordering(self, structure, num_remove_dict): self.logger.debug("Performing complete ordering...") all_structures = [] from pymatgen.symmetry.finder import SymmetryFinder symprec = 0.2 s = SymmetryFinder(structure, symprec=symprec) self.logger.debug("Symmetry of structure is determined to be {}." .format(s.get_spacegroup_symbol())) sg = s.get_spacegroup() tested_sites = [] starttime = time.time() self.logger.debug("Performing initial ewald sum...") ewaldsum = EwaldSummation(structure) self.logger.debug("Ewald sum took {} seconds." .format(time.time() - starttime)) starttime = time.time() allcombis = [] for ind, num in num_remove_dict.items(): allcombis.append(itertools.combinations(ind, num)) count = 0 for allindices in itertools.product(*allcombis): sites_to_remove = [] indices_list = [] for indices in allindices: sites_to_remove.extend([structure[i] for i in indices]) indices_list.extend(indices) mod = StructureEditor(structure) mod.delete_sites(indices_list) s_new = mod.modified_structure energy = ewaldsum.compute_partial_energy(indices_list) already_tested = False for i, tsites in enumerate(tested_sites): tenergy = all_structures[i]["energy"] if abs((energy - tenergy) / len(s_new)) < 1e-5 and \ sg.are_symmetrically_equivalent(sites_to_remove, tsites, symm_prec=symprec): already_tested = True if not already_tested: tested_sites.append(sites_to_remove) all_structures.append({"structure": s_new, "energy": energy}) count += 1 if count % 10 == 0: timenow = time.time() self.logger.debug("{} structures, {:.2f} seconds." .format(count, timenow - starttime)) self.logger.debug("Average time per combi = {} seconds" .format((timenow - starttime) / count)) self.logger.debug("{} symmetrically distinct structures found." .format(len(all_structures))) self.logger.debug("Total symmetrically distinct structures found = {}" .format(len(all_structures))) all_structures = sorted(all_structures, key=lambda s: s["energy"]) return all_structures
def fast_ordering(self, structure, num_remove_dict, num_to_return=1): """ This method uses the matrix form of ewaldsum to calculate the ewald sums of the potential structures. This is on the order of 4 orders of magnitude faster when there are large numbers of permutations to consider. There are further optimizations possible (doing a smarter search of permutations for example), but this wont make a difference until the number of permutations is on the order of 30,000. """ self.logger.debug("Performing fast ordering") starttime = time.time() self.logger.debug("Performing initial ewald sum...") ewaldmatrix = EwaldSummation(structure).total_energy_matrix self.logger.debug("Ewald sum took {} seconds." .format(time.time() - starttime)) starttime = time.time() m_list = [] for indices, num in num_remove_dict.items(): m_list.append([0, num, list(indices), None]) self.logger.debug("Calling EwaldMinimizer...") minimizer = EwaldMinimizer(ewaldmatrix, m_list, num_to_return, PartialRemoveSitesTransformation.ALGO_FAST) self.logger.debug("Minimizing Ewald took {} seconds." .format(time.time() - starttime)) all_structures = [] lowest_energy = minimizer.output_lists[0][0] num_atoms = sum(structure.composition.values()) for output in minimizer.output_lists: se = StructureEditor(structure) del_indices = [] for manipulation in output[1]: if manipulation[1] is None: del_indices.append(manipulation[0]) else: se.replace_site(manipulation[0], manipulation[1]) se.delete_sites(del_indices) struct = se.modified_structure.get_sorted_structure() all_structures.append({"energy": output[0], "energy_above_minimum": (output[0] - lowest_energy) / num_atoms, "structure": struct}) return all_structures
def best_first_ordering(self, structure, num_remove_dict): self.logger.debug("Performing best first ordering") starttime = time.time() self.logger.debug("Performing initial ewald sum...") ewaldsum = EwaldSummation(structure) self.logger.debug("Ewald sum took {} seconds." .format(time.time() - starttime)) starttime = time.time() ematrix = ewaldsum.total_energy_matrix to_delete = [] totalremovals = sum(num_remove_dict.values()) removed = {k: 0 for k in num_remove_dict.keys()} for i in xrange(totalremovals): maxindex = None maxe = float("-inf") maxindices = None for indices in num_remove_dict.keys(): if removed[indices] < num_remove_dict[indices]: for ind in indices: if ind not in to_delete: energy = sum(ematrix[:, ind]) + \ sum(ematrix[:, ind]) - ematrix[ind, ind] if energy > maxe: maxindex = ind maxe = energy maxindices = indices removed[maxindices] += 1 to_delete.append(maxindex) ematrix[:, maxindex] = 0 ematrix[maxindex, :] = 0 mod = StructureEditor(structure) mod.delete_sites(to_delete) self.logger.debug("Minimizing Ewald took {} seconds." .format(time.time() - starttime)) return [{"energy": sum(sum(ematrix)), "structure": mod.modified_structure.get_sorted_structure()}]
def apply_transformation(self, structure, return_ranked_list=False): """ For this transformation, the apply_transformation method will return only the ordered structure with the lowest Ewald energy, to be consistent with the method signature of the other transformations. However, all structures are stored in the all_structures attribute in the transformation object for easy access. Args: structure: Oxidation state decorated disordered structure to order return_ranked_list: Boolean stating whether or not multiple structures are returned. If return_ranked_list is a number, that number of structures is returned. Returns: Depending on returned_ranked list, either a transformed structure or a list of dictionaries, where each dictionary is of the form {"structure" = .... , "other_arguments"} the key "transformation" is reserved for the transformation that was actually applied to the structure. This transformation is parsed by the alchemy classes for generating a more specific transformation history. Any other information will be stored in the transformation_parameters dictionary in the transmuted structure class. """ try: num_to_return = int(return_ranked_list) except ValueError: num_to_return = 1 num_to_return = max(1, num_to_return) equivalent_sites = [] exemplars = [] #generate list of equivalent sites to order #equivalency is determined by sp_and_occu and symmetry #if symmetrized structure is true for i, site in enumerate(structure): if site.is_ordered: continue found = False for j, ex in enumerate(exemplars): sp = ex.species_and_occu if not site.species_and_occu.almost_equals(sp): continue if self._symmetrized: sym_equiv = structure.find_equivalent_sites(ex) sym_test = site in sym_equiv else: sym_test = True if sym_test: equivalent_sites[j].append(i) found = True if not found: equivalent_sites.append([i]) exemplars.append(site) #generate the list of manipulations and input structure se = StructureEditor(structure) m_list = [] for g in equivalent_sites: total_occupancy = sum([structure[i].species_and_occu for i in g], Composition()) total_occupancy = dict(total_occupancy.items()) #round total occupancy to possible values for k, v in total_occupancy.items(): if abs(v - round(v)) > 0.25: raise ValueError("Occupancy fractions not consistent " "with size of unit cell") total_occupancy[k] = int(round(v)) #start with an ordered structure initial_sp = max(total_occupancy.keys(), key=lambda x: abs(x.oxi_state)) for i in g: se.replace_site(i, initial_sp) #determine the manipulations for k, v in total_occupancy.items(): if k == initial_sp: continue m = [k.oxi_state / initial_sp.oxi_state, v, list(g), k] m_list.append(m) #determine the number of empty sites empty = len(g) - sum(total_occupancy.values()) if empty > 0.5: m_list.append([0, empty, list(g), None]) structure = se.modified_structure matrix = EwaldSummation(structure).total_energy_matrix ewald_m = EwaldMinimizer(matrix, m_list, num_to_return, self._algo) self._all_structures = [] lowest_energy = ewald_m.output_lists[0][0] num_atoms = sum(structure.composition.values()) for output in ewald_m.output_lists: se = StructureEditor(structure) # do deletions afterwards because they screw up the indices of the # structure del_indices = [] for manipulation in output[1]: if manipulation[1] is None: del_indices.append(manipulation[0]) else: se.replace_site(manipulation[0], manipulation[1]) se.delete_sites(del_indices) self._all_structures.append( {"energy": output[0], "energy_above_minimum": (output[0] - lowest_energy) / num_atoms, "structure": se.modified_structure.get_sorted_structure()}) if return_ranked_list: return self._all_structures else: return self._all_structures[0]["structure"]
def apply_transformation(self, structure): editor = StructureEditor(structure) editor.delete_sites(self._indices) return editor.modified_structure
def apply_transformation(self, structure, return_ranked_list=False): """ For this transformation, the apply_transformation method will return only the ordered structure with the lowest Ewald energy, to be consistent with the method signature of the other transformations. However, all structures are stored in the all_structures attribute in the transformation object for easy access. Args: structure: Oxidation state decorated disordered structure to order return_ranked_list: Boolean stating whether or not multiple structures are returned. If return_ranked_list is a number, that number of structures is returned. Returns: Depending on returned_ranked list, either a transformed structure or a list of dictionaries, where each dictionary is of the form {'structure' = .... , 'other_arguments'} the key 'transformation' is reserved for the transformation that was actually applied to the structure. This transformation is parsed by the alchemy classes for generating a more specific transformation history. Any other information will be stored in the transformation_parameters dictionary in the transmuted structure class. """ ordered_sites = [] sites_to_order = {} try: num_to_return = int(return_ranked_list) except: num_to_return = 1 num_to_return = max(1, num_to_return) sites = list(structure.sites) for i in range(len(structure)): site = sites[i] if sum(site.species_and_occu.values()) == 1 and len(site.species_and_occu) == 1: ordered_sites.append(site) else: species = tuple([sp for sp, occu in site.species_and_occu.items()]) #group the sites by the list of species on that site for sp, occu in site.species_and_occu.items(): if species not in sites_to_order: sites_to_order[species] = {} if sp not in sites_to_order[species]: sites_to_order[species][sp] = [[occu, i]] else: sites_to_order[species][sp].append([occu, i]) total_occu = sum(site.species_and_occu.values()) #if the total occupancy on a site is less than one, add #a list with None as the species (for removal) if total_occu < 1: if None not in sites_to_order[species]: sites_to_order[species][None] = [[1 - total_occu, i]] else: sites_to_order[species][None].append([1 - total_occu, i]) """ Create a list of [multiplication fraction, number of replacements, [indices], replacement species] """ m_list = [] se = StructureEditor(structure) for species in sites_to_order.values(): initial_sp = None sorted_keys = sorted(species.keys(), key=lambda x: x is not None and -abs(x.oxi_state) or 1000) for sp in sorted_keys: if initial_sp is None: initial_sp = sp for site in species[sp]: se.replace_site(site[1], initial_sp) else: if sp is None: oxi = 0 else: oxi = float(sp.oxi_state) manipulation = [oxi / initial_sp.oxi_state, 0, [], sp] site_list = species[sp] site_list.sort(key=itemgetter(0)) prev_fraction = site_list[0][0] for site in site_list: if site[0] - prev_fraction > .1: """ tolerance for creating a new group of sites. if site occupancies are similar, they will be put in a group where the fraction has to be consistent over the whole. """ manipulation[1] = int(round(manipulation[1])) m_list.append(manipulation) manipulation = [oxi / initial_sp.oxi_state, 0, [], sp] prev_fraction = site[0] manipulation[1] += site[0] manipulation[2].append(site[1]) if abs(manipulation[1] - round(manipulation[1])) > .25: #if the # of atoms to remove isn't within .25 of an integer raise ValueError('Occupancy fractions not consistent with size of unit cell') manipulation[1] = int(round(manipulation[1])) m_list.append(manipulation) structure = se.modified_structure matrix = EwaldSummation(structure).total_energy_matrix ewald_m = EwaldMinimizer(matrix, m_list, num_to_return, self._algo) self._all_structures = [] lowest_energy = ewald_m.output_lists[0][0] num_atoms = sum(structure.composition.values()) for output in ewald_m.output_lists: se = StructureEditor(structure) del_indices = [] #do deletions afterwards because they screw up the indices of the structure for manipulation in output[1]: if manipulation[1] is None: del_indices.append(manipulation[0]) else: se.replace_site(manipulation[0], manipulation[1]) se.delete_sites(del_indices) self._all_structures.append({'energy':output[0], 'energy_above_minimum':(output[0] - lowest_energy) / num_atoms, 'structure': se.modified_structure.get_sorted_structure()}) if return_ranked_list: return self._all_structures else: return self._all_structures[0]['structure']