From 2e7f7c2eee7374a9da72958894c12f569f1d507c Mon Sep 17 00:00:00 2001 From: RoaldL Date: Fri, 2 Feb 2018 17:27:28 +0000 Subject: [PATCH] Wrapper runs in smif 0.6 --- run.py | 122 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 60 insertions(+), 62 deletions(-) diff --git a/run.py b/run.py index 1c27e406..009842e1 100644 --- a/run.py +++ b/run.py @@ -36,22 +36,21 @@ class EDWrapper(SectorModel): """Energy Demand Wrapper """ - def before_model_run(self, data=None): - """Runs prior to any ``simulate()`` step + def __init__(self, name): + super().__init__(name) - Writes scenario data out into the scenario files + self.user_data = {} - Saves useful data into the ``self.user_data`` dictionary for access - in the ``simulate()`` method + def before_model_run(self, data_handle): + """Implement this method to conduct pre-model run tasks - Data is accessed using the `get_scenario_data()` method is provided - as a numpy array with the dimensions timesteps-by-regions-by-intervals. - - Info - ----- - `self.user_data` allows to pass data from before_model_run to main model + Arguments + --------- + data_handle: smif.data_layer.DataHandle + Access parameter values (before any model is run, no dependency + input data or state is guaranteed to be available) """ - data = defaultdict(dict, data) + data = defaultdict(dict) # Criteria data['criterias']['mode_constrained'] = False # True: Technologies are defined in ED model and fuel is provided, False: Heat is delievered not per technologies @@ -65,7 +64,7 @@ def before_model_run(self, data=None): data['criterias']['plot_crit'] = True data['criterias']['plot_tech_lp'] = True - data['sim_param']['base_yr'] = 2015 # Base year + data['sim_param']['base_yr'] = data_handle.timesteps[0] # Base year data['sim_param']['curr_yr'] = data['sim_param']['base_yr'] self.user_data['base_yr'] = data['sim_param']['base_yr'] @@ -121,12 +120,17 @@ def before_model_run(self, data=None): # ----------------------------- # Obtain external scenario data # ----------------------------- - pop_array = self.get_scenario_data('population') - pop_dict = self.array_to_dict(pop_array) - data['population'][data['sim_param']['base_yr']] = pop_dict[2015] # Get only population of base year - - gva_array = self.get_scenario_data('gva') - data['gva'] = self.array_to_dict(gva_array) + pop_array = data_handle.get_base_timestep_data('population') + pop_dict = {} + for r_idx, region in enumerate(self.get_region_names(REGION_SET_NAME)): + pop_dict[region] = pop_array[r_idx, 0] + data['population'][data['sim_param']['base_yr']] = pop_dict + + gva_array = data_handle.get_base_timestep_data('gva') + gva_dict = {} + for r_idx, region in enumerate(self.get_region_names(REGION_SET_NAME)): + gva_dict[region] = gva_array[r_idx, 0] + data['gva'][data['sim_param']['base_yr']] = gva_dict # Get building related data if data['criterias']['virtual_building_stock_criteria']: @@ -197,8 +201,10 @@ def before_model_run(self, data=None): # ------------------------ # Load all SMIF parameters and replace data dict # ------------------------ + + parameters = data_handle.get_parameters() data['assumptions'] = self.load_smif_parameters( - data, + parameters, data['assumptions']) # Update technologies after strategy definition @@ -210,8 +216,8 @@ def before_model_run(self, data=None): # ------------------------ # Pass along to simulate() # ------------------------ - self.user_data['gva'] = self.array_to_dict(gva_array) - self.user_data['population'] = self.array_to_dict(pop_array) + self.user_data['gva'] = data['gva'] + self.user_data['population'] = data['population'] self.user_data['rs_floorarea'] = rs_floorarea self.user_data['ss_floorarea'] = ss_floorarea self.user_data['data_pass_along'] = {} @@ -245,11 +251,9 @@ def before_model_run(self, data=None): pop_array[t_idx]) def initialise(self, initial_conditions): - """ - """ pass - def simulate(self, timestep, data=None): + def simulate(self, data_handle): """Runs the Energy Demand model for one `timestep` Arguments @@ -284,8 +288,8 @@ def simulate(self, timestep, data=None): """ time_start = datetime.datetime.now() - # Convert data to default dict - data = defaultdict(dict, data) + # Init default dict + data = defaultdict(dict) # Paths path_main = resource_filename(Requirement.parse("energy_demand"), "") @@ -311,7 +315,7 @@ def simulate(self, timestep, data=None): logger_setup.set_up_logger(os.path.join(data['local_paths']['data_results'], "logger_smif_run.log")) data['sim_param']['base_yr'] = self.user_data['base_yr'] # Base year definition - data['sim_param']['curr_yr'] = timestep # Read in current year from smif + data['sim_param']['curr_yr'] = data_handle.current_timestep # Read in current year from smif data['sim_param']['simulated_yrs'] = self.timesteps # Read in all simulated years from smif # --------------------------------------------- @@ -331,15 +335,26 @@ def simulate(self, timestep, data=None): # --------------------------------------------- # Scenario data # --------------------------------------------- - pop_array = self.get_scenario_data('population') #for simulation year - pop_dict = self.array_to_dict(pop_array) + pop_array_current = data_handle.get_data('population') #for simulation year + gva_array_current = data_handle.get_data('gva') #for simulation year + + gva_dict_current = {} + pop_dict_current = {} + + for r_idx, region in enumerate(self.get_region_names(REGION_SET_NAME)): + pop_dict_current[region] = pop_array_current[r_idx, 0] + gva_dict_current[region] = gva_array_current[r_idx, 0] pop_by_cy = {} - pop_by_cy[data['sim_param']['base_yr']] = pop_dict[2015] # Get population of by - pop_by_cy[data['sim_param']['curr_yr']] = pop_dict[data['sim_param']['curr_yr']] # Get population of cy + pop_by_cy[data['sim_param']['base_yr']] = self.user_data['population'][data_handle.base_timestep] # Get population of by + pop_by_cy[data['sim_param']['curr_yr']] = pop_dict_current # Get population of cy + + gva_by_cy = {} + gva_by_cy[data['sim_param']['base_yr']] = self.user_data['gva'][data_handle.base_timestep] # Get gva of by + gva_by_cy[data['sim_param']['curr_yr']] = gva_dict_current # Get gva of cy data['scenario_data'] = { - 'gva': self.user_data['gva'], + 'gva': gva_by_cy, 'population': pop_by_cy, # Only add newcastle floorarea here @@ -366,7 +381,7 @@ def simulate(self, timestep, data=None): # ------------------------------------------------ # Validation base year: Hourly temporal validation # ------------------------------------------------ - if data['criterias']['validation_criteria'] == True and timestep == data['sim_param']['base_yr']: + if data['criterias']['validation_criteria'] == True and data_handle.current_timestep == data['sim_param']['base_yr']: lad_validation.tempo_spatial_validation( data['sim_param']['base_yr'], data['assumptions']['model_yearhours_nrs'], @@ -395,7 +410,7 @@ def simulate(self, timestep, data=None): # Plot individual enduse # ---- crit_plot_enduse_lp = True - if crit_plot_enduse_lp and timestep == 2015: + if crit_plot_enduse_lp and data_handle.current_timestep == 2015: # Maybe move to result folder in a later step path_folder_lp = os.path.join(data['local_paths']['data_results'], 'individual_enduse_lp') @@ -421,23 +436,23 @@ def simulate(self, timestep, data=None): path_run = data['local_paths']['data_results_model_runs'] write_data.write_supply_results( - timestep, "result_tot_yh", path_run, sim_obj.ed_fueltype_regs_yh, "result_tot_submodels_fueltypes") + data_handle.current_timestep, "result_tot_yh", path_run, sim_obj.ed_fueltype_regs_yh, "result_tot_submodels_fueltypes") write_data.write_enduse_specific( - timestep, path_run, sim_obj.tot_fuel_y_enduse_specific_yh, "out_enduse_specific") + data_handle.current_timestep, path_run, sim_obj.tot_fuel_y_enduse_specific_yh, "out_enduse_specific") write_data.write_max_results( - timestep, path_run, "result_tot_peak_enduses_fueltype", sim_obj.tot_peak_enduses_fueltype, "tot_peak_enduses_fueltype") + data_handle.current_timestep, path_run, "result_tot_peak_enduses_fueltype", sim_obj.tot_peak_enduses_fueltype, "tot_peak_enduses_fueltype") write_data.write_lf( - path_run, "result_reg_load_factor_y", [timestep], sim_obj.reg_load_factor_y, 'reg_load_factor_y') + path_run, "result_reg_load_factor_y", [data_handle.current_timestep], sim_obj.reg_load_factor_y, 'reg_load_factor_y') write_data.write_lf( - path_run, "result_reg_load_factor_yd", [timestep], sim_obj.reg_load_factor_yd, 'reg_load_factor_yd') + path_run, "result_reg_load_factor_yd", [data_handle.current_timestep], sim_obj.reg_load_factor_yd, 'reg_load_factor_yd') write_data.write_lf( - path_run, "result_reg_load_factor_winter", [timestep], sim_obj.reg_seasons_lf['winter'], 'reg_load_factor_winter') + path_run, "result_reg_load_factor_winter", [data_handle.current_timestep], sim_obj.reg_seasons_lf['winter'], 'reg_load_factor_winter') write_data.write_lf( - path_run, "result_reg_load_factor_spring", [timestep], sim_obj.reg_seasons_lf['spring'], 'reg_load_factor_spring') + path_run, "result_reg_load_factor_spring", [data_handle.current_timestep], sim_obj.reg_seasons_lf['spring'], 'reg_load_factor_spring') write_data.write_lf( - path_run, "result_reg_load_factor_summer", [timestep], sim_obj.reg_seasons_lf['summer'], 'reg_load_factor_summer') + path_run, "result_reg_load_factor_summer", [data_handle.current_timestep], sim_obj.reg_seasons_lf['summer'], 'reg_load_factor_summer') write_data.write_lf( - path_run, "result_reg_load_factor_autumn", [timestep], sim_obj.reg_seasons_lf['autumn'], 'reg_load_factor_autumn') + path_run, "result_reg_load_factor_autumn", [data_handle.current_timestep], sim_obj.reg_seasons_lf['autumn'], 'reg_load_factor_autumn') logging.info("... finished writing results to file") # ------------------------------------ @@ -517,23 +532,7 @@ def simulate(self, timestep, data=None): return supply_results def extract_obj(self, results): - """Implement this method to return a scalar value objective function - - This method should take the results from the output of the `simulate` - method, process the results, and return a scalar value which can be - used as the objective function - - Arguments - ========= - results : :class:`dict` - The results from the `simulate` method - - Returns - ======= - float - A scalar component generated from the simulation model results - """ - pass + return 0 def pass_to_simulate(self, dict_to_copy_into, dict_to_pass_along): """Pass dict defined in before_model_run() to simlate() function @@ -599,7 +598,6 @@ def load_smif_parameters(self, data, assumptions): # Get narrative variable from input data dict strategy_variables[var_name] = data[var_name] - del data[var_name] # Add to assumptions assumptions['strategy_variables'] = strategy_variables