Skip to content

Commit

Permalink
Merge pull request #738 from openego/fixes/run-consecutive-method
Browse files Browse the repository at this point in the history
Fixes/run consecutive method
  • Loading branch information
ClaraBuettner authored Jun 12, 2024
2 parents 6319cfa + 20b3ee3 commit b77a37c
Show file tree
Hide file tree
Showing 14 changed files with 1,299 additions and 201 deletions.
43 changes: 32 additions & 11 deletions etrago/appl.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,27 @@
"db": "egon-data", # database session
"gridversion": None, # None for model_draft or Version number
"method": { # Choose method and settings for optimization
"type": "market_grid", # type of optimization, 'lopf', 'sclopf' or 'market_grid'
"n_iter": 1, # abort criterion of iterative optimization, 'n_iter' or 'threshold'
"pyomo": True, # set if pyomo is used for model building
"formulation": "pyomo",
"market_zones": "status_quo", # only used if type='market_grid'
"rolling_horizon": { # Define parameter of market optimization
"planning_horizon": 72, # number of snapshots in each optimization
"overlap": 24, # number of overlapping hours
},
"type": "lopf", # type of optimization, 'lopf' or 'sclopf'
"n_iter": 4, # abort criterion of iterative optimization, 'n_iter' or 'threshold'
"formulation": "linopy",
"market_optimization":
{
"active": True,
"market_zones": "status_quo", # only used if type='market_grid'
"rolling_horizon": {# Define parameter of market optimization
"planning_horizon": 168, # number of snapshots in each optimization
"overlap": 120, # number of overlapping hours
},
"redispatch": True,
}
},
"pf_post_lopf": {
"active": False, # choose if perform a pf after lopf
"add_foreign_lopf": True, # keep results of lopf for foreign DC-links
"q_allocation": "p_nom", # allocate reactive power via 'p_nom' or 'p'
},
"start_snapshot": 1,
"end_snapshot": 10,
"end_snapshot": 168,
"solver": "gurobi", # glpk, cplex or gurobi
"solver_options": {
"BarConvTol": 1.0e-5,
Expand Down Expand Up @@ -678,6 +682,21 @@ def run_etrago(args, json_path):
# import network from database
etrago.build_network_from_db()

# drop generators without p_nom
etrago.network.mremove(
"Generator",
etrago.network.generators[
etrago.network.generators.p_nom==0].index
)

# Temporary drop DLR as it is currently not working with sclopf
if (etrago.args["method"]["type"] == "sclopf") & (
not etrago.network.lines_t.s_max_pu.empty):
print("Setting s_max_pu timeseries to 1")
etrago.network.lines_t.s_max_pu = pd.DataFrame(
index=etrago.network.snapshots,
)

# adjust network regarding eTraGo setting
etrago.adjust_network()

Expand All @@ -688,7 +707,8 @@ def run_etrago(args, json_path):
etrago.spatial_clustering()

etrago.spatial_clustering_gas()

etrago.network.links.loc[etrago.network.links.carrier=="CH4", "p_nom"] *= 100
etrago.network.generators_t.p_max_pu.where(etrago.network.generators_t.p_max_pu>1e-5, other=0., inplace=True)
# snapshot clustering
etrago.snapshot_clustering()

Expand Down Expand Up @@ -728,6 +748,7 @@ def run_etrago(args, json_path):
# execute etrago function
print(datetime.datetime.now())
etrago = run_etrago(args, json_path=None)

print(datetime.datetime.now())
etrago.session.close()
# plots: more in tools/plot.py
Expand Down
17 changes: 13 additions & 4 deletions etrago/args.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"db": "egon-data",
"gridversion": null,
"method": {
"type": "lopf",
"method": {
"type": "lopf",
"n_iter": 4,
"pyomo": true,
"market_zones": "status_quo"
"formulation": "linopy",
"market_optimization":
{
"active": true,
"market_zones": "status_quo",
"rolling_horizon": {
"planning_horizon": 168,
"overlap": 120
},
"redispatch": true
}
},
"pf_post_lopf": {
"active": false,
Expand Down
27 changes: 15 additions & 12 deletions etrago/cluster/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,22 @@ def find_de_closest(network, bus_ne):
# busmap2 defines how the no electrical buses directly connected to AC
# are going to be clustered
busmap2 = {}

# Map crossborder AC buses in case that they were not part of the k-mean
# clustering
if (not etrago.args["network_clustering"]["cluster_foreign_AC"]) & (
cluster_met in ["kmeans", "kmedoids-dijkstra"]
):
buses_orig = network.buses.copy()
ac_buses_out = buses_orig[
(buses_orig["country"] != "DE") & (buses_orig["carrier"] == "AC")
].dropna(subset=["country", "carrier"])

for bus_out in ac_buses_out.index:
busmap2[bus_out] = bus_out
# Do not apply this part if the function is used for creating the market
# model. It adds one bus per country, which is not useful in this case.
if apply_on != "market_model":
if (not etrago.args["network_clustering"]["cluster_foreign_AC"]) & (
cluster_met in ["kmeans", "kmedoids-dijkstra"]
):
buses_orig = network.buses.copy()
ac_buses_out = buses_orig[
(buses_orig["country"] != "DE")
& (buses_orig["carrier"] == "AC")
].dropna(subset=["country", "carrier"])

for bus_out in ac_buses_out.index:
busmap2[bus_out] = bus_out

foreign_hv = network.buses[
(network.buses.country != "DE")
Expand Down Expand Up @@ -899,7 +902,7 @@ def postprocessing(
)

# merge busmap for foreign buses with the German buses
if not settings["cluster_foreign_AC"]:
if not settings["cluster_foreign_AC"] and (apply_on == "grid_model"):
for bus in busmap_foreign.index:
busmap[bus] = busmap_foreign[bus]
if bus == busmap_foreign[bus]:
Expand Down
2 changes: 1 addition & 1 deletion etrago/cluster/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ def skip_snapshots(self):
if (
self.args["temporal_disaggregation"]["active"]
and not self.args["snapshot_clustering"]["active"]
) or self.args["method"]["type"] == "market_grid":
) or self.args["method"]["market_optimization"]:
self.network_tsa = self.network.copy()

n_skip = self.args["skip_snapshots"]
Expand Down
27 changes: 22 additions & 5 deletions etrago/execute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def run_lopf(etrago, extra_functionality, method):
etrago.network_tsa.stores.e_initial = 0

else:
if method["pyomo"]:
if method["formulation"] == "pyomo":
etrago.network.lopf(
etrago.network.snapshots,
solver_name=etrago.args["solver"],
Expand All @@ -209,6 +209,22 @@ def run_lopf(etrago, extra_functionality, method):
if etrago.network.results["Solver"][0]["Status"] != "ok":
raise Exception("LOPF not solved.")

elif method["formulation"] == "linopy":
status, condition = etrago.network.optimize(
solver_name=etrago.args["solver"],
solver_options=etrago.args["solver_options"],
extra_functionality=extra_functionality,
formulation=etrago.args["model_formulation"],
)
if status != "ok":
logger.warning(
f"""Optimization failed with status {status}
and condition {condition}"""
)
etrago.network.model.print_infeasibilities()
import pdb

pdb.set_trace()
else:
status, termination_condition = network_lopf(
etrago.network,
Expand Down Expand Up @@ -408,16 +424,17 @@ def optimize(self):
"""

if self.args["method"]["type"] == "lopf":
self.lopf()

elif self.args["method"]["type"] == "market_grid": # besseren Namen finden
if self.args["method"]["market_optimization"]:
self.market_optimization()

# self.market_results_to_grid()

self.grid_optimization()

elif self.args["method"]["type"] == "lopf":

self.lopf()

elif self.args["method"]["type"] == "sclopf":
self.sclopf(
post_lopf=False,
Expand Down
Loading

0 comments on commit b77a37c

Please sign in to comment.