-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.py
145 lines (111 loc) · 4.15 KB
/
compute.py
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
""" compute.py
This script loads datapackages in the datapackages/ folder,
construct an oemof-model for each, solves the mathematical model
to minimum-cost optimality and writes back the results to ~/results.
TODO: This should be a command-line-interface program with
potentially the following options.
--solver
--clean results-folder
--cpu-cores
--debug lp file
--scenarios list
"""
import os
import json
import oemof.outputlib as outputlib
import multiprocessing as mp
from oemof.tabular import facades
from datetime import datetime
from oemof.tabular.datapackage import processing
from oemof.solph import EnergySystem, Model, Bus
from oemof.tabular.tools import postprocessing as pp
import pyomo.core as po
import pandas as pd
def compute(pk):
base_path = os.path.join(
results, pk + '-' + timestamp)
input_path = os.path.join('datapackages', pk)
output_path = os.path.join(base_path, 'output')
os.mkdir(base_path)
os.mkdir(output_path)
processing.copy_datapackage(
os.path.join(input_path, 'datapackage.json'),
os.path.join(base_path, 'input'))
es = EnergySystem.from_datapackage(
os.path.join(input_path, 'datapackage.json'),
attributemap={},
typemap=facades.TYPEMAP,
)
m = Model(es)
flows = {}
for (i, o) in m.flows:
if hasattr(m.flows[i, o], 'emission_factor'):
flows[(i, o)] = m.flows[i, o]
# emissions by country
for node in es.nodes:
if isinstance(node, Bus):
expr = sum(
m.flow[inflow, outflow, t] * m.timeincrement[t] *
flows[inflow, outflow].emission_factor
for (inflow, outflow) in flows
for t in m.TIMESTEPS if outflow.label == node.label)
setattr(
m, node.label.split('-')[0] + '_emissions',
po.Expression(expr=expr)
)
m.total_emissions = po.Expression(
expr=sum(m.flow[inflow, outflow, t] * m.timeincrement[t] *
flows[inflow, outflow].emission_factor
for (inflow, outflow) in flows
for t in m.TIMESTEPS))
m.receive_duals()
m.solve('cbc')
m.results = m.results()
pp.write_results(m, output_path, scalars=False, types=[
"dispatchable", "volatile", "storage", "reservoir"])
modelstats = outputlib.processing.meta_results(m)
modelstats.pop("solver")
modelstats["problem"].pop("Sense")
modelstats["total-emissions"] = m.total_emissions()
# store emissions by country
for node in es.nodes:
if isinstance(node, Bus):
name = node.label.split('-')[0] + '_emissions'
modelstats[name] = getattr(m, name)()
with open(os.path.join(base_path, "modelstats.json"), "w") as outfile:
json.dump(modelstats, outfile, indent=4)
supply_sum = (
pp.supply_results(
results=m.results,
es=m.es,
bus=[b.label for b in es.nodes if isinstance(b, Bus)],
types=[
"dispatchable",
"volatile",
"reservoir",
],
)
.sum()
.reset_index()
)
supply_sum['from'] = supply_sum['from'].apply(lambda i: '-'.join(i.label.split("-")[1:3:]))
supply_sum = supply_sum.groupby(['from', 'to', 'type']).sum().reset_index()
supply_sum.drop("type", axis=1, inplace=True)
supply_sum = (
supply_sum.set_index(["from", "to"]).unstack("from")
/ 1e6
)
supply_sum.columns = supply_sum.columns.droplevel(0)
summary = supply_sum
summary.to_csv(os.path.join(base_path, 'summary.csv'))
return (pk, modelstats['objective'], m.total_emissions())
packages = ['2' + i for i in list("ABCDEFG")] + ['SQ'] + ['3B', '3C', '3D', '3F'] + ['4B', '4C']
results = os.path.expanduser('~/results')
if not os.path.exists(results):
os.mkdir(results)
timestamp = str(datetime.now().strftime("%Y-%m-%d-%H-%M")).replace(':', '-').replace(' ', '-')
p = mp.Pool(7)
container = p.map(compute, packages)
pd.DataFrame(
container, columns=['datapackage', 'objective', 'emissions']
).to_csv(os.path.join(results, 'summary' + '-' + timestamp + '.csv'), index=False)