/
Honeybee_Run Daylight Simulation.py
323 lines (258 loc) · 14.2 KB
/
Honeybee_Run Daylight Simulation.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
#
# Honeybee: A Plugin for Environmental Analysis (GPL) started by Mostapha Sadeghipour Roudsari
#
# This file is part of Honeybee.
#
# Copyright (c) 2013-2015, Mostapha Sadeghipour Roudsari <Sadeghipour@gmail.com>
# Honeybee is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# Honeybee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Honeybee; If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
"""
export geometries to rad file, and run daylighting/energy simulation
-
Provided by Honeybee 0.0.57
Args:
north_: ...
_HBObjects: List of Honeybee objects
_analysisRecipe: An analysis recipe
_writeRad: Write simulation files
runRad_: Run the analysis. _writeRad should be also set to true
_numOfCPUs_: Number of CPUs to be used for the studies. This option doesn't work for image-based analysis
_workingDir_: Working directory on your system. Default is set to C:\Ladybug
_radFileName_: Input the project name as a string
meshSettings_: Custom mesh setting. Use Grasshopper mesh setting components
additionalRadFiles_: A list of fullpath to valid radiance files which will be added to the scene
exportAirWalls_: Set to True if you want to export air walls as surfaces and False if you don't want air walls be exported. The default is set to False.
overwriteResults_: Set to False if you want the component create a copy of all the results. Default is True
Returns:
readMe!: ...
analysisType: Type of the analysis (e.g. illuminance, luminance,...)
resultsUnit: Unit of the results (e.g. lux, candela, wh/m2)
radianceFile: Path to the Radiance geometry file
HDRFiles: Path to the HDR image file
gridBasedResultsFiles: Path to the results of grid based analysis (includes all the recipes except image-based and annual)
annualResultFiles: Path to the result files of annual analysis
testPts: Test points
done: True if the study is over
"""
ghenv.Component.Name = "Honeybee_Run Daylight Simulation"
ghenv.Component.NickName = 'runDaylightAnalysis'
ghenv.Component.Message = 'VER 0.0.57\nJUL_06_2015'
ghenv.Component.Category = "Honeybee"
ghenv.Component.SubCategory = "04 | Daylight | Daylight"
#compatibleHBVersion = VER 0.0.56\nMAR_11_2015
#compatibleLBVersion = VER 0.0.59\nFEB_01_2015
try: ghenv.Component.AdditionalHelpFromDocStrings = "1"
except: pass
import Rhino as rc
import scriptcontext as sc
import rhinoscriptsyntax as rs
import os
import time
import System
import shutil
import Grasshopper.Kernel as gh
import math
from Grasshopper import DataTree
from Grasshopper.Kernel.Data import GH_Path
ghenv.Component.Params.Output[5].NickName = "resultFiles"
ghenv.Component.Params.Output[5].Name = "resultFiles"
resultFiles = []
ghenv.Component.Params.Output[3].NickName = "results"
ghenv.Component.Params.Output[3].Name = "results"
results = []
def main(north, originalHBObjects, analysisRecipe, runRad, numOfCPUs, workingDir, radFileName, meshParameters, waitingTime, additionalRadFiles, overwriteResults, exportAirWalls):
# import the classes
w = gh.GH_RuntimeMessageLevel.Warning
if not sc.sticky.has_key('ladybug_release') or not sc.sticky.has_key('honeybee_release'):
print "You should first let both Ladybug and Honeybee to fly..."
ghenv.Component.AddRuntimeMessage(w, "You should first let both Ladybug and Honeybee to fly...")
return -1
units = sc.doc.ModelUnitSystem
if `units` != 'Rhino.UnitSystem.Meters':
msg = "Default Radiance parameters are set based on meters. Make sure to modify the parameters for document units or change the units to Meters."
ghenv.Component.AddRuntimeMessage(w, msg)
try:
if not sc.sticky['honeybee_release'].isCompatible(ghenv.Component): return -1
except:
warning = "You need a newer version of Honeybee to use this compoent." + \
" Use updateHoneybee component to update userObjects.\n" + \
"If you have already updated userObjects drag Honeybee_Honeybee component " + \
"into canvas and try again."
w = gh.GH_RuntimeMessageLevel.Warning
ghenv.Component.AddRuntimeMessage(w, warning)
return -1
try:
if not sc.sticky['ladybug_release'].isCompatible(ghenv.Component): return -1
except:
warning = "You need a newer version of Ladybug to use this compoent." + \
" Use updateLadybug component to update userObjects.\n" + \
"If you have already updated userObjects drag Ladybug_Ladybug component " + \
"into canvas and try again."
w = gh.GH_RuntimeMessageLevel.Warning
ghenv.Component.AddRuntimeMessage(w, warning)
return -1
lb_preparation = sc.sticky["ladybug_Preparation"]()
hb_writeRAD = sc.sticky["honeybee_WriteRAD"](ghenv.Component)
hb_writeRADAUX = sc.sticky["honeybee_WriteRADAUX"]()
hb_materilaLib = sc.sticky["honeybee_materialLib"]
hb_scheduleLib = sc.sticky["honeybee_ScheduleLib"]
hb_writeDS = sc.sticky["honeybee_WriteDS"]()
northAngle, northVector = lb_preparation.angle2north(north)
if analysisRecipe:
# read parameters
hb_writeRADAUX.readAnalysisRecipe(analysisRecipe)
# double check and make sure that the parameters are set good enough
# for grid based simulation
hb_writeRADAUX.checkInputParametersForGridBasedAnalysis()
#conversionFac = lb_preparation.checkUnits()
# check for folder
# make working directory and/or clean the directory if needed
subWorkingDir, radFileName = hb_writeRADAUX.prepareWorkingDir(workingDir, radFileName, overwriteResults)
# export mesh
hb_writeRADAUX.exportTestMesh(subWorkingDir, radFileName)
# write analysis type to folder
hb_writeRADAUX.exportTypeFile(subWorkingDir, radFileName, analysisRecipe)
# copy the sky file to the local folder except for annual analysis
radSkyFileName = hb_writeRADAUX.copySkyFile(subWorkingDir, radFileName)
#Set a defalut of False for exporting Air Walls.
if exportAirWalls == None: exportAirWalls = False
######################### WRITE RAD FILES ###########################
# 2.1 write the geometry file
########################################################################
######################## GENERATE THE BASE RAD FILE ####################
# I'm not sure how do I take care of file names for dynamic shadings
# inside this function. For now I just leave it as it is and I will move
# it to write DS later
radFileFullName, materialFileName = \
hb_writeRAD.writeRADAndMaterialFiles(originalHBObjects, subWorkingDir, \
radFileName, analysisRecipe, \
meshParameters, exportAirWalls)
######################## GENERATE POINT FILES #######################
# test points should be generated if the study is grid based
# except image-based simulation
testPtsEachCPU, lenOfPts = hb_writeRAD.writeTestPtFile(subWorkingDir, radFileName, numOfCPUs, analysisRecipe)
numOfCPUs = len(testPtsEachCPU) #in case number of CPUs are more than number of test points
######################## WRITE BATCH FILES #######################
# if analysis type is annual this function will write hea files too
initBatchFileName, batchFilesName, fileNames, pcompBatchFile, expectedResultFiles = \
hb_writeRAD.writeBatchFiles(subWorkingDir, radFileName, \
radSkyFileName, radFileFullName, materialFileName, \
numOfCPUs, testPtsEachCPU, lenOfPts, analysisRecipe, \
additionalRadFiles)
if runRad:
hb_writeRAD.runBatchFiles(initBatchFileName, batchFilesName, \
fileNames, pcompBatchFile, waitingTime)
results = hb_writeRAD.collectResults(subWorkingDir, radFileName, \
numOfCPUs, analysisRecipe, expectedResultFiles)
if analysisRecipe.type == 2:
DSResultFilesAddress, annualGlareResults = results
return radFileFullName, annualGlareResults, [], analysisRecipe.testPts, DSResultFilesAddress, [], subWorkingDir
elif analysisRecipe.type == 0:
HDRFileAddress = results
return radFileFullName, [], [], [], [], HDRFileAddress, subWorkingDir
else:
RADResultFilesAddress = results
return radFileFullName, [], RADResultFilesAddress, analysisRecipe.testPts, [], [], subWorkingDir
else:
# return name of the file
if analysisRecipe.type == 0: return radFileFullName, [], [], [], [], [], subWorkingDir
else: return radFileFullName, [], [], analysisRecipe.testPts, [], [], subWorkingDir
if _writeRad == True and _analysisRecipe!=None and ((len(_HBObjects)!=0 and _HBObjects[0]!=None) or additionalRadFiles_!=[]):
north_ = 0 # place holder for now until I implement it to the code.
report = ""
done = False
waitingTime = 0.2 # waiting time between batch files in seconds
try: numOfCPUs = int(_numOfCPUs_)
except: numOfCPUs = 1
# make sure it is not more than the number of available CPUs
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
if numOfCPUs > ncpus:
print "Sorry! But the number of available CPUs on your machine is " + str(ncpus) + "." + \
"\nHoneybee set the number of CPUs to " + str(ncpus) + ".\n"
numOfCPUs = ncpus
result = main(north_, _HBObjects, _analysisRecipe, runRad_, numOfCPUs, \
_workingDir_, _radFileName_, meshSettings_, waitingTime, \
additionalRadFiles_, overwriteResults_, exportAirWalls_)
if result!= -1:
analysisTypesDict = sc.sticky["honeybee_DLAnalaysisTypes"]
hb_readAnnualResultsAux = sc.sticky["honeybee_ReadAnnualResultsAux"]()
# RADGeoFileAddress, radiationResult, RADResultFilesAddress, testPoints, DSResultFilesAddress, HDRFileAddress = result
radGeoFile, annualGlareResults, gridBasedResultFiles, testPoints, annualResultFiles, HDRFiles, studyFolder = result
testPts = DataTree[System.Object]()
for i, ptList in enumerate(testPoints):
p = GH_Path(i)
testPts.AddRange(ptList, p)
analysisType = _analysisRecipe.type
if analysisType == 3 or analysisType == 4:
analysisTypeKey = analysisType
elif analysisType == 0 or analysisType == 1:
analysisTypeKey = _analysisRecipe.simulationType
elif analysisType == 2:
# annual analysis
analysisTypeKey = None
try:
analysisType, resultsUnit = analysisTypesDict[analysisTypeKey]
except:
analysisType, resultsUnit = "annual analysis", "var"
resultsOutputName = analysisType.split(":")[-1].strip().replace(" ", "_") + "_values"
filesOutputName = analysisType.split(":")[-1].strip().replace(" ", "_") + "_files"
# check and rename result files based on analysis type
if gridBasedResultFiles != []:
resultFiles = gridBasedResultFiles
#get the values for the results
CalculateGridBasedDLAnalysisResults = sc.sticky["honeybee_GridBasedDLResults"]
calculateResults = CalculateGridBasedDLAnalysisResults(resultFiles, int(analysisType.split(":")[0].strip()[0]))
values = calculateResults.getResults()
ghenv.Component.Params.Output[3].NickName = resultsOutputName
ghenv.Component.Params.Output[3].Name = resultsOutputName
# branch values based on test points
# this is really ugly - should fix it later
numOfPts = []
numOfBranches = testPts.BranchCount
for branchNum in range(numOfBranches):
numOfPts.append(len(testPts.Branch(branchNum)))
exec(resultsOutputName + "= DataTree[System.Object]()")
totalPtsCount = 0
for branchNum in range(numOfBranches):
p = GH_Path(branchNum)
for ptCount in range(numOfPts[branchNum]):
resValue = "%.2f"%values[totalPtsCount]
exec(resultsOutputName + ".Add(resValue, p)")
totalPtsCount += 1
elif annualResultFiles != []:
# sort ill files
resultFiles = hb_readAnnualResultsAux.sortIllFiles(annualResultFiles)
elif HDRFiles != []:
resultFiles = HDRFiles
if annualGlareResults!=[] and annualGlareResults!={}:
ghenv.Component.Params.Output[3].NickName = "dgp_values"
ghenv.Component.Params.Output[3].Name = "dgp_values"
dgp_values = DataTree[System.Object]()
keyCount = 0
for key, item in annualGlareResults.items():
p = GH_Path(keyCount)
# add heading
strToBeFound = 'key:location/dataType/units/frequency/startsAt/endsAt'
annualGlareHeading = [strToBeFound, "view: " + key, "Daylight Glare Probability", \
"%", 'Hourly', (1,1,1), (12, 31, 24)]
if len(item)!=0:
item = annualGlareHeading + item
dgp_values.AddRange(item, p)
keyCount+=1
done = True
ghenv.Component.Params.Output[5].NickName = filesOutputName
ghenv.Component.Params.Output[5].Name = filesOutputName
exec(filesOutputName + "= resultFiles")
time.sleep(.2)