-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapacitorNetwork.js
More file actions
103 lines (87 loc) · 3.03 KB
/
Copy pathCapacitorNetwork.js
File metadata and controls
103 lines (87 loc) · 3.03 KB
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
/*
* Capacitor Network — series and parallel combinations
*
* Parallel: C_total = C₁ + C₂ + ... + Cₙ
* Series: 1/C_total = 1/C₁ + 1/C₂ + ... + 1/Cₙ
*/
/* exported buildInputs, calculate */
var numCapacitors = 2;
function buildInputs() {
var n = parseInt(document.getElementById('numCaps').value, 10);
if (isNaN(n) || n < 1 || n > 20) {
showError('Please enter a number between 1 and 20.');
return;
}
numCapacitors = n;
clearError();
document.getElementById('C_out').textContent = '';
var defUnit = document.getElementById('defaultUnit').value;
var html = '';
for (var i = 1; i <= n; i++) {
html += 'C' + i + ': ';
html += '<input type="number" id="cv-' + i + '" min="0" step="any" placeholder="value" style="width:90px">';
html += '<select id="cu-' + i + '">';
html += '<option value="uF"' + (defUnit === 'uF' ? ' selected' : '') + '>uF</option>';
html += '<option value="nF"' + (defUnit === 'nF' ? ' selected' : '') + '>nF</option>';
html += '<option value="pF"' + (defUnit === 'pF' ? ' selected' : '') + '>pF</option>';
html += '</select><br>';
}
html += '<br><button onclick="calculate()">Calculate</button>';
document.getElementById('inputs-area').innerHTML = html;
}
function toFarads(val, unit) {
if (unit === 'uF') return val * 1e-6;
if (unit === 'nF') return val * 1e-9;
return val * 1e-12;
}
function formatF(f) {
var uF = f * 1e6, nF = f * 1e9, pF = f * 1e12;
if (uF >= 1) return { val: uF, label: 'uF' };
if (nF >= 1) return { val: nF, label: 'nF' };
return { val: pF, label: 'pF' };
}
function prettyNum(n) {
return parseFloat(parseFloat(n).toPrecision(6)).toLocaleString();
}
function calculate() {
var n = numCapacitors;
var mode = document.getElementById('mode').value;
var vals = [];
var labels = [];
for (var i = 1; i <= n; i++) {
var el = document.getElementById('cv-' + i);
var uel = document.getElementById('cu-' + i);
if (!el || !uel) { showError('Please click "Set capacitors" before calculating.'); return; }
var v = parseFloat(el.value);
var u = uel.value;
if (el.value === '' || isNaN(v) || v < 0) {
showError('Please enter a valid non-negative value for C' + i + '.');
return;
}
vals.push(toFarads(v, u));
labels.push(v + ' ' + u);
}
clearError();
var totalF;
if (mode === 'parallel') {
totalF = 0;
for (var j = 0; j < vals.length; j++) totalF += vals[j];
} else {
for (var k = 0; k < vals.length; k++) {
if (vals[k] === 0) { showError('Series capacitance with a 0 F capacitor is undefined.'); return; }
}
var sumRecip = 0;
for (var m = 0; m < vals.length; m++) sumRecip += 1 / vals[m];
totalF = 1 / sumRecip;
}
var fmt = formatF(totalF);
document.getElementById('C_out').textContent = prettyNum(fmt.val) + ' ' + fmt.label;
if(window.drawDiagram) window.drawDiagram();
}
function showError(msg) {
document.getElementById('error').textContent = msg;
}
function clearError() {
document.getElementById('error').textContent = '';
}
buildInputs();