- "code": "\nimport math\ntry: # python 2\n from itertools import izip as zip\nexcept ImportError: # python 3\n pass\n\ntry:\n from ladybug.viewsphere import view_sphere\n from ladybug.graphic import GraphicContainer\n from ladybug.legend import LegendParameters\n from ladybug.color import Colorset\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.config import conversion_to_meters\n from ladybug_{{cad}}.togeometry import to_joined_gridded_mesh3d\n from ladybug_{{cad}}.fromgeometry import from_mesh3d, from_point3d, from_vector3d\n from ladybug_{{cad}}.fromobjects import legend_objects\n from ladybug_{{cad}}.text import text_objects\n from ladybug_{{cad}}.intersect import join_geometry_to_mesh, intersect_mesh_rays\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, hide_output, \\\n show_output, objectify_output, de_objectify_output, recommended_processor_count\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\nif all_required_inputs(ghenv.Component) and _run:\n # set the default offset distance and _cpu_count\n _offset_dist_ = _offset_dist_ if _offset_dist_ is not None \\\n else 0.1 / conversion_to_meters()\n workers = _cpu_count_ if _cpu_count_ is not None else recommended_processor_count()\n\n # create the gridded mesh from the geometry\n study_mesh = to_joined_gridded_mesh3d(_geometry, _grid_size)\n points = [from_point3d(pt.move(vec * _offset_dist_)) for pt, vec in\n zip(study_mesh.face_centroids, study_mesh.face_normals)]\n hide_output(ghenv.Component, 1)\n\n # mesh the geometry and context\n shade_mesh = join_geometry_to_mesh(_geometry + context_)\n\n # deconstruct the matrix and get the sky dome vectors\n mtx = de_objectify_output(_sky_mtx)\n total_sky_rad = [dir_rad + dif_rad for dir_rad, dif_rad in zip(mtx[1], mtx[2])]\n ground_rad = [(sum(total_sky_rad) / len(total_sky_rad)) * mtx[0][1]] * len(total_sky_rad)\n all_rad = total_sky_rad + ground_rad \n lb_vecs = view_sphere.tregenza_dome_vectors if len(total_sky_rad) == 145 \\\n else view_sphere.reinhart_dome_vectors\n if mtx[0][0] != 0: # there is a north input for sky; rotate vectors\n north_angle = math.radians(mtx[0][0])\n lb_vecs = tuple(vec.rotate_xy(north_angle) for vec in lb_vecs)\n lb_grnd_vecs = tuple(vec.reverse() for vec in lb_vecs)\n all_vecs = [from_vector3d(vec) for vec in lb_vecs + lb_grnd_vecs]\n\n # intersect the rays with the mesh\n normals = [from_vector3d(vec) for vec in study_mesh.face_normals]\n int_matrix_init, angles = intersect_mesh_rays(\n shade_mesh, points, all_vecs, normals, cpu_count=workers)\n\n # compute the results\n results = []\n int_matrix = []\n for int_vals, angs in zip(int_matrix_init, angles):\n pt_rel = [ival * math.cos(ang) for ival, ang in zip(int_vals, angs)]\n int_matrix.append(pt_rel)\n rad_result = sum(r * w for r, w in zip(pt_rel, all_rad))\n results.append(rad_result)\n\n # convert to irradiance if requested\n study_name = 'Incident Radiation'\n if irradiance_:\n study_name = 'Incident Irradiance'\n factor = 1000 / _sky_mtx.wea_duration if hasattr(_sky_mtx, 'wea_duration') \\\n else 1000 / (((mtx[0][3] - mtx[0][2]).total_seconds() / 3600) + 1)\n results = [r * factor for r in results]\n\n # output the intersection matrix and compute total radiation\n int_mtx = objectify_output('Geometry/Sky Intersection Matrix', int_matrix)\n unit_conv = conversion_to_meters() ** 2\n total = 0\n for rad, area in zip(results, study_mesh.face_areas):\n total += rad * area * unit_conv\n\n # create the mesh and legend outputs\n l_par = legend_par_ if legend_par_ is not None else LegendParameters()\n if hasattr(_sky_mtx, 'benefit_matrix') and _sky_mtx.benefit_matrix is not None:\n study_name = '{} Benefit/Harm'.format(study_name)\n if l_par.are_colors_default:\n l_par.colors = reversed(Colorset.benefit_harm())\n if l_par.min is None:\n l_par.min = min((min(results), -max(results)))\n if l_par.max is None:\n l_par.max = max((-min(results), max(results)))\n graphic = GraphicContainer(results, study_mesh.min, study_mesh.max, l_par)\n graphic.legend_parameters.title = 'kWh/m2' if not irradiance_ else 'W/m2'\n title = text_objects(\n study_name, graphic.lower_title_location,\n graphic.legend_parameters.text_height * 1.5,\n graphic.legend_parameters.font)\n\n # create all of the visual outputs\n study_mesh.colors = graphic.value_colors\n mesh = from_mesh3d(study_mesh)\n legend = legend_objects(graphic.legend)\n",
0 commit comments