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-7456 adjustments for Twisted Interface Builder #171

Merged
merged 13 commits into from
Nov 6, 2024
64 changes: 51 additions & 13 deletions src/py/mat3ra/made/tools/build/interface/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
rotate,
translate_by_vector,
add_vacuum_sides,
add_vacuum,
)
from ...analyze import get_chemical_formula
from ...convert import to_ase, from_ase, to_pymatgen, PymatgenInterface, ASEAtoms
Expand Down Expand Up @@ -243,7 +244,7 @@ def _generate(self, configuration: _ConfigurationType) -> List[Material]:
def _update_material_name(
self, material: Material, configuration: NanoRibbonTwistedInterfaceConfiguration
) -> Material:
material.name = f"Twisted Nanoribbon Interface ({configuration.twist_angle:.2f}°)"
material.name = f"Twisted Nanoribbon Interface ({configuration.twist_angle:.2f} degrees)"
return material


Expand All @@ -252,13 +253,14 @@ class CommensurateLatticeTwistedInterfaceBuilderParameters(BaseModel):
Parameters for the commensurate lattice interface builder.

Args:
max_repetition_int (int): The maximum search range for commensurate lattices.
max_repetition_int (Optional[int]): The maximum integer for the transformation matrices. If not provided, it
will be determined based on the target angle and the lattice vectors automatically.
angle_tolerance (float): The tolerance for the angle between the commensurate lattices
and the target angle, in degrees.
return_first_match (bool): Whether to return the first match or all matches.
"""

max_repetition_int: int = 10
max_repetition_int: Optional[int] = None
angle_tolerance: float = 0.1
return_first_match: bool = False

Expand All @@ -270,36 +272,64 @@ class CommensurateLatticeTwistedInterfaceBuilder(BaseBuilder):
def _generate(self, configuration: _ConfigurationType) -> List[_GeneratedItemType]:
film = configuration.film
# substrate = configuration.substrate
max_search = self.build_parameters.max_repetition_int
a = film.lattice.vector_arrays[0][:2]
b = film.lattice.vector_arrays[1][:2]
commensurate_lattice_pairs = self.__generate_commensurate_lattices(
configuration, a, b, max_search, configuration.twist_angle
)
max_int = self.build_parameters.max_repetition_int or self.__determine_max_int(a, b, configuration.twist_angle)
commensurate_lattice_pairs: List[CommensurateLatticePair] = []
while not commensurate_lattice_pairs:
commensurate_lattice_pairs = self.__generate_commensurate_lattices(
configuration, a, b, max_int, configuration.twist_angle
)
max_int += 1
timurbazhirov marked this conversation as resolved.
Show resolved Hide resolved

return commensurate_lattice_pairs

def __determine_max_int(self, a: List[float], b: List[float], target_angle: float) -> int:
Copy link
Member

Choose a reason for hiding this comment

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

__get_initial_guess_for_max_int

"""
Determine the maximum integer for the transformation matrices based on the target angle.

Args:
a (List[float]): The a lattice vector.
b (List[float]): The b lattice vector.
target_angle (float): The target twist angle, in degrees.

Returns:
int: The maximum integer for the transformation matrices.
"""
length_a = np.linalg.norm(a)
length_b = np.linalg.norm(b)
average_length = (length_a + length_b) / 2
theta_rad = np.radians(target_angle)

if theta_rad == 0:
Copy link
Member

Choose a reason for hiding this comment

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

Should be with tolerance for zero

max_int = 1
else:
max_int = int(np.ceil(average_length / theta_rad))
return max_int

def __generate_commensurate_lattices(
self,
configuration: TwistedInterfaceConfiguration,
a: List[float],
b: List[float],
max_search: int = 10,
max_int: int,
target_angle: float = 0.0,
) -> List[CommensurateLatticePair]:
"""
Generate all commensurate lattices for a given search range and filter by closeness to target angle.
Generate all commensurate lattices for a given target angle and filter by closeness to target angle.

Args:
configuration (TwistedInterfaceConfiguration): The configuration for the twisted interface.
a (List[float]): The a lattice vector.
b (List[float]): The b lattice vector.
max_search (int): The maximum search range.
target_angle (float): The target angle, in degrees.
max_int (int): The maximum integer for the transformation matrices.
Copy link
Member

Choose a reason for hiding this comment

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

maybe max_supercell_matrix_element_int?

target_angle (float): The target twist angle, in degrees.

Returns:
List[CommensurateLatticePair]: The list of commensurate lattice pairs
"""
matrices = create_2d_supercell_matrices(max_search)
# Generate the supercell matrices using the calculated max_int
matrices = create_2d_supercell_matrices(max_int)
matrix_ab = np.array([a, b])
matrix_ab_inverse = np.linalg.inv(matrix_ab)

Expand Down Expand Up @@ -342,8 +372,10 @@ def _post_process(
new_film = translate_by_vector(
new_film, [0, 0, item.configuration.distance_z], use_cartesian_coordinates=True
)
interface = merge_materials([new_substrate, new_film])
interface = merge_materials([new_substrate, new_film], merge_dangerously=True)
interface.metadata["actual_twist_angle"] = item.angle
if item.configuration.vacuum != 0:
interface = add_vacuum(interface, item.configuration.vacuum)
interfaces.append(interface)
return interfaces

Expand All @@ -354,3 +386,9 @@ def _update_material_metadata(self, material, configuration) -> Material:
"actual_twist_angle"
]
return updated_material

def _update_material_name(
self, material: Material, configuration: NanoRibbonTwistedInterfaceConfiguration
) -> Material:
material.name = f"Twisted Bilayer Interface ({material.metadata['actual_twist_angle']:.2f} degrees)"
return material
1 change: 1 addition & 0 deletions src/py/mat3ra/made/tools/build/interface/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TwistedInterfaceConfiguration(BaseConfiguration):
substrate: Optional[Material] = None
twist_angle: float = 0.0
distance_z: float = 3.0
vacuum: float = 0.0

@property
def _json(self):
Expand Down
Loading