matsci-parse 0.1.0 → 0.1.2

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 +136 -110
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -9,7 +9,7 @@ class x {
9
9
  this.props[e] = t;
10
10
  }
11
11
  }
12
- function T(s) {
12
+ function C(s) {
13
13
  return s.sites.some((e) => Array.isArray(e.props.selectiveDynamics));
14
14
  }
15
15
  function E(s) {
@@ -20,15 +20,15 @@ function $(s, e) {
20
20
  (t) => t[0] * e[0] + t[1] * e[1] + t[2] * e[2]
21
21
  );
22
22
  }
23
- function L(s, e, t, n, o, i) {
24
- const c = E(n), l = E(o), f = E(i), d = [s, 0, 0], u = [
25
- e * Math.cos(f),
26
- e * Math.sin(f),
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),
27
27
  0
28
- ], p = t * Math.cos(l), m = t * (Math.cos(c) - Math.cos(l) * Math.cos(f)) / Math.sin(f), _ = Math.sqrt(t * t - p * p - m * m);
29
- return [d, u, [p, m, _]];
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]];
30
30
  }
31
- function C(s, e) {
31
+ function L(s, e) {
32
32
  const [t, n, o] = e;
33
33
  return [
34
34
  s[0] * t[0] + s[1] * n[0] + s[2] * o[0],
@@ -169,11 +169,11 @@ class w {
169
169
  const n = this.lattice.map(
170
170
  (i) => $(t, i)
171
171
  ), o = this.sites.map((i) => {
172
- const c = $(
172
+ const a = $(
173
173
  t,
174
174
  i.cart
175
175
  );
176
- return new x(i.speciesIndex, c, i.props);
176
+ return new x(i.speciesIndex, a, i.props);
177
177
  });
178
178
  return new w({
179
179
  lattice: n,
@@ -237,23 +237,23 @@ function F(s) {
237
237
  return s.split(/\r?\n/).map((e) => e.trim()).filter(Boolean);
238
238
  }
239
239
  function P(s, e = 6) {
240
- const t = [], n = T(s);
241
- t.push("Generated by matsci-parse"), t.push("1.0"), s.lattice.forEach((c) => {
242
- t.push(c.map((l) => l.toFixed(e)).join(" "));
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(" "));
243
243
  });
244
244
  const o = Array.from(
245
- new Set(s.sites.map((c) => s.species[c.speciesIndex]))
245
+ new Set(s.sites.map((a) => s.species[a.speciesIndex]))
246
246
  ), i = o.map(
247
- (c) => s.sites.filter((l) => s.species[l.speciesIndex] === c).length
247
+ (a) => s.sites.filter((c) => s.species[c.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((c) => {
250
- s.sites.filter((l) => s.species[l.speciesIndex] === c).forEach((l) => {
251
- const f = l.cart.map((d) => d.toFixed(e)).join(" ");
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
252
  if (n) {
253
- const u = (Array.isArray(l.props?.selectiveDynamics) ? l.props.selectiveDynamics : [!0, !0, !0]).map((p) => p ? "T" : "F").join(" ");
254
- t.push(`${f} ${u}`);
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}`);
255
255
  } else
256
- t.push(f);
256
+ t.push(h);
257
257
  });
258
258
  }), t.join(`
259
259
  `);
@@ -262,35 +262,35 @@ function j(s) {
262
262
  let e = 0;
263
263
  e++;
264
264
  const t = parseFloat(s[e++]), n = [];
265
- for (let p = 0; p < 3; p++)
265
+ for (let u = 0; u < 3; u++)
266
266
  n.push(
267
- s[e++].split(/\s+/).map((m) => parseFloat(m) * t)
267
+ s[e++].split(/\s+/).map((l) => parseFloat(l) * t)
268
268
  );
269
269
  let o = s[e].trim(), i;
270
270
  /^[A-Za-z]/.test(o) && (i = o.split(/\s+/), e++);
271
- const c = s[e++].split(/\s+/).map(Number);
272
- i || (i = c.map((p, m) => `X${m + 1}`));
273
- let l = !1;
274
- s[e]?.toLowerCase().startsWith("s") && (l = !0, e++);
275
- const d = s[e++].toLowerCase().startsWith("d"), u = [];
276
- return i.forEach((p, m) => {
277
- for (let _ = 0; _ < c[m]; _++) {
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
278
  const r = s[e++].split(/\s+/).map(Number);
279
- let h;
280
- d ? h = [
279
+ let f;
280
+ m ? f = [
281
281
  r[0] * n[0][0] + r[1] * n[1][0] + r[2] * n[2][0],
282
282
  r[0] * n[0][1] + r[1] * n[1][1] + r[2] * n[2][1],
283
283
  r[0] * n[0][2] + r[1] * n[1][2] + r[2] * n[2][2]
284
- ] : h = r.slice(0, 3);
285
- const y = {};
286
- if (l) {
287
- const g = r.slice(3, 6).map((a) => a === 1);
288
- g.length === 3 && (y.selectiveDynamics = g);
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);
289
289
  }
290
- u.push(new x(m, h, y));
290
+ p.push(new x(l, f, g));
291
291
  }
292
292
  }), {
293
- structure: new w({ lattice: n, species: i, sites: u }),
293
+ structure: new w({ lattice: n, species: i, sites: p }),
294
294
  linesConsumed: e
295
295
  };
296
296
  }
@@ -298,7 +298,7 @@ function R(s) {
298
298
  const e = F(s);
299
299
  return j(e).structure;
300
300
  }
301
- function O(s, e = 6) {
301
+ function W(s, e = 6) {
302
302
  const t = [];
303
303
  return t.push("CRYSTAL"), t.push("PRIMVEC"), s.lattice.forEach((n) => {
304
304
  t.push(n.map((o) => o.toFixed(e)).join(" "));
@@ -310,29 +310,29 @@ function O(s, e = 6) {
310
310
  }), t.join(`
311
311
  `);
312
312
  }
313
- function W(s) {
313
+ function O(s) {
314
314
  const e = F(s), t = [], n = [], o = [];
315
315
  let i = 0;
316
316
  for (; i < e.length && e[i].toUpperCase() !== "PRIMVEC"; ) i++;
317
317
  if (i === e.length) throw new Error("PRIMVEC block not found in XSF");
318
318
  i++;
319
- for (let l = 0; l < 3; l++)
319
+ for (let c = 0; c < 3; c++)
320
320
  t.push(e[i++].split(/\s+/).map(Number));
321
321
  for (; i < e.length && e[i].toUpperCase() !== "PRIMCOORD"; ) i++;
322
322
  if (i === e.length) throw new Error("PRIMCOORD block not found in XSF");
323
323
  i++;
324
- const [c] = e[i++].split(/\s+/).map(Number);
325
- for (let l = 0; l < c; l++) {
326
- const f = e[i++].split(/\s+/), d = f[0], u = f.slice(1, 4).map(Number);
327
- let p = n.indexOf(d);
328
- p === -1 && (n.push(d), p = n.length - 1), o.push(new x(p, u));
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));
329
329
  }
330
330
  return new w({ lattice: t, species: n, sites: o });
331
331
  }
332
332
  function X(s, e = 6) {
333
333
  const t = [], n = s.sites.length;
334
334
  t.push(String(n));
335
- const o = T(s), i = s.lattice, c = [
335
+ const o = C(s), i = s.lattice, a = [
336
336
  i[0][0],
337
337
  i[1][0],
338
338
  i[2][0],
@@ -342,20 +342,20 @@ function X(s, e = 6) {
342
342
  i[0][2],
343
343
  i[1][2],
344
344
  i[2][2]
345
- ].join(" "), l = [
345
+ ].join(" "), c = [
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="${c}" Properties=${l}`);
351
- for (const f of s.sites) {
352
- const d = s.species[f.speciesIndex], [u, p, m] = f.cart;
353
- let _ = `${d} ${u.toFixed(e)} ${p.toFixed(e)} ${m.toFixed(e)}`;
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)}`;
354
354
  if (o) {
355
- const r = Array.isArray(f.props?.selectiveDynamics) ? f.props.selectiveDynamics : [!0, !0, !0];
356
- _ += " " + r.map((h) => h ? "T" : "F").join(" ");
355
+ const r = Array.isArray(h.props?.selectiveDynamics) ? h.props.selectiveDynamics : [!0, !0, !0];
356
+ d += " " + r.map((f) => f ? "T" : "F").join(" ");
357
357
  }
358
- t.push(_);
358
+ t.push(d);
359
359
  }
360
360
  return t.join(`
361
361
  `);
@@ -366,43 +366,43 @@ function N(s) {
366
366
  const n = parseInt(e[t++], 10);
367
367
  if (!Number.isFinite(n))
368
368
  throw new Error("Invalid XYZ: first line must be atom count");
369
- const o = e[t++], i = {}, c = /(\w+)=(".*?"|\S+)/g;
370
- for (const r of o.matchAll(c)) {
371
- let h = r[2];
372
- h.startsWith('"') && (h = h.slice(1, -1)), i[r[1]] = h;
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;
373
373
  }
374
- let l;
374
+ let c;
375
375
  if (i.Lattice) {
376
376
  const r = i.Lattice.split(/\s+/).map(Number);
377
377
  if (r.length !== 9) throw new Error("Invalid Lattice in extended XYZ");
378
- l = [
378
+ c = [
379
379
  [r[0], r[3], r[6]],
380
380
  [r[1], r[4], r[7]],
381
381
  [r[2], r[5], r[8]]
382
382
  ];
383
383
  } else
384
384
  throw new Error("Lattice must be present in extended XYZ format");
385
- let f = 0, d = 1, u = null;
385
+ let h = 0, m = 1, p = null;
386
386
  if (i.Properties) {
387
387
  const r = i.Properties.split(":");
388
- let h = 0;
389
- for (let y = 0; y < r.length; y += 3) {
390
- const g = r[y], a = parseInt(r[y + 2], 10);
391
- g === "species" && (f = h), g === "pos" && (d = h), g === "selectiveDynamics" && (u = h), h += a;
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 += _;
392
392
  }
393
393
  }
394
- const p = [], m = /* @__PURE__ */ new Map(), _ = [];
394
+ const u = [], l = /* @__PURE__ */ new Map(), d = [];
395
395
  for (let r = 0; r < n; r++) {
396
- const h = e[t++].split(/\s+/), y = h[f];
397
- m.has(y) || (m.set(y, p.length), p.push(y));
398
- const g = m.get(y), a = h.slice(d, d + 3).map(Number), S = {};
399
- if (u !== null) {
400
- const I = h.slice(u, u + 3).map((b) => b.toUpperCase() === "T");
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
401
  I.length === 3 && (S.selectiveDynamics = I);
402
402
  }
403
- _.push(new x(g, a, S));
403
+ d.push(new x(y, _, S));
404
404
  }
405
- return new w({ lattice: l, species: p, sites: _ });
405
+ return new w({ lattice: c, species: u, sites: d });
406
406
  }
407
407
  function v(s, e) {
408
408
  return s[0] * e[0] + s[1] * e[1] + s[2] * e[2];
@@ -417,7 +417,7 @@ function D(s, e) {
417
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
418
  if (Math.abs(i) < 1e-12)
419
419
  throw new Error("Singular lattice matrix");
420
- const c = [
420
+ const a = [
421
421
  [
422
422
  (n[1] * o[2] - n[2] * o[1]) / i,
423
423
  (t[2] * o[1] - t[1] * o[2]) / i,
@@ -435,23 +435,23 @@ function D(s, e) {
435
435
  ]
436
436
  ];
437
437
  return [
438
- s[0] * c[0][0] + s[1] * c[1][0] + s[2] * c[2][0],
439
- s[0] * c[0][1] + s[1] * c[1][1] + s[2] * c[2][1],
440
- s[0] * c[0][2] + s[1] * c[1][2] + s[2] * c[2][2]
438
+ s[0] * a[0][0] + s[1] * a[1][0] + s[2] * a[2][0],
439
+ s[0] * a[0][1] + s[1] * a[1][1] + s[2] * a[2][1],
440
+ s[0] * a[0][2] + s[1] * a[1][2] + s[2] * a[2][2]
441
441
  ];
442
442
  }
443
443
  function z(s, e = 6) {
444
- const [t, n, o] = s.lattice, i = A(t), c = A(n), l = A(o), f = M(Math.acos(v(n, o) / (c * l))), d = M(Math.acos(v(t, o) / (i * l))), u = M(Math.acos(v(t, n) / (i * c)));
445
- let p = `data_made_with_matsci-parse
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
446
446
  _symmetry_space_group_name_H-M 'P 1'
447
447
  _symmetry_Int_Tables_number 1
448
448
 
449
449
  _cell_length_a ${i.toFixed(e)}
450
- _cell_length_b ${c.toFixed(e)}
451
- _cell_length_c ${l.toFixed(e)}
452
- _cell_angle_alpha ${f.toFixed(e)}
453
- _cell_angle_beta ${d.toFixed(e)}
454
- _cell_angle_gamma ${u.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)}
455
455
 
456
456
  loop_
457
457
  _atom_site_label
@@ -460,34 +460,60 @@ function z(s, e = 6) {
460
460
  _atom_site_fract_y
461
461
  _atom_site_fract_z
462
462
  `;
463
- return s.sites.forEach((m, _) => {
464
- const r = s.species[m.speciesIndex], h = D(m.cart, s.lattice);
465
- p += `${r}${_ + 1} ${r} ${h[0].toFixed(e)} ${h[1].toFixed(
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(
466
466
  6
467
- )} ${h[2].toFixed(e)}
467
+ )} ${f[2].toFixed(e)}
468
468
  `;
469
- }), p;
469
+ }), u;
470
470
  }
471
471
  function V(s) {
472
472
  const e = F(s);
473
- let t = 0, n = 0, o = 0, i = 0, c = 0, l = 0;
474
- const f = [];
475
- let d = !1, u = [];
476
- for (const a of e)
477
- a.startsWith("_cell_length_a") ? t = parseFloat(a.split(/\s+/)[1]) : a.startsWith("_cell_length_b") ? n = parseFloat(a.split(/\s+/)[1]) : a.startsWith("_cell_length_c") ? o = parseFloat(a.split(/\s+/)[1]) : a.startsWith("_cell_angle_alpha") ? i = parseFloat(a.split(/\s+/)[1]) : a.startsWith("_cell_angle_beta") ? c = parseFloat(a.split(/\s+/)[1]) : a.startsWith("_cell_angle_gamma") ? l = parseFloat(a.split(/\s+/)[1]) : a.startsWith("loop_") ? (d = !0, u = []) : d && a.startsWith("_") ? u.push(a) : d && a && !a.startsWith("_") && f.push(a.split(/\s+/));
478
- const p = L(t, n, o, i, c, l), m = [], _ = [], r = u.findIndex((a) => a.includes("fract_x")), h = u.findIndex((a) => a.includes("fract_y")), y = u.findIndex((a) => a.includes("fract_z")), g = u.findIndex((a) => a.includes("type_symbol"));
479
- if (r < 0 || h < 0 || y < 0 || g < 0)
480
- throw new Error("CIF missing required _atom_site columns");
481
- for (const a of f) {
482
- const S = [
483
- parseFloat(a[r]),
484
- parseFloat(a[h]),
485
- parseFloat(a[y])
486
- ], I = C(S, p);
487
- let b = m.indexOf(a[g]);
488
- b === -1 && (b = m.length, m.push(a[g])), _.push(new x(b, I));
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
+ }
510
+ continue;
511
+ }
512
+ }
513
+ p++;
489
514
  }
490
- return new w({ lattice: p, species: m, sites: _ });
515
+ const u = T(t, n, o, i, a, c);
516
+ return new w({ lattice: u, species: h, sites: m });
491
517
  }
492
518
  export {
493
519
  w as CrystalStructure,
@@ -496,8 +522,8 @@ export {
496
522
  R as poscarToStructure,
497
523
  z as structureToCif,
498
524
  P as structureToPoscar,
499
- O as structureToXsf,
525
+ W as structureToXsf,
500
526
  X as structureToXyz,
501
- W as xsfToStructure,
527
+ O as xsfToStructure,
502
528
  N as xyzToStructure
503
529
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "matsci-parse",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "module": "dist/main.js",