Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +42,8 @@ jobs:
steps:
- name: Checkout this repository
uses: actions/checkout@v2

with:
lfs: true
- name: Checkout actions repository
uses: actions/checkout@v2
with:
Expand Down
16 changes: 16 additions & 0 deletions src/parsers/poscar.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,24 @@ export function atomsCount(poscarFileContent) {
return atomsLine.map((x) => parseInt(x, 10)).reduce((a, b) => a + b);
}

/**
* Function returns the string on the first line of POSCAR file content. Generally the first line contains text
* containing the name of the structure.
* @param {String} name
* @param {String} fileContent
* @returns {String}
*/
export function materialNameFromFileContents(name, fileContent) {
const materialNameFromFile = fileContent.split(/\r?\n/)[0];
if (name === materialNameFromFile || !materialNameFromFile) {
return name;
}
return materialNameFromFile;
}

export default {
toPoscar,
atomicConstraintsCharFromBool,
atomsCount,
materialNameFromFileContents,
};
16 changes: 16 additions & 0 deletions src/parsers/xyz.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,26 @@ function fromMaterial(materialOrConfig, fractional = false) {
return fromBasis(basis, "%11.6f");
}

/**
* Function returns the string on the second line of XYZ file content. Generally the second line contains text
* containing the name of the structure.
* @param {String} name
* @param {String} fileContent
* @returns {String}
*/
export function materialNameFromFileContents(name, fileContent) {
const materialNameFromFile = fileContent.split(/\r?\n/)[1];
if (name === materialNameFromFile || !materialNameFromFile) {
return name;
}
return materialNameFromFile;
Copy link
Member

Choose a reason for hiding this comment

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

  1. Add a general utility - getLineFromContent(lineNumber)
  2. materialNameFromFileContents -> getNameFromContents
  3. getNameFromContents(fileContent) => {return getLineFromContent(1)} for POSCAR, and 0 for XYZ
    The default name and everything on lines 152-154 should not be dealt with here. We could add a function named: getNameFromFIleContent(type, fileContent, failoverName = "material") and use as a proxy.

}

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

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

describe("Parsers.POSCAR", () => {
it("should return a valid poscar", () => {
Expand All @@ -18,4 +18,9 @@ describe("Parsers.POSCAR", () => {
it("should return the number of atoms for a molecule in a poscar file", () => {
expect(atomsCount(H2O)).to.be.equal(3);
});
it("should return the material name based on the poscar file contents", () => {
const name = "POSCAR.poscar";
const fileContents = GenericPoscar;
expect(materialNameFromFileContents(name, fileContents)).to.be.equal("H2S");
});
});
10 changes: 9 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 { materialNameFromFileContents } from "../../src/parsers/xyz";
import { GenericXYZ, Si } from "../enums";
import { assertDeepAlmostEqual } from "../utils";

describe("Parsers:XYZ", () => {
Expand All @@ -11,4 +14,9 @@ describe("Parsers:XYZ", () => {
"units",
]);
});
it("should return the material name based on the xyz file contents", () => {
const name = "test.xyz";
const fileContents = GenericXYZ;
expect(materialNameFromFileContents(name, fileContents)).to.be.equal("Methane");
});
});