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

Feature/sof 5462 - get name from file content #54

Open
wants to merge 7 commits into
base: epic/SOF-5386
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions src/parsers/poscar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConstrainedBasis } from "../basis/constrained_basis";
import { ATOMIC_COORD_UNITS } from "../constants";
import { Lattice } from "../lattice/lattice";
import math from "../math";
import { getLineFromContent } from "./utils";

const _print = (x, printFormat = "%14.9f") => s.sprintf(printFormat, math.precise(x));
const _latticeVectorsToString = (vectors) =>
Expand Down Expand Up @@ -63,8 +64,22 @@ 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} fileContent
* @param {String} failoverName
* @returns {String}
*/
export function getNameFromContents(fileContent, failoverName = "material") {
const nameFromContent = getLineFromContent(fileContent, 0);
if (nameFromContent) return nameFromContent;
adewyer marked this conversation as resolved.
Show resolved Hide resolved
return failoverName;
}

export default {
toPoscar,
atomicConstraintsCharFromBool,
atomsCount,
getNameFromContents,
};
7 changes: 7 additions & 0 deletions src/parsers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getLineFromContent(fileContent, lineNumber) {
return fileContent.split(/\r?\n/)[lineNumber];
}

export default {
getLineFromContent,
};
adewyer marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions src/parsers/xyz.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ConstrainedBasis } from "../basis/constrained_basis";
import { Lattice } from "../lattice/lattice";
import math from "../math";
import { InvalidLineError } from "./errors";
import { getLineFromContent } from "./utils";
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`
Expand Down Expand Up @@ -140,10 +141,24 @@ 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} fileContent
* @param {String} failoverName
* @returns {String}
*/
export function getNameFromContents(fileContent, failoverName = "material") {
const nameFromContent = getLineFromContent(fileContent, 1);
if (nameFromContent) return nameFromContent;
return failoverName;
}

export default {
validate,
fromMaterial,
toBasisConfig,
fromBasis,
CombinatorialBasis,
getNameFromContents,
};
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
8 changes: 6 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, getNameFromContents } 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,8 @@ 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 fileContents = GenericPoscar;
expect(getNameFromContents(fileContents)).to.be.equal("H2S");
});
});
9 changes: 8 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 { getNameFromContents } from "../../src/parsers/xyz";
import { GenericXYZ, Si } from "../enums";
import { assertDeepAlmostEqual } from "../utils";

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