1
|
|
|
/* eslint-env node */ |
2
|
|
|
/*jslint node: true */ |
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
let jsonfile = require('jsonfile'); |
6
|
|
|
|
7
|
|
|
let resources = jsonfile.readFileSync('build/data/resources.json'); |
8
|
|
|
let elements = jsonfile.readFileSync('build/data/elements.json'); |
9
|
|
|
|
10
|
|
|
// Generates an ion resource |
11
|
|
|
function generateResource(element, charge){ |
12
|
|
|
let name = generateName(element, charge); |
13
|
|
|
addResource(name, charge, element); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
// Adds an ion resource to the resources and elements list |
17
|
|
|
function addResource(name, charge, element){ |
18
|
|
|
if(!resources[name]){ |
19
|
|
|
resources[name] = {}; |
20
|
|
|
resources[name].elements = {}; |
21
|
|
|
resources[name].elements[element] = 1; |
22
|
|
|
let isotopeEnergy = resources[elements[element].main].energy; |
23
|
|
|
resources[name].energy = isotopeEnergy - resources['e-'].energy * charge; |
24
|
|
|
resources[name].html = element + htmlPostfix(charge); |
25
|
|
|
resources[name].charge = charge; |
26
|
|
|
resources[name].type = ['ion']; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if(elements[element].includes.indexOf(name) === -1){ |
30
|
|
|
elements[element].includes.push(name); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Generates the name of a ion, e.g. O3+ |
35
|
|
|
function generateName(element, i) { |
36
|
|
|
if (i === 0) { |
37
|
|
|
return element; |
38
|
|
|
} |
39
|
|
|
let postfix = ''; |
40
|
|
|
if(Math.abs(i) > 1){ |
41
|
|
|
postfix = Math.abs(i); |
42
|
|
|
} |
43
|
|
|
postfix += getSign(i); |
44
|
|
|
return element + postfix; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function getSign(number){ |
48
|
|
|
return number > 0 ? '+': '-'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Generates the HTML postfix of an ion (+ or -) |
52
|
|
|
function htmlPostfix(index) { |
53
|
|
|
let postfix = ''; |
54
|
|
|
if(index === 0){ |
55
|
|
|
return postfix; |
56
|
|
|
} |
57
|
|
|
postfix = Math.abs(index).toString(); |
58
|
|
|
postfix = postfix === '1' ? '' : postfix; |
59
|
|
|
return '<sup>' + postfix + getSign(index) + '</sup>'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
let redox = {}; |
63
|
|
|
for (let element in elements) { |
64
|
|
|
elements[element].includes = elements[element].includes || []; |
65
|
|
|
|
66
|
|
|
let energies = {}; |
67
|
|
|
let charge = -1; |
68
|
|
|
for (let energy of elements[element].electron_affinity || []) { |
69
|
|
|
generateResource(element, charge); |
70
|
|
|
energies[charge] = -energy; |
71
|
|
|
charge--; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
energies[0] = 0; |
75
|
|
|
|
76
|
|
|
charge = 1; |
77
|
|
|
for (let energy of elements[element].ionization_energy || []) { |
78
|
|
|
generateResource(element, charge); |
79
|
|
|
energies[charge] = energy; |
80
|
|
|
charge++; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
let cummulative = {}; |
84
|
|
|
for(let charge in energies){ |
85
|
|
|
cummulative[charge] = cumulativeEnergy(energies, charge); |
86
|
|
|
} |
87
|
|
|
if(typeof redox[element] === 'undefined'){ |
88
|
|
|
redox[element] = cummulative; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* Calculates the cummulative energy of a redox level. |
93
|
|
|
The logic is the following: the redox array gives how much energy it costs |
94
|
|
|
to go from a level to the next, e.g. from +2 to +3. This function calculates |
95
|
|
|
how much it takes to go from level 0 to x by summing each successive level */ |
96
|
|
|
function cumulativeEnergy(redox, level) { |
97
|
|
|
let energy = 0; |
98
|
|
|
let start = Math.min(0, level); |
99
|
|
|
let end = Math.max(0, level); |
100
|
|
|
for (let i = start; i <= end; i++) { |
101
|
|
|
energy += redox[i]; |
102
|
|
|
} |
103
|
|
|
if (level < 0) { |
104
|
|
|
energy = -energy; |
105
|
|
|
} |
106
|
|
|
return energy; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// we delete 1H+ because it doesn't exist, it is a single proton |
110
|
|
|
delete resources['H+']; |
111
|
|
|
let index = elements.H.includes.indexOf('H+'); |
112
|
|
|
if (index > -1) { |
113
|
|
|
elements.H.includes.splice(index, 1); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
jsonfile.writeFileSync('build/data/resources.json', resources, { |
117
|
|
|
spaces: 2 |
118
|
|
|
}); |
119
|
|
|
jsonfile.writeFileSync('build/data/elements.json', elements, { |
120
|
|
|
spaces: 2 |
121
|
|
|
}); |
122
|
|
|
jsonfile.writeFileSync('build/data/redox.json', redox, { |
123
|
|
|
spaces: 2 |
124
|
|
|
}); |
125
|
|
|
|