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,44 @@
1
+ export * from './cif/index.ts';
2
+ export type { BondGraph, PeriodicComponent } from './components.ts';
3
+ export {
4
+ BOND_TOLERANCE,
5
+ covalentBondLimit,
6
+ covalentRadius,
7
+ } from './covalentRadii.ts';
8
+ export {
9
+ ASSEMBLY_OVERLAP,
10
+ DISORDER_OVERLAP,
11
+ chooseDisorder,
12
+ disorderGroupOf,
13
+ isDummy,
14
+ keptSites,
15
+ } from './disorder.ts';
16
+ export type { AsymmetricSite, ExpandedAtom } from './expand.ts';
17
+ export { expandSite, expandStructure } from './expand.ts';
18
+ export type {
19
+ CrystalMolecule,
20
+ CrystalMoleculeAtom,
21
+ CrystalMoleculeBond,
22
+ CrystalMolecules,
23
+ CrystalMoleculesOptions,
24
+ CrystalNetwork,
25
+ } from './moleculeTypes.ts';
26
+ export { SAME_ATOM_DISTANCE, crystalMolecules } from './molecules.ts';
27
+ export { structureOperations } from './operations.ts';
28
+ export type { PackingResult } from './packing.ts';
29
+ export { MAX_PACKED_ATOMS, packMolecules } from './packing.ts';
30
+ export type {
31
+ BondLimit,
32
+ PackedAtom,
33
+ PeriodicBondOptions,
34
+ PeriodicPairs,
35
+ } from './periodicBonds.ts';
36
+ export { findPeriodicBonds } from './periodicBonds.ts';
37
+ export type { ResolvedSetting } from './resolve.ts';
38
+ export {
39
+ operationsBelong,
40
+ resolveStructureSetting,
41
+ settingOfOperations,
42
+ } from './resolve.ts';
43
+ export * from './spaceGroups/index.ts';
44
+ export * from './symmetry/index.ts';
@@ -0,0 +1,169 @@
1
+ import type { CrystalSite } from './cif/types.ts';
2
+ import type { BondGraph } from './components.ts';
3
+ import { disorderGroupOf } from './disorder.ts';
4
+ import type { ExpandedAtom } from './expand.ts';
5
+ import type {
6
+ CrystalMolecule,
7
+ CrystalMoleculeAtom,
8
+ CrystalMoleculeBond,
9
+ } from './moleculeTypes.ts';
10
+ import type { Lattice } from './symmetry/lattice.ts';
11
+
12
+ /** Slack on a centroid sitting on a cell face, so it lands in one cell only. */
13
+ const CENTRE_TOLERANCE = 1e-6;
14
+
15
+ /** What a molecule is read from: the cell, its bonds, and the walk over them. */
16
+ export interface MoleculeContext {
17
+ readonly atoms: readonly ExpandedAtom[];
18
+ readonly sites: readonly CrystalSite[];
19
+ readonly graph: BondGraph;
20
+ /** The cell each atom was reached in, three integers per atom. */
21
+ readonly shifts: Int32Array;
22
+ readonly lattice: Lattice;
23
+ /** Index of the identity in the operation list, -1 when it is absent. */
24
+ readonly identityIndex: number;
25
+ /** One slot per atom of the cell, -1 everywhere between two calls. */
26
+ readonly localOf: Int32Array;
27
+ }
28
+
29
+ /**
30
+ * One molecule, whole, placed where the file wrote it.
31
+ *
32
+ * The molecule is moved by a whole number of cells so the first atom the
33
+ * file's own position generates sits at the coordinates the file gives it.
34
+ * A molecule with no such atom is moved to the cell its centre falls in.
35
+ * @param members - Its atoms, as indices into the cell.
36
+ * @param copies - How many copies of it one cell holds.
37
+ * @param context - The cell it is read from.
38
+ * @returns The molecule.
39
+ */
40
+ export function buildMolecule(
41
+ members: readonly number[],
42
+ copies: number,
43
+ context: MoleculeContext,
44
+ ): CrystalMolecule {
45
+ const { atoms, sites, shifts, lattice, localOf } = context;
46
+ const order = moleculeOrder(members, context);
47
+ const move = placement(order, context);
48
+ const m = lattice.cartesian;
49
+ const placed: CrystalMoleculeAtom[] = [];
50
+ for (let local = 0; local < order.length; local++) {
51
+ const index = order[local] ?? 0;
52
+ localOf[index] = local;
53
+ const atom = atoms[index] as ExpandedAtom;
54
+ const site = sites[atom.siteIndex] as CrystalSite;
55
+ const fractional = [0, 0, 0];
56
+ for (let axis = 0; axis < 3; axis++) {
57
+ fractional[axis] =
58
+ (atom.position[axis] ?? 0) +
59
+ (shifts[index * 3 + axis] ?? 0) +
60
+ (move[axis] ?? 0);
61
+ }
62
+ const [x = 0, y = 0, z = 0] = fractional;
63
+ placed.push({
64
+ element: atom.element,
65
+ label: atom.label,
66
+ siteIndex: atom.siteIndex,
67
+ operationIndex: atom.operationIndex,
68
+ position: [
69
+ (m[0]?.[0] ?? 0) * x + (m[0]?.[1] ?? 0) * y + (m[0]?.[2] ?? 0) * z,
70
+ (m[1]?.[0] ?? 0) * x + (m[1]?.[1] ?? 0) * y + (m[1]?.[2] ?? 0) * z,
71
+ (m[2]?.[0] ?? 0) * x + (m[2]?.[1] ?? 0) * y + (m[2]?.[2] ?? 0) * z,
72
+ ],
73
+ occupancy: atom.occupancy,
74
+ disorderGroup: disorderGroupOf(site),
75
+ });
76
+ }
77
+ const bonds = moleculeBonds(order, context);
78
+ for (let local = 0; local < order.length; local++) {
79
+ localOf[order[local] ?? 0] = -1;
80
+ }
81
+ return { atoms: placed, bonds, copies };
82
+ }
83
+
84
+ /**
85
+ * The atoms the identity generates first, in site order, then the others by
86
+ * operation and site.
87
+ * @param members - The atoms of the molecule, as indices into the cell.
88
+ * @param context - The cell they are read from.
89
+ * @returns The atoms, as indices into the cell, in molecule order.
90
+ */
91
+ function moleculeOrder(
92
+ members: readonly number[],
93
+ context: MoleculeContext,
94
+ ): Int32Array {
95
+ const { atoms, sites, identityIndex } = context;
96
+ const total = atoms.length;
97
+ const keys = new Float64Array(members.length);
98
+ for (let index = 0; index < members.length; index++) {
99
+ const member = members[index] ?? 0;
100
+ const atom = atoms[member] as ExpandedAtom;
101
+ const rank =
102
+ atom.operationIndex === identityIndex ? 0 : atom.operationIndex + 1;
103
+ keys[index] = (rank * sites.length + atom.siteIndex) * total + member;
104
+ }
105
+ const sorted = keys.toSorted();
106
+ const order = new Int32Array(members.length);
107
+ for (let index = 0; index < sorted.length; index++) {
108
+ order[index] = (sorted[index] ?? 0) % total;
109
+ }
110
+ return order;
111
+ }
112
+
113
+ /**
114
+ * The whole cells that put the molecule where the file wrote it.
115
+ * @param order - The atoms of the molecule, in molecule order.
116
+ * @param context - The cell they are read from.
117
+ * @returns The cell translation, in whole cells.
118
+ */
119
+ function placement(
120
+ order: Int32Array,
121
+ context: MoleculeContext,
122
+ ): [number, number, number] {
123
+ const { atoms, sites, shifts, identityIndex } = context;
124
+ const first = order[0] ?? 0;
125
+ const anchor = atoms[first] as ExpandedAtom;
126
+ if (anchor.operationIndex === identityIndex) {
127
+ const site = sites[anchor.siteIndex] as CrystalSite;
128
+ return [
129
+ Math.round(site.x - (anchor.position[0] + (shifts[first * 3] ?? 0))),
130
+ Math.round(site.y - (anchor.position[1] + (shifts[first * 3 + 1] ?? 0))),
131
+ Math.round(site.z - (anchor.position[2] + (shifts[first * 3 + 2] ?? 0))),
132
+ ];
133
+ }
134
+ const centre = [0, 0, 0];
135
+ for (let local = 0; local < order.length; local++) {
136
+ const index = order[local] ?? 0;
137
+ const position = (atoms[index] as ExpandedAtom).position;
138
+ for (let axis = 0; axis < 3; axis++) {
139
+ centre[axis] =
140
+ (centre[axis] ?? 0) +
141
+ (position[axis] ?? 0) +
142
+ (shifts[index * 3 + axis] ?? 0);
143
+ }
144
+ }
145
+ return [
146
+ -Math.floor((centre[0] ?? 0) / order.length + CENTRE_TOLERANCE),
147
+ -Math.floor((centre[1] ?? 0) / order.length + CENTRE_TOLERANCE),
148
+ -Math.floor((centre[2] ?? 0) / order.length + CENTRE_TOLERANCE),
149
+ ];
150
+ }
151
+
152
+ function moleculeBonds(
153
+ order: Int32Array,
154
+ context: MoleculeContext,
155
+ ): CrystalMoleculeBond[] {
156
+ const { graph, localOf } = context;
157
+ const bonds: CrystalMoleculeBond[] = [];
158
+ for (let from = 0; from < order.length; from++) {
159
+ const index = order[from] ?? 0;
160
+ const last = graph.offsets[index + 1] ?? 0;
161
+ for (let bond = graph.offsets[index] ?? 0; bond < last; bond++) {
162
+ const to = localOf[graph.targets[bond] ?? 0] ?? -1;
163
+ if (to > from) {
164
+ bonds.push({ from, to, length: graph.lengths[bond] ?? 0 });
165
+ }
166
+ }
167
+ }
168
+ return bonds.toSorted((a, b) => a.from - b.from || a.to - b.to);
169
+ }
@@ -0,0 +1,85 @@
1
+ import type { BondLimit } from './periodicBonds.ts';
2
+
3
+ /** Options of `crystalMolecules`. */
4
+ export interface CrystalMoleculesOptions {
5
+ /**
6
+ * When two atoms are bonded.
7
+ * @default covalentBondLimit
8
+ */
9
+ readonly bondLimit?: BondLimit;
10
+ /**
11
+ * The most atoms one cell may hold. A larger cell is not searched, and the
12
+ * result says so with `tooLarge`.
13
+ * @default 100000
14
+ */
15
+ readonly maxAtoms?: number;
16
+ }
17
+
18
+ /** One atom of a molecule. */
19
+ export interface CrystalMoleculeAtom {
20
+ /** Chemical symbol, e.g. `C`. */
21
+ readonly element: string;
22
+ /** The label of the site it is a copy of, e.g. `C12`. */
23
+ readonly label: string;
24
+ /** Index into `structure.sites` of that site. */
25
+ readonly siteIndex: number;
26
+ /** Index into the operation list of the operation that put it here. */
27
+ readonly operationIndex: number;
28
+ /** Cartesian coordinates, in ångström, in the cell's Cartesian frame. */
29
+ readonly position: readonly [number, number, number];
30
+ /** How much of the site the atom occupies, 1 when the file gives none. */
31
+ readonly occupancy: number;
32
+ /** The disorder group of its site, or `null` when it is in none. */
33
+ readonly disorderGroup: string | null;
34
+ }
35
+
36
+ /** One bond of a molecule. */
37
+ export interface CrystalMoleculeBond {
38
+ /** Index into the molecule's atoms. */
39
+ readonly from: number;
40
+ /** Index into the molecule's atoms, always above `from`. */
41
+ readonly to: number;
42
+ /** In ångström. */
43
+ readonly length: number;
44
+ }
45
+
46
+ /** One crystallographically independent molecule, whole. */
47
+ export interface CrystalMolecule {
48
+ /**
49
+ * Its atoms: the copies the file's own position generates first, in the
50
+ * order the file lists their sites, then those its symmetry completes.
51
+ */
52
+ readonly atoms: readonly CrystalMoleculeAtom[];
53
+ /** Its bonds, ordered on `from` then `to`. */
54
+ readonly bonds: readonly CrystalMoleculeBond[];
55
+ /** How many copies of it one cell holds. */
56
+ readonly copies: number;
57
+ }
58
+
59
+ /** A set of atoms bonded to its own translate: a chain, a layer or a framework. */
60
+ export interface CrystalNetwork {
61
+ /** The sites of the asymmetric unit it is built from, ascending. */
62
+ readonly siteIndices: readonly number[];
63
+ /** How many atoms of one cell belong to it, every symmetry copy included. */
64
+ readonly cellAtoms: number;
65
+ /** How many independent directions it extends along: 1, 2 or 3. */
66
+ readonly periodicity: 1 | 2 | 3;
67
+ }
68
+
69
+ /** What `crystalMolecules` found. */
70
+ export interface CrystalMolecules {
71
+ /**
72
+ * One per crystallographically independent molecule — a lone ion or atom
73
+ * included — ordered on the first site of the file each holds.
74
+ */
75
+ readonly molecules: readonly CrystalMolecule[];
76
+ /** One per independent network, ordered the same way. */
77
+ readonly networks: readonly CrystalNetwork[];
78
+ /**
79
+ * How many atoms one cell holds, once the disorder left out and the atoms
80
+ * written twice are removed.
81
+ */
82
+ readonly cellAtoms: number;
83
+ /** Whether the cell held more than `maxAtoms` atoms, so nothing was searched. */
84
+ readonly tooLarge: boolean;
85
+ }
@@ -0,0 +1,165 @@
1
+ import { keptPairs, writtenTwice } from './cellBonds.ts';
2
+ import type { CrystalSite, CrystalStructure } from './cif/types.ts';
3
+ import { bondGraph, walkComponents } from './components.ts';
4
+ import { covalentBondLimit } from './covalentRadii.ts';
5
+ import { chooseDisorder, isDummy } from './disorder.ts';
6
+ import type { ExpandedAtom } from './expand.ts';
7
+ import { expandSite } from './expand.ts';
8
+ import type { MoleculeContext } from './moleculeOf.ts';
9
+ import { buildMolecule } from './moleculeOf.ts';
10
+ import type {
11
+ CrystalMolecule,
12
+ CrystalMolecules,
13
+ CrystalMoleculesOptions,
14
+ CrystalNetwork,
15
+ } from './moleculeTypes.ts';
16
+ import { structureOperations } from './operations.ts';
17
+ import { findPeriodicBonds } from './periodicBonds.ts';
18
+ import { createLattice } from './symmetry/lattice.ts';
19
+ import { isIdentity } from './symmetry/operation.ts';
20
+ import type { CrystalOperation } from './symmetry/types.ts';
21
+ import {
22
+ representative,
23
+ siteIndicesOf,
24
+ symmetryClasses,
25
+ } from './symmetryClasses.ts';
26
+
27
+ /**
28
+ * Two atoms closer than this, in ångström, are one atom written twice: no
29
+ * bond is that short.
30
+ */
31
+ export const SAME_ATOM_DISTANCE = 0.5;
32
+
33
+ /**
34
+ * The molecules of a crystal, each whole and once.
35
+ *
36
+ * The asymmetric unit is expanded into the cell, the cell is bonded across its
37
+ * faces by `bondLimit`, and every connected set of atoms is walked. Copies the
38
+ * space group makes of one molecule share the sites they are built from, so
39
+ * they are told apart from an independent molecule by that alone: Z′ = 2 gives
40
+ * two molecules, the four images of a molecule in P2₁/c give one, and a
41
+ * molecule sitting on a special position comes out whole, both halves of it.
42
+ *
43
+ * - **Networks** — a set of atoms bonded to its own translate: a salt, a
44
+ * mineral, a metal, a coordination polymer — are reported in `networks` and
45
+ * never returned as molecules.
46
+ * - **Disorder**: the atoms in no disorder group are kept, and the disorder
47
+ * groups by decreasing mean occupancy, each unless it overlaps one already
48
+ * kept — the major part of each disorder (see `chooseDisorder`). An atom of
49
+ * a negative group, as SHELX writes disorder about a special position, is
50
+ * not bonded to a symmetry copy of an atom in a disorder group, as SHELX
51
+ * itself does.
52
+ * - **Hydrogen** holds one bond, to its nearest neighbour.
53
+ * - **Atoms written twice** — two sites, or two images of one site, closer than
54
+ * {@link SAME_ATOM_DISTANCE} — are one atom: the higher occupancy is kept,
55
+ * the one written first on a tie.
56
+ * @param structure - What `readCif` returned.
57
+ * @param options - Options.
58
+ * @returns The independent molecules, in Cartesian coordinates, and the
59
+ * networks.
60
+ * @throws {Error} When the file names no space group and lists no operation, or its
61
+ * cell has angles no cell can have.
62
+ */
63
+ export function crystalMolecules(
64
+ structure: CrystalStructure,
65
+ options: CrystalMoleculesOptions = {},
66
+ ): CrystalMolecules {
67
+ const { bondLimit = covalentBondLimit, maxAtoms = 100_000 } = options;
68
+ const operations = structureOperations(structure);
69
+ const lattice = createLattice(structure.cell);
70
+ const { sites } = structure;
71
+ const expanded = expandSites(sites, operations, maxAtoms);
72
+ if (expanded.count > maxAtoms) {
73
+ return {
74
+ molecules: [],
75
+ networks: [],
76
+ cellAtoms: expanded.count,
77
+ tooLarge: true,
78
+ };
79
+ }
80
+ const kept = chooseDisorder(sites, expanded.atoms, lattice);
81
+ const atoms: ExpandedAtom[] = [];
82
+ for (const atom of expanded.atoms) {
83
+ if (kept[atom.siteIndex] === 1) atoms.push(atom);
84
+ }
85
+ const pairs = findPeriodicBonds(atoms, lattice, bondLimit, {
86
+ samePlace: SAME_ATOM_DISTANCE,
87
+ });
88
+ const removed = writtenTwice(atoms, pairs.coincident);
89
+ const graph = bondGraph(
90
+ atoms.length,
91
+ pairs,
92
+ keptPairs(pairs, atoms, sites, removed),
93
+ );
94
+ const { components, shifts } = walkComponents(graph, removed);
95
+ const context: MoleculeContext = {
96
+ atoms,
97
+ sites,
98
+ graph,
99
+ shifts,
100
+ lattice,
101
+ identityIndex: identityIndexOf(operations),
102
+ localOf: new Int32Array(atoms.length).fill(-1),
103
+ };
104
+ const molecules: CrystalMolecule[] = [];
105
+ const networks: CrystalNetwork[] = [];
106
+ for (const members of symmetryClasses(components, atoms, sites.length)) {
107
+ let periodicity = 0;
108
+ let cellAtoms = 0;
109
+ for (const component of members) {
110
+ periodicity = Math.max(periodicity, component.periodicity);
111
+ cellAtoms += component.atoms.length;
112
+ }
113
+ if (periodicity > 0) {
114
+ networks.push({
115
+ siteIndices: siteIndicesOf(members, atoms),
116
+ cellAtoms,
117
+ periodicity: periodicity as 1 | 2 | 3,
118
+ });
119
+ } else {
120
+ const chosen = representative(members, atoms, context.identityIndex);
121
+ molecules.push(buildMolecule(chosen.atoms, members.length, context));
122
+ }
123
+ }
124
+ let cellAtoms = atoms.length;
125
+ for (let index = 0; index < removed.length; index++) {
126
+ cellAtoms -= removed[index] ?? 0;
127
+ }
128
+ return { molecules, networks, cellAtoms, tooLarge: false };
129
+ }
130
+
131
+ /**
132
+ * Every atom of the cell the sites that are not dummies make, counted past the cap.
133
+ * @param sites - The asymmetric unit.
134
+ * @param operations - The full coset list.
135
+ * @param maxAtoms - The most atoms kept.
136
+ * @returns The atoms, and how many the cell holds.
137
+ */
138
+ function expandSites(
139
+ sites: readonly CrystalSite[],
140
+ operations: ReadonlyArray<CrystalOperation<3>>,
141
+ maxAtoms: number,
142
+ ): { atoms: ExpandedAtom[]; count: number } {
143
+ const atoms: ExpandedAtom[] = [];
144
+ let count = 0;
145
+ for (let index = 0; index < sites.length; index++) {
146
+ const site = sites[index] as CrystalSite;
147
+ if (isDummy(site)) continue;
148
+ const images = expandSite(site, index, operations);
149
+ count += images.length;
150
+ if (count > maxAtoms) continue;
151
+ for (let image = 0; image < images.length; image++) {
152
+ atoms.push(images[image] as ExpandedAtom);
153
+ }
154
+ }
155
+ return { atoms, count };
156
+ }
157
+
158
+ function identityIndexOf(
159
+ operations: ReadonlyArray<CrystalOperation<3>>,
160
+ ): number {
161
+ for (let index = 0; index < operations.length; index++) {
162
+ if (isIdentity(operations[index] as CrystalOperation<3>)) return index;
163
+ }
164
+ return -1;
165
+ }
@@ -0,0 +1,79 @@
1
+ import type { CrystalStructure } from './cif/types.ts';
2
+ import { resolveStructureSetting } from './resolve.ts';
3
+ import { spaceGroupOperations } from './spaceGroups/spaceGroupOperations.ts';
4
+ import { closure } from './symmetry/group.ts';
5
+ import { operationKey } from './symmetry/operation.ts';
6
+ import { parseOperation } from './symmetry/parseOperation.ts';
7
+ import type { CrystalOperation } from './symmetry/types.ts';
8
+
9
+ /**
10
+ * The full coset list a structure's asymmetric unit is expanded with.
11
+ *
12
+ * The file's own operations come first, in file order, so an atom's
13
+ * `operationIndex` points at the line of the file that made it. A file that
14
+ * lists only part of the group — one coset of a centred group, or a few
15
+ * generators — is completed: from the setting its symbol names when every
16
+ * listed operation belongs to it, else by composing the listed ones. A file
17
+ * that lists none, or one that cannot be read, is expanded with the setting its
18
+ * Hall symbol, Hermann–Mauguin symbol or number names.
19
+ * @param structure - What `readCif` returned.
20
+ * @returns The operations, the identity among them.
21
+ * @throws {Error} When no listed operation can be used and the file names no space
22
+ * group the catalogue knows.
23
+ */
24
+ export function structureOperations(
25
+ structure: CrystalStructure,
26
+ ): ReadonlyArray<CrystalOperation<3>> {
27
+ const resolved = resolveStructureSetting(structure);
28
+ const listed = readListed(structure.symopsXyz);
29
+ if (typeof listed === 'string' || listed.length === 0) {
30
+ if (resolved.setting !== null) {
31
+ return spaceGroupOperations(resolved.setting);
32
+ }
33
+ throw new Error(
34
+ typeof listed === 'string'
35
+ ? `This CIF names no space group this reader knows, and its operation list cannot be read: ${listed}.`
36
+ : 'This CIF names no space group this reader knows, and lists no symmetry operation.',
37
+ );
38
+ }
39
+ const keys = new Set<string>();
40
+ const operations: Array<CrystalOperation<3>> = [];
41
+ appendNew(operations, keys, listed);
42
+ if (resolved.setting !== null && resolved.agrees === true) {
43
+ appendNew(operations, keys, spaceGroupOperations(resolved.setting));
44
+ return operations;
45
+ }
46
+ try {
47
+ appendNew(operations, keys, closure(operations));
48
+ } catch {
49
+ // Operations that close on no group are left as the file wrote them.
50
+ }
51
+ return operations;
52
+ }
53
+
54
+ function readListed(
55
+ symopsXyz: readonly string[],
56
+ ): Array<CrystalOperation<3>> | string {
57
+ const operations: Array<CrystalOperation<3>> = [];
58
+ for (const xyz of symopsXyz) {
59
+ try {
60
+ operations.push(parseOperation(xyz, 3));
61
+ } catch (error) {
62
+ return error instanceof Error ? error.message : String(error);
63
+ }
64
+ }
65
+ return operations;
66
+ }
67
+
68
+ function appendNew(
69
+ operations: Array<CrystalOperation<3>>,
70
+ keys: Set<string>,
71
+ candidates: ReadonlyArray<CrystalOperation<3>>,
72
+ ) {
73
+ for (const operation of candidates) {
74
+ const key = operationKey(operation);
75
+ if (keys.has(key)) continue;
76
+ keys.add(key);
77
+ operations.push(operation);
78
+ }
79
+ }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Whole molecules instead of atoms cut at the cell walls.
3
+ *
4
+ * The expanded cell holds every atom wrapped into [0, 1), so a molecule lying
5
+ * across a face is drawn in pieces at opposite ends of the box. Here the cell
6
+ * is read as the periodic graph it is: two atoms are bonded, possibly across a
7
+ * face, by the rule handed in — a viewer hands the one it draws bonds with, so
8
+ * what is completed here is exactly what it joins. Each molecule is then moved,
9
+ * whole, to the cell its centre falls in, and appears once per cell.
10
+ *
11
+ * A component that reaches its own translate is not a molecule but a network
12
+ * — rock salt, quartz, a metal — and has no whole to complete: its atoms stay
13
+ * where the cell put them.
14
+ */
15
+
16
+ import { bondGraph, walkComponents } from './components.ts';
17
+ import type { BondLimit, PackedAtom } from './periodicBonds.ts';
18
+ import { findPeriodicBonds } from './periodicBonds.ts';
19
+ import type { Lattice } from './symmetry/lattice.ts';
20
+
21
+ /** What {@link packMolecules} found. */
22
+ export interface PackingResult {
23
+ /** The atoms, each molecule whole and once per cell. */
24
+ readonly atoms: readonly PackedAtom[];
25
+ /**
26
+ * How many molecules one cell holds: sets of two atoms or more bonded to
27
+ * nothing outside themselves. A lone ion is not counted.
28
+ */
29
+ readonly molecules: number;
30
+ /** Whether some atoms belong to a network extending through the crystal. */
31
+ readonly extended: boolean;
32
+ }
33
+
34
+ /**
35
+ * The most atoms per cell the bonds are searched over, so a cell beyond it is
36
+ * drawn as it is rather than holding a page.
37
+ */
38
+ export const MAX_PACKED_ATOMS = 2000;
39
+
40
+ /** Slack on a centroid sitting on a cell face, so it lands in one cell only. */
41
+ const CENTRE_TOLERANCE = 1e-6;
42
+
43
+ /**
44
+ * Complete the molecules of one cell.
45
+ * @param atoms - Every atom of one cell, fractional, wrapped into [0, 1).
46
+ * @param lattice - The cell they sit in.
47
+ * @param bondLimit - When two atoms are bonded.
48
+ * @returns The atoms, molecules whole; the input unchanged when it holds more
49
+ * than {@link MAX_PACKED_ATOMS}.
50
+ */
51
+ export function packMolecules(
52
+ atoms: readonly PackedAtom[],
53
+ lattice: Lattice,
54
+ bondLimit: BondLimit,
55
+ ): PackingResult {
56
+ const count = atoms.length;
57
+ if (count === 0 || count > MAX_PACKED_ATOMS) {
58
+ return { atoms, molecules: 0, extended: false };
59
+ }
60
+ const graph = bondGraph(count, findPeriodicBonds(atoms, lattice, bondLimit));
61
+ const { components, shifts } = walkComponents(graph);
62
+ const packed: PackedAtom[] = [];
63
+ let molecules = 0;
64
+ let extended = false;
65
+ for (const component of components) {
66
+ if (component.periodicity > 0) {
67
+ extended = true;
68
+ for (const index of component.atoms) {
69
+ packed.push(atoms[index] as PackedAtom);
70
+ }
71
+ continue;
72
+ }
73
+ if (component.atoms.length > 1) molecules++;
74
+ pushMolecule(packed, atoms, component.atoms, shifts);
75
+ }
76
+ return { atoms: packed, molecules, extended };
77
+ }
78
+
79
+ /**
80
+ * Add one molecule, moved whole to the cell its centre falls in.
81
+ * @param packed - The atoms placed so far, appended to.
82
+ * @param atoms - The atoms of the cell.
83
+ * @param members - The atoms of the molecule.
84
+ * @param shifts - The cell each atom was reached in.
85
+ */
86
+ function pushMolecule(
87
+ packed: PackedAtom[],
88
+ atoms: readonly PackedAtom[],
89
+ members: readonly number[],
90
+ shifts: Int32Array,
91
+ ): void {
92
+ const centre = [0, 0, 0];
93
+ for (const index of members) {
94
+ const position = (atoms[index] as PackedAtom).position;
95
+ for (let axis = 0; axis < 3; axis++) {
96
+ centre[axis] =
97
+ (centre[axis] ?? 0) +
98
+ (position[axis] ?? 0) +
99
+ (shifts[index * 3 + axis] ?? 0);
100
+ }
101
+ }
102
+ const move: [number, number, number] = [0, 0, 0];
103
+ for (let axis = 0; axis < 3; axis++) {
104
+ const mean = (centre[axis] ?? 0) / members.length;
105
+ move[axis] = -Math.floor(mean + CENTRE_TOLERANCE);
106
+ }
107
+ for (const index of members) {
108
+ const atom = atoms[index] as PackedAtom;
109
+ const position: [number, number, number] = [0, 0, 0];
110
+ for (let axis = 0; axis < 3; axis++) {
111
+ const shift = shifts[index * 3 + axis] ?? 0;
112
+ position[axis] = (atom.position[axis] ?? 0) + shift + (move[axis] ?? 0);
113
+ }
114
+ packed.push({ element: atom.element, position });
115
+ }
116
+ }