-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc.js
283 lines (248 loc) · 8.72 KB
/
calc.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
function addCitySuggestions(){
let datalist = document.getElementById("cityOptions");
for (const cityData of temperatureDataSet){
let option = document.createElement("option");
option.setAttribute("value", cityData[1]);
datalist.appendChild(option);
}
}
function onLoad(){
addCitySuggestions();
}
// Sources
// https://www.eia.gov/energyexplained/units-and-calculators/
const kWhPerUnit = {
// Natural Gas
"m3": 10.55,
"ft3": 0.3045008,
"therm": 29.307107,
// Propane
"L": 7.08,
"gal": 26.801936,
// Oil
"oil_L": 10.35,
"oil_gal": 40.6460267,
// Electric Resistance
"kWh": 1,
}
function hideAllChildren(element){
for (let i = 0; i < element.children.length; i++) {
element.children[i].style.display = "none";
}
}
function displayIds(ids){
for (const id of ids){
document.getElementById(id).style.display = "block";
}
}
function setExisting(){
let existingHeat = document.getElementById("existing").value;
let unitSelect = document.getElementById("unit");
unitSelect.value = "";
hideAllChildren(unitSelect);
if (existingHeat === "0"){ // Natural Gas
let emptyOption = document.getElementById("emptyOption");
emptyOption.style.display = "block";
displayIds(["m3", "therm", "ft3"]);
let existingPrice = document.getElementById("existingPrice");
existingPrice.style.display = "flex";
let existingFurnace = document.getElementById("existingFurnace");
existingFurnace.style.display = "block";
}
else if (existingHeat === "1" || existingHeat === "2"){
displayIds(["L", "gal"]);
let existingPrice = document.getElementById("existingPrice");
existingPrice.style.display = "flex";
let existingFurnace = document.getElementById("existingFurnace");
existingFurnace.style.display = "block";
}
else if (existingHeat === "3"){
let existingPrice = document.getElementById("existingPrice");
existingPrice.style.display = "none";
let existingFurnace = document.getElementById("existingFurnace");
existingFurnace.style.display = "none";
}
}
function getTemperatureData(city){
for (const cityData of temperatureDataSet){
if (cityData[1] === city){
return cityData;
}
}
}
function getNumberOfHeatingMonths(temperatureData){
let count = 0;
// The first two items are country and city
const startIndex = 2;
// We don't care about the last two elements
const endIndex = temperatureData.length - 2;
// Temperature to stop heating
let stopHeatingTemp = 15;
let partialHeatingTemp = 10;
for(let i=startIndex;i<endIndex;i++){
// Populate table
document.getElementById("m_" + (i - startIndex)).innerText = temperatureData[i];
if (temperatureData[i] <= partialHeatingTemp){
count += 1;
document.getElementById("mh_" + (i - startIndex)).innerText = "*";
}
else if (temperatureData[i] <= stopHeatingTemp){
count += 0.5;
document.getElementById("mh_" + (i - startIndex)).innerText = "*";
}
}
return count;
}
function calculateCOP(){
// Cooling
let seer2 = document.getElementById("seer2").value;
let heatPumpSEER2 = document.getElementById("heatPumpSEER2").value;
let heatPumpSEER2Unit = document.getElementById("heatPumpSEER2Unit").value;
let acCOP;
let hpAcCOP;
if (seer2 && heatPumpSEER2 && heatPumpSEER2Unit){
// To convert SEER to COP, multiply by 0.293 or 1055/3600
// SEER vs SEER2: SEER2 will just be lower (more correct)
acCOP = seer2 * (1055/3600);
if(heatPumpSEER2Unit === "SEER2"){
hpAcCOP = heatPumpSEER2 * (1055/3600);
}
else if (heatPumpSEER2Unit === "COP"){
hpAcCOP = heatPumpSEER2;
}
let acText = "";
let acRatio = acCOP / hpAcCOP;
if (hpAcCOP > acCOP) {
acText = ((1 - acRatio) * 100).toFixed(0) + "% LESS";
}
else if(hpAcCOP < acCOP){
acText = ((acRatio - 1) * 100).toFixed(0) + "% MORE";
}
else {
acText = " the same";
}
document.getElementById("acCostMultiplier").innerText = acText;
}
let price = Number(document.getElementById("price").value.replaceAll("$", ""));
let unit = document.getElementById("unit").value;
let efficiency = Number(document.getElementById("efficiency").value);
let electricity_price = Number(document.getElementById("electricity_price").value.replaceAll("$", ""));
let efficiencyAsDecimal;
let equivalentCOP;
let realkWhPerUnit;
let costPerkWhHeat;
let existing = document.getElementById("existing").value;
if (existing === "3"){ // Electric Resistance
equivalentCOP = 1;
unit = "kWh";
efficiencyAsDecimal = 1;
price = electricity_price;
realkWhPerUnit = kWhPerUnit[unit] * efficiencyAsDecimal;
costPerkWhHeat = price / realkWhPerUnit;
}
else if (price && unit && efficiency){
if (unit == "L" && existing === "2"){
unit = "oil_L";
}
else if(unit == "gal" && existing == "2"){
unit = "oil_gal";
}
efficiencyAsDecimal = efficiency * 0.01;
realkWhPerUnit = kWhPerUnit[unit] * efficiencyAsDecimal;
costPerkWhHeat = price / realkWhPerUnit;
equivalentCOP = electricity_price / costPerkWhHeat;
}
else {
return;
}
document.getElementById("cop").innerText = equivalentCOP.toFixed(2);
let heatPumpSpec = document.getElementById("heatPump").value;
let heatPumpSpecUnit = document.getElementById("heatPumpUnit").value;
if(!heatPumpSpecUnit){
return;
}
let hpCOP = heatPumpSpec;
if (heatPumpSpecUnit === "HSPF"){
hpCOP = hpCOP / 3.41;
}
let ratio = equivalentCOP / hpCOP;
let text = "";
if(hpCOP > equivalentCOP){
text = ((1 - ratio) * 100).toFixed(0) + "% LESS";
}
else if(hpCOP < equivalentCOP){
text = ((ratio - 1) * 100).toFixed(0) + "% MORE";
}
else {
text = " the same";
}
document.getElementById("costMultiplier").innerText = text;
let yearlyDifference = 0;
let fuelused = document.getElementById("fuelUsed").value;
let costHeatingSeasonExisting = 0;
let costHeatingSeasonHP = 0;
if (fuelused){
costHeatingSeasonExisting = fuelused * price;
let heatNeeded_kWh = fuelused * kWhPerUnit[unit] * efficiencyAsDecimal;
// reduce heatNeeded_kWh by COP then multiple by price
costHeatingSeasonHP = (heatNeeded_kWh / hpCOP) * electricity_price;
let seasonalSavingsText = 'Every heating season with that heat pump will ';
if (costHeatingSeasonExisting > costHeatingSeasonHP) {
yearlyDifference = costHeatingSeasonExisting - costHeatingSeasonHP;
seasonalSavingsText += 'save $' + yearlyDifference.toFixed(2);
}
else if (costHeatingSeasonExisting < costHeatingSeasonHP){
yearlyDifference = costHeatingSeasonHP - costHeatingSeasonExisting;
seasonalSavingsText += 'cost $' + yearlyDifference.toFixed(2) + ' more.';
}
else {
seasonalSavingsText += 'cost the same.';
}
document.getElementById("seasonalSavings").innerText = seasonalSavingsText;
}
else {
let energyPerHour = document.getElementById("heatLoss").value;
let energyPerHourUnit = document.getElementById("heatLossUnit").value;
if (!energyPerHour || !energyPerHourUnit){
return;
}
if (energyPerHourUnit === "BTUh"){
energyPerHour = energyPerHour / 3412;
}
let otherCostPerHour = energyPerHour * costPerkWhHeat;
let hpCostPerHour = (energyPerHour / hpCOP) * electricity_price;
let hourlyDifference;
hourlyDifference = otherCostPerHour - hpCostPerHour;
document.getElementById("hourlyDifference").innerText = hourlyDifference.toFixed(2);
let monthlyDifference = hourlyDifference * 24 * 30;
document.getElementById("monthlyDifference").innerText = monthlyDifference.toFixed(0);
let city = document.getElementById("city").value;
if(!city){
return;
}
let temperatureData = getTemperatureData(city);
if (temperatureData){
let numMonthsOfHeat = getNumberOfHeatingMonths(temperatureData);
document.getElementById("numHeatingMonths").innerText = numMonthsOfHeat;
if (numMonthsOfHeat > 0){
yearlyDifference = monthlyDifference * numMonthsOfHeat;
}
}
}
document.getElementById("yearlyDifference").innerText = yearlyDifference.toFixed(0);
let heatPumpCost = document.getElementById("heatPumpCost").value;
let otherCost = Number(document.getElementById("otherCost").value);
if (Number.isFinite(otherCost)){
heatPumpCost -= otherCost;
}
if ((!fuelused && yearlyDifference > 0)
|| costHeatingSeasonExisting > costHeatingSeasonHP){
let breakEvenYear = heatPumpCost / yearlyDifference;
document.getElementById("breakEvenYear").innerText = breakEvenYear.toFixed(2);
document.getElementById("breakEvenParagraph").style.visibility = "visible";
}
else {
document.getElementById("breakEvenParagraph").style.visibility = "hidden";
}
}
document.addEventListener('DOMContentLoaded', onLoad, false);