-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualization.py
381 lines (255 loc) · 10.5 KB
/
visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
import numpy as np
np.bool = bool
from scipy.stats import chi2
import pyvista
pyvista.set_plot_theme('document')
class CrystalStructure:
"""
Draw a crystal structure. Atom can drawn with various radii with occupancy
displayed using opacity. Atoms can allso be draw as atomic displacement
ellipsoids or be decorated with magnetic moments vectors.
Parameters
----------
A : 2d-array
Real-space Cartesian transform.
ux, uy, uz : 1d-array
Cartesian fractional coordinates.
atms : 1d-array, str
Atom labels.
colors : 2d-array
RGB color for each atom.
nu, nv, nw : int
Number of cells along each dimension. Default is ``1`` along each
dimension.
Attributres
-----------
pl : plotter
Plot object.
Methods
-------
show_figure()
save_figure()
view_direction)
draw_basis_vectors()
draw_cell_edges()
atomic_radii()
atomic_displacement_ellipsoids()
magnetic_vectors()
"""
def __init__(self, A, ux, uy, uz, atms, colors, nu=1, nv=1, nw=1):
self.pl = pyvista.Plotter()
self.pl.enable_parallel_projection()
self.pl.enable_depth_peeling()
self.pl.enable_mesh_picking(callback=self.__callback, style='surface',
left_clicking=True, show=True,
show_message=False, smooth_shading=True)
self._A = A
self._nu, self._nv, self._nw = nu, nv, nw
self._ux, self._uy, self._uz = self.__wrap_coordinates(ux, uy, uz)
self._atms = atms
self._colors = colors
self._atm_scale = 0.5
def __callback(self, mesh):
display, atom = mesh.__name.split('-')
i = int(atom)
n_atm = len(self._colors)
k = i % n_atm
atm, ux, uy, uz = self._atms[k], self._ux[i], self._uy[i], self._uz[i]
u, v, w = np.dot(np.linalg.inv(self._A), [ux,uy,uz])
header = 'atm u v w\n'
divide = '========================\n'
coords = '{:6}{:6.3}{:6.3}{:6.3}\n'
site = coords.format(atm,u,v,w)
print(header+divide+site)
def __wrap_coordinates(self, ux, uy, uz):
nu, nv, nw = self._nu, self._nv, self._nw
u, v, w = np.dot(np.linalg.inv(self._A), [ux,uy,uz])
wrap = np.isclose([u,v,w], 0)
mask = wrap.sum(axis=0)
face = mask == 1
edge = mask == 2
corner = mask == 3
indices = np.arange(mask.size)
face_indices = indices[face]
u_face, v_face, w_face = u[face].copy(), v[face].copy(), w[face].copy()
mask_u_face = np.isclose(u_face, 0)
mask_v_face = np.isclose(v_face, 0)
mask_w_face = np.isclose(w_face, 0)
u_face[mask_u_face] += nu
v_face[mask_v_face] += nv
w_face[mask_w_face] += nw
edge_indices = np.repeat(indices[edge], 3)
u_edge, v_edge, w_edge = u[edge], v[edge], w[edge]
u_edges = np.repeat(u_edge, 3).reshape(-1,3)
v_edges = np.repeat(v_edge, 3).reshape(-1,3)
w_edges = np.repeat(w_edge, 3).reshape(-1,3)
mask_uv_edge = np.isclose([u_edge,v_edge], 0).all(axis=0)
mask_vw_edge = np.isclose([v_edge,w_edge], 0).all(axis=0)
mask_wu_edge = np.isclose([w_edge,u_edge], 0).all(axis=0)
u_edges[mask_uv_edge] += np.array([nu,0,nu])
v_edges[mask_uv_edge] += np.array([0,nv,nv])
v_edges[mask_vw_edge] += np.array([nv,0,nv])
w_edges[mask_vw_edge] += np.array([0,nw,nw])
w_edges[mask_wu_edge] += np.array([nw,0,nw])
u_edges[mask_wu_edge] += np.array([0,nu,nu])
corner_indices = np.repeat(indices[corner], 7)
u_corner, v_corner, w_corner = u[corner], v[corner], w[corner]
u_corners = np.repeat(u_corner, 7).reshape(-1,7)
v_corners = np.repeat(v_corner, 7).reshape(-1,7)
w_corners = np.repeat(w_corner, 7).reshape(-1,7)
mask_uvw_corner = np.isclose([u_corner,
v_corner,
w_corner], 0).all(axis=0)
u_corners[mask_uvw_corner] += np.array([nu,0,0,nu,nu,0,nu])
v_corners[mask_uvw_corner] += np.array([0,nv,0,0,nv,nv,nv])
w_corners[mask_uvw_corner] += np.array([0,0,nw,nw,0,nw,nw])
u = np.concatenate((u,u_face,u_edges.flatten(),u_corners.flatten()))
v = np.concatenate((v,v_face,v_edges.flatten(),v_corners.flatten()))
w = np.concatenate((w,w_face,w_edges.flatten(),w_corners.flatten()))
self._indices = np.concatenate((indices,face_indices,
edge_indices,corner_indices))
return np.dot(self._A, [u,v,w])
def __probability_ellipsoid(self, Uxx, Uyy, Uzz, Uyz, Uxz, Uxy, p=0.99):
U = np.array([[Uxx,Uxy,Uxz],
[Uxy,Uyy,Uyz],
[Uxz,Uyz,Uzz]])
w, v = np.linalg.eig(U)
r_eff = chi2.ppf(p, 3) if p < 0.999999 else 77.39631549062088
radii = np.sqrt(r_eff*w.real)
T = np.dot(v.real,np.dot(np.diag(radii),v.real.T))
return T
def show_figure(self):
"""
Display the drawing.
"""
self.pl.show()
def save_figure(self, filename):
"""
Save the figure as a graphic image.
Parameters
----------
filename : str
Name of file with extension. Supported extensions are ``.svg`` and
``.pdf``.
"""
_, ext = os.path.splitext(filename)
if ext == '': filename + '.pdf'
self.pl.save_graphic(filename)
def view_direction(self, u, v, w):
"""
View drawing along a crystallographic direction.
Parameters
----------
u, v, w : float
Components of the viewing axis. Units are in fractional
coordinates.
"""
nu, nv, nw = self._nu, self._nv, self._nw
x, y, z = 2*np.dot(self._A, [u,v,w])
xc, yc, zc = np.dot(self._A, [0.5*nu,0.5*nv,0.5*nw])
self.pl.camera.focal_point = (xc, yc, zc)
self.pl.camera.position = (xc+x, yc+y, zc+z)
def draw_basis_vectors(self):
"""
Draw the baasis vector directions for the crytallographic directions.
"""
t = self._A.copy()
t /= np.max(t, axis=1)
a = pyvista._vtk.vtkMatrix4x4()
for i in range(3):
for j in range(3):
a.SetElement(i,j,t[i,j])
actor = self.pl.add_axes(xlabel='a', ylabel='b', zlabel='c')
actor.SetUserMatrix(a)
# actor = self.pl.add_camera_orientation_widget()
def draw_cell_edges(self):
"""
Draw unit cell edges.
"""
T = np.eye(4)
T[:3,:3] = self._A
mesh = pyvista.Box(bounds=(0,1,0,1,0,1), level=0, quads=True)
mesh.transform(T, inplace=True)
self.pl.add_mesh(mesh, style='wireframe',
render_lines_as_tubes=True, show_edges=True)
def atomic_radii(self, radii, occ):
"""
Draw atoms with given radii and displayed with opacity corresponding to
site occupancy
Parameters
----------
radii : 1d-array
The radii of the atom sites.
occ : 1d-array
The site occupancy of each atom. The value ranges from 0-1.
"""
n_atm = len(self._colors)
mesh = pyvista.PolyData(np.column_stack((0,0,0)))
geom = pyvista.Sphere(radius=1, theta_resolution=10, phi_resolution=10)
for j, i in enumerate(self._indices):
k = i % n_atm
glyph = mesh.glyph(scale=1, geom=geom)
T = np.zeros((4,4))
T[0,0] = T[1,1] = T[2,2] = radii[k]*self._atm_scale
T[:3,-1] = np.array([self._ux[j], self._uy[j], self._uz[j]])
T[-1,-1] = 1
glyph.transform(T, inplace=True)
scalars = np.tile(np.column_stack([*self._colors[k],occ[i]]),
geom.n_cells).reshape(-1,4)
glyph.__name = 's-{}'.format(i)
self.pl.add_mesh(glyph, name='s-{}'.format(j), scalars=scalars,
style='surface', rgb=True, smooth_shading=True)
self.radii = radii
def atomic_displacement_ellipsoids(self, Uxx, Uyy, Uzz, Uyz, Uxz, Uxy,
p=0.99):
"""
Draw atomic displacement ellipsoids
Parameters
----------
Uxx, Uyy, Uzz, Uyz, Uxz, Uxy : 1d-array
Atomic displacement parameters in Cartesian coordinates.
p : float
Probability surface. Default is ``p=0.99``
"""
n_atm = len(self._colors)
mesh = pyvista.PolyData(np.column_stack((0,0,0)))
geom = pyvista.Sphere(radius=1, theta_resolution=10, phi_resolution=10)
for j, i in enumerate(self._indices):
k = i % n_atm
P = self.__probability_ellipsoid(Uxx[i], Uyy[i], Uzz[i],
Uyz[i], Uxz[i], Uxy[i], p)
glyph = mesh.glyph(scale=1, geom=geom)
T = np.zeros((4,4))
T[:3,:3] = P
T[:3,-1] = np.array([self._ux[j], self._uy[j], self._uz[j]])
T[-1,-1] = 1
glyph.transform(T, inplace=True)
scalars = np.tile(np.column_stack(self._colors[k]),
geom.n_cells).reshape(-1,3)
glyph.__name = 'e-{}'.format(i)
self.pl.add_mesh(glyph, name='e-{}'.format(j), scalars=scalars,
rgb=True, smooth_shading=True)
def magnetic_vectors(self, sx, sy, sz):
"""
Draw magnetic vectors.
Parameters
----------
sx, sy, sz : 1d-array
The magnetic moment vector components in Cartesian coordinates.
"""
n_atm = len(self._colors)
k = np.arange(len(sx)) % n_atm
s = np.sqrt(sx**2+sy**2+sz**2)
mask = s > 0
mag_scale = 2.5*np.mean(self.radii[k[mask]]/s[mask])*self._atm_scale
for j, i in enumerate(self._indices):
r = np.array([self._ux[j],self._uy[j],self._uz[j]])
v = np.array([sx[i],sy[i],sz[i]])
s = np.linalg.norm(v)
if s > 0: v /= s
glyph = pyvista.Arrow(start=r-s*mag_scale*v,
direction=v, scale=2*s*mag_scale,
shaft_resolution=10, tip_resolution=10)
self.pl.add_mesh(glyph, color='red',
smooth_shading=True, pickable=False)