Skip to content

Commit

Permalink
chore: renames
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Dec 7, 2024
1 parent 5716991 commit 683025b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
58 changes: 37 additions & 21 deletions src/py/mat3ra/made/tools/analyze/coordination.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ def are_bonds_templates_similar(template1: np.ndarray, template2: np.ndarray, to
return True


def find_template_vectors(
atom_vectors: List[List[np.ndarray]], atom_elements: List[str], angle_tolerance: float = 0.1
) -> Dict[str, List[np.ndarray]]:
def find_template_vectors(material: Material, angle_tolerance: float = 0.1) -> Dict[str, List[np.ndarray]]:
"""
Find unique bond templates for each element type.
Expand All @@ -220,26 +218,44 @@ def find_template_vectors(
"""
element_templates = {}

for element in set(atom_elements):
element_indices = [i for i, e in enumerate(atom_elements) if e == element]
element_vector_lists = [np.array(atom_vectors[i]) for i in element_indices]
for element in set(material.basis.elements.values):
element_templates[element] = find_template_vectors_for_element(material, element, angle_tolerance)

if not element_vector_lists:
continue
return element_templates

max_coord = max(len(vectors) for vectors in element_vector_lists)
max_coord_vectors = [v for v in element_vector_lists if len(v) == max_coord]

unique_templates: List[np.ndarray] = []
for template in max_coord_vectors:
if not any(
are_bonds_templates_similar(template, existing, angle_tolerance) for existing in unique_templates
):
unique_templates.append(template)
def find_template_vectors_for_element(
material: Material, element: str, angle_tolerance: float = 0.1
) -> List[np.ndarray]:
"""
Find unique bond templates for a single element type.
element_templates[element] = unique_templates
Args:
material (Material): The material object.
element (str): Chemical element to find templates for.
angle_tolerance (float): Tolerance for comparing angles between bond vectors.
return element_templates
Returns:
List[np.ndarray]: List of unique bond templates for the element.
"""
atom_vectors = get_nearest_neighbors_vectors(material).values
atom_elements = material.basis.elements.values

element_indices = [i for i, e in enumerate(atom_elements) if e == element]
element_vector_lists = [np.array(atom_vectors[i]) for i in element_indices]

if not element_vector_lists:
return []

max_coordination_number = max(len(vectors) for vectors in element_vector_lists)
max_coordination_number_vectors = [v for v in element_vector_lists if len(v) == max_coordination_number]

unique_templates: List[np.ndarray] = []
for template in max_coordination_number_vectors:
if not any(are_bonds_templates_similar(template, existing, angle_tolerance) for existing in unique_templates):
unique_templates.append(template)

return unique_templates


def reconstruct_missing_bonds_for_element(
Expand All @@ -266,9 +282,9 @@ def reconstruct_missing_bonds_for_element(
return []

existing_vectors = np.array(vectors) if vectors else np.empty((0, 3))
max_coordination = len(templates[element][0])
max_coordination_number = len(templates[element][0])

if len(existing_vectors) >= max_coordination:
if len(existing_vectors) >= max_coordination_number:
return []

best_missing = None
Expand Down Expand Up @@ -297,7 +313,7 @@ def reconstruct_missing_bonds_for_element(
num_bonds_to_add = min(
len(best_missing),
max_bonds_to_passivate,
max_coordination - len(existing_vectors),
max_coordination_number - len(existing_vectors),
)
return best_missing[:num_bonds_to_add].tolist()

Expand Down
4 changes: 1 addition & 3 deletions src/py/mat3ra/made/tools/build/passivation/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ def create_passivated_material(self, configuration: PassivationConfiguration) ->
nearest_neighbors_vectors_array = get_nearest_neighbors_vectors(
material=material, cutoff=self.build_parameters.shadowing_radius
)
templates = coordination.find_template_vectors(
nearest_neighbors_vectors_array.values, material.basis.elements.values
)
templates = coordination.find_template_vectors(material)
reconstructed_bonds = coordination.reconstruct_missing_bonds(
nearest_neighbors_vectors_array.values,
material.basis.elements.values,
Expand Down
2 changes: 1 addition & 1 deletion tests/py/unit/test_tools_build_passivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_get_unique_coordination_numbers():
assert unique_coordination_numbers == [2, 3]


def test_passivate_edge():
def test_passivate_coordination_based():
config = PassivationConfiguration(slab=Material(GRAPHENE_ZIGZAG_NANORIBBON), passivant="H", bond_length=1.48)
params = CoordinationBasedPassivationBuilderParameters(
shadowing_radius=2.5, coordination_threshold=2, bonds_to_passivate=1
Expand Down

0 comments on commit 683025b

Please sign in to comment.