def unitcell(h5file, vasp_dir, elements=None, poscar_equiv='POSCAR'): """POSCAR parser Reads lattice vectors and atom positions from POSCAR file and writes data to an HDF5 file. If no element symbols are given as an argument, the parser looks for them in the POTCAR file, or in POSCAR if no POTCAR file is found. If POSCAR uses cartesian coordinates, they will be transformed. If the given HDF5 file already contains unit cell data, nothing is parsed. Parameters ---------- h5file : str Path to HDF5 file vasp_dir : str Path to directory containing POSCAR file elements : list of str (Default value = None) List of element symbols Returns ------- bool True if POSCAR was parsed, False otherwise. """ if os.path.isfile(h5file): with h5py.File(h5file, 'r') as h5: if "/UnitCell" in h5: print("Already parsed. Skipping.") return True try: # Parses lattice vectors and atom positions from POSCAR with open(os.path.join(vasp_dir,poscar_equiv), "r") as f: scaling_factor, basis = _parse_lattice(f) elements, atoms = _find_elements(f, elements, vasp_dir) coords_list = _parse_coordinates( f, sum(atoms), _cartesian(f), np.linalg.inv(basis) ) _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor) _write_coordinates( h5file, atoms, coords_list, elements, '/UnitCell' ) return True except FileNotFoundError: print("POSCAR file not found.") return False
def md(h5_path, vasp_dir, elements=None): """XDATCAR parser Reads atom positions for each time step and lattice vectors from XDATCAR file and writes data to an HDF5 file. If no element symbols are given as an argument, the parser looks for them in the POTCAR file, or in XDATCAR if no POTCAR file is found. If the given HDF5 file already contains molecular dynamics data, nothing is parsed. Parameters ---------- h5_path : str Path to HDF5 file vasp_dir : str Path to directory containing XDATCAR file elements : list of str (Default value = None) List of element symbols Returns ------- bool True if XDATCAR was parsed, False otherwise. """ if os.path.isfile(h5_path): with h5py.File(h5_path, 'r') as h5: if "/MD" in h5: print("Already parsed. Skipping.") return False try: with open(os.path.join(vasp_dir, 'XDATCAR'), "r") as f: scaling_factor, basis = _parse_lattice(f) elements, atoms = _find_elements(f, elements, vasp_dir) _write_basis(h5_path, scaling_factor * basis) step = 0 while True: coords_list = [] try: coords_list = _parse_coordinates(f, sum(atoms)) except Exception as instance: text, length = instance.args if length != 0: raise return False if not coords_list: break _write_md(h5_path, atoms, coords_list, elements, step) step += 1 _write_steps(h5_path, step) return True except FileNotFoundError: print("XDATCAR file not found.") return False
def unitcell_parser(h5file, ELK_dir): elements, number_of_atoms = find_elements(ELK_dir) scaling_factor, basis = parse_lattice(ELK_dir) coords_list = parse_coordinates(ELK_dir, number_of_atoms) #with open(os.path.join(ELK_dir,info_equiv), "r") as f: _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor=1) _write_coordinates(h5file, number_of_atoms, coords_list, elements, '/UnitCell') return
def parse_elf(h5file, ELK_dir): scaling_factor, basis = parse_lattice(ELK_dir) _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor=1) ELK_file = Path(ELK_dir).joinpath('ELF3D.OUT') with open(ELK_file, "r+") as f: for i in itertools.count(): array, data_dim = parse_vol(f) if not _write_volume(h5file, i, array, data_dim, "ELF"): return False if not array: break return True
def volume(h5file, hdfgroup, vasp_dir, vasp_file): """ Reads volume data from vasp_file, either CHG or ELFCAR, and stores it in an HDF-file. Parameters ---------- h5file : str String that asserts which HDF-file to write to hdfgroup : str String that asserts which group to write to in HDF-file vasp_dir : str Path to directory containing volume file vasp_file : str String that asserts which file to open in the directory Returns ------- bool True if volume file was parsed, False otherwise. """ if os.path.isfile(h5file): with h5py.File(h5file, 'r') as h5: if '/{}'.format(hdfgroup) in h5: print(vasp_file + ' already parsed. Skipping.') return False try: with open(os.path.join(vasp_dir, 'POSCAR'), 'r') as f: scaling_factor, basis = _parse_lattice(f) _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor) except FileNotFoundError: print("POSCAR file not in directory. Skipping.") try: with open(os.path.join(vasp_dir, vasp_file), "r") as f: for i in itertools.count(): array, data_dim = parse_volume(f, hdfgroup) if not _write_volume(h5file, i, array, data_dim, hdfgroup): return False if not array: break except FileNotFoundError: print(vasp_file + ' file not in directory. Skipping.') return False print(vasp_file + ' was parsed successfully.') return True
def parse_force_elk(h5file, elk_dir, inviwo=False, elements=None): elements, atoms = find_elements(elk_dir) coordinates = parse_coordinates(elk_dir, atoms) scaling_factor, basis = parse_lattice(elk_dir) force_tips = parse_force(elk_dir, coordinates) if os.path.isfile(h5file): with h5py.File(h5file, 'r') as h5: if "/UnitCell" not in h5: _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor=1) _write_coordinates(h5file, atoms, coordinates, elements, '/UnitCell') _write_forces(h5file, atoms, force_tips, '/Forces') return True else: _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor=1) _write_coordinates(h5file, atoms, coordinates, elements, '/UnitCell') _write_forces(h5file, atoms, force_tips, '/Forces') return True
def force_parser(h5file, vasp_dir, inviwo=False, elements=None, poscar_equiv='POSCAR'): if has_been_parsed("force_parser", h5file, vasp_dir) and inviwo: print("Already Parsed, skipping") return True try: # Parses lattice vectors and atom positions from POSCAR with open(os.path.join(vasp_dir, poscar_equiv), "r") as f: scaling_factor, basis = _parse_lattice(f) elements, atoms = _find_elements(f, elements, vasp_dir) coords_list = _parse_coordinates(f, sum(atoms), _cartesian(f), np.linalg.inv(basis)) force_list = _parse_forces(vasp_dir, True, coords_list) if os.path.isfile(h5file): with h5py.File(h5file, 'r') as h5: if "/UnitCell" in h5: h5.close() _write_forces(h5file, atoms, force_list, '/Forces') return True else: h5.close() _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor) _write_coordinates(h5file, atoms, coords_list, elements, '/UnitCell') else: _write_basis(h5file, basis) _write_scaling_factor(h5file, scaling_factor) _write_coordinates(h5file, atoms, coords_list, elements, '/UnitCell') _write_forces(h5file, atoms, force_list, '/Forces') return True except FileNotFoundError: print("POSCAR file not found........") return False