symcrys 0.2.0 → 0.3.1

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,437 @@
1
+ /**
2
+ * The Wyckoff positions of the crystal classes as ITA prints them (6th ed., 2016, Table 3.2.3.2,
3
+ * pp. 745–769), one entry per printed setting: letter, multiplicity, oriented site-symmetry symbol,
4
+ * the first coordinate triplet and the Miller indices of the first face. The same rows are in
5
+ * references/itc/vol-a-table-3.2.3.2-wyckoff-positions.md, which the reference test re-parses and
6
+ * compares with this file; change both only by transcription from print.
7
+ *
8
+ * Notation: a leading `-` is an overbar; components separated by commas; Bravais–Miller indices
9
+ * (four components) for hexagonal axes. The origin (letter o) has no face (`''`).
10
+ */
11
+
12
+ /** [letter, multiplicity, oriented site symmetry, coordinates, Miller indices]. */
13
+ export type WyckoffRow = readonly [string, number, string, string, string];
14
+
15
+ export interface WyckoffEntry {
16
+ /** ITC short symbol of the setting as in the headline of the entry. */
17
+ readonly symbol: string;
18
+ /** Monoclinic: the unique axis of the entry. */
19
+ readonly uniqueAxis?: 'b' | 'c';
20
+ /** Printed page. */
21
+ readonly page: number;
22
+ /** In printed order, general position first. */
23
+ readonly rows: readonly WyckoffRow[];
24
+ }
25
+
26
+ export const ITA_WYCKOFF: readonly WyckoffEntry[] = [
27
+ {
28
+ symbol: '1',
29
+ page: 745,
30
+ rows: [['a', 1, '1', 'x,y,z', 'h,k,l']],
31
+ },
32
+ {
33
+ symbol: '-1',
34
+ page: 745,
35
+ rows: [
36
+ ['a', 2, '1', 'x,y,z', 'h,k,l'],
37
+ ['o', 1, '-1', '0,0,0', ''],
38
+ ],
39
+ },
40
+ {
41
+ symbol: '2',
42
+ uniqueAxis: 'b',
43
+ page: 745,
44
+ rows: [
45
+ ['b', 2, '1', 'x,y,z', 'h,k,l'],
46
+ ['a', 1, '2', '0,y,0', '0,1,0'],
47
+ ],
48
+ },
49
+ {
50
+ symbol: '2',
51
+ uniqueAxis: 'c',
52
+ page: 745,
53
+ rows: [
54
+ ['b', 2, '1', 'x,y,z', 'h,k,l'],
55
+ ['a', 1, '2', '0,0,z', '0,0,1'],
56
+ ],
57
+ },
58
+ {
59
+ symbol: 'm',
60
+ uniqueAxis: 'b',
61
+ page: 746,
62
+ rows: [
63
+ ['b', 2, '1', 'x,y,z', 'h,k,l'],
64
+ ['a', 1, 'm', 'x,0,z', 'h,0,l'],
65
+ ],
66
+ },
67
+ {
68
+ symbol: 'm',
69
+ uniqueAxis: 'c',
70
+ page: 746,
71
+ rows: [
72
+ ['b', 2, '1', 'x,y,z', 'h,k,l'],
73
+ ['a', 1, 'm', 'x,y,0', 'h,k,0'],
74
+ ],
75
+ },
76
+ {
77
+ symbol: '2/m',
78
+ uniqueAxis: 'b',
79
+ page: 746,
80
+ rows: [
81
+ ['c', 4, '1', 'x,y,z', 'h,k,l'],
82
+ ['b', 2, 'm', 'x,0,z', 'h,0,l'],
83
+ ['a', 2, '2', '0,y,0', '0,1,0'],
84
+ ['o', 1, '2/m', '0,0,0', ''],
85
+ ],
86
+ },
87
+ {
88
+ symbol: '2/m',
89
+ uniqueAxis: 'c',
90
+ page: 747,
91
+ rows: [
92
+ ['c', 4, '1', 'x,y,z', 'h,k,l'],
93
+ ['b', 2, 'm', 'x,y,0', 'h,k,0'],
94
+ ['a', 2, '2', '0,0,z', '0,0,1'],
95
+ ['o', 1, '2/m', '0,0,0', ''],
96
+ ],
97
+ },
98
+ {
99
+ symbol: '222',
100
+ page: 747,
101
+ rows: [
102
+ ['d', 4, '1', 'x,y,z', 'h,k,l'],
103
+ ['c', 2, '..2', '0,0,z', '0,0,1'],
104
+ ['b', 2, '.2.', '0,y,0', '0,1,0'],
105
+ ['a', 2, '2..', 'x,0,0', '1,0,0'],
106
+ ['o', 1, '222', '0,0,0', ''],
107
+ ],
108
+ },
109
+ {
110
+ symbol: 'mm2',
111
+ page: 748,
112
+ rows: [
113
+ ['d', 4, '1', 'x,y,z', 'h,k,l'],
114
+ ['c', 2, 'm..', '0,y,z', '0,k,l'],
115
+ ['b', 2, '.m.', 'x,0,z', 'h,0,l'],
116
+ ['a', 1, 'mm2', '0,0,z', '0,0,1'],
117
+ ],
118
+ },
119
+ {
120
+ symbol: 'mmm',
121
+ page: 748,
122
+ rows: [
123
+ ['g', 8, '1', 'x,y,z', 'h,k,l'],
124
+ ['f', 4, '..m', 'x,y,0', 'h,k,0'],
125
+ ['e', 4, '.m.', 'x,0,z', 'h,0,l'],
126
+ ['d', 4, 'm..', '0,y,z', '0,k,l'],
127
+ ['c', 2, 'mm2', '0,0,z', '0,0,1'],
128
+ ['b', 2, 'm2m', '0,y,0', '0,1,0'],
129
+ ['a', 2, '2mm', 'x,0,0', '1,0,0'],
130
+ ['o', 1, 'mmm', '0,0,0', ''],
131
+ ],
132
+ },
133
+ {
134
+ symbol: '4',
135
+ page: 749,
136
+ rows: [
137
+ ['b', 4, '1', 'x,y,z', 'h,k,l'],
138
+ ['a', 1, '4..', '0,0,z', '0,0,1'],
139
+ ],
140
+ },
141
+ {
142
+ symbol: '-4',
143
+ page: 749,
144
+ rows: [
145
+ ['b', 4, '1', 'x,y,z', 'h,k,l'],
146
+ ['a', 2, '2..', '0,0,z', '0,0,1'],
147
+ ['o', 1, '-4..', '0,0,0', ''],
148
+ ],
149
+ },
150
+ {
151
+ symbol: '4/m',
152
+ page: 749,
153
+ rows: [
154
+ ['c', 8, '1', 'x,y,z', 'h,k,l'],
155
+ ['b', 4, 'm..', 'x,y,0', 'h,k,0'],
156
+ ['a', 2, '4..', '0,0,z', '0,0,1'],
157
+ ['o', 1, '4/m..', '0,0,0', ''],
158
+ ],
159
+ },
160
+ {
161
+ symbol: '422',
162
+ page: 750,
163
+ rows: [
164
+ ['d', 8, '1', 'x,y,z', 'h,k,l'],
165
+ ['c', 4, '.2.', 'x,0,0', '1,0,0'],
166
+ ['b', 4, '..2', 'x,x,0', '1,1,0'],
167
+ ['a', 2, '4..', '0,0,z', '0,0,1'],
168
+ ['o', 1, '422', '0,0,0', ''],
169
+ ],
170
+ },
171
+ {
172
+ symbol: '4mm',
173
+ page: 750,
174
+ rows: [
175
+ ['d', 8, '1', 'x,y,z', 'h,k,l'],
176
+ ['c', 4, '.m.', 'x,0,z', 'h,0,l'],
177
+ ['b', 4, '..m', 'x,x,z', 'h,h,l'],
178
+ ['a', 1, '4mm', '0,0,z', '0,0,1'],
179
+ ],
180
+ },
181
+ {
182
+ symbol: '-42m',
183
+ page: 751,
184
+ rows: [
185
+ ['d', 8, '1', 'x,y,z', 'h,k,l'],
186
+ ['c', 4, '..m', 'x,x,z', 'h,h,l'],
187
+ ['b', 4, '.2.', 'x,0,0', '1,0,0'],
188
+ ['a', 2, '2.mm', '0,0,z', '0,0,1'],
189
+ ['o', 1, '-42m', '0,0,0', ''],
190
+ ],
191
+ },
192
+ {
193
+ symbol: '-4m2',
194
+ page: 751,
195
+ rows: [
196
+ ['d', 8, '1', 'x,y,z', 'h,k,l'],
197
+ ['c', 4, '.m.', 'x,0,z', 'h,0,l'],
198
+ ['b', 4, '..2', 'x,x,0', '1,1,0'],
199
+ ['a', 2, '2mm.', '0,0,z', '0,0,1'],
200
+ ['o', 1, '-4m2', '0,0,0', ''],
201
+ ],
202
+ },
203
+ {
204
+ symbol: '4/mmm',
205
+ page: 752,
206
+ rows: [
207
+ ['g', 16, '1', 'x,y,z', 'h,k,l'],
208
+ ['f', 8, '.m.', 'x,0,z', 'h,0,l'],
209
+ ['e', 8, '..m', 'x,x,z', 'h,h,l'],
210
+ ['d', 8, 'm..', 'x,y,0', 'h,k,0'],
211
+ ['c', 4, 'm2m.', 'x,0,0', '1,0,0'],
212
+ ['b', 4, 'm.m2', 'x,x,0', '1,1,0'],
213
+ ['a', 2, '4mm', '0,0,z', '0,0,1'],
214
+ ['o', 1, '4/mmm', '0,0,0', ''],
215
+ ],
216
+ },
217
+ {
218
+ symbol: '3',
219
+ page: 752,
220
+ rows: [
221
+ ['b', 3, '1', 'x,y,z', 'h,k,i,l'],
222
+ ['a', 1, '3..', '0,0,z', '0,0,0,1'],
223
+ ],
224
+ },
225
+ {
226
+ symbol: '-3',
227
+ page: 753,
228
+ rows: [
229
+ ['b', 6, '1', 'x,y,z', 'h,k,i,l'],
230
+ ['a', 2, '3..', '0,0,z', '0,0,0,1'],
231
+ ['o', 1, '-3..', '0,0,0', ''],
232
+ ],
233
+ },
234
+ {
235
+ symbol: '312',
236
+ page: 754,
237
+ rows: [
238
+ ['c', 6, '1', 'x,y,z', 'h,k,i,l'],
239
+ ['b', 3, '..2', 'x,-x,0', '1,0,-1,0'],
240
+ ['a', 2, '3..', '0,0,z', '0,0,0,1'],
241
+ ['o', 1, '3.2', '0,0,0', ''],
242
+ ],
243
+ },
244
+ {
245
+ symbol: '321',
246
+ page: 754,
247
+ rows: [
248
+ ['c', 6, '1', 'x,y,z', 'h,k,i,l'],
249
+ ['b', 3, '.2.', 'x,0,0', '1,1,-2,0'],
250
+ ['a', 2, '3..', '0,0,z', '0,0,0,1'],
251
+ ['o', 1, '32.', '0,0,0', ''],
252
+ ],
253
+ },
254
+ {
255
+ symbol: '3m1',
256
+ page: 755,
257
+ rows: [
258
+ ['c', 6, '1', 'x,y,z', 'h,k,i,l'],
259
+ ['b', 3, '.m.', 'x,-x,z', 'h,0,-h,l'],
260
+ ['a', 1, '3m.', '0,0,z', '0,0,0,1'],
261
+ ],
262
+ },
263
+ {
264
+ symbol: '31m',
265
+ page: 756,
266
+ rows: [
267
+ ['c', 6, '1', 'x,y,z', 'h,k,i,l'],
268
+ ['b', 3, '..m', 'x,0,z', 'h,h,-2h,l'],
269
+ ['a', 1, '3.m', '0,0,z', '0,0,0,1'],
270
+ ],
271
+ },
272
+ {
273
+ symbol: '-31m',
274
+ page: 757,
275
+ rows: [
276
+ ['d', 12, '1', 'x,y,z', 'h,k,i,l'],
277
+ ['c', 6, '..m', 'x,0,z', 'h,h,-2h,l'],
278
+ ['b', 6, '..2', 'x,-x,0', '1,0,-1,0'],
279
+ ['a', 2, '3.m', '0,0,z', '0,0,0,1'],
280
+ ['o', 1, '-3.m', '0,0,0', ''],
281
+ ],
282
+ },
283
+ {
284
+ symbol: '-3m1',
285
+ page: 758,
286
+ rows: [
287
+ ['d', 12, '1', 'x,y,z', 'h,k,i,l'],
288
+ ['c', 6, '.m.', 'x,-x,z', 'h,0,-h,l'],
289
+ ['b', 6, '.2.', 'x,0,0', '1,1,-2,0'],
290
+ ['a', 2, '3m.', '0,0,z', '0,0,0,1'],
291
+ ['o', 1, '-3m.', '0,0,0', ''],
292
+ ],
293
+ },
294
+ {
295
+ symbol: '6',
296
+ page: 760,
297
+ rows: [
298
+ ['b', 6, '1', 'x,y,z', 'h,k,i,l'],
299
+ ['a', 1, '6..', '0,0,z', '0,0,0,1'],
300
+ ],
301
+ },
302
+ {
303
+ symbol: '-6',
304
+ page: 760,
305
+ rows: [
306
+ ['c', 6, '1', 'x,y,z', 'h,k,i,l'],
307
+ ['b', 3, 'm..', 'x,y,0', 'h,k,i,0'],
308
+ ['a', 2, '3..', '0,0,z', '0,0,0,1'],
309
+ ['o', 1, '-6..', '0,0,0', ''],
310
+ ],
311
+ },
312
+ {
313
+ symbol: '6/m',
314
+ page: 760,
315
+ rows: [
316
+ ['c', 12, '1', 'x,y,z', 'h,k,i,l'],
317
+ ['b', 6, 'm..', 'x,y,0', 'h,k,i,0'],
318
+ ['a', 2, '6..', '0,0,z', '0,0,0,1'],
319
+ ['o', 1, '6/m..', '0,0,0', ''],
320
+ ],
321
+ },
322
+ {
323
+ symbol: '622',
324
+ page: 761,
325
+ rows: [
326
+ ['d', 12, '1', 'x,y,z', 'h,k,i,l'],
327
+ ['c', 6, '..2', 'x,-x,0', '1,0,-1,0'],
328
+ ['b', 6, '.2.', 'x,0,0', '1,1,-2,0'],
329
+ ['a', 2, '6..', '0,0,z', '0,0,0,1'],
330
+ ['o', 1, '622', '0,0,0', ''],
331
+ ],
332
+ },
333
+ {
334
+ symbol: '6mm',
335
+ page: 762,
336
+ rows: [
337
+ ['d', 12, '1', 'x,y,z', 'h,k,i,l'],
338
+ ['c', 6, '.m.', 'x,-x,z', 'h,0,-h,l'],
339
+ ['b', 6, '..m', 'x,0,z', 'h,h,-2h,l'],
340
+ ['a', 1, '6mm', '0,0,z', '0,0,0,1'],
341
+ ],
342
+ },
343
+ {
344
+ symbol: '-6m2',
345
+ page: 762,
346
+ rows: [
347
+ ['e', 12, '1', 'x,y,z', 'h,k,i,l'],
348
+ ['d', 6, 'm..', 'x,y,0', 'h,k,i,0'],
349
+ ['c', 6, '.m.', 'x,-x,z', 'h,0,-h,l'],
350
+ ['b', 3, 'mm2', 'x,-x,0', '1,0,-1,0'],
351
+ ['a', 2, '3m.', '0,0,z', '0,0,0,1'],
352
+ ['o', 1, '-6m2', '0,0,0', ''],
353
+ ],
354
+ },
355
+ {
356
+ symbol: '-62m',
357
+ page: 763,
358
+ rows: [
359
+ ['e', 12, '1', 'x,y,z', 'h,k,i,l'],
360
+ ['d', 6, 'm..', 'x,y,0', 'h,k,i,0'],
361
+ ['c', 6, '..m', 'x,0,z', 'h,h,-2h,l'],
362
+ ['b', 3, 'm2m', 'x,0,0', '1,1,-2,0'],
363
+ ['a', 2, '3.m', '0,0,z', '0,0,0,1'],
364
+ ['o', 1, '-62m', '0,0,0', ''],
365
+ ],
366
+ },
367
+ {
368
+ symbol: '6/mmm',
369
+ page: 764,
370
+ rows: [
371
+ ['g', 24, '1', 'x,y,z', 'h,k,i,l'],
372
+ ['f', 12, 'm..', 'x,y,0', 'h,k,i,0'],
373
+ ['e', 12, '.m.', 'x,2x,z', 'h,0,-h,l'],
374
+ ['d', 12, '..m', 'x,0,z', 'h,h,-2h,l'],
375
+ ['c', 6, 'mm2', 'x,2x,0', '1,0,-1,0'],
376
+ ['b', 6, 'm2m', 'x,0,0', '1,1,-2,0'],
377
+ ['a', 2, '6mm', '0,0,z', '0,0,0,1'],
378
+ ['o', 1, '6mmm', '0,0,0', ''],
379
+ ],
380
+ },
381
+ {
382
+ symbol: '23',
383
+ page: 765,
384
+ rows: [
385
+ ['c', 12, '1', 'x,y,z', 'h,k,l'],
386
+ ['b', 6, '2..', 'x,0,0', '1,0,0'],
387
+ ['a', 4, '.3.', 'x,x,x', '1,1,1'],
388
+ ['o', 1, '23.', '0,0,0', ''],
389
+ ],
390
+ },
391
+ {
392
+ symbol: 'm-3',
393
+ page: 766,
394
+ rows: [
395
+ ['d', 24, '1', 'x,y,z', 'h,k,l'],
396
+ ['c', 12, 'm..', '0,y,z', '0,k,l'],
397
+ ['b', 8, '.3.', 'x,x,x', '1,1,1'],
398
+ ['a', 6, '2mm..', 'x,0,0', '1,0,0'],
399
+ ['o', 1, 'm-3.', '0,0,0', ''],
400
+ ],
401
+ },
402
+ {
403
+ symbol: '432',
404
+ page: 767,
405
+ rows: [
406
+ ['d', 24, '1', 'x,y,z', 'h,k,l'],
407
+ ['c', 12, '..2', '0,y,y', '0,1,1'],
408
+ ['b', 8, '.3.', 'x,x,x', '1,1,1'],
409
+ ['a', 6, '4..', 'x,0,0', '1,0,0'],
410
+ ['o', 1, '432', '0,0,0', ''],
411
+ ],
412
+ },
413
+ {
414
+ symbol: '-43m',
415
+ page: 768,
416
+ rows: [
417
+ ['d', 24, '1', 'x,y,z', 'h,k,l'],
418
+ ['c', 12, '..m', 'x,x,z', 'h,h,l'],
419
+ ['b', 6, '2.mm', 'x,0,0', '1,0,0'],
420
+ ['a', 4, '.3m', 'x,x,x', '1,1,1'],
421
+ ['o', 1, '-43m', '0,0,0', ''],
422
+ ],
423
+ },
424
+ {
425
+ symbol: 'm-3m',
426
+ page: 769,
427
+ rows: [
428
+ ['f', 48, '1', 'x,y,z', 'h,k,l'],
429
+ ['e', 24, '..m', 'x,x,z', 'h,h,l'],
430
+ ['d', 24, 'm..', '0,y,z', '0,k,l'],
431
+ ['c', 12, 'm.m2', '0,y,y', '0,1,1'],
432
+ ['b', 8, '.3m', 'x,x,x', '1,1,1'],
433
+ ['a', 6, '4m.m', 'x,0,0', '1,0,0'],
434
+ ['o', 1, 'm-3m', '0,0,0', ''],
435
+ ],
436
+ },
437
+ ];
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Site symmetry of a direction under any list of point operations: which operations leave it
3
+ * fixed (docs/conventions.md, "Site symmetry and Wyckoff positions").
4
+ */
5
+
6
+ import { apply, matricesEqual, multiply, type Vec3 } from './linalg.js';
7
+ import type { Operation } from './operations.js';
8
+ import { EPSILON } from './tolerances.js';
9
+
10
+ export interface SiteSymmetryOptions {
11
+ /**
12
+ * A direction counts as fixed by an operation if the operation moves its unit vector by less
13
+ * than this (Euclidean distance). Default `EPSILON` (10⁻⁶); a caller whose directions carry
14
+ * rounding errors (e.g. read from a rounded link) passes a larger value.
15
+ */
16
+ readonly tolerance?: number;
17
+ }
18
+
19
+ /** The site symmetry of a direction. */
20
+ export interface SiteSymmetry {
21
+ /**
22
+ * Indices into the given operations of those that leave the direction fixed, ascending: for a group,
23
+ * a subgroup (the operations fixing within the tolerance, closed under products, see `siteSymmetry`).
24
+ */
25
+ readonly operations: readonly number[];
26
+ /**
27
+ * The unit vector of the direction, set onto the fixed space of its site symmetry (the mean of
28
+ * its images under the site symmetry, normalised), which removes rounding errors within the
29
+ * tolerance.
30
+ */
31
+ readonly direction: Vec3;
32
+ /** Number of operations divided by the order of the site symmetry: the number of equivalent directions. */
33
+ readonly multiplicity: number;
34
+ /** True if no operation other than the identity leaves the direction fixed (general position). */
35
+ readonly general: boolean;
36
+ }
37
+
38
+ function unit(v: Vec3): Vec3 {
39
+ const n = Math.hypot(v[0], v[1], v[2]);
40
+ if (!(n > 0) || !Number.isFinite(n)) throw new RangeError(`Not a direction: ${JSON.stringify(v)}`);
41
+ return [v[0] / n, v[1] / n, v[2] / n];
42
+ }
43
+
44
+ function fixes(op: Operation, v: Vec3, tolerance: number): boolean {
45
+ const w = apply(op.matrix, v);
46
+ return Math.hypot(w[0] - v[0], w[1] - v[1], w[2] - v[2]) < tolerance;
47
+ }
48
+
49
+ function isIdentity(op: Operation): boolean {
50
+ return op.matrix.every((row, i) => row.every((v, j) => Math.abs(v - (i === j ? 1 : 0)) < EPSILON));
51
+ }
52
+
53
+ /** Products already looked up, per list of operations: `i * n + j` → index of op[i]·op[j] in the list, or -1. */
54
+ const productTables = new WeakMap<readonly Operation[], Map<number, number>>();
55
+
56
+ function productIndex(operations: readonly Operation[], i: number, j: number): number {
57
+ let table = productTables.get(operations);
58
+ if (!table) {
59
+ table = new Map();
60
+ productTables.set(operations, table);
61
+ }
62
+ const key = i * operations.length + j;
63
+ let k = table.get(key);
64
+ if (k === undefined) {
65
+ const product = multiply(operations[i].matrix, operations[j].matrix);
66
+ k = operations.findIndex((op) => matricesEqual(op.matrix, product));
67
+ table.set(key, k);
68
+ }
69
+ return k;
70
+ }
71
+
72
+ /** The indices plus those of every product of two of them that is itself in the list, until closed. */
73
+ function closeWithin(operations: readonly Operation[], indices: readonly number[]): number[] {
74
+ const set = [...indices];
75
+ const inSet = new Set(set);
76
+ // Each element is multiplied with every element before it once, in both orders, as it is added.
77
+ for (let i = 0; i < set.length; i++) {
78
+ for (let j = 0; j <= i; j++) {
79
+ for (const k of [productIndex(operations, set[i], set[j]), productIndex(operations, set[j], set[i])]) {
80
+ if (k >= 0 && !inSet.has(k)) {
81
+ inSet.add(k);
82
+ set.push(k);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ return set.sort((x, y) => x - y);
88
+ }
89
+
90
+ /** The normalised mean of the images of v under the operations at `indices`; undefined if it vanishes. */
91
+ function meanImage(operations: readonly Operation[], indices: readonly number[], v: Vec3): Vec3 | undefined {
92
+ if (indices.length === 0) return v;
93
+ const sum: [number, number, number] = [0, 0, 0];
94
+ for (const i of indices) {
95
+ const w = apply(operations[i].matrix, v);
96
+ sum[0] += w[0];
97
+ sum[1] += w[1];
98
+ sum[2] += w[2];
99
+ }
100
+ const n = Math.hypot(sum[0], sum[1], sum[2]);
101
+ return n > 1e-9 * indices.length ? [sum[0] / n, sum[1] / n, sum[2] / n] : undefined;
102
+ }
103
+
104
+ const sameIndices = (a: readonly number[], b: readonly number[]): boolean =>
105
+ a.length === b.length && a.every((x, i) => x === b[i]);
106
+
107
+ /** The tolerance of the options, checked. */
108
+ export function siteTolerance(options?: SiteSymmetryOptions): number {
109
+ const t = options?.tolerance ?? EPSILON;
110
+ if (!(typeof t === 'number' && t > 0 && t < 1)) throw new RangeError(`Invalid tolerance: ${String(t)}`);
111
+ return t;
112
+ }
113
+
114
+ /**
115
+ * The site symmetry of `direction` (any non-zero vector, Cartesian) under `operations`, which may
116
+ * be any list of point operations (for instance those of a distorted crystal). Time reversal is
117
+ * ignored: a direction or a face normal is not changed by it. The operations fixing the direction
118
+ * within the tolerance are closed under the products found in the list and the direction is set onto
119
+ * their fixed space, repeated until stable, so that for a group the result is a subgroup and the
120
+ * set direction lies exactly on its elements. For a list that is not a group, `multiplicity` may
121
+ * not be an integer. Throws a RangeError for a zero vector, and if the closed operations fix no
122
+ * common direction (a tolerance far too large, e.g. 0.5).
123
+ */
124
+ export function siteSymmetry(
125
+ operations: readonly Operation[],
126
+ direction: Vec3,
127
+ options?: SiteSymmetryOptions,
128
+ ): SiteSymmetry {
129
+ const tolerance = siteTolerance(options);
130
+ const v = unit(direction);
131
+ const fixing = (w: Vec3): number[] => {
132
+ const out: number[] = [];
133
+ operations.forEach((op, i) => {
134
+ if (fixes(op, w, tolerance)) out.push(i);
135
+ });
136
+ return out;
137
+ };
138
+ // Within the tolerance the operations fixing v need not form a group (near an axis a rotation can count
139
+ // as fixing while its square does not). Close them under products found in the list, set v onto the
140
+ // fixed space of the closure (the mean of its images, the projector for a group), and repeat on the
141
+ // projected direction until the set is stable.
142
+ let indices = fixing(v);
143
+ let projected = v;
144
+ let set = closeWithin(operations, indices);
145
+ for (let pass = 0; pass < 8; pass++) {
146
+ const mean = meanImage(operations, set, v);
147
+ if (mean === undefined) {
148
+ throw new RangeError(
149
+ `No common fixed direction for the operations fixing ${JSON.stringify(direction)} within ${tolerance} (tolerance too large?)`,
150
+ );
151
+ }
152
+ const next = closeWithin(operations, fixing(mean));
153
+ if (sameIndices(next, set)) {
154
+ indices = set;
155
+ projected = mean;
156
+ break;
157
+ }
158
+ set = next;
159
+ }
160
+ return Object.freeze({
161
+ operations: Object.freeze(indices),
162
+ direction: Object.freeze(projected) as Vec3,
163
+ multiplicity: indices.length > 0 ? operations.length / indices.length : operations.length,
164
+ general: indices.every((i) => isIdentity(operations[i])),
165
+ });
166
+ }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** Version of symcrys. Kept equal to `version` in package.json (checked by version.test.ts). */
2
- export const VERSION = '0.2.0';
2
+ export const VERSION = '0.3.1';