react-cheminfo 0.134.1 → 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 (177) 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/package.json +3 -1
  143. package/src/crystal/core/bondGrid.ts +98 -0
  144. package/src/crystal/core/cellBonds.ts +121 -0
  145. package/src/crystal/core/cif/element.ts +40 -0
  146. package/src/crystal/core/cif/index.ts +12 -0
  147. package/src/crystal/core/cif/number.ts +62 -0
  148. package/src/crystal/core/cif/read.ts +218 -0
  149. package/src/crystal/core/cif/readGeometry.ts +104 -0
  150. package/src/crystal/core/cif/tags.ts +143 -0
  151. package/src/crystal/core/cif/types.ts +164 -0
  152. package/src/crystal/core/components.ts +183 -0
  153. package/src/crystal/core/covalentRadii.ts +56 -0
  154. package/src/crystal/core/disorder.ts +190 -0
  155. package/src/crystal/core/expand.ts +105 -0
  156. package/src/crystal/core/index.ts +44 -0
  157. package/src/crystal/core/moleculeOf.ts +169 -0
  158. package/src/crystal/core/moleculeTypes.ts +85 -0
  159. package/src/crystal/core/molecules.ts +165 -0
  160. package/src/crystal/core/operations.ts +79 -0
  161. package/src/crystal/core/packing.ts +116 -0
  162. package/src/crystal/core/periodicBonds.ts +234 -0
  163. package/src/crystal/core/resolve.ts +187 -0
  164. package/src/crystal/core/spaceGroups/index.ts +16 -0
  165. package/src/crystal/core/spaceGroups/spaceGroupOperations.ts +32 -0
  166. package/src/crystal/core/spaceGroups/spaceGroupRows.ts +536 -0
  167. package/src/crystal/core/spaceGroups/spaceGroupSymbols.ts +179 -0
  168. package/src/crystal/core/spaceGroups/spaceGroups.ts +154 -0
  169. package/src/crystal/core/symmetry/group.ts +42 -0
  170. package/src/crystal/core/symmetry/index.ts +23 -0
  171. package/src/crystal/core/symmetry/integerMatrix.ts +153 -0
  172. package/src/crystal/core/symmetry/lattice.ts +152 -0
  173. package/src/crystal/core/symmetry/operation.ts +223 -0
  174. package/src/crystal/core/symmetry/parseOperation.ts +180 -0
  175. package/src/crystal/core/symmetry/types.ts +32 -0
  176. package/src/crystal/core/symmetryClasses.ts +105 -0
  177. package/src/crystal.ts +12 -0
@@ -0,0 +1,145 @@
1
+ import { matrixDeterminant } from "./integerMatrix.js";
2
+ import { AXIS_NAMES, TWELFTHS_PER_CELL } from "./types.js";
3
+ /** How far a decimal translation may sit from a twelfth and still be snapped to it. */
4
+ const DECIMAL_TOLERANCE = 5e-3;
5
+ /**
6
+ * Parse one coordinate triplet — `x,y,z`, `-x+1/2,-y,z+1/2`, `-x+y+2/3,-x+1/3,z+1/3`
7
+ * — into an exact operation.
8
+ *
9
+ * Accepts what the International Tables print and what a CIF may carry: any
10
+ * ordering of the terms (`1/2-x` as well as `-x+1/2`), a leading `+`, upper or
11
+ * lower case, decimal translations (`0.5+x`, `0.33333+y`), a translation of a
12
+ * whole cell, and whitespace anywhere. A two-dimensional operation is `x,y`.
13
+ * @param xyz - The triplet, with or without surrounding whitespace.
14
+ * @param dimension - 2 for a plane group, 3 for a space group.
15
+ * @throws {SyntaxError | RangeError} When the triplet has the wrong number of components, carries a
16
+ * variable coefficient outside {−1, 0, +1}, names an axis the dimension does
17
+ * not have, has a translation that is not a multiple of 1/12, or is singular.
18
+ * @returns The exact operation.
19
+ */
20
+ export function parseOperation(xyz, dimension) {
21
+ const components = xyz.split(',');
22
+ if (components.length !== dimension) {
23
+ throw new SyntaxError(`expected ${dimension} components in "${xyz.trim()}", received ${components.length}`);
24
+ }
25
+ const rotation = [];
26
+ const translation = [];
27
+ for (let index = 0; index < dimension; index++) {
28
+ const component = parseComponent(components[index] ?? '', dimension, xyz);
29
+ rotation.push(component.row);
30
+ translation.push(component.translation);
31
+ }
32
+ const determinant = matrixDeterminant(rotation);
33
+ if (determinant !== 1 && determinant !== -1) {
34
+ throw new RangeError(`"${xyz.trim()}" is not a symmetry operation: its determinant is ${determinant}, not ±1`);
35
+ }
36
+ return { dimension, rotation, translation };
37
+ }
38
+ /**
39
+ * One component of the triplet: a row of the matrix and one translation.
40
+ * @param source - The component, e.g. `-x+1/2`.
41
+ * @param dimension - 2 or 3.
42
+ * @param xyz - The whole triplet, for the error message.
43
+ * @returns One row of the matrix and the translation.
44
+ */
45
+ function parseComponent(source, dimension, xyz) {
46
+ const text = source.replaceAll(/\s+/g, '').toLowerCase();
47
+ if (text.length === 0) {
48
+ throw new SyntaxError(`empty component in "${xyz.trim()}"`);
49
+ }
50
+ const row = new Array(dimension).fill(0);
51
+ let twelfths = 0;
52
+ let cursor = 0;
53
+ while (cursor < text.length) {
54
+ let sign = 1;
55
+ const character = text[cursor];
56
+ if (character === '+') {
57
+ cursor += 1;
58
+ }
59
+ else if (character === '-') {
60
+ sign = -1;
61
+ cursor += 1;
62
+ }
63
+ else if (cursor > 0) {
64
+ throw new SyntaxError(`missing a sign before "${text.slice(cursor)}" in "${xyz.trim()}"`);
65
+ }
66
+ const number = readNumber(text, cursor, xyz);
67
+ cursor = number.cursor;
68
+ if (text[cursor] === '*')
69
+ cursor += 1;
70
+ const axis = AXIS_NAMES.indexOf((text[cursor] ?? ''));
71
+ if (axis === -1) {
72
+ if (number.value === null) {
73
+ throw new SyntaxError(`"${text}" of "${xyz.trim()}" has a term with nothing in it`);
74
+ }
75
+ twelfths += sign * toTwelfths(number.value, xyz);
76
+ continue;
77
+ }
78
+ if (axis >= dimension) {
79
+ throw new SyntaxError(`"${xyz.trim()}" names the axis ${AXIS_NAMES[axis]} in ${dimension}D`);
80
+ }
81
+ const coefficient = number.value ?? 1;
82
+ if (coefficient !== 1) {
83
+ throw new RangeError(`"${xyz.trim()}" carries the coefficient ${sign * coefficient} on ${AXIS_NAMES[axis]}`);
84
+ }
85
+ const current = row[axis] ?? 0;
86
+ const updated = current + sign;
87
+ if (Math.abs(updated) > 1) {
88
+ throw new RangeError(`"${xyz.trim()}" carries the coefficient ${updated} on ${AXIS_NAMES[axis]}`);
89
+ }
90
+ row[axis] = updated;
91
+ cursor += 1;
92
+ }
93
+ return {
94
+ row,
95
+ translation: ((twelfths % TWELFTHS_PER_CELL) + TWELFTHS_PER_CELL) % TWELFTHS_PER_CELL,
96
+ };
97
+ }
98
+ /**
99
+ * A fraction, a decimal or an integer at `cursor`, or `null` when none is there.
100
+ * @param text - The component, whitespace removed.
101
+ * @param cursor - Where to read.
102
+ * @param xyz - The whole triplet, for the error message.
103
+ * @returns The number, or `null` when none is there, and where reading stopped.
104
+ */
105
+ function readNumber(text, cursor, xyz) {
106
+ let end = cursor;
107
+ while (end < text.length && (isDigit(text[end]) || text[end] === '.')) {
108
+ end += 1;
109
+ }
110
+ if (end === cursor)
111
+ return { value: null, cursor };
112
+ const numerator = Number(text.slice(cursor, end));
113
+ if (!Number.isFinite(numerator)) {
114
+ throw new SyntaxError(`"${text.slice(cursor, end)}" is not a number, in "${xyz.trim()}"`);
115
+ }
116
+ if (text[end] !== '/')
117
+ return { value: numerator, cursor: end };
118
+ let denominatorEnd = end + 1;
119
+ while (denominatorEnd < text.length && isDigit(text[denominatorEnd])) {
120
+ denominatorEnd += 1;
121
+ }
122
+ const denominator = Number(text.slice(end + 1, denominatorEnd));
123
+ if (denominatorEnd === end + 1 || denominator === 0) {
124
+ throw new SyntaxError(`"${text}" of "${xyz.trim()}" has an unusable denominator`);
125
+ }
126
+ return { value: numerator / denominator, cursor: denominatorEnd };
127
+ }
128
+ /**
129
+ * A translation in twelfths, snapping `0.33333` to 4 and refusing `0.4`.
130
+ * @param value - A translation, in cells.
131
+ * @param xyz - The whole triplet, for the error message.
132
+ * @returns The translation, in twelfths.
133
+ */
134
+ function toTwelfths(value, xyz) {
135
+ const scaled = value * TWELFTHS_PER_CELL;
136
+ const twelfths = Math.round(scaled);
137
+ if (Math.abs(scaled - twelfths) > DECIMAL_TOLERANCE) {
138
+ throw new RangeError(`the translation ${value} of "${xyz.trim()}" is not a multiple of 1/12`);
139
+ }
140
+ return twelfths;
141
+ }
142
+ function isDigit(character) {
143
+ return character !== undefined && character >= '0' && character <= '9';
144
+ }
145
+ //# sourceMappingURL=parseOperation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseOperation.js","sourceRoot":"","sources":["../../../../src/crystal/core/symmetry/parseOperation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE3D,uFAAuF;AACvF,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAE/B;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,cAAc,CAC5B,GAAW,EACX,SAAY;IAEZ,MAAM,UAAU,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,IAAI,UAAU,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QACpC,MAAM,IAAI,WAAW,CACnB,YAAY,SAAS,mBAAmB,GAAG,CAAC,IAAI,EAAE,eAAe,UAAU,CAAC,MAAM,EAAE,CACrF,CAAC;IACJ,CAAC;IACD,MAAM,QAAQ,GAAe,EAAE,CAAC;IAChC,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,MAAM,SAAS,GAAG,cAAc,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;QAC1E,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CA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the
3
+ * space groups.
4
+ */
5
+ export type Dimension = 2 | 3;
6
+ /**
7
+ * A symmetry operation of a crystallographic group, in the crystal basis.
8
+ *
9
+ * The rotation part is an exact integer matrix and the translation part is an
10
+ * exact integer count of {@link TWELFTHS_PER_CELL}, so two operations compare
11
+ * with `===` on their `operationKey` and a group expansion terminates. Every
12
+ * translation occurring in the 230 space groups and the 17 wallpaper groups is a
13
+ * twelfth: the denominators that occur are 2, 3, 4 and 6.
14
+ */
15
+ export interface CrystalOperation<D extends Dimension = 3> {
16
+ /** How many axes the operation acts on. A `CrystalOperation<2>` is not a `CrystalOperation<3>`. */
17
+ readonly dimension: D;
18
+ /** Rows of the D×D integer matrix W, acting on the fractional column (x, y, z). */
19
+ readonly rotation: ReadonlyArray<readonly number[]>;
20
+ /** Translation w, in twelfths of a cell edge, reduced into [0, 12). */
21
+ readonly translation: readonly number[];
22
+ }
23
+ /** An exact integer matrix, row-major. */
24
+ export type IntegerMatrix = ReadonlyArray<readonly number[]>;
25
+ /** The unit a translation is counted in: `6` is 1/2, `4` is 1/3, `3` is 1/4. */
26
+ export declare const TWELFTHS_PER_CELL = 12;
27
+ /** Variable names of a fractional coordinate, in order. */
28
+ export declare const AXIS_NAMES: readonly ["x", "y", "z"];
29
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/crystal/core/symmetry/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,CAAC,CAAC;AAE9B;;;;;;;;GAQG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,SAAS,GAAG,CAAC;IACvD,mGAAmG;IACnG,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;IACtB,mFAAmF;IACnF,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;IACpD,uEAAuE;IACvE,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;CACzC;AAED,0CAA0C;AAC1C,MAAM,MAAM,aAAa,GAAG,aAAa,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;AAE7D,gFAAgF;AAChF,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,2DAA2D;AAC3D,eAAO,MAAM,UAAU,0BAA2B,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** The unit a translation is counted in: `6` is 1/2, `4` is 1/3, `3` is 1/4. */
2
+ export const TWELFTHS_PER_CELL = 12;
3
+ /** Variable names of a fractional coordinate, in order. */
4
+ export const AXIS_NAMES = ['x', 'y', 'z'];
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/crystal/core/symmetry/types.ts"],"names":[],"mappings":"AA2BA,gFAAgF;AAChF,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAEpC,2DAA2D;AAC3D,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAU,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { PeriodicComponent } from './components.ts';
2
+ import type { ExpandedAtom } from './expand.ts';
3
+ /**
4
+ * The connected sets grouped by the sites they are built from: sets sharing a
5
+ * site are symmetry copies of one another.
6
+ * @param components - The connected sets of the cell.
7
+ * @param atoms - The atoms of the cell.
8
+ * @param siteCount - How many sites the asymmetric unit holds.
9
+ * @returns The groups, ordered on their first site.
10
+ */
11
+ export declare function symmetryClasses(components: readonly PeriodicComponent[], atoms: readonly ExpandedAtom[], siteCount: number): PeriodicComponent[][];
12
+ /**
13
+ * The copy of a molecule closest to the file: the one holding the most atoms
14
+ * the identity made, the first on a tie.
15
+ * @param members - The copies.
16
+ * @param atoms - The atoms of the cell.
17
+ * @param identityIndex - Index of the identity in the operation list.
18
+ * @returns The copy.
19
+ */
20
+ export declare function representative(members: readonly PeriodicComponent[], atoms: readonly ExpandedAtom[], identityIndex: number): PeriodicComponent;
21
+ /**
22
+ * The sites a group of connected sets is built from.
23
+ * @param members - The sets.
24
+ * @param atoms - The atoms of the cell.
25
+ * @returns The site indices, ascending.
26
+ */
27
+ export declare function siteIndicesOf(members: readonly PeriodicComponent[], atoms: readonly ExpandedAtom[]): number[];
28
+ //# sourceMappingURL=symmetryClasses.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"symmetryClasses.d.ts","sourceRoot":"","sources":["../../../src/crystal/core/symmetryClasses.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,SAAS,iBAAiB,EAAE,EACxC,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,SAAS,EAAE,MAAM,GAChB,iBAAiB,EAAE,EAAE,CAuBvB;AAmBD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,aAAa,EAAE,MAAM,GACpB,iBAAiB,CAgBnB;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,KAAK,EAAE,SAAS,YAAY,EAAE,GAC7B,MAAM,EAAE,CAQV"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * The connected sets grouped by the sites they are built from: sets sharing a
3
+ * site are symmetry copies of one another.
4
+ * @param components - The connected sets of the cell.
5
+ * @param atoms - The atoms of the cell.
6
+ * @param siteCount - How many sites the asymmetric unit holds.
7
+ * @returns The groups, ordered on their first site.
8
+ */
9
+ export function symmetryClasses(components, atoms, siteCount) {
10
+ const parent = new Int32Array(siteCount);
11
+ for (let site = 0; site < siteCount; site++)
12
+ parent[site] = site;
13
+ for (const component of components) {
14
+ const first = atoms[component.atoms[0] ?? 0].siteIndex;
15
+ for (const index of component.atoms) {
16
+ unite(parent, first, atoms[index].siteIndex);
17
+ }
18
+ }
19
+ const classes = new Map();
20
+ for (const component of components) {
21
+ const site = atoms[component.atoms[0] ?? 0].siteIndex;
22
+ const root = rootOf(parent, site);
23
+ const members = classes.get(root);
24
+ if (members === undefined)
25
+ classes.set(root, [component]);
26
+ else
27
+ members.push(component);
28
+ }
29
+ const roots = Int32Array.from(classes.keys()).toSorted();
30
+ const ordered = [];
31
+ for (let index = 0; index < roots.length; index++) {
32
+ ordered.push(classes.get(roots[index] ?? 0) ?? []);
33
+ }
34
+ return ordered;
35
+ }
36
+ function rootOf(parent, site) {
37
+ let current = site;
38
+ while (parent[current] !== current) {
39
+ const next = parent[current] ?? current;
40
+ parent[current] = parent[next] ?? next;
41
+ current = next;
42
+ }
43
+ return current;
44
+ }
45
+ function unite(parent, a, b) {
46
+ const rootA = rootOf(parent, a);
47
+ const rootB = rootOf(parent, b);
48
+ if (rootA < rootB)
49
+ parent[rootB] = rootA;
50
+ else if (rootB < rootA)
51
+ parent[rootA] = rootB;
52
+ }
53
+ /**
54
+ * The copy of a molecule closest to the file: the one holding the most atoms
55
+ * the identity made, the first on a tie.
56
+ * @param members - The copies.
57
+ * @param atoms - The atoms of the cell.
58
+ * @param identityIndex - Index of the identity in the operation list.
59
+ * @returns The copy.
60
+ */
61
+ export function representative(members, atoms, identityIndex) {
62
+ let best = members[0];
63
+ let bestCount = -1;
64
+ for (const component of members) {
65
+ let count = 0;
66
+ for (const index of component.atoms) {
67
+ if (atoms[index].operationIndex === identityIndex) {
68
+ count++;
69
+ }
70
+ }
71
+ if (count > bestCount) {
72
+ best = component;
73
+ bestCount = count;
74
+ }
75
+ }
76
+ return best;
77
+ }
78
+ /**
79
+ * The sites a group of connected sets is built from.
80
+ * @param members - The sets.
81
+ * @param atoms - The atoms of the cell.
82
+ * @returns The site indices, ascending.
83
+ */
84
+ export function siteIndicesOf(members, atoms) {
85
+ const seen = new Set();
86
+ for (const component of members) {
87
+ for (const index of component.atoms) {
88
+ seen.add(atoms[index].siteIndex);
89
+ }
90
+ }
91
+ return Array.from(Int32Array.from(seen).toSorted());
92
+ }
93
+ //# sourceMappingURL=symmetryClasses.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"symmetryClasses.js","sourceRoot":"","sources":["../../../src/crystal/core/symmetryClasses.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAC7B,UAAwC,EACxC,KAA8B,EAC9B,SAAiB;IAEjB,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;IACzC,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE;QAAE,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACjE,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,KAAK,GAAI,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAkB,CAAC,SAAS,CAAC;QACzE,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACpC,KAAK,CAAC,MAAM,EAAE,KAAK,EAAG,KAAK,CAAC,KAAK,CAAkB,CAAC,SAAS,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,GAAG,EAA+B,CAAC;IACvD,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,IAAI,GAAI,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAkB,CAAC,SAAS,CAAC;QACxE,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAClC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAA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,GAAG,KAAK;QAAE,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;AAChD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAqC,EACrC,KAA8B,EAC9B,aAAqB;IAErB,IAAI,IAAI,GAAG,OAAO,CAAC,CAAC,CAAsB,CAAC;IAC3C,IAAI,SAAS,GAAG,CAAC,CAAC,CAAC;IACnB,KAAK,MAAM,SAAS,IAAI,OAAO,EAAE,CAAC;QAChC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACpC,IAAK,KAAK,CAAC,KAAK,CAAkB,CAAC,cAAc,KAAK,aAAa,EAAE,CAAC;gBACpE,KAAK,EAAE,CAAC;YACV,CAAC;QACH,CAAC;QACD,IAAI,KAAK,GAAG,SAAS,EAAE,CAAC;YACtB,IAAI,GAAG,SAAS,CAAC;YACjB,SAAS,GAAG,KAAK,CAAC;QACpB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAC3B,OAAqC,EACrC,KAA8B;IAE9B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,MAAM,SAAS,IAAI,OAAO,EAAE,CAAC;QAChC,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;YACpC,IAAI,CAAC,GAAG,CAAE,KAAK,CAAC,KAAK,CAAkB,CAAC,SAAS,CAAC,CAAC;QACrD,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Crystal structures: a CIF read, its asymmetric unit expanded by the space
3
+ * group, and the cell turned into whole, symmetry-independent molecules with
4
+ * Cartesian coordinates — or into the network a salt, a mineral or a metal is.
5
+ *
6
+ * Its own door because it carries the 230 space groups and a CIF reader, which
7
+ * a page that never opens a crystal must not download. It loads neither React
8
+ * nor openchemlib nor molstar, so a Node batch reading a whole database imports
9
+ * it as it is.
10
+ */
11
+ export * from './crystal/core/index.ts';
12
+ //# sourceMappingURL=crystal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crystal.d.ts","sourceRoot":"","sources":["../src/crystal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,cAAc,yBAAyB,CAAC"}
package/lib/crystal.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Crystal structures: a CIF read, its asymmetric unit expanded by the space
3
+ * group, and the cell turned into whole, symmetry-independent molecules with
4
+ * Cartesian coordinates — or into the network a salt, a mineral or a metal is.
5
+ *
6
+ * Its own door because it carries the 230 space groups and a CIF reader, which
7
+ * a page that never opens a crystal must not download. It loads neither React
8
+ * nor openchemlib nor molstar, so a Node batch reading a whole database imports
9
+ * it as it is.
10
+ */
11
+ export * from "./crystal/core/index.js";
12
+ //# sourceMappingURL=crystal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crystal.js","sourceRoot":"","sources":["../src/crystal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,cAAc,yBAAyB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cheminfo",
3
- "version": "0.134.1",
3
+ "version": "0.135.0",
4
4
  "license": "MIT",
5
5
  "description": "React components, hooks and framework-free helpers shared by the cheminfo sites: site chrome, citations, sharing, routing, pedagogy, charts, and the structure, orbital and 3D molecule viewers",
6
6
  "author": "Luc Patiny",
@@ -20,6 +20,7 @@
20
20
  "./card": "./lib/card.js",
21
21
  "./conformers": "./lib/conformers.js",
22
22
  "./core": "./lib/core.js",
23
+ "./crystal": "./lib/crystal.js",
23
24
  "./demo": "./lib/demo.js",
24
25
  "./drawing": "./lib/drawing.js",
25
26
  "./formula": "./lib/formula.js",
@@ -87,6 +88,7 @@
87
88
  "bezier-js": "^6.1.4",
88
89
  "cheminfo-font": "^1.28.0",
89
90
  "cheminfo-types": "^1.16.0",
91
+ "cif-to-json": "^2.1.0",
90
92
  "convert-to-jcamp": "^7.0.3",
91
93
  "countries-and-timezones": "^3.11.0",
92
94
  "crc-32": "^1.2.2",
@@ -0,0 +1,98 @@
1
+ import type { PackedAtom } from './periodicBonds.ts';
2
+ import type { Lattice } from './symmetry/lattice.ts';
3
+
4
+ /** The atoms of each bin, and the bin each atom falls in. */
5
+ export interface Grid {
6
+ /** Bins along a, b and c. */
7
+ readonly counts: readonly [number, number, number];
8
+ /** How many bins away along each axis a bond can reach. */
9
+ readonly reaches: readonly [number, number, number];
10
+ /** `start[bin]` to `start[bin + 1]` index the atoms of a bin in `members`. */
11
+ readonly start: Int32Array;
12
+ readonly members: Int32Array;
13
+ /** The bin of each atom, three indices per atom. */
14
+ readonly cells: Int32Array;
15
+ }
16
+
17
+ /**
18
+ * Bin the atoms of a cell on a grid of the cell itself.
19
+ *
20
+ * A bin holds about one atom and is never thinner than a bond, so a dense cell
21
+ * is not compared pair by pair and a sparse one is not cut into empty bins.
22
+ * @param atoms - The atoms, fractional, wrapped into [0, 1).
23
+ * @param lattice - The cell.
24
+ * @param reach - The longest bond searched for, Å.
25
+ * @returns The bins, and how many of them a bond can reach along each axis.
26
+ */
27
+ export function createGrid(
28
+ atoms: readonly PackedAtom[],
29
+ lattice: Lattice,
30
+ reach: number,
31
+ ): Grid {
32
+ const count = atoms.length;
33
+ const spacing = Math.max(reach, Math.cbrt(lattice.volume / count));
34
+ const widths = planeSpacings(lattice);
35
+ const counts: [number, number, number] = [1, 1, 1];
36
+ const reaches: [number, number, number] = [1, 1, 1];
37
+ for (let axis = 0; axis < 3; axis++) {
38
+ const width = widths[axis] ?? 1;
39
+ const bins = Math.max(1, Math.floor(width / spacing));
40
+ counts[axis] = bins;
41
+ reaches[axis] = Math.max(1, Math.ceil((reach * bins) / width - 1e-9));
42
+ }
43
+ const [na, nb, nc] = counts;
44
+ const cells = new Int32Array(count * 3);
45
+ const binOf = new Int32Array(count);
46
+ const start = new Int32Array(na * nb * nc + 1);
47
+ for (let index = 0; index < count; index++) {
48
+ const position = (atoms[index] as PackedAtom).position;
49
+ for (let axis = 0; axis < 3; axis++) {
50
+ const bins = counts[axis] ?? 1;
51
+ const value = Math.floor((position[axis] ?? 0) * bins);
52
+ cells[index * 3 + axis] = Math.min(bins - 1, Math.max(0, value));
53
+ }
54
+ const bin =
55
+ ((cells[index * 3] ?? 0) * nb + (cells[index * 3 + 1] ?? 0)) * nc +
56
+ (cells[index * 3 + 2] ?? 0);
57
+ binOf[index] = bin;
58
+ start[bin + 1] = (start[bin + 1] ?? 0) + 1;
59
+ }
60
+ for (let bin = 0; bin < na * nb * nc; bin++) {
61
+ start[bin + 1] = (start[bin + 1] ?? 0) + (start[bin] ?? 0);
62
+ }
63
+ const cursor = start.slice(0, -1);
64
+ const members = new Int32Array(count);
65
+ for (let index = 0; index < count; index++) {
66
+ const bin = binOf[index] ?? 0;
67
+ members[cursor[bin] ?? 0] = index;
68
+ cursor[bin] = (cursor[bin] ?? 0) + 1;
69
+ }
70
+ return { counts, reaches, start, members, cells };
71
+ }
72
+
73
+ /**
74
+ * The distance between consecutive lattice planes normal to each axis, Å.
75
+ * @param lattice - The cell.
76
+ * @returns The spacings normal to a, b and c.
77
+ */
78
+ function planeSpacings(lattice: Lattice): [number, number, number] {
79
+ const m = lattice.cartesian;
80
+ const column = (index: number): [number, number, number] => [
81
+ m[0]?.[index] ?? 0,
82
+ m[1]?.[index] ?? 0,
83
+ m[2]?.[index] ?? 0,
84
+ ];
85
+ const [a, b, c] = [column(0), column(1), column(2)];
86
+ return [
87
+ lattice.volume / crossLength(b, c),
88
+ lattice.volume / crossLength(c, a),
89
+ lattice.volume / crossLength(a, b),
90
+ ];
91
+ }
92
+
93
+ function crossLength(u: readonly number[], v: readonly number[]): number {
94
+ const x = (u[1] ?? 0) * (v[2] ?? 0) - (u[2] ?? 0) * (v[1] ?? 0);
95
+ const y = (u[2] ?? 0) * (v[0] ?? 0) - (u[0] ?? 0) * (v[2] ?? 0);
96
+ const z = (u[0] ?? 0) * (v[1] ?? 0) - (u[1] ?? 0) * (v[0] ?? 0);
97
+ return Math.hypot(x, y, z);
98
+ }
@@ -0,0 +1,121 @@
1
+ import type { CrystalSite } from './cif/types.ts';
2
+ import { disorderGroupOf } from './disorder.ts';
3
+ import type { ExpandedAtom } from './expand.ts';
4
+ import type { PeriodicPairs } from './periodicBonds.ts';
5
+
6
+ /**
7
+ * The atoms to drop because another one already stands in their place.
8
+ * @param atoms - The atoms of one cell.
9
+ * @param coincident - The pairs of atoms closer than an atom to itself.
10
+ * @returns One flag per atom, 1 for an atom dropped.
11
+ */
12
+ export function writtenTwice(
13
+ atoms: readonly ExpandedAtom[],
14
+ coincident: Int32Array,
15
+ ): Uint8Array {
16
+ const removed = new Uint8Array(atoms.length);
17
+ for (let pair = 0; pair < coincident.length; pair += 2) {
18
+ const first = coincident[pair] ?? 0;
19
+ const second = coincident[pair + 1] ?? 0;
20
+ if (removed[first] === 1 || removed[second] === 1) continue;
21
+ const higher =
22
+ (atoms[second] as ExpandedAtom).occupancy >
23
+ (atoms[first] as ExpandedAtom).occupancy;
24
+ removed[higher ? first : second] = 1;
25
+ }
26
+ return removed;
27
+ }
28
+
29
+ /**
30
+ * The pairs kept as bonds: none to an atom dropped, none between the copies of
31
+ * a disorder about a special position, and one per hydrogen.
32
+ * @param pairs - The bonded pairs of the cell.
33
+ * @param atoms - The atoms of the cell.
34
+ * @param sites - The asymmetric unit.
35
+ * @param removed - One flag per atom, 1 for an atom dropped.
36
+ * @returns One flag per pair, 1 to keep it.
37
+ */
38
+ export function keptPairs(
39
+ pairs: PeriodicPairs,
40
+ atoms: readonly ExpandedAtom[],
41
+ sites: readonly CrystalSite[],
42
+ removed: Uint8Array,
43
+ ): Uint8Array {
44
+ const keep = new Uint8Array(pairs.count);
45
+ for (let pair = 0; pair < pairs.count; pair++) {
46
+ const first = pairs.from[pair] ?? 0;
47
+ const second = pairs.to[pair] ?? 0;
48
+ if (removed[first] === 1 || removed[second] === 1) continue;
49
+ if (keptApart(atoms[first], atoms[second], sites)) continue;
50
+ keep[pair] = 1;
51
+ }
52
+ keepNearestToHydrogen(pairs, atoms, keep);
53
+ return keep;
54
+ }
55
+
56
+ /**
57
+ * Whether two atoms are copies of a disorder that lies about a special
58
+ * position — a negative group, as SHELX writes it — made by different
59
+ * operations: such copies overlap on purpose, and are never bonded.
60
+ * @param a - One atom of the cell.
61
+ * @param b - Another one.
62
+ * @param sites - The asymmetric unit.
63
+ * @returns Whether they are kept apart.
64
+ */
65
+ function keptApart(
66
+ a: ExpandedAtom | undefined,
67
+ b: ExpandedAtom | undefined,
68
+ sites: readonly CrystalSite[],
69
+ ): boolean {
70
+ if (a === undefined || b === undefined) return false;
71
+ if (a.operationIndex === b.operationIndex) return false;
72
+ const groupA = disorderGroupOf(sites[a.siteIndex] as CrystalSite);
73
+ const groupB = disorderGroupOf(sites[b.siteIndex] as CrystalSite);
74
+ return (
75
+ groupA !== null &&
76
+ groupB !== null &&
77
+ (groupA.startsWith('-') || groupB.startsWith('-'))
78
+ );
79
+ }
80
+
81
+ /**
82
+ * A hydrogen holds one bond, to its nearest neighbour: one sitting between two
83
+ * oxygens of a short hydrogen bond would otherwise chain the molecules.
84
+ * @param pairs - The bonded pairs of the cell.
85
+ * @param atoms - The atoms of the cell.
86
+ * @param keep - One flag per pair, cleared where a hydrogen has a nearer partner.
87
+ */
88
+ function keepNearestToHydrogen(
89
+ pairs: PeriodicPairs,
90
+ atoms: readonly ExpandedAtom[],
91
+ keep: Uint8Array,
92
+ ) {
93
+ const nearest = new Int32Array(atoms.length).fill(-1);
94
+ for (let pair = 0; pair < pairs.count; pair++) {
95
+ if (keep[pair] === 0) continue;
96
+ for (let side = 0; side < 2; side++) {
97
+ const atom = (side === 0 ? pairs.from[pair] : pairs.to[pair]) ?? 0;
98
+ if ((atoms[atom] as ExpandedAtom).element !== 'H') continue;
99
+ const best = nearest[atom] ?? -1;
100
+ if (
101
+ best === -1 ||
102
+ (pairs.lengths[pair] ?? 0) < (pairs.lengths[best] ?? 0)
103
+ ) {
104
+ nearest[atom] = pair;
105
+ }
106
+ }
107
+ }
108
+ for (let pair = 0; pair < pairs.count; pair++) {
109
+ if (keep[pair] === 0) continue;
110
+ const from = pairs.from[pair] ?? 0;
111
+ const to = pairs.to[pair] ?? 0;
112
+ const fromHydrogen = (atoms[from] as ExpandedAtom).element === 'H';
113
+ const toHydrogen = (atoms[to] as ExpandedAtom).element === 'H';
114
+ if (
115
+ (fromHydrogen && nearest[from] !== pair) ||
116
+ (toHydrogen && nearest[to] !== pair)
117
+ ) {
118
+ keep[pair] = 0;
119
+ }
120
+ }
121
+ }
@@ -0,0 +1,40 @@
1
+ import { elementBySymbol } from '../../../periodic/core/elements.ts';
2
+
3
+ /**
4
+ * Read the element of an atom site: the type symbol's, else the label's.
5
+ * @param typeSymbol - `_atom_site_type_symbol`, or `null` when absent.
6
+ * @param label - `_atom_site_label`.
7
+ * @returns The chemical symbol.
8
+ * @throws {Error} When neither opens with an element symbol.
9
+ */
10
+ export function elementOf(typeSymbol: string | null, label: string): string {
11
+ const fromType = typeSymbol === null ? null : symbolOf(typeSymbol);
12
+ if (fromType !== null) return fromType;
13
+ const fromLabel = symbolOf(label);
14
+ if (fromLabel !== null) return fromLabel;
15
+ throw new Error(
16
+ `Atom site "${label}" names no element: neither its type symbol nor its label opens with one.`,
17
+ );
18
+ }
19
+
20
+ /**
21
+ * Read the chemical symbol a CIF token opens with.
22
+ *
23
+ * A type symbol carries the oxidation state (`Na1+`, `Cl1-`) and a label
24
+ * carries an index in whatever case the refinement wrote it (`NA1`, `CL2`), so
25
+ * the letters are taken, recapitalised and checked against the periodic table.
26
+ * Deuterium, which a neutron structure writes `D`, is hydrogen.
27
+ * @param token - a type symbol or an atom-site label.
28
+ * @returns the symbol, or `null` when the letters name no element.
29
+ */
30
+ function symbolOf(token: string): string | null {
31
+ const letters = /^(?<letters>[A-Za-z]{1,2})/.exec(token)?.groups?.letters;
32
+ if (letters === undefined) return null;
33
+ for (let length = letters.length; length >= 1; length--) {
34
+ const symbol =
35
+ letters.charAt(0).toUpperCase() + letters.slice(1, length).toLowerCase();
36
+ if (elementBySymbol(symbol) !== undefined) return symbol;
37
+ if (symbol === 'D') return 'H';
38
+ }
39
+ return null;
40
+ }