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.
- package/dist/main.js +136 -110
- 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
|
|
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
|
|
24
|
-
const
|
|
25
|
-
e * Math.cos(
|
|
26
|
-
e * Math.sin(
|
|
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
|
-
],
|
|
29
|
-
return [
|
|
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
|
|
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
|
|
172
|
+
const a = $(
|
|
173
173
|
t,
|
|
174
174
|
i.cart
|
|
175
175
|
);
|
|
176
|
-
return new x(i.speciesIndex,
|
|
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 =
|
|
241
|
-
t.push("Generated by matsci-parse"), t.push("1.0"), s.lattice.forEach((
|
|
242
|
-
t.push(
|
|
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((
|
|
245
|
+
new Set(s.sites.map((a) => s.species[a.speciesIndex]))
|
|
246
246
|
), i = o.map(
|
|
247
|
-
(
|
|
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((
|
|
250
|
-
s.sites.filter((
|
|
251
|
-
const
|
|
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
|
|
254
|
-
t.push(`${
|
|
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(
|
|
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
|
|
265
|
+
for (let u = 0; u < 3; u++)
|
|
266
266
|
n.push(
|
|
267
|
-
s[e++].split(/\s+/).map((
|
|
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
|
|
272
|
-
i || (i =
|
|
273
|
-
let
|
|
274
|
-
s[e]?.toLowerCase().startsWith("s") && (
|
|
275
|
-
const
|
|
276
|
-
return i.forEach((
|
|
277
|
-
for (let
|
|
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
|
|
280
|
-
|
|
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
|
-
] :
|
|
285
|
-
const
|
|
286
|
-
if (
|
|
287
|
-
const
|
|
288
|
-
|
|
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
|
-
|
|
290
|
+
p.push(new x(l, f, g));
|
|
291
291
|
}
|
|
292
292
|
}), {
|
|
293
|
-
structure: new w({ lattice: n, species: i, sites:
|
|
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
|
|
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
|
|
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
|
|
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 [
|
|
325
|
-
for (let
|
|
326
|
-
const
|
|
327
|
-
let
|
|
328
|
-
|
|
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 =
|
|
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(" "),
|
|
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="${
|
|
351
|
-
for (const
|
|
352
|
-
const
|
|
353
|
-
let
|
|
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(
|
|
356
|
-
|
|
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 = {},
|
|
370
|
-
for (const r of o.matchAll(
|
|
371
|
-
let
|
|
372
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
385
|
+
let h = 0, m = 1, p = null;
|
|
386
386
|
if (i.Properties) {
|
|
387
387
|
const r = i.Properties.split(":");
|
|
388
|
-
let
|
|
389
|
-
for (let
|
|
390
|
-
const
|
|
391
|
-
|
|
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
|
|
394
|
+
const u = [], l = /* @__PURE__ */ new Map(), d = [];
|
|
395
395
|
for (let r = 0; r < n; r++) {
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
const
|
|
399
|
-
if (
|
|
400
|
-
const I =
|
|
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
|
-
|
|
403
|
+
d.push(new x(y, _, S));
|
|
404
404
|
}
|
|
405
|
-
return new w({ lattice:
|
|
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
|
|
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] *
|
|
439
|
-
s[0] *
|
|
440
|
-
s[0] *
|
|
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),
|
|
445
|
-
let
|
|
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 ${
|
|
451
|
-
_cell_length_c ${
|
|
452
|
-
_cell_angle_alpha ${
|
|
453
|
-
_cell_angle_beta ${
|
|
454
|
-
_cell_angle_gamma ${
|
|
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((
|
|
464
|
-
const r = s.species[
|
|
465
|
-
|
|
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
|
-
)} ${
|
|
467
|
+
)} ${f[2].toFixed(e)}
|
|
468
468
|
`;
|
|
469
|
-
}),
|
|
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,
|
|
474
|
-
const
|
|
475
|
-
let
|
|
476
|
-
for (
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
parseFloat(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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
|
-
|
|
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
|
-
|
|
525
|
+
W as structureToXsf,
|
|
500
526
|
X as structureToXyz,
|
|
501
|
-
|
|
527
|
+
O as xsfToStructure,
|
|
502
528
|
N as xyzToStructure
|
|
503
529
|
};
|