Source code for ase2sprkkr.sprkkr.sprkkr_atoms

"""This file contains SPRKKRAtoms - an enhanced version of Atoms to be used
with SPRKKR"""

from functools import lru_cache
from ase import Atoms
import numpy as np
from typing import List, Union
from .sites import Site
from .spacegroup_info import SpacegroupInfo


[docs] def _restore_promoted_ase_atoms(original_class, reconstruct, args): """Recreate a promoted Atoms subclass before pickle restores its state.""" obj = reconstruct(*args) obj.__class__ = _promoted_ase_atoms_class(original_class) return obj
[docs] def _reduce_promoted_ase_atoms(self, protocol): """Preserve the original subclass reducer while restoring the promoted type.""" promoted_class = type(self) original_class = promoted_class._sprkkr_original_class reduction = original_class.__reduce_ex__(self, protocol) if isinstance(reduction, str): raise TypeError( f"Can not pickle promoted {original_class.__name__}: " "the original class uses a global-name reducer" ) reconstruct, args, *rest = reduction if reconstruct is promoted_class: reconstruct = original_class if args and args[0] is promoted_class: args = (original_class,) + args[1:] return ( _restore_promoted_ase_atoms, (original_class, reconstruct, args), *rest, )
[docs] @lru_cache(maxsize=None) def _promoted_ase_atoms_class(original_class): """Return the unique SPRKKR-enabled class for an ASE Atoms subclass.""" if original_class is Atoms: return SPRKKRAtoms name = f"SPRKKR{original_class.__name__}" return type( name, (original_class, SPRKKRAtoms), { "__module__": __name__, "_sprkkr_original_class": original_class, "__reduce_ex__": _reduce_promoted_ase_atoms, }, )
[docs] class SPRKKRAtoms(Atoms): """ASE Atoms object extended by the data necessary for SPR-KKR calculations""" sites_array_name = "sprkkr_sites_data"
[docs] @staticmethod def promote_ase_atoms(obj, symmetry=None, update_info=None): """Promote an ASE atoms object for SPR-KKR calculations. The object is promoted in place by replacing its ``__class__`` with an SPRKKR-enabled subclass. Parameters ---------- obj : ase.Atoms Atoms to promote. symmetry : bool or None, optional If enabled, symmetry-equivalent atoms share a site type. For an unpromoted object, ``None`` behaves as ``True``; for an existing SPRKKRAtoms object, ``None`` retains its present site symmetry. update_info : bool or None, optional If ``True``, recompute space-group information; if ``False``, never recompute it. If ``None``, update it when site information has already been initialized. Returns ------- SPRKKRAtoms or None The promoted object, or ``None`` for a ``None`` input. """ if obj is None: return None if isinstance(obj, SPRKKRAtoms): if symmetry is not None: obj.compute_sites_symmetry(symmetry) else: if symmetry is None: symmetry = True if not isinstance(obj, Atoms): raise ValueError(f"Can not promote class {obj} of class {obj.__class__} to {SPRKKRAtoms}") obj.__class__ = _promoted_ase_atoms_class(obj.__class__) obj._init(symmetry=symmetry) # if Atoms are summed with SPRKKRAtoms, the content of sites # can be wrong if update_info is not False: if update_info or obj.are_sites_inited(): obj.spacegroup_info.recompute() return obj
[docs] def __init__(self, *args, potential=None, symmetry=True, **kwargs): """ Creates SPRKKRAtoms atoms Parameters ---------- *args: list The positionals arguments of ase.Atoms.__init__ symmetry: bool The symmetry will be computed when the sites property will be initialized. I.e., the by-symmetry-equal atomic sites will share the same sites object. **kwargs: dict The named arguments of ase.Atoms.__init__ """ super().__init__(*args, **kwargs) self._init(symmetry, potential)
[docs] def _init(self, symmetry=True, potential=None): """The initialization of the additional (not-in-ASE) properties. To be used by constructor and by promote_ase_atoms""" self._potential = potential self._regions = {} self.info["spacegroup_info"] = SpacegroupInfo(self, symmetry)
@property def regions(self): return self._regions
[docs] def add_region(self, region): """ Add a region of a given name """ self._regions[region.name] = region region.set_atoms(self, False) self.info["spacegroup_info"].recompute()
[docs] def remove_region(self, name): del self._regions[name]
[docs] def set_regions(self, regions: List): with self.info["spacegroup_info"].block_updating(): self._regions = {r.name: r for r in regions} for region in self._regions.values(): region.set_atoms(self)
@property def symmetry(self): """ Whether the sites property is/will be generated using symmetry, i.e. whether the Sites objects in the sites property will be shared among symmetric atomic sites. """ return self.info["spacegroup_info"].symmetry @symmetry.setter def symmetry(self, value): """ Recomputes the sites with enabled/disabled symmetry if the value of the property has changed. """ if self.spacegroup_info.symmetry != value: self.compute_sites_symmetry(value)
[docs] def update_sites_from_symbols(self): for number, site in zip(self.numbers, self.sites): site.update_atomic_number(number)
[docs] def compute_sites_symmetry( self, symmetry=None, consider_old=False, symmetry_precision=1e-5, angular_precision=1e-5 ): """SPRKKR has some properties shared by all by-symmetry-equal sites. This method can be called to recompute the equivalent sites. """ self.spacegroup_info.recompute(symmetry, consider_old, symmetry_precision, angular_precision) self.spacegroup_info.update_spacegroup_kinds()
[docs] def breaks_sites_symmetry(self, *args, **kwargs): """Cancel the use of symmetry in the structure, i.e., makes the Site object uniqe (not shared) for each atomic site. Calling this method is nearly equivalent to setting the symmetry property to False, however, this method always recompute the sites object, while setting symmetry=False recomputes the sites property only if it was previously set to True. """ self.compute_sites_symmetry(False, *args, **kwargs)
@property def sites(self): """The sites property holds all the information for the SPR-KKR package: atomic types (including number of semicore and valence electrons), occupancy, symmetries, meshes... Some of the properties are stored in the ASE atoms properties (e.g. occupancy, atomic symbol), however, ASE is not able to hold them all and/or to describe fully the SPR-KKR options; thus, these properties are hold in this array. The changes made on this array are reflected (as is possible) to the ASE properties, but the opposite does not hold - to reflect the changes in these properties please create a new Atoms object with given properties. """ if SPRKKRAtoms.sites_array_name not in self.arrays: self.spacegroup_info.recompute(init=True) return self.arrays[SPRKKRAtoms.sites_array_name] @sites.setter def sites(self, v): """Set the sites property and updatei/clear all the other dependent properties (symbols, occupancy, spacegroup_info) according to the sites.""" self.set_sites(v) @sites.deleter def sites(self): del self.arrays[SPRKKRAtoms.sites_array_name]
[docs] def are_sites_inited(self): return SPRKKRAtoms.sites_array_name in self.arrays
[docs] def set_sites( self, sites: np.ndarray, spacegroup_info: Union[SpacegroupInfo, bool, None] = None, update: bool = True ): """Set the sites property and update all other dependent properties (symbols, occupancy) according to the sites. Unlike ``sites`` setter, this method allow also set the spacegoup_info containing the computed informations about the symmetry. Parameters ---------- sites The array of the :class:`ase2sprkkr.sprkkr.sites.Site` objects. sg_info Information, about the symmetry. If None is given, no info is available, the symmetry will be determined if needed. If False, there is no symmetry. If True, retain the current symmetry. update Pass False to prevent updating spacegroup_kinds and occupancy """ an = np.zeros(len(sites), dtype=int) for i, j in enumerate(sites): an[i] = j.occupation.primary_atomic_number self.set_atomic_numbers(an) if spacegroup_info is True: # retain the current symmetry pass elif spacegroup_info is False: self.info["spacegroup_info"] = SpacegroupInfo(self, symmetry=False) elif spacegroup_info: self.info["spacegroup_info"] = spacegroup_info self.set_array(SPRKKRAtoms.sites_array_name, sites) if update: self.spacegroup_info.update_spacegroup_kinds()
@property def spacegroup_info(self): return self.info["spacegroup_info"] def __getitem__(self, i): out = super().__getitem__(i) if isinstance(out, Atoms) and "spacegroup_info" in out.info: out.info["spacegroup_info"] = out.info["spacegroup_info"].copy_for(out) if self.are_sites_inited(): out.info["spacegroup_info"].recompute(copy=True) return out @property def potential(self): if self._potential is None: self._potential = potentials.Potential.from_atoms(self) return self._potential
[docs] def has_potential(self): return self._potential is not None
[docs] def sprkkr_has_converged_data(self): """Return whether all atomic types contain restartable SCF data.""" if not self.are_sites_inited(): return False return all( atomic_type.has_converged_data() for site in self.sites for atomic_type in site.occupation.atomic_types() )
[docs] def sprkkr_is_scf_converged(self): """Return whether the SPR-KKR SCF state represented by these atoms is converged. If a Potential is attached, its SCF status is authoritative. Without an attached Potential, convergence is inferred from restartable data stored in the atomic types. The check does not create a Potential as a side effect. """ if self._potential is not None: return self._potential.is_scf_converged(self) return self.sprkkr_has_converged_data()
[docs] def sprkkr_reset_potential(self): """Reset all SPR-KKR potential data stored in the atoms.""" for i in self.sites: i.reset() if self._potential: self._potential.reset(update_atoms=False) self._potential.set_from_atoms()
[docs] def copy(self): out = super().copy() SPRKKRAtoms.promote_ase_atoms(out) if self.are_sites_inited(): out.set_sites(Site.copy_sites(out.sites, out), self.spacegroup_info.copy_for(out)) return out
[docs] def _promote_if_other(self, other): if self.are_sites_inited(): SPRKKRAtoms.promote_ase_atoms(other) other.sites elif isinstance(other, SPRKKRAtoms) and other.are_sites_inited(): self.sites
def __add__(self, other): return super().__add__(other) def __iadd__(self, other): return super().__iadd__(other)
[docs] def extend(self, other): self._promote_if_other(other) ln = len(self) super().extend(other) if "spacegroup_kinds" in self.arrays: sk = self.arrays["spacegroup_kinds"] # There is no existing kind to shift by when extending an empty # promoted Atoms object (for example for a zero-repeat region). shift = np.max(sk[:ln]) + 1 if ln else 0 sk[ln:] += shift if "occupancy" in self.info or "occupancy" in other.info: self.info.setdefault("occupancy", {}) self.info["occupancy"].update( {str(int(i) + shift): v for i, v in other.info.get("occupancy", {}).items()} ) self.info["spacegroup_info"]._dataset = None if self.are_sites_inited(): sites = self.sites for i in range(ln, len(self)): sites[i] = sites[i].copy(self)
# at the last - to avoid circular imports from ..potentials import potentials # NOQA: E402