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

min_max_runtimes   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 55
dl 0
loc 111
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 81 2
1
# -*- coding: utf-8 -*-
2
3
"""
4
General description
5
-------------------
6
Example that illustrates how to model min and max runtimes.
7
8
Installation requirements
9
-------------------------
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
from matplotlib import pyplot as plt
22
23
from oemof import solph
24
25
26
def main():
27
    # Create demand data
28
    demand_el = [0] * 24
29
    for n in [10, 15, 19]:
30
        demand_el[n] = 1
31
32
    # create an energy system
33
    idx = solph.create_time_index(2017, number=24)
34
    es = solph.EnergySystem(timeindex=idx, infer_last_interval=False)
35
36
    # power bus and components
37
    bel = solph.Bus(label="bel")
38
39
    demand_el = solph.components.Sink(
40
        label="demand_el",
41
        inputs={bel: solph.Flow(fix=demand_el, nominal_value=10)},
42
    )
43
44
    dummy_el = solph.components.Sink(
45
        label="dummy_el", inputs={bel: solph.Flow(variable_costs=10)}
46
    )
47
48
    pp1 = solph.components.Source(
49
        label="plant_min_down_constraints",
50
        outputs={
51
            bel: solph.Flow(
52
                nominal_value=10,
53
                min=0.5,
54
                max=1.0,
55
                variable_costs=10,
56
                nonconvex=solph.NonConvex(
57
                    minimum_downtime=4, initial_status=0
58
                ),
59
            )
60
        },
61
    )
62
63
    pp2 = solph.components.Source(
64
        label="plant_min_up_constraints",
65
        outputs={
66
            bel: solph.Flow(
67
                nominal_value=10,
68
                min=0.5,
69
                max=1.0,
70
                variable_costs=10,
71
                nonconvex=solph.NonConvex(minimum_uptime=2, initial_status=1),
72
            )
73
        },
74
    )
75
76
    es.add(bel, dummy_el, demand_el, pp1, pp2)
77
78
    # create an optimization problem and solve it
79
    om = solph.Model(es)
80
81
    # debugging
82
    # om.write('problem.lp', io_options={'symbolic_solver_labels': True})
83
84
    # solve model
85
    om.solve(solver="cbc", solve_kwargs={"tee": True})
86
87
    # create result object
88
    results = solph.processing.results(om)
89
90
    # plot data
91
    data = solph.views.node(results, "bel")["sequences"]
92
    data[[(("bel", "demand_el"), "flow"), (("bel", "dummy_el"), "flow")]] *= -1
93
    exclude = ["dummy_el", "status"]
94
    columns = [
95
        c
96
        for c in data.columns
97
        if not any(s in c[0] or s in c[1] for s in exclude)
98
    ]
99
    data = data[columns]
100
    fig, ax = plt.subplots(figsize=(10, 5))
101
    data.plot(ax=ax, kind="line", drawstyle="steps-post", grid=True, rot=0)
102
    ax.set_xlabel("Hour")
103
    ax.set_ylabel("P [MW]")
104
    plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.3), ncol=1)
105
    fig.subplots_adjust(top=0.8)
106
    plt.show()
107
108
109
if __name__ == "__main__":
110
    main()
111