Skip to content

Commit

Permalink
VaspInputSet.write_input: Improve error message (#3999)
Browse files Browse the repository at this point in the history
* VaspInputSet.write_input: Improve error message

* src/pymatgen/io/vasp/inputs.py (PotcarSingle.from_symbol_and_functional):
Raise custom PMG_VASP_PSP_DIR_Error when PMG_VASP_PSP_DIR is not configured.

(PMG_VASP_PSP_DIR_Error): New error inheriting from ValueError.

* src/pymatgen/io/vasp/sets.py (VaspInputSet.write_input):
When PMG_VASP_PSP_DIR_Error provide a more helpful message advicing
that potcar_spec argument can be used.  The idea is to provide
instructions for users with no VASP license who are also not aware
about potcar_spec argument.

* pre-commit auto-fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
yantar92 and pre-commit-ci[bot] authored Aug 15, 2024
1 parent b7b7389 commit 1a4c538
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,10 @@ class OrbitalDescription(NamedTuple):
POTCAR_STATS_PATH: str = os.path.join(MODULE_DIR, "potcar-summary-stats.json.bz2")


class PMG_VASP_PSP_DIR_Error(ValueError):
"""Error thrown when PMG_VASP_PSP_DIR is not configured, but POTCAR is requested."""


class PotcarSingle:
"""
Object for a **single** POTCAR. The builder assumes the POTCAR contains
Expand Down Expand Up @@ -2272,7 +2276,7 @@ def from_symbol_and_functional(
functional_subdir = SETTINGS.get("PMG_VASP_PSP_SUB_DIRS", {}).get(functional, cls.functional_dir[functional])
PMG_VASP_PSP_DIR = SETTINGS.get("PMG_VASP_PSP_DIR")
if PMG_VASP_PSP_DIR is None:
raise ValueError(
raise PMG_VASP_PSP_DIR_Error(
f"No POTCAR for {symbol} with {functional=} found. Please set the PMG_VASP_PSP_DIR in .pmgrc.yaml."
)
if not os.path.isdir(PMG_VASP_PSP_DIR):
Expand Down
12 changes: 10 additions & 2 deletions src/pymatgen/io/vasp/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from pymatgen.analysis.structure_matcher import StructureMatcher
from pymatgen.core import Element, PeriodicSite, SiteCollection, Species, Structure
from pymatgen.io.core import InputGenerator
from pymatgen.io.vasp.inputs import Incar, Kpoints, Poscar, Potcar, VaspInput
from pymatgen.io.vasp.inputs import Incar, Kpoints, PMG_VASP_PSP_DIR_Error, Poscar, Potcar, VaspInput
from pymatgen.io.vasp.outputs import Outcar, Vasprun
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.symmetry.bandstructure import HighSymmKpath
Expand Down Expand Up @@ -341,7 +341,15 @@ def write_input(
zip_output (bool): If True, output will be zipped into a file with the
same name as the InputSet (e.g., MPStaticSet.zip).
"""
vasp_input = self.get_input_set(potcar_spec=potcar_spec)
try:
vasp_input = self.get_input_set(potcar_spec=potcar_spec)
except PMG_VASP_PSP_DIR_Error:
assert potcar_spec is False
raise ValueError(
"PMG_VASP_PSP_DIR is not set."
" Please set the PMG_VASP_PSP_DIR in .pmgrc.yaml"
" or use potcar_spec=True argument."
)

cif_name = None
if include_cif:
Expand Down

0 comments on commit 1a4c538

Please sign in to comment.