Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inequality in get_linear_interpolated_value #4299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/pymatgen/analysis/local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3745,23 +3745,23 @@ def get_nn_info(self, structure: Structure, n: int):

if self.use_fictive_radius:
# calculate fictive ionic radii
firs = [_get_fictive_ionic_radius(site, neighbor) for neighbor in neighbors]
fict_ionic_radii = [_get_fictive_ionic_radius(site, neighbor) for neighbor in neighbors]
else:
# just use the bond distance
firs = [neighbor.nn_distance for neighbor in neighbors]
fict_ionic_radii = [neighbor.nn_distance for neighbor in neighbors]

# calculate mean fictive ionic radius
mefir = _get_mean_fictive_ionic_radius(firs)
mefir = _get_mean_fictive_ionic_radius(fict_ionic_radii)

# iteratively solve MEFIR; follows equation 4 in Hoppe's EconN paper
prev_mefir = float("inf")
while abs(prev_mefir - mefir) > 1e-4:
# this is guaranteed to converge
prev_mefir = mefir
mefir = _get_mean_fictive_ionic_radius(firs, minimum_fir=mefir)
mefir = _get_mean_fictive_ionic_radius(fict_ionic_radii, minimum_fir=mefir)

siw = []
for nn, fir in zip(neighbors, firs, strict=True):
for nn, fir in zip(neighbors, fict_ionic_radii, strict=True):
if nn.nn_distance < self.cutoff:
w = math.exp(1 - (fir / mefir) ** 6)
if w > self.tol:
Expand Down
7 changes: 5 additions & 2 deletions src/pymatgen/electronic_structure/dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ def get_interpolated_gap(
energies = self.x
below_fermi = [i for i in range(len(energies)) if energies[i] < self.efermi and tdos[i] > tol]
above_fermi = [i for i in range(len(energies)) if energies[i] > self.efermi and tdos[i] > tol]
if not below_fermi or not above_fermi:
return 0.0, self.efermi, self.efermi

vbm_start = max(below_fermi)
cbm_start = min(above_fermi)
if vbm_start == cbm_start:
if vbm_start in [cbm_start, cbm_start - 1]:
return 0.0, self.efermi, self.efermi

# Interpolate between adjacent values
Expand Down Expand Up @@ -311,7 +314,7 @@ def get_interpolated_gap(

vbm_start = max(below_fermi)
cbm_start = min(above_fermi)
if vbm_start == cbm_start:
if vbm_start in [cbm_start, cbm_start - 1]:
return 0.0, self.efermi, self.efermi

# Interpolate between adjacent values
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/util/coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_linear_interpolated_value(x_values: ArrayLike, y_values: ArrayLike, x: f
"""
arr = np.array(sorted(zip(x_values, y_values, strict=True), key=lambda d: d[0]))

indices = np.where(arr[:, 0] >= x)[0]
indices = np.where(arr[:, 0] > x)[0]

if len(indices) == 0 or indices[0] == 0:
raise ValueError(f"{x=} is out of range of provided x_values ({min(x_values)}, {max(x_values)})")
Expand Down
5 changes: 5 additions & 0 deletions tests/util/test_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def test_get_linear_interpolated_value(self):
with pytest.raises(ValueError, match=r"x=6 is out of range of provided x_values \(0, 5\)"):
coord.get_linear_interpolated_value(x_vals, y_vals, 6)

# test when x is equal to first value in x_vals (previously broke, fixed in #4299):
assert coord.get_linear_interpolated_value(x_vals, y_vals, 0) == approx(3)
with pytest.raises(ValueError, match=r"x=-0.5 is out of range of provided x_values \(0, 5\)"):
coord.get_linear_interpolated_value(x_vals, y_vals, -0.5)

def test_in_coord_list(self):
coords = [[0, 0, 0], [0.5, 0.5, 0.5]]
test_coord = [0.1, 0.1, 0.1]
Expand Down