Skip to content

Commit

Permalink
Merge pull request #9 from Materials-Data-Science-and-Informatics/imp…
Browse files Browse the repository at this point in the history
…rove_defect_detection

Improve defect detection
  • Loading branch information
NinadBhat authored Apr 23, 2024
2 parents 03bb611 + 4ab8170 commit 3df4ea1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
40 changes: 20 additions & 20 deletions src/atomid.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,27 @@ def read_crystal_structure_file(
return crystal_structure, system, kg


def annotate_defects_in_crystal_structure(
pyscal_system: System, reference_data_file: str, ref_format: str
) -> KnowledgeGraph:
def identify_defects_in_crystal_structure(
pyscal_system: System, reference_data_file: str, ref_format: Optional[str] = None
) -> dict[str, dict[str, float]]:
"""Annotates defects in the crystal structure using the reference data file."""
actual_positions = pyscal_system.atoms.positions
_, ref_system, _ = read_crystal_structure_file(
reference_data_file, format=ref_format
)
ref_positions = ref_system.atoms.positions
ref_ase = ase_read(reference_data_file, format=ref_format)
ref_positions = ref_ase.positions

defects = analyze_defects(
defects: dict[str, dict[str, float]] = analyze_defects(
reference_positions=ref_positions, actual_positions=actual_positions
)
vacancies = defects.get("Vacancies", {"count": 0, "fraction": 0})

if vacancies["count"] > 0:
logging.info("Adding vacancies to the graph.")
pyscal_system.add_vacancy(
concentration=vacancies["fraction"], number=vacancies["count"]
)

return pyscal_system.graph
return defects


def annotate_crystal_structure(
data_file: str,
format: str,
output_file: str,
annotate_defects: Optional[bool] = None,
reference_data_file: Optional[str] = None,
ref_format: Optional[str] = None,
) -> None:
"""Annotate the crystal structure using pyscal and save the results to a knowledge graph.
Expand All @@ -94,7 +85,9 @@ def annotate_crystal_structure(
-------
None
"""
crystal_structure, system, kg = read_crystal_structure_file(data_file, format="cif")
crystal_structure, system, kg = read_crystal_structure_file(
data_file, format=format
)
system.to_graph()

crystal_type = get_crystal_structure_using_cna(system)
Expand All @@ -110,8 +103,15 @@ def annotate_crystal_structure(
lattice_constant=lattice_constants,
)

if annotate_defects:
if reference_data_file:
logging.info("Annotating defects in the crystal structure")
kg = annotate_defects_in_crystal_structure(system, reference_data_file, format)
defects = identify_defects_in_crystal_structure(
system, reference_data_file, ref_format
)
vacancies = defects.get("Vacancies", {"count": 0, "fraction": 0})

if vacancies["count"] > 0:
system.add_vacancy(
concentration=vacancies["fraction"], number=vacancies["count"]
)
kg.write(output_file, format="ttl")
16 changes: 9 additions & 7 deletions src/point_defect_analysis/wigner_seitz_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import numpy as np


def find_nearest_atom(atom: tuple, atom_positions: np.ndarray) -> Tuple[int, list]:
def find_nearest_atom(
atom: tuple, atom_positions: np.ndarray
) -> Tuple[np.signedinteger, list]:
"""Find the nearest atom to a given defect position.
Parameters
Expand All @@ -23,16 +25,16 @@ def find_nearest_atom(atom: tuple, atom_positions: np.ndarray) -> Tuple[int, lis
The distance between the defect and the nearest atom.
"""
distances = np.linalg.norm(atom_positions - atom, axis=1)
nearest_index: int = np.argmin(distances)
nearest_index: np.signedinteger = np.argmin(distances)
return nearest_index, distances[nearest_index]


def analyze_defects(
reference_positions: list,
actual_positions: list,
reference_positions_list: list,
actual_positions_list: list,
species_ref: Optional[list] = None,
species_actual: Optional[list] = None,
) -> dict:
) -> dict[str, dict[str, float]]:
"""Analyze the lattice for vacancy and interstitial defects.
Parameters
Expand All @@ -48,8 +50,8 @@ def analyze_defects(
A dictionary containing the vacancy and interstitial defects.
"""
reference_positions = np.array(reference_positions)
actual_positions = np.array(actual_positions)
reference_positions: np.ndarray = np.array(reference_positions_list)
actual_positions: np.ndarray = np.array(actual_positions_list)
atom_position_count = np.zeros(len(reference_positions))
substitution_count = np.zeros(len(reference_positions))

Expand Down

0 comments on commit 3df4ea1

Please sign in to comment.