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

flow_count_limit   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 68
dl 0
loc 133
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 95 2
1
# -*- coding: utf-8 -*-
2
3
"""
4
General description
5
-------------------
6
7
Something...
8
9
10
Installation requirements
11
-------------------------
12
13
This example requires oemof.solph (v0.5.x), install by:
14
15
    pip install oemof.solph[examples]
16
17
License
18
-------
19
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
20
"""
21
import pandas as pd
22
from oemof.network.network import Node
23
24
import oemof.solph as solph
25
from oemof.solph import processing
26
from oemof.solph import views
27
28
try:
29
    import matplotlib.pyplot as plt
30
except ImportError:
31
    plt = None
32
33
34
def main():
35
    energy_system = solph.EnergySystem(
36
        timeindex=pd.date_range("1/1/2012", periods=4, freq="H")
37
    )
38
    Node.registry = energy_system
39
40
    bel = solph.Bus(label="bel")
41
42
    # There are a sink and a source, both creating a revenue (negative cost),
43
    # so it would be optimal to use both at the same time. To suppress this,
44
    # the constraint "limit_active_flow_count" is used.
45
    # You might define any keyword (here "my_keyword") like:
46
    # > Flow(nonconvex=solph.NonConvex(),
47
    # >      my_keyword=True,
48
    #        ...)
49
    # But also any existing one (e.g. "emission_factor") can be used.
50
51
    solph.components.Source(
52
        label="source1",
53
        outputs={
54
            bel: solph.Flow(
55
                nonconvex=solph.NonConvex(),
56
                nominal_value=210,
57
                variable_costs=[-1, -5, -1, -1],
58
                max=[1, 1, 1, 0],
59
                my_keyword=True,
60
            )
61
        },
62
    )
63
64
    # Note: The keyword is also defined when set to False.
65
    solph.components.Sink(
66
        label="sink1",
67
        inputs={
68
            bel: solph.Flow(
69
                nonconvex=solph.NonConvex(),
70
                variable_costs=[-2, -1, -2, -2],
71
                nominal_value=250,
72
                max=[1, 1, 1, 0],
73
                my_keyword=False,
74
            )
75
        },
76
    )
77
78
    # Should be ignored because my_keyword is not defined.
79
    solph.components.Source(
80
        label="source2",
81
        outputs={
82
            bel: solph.Flow(
83
                variable_costs=1,
84
                nonconvex=solph.NonConvex(),
85
                max=[1, 1, 1, 0],
86
                nominal_value=145,
87
            )
88
        },
89
    )
90
91
    # Should be ignored because it is not NonConvex.
92
    solph.components.Sink(
93
        label="sink2",
94
        inputs={
95
            bel: solph.Flow(
96
                my_keyword=True, fix=[0, 1, 1, 0], nominal_value=130
97
            )
98
        },
99
    )
100
101
    model = solph.Model(energy_system)
102
103
    # only one of the two flows may be active at a time
104
    solph.constraints.limit_active_flow_count_by_keyword(
105
        model, "my_keyword", lower_limit=0, upper_limit=1
106
    )
107
108
    model.solve()
109
110
    results = processing.results(model)
111
112
    if plt is not None:
113
        data = views.node(results, "bel")["sequences"]
114
        ax = data.plot(kind="line", grid=True)
115
        ax.set_xlabel("Time (h)")
116
        ax.set_ylabel("P (MW)")
117
118
        plt.figure()
119
        ax = plt.gca()
120
        plt.plot(
121
            results[("my_keyword", "my_keyword")]["sequences"],
122
            label="my_keyword_count",
123
        )
124
        ax.set_xlabel("Time (h)")
125
        ax.set_ylabel("Count (1)")
126
        plt.grid()
127
        plt.legend()
128
        plt.show()
129
130
131
if __name__ == "__main__":
132
    main()
133