Skip to content

Commit def4a66

Browse files
authored
Merge pull request #406 from naik-aakash/update_featurizers
Improve calc quality analysis implementation
2 parents 85f9dcb + 9eb55f8 commit def4a66

File tree

3 files changed

+77
-78
lines changed

3 files changed

+77
-78
lines changed

src/lobsterpy/cohp/analyze.py

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pymatgen.core.structure import Structure
1616
from pymatgen.electronic_structure.cohp import CompleteCohp
1717
from pymatgen.electronic_structure.core import Spin
18-
from pymatgen.electronic_structure.dos import LobsterCompleteDos
18+
from pymatgen.electronic_structure.dos import CompleteDos, LobsterCompleteDos
1919
from pymatgen.io.lobster import (
2020
Bandoverlaps,
2121
Charge,
@@ -1450,6 +1450,7 @@ def get_lobster_calc_quality_summary(
14501450
bandoverlaps_obj: Bandoverlaps | None = None,
14511451
lobster_completedos_obj: LobsterCompleteDos | None = None,
14521452
vasprun_obj: Vasprun | None = None,
1453+
ref_dos_obj: CompleteDos | None = None,
14531454
dos_comparison: bool = False,
14541455
e_range: list = [-5, 0],
14551456
n_bins: int | None = None,
@@ -1474,6 +1475,7 @@ def get_lobster_calc_quality_summary(
14741475
:param bandoverlaps_obj: pymatgen lobster.io.BandOverlaps object
14751476
:param lobster_completedos_obj: pymatgen.electronic_structure.dos.LobsterCompleteDos object
14761477
:param vasprun_obj: pymatgen vasp.io.Vasprun object
1478+
:param ref_dos_obj: pymatgen.electronic_structure.dos.CompleteDos object
14771479
:param dos_comparison: will compare DOS from VASP and LOBSTER and return tanimoto index
14781480
:param e_range: energy range for DOS comparisons
14791481
:param n_bins: number of bins to discretize DOS for comparisons
@@ -1635,7 +1637,7 @@ def get_lobster_calc_quality_summary(
16351637
stacklevel=2,
16361638
)
16371639
if dos_comparison:
1638-
if "LSO" not in str(path_to_doscar).split("."):
1640+
if "LSO" not in str(path_to_doscar).split(".") and lobster_completedos_obj is None:
16391641
warnings.warn(
16401642
"Consider using DOSCAR.LSO.lobster, as non LSO DOS from LOBSTER can have negative DOS values",
16411643
stacklevel=2,
@@ -1655,63 +1657,68 @@ def get_lobster_calc_quality_summary(
16551657
"Dos comparison is requested, so please provide either path_to_doscar or lobster_completedos_obj"
16561658
)
16571659

1658-
if path_to_vasprun:
1659-
vasprun = Vasprun(path_to_vasprun, parse_potcar_file=False, parse_eigen=False)
1660-
elif vasprun_obj:
1661-
vasprun = vasprun_obj
1660+
if path_to_vasprun and not ref_dos_obj:
1661+
dos_vasp = Vasprun(path_to_vasprun, parse_potcar_file=False, parse_eigen=False).complete_dos
1662+
elif vasprun_obj and not ref_dos_obj:
1663+
dos_vasp = vasprun_obj.complete_dos
1664+
elif ref_dos_obj:
1665+
dos_vasp = ref_dos_obj
16621666
else:
16631667
raise ValueError(
1664-
"Dos comparison is requested, so please provide either path to vasprun.xml or vasprun_obj"
1668+
"Dos comparison is requested, so please provide either path to vasprun.xml or "
1669+
"vasprun_obj or ref_dos_obj"
16651670
)
1666-
dos_vasp = vasprun.complete_dos
16671671

16681672
quality_dict["dos_comparisons"] = {} # type: ignore
16691673

1670-
for orb in dos_lobster.get_spd_dos():
1671-
if e_range[0] >= min(dos_vasp.energies) and e_range[0] >= min(dos_lobster.energies):
1672-
min_e = e_range[0]
1673-
else:
1674-
warnings.warn(
1675-
"Minimum energy range requested for DOS comparisons is not available "
1676-
"in VASP or LOBSTER calculation. Thus, setting min_e to -5 eV",
1677-
stacklevel=2,
1678-
)
1679-
min_e = -5
1674+
min_e = int(max(e_range[0], min(dos_vasp.energies), min(dos_lobster.energies)))
1675+
max_e = int(min(e_range[-1], max(dos_vasp.energies), max(dos_lobster.energies)))
16801676

1681-
if e_range[-1] <= max(dos_vasp.energies) and e_range[-1] <= max(dos_lobster.energies):
1682-
max_e = e_range[-1]
1683-
else:
1684-
warnings.warn(
1685-
"Maximum energy range requested for DOS comparisons is not available "
1686-
"in VASP or LOBSTER calculation. Thus, setting max_e to 0 eV",
1687-
stacklevel=2,
1688-
)
1689-
max_e = 0
1690-
1691-
if np.diff(dos_vasp.energies)[0] >= 0.1 or np.diff(dos_lobster.energies)[0] >= 0.1:
1692-
warnings.warn(
1693-
"Input DOS files have very few points in the energy interval and thus "
1694-
"comparisons will not be reliable. Please rerun the calculations with "
1695-
"higher number of DOS points. Set NEDOS and COHPSteps tags to >= 2000 in VASP and LOBSTER "
1696-
"calculations, respectively.",
1697-
stacklevel=2,
1698-
)
1677+
if min_e > e_range[0]:
1678+
warnings.warn(
1679+
f"Minimum energy range requested for DOS comparisons is not available in "
1680+
"VASP or LOBSTER calculation. "
1681+
f"Thus, setting `min_e` to the minimum possible value of {min_e} eV",
1682+
stacklevel=2,
1683+
)
1684+
if max_e < e_range[-1]:
1685+
warnings.warn(
1686+
f"Maximum energy range requested for DOS comparisons is not available in "
1687+
"VASP or LOBSTER calculation. "
1688+
f"Thus, setting `max_e` to the maximum possible value of {max_e} eV",
1689+
stacklevel=2,
1690+
)
1691+
1692+
minimum_n_bins = min(
1693+
len(dos_vasp.energies[(dos_vasp.energies >= min_e) & (dos_vasp.energies <= max_e)]),
1694+
len(dos_lobster.energies[(dos_lobster.energies >= min_e) & (dos_lobster.energies <= max_e)]),
1695+
)
1696+
1697+
n_bins = n_bins or minimum_n_bins
1698+
1699+
if n_bins > minimum_n_bins:
1700+
warnings.warn(
1701+
f"Number of bins requested for DOS comparisons is larger than the "
1702+
"number of points in the energy interval. Thus, setting "
1703+
f"`n_bins` to {minimum_n_bins}.",
1704+
stacklevel=2,
1705+
)
1706+
n_bins = minimum_n_bins
16991707

1700-
if not n_bins:
1701-
n_bins = 56
1708+
dos_fp_kwargs = {
1709+
"min_e": min_e,
1710+
"max_e": max_e,
1711+
"n_bins": n_bins,
1712+
"normalize": True,
1713+
}
17021714

1715+
for orb in dos_lobster.get_spd_dos():
17031716
fp_lobster_orb = dos_lobster.get_dos_fp(
1704-
min_e=min_e,
1705-
max_e=max_e,
1706-
n_bins=n_bins,
1707-
normalize=True,
1717+
**dos_fp_kwargs,
17081718
fp_type=orb.name,
17091719
)
17101720
fp_vasp_orb = dos_vasp.get_dos_fp(
1711-
min_e=min_e,
1712-
max_e=max_e,
1713-
n_bins=n_bins,
1714-
normalize=True,
1721+
**dos_fp_kwargs,
17151722
fp_type=orb.name,
17161723
)
17171724

@@ -1722,17 +1729,11 @@ def get_lobster_calc_quality_summary(
17221729
quality_dict["dos_comparisons"][f"tanimoto_orb_{orb.name}"] = tani_orb # type: ignore
17231730

17241731
fp_lobster = dos_lobster.get_dos_fp(
1725-
min_e=min_e,
1726-
max_e=max_e,
1727-
n_bins=n_bins,
1728-
normalize=True,
1732+
**dos_fp_kwargs,
17291733
fp_type="summed_pdos",
17301734
)
17311735
fp_vasp = dos_vasp.get_dos_fp(
1732-
min_e=min_e,
1733-
max_e=max_e,
1734-
n_bins=n_bins,
1735-
normalize=True,
1736+
**dos_fp_kwargs,
17361737
fp_type="summed_pdos",
17371738
)
17381739

tests/cli/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def test_calc_quality_summary_nacl(self, tmp_path):
553553
"The atomic charge signs from Mulliken population analysis agree with the bond valence analysis. "
554554
"The atomic charge signs from Loewdin population analysis agree with the bond valence analysis. "
555555
"The Tanimoto index from DOS comparisons in the energy range between -20, 0 eV for s, p, summed orbitals "
556-
"are: 0.9935, 0.9983, 0.9822."
556+
"are: 0.9795, 0.9864, 0.9628."
557557
)
558558

559559
assert calc_quality_text == ref_text
@@ -595,7 +595,7 @@ def test_calc_quality_summary_k3sb(self, tmp_path):
595595
"The atomic charge signs from Mulliken population analysis agree with the bond valence analysis. "
596596
"The atomic charge signs from Loewdin population analysis agree with the bond valence analysis. "
597597
"The Tanimoto index from DOS comparisons in the energy range between -20, 0 eV for s, p, summed orbitals "
598-
"are: 0.8367, 0.9565, 0.9357."
598+
"are: 0.7953, 0.8994, 0.8763."
599599
)
600600

601601
assert calc_quality_text == ref_text

tests/cohp/test_describe.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -362,28 +362,26 @@ def test_warnings(self):
362362
e_range=[-50, 60],
363363
dos_comparison=True,
364364
bva_comp=False,
365+
n_bins=500,
365366
)
366-
messages = []
367-
for warning in w:
368-
messages.append(str(warning.message))
369-
count0 = 0
370-
count1 = 0
371-
count2 = 0
372-
count3 = 0
373-
for msg in messages:
374-
if "Consider using DOSCAR.LSO.lobster" in msg:
375-
count0 += 1
376-
if "Minimum energy range requested" in msg:
377-
count1 += 1
378-
if "Maximum energy range requested" in msg:
379-
count2 += 1
380-
if "Input DOS files have very few points" in msg:
381-
count3 += 1
367+
# messages = []
368+
actual_warnings = [str(warning.message) for warning in w]
382369

383-
assert count0 == 1
384-
assert count1 == 1
385-
assert count2 == 1
386-
assert count3 == 1
370+
expected_warnings = [
371+
"Consider using DOSCAR.LSO.lobster, as non LSO DOS from LOBSTER can have negative DOS values",
372+
"Minimum energy range requested for DOS comparisons is not available in VASP or LOBSTER calculation. "
373+
"Thus, setting `min_e` to the minimum possible value of -15 eV",
374+
"Maximum energy range requested for DOS comparisons is not available in VASP or LOBSTER calculation. "
375+
"Thus, setting `max_e` to the maximum possible value of 5 eV",
376+
"Number of bins requested for DOS comparisons is larger than the number of points in the energy interval. "
377+
"Thus, setting `n_bins` to 107.",
378+
"Input DOS files have very few points in the energy interval and thus comparisons will not be reliable. "
379+
"Please rerun the calculations with higher number of DOS points. "
380+
"Set NEDOS and COHPSteps tags to >= 2000 in VASP and LOBSTER calculations, respectively.",
381+
]
382+
383+
for actual, expected in zip(actual_warnings, expected_warnings):
384+
assert actual == expected
387385

388386
calc_des = Description.get_calc_quality_description(calc_quality_warnings)
389387

@@ -392,8 +390,8 @@ def test_warnings(self):
392390
"The absolute and total charge spilling for the calculation is 2.255 and 12.72 %, respectively.",
393391
"The projected wave function is completely orthonormalized as no bandOverlaps.lobster file is "
394392
"generated during the LOBSTER run.",
395-
"The Tanimoto index from DOS comparisons in the energy range between -5, 0 eV for s, p, summed orbitals "
396-
"are: 0.4057, 0.2831, 0.2762.",
393+
"The Tanimoto index from DOS comparisons in the energy range between -15, 5 eV for s, p, summed orbitals "
394+
"are: 0.6712, 0.8113, 0.8064.",
397395
]
398396

399397
with warnings.catch_warnings(record=True) as w2:

0 commit comments

Comments
 (0)