all files / src/workflows/nwchem/nwchem-neb/components/steps/ Visualization.js

50% Statements 48/96
31.91% Branches 15/47
26.09% Functions 6/23
45.45% Lines 30/66
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                                                                                                                                                                                                                 
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
var _candela = require('candela');
 
var _candela2 = _interopRequireDefault(_candela);
 
var _network = require('../../../../../network');
 
var _network2 = _interopRequireDefault(_network);
 
var _LoadingPanel = require('../../../../../panels/LoadingPanel');
 
var _LoadingPanel2 = _interopRequireDefault(_LoadingPanel);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
var auToEv = 27.2116;
var evToKcal = 23.06;
var auToKcal = auToEv * evToKcal;
var x = 'Reaction Coordinate';
var y = 'Energy (kcal/mol)';
 
// ----------------------------------------------------------------------------
 
function extractEnergies(energyData) {
  var energies = [];
  energyData.split('\n').forEach(function (line) {
    var trimmedLine = line.trim();
    if (!trimmedLine.startsWith('#') && trimmedLine.length > 0) {
      var parts = trimmedLine.split(/\s+/);
      var point = {};
      point[x] = Number.parseFloat(parts[0].trim());
      point[y] = Number.parseFloat(parts[1].trim());
      energies.push(point);
    }
  });
 
  var minimumEnergy = Math.min.apply(Math, _toConsumableArray(energies.map(function (point) {
    return Number.parseFloat(point[y]);
  })));
 
  // Convert energies to kcal and make positive
  energies = energies.map(function (point) {
    point[y] = (point[y] + Math.abs(minimumEnergy)) * auToKcal;
 
    return point;
  });
 
  return energies;
}
 
// ----------------------------------------------------------------------------
 
var VisualizationView = function (_React$Component) {
  _inherits(VisualizationView, _React$Component);
 
  function VisualizationView(props) {
    _classCallCheck(this, VisualizationView);
 
    var _this = _possibleConstructorReturn(this, (VisualizationView.__proto__ || Object.getPrototypeOf(VisualizationView)).call(this, props));
 
    _this.state = { energies: [] };
 
    // Look up and fetch neb.neb_final_epath
    _network2.default.listItems({
      folderId: props.simulation.metadata.outputFolder._id,
      name: 'neb.neb_final_epath'
    }).then(function (resp) {
      return _network2.default.listFiles(resp.data[0]._id);
    })
    // Finally download the file
    .then(function (resp) {
      return _network2.default.downloadFile(resp.data[0]._id);
    }).then(function (resp) {
      _this.setState({
        energies: extractEnergies(resp.data)
      });
    }).catch(function (error) {
      console.error('Unable to neb.neb_final_epath.');
      // throw error;
    });
    return _this;
  }
 
  _createClass(VisualizationView, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      if (this.container) {
        // Add indexes to values, to use when a point is clicked on
        var energies = this.state.energies.map(function (energy, index) {
          energy.index = index;
          return energy;
        });
 
        this.chart = new _candela2.default.components.LineChart(this.container, {
          data: energies,
          x: x,
          y: [y],
          width: this.container.getClientRects()[0].width - 20 || 600,
          height: this.container.getClientRects()[0].height - 20 || 400,
          showPoints: true
        });
 
        this.chart.on('click', function (d, item) {
          // Place holder
        });
 
        this.chart.render();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;
 
      if (this.state.energies.length === 0) {
        return _react2.default.createElement(_LoadingPanel2.default, null);
      }
      return _react2.default.createElement('div', {
        style: { width: '100%', height: '100%', padding: 10 },
        ref: function ref(c) {
          _this2.container = c;
        }
      });
    }
  }]);
 
  return VisualizationView;
}(_react2.default.Component);
 
exports.default = VisualizationView;
 
 
VisualizationView.propTypes = {
  simulation: _propTypes2.default.object.isRequired
};