All files / src/parsers VASP.ts

81.81% Statements 45/55
63.63% Branches 7/11
100% Functions 1/1
80.39% Lines 41/51

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                    4x 4x   4x   4x 1x     3x 2x   1x     1x     2x 1x     1x     1x 1x 1x   1x                       1x 1x 1x       1x     1x   1x   1x 1x               1x             1x   1x   1x 2x 2x 127x       127x 127x 127x         127x         127x         127x             127x   127x   2x     1x    
import { Matrix3 } from "../WebGL";
 
/**
 * @param {string}
 *            str
 * @param {ParserOptionsSpec}
 *            options
 * @category Parsers
 */
export function VASP(str /*,options*/) {
  var atoms: any[][] & Record<string, any> = [[]];
  var lattice: Record<string, number | Float32Array> = {};
 
  var lines = str.replace(/^\s+/, "").split(/\r?\n/);
 
  if (lines.length < 3) {
    return atoms;
  }
 
  if (lines[1].match(/\d+/)) {
    lattice.length = parseFloat(lines[1]);
  } else {
    console.log(
      "Warning: second line of the vasp structure file must be a number"
    );
    return atoms;
  }
 
  if (lattice.length < 0) {
    console.log(
      "Warning: Vasp implementation for negative lattice lengths is not yet available"
    );
    return atoms;
  }
 
  lattice.xVec = new Float32Array(lines[2].replace(/^\s+/, "").split(/\s+/));
  lattice.yVec = new Float32Array(lines[3].replace(/^\s+/, "").split(/\s+/));
  lattice.zVec = new Float32Array(lines[4].replace(/^\s+/, "").split(/\s+/));
 
  var matrix = new Matrix3(
    lattice.xVec[0],
    lattice.xVec[1],
    lattice.xVec[2],
    lattice.yVec[0],
    lattice.yVec[1],
    lattice.yVec[2],
    lattice.zVec[0],
    lattice.zVec[1],
    lattice.zVec[2]
  );
 
  matrix.multiplyScalar(lattice.length);
  atoms.modelData = [{ symmetries: [], cryst: { matrix: matrix } }];
  var atomSymbols = lines[5]
    .replace(/\s+/, "")
    .replace(/\s+$/, "")
    .split(/\s+/);
  var atomSpeciesNumber = new Int16Array(
    lines[6].replace(/^\s+/, "").split(/\s+/)
  );
  var vaspMode = lines[7].replace(/\s+/, "");
 
  Iif (vaspMode.match(/C/)) {
    vaspMode = "cartesian";
  } else if (vaspMode.match(/D/)) {
    vaspMode = "direct";
  } else E{
    console.log(
      "Warning: Unknown vasp mode in POSCAR file: mode must be either C(artesian) or D(irect)"
    );
    return atoms;
  }
 
  Iif (atomSymbols.length != atomSpeciesNumber.length) {
    console.log("Warning: declaration of atomary species wrong:");
    console.log(atomSymbols);
    console.log(atomSpeciesNumber);
    return atoms;
  }
 
  lines.splice(0, 8);
 
  var atomCounter = 0;
 
  for (var i = 0, len = atomSymbols.length; i < len; i++) {
    var atomSymbol = atomSymbols[i];
    for (var j = 0, atomLen = atomSpeciesNumber[i]; j < atomLen; j++) {
      var coords = new Float32Array(
        lines[atomCounter + j].replace(/^\s+/, "").split(/\s+/)
      );
 
      var atom: Record<string, number | []> = {};
      atom.elem = atomSymbol;
      Iif (vaspMode == "cartesian") {
        atom.x = lattice.length * coords[0];
        atom.y = lattice.length * coords[1];
        atom.z = lattice.length * coords[2];
      } else {
        atom.x =
          lattice.length *
          (coords[0] * lattice.xVec[0] +
            coords[1] * lattice.yVec[0] +
            coords[2] * lattice.zVec[0]);
        atom.y =
          lattice.length *
          (coords[0] * lattice.xVec[1] +
            coords[1] * lattice.yVec[1] +
            coords[2] * lattice.zVec[1]);
        atom.z =
          lattice.length *
          (coords[0] * lattice.xVec[2] +
            coords[1] * lattice.yVec[2] +
            coords[2] * lattice.zVec[2]);
      }
 
      atom.bonds = [];
 
      atoms[0].push(atom);
    }
    atomCounter += atomSpeciesNumber[i];
  }
 
  return atoms;
}