def dict_to_simulation(sim_dict, raise_exception=True): """Get a Python object of any Simulation object from a dictionary. Args: sim_dict: A dictionary of any Honeybee energy simulation object. Note that this should be a non-abridged dictionary to be valid. raise_exception: Boolean to note whether an excpetion should be raised if the object is not identified as a simulation object. Default: True. Returns: A Python object derived from the input sim_dict. """ try: # get the type key from the dictionary sim_type = sim_dict['type'] except KeyError: raise ValueError('Simulation dictionary lacks required "type" key.') if sim_type == 'SimulationControl': return SimulationControl.from_dict(sim_dict) elif sim_type == 'RunPeriod': return RunPeriod.from_dict(sim_dict) elif sim_type == 'DaylightSavingTime': return DaylightSavingTime.from_dict(sim_dict) elif sim_type == 'ShadowCalculation': return ShadowCalculation.from_dict(sim_dict) elif sim_type == 'SizingParameter': return SizingParameter.from_dict(sim_dict) elif sim_type == 'SimulationOutput': return SimulationOutput.from_dict(sim_dict) elif sim_type == 'SimulationParameter': return SimulationParameter.from_dict(sim_dict) elif raise_exception: raise ValueError( '{} is not a recognized energy Simulation type'.format(sim_type))
def default_sim_par(ddy_file, run_period_json, filter_des_days, output_file): """Get a SimulationParameter JSON with default outputs for energy use only. \n Args: ddy_file: Full path to a DDY file that will be used to specify design days within the simulation parameter. """ try: assert os.path.isfile(ddy_file), 'No DDY file found at {}.'.format( ddy_file) sim_par = SimulationParameter() sim_par.output.add_zone_energy_use() sim_par.output.add_hvac_energy_use() if run_period_json: assert os.path.isfile(run_period_json), \ 'No run period JSON file found at {}.'.format(run_period_json) with open(run_period_json) as json_file: data = json.load(json_file) sim_par.run_period = RunPeriod.from_dict(data) if filter_des_days: sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) else: sim_par.sizing_parameter.add_from_ddy(ddy_file) output_file.write(json.dumps(sim_par.to_dict())) except Exception as e: _logger.exception( 'Failed to generate simulation parameter.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def custom_sim_par(ddy_file, output_names, run_period_json, filter_des_days, output_file): """Get a SimulationParameter JSON with outputs for thermal load balances. \n Args: ddy_file: Full path to a DDY file that will be used to specify design days within the simulation parameter. output_names: A list of EnergyPlus output names as strings (eg. 'Surface Window System Solar Transmittance'. These outputs will be requested from the simulation. """ try: assert os.path.isfile(ddy_file), 'No DDY file found at {}.'.format( ddy_file) sim_par = SimulationParameter() for output_name in output_names: sim_par.output.add_output(output_name) if run_period_json: assert os.path.isfile(run_period_json), \ 'No run period JSON file found at {}.'.format(run_period_json) with open(run_period_json) as json_file: data = json.load(json_file) sim_par.run_period = RunPeriod.from_dict(data) if filter_des_days: sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) else: sim_par.sizing_parameter.add_from_ddy(ddy_file) output_file.write(json.dumps(sim_par.to_dict())) except Exception as e: _logger.exception( 'Failed to generate simulation parameter.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def load_balance_sim_par(ddy_file, load_type, run_period_json, filter_des_days, output_file): """Get a SimulationParameter JSON with outputs for thermal load balances. \n Args: ddy_file: Full path to a DDY file that will be used to specify design days within the simulation parameter. """ try: assert os.path.isfile(ddy_file), 'No DDY file found at {}.'.format( ddy_file) sim_par = SimulationParameter() sim_par.output.add_zone_energy_use(load_type) gl_load_type = load_type if load_type != 'All' else 'Total' sim_par.output.add_gains_and_losses(gl_load_type) sim_par.output.add_surface_energy_flow() if run_period_json: assert os.path.isfile(run_period_json), \ 'No run period JSON file found at {}.'.format(run_period_json) with open(run_period_json) as json_file: data = json.load(json_file) sim_par.run_period = RunPeriod.from_dict(data) if filter_des_days: sim_par.sizing_parameter.add_from_ddy_996_004(ddy_file) else: sim_par.sizing_parameter.add_from_ddy(ddy_file) output_file.write(json.dumps(sim_par.to_dict())) except Exception as e: _logger.exception( 'Failed to generate simulation parameter.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def test_run_period(): """Test the run_period command.""" runner = CliRunner() result = runner.invoke(run_period, ['1', '6', '1', '12']) assert result.exit_code == 0 run_per_dict = json.loads(result.output) run_per = RunPeriod.from_dict(run_per_dict) assert run_per.start_date == Date(1, 6) assert run_per.end_date == Date(1, 12)
def test_run_period_dict_methods(): """Test the to/from dict methods.""" run_period = RunPeriod() run_period.start_date = Date(1, 1) run_period.end_date = Date(6, 21) run_period.start_day_of_week = 'Monday' run_period.holidays = (Date(1, 1), Date(3, 17)) run_period.daylight_saving_time = DaylightSavingTime() rp_dict = run_period.to_dict() new_run_period = RunPeriod.from_dict(rp_dict) assert new_run_period == run_period assert rp_dict == new_run_period.to_dict()