react-cheminfo 0.134.0 → 0.135.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 (181) hide show
  1. package/README.md +13 -0
  2. package/lib/crystal/core/bondGrid.d.ts +26 -0
  3. package/lib/crystal/core/bondGrid.d.ts.map +1 -0
  4. package/lib/crystal/core/bondGrid.js +76 -0
  5. package/lib/crystal/core/bondGrid.js.map +1 -0
  6. package/lib/crystal/core/cellBonds.d.ts +21 -0
  7. package/lib/crystal/core/cellBonds.d.ts.map +1 -0
  8. package/lib/crystal/core/cellBonds.js +100 -0
  9. package/lib/crystal/core/cellBonds.js.map +1 -0
  10. package/lib/crystal/core/cif/element.d.ts +9 -0
  11. package/lib/crystal/core/cif/element.d.ts.map +1 -0
  12. package/lib/crystal/core/cif/element.js +41 -0
  13. package/lib/crystal/core/cif/element.js.map +1 -0
  14. package/lib/crystal/core/cif/index.d.ts +4 -0
  15. package/lib/crystal/core/cif/index.d.ts.map +1 -0
  16. package/lib/crystal/core/cif/index.js +3 -0
  17. package/lib/crystal/core/cif/index.js.map +1 -0
  18. package/lib/crystal/core/cif/number.d.ts +27 -0
  19. package/lib/crystal/core/cif/number.d.ts.map +1 -0
  20. package/lib/crystal/core/cif/number.js +57 -0
  21. package/lib/crystal/core/cif/number.js.map +1 -0
  22. package/lib/crystal/core/cif/read.d.ts +12 -0
  23. package/lib/crystal/core/cif/read.d.ts.map +1 -0
  24. package/lib/crystal/core/cif/read.js +183 -0
  25. package/lib/crystal/core/cif/read.js.map +1 -0
  26. package/lib/crystal/core/cif/readGeometry.d.ts +17 -0
  27. package/lib/crystal/core/cif/readGeometry.d.ts.map +1 -0
  28. package/lib/crystal/core/cif/readGeometry.js +93 -0
  29. package/lib/crystal/core/cif/readGeometry.js.map +1 -0
  30. package/lib/crystal/core/cif/tags.d.ts +62 -0
  31. package/lib/crystal/core/cif/tags.d.ts.map +1 -0
  32. package/lib/crystal/core/cif/tags.js +115 -0
  33. package/lib/crystal/core/cif/tags.js.map +1 -0
  34. package/lib/crystal/core/cif/types.d.ts +158 -0
  35. package/lib/crystal/core/cif/types.d.ts.map +1 -0
  36. package/lib/crystal/core/cif/types.js +2 -0
  37. package/lib/crystal/core/cif/types.js.map +1 -0
  38. package/lib/crystal/core/components.d.ts +56 -0
  39. package/lib/crystal/core/components.d.ts.map +1 -0
  40. package/lib/crystal/core/components.js +132 -0
  41. package/lib/crystal/core/components.js.map +1 -0
  42. package/lib/crystal/core/covalentRadii.d.ts +24 -0
  43. package/lib/crystal/core/covalentRadii.d.ts.map +1 -0
  44. package/lib/crystal/core/covalentRadii.js +55 -0
  45. package/lib/crystal/core/covalentRadii.js.map +1 -0
  46. package/lib/crystal/core/disorder.d.ts +59 -0
  47. package/lib/crystal/core/disorder.d.ts.map +1 -0
  48. package/lib/crystal/core/disorder.js +168 -0
  49. package/lib/crystal/core/disorder.js.map +1 -0
  50. package/lib/crystal/core/expand.d.ts +62 -0
  51. package/lib/crystal/core/expand.d.ts.map +1 -0
  52. package/lib/crystal/core/expand.js +61 -0
  53. package/lib/crystal/core/expand.js.map +1 -0
  54. package/lib/crystal/core/index.d.ts +18 -0
  55. package/lib/crystal/core/index.d.ts.map +1 -0
  56. package/lib/crystal/core/index.js +12 -0
  57. package/lib/crystal/core/index.js.map +1 -0
  58. package/lib/crystal/core/moleculeOf.d.ts +31 -0
  59. package/lib/crystal/core/moleculeOf.d.ts.map +1 -0
  60. package/lib/crystal/core/moleculeOf.js +128 -0
  61. package/lib/crystal/core/moleculeOf.js.map +1 -0
  62. package/lib/crystal/core/moleculeTypes.d.ts +80 -0
  63. package/lib/crystal/core/moleculeTypes.d.ts.map +1 -0
  64. package/lib/crystal/core/moleculeTypes.js +2 -0
  65. package/lib/crystal/core/moleculeTypes.js.map +1 -0
  66. package/lib/crystal/core/molecules.d.ts +39 -0
  67. package/lib/crystal/core/molecules.d.ts.map +1 -0
  68. package/lib/crystal/core/molecules.js +140 -0
  69. package/lib/crystal/core/molecules.js.map +1 -0
  70. package/lib/crystal/core/operations.d.ts +19 -0
  71. package/lib/crystal/core/operations.d.ts.map +1 -0
  72. package/lib/crystal/core/operations.js +68 -0
  73. package/lib/crystal/core/operations.js.map +1 -0
  74. package/lib/crystal/core/packing.d.ts +43 -0
  75. package/lib/crystal/core/packing.d.ts.map +1 -0
  76. package/lib/crystal/core/packing.js +89 -0
  77. package/lib/crystal/core/packing.js.map +1 -0
  78. package/lib/crystal/core/periodicBonds.d.ts +57 -0
  79. package/lib/crystal/core/periodicBonds.d.ts.map +1 -0
  80. package/lib/crystal/core/periodicBonds.js +167 -0
  81. package/lib/crystal/core/periodicBonds.js.map +1 -0
  82. package/lib/crystal/core/resolve.d.ts +61 -0
  83. package/lib/crystal/core/resolve.d.ts.map +1 -0
  84. package/lib/crystal/core/resolve.js +149 -0
  85. package/lib/crystal/core/resolve.js.map +1 -0
  86. package/lib/crystal/core/spaceGroups/index.d.ts +5 -0
  87. package/lib/crystal/core/spaceGroups/index.d.ts.map +1 -0
  88. package/lib/crystal/core/spaceGroups/index.js +4 -0
  89. package/lib/crystal/core/spaceGroups/index.js.map +1 -0
  90. package/lib/crystal/core/spaceGroups/spaceGroupOperations.d.ts +14 -0
  91. package/lib/crystal/core/spaceGroups/spaceGroupOperations.d.ts.map +1 -0
  92. package/lib/crystal/core/spaceGroups/spaceGroupOperations.js +24 -0
  93. package/lib/crystal/core/spaceGroups/spaceGroupOperations.js.map +1 -0
  94. package/lib/crystal/core/spaceGroups/spaceGroupRows.d.ts +3 -0
  95. package/lib/crystal/core/spaceGroups/spaceGroupRows.d.ts.map +1 -0
  96. package/lib/crystal/core/spaceGroups/spaceGroupRows.js +536 -0
  97. package/lib/crystal/core/spaceGroups/spaceGroupRows.js.map +1 -0
  98. package/lib/crystal/core/spaceGroups/spaceGroupSymbols.d.ts +43 -0
  99. package/lib/crystal/core/spaceGroups/spaceGroupSymbols.d.ts.map +1 -0
  100. package/lib/crystal/core/spaceGroups/spaceGroupSymbols.js +169 -0
  101. package/lib/crystal/core/spaceGroups/spaceGroupSymbols.js.map +1 -0
  102. package/lib/crystal/core/spaceGroups/spaceGroups.d.ts +78 -0
  103. package/lib/crystal/core/spaceGroups/spaceGroups.d.ts.map +1 -0
  104. package/lib/crystal/core/spaceGroups/spaceGroups.js +83 -0
  105. package/lib/crystal/core/spaceGroups/spaceGroups.js.map +1 -0
  106. package/lib/crystal/core/symmetry/group.d.ts +15 -0
  107. package/lib/crystal/core/symmetry/group.d.ts.map +1 -0
  108. package/lib/crystal/core/symmetry/group.js +37 -0
  109. package/lib/crystal/core/symmetry/group.js.map +1 -0
  110. package/lib/crystal/core/symmetry/index.d.ts +8 -0
  111. package/lib/crystal/core/symmetry/index.d.ts.map +1 -0
  112. package/lib/crystal/core/symmetry/index.js +6 -0
  113. package/lib/crystal/core/symmetry/index.js.map +1 -0
  114. package/lib/crystal/core/symmetry/integerMatrix.d.ts +45 -0
  115. package/lib/crystal/core/symmetry/integerMatrix.d.ts.map +1 -0
  116. package/lib/crystal/core/symmetry/integerMatrix.js +139 -0
  117. package/lib/crystal/core/symmetry/integerMatrix.js.map +1 -0
  118. package/lib/crystal/core/symmetry/lattice.d.ts +64 -0
  119. package/lib/crystal/core/symmetry/lattice.d.ts.map +1 -0
  120. package/lib/crystal/core/symmetry/lattice.js +113 -0
  121. package/lib/crystal/core/symmetry/lattice.js.map +1 -0
  122. package/lib/crystal/core/symmetry/operation.d.ts +91 -0
  123. package/lib/crystal/core/symmetry/operation.d.ts.map +1 -0
  124. package/lib/crystal/core/symmetry/operation.js +184 -0
  125. package/lib/crystal/core/symmetry/operation.js.map +1 -0
  126. package/lib/crystal/core/symmetry/parseOperation.d.ts +18 -0
  127. package/lib/crystal/core/symmetry/parseOperation.d.ts.map +1 -0
  128. package/lib/crystal/core/symmetry/parseOperation.js +145 -0
  129. package/lib/crystal/core/symmetry/parseOperation.js.map +1 -0
  130. package/lib/crystal/core/symmetry/types.d.ts +29 -0
  131. package/lib/crystal/core/symmetry/types.d.ts.map +1 -0
  132. package/lib/crystal/core/symmetry/types.js +5 -0
  133. package/lib/crystal/core/symmetry/types.js.map +1 -0
  134. package/lib/crystal/core/symmetryClasses.d.ts +28 -0
  135. package/lib/crystal/core/symmetryClasses.d.ts.map +1 -0
  136. package/lib/crystal/core/symmetryClasses.js +93 -0
  137. package/lib/crystal/core/symmetryClasses.js.map +1 -0
  138. package/lib/crystal.d.ts +12 -0
  139. package/lib/crystal.d.ts.map +1 -0
  140. package/lib/crystal.js +12 -0
  141. package/lib/crystal.js.map +1 -0
  142. package/lib/server/spaSite.d.ts.map +1 -1
  143. package/lib/server/spaSite.js +9 -0
  144. package/lib/server/spaSite.js.map +1 -1
  145. package/package.json +3 -1
  146. package/src/crystal/core/bondGrid.ts +98 -0
  147. package/src/crystal/core/cellBonds.ts +121 -0
  148. package/src/crystal/core/cif/element.ts +40 -0
  149. package/src/crystal/core/cif/index.ts +12 -0
  150. package/src/crystal/core/cif/number.ts +62 -0
  151. package/src/crystal/core/cif/read.ts +218 -0
  152. package/src/crystal/core/cif/readGeometry.ts +104 -0
  153. package/src/crystal/core/cif/tags.ts +143 -0
  154. package/src/crystal/core/cif/types.ts +164 -0
  155. package/src/crystal/core/components.ts +183 -0
  156. package/src/crystal/core/covalentRadii.ts +56 -0
  157. package/src/crystal/core/disorder.ts +190 -0
  158. package/src/crystal/core/expand.ts +105 -0
  159. package/src/crystal/core/index.ts +44 -0
  160. package/src/crystal/core/moleculeOf.ts +169 -0
  161. package/src/crystal/core/moleculeTypes.ts +85 -0
  162. package/src/crystal/core/molecules.ts +165 -0
  163. package/src/crystal/core/operations.ts +79 -0
  164. package/src/crystal/core/packing.ts +116 -0
  165. package/src/crystal/core/periodicBonds.ts +234 -0
  166. package/src/crystal/core/resolve.ts +187 -0
  167. package/src/crystal/core/spaceGroups/index.ts +16 -0
  168. package/src/crystal/core/spaceGroups/spaceGroupOperations.ts +32 -0
  169. package/src/crystal/core/spaceGroups/spaceGroupRows.ts +536 -0
  170. package/src/crystal/core/spaceGroups/spaceGroupSymbols.ts +179 -0
  171. package/src/crystal/core/spaceGroups/spaceGroups.ts +154 -0
  172. package/src/crystal/core/symmetry/group.ts +42 -0
  173. package/src/crystal/core/symmetry/index.ts +23 -0
  174. package/src/crystal/core/symmetry/integerMatrix.ts +153 -0
  175. package/src/crystal/core/symmetry/lattice.ts +152 -0
  176. package/src/crystal/core/symmetry/operation.ts +223 -0
  177. package/src/crystal/core/symmetry/parseOperation.ts +180 -0
  178. package/src/crystal/core/symmetry/types.ts +32 -0
  179. package/src/crystal/core/symmetryClasses.ts +105 -0
  180. package/src/crystal.ts +12 -0
  181. package/src/server/spaSite.ts +9 -0
@@ -0,0 +1,234 @@
1
+ /**
2
+ * The bonds of one cell, found across its faces.
3
+ *
4
+ * The atoms are binned on a grid of the cell itself, each bin at least one
5
+ * bond long across, so an atom is compared only with the atoms of its own bin
6
+ * and of the bins around it — wrapped across the faces with the cell they lie
7
+ * in — rather than with every atom of 27 cells. A cell a bond is longer than
8
+ * is searched over as many cells as the bond can reach.
9
+ */
10
+
11
+ import { createGrid } from './bondGrid.ts';
12
+ import type { Lattice } from './symmetry/lattice.ts';
13
+
14
+ /**
15
+ * The longest bond between two elements, in ångström; 0 for a pair never
16
+ * bonded.
17
+ */
18
+ export type BondLimit = (a: string, b: string) => number;
19
+
20
+ /** One atom of a cell, in fractional coordinates. */
21
+ export interface PackedAtom {
22
+ readonly element: string;
23
+ readonly position: readonly [number, number, number];
24
+ }
25
+
26
+ /** The bonded pairs of one cell, each listed once. */
27
+ export interface PeriodicPairs {
28
+ /** How many pairs. */
29
+ readonly count: number;
30
+ /** The first atom of each pair. */
31
+ readonly from: Int32Array;
32
+ /** The second atom of each pair; never lower than `from`. */
33
+ readonly to: Int32Array;
34
+ /** The cell `to` is bonded in, seen from `from`: three integers per pair. */
35
+ readonly shifts: Int32Array;
36
+ /** The length of each bond, Å. */
37
+ readonly lengths: Float64Array;
38
+ /**
39
+ * The pairs of atoms closer than `samePlace`, which are one atom written
40
+ * twice and are never bonded: `[i, j, i, j, …]`, each `i` below its `j`.
41
+ */
42
+ readonly coincident: Int32Array;
43
+ }
44
+
45
+ /** Options of {@link findPeriodicBonds}. */
46
+ export interface PeriodicBondOptions {
47
+ /**
48
+ * Two atoms closer than this, in ångström, are one atom written twice.
49
+ * @default 1e-4
50
+ */
51
+ readonly samePlace?: number;
52
+ }
53
+
54
+ /**
55
+ * Every bond of one cell, each from an atom of the cell to an atom of it or of
56
+ * a cell around it.
57
+ * @param atoms - The atoms of one cell, fractional, wrapped into [0, 1).
58
+ * @param lattice - The cell they sit in.
59
+ * @param bondLimit - When two atoms are bonded.
60
+ * @param options - Options.
61
+ * @returns The bonded pairs, and the atoms written twice.
62
+ */
63
+ export function findPeriodicBonds(
64
+ atoms: readonly PackedAtom[],
65
+ lattice: Lattice,
66
+ bondLimit: BondLimit,
67
+ options: PeriodicBondOptions = {},
68
+ ): PeriodicPairs {
69
+ const { samePlace = 1e-4 } = options;
70
+ const count = atoms.length;
71
+ const kinds = elementKinds(atoms);
72
+ const limits = pairLimits(kinds.symbols, bondLimit);
73
+ let reach = samePlace;
74
+ for (let index = 0; index < limits.length; index++) {
75
+ reach = Math.max(reach, limits[index] ?? 0);
76
+ }
77
+ const xyz = cartesianOf(atoms, lattice);
78
+ const grid = createGrid(atoms, lattice, reach);
79
+ const found = new PairBuffer();
80
+ const reachSquared = reach * reach;
81
+ const sameSquared = samePlace * samePlace;
82
+ const kindCount = kinds.symbols.length;
83
+ const { counts, reaches, start, members, cells } = grid;
84
+ const [na, nb, nc] = counts;
85
+ const [ka, kb, kc] = reaches;
86
+ const m = lattice.cartesian;
87
+
88
+ for (let i = 0; i < count; i++) {
89
+ const xi = xyz[i * 3] ?? 0;
90
+ const yi = xyz[i * 3 + 1] ?? 0;
91
+ const zi = xyz[i * 3 + 2] ?? 0;
92
+ const row = (kinds.indices[i] ?? 0) * kindCount;
93
+ for (let oa = -ka; oa <= ka; oa++) {
94
+ const ta = (cells[i * 3] ?? 0) + oa;
95
+ const sa = Math.floor(ta / na);
96
+ for (let ob = -kb; ob <= kb; ob++) {
97
+ const tb = (cells[i * 3 + 1] ?? 0) + ob;
98
+ const sb = Math.floor(tb / nb);
99
+ for (let oc = -kc; oc <= kc; oc++) {
100
+ const tc = (cells[i * 3 + 2] ?? 0) + oc;
101
+ const sc = Math.floor(tc / nc);
102
+ const positive =
103
+ sa > 0 || (sa === 0 && (sb > 0 || (sb === 0 && sc > 0)));
104
+ const bin =
105
+ ((ta - sa * na) * nb + (tb - sb * nb)) * nc + (tc - sc * nc);
106
+ const ox = shiftAxis(m, 0, sa, sb, sc) - xi;
107
+ const oy = shiftAxis(m, 1, sa, sb, sc) - yi;
108
+ const oz = shiftAxis(m, 2, sa, sb, sc) - zi;
109
+ const last = start[bin + 1] ?? 0;
110
+ for (let slot = start[bin] ?? 0; slot < last; slot++) {
111
+ const j = members[slot] ?? 0;
112
+ if (j < i || (j === i && !positive)) continue;
113
+ const dx = (xyz[j * 3] ?? 0) + ox;
114
+ const dy = (xyz[j * 3 + 1] ?? 0) + oy;
115
+ const dz = (xyz[j * 3 + 2] ?? 0) + oz;
116
+ const squared = dx * dx + dy * dy + dz * dz;
117
+ if (squared > reachSquared) continue;
118
+ if (squared < sameSquared) {
119
+ if (j !== i) found.coincide(i, j);
120
+ continue;
121
+ }
122
+ const limit = limits[row + (kinds.indices[j] ?? 0)] ?? 0;
123
+ if (squared > limit * limit) continue;
124
+ found.bond(i, j, sa, sb, sc, Math.sqrt(squared));
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ return found.result();
131
+ }
132
+
133
+ function shiftAxis(
134
+ m: number[][],
135
+ axis: number,
136
+ sa: number,
137
+ sb: number,
138
+ sc: number,
139
+ ): number {
140
+ const row = m[axis];
141
+ return (row?.[0] ?? 0) * sa + (row?.[1] ?? 0) * sb + (row?.[2] ?? 0) * sc;
142
+ }
143
+
144
+ function cartesianOf(atoms: readonly PackedAtom[], lattice: Lattice) {
145
+ const m = lattice.cartesian;
146
+ const xyz = new Float64Array(atoms.length * 3);
147
+ for (let index = 0; index < atoms.length; index++) {
148
+ const [x, y, z] = (atoms[index] as PackedAtom).position;
149
+ for (let axis = 0; axis < 3; axis++) {
150
+ const row = m[axis];
151
+ xyz[index * 3 + axis] =
152
+ (row?.[0] ?? 0) * x + (row?.[1] ?? 0) * y + (row?.[2] ?? 0) * z;
153
+ }
154
+ }
155
+ return xyz;
156
+ }
157
+
158
+ /**
159
+ * The elements present, and which of them each atom is.
160
+ * @param atoms - The atoms of the cell.
161
+ * @returns The index of each atom’s element, and the elements.
162
+ */
163
+ function elementKinds(atoms: readonly PackedAtom[]) {
164
+ const indices = new Int32Array(atoms.length);
165
+ const symbols: string[] = [];
166
+ const known = new Map<string, number>();
167
+ for (let index = 0; index < atoms.length; index++) {
168
+ const element = (atoms[index] as PackedAtom).element;
169
+ let kind = known.get(element);
170
+ if (kind === undefined) {
171
+ kind = symbols.length;
172
+ known.set(element, kind);
173
+ symbols.push(element);
174
+ }
175
+ indices[index] = kind;
176
+ }
177
+ return { indices, symbols };
178
+ }
179
+
180
+ /**
181
+ * The bond limit of every pair of the elements present, row-major.
182
+ * @param symbols - The elements present.
183
+ * @param bondLimit - When two atoms are bonded.
184
+ * @returns The limits, row-major over the elements present.
185
+ */
186
+ function pairLimits(symbols: readonly string[], bondLimit: BondLimit) {
187
+ const size = symbols.length;
188
+ const limits = new Float64Array(size * size);
189
+ for (let a = 0; a < size; a++) {
190
+ for (let b = 0; b < size; b++) {
191
+ const limit = bondLimit(symbols[a] ?? '', symbols[b] ?? '');
192
+ limits[a * size + b] = Math.max(limit, 0);
193
+ }
194
+ }
195
+ return limits;
196
+ }
197
+
198
+ /** The pairs as they are found, turned into typed arrays once at the end. */
199
+ class PairBuffer {
200
+ readonly #from: number[] = [];
201
+ readonly #to: number[] = [];
202
+ readonly #shifts: number[] = [];
203
+ readonly #lengths: number[] = [];
204
+ readonly #coincident: number[] = [];
205
+
206
+ bond(
207
+ i: number,
208
+ j: number,
209
+ sa: number,
210
+ sb: number,
211
+ sc: number,
212
+ length: number,
213
+ ) {
214
+ this.#from.push(i);
215
+ this.#to.push(j);
216
+ this.#shifts.push(sa, sb, sc);
217
+ this.#lengths.push(length);
218
+ }
219
+
220
+ coincide(i: number, j: number) {
221
+ this.#coincident.push(i, j);
222
+ }
223
+
224
+ result(): PeriodicPairs {
225
+ return {
226
+ count: this.#from.length,
227
+ from: Int32Array.from(this.#from),
228
+ to: Int32Array.from(this.#to),
229
+ shifts: Int32Array.from(this.#shifts),
230
+ lengths: Float64Array.from(this.#lengths),
231
+ coincident: Int32Array.from(this.#coincident),
232
+ };
233
+ }
234
+ }
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Which setting a file is written in.
3
+ *
4
+ * A CIF names its group up to four ways and they do not always agree, so the
5
+ * order matters: the Hall symbol pins a setting on its own, a Hermann–Mauguin
6
+ * symbol usually does, a number alone never does. The operations in the file
7
+ * are then checked against whatever won, because a file whose symbol and
8
+ * operations disagree is common and the operations are what the structure
9
+ * actually has.
10
+ */
11
+
12
+ import type { CrystalStructure } from './cif/types.ts';
13
+ import { spaceGroupOperations } from './spaceGroups/spaceGroupOperations.ts';
14
+ import { resolveSpaceGroups } from './spaceGroups/spaceGroupSymbols.ts';
15
+ import type { SpaceGroupSetting } from './spaceGroups/spaceGroups.ts';
16
+ import { SPACE_GROUP_SETTINGS } from './spaceGroups/spaceGroups.ts';
17
+ import { operationKey } from './symmetry/operation.ts';
18
+ import { parseOperation } from './symmetry/parseOperation.ts';
19
+
20
+ /** What the file says its group is, and whether it says it twice the same way. */
21
+ export interface ResolvedSetting {
22
+ /** The setting to build the cell in, or `null` when the file names none. */
23
+ readonly setting: SpaceGroupSetting | null;
24
+ /** Which field decided it. */
25
+ readonly from: 'hall' | 'symbol' | 'number' | 'operations' | null;
26
+ /** Every setting the file's symbol matches, `setting` first. */
27
+ readonly candidates: readonly SpaceGroupSetting[];
28
+ /**
29
+ * The setting the file's own operation list is. Searched only when the symbol
30
+ * decided nothing or disagreed with it, because the search reads every one of
31
+ * the 521 settings.
32
+ */
33
+ readonly fromOperations: SpaceGroupSetting | null;
34
+ /**
35
+ * Whether every operation the file lists belongs to `setting`. `null` when
36
+ * the file lists none, which is the usual case for a published structure.
37
+ */
38
+ readonly agrees: boolean | null;
39
+ }
40
+
41
+ /**
42
+ * Read a structure's space group off the file.
43
+ * @param structure - What `readCif` returned.
44
+ * @returns The setting and how it was decided; `setting` is `null` only when
45
+ * the file names no group the catalogue recognises.
46
+ */
47
+ export function resolveStructureSetting(
48
+ structure: CrystalStructure,
49
+ ): ResolvedSetting {
50
+ const { hall, hm, number } = structure.spaceGroup;
51
+ const byHall = hall === null ? [] : resolveSpaceGroups(hall);
52
+ const bySymbol =
53
+ hm === null ? [] : withinNumber(resolveSpaceGroups(hm), number);
54
+ const named = byHall.length > 0 ? byHall : bySymbol;
55
+ const from = byHall.length > 0 ? 'hall' : 'symbol';
56
+
57
+ if (named.length > 0) {
58
+ const setting = named[0] ?? null;
59
+ const agrees =
60
+ setting === null ? null : operationsBelong(structure.symopsXyz, setting);
61
+ return {
62
+ setting,
63
+ from,
64
+ candidates: named,
65
+ // Only worth the scan when the file contradicts its own symbol.
66
+ fromOperations:
67
+ agrees === false ? settingOfOperations(structure.symopsXyz) : null,
68
+ agrees,
69
+ };
70
+ }
71
+
72
+ const fromOperations = settingOfOperations(structure.symopsXyz);
73
+ if (fromOperations !== null) {
74
+ return {
75
+ setting: fromOperations,
76
+ from: 'operations',
77
+ candidates: [fromOperations],
78
+ fromOperations,
79
+ agrees: true,
80
+ };
81
+ }
82
+
83
+ const byNumber = number === null ? [] : resolveSpaceGroups(number);
84
+ const setting = byNumber[0] ?? null;
85
+ return {
86
+ setting,
87
+ from: setting === null ? null : 'number',
88
+ candidates: byNumber,
89
+ fromOperations: null,
90
+ agrees:
91
+ setting === null ? null : operationsBelong(structure.symopsXyz, setting),
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Whether every operation the file lists is an operation of the setting.
97
+ *
98
+ * A file often lists one coset of a centred group and leaves the centring to
99
+ * the symbol, so the test is containment rather than equality: a shorter list
100
+ * is not a disagreement, an operation outside the group is.
101
+ * @param symopsXyz - The operation triplets the file carries, verbatim.
102
+ * @param setting - The setting they are checked against.
103
+ * @returns `null` when the file lists no operation; `false` when one of them is
104
+ * not the setting's, a triplet nothing can parse included.
105
+ */
106
+ export function operationsBelong(
107
+ symopsXyz: readonly string[],
108
+ setting: SpaceGroupSetting,
109
+ ): boolean | null {
110
+ if (symopsXyz.length === 0) return null;
111
+ const keys = operationKeys(setting);
112
+ for (const triplet of symopsXyz) {
113
+ const key = keyOf(triplet);
114
+ if (key === null || !keys.has(key)) return false;
115
+ }
116
+ return true;
117
+ }
118
+
119
+ /**
120
+ * The one setting whose coset list is exactly the file's, or `null`.
121
+ *
122
+ * The last resort — a file with operations and no readable symbol — and what
123
+ * names the group when a file contradicts its own symbol.
124
+ * @param symopsXyz - The operation triplets the file carries.
125
+ * @returns The setting, or `null` when none matches exactly.
126
+ */
127
+ export function settingOfOperations(
128
+ symopsXyz: readonly string[],
129
+ ): SpaceGroupSetting | null {
130
+ if (symopsXyz.length === 0) return null;
131
+ const wanted = new Set<string>();
132
+ for (const triplet of symopsXyz) {
133
+ const key = keyOf(triplet);
134
+ if (key === null) return null;
135
+ wanted.add(key);
136
+ }
137
+ for (const setting of SPACE_GROUP_SETTINGS) {
138
+ if (setting.multiplicity !== wanted.size) continue;
139
+ let same = true;
140
+ for (const key of operationKeys(setting)) {
141
+ if (!wanted.has(key)) {
142
+ same = false;
143
+ break;
144
+ }
145
+ }
146
+ if (same) return setting;
147
+ }
148
+ return null;
149
+ }
150
+
151
+ /**
152
+ * A symbol that matches several numbers is cut down by the number the file also
153
+ * gives, so `Fd-3m` plus `227` never drifts onto another group's spelling. A
154
+ * number that agrees with nothing is ignored rather than obeyed.
155
+ * @param settings - The settings the symbol matches.
156
+ * @param number - The number the file gives, or `null`.
157
+ * @returns The settings of that number, or all of them when none is.
158
+ */
159
+ function withinNumber(
160
+ settings: readonly SpaceGroupSetting[],
161
+ number: number | null,
162
+ ): readonly SpaceGroupSetting[] {
163
+ if (number === null) return settings;
164
+ const inside = settings.filter((setting) => setting.number === number);
165
+ return inside.length > 0 ? inside : settings;
166
+ }
167
+
168
+ const KEYS = new WeakMap<SpaceGroupSetting, ReadonlySet<string>>();
169
+
170
+ function operationKeys(setting: SpaceGroupSetting): ReadonlySet<string> {
171
+ const known = KEYS.get(setting);
172
+ if (known !== undefined) return known;
173
+ const keys = new Set<string>();
174
+ for (const operation of spaceGroupOperations(setting)) {
175
+ keys.add(operationKey(operation));
176
+ }
177
+ KEYS.set(setting, keys);
178
+ return keys;
179
+ }
180
+
181
+ function keyOf(triplet: string): string | null {
182
+ try {
183
+ return operationKey(parseOperation(triplet, 3));
184
+ } catch {
185
+ return null;
186
+ }
187
+ }
@@ -0,0 +1,16 @@
1
+ export type {
2
+ Centring,
3
+ CrystalSystem,
4
+ SpaceGroupSetting,
5
+ } from './spaceGroups.ts';
6
+ export {
7
+ SPACE_GROUP_SETTINGS,
8
+ SPACE_GROUP_SETTINGS_BY_NUMBER,
9
+ spaceGroup,
10
+ } from './spaceGroups.ts';
11
+ export { spaceGroupOperations } from './spaceGroupOperations.ts';
12
+ export {
13
+ normalizeSpaceGroupSymbol,
14
+ resolveSpaceGroup,
15
+ resolveSpaceGroups,
16
+ } from './spaceGroupSymbols.ts';
@@ -0,0 +1,32 @@
1
+ import { parseOperation } from '../symmetry/parseOperation.ts';
2
+ import type { CrystalOperation } from '../symmetry/types.ts';
3
+
4
+ import type { SpaceGroupSetting } from './spaceGroups.ts';
5
+
6
+ const cache = new WeakMap<
7
+ SpaceGroupSetting,
8
+ ReadonlyArray<CrystalOperation<3>>
9
+ >();
10
+
11
+ /**
12
+ * The exact operations of one setting: its coset list with the centring
13
+ * expanded, parsed from the canonical triplets the data module carries.
14
+ *
15
+ * Parsing all 7244 operations of the 521 settings takes long enough to be worth
16
+ * not doing on a page that shows one group, so a setting is parsed the first
17
+ * time it is asked for and the result is kept for as long as the setting is.
18
+ * @param setting - One setting of the catalogue.
19
+ * @returns The operations, parsed once per setting.
20
+ */
21
+ export function spaceGroupOperations(
22
+ setting: SpaceGroupSetting,
23
+ ): ReadonlyArray<CrystalOperation<3>> {
24
+ const known = cache.get(setting);
25
+ if (known !== undefined) return known;
26
+ const operations: Array<CrystalOperation<3>> = [];
27
+ for (const triplet of setting.operations) {
28
+ operations.push(parseOperation(triplet, 3));
29
+ }
30
+ cache.set(setting, operations);
31
+ return operations;
32
+ }