matsci-parse 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/main.js +368 -304
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -1,42 +1,42 @@
1
1
  class x {
2
- constructor(e, t, n = {}) {
3
- this.speciesIndex = e, this.cart = t, this.props = n;
2
+ constructor(t, e, i = {}) {
3
+ this.speciesIndex = t, this.cart = e, this.props = i;
4
4
  }
5
- getProp(e) {
6
- return this.props[e];
5
+ getProp(t) {
6
+ return this.props[t];
7
7
  }
8
- setProp(e, t) {
9
- this.props[e] = t;
8
+ setProp(t, e) {
9
+ this.props[t] = e;
10
10
  }
11
11
  }
12
12
  function C(s) {
13
- return s.sites.some((e) => Array.isArray(e.props.selectiveDynamics));
13
+ return s.sites.some((t) => Array.isArray(t.props.selectiveDynamics));
14
14
  }
15
- function E(s) {
15
+ function F(s) {
16
16
  return s * Math.PI / 180;
17
17
  }
18
- function $(s, e) {
18
+ function M(s, t) {
19
19
  return s.map(
20
- (t) => t[0] * e[0] + t[1] * e[1] + t[2] * e[2]
20
+ (e) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2]
21
21
  );
22
22
  }
23
- function T(s, e, t, n, o, i) {
24
- const a = E(n), c = E(o), h = E(i), m = [s, 0, 0], p = [
25
- e * Math.cos(h),
26
- e * Math.sin(h),
23
+ function P(s, t, e, i, o, n) {
24
+ const a = F(i), h = F(o), l = F(n), p = [s, 0, 0], f = [
25
+ t * Math.cos(l),
26
+ t * Math.sin(l),
27
27
  0
28
- ], u = t * Math.cos(c), l = t * (Math.cos(a) - Math.cos(c) * Math.cos(h)) / Math.sin(h), d = Math.sqrt(t * t - u * u - l * l);
29
- return [m, p, [u, l, d]];
28
+ ], r = e * Math.cos(h), u = e * (Math.cos(a) - Math.cos(h) * Math.cos(l)) / Math.sin(l), g = Math.sqrt(e * e - r * r - u * u);
29
+ return [p, f, [r, u, g]];
30
30
  }
31
- function L(s, e) {
32
- const [t, n, o] = e;
31
+ function O(s, t) {
32
+ const [e, i, o] = t;
33
33
  return [
34
- s[0] * t[0] + s[1] * n[0] + s[2] * o[0],
35
- s[0] * t[1] + s[1] * n[1] + s[2] * o[1],
36
- s[0] * t[2] + s[1] * n[2] + s[2] * o[2]
34
+ s[0] * e[0] + s[1] * i[0] + s[2] * o[0],
35
+ s[0] * e[1] + s[1] * i[1] + s[2] * o[1],
36
+ s[0] * e[2] + s[1] * i[2] + s[2] * o[2]
37
37
  ];
38
38
  }
39
- class w {
39
+ class S {
40
40
  /**
41
41
  * Creates a new CrystalStructure.
42
42
  *
@@ -48,11 +48,11 @@ class w {
48
48
  * @throws Will throw an error if lattice is not 3x3, species is not an array of strings, or sites are invalid
49
49
  */
50
50
  constructor({
51
- lattice: e,
52
- species: t,
53
- sites: n
51
+ lattice: t,
52
+ species: e,
53
+ sites: i
54
54
  }) {
55
- this.lattice = this._validateLattice(e), this.species = this._validateSpecies(t), this.sites = this._validateSites(n);
55
+ this.lattice = this._validateLattice(t), this.species = this._validateSpecies(e), this.sites = this._validateSites(i);
56
56
  }
57
57
  // ---------- Simple methods ----------
58
58
  /** Number of sites in the structure */
@@ -63,22 +63,22 @@ class w {
63
63
  * Returns the site at the specified index.
64
64
  * @param i - Site index
65
65
  */
66
- site(e) {
67
- return this.sites[e];
66
+ site(t) {
67
+ return this.sites[t];
68
68
  }
69
69
  /**
70
70
  * Returns the Cartesian coordinates of a site.
71
71
  * @param i - Site index
72
72
  */
73
- cartCoords(e) {
74
- return this.sites[e].cart;
73
+ cartCoords(t) {
74
+ return this.sites[t].cart;
75
75
  }
76
76
  /**
77
77
  * Returns the chemical species of a site.
78
78
  * @param i - Site index
79
79
  */
80
- siteSpecies(e) {
81
- return this.species[this.sites[e].speciesIndex];
80
+ siteSpecies(t) {
81
+ return this.species[this.sites[t].speciesIndex];
82
82
  }
83
83
  /** Returns the array of elements (species) in the structure */
84
84
  get elements() {
@@ -92,11 +92,11 @@ class w {
92
92
  * @param cart - Cartesian coordinates of the new site
93
93
  * @param position - Optional index at which to insert the site. Defaults to appending at the end.
94
94
  */
95
- addSite(e, t, n) {
96
- let o = this.species.indexOf(e);
97
- o === -1 && (this.species.push(e), o = this.species.length - 1);
98
- const i = new x(o, t);
99
- n === void 0 || n >= this.sites.length ? this.sites.push(i) : n < 0 ? this.sites.unshift(i) : this.sites.splice(n, 0, i);
95
+ addSite(t, e, i) {
96
+ let o = this.species.indexOf(t);
97
+ o === -1 && (this.species.push(t), o = this.species.length - 1);
98
+ const n = new x(o, e);
99
+ i === void 0 || i >= this.sites.length ? this.sites.push(n) : i < 0 ? this.sites.unshift(n) : this.sites.splice(i, 0, n);
100
100
  }
101
101
  /**
102
102
  * Removes a site from the crystal.
@@ -107,14 +107,14 @@ class w {
107
107
  *
108
108
  * @throws Will throw an error if index is out of bounds
109
109
  */
110
- removeSite(e) {
111
- if (e < 0 || e >= this.sites.length)
110
+ removeSite(t) {
111
+ if (t < 0 || t >= this.sites.length)
112
112
  throw new Error("siteIndex out of bounds");
113
- const n = this.sites.splice(e, 1)[0].speciesIndex;
113
+ const i = this.sites.splice(t, 1)[0].speciesIndex;
114
114
  this.sites.some(
115
- (i) => i.speciesIndex === n
116
- ) || (this.species.splice(n, 1), this.sites.forEach((i) => {
117
- i.speciesIndex > n && (i.speciesIndex -= 1);
115
+ (n) => n.speciesIndex === i
116
+ ) || (this.species.splice(i, 1), this.sites.forEach((n) => {
117
+ n.speciesIndex > i && (n.speciesIndex -= 1);
118
118
  }));
119
119
  }
120
120
  /**
@@ -127,11 +127,11 @@ class w {
127
127
  *
128
128
  * @throws Will throw an error if siteIndex is out of bounds
129
129
  */
130
- replaceSite(e, t) {
131
- if (e < 0 || e >= this.sites.length)
130
+ replaceSite(t, e) {
131
+ if (t < 0 || t >= this.sites.length)
132
132
  throw new Error("siteIndex out of bounds");
133
- const n = [...this.sites[e].cart];
134
- this.removeSite(e), this.addSite(t, n, e);
133
+ const i = [...this.sites[t].cart];
134
+ this.removeSite(t), this.addSite(e, i, t);
135
135
  }
136
136
  /**
137
137
  * Returns a new CrystalStructure transformed by a linear matrix.
@@ -146,37 +146,37 @@ class w {
146
146
  *
147
147
  * @throws Will throw an error if scale is not a number, a 3-element array, or a 3x3 matrix
148
148
  */
149
- applyLatticeTransformation(e) {
150
- let t;
151
- if (typeof e == "number")
152
- t = [
153
- [e, 0, 0],
154
- [0, e, 0],
155
- [0, 0, e]
149
+ applyLatticeTransformation(t) {
150
+ let e;
151
+ if (typeof t == "number")
152
+ e = [
153
+ [t, 0, 0],
154
+ [0, t, 0],
155
+ [0, 0, t]
156
156
  ];
157
- else if (Array.isArray(e) && e.length === 3 && e.every((i) => typeof i == "number"))
158
- t = [
159
- [e[0], 0, 0],
160
- [0, e[1], 0],
161
- [0, 0, e[2]]
157
+ else if (Array.isArray(t) && t.length === 3 && t.every((n) => typeof n == "number"))
158
+ e = [
159
+ [t[0], 0, 0],
160
+ [0, t[1], 0],
161
+ [0, 0, t[2]]
162
162
  ];
163
- else if (Array.isArray(e) && e.length === 3 && e.every((i) => Array.isArray(i) && i.length === 3))
164
- t = e;
163
+ else if (Array.isArray(t) && t.length === 3 && t.every((n) => Array.isArray(n) && n.length === 3))
164
+ e = t;
165
165
  else
166
166
  throw new Error(
167
167
  "Scale must be a number, a 3-element array, or a 3x3 matrix"
168
168
  );
169
- const n = this.lattice.map(
170
- (i) => $(t, i)
171
- ), o = this.sites.map((i) => {
172
- const a = $(
173
- t,
174
- i.cart
169
+ const i = this.lattice.map(
170
+ (n) => M(e, n)
171
+ ), o = this.sites.map((n) => {
172
+ const a = M(
173
+ e,
174
+ n.cart
175
175
  );
176
- return new x(i.speciesIndex, a, i.props);
176
+ return new x(n.speciesIndex, a, n.props);
177
177
  });
178
- return new w({
179
- lattice: n,
178
+ return new S({
179
+ lattice: i,
180
180
  species: [...this.species],
181
181
  sites: o
182
182
  });
@@ -186,38 +186,38 @@ class w {
186
186
  * Validates that the lattice is a 3x3 array of Cartesian vectors.
187
187
  * @throws Will throw an error if lattice is invalid
188
188
  */
189
- _validateLattice(e) {
190
- if (!Array.isArray(e) || e.length !== 3)
189
+ _validateLattice(t) {
190
+ if (!Array.isArray(t) || t.length !== 3)
191
191
  throw new Error("Lattice must be a 3x3 matrix");
192
- return e.forEach((t) => {
193
- if (!Array.isArray(t) || t.length !== 3)
192
+ return t.forEach((e) => {
193
+ if (!Array.isArray(e) || e.length !== 3)
194
194
  throw new Error("Lattice vectors must be length 3");
195
- }), e;
195
+ }), t;
196
196
  }
197
197
  /**
198
198
  * Validates that species is an array of strings.
199
199
  * @throws Will throw an error if species array is invalid
200
200
  */
201
- _validateSpecies(e) {
202
- if (!Array.isArray(e)) throw new Error("Species must be an array");
203
- return e.forEach((t) => {
204
- if (typeof t != "string")
201
+ _validateSpecies(t) {
202
+ if (!Array.isArray(t)) throw new Error("Species must be an array");
203
+ return t.forEach((e) => {
204
+ if (typeof e != "string")
205
205
  throw new Error("Species entries must be strings");
206
- }), e;
206
+ }), t;
207
207
  }
208
208
  /**
209
209
  * Validates the sites array, ensuring each site is a Site instance or a valid object.
210
210
  * @throws Will throw an error if sites array is invalid
211
211
  */
212
- _validateSites(e) {
213
- if (!Array.isArray(e)) throw new Error("Sites must be an array");
214
- return e.map((t) => {
215
- if (t instanceof x) return t;
216
- if (typeof t == "object" && typeof t.speciesIndex == "number" && Array.isArray(t.cart) && t.cart.length === 3)
217
- return this._validateSpeciesIndex(t.speciesIndex), new x(
218
- t.speciesIndex,
219
- t.cart,
220
- t.props ?? {}
212
+ _validateSites(t) {
213
+ if (!Array.isArray(t)) throw new Error("Sites must be an array");
214
+ return t.map((e) => {
215
+ if (e instanceof x) return e;
216
+ if (typeof e == "object" && typeof e.speciesIndex == "number" && Array.isArray(e.cart) && e.cart.length === 3)
217
+ return this._validateSpeciesIndex(e.speciesIndex), new x(
218
+ e.speciesIndex,
219
+ e.cart,
220
+ e.props ?? {}
221
221
  );
222
222
  throw new Error(
223
223
  "Each site must be a Site instance or a valid site object"
@@ -228,210 +228,210 @@ class w {
228
228
  * Validates that a species index is within bounds.
229
229
  * @throws Will throw an error if index is out of range
230
230
  */
231
- _validateSpeciesIndex(e) {
232
- if (e < 0 || e >= this.species.length)
231
+ _validateSpeciesIndex(t) {
232
+ if (t < 0 || t >= this.species.length)
233
233
  throw new Error("speciesIndex out of bounds");
234
234
  }
235
235
  }
236
- function F(s) {
237
- return s.split(/\r?\n/).map((e) => e.trim()).filter(Boolean);
236
+ function E(s) {
237
+ return s.split(/\r?\n/).map((t) => t.trim()).filter(Boolean);
238
238
  }
239
- function P(s, e = 6) {
240
- const t = [], n = C(s);
241
- t.push("Generated by matsci-parse"), t.push("1.0"), s.lattice.forEach((a) => {
242
- t.push(a.map((c) => c.toFixed(e)).join(" "));
239
+ function D(s, t = 6) {
240
+ const e = [], i = C(s);
241
+ e.push("Generated by matsci-parse"), e.push("1.0"), s.lattice.forEach((a) => {
242
+ e.push(a.map((h) => h.toFixed(t)).join(" "));
243
243
  });
244
244
  const o = Array.from(
245
245
  new Set(s.sites.map((a) => s.species[a.speciesIndex]))
246
- ), i = o.map(
247
- (a) => s.sites.filter((c) => s.species[c.speciesIndex] === a).length
246
+ ), n = o.map(
247
+ (a) => s.sites.filter((h) => s.species[h.speciesIndex] === a).length
248
248
  );
249
- return t.push(o.join(" ")), t.push(i.join(" ")), n && t.push("Selective dynamics"), t.push("Cartesian"), o.forEach((a) => {
250
- s.sites.filter((c) => s.species[c.speciesIndex] === a).forEach((c) => {
251
- const h = c.cart.map((m) => m.toFixed(e)).join(" ");
252
- if (n) {
253
- const p = (Array.isArray(c.props?.selectiveDynamics) ? c.props.selectiveDynamics : [!0, !0, !0]).map((u) => u ? "T" : "F").join(" ");
254
- t.push(`${h} ${p}`);
249
+ return e.push(o.join(" ")), e.push(n.join(" ")), i && e.push("Selective dynamics"), e.push("Cartesian"), o.forEach((a) => {
250
+ s.sites.filter((h) => s.species[h.speciesIndex] === a).forEach((h) => {
251
+ const l = h.cart.map((p) => p.toFixed(t)).join(" ");
252
+ if (i) {
253
+ const f = (Array.isArray(h.props?.selectiveDynamics) ? h.props.selectiveDynamics : [!0, !0, !0]).map((r) => r ? "T" : "F").join(" ");
254
+ e.push(`${l} ${f}`);
255
255
  } else
256
- t.push(h);
256
+ e.push(l);
257
257
  });
258
- }), t.join(`
258
+ }), e.join(`
259
259
  `);
260
260
  }
261
- function j(s) {
262
- let e = 0;
263
- e++;
264
- const t = parseFloat(s[e++]), n = [];
265
- for (let u = 0; u < 3; u++)
266
- n.push(
267
- s[e++].split(/\s+/).map((l) => parseFloat(l) * t)
261
+ function R(s) {
262
+ let t = 0;
263
+ t++;
264
+ const e = parseFloat(s[t++]), i = [];
265
+ for (let r = 0; r < 3; r++)
266
+ i.push(
267
+ s[t++].split(/\s+/).map((u) => parseFloat(u) * e)
268
268
  );
269
- let o = s[e].trim(), i;
270
- /^[A-Za-z]/.test(o) && (i = o.split(/\s+/), e++);
271
- const a = s[e++].split(/\s+/).map(Number);
272
- i || (i = a.map((u, l) => `X${l + 1}`));
273
- let c = !1;
274
- s[e]?.toLowerCase().startsWith("s") && (c = !0, e++);
275
- const m = s[e++].toLowerCase().startsWith("d"), p = [];
276
- return i.forEach((u, l) => {
277
- for (let d = 0; d < a[l]; d++) {
278
- const r = s[e++].split(/\s+/).map(Number);
279
- let f;
280
- m ? f = [
281
- r[0] * n[0][0] + r[1] * n[1][0] + r[2] * n[2][0],
282
- r[0] * n[0][1] + r[1] * n[1][1] + r[2] * n[2][1],
283
- r[0] * n[0][2] + r[1] * n[1][2] + r[2] * n[2][2]
284
- ] : f = r.slice(0, 3);
285
- const g = {};
286
- if (c) {
287
- const y = r.slice(3, 6).map((_) => _ === 1);
288
- y.length === 3 && (g.selectiveDynamics = y);
269
+ let o = s[t].trim(), n;
270
+ /^[A-Za-z]/.test(o) && (n = o.split(/\s+/), t++);
271
+ const a = s[t++].split(/\s+/).map(Number);
272
+ n || (n = a.map((r, u) => `X${u + 1}`));
273
+ let h = !1;
274
+ s[t]?.toLowerCase().startsWith("s") && (h = !0, t++);
275
+ const p = s[t++].toLowerCase().startsWith("d"), f = [];
276
+ return n.forEach((r, u) => {
277
+ for (let g = 0; g < a[u]; g++) {
278
+ const c = s[t++].split(/\s+/).map(Number);
279
+ let m;
280
+ p ? m = [
281
+ c[0] * i[0][0] + c[1] * i[1][0] + c[2] * i[2][0],
282
+ c[0] * i[0][1] + c[1] * i[1][1] + c[2] * i[2][1],
283
+ c[0] * i[0][2] + c[1] * i[1][2] + c[2] * i[2][2]
284
+ ] : m = c.slice(0, 3);
285
+ const w = {};
286
+ if (h) {
287
+ const y = c.slice(3, 6).map((b) => b === 1);
288
+ y.length === 3 && (w.selectiveDynamics = y);
289
289
  }
290
- p.push(new x(l, f, g));
290
+ f.push(new x(u, m, w));
291
291
  }
292
292
  }), {
293
- structure: new w({ lattice: n, species: i, sites: p }),
294
- linesConsumed: e
293
+ structure: new S({ lattice: i, species: n, sites: f }),
294
+ linesConsumed: t
295
295
  };
296
296
  }
297
- function R(s) {
298
- const e = F(s);
299
- return j(e).structure;
297
+ function W(s) {
298
+ const t = E(s);
299
+ return R(t).structure;
300
300
  }
301
- function W(s, e = 6) {
302
- const t = [];
303
- return t.push("CRYSTAL"), t.push("PRIMVEC"), s.lattice.forEach((n) => {
304
- t.push(n.map((o) => o.toFixed(e)).join(" "));
305
- }), t.push("PRIMCOORD"), t.push(`${s.numSites} 1`), s.sites.forEach((n) => {
306
- const o = s.species[n.speciesIndex];
307
- t.push(
308
- `${o} ${n.cart.map((i) => i.toFixed(e)).join(" ")}`
301
+ function k(s, t = 6) {
302
+ const e = [];
303
+ return e.push("CRYSTAL"), e.push("PRIMVEC"), s.lattice.forEach((i) => {
304
+ e.push(i.map((o) => o.toFixed(t)).join(" "));
305
+ }), e.push("PRIMCOORD"), e.push(`${s.numSites} 1`), s.sites.forEach((i) => {
306
+ const o = s.species[i.speciesIndex];
307
+ e.push(
308
+ `${o} ${i.cart.map((n) => n.toFixed(t)).join(" ")}`
309
309
  );
310
- }), t.join(`
310
+ }), e.join(`
311
311
  `);
312
312
  }
313
- function O(s) {
314
- const e = F(s), t = [], n = [], o = [];
315
- let i = 0;
316
- for (; i < e.length && e[i].toUpperCase() !== "PRIMVEC"; ) i++;
317
- if (i === e.length) throw new Error("PRIMVEC block not found in XSF");
318
- i++;
319
- for (let c = 0; c < 3; c++)
320
- t.push(e[i++].split(/\s+/).map(Number));
321
- for (; i < e.length && e[i].toUpperCase() !== "PRIMCOORD"; ) i++;
322
- if (i === e.length) throw new Error("PRIMCOORD block not found in XSF");
323
- i++;
324
- const [a] = e[i++].split(/\s+/).map(Number);
325
- for (let c = 0; c < a; c++) {
326
- const h = e[i++].split(/\s+/), m = h[0], p = h.slice(1, 4).map(Number);
327
- let u = n.indexOf(m);
328
- u === -1 && (n.push(m), u = n.length - 1), o.push(new x(u, p));
313
+ function X(s) {
314
+ const t = E(s), e = [], i = [], o = [];
315
+ let n = 0;
316
+ for (; n < t.length && t[n].toUpperCase() !== "PRIMVEC"; ) n++;
317
+ if (n === t.length) throw new Error("PRIMVEC block not found in XSF");
318
+ n++;
319
+ for (let h = 0; h < 3; h++)
320
+ e.push(t[n++].split(/\s+/).map(Number));
321
+ for (; n < t.length && t[n].toUpperCase() !== "PRIMCOORD"; ) n++;
322
+ if (n === t.length) throw new Error("PRIMCOORD block not found in XSF");
323
+ n++;
324
+ const [a] = t[n++].split(/\s+/).map(Number);
325
+ for (let h = 0; h < a; h++) {
326
+ const l = t[n++].split(/\s+/), p = l[0], f = l.slice(1, 4).map(Number);
327
+ let r = i.indexOf(p);
328
+ r === -1 && (i.push(p), r = i.length - 1), o.push(new x(r, f));
329
329
  }
330
- return new w({ lattice: t, species: n, sites: o });
330
+ return new S({ lattice: e, species: i, sites: o });
331
331
  }
332
- function X(s, e = 6) {
333
- const t = [], n = s.sites.length;
334
- t.push(String(n));
335
- const o = C(s), i = s.lattice, a = [
336
- i[0][0],
337
- i[1][0],
338
- i[2][0],
339
- i[0][1],
340
- i[1][1],
341
- i[2][1],
342
- i[0][2],
343
- i[1][2],
344
- i[2][2]
345
- ].join(" "), c = [
332
+ function z(s, t = 6) {
333
+ const e = [], i = s.sites.length;
334
+ e.push(String(i));
335
+ const o = C(s), n = s.lattice, a = [
336
+ n[0][0],
337
+ n[1][0],
338
+ n[2][0],
339
+ n[0][1],
340
+ n[1][1],
341
+ n[2][1],
342
+ n[0][2],
343
+ n[1][2],
344
+ n[2][2]
345
+ ].join(" "), h = [
346
346
  "species:S:1",
347
347
  "pos:R:3",
348
348
  o ? "selectiveDynamics:L:3" : null
349
349
  ].filter(Boolean).join(":");
350
- t.push(`Lattice="${a}" Properties=${c}`);
351
- for (const h of s.sites) {
352
- const m = s.species[h.speciesIndex], [p, u, l] = h.cart;
353
- let d = `${m} ${p.toFixed(e)} ${u.toFixed(e)} ${l.toFixed(e)}`;
350
+ e.push(`Lattice="${a}" Properties=${h}`);
351
+ for (const l of s.sites) {
352
+ const p = s.species[l.speciesIndex], [f, r, u] = l.cart;
353
+ let g = `${p} ${f.toFixed(t)} ${r.toFixed(t)} ${u.toFixed(t)}`;
354
354
  if (o) {
355
- const r = Array.isArray(h.props?.selectiveDynamics) ? h.props.selectiveDynamics : [!0, !0, !0];
356
- d += " " + r.map((f) => f ? "T" : "F").join(" ");
355
+ const c = Array.isArray(l.props?.selectiveDynamics) ? l.props.selectiveDynamics : [!0, !0, !0];
356
+ g += " " + c.map((m) => m ? "T" : "F").join(" ");
357
357
  }
358
- t.push(d);
358
+ e.push(g);
359
359
  }
360
- return t.join(`
360
+ return e.join(`
361
361
  `);
362
362
  }
363
- function N(s) {
364
- const e = F(s);
365
- let t = 0;
366
- const n = parseInt(e[t++], 10);
367
- if (!Number.isFinite(n))
363
+ function Z(s) {
364
+ const t = E(s);
365
+ let e = 0;
366
+ const i = parseInt(t[e++], 10);
367
+ if (!Number.isFinite(i))
368
368
  throw new Error("Invalid XYZ: first line must be atom count");
369
- const o = e[t++], i = {}, a = /(\w+)=(".*?"|\S+)/g;
370
- for (const r of o.matchAll(a)) {
371
- let f = r[2];
372
- f.startsWith('"') && (f = f.slice(1, -1)), i[r[1]] = f;
369
+ const o = t[e++], n = {}, a = /(\w+)=(".*?"|\S+)/g;
370
+ for (const c of o.matchAll(a)) {
371
+ let m = c[2];
372
+ m.startsWith('"') && (m = m.slice(1, -1)), n[c[1]] = m;
373
373
  }
374
- let c;
375
- if (i.Lattice) {
376
- const r = i.Lattice.split(/\s+/).map(Number);
377
- if (r.length !== 9) throw new Error("Invalid Lattice in extended XYZ");
378
- c = [
379
- [r[0], r[3], r[6]],
380
- [r[1], r[4], r[7]],
381
- [r[2], r[5], r[8]]
374
+ let h;
375
+ if (n.Lattice) {
376
+ const c = n.Lattice.split(/\s+/).map(Number);
377
+ if (c.length !== 9) throw new Error("Invalid Lattice in extended XYZ");
378
+ h = [
379
+ [c[0], c[3], c[6]],
380
+ [c[1], c[4], c[7]],
381
+ [c[2], c[5], c[8]]
382
382
  ];
383
383
  } else
384
384
  throw new Error("Lattice must be present in extended XYZ format");
385
- let h = 0, m = 1, p = null;
386
- if (i.Properties) {
387
- const r = i.Properties.split(":");
388
- let f = 0;
389
- for (let g = 0; g < r.length; g += 3) {
390
- const y = r[g], _ = parseInt(r[g + 2], 10);
391
- y === "species" && (h = f), y === "pos" && (m = f), y === "selectiveDynamics" && (p = f), f += _;
385
+ let l = 0, p = 1, f = null;
386
+ if (n.Properties) {
387
+ const c = n.Properties.split(":");
388
+ let m = 0;
389
+ for (let w = 0; w < c.length; w += 3) {
390
+ const y = c[w], b = parseInt(c[w + 2], 10);
391
+ y === "species" && (l = m), y === "pos" && (p = m), y === "selectiveDynamics" && (f = m), m += b;
392
392
  }
393
393
  }
394
- const u = [], l = /* @__PURE__ */ new Map(), d = [];
395
- for (let r = 0; r < n; r++) {
396
- const f = e[t++].split(/\s+/), g = f[h];
397
- l.has(g) || (l.set(g, u.length), u.push(g));
398
- const y = l.get(g), _ = f.slice(m, m + 3).map(Number), S = {};
399
- if (p !== null) {
400
- const I = f.slice(p, p + 3).map((b) => b.toUpperCase() === "T");
401
- I.length === 3 && (S.selectiveDynamics = I);
394
+ const r = [], u = /* @__PURE__ */ new Map(), g = [];
395
+ for (let c = 0; c < i; c++) {
396
+ const m = t[e++].split(/\s+/), w = m[l];
397
+ u.has(w) || (u.set(w, r.length), r.push(w));
398
+ const y = u.get(w), b = m.slice(p, p + 3).map(Number), I = {};
399
+ if (f !== null) {
400
+ const _ = m.slice(f, f + 3).map((d) => d.toUpperCase() === "T");
401
+ _.length === 3 && (I.selectiveDynamics = _);
402
402
  }
403
- d.push(new x(y, _, S));
403
+ g.push(new x(y, b, I));
404
404
  }
405
- return new w({ lattice: c, species: u, sites: d });
405
+ return new S({ lattice: h, species: r, sites: g });
406
406
  }
407
- function v(s, e) {
408
- return s[0] * e[0] + s[1] * e[1] + s[2] * e[2];
407
+ function A(s, t) {
408
+ return s[0] * t[0] + s[1] * t[1] + s[2] * t[2];
409
409
  }
410
- function A(s) {
411
- return Math.sqrt(v(s, s));
410
+ function T(s) {
411
+ return Math.sqrt(A(s, s));
412
412
  }
413
- function M(s) {
413
+ function L(s) {
414
414
  return s * 180 / Math.PI;
415
415
  }
416
- function D(s, e) {
417
- const [t, n, o] = e, i = t[0] * (n[1] * o[2] - n[2] * o[1]) - t[1] * (n[0] * o[2] - n[2] * o[0]) + t[2] * (n[0] * o[1] - n[1] * o[0]);
418
- if (Math.abs(i) < 1e-12)
416
+ function j(s, t) {
417
+ const [e, i, o] = t, n = e[0] * (i[1] * o[2] - i[2] * o[1]) - e[1] * (i[0] * o[2] - i[2] * o[0]) + e[2] * (i[0] * o[1] - i[1] * o[0]);
418
+ if (Math.abs(n) < 1e-12)
419
419
  throw new Error("Singular lattice matrix");
420
420
  const a = [
421
421
  [
422
- (n[1] * o[2] - n[2] * o[1]) / i,
423
- (t[2] * o[1] - t[1] * o[2]) / i,
424
- (t[1] * n[2] - t[2] * n[1]) / i
422
+ (i[1] * o[2] - i[2] * o[1]) / n,
423
+ (e[2] * o[1] - e[1] * o[2]) / n,
424
+ (e[1] * i[2] - e[2] * i[1]) / n
425
425
  ],
426
426
  [
427
- (n[2] * o[0] - n[0] * o[2]) / i,
428
- (t[0] * o[2] - t[2] * o[0]) / i,
429
- (t[2] * n[0] - t[0] * n[2]) / i
427
+ (i[2] * o[0] - i[0] * o[2]) / n,
428
+ (e[0] * o[2] - e[2] * o[0]) / n,
429
+ (e[2] * i[0] - e[0] * i[2]) / n
430
430
  ],
431
431
  [
432
- (n[0] * o[1] - n[1] * o[0]) / i,
433
- (t[1] * o[0] - t[0] * o[1]) / i,
434
- (t[0] * n[1] - t[1] * n[0]) / i
432
+ (i[0] * o[1] - i[1] * o[0]) / n,
433
+ (e[1] * o[0] - e[0] * o[1]) / n,
434
+ (e[0] * i[1] - e[1] * i[0]) / n
435
435
  ]
436
436
  ];
437
437
  return [
@@ -440,18 +440,18 @@ function D(s, e) {
440
440
  s[0] * a[0][2] + s[1] * a[1][2] + s[2] * a[2][2]
441
441
  ];
442
442
  }
443
- function z(s, e = 6) {
444
- const [t, n, o] = s.lattice, i = A(t), a = A(n), c = A(o), h = M(Math.acos(v(n, o) / (a * c))), m = M(Math.acos(v(t, o) / (i * c))), p = M(Math.acos(v(t, n) / (i * a)));
445
- let u = `data_made_with_matsci-parse
443
+ function V(s, t = 6) {
444
+ const [e, i, o] = s.lattice, n = T(e), a = T(i), h = T(o), l = L(Math.acos(A(i, o) / (a * h))), p = L(Math.acos(A(e, o) / (n * h))), f = L(Math.acos(A(e, i) / (n * a)));
445
+ let r = `data_made_with_matsci-parse
446
446
  _symmetry_space_group_name_H-M 'P 1'
447
447
  _symmetry_Int_Tables_number 1
448
448
 
449
- _cell_length_a ${i.toFixed(e)}
450
- _cell_length_b ${a.toFixed(e)}
451
- _cell_length_c ${c.toFixed(e)}
452
- _cell_angle_alpha ${h.toFixed(e)}
453
- _cell_angle_beta ${m.toFixed(e)}
454
- _cell_angle_gamma ${p.toFixed(e)}
449
+ _cell_length_a ${n.toFixed(t)}
450
+ _cell_length_b ${a.toFixed(t)}
451
+ _cell_length_c ${h.toFixed(t)}
452
+ _cell_angle_alpha ${l.toFixed(t)}
453
+ _cell_angle_beta ${p.toFixed(t)}
454
+ _cell_angle_gamma ${f.toFixed(t)}
455
455
 
456
456
  loop_
457
457
  _atom_site_label
@@ -460,70 +460,134 @@ function z(s, e = 6) {
460
460
  _atom_site_fract_y
461
461
  _atom_site_fract_z
462
462
  `;
463
- return s.sites.forEach((l, d) => {
464
- const r = s.species[l.speciesIndex], f = D(l.cart, s.lattice);
465
- u += `${r}${d + 1} ${r} ${f[0].toFixed(e)} ${f[1].toFixed(
463
+ return s.sites.forEach((u, g) => {
464
+ const c = s.species[u.speciesIndex], m = j(u.cart, s.lattice);
465
+ r += `${c}${g + 1} ${c} ${m[0].toFixed(t)} ${m[1].toFixed(
466
466
  6
467
- )} ${f[2].toFixed(e)}
467
+ )} ${m[2].toFixed(t)}
468
468
  `;
469
- }), u;
469
+ }), r;
470
+ }
471
+ function U(s) {
472
+ const t = E(s);
473
+ let e = 0, i = 0, o = 0, n = 0, a = 0, h = 0, l = [];
474
+ const p = [];
475
+ let f = !1, r = [], u = !1;
476
+ for (const _ of t) {
477
+ const d = _.trim();
478
+ if (d) {
479
+ if (d.startsWith("_cell_length_a")) e = parseFloat(d.split(/\s+/)[1]);
480
+ else if (d.startsWith("_cell_length_b"))
481
+ i = parseFloat(d.split(/\s+/)[1]);
482
+ else if (d.startsWith("_cell_length_c"))
483
+ o = parseFloat(d.split(/\s+/)[1]);
484
+ else if (d.startsWith("_cell_angle_alpha"))
485
+ n = parseFloat(d.split(/\s+/)[1]);
486
+ else if (d.startsWith("_cell_angle_beta"))
487
+ a = parseFloat(d.split(/\s+/)[1]);
488
+ else if (d.startsWith("_cell_angle_gamma"))
489
+ h = parseFloat(d.split(/\s+/)[1]);
490
+ else if (d.startsWith("loop_")) {
491
+ f = !0, r = [], u = !1;
492
+ continue;
493
+ }
494
+ if (f && d.startsWith("_")) {
495
+ r.push(d), d.includes("_atom_site_") && (u = !0);
496
+ continue;
497
+ }
498
+ if (f && !d.startsWith("_")) {
499
+ u && (l.length === 0 && (l = [...r]), p.push(d.split(/\s+/)));
500
+ continue;
501
+ }
502
+ }
503
+ }
504
+ if (l.length === 0)
505
+ throw new Error("No _atom_site loop found in CIF");
506
+ const g = P(e, i, o, n, a, h), c = [], m = [], w = l.findIndex((_) => _.includes("fract_x")), y = l.findIndex((_) => _.includes("fract_y")), b = l.findIndex((_) => _.includes("fract_z")), I = l.findIndex((_) => _.includes("type_symbol"));
507
+ if (w < 0 || y < 0 || b < 0 || I < 0)
508
+ throw new Error("CIF missing required _atom_site columns");
509
+ for (const _ of p) {
510
+ const d = [
511
+ parseFloat(_[w]),
512
+ parseFloat(_[y]),
513
+ parseFloat(_[b])
514
+ ], $ = O(d, g);
515
+ let v = c.indexOf(_[I]);
516
+ v === -1 && (v = c.length, c.push(_[I])), m.push(new x(v, $));
517
+ }
518
+ return new S({ lattice: g, species: c, sites: m });
470
519
  }
471
- function V(s) {
472
- const e = F(s);
473
- let t = 0, n = 0, o = 0, i = 0, a = 0, c = 0;
474
- const h = [], m = [];
475
- let p = 0;
476
- for (; p < e.length; ) {
477
- const l = e[p].trim();
478
- if (l.startsWith("_cell_length_a")) t = parseFloat(l.split(/\s+/)[1]);
479
- else if (l.startsWith("_cell_length_b"))
480
- n = parseFloat(l.split(/\s+/)[1]);
481
- else if (l.startsWith("_cell_length_c"))
482
- o = parseFloat(l.split(/\s+/)[1]);
483
- else if (l.startsWith("_cell_angle_alpha"))
484
- i = parseFloat(l.split(/\s+/)[1]);
485
- else if (l.startsWith("_cell_angle_beta"))
486
- a = parseFloat(l.split(/\s+/)[1]);
487
- else if (l.startsWith("_cell_angle_gamma"))
488
- c = parseFloat(l.split(/\s+/)[1]);
489
- else if (l.startsWith("loop_")) {
490
- p++;
491
- const d = [];
492
- for (; p < e.length && e[p].trim().startsWith("_"); )
493
- d.push(e[p].trim()), p++;
494
- if (d.some((r) => r.includes("_atom_site_"))) {
495
- const r = d.findIndex((_) => _.includes("fract_x")), f = d.findIndex((_) => _.includes("fract_y")), g = d.findIndex((_) => _.includes("fract_z")), y = d.findIndex((_) => _.includes("type_symbol"));
496
- if (r < 0 || f < 0 || g < 0 || y < 0)
497
- throw new Error("CIF missing required _atom_site columns");
498
- for (; p < e.length && e[p].trim() && !e[p].trim().startsWith("_") && !e[p].trim().startsWith("loop_"); ) {
499
- const _ = e[p].trim().split(/\s+/), S = [
500
- parseFloat(_[r]),
501
- parseFloat(_[f]),
502
- parseFloat(_[g])
503
- ], I = L(
504
- S,
505
- T(t, n, o, i, a, c)
506
- );
507
- let b = h.indexOf(_[y]);
508
- b === -1 && (b = h.length, h.push(_[y])), m.push(new x(b, I)), p++;
509
- }
520
+ function N(s) {
521
+ let t = [];
522
+ const e = [];
523
+ for (let l = 0; l < s.length; l++) {
524
+ const p = s[l].trim();
525
+ if (!(!p || p.startsWith("&")) && /^CELL_PARAMETERS/i.test(p)) {
526
+ let f = 0, r = l + 1;
527
+ for (; f < 3 && r < s.length; ) {
528
+ const u = s[r].trim();
529
+ u !== "" && (t.push(u.split(/\s+/).map(Number)), f++), r++;
530
+ }
531
+ if (f < 3) throw new Error("Incomplete CELL_PARAMETERS block");
532
+ break;
533
+ }
534
+ }
535
+ if (t.length !== 3)
536
+ throw new Error("CELL_PARAMETERS block not found or incomplete");
537
+ let i = !1, o = "angstrom";
538
+ for (let l = 0; l < s.length; l++) {
539
+ const p = s[l].trim();
540
+ if (!(!p || p.startsWith("&"))) {
541
+ if (/^ATOMIC_POSITIONS/i.test(p)) {
542
+ i = !0;
543
+ const f = p.match(/^ATOMIC_POSITIONS\s+(\w+)/i);
544
+ o = f ? f[1].toLowerCase() : "angstrom";
510
545
  continue;
511
546
  }
547
+ if (i) {
548
+ const f = p.split(/\s+/);
549
+ if (f.length < 4 || !/^[A-Za-z]/.test(f[0]) || f.slice(1, 4).some((r) => Number.isNaN(Number(r))))
550
+ break;
551
+ e.push(p);
552
+ }
512
553
  }
513
- p++;
514
554
  }
515
- const u = T(t, n, o, i, a, c);
516
- return new w({ lattice: u, species: h, sites: m });
555
+ if (e.length === 0)
556
+ throw new Error("ATOMIC_POSITIONS block not found");
557
+ const n = o === "crystal", a = [];
558
+ for (const l of e) {
559
+ const p = l.split(/\s+/)[0];
560
+ a.includes(p) || a.push(p);
561
+ }
562
+ const h = [];
563
+ for (const l of e) {
564
+ const p = l.split(/\s+/), f = p[0], r = p.slice(1, 4).map(Number);
565
+ let u;
566
+ n ? u = [
567
+ r[0] * t[0][0] + r[1] * t[1][0] + r[2] * t[2][0],
568
+ r[0] * t[0][1] + r[1] * t[1][1] + r[2] * t[2][1],
569
+ r[0] * t[0][2] + r[1] * t[1][2] + r[2] * t[2][2]
570
+ ] : u = r, h.push(new x(a.indexOf(f), u));
571
+ }
572
+ return {
573
+ structure: new S({ lattice: t, species: a, sites: h }),
574
+ linesConsumed: s.length
575
+ };
576
+ }
577
+ function Y(s) {
578
+ const t = E(s);
579
+ return N(t).structure;
517
580
  }
518
581
  export {
519
- w as CrystalStructure,
582
+ S as CrystalStructure,
520
583
  x as Site,
521
- V as cifToStructure,
522
- R as poscarToStructure,
523
- z as structureToCif,
524
- P as structureToPoscar,
525
- W as structureToXsf,
526
- X as structureToXyz,
527
- O as xsfToStructure,
528
- N as xyzToStructure
584
+ U as cifToStructure,
585
+ W as poscarToStructure,
586
+ Y as pwToStructure,
587
+ V as structureToCif,
588
+ D as structureToPoscar,
589
+ k as structureToXsf,
590
+ z as structureToXyz,
591
+ X as xsfToStructure,
592
+ Z as xyzToStructure
529
593
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "matsci-parse",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "module": "dist/main.js",