AdsorPy 1.0.2__py3-none-any.whl
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.
- adsorpy/VdW_Radii.csv +93 -0
- adsorpy/__init__.py +0 -0
- adsorpy/config.json +56 -0
- adsorpy/molecule_colour.json +111 -0
- adsorpy/molecule_lib.py +657 -0
- adsorpy/randomsequentialadsorption.py +1609 -0
- adsorpy/rsa_calculator.py +340 -0
- adsorpy/rsa_config.py +87 -0
- adsorpy/run_simulation.py +551 -0
- adsorpy-1.0.2.dist-info/METADATA +84 -0
- adsorpy-1.0.2.dist-info/RECORD +13 -0
- adsorpy-1.0.2.dist-info/WHEEL +4 -0
- adsorpy-1.0.2.dist-info/licenses/LICENSE.md +21 -0
adsorpy/molecule_lib.py
ADDED
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
"""Contains all the molecules that can be used in this simulation.
|
|
2
|
+
|
|
3
|
+
Also includes molecule loader scripts, for which the molecule data is not included in this lib.
|
|
4
|
+
You need to supply your own .xyz files, or you can use preconfigured simple shapes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING, Literal, ParamSpec, TypeAlias, cast
|
|
12
|
+
|
|
13
|
+
import matplotlib.pyplot as plt
|
|
14
|
+
import numpy as np
|
|
15
|
+
import shapely
|
|
16
|
+
import shapely.affinity as aff
|
|
17
|
+
from matplotlib.collections import PatchCollection
|
|
18
|
+
from matplotlib.patches import Circle
|
|
19
|
+
from matplotlib.widgets import Slider, TextBox
|
|
20
|
+
from numpy.typing import NDArray
|
|
21
|
+
from shapely import MultiPoint, MultiPolygon, Point, Polygon
|
|
22
|
+
from shapely.ops import unary_union
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from matplotlib import axes, figure
|
|
26
|
+
from matplotlib.axes import Axes
|
|
27
|
+
from matplotlib.figure import Figure
|
|
28
|
+
|
|
29
|
+
plt.rcParams.update(
|
|
30
|
+
{
|
|
31
|
+
"font.size": 16,
|
|
32
|
+
"xtick.top": True,
|
|
33
|
+
"xtick.direction": "in",
|
|
34
|
+
"xtick.major.width": 1.5,
|
|
35
|
+
"xtick.minor.width": 1,
|
|
36
|
+
"ytick.right": True,
|
|
37
|
+
"ytick.direction": "in",
|
|
38
|
+
"ytick.major.width": 1.5,
|
|
39
|
+
"ytick.minor.width": 1,
|
|
40
|
+
"lines.linewidth": 2.5,
|
|
41
|
+
"axes.linewidth": 1.5,
|
|
42
|
+
"figure.constrained_layout.use": True,
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
P = ParamSpec("P") # Helps with static type checkers.
|
|
47
|
+
InDict: TypeAlias = dict[str, str | float | list[str] | Path | None]
|
|
48
|
+
InInDict: TypeAlias = dict[str, InDict]
|
|
49
|
+
|
|
50
|
+
# mypy: plugins = numpy.typing.mypy_plugin
|
|
51
|
+
# Definition of some frequently-used types. Not used by the compiler, just for the user and mypy. Hello user!
|
|
52
|
+
IdxArray = np.ndarray[tuple[int], np.dtype[np.int_]] # Flat index aray of integers.
|
|
53
|
+
BoolArray = np.ndarray[tuple[int], np.dtype[np.bool_]] # Flat Boolean array.
|
|
54
|
+
CoordPair = np.ndarray[tuple[Literal[2], Literal[1]], np.dtype[np.float64]] # 2x1 array of coordinates
|
|
55
|
+
CoordsArray = np.ndarray[tuple[Literal[2], int], np.dtype[np.float64]] # 2xN array of coords.
|
|
56
|
+
Bools2D = np.ndarray[tuple[int, int], np.dtype[np.bool_]]
|
|
57
|
+
GeoArray = np.ndarray[tuple[int], np.dtype[Polygon]]
|
|
58
|
+
FloatArray = NDArray[np.float64]
|
|
59
|
+
RotMatrix = np.ndarray[tuple[Literal[3], Literal[3]], np.dtype[np.float64]]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# Van der Waals radii of atoms. Source: https://doi.org/10.1039/C3DT50599E
|
|
63
|
+
current_dir_file = Path(__file__).parent / "VdW_Radii.csv"
|
|
64
|
+
RADII: dict[str, float] = dict(
|
|
65
|
+
np.genfromtxt(
|
|
66
|
+
current_dir_file,
|
|
67
|
+
dtype=[("s", "U2"), ("f", "f4")],
|
|
68
|
+
encoding="utf-8-sig",
|
|
69
|
+
delimiter=",",
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def discorectangle(
|
|
75
|
+
params: list[float],
|
|
76
|
+
offx: float = 0.0,
|
|
77
|
+
offy: float = 0.0,
|
|
78
|
+
) -> Polygon:
|
|
79
|
+
"""Create a discorectangle using the union of two circles and a rectangle.
|
|
80
|
+
|
|
81
|
+
The circles are automatically approximated using linear segments (error of 1% or less).
|
|
82
|
+
|
|
83
|
+
:param params: Radius of the two circles and distance between the two halves in Angstrom.
|
|
84
|
+
:param offx: X offset.
|
|
85
|
+
:param offy: Y offset.
|
|
86
|
+
:return: The molecule shape as a polygon.
|
|
87
|
+
"""
|
|
88
|
+
radius: float
|
|
89
|
+
distance: float
|
|
90
|
+
offy *= -1.0
|
|
91
|
+
offx *= -1.0
|
|
92
|
+
radius, distance = params # radius: float, distance: float
|
|
93
|
+
circles = MultiPoint(
|
|
94
|
+
[(offx - distance / 2.0, offy), (offx + distance / 2.0, offy)],
|
|
95
|
+
).buffer(radius)
|
|
96
|
+
|
|
97
|
+
rectangle = Polygon(
|
|
98
|
+
[
|
|
99
|
+
(offx - distance / 2.0, offy - radius),
|
|
100
|
+
(offx + distance / 2.0, offy - radius),
|
|
101
|
+
(offx + distance / 2.0, offy + radius),
|
|
102
|
+
(offx - distance / 2.0, offy + radius),
|
|
103
|
+
],
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
return unary_union([rectangle, circles])
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def circulium(
|
|
110
|
+
radius: float,
|
|
111
|
+
x_offset: float = 0.0,
|
|
112
|
+
y_offset: float = 0.0,
|
|
113
|
+
quad_segs: int = 8,
|
|
114
|
+
) -> Polygon:
|
|
115
|
+
"""Create a simple circular polygon.
|
|
116
|
+
|
|
117
|
+
The circles are automatically approximated using linear segments (error of 1% or less).
|
|
118
|
+
|
|
119
|
+
:param radius: Radius of the circle.
|
|
120
|
+
:param x_offset: The x-offset in angstrom.
|
|
121
|
+
:param y_offset: The y-offset in angstrom.
|
|
122
|
+
:param quad_segs: The amount of linear segments in a quarter circle.
|
|
123
|
+
:return: The molecule shape as a polygon.
|
|
124
|
+
"""
|
|
125
|
+
return Point((x_offset, y_offset)).buffer(radius, quad_segs=quad_segs)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def dogbonium(scale: float = 1) -> Polygon:
|
|
129
|
+
"""Make a molecule shaped like a bone. Used as a pathological case.
|
|
130
|
+
|
|
131
|
+
:param scale: scale of the shape.
|
|
132
|
+
:return: The molecule shape as a polygon.
|
|
133
|
+
"""
|
|
134
|
+
positions = np.ones((4, 2))
|
|
135
|
+
positions *= scale
|
|
136
|
+
positions[[1, 2], 0] *= -1.0
|
|
137
|
+
positions[[2, 3], 1] *= -1.0
|
|
138
|
+
positions[:, 1] *= 0.5
|
|
139
|
+
balls: MultiPolygon = MultiPoint(positions).buffer(0.5 * scale)
|
|
140
|
+
rod: Polygon = Polygon(positions)
|
|
141
|
+
dogbone: Polygon = unary_union((rod, balls))
|
|
142
|
+
|
|
143
|
+
return dogbone
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def polygonium(
|
|
147
|
+
verts: int = 3,
|
|
148
|
+
scale: float = 1.0,
|
|
149
|
+
roundedness: float = 0.0,
|
|
150
|
+
) -> Polygon:
|
|
151
|
+
"""Create a simple regular polygon with optional rounding.
|
|
152
|
+
|
|
153
|
+
:param verts: The vertex count.
|
|
154
|
+
:param scale: The scale factor of the polygon.
|
|
155
|
+
:param roundedness: The roundedness.
|
|
156
|
+
:return: The regular (rounded) polygon.
|
|
157
|
+
"""
|
|
158
|
+
points = np.arange(verts, dtype=np.float64)
|
|
159
|
+
points *= 2 * np.pi
|
|
160
|
+
points /= verts
|
|
161
|
+
points = np.column_stack((np.sin(points), np.cos(points)))
|
|
162
|
+
points *= scale
|
|
163
|
+
|
|
164
|
+
molecule = Polygon(points)
|
|
165
|
+
|
|
166
|
+
if roundedness > 0.0:
|
|
167
|
+
center = Point((0.0, 0.0))
|
|
168
|
+
fact = molecule.exterior.hausdorff_distance(center)
|
|
169
|
+
molecule = molecule.buffer(roundedness, resolution=4 * verts)
|
|
170
|
+
fact /= molecule.exterior.hausdorff_distance(center)
|
|
171
|
+
molecule = aff.scale(molecule, xfact=fact, yfact=fact, origin=center)
|
|
172
|
+
|
|
173
|
+
return shapely.make_valid(molecule)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def xyz_reader(
|
|
177
|
+
file_name: str | Path,
|
|
178
|
+
ignore_atoms: str | list[str] | None = None,
|
|
179
|
+
x_offset: float = 0.0,
|
|
180
|
+
y_offset: float = 0.0,
|
|
181
|
+
roll: float = 0.0,
|
|
182
|
+
pitch: float = 0.0,
|
|
183
|
+
yaw: float = 0.0,
|
|
184
|
+
z_trim: float | None = None,
|
|
185
|
+
# *args: P.args,
|
|
186
|
+
# **kwargs: P.kwargs,
|
|
187
|
+
) -> Polygon:
|
|
188
|
+
"""Read files in the xyz format of VASP.
|
|
189
|
+
|
|
190
|
+
:param file_name: The name of the file, including the .xyz extension. Include the path.
|
|
191
|
+
:param ignore_atoms: Atoms to ignore when making the molecule. Useful if the slab is empty.
|
|
192
|
+
:param x_offset: The offset in the x direction.
|
|
193
|
+
:param y_offset: The offset in the x direction.
|
|
194
|
+
:param yaw: Rotation along the x-axis.
|
|
195
|
+
:param pitch: Rotation along the y-axis.
|
|
196
|
+
:param roll: Rotation along the z-axis.
|
|
197
|
+
:param z_trim: The z value below which all molecules are removed.
|
|
198
|
+
|
|
199
|
+
:return: The molecule polygon read from the xyz file.
|
|
200
|
+
"""
|
|
201
|
+
atomkeys, atompos = _initialise_reader(file_name, ignore_atoms, z_trim)
|
|
202
|
+
|
|
203
|
+
ident_mat: np.ndarray[tuple[Literal[3], Literal[3]], np.dtype[np.float64]] = cast(
|
|
204
|
+
"np.ndarray[tuple[Literal[3], Literal[3]], np.dtype[np.float64]]",
|
|
205
|
+
np.identity(3, dtype=np.float64),
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
rollmat = ident_mat.copy()
|
|
209
|
+
pitchmat = ident_mat.copy()
|
|
210
|
+
yawmat = ident_mat.copy()
|
|
211
|
+
fac = np.pi / 180.0
|
|
212
|
+
rollmat[1, [1, 2]] = np.cos(roll * fac), -np.sin(roll * fac)
|
|
213
|
+
rollmat[2, [1, 2]] = np.sin(roll * fac), np.cos(roll * fac)
|
|
214
|
+
pitchmat[0, [0, 2]] = np.cos(pitch * fac), np.sin(pitch * fac)
|
|
215
|
+
pitchmat[2, [0, 2]] = -np.sin(pitch * fac), np.cos(pitch * fac)
|
|
216
|
+
yawmat[0, [0, 1]] = np.cos(yaw * fac), -np.sin(yaw * fac)
|
|
217
|
+
yawmat[1, [0, 1]] = np.sin(yaw * fac), np.cos(yaw * fac)
|
|
218
|
+
|
|
219
|
+
ident_mat[:] = ident_mat @ yawmat @ pitchmat @ rollmat
|
|
220
|
+
|
|
221
|
+
for crdx, coords in enumerate(atompos):
|
|
222
|
+
atompos[crdx] = coords @ ident_mat
|
|
223
|
+
atom_list = [Polygon()] * atomkeys.size
|
|
224
|
+
|
|
225
|
+
for idx, (atm, coords) in enumerate(zip(atomkeys, atompos, strict=False)):
|
|
226
|
+
atom_vdw: Polygon = Point(coords).buffer(RADII[atm])
|
|
227
|
+
atom_list[idx] = atom_vdw
|
|
228
|
+
|
|
229
|
+
molecule = MultiPolygon(atom_list)
|
|
230
|
+
molecule = unary_union(molecule)
|
|
231
|
+
centre = -np.mean(atompos, axis=0)
|
|
232
|
+
if x_offset is not None:
|
|
233
|
+
centre[0] -= x_offset
|
|
234
|
+
if y_offset is not None:
|
|
235
|
+
centre[1] -= y_offset
|
|
236
|
+
|
|
237
|
+
return aff.translate(molecule, *centre)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def first_time_loader( # noqa: PLR0915
|
|
241
|
+
file_name: str | Path,
|
|
242
|
+
ignore_atoms: str | list[str] | None = None,
|
|
243
|
+
x_offset: float | None = None,
|
|
244
|
+
y_offset: float | None = None,
|
|
245
|
+
roll: float | None = None,
|
|
246
|
+
pitch: float | None = None,
|
|
247
|
+
yaw: float | None = None,
|
|
248
|
+
z_trim: float | None = None,
|
|
249
|
+
reference_lattice_spacing: float = 4.74,
|
|
250
|
+
) -> InDict:
|
|
251
|
+
"""Script to run when you first load the molecule. Shows the molecule in xy, zy, and xz perspective, and vdwaals.
|
|
252
|
+
|
|
253
|
+
Can be used to rotate the molecule until satisfied, and will print a string that can be used for the xyz reader.
|
|
254
|
+
|
|
255
|
+
:param file_name: File name. Include the path.
|
|
256
|
+
:param ignore_atoms: Atoms to be ignored, optional. Either a string or an iterable of strings.
|
|
257
|
+
:param x_offset: The x offset.
|
|
258
|
+
:param y_offset: The y offset.
|
|
259
|
+
:param roll: Rotation along the x-axis.
|
|
260
|
+
:param pitch: Rotation along the y-axis.
|
|
261
|
+
:param yaw: Rotation along the z axis.
|
|
262
|
+
:param z_trim: Value below which all molecules are ignored.
|
|
263
|
+
:param reference_lattice_spacing: The lattice spacing of the reference grid.
|
|
264
|
+
:return: The updated dict of parameters after rotation and translation.
|
|
265
|
+
"""
|
|
266
|
+
atomkeys, atompos = _initialise_reader(file_name, ignore_atoms, z_trim)
|
|
267
|
+
|
|
268
|
+
with (Path(__file__).parent / "molecule_colour.json").open("r") as file:
|
|
269
|
+
colourdict: dict[str, str] = dict(json.load(file))
|
|
270
|
+
|
|
271
|
+
map_to_colour = np.vectorize(lambda name: colourdict.get(name, "#FFFFFF"))
|
|
272
|
+
atomcolours: NDArray[np.str_] = map_to_colour(atomkeys)
|
|
273
|
+
|
|
274
|
+
roll = 0.0 if roll is None else roll
|
|
275
|
+
pitch = 0.0 if pitch is None else pitch
|
|
276
|
+
yaw = 0.0 if yaw is None else yaw
|
|
277
|
+
x_offset = 0.0 if x_offset is None else x_offset
|
|
278
|
+
y_offset = 0.0 if y_offset is None else y_offset
|
|
279
|
+
|
|
280
|
+
fig: Figure = plt.figure(figsize=(12, 10))
|
|
281
|
+
axs: list[Axes] = list(np.asarray(fig.add_gridspec(3, 2).subplots()).ravel())
|
|
282
|
+
|
|
283
|
+
for ax in axs[-2:]:
|
|
284
|
+
fig.delaxes(ax)
|
|
285
|
+
|
|
286
|
+
ax_angle_z = plt.axes(
|
|
287
|
+
(0.1, 0.25, 0.65, 0.03),
|
|
288
|
+
facecolor="lightgoldenrodyellow",
|
|
289
|
+
transform=axs[0].transAxes,
|
|
290
|
+
)
|
|
291
|
+
ax_angle_y = plt.axes(
|
|
292
|
+
(0.1, 0.2, 0.65, 0.03),
|
|
293
|
+
facecolor="lightgoldenrodyellow",
|
|
294
|
+
transform=axs[0].transAxes,
|
|
295
|
+
)
|
|
296
|
+
ax_angle_x = plt.axes(
|
|
297
|
+
(0.1, 0.15, 0.65, 0.03),
|
|
298
|
+
facecolor="lightgoldenrodyellow",
|
|
299
|
+
transform=axs[0].transAxes,
|
|
300
|
+
)
|
|
301
|
+
trans_x = plt.axes(
|
|
302
|
+
(0.1, 0.1, 0.65, 0.03),
|
|
303
|
+
facecolor="lightgoldenrodyellow",
|
|
304
|
+
transform=axs[0].transAxes,
|
|
305
|
+
)
|
|
306
|
+
trans_y = plt.axes(
|
|
307
|
+
(0.1, 0.05, 0.65, 0.03),
|
|
308
|
+
facecolor="lightgoldenrodyellow",
|
|
309
|
+
transform=axs[0].transAxes,
|
|
310
|
+
)
|
|
311
|
+
ang_z_txtbx = plt.axes((0.76, 0.25, 0.1, 0.03), transform=axs[0].transAxes)
|
|
312
|
+
ang_y_txtbx = plt.axes((0.76, 0.2, 0.1, 0.03), transform=axs[0].transAxes)
|
|
313
|
+
ang_x_txtbx = plt.axes((0.76, 0.15, 0.1, 0.03), transform=axs[0].transAxes)
|
|
314
|
+
tra_x_txtbx = plt.axes((0.76, 0.1, 0.1, 0.03), transform=axs[0].transAxes)
|
|
315
|
+
tra_y_txtbx = plt.axes((0.76, 0.05, 0.1, 0.03), transform=axs[0].transAxes)
|
|
316
|
+
|
|
317
|
+
ang_z = Slider(ax_angle_z, "roll", -180, 180, valinit=roll)
|
|
318
|
+
ang_y = Slider(ax_angle_y, "pitch", -180, 180, valinit=pitch)
|
|
319
|
+
ang_x = Slider(ax_angle_x, "yaw", -180, 180, valinit=yaw)
|
|
320
|
+
translate_x = Slider(trans_x, "x", -5, 5, valinit=x_offset)
|
|
321
|
+
translate_y = Slider(
|
|
322
|
+
trans_y,
|
|
323
|
+
"y",
|
|
324
|
+
-5,
|
|
325
|
+
5,
|
|
326
|
+
valinit=y_offset if y_offset is not None else 0.0,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
box_z = TextBox(ang_z_txtbx, "", initial=f"{roll}")
|
|
330
|
+
box_y = TextBox(ang_y_txtbx, "", initial=f"{pitch}")
|
|
331
|
+
box_x = TextBox(ang_x_txtbx, "", initial=f"{yaw}")
|
|
332
|
+
box_xoff = TextBox(tra_x_txtbx, "", initial=f"{x_offset}")
|
|
333
|
+
box_yoff = TextBox(tra_y_txtbx, "", initial=f"{y_offset}")
|
|
334
|
+
|
|
335
|
+
ang_z.valtext.set_visible(False)
|
|
336
|
+
ang_y.valtext.set_visible(False)
|
|
337
|
+
ang_x.valtext.set_visible(False)
|
|
338
|
+
translate_x.valtext.set_visible(False)
|
|
339
|
+
translate_y.valtext.set_visible(False)
|
|
340
|
+
|
|
341
|
+
def submit(text: str, slider: Slider) -> None:
|
|
342
|
+
"""Submit a value for one of the sliders.
|
|
343
|
+
|
|
344
|
+
:param text: Text, must be castable to float!
|
|
345
|
+
:param slider: The slider to be updated.
|
|
346
|
+
:return: The new value for the slider.
|
|
347
|
+
"""
|
|
348
|
+
slider.set_val(float(text))
|
|
349
|
+
|
|
350
|
+
def update(val: float) -> None:
|
|
351
|
+
"""Update the slider value.
|
|
352
|
+
|
|
353
|
+
:param val: The new value for the slider.
|
|
354
|
+
"""
|
|
355
|
+
_update(
|
|
356
|
+
val,
|
|
357
|
+
ang_x,
|
|
358
|
+
ang_y,
|
|
359
|
+
ang_z,
|
|
360
|
+
translate_x,
|
|
361
|
+
translate_y,
|
|
362
|
+
box_x,
|
|
363
|
+
box_y,
|
|
364
|
+
box_z,
|
|
365
|
+
box_xoff,
|
|
366
|
+
box_yoff,
|
|
367
|
+
atompos,
|
|
368
|
+
axs,
|
|
369
|
+
fig,
|
|
370
|
+
atomcolours,
|
|
371
|
+
atomkeys,
|
|
372
|
+
reference_lattice_spacing,
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
ident_mat: RotMatrix = cast("RotMatrix", np.identity(3, dtype=np.float64))
|
|
376
|
+
|
|
377
|
+
rollmat = ident_mat.copy()
|
|
378
|
+
pitchmat = ident_mat.copy()
|
|
379
|
+
yawmat = ident_mat.copy()
|
|
380
|
+
fac = np.pi / 180.0
|
|
381
|
+
rollmat[1, [1, 2]] = np.cos(roll * fac), -np.sin(roll * fac)
|
|
382
|
+
rollmat[2, [1, 2]] = np.sin(roll * fac), np.cos(roll * fac)
|
|
383
|
+
pitchmat[0, [0, 2]] = np.cos(pitch * fac), np.sin(pitch * fac)
|
|
384
|
+
pitchmat[2, [0, 2]] = -np.sin(pitch * fac), np.cos(pitch * fac)
|
|
385
|
+
yawmat[0, [0, 1]] = np.cos(yaw * fac), -np.sin(yaw * fac)
|
|
386
|
+
yawmat[1, [0, 1]] = np.sin(yaw * fac), np.cos(yaw * fac)
|
|
387
|
+
|
|
388
|
+
ident_mat[:] = ident_mat @ yawmat @ pitchmat @ rollmat
|
|
389
|
+
|
|
390
|
+
newatompos = atompos.copy()
|
|
391
|
+
|
|
392
|
+
for crdx, coords in enumerate(atompos):
|
|
393
|
+
newatompos[crdx] = coords @ ident_mat
|
|
394
|
+
|
|
395
|
+
newatompos -= np.mean(newatompos, axis=0)
|
|
396
|
+
if x_offset is not None:
|
|
397
|
+
newatompos[:, 0] -= x_offset
|
|
398
|
+
if y_offset is not None:
|
|
399
|
+
newatompos[:, 1] -= y_offset
|
|
400
|
+
|
|
401
|
+
for ax in axs:
|
|
402
|
+
ax.set_aspect("equal", "box")
|
|
403
|
+
ax.clear()
|
|
404
|
+
|
|
405
|
+
axs[0].set_xlabel("x-ax")
|
|
406
|
+
axs[0].set_ylabel("y-ax")
|
|
407
|
+
axs[1].set_xlabel("z-ax")
|
|
408
|
+
axs[1].set_ylabel("y-ax")
|
|
409
|
+
axs[2].set_xlabel("x-ax")
|
|
410
|
+
axs[2].set_ylabel("z-ax")
|
|
411
|
+
axs[3].set_xlabel("x-ax")
|
|
412
|
+
axs[3].set_ylabel("y-ax")
|
|
413
|
+
|
|
414
|
+
xs: FloatArray
|
|
415
|
+
ys: FloatArray
|
|
416
|
+
zs: FloatArray
|
|
417
|
+
xs, ys, zs = newatompos.T
|
|
418
|
+
|
|
419
|
+
axs[0].scatter(xs, ys, c=atomcolours)
|
|
420
|
+
axs[1].scatter(zs, ys, c=atomcolours)
|
|
421
|
+
axs[2].scatter(xs, zs, c=atomcolours)
|
|
422
|
+
|
|
423
|
+
circles = [Circle((xval, yval), RADII[atmk]) for xval, yval, atmk in zip(xs, ys, atomkeys, strict=True)]
|
|
424
|
+
axs[3].add_collection(PatchCollection(circles, fc=atomcolours, edgecolors="none", alpha=0.25))
|
|
425
|
+
axs[3].add_collection(PatchCollection(circles, ec=atomcolours, fc="none"))
|
|
426
|
+
axs[3].scatter(
|
|
427
|
+
reference_lattice_spacing * np.array([0, 1, -1, 0.5, -0.5, 0.5, -0.5]),
|
|
428
|
+
reference_lattice_spacing
|
|
429
|
+
* np.array(
|
|
430
|
+
[0, 0, 0, np.sqrt(3) / 2, np.sqrt(3) / 2, -np.sqrt(3) / 2, -np.sqrt(3) / 2],
|
|
431
|
+
),
|
|
432
|
+
c="k",
|
|
433
|
+
s=200,
|
|
434
|
+
)
|
|
435
|
+
axs[3].scatter(xs, ys, c=atomcolours, edgecolors="w")
|
|
436
|
+
|
|
437
|
+
ang_z.on_changed(update)
|
|
438
|
+
ang_y.on_changed(update)
|
|
439
|
+
ang_x.on_changed(update)
|
|
440
|
+
translate_x.on_changed(update)
|
|
441
|
+
translate_y.on_changed(update)
|
|
442
|
+
box_z.on_submit(lambda text: submit(text, ang_z))
|
|
443
|
+
box_y.on_submit(lambda text: submit(text, ang_y))
|
|
444
|
+
box_x.on_submit(lambda text: submit(text, ang_x))
|
|
445
|
+
box_xoff.on_submit(lambda text: submit(text, translate_x))
|
|
446
|
+
box_yoff.on_submit(lambda text: submit(text, translate_y))
|
|
447
|
+
plt.show()
|
|
448
|
+
|
|
449
|
+
outputparams: InDict = {
|
|
450
|
+
"x_offset": translate_x.val,
|
|
451
|
+
"y_offset": translate_y.val,
|
|
452
|
+
"roll": ang_z.val,
|
|
453
|
+
"pitch": ang_y.val,
|
|
454
|
+
"yaw": ang_x.val,
|
|
455
|
+
"file_name": str(file_name),
|
|
456
|
+
"ignore_atoms": ignore_atoms,
|
|
457
|
+
"z_trim": z_trim,
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
print(f"kwargs = {outputparams}")
|
|
461
|
+
|
|
462
|
+
return outputparams
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def _update( # noqa: PLR0913
|
|
466
|
+
val: float,
|
|
467
|
+
ang_x: Slider,
|
|
468
|
+
ang_y: Slider,
|
|
469
|
+
ang_z: Slider,
|
|
470
|
+
translate_x: Slider,
|
|
471
|
+
translate_y: Slider,
|
|
472
|
+
box_x: TextBox,
|
|
473
|
+
box_y: TextBox,
|
|
474
|
+
box_z: TextBox,
|
|
475
|
+
box_xoff: TextBox,
|
|
476
|
+
box_yoff: TextBox,
|
|
477
|
+
atompos: FloatArray,
|
|
478
|
+
axs: list[axes.Axes],
|
|
479
|
+
fig: figure.Figure,
|
|
480
|
+
atomcolours: NDArray[np.str_],
|
|
481
|
+
atomkeys: NDArray[np.str_],
|
|
482
|
+
reference_lattice_spacing: float,
|
|
483
|
+
) -> None:
|
|
484
|
+
roll = ang_z.val
|
|
485
|
+
pitch = ang_y.val
|
|
486
|
+
yaw = ang_x.val
|
|
487
|
+
x_offset = translate_x.val
|
|
488
|
+
y_offset = translate_y.val
|
|
489
|
+
|
|
490
|
+
box_z.set_val(f"{roll:.3f}")
|
|
491
|
+
box_y.set_val(f"{pitch:.3f}")
|
|
492
|
+
box_x.set_val(f"{yaw:.3f}")
|
|
493
|
+
box_xoff.set_val(f"{x_offset:.3f}")
|
|
494
|
+
box_yoff.set_val(f"{y_offset:.3f}")
|
|
495
|
+
|
|
496
|
+
ident_mat: RotMatrix = cast("RotMatrix", np.identity(3))
|
|
497
|
+
|
|
498
|
+
rollmat = ident_mat.copy()
|
|
499
|
+
pitchmat = ident_mat.copy()
|
|
500
|
+
yawmat = ident_mat.copy()
|
|
501
|
+
fac = np.pi / 180.0
|
|
502
|
+
rollmat[1, [1, 2]] = np.cos(roll * fac), -np.sin(roll * fac)
|
|
503
|
+
rollmat[2, [1, 2]] = np.sin(roll * fac), np.cos(roll * fac)
|
|
504
|
+
pitchmat[0, [0, 2]] = np.cos(pitch * fac), np.sin(pitch * fac)
|
|
505
|
+
pitchmat[2, [0, 2]] = -np.sin(pitch * fac), np.cos(pitch * fac)
|
|
506
|
+
yawmat[0, [0, 1]] = np.cos(yaw * fac), -np.sin(yaw * fac)
|
|
507
|
+
yawmat[1, [0, 1]] = np.sin(yaw * fac), np.cos(yaw * fac)
|
|
508
|
+
|
|
509
|
+
ident_mat = ident_mat @ yawmat @ pitchmat @ rollmat
|
|
510
|
+
|
|
511
|
+
newatompos = atompos.copy()
|
|
512
|
+
|
|
513
|
+
for crdx, coords in enumerate(atompos):
|
|
514
|
+
newatompos[crdx] = coords @ ident_mat
|
|
515
|
+
|
|
516
|
+
newatompos -= np.mean(newatompos, axis=0)
|
|
517
|
+
if x_offset is not None:
|
|
518
|
+
newatompos[:, 0] -= x_offset
|
|
519
|
+
if y_offset is not None:
|
|
520
|
+
newatompos[:, 1] -= y_offset
|
|
521
|
+
|
|
522
|
+
for ax in axs:
|
|
523
|
+
ax.set_aspect("equal", "box")
|
|
524
|
+
ax.clear()
|
|
525
|
+
|
|
526
|
+
axs[0].set_xlabel("x-ax")
|
|
527
|
+
axs[0].set_ylabel("y-ax")
|
|
528
|
+
axs[1].set_xlabel("z-ax")
|
|
529
|
+
axs[1].set_ylabel("y-ax")
|
|
530
|
+
axs[2].set_xlabel("x-ax")
|
|
531
|
+
axs[2].set_ylabel("z-ax")
|
|
532
|
+
axs[3].set_xlabel("x-ax")
|
|
533
|
+
axs[3].set_ylabel("y-ax")
|
|
534
|
+
|
|
535
|
+
xs: FloatArray
|
|
536
|
+
ys: FloatArray
|
|
537
|
+
zs: FloatArray
|
|
538
|
+
xs, ys, zs = newatompos.T
|
|
539
|
+
|
|
540
|
+
xminmax = np.min(xs), np.max(xs)
|
|
541
|
+
yminmax = np.min(ys), np.max(ys)
|
|
542
|
+
zminmax = np.min(zs), np.max(zs)
|
|
543
|
+
|
|
544
|
+
xmarker = 30 + (xs - xminmax[0]) / (xminmax[1] - xminmax[0]) * 30
|
|
545
|
+
ymarker = 30 + (ys - yminmax[0]) / (yminmax[1] - yminmax[0]) * 30
|
|
546
|
+
zmarker = 30 + (zs - zminmax[0]) / (zminmax[1] - zminmax[0]) * 30
|
|
547
|
+
|
|
548
|
+
xsort = np.argsort(xs)
|
|
549
|
+
ysort = np.argsort(ys)
|
|
550
|
+
zsort = np.argsort(zs)
|
|
551
|
+
|
|
552
|
+
axs[0].scatter(xs[zsort], ys[zsort], c=atomcolours[zsort], s=zmarker[zsort])
|
|
553
|
+
axs[1].scatter(zs[xsort], ys[xsort], c=atomcolours[xsort], s=xmarker[xsort])
|
|
554
|
+
axs[2].scatter(xs[ysort], zs[ysort], c=atomcolours[ysort], s=ymarker[ysort])
|
|
555
|
+
|
|
556
|
+
circles = np.array(
|
|
557
|
+
[Circle((xval, yval), RADII[atmk]) for xval, yval, atmk in zip(xs, ys, atomkeys, strict=True)],
|
|
558
|
+
)
|
|
559
|
+
axs[3].add_collection(PatchCollection(circles[zsort], fc=atomcolours[zsort], edgecolors="none", alpha=0.25))
|
|
560
|
+
axs[3].add_collection(PatchCollection(circles[zsort], ec=atomcolours[zsort], fc="none"))
|
|
561
|
+
|
|
562
|
+
axs[3].scatter(
|
|
563
|
+
reference_lattice_spacing * np.array([0, 1, -1, 0.5, -0.5, 0.5, -0.5]),
|
|
564
|
+
reference_lattice_spacing
|
|
565
|
+
* np.array(
|
|
566
|
+
[
|
|
567
|
+
0,
|
|
568
|
+
0,
|
|
569
|
+
0,
|
|
570
|
+
np.sqrt(3) / 2,
|
|
571
|
+
np.sqrt(3) / 2,
|
|
572
|
+
-np.sqrt(3) / 2,
|
|
573
|
+
-np.sqrt(3) / 2,
|
|
574
|
+
],
|
|
575
|
+
),
|
|
576
|
+
c="k",
|
|
577
|
+
s=200,
|
|
578
|
+
)
|
|
579
|
+
axs[3].scatter(xs[zsort], ys[zsort], c=atomcolours[zsort], edgecolors="w")
|
|
580
|
+
|
|
581
|
+
xlim: tuple[float, float] = axs[3].get_xlim()
|
|
582
|
+
ylim: tuple[float, float] = axs[3].get_ylim()
|
|
583
|
+
xmean: float = np.mean(xlim, dtype=float)
|
|
584
|
+
ymean: float = np.mean(ylim, dtype=float)
|
|
585
|
+
xlim = xlim[0] * 1.1 - 0.1 * xmean, xlim[1] * 1.1 - 0.1 * xmean
|
|
586
|
+
ylim = ylim[0] * 1.1 - 0.1 * ymean, ylim[1] * 1.1 - 0.1 * ymean
|
|
587
|
+
|
|
588
|
+
axs[3].set_xlim(xlim)
|
|
589
|
+
axs[3].set_ylim(ylim)
|
|
590
|
+
|
|
591
|
+
axs[0].set_xlim(xlim)
|
|
592
|
+
axs[0].set_ylim(ylim)
|
|
593
|
+
axs[1].set_ylim(ylim)
|
|
594
|
+
xmin, xmax = axs[1].get_xlim()
|
|
595
|
+
if xmax - xmin < 2: # noqa: PLR2004
|
|
596
|
+
axs[1].set_xlim((xmin - 1, xmax + 1))
|
|
597
|
+
axs[2].set_xlim(xlim)
|
|
598
|
+
ymin, ymax = axs[2].get_ylim()
|
|
599
|
+
if ymax - ymin < 2: # noqa: PLR2004
|
|
600
|
+
axs[2].set_ylim((ymin - 1, ymax + 1))
|
|
601
|
+
|
|
602
|
+
fig.canvas.blit(fig.bbox)
|
|
603
|
+
fig.canvas.flush_events()
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def _initialise_reader(
|
|
607
|
+
file_name: str | Path,
|
|
608
|
+
ignore_atoms: str | list[str] | None = None,
|
|
609
|
+
z_trim: float | None = None,
|
|
610
|
+
) -> tuple[np.ndarray[tuple[int], np.dtype[np.str_]], np.ndarray[tuple[int, Literal[3]], np.dtype[np.float64]]]:
|
|
611
|
+
"""Initialise the xyz_reader and first_time_loader.
|
|
612
|
+
|
|
613
|
+
:param file_name: name of the file to read.
|
|
614
|
+
:param ignore_atoms: list of atoms to ignore.
|
|
615
|
+
:param z_trim: z value under which to remove the atoms.
|
|
616
|
+
:return: a tuple of the atom keys and atom positions in 3D. Filtered.
|
|
617
|
+
:raises ValueError: 1) if the file type is not .xyz or 2) if the used settings would return an empty molecule.
|
|
618
|
+
"""
|
|
619
|
+
file_path = Path(file_name)
|
|
620
|
+
if (badtype := file_path.suffix) != ".xyz":
|
|
621
|
+
errmsg = f"The file type is not .xyz but {badtype}"
|
|
622
|
+
raise ValueError(errmsg)
|
|
623
|
+
data = np.loadtxt(file_path, dtype=str, skiprows=2)
|
|
624
|
+
atomkeys: np.ndarray[tuple[int], np.dtype[np.str_]] = data[:, 0]
|
|
625
|
+
atompos: np.ndarray[tuple[int, Literal[3]], np.dtype[np.float64]] = cast(
|
|
626
|
+
"np.ndarray[tuple[int, Literal[3]], np.dtype[np.float64]]",
|
|
627
|
+
data[:, 1:].astype(np.float64),
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
mask: BoolArray | None = None
|
|
631
|
+
if isinstance(ignore_atoms, str):
|
|
632
|
+
mask = atomkeys != ignore_atoms
|
|
633
|
+
elif ignore_atoms is None:
|
|
634
|
+
pass
|
|
635
|
+
elif isinstance(ignore_atoms[0], str):
|
|
636
|
+
mask = np.ones(atomkeys.size, dtype=bool)
|
|
637
|
+
for ign_atm in ignore_atoms:
|
|
638
|
+
mask &= atomkeys != ign_atm
|
|
639
|
+
|
|
640
|
+
if z_trim is not None:
|
|
641
|
+
mask = np.ones(atomkeys.size, dtype=np.bool_) if mask is None else mask
|
|
642
|
+
mask &= atompos[:, 2] > z_trim
|
|
643
|
+
|
|
644
|
+
if mask is not None:
|
|
645
|
+
atomkeys = atomkeys[mask]
|
|
646
|
+
atompos = atompos[mask]
|
|
647
|
+
|
|
648
|
+
if not atomkeys.size:
|
|
649
|
+
errmsg = "The current settings result in an empty molecule."
|
|
650
|
+
raise ValueError(errmsg)
|
|
651
|
+
|
|
652
|
+
return atomkeys, atompos
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
if __name__ == "__main__": # Best practice
|
|
656
|
+
while (file := input("File path name, or q to quit: ")).lower() not in {"q", "quit"}:
|
|
657
|
+
first_time_loader(file)
|