symcrys 0.1.1 → 0.2.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 (60) hide show
  1. package/README.md +18 -5
  2. package/dist/index.d.ts +7 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +5 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/lattice.d.ts +22 -0
  7. package/dist/lattice.d.ts.map +1 -0
  8. package/dist/lattice.js +43 -0
  9. package/dist/lattice.js.map +1 -0
  10. package/dist/linalg.d.ts +32 -0
  11. package/dist/linalg.d.ts.map +1 -0
  12. package/dist/linalg.js +120 -0
  13. package/dist/linalg.js.map +1 -0
  14. package/dist/operations.d.ts +30 -0
  15. package/dist/operations.d.ts.map +1 -0
  16. package/dist/operations.js +49 -0
  17. package/dist/operations.js.map +1 -0
  18. package/dist/pointGroups/data.d.ts +40 -0
  19. package/dist/pointGroups/data.d.ts.map +1 -0
  20. package/dist/pointGroups/data.js +204 -0
  21. package/dist/pointGroups/data.js.map +1 -0
  22. package/dist/pointGroups/elements.d.ts +75 -0
  23. package/dist/pointGroups/elements.d.ts.map +1 -0
  24. package/dist/pointGroups/elements.js +181 -0
  25. package/dist/pointGroups/elements.js.map +1 -0
  26. package/dist/pointGroups/itaOrder.d.ts +22 -0
  27. package/dist/pointGroups/itaOrder.d.ts.map +1 -0
  28. package/dist/pointGroups/itaOrder.js +675 -0
  29. package/dist/pointGroups/itaOrder.js.map +1 -0
  30. package/dist/pointGroups/pointGroups.d.ts +84 -0
  31. package/dist/pointGroups/pointGroups.d.ts.map +1 -0
  32. package/dist/pointGroups/pointGroups.js +229 -0
  33. package/dist/pointGroups/pointGroups.js.map +1 -0
  34. package/dist/pointGroups/properties.d.ts +18 -0
  35. package/dist/pointGroups/properties.d.ts.map +1 -0
  36. package/dist/pointGroups/properties.js +60 -0
  37. package/dist/pointGroups/properties.js.map +1 -0
  38. package/dist/symmetryDirections.d.ts +18 -0
  39. package/dist/symmetryDirections.d.ts.map +1 -0
  40. package/dist/symmetryDirections.js +65 -0
  41. package/dist/symmetryDirections.js.map +1 -0
  42. package/dist/tolerances.d.ts +11 -0
  43. package/dist/tolerances.d.ts.map +1 -0
  44. package/dist/tolerances.js +11 -0
  45. package/dist/tolerances.js.map +1 -0
  46. package/dist/version.d.ts +1 -1
  47. package/dist/version.js +1 -1
  48. package/package.json +5 -4
  49. package/src/index.ts +41 -0
  50. package/src/lattice.ts +59 -0
  51. package/src/linalg.ts +138 -0
  52. package/src/operations.ts +63 -0
  53. package/src/pointGroups/data.ts +294 -0
  54. package/src/pointGroups/elements.ts +264 -0
  55. package/src/pointGroups/itaOrder.ts +688 -0
  56. package/src/pointGroups/pointGroups.ts +304 -0
  57. package/src/pointGroups/properties.ts +62 -0
  58. package/src/symmetryDirections.ts +72 -0
  59. package/src/tolerances.ts +12 -0
  60. package/src/version.ts +1 -1
@@ -0,0 +1,264 @@
1
+ /**
2
+ * The geometry of a point group's operations: what each operation is (rotation, mirror, …), about
3
+ * which axis and in which sense, and which symmetry elements lie along each symmetry direction of
4
+ * the full Hermann–Mauguin symbol. Structured data only; symbols such as `3⁺ x,x,x` or `2_z` are
5
+ * left to the applications (docs/conventions.md, "Symmetry directions and elements").
6
+ */
7
+
8
+ import { det, trace, type Mat3, type Vec3 } from '../linalg.js';
9
+ import { crystalFamily, latticeBasis, type CrystalFamily } from '../lattice.js';
10
+ import type { Operation } from '../operations.js';
11
+ import { LATTICE_SYMMETRY_DIRECTIONS, type Uvw } from '../symmetryDirections.js';
12
+ import {
13
+ operations,
14
+ pointGroup,
15
+ symbol,
16
+ type Convention,
17
+ type PointGroupKey,
18
+ type SettingOptions,
19
+ } from './pointGroups.js';
20
+
21
+ /** What a point operation is; a rotoinversion n̄ is the inversion times the rotation n (n = 3, 4, 6). */
22
+ export type OperationKind = 'identity' | 'inversion' | 'rotation' | 'rotoinversion' | 'mirror';
23
+
24
+ /** A symmetry direction: as a lattice direction [uvw] and as a Cartesian unit vector. */
25
+ export interface Axis {
26
+ /**
27
+ * Integer coordinates in the conventional basis. ITC convention: sign as in ITA Table 2.1.3.1;
28
+ * Birss convention, in `describeOperations`: first non-zero Cartesian component positive.
29
+ */
30
+ readonly lattice: Uvw;
31
+ /**
32
+ * Unit vector in the Cartesian frame of symcrys. Monoclinic: a, b, c taken along x, y, z, exact for
33
+ * the unique axis only (docs/conventions.md, "Symmetry directions and elements").
34
+ */
35
+ readonly cartesian: Vec3;
36
+ }
37
+
38
+ /** One point operation described geometrically. */
39
+ export interface OperationDescription {
40
+ readonly kind: OperationKind;
41
+ /**
42
+ * The n of the symbol n or n̄: 1 for the identity and the inversion, 2 for a mirror (m = 2̄),
43
+ * otherwise the order of the rotation part.
44
+ */
45
+ readonly order: number;
46
+ /** Rotation axis, rotoinversion axis or mirror normal; absent for the identity and the inversion. */
47
+ readonly axis?: Axis;
48
+ /**
49
+ * For n ≥ 3: `'+'` if the rotation part turns by +360°/n about `axis` (counter-clockwise seen
50
+ * from the tip of `axis` looking towards the origin), `'-'` if by −360°/n.
51
+ */
52
+ readonly sense?: '+' | '-';
53
+ readonly timeReversal: boolean;
54
+ }
55
+
56
+ /** The symmetry elements along one symmetry direction. */
57
+ export interface DirectionElements extends Axis {
58
+ /** Highest n of a rotation n about this direction; 1 if there is none. */
59
+ readonly rotation: number;
60
+ /** Highest n of a rotoinversion n̄ (n = 3, 4, 6) about this direction; 0 if there is none. */
61
+ readonly rotoinversion: number;
62
+ /** True if a mirror plane is perpendicular to this direction. */
63
+ readonly mirror: boolean;
64
+ }
65
+
66
+ /** One position of the full Hermann–Mauguin symbol: its symbol and its symmetry directions. */
67
+ export interface SymmetryDirection {
68
+ /** 1-based position in the full symbol. */
69
+ readonly position: number;
70
+ /** The symbol at this position of the full symbol, if the full symbol has this position. */
71
+ readonly symbol?: string;
72
+ readonly directions: readonly DirectionElements[];
73
+ }
74
+
75
+ const AXIS_TOLERANCE = 1e-6;
76
+
77
+ function normalize(v: Vec3): Vec3 {
78
+ const n = Math.hypot(v[0], v[1], v[2]);
79
+ return [v[0] / n, v[1] / n, v[2] / n];
80
+ }
81
+
82
+ const dot = (a: Vec3, b: Vec3): number => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
83
+
84
+ /**
85
+ * The basis used to turn [uvw] into a Cartesian direction. Monoclinic and triclinic have no fixed
86
+ * basis (`latticeBasis`); there the axes a, b, c are taken along x, y, z, which is exact for the
87
+ * monoclinic unique axis, the only symmetry direction that carries an element in a symcrys frame.
88
+ */
89
+ function basisOf(family: CrystalFamily): Mat3 {
90
+ if (family === 'monoclinic' || family === 'triclinic')
91
+ return [
92
+ [1, 0, 0],
93
+ [0, 1, 0],
94
+ [0, 0, 1],
95
+ ];
96
+ return latticeBasis(family);
97
+ }
98
+
99
+ function axisOf(family: CrystalFamily, uvw: Uvw): Axis {
100
+ const b = basisOf(family);
101
+ const v: Vec3 = [
102
+ b[0][0] * uvw[0] + b[0][1] * uvw[1] + b[0][2] * uvw[2],
103
+ b[1][0] * uvw[0] + b[1][1] * uvw[1] + b[1][2] * uvw[2],
104
+ b[2][0] * uvw[0] + b[2][1] * uvw[1] + b[2][2] * uvw[2],
105
+ ];
106
+ return Object.freeze({ lattice: uvw, cartesian: Object.freeze(normalize(v)) as unknown as Vec3 });
107
+ }
108
+
109
+ /**
110
+ * The symmetry directions of a family, per position of the full symbol, in the reading of a
111
+ * convention. Birss reads position 2 of the hexagonal family along y, a tertiary direction, and
112
+ * position 3 along x, a secondary one (docs/conventions.md, "Where ITC and Birss differ").
113
+ */
114
+ function positionsOf(family: CrystalFamily, c: Convention): readonly (readonly Uvw[])[] {
115
+ const p = LATTICE_SYMMETRY_DIRECTIONS[family];
116
+ return c === 'birss' && family === 'hexagonal' ? [p[0], p[2], p[1]] : p;
117
+ }
118
+
119
+ /** The proper rotation part of a matrix and its order n (1 for the identity). */
120
+ function rotationPart(m: Mat3): { r: Mat3; n: number } {
121
+ const r = det(m) > 0 ? m : (m.map((row) => row.map((v) => -v)) as unknown as Mat3);
122
+ const cos = Math.max(-1, Math.min(1, (trace(r) - 1) / 2));
123
+ const angle = Math.acos(cos);
124
+ const n = angle < 1e-6 ? 1 : Math.round((2 * Math.PI) / angle);
125
+ if (![1, 2, 3, 4, 6].includes(n)) throw new Error(`Not a crystallographic operation (rotation order ${n})`);
126
+ return { r, n };
127
+ }
128
+
129
+ /** The axis of a proper rotation of order n ≥ 2 (sign arbitrary for n = 2). */
130
+ function rotationAxis(r: Mat3, n: number): Vec3 {
131
+ if (n === 2) {
132
+ // r + 1 has rank 1; its columns are parallel to the axis.
133
+ let best: Vec3 = [0, 0, 0];
134
+ let size = 0;
135
+ for (let j = 0; j < 3; j++) {
136
+ const col: Vec3 = [r[0][j] + (j === 0 ? 1 : 0), r[1][j] + (j === 1 ? 1 : 0), r[2][j] + (j === 2 ? 1 : 0)];
137
+ const l = Math.hypot(col[0], col[1], col[2]);
138
+ if (l > size) {
139
+ size = l;
140
+ best = col;
141
+ }
142
+ }
143
+ return normalize(best);
144
+ }
145
+ return normalize(axialVector(r));
146
+ }
147
+
148
+ /** (r − rᵀ)/2 as a vector: sin θ times the unit axis for a rotation by θ. */
149
+ 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];
150
+
151
+ function findAxis(axes: readonly Axis[], u: Vec3, what: string): Axis {
152
+ const a = axes.find((x) => Math.abs(Math.abs(dot(x.cartesian, u)) - 1) < AXIS_TOLERANCE);
153
+ if (!a) throw new Error(`${what}: axis [${u.map((v) => v.toFixed(4)).join(', ')}] is not a symmetry direction`);
154
+ return a;
155
+ }
156
+
157
+ function describe(op: Operation, axes: readonly Axis[], what: string): OperationDescription {
158
+ const proper = det(op.matrix) > 0;
159
+ const { r, n } = rotationPart(op.matrix);
160
+ const timeReversal = op.timeReversal;
161
+ if (n === 1) return Object.freeze({ kind: proper ? 'identity' : 'inversion', order: 1, timeReversal });
162
+ const axis = findAxis(axes, rotationAxis(r, n), what);
163
+ const kind: OperationKind = proper ? 'rotation' : n === 2 ? 'mirror' : 'rotoinversion';
164
+ if (n === 2) return Object.freeze({ kind, order: 2, axis, timeReversal });
165
+ const sense = dot(axialVector(r), axis.cartesian) > 0 ? '+' : '-';
166
+ return Object.freeze({ kind, order: n, axis, sense, timeReversal });
167
+ }
168
+
169
+ interface Analysis {
170
+ readonly descriptions: readonly OperationDescription[];
171
+ readonly directions: readonly SymmetryDirection[];
172
+ }
173
+
174
+ const cache = new Map<string, Analysis>();
175
+
176
+ function analysis(key: PointGroupKey, options?: SettingOptions): Analysis {
177
+ const ops = operations(key, options); // checks key, convention and setting
178
+ const c = options?.convention ?? 'itc';
179
+ const setting = options?.setting ?? 0;
180
+ const cacheKey = `${key}#${setting}#${c}`;
181
+ const hit = cache.get(cacheKey);
182
+ if (hit) return hit;
183
+
184
+ const family = crystalFamily(pointGroup(key).crystalSystem);
185
+ const positions = positionsOf(family, c).map((dirs) => dirs.map((d) => axisOf(family, d)));
186
+ const all = positions.flat();
187
+ const what = `Point group ${key}`;
188
+ const descriptions = Object.freeze(ops.map((op) => describe(op, all, what)));
189
+
190
+ const tokens = family === 'triclinic' ? [] : symbol(key, { ...options, form: 'full' }).split(' ');
191
+ const directions = Object.freeze(
192
+ positions.map((axes, i) => {
193
+ const elements = axes.map((a) => {
194
+ const on = descriptions.filter((d) => d.axis === a);
195
+ const max = (kind: OperationKind, none: number) =>
196
+ on.filter((d) => d.kind === kind).reduce((m, d) => Math.max(m, d.order), none);
197
+ return Object.freeze({
198
+ ...a,
199
+ rotation: max('rotation', 1),
200
+ rotoinversion: max('rotoinversion', 0),
201
+ mirror: on.some((d) => d.kind === 'mirror'),
202
+ });
203
+ });
204
+ const position: SymmetryDirection =
205
+ tokens[i] === undefined
206
+ ? { position: i + 1, directions: Object.freeze(elements) }
207
+ : { position: i + 1, symbol: tokens[i], directions: Object.freeze(elements) };
208
+ return Object.freeze(position);
209
+ }),
210
+ );
211
+ const result = {
212
+ descriptions: c === 'birss' ? Object.freeze(descriptions.map(birssSigned)) : descriptions,
213
+ directions,
214
+ };
215
+ cache.set(cacheKey, result);
216
+ return result;
217
+ }
218
+
219
+ const flipped = new Map<Axis, Axis>();
220
+
221
+ /**
222
+ * The description with the axis signed as in birss-app (docs/conventions.md, "Birss convention and
223
+ * birss-app"): its first non-zero Cartesian component, in the order x, y, z, is positive. If the
224
+ * axis of ITA Table 2.1.3.1 points the other way, axis and sense are reversed; the rotation stays
225
+ * the same.
226
+ */
227
+ function birssSigned(d: OperationDescription): OperationDescription {
228
+ const a = d.axis;
229
+ if (!a) return d;
230
+ const first = a.cartesian.find((x) => Math.abs(x) > 1e-9) ?? 0;
231
+ if (first > 0) return d;
232
+ let axis = flipped.get(a);
233
+ if (!axis) {
234
+ axis = Object.freeze({
235
+ lattice: Object.freeze(a.lattice.map((x) => (x === 0 ? 0 : -x))) as unknown as Uvw,
236
+ cartesian: Object.freeze(a.cartesian.map((x) => (x === 0 ? 0 : -x))) as unknown as Vec3,
237
+ });
238
+ flipped.set(a, axis);
239
+ }
240
+ const sense = d.sense === '+' ? '-' : d.sense === '-' ? '+' : undefined;
241
+ return Object.freeze({ ...d, axis, ...(sense ? { sense } : {}) });
242
+ }
243
+
244
+ /**
245
+ * Each operation of the point group described geometrically, in the order of `operations` for
246
+ * the same key and options. Axes are named by the symmetry direction they lie along: in the ITC
247
+ * convention with the sign of ITA Table 2.1.3.1, in the Birss convention signed as birss-app signs
248
+ * them (first non-zero Cartesian component positive); the sign fixes what the sense refers to.
249
+ */
250
+ export function describeOperations(key: PointGroupKey, options?: SettingOptions): readonly OperationDescription[] {
251
+ return analysis(key, options).descriptions;
252
+ }
253
+
254
+ /**
255
+ * The positions of the full Hermann–Mauguin symbol in the requested convention and setting, each
256
+ * with its symmetry directions (ITA Table 2.1.3.1) and the symmetry elements along them. All
257
+ * positions of the crystal family are listed, also those the symbol of the class leaves out (then
258
+ * without `symbol`); the triclinic family has none. The directions keep the sign of Table 2.1.3.1
259
+ * in both conventions; in the Birss convention an axis of `describeOperations` may be reversed, so
260
+ * match it to a direction by |dot| = 1, not by identity.
261
+ */
262
+ export function symmetryDirections(key: PointGroupKey, options?: SettingOptions): readonly SymmetryDirection[] {
263
+ return analysis(key, options).directions;
264
+ }