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

flow_count_limit   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 64
dl 0
loc 125
rs 10
c 0
b 0
f 0
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
energy_system = solph.EnergySystem(
34
    timeindex=pd.date_range("1/1/2012", periods=4, freq="H")
35
)
36
Node.registry = energy_system
37
38
bel = solph.Bus(label="bel")
39
40
# There are a sink and a source, both creating a revenue (negative cost),
41
# so it would be optimal to use both at the same time. To suppress this,
42
# the constraint "limit_active_flow_count" is used.
43
# You might define any keyword (here "my_keyword") like:
44
# > Flow(nonconvex=solph.NonConvex(),
45
# >      my_keyword=True,
46
#        ...)
47
# But also any existing one (e.g. "emission_factor") can be used.
48
49
solph.components.Source(
50
    label="source1",
51
    outputs={
52
        bel: solph.Flow(
53
            nonconvex=solph.NonConvex(),
54
            nominal_value=210,
55
            variable_costs=[-1, -5, -1, -1],
56
            max=[1, 1, 1, 0],
57
            my_keyword=True,
58
        )
59
    },
60
)
61
62
# Note: The keyword is also defined when set to False.
63
solph.components.Sink(
64
    label="sink1",
65
    inputs={
66
        bel: solph.Flow(
67
            nonconvex=solph.NonConvex(),
68
            variable_costs=[-2, -1, -2, -2],
69
            nominal_value=250,
70
            max=[1, 1, 1, 0],
71
            my_keyword=False,
72
        )
73
    },
74
)
75
76
# Should be ignored because my_keyword is not defined.
77
solph.components.Source(
78
    label="source2",
79
    outputs={
80
        bel: solph.Flow(
81
            variable_costs=1,
82
            nonconvex=solph.NonConvex(),
83
            max=[1, 1, 1, 0],
84
            nominal_value=145,
85
        )
86
    },
87
)
88
89
# Should be ignored because it is not NonConvex.
90
solph.components.Sink(
91
    label="sink2",
92
    inputs={
93
        bel: solph.Flow(my_keyword=True, fix=[0, 1, 1, 0], nominal_value=130)
94
    },
95
)
96
97
model = solph.Model(energy_system)
98
99
# only one of the two flows may be active at a time
100
solph.constraints.limit_active_flow_count_by_keyword(
101
    model, "my_keyword", lower_limit=0, upper_limit=1
102
)
103
104
model.solve()
105
106
results = processing.results(model)
107
108
if plt is not None:
109
    data = views.node(results, "bel")["sequences"]
110
    ax = data.plot(kind="line", grid=True)
111
    ax.set_xlabel("Time (h)")
112
    ax.set_ylabel("P (MW)")
113
114
    plt.figure()
115
    ax = plt.gca()
116
    plt.plot(
117
        results[("my_keyword", "my_keyword")]["sequences"],
118
        label="my_keyword_count",
119
    )
120
    ax.set_xlabel("Time (h)")
121
    ax.set_ylabel("Count (1)")
122
    plt.grid()
123
    plt.legend()
124
    plt.show()
125