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

Speeding up get_nn_info in local_env.py #3635

Merged
merged 5 commits into from
Feb 20, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions pymatgen/analysis/local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def _get_nn_shell_info(
return list(all_sites.values())

@staticmethod
def _get_image(structure, site):
def _get_image(structure: Structure, site: Site) -> tuple[int, int, int]:
"""Private convenience method for get_nn_info,
gives lattice image from provided PeriodicSite and Structure.

Expand All @@ -552,30 +552,36 @@ def _get_image(structure, site):
Note that this method takes O(number of sites) due to searching an original site.

Args:
structure: Structure Object
site: PeriodicSite Object
structure (Structure): Structure Object
site (Site): PeriodicSite Object

Returns:
image: ((int)*3) Lattice image
tuple[int, int , int] Lattice image
"""
if isinstance(site, PeriodicNeighbor):
return site.image

original_site = structure[NearNeighbors._get_original_site(structure, site)]
image = np.around(np.subtract(site.frac_coords, original_site.frac_coords))
return tuple(image.astype(int))

@staticmethod
def _get_original_site(structure, site):
def _get_original_site(structure: Structure, site: Site) -> int:
"""Private convenience method for get_nn_info,
gives original site index from ProvidedPeriodicSite.
"""
if isinstance(site, PeriodicNeighbor):
return site.index

if isinstance(structure, (IStructure, Structure)):
for i, s in enumerate(structure):
if site.is_periodic_image(s):
return i
for idx, struc_site in enumerate(structure):
if site.is_periodic_image(struc_site):
return idx
else:
for i, s in enumerate(structure):
if site == s:
return i
raise Exception("Site not found!")
for idx, struc_site in enumerate(structure):
if site == struc_site:
return idx
raise ValueError("Site not found in structure")

def get_bonded_structure(
self,
Expand Down
17 changes: 8 additions & 9 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,27 +1492,26 @@ def get_sites_in_sphere(

Args:
pt (3x1 array): Cartesian coordinates of center of sphere.
r (float): Radius of sphere.
r (float): Radius of sphere in Angstrom.
include_index (bool): Whether the non-supercell site index
is included in the returned data
is included in the returned data.
include_image (bool): Whether to include the supercell image
is included in the returned data
is included in the returned data.

Returns:
PeriodicNeighbor
"""
site_fcoords = np.mod(self.frac_coords, 1)
neighbors: list[PeriodicNeighbor] = []
for frac_coord, dist, i, img in self._lattice.get_points_in_sphere(site_fcoords, pt, r):
for frac_coord, dist, idx, img in self._lattice.get_points_in_sphere(self.frac_coords, pt, r):
nn_site = PeriodicNeighbor(
self[i].species,
self[idx].species,
frac_coord,
self._lattice,
properties=self[i].properties,
properties=self[idx].properties,
nn_distance=dist,
image=img, # type: ignore
index=i,
label=self[i].label,
index=idx,
label=self[idx].label,
)
neighbors.append(nn_site)
return neighbors
Expand Down