All files / src/parsers LAMMPSTRJ.ts

97.61% Statements 41/42
93.33% Branches 14/15
100% Functions 1/1
97.22% Lines 35/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                2x 2x                                     2x 2x 2x 2x 2x 10x 98x 10x 98x 10x 10x     10x 10x 10x 2560x 2560x 2560x 2560x 12800x 12800x 12800x 10240x 7680x 2560x   2560x   12800x 12800x 12800x   2560x   10x   2x 5x   2x    
import { assignBonds } from "./utils/assignBonds";
 
/**
 * Parse a lammps trajectory file from str and create atoms
 * @category Parsers
 
 */
export function LAMMPSTRJ(str, options) {
  var atoms: any[] = [];
  var dic = {
    id: "serial",
    type: "atom",
    element: "elem",
    q: "charge",
    radius: "radius",
    x: "x",
    xu: "x",
    xs: "x",
    xsu: "x",
    y: "y",
    yu: "y",
    ys: "y",
    ysu: "y",
    z: "z",
    zu: "z",
    zs: "z",
    zsu: "z",
  };
  var lines = str.split(/\r?\n|\r/);
  var offset = 0;
  var atomCount = 0;
  var start = 0;
  while (start < lines.length - 9) {
    for (var j = start; j < lines.length; j++) {
      if (lines[j].match(/ITEM: NUMBER OF ATOMS/))
        atomCount = parseInt(lines[j + 1]);
      if (lines[j].match(/ITEM: ATOMS/)) {
        offset = j + 1;
        break;
      }
    }
    var 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 = {};
      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")
            atom[prop] = parseFloat(tokens[k]);
          else Iif (prop == "charge" || prop == "radius")
            properties[prop] = parseFloat(tokens[k]);
          else atom[prop] = tokens[k];
        }
        atom.properties = properties;
        atom.bonds = [];
        atom.bondOrder = [];
      }
      atoms[atoms.length - 1][j - offset] = atom;
    }
    start = offset + atomCount - 1;
  }
  if (options.assignBonds) {
    for (var i = 0; i < atoms.length; i++) assignBonds(atoms[i]);
  }
  return atoms;
}