Passed
Pull Request — dev (#850)
by Uwe
01:35
created

example_generic_invest   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 82
dl 0
loc 191
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
r"""
3
Example that shows how to use "Generic Investment Limit".
4
5
There are two supply chains. The energy systems looks like that:
6
7
.. code-block:: text
8
9
                  bus_a_0          bus_a_1
10
                   |                 |
11
    source_a_0 --->|---> trafo_a --->|--->demand_a
12
                                     |
13
                       source_a_1--->|
14
                                     |
15
16
                  bus_b_0          bus_b_1
17
                   |                 |
18
    source_b_0 --->|---> trafo_b --->|--->demand_b
19
                                     |
20
                       source_b_1--->|
21
                                     |
22
23
Everything is identical - the costs for the sources, the demand, the efficiency
24
of the Transformer. And both Transformer have an investment at the output.
25
The source '\*_1' is in both cases very expensive, so that
26
a investment is probably done in the transformer.
27
Now, both investments share a third resource, which is called "space" in this
28
example. (This could be anything, and you could use as many additional
29
resources as you want.) And this resource is limited. In this case, every
30
Transformer capacity unit, which might be installed, needs 2 space for
31
'trafo a', and 1 space per installed capacity for 'trafo b'.
32
And the total space is limited to 24.
33
See what happens, have fun ;)
34
35
Installation requirements
36
-------------------------
37
This example requires oemof.solph (v0.5.x), install by:
38
39
    pip install oemof.solph[examples]
40
41
License
42
-------
43
Johannes Röder <[email protected]>
44
45
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
46
47
48
"""
49
50
import logging
51
import os
52
53
import matplotlib.pyplot as plt
54
55
from oemof import solph
56
57
data = [0, 15, 30, 35, 20, 25, 27, 10, 5, 2, 15, 40, 20, 0, 0]
58
59
# create an energy system
60
idx = solph.create_time_index(2020, number=len(data))
61
es = solph.EnergySystem(timeindex=idx, infer_last_interval=False)
62
63
# Parameter: costs for the sources
64
c_0 = 10
65
c_1 = 100
66
67
epc_invest = 500
68
69
# commodity a
70
bus_a_0 = solph.Bus(label="bus_a_0")
71
bus_a_1 = solph.Bus(label="bus_a_1")
72
es.add(bus_a_0, bus_a_1)
73
74
es.add(
75
    solph.components.Source(
76
        label="source_a_0", outputs={bus_a_0: solph.Flow(variable_costs=c_0)}
77
    )
78
)
79
80
es.add(
81
    solph.components.Source(
82
        label="source_a_1", outputs={bus_a_1: solph.Flow(variable_costs=c_1)}
83
    )
84
)
85
86
es.add(
87
    solph.components.Sink(
88
        label="demand_a",
89
        inputs={bus_a_1: solph.Flow(fix=data, nominal_value=1)},
90
    )
91
)
92
93
# commodity b
94
bus_b_0 = solph.Bus(label="bus_b_0")
95
bus_b_1 = solph.Bus(label="bus_b_1")
96
es.add(bus_b_0, bus_b_1)
97
es.add(
98
    solph.components.Source(
99
        label="source_b_0", outputs={bus_b_0: solph.Flow(variable_costs=c_0)}
100
    )
101
)
102
103
es.add(
104
    solph.components.Source(
105
        label="source_b_1", outputs={bus_b_1: solph.Flow(variable_costs=c_1)}
106
    )
107
)
108
109
es.add(
110
    solph.components.Sink(
111
        label="demand_b",
112
        inputs={bus_b_1: solph.Flow(fix=data, nominal_value=1)},
113
    )
114
)
115
116
# transformer a
117
es.add(
118
    solph.components.Transformer(
119
        label="trafo_a",
120
        inputs={bus_a_0: solph.Flow()},
121
        outputs={
122
            bus_a_1: solph.Flow(
123
                nominal_value=None,
124
                investment=solph.Investment(
125
                    ep_costs=epc_invest,
126
                    space=2,
127
                ),
128
            )
129
        },
130
        conversion_factors={bus_a_1: 0.8},
131
    )
132
)
133
134
# transformer b
135
es.add(
136
    solph.components.Transformer(
137
        label="trafo_b",
138
        inputs={bus_b_0: solph.Flow()},
139
        outputs={
140
            bus_b_1: solph.Flow(
141
                nominal_value=None,
142
                investment=solph.Investment(
143
                    ep_costs=epc_invest,
144
                    space=1,
145
                ),
146
            )
147
        },
148
        conversion_factors={bus_a_1: 0.8},
149
    )
150
)
151
152
153
# create an optimization problem and solve it
154
om = solph.Model(es)
155
156
# add constraint for generic investment limit
157
om = solph.constraints.additional_investment_flow_limit(om, "space", limit=24)
158
159
# export lp file
160
filename = os.path.join(
161
    solph.helpers.extend_basic_path("lp_files"), "GenericInvest.lp"
162
)
163
logging.info("Store lp-file in {0}.".format(filename))
164
om.write(filename, io_options={"symbolic_solver_labels": True})
165
166
# solve model
167
om.solve(solver="cbc", solve_kwargs={"tee": True})
168
169
# create result object
170
results = solph.processing.results(om)
171
172
bus1 = solph.views.node(results, "bus_a_1")["sequences"]
173
bus2 = solph.views.node(results, "bus_b_1")["sequences"]
174
175
# plot the time series (sequences) of a specific component/bus
176
if plt is not None:
177
    bus1.plot(kind="line", drawstyle="steps-mid")
178
    plt.legend()
179
    plt.show()
180
    bus2.plot(kind="line", drawstyle="steps-mid")
181
    plt.legend()
182
    plt.show()
183
184
space_used = om.invest_limit_space()
185
print("Space value: ", space_used)
186
print(
187
    "Investment trafo_a: ", solph.views.node(results, "trafo_a")["scalars"][0]
188
)
189
print(
190
    "Investment trafo_b: ", solph.views.node(results, "trafo_b")["scalars"][0]
191
)
192