2023-04-30 08:52:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Solve myopic operations network.
|
|
|
|
"""
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
import logging
|
2022-08-01 09:55:31 +00:00
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
import pandas as pd
|
|
|
|
import pypsa
|
2022-08-01 09:55:31 +00:00
|
|
|
from helper import override_component_attrs
|
2023-04-30 08:52:58 +00:00
|
|
|
from solve_network import prepare_network, solve_network
|
|
|
|
from solve_operations_network import (
|
|
|
|
add_load_shedding,
|
|
|
|
remove_unused_components,
|
|
|
|
set_parameters_from_optimized,
|
|
|
|
)
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
pypsa.pf.logger.setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_myopic(n, config, store_soc, storage_unit_soc):
|
|
|
|
n.stores.e_cyclic = False
|
|
|
|
n.storage_units.cyclic_state_of_charge = False
|
|
|
|
|
|
|
|
biomass_stores = n.stores.carrier.str.isin(["solid biomass", "biogas"])
|
|
|
|
biomass_potential = n.stores.loc[biomass_stores, "e_initial"]
|
|
|
|
|
2022-08-01 10:03:24 +00:00
|
|
|
# storage level contiguity across years
|
2022-08-01 09:55:31 +00:00
|
|
|
n.stores.e_initial = store_soc
|
|
|
|
n.storage_units.state_of_charge_initial = storage_unit_soc
|
|
|
|
|
2022-08-01 10:03:24 +00:00
|
|
|
# replace co2 limit with co2 price
|
2022-08-01 09:55:31 +00:00
|
|
|
n.remove("GlobalConstraint", "CO2Limit")
|
|
|
|
n.stores.at["co2 atmosphere", "marginal_cost"] = -config["co2_price"]
|
|
|
|
|
|
|
|
# handle co2 sequestration
|
2023-04-30 08:52:58 +00:00
|
|
|
assert (
|
|
|
|
sum(n.stores.carriers == "co2 stored") == 1
|
|
|
|
), "Myopic operation not implemented for spatially resolved CO2 sequestration."
|
|
|
|
n.stores.at["co2 stored", "e_nom"] = config["co2_sequestration_limit"] * 1e6 # t/a
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
# reset co2 emissions
|
2023-04-30 08:52:58 +00:00
|
|
|
n.stores.loc[n.stores.carrier == "co2 stored", "e_initial"] = 0.0
|
|
|
|
n.stores.at["co2 atmosphere", "e_initial"] = 0.0
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
# replenish fossil gas and oil with 1000 TWh each
|
|
|
|
fossil_stores = n.stores.carrier.str.isin(["gas", "oil"])
|
2023-04-30 08:52:58 +00:00
|
|
|
n.stores.loc[fossil_stores, "e_initial"] = 1e9
|
|
|
|
n.stores.loc[fossil_stores, "e_nom"] = 10e9
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
# replenish annual solid biomass and biogas potentials
|
|
|
|
n.stores.loc[biomass_stores, "e_initial"] = biomass_potential
|
|
|
|
|
|
|
|
# set storage bidding prices
|
|
|
|
bidding_prices = config["bidding_prices"]
|
|
|
|
for c in n.iterate_components({"Store", "Link", "StorageUnit"}):
|
|
|
|
c.df.marginal_cost.update(c.df.carrier.map(bidding_prices).dropna())
|
|
|
|
|
|
|
|
# deduct industry solid biomass
|
2023-04-30 08:52:58 +00:00
|
|
|
assert (
|
|
|
|
sum(n.stores.carriers == "solid biomass") == 1
|
|
|
|
), "Myopic operation not implemented for spatially resolved solid biomass."
|
|
|
|
n.stores.at["EU solid biomass", "e_initial"] -= (
|
|
|
|
n.loads.at["solid biomass for industry", "p_set"] * 8760
|
|
|
|
)
|
2022-08-01 09:55:31 +00:00
|
|
|
n.remove("Load", "solid biomass for industry")
|
|
|
|
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
def solve_network_myopic(n, config, opts="", **kwargs):
|
2022-08-01 09:55:31 +00:00
|
|
|
rolling_horizon = config["operations"]["rolling_horizon"]
|
|
|
|
|
|
|
|
freq = int(pd.infer_freq(n.snapshots)[:-1])
|
|
|
|
window = rolling_horizon["window"] * 24 // freq
|
|
|
|
overlap = rolling_horizon["overlap"] * 24 // freq
|
|
|
|
kept = window - overlap
|
|
|
|
length = len(n.snapshots)
|
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
assert (
|
|
|
|
kept > 0
|
|
|
|
), f"Overlap ({overlap} days) must be smaller than windows ({window} days)."
|
2022-08-01 09:55:31 +00:00
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
for i in range(length // kept):
|
|
|
|
snapshots = n.snapshots[i * kept : (i + 1) * kept + overlap]
|
2022-08-01 09:55:31 +00:00
|
|
|
logger.info(f"Optimising operations from {snapshots[0]} to {snapshots[-1]}")
|
|
|
|
|
|
|
|
n = solve_network(n, config, opts=opts, snapshots=snapshots, **kwargs)
|
|
|
|
|
|
|
|
last_kept = n.snapshots[(i + 1) * kept - 1]
|
|
|
|
logger.info(f"Setting initial SOCs from {last_kept} for next iteration.\n")
|
|
|
|
|
|
|
|
n.stores.e_initial = n.stores_t.e.loc[last_kept]
|
2023-04-30 08:52:58 +00:00
|
|
|
n.storage_units.state_of_charge_initial = n.storage_units_t.state_of_charge.loc[
|
|
|
|
last_kept
|
|
|
|
]
|
2022-08-01 09:55:31 +00:00
|
|
|
|
2022-08-01 10:11:41 +00:00
|
|
|
# final segment until end of year
|
2023-04-30 08:52:58 +00:00
|
|
|
snapshots = n.snapshots[(i + 1) * kept :]
|
2022-08-01 10:11:41 +00:00
|
|
|
n = solve_network(n, config, opts=opts, snapshots=snapshots, **kwargs)
|
|
|
|
|
2022-08-01 09:55:31 +00:00
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-30 08:52:58 +00:00
|
|
|
if "snakemake" not in globals():
|
2022-08-01 09:55:31 +00:00
|
|
|
from helper import mock_snakemake
|
2023-04-30 08:52:58 +00:00
|
|
|
|
2022-08-01 09:55:31 +00:00
|
|
|
snakemake = mock_snakemake(
|
2023-04-30 08:52:58 +00:00
|
|
|
"solve_operations_network_myopic",
|
2022-08-01 09:55:31 +00:00
|
|
|
capacity_year=1952,
|
2023-04-30 08:52:58 +00:00
|
|
|
simpl="",
|
|
|
|
opts="",
|
2022-08-01 09:55:31 +00:00
|
|
|
clusters=37,
|
|
|
|
lv=2.0,
|
2023-04-30 08:52:58 +00:00
|
|
|
sector_opts="Co2L0-25H-T-H-B-I-A",
|
2022-08-01 09:55:31 +00:00
|
|
|
planning_horizons=2030,
|
2023-04-30 08:52:58 +00:00
|
|
|
weather_year=2013,
|
2022-08-01 09:55:31 +00:00
|
|
|
)
|
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
filename=snakemake.log.python, level=snakemake.config["logging_level"]
|
|
|
|
)
|
2022-08-01 09:55:31 +00:00
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
tmpdir = snakemake.config["solving"].get("tmpdir")
|
2022-08-01 09:55:31 +00:00
|
|
|
if tmpdir is not None:
|
|
|
|
from pathlib import Path
|
2023-04-30 08:52:58 +00:00
|
|
|
|
2022-08-01 09:55:31 +00:00
|
|
|
Path(tmpdir).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
config = snakemake.config["operations"]
|
|
|
|
overrides = override_component_attrs(snakemake.input.overrides)
|
|
|
|
|
|
|
|
n = pypsa.Network(snakemake.input.pre, override_component_attrs=overrides)
|
|
|
|
|
|
|
|
n_post = pypsa.Network(snakemake.input.post, override_component_attrs=overrides)
|
|
|
|
n = set_parameters_from_optimized(n, n_post)
|
|
|
|
del n_post
|
2023-04-30 08:52:58 +00:00
|
|
|
|
|
|
|
n_previous = pypsa.Network(
|
|
|
|
snakemake.input.previous, override_component_attrs=overrides
|
|
|
|
)
|
2022-08-01 09:55:31 +00:00
|
|
|
store_soc = n_previous.stores_t.e.iloc[-1]
|
|
|
|
storage_unit_soc = n_previous.storage_units_t.state_of_charge.iloc[-1]
|
|
|
|
del n_previous
|
|
|
|
|
|
|
|
n = remove_unused_components(n)
|
|
|
|
n = add_load_shedding(n)
|
|
|
|
n = prepare_myopic(n, config, store_soc, storage_unit_soc)
|
|
|
|
|
2023-04-30 08:52:58 +00:00
|
|
|
opts = snakemake.wildcards.sector_opts.split("-")
|
|
|
|
solve_opts = snakemake.config["solving"]["options"]
|
|
|
|
solve_opts["skip_iterations"] = True
|
2022-08-01 09:55:31 +00:00
|
|
|
|
|
|
|
n = prepare_network(n, solve_opts)
|
2023-04-30 08:52:58 +00:00
|
|
|
|
2022-08-01 09:55:31 +00:00
|
|
|
n = solve_network_myopic(
|
|
|
|
n,
|
|
|
|
config=snakemake.config,
|
|
|
|
opts=opts,
|
|
|
|
solver_dir=tmpdir,
|
2023-04-30 08:52:58 +00:00
|
|
|
solver_logfile=snakemake.log.solver,
|
2022-08-01 09:55:31 +00:00
|
|
|
)
|
2023-04-30 08:52:58 +00:00
|
|
|
|
2022-08-01 09:55:31 +00:00
|
|
|
n.export_to_netcdf(snakemake.output[0])
|