Passed
Pull Request — dev (#854)
by Uwe
03:24 queued 21s
created

TestParameterResult.test_numeric_index()   A

Complexity

Conditions 2

Size

Total Lines 38
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 38
rs 9.112
c 0
b 0
f 0
cc 2
nop 1
1
# -*- coding: utf-8 -
2
3
"""Test the definition of the time index of the model.
4
5
SPDX-FileCopyrightText: Uwe Krien <[email protected]>
6
7
SPDX-License-Identifier: MIT
8
"""
9
import datetime
10
import random
11
12
import pandas as pd
13
14
from oemof.solph import EnergySystem
15
from oemof.solph import Investment
16
from oemof.solph import Model
17
from oemof.solph import buses
18
from oemof.solph import components as cmp
19
from oemof.solph import flows
20
from oemof.solph import processing
21
22
23
class TestParameterResult:
24
    @classmethod
25
    def setup_class(cls):
26
        dtindex1 = pd.date_range("1/1/2012", periods=24, freq="H")
27
        dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
28
        dtindex = dtindex1.union(dtindex2)
29
        es = EnergySystem(timeindex=dtindex, infer_last_interval=False)
30
31
        # BUSSES
32
        b_el1 = buses.Bus(label="b_el1")
33
        b_diesel = buses.Bus(label="b_diesel", balanced=False)
34
        es.add(b_el1, b_diesel)
35
36
        # TEST DIESEL:
37
        dg = cmp.Transformer(
38
            label="diesel_generator",
39
            inputs={b_diesel: flows.Flow(variable_costs=2)},
40
            outputs={
41
                b_el1: flows.Flow(
42
                    variable_costs=1, investment=Investment(ep_costs=500)
43
                )
44
            },
45
            conversion_factors={b_el1: 0.5},
46
        )
47
48
        batt = cmp.GenericStorage(
49
            label="storage",
50
            nominal_storage_capacity=1000,
51
            inputs={b_el1: flows.Flow(variable_costs=3)},
52
            outputs={b_el1: flows.Flow(variable_costs=2.5)},
53
            loss_rate=0.00,
54
            invest_relation_input_capacity=1 / 6,
55
            invest_relation_output_capacity=1 / 6,
56
            inflow_conversion_factor=1,
57
            outflow_conversion_factor=0.9,
58
        )
59
60
        random.seed(1)
61
        demand_values = random.sample(range(40, 120), 72)
62
        demand = cmp.Sink(
63
            label="demand_el",
64
            inputs={
65
                b_el1: flows.Flow(
66
                    nominal_value=1,
67
                    fix=demand_values,
68
                )
69
            },
70
        )
71
        es.add(dg, batt, demand)
72
        model = Model(es)
73
        model.receive_duals()
74
        model.solve()
75
        results = processing.results(model, remove_last_time_point=False)
76
        cls.flows = {k: v for k, v in results.items() if k[1] is not None}
77
        cls.comp = {k: v for k, v in results.items() if k[1] is None}
78
        cls.es = es
79
        cls.model = model
80
81 View Code Duplication
    def test_timesteps_timeincrements_with_storage_charging(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
        storage_content = [
83
            v["sequences"]["storage_content"]
84
            for k, v in self.comp.items()
85
            if k[0].label == "storage"
86
        ][0]
87
        assert storage_content[0] == storage_content[-1]
88
89
        charge = [
90
            v["sequences"]["flow"]
91
            for k, v in self.flows.items()
92
            if k[1].label == "storage"
93
        ][0]
94
        # Calculate the next storage content and verify it with the storage
95
        # content of the results (charging).
96
        # Charging - timestep (ts) with its timeincrement (ti)
97
        time = [(23, 1), (24, 0.5)]
98
        for ts, ti in time:
99
            assert round(storage_content[ts] + charge[ts] * ti, 5) == round(
100
                storage_content[ts + 1], 5
101
            )
102
            assert self.es.timeincrement[ts] == ti
103
        assert charge.isnull().any()
104
105 View Code Duplication
    def test_timesteps_timeincrements_with_storage_discharging(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
        storage_content = [
107
            v["sequences"]["storage_content"]
108
            for k, v in self.comp.items()
109
            if k[0].label == "storage"
110
        ][0]
111
        # Storage content at the last time point is equal to the content of
112
        # the first time point because the storage is balanced.
113
        assert storage_content[0] == storage_content[-1]
114
115
        discharge = [
116
            v["sequences"]["flow"]
117
            for k, v in self.flows.items()
118
            if k[0].label == "storage"
119
        ][0]
120
121
        # Calculate the next storage content and verify it with the storage
122
        # content of the results (discharging).
123
        # Discharging - timestep (ts) with its timeincrement (ti)
124
        time = [(7, 1), (40, 0.5)]
125
        for ts, ti in time:
126
            assert round(
127
                storage_content[ts]
128
                - (discharge[ts] + (discharge[ts] * 1 / 9)) * ti,
129
                5,
130
            ) == round(storage_content[ts + 1], 5)
131
            assert self.es.timeincrement[ts] == ti
132
133
    def test_timeincrements(self):
134
        assert self.es.timeincrement.sum() == 48
135
136
    def test_without_last_time_point(self):
137
        results = processing.results(self.model, remove_last_time_point=True)
138
        flow = {k: v for k, v in results.items() if k[1] is not None}
139
        comp = {k: v for k, v in results.items() if k[1] is None}
140
        storage_content = [
141
            v["sequences"]["storage_content"]
142
            for k, v in comp.items()
143
            if k[0].label == "storage"
144
        ][0]
145
        charge = [
146
            v["sequences"]["flow"]
147
            for k, v in flow.items()
148
            if k[1].label == "storage"
149
        ][0]
150
        # The first and the last value are not the same because the last value
151
        # of the storage is missing. Adding the charging of the last time step
152
        # will result the final storage content, which is equal to the first
153
        assert storage_content[0] != storage_content[-1]
154
        assert storage_content[0] == storage_content[-1] + charge[-1] / 2
155
        assert not charge.isnull().any()
156
        assert storage_content.index[0] == datetime.datetime(
157
            2012, 1, 1, 0, 0, 0
158
        )
159
        assert charge.index[0] == datetime.datetime(2012, 1, 1, 0, 0, 0)
160
        assert storage_content.index[-1] == datetime.datetime(
161
            2012, 1, 2, 23, 30, 0
162
        )
163
        assert charge.index[-1] == datetime.datetime(2012, 1, 2, 23, 30, 0)
164
165
    def test_time_index_with_last_time_point(self):
166
        storage_content = [
167
            v["sequences"]["storage_content"]
168
            for k, v in self.comp.items()
169
            if k[0].label == "storage"
170
        ][0]
171
        assert storage_content[0] == storage_content[-1]
172
173
        charge = [
174
            v["sequences"]["flow"]
175
            for k, v in self.flows.items()
176
            if k[1].label == "storage"
177
        ][0]
178
        assert storage_content.index[0] == datetime.datetime(
179
            2012, 1, 1, 0, 0, 0
180
        )
181
        assert charge.index[0] == datetime.datetime(2012, 1, 1, 0, 0, 0)
182
        assert storage_content.index[-1] == datetime.datetime(
183
            2012, 1, 3, 0, 0, 0
184
        )
185
        assert charge.index[-1] == datetime.datetime(2012, 1, 3, 0, 0, 0)
186
187
    def test_numeric_index(self):
188
        self.es.timeindex = None
189
        model = Model(self.es)
190
        model.receive_duals()
191
        model.solve()
192
        results = processing.results(self.model)
193
        flow = {k: v for k, v in results.items() if k[1] is not None}
194
        diesel_generator_out = [
195
            v["sequences"]["flow"]
196
            for k, v in flow.items()
197
            if k[0].label == "diesel_generator"
198
        ][0]
199
        assert diesel_generator_out.index[0] == 0
200
        assert diesel_generator_out.index[-1] == 72
201
        assert len(diesel_generator_out.index) == 73
202
203
        storage_content = [
204
            v["sequences"]["storage_content"]
205
            for k, v in self.comp.items()
206
            if k[0].label == "storage"
207
        ][0]
208
        assert storage_content[0] == storage_content[-1]
209
210
        charge = [
211
            v["sequences"]["flow"]
212
            for k, v in self.flows.items()
213
            if k[1].label == "storage"
214
        ][0]
215
        # Calculate the next storage content and verify it with the storage
216
        # content of the results (charging).
217
        # Charging - timestep (ts) with its timeincrement (ti)
218
        time = [(23, 1), (24, 0.5)]
219
        for ts, ti in time:
220
            assert round(storage_content[ts] + charge[ts] * ti, 5) == round(
221
                storage_content[ts + 1], 5
222
            )
223
            assert self.es.timeincrement[ts] == ti
224
        assert charge.isnull().any()
225