def simulate_idf(idf_file, epw_file, folder, log_file): """Simulate an IDF file in EnergyPlus. \b Args: idf_file: Full path to a simulate-able .idf file. epw_file: Full path to an .epw file. """ try: # set the default folder to the default if it's not specified and copy the IDF if folder is None: proj_name = os.path.basename(idf_file).replace('.idf', '') folder = os.path.join(folders.default_simulation_folder, proj_name) preparedir(folder, remove_content=False) idf = os.path.join(folder, 'in.idf') shutil.copy(idf_file, idf) # run the file through EnergyPlus gen_files = [idf] sql, eio, rdd, html, err = run_idf(idf, epw_file) if err is not None and os.path.isfile(err): gen_files.extend([sql, eio, rdd, html, err]) err_obj = Err(err) for error in err_obj.fatal_errors: log_file.write( err_obj.file_contents) # log before raising the error raise Exception(error) else: raise Exception('Running EnergyPlus failed.') log_file.write(json.dumps(gen_files)) except Exception as e: _logger.exception('IDF simulation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def run_osw_and_report_errors(i): """Run an OSW through OpenStudio CLI.""" osw = _osw[i] osm_i, idf_i = run_osw(osw, silent=silent) # process the additional strings if add_str_ != [] and add_str_[0] is not None and idf is not None: add_str = '/n'.join(add_str_) with open(idf, "a") as idf_file: idf_file.write(add_str) osm.append(osm_i) idf.append(idf_i) # run the IDF through EnergyPlus if run_: sql_i, zsz_i, rdd_i, html_i, err_i = run_idf(idf_i, _epw_file, silent=silent) # report any errors on this component if err_i is not None: err_obj = Err(err_i) err_objs.append(err_obj) for warn in err_obj.severe_errors: give_warning(ghenv.Component, warn) for error in err_obj.fatal_errors: print err_obj.file_contents # print before raising the error raise Exception(error) # append everything to the global lists sql.append(sql_i) zsz.append(zsz_i) rdd.append(rdd_i) html.append(html_i) err.append(err_i)
def test_run_idf_5vertex(): """Test the run_idf methods with 5-vertex apertures.""" # Get input Model input_hb_model = './tests/json/model_colinear_verts.json' with open(input_hb_model) as json_file: data = json.load(json_file) model = Model.from_dict(data) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(1, 7) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file_5verts', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
def test_run_idf(): """Test the prepare_idf_for_simulation and run_idf methods.""" # Get input Model room = Room.from_box('TinyHouseZone', 5, 10, 3) room.properties.energy.program_type = office_program room.properties.energy.add_default_ideal_air() model = Model('TinyHouse', [room]) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(1, 7) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
def run_idf_and_report_errors(i): """Run an IDF file through EnergyPlus and report errors/warnings on this component.""" # process the additional strings idf_i = idfs[i] if add_str_ != [] and add_str_[0] is not None: a_str = '/n'.join(add_str_) with open(idf_i, "a") as idf_file: idf_file.write(a_str) sql_i, zsz_i, rdd_i, html_i, err_i = run_idf(idf_i, _epw_file, silent=silent) # report any errors on this component if err_i is not None: err_obj = Err(err_i) err_objs[i] = err_obj for warn in err_obj.severe_errors: give_warning(ghenv.Component, warn) for error in err_obj.fatal_errors: print err_obj.file_contents # print before raising the error raise Exception(error) # append everything to the global lists sql[i] = sql_i zsz[i] = zsz_i rdd[i] = rdd_i html[i] = html_i err[i] = err_i
def run_osm_and_report_errors(i): """Run an OSW through OpenStudio CLI.""" # create a blank osw for the translation osw_dict = {'seed_file': _osm[i], 'weather_file': _epw_file} osw_directory = os.path.dirname(_osm[i]) sch_directory1 = os.path.join(os.path.dirname(osw_directory), 'schedules') sch_directory2 = os.path.join(osw_directory, 'schedules') if os.path.isdir(sch_directory1): osw_dict['file_paths'] = [sch_directory1] elif os.path.isdir(sch_directory2): osw_dict['file_paths'] = [sch_directory2] osw = os.path.join(osw_directory, 'workflow.osw') with open(osw, 'w') as fp: json.dump(osw_dict, fp, indent=4) # get an IDF from the OSM using the OpenStudio CLI osm_i, idf_i = run_osw(osw, silent=silent) if idf_i is None: log_osw = OSW(os.path.join(osw_directory, 'out.osw')) errors = [] print(log_osw.stdout) for error, tb in zip(log_osw.errors, log_osw.error_tracebacks): print(tb) errors.append(error) raise Exception('Failed to run OpenStudio CLI:\n{}'.format( '\n'.join(errors))) # process the additional strings if add_str_ != [] and add_str_[0] is not None and idf is not None: a_str = '/n'.join(add_str_) with open(idf_i, "a") as idf_file: idf_file.write(a_str) osm[i] = osm_i idf[i] = idf_i # run the IDF through EnergyPlus if run_: sql_i, zsz_i, rdd_i, html_i, err_i = run_idf(idf_i, _epw_file, silent=silent) # report any errors on this component if err_i is not None: err_obj = Err(err_i) err_objs[i] = err_obj for warn in err_obj.severe_errors: give_warning(ghenv.Component, warn) for error in err_obj.fatal_errors: print err_obj.file_contents # print before raising the error raise Exception(error) # append everything to the global lists sql[i] = sql_i zsz[i] = zsz_i rdd[i] = rdd_i html[i] = html_i err[i] = err_i
def test_err_severe(): """Test the initialization of error files with severe errors.""" err_path = './tests/result/eplusout_severe.err' err_obj = Err(err_path) str(err_obj) # test the string representation assert isinstance(err_obj.file_path, str) assert isinstance(err_obj.file_contents, str) assert len(err_obj.warnings) == 0 assert len(err_obj.severe_errors) == 4 assert len(err_obj.fatal_errors) == 1
def test_err_init(): """Test the initialization of error files and basic properties.""" err_path = './tests/result/eplusout_normal.err' err_obj = Err(err_path) str(err_obj) # test the string representation assert isinstance(err_obj.file_path, str) assert isinstance(err_obj.file_contents, str) assert len(err_obj.warnings) == 23 assert len(err_obj.severe_errors) == 0 assert len(err_obj.fatal_errors) == 0
def test_run_idf_window_ventilation(): """Test the Model.to.idf and run_idf method with a model possessing operable windows.""" room = Room.from_box('TinyHouseZone', 5, 10, 3) room.properties.energy.add_default_ideal_air() south_face = room[3] north_face = room[1] south_face.apertures_by_ratio(0.4, 0.01) north_face.apertures_by_ratio(0.4, 0.01) south_face.apertures[0].is_operable = True north_face.apertures[0].is_operable = True heat_setpt = ScheduleRuleset.from_constant_value( 'House Heating', 20, schedule_types.temperature) cool_setpt = ScheduleRuleset.from_constant_value( 'House Cooling', 28, schedule_types.temperature) setpoint = Setpoint('House Setpoint', heat_setpt, cool_setpt) room.properties.energy.setpoint = setpoint vent_control = VentilationControl(22, 27, 12, 30) room.properties.energy.window_vent_control = vent_control ventilation = VentilationOpening(wind_cross_vent=True) op_aps = room.properties.energy.assign_ventilation_opening(ventilation) assert len(op_aps) == 2 model = Model('TinyHouse', [room]) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(6, 7) sim_par.run_period.start_date = Date(6, 1) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file_window_vent', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
def test_run_idf_window_shade(): """Test the Model.to.idf and run_idf method with a model that has a window shade.""" # Get input Model room = Room.from_box('TinyHouseZone', 5, 10, 3) room.properties.energy.program_type = office_program room.properties.energy.add_default_ideal_air() double_clear = WindowConstruction('Double Pane Clear', [clear_glass, air_gap, clear_glass]) shade_mat = EnergyWindowMaterialShade('Low-e Diffusing Shade', 0.005, 0.15, 0.5, 0.25, 0.5, 0, 0.4, 0.2, 0.1, 0.75, 0.25) sched = ScheduleRuleset.from_daily_values('NighSched', [ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 ]) double_ec = WindowConstructionShade('Double Low-E Inside EC', double_clear, shade_mat, 'Interior', 'OnIfHighSolarOnWindow', 200, sched) south_face = room[3] south_face.apertures_by_ratio(0.4, 0.01) south_face.apertures[0].properties.energy.construction = double_ec north_face = room[1] north_face.apertures_by_ratio(0.4, 0.01) model = Model('TinyHouse', [room]) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(1, 7) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file_shd', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
def simulate_osm(osm_file, epw_file, folder, log_file): """Simulate an OSM file in EnergyPlus. \b Args: osm_file: Full path to a simulate-able .osm file. epw_file: Full path to an .epw file. """ try: # set the default folder to the default if it's not specified and copy the IDF if folder is None: proj_name = os.path.basename(osm_file).replace('.osm', '') folder = os.path.join(folders.default_simulation_folder, proj_name) preparedir(folder, remove_content=False) base_osm = os.path.join(folder, 'in.osm') shutil.copy(osm_file, base_osm) # create a blank osw for the translation osw_dict = {'seed_file': osm_file, 'weather_file': epw_file} osw = os.path.join(folder, 'workflow.osw') with open(osw, 'w') as fp: json.dump(osw_dict, fp, indent=4) # run the OSW through OpenStudio CLI osm, idf = run_osw(osw) # run the file through EnergyPlus if idf is not None and os.path.isfile(idf): gen_files = [osw, osm, idf] sql, eio, rdd, html, err = run_idf(idf, epw_file) if err is not None and os.path.isfile(err): gen_files.extend([sql, eio, rdd, html, err]) err_obj = Err(err) for error in err_obj.fatal_errors: log_file.write( err_obj.file_contents) # log before raising the error raise Exception(error) else: raise Exception('Running EnergyPlus failed.') else: raise Exception('Running OpenStudio CLI failed.') log_file.write(json.dumps(gen_files)) except Exception as e: _logger.exception('OSM simulation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def test_run_idf_daylight_control(): """Test Model.to.idf and run_idf with a model possessing daylight controls.""" room = Room.from_box('TinyHouseZone', 5, 10, 3) room.properties.energy.program_type = office_program room.properties.energy.add_default_ideal_air() south_face = room[3] south_face.apertures_by_ratio(0.4, 0.01) room.properties.energy.add_daylight_control_to_center(0.8, 500, 0.5) sens_pt = room.properties.energy.daylighting_control.sensor_position assert sens_pt.x == pytest.approx(2.5, rel=1e-3) assert sens_pt.y == pytest.approx(5, rel=1e-3) assert sens_pt.z == pytest.approx(0.8, rel=1e-3) room.move(Vector3D(5, 5, 0)) assert room.properties.energy.daylighting_control.sensor_position == \ sens_pt.move(Vector3D(5, 5, 0)) model = Model('TinyHouse', [room]) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(6, 7) sim_par.run_period.start_date = Date(6, 1) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file_daylight', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
def run_idf_and_report_errors(i): """Run an IDF file through EnergyPlus and report errors/warnings on this component.""" idf = _idf[i] sql_i, zsz_i, rdd_i, html_i, err_i = run_idf(idf, _epw_file) # report any errors on this component if err_i is not None: err_obj = Err(err_i) err_objs.append(err_obj) for warn in err_obj.severe_errors: give_warning(ghenv.Component, warn) for error in err_obj.fatal_errors: raise Exception(error) # append everything to the global lists sql.append(sql_i) zsz.append(zsz_i) rdd.append(rdd_i) html.append(html_i) err.append(err_i)
def test_run_idf_hot_water(): """Test the Model.to.idf and run_idf method with a model possessing hot water.""" # Get input Model room = Room.from_box('TinyHouseZone', 5, 10, 3) room.properties.energy.program_type = office_program simple_office = ScheduleDay( 'Simple Weekday', [0, 1, 0], [Time(0, 0), Time(9, 0), Time(17, 0)]) schedule = ScheduleRuleset('Office Water Use', simple_office, None, schedule_types.fractional) shw = ServiceHotWater('Office Hot Water', 0.1, schedule) room.properties.energy.service_hot_water = shw room.properties.energy.add_default_ideal_air() model = Model('TinyHouse', [room]) # Get the input SimulationParameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() ddy_file = './tests/ddy/chicago.ddy' sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par.run_period.end_date = Date(1, 7) # create the IDF string for simulation paramters and model idf_str = '\n\n'.join((sim_par.to_idf(), model.to.idf(model))) # write the final string into an IDF idf = os.path.join(folders.default_simulation_folder, 'test_file_shw', 'in.idf') write_to_file(idf, idf_str, True) # prepare the IDF for simulation epw_file = './tests/simulation/chicago.epw' prepare_idf_for_simulation(idf, epw_file) # run the IDF through EnergyPlus sql, zsz, rdd, html, err = run_idf(idf, epw_file) assert os.path.isfile(sql) assert os.path.isfile(err) err_obj = Err(err) assert 'EnergyPlus Completed Successfully' in err_obj.file_contents
is not None else energyplus_idf_version(compatibe_ep_version) sim_par_str = _sim_par_.to_idf() model_str = _model.to.idf(_model, schedule_directory=sch_directory, patch_missing_adjacencies=True) idf_str = '\n\n'.join([ver_str, sim_par_str, model_str]) # write the final string into an IDF idf = os.path.join(directory, 'in.idf') write_to_file_by_name(directory, 'in.idf', idf_str, True) # run the IDF through EnergyPlus silent = True if _run == 1 else False sql, zsz, rdd, html, err = run_idf(idf, _epw_file, silent=silent) if html is None and err is not None: # something went wrong; parse the errors err_obj = Err(err) print(err_obj.file_contents) for error in err_obj.fatal_errors: raise Exception(error) # parse the result sql and get the monthly data collections if os.name == 'nt': # we are on windows; use IronPython like usual sql_obj = SQLiteResult(sql) cool_init = sql_obj.data_collections_by_output_name(cool_out) heat_init = sql_obj.data_collections_by_output_name(heat_out) light_init = sql_obj.data_collections_by_output_name(light_out) elec_equip_init = sql_obj.data_collections_by_output_name(el_equip_out) gas_equip_init = sql_obj.data_collections_by_output_name(gas_equip_out) process1_init = sql_obj.data_collections_by_output_name(process1_out) process2_init = sql_obj.data_collections_by_output_name(process2_out) shw_init = sql_obj.data_collections_by_output_name(shw_out)