From 6b2daec7f4df215598aa535c392fdefe03153350 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 11:46:04 +0200 Subject: [PATCH 01/32] Move plot and calc_results into submodule analyze --- doc/api.rst | 1 + doc/api/etrago.analyze.rst | 20 ++++++++++++++++++++ etrago/{tools => analyze}/calc_results.py | 0 etrago/{tools => analyze}/plot.py | 0 4 files changed, 21 insertions(+) create mode 100644 doc/api/etrago.analyze.rst rename etrago/{tools => analyze}/calc_results.py (100%) rename etrago/{tools => analyze}/plot.py (100%) diff --git a/doc/api.rst b/doc/api.rst index d6533797..8c52c459 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -6,6 +6,7 @@ API :glob: :titlesonly: + api/etrago.analyze api/etrago.cluster api/etrago.tools api/appl.rst diff --git a/doc/api/etrago.analyze.rst b/doc/api/etrago.analyze.rst new file mode 100644 index 00000000..d7a9f923 --- /dev/null +++ b/doc/api/etrago.analyze.rst @@ -0,0 +1,20 @@ +etrago.analyze module +===================== + +etrago.analyze.calc\_results module +----------------------------------- + +.. automodule:: etrago.analyze.calc_results + :members: + :undoc-members: + :show-inheritance: + +etrago.analyze.plot module +--------------------------- + +.. automodule:: etrago.analyze.plot + :members: + :undoc-members: + :show-inheritance: + + diff --git a/etrago/tools/calc_results.py b/etrago/analyze/calc_results.py similarity index 100% rename from etrago/tools/calc_results.py rename to etrago/analyze/calc_results.py diff --git a/etrago/tools/plot.py b/etrago/analyze/plot.py similarity index 100% rename from etrago/tools/plot.py rename to etrago/analyze/plot.py From cafe20dd891556b6e3df06334eba22a5515f5937 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 12:02:21 +0200 Subject: [PATCH 02/32] Move spatial disaggregation to new submodule disaggregate --- etrago/disaggregate/__init__.py | 7 +++++++ .../{cluster/disaggregation.py => disaggregate/spatial.py} | 0 etrago/tools/network.py | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 etrago/disaggregate/__init__.py rename etrago/{cluster/disaggregation.py => disaggregate/spatial.py} (100%) diff --git a/etrago/disaggregate/__init__.py b/etrago/disaggregate/__init__.py new file mode 100644 index 00000000..5dd475b7 --- /dev/null +++ b/etrago/disaggregate/__init__.py @@ -0,0 +1,7 @@ +""" + +""" + +__copyright__ = "tba" +__license__ = "tba" +__author__ = "tba" diff --git a/etrago/cluster/disaggregation.py b/etrago/disaggregate/spatial.py similarity index 100% rename from etrago/cluster/disaggregation.py rename to etrago/disaggregate/spatial.py diff --git a/etrago/tools/network.py b/etrago/tools/network.py index 8b38f5eb..44f749e2 100644 --- a/etrago/tools/network.py +++ b/etrago/tools/network.py @@ -33,7 +33,7 @@ from etrago.tools import db from etrago import __version__ -from etrago.cluster.disaggregation import run_disaggregation +from etrago.disaggregate.spatial import run_disaggregation from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering from etrago.cluster.gas import run_spatial_clustering_gas from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering From 1f4bd61e87922a3ed2959be69304ae5784000ae5 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 12:10:17 +0200 Subject: [PATCH 03/32] Move temporal disaggregation into module disaggregate --- etrago/disaggregate/temporal.py | 164 ++++++++++++++++++++++++++++++++ etrago/execute/__init__.py | 116 ---------------------- etrago/tools/network.py | 2 +- 3 files changed, 165 insertions(+), 117 deletions(-) create mode 100644 etrago/disaggregate/temporal.py diff --git a/etrago/disaggregate/temporal.py b/etrago/disaggregate/temporal.py new file mode 100644 index 00000000..4b9b2aaa --- /dev/null +++ b/etrago/disaggregate/temporal.py @@ -0,0 +1,164 @@ +# -*- coding: utf-8 -*- +# Copyright 2016-2023 Flensburg University of Applied Sciences, +# Europa-Universität Flensburg, +# Centre for Sustainable Energy Systems, +# DLR-Institute for Networked Energy Systems +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# File description +""" +execute.py defines optimization and simulation methods for the etrago object. +""" +import os +import logging +import time + +import pandas as pd + +logger = logging.getLogger(__name__) + +if "READTHEDOCS" not in os.environ: + + from etrago.execute import iterate_lopf + from etrago.tools.constraints import Constraints + + +__copyright__ = ( + "Flensburg University of Applied Sciences, " + "Europa-Universität Flensburg, " + "Centre for Sustainable Energy Systems, " + "DLR-Institute for Networked Energy Systems" +) +__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" +__author__ = ( + "ulfmueller, s3pp, wolfbunke, mariusves, lukasol, KathiEsterl, " + "ClaraBuettner, CarlosEpia, AmeliaNadal" +) + + +def dispatch_disaggregation(self): + """ + Function running the tempral disaggregation meaning the optimization + of dispatch in the temporally fully resolved network; therfore, the problem + is reduced to smaller subproblems by slicing the whole considered time span + while keeping inforation on the state of charge of storage units and stores + to ensure compatibility and to reproduce saisonality. + + Returns + ------- + None. + + """ + + if self.args["temporal_disaggregation"]["active"]: + x = time.time() + + if self.args["temporal_disaggregation"]["no_slices"]: + # split dispatch_disaggregation into subproblems + # keep some information on soc in beginning and end of slices + # to ensure compatibility and to reproduce saisonality + + # define number of slices and corresponding slice length + no_slices = self.args["temporal_disaggregation"]["no_slices"] + slice_len = int(len(self.network.snapshots) / no_slices) + + # transition snapshots defining start and end of slices + transits = self.network.snapshots[0::slice_len] + if len(transits) > 1: + transits = transits[1:] + if transits[-1] != self.network.snapshots[-1]: + transits = transits.insert( + (len(transits)), self.network.snapshots[-1] + ) + # for stores, exclude emob and dsm because of their special + # constraints + sto = self.network.stores[ + ~self.network.stores.carrier.isin( + ["battery_storage", "battery storage", "dsm"] + ) + ] + + # save state of charge of storage units and stores at those + # transition snapshots + self.conduct_dispatch_disaggregation = pd.DataFrame( + columns=self.network.storage_units.index.append(sto.index), + index=transits, + ) + for storage in self.network.storage_units.index: + self.conduct_dispatch_disaggregation[storage] = ( + self.network.storage_units_t.state_of_charge[storage] + ) + for store in sto.index: + self.conduct_dispatch_disaggregation[store] = ( + self.network.stores_t.e[store] + ) + + extra_func = self.args["extra_functionality"] + self.args["extra_functionality"] = {} + + load_shedding = self.args["load_shedding"] + if not load_shedding: + self.args["load_shedding"] = True + self.load_shedding(temporal_disaggregation=True) + + iterate_lopf( + self, + Constraints( + self.args, self.conduct_dispatch_disaggregation + ).functionality, + method=self.args["method"], + ) + + # switch to temporally fully resolved network as standard network, + # temporally reduced network is stored in network_tsa + network1 = self.network.copy() + self.network = self.network_tsa.copy() + self.network_tsa = network1.copy() + network1 = 0 + + # keep original settings + + if self.args["temporal_disaggregation"]["no_slices"]: + self.args["extra_functionality"] = extra_func + self.args["load_shedding"] = load_shedding + + self.network.lines["s_nom_extendable"] = self.network_tsa.lines[ + "s_nom_extendable" + ] + self.network.links["p_nom_extendable"] = self.network_tsa.links[ + "p_nom_extendable" + ] + self.network.transformers.s_nom_extendable = ( + self.network_tsa.transformers.s_nom_extendable + ) + self.network.storage_units["p_nom_extendable"] = ( + self.network_tsa.storage_units["p_nom_extendable"] + ) + self.network.stores["e_nom_extendable"] = self.network_tsa.stores[ + "e_nom_extendable" + ] + self.network.storage_units.cyclic_state_of_charge = ( + self.network_tsa.storage_units.cyclic_state_of_charge + ) + self.network.stores.e_cyclic = self.network_tsa.stores.e_cyclic + + if not self.args["csv_export"]: + path = self.args["csv_export"] + self.export_to_csv(path) + self.export_to_csv(path + "/temporal_disaggregaton") + + y = time.time() + z = (y - x) / 60 + logger.info("Time for LOPF [min]: {}".format(round(z, 2))) diff --git a/etrago/execute/__init__.py b/etrago/execute/__init__.py index ae415833..01673832 100644 --- a/etrago/execute/__init__.py +++ b/etrago/execute/__init__.py @@ -447,122 +447,6 @@ def optimize(self): print("Method not defined") -def dispatch_disaggregation(self): - """ - Function running the tempral disaggregation meaning the optimization - of dispatch in the temporally fully resolved network; therfore, the problem - is reduced to smaller subproblems by slicing the whole considered time span - while keeping inforation on the state of charge of storage units and stores - to ensure compatibility and to reproduce saisonality. - - Returns - ------- - None. - - """ - - if self.args["temporal_disaggregation"]["active"]: - x = time.time() - - if self.args["temporal_disaggregation"]["no_slices"]: - # split dispatch_disaggregation into subproblems - # keep some information on soc in beginning and end of slices - # to ensure compatibility and to reproduce saisonality - - # define number of slices and corresponding slice length - no_slices = self.args["temporal_disaggregation"]["no_slices"] - slice_len = int(len(self.network.snapshots) / no_slices) - - # transition snapshots defining start and end of slices - transits = self.network.snapshots[0::slice_len] - if len(transits) > 1: - transits = transits[1:] - if transits[-1] != self.network.snapshots[-1]: - transits = transits.insert( - (len(transits)), self.network.snapshots[-1] - ) - # for stores, exclude emob and dsm because of their special - # constraints - sto = self.network.stores[ - ~self.network.stores.carrier.isin( - ["battery_storage", "battery storage", "dsm"] - ) - ] - - # save state of charge of storage units and stores at those - # transition snapshots - self.conduct_dispatch_disaggregation = pd.DataFrame( - columns=self.network.storage_units.index.append(sto.index), - index=transits, - ) - for storage in self.network.storage_units.index: - self.conduct_dispatch_disaggregation[storage] = ( - self.network.storage_units_t.state_of_charge[storage] - ) - for store in sto.index: - self.conduct_dispatch_disaggregation[store] = ( - self.network.stores_t.e[store] - ) - - extra_func = self.args["extra_functionality"] - self.args["extra_functionality"] = {} - - load_shedding = self.args["load_shedding"] - if not load_shedding: - self.args["load_shedding"] = True - self.load_shedding(temporal_disaggregation=True) - - iterate_lopf( - self, - Constraints( - self.args, self.conduct_dispatch_disaggregation - ).functionality, - method=self.args["method"], - ) - - # switch to temporally fully resolved network as standard network, - # temporally reduced network is stored in network_tsa - network1 = self.network.copy() - self.network = self.network_tsa.copy() - self.network_tsa = network1.copy() - network1 = 0 - - # keep original settings - - if self.args["temporal_disaggregation"]["no_slices"]: - self.args["extra_functionality"] = extra_func - self.args["load_shedding"] = load_shedding - - self.network.lines["s_nom_extendable"] = self.network_tsa.lines[ - "s_nom_extendable" - ] - self.network.links["p_nom_extendable"] = self.network_tsa.links[ - "p_nom_extendable" - ] - self.network.transformers.s_nom_extendable = ( - self.network_tsa.transformers.s_nom_extendable - ) - self.network.storage_units["p_nom_extendable"] = ( - self.network_tsa.storage_units["p_nom_extendable"] - ) - self.network.stores["e_nom_extendable"] = self.network_tsa.stores[ - "e_nom_extendable" - ] - self.network.storage_units.cyclic_state_of_charge = ( - self.network_tsa.storage_units.cyclic_state_of_charge - ) - self.network.stores.e_cyclic = self.network_tsa.stores.e_cyclic - - if not self.args["csv_export"]: - path = self.args["csv_export"] - self.export_to_csv(path) - self.export_to_csv(path + "/temporal_disaggregaton") - - y = time.time() - z = (y - x) / 60 - logger.info("Time for LOPF [min]: {}".format(round(z, 2))) - - def import_gen_from_links(network, drop_small_capacities=True): """ create gas generators from links in order to not lose them when diff --git a/etrago/tools/network.py b/etrago/tools/network.py index 44f749e2..5b12c145 100644 --- a/etrago/tools/network.py +++ b/etrago/tools/network.py @@ -34,11 +34,11 @@ from etrago import __version__ from etrago.disaggregate.spatial import run_disaggregation +from etrago.disaggregate.temporal import dispatch_disaggregation from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering from etrago.cluster.gas import run_spatial_clustering_gas from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering from etrago.execute import ( - dispatch_disaggregation, lopf, optimize, run_pf_post_lopf, From 823b56da757e4fe3cfc89dcd9f128e88005b2d05 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 12:11:30 +0200 Subject: [PATCH 04/32] Update rtd api files for module disaggregate --- doc/api/etrago.cluster.rst | 7 ------- doc/api/etrago.disaggregate.rst | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 doc/api/etrago.disaggregate.rst diff --git a/doc/api/etrago.cluster.rst b/doc/api/etrago.cluster.rst index 40fc8db3..e7432dbb 100644 --- a/doc/api/etrago.cluster.rst +++ b/doc/api/etrago.cluster.rst @@ -1,13 +1,6 @@ etrago.cluster package ======================= -etrago.cluster.disaggregation module ------------------------------------------ - -.. automodule:: etrago.cluster.disaggregation - :members: - :undoc-members: - :show-inheritance: etrago.cluster.electrical module ----------------------------------------- diff --git a/doc/api/etrago.disaggregate.rst b/doc/api/etrago.disaggregate.rst new file mode 100644 index 00000000..9929a487 --- /dev/null +++ b/doc/api/etrago.disaggregate.rst @@ -0,0 +1,19 @@ +etrago.disaggregate package +=========================== + +etrago.disaggregate.spatial module +---------------------------------- + +.. automodule:: etrago.disaggregate.spatial + :members: + :undoc-members: + :show-inheritance: + +etrago.disaggregate.temporal module +----------------------------------- + +.. automodule:: etrago.disaggregate.temporal + :members: + :undoc-members: + :show-inheritance: + From 66e3a13af698840efa40d226a88c339ce25bdc8f Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:12:47 +0200 Subject: [PATCH 05/32] Move network file into own module --- doc/api/network.rst | 7 +++++++ etrago/__init__.py | 2 +- etrago/{tools => }/network.py | 0 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 doc/api/network.rst rename etrago/{tools => }/network.py (100%) diff --git a/doc/api/network.rst b/doc/api/network.rst new file mode 100644 index 00000000..3189e5a4 --- /dev/null +++ b/doc/api/network.rst @@ -0,0 +1,7 @@ +etrago.network module +------------------- + +.. automodule:: etrago.network + :members: + :undoc-members: + :show-inheritance: diff --git a/etrago/__init__.py b/etrago/__init__.py index be816a58..c207f7d9 100644 --- a/etrago/__init__.py +++ b/etrago/__init__.py @@ -12,4 +12,4 @@ __author__ = "ulfmueller, lukasol, wolfbunke, mariusves, s3pp" __version__ = "0.4" -from etrago.tools.network import Etrago +from etrago.network import Etrago diff --git a/etrago/tools/network.py b/etrago/network.py similarity index 100% rename from etrago/tools/network.py rename to etrago/network.py From f524e14c20bcbf719af07165b223ea7c613881f6 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:19:00 +0200 Subject: [PATCH 06/32] Import functions from new modules --- etrago/network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etrago/network.py b/etrago/network.py index 5b12c145..956dfeb3 100644 --- a/etrago/network.py +++ b/etrago/network.py @@ -55,7 +55,7 @@ iterate_sclopf, post_contingency_analysis_lopf, ) -from etrago.tools.calc_results import ( +from etrago.analyze.calc_results import ( ac_export, ac_export_per_country, calc_etrago_results, @@ -71,7 +71,7 @@ decommissioning, extension, ) -from etrago.tools.plot import ( +from etrago.analyze.plot import ( bev_flexibility_potential, demand_side_management, flexibility_usage, From f4b82bb36e5fca5ca100391fe58e7ab63d22bcc2 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:21:48 +0200 Subject: [PATCH 07/32] Add new files to nox tests --- noxfile.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/noxfile.py b/noxfile.py index 998766b9..94fc8c3b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -4,18 +4,22 @@ import nox cleaned = [ - "etrago/cluster/disaggregation.py", + "etrago/analyze/__init__.py", + "etrago/analyze/calc_results.py", + "etrago/analyze/plot.py", "etrago/cluster/electrical.py", "etrago/cluster/gas.py", "etrago/cluster/snapshot.py", "etrago/cluster/spatial.py", + "etrago/disaggregate/spatial.py", + "etrago/disaggregate/temporal.py", "etrago/execute/__init__.py", "etrago/execute/grid_optimization.py", "etrago/execute/market_optimization.py", + "etrago/network.py", "etrago/tools/calc_results.py", "etrago/tools/extendable.py", "etrago/tools/io.py", - "etrago/tools/network.py", "etrago/tools/utilities.py", "noxfile.py", "setup.py", From 96c7672a9caf81fd2d55dc6c9e826e4c5ec7c4fe Mon Sep 17 00:00:00 2001 From: birgits Date: Mon, 29 Jan 2024 11:06:02 -0800 Subject: [PATCH 08/32] Don't import shapely when built on readthedocs --- etrago/analyze/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 6e524a3e..d604a119 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -25,7 +25,6 @@ import logging import os -from etrago.execute import import_gen_from_links from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse @@ -45,6 +44,7 @@ logger = logging.getLogger(__name__) if "READTHEDOCS" not in os.environ: + from etrago.execute import import_gen_from_links from geoalchemy2.shape import to_shape from pyproj import Proj, transform from shapely.geometry import LineString, MultiPoint, Point, Polygon From f52d320be9f1a34889130287407407f4c65109ba Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:34:42 +0200 Subject: [PATCH 09/32] Delete unused imports --- etrago/analyze/plot.py | 7 +++---- etrago/tools/db.py | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index d604a119..a0d09057 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -21,14 +21,14 @@ """ Plot.py defines functions necessary to plot results of eTraGo. """ -from math import log10, sqrt +from math import sqrt import logging import os from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse -from pyproj import Proj, transform + import matplotlib import matplotlib.patches as mpatches import numpy as np @@ -45,9 +45,8 @@ if "READTHEDOCS" not in os.environ: from etrago.execute import import_gen_from_links - from geoalchemy2.shape import to_shape from pyproj import Proj, transform - from shapely.geometry import LineString, MultiPoint, Point, Polygon + from shapely.geometry import LineString, Point import geopandas as gpd import tilemapbase diff --git a/etrago/tools/db.py b/etrago/tools/db.py index 3f9ca2a5..a0d19ccf 100644 --- a/etrago/tools/db.py +++ b/etrago/tools/db.py @@ -23,7 +23,6 @@ from sqlalchemy import create_engine import keyring -import oedialect def readcfg(filepath, section): From bd064a66113b1d2f711493288265b9497f24f28c Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:36:22 +0200 Subject: [PATCH 10/32] Update requirements-doc --- requirements-doc.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/requirements-doc.txt b/requirements-doc.txt index 864c6327..afabde02 100644 --- a/requirements-doc.txt +++ b/requirements-doc.txt @@ -1,12 +1,16 @@ # Packages for read the docs # Using single requirments for docs, see: # https://github.com/rtfd/readthedocs.org/issues/2070 -sphinx_rtd_theme > 1.2.2 -pypsa == 0.20.1 -numpydoc -sqlalchemy geoalchemy2 +keyring +loguru matplotlib nbsphinx +numpydoc +pandas < 2 +pyomo != 6.4.3 +pypsa == 0.20.1 saio -pyomo != 6.4.3 \ No newline at end of file +scikit-learn +sphinx_rtd_theme > 1.2.2 +sqlalchemy \ No newline at end of file From f70087bfb41b9121d878f50c876bfd73ea064b6f Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:40:01 +0200 Subject: [PATCH 11/32] Add (empty) __init__ --- etrago/analyze/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 etrago/analyze/__init__.py diff --git a/etrago/analyze/__init__.py b/etrago/analyze/__init__.py new file mode 100644 index 00000000..5dd475b7 --- /dev/null +++ b/etrago/analyze/__init__.py @@ -0,0 +1,7 @@ +""" + +""" + +__copyright__ = "tba" +__license__ = "tba" +__author__ = "tba" From 188b7b75d78d8fe1f948f8144b9309f2a60d7bc0 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:42:14 +0200 Subject: [PATCH 12/32] Add new modules to rtd spi --- doc/api.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/api.rst b/doc/api.rst index 8c52c459..4a49271d 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -8,5 +8,8 @@ API api/etrago.analyze api/etrago.cluster + api/etrago.disaggregate + api/etrago.execute api/etrago.tools api/appl.rst + api/network.rst From dbf09217e2cf38ad5cf7ab51c1a8e18cd9850823 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:44:08 +0200 Subject: [PATCH 13/32] Delete old filenames from nox testing --- noxfile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index 94fc8c3b..434c63a3 100644 --- a/noxfile.py +++ b/noxfile.py @@ -17,7 +17,6 @@ "etrago/execute/grid_optimization.py", "etrago/execute/market_optimization.py", "etrago/network.py", - "etrago/tools/calc_results.py", "etrago/tools/extendable.py", "etrago/tools/io.py", "etrago/tools/utilities.py", From 2873420d1425f64fbdaed1c021310a579ac363d4 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:46:37 +0200 Subject: [PATCH 14/32] Apply black --- etrago/analyze/plot.py | 147 +++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index a0d09057..1908454a 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -912,9 +912,9 @@ def calc_storage_expansion_per_bus( [(idx, "battery") for idx in battery_distribution.index] ) - dist.loc[ - dist.index.get_level_values("carrier") == "battery" - ] = battery_distribution + dist.loc[dist.index.get_level_values("carrier") == "battery"] = ( + battery_distribution + ) if "H2_overground" in carriers: h2_overground = network.stores[ network.stores.carrier == "H2_overground" @@ -930,9 +930,9 @@ def calc_storage_expansion_per_bus( [(idx, "H2_overground") for idx in h2_over_distribution.index] ) - dist.loc[ - dist.index.get_level_values("carrier") == "H2_overground" - ] = h2_over_distribution + dist.loc[dist.index.get_level_values("carrier") == "H2_overground"] = ( + h2_over_distribution + ) if "H2_overground" in carriers: h2_underground = network.stores[ @@ -1819,7 +1819,9 @@ def plot_background_grid(network, ax, geographical_boundaries, osm): ) -def demand_side_management(self, buses, snapshots, agg="5h", used=False, apply_on="grid_model"): +def demand_side_management( + self, buses, snapshots, agg="5h", used=False, apply_on="grid_model" +): """Calculate shifting potential of demand side management Parameters @@ -1854,13 +1856,13 @@ def demand_side_management(self, buses, snapshots, agg="5h", used=False, apply_o else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) df = pd.DataFrame(index=network.snapshots[snapshots]) link = network.links[ - (network.links.carrier == "dsm") - & (network.links.bus0.isin(buses)) + (network.links.carrier == "dsm") & (network.links.bus0.isin(buses)) ] s = network.stores[ (network.stores.carrier == "dsm") @@ -1952,15 +1954,14 @@ def bev_flexibility_potential( else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) # Initialize DataFrame df = pd.DataFrame(index=network.snapshots[snapshots]) # Select BEV buses and links - bev_buses = network.buses[ - network.buses.carrier.str.contains("Li ion") - ] + bev_buses = network.buses[network.buses.carrier.str.contains("Li ion")] bev_links = network.links[ (network.links.bus1.isin(bev_buses.index.values)) & (network.links.bus0.isin(buses)) @@ -2008,9 +2009,7 @@ def bev_flexibility_potential( ) if used: - bev_links_t_used = network.links_t.p0[bev_links.index].iloc[ - snapshots - ] + bev_links_t_used = network.links_t.p0[bev_links.index].iloc[snapshots] bev_links_t_used.columns = bev_links_t_used.columns.map(bev_links.bus1) @@ -2076,7 +2075,8 @@ def heat_stores( else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) df = pd.DataFrame(index=network.snapshots[snapshots]) @@ -2088,9 +2088,7 @@ def heat_stores( ].index ) & network.links.bus1.isin( - network.buses[ - network.buses.carrier.str.contains("heat") - ].index + network.buses[network.buses.carrier.str.contains("heat")].index ) ].bus1.unique() @@ -2174,7 +2172,8 @@ def hydrogen_stores( else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) df = pd.DataFrame(index=network.snapshots[snapshots]) @@ -2186,9 +2185,7 @@ def hydrogen_stores( ].index ) & network.links.bus1.isin( - network.buses[ - network.buses.carrier.str.contains("H2") - ].index + network.buses[network.buses.carrier.str.contains("H2")].index ) ].bus1.unique() @@ -2208,7 +2205,13 @@ def hydrogen_stores( def flexibility_usage( - self, flexibility, agg="5h", snapshots=[], buses=[], pre_path=None, apply_on="grid_model", + self, + flexibility, + agg="5h", + snapshots=[], + buses=[], + pre_path=None, + apply_on="grid_model", ): """Plots temporal distribution of potential and usage for flexibilities @@ -2246,7 +2249,8 @@ def flexibility_usage( else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) colors = coloring() colors["dlr"] = "orange" @@ -2300,9 +2304,7 @@ def flexibility_usage( df["p_min"] = su.p_nom_opt.sum() * (-1) df["p_max"] = su.p_nom_opt.sum() df["p"] = ( - network.storage_units_t.p[su.index] - .sum(axis=1) - .iloc[snapshots] + network.storage_units_t.p[su.index].sum(axis=1).iloc[snapshots] ) df["e_min"] = 0 @@ -2344,7 +2346,9 @@ def flexibility_usage( fig_e.savefig(pre_path + f"stored_e_{flexibility}") -def plot_carrier(etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="grid_model"): +def plot_carrier( + etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="grid_model" +): """ Parameters ---------- @@ -2374,7 +2378,8 @@ def plot_carrier(etrago, carrier_links=["AC"], carrier_buses=["AC"], apply_on="g else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) colors = coloring() line_colors = "lightblue" @@ -2483,7 +2488,7 @@ def plot_grid( * 'ramp_down': re-dispatch down per carrier in selected timesteps * 'PowerToH2': location and sizes of electrolyzers * 'flexibility_usage': use of DSM and BEV charger - * 'PowerToH2_correlation': indication of degree of correlation to + * 'PowerToH2_correlation': indication of degree of correlation to market or nodal price of electrolyzers timesteps : array, optional @@ -2538,7 +2543,8 @@ def plot_grid( else: logger.warning( """Parameter apply_on must be one of ['grid_model', 'market_model' - 'pre_market_model', 'disaggregated_network'.""") + 'pre_market_model', 'disaggregated_network'.""" + ) # Set colors for plotting plotting_colors(network) @@ -2851,31 +2857,40 @@ def plot_grid( bus_colors = coloring()["power_to_H2"] bus_legend = "PowerToH2" bus_unit = "TW" - - - market_bus_de = self.market_model.buses[(self.market_model.buses.country=="DE") & (self.market_model.buses.carrier=="AC")].index + market_bus_de = self.market_model.buses[ + (self.market_model.buses.country == "DE") + & (self.market_model.buses.carrier == "AC") + ].index market_price = self.market_model.buses_t.marginal_price[market_bus_de] - + bus_colors = pd.Series(index=network.buses.index, data=0) - for bus in network.links[(network.links.carrier == "power_to_H2")].bus0: - + for bus in network.links[ + (network.links.carrier == "power_to_H2") + ].bus0: + nodal_price = network.buses_t.marginal_price[bus] - - ely = network.links_t.p0[network.links[(network.links.carrier == "power_to_H2") - & (network.links.bus0==bus)].index] + + ely = network.links_t.p0[ + network.links[ + (network.links.carrier == "power_to_H2") + & (network.links.bus0 == bus) + ].index + ] df_corr = pd.DataFrame() - + df_corr["ely"] = ely df_corr["market"] = market_price df_corr["nodal_price"] = nodal_price - - bus_colors[bus] = (df_corr.corr(method = 'spearman').loc["nodal_price", "ely"]/ ( - df_corr.corr(method = 'spearman').loc["nodal_price", "ely"])+ - df_corr.corr(method = 'spearman').loc["market", "ely"]) - + + bus_colors[bus] = ( + df_corr.corr(method="spearman").loc["nodal_price", "ely"] + / (df_corr.corr(method="spearman").loc["nodal_price", "ely"]) + + df_corr.corr(method="spearman").loc["market", "ely"] + ) + bus_colors = bus_colors.abs() - + # ely.corr # ely_corr_market = ely.corrwith( # market_price, method = 'spearman', axis=1) @@ -2900,11 +2915,11 @@ def plot_grid( link_cmap=plt.cm.viridis, bus_sizes=bus_sizes, bus_colors=bus_colors, - bus_cmap = plt.cm.viridis, + bus_cmap=plt.cm.viridis, line_widths=line_widths, link_widths=link_widths, flow=flow, - #title=title, + # title=title, geomap=False, projection=ccrs.PlateCarree(), color_geomap=True, @@ -3043,18 +3058,19 @@ def plot_grid( ll[1], values=v, ticks=v[0:101:10], - #fraction=0.028, + # fraction=0.028, pad=0.04, - orientation="horizontal" + orientation="horizontal", ) # Set legend label cb.set_label(label) - - elif type(bus_colors) != str: - #import pdb; pdb.set_trace() - ll[0].set_clim([0, bus_colors.max()]) - plt.colorbar(ll[0], fraction=0.04, pad=0.004, label="correlation factor", ax=ax) + elif type(bus_colors) != str: + # import pdb; pdb.set_trace() + ll[0].set_clim([0, bus_colors.max()]) + plt.colorbar( + ll[0], fraction=0.04, pad=0.004, label="correlation factor", ax=ax + ) # Show plot or save to file if filename is None: @@ -3209,10 +3225,15 @@ def plot_clusters( & lines["bus1"].isin(map_buses.index) ] lines["geom"] = lines.apply( - lambda x: x["geom"] - if not pd.isna(x["geom"]) - else LineString( - [map_buses["geom"][x["bus0"]], map_buses["geom"][x["bus1"]]] + lambda x: ( + x["geom"] + if not pd.isna(x["geom"]) + else LineString( + [ + map_buses["geom"][x["bus0"]], + map_buses["geom"][x["bus1"]], + ] + ) ), axis=1, ) From 87949670969711b9e2745e0d55d9fd52227b2bd8 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:54:40 +0200 Subject: [PATCH 15/32] Fix flake8 issues --- etrago/analyze/plot.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 1908454a..1c41866b 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -57,8 +57,9 @@ "DLR-Institute for Networked Energy Systems" ) __license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" -__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, ClaraBuettner, -CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, BartelsJ""" +__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, +ClaraBuettner, CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, +BartelsJ""" def set_epsg_network(network): @@ -2478,7 +2479,8 @@ def plot_grid( Set static bus color or attribute to plot. The default is 'grey'. Current options: - * 'nodal_production_balance': net producer/consumer in selected timeteps + * 'nodal_production_balance': net producer/consumer in selected + time steps * 'storage_expansion': storage expansion per bus and technology * 'storage_distribution': installed storage units per bus * 'h2_battery_storage_expansion': storage expansion per bus and From 05d27e4e6707fd01a7be350817f46c3db03507b8 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:55:03 +0200 Subject: [PATCH 16/32] Apply isort --- etrago/analyze/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 1c41866b..a73fb5bb 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -28,7 +28,6 @@ from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse - import matplotlib import matplotlib.patches as mpatches import numpy as np @@ -44,12 +43,13 @@ logger = logging.getLogger(__name__) if "READTHEDOCS" not in os.environ: - from etrago.execute import import_gen_from_links from pyproj import Proj, transform from shapely.geometry import LineString, Point import geopandas as gpd import tilemapbase + from etrago.execute import import_gen_from_links + __copyright__ = ( "Flensburg University of Applied Sciences, " "Europa-Universität Flensburg, " From 2e5aed270a68fbbedf3add9d71c5b6c37144fe4c Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 13:56:01 +0200 Subject: [PATCH 17/32] Fix flake8 issues --- etrago/analyze/plot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index a73fb5bb..6b64bfe6 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -57,8 +57,8 @@ "DLR-Institute for Networked Energy Systems" ) __license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" -__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, -ClaraBuettner, CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, +__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, +ClaraBuettner, CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, BartelsJ""" @@ -2479,7 +2479,7 @@ def plot_grid( Set static bus color or attribute to plot. The default is 'grey'. Current options: - * 'nodal_production_balance': net producer/consumer in selected + * 'nodal_production_balance': net producer/consumer in selected time steps * 'storage_expansion': storage expansion per bus and technology * 'storage_distribution': installed storage units per bus From 9cfe2917c4b33f3e32a939898ca362d787b1c75a Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 14:04:56 +0200 Subject: [PATCH 18/32] Fix flake8 issues --- etrago/analyze/plot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 6b64bfe6..5f7abe4f 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -28,6 +28,7 @@ from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse +from pypsa.plot import draw_map_cartopy import matplotlib import matplotlib.patches as mpatches import numpy as np @@ -38,7 +39,6 @@ import cartopy.crs as ccrs except ImportError: cartopy_present = False -from pypsa.plot import draw_map_cartopy logger = logging.getLogger(__name__) @@ -3040,7 +3040,7 @@ def plot_grid( ) ax.add_artist(l3) - if type(line_colors) != str: + if type(line_colors) is not str: # Set fixed boundaries if selected in parameters if not boundaries: boundaries = [ @@ -3067,7 +3067,7 @@ def plot_grid( # Set legend label cb.set_label(label) - elif type(bus_colors) != str: + elif type(bus_colors) is not str: # import pdb; pdb.set_trace() ll[0].set_clim([0, bus_colors.max()]) plt.colorbar( From 0082e7ccd6a6fbd831aaf0f3f568c76dec21b98b Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 14:08:13 +0200 Subject: [PATCH 19/32] Apply isort --- etrago/disaggregate/temporal.py | 2 +- etrago/network.py | 54 +++++++++++++++------------------ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/etrago/disaggregate/temporal.py b/etrago/disaggregate/temporal.py index 4b9b2aaa..5e7b6ede 100644 --- a/etrago/disaggregate/temporal.py +++ b/etrago/disaggregate/temporal.py @@ -21,8 +21,8 @@ """ execute.py defines optimization and simulation methods for the etrago object. """ -import os import logging +import os import time import pandas as pd diff --git a/etrago/network.py b/etrago/network.py index 956dfeb3..5ef6da6a 100644 --- a/etrago/network.py +++ b/etrago/network.py @@ -33,28 +33,6 @@ from etrago.tools import db from etrago import __version__ -from etrago.disaggregate.spatial import run_disaggregation -from etrago.disaggregate.temporal import dispatch_disaggregation -from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering -from etrago.cluster.gas import run_spatial_clustering_gas -from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering -from etrago.execute import ( - lopf, - optimize, - run_pf_post_lopf, -) -from etrago.execute.grid_optimization import ( - add_redispatch_generators, - grid_optimization, -) -from etrago.execute.market_optimization import ( - build_market_model, - market_optimization, -) -from etrago.execute.sclopf import ( - iterate_sclopf, - post_contingency_analysis_lopf, -) from etrago.analyze.calc_results import ( ac_export, ac_export_per_country, @@ -64,13 +42,6 @@ german_network, system_costs_germany, ) -from etrago.tools.extendable import extendable -from etrago.tools.io import ( - NetworkScenario, - add_ch4_h2_correspondence, - decommissioning, - extension, -) from etrago.analyze.plot import ( bev_flexibility_potential, demand_side_management, @@ -88,6 +59,31 @@ plot_heat_summary, shifted_energy, ) +from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering +from etrago.cluster.gas import run_spatial_clustering_gas +from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering +from etrago.disaggregate.spatial import run_disaggregation +from etrago.disaggregate.temporal import dispatch_disaggregation +from etrago.execute import lopf, optimize, run_pf_post_lopf +from etrago.execute.grid_optimization import ( + add_redispatch_generators, + grid_optimization, +) +from etrago.execute.market_optimization import ( + build_market_model, + market_optimization, +) +from etrago.execute.sclopf import ( + iterate_sclopf, + post_contingency_analysis_lopf, +) +from etrago.tools.extendable import extendable +from etrago.tools.io import ( + NetworkScenario, + add_ch4_h2_correspondence, + decommissioning, + extension, +) from etrago.tools.utilities import ( add_missing_components, adjust_CH4_gen_carriers, From 4c0116446e1bb3424e6b974c85c3cea16e11f55b Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 15:39:46 +0200 Subject: [PATCH 20/32] Call etrago.analyze package and not module in rtd --- doc/api/etrago.analyze.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/etrago.analyze.rst b/doc/api/etrago.analyze.rst index d7a9f923..e86609ee 100644 --- a/doc/api/etrago.analyze.rst +++ b/doc/api/etrago.analyze.rst @@ -1,5 +1,5 @@ -etrago.analyze module -===================== +etrago.analyze package +====================== etrago.analyze.calc\_results module ----------------------------------- From 202a2c422372772588ba8207d62baf15f2796f6b Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Thu, 8 Aug 2024 16:16:48 +0200 Subject: [PATCH 21/32] Revert "Delete unused imports" This reverts commit f52d320be9f1a34889130287407407f4c65109ba. --- etrago/analyze/plot.py | 17 +++++++++-------- etrago/tools/db.py | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 5f7abe4f..d13e7a13 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -21,14 +21,14 @@ """ Plot.py defines functions necessary to plot results of eTraGo. """ -from math import sqrt +from math import log10, sqrt import logging import os from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse -from pypsa.plot import draw_map_cartopy +from pyproj import Proj, transform import matplotlib import matplotlib.patches as mpatches import numpy as np @@ -39,17 +39,18 @@ import cartopy.crs as ccrs except ImportError: cartopy_present = False +from pypsa.plot import draw_map_cartopy logger = logging.getLogger(__name__) if "READTHEDOCS" not in os.environ: + from etrago.execute import import_gen_from_links + from geoalchemy2.shape import to_shape from pyproj import Proj, transform - from shapely.geometry import LineString, Point + from shapely.geometry import LineString, MultiPoint, Point, Polygon import geopandas as gpd import tilemapbase - from etrago.execute import import_gen_from_links - __copyright__ = ( "Flensburg University of Applied Sciences, " "Europa-Universität Flensburg, " @@ -57,9 +58,8 @@ "DLR-Institute for Networked Energy Systems" ) __license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" -__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, -ClaraBuettner, CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, -BartelsJ""" +__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, ClaraBuettner, +CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, BartelsJ""" def set_epsg_network(network): @@ -2859,6 +2859,7 @@ def plot_grid( bus_colors = coloring()["power_to_H2"] bus_legend = "PowerToH2" bus_unit = "TW" + market_bus_de = self.market_model.buses[ (self.market_model.buses.country == "DE") diff --git a/etrago/tools/db.py b/etrago/tools/db.py index a0d19ccf..3f9ca2a5 100644 --- a/etrago/tools/db.py +++ b/etrago/tools/db.py @@ -23,6 +23,7 @@ from sqlalchemy import create_engine import keyring +import oedialect def readcfg(filepath, section): From c9af4eed2886942957cb4eee029925a15e087ae8 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Mon, 12 Aug 2024 13:27:55 +0200 Subject: [PATCH 22/32] Apply black --- etrago/analyze/plot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index d13e7a13..68b49732 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -2859,7 +2859,6 @@ def plot_grid( bus_colors = coloring()["power_to_H2"] bus_legend = "PowerToH2" bus_unit = "TW" - market_bus_de = self.market_model.buses[ (self.market_model.buses.country == "DE") From f1f90333b056e7e1cae30f96aa581d825ec1f6a1 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Mon, 12 Aug 2024 14:26:35 +0200 Subject: [PATCH 23/32] Fix flake 8 issues --- etrago/analyze/plot.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 68b49732..7cbc1980 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -21,14 +21,14 @@ """ Plot.py defines functions necessary to plot results of eTraGo. """ -from math import log10, sqrt +from math import sqrt import logging import os from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerPatch from matplotlib.patches import Circle, Ellipse -from pyproj import Proj, transform +from pypsa.plot import draw_map_cartopy import matplotlib import matplotlib.patches as mpatches import numpy as np @@ -39,15 +39,15 @@ import cartopy.crs as ccrs except ImportError: cartopy_present = False -from pypsa.plot import draw_map_cartopy + logger = logging.getLogger(__name__) if "READTHEDOCS" not in os.environ: from etrago.execute import import_gen_from_links - from geoalchemy2.shape import to_shape + from geoalchemy2.shape import to_shape # noqa: F401 from pyproj import Proj, transform - from shapely.geometry import LineString, MultiPoint, Point, Polygon + from shapely.geometry import LineString, Point import geopandas as gpd import tilemapbase @@ -58,8 +58,9 @@ "DLR-Institute for Networked Energy Systems" ) __license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)" -__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, ClaraBuettner, -CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, BartelsJ""" +__author__ = """ulfmueller, MarlonSchlemminger, mariusves, lukasol, +ClaraBuettner, CarlosEpia, pieterhexen, gnn, fwitte, lukasol, KathiEsterl, +BartelsJ""" def set_epsg_network(network): From 7fb996d99c0c4edcb531cbf51d645cdf8704f10d Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Mon, 12 Aug 2024 14:29:39 +0200 Subject: [PATCH 24/32] Apply isort --- etrago/analyze/plot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/etrago/analyze/plot.py b/etrago/analyze/plot.py index 7cbc1980..07dd75d6 100644 --- a/etrago/analyze/plot.py +++ b/etrago/analyze/plot.py @@ -44,13 +44,14 @@ logger = logging.getLogger(__name__) if "READTHEDOCS" not in os.environ: - from etrago.execute import import_gen_from_links from geoalchemy2.shape import to_shape # noqa: F401 from pyproj import Proj, transform from shapely.geometry import LineString, Point import geopandas as gpd import tilemapbase + from etrago.execute import import_gen_from_links + __copyright__ = ( "Flensburg University of Applied Sciences, " "Europa-Universität Flensburg, " From e67f17bf2507d57b1b15a38a5664cd4028474bfd Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 13 Aug 2024 11:44:22 +0200 Subject: [PATCH 25/32] Correct checks if pyomo is used when temporal disaggregation is selected --- etrago/execute/__init__.py | 95 ++++++++++++++++++------------------- etrago/tools/constraints.py | 2 +- etrago/tools/utilities.py | 15 ++++-- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/etrago/execute/__init__.py b/etrago/execute/__init__.py index 01673832..626a40a3 100644 --- a/etrago/execute/__init__.py +++ b/etrago/execute/__init__.py @@ -141,56 +141,55 @@ def run_lopf(etrago, extra_functionality, method): ) )[0] - if method["pyomo"]: - # repeat the optimization for all slices - for i in range(0, no_slices): - # keep information on the initial state of charge for the - # respectng slice - initial = transits[i - 1] - soc_initial = etrago.conduct_dispatch_disaggregation.loc[ - [etrago.network_tsa.snapshots[initial]] - ].transpose() - etrago.network_tsa.storage_units.state_of_charge_initial = ( - soc_initial - ) - etrago.network_tsa.stores.e_initial = soc_initial - etrago.network_tsa.stores.e_initial.fillna(0, inplace=True) - # the state of charge at the end of each slice is set within - # split_dispatch_disaggregation_constraints in constraints.py - - # adapt start and end snapshot of respecting slice - start = transits[i - 1] + skipped - end = transits[i] + (skipped - 1) - if i == 0: - start = 0 - if i == no_slices - 1: - end = len(etrago.network_tsa.snapshots) - - etrago.network_tsa.lopf( - etrago.network_tsa.snapshots[start : end + 1], - solver_name=etrago.args["solver"], - solver_options=etrago.args["solver_options"], - pyomo=True, - extra_functionality=extra_functionality, - formulation=etrago.args["model_formulation"], - ) - if etrago.network_tsa.results["Solver"][0]["Status"] != "ok": - raise Exception("LOPF not solved.") - - else: - for i in range(0, no_slices): - status, termination_condition = network_lopf( - etrago.network_tsa, - etrago.network_tsa.snapshots[start : end + 1], - solver_name=etrago.args["solver"], - solver_options=etrago.args["solver_options"], - extra_functionality=extra_functionality, - formulation=etrago.args["model_formulation"], - ) + # repeat the optimization for all slices + for i in range(0, no_slices): + # keep information on the initial state of charge for the + # respectng slice + initial = transits[i - 1] + soc_initial = etrago.conduct_dispatch_disaggregation.loc[ + [etrago.network_tsa.snapshots[initial]] + ].transpose() + etrago.network_tsa.storage_units.state_of_charge_initial = ( + soc_initial + ) + etrago.network_tsa.stores.e_initial = soc_initial + etrago.network_tsa.stores.e_initial.fillna(0, inplace=True) + # the state of charge at the end of each slice is set within + # split_dispatch_disaggregation_constraints in constraints.py + + # adapt start and end snapshot of respecting slice + start = transits[i - 1] + skipped + end = transits[i] + (skipped - 1) + if i == 0: + start = 0 + if i == no_slices - 1: + end = len(etrago.network_tsa.snapshots) + + if method["formulation"] == "pyomo": + etrago.network_tsa.lopf( + etrago.network_tsa.snapshots[start : end + 1], + solver_name=etrago.args["solver"], + solver_options=etrago.args["solver_options"], + pyomo=True, + extra_functionality=extra_functionality, + formulation=etrago.args["model_formulation"], + ) - if status != "ok": - raise Exception("LOPF not solved.") + if etrago.network_tsa.results["Solver"][0]["Status"] != "ok": + raise Exception("LOPF not solved.") + else: + raise Exception("Temporal disaggregation currently only works when using pyomo.") + status, termination_condition = network_lopf( + etrago.network_tsa, + etrago.network_tsa.snapshots[start : end + 1], + solver_name=etrago.args["solver"], + solver_options=etrago.args["solver_options"], + extra_functionality=extra_functionality, + formulation=etrago.args["model_formulation"], + ) + if status != "ok": + raise Exception("LOPF not solved.") etrago.network_tsa.storage_units.state_of_charge_initial = 0 etrago.network_tsa.stores.e_initial = 0 diff --git a/etrago/tools/constraints.py b/etrago/tools/constraints.py index 131c5b4e..06dff351 100755 --- a/etrago/tools/constraints.py +++ b/etrago/tools/constraints.py @@ -3390,7 +3390,7 @@ def functionality(self, network, snapshots): ) if self.conduct_dispatch_disaggregation is not False: - if self.args["method"]["pyomo"]: + if self.args["method"]["formulation"]=="pyomo": split_dispatch_disaggregation_constraints( self, network, snapshots ) diff --git a/etrago/tools/utilities.py b/etrago/tools/utilities.py index eb63e603..4bf7a73c 100755 --- a/etrago/tools/utilities.py +++ b/etrago/tools/utilities.py @@ -2581,13 +2581,13 @@ def check_args(etrago): etrago.args["snapshot_clustering"]["n_segments"] ), "Number of segments is higher than number of snapshots" - if not etrago.args["method"]["pyomo"]: + if etrago.args["method"]["formulation"] != "pyomo": logger.warning( "Snapshot clustering constraints are" " not yet correctly implemented without pyomo." - " Setting `args['method']['pyomo']` to `True`." + " Setting `args['method']['formulation']` to `pyomo`." ) - etrago.args["method"]["pyomo"] = True + etrago.args["method"]["formulation"] = "pyomo" if etrago.args["method"]["formulation"] != "pyomo": try: @@ -2607,6 +2607,15 @@ def check_args(etrago): ) raise + if (etrago.args["method"]["formulation"] != "pyomo") & ( + etrago.args["temporal_disaggregation"]["active"]): + logger.warning( + "Temporal disaggregation is" + " not yet correctly implemented without pyomo." + " Setting `args['method']['formulation']` to `pyomo`." + ) + etrago.args["method"]["formulation"] = "pyomo" + def drop_sectors(self, drop_carriers): """ From db8f173768e622e6208f3fe239d170d40c50e86e Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 13 Aug 2024 11:46:13 +0200 Subject: [PATCH 26/32] Remove pdb statement if lopf was failing --- etrago/execute/market_optimization.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/etrago/execute/market_optimization.py b/etrago/execute/market_optimization.py index c0a40007..e175ed44 100644 --- a/etrago/execute/market_optimization.py +++ b/etrago/execute/market_optimization.py @@ -72,10 +72,7 @@ def market_optimization(self): f"""Optimization failed with status {status} and condition {condition}""" ) - self.pre_market_model.model.print_infeasibilities() - import pdb - pdb.set_trace() else: logger.warning("Method type must be either 'pyomo' or 'linopy'") From c3cb6a4af7916f7404095ecc3e77eef53906acc4 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 13 Aug 2024 11:47:09 +0200 Subject: [PATCH 27/32] Limit pyomo version The optimization is failing when a newer version is installed --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index f108aad3..da278ce7 100755 --- a/setup.py +++ b/setup.py @@ -55,6 +55,7 @@ def read(*names, **kwargs): "matplotlib >= 3.0.3", "oedialect", "pandas < 2.2", + "pyomo < 6.6", "pypsa == 0.26.2", "rtree", "saio", From ae18e9d4071716ab6b27c747e2b932052900e050 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 13 Aug 2024 11:50:29 +0200 Subject: [PATCH 28/32] Apply black --- etrago/execute/__init__.py | 26 +++++++++++++++----------- etrago/tools/utilities.py | 15 ++++++++------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/etrago/execute/__init__.py b/etrago/execute/__init__.py index 626a40a3..81085a8f 100644 --- a/etrago/execute/__init__.py +++ b/etrago/execute/__init__.py @@ -141,7 +141,6 @@ def run_lopf(etrago, extra_functionality, method): ) )[0] - # repeat the optimization for all slices for i in range(0, no_slices): # keep information on the initial state of charge for the @@ -176,18 +175,23 @@ def run_lopf(etrago, extra_functionality, method): formulation=etrago.args["model_formulation"], ) - if etrago.network_tsa.results["Solver"][0]["Status"] != "ok": + if ( + etrago.network_tsa.results["Solver"][0]["Status"] + != "ok" + ): raise Exception("LOPF not solved.") else: - raise Exception("Temporal disaggregation currently only works when using pyomo.") + raise Exception( + "Temporal disaggregation currently only works when using pyomo." + ) status, termination_condition = network_lopf( - etrago.network_tsa, - etrago.network_tsa.snapshots[start : end + 1], - solver_name=etrago.args["solver"], - solver_options=etrago.args["solver_options"], - extra_functionality=extra_functionality, - formulation=etrago.args["model_formulation"], - ) + etrago.network_tsa, + etrago.network_tsa.snapshots[start : end + 1], + solver_name=etrago.args["solver"], + solver_options=etrago.args["solver_options"], + extra_functionality=extra_functionality, + formulation=etrago.args["model_formulation"], + ) if status != "ok": raise Exception("LOPF not solved.") @@ -423,7 +427,7 @@ def optimize(self): """ - if self.args["method"]["market_optimization"]: + if self.args["method"]["market_optimization"]["active"]: self.market_optimization() # self.market_results_to_grid() diff --git a/etrago/tools/utilities.py b/etrago/tools/utilities.py index 4bf7a73c..3214981e 100755 --- a/etrago/tools/utilities.py +++ b/etrago/tools/utilities.py @@ -2608,13 +2608,14 @@ def check_args(etrago): raise if (etrago.args["method"]["formulation"] != "pyomo") & ( - etrago.args["temporal_disaggregation"]["active"]): - logger.warning( - "Temporal disaggregation is" - " not yet correctly implemented without pyomo." - " Setting `args['method']['formulation']` to `pyomo`." - ) - etrago.args["method"]["formulation"] = "pyomo" + etrago.args["temporal_disaggregation"]["active"] + ): + logger.warning( + "Temporal disaggregation is" + " not yet correctly implemented without pyomo." + " Setting `args['method']['formulation']` to `pyomo`." + ) + etrago.args["method"]["formulation"] = "pyomo" def drop_sectors(self, drop_carriers): From 1038234ff40bc229c75f48e337288c0dcddfa516 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Tue, 13 Aug 2024 11:56:59 +0200 Subject: [PATCH 29/32] Fix flake8 issue --- etrago/execute/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/etrago/execute/__init__.py b/etrago/execute/__init__.py index 81085a8f..7d00e861 100644 --- a/etrago/execute/__init__.py +++ b/etrago/execute/__init__.py @@ -182,7 +182,8 @@ def run_lopf(etrago, extra_functionality, method): raise Exception("LOPF not solved.") else: raise Exception( - "Temporal disaggregation currently only works when using pyomo." + """Temporal disaggregation currently only works when + using pyomo.""" ) status, termination_condition = network_lopf( etrago.network_tsa, From ca94548b563d3a84777600ec7670e9380fd916dd Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Wed, 14 Aug 2024 10:29:18 +0200 Subject: [PATCH 30/32] Rename cluster.snapshot to cluster.temporal --- doc/api/etrago.cluster.rst | 4 ++-- etrago/cluster/{snapshot.py => temporal.py} | 0 etrago/network.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename etrago/cluster/{snapshot.py => temporal.py} (100%) diff --git a/doc/api/etrago.cluster.rst b/doc/api/etrago.cluster.rst index e7432dbb..6fd27ff3 100644 --- a/doc/api/etrago.cluster.rst +++ b/doc/api/etrago.cluster.rst @@ -18,10 +18,10 @@ etrago.cluster.gas module :undoc-members: :show-inheritance: -etrago.cluster.snapshot module +etrago.cluster.temporal module -------------------------------- -.. automodule:: etrago.cluster.snapshot +.. automodule:: etrago.cluster.temporal :members: :undoc-members: :show-inheritance: diff --git a/etrago/cluster/snapshot.py b/etrago/cluster/temporal.py similarity index 100% rename from etrago/cluster/snapshot.py rename to etrago/cluster/temporal.py diff --git a/etrago/network.py b/etrago/network.py index 5ef6da6a..c69d0e0b 100644 --- a/etrago/network.py +++ b/etrago/network.py @@ -61,7 +61,7 @@ ) from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering from etrago.cluster.gas import run_spatial_clustering_gas -from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering +from etrago.cluster.temporal import skip_snapshots, snapshot_clustering from etrago.disaggregate.spatial import run_disaggregation from etrago.disaggregate.temporal import dispatch_disaggregation from etrago.execute import lopf, optimize, run_pf_post_lopf From 22727661399cbbf4102bd8eea5d9415b2761f541 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Wed, 14 Aug 2024 10:35:40 +0200 Subject: [PATCH 31/32] Rename temporal clustering file --- noxfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index fc095493..0166dd29 100644 --- a/noxfile.py +++ b/noxfile.py @@ -10,8 +10,8 @@ "etrago/analyze/plot.py", "etrago/cluster/electrical.py", "etrago/cluster/gas.py", - "etrago/cluster/snapshot.py", "etrago/cluster/spatial.py", + "etrago/cluster/temporal.py", "etrago/disaggregate/spatial.py", "etrago/disaggregate/temporal.py", "etrago/execute/__init__.py", From 1f2a52a6b55ec9881fbf02eaa11a21d1ddce4c62 Mon Sep 17 00:00:00 2001 From: ClaraBuettner Date: Wed, 14 Aug 2024 13:53:34 +0200 Subject: [PATCH 32/32] Update filename --- etrago/tools/extendable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etrago/tools/extendable.py b/etrago/tools/extendable.py index df115aa6..001cf5bd 100644 --- a/etrago/tools/extendable.py +++ b/etrago/tools/extendable.py @@ -27,7 +27,7 @@ import numpy as np import pandas as pd -from etrago.cluster.snapshot import snapshot_clustering +from etrago.cluster.temporal import snapshot_clustering from etrago.tools.utilities import convert_capital_costs, find_snapshots __copyright__ = (