-
Notifications
You must be signed in to change notification settings - Fork 18
/
Snakefile
305 lines (272 loc) · 11.5 KB
/
Snakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import glob
from pathlib import Path
from snakemake.utils import validate, min_version, makedirs
configfile: "config/default.yaml"
validate(config, "config/schema.yaml")
root_dir = config["root-directory"] + "/" if config["root-directory"] not in ["", "."] else ""
__version__ = open(f"{root_dir}VERSION").readlines()[0].strip()
test_dir = f"{root_dir}tests/"
model_test_dir = f"{test_dir}model"
resources_test_dir = f"{test_dir}resources"
template_dir = f"{root_dir}templates/"
model_template_dir = f"{template_dir}models/"
techs_template_dir = f"{model_template_dir}techs/"
include: "./rules/shapes.smk"
include: "./rules/data.smk"
include: "./rules/jrc-idees.smk"
include: "./rules/wind-and-solar.smk"
include: "./rules/biofuels.smk"
include: "./rules/hydro.smk"
include: "./rules/transmission.smk"
include: "./rules/demand.smk"
include: "./rules/nuclear.smk"
include: "./rules/transport.smk"
include: "./rules/sync.smk"
include: "./rules/heat.smk"
include: "./rules/modules.smk"
min_version("8.10")
localrules: all, clean
wildcard_constraints:
resolution = "continental|national|regional|ehighways",
group_and_tech = "(demand|storage|supply|transmission)\/\w+"
ruleorder: module_with_location_specific_data > module_without_location_specific_data
ALL_CF_TECHNOLOGIES = [
"wind-onshore", "wind-offshore", "open-field-pv",
"rooftop-pv", "rooftop-pv-n", "rooftop-pv-e-w", "rooftop-pv-s-flat", "hydro-run-of-river",
"hydro-reservoir"
]
def ensure_lib_folder_is_linked():
if not (hasattr(workflow, "deployment_settings") and not
hasattr(workflow.deployment_settings, "conda_prefix")):
return
link = Path(workflow.deployment_settings.conda_prefix) / "lib"
if not link.exists():
# Link either does not exist or is an invalid symlink
print("Creating link from conda env dir to eurocalliopelib.")
if link.is_symlink(): # Deal with existing but invalid symlink
shell(f"rm {link}")
makedirs(workflow.deployment_settings.conda_prefix)
shell(f"ln -s {workflow.basedir}/lib {link}")
ensure_lib_folder_is_linked()
onstart:
shell("mkdir -p build/logs")
onsuccess:
if "email" in config.keys():
shell("echo "" | mail -s 'euro-calliope succeeded' {config[email]}")
onerror:
if "email" in config.keys():
shell("echo "" | mail -s 'euro-calliope failed' {config[email]}")
rule all:
message: "Generate euro-calliope pre-built models and run tests."
localrule: True
default_target: True
input:
"build/logs/continental/test.success",
"build/logs/national/test.success",
expand(
"build/models/{resolution}/{file}",
resolution=["continental", "national", "regional", "ehighways"],
file=["example-model.yaml", "build-metadata.yaml", "summary-of-potentials.nc", "summary-of-potentials.csv"]
)
rule all_tests:
message: "Generate euro-calliope pre-built models and run all tests."
input:
expand(
"build/logs/{resolution}/test.success",
resolution=["continental", "national", "regional", "ehighways"],
),
expand(
"build/models/{resolution}/{file}",
resolution=["continental", "national", "regional", "ehighways"],
file=["example-model.yaml", "build-metadata.yaml", "summary-of-potentials.nc", "summary-of-potentials.csv"]
)
rule module_with_location_specific_data:
message: "Create {wildcards.resolution} definition file for {wildcards.group_and_tech}."
input:
template = techs_template_dir + "{group_and_tech}.yaml.jinja",
locations = "build/data/{resolution}/{group_and_tech}.csv"
params:
scaling_factors = config["scaling-factors"],
capacity_factors = config["capacity-factors"]["average"],
max_power_densities = config["parameters"]["maximum-installable-power-density"],
heat_pump_shares = config["parameters"]["heat-pump"]["heat-pump-shares"],
biofuel_efficiency = config["parameters"]["biofuel-efficiency"],
wildcard_constraints:
# Exclude all outputs that have their own `techs_and_locations_template` implementation
group_and_tech = "(?!transmission\/electricity-|supply\/biofuel|supply\/electrified-biofuel).*"
conda: "envs/default.yaml"
output: "build/models/{resolution}/techs/{group_and_tech}.yaml"
script: "scripts/template_techs.py"
use rule module_with_location_specific_data as module_without_location_specific_data with:
# For all cases where we don't have any location-specific data that we want to supply to the template
input:
template = techs_template_dir + "{group_and_tech}.yaml.jinja",
locations = rules.locations_module.output.csv
rule module_without_specific_data:
message: "Create {wildcards.resolution} configuration files from templates where no parameterisation is required."
input:
template = model_template_dir + "{template}",
output: "build/models/{resolution}/{template}"
wildcard_constraints:
template = "interest-rate.yaml"
conda: "envs/shell.yaml"
shell: "cp {input.template} {output}"
rule auxiliary_files:
message: "Create auxiliary output files (i.e. those not used to define a Calliope model) from templates where no parameterisation is required."
input:
template = template_dir + "{template}",
output: "build/models/{template}"
wildcard_constraints:
template = "[^/]*"
conda: "envs/shell.yaml"
shell: "cp {input.template} {output}"
rule model:
message: "Generate top-level {wildcards.resolution} model configuration file from template"
input:
template = model_template_dir + "example-model.yaml.jinja",
auxiliary_files = expand(
"build/models/{template}", template=["environment.yaml", "README.md"]
),
modules = expand(
"build/models/{{resolution}}/{module}",
module=[
"interest-rate.yaml",
"locations.yaml",
"techs/demand/electricity.yaml",
"techs/demand/electrified-transport.yaml",
"techs/storage/electricity.yaml",
"techs/storage/hydro.yaml",
"techs/supply/hydro.yaml",
"techs/supply/load-shedding.yaml",
"techs/supply/open-field-solar-and-wind-onshore.yaml",
"techs/supply/rooftop-solar.yaml",
"techs/supply/wind-offshore.yaml",
"techs/supply/nuclear.yaml",
]
),
heat_timeseries_data = (
"build/models/{resolution}/timeseries/conversion/heat-pump-cop.csv",
"build/models/{resolution}/timeseries/supply/historic-electrified-heat.csv",
),
capacityfactor_timeseries_data = expand(
"build/models/{{resolution}}/timeseries/supply/capacityfactors-{technology}.csv",
technology=ALL_CF_TECHNOLOGIES
),
demand_timeseries_data = expand(
"build/models/{{resolution}}/timeseries/demand/{filename}",
filename=[
"electricity.csv",
"uncontrolled-electrified-road-transport.csv",
"uncontrolled-road-transport-historic-electrification.csv",
"demand-shape-min-ev.csv",
"demand-shape-max-ev.csv",
"demand-shape-equals-ev.csv",
"plugin-profiles-ev.csv",
"heat.csv",
"electrified-heat.csv",
]
),
optional_transmission_modules = lambda wildcards: expand(
f"build/models/{wildcards.resolution}/{{module}}",
module=[
"techs/transmission/electricity-linked-neighbours.yaml",
] + ["techs/transmission/electricity-entsoe.yaml" for i in [None] if wildcards.resolution == "national"]
),
optional_heat_modules = expand(
"build/models/{{resolution}}/techs/{module}",
module=[
"demand/heat.yaml",
"demand/electrified-heat.yaml",
"storage/heat.yaml",
"conversion/heat-from-electricity.yaml",
"conversion/heat-from-biofuel.yaml",
"conversion/heat-from-methane.yaml",
"supply/historic-electrified-heat.yaml"
]
),
optional_biofuel_modules = expand(
"build/models/{{resolution}}/techs/{module}",
module=[
"supply/biofuel.yaml",
"supply/electrified-biofuel.yaml",
"conversion/electricity-from-biofuel.yaml"
]
),
optional_synfuel_modules = expand(
"build/models/{{resolution}}/techs/{module}",
module=[
"conversion/synfuels-from-hydrogen.yaml",
"conversion/synfuels-from-biofuel.yaml",
"conversion/hydrogen-from-electricity.yaml",
]
)
params:
year = config["scope"]["temporal"]["first-year"]
conda: "envs/default.yaml"
output: "build/models/{resolution}/example-model.yaml"
script: "scripts/template_model.py"
rule build_metadata:
message: "Generate build metadata."
input:
"build/models/{resolution}/example-model.yaml",
params:
config = config,
version = __version__
output: "build/models/{resolution}/build-metadata.yaml"
conda: "envs/default.yaml"
script: "scripts/metadata.py"
rule dag_dot:
output: temp("build/dag.dot")
shell:
"snakemake --rulegraph > {output}"
rule dag:
message: "Plot dependency graph of the workflow."
input: rules.dag_dot.output[0]
# Output is deliberatly omitted so rule is executed each time.
conda: "envs/dag.yaml"
shell:
"dot -Tpdf {input} -o build/dag.pdf"
rule clean: # removes all generated results
localrule: True
shell:
"""
rm -r build/
echo "Data downloaded to data/automatic/ has not been cleaned."
"""
rule test:
message: "Run tests"
input:
test_dir = model_test_dir,
test_resources_dir = resources_test_dir,
tests = map(str, Path(model_test_dir).glob("**/test_*.py")),
example_model = "build/models/{resolution}/example-model.yaml",
capacity_factor_timeseries = expand(
"build/models/{{resolution}}/timeseries/supply/capacityfactors-{technology}.csv",
technology=ALL_CF_TECHNOLOGIES
),
electrified_heat_demand = "build/models/{resolution}/timeseries/demand/electrified-heat.csv",
heat_demand = "build/models/{resolution}/timeseries/demand/heat.csv",
historic_electrified_heat = "build/models/{resolution}/timeseries/supply/historic-electrified-heat.csv",
cop = "build/models/{resolution}/timeseries/conversion/heat-pump-cop.csv"
params:
config = config,
test_args = [] # add e.g. "--pdb" to enter ipdb on test failure
log: "build/logs/{resolution}/test-report.html"
output: "build/logs/{resolution}/test.success"
conda: "./envs/test.yaml"
resources:
runtime = 240
script: "./tests/model/test_runner.py"
rule summarise_potentials:
message: "Generates netcdf and csv file with potentials for each technology."
input:
path_to_model = "build/models/{resolution}/example-model.yaml"
output:
netcdf = "build/models/{resolution}/summary-of-potentials.nc",
csv = "build/models/{resolution}/summary-of-potentials.csv"
params:
scaling_factors = config["scaling-factors"]
conda:
"./envs/test.yaml"
script:
"./scripts/summarise_potentials.py"