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

non_equidistant_time_step_example.main()   B

Complexity

Conditions 2

Size

Total Lines 139
Code Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 139
rs 7
c 0
b 0
f 0
cc 2
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
7
An example to show how non-equidistant time steps work.
8
In addition to the comments in the simple example, note that:
9
10
*   Time steps in the beginning are 15 minutes.
11
*   Time steps in the end are hourly.
12
*   In the middle, there is a very short demand peak of one minute.
13
    This, however, does barely influence the storage contents.
14
*   Storage losses are defined per hour.
15
    *   storage_fixed looses 1 energy unit per hour
16
    *   storage_relative looses 50 % of its contents per hour
17
*   If possible, energy is transferred from storage with
18
    relative losses to the one with fixed losses to minimise total
19
    losses.
20
21
Installation requirements
22
-------------------------
23
24
This example requires oemof.solph, install by:
25
26
    pip install oemof.solph
27
"""
28
import pandas as pd
29
from oemof import solph
30
31
try:
32
    import matplotlib.pyplot as plt
33
except ModuleNotFoundError:
34
    plt = None
35
36
37
def main():
38
    solver = "cbc"  # 'glpk', 'gurobi',...
39
    solver_verbose = False  # show/hide solver output
40
41
    date_time_index = pd.DatetimeIndex(
42
        data=[
43
            "2000-1-1T00:00:00",
44
            "2000-1-1T00:15:00",
45
            "2000-1-1T00:30:00",
46
            "2000-1-1T00:45:00",
47
            "2000-1-1T01:00:00",
48
            "2000-1-1T01:00:01",
49
            "2000-1-1T02:00:00",
50
            "2000-1-1T03:00:00",
51
            "2000-1-1T04:00:00",
52
            "2000-1-1T05:00:00",
53
        ]
54
    )
55
56
    energy_system = solph.EnergySystem(
57
        timeindex=date_time_index, infer_last_interval=False
58
    )
59
60
    bus = solph.buses.Bus(label="bus")
61
    source = solph.components.Source(
62
        label="source",
63
        outputs={
64
            bus: solph.flows.Flow(
65
                nominal_value=16,
66
                variable_costs=0.2,
67
                max=[0, 0, 0, 0, 0, 0, 0, 1, 1],
68
            )
69
        },
70
    )
71
72
    # storage with constant losses
73
    storage_fixed = solph.components.GenericStorage(
74
        label="storage_fixed",
75
        inputs={bus: solph.flows.Flow()},
76
        outputs={bus: solph.flows.Flow()},
77
        nominal_storage_capacity=8,
78
        initial_storage_level=1,
79
        fixed_losses_absolute=1,  # 1 energy unit loss per hour
80
    )
81
82
    # storage with relative losses, we disallow outflows in the first time
83
    # steps, so that the content is not transferred to storage_fixed
84
    storage_relative = solph.components.GenericStorage(
85
        label="storage_relative",
86
        inputs={bus: solph.flows.Flow()},
87
        outputs={
88
            bus: solph.flows.Flow(
89
                nominal_value=4, max=[0, 0, 0, 0, 0, 0, 0, 1, 1]
90
            )
91
        },
92
        nominal_storage_capacity=8,
93
        initial_storage_level=1,
94
        loss_rate=0.5,  # 50 % losses per hour
95
    )
96
    sink = solph.components.Sink(
97
        label="sink",
98
        inputs={
99
            bus: solph.flows.Flow(
100
                nominal_value=8,
101
                variable_costs=0.1,
102
                fix=[0.75, 0.5, 0, 0, 1, 0, 0, 0, 0],
103
            )
104
        },
105
    )
106
107
    energy_system.add(bus, source, sink, storage_relative, storage_fixed)
108
    model = solph.Model(energy_system)
109
    model.solve(solver=solver, solve_kwargs={"tee": solver_verbose})
110
111
    results = solph.processing.results(model)
112
113
    results_df = results[(storage_fixed, None)]["sequences"].copy()
114
    results_df.rename(
115
        columns={"storage_content": "storage_fixed"}, inplace=True
116
    )
117
    results_df["storage_fixed_inflow"] = results[(bus, storage_fixed)][
118
        "sequences"
119
    ]["flow"]
120
    results_df["storage_fixed_outflow"] = results[(storage_fixed, bus)][
121
        "sequences"
122
    ]["flow"]
123
    results_df["storage_relative"] = results[(storage_relative, None)][
124
        "sequences"
125
    ]
126
    results_df["storage_relative_inflow"] = results[(bus, storage_relative)][
127
        "sequences"
128
    ]["flow"]
129
    results_df["storage_relative_outflow"] = results[(storage_relative, bus)][
130
        "sequences"
131
    ]["flow"]
132
133
    print(results_df)
134
135
    if plt is not None:
136
        plt.plot(
137
            results[(bus, storage_fixed)]["sequences"],
138
            "r-",
139
            drawstyle="steps-post",
140
            label="storage_fixed inflow",
141
        )
142
        plt.plot(
143
            results[(storage_fixed, None)]["sequences"],
144
            "r--",
145
            label="storage_fixed content",
146
        )
147
        plt.plot(results[(storage_fixed, None)]["sequences"], "r+")
148
        plt.plot(
149
            results[(storage_fixed, bus)]["sequences"],
150
            "r:",
151
            drawstyle="steps-post",
152
            label="storage_fixed outflow",
153
        )
154
155
        plt.plot(
156
            results[(bus, storage_relative)]["sequences"],
157
            "m-",
158
            drawstyle="steps-post",
159
            label="storage_relative inflow",
160
        )
161
        plt.plot(
162
            results[(storage_relative, None)]["sequences"],
163
            "m--",
164
            label="storage_relative content",
165
        )
166
        plt.plot(results[(storage_relative, None)]["sequences"], "m+")
167
        plt.plot(
168
            results[(storage_relative, bus)]["sequences"],
169
            "m:",
170
            drawstyle="steps-post",
171
            label="storage_relative outflow",
172
        )
173
174
        plt.legend()
175
        plt.show()
176
177
178
if __name__ == "__main__":
179
    main()
180