symcrys 0.6.0 → 0.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.
Files changed (89) hide show
  1. package/README.md +5 -4
  2. package/dist/crystalClasses.d.ts +106 -1
  3. package/dist/crystalClasses.d.ts.map +1 -1
  4. package/dist/crystalClasses.js +80 -1
  5. package/dist/crystalClasses.js.map +1 -1
  6. package/dist/index.d.ts +4 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/lattice.d.ts +5 -1
  11. package/dist/lattice.d.ts.map +1 -1
  12. package/dist/lattice.js +3 -1
  13. package/dist/lattice.js.map +1 -1
  14. package/dist/magnetic.d.ts +3 -0
  15. package/dist/magnetic.d.ts.map +1 -1
  16. package/dist/magnetic.js +2 -0
  17. package/dist/magnetic.js.map +1 -1
  18. package/dist/operations.d.ts +2 -0
  19. package/dist/operations.d.ts.map +1 -1
  20. package/dist/operations.js.map +1 -1
  21. package/dist/pointGroups/crystalForms.d.ts +70 -0
  22. package/dist/pointGroups/crystalForms.d.ts.map +1 -0
  23. package/dist/pointGroups/crystalForms.js +69 -0
  24. package/dist/pointGroups/crystalForms.js.map +1 -0
  25. package/dist/pointGroups/crystalFormsData.d.ts +32 -0
  26. package/dist/pointGroups/crystalFormsData.d.ts.map +1 -0
  27. package/dist/pointGroups/crystalFormsData.js +518 -0
  28. package/dist/pointGroups/crystalFormsData.js.map +1 -0
  29. package/dist/pointGroups/data.d.ts +2 -1
  30. package/dist/pointGroups/data.d.ts.map +1 -1
  31. package/dist/pointGroups/data.js.map +1 -1
  32. package/dist/pointGroups/elements.d.ts +9 -2
  33. package/dist/pointGroups/elements.d.ts.map +1 -1
  34. package/dist/pointGroups/elements.js +7 -3
  35. package/dist/pointGroups/elements.js.map +1 -1
  36. package/dist/pointGroups/identify.d.ts +75 -0
  37. package/dist/pointGroups/identify.d.ts.map +1 -0
  38. package/dist/pointGroups/identify.js +193 -0
  39. package/dist/pointGroups/identify.js.map +1 -0
  40. package/dist/pointGroups/magneticData.d.ts +6 -1
  41. package/dist/pointGroups/magneticData.d.ts.map +1 -1
  42. package/dist/pointGroups/magneticData.js +4 -0
  43. package/dist/pointGroups/magneticData.js.map +1 -1
  44. package/dist/pointGroups/orbits.d.ts +85 -0
  45. package/dist/pointGroups/orbits.d.ts.map +1 -0
  46. package/dist/pointGroups/orbits.js +134 -0
  47. package/dist/pointGroups/orbits.js.map +1 -0
  48. package/dist/pointGroups/pointGroups.d.ts +10 -2
  49. package/dist/pointGroups/pointGroups.d.ts.map +1 -1
  50. package/dist/pointGroups/pointGroups.js +2 -2
  51. package/dist/pointGroups/pointGroups.js.map +1 -1
  52. package/dist/pointGroups/wyckoff.d.ts +42 -1
  53. package/dist/pointGroups/wyckoff.d.ts.map +1 -1
  54. package/dist/pointGroups/wyckoff.js +55 -27
  55. package/dist/pointGroups/wyckoff.js.map +1 -1
  56. package/dist/siteSymmetry.d.ts +2 -1
  57. package/dist/siteSymmetry.d.ts.map +1 -1
  58. package/dist/siteSymmetry.js.map +1 -1
  59. package/dist/tensors/propertyTable.d.ts +12 -2
  60. package/dist/tensors/propertyTable.d.ts.map +1 -1
  61. package/dist/tensors/propertyTable.js +11 -2
  62. package/dist/tensors/propertyTable.js.map +1 -1
  63. package/dist/tensors/tensorForm.d.ts +6 -1
  64. package/dist/tensors/tensorForm.d.ts.map +1 -1
  65. package/dist/tensors/tensorForm.js +1 -1
  66. package/dist/tensors/tensorForm.js.map +1 -1
  67. package/dist/version.d.ts +6 -2
  68. package/dist/version.d.ts.map +1 -1
  69. package/dist/version.js +6 -2
  70. package/dist/version.js.map +1 -1
  71. package/package.json +2 -2
  72. package/src/crystalClasses.ts +164 -3
  73. package/src/index.ts +12 -0
  74. package/src/lattice.ts +5 -1
  75. package/src/magnetic.ts +18 -0
  76. package/src/operations.ts +2 -0
  77. package/src/pointGroups/crystalForms.ts +143 -0
  78. package/src/pointGroups/crystalFormsData.ts +539 -0
  79. package/src/pointGroups/data.ts +4 -1
  80. package/src/pointGroups/elements.ts +10 -3
  81. package/src/pointGroups/identify.ts +255 -0
  82. package/src/pointGroups/magneticData.ts +7 -1
  83. package/src/pointGroups/orbits.ts +228 -0
  84. package/src/pointGroups/pointGroups.ts +10 -2
  85. package/src/pointGroups/wyckoff.ts +61 -26
  86. package/src/siteSymmetry.ts +2 -1
  87. package/src/tensors/propertyTable.ts +12 -2
  88. package/src/tensors/tensorForm.ts +6 -1
  89. package/src/version.ts +6 -2
@@ -37,6 +37,7 @@ export interface Axis {
37
37
 
38
38
  /** One point operation described geometrically. */
39
39
  export interface OperationDescription {
40
+ /** What the operation is, without time reversal. */
40
41
  readonly kind: OperationKind;
41
42
  /**
42
43
  * The n of the symbol n or n̄: 1 for the identity and the inversion, 2 for a mirror (m = 2̄),
@@ -50,6 +51,7 @@ export interface OperationDescription {
50
51
  * from the tip of `axis` looking towards the origin), `'-'` if by −360°/n.
51
52
  */
52
53
  readonly sense?: '+' | '-';
54
+ /** True if the operation is combined with time reversal (a primed operation). */
53
55
  readonly timeReversal: boolean;
54
56
  }
55
57
 
@@ -76,6 +78,7 @@ export interface SymmetryDirection {
76
78
  * magnetic group with its primes (`4'/m`).
77
79
  */
78
80
  readonly symbol?: string;
81
+ /** The symmetry directions of this position (ITA Table 2.1.3.1), each with the elements along it. */
79
82
  readonly directions: readonly DirectionElements[];
80
83
  }
81
84
 
@@ -134,7 +137,7 @@ function rotationPart(m: Mat3): { r: Mat3; n: number } {
134
137
  }
135
138
 
136
139
  /** The axis of a proper rotation of order n ≥ 2 (sign arbitrary for n = 2). */
137
- function rotationAxis(r: Mat3, n: number): Vec3 {
140
+ export function rotationAxis(r: Mat3, n: number): Vec3 {
138
141
  if (n === 2) {
139
142
  // r + 1 has rank 1; its columns are parallel to the axis.
140
143
  let best: Vec3 = [0, 0, 0];
@@ -153,7 +156,11 @@ function rotationAxis(r: Mat3, n: number): Vec3 {
153
156
  }
154
157
 
155
158
  /** (r − rᵀ)/2 as a vector: sin θ times the unit axis for a rotation by θ. */
156
- const axialVector = (r: Mat3): Vec3 => [(r[2][1] - r[1][2]) / 2, (r[0][2] - r[2][0]) / 2, (r[1][0] - r[0][1]) / 2];
159
+ export const axialVector = (r: Mat3): Vec3 => [
160
+ (r[2][1] - r[1][2]) / 2,
161
+ (r[0][2] - r[2][0]) / 2,
162
+ (r[1][0] - r[0][1]) / 2,
163
+ ];
157
164
 
158
165
  function findAxis(axes: readonly Axis[], u: Vec3, what: string): Axis {
159
166
  const a = axes.find((x) => Math.abs(Math.abs(dot(x.cartesian, u)) - 1) < AXIS_TOLERANCE);
@@ -273,7 +280,7 @@ export function describeOperations(
273
280
  * positions of the crystal family are listed, also those the symbol of the class leaves out (then
274
281
  * without `symbol`); the triclinic family has none. The directions keep the sign of Table 2.1.3.1
275
282
  * in both conventions; in the Birss convention an axis of `describeOperations` may be reversed, so
276
- * match it to a direction by |dot| = 1, not by identity.
283
+ * match it to a direction whose dot product with it is ±1, not by identity.
277
284
  */
278
285
  export function symmetryDirections(key: MagneticPointGroupKey, options?: SettingOptions): readonly SymmetryDirection[] {
279
286
  return analysis(key, options).directions;
@@ -0,0 +1,255 @@
1
+ /**
2
+ * The point group of a list of operations, and the operations described geometrically, for any
3
+ * list (for instance the operations left after distorting a crystal; docs/conventions.md, "Point
4
+ * group of a list of operations"). Shared by both entry points.
5
+ */
6
+
7
+ import { det, multiply, trace, type Mat3, type Vec3 } from '../linalg.js';
8
+ import { checkOrthogonal, checkTimeReversal, type Operation } from '../operations.js';
9
+ import { EPSILON } from '../tolerances.js';
10
+ import { axialVector, rotationAxis, type OperationKind } from './elements.js';
11
+ import {
12
+ MAGNETIC_POINT_GROUP_KEYS,
13
+ operations as groupOperations,
14
+ settings,
15
+ type MagneticPointGroupKey,
16
+ } from './pointGroups.js';
17
+
18
+ /** Options of `pointGroupOf` and `describeOperationsOf`. */
19
+ export interface PointGroupOfOptions {
20
+ /**
21
+ * Two matrices are the same operation if no entry differs by this much or more, and a matrix
22
+ * counts as orthogonal and crystallographic within it. Default `EPSILON` (10⁻⁶); a caller whose
23
+ * matrices carry rounding errors passes a larger value, below 0.1.
24
+ */
25
+ readonly tolerance?: number;
26
+ }
27
+
28
+ /** The point group of a list of operations: its key, and the setting if the operations are exactly those of one. */
29
+ export interface IdentifiedPointGroup {
30
+ /** The key of the point group the operations generate. */
31
+ readonly key: MagneticPointGroupKey;
32
+ /**
33
+ * The 1-based index of the setting whose operations the group equals, in the Cartesian frame of
34
+ * symcrys; absent if the group is the point group in another orientation.
35
+ */
36
+ readonly setting?: number;
37
+ }
38
+
39
+ /** An axis in the Cartesian frame only, for operations that are not tied to a lattice. */
40
+ export interface CartesianAxis {
41
+ /** Unit vector; its first non-zero component, in the order x, y, z, is positive. */
42
+ readonly cartesian: Vec3;
43
+ }
44
+
45
+ /** One operation of any list described geometrically (`describeOperationsOf`). */
46
+ export interface CartesianOperationDescription {
47
+ /** What the operation is, without time reversal. */
48
+ readonly kind: OperationKind;
49
+ /**
50
+ * The n of the symbol n or n̄: 1 for the identity and the inversion, 2 for a mirror (m = 2̄),
51
+ * otherwise the order of the rotation part.
52
+ */
53
+ readonly order: number;
54
+ /** Rotation axis, rotoinversion axis or mirror normal; absent for the identity and the inversion. */
55
+ readonly axis?: CartesianAxis;
56
+ /**
57
+ * For n ≥ 3: `'+'` if the rotation part turns by +360°/n about `axis` (counter-clockwise seen
58
+ * from the tip of `axis` looking towards the origin), `'-'` if by −360°/n.
59
+ */
60
+ readonly sense?: '+' | '-';
61
+ /** True if the operation is combined with time reversal (a primed operation). */
62
+ readonly timeReversal: boolean;
63
+ }
64
+
65
+ /** Throws unless mᵀm = 1 within the tolerance (a matrix with rounding errors passes with a larger one). */
66
+ function checkOrthogonalWithin(m: Mat3, what: string, tolerance: number): void {
67
+ if (tolerance <= EPSILON) {
68
+ checkOrthogonal(m, what);
69
+ return;
70
+ }
71
+ for (let i = 0; i < 3; i++) {
72
+ for (let j = 0; j < 3; j++) {
73
+ const d = m[0][i] * m[0][j] + m[1][i] * m[1][j] + m[2][i] * m[2][j];
74
+ if (!(Math.abs(d - (i === j ? 1 : 0)) < tolerance)) {
75
+ throw new RangeError(`${what} is not an orthogonal (Cartesian) matrix: ${JSON.stringify(m)}`);
76
+ }
77
+ }
78
+ }
79
+ }
80
+
81
+ /** No point group has more than 96 operations (m-3m1'). */
82
+ const MAX_ORDER = 96;
83
+
84
+ function toleranceOf(options?: PointGroupOfOptions): number {
85
+ const t = options?.tolerance ?? EPSILON;
86
+ if (!(typeof t === 'number' && t > 0 && t < 0.1)) throw new RangeError(`Invalid tolerance: ${String(t)}`);
87
+ return t;
88
+ }
89
+
90
+ /**
91
+ * How far the trace of a matrix with entries within `tolerance` may lie from an integer: three
92
+ * diagonal entries, so three times the tolerance (at least that of `EPSILON`).
93
+ */
94
+ const traceTolerance = (tolerance: number): number => 3 * Math.max(tolerance, EPSILON);
95
+
96
+ const same = (a: Operation, b: Operation, tolerance: number): boolean =>
97
+ a.timeReversal === b.timeReversal &&
98
+ a.matrix.every((row, i) => row.every((v, j) => Math.abs(v - b.matrix[i][j]) < tolerance));
99
+
100
+ /** The kind of an operation (rounded trace, sign of the determinant, time reversal): equal operations have the same. */
101
+ const kindOf = (op: Operation): string =>
102
+ `${Math.round(trace(op.matrix))}${det(op.matrix) > 0 ? '+' : '-'}${op.timeReversal ? "'" : ''}`;
103
+
104
+ /**
105
+ * The group the operations generate, or undefined if it is larger than any point group. The
106
+ * operations of the list that are not yet in the group are taken as generators, and the group is
107
+ * grown by multiplying each element by each generator until nothing new appears; a new operation
108
+ * is compared only with the operations of its kind.
109
+ */
110
+ function close(ops: readonly Operation[], tolerance: number): Operation[] | undefined {
111
+ const identity: Operation = {
112
+ matrix: [
113
+ [1, 0, 0],
114
+ [0, 1, 0],
115
+ [0, 0, 1],
116
+ ],
117
+ timeReversal: false,
118
+ };
119
+ const group: Operation[] = [];
120
+ const byKind = new Map<string, Operation[]>();
121
+ const has = (op: Operation): boolean => (byKind.get(kindOf(op)) ?? []).some((g) => same(g, op, tolerance));
122
+ const push = (op: Operation): void => {
123
+ group.push(op);
124
+ const k = kindOf(op);
125
+ const list = byKind.get(k);
126
+ if (list) list.push(op);
127
+ else byKind.set(k, [op]);
128
+ };
129
+ push(identity);
130
+ const generators: Operation[] = [];
131
+ for (const s of ops) {
132
+ if (has(s)) continue;
133
+ generators.push(s);
134
+ for (let i = 0; i < group.length; i++) {
135
+ for (const t of generators) {
136
+ const product: Operation = {
137
+ matrix: multiply(group[i].matrix, t.matrix),
138
+ timeReversal: group[i].timeReversal !== t.timeReversal,
139
+ };
140
+ if (has(product)) continue;
141
+ if (group.length >= MAX_ORDER) return undefined;
142
+ push(product);
143
+ }
144
+ }
145
+ }
146
+ return group;
147
+ }
148
+
149
+ /**
150
+ * How many operations of each kind a group has: determinant, trace and time reversal of each. For
151
+ * the 122 point groups these counts are all different (a test checks it), and since the 122 are
152
+ * all point groups up to orientation, the counts identify the point group of any finite group of
153
+ * crystallographic operations.
154
+ */
155
+ const signature = (ops: readonly Operation[], tolerance = EPSILON): string =>
156
+ ops
157
+ .map((op) => {
158
+ const t = trace(op.matrix);
159
+ const rounded = Math.round(t);
160
+ if (Math.abs(t - rounded) > traceTolerance(tolerance)) return 'x'; // not a crystallographic operation
161
+ return `${Math.round(det(op.matrix))}:${rounded}${op.timeReversal ? "'" : ''}`;
162
+ })
163
+ .sort()
164
+ .join(' ');
165
+
166
+ let bySignature: Map<string, MagneticPointGroupKey> | undefined;
167
+
168
+ function keyOfSignature(s: string): MagneticPointGroupKey | undefined {
169
+ if (!bySignature) {
170
+ bySignature = new Map();
171
+ for (const key of MAGNETIC_POINT_GROUP_KEYS) bySignature.set(signature(groupOperations(key)), key);
172
+ }
173
+ return bySignature.get(s);
174
+ }
175
+
176
+ /**
177
+ * The point group generated by a list of Cartesian operations, for instance those left after
178
+ * distorting a crystal: its key, and the setting whose operations it equals in the frame of symcrys
179
+ * (absent if the group is the point group in another orientation). The list is closed into a group
180
+ * first, two matrices being equal when no entry differs by `tolerance` (default `EPSILON`) or more.
181
+ * Time reversal counts: a list with time reversal gives a magnetic point group. Returns undefined
182
+ * if the operations generate no point group (an operation of order 5, a group larger than 96).
183
+ * Throws a RangeError for a matrix that is not orthogonal (within `tolerance`, at least
184
+ * `EPSILON`). The classes `1` and `-1` have one setting and no orientation, so they always have
185
+ * setting 1.
186
+ */
187
+ export function pointGroupOf(
188
+ operations: readonly Operation[],
189
+ options?: PointGroupOfOptions,
190
+ ): IdentifiedPointGroup | undefined {
191
+ const tolerance = toleranceOf(options);
192
+ checkTimeReversal(operations);
193
+ operations.forEach((op, i) => checkOrthogonalWithin(op.matrix, `Operation ${i}`, tolerance));
194
+ const group = close(operations, tolerance);
195
+ if (!group) return undefined;
196
+ const key = keyOfSignature(signature(group, tolerance));
197
+ if (!key) return undefined;
198
+ // The operations passed in generate the group; if each is an operation of a setting of the same
199
+ // order, the group is that setting. Products are not compared, since their errors add up.
200
+ const setting = settings(key, 'itc').find((s) => {
201
+ const ops = groupOperations(key, { convention: 'itc', setting: s.index });
202
+ return ops.length === group.length && operations.every((g) => ops.some((op) => same(g, op, tolerance)));
203
+ });
204
+ return Object.freeze(setting ? { key, setting: setting.index } : { key });
205
+ }
206
+
207
+ /**
208
+ * The axis signed so that its first non-zero component, in the order x, y, z, is positive; a
209
+ * component smaller than the tolerance counts as zero.
210
+ */
211
+ function signed(v: Vec3, tolerance: number): { axis: Vec3; flip: boolean } {
212
+ const first = v.find((x) => Math.abs(x) >= Math.max(tolerance, EPSILON)) ?? 0;
213
+ return first < 0 ? { axis: [-v[0] + 0, -v[1] + 0, -v[2] + 0], flip: true } : { axis: v, flip: false };
214
+ }
215
+
216
+ const dot = (a: Vec3, b: Vec3): number => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
217
+
218
+ function describe(op: Operation, i: number, tolerance: number): CartesianOperationDescription {
219
+ checkOrthogonalWithin(op.matrix, `Operation ${i}`, tolerance);
220
+ const notCrystallographic = `Operation ${i} is not a crystallographic point operation`;
221
+ // The trace of the rotation part is an integer exactly for the angles 360°/n, n = 1, 2, 3, 4, 6.
222
+ const t = det(op.matrix) > 0 ? trace(op.matrix) : -trace(op.matrix);
223
+ if (Math.abs(t - Math.round(t)) > traceTolerance(tolerance)) throw new RangeError(notCrystallographic);
224
+ // The order from the trace of the rotation part, 1 + 2 cos(360°/n): 3, −1, 0, 1, 2 for n = 1, 2, 3, 4, 6.
225
+ const n = ({ 3: 1, [-1]: 2, 0: 3, 1: 4, 2: 6 } as Record<number, number>)[Math.round(t)];
226
+ if (n === undefined) throw new RangeError(notCrystallographic);
227
+ const r = (det(op.matrix) > 0 ? op.matrix : op.matrix.map((row) => row.map((v) => -v))) as Mat3;
228
+ const proper = det(op.matrix) > 0;
229
+ const timeReversal = op.timeReversal;
230
+ if (n === 1) return Object.freeze({ kind: proper ? 'identity' : 'inversion', order: 1, timeReversal });
231
+ const { axis } = signed(rotationAxis(r, n), tolerance);
232
+ const frozenAxis = Object.freeze({ cartesian: Object.freeze(axis) as unknown as Vec3 });
233
+ const kind: OperationKind = proper ? 'rotation' : n === 2 ? 'mirror' : 'rotoinversion';
234
+ if (n === 2) return Object.freeze({ kind, order: 2, axis: frozenAxis, timeReversal });
235
+ const sense = dot(axialVector(r), axis) > 0 ? '+' : '-';
236
+ return Object.freeze({ kind, order: n, axis: frozenAxis, sense, timeReversal });
237
+ }
238
+
239
+ /**
240
+ * Each operation of a list described geometrically, in the order of the list: kind, n, the
241
+ * Cartesian axis (rotation or rotoinversion axis, mirror normal) with its first non-zero component,
242
+ * in the order x, y, z, positive, and for n ≥ 3 the sense about that axis. The axes are not named
243
+ * by lattice directions, since the list need not belong to a setting; for a point group given by its
244
+ * key, `describeOperations` names them. Throws a RangeError for a matrix that is not orthogonal or
245
+ * not a crystallographic operation (a rotation part by an angle other than 360°/n, n = 1, 2, 3, 4,
246
+ * 6), both within `tolerance` (default `EPSILON`, as for `pointGroupOf`).
247
+ */
248
+ export function describeOperationsOf(
249
+ operations: readonly Operation[],
250
+ options?: PointGroupOfOptions,
251
+ ): readonly CartesianOperationDescription[] {
252
+ const tolerance = toleranceOf(options);
253
+ checkTimeReversal(operations);
254
+ return Object.freeze(operations.map((op, i) => describe(op, i, tolerance)));
255
+ }
@@ -112,11 +112,17 @@ export const BLACK_WHITE_POINT_GROUP_KEYS = Object.freeze([
112
112
  "m'-3'm",
113
113
  ] as const);
114
114
 
115
- export type BlackWhitePointGroupKey = (typeof BLACK_WHITE_POINT_GROUP_KEYS)[number];
115
+ /** The key of one of the 58 black-and-white magnetic point groups. */
116
+ // `Extract<…, string>`: the same type, named by the documentation (see `PointGroupKey`).
117
+ export type BlackWhitePointGroupKey = Extract<(typeof BLACK_WHITE_POINT_GROUP_KEYS)[number], string>;
116
118
 
117
119
  /** Keys of all 122 magnetic point groups: the 32 crystal classes, the 32 grey groups, the 58 black-and-white groups. */
118
120
  export type MagneticPointGroupKey = PointGroupKey | GreyPointGroupKey | BlackWhitePointGroupKey;
119
121
 
122
+ /**
123
+ * Keys of the 122 magnetic point groups: the 32 crystal classes (white groups), the 32 grey groups
124
+ * and the 58 black-and-white groups, in this order (docs/conventions.md, "Keys").
125
+ */
120
126
  export const MAGNETIC_POINT_GROUP_KEYS: readonly MagneticPointGroupKey[] = Object.freeze([
121
127
  ...POINT_GROUP_KEYS,
122
128
  ...GREY_POINT_GROUP_KEYS,
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Operations in the lattice basis and orbits of faces and directions (docs/conventions.md,
3
+ * "Lattice basis and orbits"). Shared by both entry points.
4
+ */
5
+
6
+ import { crystalFamily, latticeBasis } from '../lattice.js';
7
+ import { apply, inverse, multiply, type Mat3, type Vec3 } from '../linalg.js';
8
+ import { checkOrthogonal, type Operation } from '../operations.js';
9
+ import { siteTolerance } from '../siteSymmetry.js';
10
+ import type { Uvw } from '../symmetryDirections.js';
11
+ import {
12
+ operations as groupOperations,
13
+ pointGroup,
14
+ type MagneticPointGroupKey,
15
+ type SettingOptions,
16
+ } from './pointGroups.js';
17
+ import type { Hkl } from './wyckoff.js';
18
+
19
+ const IDENTITY: Mat3 = [
20
+ [1, 0, 0],
21
+ [0, 1, 0],
22
+ [0, 0, 1],
23
+ ];
24
+
25
+ /**
26
+ * The conventional basis a, b, c (columns) in the Cartesian frame of a point group: that of
27
+ * `latticeBasis`, and for the monoclinic and triclinic families a, b, c along x, y, z, as for the
28
+ * Wyckoff positions (the integer matrices do not depend on the cell angles).
29
+ */
30
+ function conventionalBasis(key: MagneticPointGroupKey): Mat3 {
31
+ const family = crystalFamily(pointGroup(key).crystalSystem);
32
+ return family === 'monoclinic' || family === 'triclinic' ? IDENTITY : latticeBasis(family);
33
+ }
34
+
35
+ /** An integer matrix from one that should be one; throws if it is not (a data error). */
36
+ function integer(m: Mat3, what: string): Mat3 {
37
+ const r = m.map((row) => row.map((v) => Math.round(v) + 0)) as unknown as Mat3;
38
+ if (!m.every((row, i) => row.every((v, j) => Math.abs(v - r[i][j]) < 1e-9))) {
39
+ throw new Error(`${what} is not an integer matrix in the lattice basis: ${JSON.stringify(m)}`);
40
+ }
41
+ return r;
42
+ }
43
+
44
+ const deepFreeze = (m: Mat3): Mat3 => Object.freeze(m.map((row) => Object.freeze([...row]))) as unknown as Mat3;
45
+
46
+ const latticeCache = new WeakMap<readonly Operation[], readonly Operation[]>();
47
+
48
+ /**
49
+ * The operations of a point group in the conventional lattice basis (a, b, c; for the trigonal
50
+ * classes the hexagonal axes), in the order and with the time reversal of `operations` for the same
51
+ * setting and convention: integer matrices W acting on the coordinates of a lattice vector [uvw],
52
+ * W = B⁻¹·R·B with the basis B of `latticeBasis` and the Cartesian matrix R. They do not depend on
53
+ * the cell angles, so they exist for every family; for the monoclinic and triclinic families a, b,
54
+ * c are taken along x, y, z, as for the Wyckoff positions. In the ITC convention they are the
55
+ * matrices of ITA's general position (6th ed., Table 3.2.3.2). Not orthogonal in general: pass
56
+ * them to no function that takes Cartesian operations (docs/conventions.md, "Lattice basis and
57
+ * orbits").
58
+ */
59
+ export function latticeOperations(key: MagneticPointGroupKey, options?: SettingOptions): readonly Operation[] {
60
+ const ops = groupOperations(key, options);
61
+ let result = latticeCache.get(ops);
62
+ if (!result) {
63
+ const b = conventionalBasis(key);
64
+ const bInv = inverse(b);
65
+ result = Object.freeze(
66
+ ops.map((op, i) =>
67
+ Object.freeze({
68
+ matrix: deepFreeze(integer(multiply(multiply(bInv, op.matrix), b), `Operation ${i} of ${key}`)),
69
+ timeReversal: op.timeReversal,
70
+ }),
71
+ ),
72
+ );
73
+ latticeCache.set(ops, result);
74
+ }
75
+ return result;
76
+ }
77
+
78
+ /** The inverse of an integer matrix of determinant ±1, exactly (the adjugate). */
79
+ function integerInverse(w: Mat3): Mat3 {
80
+ return integer(inverse(w), 'The inverse of a lattice operation');
81
+ }
82
+
83
+ /** Indices of a face or a lattice direction, checked: three integers, not all zero. */
84
+ function indicesOf(value: unknown, name: string): readonly [number, number, number] {
85
+ if (
86
+ !Array.isArray(value) ||
87
+ value.length !== 3 ||
88
+ !value.every((x) => typeof x === 'number' && Number.isSafeInteger(x)) ||
89
+ value.every((x) => x === 0)
90
+ ) {
91
+ throw new RangeError(`${name} must be three (safe) integers, not all zero: ${JSON.stringify(value)}`);
92
+ }
93
+ return [value[0] + 0, value[1] + 0, value[2] + 0];
94
+ }
95
+
96
+ /** One face of the orbit of a face: its Miller indices and the operations that map the given face onto it. */
97
+ export interface FaceOrbitEntry {
98
+ /** Miller indices (hkl) of the face, in the conventional basis (hexagonal axes: without i). */
99
+ readonly hkl: Hkl;
100
+ /** Indices into `operations` (same setting and convention) of the operations that map the given face onto this one, ascending. */
101
+ readonly operations: readonly number[];
102
+ }
103
+
104
+ /** One direction of the orbit of a lattice direction: its indices and the operations that map the given direction onto it. */
105
+ export interface DirectionOrbitEntry {
106
+ /** The lattice direction [uvw] in the conventional basis. */
107
+ readonly uvw: Uvw;
108
+ /** Indices into `operations` (same setting and convention) of the operations that map the given direction onto this one, ascending. */
109
+ readonly operations: readonly number[];
110
+ }
111
+
112
+ /**
113
+ * The orbit of a face (hkl) or of a lattice direction [uvw] under a point group, exactly, in the
114
+ * lattice basis: the distinct faces (directions), in the order in which the operations reach them
115
+ * (so in the ITC convention the given face first), each with the indices of the operations that map
116
+ * the given one onto it. A face transforms with the inverse transpose of a lattice operation,
117
+ * h' = W⁻ᵀ·h, a direction with the operation itself, u' = W·u. The number of entries is the
118
+ * multiplicity of the face (direction), and the operations of the first entry, if it is the given
119
+ * one, are its site symmetry. Time reversal plays no part. Throws a RangeError unless exactly one of
120
+ * `hkl` and `uvw` is given, as three integers not all zero (hexagonal axes: three indices, without
121
+ * i).
122
+ */
123
+ export function orbit(
124
+ key: MagneticPointGroupKey,
125
+ indices: { readonly hkl: Hkl },
126
+ options?: SettingOptions,
127
+ ): readonly FaceOrbitEntry[];
128
+ export function orbit(
129
+ key: MagneticPointGroupKey,
130
+ indices: { readonly uvw: Uvw },
131
+ options?: SettingOptions,
132
+ ): readonly DirectionOrbitEntry[];
133
+ export function orbit(
134
+ key: MagneticPointGroupKey,
135
+ indices: { readonly hkl: Hkl } | { readonly uvw: Uvw },
136
+ options?: SettingOptions,
137
+ ): readonly FaceOrbitEntry[] | readonly DirectionOrbitEntry[];
138
+ export function orbit(
139
+ key: MagneticPointGroupKey,
140
+ indices: { readonly hkl: Hkl } | { readonly uvw: Uvw },
141
+ options?: SettingOptions,
142
+ ): readonly FaceOrbitEntry[] | readonly DirectionOrbitEntry[] {
143
+ const given = (indices ?? {}) as { readonly hkl?: unknown; readonly uvw?: unknown };
144
+ const hasHkl = given.hkl !== undefined;
145
+ const hasUvw = given.uvw !== undefined;
146
+ if (hasHkl === hasUvw) throw new RangeError('orbit: give either hkl (a face) or uvw (a lattice direction)');
147
+ const field = hasHkl ? 'hkl' : 'uvw';
148
+ const v = indicesOf(hasHkl ? given.hkl : given.uvw, field);
149
+ const ws = latticeOperations(key, options).map((op) => (hasHkl ? integerInverseTranspose(op.matrix) : op.matrix));
150
+ const images: { key: string; value: [number, number, number]; operations: number[] }[] = [];
151
+ ws.forEach((w, i) => {
152
+ const image = apply(w, v as unknown as Vec3).map((x) => x + 0) as [number, number, number];
153
+ const k = image.join(',');
154
+ const entry = images.find((e) => e.key === k);
155
+ if (entry) entry.operations.push(i);
156
+ else images.push({ key: k, value: image, operations: [i] });
157
+ });
158
+ return Object.freeze(
159
+ images.map((e) => Object.freeze({ [field]: Object.freeze(e.value), operations: Object.freeze(e.operations) })),
160
+ ) as unknown as readonly FaceOrbitEntry[] | readonly DirectionOrbitEntry[];
161
+ }
162
+
163
+ const inverseTransposeCache = new WeakMap<Mat3, Mat3>();
164
+
165
+ function integerInverseTranspose(w: Mat3): Mat3 {
166
+ let r = inverseTransposeCache.get(w);
167
+ if (!r) {
168
+ const inv = integerInverse(w);
169
+ r = deepFreeze([0, 1, 2].map((i) => [0, 1, 2].map((j) => inv[j][i])) as unknown as Mat3);
170
+ inverseTransposeCache.set(w, r);
171
+ }
172
+ return r;
173
+ }
174
+
175
+ /** Options of `orbitOf`. */
176
+ export interface OrbitOptions {
177
+ /**
178
+ * Two images count as one pole if their unit vectors are closer than this (Euclidean distance).
179
+ * Default 10⁻⁶; a caller whose directions carry rounding errors passes a larger value.
180
+ */
181
+ readonly tolerance?: number;
182
+ }
183
+
184
+ /** One pole of the orbit of a direction under a list of operations. */
185
+ export interface OrbitPole {
186
+ /** The image of the given direction as a Cartesian unit vector (the first operation's image where several coincide). */
187
+ readonly direction: Vec3;
188
+ /** Indices into the given operations of those that map the direction here, ascending. */
189
+ readonly operations: readonly number[];
190
+ }
191
+
192
+ /**
193
+ * The orbit of a direction (any non-zero vector, Cartesian, e.g. a face normal) under a list of
194
+ * operations, for instance those of a distorted crystal: the images of its unit vector under each
195
+ * operation, coinciding images merged (closer than `tolerance`, default `EPSILON`, as for
196
+ * `siteSymmetry`), in the order in which the operations reach them, each with the indices of the
197
+ * operations that map the direction there. The list is taken as it is, not closed into a group.
198
+ * Time reversal is ignored. Throws a RangeError for a zero vector and for a matrix that is not
199
+ * orthogonal (Cartesian; not one of `latticeOperations`).
200
+ */
201
+ export function orbitOf(
202
+ operations: readonly { readonly matrix: Mat3 }[],
203
+ direction: Vec3,
204
+ options?: OrbitOptions,
205
+ ): readonly OrbitPole[] {
206
+ const tolerance = siteTolerance(options);
207
+ operations.forEach((op, i) => checkOrthogonal(op.matrix, `Operation ${i}`));
208
+ const n = Math.hypot(direction[0], direction[1], direction[2]);
209
+ if (!(n > 0) || !Number.isFinite(n)) throw new RangeError(`Not a direction: ${JSON.stringify(direction)}`);
210
+ const u: Vec3 = [direction[0] / n, direction[1] / n, direction[2] / n];
211
+ const poles: { direction: Vec3; operations: number[] }[] = [];
212
+ operations.forEach((op, i) => {
213
+ const w = apply(op.matrix, u);
214
+ const pole = poles.find(
215
+ (p) => Math.hypot(p.direction[0] - w[0], p.direction[1] - w[1], p.direction[2] - w[2]) < tolerance,
216
+ );
217
+ if (pole) pole.operations.push(i);
218
+ else poles.push({ direction: w, operations: [i] });
219
+ });
220
+ return Object.freeze(
221
+ poles.map((p) =>
222
+ Object.freeze({
223
+ direction: Object.freeze(p.direction) as unknown as Vec3,
224
+ operations: Object.freeze(p.operations),
225
+ }),
226
+ ),
227
+ );
228
+ }
@@ -45,13 +45,16 @@ export type PointGroupType = 'I' | 'II' | 'III';
45
45
  * setting or the convention (docs/conventions.md, "Properties").
46
46
  */
47
47
  export interface PointGroup<K extends MagneticPointGroupKey = PointGroupKey> {
48
+ /** The key (docs/conventions.md, "Keys"). */
48
49
  readonly key: K;
50
+ /** `'I'` crystal class (white group), `'II'` grey group, `'III'` black-and-white group (docs/conventions.md, "Types"). */
49
51
  readonly type: PointGroupType;
50
52
  /**
51
53
  * The crystal class of the group: the group itself for a class, otherwise the class it becomes
52
54
  * when time reversal is dropped from every operation.
53
55
  */
54
56
  readonly crystalClass: PointGroupKey;
57
+ /** The crystal system of the crystal class. */
55
58
  readonly crystalSystem: CrystalSystem;
56
59
  /** Number of operations. */
57
60
  readonly order: number;
@@ -68,6 +71,7 @@ export interface PointGroup<K extends MagneticPointGroupKey = PointGroupKey> {
68
71
  readonly laueClass: PointGroupKey;
69
72
  }
70
73
 
74
+ /** A setting of a point group: an orientation of its symmetry directions in the Cartesian frame (docs/conventions.md, "Settings"). */
71
75
  export interface Setting {
72
76
  /** 1-based; the same physical frame in both conventions. */
73
77
  readonly index: number;
@@ -86,6 +90,7 @@ export interface Setting {
86
90
  readonly isDefault: boolean;
87
91
  }
88
92
 
93
+ /** Which convention and which setting a function answers in. */
89
94
  export interface SettingOptions {
90
95
  /** Default `'itc'`. */
91
96
  readonly convention?: Convention;
@@ -93,6 +98,7 @@ export interface SettingOptions {
93
98
  readonly setting?: number;
94
99
  }
95
100
 
101
+ /** Convention, setting and form of a symbol. */
96
102
  export interface SymbolOptions extends SettingOptions {
97
103
  /**
98
104
  * `'short'` (default): short symbol of the setting, e.g. `321` (ITC) for class `32`.
@@ -475,7 +481,7 @@ export function pointGroups(): readonly PointGroup[] {
475
481
  }
476
482
 
477
483
  /**
478
- * All 122 magnetic point groups, in the order of `MAGNETIC_POINT_GROUP_KEYS`: the 32 crystal
484
+ * All 122 magnetic point groups, in the order of `POINT_GROUP_KEYS` of the magnetic API: the 32 crystal
479
485
  * classes (type I), the 32 grey groups (type II) in the same order, and the 58 black-and-white
480
486
  * groups (type III) in the order of ITC Vol. D Table 1.5.2.3.
481
487
  */
@@ -528,7 +534,7 @@ export function operations(key: MagneticPointGroupKey, options?: SettingOptions)
528
534
  * convention: ITA's selected generators in printed order, without the identity and ITA's lattice
529
535
  * translations, so the list is empty for `1`; for `mm2` settings 2 and 3, which ITA does not list,
530
536
  * those of setting 1 carried into their frame; for a magnetic group, those of its class with the
531
- * time reversal they have in the group, for a grey group followed by 1'. Birss convention:
537
+ * time reversal they have in the group, for a grey group followed by `1'`. Birss convention:
532
538
  * birss-app's generators (for `1` the identity, as in birss-app).
533
539
  */
534
540
  export function generators(key: MagneticPointGroupKey, options?: SettingOptions): readonly Operation[] {
@@ -550,7 +556,9 @@ export function symbol(key: MagneticPointGroupKey, options?: SymbolOptions): str
550
556
 
551
557
  /** A point group in a given setting. */
552
558
  export interface PointGroupInSetting<K extends MagneticPointGroupKey = PointGroupKey> {
559
+ /** The key of the point group. */
553
560
  readonly key: K;
561
+ /** The 1-based index of the setting. */
554
562
  readonly setting: number;
555
563
  }
556
564