Completed
Push — dev ( 2c1655...16e93b )
by Patrik
20s queued 11s
created

storage_level_constraint.main()   B

Complexity

Conditions 2

Size

Total Lines 92
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 92
rs 8.1454
c 0
b 0
f 0
cc 2
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
3
"""
4
General description
5
-------------------
6
Example that shows the `storage_level_constraint`.
7
8
Code
9
----
10
Download source code: :download:`storage_level_constraint.py </../examples/storage_level_constraint/storage_level_constraint.py>`
11
12
.. dropdown:: Click to display code
13
14
    .. literalinclude:: /../examples/storage_level_constraint/storage_level_constraint.py
15
        :language: python
16
        :lines: 33-
17
18
Installation requirements
19
-------------------------
20
This example requires oemof.solph (at least v0.5.0) and matplotlib, install by:
21
22
.. code:: bash
23
24
    pip install oemof.solph>=0.5 matplotlib
25
26
27
License
28
-------
29
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
30
"""
31
32
33
import matplotlib.pyplot as plt
34
import pandas as pd
35
36
from oemof.solph import Bus
37
from oemof.solph import EnergySystem
38
from oemof.solph import Flow
39
from oemof.solph import Model
40
from oemof.solph.components import GenericStorage
41
from oemof.solph.components import Sink
42
from oemof.solph.components import Source
43
from oemof.solph.constraints import storage_level_constraint
44
from oemof.solph.processing import results
45
46
47
def main(optimize=True):
48
    es = EnergySystem(
49
        timeindex=pd.date_range("2022-01-01", freq="1H", periods=24),
50
        infer_last_interval=True,
51
    )
52
53
    multiplexer = Bus(
54
        label="multiplexer",
55
    )
56
57
    storage = GenericStorage(
58
        label="storage",
59
        nominal_capacity=3,
60
        initial_storage_level=1,
61
        balanced=True,
62
        loss_rate=0.05,
63
        inputs={multiplexer: Flow()},
64
        outputs={multiplexer: Flow()},
65
    )
66
67
    es.add(multiplexer, storage)
68
69
    in_0 = Source(
70
        label="in_0",
71
        outputs={multiplexer: Flow(nominal_capacity=0.5, variable_costs=0.15)},
72
    )
73
    es.add(in_0)
74
75
    in_1 = Source(
76
        label="in_1", outputs={multiplexer: Flow(nominal_capacity=0.1)}
77
    )
78
    es.add(in_1)
79
80
    out_0 = Sink(
81
        label="out_0",
82
        inputs={multiplexer: Flow(nominal_capacity=0.25, variable_costs=-0.1)},
83
    )
84
    es.add(out_0)
85
86
    out_1 = Sink(
87
        label="out_1",
88
        inputs={multiplexer: Flow(nominal_capacity=0.15, variable_costs=-0.1)},
89
    )
90
    es.add(out_1)
91
92
    if optimize is False:
93
        return es
94
95
    model = Model(es)
96
97
    storage_level_constraint(
98
        model=model,
99
        name="multiplexer",
100
        storage_component=storage,
101
        multiplexer_bus=multiplexer,
102
        input_levels={in_1: 1 / 3},  # in_0 is always active
103
        output_levels={out_0: 1 / 6, out_1: 1 / 2},
104
    )
105
    model.solve()
106
107
    my_results = results(model)
108
109
    df = pd.DataFrame(my_results[(storage, None)]["sequences"])
110
    df["in1_status"] = my_results[(in_1, None)]["sequences"]
111
    df["out1_status"] = my_results[(out_1, None)]["sequences"]
112
    df["out0_status"] = my_results[(out_0, None)]["sequences"]
113
114
    df["in1"] = my_results[(in_1, multiplexer)]["sequences"]
115
    df["in0"] = my_results[(in_0, multiplexer)]["sequences"]
116
    df["out0"] = my_results[(multiplexer, out_0)]["sequences"]
117
    df["out1"] = my_results[(multiplexer, out_1)]["sequences"]
118
119
    plt.step(df.index, df["in0"], where="post", label="inflow (<= 1)")
120
    plt.step(df.index, df["in1"], where="post", label="inflow (< 1/3)")
121
    plt.step(df.index, df["out0"], where="post", label="outflow (> 1/6)")
122
    plt.step(df.index, df["out1"], where="post", label="outflow (> 1/2)")
123
124
    plt.grid()
125
    plt.legend()
126
    plt.ylabel("Flow Power (arb. units)")
127
    plt.ylim(0, 0.5)
128
129
    plt.twinx()
130
131
    plt.plot(df.index, df["storage_content"], "k--", label="storage content")
132
    plt.ylim(0, 3)
133
    plt.legend(loc="center right")
134
    plt.ylabel("Stored Energy (arb. units)")
135
136
    print(df)
137
138
    plt.show()
139
140
141
if __name__ == "__main__":
142
    main()
143