def test_model_from_dict(self): """ Test loading a file from dict/AttrDict instead of from YAML """ this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, 'common', 'test_model', 'model.yaml') model_dict = AttrDict.from_yaml(model_location) location_dict = AttrDict({ 'locations': { '0': { 'techs': { 'test_supply_elec': {}, 'test_demand_elec': {} } }, '1': { 'techs': { 'test_supply_elec': {}, 'test_demand_elec': {} } } } }) model_dict.union(location_dict) model_dict.model['timeseries_data_path'] = os.path.join( this_path, 'common', 'test_model', model_dict.model['timeseries_data_path']) # test as AttrDict calliope.Model(model_dict) # test as dict calliope.Model(model_dict.as_dict())
def PSM_importance_subsampling(self, num_ts, num_ts_h): """Run PSM with demand and wind timeseries inputs generated using importance subsampling method. Parameters: ----------- num_ts: total number of sampled timesteps. If a number besides 120, 240, 480, 960, 1920, 3840 or 8760 is desired, the user should modify model_files/overrides.yaml to make a new entry num_ts_h: number of timesteps in bin with high variable cost Returns: none, but exports model outputs to CSV files """ # Create Calliope model with correct number of timesteps groupname = str(num_ts) + 'ts' override_file = 'model_files/overrides.yaml:' + groupname model = calliope.Model('model_files/model.yaml', override_file=override_file) # Stage 1: random subsampling of timesteps timesteps = np.random.choice(np.arange(self.data.shape[0]), num_ts, replace=False) data_sample_s1 = self.data.values[timesteps] demand_s1, wind_s1 = data_sample_s1[:, 0], data_sample_s1[:, 1] # Create model, adjust timesteps and weights, and run model.inputs.resource.loc['region1::demand_power'].values[:] = \ -demand_s1[:] model.inputs.resource.loc['region1::wind'].values[:] = wind_s1[:] model.run() # Stage 1 capacities used to calculate variable cost res = model.results cap_bl = float(res.energy_cap.loc['region1::baseload'].values) cap_mm = float(res.energy_cap.loc['region1::midmerit'].values) cap_pk = float(res.energy_cap.loc['region1::peaking'].values) cap_wd = float(res.resource_area.loc['region1::wind'].values) caps = [cap_bl, cap_mm, cap_pk, cap_wd] # Stage 2 demand, wind and weights demand_s2, wind_s2, weights_s2 = \ self.importance_subsampling(caps, num_ts, num_ts_h) # Create the model, input demand, wind and weights, run, and # export outputs to CSV model = calliope.Model('model_files/model.yaml', override_file=override_file) model.inputs.resource.loc['region1::demand_power'].values[:] = \ -demand_s2[:] model.inputs.resource.loc['region1::wind'].values[:] = wind_s2[:] model.inputs.timestep_weights.values = weights_s2 model.run() model.to_csv('outputs')
def build_test_model(override_dict=None, scenario=None): this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, 'test_model', 'model.yaml') return calliope.Model(model_location, override_dict=override_dict, scenario=scenario)
def test_info_minimal_model(self): this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, "common", "test_model", "model_minimal.yaml") model = calliope.Model(model_location) model.info()
def test_info_minimal_model(self): this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, 'common', 'test_model', 'model_minimal.yaml') model = calliope.Model(model_location) model.info()
def run_single_location(path_to_yaml, path_to_log, location, all_locations, subset_time, time_resolution, path_to_result): subset_time = subset_time[1:-1].split(",") subset_time = [x.strip()[1:-1] for x in subset_time] override_dict = { "model.subset_time": [subset_time[0], subset_time[1]], "model.time.function": "resample", "model.time.function_options": { "resolution": time_resolution }, f"locations.{location}.exists": "True" } no_locations = { f"locations.{other_location}.exists": False for other_location in all_locations if other_location != location } model = calliope.Model(path_to_yaml, override_dict={ **override_dict, **no_locations }) model.run_config['save_logs'] = path_to_log model.run() with open(path_to_result, "w") as f_result: f_result.write(model.results.termination_condition)
def build_model(override_dict, scenario): model_path = os.path.join(os.path.dirname(__file__), 'common', 'test_model', 'model.yaml') return calliope.Model(model_path, override_dict=override_dict, scenario=scenario)
def build_test_model(override_dict=None, scenario=None, model_file='model.yaml'): return calliope.Model(os.path.join(os.path.dirname(__file__), 'test_model', model_file), override_dict=override_dict, scenario=scenario)
def test_fail_with_spores_as_input_dim(self, base_model_data): spores_model = calliope.Model(config=None, model_data=base_model_data.loc[{ "spores": [0, 1] }]) with pytest.raises(exceptions.ModelError) as excinfo: spores_model.run(force_rerun=True) assert check_error_or_warning( excinfo, "Cannot run SPORES with a SPORES dimension in any input")
def build_model(override_dict, override_groups): model_path = os.path.join(os.path.dirname(__file__), 'common', 'test_model', 'model.yaml') overrides_path = os.path.join(os.path.dirname(__file__), 'common', 'test_model', 'overrides.yaml') return calliope.Model(model_path, override_dict=override_dict, override_file=overrides_path + ':' + override_groups)
def build_test_model(override_dict=None, override_groups=None): this_path = os.path.dirname(__file__) model_location = os.path.join(this_path, 'test_model', 'model.yaml') override_location = os.path.join(this_path, 'test_model', 'overrides.yaml') if override_groups: override_file = override_location + ':' + override_groups else: override_file = None return calliope.Model(model_location, override_dict=override_dict, override_file=override_file)
def build_test_model( override_dict=None, scenario=None, model_file="model.yaml", timeseries_dataframes=None, ): return calliope.Model( os.path.join(os.path.dirname(__file__), "test_model", model_file), override_dict=override_dict, scenario=scenario, timeseries_dataframes=timeseries_dataframes, )
def run_scenario(countries, regions_method, area_factor, rooftop_pv, save_dir, national=False): calliope.set_log_verbosity('CRITICAL', include_solver_output=False) eu_ds = euses.import_dataset('euses_datasets.nc') # remove power plants not considered fuels_considered = ['Biomass and biogas', 'Natural gas', 'Solar', 'Wind'] fuels_removed = np.setdiff1d(eu_ds.ds.coords['fuel'].values, fuels_considered) eu_ds.ds = eu_ds.ds.drop(fuels_removed, dim='fuel') tech_not_grouped = ['Solar', 'Wind', 'Wind Offshore'] tech_list = np.setdiff1d(eu_ds.ds.coords['tech'].values, tech_not_grouped) # group natural gas and biomass/biogas based power_plants in single group sum_var = eu_ds.ds['power_plants'].loc[:, tech_list, :].sum(axis=1) tech_list = np.setdiff1d(tech_list, ['Combined cycle']) eu_ds.ds['power_plants'].loc[:, ['Combined cycle']] = [[g] for g in sum_var.values ] eu_ds.ds = eu_ds.ds.drop(tech_list, dim='tech') # only percentage of rooftop_pv and utility_pv area considered eu_ds.ds['rooftop_pv'] = eu_ds.ds['rooftop_pv'] * rooftop_pv eu_ds.ds['utility_pv'] = eu_ds.ds['utility_pv'] * 0.50 filt_ds = eu_ds.filter_countries(countries) # Build and solve calliope model filt_ds.create_regions(regions_method, area_factor) regions_gpd = gpd.GeoDataFrame( filt_ds.ds_regions['regions'].values, columns=['id'], geometry=filt_ds.ds_regions['geometry'].values) filt_ds.create_calliope_model(op_mode='plan', sectors=['power', 'heat'], co2_cap_factor=0.2, national=national) model = calliope.Model('calliope_model/model.yaml', scenario='time_3H', override_dict={'run.solver': 'gurobi'}) model.run() model.to_netcdf('calliope_model/results/' + save_dir + '.nc') return model, regions_gpd
def test_nationalscale_skip_cost_op_spores(self, base_model_data, init_spore): spores_model = calliope.Model(config=None, model_data=base_model_data.loc[{ "spores": [init_spore + 1] }]) spores_model._model_data.coords["spores"] = [init_spore] spores_model.run_config["spores_options"]["skip_cost_op"] = True spores_model.run(force_rerun=True) assert set(spores_model.results.spores.values) == set( range(init_spore, 4)) assert base_model_data.loc[{ "spores": slice(init_spore + 1, None) }].equals(spores_model._model_data.loc[{ "spores": slice(init_spore + 1, None) }])
def example_model(self): return calliope.Model(path_to_example_model)
def build_model(override_dict, override_groups): return calliope.Model(model_location, override_dict=override_dict, override_file=override_location + ':' + override_groups)
import os import pandas as pd import numpy as np c = 0 # setting the path for calliope _PATHS = { 'GISEle': os.path.join(os.path.dirname(__file__), '..', 'Input', '7_sizing_calliope') } print(_PATHS) # calling and running the Model calliope.set_log_level('INFO') model = calliope.Model(os.path.join(_PATHS['GISEle'], 'model.yaml')) print('ciao') model.run() print("Model runned") os.chdir(r'..//Output//Sizing_calliope') 'Saving results' #model.to_csv("GISEle_results_" + str(c)) model.plot.summary(to_file='gisele' + str(c) + '.html') model.plot.transmission( mapbox_access_token= 'pk.eyJ1Ijoibm9uZGFybG8iLCJhIjoiY2p3ZHNlOHRuMGVvaTQ5bXJrY295czk2aiJ9.tLfzfo2Q6DTKNwfHuTERbQ' )
def build_model(override_dict, scenario): model_path = os.path.join( os.path.dirname(__file__), "common", "test_model", "model.yaml" ) return calliope.Model(model_path, override_dict=override_dict, scenario=scenario)
def model(self): model = calliope.Model() model.run() return model
def test_model_initialization_override_dict(self): override = {'output.save': True} with pytest.raises(AssertionError): calliope.Model(override=override)
def test_model_initialization_default(self): model = calliope.Model() model.run()
# -*- coding: utf-8 -*- """ Created on Tue Sep 1 11:12:58 2020 @author: Mohammad Amin Tahavori """ import time start = time.time() import calliope import pickle try: calliope.set_log_level('INFO') except: calliope.set_log_verbosity('INFO') model = calliope.Model('model.yaml') model.run() end=time.time() print(str(end-start)) #%% import cal_graph as CG amin=CG.C_Graph(model=model,ex_path=r'Graph_inputs.xlsx',unit='kW') # with open('cof_4n', 'wb') as config_dictionary_file: # pickle.dump(amin,config_dictionary_file) #%% amin.system_pie(kind='absolute',unit='GWh',v_round=1,title_font=40) #%% amin.node_pie(kind='absolute',unit='GWh',v_round=1,title_font=40) #%% amin.node_pie(kind='absolute',unit='GWh',v_round=1,title_font=40,rational='consumption')
def optimised_example_model(self): model = calliope.Model(path_to_example_model) model.run() return model
import calliope import os DIR = os.getcwd() calliope.set_log_verbosity('INFO') model = calliope.Model(DIR + '/model.yaml', scenario='milp') model.run() # model.results
def rerun_pyomo_model(model_data, run_config, backend_model): """ Rerun the Pyomo backend, perhaps after updating a parameter value, (de)activating a constraint/objective or updating run options in the model model_data object (e.g. `run.solver`). Returns ------- new_model : calliope.Model New calliope model, including both inputs and results, but no backend interface. """ backend_model.__calliope_run_config = run_config if run_config["mode"] != "plan": raise exceptions.ModelError( "Cannot rerun the backend in {} run mode. Only `plan` mode is " "possible.".format(run_config["mode"])) timings = {} log_time(logger, timings, "model_creation") results, backend_model = backend_run.run_plan( model_data, run_config, timings, run_pyomo, build_only=False, backend_rerun=backend_model, ) inputs = access_pyomo_model_inputs(backend_model) # Add additional post-processed result variables to results if results.attrs.get("termination_condition", None) in ["optimal", "feasible"]: results = postprocess_model_results(results, model_data.reindex(results.coords), timings) for key, var in results.data_vars.items(): var.attrs["is_result"] = 1 for key, var in inputs.data_vars.items(): var.attrs["is_result"] = 0 new_model_data = xr.merge((results, inputs)) new_model_data.attrs.update(model_data.attrs) new_model_data.attrs.update(results.attrs) # Only add coordinates from the original model_data that don't already exist new_coords = [ i for i in model_data.coords.keys() if i not in new_model_data.coords.keys() ] new_model_data = new_model_data.update(model_data[new_coords]) # Reorganise the coordinates so that model data and new model data share # the same order of items in each dimension new_model_data = new_model_data.reindex(model_data.coords) exceptions.warn( "The results of rerunning the backend model are only available within " "the Calliope model returned by this function call.") new_calliope_model = calliope.Model(config=None, model_data=new_model_data) new_calliope_model._timings = timings return new_calliope_model
def test_model_initialization_follow_nested_import_statements(self): model = calliope.Model() assert 'links' in model.config_model
def model(self, scenario): return calliope.Model(path_to_model, scenario=scenario)
def test_model_initialization_override_attrdict(self): override = calliope.utils.AttrDict({'output': {'save': True}}) model = calliope.Model(override=override) assert model.config_run.output.save is True
excl_nos_number = 3 # number of NOS to be generated for each loc::tech minimisation attempt ppt_nos_number = 20 # number of NOS to be generated for ppts minimisation desired_slack = 'max_cost20' # selects the desired slack percentage #%% ''' -------------------------------------------------------- ---------------ITERATION 0---------------(min total_cost) -------------------------------------------------------- ''' ''' Model creation, run and saving to netCDF - Iteration 0 ''' model = calliope.Model( 'Model/model.yaml', scenario='2050_eff') #this version only includes the power sector model.run() model.to_netcdf('NetCDFs/results_0.nc') ''' Alternatively, previously run solutions can be read from netCDF files ''' # model = calliope.read_netcdf('NetCDFs/results_0.nc') # model.run(build_only=True, force_rerun=True) model_0 = calliope.read_netcdf('NetCDFs/results_0.nc') ''' Computation of nos_scores per location ''' cap_loc_score_0 = cap_loc_score_potential(model_0, techs=techs_new) ''' Extrapolation of relevant indicators
def test_model_initialization_default(self): model = calliope.Model() assert hasattr(model, 'data') assert hasattr(model, 'config_run') assert hasattr(model, 'config_model') assert model.config_run.mode == 'plan'