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

gradient_example.main()   B

Complexity

Conditions 1

Size

Total Lines 162
Code Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 162
rs 7
c 0
b 0
f 0
cc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""
2
General description
3
-------------------
4
The gradient constraint can restrict a component to change the output within
5
one time step. In this example a storage will buffer this restriction, so the
6
more flexible the power plant can be run the less the storage will be used.
7
8
Change the GRADIENT variable in the example to see the effect on the usage of
9
the storage.
10
11
12
Installation requirements
13
-------------------------
14
This example requires oemof.solph (v0.5.x), install by:
15
16
    pip install oemof.solph[examples]
17
18
19
License
20
-------
21
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
22
"""
23
24
import matplotlib.pyplot as plt
25
import pandas as pd
26
27
from oemof.solph import EnergySystem
28
from oemof.solph import Model
29
from oemof.solph import buses
30
from oemof.solph import components as cmp
31
from oemof.solph import flows
32
from oemof.solph import processing
33
34
35
def main():
36
    # The gradient for the output of the natural gas power plant.
37
    # Change the gradient between 0.1 and 0.0001 and check the results. The
38
    # more flexible the power plant can be run the less the storage will be
39
    # used.
40
    gradient = 0.01
41
42
    date_time_index = pd.date_range("1/1/2012", periods=48, freq="H")
43
    print(date_time_index)
44
    energysystem = EnergySystem(timeindex=date_time_index, timemode="explicit")
45
46
    demand = [
47
        209643,
48
        207497,
49
        200108,
50
        191892,
51
        185717,
52
        180672,
53
        172683,
54
        170048,
55
        171132,
56
        179532,
57
        189155,
58
        201026,
59
        208466,
60
        207718,
61
        205443,
62
        206255,
63
        217240,
64
        232798,
65
        237321,
66
        232387,
67
        224306,
68
        219280,
69
        223701,
70
        213926,
71
        201834,
72
        192215,
73
        187152,
74
        184355,
75
        184438,
76
        182786,
77
        180105,
78
        191509,
79
        207104,
80
        222501,
81
        231127,
82
        238410,
83
        241184,
84
        237413,
85
        234469,
86
        235193,
87
        242730,
88
        264196,
89
        265950,
90
        260283,
91
        245578,
92
        238849,
93
        241553,
94
        231372,
95
    ]
96
97
    # create natural gas bus
98
    bgas = buses.Bus(label="natural_gas")
99
100
    # create electricity bus
101
    bel = buses.Bus(label="electricity")
102
103
    # adding the buses to the energy system
104
    energysystem.add(bgas, bel)
105
106
    # create excess component for the electricity bus to allow overproduction
107
    energysystem.add(cmp.Sink(label="excess_bel", inputs={bel: flows.Flow()}))
108
109
    # create source object representing the gas commodity (annual limit)
110
    energysystem.add(
111
        cmp.Source(
112
            label="rgas",
113
            outputs={bgas: flows.Flow(variable_costs=5)},
114
        )
115
    )
116
117
    # create simple sink object representing the electrical demand
118
    energysystem.add(
119
        cmp.Sink(
120
            label="demand",
121
            inputs={bel: flows.Flow(fix=demand, nominal_value=1)},
122
        )
123
    )
124
125
    # create simple transformer object representing a gas power plant
126
    energysystem.add(
127
        cmp.Transformer(
128
            label="pp_gas",
129
            inputs={bgas: flows.Flow()},
130
            outputs={
131
                bel: flows.Flow(
132
                    nominal_value=10e5,
133
                    negative_gradient={"ub": gradient},
134
                    positive_gradient={"ub": gradient},
135
                )
136
            },
137
            conversion_factors={bel: 0.58},
138
        )
139
    )
140
141
    # create storage object representing a battery
142
    storage = cmp.GenericStorage(
143
        nominal_storage_capacity=999999999,
144
        label="storage",
145
        inputs={bel: flows.Flow()},
146
        outputs={bel: flows.Flow()},
147
        loss_rate=0.0,
148
        initial_storage_level=None,
149
        inflow_conversion_factor=1,
150
        outflow_conversion_factor=0.8,
151
    )
152
153
    energysystem.add(storage)
154
155
    # initialise the operational model
156
    model = Model(energysystem)
157
158
    # solve
159
    model.solve(solver="cbc")
160
161
    # processing the results
162
    results = processing.results(model)
163
164
    # *** Create a table with all sequences and store it into a file (csv/xlsx)
165
    flows_to_bus = pd.DataFrame(
166
        {
167
            str(k[0].label): v["sequences"]["flow"]
168
            for k, v in results.items()
169
            if k[1] is not None and k[1] == bel
170
        }
171
    )
172
    flows_from_bus = pd.DataFrame(
173
        {
174
            str(k[1].label): v["sequences"]["flow"]
175
            for k, v in results.items()
176
            if k[1] is not None and k[0] == bel
177
        }
178
    )
179
180
    storage = pd.DataFrame(
181
        {
182
            str(k[0].label): v["sequences"]["storage_content"]
183
            for k, v in results.items()
184
            if k[1] is None and k[0] == storage
185
        }
186
    )
187
188
    my_flows = pd.concat(
189
        [flows_to_bus, flows_from_bus, storage],
190
        keys=["to_bus", "from_bus", "content", "duals"],
191
        axis=1,
192
    )
193
194
    print(my_flows)
195
    my_flows.plot()
196
    plt.show()
197
198
199
if __name__ == "__main__":
200
    main()
201