Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simplify_link bug with links #880

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ rule simplify_network:
build_shape_options=config["build_shape_options"],
electricity=config["electricity"],
costs=config["costs"],
lines_types=config["lines"]["types"],
lines_length_factor=config["lines"]["length_factor"],
config_lines=config["lines"],
config_links=config["links"],
focus_weights=config.get("focus_weights", None),
input:
network="networks/" + RDIR + "elec.nc",
Expand Down
2 changes: 1 addition & 1 deletion doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ E.g. if a new rule becomes available describe how to use it `snakemake -j1 run_t

**New Features and major Changes**

* Add params: section in rule definition to keep track of changed settings in config.yaml. `PR #823 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/823>`__
* Add params: section in rule definition to keep track of changed settings in config.yaml. `PR #823 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/823>`__ and `PR #880 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/880>`__

* Fix Natural Gas implementation in "add_electricity" to avoid "Natural Gas" to be filtered out `PR #797 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/797>`__

Expand Down
45 changes: 28 additions & 17 deletions scripts/simplify_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ def simplify_network_to_base_voltage(n, linetype, base_voltage):
return n, trafo_map


def _prepare_connection_costs_per_link(n, costs, renewable_config, lines_length_factor):
def _prepare_connection_costs_per_link(
n, costs, renewable_config, hvdc_as_lines, lines_length_factor
):
if n.links.empty:
return {}

connection_costs_per_link = {}

# initialize dc_lengths and underwater_fractions by the hvdc_as_lines option
if config["electricity"]["hvdc_as_lines"]:
if hvdc_as_lines:
dc_lengths = n.lines.length
unterwater_fractions = n.lines.underwater_fraction
else:
Expand Down Expand Up @@ -190,13 +192,14 @@ def _compute_connection_costs_to_bus(
busmap,
costs,
renewable_config,
hvdc_as_lines,
lines_length_factor,
connection_costs_per_link=None,
buses=None,
):
if connection_costs_per_link is None:
connection_costs_per_link = _prepare_connection_costs_per_link(
n, costs, renewable_config, lines_length_factor
n, costs, renewable_config, hvdc_as_lines, lines_length_factor
)

if buses is None:
Expand Down Expand Up @@ -304,8 +307,11 @@ def simplify_links(
n,
costs,
renewable_config,
lines_length_factor,
hvdc_as_lines,
config_lines,
config_links,
output,
exclude_carriers=[],
aggregation_strategies=dict(),
):
## Complex multi-node links are folded into end-points
Expand Down Expand Up @@ -378,7 +384,7 @@ def split_links(nodes):
busmap = n.buses.index.to_series()

connection_costs_per_link = _prepare_connection_costs_per_link(
n, costs, renewable_config, lines_length_factor
n, costs, renewable_config, hvdc_as_lines, config_lines["length_factor"]
)
connection_costs_to_bus = pd.DataFrame(
0.0, index=n.buses.index, columns=list(connection_costs_per_link)
Expand All @@ -401,7 +407,8 @@ def split_links(nodes):
busmap,
costs,
renewable_config,
lines_length_factor,
hvdc_as_lines,
config_lines,
connection_costs_per_link,
buses,
)
Expand All @@ -412,15 +419,14 @@ def split_links(nodes):
all_links = list(set(n.links.index).intersection(all_dc_branches))
all_dc_lines = list(set(n.lines.index).intersection(all_dc_branches))

# p_max_pu = config["links"].get("p_max_pu", 1.0)
all_dc_lengths = pd.concat(
[n.links.loc[all_links, "length"], n.lines.loc[all_dc_lines, "length"]]
)
name = all_dc_lengths.idxmax() + "+{}".format(len(all_dc_branches) - 1)

# HVDC part is represented as "Link" component
if dc_as_links:
p_max_pu = config["links"].get("p_max_pu", 1.0)
p_max_pu = config_links.get("p_max_pu", 1.0)
lengths = n.links.loc[all_links, "length"]
i_links = [i for _, i in sum(dc_edges, []) if _ == "Link"]
length = sum(n.links.loc[i_links, "length"].mean() for l in dc_edges)
Expand All @@ -430,7 +436,7 @@ def split_links(nodes):
).sum() / lengths.sum()
# HVDC part is represented as "Line" component
else:
p_max_pu = config["lines"].get("p_max_pu", 1.0)
p_max_pu = config_lines.get("p_max_pu", 1.0)
lengths = n.lines.loc[all_dc_lines, "length"]
length = lengths.sum() / len(lengths) if len(lengths) > 0 else 0
p_nom = n.lines.loc[all_dc_lines, "s_nom"].min()
Expand Down Expand Up @@ -470,10 +476,6 @@ def split_links(nodes):

logger.debug("Collecting all components using the busmap")

exclude_carriers = config["cluster_options"]["simplify_network"].get(
"exclude_carriers", []
)

_aggregate_and_move_components(
n,
busmap,
Expand All @@ -490,6 +492,7 @@ def remove_stubs(
costs,
cluster_config,
renewable_config,
hvdc_as_lines,
lines_length_factor,
output,
aggregation_strategies=dict(),
Expand All @@ -506,6 +509,7 @@ def remove_stubs(
busmap,
costs,
renewable_config,
hvdc_as_lines,
lines_length_factor,
)

Expand Down Expand Up @@ -829,8 +833,11 @@ def merge_isolated_nodes(n, threshold, aggregation_strategies=dict()):
n = pypsa.Network(snakemake.input.network)

base_voltage = snakemake.params.electricity["base_voltage"]
linetype = snakemake.params.lines_types[base_voltage]

linetype = snakemake.params.config_lines["types"][base_voltage]
exclude_carriers = snakemake.params.cluster_options["simplify_network"].get(
"exclude_carriers", []
)
hvdc_as_lines = snakemake.params.electricity["hvdc_as_lines"]
aggregation_strategies = snakemake.params.cluster_options.get(
"aggregation_strategies", {}
)
Expand All @@ -854,22 +861,26 @@ def merge_isolated_nodes(n, threshold, aggregation_strategies=dict()):
n,
technology_costs,
snakemake.params.renewable,
snakemake.params.lines_length_factor,
hvdc_as_lines,
snakemake.params.config_lines,
snakemake.params.config_links,
snakemake.output,
exclude_carriers,
aggregation_strategies,
)

busmaps = [trafo_map, simplify_links_map]

cluster_config = snakemake.params.cluster_options["simplify_network"]
renewable_config = snakemake.params.renewable
lines_length_factor = snakemake.params.lines_length_factor
lines_length_factor = snakemake.params.config_lines["length_factor"]
if cluster_config.get("remove_stubs", True):
n, stub_map = remove_stubs(
n,
technology_costs,
cluster_config,
renewable_config,
hvdc_as_lines,
lines_length_factor,
snakemake.output,
aggregation_strategies=aggregation_strategies,
Expand Down