From e705166ca894f331b425231096a9cb8710548923 Mon Sep 17 00:00:00 2001 From: Magdalena Dorfner Date: Wed, 13 Mar 2019 14:46:24 +0100 Subject: [PATCH 1/2] Define e_pro_in/out per pro_input/output_tuples to reduce model variables and fix resulting bug in output.py * already defined pro_input_tuples/pro_output_tuples sets taken for variable definition * change consumed, imported, exported, stock, demand dataframes index to timesteps[1:] in case of empty frames to ensure that plot is working --- urbs/model.py | 4 ++-- urbs/output.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/urbs/model.py b/urbs/model.py index effa4248..9c6168a5 100644 --- a/urbs/model.py +++ b/urbs/model.py @@ -258,11 +258,11 @@ def create_model(data, dt=1, timesteps=None, objective='cost', within=pyomo.NonNegativeReals, doc='Power flow (MW) through process') m.e_pro_in = pyomo.Var( - m.tm, m.pro_tuples, m.com, + m.tm, m.pro_input_tuples, within=pyomo.NonNegativeReals, doc='Power flow of commodity into process (MW) per timestep') m.e_pro_out = pyomo.Var( - m.tm, m.pro_tuples, m.com, + m.tm, m.pro_output_tuples, within=pyomo.NonNegativeReals, doc='Power flow out of process (MW) per timestep') diff --git a/urbs/output.py b/urbs/output.py index a1472d2e..9e230fef 100644 --- a/urbs/output.py +++ b/urbs/output.py @@ -105,7 +105,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): level=1)[sites].sum( axis=1)) except KeyError: - demand = pd.Series(0, index=timesteps) + demand = pd.Series(0, index=timesteps[1:]) demand.name = 'Demand' # STOCK @@ -114,7 +114,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): eco = eco.xs([stf, com, 'Stock'], level=['stf', 'com', 'com_type']) stock = eco.unstack()[sites].sum(axis=1) except KeyError: - stock = pd.Series(0, index=timesteps) + stock = pd.Series(0, index=timesteps[1:]) stock.name = 'Stock' # PROCESS @@ -134,7 +134,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): consumed = consumed.unstack(level='pro') consumed = drop_all_zero_columns(consumed) except KeyError: - consumed = pd.DataFrame(index=timesteps) + consumed = pd.DataFrame(index=timesteps[1:]) # TRANSMISSION other_sites = (get_input(instance, 'site') @@ -171,17 +171,17 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): exported = exported[other_sites] # ...to other_sites exported = drop_all_zero_columns(exported) else: - imported = pd.DataFrame(index=timesteps) - exported = pd.DataFrame(index=timesteps) - internal_export = pd.Series(0, index=timesteps) - internal_import = pd.Series(0, index=timesteps) + imported = pd.DataFrame(index=timesteps[1:]) + exported = pd.DataFrame(index=timesteps[1:]) + internal_export = pd.Series(0, index=timesteps[1:]) + internal_import = pd.Series(0, index=timesteps[1:]) # to be discussed: increase demand by internal transmission losses internal_transmission_losses = internal_export - internal_import demand = demand + internal_transmission_losses except KeyError: # imported and exported are empty - imported = exported = pd.DataFrame(index=timesteps) + imported = exported = pd.DataFrame(index=timesteps[1:]) # STORAGE # group storage energies by commodity @@ -225,7 +225,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): # derive secondary timeseries delta = dsmup - dsmdo except KeyError: - delta = pd.Series(0, index=timesteps) + delta = pd.Series(0, index=timesteps[1:]) shifted = demand + delta From 7998cfa391c0880df55cae92fdd3947ddd467997 Mon Sep 17 00:00:00 2001 From: Magdalena Dorfner Date: Wed, 13 Mar 2019 16:53:39 +0100 Subject: [PATCH 2/2] Fixes to get_timeseries in case that the key cannot be found in e_pro_in/e_pro_out * consumed extraction now put into the already existing try/except * imported, exported, demand and delta backup Series are again with complete index --- urbs/output.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/urbs/output.py b/urbs/output.py index 9e230fef..6a2caaa8 100644 --- a/urbs/output.py +++ b/urbs/output.py @@ -105,7 +105,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): level=1)[sites].sum( axis=1)) except KeyError: - demand = pd.Series(0, index=timesteps[1:]) + demand = pd.Series(0, index=timesteps) demand.name = 'Demand' # STOCK @@ -114,7 +114,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): eco = eco.xs([stf, com, 'Stock'], level=['stf', 'com', 'com_type']) stock = eco.unstack()[sites].sum(axis=1) except KeyError: - stock = pd.Series(0, index=timesteps[1:]) + stock = pd.Series(0, index=timesteps) stock.name = 'Stock' # PROCESS @@ -128,8 +128,8 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): created = pd.DataFrame(index=timesteps) consumed = get_entity(instance, 'e_pro_in') - consumed = consumed.xs([stf, com], level=['stf', 'com']).loc[timesteps] try: + consumed = consumed.xs([stf, com], level=['stf', 'com']).loc[timesteps] consumed = consumed.unstack(level='sit')[sites].fillna(0).sum(axis=1) consumed = consumed.unstack(level='pro') consumed = drop_all_zero_columns(consumed) @@ -171,17 +171,17 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): exported = exported[other_sites] # ...to other_sites exported = drop_all_zero_columns(exported) else: - imported = pd.DataFrame(index=timesteps[1:]) - exported = pd.DataFrame(index=timesteps[1:]) - internal_export = pd.Series(0, index=timesteps[1:]) - internal_import = pd.Series(0, index=timesteps[1:]) + imported = pd.DataFrame(index=timesteps) + exported = pd.DataFrame(index=timesteps) + internal_export = pd.Series(0, index=timesteps) + internal_import = pd.Series(0, index=timesteps) # to be discussed: increase demand by internal transmission losses internal_transmission_losses = internal_export - internal_import demand = demand + internal_transmission_losses except KeyError: # imported and exported are empty - imported = exported = pd.DataFrame(index=timesteps[1:]) + imported = exported = pd.DataFrame(index=timesteps) # STORAGE # group storage energies by commodity @@ -225,7 +225,7 @@ def get_timeseries(instance, stf, com, sites, timesteps=None): # derive secondary timeseries delta = dsmup - dsmdo except KeyError: - delta = pd.Series(0, index=timesteps[1:]) + delta = pd.Series(0, index=timesteps) shifted = demand + delta