|
1 | | -import React from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import candela from 'candela'; |
| 3 | +import client from '../../../../../../../network'; |
| 4 | + |
| 5 | +const auToEv = 27.2116; |
| 6 | +const evToKcal = 23.06; |
| 7 | +const auToKcal = auToEv * evToKcal; |
| 8 | +const x = 'Reaction Coordinate'; |
| 9 | +const y = 'Energy (kcal/mol)'; |
3 | 10 |
|
4 | 11 | const VisualizationView = React.createClass({ |
5 | 12 |
|
6 | 13 | displayName: 'nwchem_neb/steps/Visualization', |
7 | 14 |
|
| 15 | + propTypes: { |
| 16 | + simulation: React.PropTypes.object, |
| 17 | + }, |
| 18 | + |
8 | 19 | getInitialState() { |
9 | | - var data = []; |
10 | | - for (var d = 0; d < 10; d += 1) { |
11 | | - data.push({ |
12 | | - a: d, |
13 | | - b: d |
| 20 | + return { energies: [] }; |
| 21 | + }, |
| 22 | + |
| 23 | + componentDidMount() { |
| 24 | + // Look up and fetch neb.neb_final_epath |
| 25 | + client.listItems({ |
| 26 | + folderId: this.props.simulation.metadata.outputFolder._id, |
| 27 | + name: 'neb.neb_final_epath', |
| 28 | + }) |
| 29 | + .then((resp) => client.listFiles(resp.data[0]._id)) |
| 30 | + // Finally download the file |
| 31 | + .then((resp) => client.downloadFile(resp.data[0]._id)) |
| 32 | + .then((resp) => { |
| 33 | + this.setState({ |
| 34 | + energies: this._extractEnergies(resp.data), |
14 | 35 | }); |
| 36 | + this._createLineChart(); |
| 37 | + }).catch((error) => { |
| 38 | + console.error('Unable to neb.neb_final_epath.'); |
| 39 | + throw error; |
| 40 | + }); |
| 41 | + }, |
| 42 | + |
| 43 | + componentWillUpdate(nextProps, nextState) { |
| 44 | + if (this.chart) { |
| 45 | + this.chart.render(); |
15 | 46 | } |
16 | | - return { data }; |
17 | 47 | }, |
18 | 48 |
|
19 | | - componentDidMount() { |
20 | | - this.chart = new candela.components.LineChart(this.refs.container, { |
21 | | - data: this.state.data, |
22 | | - x: 'a', |
23 | | - y: ['b'], |
24 | | - width: 700, |
25 | | - height: 400 |
| 49 | + _extractEnergies(energyData) { |
| 50 | + var energies = []; |
| 51 | + energyData.split('\n').forEach((line) => { |
| 52 | + const trimmedLine = line.trim(); |
| 53 | + if (!trimmedLine.startsWith('#') && trimmedLine.length > 0) { |
| 54 | + const parts = trimmedLine.split(/\s+/); |
| 55 | + const point = {}; |
| 56 | + point[x] = Number.parseFloat(parts[0].trim()); |
| 57 | + point[y] = Number.parseFloat(parts[1].trim()); |
| 58 | + energies.push(point); |
| 59 | + } |
26 | 60 | }); |
27 | 61 |
|
28 | | - this.chart.render(); |
| 62 | + const minimumEnergy = Math.min(...energies.map((point) => Number.parseFloat(point[y]))); |
| 63 | + |
| 64 | + // Convert energies to kcal and make positive |
| 65 | + energies = energies.map(point => { |
| 66 | + point[y] = (point[y] + Math.abs(minimumEnergy)) * auToKcal; |
| 67 | + |
| 68 | + return point; |
| 69 | + }); |
| 70 | + |
| 71 | + return energies; |
29 | 72 | }, |
| 73 | + _createLineChart() { |
| 74 | + this.chart = new candela.components.LineChart(this.refs.container, { |
| 75 | + data: this.state.energies, |
| 76 | + x, |
| 77 | + y: [y], |
| 78 | + width: 700, |
| 79 | + height: 400, |
| 80 | + }); |
30 | 81 |
|
31 | | - componentWillUpdate(nextProps, nextState) { |
32 | 82 | this.chart.render(); |
33 | 83 | }, |
34 | 84 |
|
35 | 85 | render() { |
36 | 86 | return (<div ref="container"></div>); |
37 | | - } |
| 87 | + }, |
38 | 88 | }); |
39 | 89 |
|
40 | 90 |
|
|
0 commit comments