def _get_atom(cba): """Convert a _atom loop to an Atom""" atom = strx.Atom() atom.element_id = _get_element(cba) if atom.element_id == 'D' or atom.element_id == 'T': atom.element_id = 'H' atom.coord = _get_atom_coord(cba) atom.occupancy = _get_occupancy(cba) atom.b_factor = _get_b_factor(cba) return atom
def read(poscar, species=None): """ Read a POSCAR format file. Reads VASP 4.x and 5.x format POSCAR files. Keyword Arguments: `species`: If the file is 4.x format and the title line doesn't indicate what species are present, you must supply them (in the correct order) with the `species` keyword. Raises: POSCARError: If species data is not available. Examples:: >>> io.poscar.read(INSTALL_PATH+'/io/files/POSCAR_FCC') """ # Initialize the structure output struct = st.Structure() # Read in the title block, and system sell if isinstance(poscar, StringIO.StringIO): poscar = poscar else: poscar = open(poscar, 'r') title = poscar.readline().strip() scale = float(poscar.readline().strip()) s = float(scale) cell = [[float(v) for v in poscar.readline().split()], [float(v) for v in poscar.readline().split()], [float(v) for v in poscar.readline().split()]] cell = np.array(cell) if s > 0: struct.cell = cell * s else: struct.cell = cell struct.volume = -1 * s # Determine whether POSCAR is in VASP 5 format # VASP 5 has the elements listed after the # the cell parameters vasp5 = False _species = poscar.readline().strip().split() try: float(_species[0]) except: vasp5 = True counts = [int(v) for v in poscar.readline().split()] # If the format is not VASP 5, the elements should # have been listed in the title if not vasp5: counts = map(int, _species) if not species: _species = title.strip().split() for s in _species: if not s in qmpy.elements.keys(): msg = 'In VASP4.x format, title line MUST be species present' raise POSCARError else: _species = species species = _species # Prepare a list of numbers of atom types atom_types = [] for n, e in zip(counts, species): atom_types += [e] * n # Determine whether coordinates are in direct or cartesian direct = False style = poscar.readline() if style[0].lower() == 's': # The POSCAR contains selective dynamics info style = poscar.readline() if style[0] in ['D', 'd']: direct = True # Read in the atom coordinates struct.natoms = sum(counts) struct.ntypes = len(counts) atoms = [] inv = np.linalg.inv(cell).T for i in range(struct.natoms): atom = st.Atom() atom.element_id = atom_types[i] if direct: atom.coord = [float(v) for v in poscar.readline().split()[0:3]] else: cart = [float(v) for v in poscar.readline().split()[0:3]] atom.coord = np.dot(inv, cart) struct.add_atom(atom) struct.get_volume() struct.set_composition() return struct
def read(poscar, species=None): """ Read a POSCAR format file. Reads VASP 4.x and 5.x format POSCAR files. Keyword Arguments: `species`: If the file is 4.x format and the title line doesn't indicate what species are present, you must supply them (in the correct order) with the `species` keyword. Raises: POSCARError: If species data is not available. Examples:: >>> io.poscar.read(INSTALL_PATH+'/io/files/POSCAR_FCC') """ struct = st.Structure() poscar = open(poscar, 'r') title = poscar.readline().strip() scale = float(poscar.readline().strip()) s = float(scale) cell = [[float(v) for v in poscar.readline().split()], [float(v) for v in poscar.readline().split()], [float(v) for v in poscar.readline().split()]] cell = np.array(cell) if s > 0: struct.cell = cell * s else: struct.cell = cell struct.volume = -1 * s vasp5 = False _species = poscar.readline().strip().split() try: float(_species[0]) except: vasp5 = True counts = [int(v) for v in poscar.readline().split()] if not vasp5: counts = map(int, _species) if not species: _species = title.strip().split() for s in _species: if not s in qmpy.elements.keys(): msg = 'In VASP4.x format, title line MUST be species present' raise POSCARError else: _species = species species = _species atom_types = [] for n, e in zip(counts, species): atom_types += [e] * n style = poscar.readline() direct = False if style[0] in ['D', 'd']: direct = True struct.natoms = sum(counts) struct.ntypes = len(counts) atoms = [] inv = np.linalg.inv(cell).T for i in range(struct.natoms): atom = st.Atom() atom.element_id = atom_types[i] if direct: atom.coord = [float(v) for v in poscar.readline().split()[0:3]] else: cart = [float(v) for v in poscar.readline().split()[0:3]] atom.coord = np.dot(inv, cart) struct.add_atom(atom) struct.get_volume() struct.set_composition() return struct