-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
384 lines (345 loc) · 11.4 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
let pkaList = [];
let firstGraph = true;
let firstCalc = true;
let dropDownShown = false;
let acidName;
let infoClicked = false;
function transpose2d(arr) {
newArr = []
for (i = 0; i < arr[0].length; i++) {
newArr.push([]);
for (j = 0; j < arr.length; j++) {
newArr[i].push(arr[j][i]);
}
}
return newArr;
}
function replaceTableData(parentID, oldDataID, newText, reset = false) {
let parent = document.getElementById(parentID);
let oldDataElement = document.getElementById(oldDataID);
let newDataElement = document.createElement("td");
newDataElement.setAttribute("id", oldDataID);
if (reset) {
newText = document.createTextNode(newText);
} else {
newText = document.createTextNode(newText.toFixed(5));
}
newDataElement.appendChild(newText);
parent.replaceChild(newDataElement, oldDataElement);
}
function calcFractions(pH) {
let protonNum = pkaList.length;
let fractions = [];
let ka1 = Math.pow(10, -pkaList[0]);
let h = Math.pow(10, -pH);
if (protonNum == 1) {
fractions.push(h / (h + ka1));
fractions.push(ka1 / (h + ka1));
} else if (protonNum == 2) {
let ka2 = Math.pow(10, -pkaList[1]);
let d = (h * h) + (ka1 * h) + (ka1 * ka2);
fractions.push((h * h) / d);
fractions.push((ka1 * h) / d);
fractions.push((ka1 * ka2) / d);
} else if (protonNum == 3) {
let ka2 = Math.pow(10, -pkaList[1]);
let ka3 = Math.pow(10, -pkaList[2]);
let d = Math.pow(h, 3) + Math.pow(h, 2) * ka1 + h * ka1 * ka2 + ka1 * ka2 * ka3;
fractions.push(Math.pow(h, 3) / d);
fractions.push((Math.pow(h, 2) * ka1) / d);
fractions.push((h * ka1 * ka2) / d);
fractions.push((ka1 * ka2 * ka3) / d);
}
return fractions;
}
function makeFileText() {
if (infoClicked) {
infoClicked = false;
return;
}
if (firstGraph) {
alert("Please select an acid or graph pka values before saving data.")
return;
}
let start = parseFloat(document.getElementById("pHstart").value);
let final = parseFloat(document.getElementById("pHfinal").value);
if (isNaN(start) || isNaN(final)) {
alert("Please enter a numeric value for the start and final pH values.");
return;
}
let fileString = "pH\t\u03B10\t\u03B11\t\u03B12\t\u03B13\r\n";
for (let i = start; i <= final; i += 0.1) {
fractions = calcFractions(i);
fileString += (i).toFixed(1).toString(10) + "\t";
for (let j = 0; j < fractions.length; j++) {
fileString += fractions[j].toFixed(5).toString(10);
if (j < fractions.length - 1) {
fileString += "\t";
}
}
if (i < (final - start) * 10) {
fileString += "\r\n";
}
}
return fileString;
}
function displayFractions() {
if (firstGraph) {
alert("Please select an acid or graph pka values before calculating fractions.");
return
}
//reset the alpha values from the last calculation
if (!firstCalc) {
let pH = parseFloat(document.getElementById("userPH").value);
let fractions = calcFractions(pH);
let parent = document.getElementById("calculatorSection");
let oldAlpha0 = document.getElementById("userAlpha0");
let newAlpha0 = document.createElement("h3");
newAlpha0.setAttribute("id", "userAlpha0");
newAlpha0.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode0 = document.createTextNode("\u03B10 = ");
newAlpha0.appendChild(newNode0);
parent.replaceChild(newAlpha0, oldAlpha0);
let oldAlpha1 = document.getElementById("userAlpha1");
let newAlpha1 = document.createElement("h3");
newAlpha1.setAttribute("id", "userAlpha1");
newAlpha1.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode1 = document.createTextNode("\u03B11 = ");
newAlpha1.appendChild(newNode1);
parent.replaceChild(newAlpha1, oldAlpha1);
let oldAlpha2 = document.getElementById("userAlpha2");
let newAlpha2 = document.createElement("h3");
newAlpha2.setAttribute("id", "userAlpha2");
newAlpha2.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode2 = document.createTextNode("\u03B12 = ");
newAlpha2.appendChild(newNode2);
parent.replaceChild(newAlpha2, oldAlpha2);
let oldAlpha3 = document.getElementById("userAlpha3");
let newAlpha3 = document.createElement("h3");
newAlpha3.setAttribute("id", "userAlpha3");
newAlpha3.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode3 = document.createTextNode("\u03B13 = ");
newAlpha3.appendChild(newNode3);
parent.replaceChild(newAlpha3, oldAlpha3);
}
let pH = parseFloat(document.getElementById("userPH").value);
if (isNaN(pH)) {
return;
}
let fractions = calcFractions(pH);
let parent = document.getElementById("calculatorSection");
let oldAlpha0 = document.getElementById("userAlpha0");
let newAlpha0 = document.createElement("h3");
newAlpha0.setAttribute("id", "userAlpha0");
newAlpha0.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode0 = document.createTextNode("\u03B10 = " + fractions[0].toFixed(5).toString(10));
newAlpha0.appendChild(newNode0);
parent.replaceChild(newAlpha0, oldAlpha0);
let oldAlpha1 = document.getElementById("userAlpha1");
let newAlpha1 = document.createElement("h3");
newAlpha1.setAttribute("id", "userAlpha1");
newAlpha1.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode1 = document.createTextNode("\u03B11 = " + fractions[1].toFixed(5).toString(10));
newAlpha1.appendChild(newNode1);
parent.replaceChild(newAlpha1, oldAlpha1);
if (fractions.length > 2) {
let oldAlpha2 = document.getElementById("userAlpha2");
let newAlpha2 = document.createElement("h3");
newAlpha2.setAttribute("id", "userAlpha2");
newAlpha2.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode2 = document.createTextNode("\u03B12 = " + fractions[2].toFixed(5).toString(10));
newAlpha2.appendChild(newNode2);
parent.replaceChild(newAlpha2, oldAlpha2);
}
if (fractions.length > 3) {
let oldAlpha3 = document.getElementById("userAlpha3");
let newAlpha3 = document.createElement("h3");
newAlpha3.setAttribute("id", "userAlpha3");
newAlpha3.setAttribute("style", "color:rgb(90, 97, 104)");
let newNode3 = document.createTextNode("\u03B13 = " + fractions[3].toFixed(5).toString(10));
newAlpha3.appendChild(newNode3);
parent.replaceChild(newAlpha3, oldAlpha3);
}
firstCalc = false;
}
function setUserPkas() {
if (isNaN(parseFloat(document.getElementById("pka1input").value))) {
alert("Please select an acid or enter at least a pka1 value before graphing.");
return;
}
let pka1 = parseFloat(document.getElementById("pka1input").value);
let pka2 = parseFloat(document.getElementById("pka2input").value);
let pka3 = parseFloat(document.getElementById("pka3input").value);
pkaList = [];
let outOfOrder = false;
pkaList.push(pka1);
acidName = "pka(s) = " + pka1;
if (!isNaN(pka2)) {
if (pka2 <= pka1) {
outOfOrder = true;
}
pkaList.push(pka2);
acidName += (", " + pka2);
}
if (!isNaN(pka3)) {
if (pka3 <= pka1 || pka3 <= pka2) {
outOfOrder = true;
}
acidName += (", " + pka3);
pkaList.push(pka3);
}
if (outOfOrder) {
alert("pka values should be as such: pka1 < pka2 < pka3.");
}
graphPkaValues();
}
function graphPkaValues() {
let Xs = [];
let Ys = [];
let notAtLim = true;
let i = 0;
// keep calculating fractions until every species is at 100% at some point in
// the graph, or at least fill the graph from 0-14
while (notAtLim || i < 141) {
Xs.push(i / 10);
Ys.push(calcFractions(Xs[i]));
if (i >= 10 && Ys[i - 10][pkaList.length] >= 0.999) {
notAtLim = false;
}
i += 1;
}
// If a0 is never 100% (for very strong acid) then calculate fractions at pH
// lower than zero until every species is at 100% at some point on the graph
if (Ys[10][0] < 0.999) {
i = 1;
while (Ys[10][0] < 0.999) {
Xs.unshift(-i / 10);
Ys.unshift(calcFractions(-i / 10));
i += 1;
}
}
Ys = transpose2d(Ys);
colors = ["#B3EFFF", "#00CFFF", "#046B99", "#1C304A"];
data = [];
for (i = 0; i < Ys.length; i++) {
let trace = {
x: Xs,
y: Ys[i],
type: 'scatter',
name: "\u03B1" + i,
line: {
color: colors[i],
width: 4
},
};
data.push(trace);
}
let layout = {
title: acidName,
titlefont: {
family: "arial, sans-serif",
size: 28
},
xaxis: {
title: "pH",
titlefont: {
family: "arial, sans-serif",
size: 24
},
tickfont: {
family: "sans-serif",
size: 18
}
},
yaxis: {
title: "Ion Fraction",
titlefont: {
family: "sans-serif",
size: 24
},
tickfont: {
family: "sans-serif",
size: 18
}
}
}
let plotArea = document.getElementById("graph");
plotArea.innerHTML = "";
Plotly.newPlot('graph', data, layout, {
showSendToCloud: true,
});
if (!firstGraph) {
for (i = 0; i <= 14; i++) {
let fractions = calcFractions(i);
for (let j = 0; j < 4; j++) {
replaceTableData("a" + j + "col", j.toString(10) + i.toString(10), "-", true);
}
}
}
for (i = 0; i <= 14; i++) {
let fractions = calcFractions(i);
for (let j = 0; j < Ys.length; j++) {
replaceTableData("a" + j + "col", j.toString(10) + i.toString(10), fractions[j], false);
}
}
firstGraph = false;
}
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function toggleDropdown() {
document.getElementById("myDropdown").classList.toggle("show");
let parent = document.getElementById("acid_selection");
if (!dropDownShown) {
document.getElementById("userPkaInput").style.display = "none";
dropDownShown = true;
} else {
document.getElementById("userPkaInput").style.display = "block";
dropDownShown = false;
}
}
function filterFunction() {
let input, filter, ul, li, acidButtonsArr, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
acidButtonsArr = div.getElementsByTagName("button");
for (i = 0; i < acidButtonsArr.length; i++) {
txtValue = acidButtonsArr[i].textContent;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
acidButtonsArr[i].style.display = "";
} else {
acidButtonsArr[i].style.display = "none";
}
}
}
function getAcidPkas(pka1, pka2, pka3, name) {
acidName = name;
pkaList = [];
pkaList.push(pka1);
if (pka2 != null) {
pkaList.push(pka2);
}
if (pka3 != null) {
pkaList.push(pka3);
}
graphPkaValues();
}
function infoClick() {
infoClicked = true;
alert("\u2022You can save a text file of ion fraction values from a start pH to a final pH. \n\n\u2022The pH value increases by steps of 1/10 a pH value. \n\n\u2022The file pkaAnalyzerData.txt will be saved to your Downloads folder.");
}
function download(filename, text) {
var elem = document.createElement('a');
elem.style.display = 'none';
// Define the data of the file using encode URIComponent
elem.setAttribute('href', 'data:text/plain;charset-utf-8,' + encodeURIComponent(text));
// Add the download attribute of the hidden link
elem.setAttribute('download', filename);
document.body.appendChild(elem);
//Simulate click of the created link
elem.click();
document.body.removeChild(elem);
}
document.getElementById("save-btn").addEventListener("click", function() {
download("pkAnalyzer.txt", makeFileText());
}, false);