-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
491 lines (424 loc) · 13.3 KB
/
main.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
let dropDownShown = false;
let infoClicked = false;
// Holds current state of on-screen tools.
// Either 'pka' or 'titrate'
let currentView = 'pka';
// This holds graph state to be used when resizing
const graphState = {};
// This holds the current state of the animated gif in pka view.
let showGif = true;
/*
This function is responsible for creating the initial view of the
webpage. Including the default Acetic Acid selection and
loading the acid list.
*/
window.onload = () => {
// Preload Acetic Acid
getAcidPkas(4.7, null, null, "Acetic Acid");
// Add acids to the dropdown.
// This array comes from acids.js
const dropdown = document.getElementById("acid_buttons");
acids.forEach((acid) => {
dropdown.innerHTML += `<button
onclick="getAcidPkas(${acid.pka1}, ${acid.pka2}, ${acid.pka3}, '${acid.name}')">
${acid.name}
</button>`;
});
// At start, view pka. Disable titrate.
const titrateContainer = document.getElementById('titrateContainer')
titrateContainer.style['display'] = 'none';
};
function transpose2d(arr) {
const 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 calcFractions(pH) {
let protonNum = graphState.pkaList.length;
let fractions = [];
let ka1 = Math.pow(10, -graphState.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, -graphState.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, -graphState.pkaList[1]);
let ka3 = Math.pow(10, -graphState.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 displayFractions() {
let pH = parseFloat(document.getElementById("userPH").value);
if (isNaN(pH)) {
alert("Please enter a number into the text input.");
return;
}
const fractions = calcFractions(pH);
const parent = document.getElementById("calculatorSection");
// Clear all html within old h3 children
for (let i = 0; i < parent.children.length; i++) {
const child = parent.children[i];
if (child.tagName === "H3") {
child.innerHTML = "";
}
}
// Insert new alphas
fractions.forEach((fraction, index) => {
const oldAlpha = document.getElementById(`userAlpha${index}`);
const newAlpha = document.createElement("h3");
newAlpha.innerHTML = `<b>\u03B1${index} = ${fraction.toFixed(5)}</b>`;
newAlpha.setAttribute("id", `userAlpha${index}`);
parent.replaceChild(newAlpha, oldAlpha);
});
}
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);
graphState.pkaList = [];
let outOfOrder = false;
if (!isNaN(pka2)) {
if (pka2 <= pka1) {
outOfOrder = true;
}
}
if (!isNaN(pka3)) {
if (pka3 <= pka1 || pka3 <= pka2) {
outOfOrder = true;
}
}
if (outOfOrder) {
alert("pka values should be as such: pka1 < pka2 < pka3.");
return;
}
// Remove possible table background image
const table = document.getElementById("tableSection");
table.style["background-image"] = "url()";
graphState.acidName = `Unknown Chemical<br>pK<sub>a</sub> values: ${pka1}, ${pka2}, ${pka3}`;
// Was pkaList.push(pka1, pka2, pka3);
graphState.pkaList = [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
for (; notAtLim || i < 141; i++) {
Xs.push(i / 10);
Ys.push(calcFractions(Xs[i]));
if (i >= 10 && Ys[i - 10][graphState.pkaList.length] >= 0.999) {
notAtLim = false;
}
}
// 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;
for (; Ys[10][0] < 0.999; i++) {
Xs.unshift(-i / 10);
Ys.unshift(calcFractions(-i / 10));
}
}
Ys = transpose2d(Ys);
const colors = ['#69a3ff', '#0175fa', '#004bc6', '#001255'];
// const colors = ["#b0bec5", "#78909c", "#455a64", "#263238"];
const 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: `<b style="color: #1c313a;">${graphState.acidName}</b>`,
titlefont: {
family: "arial, sans-serif",
size: 28,
},
xaxis: {
title: '<b style="color: #1c313a;">pH</b>',
titlefont: {
family: "arial, sans-serif",
size: 24,
},
tickfont: {
family: "sans-serif",
size: 18,
},
},
yaxis: {
title: '<b style="color: #1c313a;">Ion Fraction</b>',
titlefont: {
family: "arial, sans-serif",
size: 24,
},
tickfont: {
family: "sans-serif",
size: 18,
},
},
};
const plotArea = document.getElementById("graph");
// Remove display style from graph's div
plotArea.style.display = "block";
plotArea.innerHTML = "";
Plotly.newPlot("graph", data, layout, {
showSendToCloud: true,
});
// Replacing Table Data
const acidTable = document.getElementById("acidTable");
const baseTable = document.getElementById("baseTable");
const header = `<tr>
<th>pH</th>
<th>α0</th>
<th>α1</th>
<th>α2</th>
<th>α3</th>
</tr>`;
acidTable.innerHTML = header;
baseTable.innerHTML = header;
for (i = 0; i <= 14; i++) {
const fractions = calcFractions(i);
if (i <= 7) acidTable.innerHTML += createDataRow(fractions, i);
if (i >= 7) baseTable.innerHTML += createDataRow(fractions, i);
}
}
function createDataRow(fractions, ph) {
//Build the new row
function fractionsToHTML(fractions) {
const stringFractions = fractions.map((frac) => {
return frac.toFixed(5).toString();
});
const html = `
<td>${stringFractions[0] || "-"}</td>
<td>${stringFractions[1] || "-"}</td>
<td>${stringFractions[2] || "-"}</td>
<td>${stringFractions[3] || "-"}</td>
`;
return html;
}
return `<tr><th>${ph}.0</th>${fractionsToHTML(fractions)}</tr>`;
}
/* Update the pka text inputs in the left sidebar to match the
range slider.
*/
function updateInput(inputID, inputValue) {
const inputElement = document.getElementById(inputID);
inputElement.value = inputValue;
}
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function toggleDropdown() {
document.getElementById("myDropdown").classList.toggle("show");
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) {
// Set Table Background image
const table = document.getElementById("tableSection");
if (showGif)
table.style["background-image"] = `url('gifs/${name}.gif')`;
graphState.acidName = name;
graphState.pkaList = [];
graphState.pkaList.push(pka1);
if (pka2 != null) {
graphState.pkaList.push(pka2);
}
if (pka3 != null) {
graphState.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. \n\n\u2022Data is saved as a Tab-Separated-Values file."
);
}
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);
}
function makeFileText() {
if (infoClicked) {
infoClicked = false;
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) {
const fractions = calcFractions(i);
fileString += i.toFixed(1).toString(10) + "\t";
fractions.forEach((fraction, index) => {
fileString += fraction.toFixed(5).toString(10);
if (index < fractions.length - 1) fileString += "\t";
});
if (i < (final - start) * 10) {
fileString += "\r\n";
}
}
download("pKAnalyzer.tsv", fileString);
}
/*
This function is responsible for resizing the graph when the size of
the viewport changes. This keeps the graph looking nice if the user
plays with our window.
*/
function resizeGraph() {
graphPkaValues();
}
/*
This function disables/enables the animated gif in pka view.
Then this function updates the current state.
*/
function switchGif() {
const table = document.getElementById("tableSection");
const gifSwitch = document.getElementById("gifSwitch");
if (showGif) {
table.style['background-image'] = "url('')";
gifSwitch.innerText = 'Enable Gif';
showGif = false;
} else {
// Use Graph State to find the acid name.
// If the acid is unknown then just update state
const acidName = graphState.acidName;
if (!acidName.includes('Unknown'))
table.style['background-image'] = `url('gifs/${acidName}.gif')`;
gifSwitch.innerText = 'Disable Gif';
showGif = true;
}
}
/*
This function is responsible for switching
between the pka analyzer and the titration
analyzer
*/
function switchViews() {
const pkaContainer = document.getElementById('pkaContainer')
const titrateContainer = document.getElementById('titrateContainer')
if (currentView === 'pka') {
// Switch from pka to titrate
pkaContainer.style.display = 'none';
titrateContainer.style.display = 'grid';
currentView = 'titrate'
} else {
// Switch from pka to titrate
titrateContainer.style.display = 'none';
pkaContainer.style.display = 'grid';
currentView = 'pka'
}
}
/*
This function takes:
c0: Initial molar concentration of the titrand
ct: Initial molar concentration of titrant
k: ionization constant of the titrand
This function should use these inputs with the proper formulae to generate
points along a titration curve.
I'm starting with strong base/strong acid titrations for testing
*/
function calculateTitrationCurve(c0, ct, k) {
// This function create an array, modeled on Python's range()
function range(start, end, step = 1) {
const len = Math.floor((end - start) / step);
return Array(len)
.fill()
.map((_, i) => start + i * step);
}
console.log(range(0, 10, 0.1));
// Water Ionization Constant
const kw = Math.pow(10, -14);
const initalpH = 14 - Math.log(c0);
const equivPoint = -Math.log(kw) / 2;
function pHBeforeEquiv(f) {
return 14 - -Math.pow(c0) - -Math.log(1 - f);
}
function pHAfterEquiv(f) {
return -Math.log(ct) + -Math.log(f - 1);
}
const degreeArray = range(0, 100, 0.01);
function internalCalculator(eps) {
degreeArray.forEach((f, i) => {
const beforeEquiv = pHBeforeEquiv(f);
const afterEquiv = pHAfterEquiv(f);
if (equivPoint - beforeEquiv > eps) {
degreeArray[i] = beforeEquiv;
} else if (Math.abs(afterEquiv - beforeEquiv) < eps) {
degreeArray[i] = equivPoint;
} else {
degreeArray[i] = afterEquiv;
}
});
}
internalCalculator(Math.pow(10, -6));
return degreeArray;
}