Skip to content

Commit

Permalink
Merge pull request #785 from prajwalkulkarni/refactor-lammpstrj
Browse files Browse the repository at this point in the history
refactor: LAMMPSTRJ.ts
  • Loading branch information
dkoes authored May 19, 2024
2 parents cb602bd + efbf907 commit 841f8d1
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/parsers/LAMMPSTRJ.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AtomSpec } from "specs";
import { ParserOptionsSpec } from "./ParserOptionsSpec";
import { assignBonds } from "./utils/assignBonds";

Expand All @@ -6,10 +7,7 @@ import { assignBonds } from "./utils/assignBonds";
*
* @category Parsers
*/

export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
var atoms: any[] = [];
var dic:any = {
const dic: Record<string,string> = {
id: "serial",
type: "atom",
element: "elem",
Expand All @@ -28,10 +26,13 @@ export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
zs: "z",
zsu: "z",
};
var lines = str.split(/\r?\n|\r/);
var offset = 0;
var atomCount = 0;
var start = 0;

export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
const atoms: AtomSpec[][] = [];
const lines = str.split(/\r?\n|\r/);
let offset = 0;
let atomCount = 0;
let start = 0;
while (start < lines.length - 9) {
for (var j = start; j < lines.length; j++) {
if (lines[j].match(/ITEM: NUMBER OF ATOMS/))
Expand All @@ -41,19 +42,19 @@ export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
break;
}
}
var types = lines[offset - 1].replace("ITEM: ATOMS ", "").split(" ");
const types = lines[offset - 1].replace("ITEM: ATOMS ", "").split(" ");
atoms.push([]);
for (let j = offset; j < offset + atomCount; j++) {
var atom: Record<string, any> = {};
var properties: any = {};
var tokens = lines[j].split(" ");
for (var k = 0; k < tokens.length; k++) {
var prop = dic[types[k]];
if (prop != undefined) {
if (prop == "serial") atom[prop] = parseInt(tokens[k]);
else if (prop == "x" || prop == "y" || prop === "z")
const atom: AtomSpec = {};
const properties = {};
const tokens = lines[j].split(" ");
for (let k = 0; k < tokens.length; k++) {
const prop = dic[types[k]];
if (prop !== undefined) {
if (prop === "serial") atom[prop] = parseInt(tokens[k]);
else if (prop === "x" || prop === "y" || prop === "z")
atom[prop] = parseFloat(tokens[k]);
else if (prop == "charge" || prop == "radius")
else if (prop === "charge" || prop === "radius")
properties[prop] = parseFloat(tokens[k]);
else atom[prop] = tokens[k];
}
Expand All @@ -66,7 +67,7 @@ export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
start = offset + atomCount - 1;
}
if (options.assignBonds) {
for (var i = 0; i < atoms.length; i++) assignBonds(atoms[i], options);
for (let i = 0; i < atoms.length; i++) assignBonds(atoms[i], options);
}
return atoms;
}

0 comments on commit 841f8d1

Please sign in to comment.