Skip to content

Commit

Permalink
Merge branch 'master' into shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyxng authored Jan 17, 2025
2 parents ebebb46 + b6b18ad commit 1664695
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 40 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ install-pinned-macos: _conda_check
# Run default tests
test:
set -e
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.overnight.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.myopic.yaml --rerun-triggers=mtime
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime
snakemake --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml
snakemake --configfile config/test/config.overnight.yaml
snakemake --configfile config/test/config.myopic.yaml
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml
snakemake --configfile config/test/config.scenarios.yaml -n
echo "All tests completed successfully."

unit-test:
pytest test

# Cleans all output files from tests
clean-tests:
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.overnight.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.myopic.yaml --rerun-triggers=mtime --delete-all-output
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime --delete-all-output
snakemake --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n --delete-all-output
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --delete-all-output
snakemake --configfile config/test/config.overnight.yaml --delete-all-output
snakemake --configfile config/test/config.myopic.yaml --delete-all-output
snakemake make_summary_perfect --configfile config/test/config.perfect.yaml --delete-all-output
snakemake --configfile config/test/config.scenarios.yaml -n --delete-all-output

# Removes all created files except for large cutout files (similar to fresh clone)
reset:
Expand Down
6 changes: 6 additions & 0 deletions config/config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,12 @@ solving:
cbc-default: {} # Used in CI
glpk-default: {} # Used in CI

check_objective:
enable: false
expected_value: None
atol: 1_000_000
rtol: 0.01

mem_mb: 30000 #memory in MB; 20 GB enough for 50+B+I+H2; 100 GB for 181+B+I+H2
runtime: 6h #runtime in humanfriendly style https://humanfriendly.readthedocs.io/en/latest/

Expand Down
3 changes: 3 additions & 0 deletions config/test/config.electricity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ solving:
name: highs
options: highs-default

check_objective:
enable: true
expected_value: 3.8120188094e+07

plotting:
map:
Expand Down
4 changes: 4 additions & 0 deletions config/test/config.overnight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ solving:
options: highs-default
mem: 4000

check_objective:
enable: true
expected_value: 7.0847670388e+08

plotting:
map:
boundaries:
Expand Down
4 changes: 4 additions & 0 deletions config/test/config.perfect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ solving:
options: highs-default
mem: 4000

check_objective:
enable: true
expected_value: 1.4427662256e+10

plotting:
map:
boundaries:
Expand Down
25 changes: 14 additions & 11 deletions rules/retrieve.smk
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,15 @@ if config["enable"]["retrieve"] and (
config["electricity"]["base_network"] == "osm-prebuilt"
):
OSM_VERSION = config["electricity"]["osm-prebuilt-version"]
OSM_COMPONENTS = ["buses", "converters", "lines", "links", "transformers"]
OSM_FILES = [
"buses.csv",
"converters.csv",
"lines.csv",
"links.csv",
"transformers.csv",
]
if OSM_VERSION >= 0.6:
OSM_COMPONENTS.append("map")
OSM_FILES.append("map.html")
OSM_ZENODO_IDS = {
0.1: "12799202",
0.2: "13342577",
Expand All @@ -561,17 +567,14 @@ if config["enable"]["retrieve"] and (
# update rule to use the correct version
rule retrieve_osm_prebuilt:
input:
[
storage(
f"https://zenodo.org/records/{OSM_ZENODO_IDS[OSM_VERSION]}/files/{component}.csv"
**{
file: storage(
f"https://zenodo.org/records/{OSM_ZENODO_IDS[OSM_VERSION]}/files/{file}"
)
for component in OSM_COMPONENTS
],
for file in OSM_FILES
},
output:
[
f"data/osm-prebuilt/{OSM_VERSION}/{component}.csv"
for component in OSM_COMPONENTS
],
**{file: f"data/osm-prebuilt/{OSM_VERSION}/{file}" for file in OSM_FILES},
log:
"logs/retrieve_osm_prebuilt.log",
threads: 1
Expand Down
28 changes: 13 additions & 15 deletions scripts/make_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,24 +542,22 @@ def calculate_weighted_prices(n, label, weighted_prices):
carriers = n.buses.carrier.unique()

for carrier in carriers:
load = (
n.statistics.withdrawal(
groupby=pypsa.statistics.groupers["bus", "carrier"],
aggregate_time=False,
nice_names=False,
bus_carrier=carrier,
)
.groupby(level="bus")
.sum()
.T.fillna(0)
load = n.statistics.withdrawal(
groupby=pypsa.statistics.groupers["bus", "carrier"],
aggregate_time=False,
nice_names=False,
bus_carrier=carrier,
)

price = n.buses_t.marginal_price.loc[:, n.buses.carrier == carrier]
price = price.reindex(columns=load.columns, fill_value=1)
if not load.empty and load.sum().sum() > 0:
load = load.groupby(level="bus").sum().T.fillna(0)

price = n.buses_t.marginal_price.loc[:, n.buses.carrier == carrier]
price = price.reindex(columns=load.columns, fill_value=1)

weighted_prices.loc[carrier, label] = (
load * price
).sum().sum() / load.sum().sum()
weighted_prices.loc[carrier, label] = (
load * price
).sum().sum() / load.sum().sum()

return weighted_prices

Expand Down
28 changes: 24 additions & 4 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
pypsa.pf.logger.setLevel(logging.WARNING)


class ObjectiveValueError(Exception):
pass


def add_land_use_constraint_perfect(n):
"""
Add global constraints for tech capacity limit.
Expand Down Expand Up @@ -986,6 +990,19 @@ def extra_functionality(n, snapshots):
custom_extra_functionality(n, snapshots, snakemake)


def check_objective_value(n, solving):
check_objective = solving["check_objective"]
if check_objective["enable"]:
atol = check_objective["atol"]
rtol = check_objective["rtol"]
expected_value = check_objective["expected_value"]
if not np.isclose(n.objective, expected_value, atol=atol, rtol=rtol):
raise ObjectiveValueError(
f"Objective value {n.objective} differs from expected value "
f"{expected_value} by more than {atol}."
)


def solve_network(n, config, params, solving, **kwargs):
set_of_options = solving["solver"]["options"]
cf_solving = solving["options"]
Expand Down Expand Up @@ -1034,10 +1051,13 @@ def solve_network(n, config, params, solving, **kwargs):
**kwargs
)

if status != "ok" and not rolling_horizon:
logger.warning(
f"Solving status '{status}' with termination condition '{condition}'"
)
if not rolling_horizon:
if status != "ok":
logger.warning(
f"Solving status '{status}' with termination condition '{condition}'"
)
check_objective_value(n, solving)

if "infeasible" in condition:
labels = n.model.compute_infeasibilities()
logger.info(f"Labels:\n{labels}")
Expand Down

0 comments on commit 1664695

Please sign in to comment.