def load_network_for_plots(fn, tech_costs, config, combine_hydro_ps=True): import pypsa from add_electricity import update_transmission_costs, load_costs opts = config['plotting'] n = pypsa.Network(fn) n.loads["carrier"] = n.loads.bus.map(n.buses.carrier) + " load" n.stores["carrier"] = n.stores.bus.map(n.buses.carrier) n.links["carrier"] = (n.links.bus0.map(n.buses.carrier) + "-" + n.links.bus1.map(n.buses.carrier)) n.lines["carrier"] = "AC line" n.transformers["carrier"] = "AC transformer" n.lines['s_nom'] = n.lines['s_nom_min'] n.links['p_nom'] = n.links['p_nom_min'] if combine_hydro_ps: n.storage_units.loc[n.storage_units.carrier.isin({'PHS', 'hydro'}), 'carrier'] = 'hydro+PHS' # #if the carrier was not set on the heat storage units # bus_carrier = n.storage_units.bus.map(n.buses.carrier) # n.storage_units.loc[bus_carrier == "heat","carrier"] = "water tanks" Nyears = n.snapshot_weightings.sum() / 8760. costs = load_costs(Nyears, tech_costs, config['costs'], config['electricity']) update_transmission_costs(n, costs) return n
def set_transmission_limit(n, ll_type, factor, Nyears=1): links_dc_b = n.links.carrier == 'DC' if not n.links.empty else pd.Series() _lines_s_nom = (np.sqrt(3) * n.lines.type.map(n.line_types.i_nom) * n.lines.num_parallel * n.lines.bus0.map(n.buses.v_nom)) lines_s_nom = n.lines.s_nom.where(n.lines.type == '', _lines_s_nom) col = 'capital_cost' if ll_type == 'c' else 'length' ref = (lines_s_nom @ n.lines[col] + n.links[links_dc_b].p_nom @ n.links[links_dc_b][col]) costs = load_costs(Nyears, snakemake.input.tech_costs, snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs, simple_hvdc_costs=False) if factor == 'opt' or float(factor) > 1.0: n.lines['s_nom_min'] = lines_s_nom n.lines['s_nom_extendable'] = True n.links.loc[links_dc_b, 'p_nom_min'] = n.links.loc[links_dc_b, 'p_nom'] n.links.loc[links_dc_b, 'p_nom_extendable'] = True if factor != 'opt': con_type = 'expansion_cost' if ll_type == 'c' else 'volume_expansion' rhs = float(factor) * ref n.add('GlobalConstraint', f'l{ll_type}_limit', type=f'transmission_{con_type}_limit', sense='<=', constant=rhs, carrier_attribute='AC, DC') return n
def set_line_cost_limit(n, lc, Nyears=1.): links_dc_b = n.links.carrier == 'DC' if not n.links.empty else pd.Series() lines_s_nom = n.lines.s_nom.where( n.lines.type == '', np.sqrt(3) * n.lines.num_parallel * n.lines.type.map(n.line_types.i_nom) * n.lines.bus0.map(n.buses.v_nom) ) n.lines['capital_cost_lc'] = n.lines['capital_cost'] n.links['capital_cost_lc'] = n.links['capital_cost'] total_line_cost = ((lines_s_nom * n.lines['capital_cost_lc']).sum() + n.links.loc[links_dc_b].eval('p_nom * capital_cost_lc').sum()) if lc == 'opt': costs = load_costs(Nyears, snakemake.input.tech_costs, snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs, simple_hvdc_costs=False) else: # Either line_volume cap or cost n.lines['capital_cost'] = 0. n.links.loc[links_dc_b, 'capital_cost'] = 0. if lc == 'opt' or float(lc) > 1.0: n.lines['s_nom_min'] = lines_s_nom n.lines['s_nom_extendable'] = True n.links.loc[links_dc_b, 'p_nom_min'] = n.links.loc[links_dc_b, 'p_nom'] n.links.loc[links_dc_b, 'p_nom_extendable'] = True if lc != 'opt': n.line_cost_limit = float(lc) * total_line_cost return n
def make_summaries(networks_dict, country='all'): columns = pd.MultiIndex.from_tuples(networks_dict.keys(),names=["simpl","clusters","ll","opts"]) dfs = {} for output in outputs: dfs[output] = pd.DataFrame(columns=columns,dtype=float) for label, filename in iteritems(networks_dict): print(label, filename) if not os.path.exists(filename): print("does not exist!!") continue try: n = pypsa.Network(filename) except OSError: logger.warning("Skipping {filename}".format(filename=filename)) continue if country != 'all': n = n[n.buses.country == country] Nyears = n.snapshot_weightings.sum() / 8760. costs = load_costs(Nyears, snakemake.input[0], snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs, simple_hvdc_costs=False) assign_carriers(n) for output in outputs: dfs[output] = globals()["calculate_" + output](n, label, dfs[output]) return dfs
def set_line_volume_limit(n, lv, Nyears=1.): links_dc_b = n.links.carrier == 'DC' if np.isinf(lv): costs = load_costs(Nyears, snakemake.input.tech_costs, snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs, simple_hvdc_costs=True) else: # Either line_volume cap or cost n.lines['capital_cost'] = 0. n.links.loc[links_dc_b, 'capital_cost'] = 0. if lv > 1.0: lines_s_nom = n.lines.s_nom.where( n.lines.type == '', np.sqrt(3) * n.lines.num_parallel * n.lines.type.map(n.line_types.i_nom) * n.lines.bus0.map(n.buses.v_nom)) n.lines['s_nom_min'] = lines_s_nom n.lines['s_nom_extendable'] = True n.links.loc[links_dc_b, 'p_nom_min'] = n.links.loc[links_dc_b, 'p_nom'] n.links.loc[links_dc_b, 'p_nom_extendable'] = True n.line_volume_limit = lv * ( (lines_s_nom * n.lines['length']).sum() + n.links.loc[links_dc_b].eval('p_nom * length').sum()) return n
def load_network(fn, tech_costs, config, combine_hydro_ps=True): opts = config['plotting'] n = pypsa.Network(fn) n.loads["carrier"] = n.loads.bus.map(n.buses.carrier) + " load" n.stores["carrier"] = n.stores.bus.map(n.buses.carrier) n.links["carrier"] = (n.links.bus0.map(n.buses.carrier) + "-" + n.links.bus1.map(n.buses.carrier)) n.lines["carrier"] = "AC line" n.transformers["carrier"] = "AC transformer" n.lines['s_nom'] = n.lines['s_nom_min'] n.links['p_nom'] = n.links['p_nom_min'] if combine_hydro_ps: n.storage_units.loc[n.storage_units.carrier.isin({'PHS', 'hydro'}), 'carrier'] = 'hydro+PHS' # #if the carrier was not set on the heat storage units # bus_carrier = n.storage_units.bus.map(n.buses.carrier) # n.storage_units.loc[bus_carrier == "heat","carrier"] = "water tanks" for name in opts['heat_links'] + opts['heat_generators']: n.links.loc[n.links.index.to_series().str.endswith(name), "carrier"] = name Nyears = n.snapshot_weightings.sum() / 8760. costs = load_costs(Nyears, tech_costs, config['costs'], config['electricity']) update_transmission_costs(n, costs) return n
def load_network_for_plots(fn, tech_costs, config, combine_hydro_ps=True): import pypsa from add_electricity import load_costs, update_transmission_costs n = pypsa.Network(fn) n.loads["carrier"] = n.loads.bus.map(n.buses.carrier) + " load" n.stores["carrier"] = n.stores.bus.map(n.buses.carrier) n.links["carrier"] = ( n.links.bus0.map(n.buses.carrier) + "-" + n.links.bus1.map(n.buses.carrier) ) n.lines["carrier"] = "AC line" n.transformers["carrier"] = "AC transformer" n.lines["s_nom"] = n.lines["s_nom_min"] n.links["p_nom"] = n.links["p_nom_min"] if combine_hydro_ps: n.storage_units.loc[ n.storage_units.carrier.isin({"PHS", "hydro"}), "carrier" ] = "hydro+PHS" # if the carrier was not set on the heat storage units # bus_carrier = n.storage_units.bus.map(n.buses.carrier) # n.storage_units.loc[bus_carrier == "heat","carrier"] = "water tanks" Nyears = n.snapshot_weightings.objective.sum() / 8760.0 costs = load_costs(Nyears, tech_costs, config["costs"], config["electricity"]) update_transmission_costs(n, costs) return n
def make_summaries(networks_dict): columns = pd.MultiIndex.from_tuples( networks_dict.keys(), names=["simpl", "clusters", "lv", "opts"]) dfs = {} for output in outputs: dfs[output] = pd.DataFrame(columns=columns, dtype=float) for label, filename in iteritems(networks_dict): print(label, filename) if not os.path.exists(filename): print("does not exist!!") continue n = pypsa.Network(filename) assign_carriers(n) Nyears = n.snapshot_weightings.sum() / 8760. costs = load_costs(Nyears, snakemake.input[0], snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs) for output in outputs: dfs[output] = globals()["calculate_" + output](n, label, dfs[output]) return dfs
def set_transmission_limit(n, ll_type, factor, Nyears=1): links_dc_b = n.links.carrier == "DC" if not n.links.empty else pd.Series() _lines_s_nom = (np.sqrt(3) * n.lines.type.map(n.line_types.i_nom) * n.lines.num_parallel * n.lines.bus0.map(n.buses.v_nom)) lines_s_nom = n.lines.s_nom.where(n.lines.type == "", _lines_s_nom) col = "capital_cost" if ll_type == "c" else "length" ref = (lines_s_nom @ n.lines[col] + n.links.loc[links_dc_b, "p_nom"] @ n.links.loc[links_dc_b, col]) costs = load_costs( Nyears, snakemake.input.tech_costs, snakemake.config["costs"], snakemake.config["electricity"], ) update_transmission_costs(n, costs, simple_hvdc_costs=False) if factor == "opt" or float(factor) > 1.0: n.lines["s_nom_min"] = lines_s_nom n.lines["s_nom_extendable"] = True n.links.loc[links_dc_b, "p_nom_min"] = n.links.loc[links_dc_b, "p_nom"] n.links.loc[links_dc_b, "p_nom_extendable"] = True if factor != "opt": con_type = "expansion_cost" if ll_type == "c" else "volume_expansion" rhs = float(factor) * ref n.add( "GlobalConstraint", f"l{ll_type}_limit", type=f"transmission_{con_type}_limit", sense="<=", constant=rhs, carrier_attribute="AC, DC", ) return n
def set_line_volume_limit(n, lv, Nyears=1.): links_dc_b = n.links.carrier == 'DC' if not n.links.empty else pd.Series() lines_s_nom = n.lines.s_nom.where( n.lines.type == '', np.sqrt(3) * n.lines.num_parallel * n.lines.type.map(n.line_types.i_nom) * n.lines.bus0.map(n.buses.v_nom)) total_line_volume = ((lines_s_nom * n.lines['length']).sum() + n.links.loc[links_dc_b].eval('p_nom * length').sum()) if lv == 'opt': costs = load_costs(Nyears, snakemake.input.tech_costs, snakemake.config['costs'], snakemake.config['electricity']) update_transmission_costs(n, costs, simple_hvdc_costs=True) else: # Either line_volume cap or cost n.lines['capital_cost'] = 0. n.links.loc[links_dc_b, 'capital_cost'] = 0. if lv == 'opt' or float(lv) > 1.0: n.lines['s_nom_min'] = lines_s_nom n.lines['s_nom_extendable'] = True n.links.loc[links_dc_b, 'p_nom_min'] = n.links.loc[links_dc_b, 'p_nom'] n.links.loc[links_dc_b, 'p_nom_extendable'] = True if lv != 'opt': line_volume = float(lv) * total_line_volume n.add('GlobalConstraint', 'lv_limit', type='transmission_volume_expansion_limit', sense='<=', constant=line_volume, carrier_attribute='AC, DC') return n