Passed
Pull Request — dev (#1208)
by Patrik
04:04 queued 02:13
created

TestParameterResult.test_timeincrements()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
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
import pytest
14
15
from oemof.solph import EnergySystem
16
from oemof.solph import Investment
17
from oemof.solph import Model
18
from oemof.solph import buses
19
from oemof.solph import components as cmp
20
from oemof.solph import flows
21
from oemof.solph import processing
22
23
24
class TestParameterResult:
25
    @classmethod
26
    def setup_class(cls):
27
        dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
28
        dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
29
        dtindex = dtindex1.union(dtindex2)
30
        es = EnergySystem(timeindex=dtindex, infer_last_interval=False)
31
32
        # BUSSES
33
        b_el1 = buses.Bus(label="b_el1")
34
        b_diesel = buses.Bus(label="b_diesel", balanced=False)
35
        es.add(b_el1, b_diesel)
36
37
        # TEST DIESEL:
38
        dg = cmp.Converter(
39
            label="diesel_generator",
40
            inputs={b_diesel: flows.Flow(variable_costs=2)},
41
            outputs={
42
                b_el1: flows.Flow(
43
                    variable_costs=1, nominal_capacity=Investment(ep_costs=500)
44
                )
45
            },
46
            conversion_factors={b_el1: 0.5},
47
        )
48
49
        batt = cmp.GenericStorage(
50
            label="storage",
51
            nominal_capacity=1000,
52
            inputs={b_el1: flows.Flow(variable_costs=3)},
53
            outputs={b_el1: flows.Flow(variable_costs=2.5)},
54
            loss_rate=0.00,
55
            invest_relation_input_capacity=1 / 6,
56
            invest_relation_output_capacity=1 / 6,
57
            inflow_conversion_factor=1,
58
            outflow_conversion_factor=0.9,
59
        )
60
61
        random.seed(1)
62
        demand_values = random.sample(range(40, 120), 72)
63
        demand = cmp.Sink(
64
            label="demand_el",
65
            inputs={
66
                b_el1: flows.Flow(
67
                    nominal_capacity=1,
68
                    fix=demand_values,
69
                )
70
            },
71
        )
72
        es.add(dg, batt, demand)
73
        model = Model(es)
74
        model.receive_duals()
75
        model.solve()
76
        results = processing.results(model, remove_last_time_point=False)
77
        cls.flows = {k: v for k, v in results.items() if k[1] is not None}
78
        cls.comp = {k: v for k, v in results.items() if k[1] is None}
79
        cls.es = es
80
        cls.model = model
81
82 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...
83
        storage_content = [
84
            v["sequences"]["storage_content"]
85
            for k, v in self.comp.items()
86
            if k[0].label == "storage"
87
        ][0]
88
        assert storage_content.iloc[0] == storage_content.iloc[-1]
89
90
        charge = [
91
            v["sequences"]["flow"]
92
            for k, v in self.flows.items()
93
            if k[1].label == "storage"
94
        ][0]
95
        # Calculate the next storage content and verify it with the storage
96
        # content of the results (charging).
97
        # Charging - timestep (ts) with its timeincrement (ti)
98
        time = [(23, 1), (24, 0.5)]
99
        for ts, ti in time:
100
            assert storage_content.iloc[ts] + charge.iloc[
101
                ts
102
            ] * ti == pytest.approx(storage_content.iloc[ts + 1])
103
            assert self.es.timeincrement[ts] == ti
104
        assert charge.isnull().any()
105
106 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...
107
        storage_content = [
108
            v["sequences"]["storage_content"]
109
            for k, v in self.comp.items()
110
            if k[0].label == "storage"
111
        ][0]
112
        # Storage content at the last time point is equal to the content of
113
        # the first time point because the storage is balanced.
114
        assert storage_content.iloc[0] == storage_content.iloc[-1]
115
116
        discharge = [
117
            v["sequences"]["flow"]
118
            for k, v in self.flows.items()
119
            if k[0].label == "storage"
120
        ][0]
121
122
        # Calculate the next storage content and verify it with the storage
123
        # content of the results (discharging).
124
        # Discharging - timestep (ts) with its timeincrement (ti)
125
        time = [(7, 1), (40, 0.5)]
126
        for ts, ti in time:
127
            assert (
128
                storage_content.iloc[ts]
129
                - (discharge.iloc[ts] + (discharge.iloc[ts] * 1 / 9)) * ti
130
            ) == pytest.approx(storage_content.iloc[ts + 1])
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.iloc[0] != storage_content.iloc[-1]
154
        assert (
155
            storage_content.iloc[0]
156
            == storage_content.iloc[-1] + charge.iloc[-1] / 2
157
        )
158
        assert not charge.isnull().any()
159
        assert storage_content.index[0] == datetime.datetime(
160
            2012, 1, 1, 0, 0, 0
161
        )
162
        assert charge.index[0] == datetime.datetime(2012, 1, 1, 0, 0, 0)
163
        assert storage_content.index[-1] == datetime.datetime(
164
            2012, 1, 2, 23, 30, 0
165
        )
166
        assert charge.index[-1] == datetime.datetime(2012, 1, 2, 23, 30, 0)
167
168
    def test_time_index_with_last_time_point(self):
169
        storage_content = [
170
            v["sequences"]["storage_content"]
171
            for k, v in self.comp.items()
172
            if k[0].label == "storage"
173
        ][0]
174
        assert storage_content.iloc[0] == storage_content.iloc[-1]
175
176
        charge = [
177
            v["sequences"]["flow"]
178
            for k, v in self.flows.items()
179
            if k[1].label == "storage"
180
        ][0]
181
        assert storage_content.index[0] == datetime.datetime(
182
            2012, 1, 1, 0, 0, 0
183
        )
184
        assert charge.index[0] == datetime.datetime(2012, 1, 1, 0, 0, 0)
185
        assert storage_content.index[-1] == datetime.datetime(
186
            2012, 1, 3, 0, 0, 0
187
        )
188
        assert charge.index[-1] == datetime.datetime(2012, 1, 3, 0, 0, 0)
189
190
    def test_numeric_index(self):
191
        self.es.timeindex = None
192
        model = Model(self.es)
193
        model.receive_duals()
194
        model.solve()
195
        results = processing.results(self.model)
196
        flow = {k: v for k, v in results.items() if k[1] is not None}
197
        diesel_generator_out = [
198
            v["sequences"]["flow"]
199
            for k, v in flow.items()
200
            if k[0].label == "diesel_generator"
201
        ][0]
202
        assert diesel_generator_out.index[0] == 0
203
        assert diesel_generator_out.index[-1] == 72
204
        assert len(diesel_generator_out.index) == 73
205
206
        storage_content = [
207
            v["sequences"]["storage_content"]
208
            for k, v in self.comp.items()
209
            if k[0].label == "storage"
210
        ][0]
211
        assert storage_content.iloc[0] == storage_content.iloc[-1]
212
213
        charge = [
214
            v["sequences"]["flow"]
215
            for k, v in self.flows.items()
216
            if k[1].label == "storage"
217
        ][0]
218
        # Calculate the next storage content and verify it with the storage
219
        # content of the results (charging).
220
        # Charging - timestep (ts) with its timeincrement (ti)
221
        time = [(23, 1), (24, 0.5)]
222
        for ts, ti in time:
223
            assert (
224
                storage_content.iloc[ts] + charge.iloc[ts] * ti
225
            ) == pytest.approx(storage_content.iloc[ts + 1])
226
            assert self.es.timeincrement[ts] == ti
227
        assert charge.isnull().any()
228