Completed
Push — dev ( 468d85...f4c061 )
by Patrik
23s queued 17s
created

storage.main()   B

Complexity

Conditions 4

Size

Total Lines 112
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 112
rs 8.0145
c 0
b 0
f 0
cc 4
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 parameter `balanced` of `GenericStorage`.
7
8
Code
9
----
10
Download source code: :download:`storage.py </../examples/storage_balanced_unbalanced/storage.py>`
11
12
.. dropdown:: Click to display code
13
14
    .. literalinclude:: /../examples/storage_balanced_unbalanced/storage.py
15
        :language: python
16
        :lines: 32-
17
18
Installation requirements
19
-------------------------
20
This example requires oemof.solph (v0.5.x), install by:
21
22
.. code:: bash
23
24
    pip install oemof.solph[examples]
25
26
27
License
28
-------
29
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
30
"""
31
32
import pandas as pd
33
from matplotlib import pyplot as plt
34
35
from oemof import solph
36
37
DATA = [
38
    {
39
        "name": "unbalanced (20% filled)",
40
        "initial_storage_level": 0.2,
41
        "balanced": False,
42
    },
43
    {
44
        "name": "unbalanced (None)",
45
        "initial_storage_level": None,
46
        "balanced": False,
47
    },
48
    {
49
        "name": "balanced (20% filled)",
50
        "initial_storage_level": 0.2,
51
        "balanced": True,
52
    },
53
    {
54
        "name": "balanced (None)",
55
        "initial_storage_level": None,
56
        "balanced": True,
57
    },
58
]
59
60
PARAMETER = {"el_price": 10, "ex_price": 5, "nominal_storage_capacity": 7}
61
62
63
def main(optimize=True):
64
    timeseries = pd.DataFrame(
65
        {"demand_el": [7, 6, 6, 7], "pv_el": [3, 5, 3, 12]}
66
    )
67
68
    # create an energy system
69
    idx = pd.date_range("1/1/2017", periods=len(timeseries), freq="h")
70
    es = solph.EnergySystem(timeindex=idx, infer_last_interval=True)
71
72
    for data_set in DATA:
73
        name = data_set["name"]
74
75
        # power bus
76
        bel = solph.Bus(label="bel_{0}".format(name))
77
        es.add(bel)
78
79
        es.add(
80
            solph.components.Source(
81
                label="source_el_{0}".format(name),
82
                outputs={
83
                    bel: solph.Flow(variable_costs=PARAMETER["el_price"])
84
                },
85
            )
86
        )
87
88
        es.add(
89
            solph.components.Source(
90
                label="pv_el_{0}".format(name),
91
                outputs={
92
                    bel: solph.Flow(
93
                        fix=timeseries["pv_el"], nominal_capacity=1
94
                    )
95
                },
96
            )
97
        )
98
99
        es.add(
100
            solph.components.Sink(
101
                label="demand_el_{0}".format(name),
102
                inputs={
103
                    bel: solph.Flow(
104
                        fix=timeseries["demand_el"], nominal_capacity=1
105
                    )
106
                },
107
            )
108
        )
109
110
        es.add(
111
            solph.components.Sink(
112
                label="excess_{0}".format(name),
113
                inputs={bel: solph.Flow()},
114
            )
115
        )
116
117
        # Electric Storage
118
        es.add(
119
            solph.components.GenericStorage(
120
                label="storage_elec_{0}".format(name),
121
                nominal_capacity=PARAMETER["nominal_storage_capacity"],
122
                inputs={bel: solph.Flow()},
123
                outputs={bel: solph.Flow()},
124
                initial_storage_level=data_set["initial_storage_level"],
125
                balanced=data_set["balanced"],
126
            )
127
        )
128
129
    if optimize is False:
130
        return es
131
132
    # create an optimization problem and solve it
133
    om = solph.Model(es)
134
135
    # solve model
136
    om.solve(solver="cbc")
137
138
    # create result object
139
    results = solph.processing.results(om)
140
141
    components = [x for x in results if x[1] is None]
142
143
    storage_cap = pd.DataFrame()
144
    balance = pd.Series(dtype=float)
145
146
    storages = [x[0] for x in components if "storage" in x[0].label]
147
148
    for s in storages:
149
        name = s.label
150
        storage_cap[name] = results[s, None]["sequences"]["storage_content"]
151
        balance[name] = storage_cap.iloc[0][name] - storage_cap.iloc[-1][name]
152
153
    storage_cap.plot(
154
        drawstyle="steps-mid",
155
        subplots=False,
156
        sharey=True,
157
        title="Storage content",
158
    )
159
    storage_cap.plot(
160
        drawstyle="steps-mid",
161
        subplots=True,
162
        sharey=True,
163
        title="Storage content",
164
    )
165
166
    balance.plot(
167
        kind="bar",
168
        linewidth=1,
169
        edgecolor="#000000",
170
        rot=0,
171
        ax=plt.subplots()[1],
172
        title="Gained energy from storage",
173
    )
174
    plt.show()
175
176
177
if __name__ == "__main__":
178
    main()
179