2022-09-16 13:04:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-02-16 10:50:55 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2017-2023 The PyPSA-Eur Authors
|
2020-05-29 07:50:55 +00:00
|
|
|
#
|
2021-09-14 14:37:41 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
2022-09-16 13:04:04 +00:00
|
|
|
Solves linear optimal dispatch in hourly resolution using the capacities of
|
|
|
|
previous capacity expansion in rule :mod:`solve_network`.
|
2019-08-08 13:02:28 +00:00
|
|
|
"""
|
|
|
|
|
2019-11-28 07:22:52 +00:00
|
|
|
import logging
|
|
|
|
|
2018-03-13 09:47:47 +00:00
|
|
|
import numpy as np
|
2022-09-16 13:04:04 +00:00
|
|
|
import pypsa
|
2023-03-09 21:51:56 +00:00
|
|
|
from _helpers import (
|
|
|
|
configure_logging,
|
|
|
|
override_component_attrs,
|
|
|
|
update_config_with_sector_opts,
|
|
|
|
)
|
2022-09-16 13:04:04 +00:00
|
|
|
from solve_network import prepare_network, solve_network
|
2018-03-13 09:47:47 +00:00
|
|
|
from vresutils.benchmark import memory_logger
|
2018-02-19 09:17:49 +00:00
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-02-19 09:17:49 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-09-16 13:04:04 +00:00
|
|
|
if "snakemake" not in globals():
|
2019-12-09 20:29:15 +00:00
|
|
|
from _helpers import mock_snakemake
|
2018-02-19 09:17:49 +00:00
|
|
|
|
2022-09-16 13:04:04 +00:00
|
|
|
snakemake = mock_snakemake(
|
|
|
|
"solve_operations_network",
|
2023-03-15 16:00:06 +00:00
|
|
|
configfiles="test/config.electricity.yaml",
|
2023-04-29 10:40:55 +00:00
|
|
|
weather_year="",
|
2022-09-16 13:04:04 +00:00
|
|
|
simpl="",
|
2023-03-09 21:51:56 +00:00
|
|
|
opts="",
|
2022-09-16 13:04:04 +00:00
|
|
|
clusters="5",
|
2023-03-09 21:51:56 +00:00
|
|
|
ll="v1.5",
|
|
|
|
sector_opts="",
|
|
|
|
planning_horizons="",
|
2022-09-16 13:04:04 +00:00
|
|
|
)
|
2018-02-19 09:17:49 +00:00
|
|
|
|
2023-03-09 21:51:56 +00:00
|
|
|
configure_logging(snakemake)
|
|
|
|
update_config_with_sector_opts(snakemake.config, snakemake.wildcards.sector_opts)
|
2018-02-19 09:17:49 +00:00
|
|
|
|
2023-03-09 21:51:56 +00:00
|
|
|
opts = (snakemake.wildcards.opts + "-" + snakemake.wildcards.sector_opts).split("-")
|
|
|
|
opts = [o for o in opts if o != ""]
|
|
|
|
solve_opts = snakemake.config["solving"]["options"]
|
2018-02-19 09:17:49 +00:00
|
|
|
|
2023-03-09 21:51:56 +00:00
|
|
|
np.random.seed(solve_opts.get("seed", 123))
|
2022-09-16 13:04:04 +00:00
|
|
|
|
|
|
|
fn = getattr(snakemake.log, "memory", None)
|
|
|
|
with memory_logger(filename=fn, interval=30.0) as mem:
|
2023-03-09 21:51:56 +00:00
|
|
|
if "overrides" in snakemake.input:
|
|
|
|
overrides = override_component_attrs(snakemake.input.overrides)
|
|
|
|
n = pypsa.Network(
|
|
|
|
snakemake.input.network, override_component_attrs=overrides
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
n = pypsa.Network(snakemake.input.network)
|
|
|
|
|
|
|
|
n.optimize.fix_optimal_capacities()
|
|
|
|
n = prepare_network(n, solve_opts, config=snakemake.config)
|
2022-09-16 13:04:04 +00:00
|
|
|
n = solve_network(
|
2023-03-09 21:51:56 +00:00
|
|
|
n, config=snakemake.config, opts=opts, log_fn=snakemake.log.solver
|
2022-09-16 13:04:04 +00:00
|
|
|
)
|
2023-03-09 21:51:56 +00:00
|
|
|
|
2022-06-30 06:39:03 +00:00
|
|
|
n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
|
2018-02-19 09:17:49 +00:00
|
|
|
n.export_to_netcdf(snakemake.output[0])
|
|
|
|
|
2018-03-13 09:47:47 +00:00
|
|
|
logger.info("Maximum memory usage: {}".format(mem.mem_usage))
|