Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/parsers/xyz.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Integer from "lodash/string";
import _ from "underscore";
import s from "underscore.string";

Expand Down Expand Up @@ -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]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. Why do we even need this function, if all this does is just split on newlines? This is not specific to XYZ format, is it?
  2. No docstring, explanation of the expected behavior

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The first line of an xyz file contains the number of atoms. So we split the string by new lines and then return the first new line to get the number of atoms in the file.

}

export function linesCount(xyzFile) {
const content = xyzFile.split(/\r?\n/);
if (!content[-1]) {
content.pop();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure what's happening above - no explanation either.

return content.length;
}

export default {
validate,
fromMaterial,
toBasisConfig,
fromBasis,
CombinatorialBasis,
atomsCount,
linesCount,
};
2 changes: 2 additions & 0 deletions tests/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
3 changes: 3 additions & 0 deletions tests/fixtures/CH4.xyz
Git LFS file not shown
11 changes: 10 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 { atomsCount, linesCount } from "../../src/parsers/xyz";
import { CH4, Si } from "../enums";
import { assertDeepAlmostEqual } from "../utils";

describe("Parsers:XYZ", () => {
Expand All @@ -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);
});
});