def run_scenarios(model, baseline, database, shale_costs, carbon_costs): # launch a platform to access the database mp = ixmp.Platform(dbprops=f'db/{database}', dbtype='HSQLDB') base = message_ix.Scenario(mp, model=model, scenario=baseline) if base.has_solution(): base.remove_solution() base.solve(model='MESSAGE-MACRO') # Define values for progress report num = len(list(product(shale_costs, carbon_costs))) i = 0 infeasible_models = [] # Run Scenario ################################## for s, c in product(shale_costs, carbon_costs): scenario = f'{s}USDpGJ-{c}USDtCO2' scen = base.clone(model, scenario, keep_solution=False) scen.set_as_default() scen.check_out() year = base.set('year').astype(int) first_model_year = scen.cat('year', 'firstmodelyear') model_years = list(year[year >= int(first_model_year)]) # update scenario carbon cost filters = dict(node='South Africa', type_emission='CO2', type_tec='all', type_year=model_years) par = scen.par('tax_emission', filters) par['value'] = [(c * ((1 + 0.05)**n)) for n in range(0, len(model_years) * 10, 10)] scen.add_par('tax_emission', par) # update scenario shale gas extraction costs filters = dict(mode='M1', node_loc='South Africa', time='year', year_act=model_years, year_vtg=model_years, technology='shale_extr') par = scen.par('var_cost', filters) par['value'] = s * 3.6 * 8.76 # from USD/GJ to USD/MWh to MUSD/GWa scen.add_par('var_cost', par) scen.commit( 'update variable costs and carbon price according to the scenario specifications' ) # Solve Model try: scen.solve(model='MESSAGE-MACRO') except: infeasible_models = infeasible_models + [scenario] print( f'Infeasible model. Shale gas extraction costs: {s}, carbon price {c}.' ) continue print(f'Done running {i + 1} out of {num} scenarios.') i = i + 1 mp.close_db() print(f'The following scenarios are infeasible: {infeasible_models}')
def fetch_scenarios(path, dbprops): mp = ixmp.Platform(dbprops=dbprops) for id, data in iter_scenarios(): scen = message_ix.Scenario(mp, data["model"], data["scenario"]) scen.to_excel((path / id).with_suffix(".xlsx"))
def results_to_xlsx(model, baseline, database, shale_costs, carbon_costs, l_year=2050): # Launch data base and load baseline mp = ixmp.Platform(dbprops=f'db/{database}', dbtype='HSQLDB') base = message_ix.Scenario(mp, model=model, scenario=baseline) year = base.set('year').astype(int) f_mod_year = base.cat('year', 'firstmodelyear') years = list(year[(year >= int(f_mod_year)) & (year <= l_year)]) columns = ['variable', 'tax', 'cost'] + years all_ts = pd.DataFrame(columns=columns) # retrieve data from scenarios ################################## for s, c in [('none', 0)] + list(product(shale_costs, carbon_costs)): if s == 'none': scen = base else: scenario = f'{s}USDpMWh-{c}USDtCO2' scen = message_ix.Scenario(mp, model=model, scenario=scenario) # EMISSION DATA data = scen.var('EMISS', { 'node': 'South Africa', 'year': years }).drop(['type_tec', 'mrg'], axis=1) ts = create_timeseries(data, 'Emissions|Total', s, c, 'node') all_ts = all_ts.append(ts, sort=True) # POWER SECTOR DATA act_dic = { 'Coal wo ccs': ['coal_adv', 'coal_ppl', 'igcc'], 'Coal ccs': ['coal_adv_ccs', 'igcc_ccs'], 'Gas wo ccs': ['gas_cc', 'gas_ct', 'gas_ppl'], 'Gas ccs': ['gas_cc_ccs'], 'Other ppls': ['foil_ppl', 'loil_ppl', 'elec_imp'], 'Renewable': [ 'wind_ppl', 'solar_th_ppl_base', 'solar_th_ppl', 'hydro_ppl', 'solar_pv_ppl' ], 'Nuclear': ['nuc_ppl'] } for k, t in act_dic.items(): data = scen.var('ACT', { 'node_loc': 'South Africa', 'year_act': years, 'technology': t }) data = data.rename(columns={'year_act': 'year'}) ts = create_timeseries(data, f'Activity|{k}', s, c, 'node_loc') all_ts = all_ts.append(ts, sort=True) data = scen.var('CAP', { 'node_loc': 'South Africa', 'year_vtg': years, 'technology': t }) data = data.rename(columns={'year_vtg': 'year'}) ts = create_timeseries(data, f'Capacity|{k}', s, c, 'node_loc') all_ts = all_ts.append(ts, sort=True) # ENERGY USE DATA act_dic = { 'shale_extr': 'shale_extr', 'coal_extr': 'coal_extr', 'all_extr': ['shale_extr', 'coal_extr', 'gas_extr', 'oil_extr'], 'all_imp': [ 'coal_imp', 'gas_imp', 'oil_imp', 'loil_imp', 'foil_imp', 'elec_imp' ], 'all_exp': [ 'coal_exp', 'gas_exp', 'oil_exp', 'loil_exp', 'foil_exp', 'elec_exp' ], 'renewable_energy': [ 'solar_th_ppl_base', 'solar_i', 'bio_extr', 'solar_rc', 'wind_ppl', 'solar_pv_ppl', 'hydro_ppl', 'solar_th_ppl' ] } for k, t in act_dic.items(): data = scen.var('ACT', { 'node_loc': 'South Africa', 'year_act': years, 'technology': t }) data = data.rename(columns={'year_act': 'year'}) ts = create_timeseries(data, f'Activity|{k}', s, c, 'node_loc') all_ts = all_ts.append(ts, sort=True) all_ts = all_ts[columns] if not os.path.exists('results/'): os.makedirs('results/') all_ts.to_excel('results/timeseries.xlsx')
def fetch_scenarios(path, dbprops): mp = ixmp.Platform(dbprops=dbprops) for id, data in iter_scenarios(): scen = message_ix.Scenario(mp, data['model'], data['scenario']) scen.to_excel((path / id).with_suffix('.xlsx'))
def results_to_iamc(model, scenario, region, database, l_year=2050): # Launch data base and load baseline mp = ixmp.Platform(dbprops=f'db/{database}', dbtype='HSQLDB') scen = message_ix.Scenario(mp, model=model, scenario=scenario) year = scen.set('year').astype(int) f_mod_year = scen.cat('year', 'firstmodelyear') years = list(year[(year >= int(f_mod_year)) & (year <= l_year)]) columns = ['model', 'scenario', 'region', 'variable', 'unit'] + years all_ts = pd.DataFrame(columns=columns) # retrieve data from scenarios ################################## # EMISSION DATA data = scen.var('EMISS', {'node': region, 'year': years} ).drop(['type_tec', 'mrg'], axis=1) ts = create_timeseries(data, 'Emissions|GHG', model, scenario, region, 'MtCO2eq', 'node') all_ts = all_ts.append(ts, sort=True) # POWER SECTOR DATA act_dic = {'Electricity|Coal|w/o CCS': ['coal_adv', 'coal_ppl', 'igcc'], 'Electricity|Coal|w/ CCS': ['coal_adv_ccs', 'igcc_ccs'], 'Electricity|Gas|w/o CCS': ['gas_cc', 'gas_ct', 'gas_ppl'], 'Electricity|Gas|w/ CCS': ['gas_cc_ccs'], 'Electricity|Oil|w/o CCS': ['foil_ppl', 'loil_ppl'], 'Electricity|Import': ['elec_imp'], 'Electricity|Wind': ['wind_ppl'], 'Electricity|Solar': ['solar_th_ppl_base', 'solar_th_ppl', 'solar_pv_ppl'], 'Electricity|Hydro': ['hydro_ppl'], 'Electricity|Nuclear': ['nuc_ppl']} for k, t in act_dic.items(): data = scen.var('ACT', {'node_loc': region, 'year_act': years, 'technology': t}) data = data.rename(columns={'year_act': 'year'}) ts = create_timeseries(data, f'Secondary Energy|{k}', model, scenario, region, 'GWa', 'node_loc') all_ts = all_ts.append(ts, sort=True) data = scen.var('CAP', {'node_loc': 'South Africa', 'year_vtg': years, 'technology': t}) data = data.rename(columns={'year_vtg': 'year'}) ts = create_timeseries(data, f'Capacity|{k}', model, scenario, region, 'GW', 'node_loc') all_ts = all_ts.append(ts, sort=True) # ENERGY USE DATA act_dic = {'shale_extr': 'shale_extr', 'coal_extr': 'coal_extr', 'all_extr': ['shale_extr', 'coal_extr', 'gas_extr', 'oil_extr'], 'all_imp': ['coal_imp', 'gas_imp', 'oil_imp', 'loil_imp', 'foil_imp', 'elec_imp'], 'all_exp': ['coal_exp', 'gas_exp', 'oil_exp', 'loil_exp', 'foil_exp', 'elec_exp'], 'renewable_energy': ['solar_th_ppl_base', 'solar_i', 'bio_extr', 'solar_rc', 'wind_ppl', 'solar_pv_ppl', 'hydro_ppl', 'solar_th_ppl']} for k, t in act_dic.items(): data = scen.var('ACT', {'node_loc': 'South Africa', 'year_act': years, 'technology': t}) data = data.rename(columns={'year_act': 'year'}) ts = create_timeseries(data, f'Activity|{k}', model, scenario, region, 'GWa', 'node_loc') all_ts = all_ts.append(ts, sort=True) all_ts = all_ts[columns] if not os.path.exists('results/'): os.makedirs('results/') all_ts.to_excel('results/timeseries_%s.xlsx' % scenario)
importlib.reload(LoadParams) #%% Base model load # launch the IX modeling platform using the local default database mp = ix.Platform( dbprops=r'H:\MyDocuments\MESSAGE\message_ix\config\default.org.properties') # new model name in ix platform modelName = "JM_GLB_NITRO" basescenarioName = "NoPolicy" newscenarioName = "NoPolicy_Trd" comment = "MESSAGE global test for new representation of nitrogen cycle with global trade" Sc_nitro = message_ix.Scenario(mp, modelName, basescenarioName) #%% Clone the model Sc_nitro_trd = Sc_nitro.clone(modelName, newscenarioName, comment) Sc_nitro_trd.remove_solution() Sc_nitro_trd.check_out() #%% Add tecs to set # Only fertilizer traded for now (NH3 trade data not yet available) comm_for_trd = ['Fertilizer Use|Nitrogen'] lvl_for_trd = ['material_final'] newtechnames_trd = ['NFert_trd'] newtechnames_imp = ['NFert_imp'] newtechnames_exp = ['NFert_exp']
""" Created on Tue May 24 13:28:00 2020 @author: """ import pandas as pd import ixmp import message_ix from message_ix.utils import make_df mp = ixmp.Platform() scenario = message_ix.Scenario(mp, model='Westeros Electrified', scenario='baseline', version='new') history = [690] model_horizon = [700, 710, 720] scenario.add_horizon({ 'year': history + model_horizon, 'firstmodelyear': model_horizon[0] }) country = 'Westeros' scenario.add_spatial_sets({'country': country}) scenario.add_set("commodity", ["electricity", "light"]) scenario.add_set("level", ["secondary", "final", "useful"]) scenario.add_set("technology", ['coal_ppl', 'wind_ppl', 'grid', 'bulb']) scenario.add_set("mode", "standard")
for fname in [ os.path.join(db_dir, 'ixmptest.lobs'), os.path.join(db_dir, 'ixmptest.properties'), os.path.join(db_dir, 'ixmptest.script') ]: if os.path.isfile(fname): os.remove(fname) # launch the modeling platform instance, creating a new test database file mp = ix.Platform(dbprops=test_db, dbtype='HSQLDB') model = "canning problem (MESSAGE scheme)" scenario = "standard" annot = "Dantzig's canning problem as a MESSAGE-scheme ixmp.Scenario" scen = message_ix.Scenario(mp, model, scenario, version='new', annotation=annot) input_file = os.path.join(db_dir, 'input.xlsx') scen.read_excel(input_file) comment = "importing a MESSAGE-scheme version of the transport problem" scen.commit(comment) scen.set_as_default() # duplicate the MESSAGE-scheme transport scenario for additional unit tests scen = message_ix.Scenario(mp, model, scenario) scen = scen.clone(scen='multi-year', annotation='adding additional years for unit-testing') scen.check_out() scen.add_set('year', [2020, 2030]) scen.add_par('technical_lifetime', ['seattle', 'canning_plant', '2020'], 30,
def dantzig_reporter(message_test_mp): scen = message_ix.Scenario(message_test_mp, **SCENARIO["dantzig"]) if not scen.has_solution(): scen.solve() yield message_ix.Reporter.from_scenario(scen)
def main(context, model_new, scen_new, create_new, years_new, firstyear_new, lastyear_new, macro, baseyear_macro, parameter, region, rewrite, unit_check, extrapol_neg, bound_extend, dry_run): # The reference scenario is loaded according to the options given to # the top-level message-ix (=ixmp) CLI: try: sc_ref = context.get('scen', None) # AttributeError if context is None if not isinstance(sc_ref, ixmp.Scenario): raise AttributeError except AttributeError: raise click.UsageError('add-years requires a base scenario; use' '--url or --platform, --model, --scenario, and ' 'optionally --version') start = timer() print('>> message_ix.tools.add_year...') # Handle default arguments if model_new is None: model_new = sc_ref.model if scen_new is None: # FIXME is this a good default? scen_new = sc_ref.scenario + '_5y' new_kw = dict(model=model_new, scenario=scen_new) if create_new: new_kw.update(dict( version='new', annotation='5 year modelling', )) # Output for debugging print(years_new) if dry_run: # Print arguments debugging and return print( 'sc_ref:', (sc_ref.model, sc_ref.scenario, sc_ref.version), 'sc_new:', new_kw, 'years_new:', years_new, 'firstyear_new:', firstyear_new, 'lastyear_new:', lastyear_new, 'macro:', macro, 'baseyear_macro:', baseyear_macro, 'parameter:', parameter, 'region:', region, 'rewrite:', rewrite, 'unit_check:', unit_check, 'extrapol_neg:', extrapol_neg, 'bound_extend:', bound_extend) return # Retrieve the Platform that sc_ref is stored on mp = sc_ref.platform # Load or create the new scenario to which to add years sc_new = message_ix.Scenario(mp, **new_kw) if not create_new: # Existing scenario: remove solution and check out if sc_new.has_solution(): sc_new.remove_solution() sc_new.check_out() # Calling the main function add_year( sc_ref=sc_ref, sc_new=sc_new, years_new=years_new, firstyear_new=firstyear_new, lastyear_new=lastyear_new, macro=macro, baseyear_macro=baseyear_macro, parameter=parameter, region=region, rewrite=rewrite, unit_check=unit_check, extrapol_neg=extrapol_neg, bound_extend=bound_extend) end = timer() mp.close_db() print('> Elapsed time for adding new years:', round((end - start) / 60), 'min and', round((end - start) % 60, 1), 'sec.') print('> New scenario with additional years is:\n' ' ixmp://{}/{}/{}#{}'.format(sc_new.platform.name, sc_new.model, sc_new.scenario, sc_new.version))