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

example_generic_invest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 92
dl 0
loc 205
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 142 2
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
58
def main():
59
    data = [0, 15, 30, 35, 20, 25, 27, 10, 5, 2, 15, 40, 20, 0, 0]
60
61
    # create an energy system
62
    idx = solph.create_time_index(2020, number=len(data))
63
    es = solph.EnergySystem(timeindex=idx, infer_last_interval=False)
64
65
    # Parameter: costs for the sources
66
    c_0 = 10
67
    c_1 = 100
68
69
    epc_invest = 500
70
71
    # commodity a
72
    bus_a_0 = solph.Bus(label="bus_a_0")
73
    bus_a_1 = solph.Bus(label="bus_a_1")
74
    es.add(bus_a_0, bus_a_1)
75
76
    es.add(
77
        solph.components.Source(
78
            label="source_a_0",
79
            outputs={bus_a_0: solph.Flow(variable_costs=c_0)},
80
        )
81
    )
82
83
    es.add(
84
        solph.components.Source(
85
            label="source_a_1",
86
            outputs={bus_a_1: solph.Flow(variable_costs=c_1)},
87
        )
88
    )
89
90
    es.add(
91
        solph.components.Sink(
92
            label="demand_a",
93
            inputs={bus_a_1: solph.Flow(fix=data, nominal_value=1)},
94
        )
95
    )
96
97
    # commodity b
98
    bus_b_0 = solph.Bus(label="bus_b_0")
99
    bus_b_1 = solph.Bus(label="bus_b_1")
100
    es.add(bus_b_0, bus_b_1)
101
    es.add(
102
        solph.components.Source(
103
            label="source_b_0",
104
            outputs={bus_b_0: solph.Flow(variable_costs=c_0)},
105
        )
106
    )
107
108
    es.add(
109
        solph.components.Source(
110
            label="source_b_1",
111
            outputs={bus_b_1: solph.Flow(variable_costs=c_1)},
112
        )
113
    )
114
115
    es.add(
116
        solph.components.Sink(
117
            label="demand_b",
118
            inputs={bus_b_1: solph.Flow(fix=data, nominal_value=1)},
119
        )
120
    )
121
122
    # transformer a
123
    es.add(
124
        solph.components.Transformer(
125
            label="trafo_a",
126
            inputs={bus_a_0: solph.Flow()},
127
            outputs={
128
                bus_a_1: solph.Flow(
129
                    nominal_value=None,
130
                    investment=solph.Investment(
131
                        ep_costs=epc_invest,
132
                        space=2,
133
                    ),
134
                )
135
            },
136
            conversion_factors={bus_a_1: 0.8},
137
        )
138
    )
139
140
    # transformer b
141
    es.add(
142
        solph.components.Transformer(
143
            label="trafo_b",
144
            inputs={bus_b_0: solph.Flow()},
145
            outputs={
146
                bus_b_1: solph.Flow(
147
                    nominal_value=None,
148
                    investment=solph.Investment(
149
                        ep_costs=epc_invest,
150
                        space=1,
151
                    ),
152
                )
153
            },
154
            conversion_factors={bus_a_1: 0.8},
155
        )
156
    )
157
158
    # create an optimization problem and solve it
159
    om = solph.Model(es)
160
161
    # add constraint for generic investment limit
162
    om = solph.constraints.additional_investment_flow_limit(
163
        om, "space", limit=24
164
    )
165
166
    # export lp file
167
    filename = os.path.join(
168
        solph.helpers.extend_basic_path("lp_files"), "GenericInvest.lp"
169
    )
170
    logging.info("Store lp-file in {0}.".format(filename))
171
    om.write(filename, io_options={"symbolic_solver_labels": True})
172
173
    # solve model
174
    om.solve(solver="cbc", solve_kwargs={"tee": True})
175
176
    # create result object
177
    results = solph.processing.results(om)
178
179
    bus1 = solph.views.node(results, "bus_a_1")["sequences"]
180
    bus2 = solph.views.node(results, "bus_b_1")["sequences"]
181
182
    # plot the time series (sequences) of a specific component/bus
183
    if plt is not None:
184
        bus1.plot(kind="line", drawstyle="steps-mid")
185
        plt.legend()
186
        plt.show()
187
        bus2.plot(kind="line", drawstyle="steps-mid")
188
        plt.legend()
189
        plt.show()
190
191
    space_used = om.invest_limit_space()
192
    print("Space value: ", space_used)
193
    print(
194
        "Investment trafo_a: ",
195
        solph.views.node(results, "trafo_a")["scalars"][0],
196
    )
197
    print(
198
        "Investment trafo_b: ",
199
        solph.views.node(results, "trafo_b")["scalars"][0],
200
    )
201
202
203
if __name__ == "__main__":
204
    main()
205