Passed
Pull Request — dev (#799)
by Uwe
01:54
created

time_step_example   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 53
dl 0
loc 101
rs 10
c 0
b 0
f 0
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
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
solver = "cbc"  # 'glpk', 'gurobi',...
37
solver_verbose = False  # show/hide solver output
38
39
date_time_index = pd.date_range("1/1/2000", periods=9, freq="15T")
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
if plt is not None:
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