kim-tools 0.3.6__py3-none-any.whl → 0.4.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.
@@ -0,0 +1,390 @@
1
+ import logging
2
+ import math
3
+ import os
4
+ import shutil
5
+ from typing import Dict, List, Tuple, Union
6
+
7
+ import numpy as np
8
+ import numpy.typing as npt
9
+
10
+ from .core import DATA_DIR, _check_space_group
11
+
12
+ logger = logging.getLogger(__name__)
13
+ logging.basicConfig(filename="kim-tools.log", level=logging.INFO, force=True)
14
+
15
+
16
+ def voigt_elast_class(sgnum: Union[int, str]) -> str:
17
+ """
18
+ Get the name of the class of the structure of the elasticity tensor, out of
19
+ the following possibilities:
20
+ "cubic", "hexagonal", "trigonal_3bar_m_2nd_pos", "trigonal_3bar_m_3rd_pos",
21
+ "trigonal_3bar", "tetragonal_class_4_slash_mmm", "tetragonal_class_4_slash_m",
22
+ "orthorhombic", "monoclinic", "triclinic". The names that don't
23
+ correspond to a crystal system are taken from
24
+ https://dictionary.iucr.org/Laue_class
25
+
26
+ Note that this is a novel classification as far as the authors are aware.
27
+ Most crystallography texts on the subject will show 9 classes. This function
28
+ returns 10 possibilities, because the structure of the components of
29
+ the elasticity tensor for Laue class -3m depends on whether the twofold
30
+ axis is aligned along the Cartesian x or y axis. Assuming one adopts a
31
+ standard orientation of the hexagonal unit cell w.r.t. the Cartesian coordinate
32
+ system, as we do, different space groups in this Laue class have different
33
+ forms.
34
+
35
+ See https://doi.org/10.1017/CBO9781139017657.008 pg 232 for a review
36
+ of the subject.
37
+ """
38
+ _check_space_group(sgnum)
39
+ sgnum = int(sgnum)
40
+
41
+ if sgnum < 3:
42
+ return "triclinic"
43
+ elif sgnum < 16:
44
+ return "monoclinic"
45
+ elif sgnum < 75:
46
+ return "orthorhombic"
47
+ elif sgnum < 89:
48
+ return "tetragonal_4_slash_m"
49
+ elif sgnum < 143:
50
+ return "tetragonal_4_slash_mmm"
51
+ elif sgnum < 149:
52
+ return "trigonal_3bar"
53
+ elif sgnum < 168:
54
+ # Determine if this is one of the groups with the 2-fold operation in the
55
+ # third position (e.g. 149:P312), which has different equations
56
+ if sgnum in (149, 151, 153, 157, 159, 162, 163):
57
+ return "trigonal_3bar_m_3rd_pos"
58
+ else:
59
+ return "trigonal_3bar_m_2nd_pos"
60
+ elif sgnum < 195:
61
+ return "hexagonal"
62
+ else:
63
+ return "cubic"
64
+
65
+
66
+ def voigt_elast_compon_eqn(sgnum: Union[int, str]) -> Dict:
67
+ """
68
+ Get the algebraic equations describing the symmetry restrictions
69
+ on the elasticity matrix in Voigt form given a space group number.
70
+ The unit cell
71
+ must be in the orientation defined in doi.org/10.1016/j.commatsci.2017.01.017
72
+ for these equations to be correct.
73
+
74
+ Returns:
75
+ Encoding of symmetry restrictions on elasticity matrices.
76
+ The keys are the Voigt indices of non-independent components.
77
+ The values are a pair of lists representing the linear combination
78
+ of the unique compoonents that is used to determine the non-unique component
79
+ specified in the key. The first list is the coefficients, the second
80
+ is the indices. If a non-independent component is zero, this is indicated
81
+ by a value of None. Any components not listed as a key are assumed to
82
+ be independent. Only the upper triangle (i<j) is listed. Indices are
83
+ one-based.
84
+ """
85
+ ELASTICITY_MATRIX_EQNS = {
86
+ "cubic": {
87
+ (1, 3): ([1], [(1, 2)]),
88
+ (1, 4): None,
89
+ (1, 5): None,
90
+ (1, 6): None,
91
+ (2, 2): ([1], [(1, 1)]),
92
+ (2, 3): ([1], [(1, 2)]),
93
+ (2, 4): None,
94
+ (2, 5): None,
95
+ (2, 6): None,
96
+ (3, 3): ([1], [(1, 1)]),
97
+ (3, 4): None,
98
+ (3, 5): None,
99
+ (3, 6): None,
100
+ (4, 5): None,
101
+ (4, 6): None,
102
+ (5, 5): ([1], [(4, 4)]),
103
+ (5, 6): None,
104
+ (6, 6): ([1], [(4, 4)]),
105
+ },
106
+ "hexagonal": {
107
+ (1, 4): None,
108
+ (1, 5): None,
109
+ (1, 6): None,
110
+ (2, 2): ([1], [(1, 1)]),
111
+ (2, 3): ([1], [(1, 3)]),
112
+ (2, 4): None,
113
+ (2, 5): None,
114
+ (2, 6): None,
115
+ (3, 4): None,
116
+ (3, 5): None,
117
+ (3, 6): None,
118
+ (4, 5): None,
119
+ (4, 6): None,
120
+ (5, 5): ([1], [(4, 4)]),
121
+ (5, 6): None,
122
+ (6, 6): ([0.5, -0.5], [(1, 1), (1, 2)]),
123
+ },
124
+ "trigonal_3bar_m_2nd_pos": {
125
+ (1, 5): None,
126
+ (1, 6): None,
127
+ (2, 2): ([1], [(1, 1)]),
128
+ (2, 3): ([1], [(1, 3)]),
129
+ (2, 4): ([-1], [(1, 4)]),
130
+ (2, 5): None,
131
+ (2, 6): None,
132
+ (3, 4): None,
133
+ (3, 5): None,
134
+ (3, 6): None,
135
+ (4, 5): None,
136
+ (4, 6): None,
137
+ (5, 5): ([1], [(4, 4)]),
138
+ (5, 6): ([1], [(1, 4)]),
139
+ (6, 6): ([0.5, -0.5], [(1, 1), (1, 2)]),
140
+ },
141
+ "trigonal_3bar_m_3rd_pos": {
142
+ (1, 4): None,
143
+ (1, 6): None,
144
+ (2, 2): ([1], [(1, 1)]),
145
+ (2, 3): ([1], [(1, 3)]),
146
+ (2, 4): None,
147
+ (2, 5): ([-1], [(1, 5)]),
148
+ (2, 6): None,
149
+ (3, 4): None,
150
+ (3, 5): None,
151
+ (3, 6): None,
152
+ (4, 5): None,
153
+ (4, 6): ([-1], [(1, 5)]),
154
+ (5, 5): ([1], [(4, 4)]),
155
+ (5, 6): None,
156
+ (6, 6): ([0.5, -0.5], [(1, 1), (1, 2)]),
157
+ },
158
+ "trigonal_3bar": {
159
+ (1, 6): None,
160
+ (2, 2): ([1], [(1, 1)]),
161
+ (2, 3): ([1], [(1, 3)]),
162
+ (2, 4): ([-1], [(1, 4)]),
163
+ (2, 5): ([-1], [(1, 5)]),
164
+ (2, 6): None,
165
+ (3, 4): None,
166
+ (3, 5): None,
167
+ (3, 6): None,
168
+ (4, 5): None,
169
+ (4, 6): ([-1], [(1, 5)]),
170
+ (5, 5): ([1], [(4, 4)]),
171
+ (5, 6): ([1], [(1, 4)]),
172
+ (6, 6): ([0.5, -0.5], [(1, 1), (1, 2)]),
173
+ },
174
+ "tetragonal_4_slash_mmm": {
175
+ (1, 4): None,
176
+ (1, 5): None,
177
+ (1, 6): None,
178
+ (2, 2): ([1], [(1, 1)]),
179
+ (2, 3): ([1], [(1, 3)]),
180
+ (2, 4): None,
181
+ (2, 5): None,
182
+ (2, 6): None,
183
+ (3, 4): None,
184
+ (3, 5): None,
185
+ (3, 6): None,
186
+ (4, 5): None,
187
+ (4, 6): None,
188
+ (5, 5): ([1], [(4, 4)]),
189
+ (5, 6): None,
190
+ },
191
+ "tetragonal_4_slash_m": {
192
+ (1, 4): None,
193
+ (1, 5): None,
194
+ (2, 2): ([1], [(1, 1)]),
195
+ (2, 3): ([1], [(1, 3)]),
196
+ (2, 4): None,
197
+ (2, 5): None,
198
+ (2, 6): ([-1], [(1, 6)]),
199
+ (3, 4): None,
200
+ (3, 5): None,
201
+ (3, 6): None,
202
+ (4, 5): None,
203
+ (4, 6): None,
204
+ (5, 5): ([1], [(4, 4)]),
205
+ (5, 6): None,
206
+ },
207
+ "orthorhombic": {
208
+ (1, 4): None,
209
+ (1, 5): None,
210
+ (1, 6): None,
211
+ (2, 4): None,
212
+ (2, 5): None,
213
+ (2, 6): None,
214
+ (3, 4): None,
215
+ (3, 5): None,
216
+ (3, 6): None,
217
+ (4, 5): None,
218
+ (4, 6): None,
219
+ (5, 6): None,
220
+ },
221
+ "monoclinic": {
222
+ (1, 4): None,
223
+ (1, 6): None,
224
+ (2, 4): None,
225
+ (2, 6): None,
226
+ (3, 4): None,
227
+ (3, 6): None,
228
+ (4, 5): None,
229
+ (5, 6): None,
230
+ },
231
+ "triclinic": {},
232
+ }
233
+
234
+ # error check typing in the above dicts
235
+ for eqn in ELASTICITY_MATRIX_EQNS.values():
236
+ # only unique keys
237
+ assert sorted(list(set(eqn.keys()))) == sorted(list(eqn.keys()))
238
+ # check that all components appearing in RHS of relations are independent, i.e.
239
+ # they don't appear as a key
240
+ for dependent_component in eqn:
241
+ if eqn[dependent_component] is not None:
242
+ for independent_component in eqn[dependent_component][1]:
243
+ assert not (independent_component in eqn)
244
+
245
+ return ELASTICITY_MATRIX_EQNS[voigt_elast_class(sgnum)]
246
+
247
+
248
+ def voigt_elast_struct_svg(sgnum: Union[int, str], dest_filename: str) -> None:
249
+ """
250
+ Write a copy of the image showing the structure of the Voigt elasticity matrix for
251
+ the specified space group
252
+ """
253
+ src_filename = os.path.join(DATA_DIR, "elast_" + voigt_elast_class(sgnum) + ".svg")
254
+ shutil.copyfile(src_filename, dest_filename)
255
+
256
+
257
+ def indep_elast_compon_names_and_values_from_voigt(
258
+ voigt: npt.ArrayLike, sgnum: Union[int, str]
259
+ ) -> Tuple[List[str], List[float]]:
260
+ """
261
+ From an elasticity matrix in Voigt order and a space group number,
262
+ extract the elastic constants that should be unique (cij where first i is as low as
263
+ possible, then j)
264
+ """
265
+ eqn = voigt_elast_compon_eqn(sgnum)
266
+
267
+ elastic_constants_names = []
268
+ elastic_constants_values = []
269
+
270
+ # first, figure out which constants are unique and extract them
271
+ for i in range(1, 7):
272
+ for j in range(i, 7):
273
+ if (i, j) not in eqn:
274
+ elastic_constants_names.append("c" + str(i) + str(j))
275
+ elastic_constants_values.append(voigt[i - 1, j - 1])
276
+
277
+ return elastic_constants_names, elastic_constants_values
278
+
279
+
280
+ def calc_bulk(elastic_constants):
281
+ """
282
+ Compute the bulk modulus given the elastic constants matrix in
283
+ Voigt ordering.
284
+
285
+ Parameters:
286
+ elastic_constants : float
287
+ A 6x6 numpy array containing the elastic constants in
288
+ Voigt ordering. The material can have arbitrary anisotropy.
289
+
290
+ Returns:
291
+ bulk : float
292
+ The bulk modulus, defined as the ratio between the hydrostatic
293
+ stress (negative of the pressure p) in hydrostatic loading and
294
+ the diltation e (trace of the strain tensor), i.e. B = -p/e
295
+ """
296
+ # Compute bulk modulus, based on exercise 6.14 in Tadmor, Miller, Elliott,
297
+ # Continuum Mechanics and Thermodynamics, Cambridge University Press, 2012.
298
+ rank_elastic_constants = np.linalg.matrix_rank(elastic_constants)
299
+ elastic_constants_aug = np.concatenate(
300
+ (elastic_constants, np.transpose([[1, 1, 1, 0, 0, 0]])), 1
301
+ )
302
+ rank_elastic_constants_aug = np.linalg.matrix_rank(elastic_constants_aug)
303
+ if rank_elastic_constants_aug > rank_elastic_constants:
304
+ assert rank_elastic_constants_aug == rank_elastic_constants + 1
305
+ logger.info(
306
+ "Information: Hydrostatic pressure not in the image of the elasticity "
307
+ "matrix, zero bulk modulus!"
308
+ )
309
+ return 0.0
310
+ else:
311
+ # if a solution exists for a stress state of [1,1,1,0,0,0],
312
+ # you can always use the pseudoinverse
313
+ compliance = np.linalg.pinv(elastic_constants)
314
+ bulk = 1 / np.sum(compliance[0:3, 0:3])
315
+ return bulk
316
+
317
+
318
+ def map_to_Kelvin(C: npt.ArrayLike) -> npt.ArrayLike:
319
+ """
320
+ Compute the Kelvin form of the input 6x6 Voigt matrix
321
+ """
322
+ Ch = C.copy()
323
+ Ch[0:3, 3:6] *= math.sqrt(2.0)
324
+ Ch[3:6, 0:3] *= math.sqrt(2.0)
325
+ Ch[3:6, 3:6] *= 2.0
326
+ return Ch
327
+
328
+
329
+ def function_of_matrix(A, f):
330
+ """Compute the function of a matrix"""
331
+ ev, R = np.linalg.eigh(A)
332
+ Dtilde = np.diag([f(e) for e in ev])
333
+ return np.matmul(np.matmul(R, Dtilde), np.transpose(R))
334
+
335
+
336
+ def find_nearest_isotropy(elastic_constants):
337
+ """
338
+ Compute the distance between the provided matrix of elastic constants
339
+ in Voigt notation, to the nearest matrix of elastic constants for an
340
+ isotropic material. Return this distance, and the isotropic bulk and
341
+ shear modulus.
342
+
343
+ Ref: Morin, L; Gilormini, P and Derrien, K,
344
+ "Generalized Euclidean Distances for Elasticity Tensors",
345
+ Journal of Elasticity, Vol 138, pp. 221-232 (2020).
346
+
347
+ Parameters:
348
+ elastic_constants : float
349
+ A 6x6 numpy array containing the elastic constants in
350
+ Voigt ordering. The material can have arbitrary anisotropy.
351
+
352
+ Returns:
353
+ d : float
354
+ Distance to the nearest elastic constants.
355
+ log Euclidean metric.
356
+ kappa : float
357
+ Isotropic bulk modulus
358
+ mu : float
359
+ Isotropic shear modulus
360
+ """
361
+ E0 = 1.0 # arbitrary scaling constant (result unaffected by it)
362
+
363
+ JJ = np.zeros(shape=(6, 6))
364
+ KK = np.zeros(shape=(6, 6))
365
+ v = {0: [0, 0], 1: [1, 1], 2: [2, 2], 3: [1, 2], 4: [0, 2], 5: [0, 1]}
366
+ for ii in range(6):
367
+ for jj in range(6):
368
+ # i j k l = v[ii][0] v[ii][1] v[jj][0] v[jj][1]
369
+ JJ[ii][jj] = (1.0 / 3.0) * (v[ii][0] == v[ii][1]) * (v[jj][0] == v[jj][1])
370
+ KK[ii][jj] = (1.0 / 2.0) * (
371
+ (v[ii][0] == v[jj][0]) * (v[ii][1] == v[jj][1])
372
+ + (v[ii][0] == v[jj][1]) * (v[ii][1] == v[jj][0])
373
+ ) - JJ[ii][jj]
374
+ Chat = map_to_Kelvin(elastic_constants)
375
+ JJhat = map_to_Kelvin(JJ)
376
+ KKhat = map_to_Kelvin(KK)
377
+
378
+ # Eqn (49) in Morin et al.
379
+ fCoverE0 = function_of_matrix(Chat / E0, math.log)
380
+ kappa = (E0 / 3.0) * math.exp(np.einsum("ij,ij", fCoverE0, JJhat))
381
+ mu = (E0 / 2.0) * math.exp(0.2 * np.einsum("ij,ij", fCoverE0, KKhat))
382
+
383
+ # Eqn (47) in Morin et al.
384
+ dmat = (
385
+ fCoverE0 - math.log(3.0 * kappa / E0) * JJhat - math.log(2.0 * mu / E0) * KKhat
386
+ )
387
+ d = math.sqrt(np.einsum("ij,ij", dmat, dmat))
388
+
389
+ # Return results
390
+ return d, kappa, mu