py4vasp
py4vasp is a Python interface for analyzing and setting up VASP calculations, primarily by extracting data from the new HDF5 file format (`VASP_H5_data.h5`). It's designed for quick data inspection, plotting, and export to other tools, serving as a powerful assistant for materials science researchers working with VASP output. The library is actively developed, with regular releases providing new features and improvements.
Common errors
-
FileNotFoundError: VASP_H5_data.h5
cause The VASP HDF5 output file is missing or located in a different directory than specified. This often happens with older VASP versions (pre-VASP 6), calculations that failed before HDF5 output, or incorrect paths.fixEnsure your VASP version (VASP 6+) is configured to produce `VASP_H5_data.h5`. Check that the file exists in the directory passed to `pv.Calculation()` or the current working directory. If you are using an older VASP version, py4vasp will not work without manual data conversion. -
AttributeError: 'SomeDataObject' object has no attribute 'plot_bands'
cause In py4vasp v0.11.0 and later, specific plotting methods like `plot_bands()` or `plot_dos()` were consolidated into a single, generic `.plot()` method available on data objects.fixUpdate your code to use the generic `.plot()` method. For example, change `calc.bands.plot_bands()` to `calc.bands.plot()` and `calc.dos.plot_dos()` to `calc.dos.plot()`. -
OSError: Unable to open file (file is not an HDF5 file) or OSError: Unable to open file (file is truncated)
cause The `VASP_H5_data.h5` file is corrupted, incomplete, or not a valid HDF5 file. This can occur if a VASP calculation was interrupted prematurely or if the file was transferred incorrectly.fixRe-run the VASP calculation to ensure it completes successfully and generates a valid HDF5 file. Check the file size; a very small `VASP_H5_data.h5` often indicates a truncated or empty file. You can also use `h5dump` or `h5ls` command-line tools to inspect the file integrity. -
KeyError: 'Some_Expected_Data_Key' or AttributeError: 'Calculation' object has no attribute 'some_data_type'
cause The specific data requested (e.g., Density of States, Band Structure, specific magnetic moments) was not calculated in the VASP run, or it is not present in the `VASP_H5_data.h5` output file.fixVerify that your VASP input files (e.g., INCAR, KPOINTS) were correctly configured to perform the calculation that generates the desired data. Refer to the VASP manual for required tags. You can inspect the contents of `VASP_H5_data.h5` using `h5ls VASP_H5_data.h5` to see what data is actually available.
Warnings
- breaking Significant API changes occurred in v0.11.0. The `Kpoint` class was renamed to `Kpoints`, and specific plotting methods (e.g., `plot_bands()`, `plot_dos()`) were consolidated into a generic `.plot()` method available on data objects.
- breaking In v0.10.0, the explicit `Selection` class for filtering data was removed and replaced by direct attribute access or arguments passed to data access and plotting methods. The `.read()` method on data objects was also deprecated.
- gotcha When initializing `pv.Calculation(path)`, py4vasp expects the VASP output files (especially `VASP_H5_data.h5`) to be located at the specified `path` or in the current working directory. If your script is run from a different location, it will fail to find the files.
Install
-
pip install py4vasp
Imports
- py4vasp
import py4vasp as pv
- Calculation
from py4vasp.raw import Calculation
from py4vasp import Calculation
Quickstart
import py4vasp as pv
import matplotlib.pyplot as plt
import os
# This example assumes a VASP calculation's output files
# (especially VASP_H5_data.h5) are present in the 'example_calc_path' directory.
# Replace 'example_calc_path' with the actual directory containing your VASP output.
# For demonstration, we use a placeholder and handle potential errors.
# If you have VASP output in a specific directory, set it here:
example_calc_path = os.environ.get("VASP_CALC_PATH", ".") # Defaults to current directory
try:
# Initialize a Calculation object from the path
calc = pv.Calculation(example_calc_path)
# Access and plot the Density of States (DOS)
if hasattr(calc, 'dos') and calc.dos.is_readable():
print(f"Loading DOS from: {example_calc_path}")
dos_plot = calc.dos.plot()
# dos_plot.show() # Uncomment to display the plot (requires matplotlib backend)
# Export DOS data to a dictionary
dos_data = calc.dos.to_dict()
print("DOS data keys:", dos_data.keys())
else:
print(f"DOS data not found or not readable in '{example_calc_path}'.")
print("Ensure VASP_H5_data.h5 exists and contains DOS information.")
except Exception as e:
print(f"Error accessing VASP calculation at '{example_calc_path}': {e}")
print("Please ensure the path is correct and contains valid VASP HDF5 output files.")
# Example of accessing the band structure data object directly
bands_object = pv.data.band()
print(f"\nExample of accessing band structure data object: {bands_object}")
# bands_object.plot().show() # Would require a VASP_H5_data.h5 with band data