-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
425 lines (375 loc) · 15.2 KB
/
script.js
File metadata and controls
425 lines (375 loc) · 15.2 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
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
// Global variables
let map;
let currentSystemType = 'grid-tied';
let monthlyChart, roiChart, costChart;
let currentLocation = { lat: 40.7128, lng: -74.0060, irradiance: 4.5 }; // Default: New York
// Solar irradiance data by location (simplified)
const solarData = {
'phoenix': { lat: 33.4484, lng: -112.0740, irradiance: 6.5 },
'miami': { lat: 25.7617, lng: -80.1918, irradiance: 5.8 },
'denver': { lat: 39.7392, lng: -104.9903, irradiance: 5.5 },
'seattle': { lat: 47.6062, lng: -122.3321, irradiance: 3.4 },
'chicago': { lat: 41.8781, lng: -87.6298, irradiance: 4.2 },
'utah': { lat: 39.5501, lng: -111.8947, irradiance: 5.8 }
};
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
initializeMap();
initializeSliders();
initializeCharts();
calculateSystem();
});
// Initialize the map
function initializeMap() {
map = L.map('map').setView([currentLocation.lat, currentLocation.lng], 10);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// Add marker for current location
L.marker([currentLocation.lat, currentLocation.lng])
.addTo(map)
.bindPopup('Solar Resource: ' + currentLocation.irradiance + ' kWh/m²/day')
.openPopup();
}
// Initialize sliders with event listeners
function initializeSliders() {
const sliders = [
'consumption', 'roofArea', 'budget', 'electricityRate',
'batteryCapacity', 'backupDays', 'panelEfficiency',
'systemLosses', 'panelCost', 'batteryCost'
];
sliders.forEach(sliderId => {
const slider = document.getElementById(sliderId);
const valueSpan = document.getElementById(sliderId + 'Value');
slider.addEventListener('input', function() {
let value = parseFloat(this.value);
let formattedValue = value;
// Format values based on type
if (sliderId.includes('Cost') || sliderId === 'electricityRate') {
formattedValue = '$' + value.toFixed(2);
} else if (sliderId.includes('Efficiency') || sliderId.includes('Losses')) {
formattedValue = value + '%';
} else if (sliderId.includes('Area')) {
formattedValue = value + ' m²';
} else if (sliderId.includes('Capacity')) {
formattedValue = value + ' kWh';
} else if (sliderId.includes('Days')) {
formattedValue = value + ' days';
} else if (sliderId === 'consumption') {
formattedValue = value + ' kWh';
} else if (sliderId === 'budget') {
formattedValue = '$' + value.toLocaleString();
}
valueSpan.textContent = formattedValue;
calculateSystem();
});
});
}
// Search location function
function searchLocation() {
const location = document.getElementById('location').value.toLowerCase();
if (solarData[location]) {
currentLocation = solarData[location];
updateLocationData();
map.setView([currentLocation.lat, currentLocation.lng], 10);
// Clear existing markers
map.eachLayer(layer => {
if (layer instanceof L.Marker) {
map.removeLayer(layer);
}
});
// Add new marker
L.marker([currentLocation.lat, currentLocation.lng])
.addTo(map)
.bindPopup('Solar Resource: ' + currentLocation.irradiance + ' kWh/m²/day')
.openPopup();
calculateSystem();
} else {
alert('Location not found. Try: phoenix, miami, denver, seattle, chicago, utah');
}
}
// Update location data display
function updateLocationData() {
document.getElementById('irradiance').textContent = currentLocation.irradiance + ' kWh/m²/day';
document.getElementById('peakSunHours').textContent = currentLocation.irradiance + ' hours';
}
// Select system type
function selectSystemType(type) {
currentSystemType = type;
// Update button states
document.querySelectorAll('.system-btn').forEach(btn => {
btn.classList.remove('active');
});
event.target.classList.add('active');
// Show/hide battery section
const batterySection = document.getElementById('batterySection');
if (type === 'off-grid' || type === 'hybrid') {
batterySection.classList.remove('hidden');
} else {
batterySection.classList.add('hidden');
}
calculateSystem();
}
// Main calculation function
function calculateSystem() {
const inputs = getInputs();
const results = performCalculations(inputs);
updateResults(results);
updateCharts(results);
}
// Get all input values
function getInputs() {
return {
consumption: parseFloat(document.getElementById('consumption').value),
roofArea: parseFloat(document.getElementById('roofArea').value),
budget: parseFloat(document.getElementById('budget').value),
electricityRate: parseFloat(document.getElementById('electricityRate').value),
batteryCapacity: parseFloat(document.getElementById('batteryCapacity').value),
backupDays: parseFloat(document.getElementById('backupDays').value),
panelEfficiency: parseFloat(document.getElementById('panelEfficiency').value),
systemLosses: parseFloat(document.getElementById('systemLosses').value),
panelCost: parseFloat(document.getElementById('panelCost').value),
batteryCost: parseFloat(document.getElementById('batteryCost').value),
irradiance: currentLocation.irradiance,
systemType: currentSystemType
};
}
// Perform all calculations
function performCalculations(inputs) {
// Calculate required system size
const dailyProduction = inputs.consumption;
const systemEfficiency = (100 - inputs.systemLosses) / 100;
const systemSize = dailyProduction / (inputs.irradiance * systemEfficiency); // kW
// Calculate panel count (assuming 400W panels)
const panelWattage = 0.4; // 400W panels
const panelCount = Math.ceil(systemSize / panelWattage);
// Check roof area constraint
const panelArea = 2.0; // m² per panel
const requiredArea = panelCount * panelArea;
const areaConstrained = requiredArea > inputs.roofArea;
if (areaConstrained) {
const maxPanels = Math.floor(inputs.roofArea / panelArea);
const actualSystemSize = maxPanels * panelWattage;
const actualProduction = actualSystemSize * inputs.irradiance * systemEfficiency;
return calculateCosts({
...inputs,
systemSize: actualSystemSize,
panelCount: maxPanels,
annualProduction: actualProduction * 365,
energyDeficit: dailyProduction - actualProduction
});
}
return calculateCosts({
...inputs,
systemSize: systemSize,
panelCount: panelCount,
annualProduction: dailyProduction * 365,
energyDeficit: 0
});
}
// Calculate system costs
function calculateCosts(params) {
const panelCost = params.panelCount * 400 * params.panelCost; // 400W panels
const inverterCost = params.systemSize * 0.15 * 1000; // $0.15/W
const installationCost = params.systemSize * 0.50 * 1000; // $0.50/W
const otherCosts = params.systemSize * 0.25 * 1000; // $0.25/W (mounting, wiring, etc.)
let batteryCost = 0;
if (params.systemType === 'off-grid' || params.systemType === 'hybrid') {
batteryCost = params.batteryCapacity * params.batteryCost;
}
const totalCost = panelCost + inverterCost + installationCost + otherCosts + batteryCost;
// Calculate annual savings
const annualSavings = params.annualProduction * params.electricityRate;
const paybackPeriod = totalCost / annualSavings;
// Calculate LCOE (25-year lifetime)
const lcoe = totalCost / (params.annualProduction * 25);
return {
systemSize: params.systemSize,
panelCount: params.panelCount,
totalCost: totalCost,
annualProduction: params.annualProduction,
paybackPeriod: paybackPeriod,
lcoe: lcoe,
annualSavings: annualSavings,
panelCost: panelCost,
inverterCost: inverterCost,
installationCost: installationCost,
batteryCost: batteryCost,
otherCosts: otherCosts,
energyDeficit: params.energyDeficit || 0
};
}
// Update results display
function updateResults(results) {
document.getElementById('systemSize').textContent = results.systemSize.toFixed(1) + ' kW';
document.getElementById('panelCount').textContent = results.panelCount;
document.getElementById('totalCost').textContent = '$' + results.totalCost.toLocaleString();
document.getElementById('paybackPeriod').textContent = results.paybackPeriod.toFixed(1);
document.getElementById('annualProduction').textContent = results.annualProduction.toLocaleString();
document.getElementById('lcoe').textContent = '$' + results.lcoe.toFixed(3);
}
// Initialize charts
function initializeCharts() {
// Monthly Energy Chart
const monthlyCtx = document.getElementById('monthlyChart').getContext('2d');
monthlyChart = new Chart(monthlyCtx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [{
label: 'Solar Production',
data: [],
borderColor: 'rgb(255, 206, 84)',
backgroundColor: 'rgba(255, 206, 84, 0.2)',
tension: 0.1
}, {
label: 'Energy Consumption',
data: [],
borderColor: 'rgb(54, 162, 235)',
backgroundColor: 'rgba(54, 162, 235, 0.2)',
tension: 0.1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Energy (kWh)'
}
}
}
}
});
// ROI Chart
const roiCtx = document.getElementById('roiChart').getContext('2d');
roiChart = new Chart(roiCtx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Cumulative Savings',
data: [],
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
tension: 0.1
}, {
label: 'Initial Investment',
data: [],
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
tension: 0.1
}]
},
options: {
responsive: true,
scales: {
y: {
title: {
display: true,
text: 'Cumulative ($)'
}
}
}
}
});
// Cost Breakdown Chart
const costCtx = document.getElementById('costChart').getContext('2d');
costChart = new Chart(costCtx, {
type: 'doughnut',
data: {
labels: ['Solar Panels', 'Inverter', 'Installation', 'Battery', 'Other'],
datasets: [{
data: [],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)',
'rgb(75, 192, 192)',
'rgb(153, 102, 255)'
]
}]
},
options: {
responsive: true,
plugins: {
legend: {
position: 'bottom'
}
}
}
});
}
// Update charts with new data
function updateCharts(results) {
const inputs = getInputs();
// Monthly production data (simplified seasonal variation)
const monthlyMultipliers = [0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7];
const monthlyProduction = monthlyMultipliers.map(m =>
results.annualProduction / 12 * m
);
const monthlyConsumption = Array(12).fill(inputs.consumption * 30);
monthlyChart.data.datasets[0].data = monthlyProduction;
monthlyChart.data.datasets[1].data = monthlyConsumption;
monthlyChart.update();
// ROI data
const years = Array.from({length: 26}, (_, i) => i);
const cumulativeSavings = years.map(year =>
year * results.annualSavings - results.totalCost
);
const investment = Array(26).fill(-results.totalCost);
roiChart.data.labels = years;
roiChart.data.datasets[0].data = cumulativeSavings;
roiChart.data.datasets[1].data = investment;
roiChart.update();
// Cost breakdown
costChart.data.datasets[0].data = [
results.panelCost,
results.inverterCost,
results.installationCost,
results.batteryCost,
results.otherCosts
];
costChart.update();
}
// What-if scenario analysis
function runScenario(scenario) {
const originalInputs = getInputs();
let scenarioInputs = {...originalInputs};
let scenarioName = '';
switch(scenario) {
case 'efficiency':
scenarioInputs.panelEfficiency += 5;
scenarioName = '+5% Panel Efficiency';
break;
case 'battery':
scenarioInputs.batteryCost *= 0.5;
scenarioName = '50% Battery Cost Reduction';
break;
case 'hybrid':
scenarioInputs.systemType = 'hybrid';
scenarioName = 'Add Battery Storage';
break;
case 'tariff':
scenarioInputs.electricityRate *= 1.25;
scenarioName = '+25% Electricity Rate';
break;
}
const originalResults = performCalculations(originalInputs);
const scenarioResults = performCalculations(scenarioInputs);
const costDiff = scenarioResults.totalCost - originalResults.totalCost;
const paybackDiff = scenarioResults.paybackPeriod - originalResults.paybackPeriod;
const resultsDiv = document.getElementById('scenarioResults');
resultsDiv.innerHTML = `
<h4>${scenarioName} Impact:</h4>
<p><strong>Cost Change:</strong> ${costDiff >= 0 ? '+' : ''}$${costDiff.toLocaleString()}</p>
<p><strong>Payback Change:</strong> ${paybackDiff >= 0 ? '+' : ''}${paybackDiff.toFixed(1)} years</p>
<p><strong>New LCOE:</strong> $${scenarioResults.lcoe.toFixed(3)}/kWh</p>
<p><strong>Annual Production:</strong> ${scenarioResults.annualProduction.toLocaleString()} kWh</p>
`;
}
// Initialize location data on page load
updateLocationData();