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

feature/SOF-7442 feat: detect local z extremum #157

Merged
merged 7 commits into from
Sep 11, 2024
Merged
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
40 changes: 40 additions & 0 deletions src/py/mat3ra/made/tools/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,43 @@ def get_undercoordinated_atom_indices(
coordination_numbers = get_coordination_numbers(material, indices, cutoff)
undercoordinated_atoms_indices = [i for i, cn in enumerate(coordination_numbers) if cn <= coordination_threshold]
return undercoordinated_atoms_indices


def get_local_extremum_atom_index(
material: Material,
coordinate: List[float],
extremum: Literal["max", "min"] = "max",
vicinity: float = 1.0,
use_cartesian_coordinates: bool = False,
) -> int:
"""
Return the id of the atom with the minimum or maximum z-coordinate
within a certain vicinity of a given (x, y) coordinate.

Args:
material (Material): Material object.
coordinate (List[float]): (x, y, z) coordinate to find the local extremum atom index for.
extremum (str): "min" or "max".
vicinity (float): Radius of the vicinity, in Angstroms.
use_cartesian_coordinates (bool): Whether to use Cartesian coordinates.

Returns:
int: id of the atom with the minimum or maximum z-coordinate.
"""
new_material = material.clone()
new_material.to_cartesian()
if not use_cartesian_coordinates:
coordinate = new_material.basis.cell.convert_point_to_cartesian(coordinate)

coordinates = np.array(new_material.basis.coordinates.values)
ids = np.array(new_material.basis.coordinates.ids)
tree = cKDTree(coordinates[:, :2])
indices = tree.query_ball_point(coordinate[:2], vicinity)
z_values = [(id, coord[2]) for id, coord in zip(ids[indices], coordinates[indices])]

if extremum == "max":
extremum_z_atom = max(z_values, key=lambda item: item[1])
else:
extremum_z_atom = min(z_values, key=lambda item: item[1])

return extremum_z_atom[0]
Loading