def pymatgen2pychemia(pmg_struct): """ Converts a pymatgen structure object into a pychemia structure object :param pmg_struct: (pymatgen.Structure) Structure from pymatgen that will be converted :return: """ # if not pmg_struct.is_ordered: # return None cell = pmg_struct.lattice_vectors() reduced = np.array([]) sites = [] symbols = [] occupancies = [] index = 0 for i in pmg_struct: for j in i.species_and_occu: symbols.append(j.symbol) occupancies.append(i.species_and_occu[j]) sites.append(index) reduced = np.concatenate((reduced, i.frac_coords)) index += 1 reduced.reshape((-1, 3)) return Structure(cell=cell, reduced=reduced, symbols=symbols, sites=sites, occupancies=occupancies)
def load(filep): """ Read an V_sim .ascii file and returns a pychemia Structure object Args: filep: (string) Path to a .ascii file or an actual file-like object Returns: struct: (object) A pychemia Structure object """ if isinstance(filep, str): f = open(filep) else: # Assume it's a file-like object f = filep comment = f.readline() line = f.readline() + ' ' + f.readline() box = line.split() for i in range(len(box)): box[i] = float(box[i]) keywords = [] positions = [] symbols = [] re_comment = _re.compile('^\s*[#!]') re_node = _re.compile('^\s*\S+\s+\S+\s+\S+\s+\S+') while True: line = f.readline() if line == '': break # EOF -> Exit p = re_comment.match(line) if p is not None: # remove comment character at the beginning of line line = line[p.end():].replace(',', ' ').lower() if line[:8] == "keyword:": keywords.extend(line[8:].split()) elif re_node.match(line): unit = 1.0 if not ("reduced" in keywords): if ("bohr" in keywords) or ("bohrd0" in keywords) or ( "atomic" in keywords) or ("atomicd0" in keywords): unit = bohr_angstrom fields = line.split() positions.append([ unit * float(fields[0]), unit * float(fields[1]), unit * float(fields[2]) ]) symbols.append(fields[3]) f.close() if ("surface" in keywords) or ("freeBC" in keywords): raise NotImplementedError # create atoms object based on the information if "angdeg" in keywords: cell = cell.par_to_cell(box) else: unit = 1.0 if ("bohr" in keywords) or ("bohrd0" in keywords) or ( "atomic" in keywords) or ("atomicd0" in keywords): unit = bohr_angstrom cell = [[unit * box[0], 0.0, 0.0], [unit * box[1], unit * box[2], 0.0], [unit * box[3], unit * box[4], unit * box[5]]] if "reduced" in keywords: struct = Structure(cell=cell, reduced=positions, symbols=symbols, name=comment) else: struct = Structure(cell=cell, positions=positions, symbols=symbols, name=comment) return struct
def get_structure(self, idtset=None, units='bohr'): """ Return the atomic structure from the input object for a given dataset (no dataset by default) """ # NATOM natom = self.get_value('natom', idtset) # SYMBOLS ntypat = self.get_value('ntypat', idtset) symbols = [] znucl = self.get_value('znucl', idtset) typat = self.get_value('typat', idtset) for i in range(natom): # NOTE: znucl is a real number in OUT.nc # Alchemic mixing is not allow here if ntypat == 1: symbols.append(atomic_symbol(int(znucl))) else: symbols.append(atomic_symbol(int(znucl[typat[i] - 1]))) # POSITIONS xangst = self.get_value('xangst', idtset) xcart = self.get_value('xcart', idtset) xred = self.get_value('xred', idtset) rprim = self.get_value('rprim', idtset) acell = np.array(self.get_value('acell', idtset)) # Set rprimd and acell using the default values # if not found if rprim is None: rprim = np.identity(3) else: rprim = np.array(rprim).reshape((3, 3)) if acell is None: acell = np.ones(3) rprimd = np.zeros((3, 3)) rprimd[0] = rprim[0] * acell rprimd[1] = rprim[1] * acell rprimd[2] = rprim[2] * acell if xangst is not None: xangst = np.array(xangst) positions = xangst.reshape((natom, 3)) if units == 'bohr': positions = positions * angstrom_bohr elif xcart is not None: xcart = np.array(xcart) positions = xcart.reshape((natom, 3)) if units == 'angstrom': positions = positions * bohr_angstrom elif xred is not None: xred = np.array(xred) xred = xred.reshape((natom, 3)) xcart = np.zeros((natom, 3)) # print rprimd # print xred for i in range(natom): xcart[i] = xred[i, 0] * rprimd[0] + xred[i, 1] * rprimd[1] + xred[i, 2] * rprimd[2] positions = xcart if units == 'angstrom': positions = positions * bohr_angstrom else: positions = None if units == 'angstrom': rprimd = rprimd * bohr_angstrom # Create an object atomic_structure structure = Structure(natom=natom, symbols=symbols, positions=positions, cell=rprimd) return structure