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/SOF 7453 #187

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
28 changes: 20 additions & 8 deletions src/py/mat3ra/made/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
DEFAULT_TYPE = "TRI"


class LatticeVectors(BaseModel):
"""
A class to represent the lattice vectors.
"""

a: List[float] = [1.0, 0.0, 0.0]
b: List[float] = [0.0, 1.0, 0.0]
c: List[float] = [0.0, 0.0, 1.0]


class Lattice(RoundNumericValuesMixin, BaseModel):
a: float = 1.0
b: float = a
Expand All @@ -23,7 +33,7 @@ class Lattice(RoundNumericValuesMixin, BaseModel):
type: str = DEFAULT_TYPE

@property
def vectors(self) -> List[List[float]]:
def vectors(self) -> LatticeVectors:
a = self.a
b = self.b
c = self.c
Expand All @@ -44,12 +54,12 @@ def vectors(self) -> List[List[float]]:
cos_gamma_star = math.cos(gamma_star)
sin_gamma_star = math.sin(gamma_star)

# Return the lattice matrix using the derived trigonometric values
return [
[a * sin_beta, 0.0, a * cos_beta],
[-b * sin_alpha * cos_gamma_star, b * sin_alpha * sin_gamma_star, b * cos_alpha],
[0.0, 0.0, c],
]
# Calculate the vectors
vector_a = [a * sin_beta, 0.0, a * cos_beta]
vector_b = [-b * sin_alpha * cos_gamma_star, b * sin_alpha * sin_gamma_star, b * cos_alpha]
vector_c = [0.0, 0.0, c]

return LatticeVectors(a=vector_a, b=vector_b, c=vector_c)

@classmethod
def from_vectors_array(
Expand Down Expand Up @@ -96,7 +106,9 @@ def clone(self, extra_context: Optional[Dict[str, Any]] = None) -> "Lattice":

@property
def vector_arrays(self) -> List[List[float]]:
return self.vectors
"""Returns lattice vectors as a nested array"""
v = self.vectors
return [v.a, v.b, v.c]

@property
def cell(self) -> Cell:
Expand Down
57 changes: 56 additions & 1 deletion src/py/mat3ra/made/material.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Union
from typing import Any, Dict, List, Optional, Union

from mat3ra.code.constants import AtomicCoordinateUnits, Units
from mat3ra.code.entity import HasDescriptionHasMetadataNamedDefaultableInMemoryEntity
Expand Down Expand Up @@ -112,3 +112,58 @@ def add_atom(self, element: str, coordinate: List[float], use_cartesian_coordina
new_basis = self.basis.copy()
new_basis.add_atom(element, coordinate, use_cartesian_coordinates)
self.basis = new_basis

@classmethod
def create_empty(
cls,
a: float = 1.0,
b: Optional[float] = None,
c: Optional[float] = None,
alpha: float = 90.0,
beta: float = 90.0,
gamma: float = 90.0,
lattice_type: str = "CUB",
name: str = "New Material",
) -> "Material":
"""
Create an empty material with specified lattice parameters.

Args:
a (float): First lattice constant. Defaults to 1.0.
b (float, optional): Second lattice constant. Defaults to value of 'a'.
c (float, optional): Third lattice constant. Defaults to value of 'a'.
alpha (float): First lattice angle in degrees. Defaults to 90.0.
beta (float): Second lattice angle in degrees. Defaults to 90.0.
gamma (float): Third lattice angle in degrees. Defaults to 90.0.
lattice_type (str): Type of lattice. Defaults to "CUB".
name (str): Name of the material. Defaults to "New Material".

Returns:
Material: A new empty Material instance with specified lattice
"""
b = b if b is not None else a
c = c if c is not None else a

basis_config = {
"elements": [],
"coordinates": [],
"units": AtomicCoordinateUnits.cartesian,
}

lattice_config = {
"type": lattice_type,
"a": a,
"b": b,
"c": c,
"alpha": alpha,
"beta": beta,
"gamma": gamma,
"units": {
"length": Units.angstrom,
"angle": Units.degree,
},
}

config = {"name": name, "basis": basis_config, "lattice": lattice_config}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code duplicate - needs to be handled at the lattice level or through self.create

return cls(config)
10 changes: 5 additions & 5 deletions src/py/mat3ra/made/tools/build/nanoribbon/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def _calculate_cartesian_dimensions(config: NanoribbonConfiguration, material: M
nanoribbon_length, nanoribbon_width = nanoribbon_width, nanoribbon_length
vacuum_width, vacuum_length = vacuum_length, vacuum_width

length_cartesian = nanoribbon_length * np.dot(np.array(material.lattice.vectors[0]), np.array([1, 0, 0]))
width_cartesian = nanoribbon_width * np.dot(np.array(material.lattice.vectors[1]), np.array([0, 1, 0]))
height_cartesian = np.dot(np.array(material.lattice.vectors[2]), np.array([0, 0, 1]))
vacuum_length_cartesian = vacuum_length * np.dot(np.array(material.lattice.vectors[0]), np.array([1, 0, 0]))
vacuum_width_cartesian = vacuum_width * np.dot(np.array(material.lattice.vectors[1]), np.array([0, 1, 0]))
length_cartesian = nanoribbon_length * np.dot(np.array(material.lattice.vectors.a), np.array([1, 0, 0]))
width_cartesian = nanoribbon_width * np.dot(np.array(material.lattice.vectors.b), np.array([0, 1, 0]))
height_cartesian = np.dot(np.array(material.lattice.vectors.c), np.array([0, 0, 1]))
vacuum_length_cartesian = vacuum_length * np.dot(np.array(material.lattice.vectors.a), np.array([1, 0, 0]))
vacuum_width_cartesian = vacuum_width * np.dot(np.array(material.lattice.vectors.b), np.array([0, 1, 0]))

return length_cartesian, width_cartesian, height_cartesian, vacuum_length_cartesian, vacuum_width_cartesian

Expand Down
23 changes: 22 additions & 1 deletion src/py/mat3ra/made/tools/build/passivation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Union, List
from collections import Counter
from typing import Union, List, Dict

from mat3ra.made.material import Material
from .configuration import PassivationConfiguration
Expand Down Expand Up @@ -36,3 +37,23 @@ def get_unique_coordination_numbers(
material_with_crystal_sites = MaterialWithCrystalSites.from_material(configuration.slab)
material_with_crystal_sites.analyze()
return material_with_crystal_sites.get_unique_coordination_numbers(cutoff=cutoff)

def get_coordination_numbers_distribution(
configuration: PassivationConfiguration,
cutoff: float = 3.0,
) -> Dict[int, int]:
"""
Get the unique coordination numbers for the provided passivation configuration and cutoff radius.

Args:
configuration (PassivationConfiguration): The configuration object.
cutoff (float): The cutoff radius for defining neighbors.
Returns:
set: The unique coordination numbers.
"""
material_with_crystal_sites = MaterialWithCrystalSites.from_material(configuration.slab)
material_with_crystal_sites.analyze()
coordinatation_numbers = material_with_crystal_sites.get_coordination_numbers(cutoff=cutoff)
sorted_coordinatation_numbers = sorted(coordinatation_numbers.values)
distribution = dict(Counter(sorted_coordinatation_numbers))
return distribution
2 changes: 1 addition & 1 deletion src/py/mat3ra/made/tools/build/perturbation/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def create_perturbed_slab(self, configuration: PerturbationConfiguration) -> Mat

class CellMatchingDistancePreservingSlabPerturbationBuilder(DistancePreservingSlabPerturbationBuilder):
def _transform_lattice_vectors(self, configuration: PerturbationConfiguration) -> List[List[float]]:
cell_vectors = configuration.material.lattice.vectors
cell_vectors = configuration.material.lattice.vector_arrays
return [configuration.perturbation_function_holder.transform_coordinates(coord) for coord in cell_vectors]

def create_perturbed_slab(self, configuration: PerturbationConfiguration) -> Material:
Expand Down
Loading
Loading