-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICSolar.py
523 lines (421 loc) · 19.6 KB
/
ICSolar.py
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import src.icsolar as icsolar
import src.casegeom as casegeom
import src.shading as shading
import src.weather as weather
import src.solar as solar
import src.icsolar_support as support
import src.base.geom as sgeom
import test
import numpy as np
import time as cputime
import matplotlib.pyplot as plt
import os
import sys
from joblib import Parallel, delayed
"""
This file contains an example run for ICSolar with geometry
It relies on the icsolar problem setup in src.icsolar,
as well as support functions in other files.
All results will end up in Results/<directory name>
There are three ways to get data out of the simulation.
The names of this data is in the solverInputs <name>DataNames
There are Paraview VTK files for each module on the facade,
the data of which is dictated in the geometry, g.data
There are facadeData with facadeDataNames which are per facade
There is directionData with directionDataNames which are per direction
Each of these is defaulted to zero and needs to be
filled inside the solve function
"""
"""
This is used to disable the warnings which occur when
solving an ill-conditioned problem
such as DNI ~ 0.
The warning:
minpack.py:236: RuntimeWarning: The iteration is not making
good progress, as measured by the
improvement from the last ten iterations.
is fairly common, and as such is filtered out
"""
import warnings
warnings.filterwarnings("ignore", \
message = 'The iteration is not making good progress')
"""
This takes in two sets of inputs (see __main__ at the bottom)
and is used per process, in parallel
it also calls icsolar.solve and handles
solving at each timestep, and keeping track of data
This is where data gets created and stored
"""
def solve(problemInputs,solverInputs):
############################################### take data from problemInputs
geometry = problemInputs['geometry']
useSunlitFraction = problemInputs['useSunlitFraction']
directory = problemInputs['directory']
startDay = problemInputs['range'][0]
endDay = problemInputs['range'][1]
DNI = problemInputs['DNI']
exteriorAirTemp = problemInputs['exteriorAirTemp']
DHI = problemInputs['DHI']
stepsPerDay = problemInputs['stepsPerDay']
days = endDay - startDay
timesteps = days*stepsPerDay
startStep = startDay*stepsPerDay
endStep = (days+startDay)*stepsPerDay
############################################### set up results
# this bins things by facade direction
bins = set([g.dir for g in geometry]);
bins = list(bins)
bins.append('total')
directionData = {name:{b:np.zeros(timesteps) for b in bins} \
for name in solverInputs['directionDataNames']}
# this collects things by facade number
facadeBins = range(len(geometry))
facadeData = {name:[] for name in solverInputs['facadeDataNames']}
for name in solverInputs['facadeDataNames']:
for i in range(len(geometry)):
facadeData[name].append(np.zeros(timesteps))
############################################### pre-computed information
# this is a lookup table to connect each module to a shading table
shadingIndices = []
for i in range(len(geometry)):
shadingIndices.append([])
for g in geometry:
if g.facadeType == 'window':
for i in range(g.nY):
shadingIndices[geometry.index(g)].append \
(shading.getShadingIndex(0,i,1,g.nY))
############################################### daytime setup
# idea is that we don't solve when its not daytime
daytime = False
previousDayTime = False
############################################### solver
clockStart = cputime.time()
for ts in range(startStep,endStep):
# its daytime if DNI > 0 for three straight hours
# this logic is suspect at best
if(1 < ts and ts < endStep-1):
daytime = (DNI[ts-startStep-1]+DNI[ts-startStep]+DNI[ts-startStep+1] > 0)
# once the first daytime hits, we are done initializing
time = solverInputs['dt']*ts
solverInputs['exteriorAirTemp'] = exteriorAirTemp[ts-startStep]
sunPosition = solar.getSunPosition(time)
for g in geometry:
if g.facadeType != 'window':
continue
index = geometry.index(g)
matches = geometry.getMatches(g)
# if there are no matches, or the first match index is greater than
# this index, it is the first one that needs to be solved
# otherwise, don't waste time solving this
if not matches or geometry.index(matches[0]) > index:
# averaged -> shading vector applied uniformly, not to top or bottom
if useSunlitFraction is True:
(sunlit,averaged) = shading.getMeanSunlitFraction(geometry,g,time,solverInputs['dt'],5)
else:
# if not using sunlit fraction, then just pick 0 or 1
averaged = True
if(DNI[ts-startStep] == 0 and DHI[ts-startStep] == 0):
sunlit = 0.
else:
sunlit = 1.0
# iterate over modules, from bottom to top,
# computing local shading fractions
shade = np.ones(g.nY)
for m in range(g.nY):
shade[m] = shading.getUnshadedFraction(sunPosition,g,
shadingIndices[index][m])
glaze = max(solar.getGlazingTransmitted(sunPosition,g,1),0)
AOI = solar.getAOI(sunPosition,g)
(pitch,yaw) = solar.getArrayAngle(sunPosition,g)
# lets collect some data
facadeData['glaze'][index][ts-startStep] = glaze
facadeData['aoi'][index][ts-startStep] = AOI
facadeData['yaw'][index][ts-startStep] = yaw
facadeData['pitch'][index][ts-startStep] = pitch
facadeData['shade'][index][ts-startStep] = shade[int(g.nY/2)] # shading in the middle
facadeData['dni'][index][ts-startStep] = DNI[ts-startStep]
# handle the matching facades, the ones we wont solve for, but make sure their data is
# properly stored
for name in ['glaze','aoi','yaw','pitch','shade','dni']:
for match in matches:
facadeData[name][geometry.index(match)] = facadeData[name][index]
# set up air temperature
g.data['externalAirT'] = np.ones(g.nY)*exteriorAirTemp[ts-startStep]
# don't solve this facade if the sun can't see it, but zero
# all data
if(not daytime):
for name in g.data:
g.data[name] = np.zeros(g.nY)
continue
# energy per cell in the middle module
facadeData['epc'][index][ts-startStep] = 0.866*625.5*0.0001*g.data['DNIatModule'][int(g.nY/2)]
shadedVector = shading.applySunlitFraction(sunlit,g,averaged)
g.data['DNI'] = DNI[ts-startStep]*shadedVector
g.data['DHI'] = DHI[ts-startStep]*shadedVector
g.data['Glazing'] = glaze*shadedVector
g.data['DNIatModule'] = g.data['DNI']*glaze*shade
g.data['DHIatModule'] = g.data['DHI']*glaze*shade*0.5*(1.+np.sin(g.tilt))
# solverInputs['Q_w'] = 0.024801*(0.66)*1e-3*g.data['DNIatModule']
solverInputs['Q_d'] = 0.866*625.5*0.0001*g.data['DNIatModule']*(1.-solverInputs['eta'])
solverInputs['Q_a'] = np.zeros(g.nY)
# (Q_c,Q_I) = support.getRadiativeGain(sunPosition,g,g.data['DNIatModule'],g.data['DHIatModule'])
# solverInputs['Q_c'] = g.data['DHIatModule']*icsolar.moduleHeight*icsolar.moduleWidth
solverInputs['numModules'] = g.nY
# set up previous temperature
if (not previousDayTime):
solverInputs['previousWaterModuleT'] = solverInputs['inletWaterTemp']*np.ones(g.nY)
solverInputs['previousWaterTubeT'] = solverInputs['inletWaterTemp']*np.ones(g.nY)
else:
solverInputs['previousWaterModuleT'] = g.data['waterModuleT']
solverInputs['previousWaterTubeT'] = g.data['waterTubeT']
# solve the problem
results = icsolar.solve(solverInputs)
# process results for storage
g.data['waterModuleT'] = results['waterModule']
g.data['waterTubeT'] = results['waterTube']
g.data['inletAirTemp'] = results['airModule']
g.data['receiverT'] = results['receiver']
# electrical calculations
electData = np.sum(solverInputs['eta'] \
*1e-3*0.866*625.5*0.0001*g.data['DNIatModule'])*g.nX
directionData['elect'][g.dir][ts-startStep] += electData*(1+len(matches))
facadeData['elect'][index][ts-startStep] = electData
facadeData['waterModule'][index][ts-startStep] = results['waterModule'][-1]
# thermal calculations
thermalData = np.sum(solverInputs['waterFlowRate']*4.218 \
*(g.data['waterModuleT']-g.data['waterTubeT']))*g.nX
g.data['thermal'] = solverInputs['waterFlowRate']*4.218 \
*(g.data['waterModuleT']-g.data['waterTubeT'])
# only add the contribution if its positive
if(thermalData > 0):
directionData['thermal'][g.dir][ts-startStep] += thermalData*(1+len(matches))
facadeData['thermal'][index][ts-startStep] = thermalData
# fill in matching data for thermal, electrical, and epc
for name in ['thermal','elect','epc','waterModule']:
for match in matches:
facadeData[name][geometry.index(match)] = facadeData[name][index]
# done with the step
previousDayTime = daytime
# set up directional data by summing over things
directionData['thermal']['total'][ts-startStep] = \
sum([directionData['thermal'][b][ts-startStep] for b in bins if b is not 'total'])
directionData['elect']['total'][ts-startStep] = \
sum([directionData['elect'][b][ts-startStep] for b in bins if b is not 'total'])
# post processing and cleanup
if(daytime and problemInputs['writeVTKFiles']):
casegeom.writeVTKfile(geometry,'Results/'+directory+'/VTK/geom'+'0'*(4-len(str(ts)))+str(ts)+'.vtk','')
print "runtime for days",startDay,"to",startDay+days-1,":",'%.2f' % (cputime.time()-clockStart)
return (directionData,facadeData)
"""
This is a load balancing step, because dividing up the days by the number of processors
isn't ideal, we can do a lot better with a little bit of work. The assumption is that
the workload/runtime is proportional to the number of hours of sunlight,
so we divide everything into days, getting a rough balance of work. As
there are more hours in the summer, this generally leads to an uneven split,
for example, NYC with 4 processors splits as [(0, 102), (102, 181), (181, 261), (261, 365)]
with the first 102 days on proc, the next 79 on one, the next 80 on another, and the last 104
on the final processor
"""
def loadBalance(days,startDay,numProc,DNI):
hoursPerProc = np.count_nonzero(DNI[startDay*24:((startDay+days)*24)])/numProc
pairs = []
procStartDay = startDay
endDay = startDay
for i in range(numProc-1):
hours = 0
while hours < hoursPerProc and endDay < startDay+days-1:
hours += np.count_nonzero(DNI[endDay*24:(endDay+1)*24])
endDay = endDay+1
pairs.append((procStartDay,endDay))
procStartDay = endDay
pairs.append((procStartDay,startDay+days))
return pairs
"""
This is the base program, after main is called, that organizes all the parallel data
collects information, makes a few plots, and sets up parameters for each run,
and saves all the files
This also does the parallelization, load balancing, time zones, etc
"""
def run(init,solverInputs):
############################################### data loading
if init['TMY'].endswith('.csv'):
data = weather.readTMY(init['TMY'])
elif init['TMY'].endswith('.epw'):
data = weather.readEPW(init['TMY'])
else:
sys.exit('The weather file was not loaded correctly. Please provide either an .epw file from \
https://energyplus.net/weather or a .csv file from http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/by_state_and_city.html')
DNI = data['DNI']
DHI = data['DHI']
exteriorAirTemp = data['airTemp']
solar.setTimezone(data['timezone'])
solar.setLocation(data['lat'],data['lon'])
# set up geometry
geometry = casegeom.readFile(init['geomfile'],"ICSolar",
init['useSunlitFraction'])
geometry.computeBlockCounts(icsolar.moduleHeight,icsolar.moduleWidth)
geometry.initializeData(solverInputs['moduleDataNames'])
casegeom.tiltGeometry(geometry,init['tilt'],init['useSunlitFraction'])
stepsPerDay = 24
timesteps = init['days']*stepsPerDay
startStep = init['startDay']*stepsPerDay
endStep = (init['days']+init['startDay'])*stepsPerDay
procSplit = loadBalance(init['days'],init['startDay'],init['numProcs'],DNI)
clockStart = cputime.time()
############################################### set up results
# create the directories
if not os.path.exists('Results/'+init['directory']):
os.makedirs('Results/'+init['directory'])
if ( init['writeVTKFiles'] ):
if not os.path.exists('Results/'+init['directory']+'/VTK'):
os.makedirs('Results/'+init['directory']+'/VTK')
# this bins things by facade direction,
# it is directionData['variablename']['direction'][data]
# and for facadeData['variablename'][index][data]
bins = list(set([g.dir for g in geometry]));
bins.append('total')
directionData = {name:{b:np.zeros(timesteps) for b in bins} \
for name in solverInputs['directionDataNames']}
# this collects things by facade number
facadeData = {name:[] for name in solverInputs['facadeDataNames']}
for name in solverInputs['facadeDataNames']:
for i in range(len(geometry)):
facadeData[name].append(np.zeros(timesteps))
area = geometry.getAreas()
############################################### run in parallel
"""
This is the tricky pair, the processor splits are in procSplit, and each portion of the weather data
thats needed is passed into the solver itself, so theres a lot of offsetting to be done,
This sets up the inputs to pass into the solver itself
"""
problemInputs = []
for (start,end) in procSplit:
stepRange = slice(start*stepsPerDay,end*stepsPerDay)
inputDict = {
'range':(start,end),
'directory':init['directory'],
'geometry':geometry,
'useSunlitFraction':init['useSunlitFraction'],
'writeVTKFiles':init['writeVTKFiles'],
'DNI':DNI[stepRange],
'exteriorAirTemp':exteriorAirTemp[stepRange],
'DHI':DHI[stepRange],
'stepsPerDay':init['stepsPerDay'],
}
problemInputs.append(inputDict)
results = Parallel(n_jobs = init['numProcs'])(delayed(solve)(problemInputs[i],solverInputs) for i in range(init['numProcs']))
print data['city'],'final runtime is','%.2f' % (cputime.time()-clockStart)
############################################### collapse results
for i in range(init['numProcs']):
(start,end) = procSplit[i]
resultRange = slice((start-init['startDay'])*stepsPerDay,(end-init['startDay'])*stepsPerDay)
for b in bins:
for name in solverInputs['directionDataNames']:
directionData[name][b][resultRange] = results[i][0][name][b]
for j in range(len(geometry)):
for name in solverInputs['facadeDataNames']:
facadeData[name][j][resultRange] = results[i][1][name][j]
############################################### write Data Files if requested
if init['writeDataFiles']:
outputfiles = {}
outputFacadeFiles = {}
# put headers into files
for b in bins:
outputfiles[b] = (open('Results/'+init['directory']+'/output_'+b+'.txt','w'))
outputfiles[b].write('# time,'+','.join(solverInputs['directionDataNames'])+'\n')
for j in range(len(geometry)):
outputFacadeFiles[j] = (open('Results/'+init['directory']+'/output_'+str(j)+'.txt','w'))
outputFacadeFiles[j].write('# time,'+','.join(solverInputs['facadeDataNames'])+'\n')
# write the data
for ts in range(startStep,endStep):
for b in bins:
outputfiles[b].write(str(ts*solverInputs['dt'])+','+','.join(['%.3e' % directionData[name][b][ts-startStep] \
for name in solverInputs['directionDataNames']])+'\n')
for j in range(len(geometry)):
outputFacadeFiles[j].write(str(ts*solverInputs['dt'])+','+','.join(['%.3e' % facadeData[name][j][ts-startStep] \
for name in solverInputs['facadeDataNames']])+'\n')
# close the files
for b in bins:
outputfiles[b].close()
for j in range(len(geometry)):
outputFacadeFiles[j].close()
# generate some plots in the Results/ directory
# check the data is available
if ('thermal' in directionData and 'elect' in directionData):
linestyles = {'south':'--sk', 'roof':'--k', 'east':'--xk', 'west':'--ok', 'total':'-k'}
plt.subplot(1,3,1)
for b in bins:
plt.plot(np.arange(startStep,endStep)/stepsPerDay,np.cumsum(directionData['thermal'][b])/area[b],linestyles[b],
linewidth=2.0,label=b,markevery=30*stepsPerDay,markersize=7,fillstyle='none',markeredgewidth=2)
axes = plt.gca()
axes.tick_params(axis='both', which='major', labelsize=18)
plt.legend(loc='upper left',fontsize=18)
plt.ylabel('Thermal Output ($kWh/m^2$)',fontsize=18), plt.xlabel('Time (Days)',fontsize=18)
plt.xlim(init['startDay'],init['startDay']+init['days'])
plt.subplot(1,3,2)
for b in bins:
plt.plot(np.arange(startStep,endStep)/stepsPerDay,np.cumsum(directionData['elect'][b])/area[b],linestyles[b],
linewidth=2.0,label=b,markevery=30*stepsPerDay,markersize=7,fillstyle='none',markeredgewidth=2)
plt.legend(loc='upper left',fontsize=18)
plt.ylabel('Electrical Output ($kWh/m^2$)',fontsize=18), plt.xlabel('Time (Days)',fontsize=18)
plt.xlim(init['startDay'],init['startDay']+init['days'])
axes = plt.gca()
axes.tick_params(axis='both', which='major', labelsize=18)
plt.subplot(1,3,3)
plt.plot(np.arange(startStep,endStep)/stepsPerDay,
np.cumsum(directionData['thermal']['total']+directionData['elect']['total'])/area['total'],
linewidth=2.0,label='total',linestyle='-',color='0.01')
plt.plot(np.arange(startStep,endStep)/stepsPerDay,np.cumsum(directionData['thermal']['total'])/area['total'],
linewidth=2.0,label='thermal',linestyle='--',color='0.20')
plt.plot(np.arange(startStep,endStep)/stepsPerDay,np.cumsum(directionData['elect']['total'])/area['total'],
linewidth=2.0,label='electrical',linestyle='-.',color='0.4')
plt.ylabel('Total Output ($kWh/m^2$)',fontsize=18), plt.xlabel('Time (Days)',fontsize=18)
plt.xlim(init['startDay'],init['startDay']+init['days'])
axes = plt.gca()
plt.legend(loc='upper left',fontsize=18)
axes.tick_params(axis='both', which='major', labelsize=18)
plt.gcf().set_size_inches(15,5)
plt.tight_layout()
plt.savefig('Results/'+init['directory']+'_outputs.png')
plt.close()
if __name__ == "__main__":
test.runTests()
if not os.path.exists('Results'):
os.makedirs('Results')
tilt = 0
# these are general variables
init = {
'numProcs':8,
'tilt':tilt,
'startDay':0,
'days':365,
'directory':'NYC'+str(tilt),
'TMY':'data/TMY/NYC.csv',
'geomfile':'data/geometry/whole-building.txt',
'useSunlitFraction':True,
'writeDataFiles':True,
'writeVTKFiles':False,
'stepsPerDay':24,
}
# these are run specific variables
solverInputs = {
'dt':init['stepsPerDay']/24.*3600.,
'eta':0.33, # electrical efficiency, constant for now.
'length':icsolar.moduleHeight,
'inletWaterTemp':20.0,
'inletAirTemp':20.0,
'interiorAirTemp':20.0,
'waterFlowRate':1.6*1000.*1e-6, # 1.6 mL/s in kg/s
# 2.0 m/s * 0.16, cross sectional area, times density in kg/s
'airFlowRate':2.0*0.16*1.200,
# data for each facade (one number for each facade)
'facadeDataNames':['epc','glaze','aoi','yaw','pitch',
'shade','dni','waterModule','thermal','elect'],
# data on modules (a number for each module)
'moduleDataNames':['DNI','DNIatModule','DHI','DHIatModule', 'Glazing', \
'waterModuleT','waterTubeT','inletAirTemp','externalAirT',
'receiverT','thermal','electrical'],
# data per direction
'directionDataNames':['thermal','elect'],
}
run(init,solverInputs)