Passed
Pull Request — dev (#1174)
by
unknown
02:47 queued 01:01
created

emission_constraint_invest.main()   B

Complexity

Conditions 3

Size

Total Lines 143
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 85
dl 0
loc 143
rs 7.4909
c 0
b 0
f 0
cc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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