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

time_index_example   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 101
Duplicated Lines 14.85 %

Importance

Changes 0
Metric Value
wmc 0
eloc 49
dl 15
loc 101
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
3
"""
4
General description
5
-------------------
6
7
A minimal example to show how time steps work.
8
9
*   Flows are defined in time intervals, storage content at points in time.
10
    Thus, there is one more value for storage contents then for the
11
    flow values.
12
*   Time intervals are named by the time at the beginning of that interval.
13
    The quantity changes to the given value at the given point in time.
14
*   The initial_storage_level of a GenericStorage is given
15
    at the first time step. If the storage is balanced,
16
    this is the same storage level as in the last time step.
17
*   The nominal_value in Flows has to be interpreted in means of power:
18
    We have nominal_value=0.5, but the maximum change of the storage content
19
    of an ideal storage is 0.125.
20
21
Installation requirements
22
-------------------------
23
This example requires oemof.solph (v0.5.x), install by:
24
25
    pip install oemof.solph[examples]
26
27
28
License
29
-------
30
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
31
"""
32
import matplotlib.pyplot as plt
33
34
from oemof import solph
35
36
solver = "cbc"  # 'glpk', 'gurobi',...
37
solver_verbose = False  # show/hide solver output
38
39
date_time_index = solph.create_time_index(2000, interval=0.25, number=8)
40
41
energy_system = solph.EnergySystem(
42
    timeindex=date_time_index, infer_last_interval=False
43
)
44
45
bus = solph.buses.Bus(label="bus")
46
source = solph.components.Source(
47
    label="source",
48
    outputs={
49
        bus: solph.flows.Flow(
50
            nominal_value=2,
51
            variable_costs=0.2,
52
            max=[0, 0, 0, 0, 1, 0.25, 0.75, 1],
53
        )
54
    },
55
)
56
storage = solph.components.GenericStorage(
57
    label="storage",
58
    inputs={bus: solph.flows.Flow()},
59
    outputs={bus: solph.flows.Flow()},
60
    nominal_storage_capacity=4,
61
    initial_storage_level=0.5,
62
)
63
sink = solph.components.Sink(
64
    label="sink",
65
    inputs={
66
        bus: solph.flows.Flow(
67
            nominal_value=2,
68
            variable_costs=0.1,
69
            fix=[1, 1, 0.5, 0.5, 0, 0, 0, 0],
70
        )
71
    },
72
)
73
74
energy_system.add(bus, source, sink, storage)
75
model = solph.Model(energy_system)
76
model.solve(solver=solver, solve_kwargs={"tee": solver_verbose})
77
78
results = solph.processing.results(model)
79
80
results_df = results[(storage, None)]["sequences"].copy()
81
results_df["storage_inflow"] = results[(bus, storage)]["sequences"]["flow"]
82
results_df["storage_outflow"] = results[(storage, bus)]["sequences"]["flow"]
83
84
print(results_df)
85
86 View Code Duplication
if plt is not None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
87
    plt.plot(
88
        results[(bus, storage)]["sequences"],
89
        drawstyle="steps-post",
90
        label="Storage inflow",
91
    )
92
    plt.plot(results[(storage, None)]["sequences"], label="Storage content")
93
    plt.plot(
94
        results[(storage, bus)]["sequences"],
95
        drawstyle="steps-post",
96
        label="Storage outflow",
97
    )
98
99
    plt.legend(loc="lower left")
100
    plt.show()
101