def model_to_idf(model_json, sim_par_json, additional_str, output_file): """Translate a Model JSON file to an IDF using direct-to-idf translators. \n The resulting IDF should be simulate-able but not all Model properties might make it into the IDF given that the direct-to-idf translators are used. \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) # check that the simulation parameters are there and load them if sim_par_json is not None: 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) else: sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() # re-serialize the Model to Python with open(model_json) as json_file: data = json.load(json_file) model = Model.from_dict(data) # set the schedule directory in case it is needed sch_path = os.path.abspath(model_json) if 'stdout' in str(output_file) \ else os.path.abspath(str(output_file)) sch_directory = os.path.join(os.path.split(sch_path)[0], 'schedules') # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if folders.energyplus_version \ is not None else energyplus_idf_version((9, 2, 0)) sim_par_str = sim_par.to_idf() model_str = model.to.idf(model, schedule_directory=sch_directory) idf_str = '\n\n'.join( [ver_str, sim_par_str, model_str, additional_str]) # write out the IDF file output_file.write(idf_str) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def model_to_idf(model_json, sim_par_json, additional_str, output_file): """Translate a Model JSON file to an IDF using direct-to-idf translators. If the model contains a feature that is not translate-able through direct-to-idf translators, an exception will be raised. \b Args: model_json: Full path to a Model JSON file. """ try: # load simulation parameters or generate default ones if sim_par_json is not None: with open(sim_par_json) as json_file: data = json.load(json_file) sim_par = SimulationParameter.from_dict(data) else: sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() # re-serialize the Model to Python with open(model_json) as json_file: data = json.load(json_file) model = Model.from_dict(data) # set the schedule directory in case it is needed sch_path = os.path.abspath(model_json) if 'stdout' in str(output_file) \ else os.path.abspath(str(output_file)) sch_directory = os.path.join(os.path.split(sch_path)[0], 'schedules') # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if folders.energyplus_version \ is not None else '' sim_par_str = sim_par.to_idf() model_str = model.to.idf(model, schedule_directory=sch_directory) idf_str = '\n\n'.join( [ver_str, sim_par_str, model_str, additional_str]) # write out the IDF file output_file.write(idf_str) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
else: msg = 'No .ddy file was found next to the _epw_file.' if msg is not None: 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 msg = msg + '\nDesign days were generated from the input _epw_file but this ' \ '\nis not as accurate as design days from DDYs distributed with the EPW.' give_warning(ghenv.Component, msg) print msg # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if energy_folders.energyplus_version \ 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
def test_writer_version_idf(): """Test the energyplus_idf_version method.""" assert energyplus_idf_version() is None or \ isinstance(energyplus_idf_version(), str) assert isinstance(energyplus_idf_version((9, 2, 0)), str)
# process the additional strings add_str = '/n'.join(add_str_) if add_str_ is not None else '' # process the simulation folder name and the directory _folder_ = folders.default_simulation_folder if _folder_ is None else _folder_ directory = os.path.join(_folder_, _model.identifier, 'EnergyPlus') sch_directory = os.path.join(directory, 'schedules') # check the model to be sure there are no orphaned faces, apertures, or doors assert len(_model.orphaned_faces) == 0, orphaned_warning('Face') assert len(_model.orphaned_apertures) == 0, orphaned_warning('Aperture') assert len(_model.orphaned_doors) == 0, orphaned_warning('Door') # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if energy_folders.energyplus_version \ is not None else energyplus_idf_version((9, 2, 0)) sim_par_str = _sim_par_.to_idf() model_str = _model.to.idf(_model, schedule_directory=sch_directory, solar_distribution=_sim_par_.shadow_calculation.solar_distribution) idf_str = '\n\n'.join([ver_str, sim_par_str, model_str, add_str]) # delete any existing files in the directory nukedir(directory) # 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) if run_: sql, zsz, rdd, html, err = run_idf(idf, _epw_file)
def model_to_idf(model_json, sim_par_json, obj_per_model, multiplier, no_plenum, no_cap, shade_dist, folder, log_file): """Translate a Model JSON file to an IDF using direct-to-idf translators. The resulting IDF should be simulate-able but not all Model properties might make it into the IDF given that the direct-to-idf translators are used. \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, 'EnergyPlus') preparedir(folder, remove_content=False) # check that the simulation parameters are there and load them if sim_par_json is not None: with open(sim_par_json) as json_file: data = json.load(json_file) sim_par = SimulationParameter.from_dict(data) else: sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() # re-serialize the Dragonfly Model with open(model_json) as json_file: data = json.load(json_file) df_model = Model.from_dict(data) df_model.convert_to_units('Meters') # convert Dragonfly Model to Honeybee add_plenum = not no_plenum cap = not no_cap hb_models = df_model.to_honeybee(obj_per_model, shade_dist, multiplier, add_plenum, cap) # set the schedule directory in case it is needed sch_path = os.path.abspath(model_json) if 'stdout' in str(log_file) \ else os.path.abspath(str(log_file)) sch_directory = os.path.join(os.path.split(sch_path)[0], 'schedules') # write out the honeybee JSONs idfs = [] for hb_model in hb_models: # create the strings for simulation paramters and model ver_str = energyplus_idf_version() if folders.energyplus_version \ is not None else '' sim_par_str = sim_par.to_idf() model_str = hb_model.to.idf(hb_model, schedule_directory=sch_directory) idf_str = '\n\n'.join([ver_str, sim_par_str, model_str]) # write out the IDF files idf_path = os.path.join(folder, '{}.idf'.format(hb_model.identifier)) with open(idf_path, 'w') as idf_file: idf_file.write(idf_str) idfs.append(idf_path) log_file.write('\n'.join(idfs)) except Exception as e: _logger.exception('Model translation failed.\n{}\n'.format(e)) sys.exit(1) else: sys.exit(0)