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

storage_costs.storage_costs_example()   B

Complexity

Conditions 1

Size

Total Lines 108
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 108
rs 8.0145
c 0
b 0
f 0
cc 1
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 `storage_costs` of `GenericStorage`.
7
A battery is used to make profit from fluctuating electricity prices.
8
For a battery without storage costs, it is beneficial to be empty
9
the end of the time horizon of the optimisation. For a battery that
10
assumes the average revenue, energy is kept at the end.
11
12
Code
13
----
14
Download source code: :download:`storage_costs.py </../examples/storage_costs/storage_costs.py>`
15
16
.. dropdown:: Click to display code
17
18
    .. literalinclude:: /../examples/storage_costs/storage_costs.py
19
        :language: python
20
        :lines: 36-
21
22
Installation requirements
23
-------------------------
24
This example requires oemof.solph (v0.5.x) and matplotlib, install by:
25
26
.. code:: bash
27
28
    pip install oemof.solph[examples] matplotlib
29
30
31
License
32
-------
33
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
34
"""
35
36
import numpy as np
37
import pandas as pd
38
from matplotlib import pyplot as plt
39
40
from oemof import solph
41
42
43
def main(optimize=True):
44
    # create an energy system
45
    idx = pd.date_range("1/1/2023", periods=13, freq="h")
46
    es = solph.EnergySystem(timeindex=idx, infer_last_interval=False)
47
48
    # power bus
49
    bel = solph.Bus(label="bel")
50
    es.add(bel)
51
52
    es.add(
53
        solph.components.Source(
54
            label="source_el",
55
            outputs={bel: solph.Flow()},
56
        )
57
    )
58
59
    es.add(
60
        solph.components.Sink(
61
            label="sink_el",
62
            inputs={bel: solph.Flow()},
63
        )
64
    )
65
66
    electricity_price = np.array(
67
        [
68
            0.38,
69
            0.31,
70
            0.32,
71
            0.33,
72
            0.37,
73
            0.32,
74
            0.33,
75
            0.34,
76
            0.39,
77
            0.38,
78
            0.37,
79
            0.35,
80
            0.35,
81
        ]
82
    )
83
84
    # Electric Storage 1
85
    # Costs are designed in a way that storing energy is benificial until the
86
    # last four time steps but emptying it is not a good option.
87
    battery1 = solph.components.GenericStorage(
88
        label="battery 1",
89
        nominal_capacity=10,
90
        inputs={
91
            bel: solph.Flow(
92
                nominal_capacity=1,
93
                variable_costs=electricity_price,
94
            )
95
        },
96
        outputs={
97
            bel: solph.Flow(
98
                nominal_capacity=1,
99
                variable_costs=-electricity_price,
100
            )
101
        },
102
        initial_storage_level=0.5,
103
        balanced=False,
104
    )
105
    es.add(battery1)
106
107
    # storages that balance our fluctuating costs
108
    # Electric Storage 2
109
    battery2 = solph.components.GenericStorage(
110
        label="battery 2",
111
        nominal_capacity=10,
112
        inputs={
113
            bel: solph.Flow(
114
                nominal_capacity=1,
115
                variable_costs=electricity_price,
116
            )
117
        },
118
        outputs={
119
            bel: solph.Flow(
120
                nominal_capacity=1,
121
                variable_costs=-electricity_price,
122
            )
123
        },
124
        storage_costs=12 * [0] + [-np.mean(electricity_price)],
125
        initial_storage_level=0.5,
126
        balanced=False,
127
    )
128
    es.add(battery2)
129
130
    if optimize is False:
131
        return es
132
133
    # create an optimization problem and solve it
134
    model = solph.Model(es)
135
136
    # solve model
137
    model.solve(solver="cbc")
138
139
    # create result object
140
    results = solph.processing.results(model)
141
142
    plt.plot(
143
        results[(battery1, None)]["sequences"],
144
        label="content w/o storage costs",
145
    )
146
    plt.plot(
147
        results[(battery2, None)]["sequences"],
148
        label="content w/ storage revenue",
149
    )
150
    plt.legend()
151
    plt.grid()
152
153
    plt.show()
154
155
156
if __name__ == "__main__":
157
    storage_costs_example()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable storage_costs_example does not seem to be defined.
Loading history...
158