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

storage.storage_example()   B

Complexity

Conditions 3

Size

Total Lines 107
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 107
rs 8.1127
c 0
b 0
f 0
cc 3
nop 0

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