From abe88c2d18fb4800d8e2739866ea39bb3c3ca735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6rsch?= Date: Tue, 30 Jan 2018 23:12:36 +0100 Subject: [PATCH] Fix nans introduced through marginal_cost --- config.yaml | 4 +++- scripts/add_electricity.py | 1 + scripts/prepare_network.py | 48 +++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/config.yaml b/config.yaml index dcf0afd2..b5c28bdd 100644 --- a/config.yaml +++ b/config.yaml @@ -4,7 +4,7 @@ logging_level: INFO scenario: sectors: [E] # ,E+EV,E+BEV,E+BEV+V2G] # [ E+EV, E+BEV, E+BEV+V2G ] lv: [1., 1.125, 1.25, 1.5, 2.0, 3.0] - clusters: [37, 45, 64, 90, 128, 181, 256, 362] # np.r_[37, (2**np.arange(5.5, 9, 0.5)).astype(int)] + clusters: [45, 64, 90, 128, 181, 256, 362] # (2**np.r_[5.5:9:.5]).astype(int) opts: [Co2L] #, LC-FL, LC-T, Ep-T, Co2L-T] countries: ['AL', 'AT', 'BA', 'BE', 'BG', 'CH', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'ME', 'MK', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'SE', 'SI', 'SK'] @@ -107,6 +107,8 @@ costs: onwind: 0.015 offwind: 0.015 hydro: 0. + H2: 0. + battery: 0. emission_prices: # only used with the option Ep (emission prices) co2: 0. diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 66878a42..43a5a49b 100644 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -60,6 +60,7 @@ def load_costs(Nyears=1.): capital_cost += link2['capital_cost'] efficiency *= link2['efficiency']**0.5 return pd.Series(dict(capital_cost=capital_cost, + marginal_cost=0., efficiency=efficiency, co2_emissions=0.)) diff --git a/scripts/prepare_network.py b/scripts/prepare_network.py index c44922d8..b780a186 100644 --- a/scripts/prepare_network.py +++ b/scripts/prepare_network.py @@ -8,7 +8,9 @@ idx = pd.IndexSlice import numpy as np import scipy as sp import xarray as xr +import re +from six import iterkeys import geopandas as gpd import pypsa @@ -22,6 +24,8 @@ def add_co2limit(n, Nyears=1.): constant=snakemake.config['electricity']['co2limit'] * Nyears) def add_emission_prices(n, emission_prices=None, exclude_co2=False): + assert False, "Needs to be fixed, adds NAN" + if emission_prices is None: emission_prices = snakemake.config['costs']['emission_prices'] if exclude_co2: emission_prices.pop('co2') @@ -29,6 +33,16 @@ def add_emission_prices(n, emission_prices=None, exclude_co2=False): n.generators['marginal_cost'] += n.generators.carrier.map(ep) n.storage_units['marginal_cost'] += n.storage_units.carrier.map(ep) +def set_line_s_max_pu(n): + # set n-1 security margin to 0.5 for 45 clusters and to 0.7 from 200 clusters + n_clusters = len(n.buses) + s_max_pu = np.clip(0.5 + 0.2 * (n_clusters - 45) / (200 - 45), 0.5, 0.7) + n.lines['s_max_pu'] = s_max_pu + + dc_b = n.links.carrier == 'DC' + n.links.loc[dc_b, 'p_max_pu'] = s_max_pu + n.links.loc[dc_b, 'p_min_pu'] = - s_max_pu + def set_line_volume_limit(n, lv): # Either line_volume cap or cost n.lines['capital_cost'] = 0. @@ -52,6 +66,24 @@ def set_line_volume_limit(n, lv): return n +def average_every_nhours(n, offset): + logger.info('Resampling the network to {}'.format(offset)) + m = n.copy(with_time=False) + + snapshot_weightings = n.snapshot_weightings.resample(offset).sum() + m.set_snapshots(snapshot_weightings.index) + m.snapshot_weightings = snapshot_weightings + + for c in n.iterate_components(): + pnl = getattr(m, c.list_name+"_t") + for k in iterkeys(c.pnl): + df = c.pnl[k] + if not df.empty: + pnl[k] = df.resample(offset).mean() + + return m + + if __name__ == "__main__": # Detect running outside of snakemake and mock snakemake for testing if 'snakemake' not in globals(): @@ -71,12 +103,22 @@ if __name__ == "__main__": n = pypsa.Network(snakemake.input[0]) Nyears = n.snapshot_weightings.sum()/8760. + set_line_s_max_pu(n) + + for o in opts: + m = re.match(r'^\d+h$', o, re.IGNORECASE) + if m is not None: + n = average_every_nhours(n, m.group(0)) + break + else: + logger.info("No resampling") + if 'Co2L' in opts: add_co2limit(n, Nyears) - add_emission_prices(n, exclude_co2=True) + # add_emission_prices(n, exclude_co2=True) - if 'Ep' in opts: - add_emission_prices(n) + # if 'Ep' in opts: + # add_emission_prices(n) set_line_volume_limit(n, float(snakemake.wildcards.lv))