def read_all_structures(self): """Return the list of structures at the different iteration steps.""" rprimd_list = self.read_value("rprimd") xred_list = self.read_value("xred") # Alchemical mixing is not supported. num_pseudos = self.read_dimvalue("npsp") ntypat = self.read_dimvalue("ntypat") if num_pseudos != ntypat: raise NotImplementedError("Alchemical mixing is not supported, num_pseudos != ntypat") znucl, typat = self.read_value("znucl"), self.read_value("typat").astype(int) #print(znucl.dtype, typat) cart_forces_step = self.read_cart_forces(unit="eV ang^-1") structures = [] #print("typat", type(typat)) for step in range(self.num_steps): s = Structure.from_abivars( xred=xred_list[step], rprim=rprimd_list[step], acell=3 * [1.0], # FIXME ntypat, typat, znucl are missing! znucl=znucl, typat=typat, ) s.add_site_property("cartesian_forces", cart_forces_step[step]) structures.append(s) return structures
def read_all_structures(self): """Return the list of structures at the different iteration steps.""" rprimd_list = self.read_value("rprimd") xred_list = self.read_value("xred") # Alchemical mixing is not supported. num_pseudos = self.read_dimvalue("npsp") ntypat = self.read_dimvalue("ntypat") if num_pseudos != ntypat: raise NotImplementedError( "Alchemical mixing is not supported, num_pseudos != ntypat") znucl, typat = self.read_value("znucl"), self.read_value( "typat").astype(int) #print(znucl.dtype, typat) cart_forces_step = self.read_cart_forces(unit="eV ang^-1") structures = [] #print("typat", type(typat)) for step in range(self.num_steps): s = Structure.from_abivars( xred=xred_list[step], rprim=rprimd_list[step], acell=3 * [1.0], # FIXME ntypat, typat, znucl are missing! znucl=znucl, typat=typat, ) s.add_site_property("cartesian_forces", cart_forces_step[step]) structures.append(s) return structures
def structure(self): kwargs = {} if "angdeg" in self: assert "rprim" not in self raise NotImplementedError("angdeg") #kwargs["angdeg"] = else: # Handle structure specified with rprim. kwargs["rprim"] = str2array_bohr(self.get("rprim", "1.0 0 0 0 1 0 0 0 1")) # Default value for acell acell = str2array_bohr(self.get("acell", "1.0 1.0 1.0")) check = {k: 1 for k in ("xred", "xcart", "xangst") if k in self} if len(check) != 1: raise ValueError("Atomic positions are not specified correctly:\n%s" % str(check)) if "xred" in self: kwargs["xred"] = np.fromstring(self["xred"], sep=" ") elif "xcart" in self: kwargs["xcart"] = str2array_bohr(self["xcart"]) elif "xangst" in self: kwargs["xangst"] = np.fromstring(self["xangst"], sep=" ") return Structure.from_abivars( acell=acell, znucl=str2array(self["znucl"]), typat=str2array(self["typat"]), **kwargs )
def structure(self): structure = Structure.from_abivars(**self.header) # Add Spacegroup (needed in guessed_ngkpt) # FIXME: has_timerev is always True spgid, has_timerev, h = 0, True, self.header structure.set_spacegroup(SpaceGroup(spgid, h.symrel, h.tnons, h.symafm, has_timerev)) return structure
def structure(self): # Get lattice. kwargs = {} if "angdeg" in self: if "rprim" in self: raise ValueError("rprim and angdeg cannot be used together!") angdeg = str2array(self["angdeg"]) angdeg.shape = (3) kwargs["angdeg"] = angdeg else: # Handle structure specified with rprim. kwargs["rprim"] = str2array_bohr(self.get("rprim", "1.0 0 0 0 1 0 0 0 1")) # Default value for acell. acell = str2array_bohr(self.get("acell", "1.0 1.0 1.0")) # Get important dimensions. ntypat = int(self.get("ntypat", 1)) natom = int(self.get("natom", 1)) # znucl(npsp) znucl = self["znucl"] if znucl.startswith("*"): i = znucl.find("*") znucl_size = natom if "npsp" not in self else int(self["npsp"]) znucl = znucl_size * [float(znucl[i+1:])] else: znucl = str2array(self["znucl"]) # v67mbpt/Input/t12.in typat = self["typat"] if typat.startswith("*"): i = typat.find("*") typat = np.array(natom * [int(typat[i+1:])], dtype=int) else: typat = str2array(self["typat"], dtype=int) # Extract atomic positions. # Select first natom entries (needed if multidatasets with different natom) # # v3/Input/t05.in typat = typat[:natom] for k in ("xred", "xcart", "xangst"): toarray = str2array_bohr if k == "xcart" else str2array if k in self: arr = np.reshape(toarray(self[k]), (-1, 3)) kwargs[k] = arr[:natom] break else: raise ValueError("xred|xcart|xangst must be given in input") try: return Structure.from_abivars(acell=acell, znucl=znucl, typat=typat, **kwargs) except Exception as exc: print("Wrong inputs passed to Structure.from_abivars:") print(" acell", acell) print(" znucl", znucl) print(" typat", typat) print(" kwargs", kwargs) raise exc
def structure(self): # Get lattice. kwargs = {} if "angdeg" in self: if "rprim" in self: raise ValueError("rprim and angdeg cannot be used together!") angdeg = str2array(self["angdeg"]) angdeg.shape = (3) kwargs["angdeg"] = angdeg else: # Handle structure specified with rprim. kwargs["rprim"] = str2array_bohr(self.get("rprim", "1.0 0 0 0 1 0 0 0 1")) # Default value for acell. acell = str2array_bohr(self.get("acell", "1.0 1.0 1.0")) # Get important dimensions. ntypat = int(self.get("ntypat", 1)) natom = int(self.get("natom", 1)) # znucl(npsp) znucl = self["znucl"] if znucl.startswith("*"): i = znucl.find("*") znucl_size = natom if "npsp" not in self else int(self["npsp"]) znucl = znucl_size * [float(znucl[i+1:])] else: znucl = str2array(self["znucl"]) # v67mbpt/Input/t12.in typat = self["typat"] if typat.startswith("*"): i = typat.find("*") typat = np.array(natom * [int(typat[i+1:])], dtype=int) else: typat = str2array(self["typat"], dtype=int) # Extract atomic positions. # Select first natom entries (needed if multidatasets with different natom) # # v3/Input/t05.in typat = typat[:natom] for k in ("xred", "xcart", "xangst"): toarray = str2array_bohr if k == "xcart" else str2array if k in self: arr = np.reshape(toarray(self[k]), (-1, 3)) kwargs[k] = arr[:natom] break else: raise ValueError("xred|xcart|xangst must be given in input") try: return Structure.from_abivars(acell=acell, znucl=znucl, typat=typat, **kwargs) except Exception as exc: print("Wrong inputs passed to Structure.from_abivars:") print("acell:", acell, "znucl:", znucl, "typat:", typat, "kwargs:", kwargs, sep="\n") raise exc
def __init__(self, filepath): super(DdbFile, self).__init__(filepath) self._header = self._parse_header() self._structure = Structure.from_abivars(**self.header) # Add Spacegroup (needed in guessed_ngkpt) # FIXME: has_timerev is always True spgid, has_timerev, h = 0, True, self.header self._structure.set_spacegroup(SpaceGroup(spgid, h.symrel, h.tnons, h.symafm, has_timerev)) frac_coords = self._read_qpoints() self._qpoints = KpointList(self.structure.reciprocal_lattice, frac_coords, weights=None, names=None) # Guess q-mesh self._guessed_ngqpt = self._guess_ngqpt()
def structure(self): """Returns the :class:`Structure` associated to this dataset.""" # TODO: Avoid calling Structure.from_abivars, find a way to cache the object and invalidate it. return Structure.from_abivars(self.allvars)
def _get_structures(self, what): if what == "header": vars_global, vars_dataset = self.initial_vars_global, self.initial_vars_dataset elif what == "footer": vars_global, vars_dataset = self.final_vars_global, self.final_vars_dataset else: raise ValueError("Invalid value for what: `%s`" % str(what)) #print("global", vars_global["acell"]) from abipy.abio.abivars import is_abiunit inigeo = {k: vars_global[k] for k in GEOVARS if k in vars_global} spgvars = ("spgroup", "symrel", "tnons", "symafm") spgd_global = {k: vars_global[k] for k in spgvars if k in vars_global} global_kptopt = vars_global.get("kptopt", 1) structures = [] for i in self.datasets: # This code breaks down if there are conflicting GEOVARS in globals and dataset. d = inigeo.copy() d.update({ k: vars_dataset[i][k] for k in GEOVARS if k in vars_dataset[i] }) for key, value in d.items(): # Must handle possible unit. fact = 1.0 tokens = [t.lower() for t in value.split()] if is_abiunit(tokens[-1]): tokens, unit = tokens[:-1], tokens[-1] if unit in ("angstr", "angstrom", "angstroms"): fact = 1.0 / bohr_to_ang elif unit in ("bohr", "bohrs", "au"): fact = 1.0 else: raise ValueError("Don't know how to handle unit: %s" % unit) s = " ".join(tokens) dtype = np.float if key not in ("ntypat", "typat", "natom") else np.int try: #print(key, s) value = np.fromstring(s, sep=" ", dtype=dtype) #print(key, value) if fact != 1.0: value *= fact # Do not change integer arrays e.g typat! d[key] = value except ValueError as exc: print(key, s) raise exc if "rprim" not in d and "angdeg" not in d: d["rprim"] = np.eye(3) if "natom" in d and d["natom"] == 1 and all( k not in d for k in ("xred", "xcart", "xangst")): d["xred"] = np.zeros(3) #print(d) abistr = Structure.from_abivars(d) # Extract Abinit spacegroup. spgd = spgd_global.copy() spgd.update({ k: vars_dataset[i][k] for k in spgvars if k in vars_dataset[i] }) spgid = int(spgd.get("spgroup", 0)) if "symrel" not in spgd: symrel = np.reshape(np.eye(3, 3, dtype=np.int), (1, 3, 3)) spgd["symrel"] = " ".join((str(i) for i in symrel.flatten())) else: symrel = np.reshape( np.array([int(n) for n in spgd["symrel"].split()], dtype=np.int), (-1, 3, 3)) nsym = len(symrel) assert nsym == spgd.get("nsym", nsym) #; print(symrel.shape) if "tnons" in spgd: tnons = np.reshape( np.array([float(t) for t in spgd["tnons"].split()], dtype=np.float), (nsym, 3)) else: tnons = np.zeros((nsym, 3)) if "symafm" in spgd: symafm = np.array([int(n) for n in spgd["symafm"].split()], dtype=np.int) symafm.shape = (nsym, ) else: symafm = np.ones(nsym, dtype=np.int) try: has_timerev = has_timrev_from_kptopt(vars_dataset[i].get( "kptopt", global_kptopt)) abi_spacegroup = AbinitSpaceGroup(spgid, symrel, tnons, symafm, has_timerev, inord="C") abistr.set_abi_spacegroup(abi_spacegroup) except Exception as exc: print( "Cannot build AbinitSpaceGroup from the variables reported in file!\n", str(exc)) structures.append(abistr) return structures
def _get_structures(self, what): if what == "header": vars_global, vars_dataset = self.initial_vars_global, self.initial_vars_dataset elif what == "footer": vars_global, vars_dataset = self.final_vars_global, self.final_vars_dataset else: raise ValueError("Invalid value for what: `%s`" % str(what)) #print("global", vars_global["acell"]) from abipy.abio.abivars import is_abiunit inigeo = {k: vars_global[k] for k in GEOVARS if k in vars_global} spgvars = ("spgroup", "symrel", "tnons", "symafm") spgd_global = {k: vars_global[k] for k in spgvars if k in vars_global} global_kptopt = vars_global.get("kptopt", 1) structures = [] for i in self.datasets: # This code breaks down if there are conflicting GEOVARS in globals and dataset. d = inigeo.copy() d.update({k: vars_dataset[i][k] for k in GEOVARS if k in vars_dataset[i]}) for key, value in d.items(): # Must handle possible unit. fact = 1.0 tokens = [t.lower() for t in value.split()] if is_abiunit(tokens[-1]): tokens, unit = tokens[:-1], tokens[-1] if unit in ("angstr", "angstrom", "angstroms"): fact = 1.0 / bohr_to_ang elif unit in ("bohr", "bohrs", "au"): fact = 1.0 else: raise ValueError("Don't know how to handle unit: %s" % unit) s = " ".join(tokens) dtype = np.float if key not in ("ntypat", "typat", "natom") else np.int try: #print(key, s) value = np.fromstring(s, sep=" ", dtype=dtype) #print(key, value) if fact != 1.0: value *= fact # Do not change integer arrays e.g typat! d[key] = value except ValueError as exc: print(key, s) raise exc if "rprim" not in d and "angdeg" not in d: d["rprim"] = np.eye(3) if "natom" in d and d["natom"] == 1 and all(k not in d for k in ("xred", "xcart", "xangst")): d["xred"] = np.zeros(3) #print(d) abistr = Structure.from_abivars(d) # Extract Abinit spacegroup. spgd = spgd_global.copy() spgd.update({k: vars_dataset[i][k] for k in spgvars if k in vars_dataset[i]}) spgid = int(spgd.get("spgroup", 0)) if "symrel" not in spgd: symrel = np.reshape(np.eye(3, 3, dtype=np.int), (1, 3, 3)) spgd["symrel"] = " ".join((str(i) for i in symrel.flatten())) else: symrel = np.reshape(np.array([int(n) for n in spgd["symrel"].split()], dtype=np.int), (-1, 3, 3)) nsym = len(symrel) assert nsym == spgd.get("nsym", nsym) #; print(symrel.shape) if "tnons" in spgd: tnons = np.reshape(np.array([float(t) for t in spgd["tnons"].split()], dtype=np.float), (nsym, 3)) else: tnons = np.zeros((nsym, 3)) if "symafm" in spgd: symafm = np.array([int(n) for n in spgd["symafm"].split()], dtype=np.int) symafm.shape = (nsym,) else: symafm = np.ones(nsym, dtype=np.int) try: has_timerev = has_timrev_from_kptopt(vars_dataset[i].get("kptopt", global_kptopt)) abi_spacegroup = AbinitSpaceGroup(spgid, symrel, tnons, symafm, has_timerev, inord="C") abistr.set_abi_spacegroup(abi_spacegroup) except Exception as exc: print("Cannot build AbinitSpaceGroup from the variables reported in file!\n", str(exc)) structures.append(abistr) return structures
def structure(self): """ The initial structure associated to the dataset. """ # First of all check whether the structure is defined through external file. if "structure" in self: s = self["structure"].replace('"', "") filetype, path = s.split(":") from abipy import abilab with abilab.abiopen(path) as abifile: return abifile.structure # Get lattice. kwargs = {} if "angdeg" in self: if "rprim" in self: raise ValueError("rprim and angdeg cannot be used together!") angdeg = str2array(self["angdeg"]) angdeg.shape = (3) kwargs["angdeg"] = angdeg else: # Handle structure specified with rprim. kwargs["rprim"] = str2array_bohr( self.get("rprim", "1.0 0 0 0 1 0 0 0 1")) # Default value for acell. acell = str2array_bohr(self.get("acell", "1.0 1.0 1.0")) # Get important dimensions. ntypat = int(self.get("ntypat", 1)) natom = int(self.get("natom", 1)) # znucl(npsp) znucl = self["znucl"] if znucl.startswith("*"): i = znucl.find("*") znucl_size = natom if "npsp" not in self else int(self["npsp"]) znucl = znucl_size * [float(znucl[i + 1:])] else: znucl = str2array(self["znucl"]) # v67mbpt/Input/t12.in typat = self["typat"] if typat.startswith("*"): i = typat.find("*") typat = np.array(natom * [int(typat[i + 1:])], dtype=int) else: typat = str2array(self["typat"], dtype=int) # Extract atomic positions. # Select first natom entries (needed if multidatasets with different natom) # # v3/Input/t05.in typat = typat[:natom] for k in ("xred", "xcart", "xangst"): toarray = str2array_bohr if k == "xcart" else str2array if k in self: arr = np.reshape(toarray(self[k]), (-1, 3)) kwargs[k] = arr[:natom] break else: raise ValueError("xred|xcart|xangst must be given in input") try: return Structure.from_abivars(acell=acell, znucl=znucl, typat=typat, **kwargs) except Exception as exc: print("Wrong inputs passed to Structure.from_abivars:") print("acell:", acell, "znucl:", znucl, "typat:", typat, "kwargs:", kwargs, sep="\n") raise exc