Skip to content

Commit abd9893

Browse files
authored
Issue stronger warning if bader is run without the AECCARs (#3458)
* bump ruff pre-commit hook * fix BaderAnalysis.from_path issuing missing POTCAR warning for missing AECCAR0/2 and vice versa
1 parent 2ccbfa1 commit abd9893

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ci:
88

99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.1.3
11+
rev: v0.1.4
1212
hooks:
1313
- id: ruff
1414
args: [--fix]

pymatgen/command_line/bader_caller.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,15 @@ def from_path(cls, path: str, suffix: str = "") -> BaderAnalysis:
371371
to perform Bader analysis.
372372
373373
Args:
374-
path (str): Name of directory where VASP output files are
375-
stored.
374+
path (str): Name of directory where VASP output files are stored.
376375
suffix (str): specific suffix to look for (e.g. '.relax1' for 'CHGCAR.relax1.gz').
377376
378377
Returns:
379378
BaderAnalysis
380379
"""
381380

382381
def _get_filepath(filename):
383-
name_pattern = filename + suffix + "*" if filename != "POTCAR" else filename + "*"
382+
name_pattern = f"{filename}{suffix}*" if filename != "POTCAR" else f"{filename}*"
384383
paths = glob(f"{path}/{name_pattern}")
385384
fpath = ""
386385
if len(paths) >= 1:
@@ -394,10 +393,10 @@ def _get_filepath(filename):
394393
fpath = paths[0]
395394
else:
396395
msg = f"Could not find {filename!r}"
397-
if filename in ["AECCAR0", "AECCAR2"]:
398-
msg += ", cannot calculate charge transfer."
396+
if filename in ("AECCAR0", "AECCAR2"):
397+
msg += ", interpret Bader results with severe caution."
399398
elif filename == "POTCAR":
400-
msg += ", interpret Bader results with caution."
399+
msg += ", cannot calculate charge transfer."
401400
warnings.warn(msg)
402401
return fpath
403402

@@ -440,10 +439,9 @@ def bader_analysis_from_path(path, suffix=""):
440439
summary dict
441440
"""
442441

443-
def _get_filepath(filename, warning, path=path, suffix=suffix):
442+
def _get_filepath(filename, path=path, suffix=suffix):
444443
paths = glob(f"{path}/{filename}{suffix}*")
445-
if not paths:
446-
warnings.warn(warning)
444+
if len(paths) == 0:
447445
return None
448446
if len(paths) > 1:
449447
# using reverse=True because, if multiple files are present,
@@ -457,13 +455,19 @@ def _get_filepath(filename, warning, path=path, suffix=suffix):
457455
chgcar_path = _get_filepath("CHGCAR", "Could not find CHGCAR!")
458456
chgcar = Chgcar.from_file(chgcar_path)
459457

460-
aeccar0_path = _get_filepath("AECCAR0", "Could not find AECCAR0, interpret Bader results with caution.")
458+
aeccar0_path = _get_filepath("AECCAR0")
459+
if not aeccar0_path:
460+
warnings.warn("Could not find AECCAR0, interpret Bader results with severe caution!")
461461
aeccar0 = Chgcar.from_file(aeccar0_path) if aeccar0_path else None
462462

463-
aeccar2_path = _get_filepath("AECCAR2", "Could not find AECCAR2, interpret Bader results with caution.")
463+
aeccar2_path = _get_filepath("AECCAR2")
464+
if not aeccar2_path:
465+
warnings.warn("Could not find AECCAR2, interpret Bader results with severe caution!")
464466
aeccar2 = Chgcar.from_file(aeccar2_path) if aeccar2_path else None
465467

466-
potcar_path = _get_filepath("POTCAR", "Could not find POTCAR, cannot calculate charge transfer.")
468+
potcar_path = _get_filepath("POTCAR")
469+
if not potcar_path:
470+
warnings.warn("Could not find POTCAR, cannot calculate charge transfer.")
467471
potcar = Potcar.from_file(potcar_path) if potcar_path else None
468472

469473
return bader_analysis_from_objects(chgcar, potcar, aeccar0, aeccar2)

pymatgen/io/abinit/netcdf.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
__date__ = "Feb 21, 2013M"
3737

3838

39-
def _asreader(file, cls):
40-
closeit = False
39+
def _as_reader(file, cls):
40+
close_it = False
4141
if not isinstance(file, cls):
42-
file, closeit = cls(file), True
43-
return file, closeit
42+
file, close_it = cls(file), True
43+
return file, close_it
4444

4545

4646
def as_ncreader(file):
4747
"""
4848
Convert file into a NetcdfReader instance.
49-
Returns reader, closeit where closeit is set to True
49+
Returns reader, close_it where close_it is set to True
5050
if we have to close the file before leaving the procedure.
5151
"""
52-
return _asreader(file, NetcdfReader)
52+
return _as_reader(file, NetcdfReader)
5353

5454

5555
def as_etsfreader(file):
5656
"""Return an EtsfReader. Accepts filename or EtsfReader."""
57-
return _asreader(file, EtsfReader)
57+
return _as_reader(file, EtsfReader)
5858

5959

6060
class NetcdfReaderError(Exception):
@@ -302,7 +302,7 @@ def structure_from_ncdata(ncdata, site_properties=None, cls=Structure):
302302
site_properties: Dictionary with site properties.
303303
cls: The Structure class to instantiate.
304304
"""
305-
ncdata, closeit = as_ncreader(ncdata)
305+
ncdata, close_it = as_ncreader(ncdata)
306306

307307
# TODO check whether atomic units are used
308308
lattice = ArrayWithUnit(ncdata.read_value("primitive_vectors"), "bohr").to("ang")
@@ -337,7 +337,7 @@ def structure_from_ncdata(ncdata, site_properties=None, cls=Structure):
337337
except ImportError:
338338
pass
339339

340-
if closeit:
340+
if close_it:
341341
ncdata.close()
342342

343343
return structure

pymatgen/io/vasp/inputs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,5 +2696,5 @@ def run_vasp(
26962696
vasp_cmd = [os.path.expanduser(os.path.expandvars(t)) for t in vasp_cmd]
26972697
if not vasp_cmd:
26982698
raise RuntimeError("You need to supply vasp_cmd or set the PMG_VASP_EXE in .pmgrc.yaml to run VASP.")
2699-
with cd(run_dir), open(output_file, "w") as f_std, open(err_file, "w", buffering=1) as f_err:
2700-
subprocess.check_call(vasp_cmd, stdout=f_std, stderr=f_err)
2699+
with cd(run_dir), open(output_file, "w") as stdout_file, open(err_file, "w", buffering=1) as stderr_file:
2700+
subprocess.check_call(vasp_cmd, stdout=stdout_file, stderr=stderr_file)

0 commit comments

Comments
 (0)