@mat3ra/made 2024.7.2-0 → 2024.7.7-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/py/mat3ra/made/basis.py +2 -2
- package/src/py/mat3ra/made/cell.py +1 -1
- package/src/py/mat3ra/made/tools/analyze.py +29 -2
- package/src/py/mat3ra/made/tools/convert/__init__.py +3 -0
- package/src/py/mat3ra/made/tools/modify.py +110 -18
- package/src/py/mat3ra/made/tools/third_party.py +2 -0
- package/tests/py/unit/fixtures.py +19 -6
- package/tests/py/unit/test_tools_modify.py +23 -1
package/package.json
CHANGED
|
@@ -67,13 +67,13 @@ class Basis(RoundNumericValuesMixin, BaseModel):
|
|
|
67
67
|
def to_cartesian(self):
|
|
68
68
|
if self.is_in_cartesian_units:
|
|
69
69
|
return
|
|
70
|
-
self.coordinates
|
|
70
|
+
self.coordinates.map_array_in_place(self.cell.convert_point_to_cartesian)
|
|
71
71
|
self.units = AtomicCoordinateUnits.cartesian
|
|
72
72
|
|
|
73
73
|
def to_crystal(self):
|
|
74
74
|
if self.is_in_crystal_units:
|
|
75
75
|
return
|
|
76
|
-
self.coordinates
|
|
76
|
+
self.coordinates.map_array_in_place(self.cell.convert_point_to_crystal)
|
|
77
77
|
self.units = AtomicCoordinateUnits.crystal
|
|
78
78
|
|
|
79
79
|
def add_atom(self, element="Si", coordinate=[0.5, 0.5, 0.5]):
|
|
@@ -44,7 +44,7 @@ class Cell(RoundNumericValuesMixin, BaseModel):
|
|
|
44
44
|
np_vector = np.array(self.vectors_as_nested_array)
|
|
45
45
|
return np.dot(point, np_vector)
|
|
46
46
|
|
|
47
|
-
def
|
|
47
|
+
def convert_point_to_crystal(self, point):
|
|
48
48
|
np_vector = np.array(self.vectors_as_nested_array)
|
|
49
49
|
return np.dot(point, np.linalg.inv(np_vector))
|
|
50
50
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Callable, List, Optional
|
|
1
|
+
from typing import Callable, List, Optional, Literal
|
|
2
2
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
|
|
@@ -211,7 +211,7 @@ def get_atom_indices_with_condition_on_coordinates(
|
|
|
211
211
|
Args:
|
|
212
212
|
material (Material): Material object
|
|
213
213
|
condition (Callable[List[float], bool]): Function that checks if coordinates satisfy the condition.
|
|
214
|
-
|
|
214
|
+
use_cartesian_coordinates (bool): Whether to use Cartesian coordinates for the condition evaluation.
|
|
215
215
|
|
|
216
216
|
Returns:
|
|
217
217
|
List[int]: List of indices of atoms whose coordinates satisfy the condition.
|
|
@@ -229,3 +229,30 @@ def get_atom_indices_with_condition_on_coordinates(
|
|
|
229
229
|
selected_indices.append(coord.id)
|
|
230
230
|
|
|
231
231
|
return selected_indices
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def get_atomic_coordinates_extremum(
|
|
235
|
+
material: Material,
|
|
236
|
+
extremum: Literal["max", "min"] = "max",
|
|
237
|
+
axis: Literal["x", "y", "z"] = "z",
|
|
238
|
+
use_cartesian_coordinates: bool = False,
|
|
239
|
+
) -> float:
|
|
240
|
+
"""
|
|
241
|
+
Return minimum or maximum of coordinates along the specified axis.
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
material (Material): Material object.
|
|
245
|
+
extremum (str): "min" or "max".
|
|
246
|
+
axis (str): "x", "y", or "z".
|
|
247
|
+
use_cartesian_coordinates (bool): Whether to use Cartesian coordinates.
|
|
248
|
+
Returns:
|
|
249
|
+
float: Minimum or maximum of coordinates along the specified axis.
|
|
250
|
+
"""
|
|
251
|
+
new_material = material.clone()
|
|
252
|
+
if use_cartesian_coordinates:
|
|
253
|
+
new_basis = new_material.basis
|
|
254
|
+
new_basis.to_cartesian()
|
|
255
|
+
new_material.basis = new_basis
|
|
256
|
+
coordinates = new_material.basis.coordinates.to_array_of_values_with_ids()
|
|
257
|
+
values = [coord.value[{"x": 0, "y": 1, "z": 2}[axis]] for coord in coordinates]
|
|
258
|
+
return getattr(np, extremum)(values)
|
|
@@ -184,6 +184,8 @@ def to_ase(material_or_material_data: Union[Material, Dict[str, Any]]) -> ASEAto
|
|
|
184
184
|
atoms.set_tags(map_array_with_id_value_to_array(atomic_labels))
|
|
185
185
|
if "metadata" in material_config:
|
|
186
186
|
atoms.info.update({"metadata": material_config["metadata"]})
|
|
187
|
+
|
|
188
|
+
atoms.info.update({"name": material_config["name"]})
|
|
187
189
|
return atoms
|
|
188
190
|
|
|
189
191
|
|
|
@@ -205,6 +207,7 @@ def from_ase(ase_atoms: ASEAtoms) -> Dict[str, Any]:
|
|
|
205
207
|
ase_metadata = ase_atoms.info.get("metadata", {})
|
|
206
208
|
if ase_metadata:
|
|
207
209
|
material["metadata"].update(ase_metadata)
|
|
210
|
+
material["name"] = ase_atoms.info.get("name", "")
|
|
208
211
|
return material
|
|
209
212
|
|
|
210
213
|
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
from typing import Callable, List, Optional, Union
|
|
1
|
+
from typing import Callable, List, Literal, Optional, Union
|
|
2
2
|
|
|
3
|
-
import numpy as np
|
|
4
3
|
from mat3ra.made.material import Material
|
|
5
4
|
|
|
6
|
-
from .analyze import
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
from .analyze import (
|
|
6
|
+
get_atom_indices_with_condition_on_coordinates,
|
|
7
|
+
get_atom_indices_within_radius_pbc,
|
|
8
|
+
get_atomic_coordinates_extremum,
|
|
9
|
+
)
|
|
10
|
+
from .convert import decorator_convert_material_args_kwargs_to_structure, from_ase, to_ase
|
|
11
|
+
from .third_party import PymatgenStructure, ase_add_vacuum
|
|
9
12
|
from .utils import (
|
|
10
13
|
is_coordinate_in_box,
|
|
11
14
|
is_coordinate_in_cylinder,
|
|
12
15
|
is_coordinate_in_triangular_prism,
|
|
13
16
|
is_coordinate_within_layer,
|
|
14
|
-
translate_to_bottom_pymatgen_structure,
|
|
15
17
|
)
|
|
16
18
|
|
|
17
19
|
|
|
@@ -35,22 +37,54 @@ def filter_by_label(material: Material, label: Union[int, str]) -> Material:
|
|
|
35
37
|
return new_material
|
|
36
38
|
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
def translate_to_z_level(
|
|
41
|
+
material: Material, z_level: Optional[Literal["top", "bottom", "center"]] = "bottom"
|
|
42
|
+
) -> Material:
|
|
40
43
|
"""
|
|
41
|
-
Translate atoms to the
|
|
42
|
-
If use_conventional_cell is passed, conventional cell is used.
|
|
44
|
+
Translate atoms to the specified z-level.
|
|
43
45
|
|
|
44
46
|
Args:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
material (Material): The material object to normalize.
|
|
48
|
+
z_level (str): The z-level to translate the atoms to (top, bottom, center)
|
|
47
49
|
Returns:
|
|
48
|
-
|
|
50
|
+
Material: The translated material object.
|
|
49
51
|
"""
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
min_z = get_atomic_coordinates_extremum(material, "min")
|
|
53
|
+
max_z = get_atomic_coordinates_extremum(material)
|
|
54
|
+
if z_level == "top":
|
|
55
|
+
material = translate_by_vector(material, vector=[0, 0, 1 - max_z])
|
|
56
|
+
elif z_level == "bottom":
|
|
57
|
+
material = translate_by_vector(material, vector=[0, 0, -min_z])
|
|
58
|
+
elif z_level == "center":
|
|
59
|
+
material = translate_by_vector(material, vector=[0, 0, (1 - min_z - max_z) / 2])
|
|
60
|
+
return material
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def translate_by_vector(
|
|
64
|
+
material: Material,
|
|
65
|
+
vector: Optional[List[float]] = None,
|
|
66
|
+
use_cartesian_coordinates: bool = False,
|
|
67
|
+
) -> Material:
|
|
68
|
+
"""
|
|
69
|
+
Translate atoms by a vector.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
material (Material): The material object to normalize.
|
|
73
|
+
vector (List[float]): The vector to translate the atoms by (in crystal coordinates by default).
|
|
74
|
+
use_cartesian_coordinates (bool): Whether to use cartesian coordinates.
|
|
75
|
+
Returns:
|
|
76
|
+
Material: The translated material object.
|
|
77
|
+
"""
|
|
78
|
+
if not use_cartesian_coordinates:
|
|
79
|
+
vector = material.basis.cell.convert_point_to_cartesian(vector)
|
|
80
|
+
|
|
81
|
+
if vector is None:
|
|
82
|
+
vector = [0, 0, 0]
|
|
83
|
+
|
|
84
|
+
atoms = to_ase(material)
|
|
85
|
+
# ASE accepts cartesian coordinates for translation
|
|
86
|
+
atoms.translate(tuple(vector))
|
|
87
|
+
return Material(from_ase(atoms))
|
|
54
88
|
|
|
55
89
|
|
|
56
90
|
@decorator_convert_material_args_kwargs_to_structure
|
|
@@ -140,7 +174,7 @@ def filter_by_layers(
|
|
|
140
174
|
if central_atom_id is not None:
|
|
141
175
|
center_coordinate = material.basis.coordinates.get_element_value_by_index(central_atom_id)
|
|
142
176
|
vectors = material.lattice.vectors
|
|
143
|
-
direction_vector =
|
|
177
|
+
direction_vector = vectors[2]
|
|
144
178
|
|
|
145
179
|
def condition(coordinate):
|
|
146
180
|
return is_coordinate_within_layer(coordinate, center_coordinate, direction_vector, layer_thickness)
|
|
@@ -323,3 +357,61 @@ def filter_by_triangle_projection(
|
|
|
323
357
|
return filter_by_condition_on_coordinates(
|
|
324
358
|
material, condition, use_cartesian_coordinates=use_cartesian_coordinates, invert_selection=invert_selection
|
|
325
359
|
)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def add_vacuum(material: Material, vacuum: float = 5.0, on_top=True, to_bottom=False) -> Material:
|
|
363
|
+
"""
|
|
364
|
+
Add vacuum to the material along the c-axis.
|
|
365
|
+
On top, on bottom, or both.
|
|
366
|
+
|
|
367
|
+
Args:
|
|
368
|
+
material (Material): The material object to add vacuum to.
|
|
369
|
+
vacuum (float): The thickness of the vacuum to add in angstroms.
|
|
370
|
+
on_top (bool): Whether to add vacuum on top.
|
|
371
|
+
to_bottom (bool): Whether to add vacuum on bottom.
|
|
372
|
+
|
|
373
|
+
Returns:
|
|
374
|
+
Material: The material object with vacuum added.
|
|
375
|
+
"""
|
|
376
|
+
new_material_atoms = to_ase(material)
|
|
377
|
+
vacuum_amount = vacuum * 2 if on_top and to_bottom else vacuum
|
|
378
|
+
ase_add_vacuum(new_material_atoms, vacuum_amount)
|
|
379
|
+
new_material = Material(from_ase(new_material_atoms))
|
|
380
|
+
if to_bottom and not on_top:
|
|
381
|
+
new_material = translate_to_z_level(new_material, z_level="top")
|
|
382
|
+
elif on_top and to_bottom:
|
|
383
|
+
new_material = translate_to_z_level(new_material, z_level="center")
|
|
384
|
+
return new_material
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def remove_vacuum(material: Material, from_top=True, from_bottom=True, fixed_padding=1.0) -> Material:
|
|
388
|
+
"""
|
|
389
|
+
Remove vacuum from the material along the c-axis.
|
|
390
|
+
From top, from bottom, or from both.
|
|
391
|
+
|
|
392
|
+
Args:
|
|
393
|
+
material (Material): The material object to set the vacuum thickness.
|
|
394
|
+
from_top (bool): Whether to remove vacuum from the top.
|
|
395
|
+
from_bottom (bool): Whether to remove vacuum from the bottom.
|
|
396
|
+
fixed_padding (float): The fixed padding of vacuum to add to avoid collisions in pbc (in angstroms).
|
|
397
|
+
|
|
398
|
+
Returns:
|
|
399
|
+
Material: The material object with the vacuum thickness set.
|
|
400
|
+
"""
|
|
401
|
+
translated_material = translate_to_z_level(material, z_level="bottom")
|
|
402
|
+
new_basis = translated_material.basis
|
|
403
|
+
new_basis.to_cartesian()
|
|
404
|
+
new_lattice = translated_material.lattice
|
|
405
|
+
new_lattice.c = get_atomic_coordinates_extremum(translated_material, use_cartesian_coordinates=True) + fixed_padding
|
|
406
|
+
new_basis.cell.vector3 = new_lattice.vectors[2]
|
|
407
|
+
new_basis.to_crystal()
|
|
408
|
+
new_material = material.clone()
|
|
409
|
+
|
|
410
|
+
new_material.basis = new_basis
|
|
411
|
+
new_material.lattice = new_lattice
|
|
412
|
+
|
|
413
|
+
if from_top and not from_bottom:
|
|
414
|
+
new_material = translate_to_z_level(new_material, z_level="top")
|
|
415
|
+
if from_bottom and not from_top:
|
|
416
|
+
new_material = translate_to_z_level(new_material, z_level="bottom")
|
|
417
|
+
return new_material
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from ase import Atoms as ASEAtoms
|
|
2
|
+
from ase.build import add_vacuum as ase_add_vacuum
|
|
2
3
|
from ase.build.supercells import make_supercell as ase_make_supercell
|
|
3
4
|
from ase.calculators.calculator import Calculator as ASECalculator
|
|
4
5
|
from ase.calculators.emt import EMT as ASECalculatorEMT
|
|
@@ -36,6 +37,7 @@ __all__ = [
|
|
|
36
37
|
"PymatgenInterstitial",
|
|
37
38
|
"label_pymatgen_slab_termination",
|
|
38
39
|
"ase_make_supercell",
|
|
40
|
+
"ase_add_vacuum",
|
|
39
41
|
"PymatgenAseAtomsAdaptor",
|
|
40
42
|
"PymatgenPoscar",
|
|
41
43
|
]
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
from typing import Any, Dict
|
|
3
|
+
|
|
1
4
|
from ase.build import bulk
|
|
2
5
|
from mat3ra.made.material import Material
|
|
3
6
|
from mat3ra.made.tools.build.interface.termination_pair import TerminationPair
|
|
@@ -59,7 +62,7 @@ INTERFACE_STRUCTURE.interface_properties = INTERFACE_PROPERTIES_MOCK
|
|
|
59
62
|
INTERFACE_NAME = "Cu4(001)-Si8(001), Interface, Strain 0.062pct"
|
|
60
63
|
|
|
61
64
|
# TODO: Use fixtures package when available
|
|
62
|
-
SI_CONVENTIONAL_CELL = {
|
|
65
|
+
SI_CONVENTIONAL_CELL: Dict[str, Any] = {
|
|
63
66
|
"name": "Si8",
|
|
64
67
|
"basis": {
|
|
65
68
|
"elements": [
|
|
@@ -110,7 +113,7 @@ SI_CONVENTIONAL_CELL = {
|
|
|
110
113
|
"isUpdated": True,
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
SI_SUPERCELL_2X2X1 = {
|
|
116
|
+
SI_SUPERCELL_2X2X1: Dict[str, Any] = {
|
|
114
117
|
"name": "Si8",
|
|
115
118
|
"basis": {
|
|
116
119
|
"elements": [
|
|
@@ -162,7 +165,7 @@ SI_SUPERCELL_2X2X1 = {
|
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
|
|
165
|
-
SI_SLAB_CONFIGURATION = {
|
|
168
|
+
SI_SLAB_CONFIGURATION: Dict[str, Any] = {
|
|
166
169
|
"type": "SlabConfiguration",
|
|
167
170
|
"bulk": SI_CONVENTIONAL_CELL,
|
|
168
171
|
"miller_indices": (0, 0, 1),
|
|
@@ -173,9 +176,7 @@ SI_SLAB_CONFIGURATION = {
|
|
|
173
176
|
"use_orthogonal_z": True,
|
|
174
177
|
}
|
|
175
178
|
|
|
176
|
-
|
|
177
|
-
SI_SLAB = {
|
|
178
|
-
"name": "Si8(001), termination Si_P4/mmm_1, Slab",
|
|
179
|
+
SI_SLAB: Dict[str, Any] = {
|
|
179
180
|
"basis": {
|
|
180
181
|
"elements": [
|
|
181
182
|
{"id": 0, "value": "Si"},
|
|
@@ -211,6 +212,7 @@ SI_SLAB = {
|
|
|
211
212
|
"units": "angstrom",
|
|
212
213
|
},
|
|
213
214
|
},
|
|
215
|
+
"name": "Si8(001), termination Si_P4/mmm_1, Slab",
|
|
214
216
|
"isNonPeriodic": False,
|
|
215
217
|
"_id": "",
|
|
216
218
|
"metadata": {
|
|
@@ -220,3 +222,14 @@ SI_SLAB = {
|
|
|
220
222
|
},
|
|
221
223
|
"isUpdated": True,
|
|
222
224
|
}
|
|
225
|
+
|
|
226
|
+
SI_SLAB_VACUUM = copy.deepcopy(SI_SLAB)
|
|
227
|
+
SI_SLAB_VACUUM["basis"]["coordinates"] = [
|
|
228
|
+
{"id": 0, "value": [0.5, 0.5, 0.386029718]},
|
|
229
|
+
{"id": 1, "value": [0.5, 0.0, 0.4718141]},
|
|
230
|
+
{"id": 2, "value": [0.0, 0.0, 0.557598482]},
|
|
231
|
+
{"id": 3, "value": [-0.0, 0.5, 0.643382864]},
|
|
232
|
+
]
|
|
233
|
+
SI_SLAB_VACUUM["basis"]["cell"] = [[3.867, 0.0, 0.0], [-0.0, 3.867, 0.0], [0.0, 0.0, 15.937527692]]
|
|
234
|
+
SI_SLAB_VACUUM["lattice"]["c"] = 15.937527692
|
|
235
|
+
SI_SLAB_VACUUM["lattice"]["vectors"]["c"] = [0.0, 0.0, 15.937527692]
|
|
@@ -2,16 +2,19 @@ from ase.build import bulk
|
|
|
2
2
|
from mat3ra.made.material import Material
|
|
3
3
|
from mat3ra.made.tools.convert import from_ase
|
|
4
4
|
from mat3ra.made.tools.modify import (
|
|
5
|
+
add_vacuum,
|
|
5
6
|
filter_by_circle_projection,
|
|
6
7
|
filter_by_label,
|
|
7
8
|
filter_by_layers,
|
|
8
9
|
filter_by_rectangle_projection,
|
|
9
10
|
filter_by_sphere,
|
|
10
11
|
filter_by_triangle_projection,
|
|
12
|
+
remove_vacuum,
|
|
13
|
+
translate_to_z_level,
|
|
11
14
|
)
|
|
12
15
|
from mat3ra.utils import assertion as assertion_utils
|
|
13
16
|
|
|
14
|
-
from .fixtures import SI_CONVENTIONAL_CELL
|
|
17
|
+
from .fixtures import SI_CONVENTIONAL_CELL, SI_SLAB, SI_SLAB_VACUUM
|
|
15
18
|
|
|
16
19
|
COMMON_PART = {
|
|
17
20
|
"units": "crystal",
|
|
@@ -136,3 +139,22 @@ def test_filter_by_triangle_projection():
|
|
|
136
139
|
cavity = filter_by_triangle_projection(material, [0.4, 0.4], [0.4, 0.5], [0.5, 0.5], invert_selection=True)
|
|
137
140
|
assertion_utils.assert_deep_almost_equal(expected_basis_sphere_cluster, section.basis.to_json())
|
|
138
141
|
assertion_utils.assert_deep_almost_equal(expected_basis_sphere_cavity, cavity.basis.to_json())
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def test_add_vacuum():
|
|
145
|
+
material = Material(SI_SLAB)
|
|
146
|
+
material_with_vacuum = add_vacuum(material, 5.0)
|
|
147
|
+
assertion_utils.assert_deep_almost_equal(SI_SLAB_VACUUM, material_with_vacuum.to_json())
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_remove_vacuum():
|
|
151
|
+
material_with_vacuum = Material(SI_SLAB_VACUUM)
|
|
152
|
+
vacuum = 6.836
|
|
153
|
+
material_with_no_vacuum = remove_vacuum(material_with_vacuum, from_top=True, from_bottom=True, fixed_padding=0)
|
|
154
|
+
material_with_set_vacuum = add_vacuum(material_with_no_vacuum, vacuum)
|
|
155
|
+
# to compare correctly, we need to translate the expected material to the bottom
|
|
156
|
+
# as it down when setting vacuum to 0
|
|
157
|
+
material = Material(SI_SLAB)
|
|
158
|
+
material_down = translate_to_z_level(material, z_level="bottom")
|
|
159
|
+
|
|
160
|
+
assertion_utils.assert_deep_almost_equal(material_down.to_json(), material_with_set_vacuum.to_json())
|