Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: xyz file parsing - nAtoms & nLines to check formatting. Created… #52

Open
wants to merge 13 commits into
base: epic/SOF-5386
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/parsers/parsers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
// eslint-disable-next-line import/no-cycle
import { defaultMaterialConfig } from "../material";
Copy link
Contributor Author

@adewyer adewyer Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the xyzToPoscar function from parsers/xyz.js to this file fixed the import xyz circular dependency, but created one for defaultMaterialConfig. Previously this circular dependency was noted inside parsers/xyz.js

import cif from "./cif";
import espresso from "./espresso";
import poscar from "./poscar";
import xyz from "./xyz";

/**
* 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 getNumberOfAtomsInFileByExtension(fileExtension, fileContent) {
let numberOfAtoms = 0;
if (fileExtension === "poscar") {
numberOfAtoms = poscar.getAtomsCount(fileContent);
}
if (fileExtension === "xyz") {
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,
};
4 changes: 2 additions & 2 deletions src/parsers/poscar.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ function toPoscar(materialOrConfig, omitConstraints = false) {
* @param poscarFileContent
* @returns {Number}
*/
export function atomsCount(poscarFileContent) {
export function getAtomsCount(poscarFileContent) {
const atomsLine = poscarFileContent.split("\n")[6].split(" ");
return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b);
}

export default {
toPoscar,
atomicConstraintsCharFromBool,
atomsCount,
getAtomsCount,
};
11 changes: 11 additions & 0 deletions src/parsers/xyz.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,21 @@ 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 getAtomsCount(xyzFile) {
return parseInt(xyzFile.split(/\r?\n/)[0], 10);
}

export default {
validate,
fromMaterial,
toBasisConfig,
fromBasis,
CombinatorialBasis,
getAtomsCount,
};
3 changes: 3 additions & 0 deletions tests/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ 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"));
export const CH4POSCAR = readFile(path.join(FIXTURES_DIR, "CH4.poscar"));
3 changes: 3 additions & 0 deletions tests/fixtures/CH4.poscar
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/fixtures/CH4.xyz
Git LFS file not shown
9 changes: 9 additions & 0 deletions tests/parsers/parsers.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
4 changes: 2 additions & 2 deletions tests/parsers/poscar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";

import { Material } from "../../src/material";
import { atomsCount } from "../../src/parsers/poscar";
import { getAtomsCount } from "../../src/parsers/poscar";
import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums";

describe("Parsers.POSCAR", () => {
Expand All @@ -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(getAtomsCount(H2O)).to.be.equal(3);
});
});
8 changes: 7 additions & 1 deletion tests/parsers/xyz.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect } from "chai";

import parsers from "../../src/parsers/parsers";
import { Si } from "../enums";
import { getAtomsCount } from "../../src/parsers/xyz";
import { CH4, Si } from "../enums";
import { assertDeepAlmostEqual } from "../utils";

describe("Parsers:XYZ", () => {
Expand All @@ -11,4 +14,7 @@ describe("Parsers:XYZ", () => {
"units",
]);
});
it("should return the number of atoms for a molecule in an xyz file", () => {
expect(getAtomsCount(CH4)).to.be.equal(5);
});
});