Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 69 additions & 24 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ scenario_groups = {
"all-scenarios": [os.path.splitext(scenario)[0] for scenario in os.listdir("scenarios")]
}

sensitivities = {"A": ("2050-gas_lessCH4", "2050-gas_moreCH4", 2)}

wildcard_constraints:
single_scenarios="|".join([os.path.join("sensitivities/", item) for item in list(sensitivities.keys())] + ["scenarios"])

resource_plots = ['scal_conv_pp-capacity_net_el']


Expand All @@ -20,20 +25,20 @@ rule plot_all_resources:
rule plot_all_examples:
input:
expand(
"results/{scenario}/plotted/{plot_type}",
scenario=scenario_groups["examples"],
"results/scenarios/{example}/plotted/{plot_type}",
example=scenario_groups["examples"],
plot_type=["scalars", "dispatch"],
)

rule process_all_scenarios:
input:
plots=expand(
"results/{scenario}/plotted/{plot_type}",
"results/scenarios/{scenario}/plotted/{plot_type}",
scenario=scenario_groups["all-scenarios"],
plot_type=["scalars", "dispatch"],
),
tables=expand(
"results/{scenario}/tables",
"results/scenarios/{scenario}/tables",
scenario=scenario_groups["all-scenarios"],
)

Expand Down Expand Up @@ -63,12 +68,12 @@ rule create_input_data_overview:

rule prepare_example:
input:
"examples/{scenario}/preprocessed/"
"examples/{example}/preprocessed/"
output:
directory("results/{scenario}/preprocessed")
directory("results/scenarios/{example}/preprocessed")
wildcard_constraints:
# necessary to distinguish from those scenarios that are not pre-fabricated
scenario="|".join(scenario_groups["examples"])
example="|".join(scenario_groups["examples"])
run:
import shutil
shutil.copytree(src=input[0], dst=output[0])
Expand Down Expand Up @@ -173,37 +178,37 @@ rule build_datapackage:
get_paths_scenario_input,
scenario="scenarios/{scenario}.yml"
output:
directory("results/{scenario}/preprocessed")
directory("results/scenarios/{scenario}/preprocessed")
params:
logfile="logs/{scenario}.log"
shell:
"python scripts/build_datapackage.py {input.scenario} {output} {params.logfile}"

rule optimize:
input:
"results/{scenario}/preprocessed"
"results/{single_scenarios}/{scenario}/preprocessed"
output:
directory("results/{scenario}/optimized/")
directory("results/{single_scenarios}/{scenario}/optimized/")
params:
logfile="logs/{scenario}.log"
shell:
"python scripts/optimize.py {input} {output} {params.logfile}"

rule postprocess:
input:
"results/{scenario}/optimized"
"results/{single_scenarios}/{scenario}/optimized"
output:
directory("results/{scenario}/postprocessed/")
directory("results/{single_scenarios}/{scenario}/postprocessed/")
params:
logfile="logs/{scenario}.log"
shell:
"python scripts/postprocess.py {input} {wildcards.scenario} {output} {params.logfile}"

rule create_results_table:
input:
"results/{scenario}/postprocessed/"
"results/{single_scenarios}/{scenario}/postprocessed/"
output:
directory("results/{scenario}/tables/")
directory("results/{single_scenarios}/{scenario}/tables/")
params:
logfile="logs/{scenario}.log"
shell:
Expand All @@ -221,9 +226,9 @@ rule create_joined_results_table:

rule plot_dispatch:
input:
"results/{scenario}/postprocessed/"
"results/{single_scenarios}/{scenario}/postprocessed/"
output:
directory("results/{scenario}/plotted/dispatch")
directory("results/{single_scenarios}/{scenario}/plotted/dispatch")
params:
logfile="logs/{scenario}.log"
shell:
Expand All @@ -239,35 +244,37 @@ rule plot_conv_pp_scalars:

rule plot_scalar_results:
input:
"results/{scenario}/postprocessed/"
"results/{single_scenarios}/{scenario}/postprocessed/"
output:
directory("results/{scenario}/plotted/scalars/")
directory("results/{single_scenarios}/{scenario}/plotted/scalars/")
params:
logfile="logs/{scenario}.log"
shell:
"python scripts/plot_scalar_results.py {input} {output} {params.logfile}"

rule plot_joined_scalars:
input:
"results/joined_scenarios/{scenario_group}/joined/"
"results/{joined_scenarios}/{scenario_group}/joined/"
output:
directory("results/joined_scenarios/{scenario_group}/joined_plotted/")
directory("results/{joined_scenarios}/{scenario_group}/joined_plotted/")
params:
logfile="logs/{scenario_group}.log"
wildcard_constraints:
joined_scenarios="joined_scenarios|sensitivities"
shell:
"python scripts/plot_scalar_results.py {input} {output} {params.logfile}"

rule report:
input:
template="report/report.md",
template_interactive="report/report_interactive.md",
plots_scalars="results/{scenario}/plotted/scalars",
plots_dispatch="results/{scenario}/plotted/dispatch",
plots_scalars="results/{single_scenarios}/{scenario}/plotted/scalars",
plots_dispatch="results/{single_scenarios}/{scenario}/plotted/dispatch",
output:
directory("results/{scenario}/report/")
directory("results/{single_scenarios}/{scenario}/report/")
params:
logfile="logs/{scenario}.log",
all_plots="results/{scenario}/plotted/",
all_plots="results/{single_scenarios}/{scenario}/plotted/",
run:
import os
import shutil
Expand Down Expand Up @@ -319,3 +326,41 @@ rule join_scenario_results:
directory("results/joined_scenarios/{scenario_group}/joined/")
shell:
"python scripts/join_scenarios.py {input} {output}"

def extend_scenario_groups(wildcards):
lb, ub, n = sensitivities[wildcards.sensitivity]
return [os.path.join("results", "scenarios", scenario, "preprocessed") for scenario in [lb, ub]]

def get_n(wildcards):
n = sensitivities[wildcards.sensitivity][2]
return n

rule build_sensitivity:
input:
extend_scenario_groups
output:
directory("results/sensitivities/{sensitivity}")
wildcard_constraints:
sensitivity="|".join(sensitivities.keys())
params:
n=get_n,
logfile="logs/{sensitivity}.log",
shell:
"python scripts/build_sensitivity.py {input[0]} {input[1]} {output} {params.n} {params.logfile}"


def get_sample_of_sensitivity(wildcards):
return [
os.path.join("results", "sensitivities", wildcards.sensitivity, sample, "postprocessed")
for sample in os.listdir(os.path.join("results", "sensitivities", wildcards.sensitivity))
if not (sample == ".snakemake_timestamp" or sample == "joined" or sample == "joined_plotted")
]


rule join_sensitivity_results:
input:
get_sample_of_sensitivity
output:
directory("results/sensitivities/{sensitivity}/joined/")
shell:
"python scripts/join_scenarios.py {input} {output}"
84 changes: 84 additions & 0 deletions scripts/build_sensitivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# coding: utf-8
r"""
Inputs
-------
scenario_specs : str
``scenarios/{scenario}.yml``: path of input file (.yml) containing scenario specifications
destination : str
``results/{scenario}/preprocessed``: path of output directory
logfile : str
``logs/{scenario}.log``: path to logfile

Outputs
---------
oemoflex.EnergyDatapackage
EnergyDatapackage that can be read by oemof.tabular, with data (scalars and timeseries)
as csv and metadata (describing resources and foreign key relations) as json.

Description
-------------
The script creates an empty EnergyDatapackage from the specifications given in the scenario_specs,
fills it with scalar and timeseries data, infers the metadata and saves it to the given destination.
Further, additional parameters like emission limit are saved in a separate file.
"""
import os
import sys
from oemof_b3.config import config
from oemoflex.model.datapackage import EnergyDataPackage
from oemoflex.model.variations import EDPSensitivity
from oemof_b3.model import foreign_keys_update
import oemof_b3.tools.data_processing as dp
from pandas.testing import assert_frame_equal

if __name__ == "__main__":
path_lb = sys.argv[1]

path_ub = sys.argv[2]

destination = sys.argv[3]

n = int(sys.argv[4])

logfile = sys.argv[5]

logger = config.add_snake_logger(logfile, "build_linear_slide")

if not os.path.exists(destination):
os.mkdir(destination)

lb = EnergyDataPackage.from_metadata(os.path.join(path_lb, "datapackage.json"))
ub = EnergyDataPackage.from_metadata(os.path.join(path_ub, "datapackage.json"))

# load additional scalars and make sure that they are the same
# TODO: Currently, varying the parameters for the constraints is not supported.
# As soon as the additional scalars representing for contraints are part of the
# datapackage, this step can be removed.
additional_scalars_lb = dp.load_b3_scalars(
os.path.join(path_lb, "additional_scalars.csv")
)
additional_scalars_ub = dp.load_b3_scalars(
os.path.join(path_ub, "additional_scalars.csv")
)
assert_frame_equal(additional_scalars_lb, additional_scalars_ub)

lb.stack_components()
ub.stack_components()

sensitivity = EDPSensitivity(lb, ub)

logger.info(f"Creating {n} samples.")
samples = sensitivity.get_linear_slide(n)

for n_sample, sample in samples.items():

sample.unstack_components()

path = os.path.join(destination, str(n_sample), "preprocessed")

sample.basepath = path

sample.to_csv_dir(path)

dp.save_df(additional_scalars_lb, os.path.join(path, "additional_scalars.csv"))

sample.infer_metadata(foreign_keys_update)