def make_dantzig(mp, solve=False, multi_year=False, **solve_opts): """Return an :class:`message_ix.Scenario` for Dantzig's canning problem. Parameters ---------- mp : ixmp.Platform Platform on which to create the scenario. solve : bool, optional If True, the scenario is solved. multi_year : bool, optional If True, the scenario has years 1963--1965 inclusive. Otherwise, the scenario has the single year 1963. """ # add custom units and region for timeseries data mp.add_unit('USD/case') mp.add_unit('case') mp.add_region('DantzigLand', 'country') # initialize a new (empty) instance of an `ixmp.Scenario` scen = Scenario( mp, model=SCENARIO['dantzig']['model'], scenario='multi-year' if multi_year else 'standard', annotation="Dantzig's canning problem as a MESSAGE-scheme Scenario", version='new') # Sets # NB commit() is refused if technology and year are not given t = ['canning_plant', 'transport_from_seattle', 'transport_from_san-diego'] sets = { 'technology': t, 'node': 'seattle san-diego new-york chicago topeka'.split(), 'mode': 'production to_new-york to_chicago to_topeka'.split(), 'level': 'supply consumption'.split(), 'commodity': ['cases'], } for name, values in sets.items(): scen.add_set(name, values) scen.add_horizon({'year': [1962, 1963], 'firstmodelyear': 1963}) # Parameters par = {} demand = { 'node': 'new-york chicago topeka'.split(), 'value': [325, 300, 275] } par['demand'] = make_df(pd.DataFrame.from_dict(demand), commodity='cases', level='consumption', time='year', unit='case', year=1963) b_a_u = {'node_loc': ['seattle', 'san-diego'], 'value': [350, 600]} par['bound_activity_up'] = make_df(pd.DataFrame.from_dict(b_a_u), mode='production', technology='canning_plant', time='year', unit='case', year_act=1963) par['ref_activity'] = par['bound_activity_up'].copy() input = pd.DataFrame( [ ['to_new-york', 'seattle', 'seattle', t[1]], ['to_chicago', 'seattle', 'seattle', t[1]], ['to_topeka', 'seattle', 'seattle', t[1]], ['to_new-york', 'san-diego', 'san-diego', t[2]], ['to_chicago', 'san-diego', 'san-diego', t[2]], ['to_topeka', 'san-diego', 'san-diego', t[2]], ], columns=['mode', 'node_loc', 'node_origin', 'technology']) par['input'] = make_df(input, commodity='cases', level='supply', time='year', time_origin='year', unit='case', value=1, year_act=1963, year_vtg=1963) output = pd.DataFrame( [ ['supply', 'production', 'seattle', 'seattle', t[0]], ['supply', 'production', 'san-diego', 'san-diego', t[0]], ['consumption', 'to_new-york', 'new-york', 'seattle', t[1]], ['consumption', 'to_chicago', 'chicago', 'seattle', t[1]], ['consumption', 'to_topeka', 'topeka', 'seattle', t[1]], ['consumption', 'to_new-york', 'new-york', 'san-diego', t[2]], ['consumption', 'to_chicago', 'chicago', 'san-diego', t[2]], ['consumption', 'to_topeka', 'topeka', 'san-diego', t[2]], ], columns=['level', 'mode', 'node_dest', 'node_loc', 'technology']) par['output'] = make_df(output, commodity='cases', time='year', time_dest='year', unit='case', value=1, year_act=1963, year_vtg=1963) # Variable cost: cost per kilometre × distance (neither parametrized # explicitly) var_cost = pd.DataFrame( [ ['to_new-york', 'seattle', 'transport_from_seattle', 0.225], ['to_chicago', 'seattle', 'transport_from_seattle', 0.153], ['to_topeka', 'seattle', 'transport_from_seattle', 0.162], ['to_new-york', 'san-diego', 'transport_from_san-diego', 0.225], ['to_chicago', 'san-diego', 'transport_from_san-diego', 0.162], ['to_topeka', 'san-diego', 'transport_from_san-diego', 0.126], ], columns=['mode', 'node_loc', 'technology', 'value']) par['var_cost'] = make_df(var_cost, time='year', unit='USD/case', year_act=1963, year_vtg=1963) for name, value in par.items(): scen.add_par(name, value) if multi_year: scen.add_set('year', [1964, 1965]) scen.add_par('technical_lifetime', ['seattle', 'canning_plant', 1964], 3, 'y') if solve: # Always read one equation. Used by test_core.test_year_int. scen.init_equ('COMMODITY_BALANCE_GT', ['node', 'commodity', 'level', 'year', 'time']) solve_opts['equ_list'] = solve_opts.get('equ_list', []) \ + ['COMMODITY_BALANCE_GT'] scen.commit('Created a MESSAGE-scheme version of the transport problem.') scen.set_as_default() if solve: scen.solve(**solve_opts) scen.check_out(timeseries_only=True) scen.add_timeseries(HIST_DF, meta=True) scen.add_timeseries(INP_DF) scen.commit("Import Dantzig's transport problem for testing.") return scen
def make_westeros(mp, emissions=False, solve=False): """Return an :class:`message_ix.Scenario` for the Westeros model. This is the same model used in the ``westeros_baseline.ipynb`` tutorial. Parameters ---------- mp : ixmp.Platform Platform on which to create the scenario. emissions : bool, optional If True, the ``emissions_factor`` parameter is also populated for CO2. solve : bool, optional If True, the scenario is solved. """ scen = Scenario(mp, version='new', **SCENARIO['westeros']) # Sets history = [690] model_horizon = [700, 710, 720] scen.add_horizon({ 'year': history + model_horizon, 'firstmodelyear': model_horizon[0] }) country = 'Westeros' scen.add_spatial_sets({'country': country}) sets = { 'technology': 'coal_ppl wind_ppl grid bulb'.split(), 'mode': ['standard'], 'level': 'secondary final useful'.split(), 'commodity': 'electricity light'.split(), } for name, values in sets.items(): scen.add_set(name, values) # Parameters — copy & paste from the tutorial notebook gdp_profile = pd.Series([1., 1.5, 1.9], index=pd.Index(model_horizon, name='Time')) demand_per_year = 40 * 12 * 1000 / 8760 light_demand = pd.DataFrame({ 'node': country, 'commodity': 'light', 'level': 'useful', 'year': model_horizon, 'time': 'year', 'value': (100 * gdp_profile).round(), 'unit': 'GWa', }) scen.add_par("demand", light_demand) year_df = scen.vintage_and_active_years() vintage_years, act_years = year_df['year_vtg'], year_df['year_act'] base = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'mode': 'standard', 'time': 'year', 'unit': '-', } base_input = make_df(base, node_origin=country, time_origin='year') base_output = make_df(base, node_dest=country, time_dest='year') bulb_out = make_df(base_output, technology='bulb', commodity='light', level='useful', value=1.0) scen.add_par('output', bulb_out) bulb_in = make_df(base_input, technology='bulb', commodity='electricity', level='final', value=1.0) scen.add_par('input', bulb_in) grid_efficiency = 0.9 grid_out = make_df(base_output, technology='grid', commodity='electricity', level='final', value=grid_efficiency) scen.add_par('output', grid_out) grid_in = make_df(base_input, technology='grid', commodity='electricity', level='secondary', value=1.0) scen.add_par('input', grid_in) coal_out = make_df(base_output, technology='coal_ppl', commodity='electricity', level='secondary', value=1.) scen.add_par('output', coal_out) wind_out = make_df(base_output, technology='wind_ppl', commodity='electricity', level='secondary', value=1.) scen.add_par('output', wind_out) base_capacity_factor = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'time': 'year', 'unit': '-', } capacity_factor = { 'coal_ppl': 1, 'wind_ppl': 1, 'bulb': 1, } for tec, val in capacity_factor.items(): df = make_df(base_capacity_factor, technology=tec, value=val) scen.add_par('capacity_factor', df) base_technical_lifetime = { 'node_loc': country, 'year_vtg': model_horizon, 'unit': 'y', } lifetime = { 'coal_ppl': 20, 'wind_ppl': 20, 'bulb': 1, } for tec, val in lifetime.items(): df = make_df(base_technical_lifetime, technology=tec, value=val) scen.add_par('technical_lifetime', df) base_growth = { 'node_loc': country, 'year_act': model_horizon, 'time': 'year', 'unit': '-', } growth_technologies = [ "coal_ppl", "wind_ppl", ] for tec in growth_technologies: df = make_df(base_growth, technology=tec, value=0.1) scen.add_par('growth_activity_up', df) historic_demand = 0.85 * demand_per_year historic_generation = historic_demand / grid_efficiency coal_fraction = 0.6 base_capacity = { 'node_loc': country, 'year_vtg': history, 'unit': 'GWa', } base_activity = { 'node_loc': country, 'year_act': history, 'mode': 'standard', 'time': 'year', 'unit': 'GWa', } old_activity = { 'coal_ppl': coal_fraction * historic_generation, 'wind_ppl': (1 - coal_fraction) * historic_generation, } for tec, val in old_activity.items(): df = make_df(base_activity, technology=tec, value=val) scen.add_par('historical_activity', df) act_to_cap = { # 20 year lifetime 'coal_ppl': 1 / 10 / capacity_factor['coal_ppl'] / 2, 'wind_ppl': 1 / 10 / capacity_factor['wind_ppl'] / 2, } for tec in act_to_cap: value = old_activity[tec] * act_to_cap[tec] df = make_df(base_capacity, technology=tec, value=value) scen.add_par('historical_new_capacity', df) rate = [0.05] * len(model_horizon) unit = ['-'] * len(model_horizon) scen.add_par("interestrate", model_horizon, rate, unit) base_inv_cost = { 'node_loc': country, 'year_vtg': model_horizon, 'unit': 'USD/GWa', } # in $ / kW costs = { 'coal_ppl': 500, 'wind_ppl': 1500, 'bulb': 5, } for tec, val in costs.items(): df = make_df(base_inv_cost, technology=tec, value=val) scen.add_par('inv_cost', df) base_fix_cost = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'unit': 'USD/GWa', } # in $ / kW costs = { 'coal_ppl': 30, 'wind_ppl': 10, } for tec, val in costs.items(): df = make_df(base_fix_cost, technology=tec, value=val) scen.add_par('fix_cost', df) base_var_cost = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'mode': 'standard', 'time': 'year', 'unit': 'USD/GWa', } # in $ / MWh costs = { 'coal_ppl': 30, 'grid': 50, } for tec, val in costs.items(): df = make_df(base_var_cost, technology=tec, value=val) scen.add_par('var_cost', df) scen.commit('basic model of Westerosi electrification') scen.set_as_default() if emissions: scen.check_out() # Introduce the emission species CO2 and the emission category GHG scen.add_set('emission', 'CO2') scen.add_cat('emission', 'GHG', 'CO2') # we now add CO2 emissions to the coal powerplant base_emission_factor = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'mode': 'standard', 'unit': 'USD/GWa', } emission_factor = make_df(base_emission_factor, technology='coal_ppl', emission='CO2', value=100.) scen.add_par('emission_factor', emission_factor) scen.commit('Added emissions sets/params to Westeros model.') if solve: scen.solve() return scen
def add_reliability_flexibility_parameter(data, model_par, raw_data): rel_flex = raw_data['base_input']['rel_and_flex'] model = {} rating_bin = [] reliability_factor = [] flexibility_factor = [] rating_bin_unit = data['units']['rating_bin']['unit'] reliability_factor_unit = data['units']['reliability_factor']['unit'] flexibility_factor_unit = data['units']['flexibility_factor']['unit'] output = model_par['output'] for i in rel_flex.index: node = rel_flex.at[i, 'node'] technology = rel_flex.at[i, 'technology'] mode = data['technology'][technology]['mode'] commodity = rel_flex.at[i, 'commodity'] level = rel_flex.at[i, 'level'] time = rel_flex.at[i, 'time'] rating = rel_flex.at[i, 'rating'] logger.debug(f'Create reliability flexibility parameters ' f'for {technology} in {node}') rating_bin_value = rel_flex.at[i, 'rating_bin'] reliability_factor_value = rel_flex.at[i, 'reliability_factor'] flexibility_factor_value = rel_flex.at[i, 'flexibility_factor'] model_years = output[ output.technology == technology].year_act.unique().tolist() active_years = output[ output.technology == technology].year_act.tolist() vintage_years = output[ output.technology == technology].year_vtg.tolist() base_par = pd.DataFrame({ 'node': node, 'technology': technology, 'year_act': model_years, 'commodity': commodity, 'level': level, 'time': time, 'rating': rating}) rating_bin.append( make_df(base_par, value=rating_bin_value, unit=rating_bin_unit)) reliability_factor.append( make_df(base_par, value=reliability_factor_value, unit=reliability_factor_unit)) base_flex = pd.DataFrame({ 'node_loc': node, 'technology': technology, 'year_act': active_years, 'year_vtg': vintage_years, 'commodity': commodity, 'level': level, 'mode': mode, 'time': time, 'rating': rating}) flexibility_factor.append( make_df(base_flex, value=flexibility_factor_value, unit=flexibility_factor_unit)) rating_bin = pd.concat(rating_bin, sort=False, ignore_index=True) rating_bin['year_act'] = rating_bin['year_act'].astype(int) model['rating_bin'] = rating_bin reliability_factor = pd.concat(reliability_factor, sort=False, ignore_index=True) reliability_factor['year_act'] = reliability_factor['year_act'].astype(int) model['reliability_factor'] = reliability_factor flexibility_factor = pd.concat(flexibility_factor, sort=False, ignore_index=True) flexibility_factor['year_act'] = flexibility_factor['year_act'].astype(int) flexibility_factor['year_vtg'] = flexibility_factor['year_vtg'].astype(int) model['flexibility_factor'] = flexibility_factor return model
def test_make_df(): base = {'foo': 'bar'} exp = pd.DataFrame({'foo': 'bar', 'baz': [42, 42]}) obs = utils.make_df(base, baz=[42, 42]) pdt.assert_frame_equal(obs, exp)
# We use add_par for adding data to a MESSAGEix parameter scenario.add_par("demand", light_demand) year_df = scenario.vintage_and_active_years() vintage_years, act_years = year_df['year_vtg'], year_df['year_act'] base = { 'node_loc': country, 'year_vtg': vintage_years, 'year_act': act_years, 'mode': 'standard', 'time': 'year', 'unit': '-', } base_input = make_df(base, node_origin=country, time_origin='year') base_output = make_df(base, node_dest=country, time_dest='year') bulb_out = make_df(base_output, technology='bulb', commodity='light', level='useful', value=1.0) scenario.add_par('output', bulb_out) bulb_in = make_df(base_input, technology='bulb', commodity='electricity', level='final', value=1.0) scenario.add_par('input', bulb_in)
def add_reliability_flexibility_parameter( data: Data, model_par: ModelPar, raw_data: RawData) -> Dict[str, pd.DataFrame]: rel_flex = raw_data['base_input']['rel_and_flex'] model = {} _rating_bin = [] _reliability_factor = [] _flexibility_factor = [] rating_bin_unit = data['units']['rating_bin']['unit'] reliability_factor_unit = data['units']['reliability_factor']['unit'] flexibility_factor_unit = data['units']['flexibility_factor']['unit'] output: pd.DataFrame = model_par['output'].copy() for i in rel_flex.index: node = rel_flex.at[i, 'node'] technology = rel_flex.at[i, 'technology'] mode = data['technology'][technology]['mode'] commodity = rel_flex.at[i, 'commodity'] level = rel_flex.at[i, 'level'] time = rel_flex.at[i, 'time'] rating = rel_flex.at[i, 'rating'] logger.debug( f'Create reliability flexibility parameters for {technology} in {node}' ) rating_bin_value = rel_flex.at[i, 'rating_bin'] reliability_factor_value = rel_flex.at[i, 'reliability_factor'] flexibility_factor_value = rel_flex.at[i, 'flexibility_factor'] _output_technology = output[output['technology'] == technology] _model_years = list(_output_technology['year_act'].unique()) _active_years = list(_output_technology['year_act']) _vintage_years = list(_output_technology['year_vtg']) base_par = pd.DataFrame({ 'node': node, 'technology': technology, 'year_act': _model_years, 'commodity': commodity, 'level': level, 'time': time, 'rating': rating }) _rating_bin.append( make_df(base_par, value=rating_bin_value, unit=rating_bin_unit)) _reliability_factor.append( make_df(base_par, value=reliability_factor_value, unit=reliability_factor_unit)) base_flex = pd.DataFrame({ 'node_loc': node, 'technology': technology, 'year_act': _active_years, 'year_vtg': _vintage_years, 'commodity': commodity, 'level': level, 'mode': mode, 'time': time, 'rating': rating }) _flexibility_factor.append( make_df(base_flex, value=flexibility_factor_value, unit=flexibility_factor_unit)) rating_bin = pd.concat(_rating_bin, sort=False, ignore_index=True) rating_bin['year_act'] = rating_bin['year_act'].astype(int) model['rating_bin'] = rating_bin reliability_factor = pd.concat(_reliability_factor, sort=False, ignore_index=True) reliability_factor['year_act'] = reliability_factor['year_act'].astype(int) model['reliability_factor'] = reliability_factor flexibility_factor = pd.concat(_flexibility_factor, sort=False, ignore_index=True) flexibility_factor['year_act'] = flexibility_factor['year_act'].astype(int) flexibility_factor['year_vtg'] = flexibility_factor['year_vtg'].astype(int) model['flexibility_factor'] = flexibility_factor return model