From ac672c8857a84d19b9ebbc50f623c20d67db1c4d Mon Sep 17 00:00:00 2001 From: adewyer Date: Thu, 30 Dec 2021 15:09:13 -0500 Subject: [PATCH 01/12] Feat: xyz file parsing - nAtoms & nLines to check formatting. Created functions and tests --- src/parsers/xyz.js | 16 ++++++++++++++++ tests/enums.js | 2 ++ tests/fixtures/CH4.xyz | 3 +++ tests/parsers/xyz.js | 11 ++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/CH4.xyz diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index 20da6e54..8df1ab7e 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -1,3 +1,4 @@ +import Integer from "lodash/string"; import _ from "underscore"; import s from "underscore.string"; @@ -140,10 +141,25 @@ function fromMaterial(materialOrConfig, fractional = false) { return fromBasis(basis, "%11.6f"); } +export function atomsCount(xyzFile) { + const xyzArray = xyzFile.split(/\r?\n/); + return Integer.parseInt(xyzArray[0]); +} + +export function linesCount(xyzFile) { + const content = xyzFile.split(/\r?\n/); + if (!content[-1]) { + content.pop(); + } + return content.length; +} + export default { validate, fromMaterial, toBasisConfig, fromBasis, CombinatorialBasis, + atomsCount, + linesCount, }; diff --git a/tests/enums.js b/tests/enums.js index 1b876f27..e700d08f 100644 --- a/tests/enums.js +++ b/tests/enums.js @@ -31,3 +31,5 @@ export const SiPWSCFInput = readFile(path.join(FIXTURES_DIR, "Si-pwscf.in")); export const Zr1H23Zr1H1 = readJSONFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.json")); export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.poscar")); export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar")); + +export const CH4 = readFile(path.join(FIXTURES_DIR, "CH4.xyz")); diff --git a/tests/fixtures/CH4.xyz b/tests/fixtures/CH4.xyz new file mode 100644 index 00000000..e0ea7d3e --- /dev/null +++ b/tests/fixtures/CH4.xyz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78e5d267cfe21a127d63beef525912bcff256e5b955c77a944b1a1c59721c989 +size 203 diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index 6d685627..b086296d 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,5 +1,8 @@ +import { expect } from "chai"; + import parsers from "../../src/parsers/parsers"; -import { Si } from "../enums"; +import { atomsCount, linesCount } from "../../src/parsers/xyz"; +import { CH4, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; describe("Parsers:XYZ", () => { @@ -11,4 +14,10 @@ describe("Parsers:XYZ", () => { "units", ]); }); + it("should return the number of atoms for a molecule in an xyz file", () => { + expect(atomsCount(CH4)).to.be.equal(5); + }); + it("should return the number of lines in an xyz file", () => { + expect(linesCount(CH4)).to.be.equal(7); + }); }); From c06358a3277551b4e97e4a3dad79c62549b183fc Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 7 Jan 2022 09:15:56 -0800 Subject: [PATCH 02/12] Feat: updates to move all functions for dealing w/ parsing xyz file and converting to poscar to made instead of web-app --- src/parsers/parsers.js | 30 ++++++++++++++++++++++++++++++ src/parsers/xyz.js | 30 +++++++++++++++++++++++------- tests/enums.js | 1 + tests/fixtures/CH4.poscar | 3 +++ tests/parsers/xyz.js | 8 ++++---- 5 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/CH4.poscar diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 2e9924c3..88215c10 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,11 +1,41 @@ import cif from "./cif"; +// eslint-disable-next-line import/no-cycle import espresso from "./espresso"; import poscar from "./poscar"; +// eslint-disable-next-line import/no-cycle import xyz from "./xyz"; +/** + * Function splits the contents of a string at each newline and returns the resulting array. + * @param fileString + * @returns {Array} + */ +export function getFileStringAsArray(fileString) { + return fileString.split(/\r?\n/); +} + +/** + * Function returns the number of atoms in a file using the proper parser function based on the file extension. + * @param {String} fileExtension + * @param {String} fileContent + * @returns {Number} + */ +export function getAtomsInFileByExtension(fileExtension, fileContent) { + let numberOfAtoms = 0; + if (fileExtension === "poscar") { + numberOfAtoms = poscar.atomsCount(fileContent); + } + if (fileExtension === "xyz") { + numberOfAtoms = xyz.atomsCount(fileContent); + } + return numberOfAtoms; +} + export default { xyz, poscar, cif, espresso, + getFileStringAsArray, + getAtomsInFileByExtension, }; diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index 8df1ab7e..e0390968 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -5,8 +5,11 @@ import s from "underscore.string"; import { Basis } from "../basis/basis"; import { ConstrainedBasis } from "../basis/constrained_basis"; import { Lattice } from "../lattice/lattice"; +// eslint-disable-next-line import/no-cycle +import { defaultMaterialConfig } from "../material"; import math from "../math"; import { InvalidLineError } from "./errors"; +import poscar from "./poscar"; import { CombinatorialBasis } from "./xyz_combinatorial_basis"; // Regular expression for an XYZ line with atomic constraints, eg. Si 0.000000 0.500000 0.446678 1 1 1` @@ -141,17 +144,30 @@ function fromMaterial(materialOrConfig, fractional = false) { return fromBasis(basis, "%11.6f"); } +/** + * Function splits the xyzFile string at new lines and then returns the first element of the array. + * The first line of the XYZ file should contain the number of atoms in the structure. + * @param {String} xyzFile + * @returns {Number} + */ export function atomsCount(xyzFile) { const xyzArray = xyzFile.split(/\r?\n/); return Integer.parseInt(xyzArray[0]); } -export function linesCount(xyzFile) { - const content = xyzFile.split(/\r?\n/); - if (!content[-1]) { - content.pop(); - } - return content.length; +/** + * Function converts an XYZ formatted structure as a POSCAR formatted structure + * @param {String} xyzContent + * @returns {String} + */ +export function xyzToPoscar(xyzContent) { + const xyzConfig = defaultMaterialConfig; + const xyzArray = xyzContent.split(/\r?\n/); + const xyzArrayBasisOnly = xyzArray.slice(2, -1); + const xyzBasis = xyzArrayBasisOnly.join("\n"); + xyzConfig.basis = toBasisConfig(xyzBasis); + xyzConfig.basis.units = "cartesian"; + return poscar.toPoscar(xyzConfig); } export default { @@ -161,5 +177,5 @@ export default { fromBasis, CombinatorialBasis, atomsCount, - linesCount, + xyzToPoscar, }; diff --git a/tests/enums.js b/tests/enums.js index e700d08f..99066d6c 100644 --- a/tests/enums.js +++ b/tests/enums.js @@ -33,3 +33,4 @@ export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.p export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar")); export const CH4 = readFile(path.join(FIXTURES_DIR, "CH4.xyz")); +export const CH4POSCAR = readFile(path.join(FIXTURES_DIR, "CH4.poscar")); diff --git a/tests/fixtures/CH4.poscar b/tests/fixtures/CH4.poscar new file mode 100644 index 00000000..025bf40a --- /dev/null +++ b/tests/fixtures/CH4.poscar @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7a0be33e7640e5f03bbab1c2d130eaa01f791710739919c0224593f8d9d15c95 +size 457 diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index b086296d..e8150ffd 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,8 +1,8 @@ import { expect } from "chai"; import parsers from "../../src/parsers/parsers"; -import { atomsCount, linesCount } from "../../src/parsers/xyz"; -import { CH4, Si } from "../enums"; +import { atomsCount, xyzToPoscar } from "../../src/parsers/xyz"; +import { CH4, CH4POSCAR, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; describe("Parsers:XYZ", () => { @@ -17,7 +17,7 @@ describe("Parsers:XYZ", () => { it("should return the number of atoms for a molecule in an xyz file", () => { expect(atomsCount(CH4)).to.be.equal(5); }); - it("should return the number of lines in an xyz file", () => { - expect(linesCount(CH4)).to.be.equal(7); + it("should return the xyz file content in poscar file format", () => { + assertDeepAlmostEqual(xyzToPoscar(CH4), CH4POSCAR); }); }); From e5e38b685344a785a50c202c93d3305a3c8db9ae Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 7 Jan 2022 10:04:00 -0800 Subject: [PATCH 03/12] simplified atomsCount function for xyz file and updated function name for getting number of atoms in parsers --- src/parsers/parsers.js | 8 ++++---- src/parsers/poscar.js | 4 ++-- src/parsers/xyz.js | 7 +++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 88215c10..5cf21ef0 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -20,13 +20,13 @@ export function getFileStringAsArray(fileString) { * @param {String} fileContent * @returns {Number} */ -export function getAtomsInFileByExtension(fileExtension, fileContent) { +export function getNumberOfAtomsInFileByExtension(fileExtension, fileContent) { let numberOfAtoms = 0; if (fileExtension === "poscar") { - numberOfAtoms = poscar.atomsCount(fileContent); + numberOfAtoms = poscar.poscarFileAtomsCount(fileContent); } if (fileExtension === "xyz") { - numberOfAtoms = xyz.atomsCount(fileContent); + numberOfAtoms = xyz.xyzFileAtomsCount(fileContent); } return numberOfAtoms; } @@ -37,5 +37,5 @@ export default { cif, espresso, getFileStringAsArray, - getAtomsInFileByExtension, + getNumberOfAtomsInFileByExtension, }; diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 17e0244e..88b3b304 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -58,7 +58,7 @@ function toPoscar(materialOrConfig, omitConstraints = false) { * @param poscarFileContent * @returns {Number} */ -export function atomsCount(poscarFileContent) { +export function poscarFileAtomsCount(poscarFileContent) { const atomsLine = poscarFileContent.split("\n")[6].split(" "); return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } @@ -66,5 +66,5 @@ export function atomsCount(poscarFileContent) { export default { toPoscar, atomicConstraintsCharFromBool, - atomsCount, + poscarFileAtomsCount, }; diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index e0390968..afd8a4c5 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -150,9 +150,8 @@ function fromMaterial(materialOrConfig, fractional = false) { * @param {String} xyzFile * @returns {Number} */ -export function atomsCount(xyzFile) { - const xyzArray = xyzFile.split(/\r?\n/); - return Integer.parseInt(xyzArray[0]); +export function xyzFileAtomsCount(xyzFile) { + return Integer.parseInt(xyzFile.split(/\r?\n/)[0]); } /** @@ -176,6 +175,6 @@ export default { toBasisConfig, fromBasis, CombinatorialBasis, - atomsCount, + xyzFileAtomsCount, xyzToPoscar, }; From aef4e7bcb0544e6f0eaa86dd80593c6b83aacc13 Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 7 Jan 2022 10:07:55 -0800 Subject: [PATCH 04/12] removed unused function from parsers --- src/parsers/parsers.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 5cf21ef0..c2a6a2f7 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -5,15 +5,6 @@ import poscar from "./poscar"; // eslint-disable-next-line import/no-cycle import xyz from "./xyz"; -/** - * Function splits the contents of a string at each newline and returns the resulting array. - * @param fileString - * @returns {Array} - */ -export function getFileStringAsArray(fileString) { - return fileString.split(/\r?\n/); -} - /** * Function returns the number of atoms in a file using the proper parser function based on the file extension. * @param {String} fileExtension @@ -36,6 +27,5 @@ export default { poscar, cif, espresso, - getFileStringAsArray, getNumberOfAtomsInFileByExtension, }; From 65c6528bd79ccbeaccfe8071a2f557c25e5736b5 Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 7 Jan 2022 10:35:06 -0800 Subject: [PATCH 05/12] updated tests for new function naming --- tests/parsers/poscar.js | 4 ++-- tests/parsers/xyz.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index c4642fb1..da7dc60e 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { atomsCount } from "../../src/parsers/poscar"; +import { poscarFileAtomsCount } from "../../src/parsers/poscar"; import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { @@ -16,6 +16,6 @@ describe("Parsers.POSCAR", () => { }); it("should return the number of atoms for a molecule in a poscar file", () => { - expect(atomsCount(H2O)).to.be.equal(3); + expect(poscarFileAtomsCount(H2O)).to.be.equal(3); }); }); diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index e8150ffd..c0733b7b 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import parsers from "../../src/parsers/parsers"; -import { atomsCount, xyzToPoscar } from "../../src/parsers/xyz"; +import { xyzFileAtomsCount, xyzToPoscar } from "../../src/parsers/xyz"; import { CH4, CH4POSCAR, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; @@ -15,7 +15,7 @@ describe("Parsers:XYZ", () => { ]); }); it("should return the number of atoms for a molecule in an xyz file", () => { - expect(atomsCount(CH4)).to.be.equal(5); + expect(xyzFileAtomsCount(CH4)).to.be.equal(5); }); it("should return the xyz file content in poscar file format", () => { assertDeepAlmostEqual(xyzToPoscar(CH4), CH4POSCAR); From 6ef320c4aa2cfc429a347528c32d6e355a375896 Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 7 Jan 2022 19:09:55 -0800 Subject: [PATCH 06/12] moved functions and updated naming based on PR comments --- src/parsers/parsers.js | 24 ++++++++++++++++++++---- src/parsers/poscar.js | 4 ++-- src/parsers/xyz.js | 26 +++----------------------- tests/parsers/parsers.js | 9 +++++++++ tests/parsers/poscar.js | 4 ++-- tests/parsers/xyz.js | 9 +++------ 6 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 tests/parsers/parsers.js diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index c2a6a2f7..32ca3e93 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,8 +1,8 @@ -import cif from "./cif"; // eslint-disable-next-line import/no-cycle +import { defaultMaterialConfig } from "../material"; +import cif from "./cif"; import espresso from "./espresso"; import poscar from "./poscar"; -// eslint-disable-next-line import/no-cycle import xyz from "./xyz"; /** @@ -14,18 +14,34 @@ import xyz from "./xyz"; export function getNumberOfAtomsInFileByExtension(fileExtension, fileContent) { let numberOfAtoms = 0; if (fileExtension === "poscar") { - numberOfAtoms = poscar.poscarFileAtomsCount(fileContent); + numberOfAtoms = poscar.getAtomsCount(fileContent); } if (fileExtension === "xyz") { - numberOfAtoms = xyz.xyzFileAtomsCount(fileContent); + numberOfAtoms = xyz.getAtomsCount(fileContent); } return numberOfAtoms; } +/** + * Function converts an XYZ formatted structure as a POSCAR formatted structure + * @param {String} xyzContent + * @returns {String} + */ +export function xyzToPoscar(xyzContent) { + const xyzConfig = defaultMaterialConfig; + const xyzArray = xyzContent.split(/\r?\n/); + const xyzArrayBasisOnly = xyzArray.slice(2, -1); + const xyzBasis = xyzArrayBasisOnly.join("\n"); + xyzConfig.basis = xyz.toBasisConfig(xyzBasis); + xyzConfig.basis.units = "cartesian"; + return poscar.toPoscar(xyzConfig); +} + export default { xyz, poscar, cif, espresso, getNumberOfAtomsInFileByExtension, + xyzToPoscar, }; diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 88b3b304..b04cb8cd 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -58,7 +58,7 @@ function toPoscar(materialOrConfig, omitConstraints = false) { * @param poscarFileContent * @returns {Number} */ -export function poscarFileAtomsCount(poscarFileContent) { +export function getAtomsCount(poscarFileContent) { const atomsLine = poscarFileContent.split("\n")[6].split(" "); return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } @@ -66,5 +66,5 @@ export function poscarFileAtomsCount(poscarFileContent) { export default { toPoscar, atomicConstraintsCharFromBool, - poscarFileAtomsCount, + getAtomsCount, }; diff --git a/src/parsers/xyz.js b/src/parsers/xyz.js index afd8a4c5..070700c3 100644 --- a/src/parsers/xyz.js +++ b/src/parsers/xyz.js @@ -1,15 +1,11 @@ -import Integer from "lodash/string"; import _ from "underscore"; import s from "underscore.string"; import { Basis } from "../basis/basis"; import { ConstrainedBasis } from "../basis/constrained_basis"; import { Lattice } from "../lattice/lattice"; -// eslint-disable-next-line import/no-cycle -import { defaultMaterialConfig } from "../material"; import math from "../math"; import { InvalidLineError } from "./errors"; -import poscar from "./poscar"; import { CombinatorialBasis } from "./xyz_combinatorial_basis"; // Regular expression for an XYZ line with atomic constraints, eg. Si 0.000000 0.500000 0.446678 1 1 1` @@ -150,23 +146,8 @@ function fromMaterial(materialOrConfig, fractional = false) { * @param {String} xyzFile * @returns {Number} */ -export function xyzFileAtomsCount(xyzFile) { - return Integer.parseInt(xyzFile.split(/\r?\n/)[0]); -} - -/** - * Function converts an XYZ formatted structure as a POSCAR formatted structure - * @param {String} xyzContent - * @returns {String} - */ -export function xyzToPoscar(xyzContent) { - const xyzConfig = defaultMaterialConfig; - const xyzArray = xyzContent.split(/\r?\n/); - const xyzArrayBasisOnly = xyzArray.slice(2, -1); - const xyzBasis = xyzArrayBasisOnly.join("\n"); - xyzConfig.basis = toBasisConfig(xyzBasis); - xyzConfig.basis.units = "cartesian"; - return poscar.toPoscar(xyzConfig); +export function getAtomsCount(xyzFile) { + return parseInt(xyzFile.split(/\r?\n/)[0], 10); } export default { @@ -175,6 +156,5 @@ export default { toBasisConfig, fromBasis, CombinatorialBasis, - xyzFileAtomsCount, - xyzToPoscar, + getAtomsCount, }; diff --git a/tests/parsers/parsers.js b/tests/parsers/parsers.js new file mode 100644 index 00000000..36d83df6 --- /dev/null +++ b/tests/parsers/parsers.js @@ -0,0 +1,9 @@ +import { xyzToPoscar } from "../../src/parsers/parsers"; +import { CH4, CH4POSCAR } from "../enums"; +import { assertDeepAlmostEqual } from "../utils"; + +describe("Parsers:XYZ", () => { + it("should return the xyz file content in poscar file format", () => { + assertDeepAlmostEqual(xyzToPoscar(CH4), CH4POSCAR); + }); +}); diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index da7dc60e..a77227b9 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,7 +1,7 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { poscarFileAtomsCount } from "../../src/parsers/poscar"; +import { getAtomsCount } from "../../src/parsers/poscar"; import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { @@ -16,6 +16,6 @@ describe("Parsers.POSCAR", () => { }); it("should return the number of atoms for a molecule in a poscar file", () => { - expect(poscarFileAtomsCount(H2O)).to.be.equal(3); + expect(getAtomsCount(H2O)).to.be.equal(3); }); }); diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index c0733b7b..fe3090e7 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,8 +1,8 @@ import { expect } from "chai"; import parsers from "../../src/parsers/parsers"; -import { xyzFileAtomsCount, xyzToPoscar } from "../../src/parsers/xyz"; -import { CH4, CH4POSCAR, Si } from "../enums"; +import { getAtomsCount } from "../../src/parsers/xyz"; +import { CH4, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; describe("Parsers:XYZ", () => { @@ -15,9 +15,6 @@ describe("Parsers:XYZ", () => { ]); }); it("should return the number of atoms for a molecule in an xyz file", () => { - expect(xyzFileAtomsCount(CH4)).to.be.equal(5); - }); - it("should return the xyz file content in poscar file format", () => { - assertDeepAlmostEqual(xyzToPoscar(CH4), CH4POSCAR); + expect(getAtomsCount(CH4)).to.be.equal(5); }); }); From 78572e9ad5049d6abba25f7bbfeee2bbcbf8eac7 Mon Sep 17 00:00:00 2001 From: adewyer Date: Thu, 13 Jan 2022 09:16:48 -0800 Subject: [PATCH 07/12] moved defaultMaterialConfig to it's own file to avoid circular imports --- src/default_material.js | 43 ++++++++++++++++++++++++++++ src/made.js | 3 +- src/material.js | 63 +++++++---------------------------------- src/parsers/parsers.js | 2 +- 4 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 src/default_material.js diff --git a/src/default_material.js b/src/default_material.js new file mode 100644 index 00000000..2b5692e3 --- /dev/null +++ b/src/default_material.js @@ -0,0 +1,43 @@ +import { ATOMIC_COORD_UNITS, units } from "./constants"; +import { LATTICE_TYPE } from "./lattice/types"; + +export const defaultMaterialConfig = { + name: "Silicon FCC", + basis: { + elements: [ + { + id: 1, + value: "Si", + }, + { + id: 2, + value: "Si", + }, + ], + coordinates: [ + { + id: 1, + value: [0.0, 0.0, 0.0], + }, + { + id: 2, + value: [0.25, 0.25, 0.25], + }, + ], + units: ATOMIC_COORD_UNITS.crystal, + }, + lattice: { + // Primitive cell for Diamond FCC Silicon at ambient conditions + type: LATTICE_TYPE.FCC, + a: 3.867, + b: 3.867, + c: 3.867, + alpha: 60, + beta: 60, + gamma: 60, + units: { + length: units.angstrom, + angle: units.degree, + }, + }, +}; diff --git a/src/made.js b/src/made.js index 6d4c1fef..299a4fb0 100644 --- a/src/made.js +++ b/src/made.js @@ -2,10 +2,11 @@ import { ArrayWithIds } from "./abstract/array_with_ids"; import { Basis } from "./basis/basis"; import { ATOMIC_COORD_UNITS, coefficients, tolerance, units } from "./constants"; import { AtomicConstraints } from "./constraints/constraints"; +import { defaultMaterialConfig } from "./default_material"; import { Lattice, nonPeriodicLatticeScalingFactor } from "./lattice/lattice"; import { ReciprocalLattice } from "./lattice/reciprocal/lattice_reciprocal"; import { DEFAULT_LATTICE_UNITS, LATTICE_TYPE_CONFIGS } from "./lattice/types"; -import { defaultMaterialConfig, Material } from "./material"; +import { Material } from "./material"; import MadeMath from "./math"; import parsers from "./parsers/parsers"; import tools from "./tools/index"; diff --git a/src/material.js b/src/material.js index 871e4b2c..469434ef 100644 --- a/src/material.js +++ b/src/material.js @@ -7,53 +7,10 @@ import { PRIMITIVE_TO_CONVENTIONAL_CELL_LATTICE_TYPES, PRIMITIVE_TO_CONVENTIONAL_CELL_MULTIPLIERS, } from "./cell/conventional_cell"; -import { ATOMIC_COORD_UNITS, units } from "./constants"; import { Lattice } from "./lattice/lattice"; -import { LATTICE_TYPE } from "./lattice/types"; import parsers from "./parsers/parsers"; import supercellTools from "./tools/supercell"; -export const defaultMaterialConfig = { - name: "Silicon FCC", - basis: { - elements: [ - { - id: 1, - value: "Si", - }, - { - id: 2, - value: "Si", - }, - ], - coordinates: [ - { - id: 1, - value: [0.0, 0.0, 0.0], - }, - { - id: 2, - value: [0.25, 0.25, 0.25], - }, - ], - units: ATOMIC_COORD_UNITS.crystal, - }, - lattice: { - // Primitive cell for Diamond FCC Silicon at ambient conditions - type: LATTICE_TYPE.FCC, - a: 3.867, - b: 3.867, - c: 3.867, - alpha: 60, - beta: 60, - gamma: 60, - units: { - length: units.angstrom, - angle: units.degree, - }, - }, -}; - export class Material { constructor(config) { this._json = lodash.cloneDeep(config || {}); @@ -111,7 +68,7 @@ export class Material { * @returns {Object} */ getDerivedPropertyByName(name) { - return this.getDerivedProperties().find(x => x.name === name); + return this.getDerivedProperties().find((x) => x.name === name); } /** @@ -119,7 +76,7 @@ export class Material { * @returns {Array} */ getDerivedProperties() { - return this.prop('derivedProperties', []); + return this.prop("derivedProperties", []); } /** @@ -197,12 +154,11 @@ export class Material { * @returns {String} */ getInchiStringForHash() { - const inchi = this.getDerivedPropertyByName('inchi'); + const inchi = this.getDerivedPropertyByName("inchi"); if (inchi) { - return inchi.value - } else { - throw new Error("Hash cannot be created. Missing InChI string in derivedProperties") + return inchi.value; } + throw new Error("Hash cannot be created. Missing InChI string in derivedProperties"); } /** @@ -215,10 +171,11 @@ export class Material { * @param salt {String} Salt for hashing, empty string by default. * @param isScaled {Boolean} Whether to scale the lattice parameter 'a' to 1. */ - calculateHash(salt = '', isScaled = false) { + calculateHash(salt = "", isScaled = false) { let message; if (!this.isNonPeriodic) { - message = this.Basis.hashString + "#" + this.Lattice.getHashString(isScaled) + "#" + salt; + message = + this.Basis.hashString + "#" + this.Lattice.getHashString(isScaled) + "#" + salt; } else { message = this.getInchiStringForHash(); } @@ -226,11 +183,11 @@ export class Material { } set hash(hash) { - this.setProp('hash', hash); + this.setProp("hash", hash); } get hash() { - return this.prop('hash'); + return this.prop("hash"); } /** diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 32ca3e93..4ab31fee 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,5 +1,5 @@ // eslint-disable-next-line import/no-cycle -import { defaultMaterialConfig } from "../material"; +import { defaultMaterialConfig } from "../default_material"; import cif from "./cif"; import espresso from "./espresso"; import poscar from "./poscar"; From df3121489d8b92fe4a16cbce461dc0213f64a334 Mon Sep 17 00:00:00 2001 From: tjduigna Date: Thu, 13 Jan 2022 12:32:49 -0500 Subject: [PATCH 08/12] feat: git lfs pull --- .github/workflows/cicd.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index e3e6c484..62428486 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -18,6 +18,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 @@ -40,6 +42,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 From bce5e1b9fe9439eed4f5cdf4e254d597818af53e Mon Sep 17 00:00:00 2001 From: adewyer Date: Thu, 13 Jan 2022 12:09:56 -0800 Subject: [PATCH 09/12] remove eslint-disable-next-line import/no-cycle --- src/parsers/parsers.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 4ab31fee..9321c673 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,4 +1,3 @@ -// eslint-disable-next-line import/no-cycle import { defaultMaterialConfig } from "../default_material"; import cif from "./cif"; import espresso from "./espresso"; From d859afce1b7ee1a532622f0d0959206d153a446c Mon Sep 17 00:00:00 2001 From: adewyer Date: Fri, 11 Feb 2022 18:29:48 -0800 Subject: [PATCH 10/12] updated functions for converting between formats --- src/parsers/parsers.js | 17 ----------------- src/parsers/poscar.js | 33 +++++++++++++++++++++++++++++++++ tests/parsers/parsers.js | 9 --------- tests/parsers/poscar.js | 16 ++++++++++++++-- 4 files changed, 47 insertions(+), 28 deletions(-) delete mode 100644 tests/parsers/parsers.js diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 9321c673..1f088fe0 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,4 +1,3 @@ -import { defaultMaterialConfig } from "../default_material"; import cif from "./cif"; import espresso from "./espresso"; import poscar from "./poscar"; @@ -21,26 +20,10 @@ export function getNumberOfAtomsInFileByExtension(fileExtension, fileContent) { return numberOfAtoms; } -/** - * Function converts an XYZ formatted structure as a POSCAR formatted structure - * @param {String} xyzContent - * @returns {String} - */ -export function xyzToPoscar(xyzContent) { - const xyzConfig = defaultMaterialConfig; - const xyzArray = xyzContent.split(/\r?\n/); - const xyzArrayBasisOnly = xyzArray.slice(2, -1); - const xyzBasis = xyzArrayBasisOnly.join("\n"); - xyzConfig.basis = xyz.toBasisConfig(xyzBasis); - xyzConfig.basis.units = "cartesian"; - return poscar.toPoscar(xyzConfig); -} - export default { xyz, poscar, cif, espresso, getNumberOfAtomsInFileByExtension, - xyzToPoscar, }; diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index b04cb8cd..3cf274ec 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -2,8 +2,10 @@ import s from "underscore.string"; import { ConstrainedBasis } from "../basis/constrained_basis"; import { ATOMIC_COORD_UNITS } from "../constants"; +import { defaultMaterialConfig } from "../default_material"; import { Lattice } from "../lattice/lattice"; import math from "../math"; +import xyz from "./xyz"; const _print = (x, printFormat = "%14.9f") => s.sprintf(printFormat, math.precise(x)); const _latticeVectorsToString = (vectors) => @@ -63,8 +65,39 @@ export function getAtomsCount(poscarFileContent) { return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b); } +/** + * Converts XYZ formated string content to POSCAR formated string content. + * @param xyzContent + * @returns {string} + */ +export function convertFromXyz(xyzContent) { + const xyzConfig = defaultMaterialConfig; + const xyzArray = xyzContent.split(/\r?\n/); + const xyzArrayBasisOnly = xyzArray.slice(2, -1); + const xyzBasis = xyzArrayBasisOnly.join("\n"); + xyzConfig.basis = xyz.toBasisConfig(xyzBasis); + xyzConfig.basis.units = "cartesian"; + return toPoscar(xyzConfig); +} + +/** + * Function converts a string of content into a POSCAR formatted string of content. The function called to convert the + * format is determined by the format type passed to the function. + * @param {String} xyzContent + * @param {String} format + * @returns {String} + */ +export function convertFromOtherFormat(content, format) { + if (format === "xyz") { + return convertFromXyz(content); + } + return content; +} + export default { toPoscar, atomicConstraintsCharFromBool, getAtomsCount, + convertFromOtherFormat, + convertFromXyz, }; diff --git a/tests/parsers/parsers.js b/tests/parsers/parsers.js deleted file mode 100644 index 36d83df6..00000000 --- a/tests/parsers/parsers.js +++ /dev/null @@ -1,9 +0,0 @@ -import { xyzToPoscar } from "../../src/parsers/parsers"; -import { CH4, CH4POSCAR } from "../enums"; -import { assertDeepAlmostEqual } from "../utils"; - -describe("Parsers:XYZ", () => { - it("should return the xyz file content in poscar file format", () => { - assertDeepAlmostEqual(xyzToPoscar(CH4), CH4POSCAR); - }); -}); diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index a77227b9..cf61664c 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,8 +1,17 @@ import { expect } from "chai"; import { Material } from "../../src/material"; -import { getAtomsCount } from "../../src/parsers/poscar"; -import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; +import { convertFromOtherFormat, getAtomsCount } from "../../src/parsers/poscar"; +import { + CH4, + CH4POSCAR, + H2O, + Na4Cl4, + Na4Cl4Poscar, + Zr1H23Zr1H1, + Zr1H23Zr1H1Poscar, +} from "../enums"; +import { assertDeepAlmostEqual } from "../utils"; describe("Parsers.POSCAR", () => { it("should return a valid poscar", () => { @@ -18,4 +27,7 @@ describe("Parsers.POSCAR", () => { it("should return the number of atoms for a molecule in a poscar file", () => { expect(getAtomsCount(H2O)).to.be.equal(3); }); + it("should return the xyz file content in poscar file format", () => { + assertDeepAlmostEqual(convertFromOtherFormat(CH4, "xyz"), CH4POSCAR); + }); }); From 8750daec4fecfd3c5abd35a65a0dced18a0ab4e5 Mon Sep 17 00:00:00 2001 From: adewyer Date: Wed, 16 Feb 2022 10:07:33 -0800 Subject: [PATCH 11/12] updated how we copy the default material in converting other formats to poscar --- src/parsers/poscar.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 3cf274ec..50b6e288 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -1,5 +1,6 @@ import s from "underscore.string"; +import { Material } from "../../lib/material"; import { ConstrainedBasis } from "../basis/constrained_basis"; import { ATOMIC_COORD_UNITS } from "../constants"; import { defaultMaterialConfig } from "../default_material"; @@ -71,7 +72,8 @@ export function getAtomsCount(poscarFileContent) { * @returns {string} */ export function convertFromXyz(xyzContent) { - const xyzConfig = defaultMaterialConfig; + const newMaterial = new Material(defaultMaterialConfig).clone(); + const xyzConfig = newMaterial._json; const xyzArray = xyzContent.split(/\r?\n/); const xyzArrayBasisOnly = xyzArray.slice(2, -1); const xyzBasis = xyzArrayBasisOnly.join("\n"); From 431a26933ac7405ed96f78ed4d421f236570e58e Mon Sep 17 00:00:00 2001 From: adewyer Date: Wed, 16 Feb 2022 15:19:29 -0800 Subject: [PATCH 12/12] updates for converting xyz --> poscar without altering default material --- src/default_material.js | 13 +++++++++++++ src/made.js | 3 ++- src/parsers/poscar.js | 6 ++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/default_material.js b/src/default_material.js index 2b5692e3..7b5736f7 100644 --- a/src/default_material.js +++ b/src/default_material.js @@ -41,3 +41,16 @@ export const defaultMaterialConfig = { }, }, }; + +/* + * Function returns the defaultMaterial object. + * @returns {Object} + */ +export function getDefaultMaterialConfig() { + return defaultMaterialConfig; +} + +export default { + defaultMaterialConfig, + getDefaultMaterialConfig, +}; diff --git a/src/made.js b/src/made.js index 299a4fb0..022e408d 100644 --- a/src/made.js +++ b/src/made.js @@ -2,7 +2,7 @@ import { ArrayWithIds } from "./abstract/array_with_ids"; import { Basis } from "./basis/basis"; import { ATOMIC_COORD_UNITS, coefficients, tolerance, units } from "./constants"; import { AtomicConstraints } from "./constraints/constraints"; -import { defaultMaterialConfig } from "./default_material"; +import { defaultMaterialConfig, getDefaultMaterialConfig } from "./default_material"; import { Lattice, nonPeriodicLatticeScalingFactor } from "./lattice/lattice"; import { ReciprocalLattice } from "./lattice/reciprocal/lattice_reciprocal"; import { DEFAULT_LATTICE_UNITS, LATTICE_TYPE_CONFIGS } from "./lattice/types"; @@ -20,6 +20,7 @@ export const Made = { Material, defaultMaterialConfig, + getDefaultMaterialConfig, Lattice, nonPeriodicLatticeScalingFactor, ReciprocalLattice, diff --git a/src/parsers/poscar.js b/src/parsers/poscar.js index 50b6e288..4b991fb2 100644 --- a/src/parsers/poscar.js +++ b/src/parsers/poscar.js @@ -1,9 +1,8 @@ import s from "underscore.string"; -import { Material } from "../../lib/material"; import { ConstrainedBasis } from "../basis/constrained_basis"; import { ATOMIC_COORD_UNITS } from "../constants"; -import { defaultMaterialConfig } from "../default_material"; +import { getDefaultMaterialConfig } from "../default_material"; import { Lattice } from "../lattice/lattice"; import math from "../math"; import xyz from "./xyz"; @@ -72,8 +71,7 @@ export function getAtomsCount(poscarFileContent) { * @returns {string} */ export function convertFromXyz(xyzContent) { - const newMaterial = new Material(defaultMaterialConfig).clone(); - const xyzConfig = newMaterial._json; + const xyzConfig = { ...getDefaultMaterialConfig() }; const xyzArray = xyzContent.split(/\r?\n/); const xyzArrayBasisOnly = xyzArray.slice(2, -1); const xyzBasis = xyzArrayBasisOnly.join("\n");