Skip to content

Commit 82ea137

Browse files
kavanasejanosh
andauthored
Suspected Typo Fix in pymatgen.io.vasp.optics (#2989)
* Small fix for a suspected typo in handling `ismear` * typo * add tests for io.vasp.optics.(delta_func|step_func) --------- Co-authored-by: Janosh Riebesell <[email protected]>
1 parent ed0ec15 commit 82ea137

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

pymatgen/io/vasp/optics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class DielectricFunctionCalculator(MSONable):
4848
- Perform symmetry operations (this is not implemented here)
4949
- Calculate the real part
5050
51-
Currently, this Calculator only works for ``ISYM=0`` calculations since we cannot gauranttee that our
52-
externally defined symmetry operations are the same as VASP's. This can be fixed by printing the
53-
symmetry operators into the vasprun.xml file. If this happens in future versions of VASP,
51+
Currently, this Calculator only works for ``ISYM=0`` calculations since we cannot guarantee that our
52+
externally defined symmetry operations are the same as VASP's. This can be fixed by printing the
53+
symmetry operators into the vasprun.xml file. If this happens in future versions of VASP,
5454
we can dramatically speed up the calculations here by considering only the irreducible kpoints.
5555
"""
5656

@@ -279,7 +279,7 @@ def delta_func(x, ismear):
279279
raise ValueError("Delta function not implemented for ismear < -1")
280280
if ismear == -1:
281281
return step_func(x, -1) * (1 - step_func(x, -1))
282-
if ismear < 0:
282+
if ismear == 0:
283283
return np.exp(-(x * x)) / np.sqrt(np.pi)
284284
return delta_methfessel_paxton(x, ismear)
285285

@@ -290,7 +290,7 @@ def step_func(x, ismear):
290290
raise ValueError("Delta function not implemented for ismear < -1")
291291
if ismear == -1:
292292
return 1 / (1.0 + np.exp(-x))
293-
if ismear < 0:
293+
if ismear == 0:
294294
return 0.5 + 0.5 * scipy.special.erf(x)
295295
return step_methfessel_paxton(x, ismear)
296296

pymatgen/io/vasp/tests/test_optics.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

33
import numpy as np
4+
import pytest
5+
import scipy.special
46

5-
from pymatgen.io.vasp.optics import DielectricFunctionCalculator
7+
from pymatgen.io.vasp.optics import DielectricFunctionCalculator, delta_func, delta_methfessel_paxton, step_func
68
from pymatgen.io.vasp.outputs import Vasprun
79
from pymatgen.util.testing import PymatgenTest
810

@@ -18,6 +20,10 @@ def test_optics(self):
1820
dfc = DielectricFunctionCalculator.from_directory(eps_data_path)
1921
egrid, eps = dfc.get_epsilon(0, 0)
2022

23+
assert egrid[0] == 0
24+
assert egrid[-1] == 59.3802
25+
assert len(egrid) == len(eps) == 3000
26+
2127
_, eps_real_ref, eps_imag_ref = vrun.dielectric
2228
eps_real_ref = np.array(eps_real_ref)[:, 0]
2329
eps_imag_ref = np.array(eps_imag_ref)[:, 0]
@@ -40,3 +46,39 @@ def test_optics(self):
4046
mask = np.ones_like(dfc.cder, dtype=float)
4147
x_val, y_val, text = dfc.plot_weighted_transition_data(0, 0, mask=mask, min_val=0.001)
4248
assert len(x_val) == len(y_val) == len(text)
49+
50+
51+
def test_delta_func():
52+
x = np.array([0, 1, 2, 3, 4, 5])
53+
54+
# ismear < -1
55+
with pytest.raises(ValueError, match="Delta function not implemented for ismear < -1"):
56+
delta_func(x, -2)
57+
58+
# ismear == -1
59+
assert np.all(delta_func(x, -1) == step_func(x, -1) * (1 - step_func(x, -1)))
60+
61+
# ismear == 0
62+
assert np.all(delta_func(x, 0) == np.exp(-(x * x)) / np.sqrt(np.pi))
63+
64+
# ismear > 0
65+
for ismear in [1, 2, 3]:
66+
assert np.all(delta_func(x, ismear) == delta_methfessel_paxton(x, ismear))
67+
68+
69+
def test_step_func():
70+
# array of positive values
71+
x = np.array([1, 2, 3, 4, 5])
72+
assert np.allclose(step_func(x, -1), 1 / (1.0 + np.exp(-x)))
73+
74+
# array of negative values
75+
x = np.array([-1, -2, -3, -4, -5])
76+
assert np.allclose(step_func(x, -1), 1 / (1.0 + np.exp(-x)))
77+
78+
# array that includes zero
79+
x = np.array([-1, 0, 1])
80+
assert np.allclose(step_func(x, -1), 1 / (1.0 + np.exp(-x)))
81+
82+
# ismear == 0
83+
x = np.array([1, 2, 3, 4, 5])
84+
assert np.allclose(step_func(x, 0), 0.5 + 0.5 * scipy.special.erf(x))

0 commit comments

Comments
 (0)