def test_to_openstudio_osw(): """Test to_openstudio_osw.""" # create the model room = Room.from_box('TinyHouseZone', 5, 10, 3) model = Model('TinyHouse', [room]) model_json_path = './tests/simulation/model_osw_test.json' with open(model_json_path, 'w') as fp: json.dump(model.to_dict(included_prop=['energy']), fp) # create the simulation parameter sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() simpar_json_path = './tests/simulation/simpar_osw_test.json' with open(simpar_json_path, 'w') as fp: json.dump(sim_par.to_dict(), fp) # create additional measures measure_path = './tests/measure/edit_fraction_radiant_of_lighting_and_equipment' measure = Measure(measure_path) measure.arguments[0].value = 0.25 measure.arguments[1].value = 0.25 # test it without measures folder = './tests/simulation/' osw_path = os.path.join(folder, 'workflow.osw') osw = to_openstudio_osw(folder, model_json_path, simpar_json_path) assert os.path.isfile(osw_path) os.remove(osw_path) # test it with measures osw = to_openstudio_osw(folder, model_json_path, additional_measures=[measure]) assert os.path.isfile(osw_path) os.remove(osw_path) os.remove(model_json_path) os.remove(simpar_json_path)
def model_to_osm(model_json, sim_par_json, folder, check_model, log_file): """Translate a Model JSON file into an OpenStudio Model and corresponding IDF. \n Args: model_json: Full path to a Model JSON file. """ try: # check that the model JSON is there assert os.path.isfile(model_json), \ 'No Model JSON file found at {}.'.format(model_json) # set the default folder if it's not specified if folder is None: folder = os.path.dirname(os.path.abspath(model_json)) # check that the simulation parameters are there if sim_par_json is not None: assert os.path.isfile(sim_par_json), \ 'No simulation parameter file found at {}.'.format(sim_par_json) else: sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() sim_par_dict = sim_par.to_dict() sp_json = os.path.abspath( os.path.join(folder, 'simulation_parameter.json')) with open(sp_json, 'w') as fp: json.dump(sim_par_dict, fp) # run the Model re-serialization and check if specified if check_model: model_json = measure_compatible_model_json(model_json, folder) # Write the osw file to translate the model to osm osw = to_openstudio_osw(folder, model_json, sim_par_json) # run the measure to translate the model JSON to an openstudio measure if osw is not None and os.path.isfile(osw): osm, idf = run_osw(osw) # run the resulting idf through EnergyPlus if idf is not None and os.path.isfile(idf): log_file.write(json.dumps([osm, idf])) else: raise Exception('Running OpenStudio CLI failed.') else: raise Exception('Writing OSW file failed.') except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
sim_par_dict = _sim_par_.to_dict() sim_par_json = os.path.join(directory, 'simulation_parameter.json') with open(sim_par_json, 'w') as fp: json.dump(sim_par_dict, fp) # process any measures input to the component measures = None if len( measures_) == 0 or measures_[0] is None else measures_ no_report_meas = True if measures is None else \ all(meas.type != 'ReportingMeasure' for meas in measures) # collect the two jsons for output and write out the osw file jsons = [model_json, sim_par_json] osw = to_openstudio_osw(directory, model_json, sim_par_json, additional_measures=measures, epw_file=_epw_file, schedule_directory=sch_directory) # run the measure to translate the model JSON to an openstudio measure silent = True if run_ == 3 else False if run_ > 0 and not no_report_meas: # everything must run with OS CLI osm, idf = run_osw(osw, measures_only=False, silent=silent) sql, zsz, rdd, html, err = output_energyplus_files( os.path.dirname(idf)) elif run_ > 0: # no reporting measure; simulate separately from measure application osm, idf = 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:
def simulate_model(model_json, epw_file, sim_par_json, base_osw, folder, check_model, log_file): """Simulate a Model JSON file in EnergyPlus. \b Args: model_json: Full path to a Model JSON file. epw_file: Full path to an .epw file. """ try: # get a ddy variable that might get used later epw_folder, epw_file_name = os.path.split(epw_file) ddy_file = os.path.join(epw_folder, epw_file_name.replace('.epw', '.ddy')) # set the default folder to the default if it's not specified if folder is None: proj_name = \ os.path.basename(model_json).replace('.json', '').replace('.hbjson', '') folder = os.path.join(folders.default_simulation_folder, proj_name, 'OpenStudio') preparedir(folder, remove_content=False) # process the simulation parameters and write new ones if necessary def ddy_from_epw(epw_file, sim_par): """Produce a DDY from an EPW file.""" epw_obj = EPW(epw_file) des_days = [ epw_obj.approximate_design_day('WinterDesignDay'), epw_obj.approximate_design_day('SummerDesignDay') ] sim_par.sizing_parameter.design_days = des_days def write_sim_par(sim_par): """Write simulation parameter object to a JSON.""" sim_par_dict = sim_par.to_dict() sp_json = os.path.abspath( os.path.join(folder, 'simulation_parameter.json')) with open(sp_json, 'w') as fp: json.dump(sim_par_dict, fp) return sp_json if sim_par_json is None: # generate some default simulation parameters sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() else: with open(sim_par_json) as json_file: data = json.load(json_file) sim_par = SimulationParameter.from_dict(data) if len(sim_par.sizing_parameter.design_days) == 0 and os.path.isfile( ddy_file): try: sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) except AssertionError: # no design days within the DDY file ddy_from_epw(epw_file, sim_par) elif len(sim_par.sizing_parameter.design_days) == 0: ddy_from_epw(epw_file, sim_par) sim_par_json = write_sim_par(sim_par) # run the Model re-serialization and check if specified if check_model: model_json = measure_compatible_model_json(model_json, folder) # Write the osw file to translate the model to osm osw = to_openstudio_osw(folder, model_json, sim_par_json, base_osw=base_osw, epw_file=epw_file) # run the measure to translate the model JSON to an openstudio measure if osw is not None and os.path.isfile(osw): gen_files = [osw] if base_osw is None: # separate the OS CLI run from the E+ run osm, idf = run_osw(osw) # run the resulting idf through EnergyPlus if idf is not None and os.path.isfile(idf): gen_files.extend([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]) else: raise Exception('Running EnergyPlus failed.') else: raise Exception('Running OpenStudio CLI failed.') else: # run the whole simulation with the OpenStudio CLI osm, idf = run_osw(osw, measures_only=False) if idf is not None and os.path.isfile(idf): gen_files.extend([osm, idf]) else: raise Exception('Running OpenStudio CLI failed.') sql, eio, rdd, html, err = output_energyplus_files( os.path.dirname(idf)) if os.path.isfile(err): gen_files.extend([sql, eio, rdd, html, err]) else: raise Exception('Running EnergyPlus failed.') log_file.write(json.dumps(gen_files)) else: raise Exception('Writing OSW file failed.') except Exception as e: _logger.exception('Model simulation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def simulate_model(model_json, epw_file, sim_par_json, base_osw, folder, check_model, log_file): """Simulate a Model JSON file in EnergyPlus. \n Args: model_json: Full path to a Model JSON file.\n epw_file: Full path to an .epw file. """ try: # check that the model JSON and the EPW file is there assert os.path.isfile(model_json), \ 'No Model JSON file found at {}.'.format(model_json) assert os.path.isfile(epw_file), \ 'No EPW file found at {}.'.format(epw_file) # ddy variable that might get used later epw_folder, epw_file_name = os.path.split(epw_file) ddy_file = os.path.join(epw_folder, epw_file_name.replace('.epw', '.ddy')) # set the default folder to the default if it's not specified if folder is None: proj_name = os.path.basename(model_json).replace('.json', '') folder = os.path.join(folders.default_simulation_folder, proj_name, 'OpenStudio') preparedir(folder, remove_content=False) # process the simulation parameters and write new ones if necessary def write_sim_par(sim_par): """Write simulation parameter object to a JSON.""" sim_par_dict = sim_par.to_dict() sp_json = os.path.abspath( os.path.join(folder, 'simulation_parameter.json')) with open(sp_json, 'w') as fp: json.dump(sim_par_dict, fp) return sp_json if sim_par_json is None: # generate some default simulation parameters sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() if os.path.isfile(ddy_file): sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) else: raise ValueError( 'No sim-par-json was input and there is no .ddy file next to ' 'the .epw.\nAt least one of these two cirtieria must be satisfied ' 'for a successful simulation.') sim_par_json = write_sim_par(sim_par) else: assert os.path.isfile(sim_par_json), \ 'No simulation parameter file found at {}.'.format(sim_par_json) with open(sim_par_json) as json_file: data = json.load(json_file) sim_par = SimulationParameter.from_dict(data) if len(sim_par.sizing_parameter.design_days ) == 0 and os.path.isfile(ddy_file): sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) sim_par_json = write_sim_par(sim_par) elif len(sim_par.sizing_parameter.design_days) == 0: raise ValueError( 'No design days were found in the input sim-par-json and there is ' 'no .ddy file next to the .epw.\nAt least one of these two cirtieria ' 'must be satisfied for a successful simulation.') # run the Model re-serialization and check if specified if check_model: model_json = measure_compatible_model_json(model_json, folder) # Write the osw file to translate the model to osm osw = to_openstudio_osw(folder, model_json, sim_par_json, base_osw=base_osw, epw_file=epw_file) # run the measure to translate the model JSON to an openstudio measure if osw is not None and os.path.isfile(osw): gen_files = [osw] if base_osw is None: # separate the OS CLI run from the E+ run osm, idf = run_osw(osw) # run the resulting idf through EnergyPlus if idf is not None and os.path.isfile(idf): gen_files.extend([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]) else: raise Exception('Running EnergyPlus failed.') else: raise Exception('Running OpenStudio CLI failed.') else: # run the whole simulation with the OpenStudio CLI osm, idf = run_osw(osw, measures_only=False) if idf is not None and os.path.isfile(idf): gen_files.extend([osm, idf]) else: raise Exception('Running OpenStudio CLI failed.') sql, eio, rdd, html, err = output_energyplus_files( os.path.dirname(idf)) if os.path.isfile(err): gen_files.extend([sql, eio, rdd, html, err]) else: raise Exception('Running EnergyPlus failed.') log_file.write(json.dumps(gen_files)) else: raise Exception('Writing OSW file failed.') except Exception as e: _logger.exception('Model simulation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def simulate_model(model_json, epw_file, sim_par_json, obj_per_model, multiplier, no_plenum, no_cap, shade_dist, base_osw, folder, log_file): """Simulate a Dragonfly Model JSON file in EnergyPlus. \b Args: model_json: Full path to a Dragonfly Model JSON file. epw_file: Full path to an .epw file. """ try: # get a ddy variable that might get used later epw_folder, epw_file_name = os.path.split(epw_file) ddy_file = os.path.join(epw_folder, epw_file_name.replace('.epw', '.ddy')) # set the default folder to the default if it's not specified if folder is None: proj_name = \ os.path.basename(model_json).replace('.json', '').replace('.dfjson', '') folder = os.path.join(folders.default_simulation_folder, proj_name, 'OpenStudio') preparedir(folder, remove_content=False) # process the simulation parameters and write new ones if necessary def ddy_from_epw(epw_file, sim_par): """Produce a DDY from an EPW file.""" epw_obj = EPW(epw_file) des_days = [ epw_obj.approximate_design_day('WinterDesignDay'), epw_obj.approximate_design_day('SummerDesignDay') ] sim_par.sizing_parameter.design_days = des_days def write_sim_par(sim_par): """Write simulation parameter object to a JSON.""" sim_par_dict = sim_par.to_dict() sp_json = os.path.abspath( os.path.join(folder, 'simulation_parameter.json')) with open(sp_json, 'w') as fp: json.dump(sim_par_dict, fp) return sp_json if sim_par_json is None: # generate some default simulation parameters sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() else: with open(sim_par_json) as json_file: data = json.load(json_file) sim_par = SimulationParameter.from_dict(data) if len(sim_par.sizing_parameter.design_days) == 0 and os.path.isfile( ddy_file): try: sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) except AssertionError: # no design days within the DDY file ddy_from_epw(epw_file, sim_par) elif len(sim_par.sizing_parameter.design_days) == 0: ddy_from_epw(epw_file, sim_par) sim_par_json = write_sim_par(sim_par) # re-serialize the Dragonfly Model with open(model_json) as json_file: data = json.load(json_file) model = Model.from_dict(data) model.convert_to_units('Meters') # convert Dragonfly Model to Honeybee add_plenum = not no_plenum cap = not no_cap hb_models = model.to_honeybee(obj_per_model, shade_dist, multiplier, add_plenum, cap) # write out the honeybee JSONs osms = [] idfs = [] sqls = [] for hb_model in hb_models: model_dict = hb_model.to_dict(triangulate_sub_faces=True) directory = os.path.join(folder, hb_model.identifier) file_path = os.path.join(directory, '{}.json'.format(hb_model.identifier)) preparedir(directory, remove_content=False) # create the directory with open(file_path, 'w') as fp: json.dump(model_dict, fp, indent=4) # Write the osw file to translate the model to osm osw = to_openstudio_osw(directory, file_path, sim_par_json, base_osw=base_osw, epw_file=epw_file) # run the measure to translate the model JSON to an openstudio measure if osw is not None and os.path.isfile(osw): if base_osw is None: # separate the OS CLI run from the E+ run osm, idf = run_osw(osw) if idf is not None and os.path.isfile(idf): sql, eio, rdd, html, err = run_idf(idf, epw_file) osms.append(osm) idfs.append(idf) sqls.append(sql) if err is None or not os.path.isfile(err): raise Exception('Running EnergyPlus failed.') else: raise Exception('Running OpenStudio CLI failed.') else: # run the whole simulation with the OpenStudio CLI osm, idf = run_osw(osw, measures_only=False) if idf is None or not os.path.isfile(idf): raise Exception('Running OpenStudio CLI failed.') sql, eio, rdd, html, err = \ output_energyplus_files(os.path.dirname(idf)) if err is None or not os.path.isfile(err): raise Exception('Running EnergyPlus failed.') osms.append(osm) idfs.append(idf) sqls.append(sql) else: raise Exception('Writing OSW file failed.') log_file.write(json.dumps({'osm': osms, 'idf': idfs, 'sql': sqls})) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
# write the model parameter JSONs model_dict = _model.to_dict(triangulate_sub_faces=True) model_json = os.path.join(directory, '{}.json'.format(_model.identifier)) with open(model_json, 'w') as fp: json.dump(model_dict, fp) # write the simulation parameter JSONs sim_par_dict = _sim_par_.to_dict() sim_par_json = os.path.join(directory, 'simulation_parameter.json') with open(sim_par_json, 'w') as fp: json.dump(sim_par_dict, fp) # collect the two jsons for output and write out the osw file jsons = [model_json, sim_par_json] osw = to_openstudio_osw(directory, model_json, sim_par_json, _epw_file) # run the measure to translate the model JSON to an openstudio measure if run_ > 0: osm, idf = run_osw(osw) # 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) # run the resulting idf throught EnergyPlus if run_ == 1: sql, zsz, rdd, html, err = run_idf(idf, _epw_file) if err is not None: err_obj = Err(err)
def model_to_osm(model_json, sim_par_json, obj_per_model, multiplier, no_plenum, no_cap, shade_dist, folder, log_file): """Translate a Model JSON file into an OpenStudio Model. \b Args: model_json: Full path to a Dragonfly Model JSON file. """ try: # set the default folder to the default if it's not specified if folder is None: proj_name = \ os.path.basename(model_json).replace('.json', '').replace('.dfjson', '') folder = os.path.join(hb_folders.default_simulation_folder, proj_name, 'OpenStudio') preparedir(folder, remove_content=False) # generate default simulation parameters if sim_par_json is None: sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() sim_par_dict = sim_par.to_dict() sp_json = os.path.abspath( os.path.join(folder, 'simulation_parameter.json')) with open(sp_json, 'w') as fp: json.dump(sim_par_dict, fp) # re-serialize the Dragonfly Model with open(model_json) as json_file: data = json.load(json_file) model = Model.from_dict(data) model.convert_to_units('Meters') # convert Dragonfly Model to Honeybee add_plenum = not no_plenum cap = not no_cap hb_models = model.to_honeybee(obj_per_model, shade_dist, multiplier, add_plenum, cap) # write out the honeybee JSONs osms = [] idfs = [] for hb_model in hb_models: model_dict = hb_model.to_dict(triangulate_sub_faces=True) directory = os.path.join(folder, hb_model.identifier) file_path = os.path.join(directory, '{}.json'.format(hb_model.identifier)) preparedir(directory, remove_content=False) # create the directory with open(file_path, 'w') as fp: json.dump(model_dict, fp, indent=4) # Write the osw file to translate the model to osm osw = to_openstudio_osw(directory, file_path, sim_par_json) # run the measure to translate the model JSON to an openstudio measure if os.path.isfile(osw): osm, idf = run_osw(osw) if osm is not None and os.path.isfile(osm): osms.append(osm) idfs.append(idf) else: raise Exception('Running OpenStudio CLI failed.') else: raise Exception('Writing OSW file failed.') log_file.write(json.dumps({'osm': osms, 'idf': idfs})) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)