|
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
|
|
|
SPDX-FileCopyrightText: Stephan Günther |
|
7
|
|
|
|
|
8
|
|
|
SPDX-License-Identifier: MIT |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
|
|
import pandas as pd |
|
12
|
|
|
import pytest |
|
13
|
|
|
|
|
14
|
|
|
from oemof import solph |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def test_energysystem_with_datetimeindex_infer_last_interval(): |
|
18
|
|
|
"""Test EnergySystem with DatetimeIndex (equidistant)""" |
|
19
|
|
|
datetimeindex = pd.date_range("1/1/2012", periods=24, freq="H") |
|
20
|
|
|
es = solph.EnergySystem(timeindex=datetimeindex) |
|
21
|
|
|
assert es.timeincrement[1] == 1.0 |
|
22
|
|
|
assert es.timeincrement.sum() == 24 |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_energysystem_with_datetimeindex(): |
|
26
|
|
|
datetimeindex = pd.date_range("1/1/2012", periods=24, freq="H") |
|
27
|
|
|
es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=False) |
|
28
|
|
|
assert es.timeincrement[1] == 1.0 |
|
29
|
|
|
assert es.timeincrement.sum() == 23 |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def test_energysystem_with_datetimeindex_non_equidistant_infer_last_interval(): |
|
33
|
|
|
"""Test EnergySystem with DatetimeIndex (non-equidistant)""" |
|
34
|
|
|
dtindex1 = pd.date_range("1/1/2012", periods=24, freq="H") |
|
35
|
|
|
dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min") |
|
36
|
|
|
dtindex = dtindex1.union(dtindex2) |
|
37
|
|
|
msg = ( |
|
38
|
|
|
"You cannot infer the last interval if the 'freq' attribute of your " |
|
39
|
|
|
"DatetimeIndex is None." |
|
40
|
|
|
) |
|
41
|
|
|
with pytest.raises(AttributeError, match=msg): |
|
42
|
|
|
solph.EnergySystem(timeindex=dtindex) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_energysystem_with_datetimeindex_non_equidistant(): |
|
46
|
|
|
"""Test EnergySystem with DatetimeIndex (non-equidistant)""" |
|
47
|
|
|
dtindex1 = pd.date_range("1/1/2012", periods=24, freq="H") |
|
48
|
|
|
dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min") |
|
49
|
|
|
dtindex = dtindex1.union(dtindex2) |
|
50
|
|
|
es = solph.EnergySystem(timeindex=dtindex, infer_last_interval=False) |
|
51
|
|
|
assert es.timeincrement.sum() == 48.0 |
|
52
|
|
|
assert es.timeincrement[0] == 1 |
|
53
|
|
|
assert es.timeincrement[25] == 0.5 |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def test_energysystem_with_numeric_index_infer_last_interval(): |
|
57
|
|
|
"""Test EnergySystem with numeric index (equidistant)""" |
|
58
|
|
|
time_increments = [1, 1, 1, 1, 1] |
|
59
|
|
|
es = solph.EnergySystem(timeincrement=time_increments) |
|
60
|
|
|
assert es.timeincrement[1] == 1.0 |
|
61
|
|
|
assert pd.Series(es.timeincrement).sum() == 5 |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_energysystem_with_numeric_index(): |
|
65
|
|
|
"""Test EnergySystem with numeric index (equidistant)""" |
|
66
|
|
|
time_increments = [1, 1, 1, 1, 1] |
|
67
|
|
|
es = solph.EnergySystem( |
|
68
|
|
|
timeincrement=time_increments, infer_last_interval=False |
|
69
|
|
|
) |
|
70
|
|
|
assert es.timeincrement[1] == 1.0 |
|
71
|
|
|
assert pd.Series(es.timeincrement).sum() == 5 |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_energysystem_with_numeric_index_non_equidistant_infer_last_interval(): |
|
75
|
|
|
""" |
|
76
|
|
|
Test EnergySystem with DatetimeIndex (non-equidistant) |
|
77
|
|
|
'infer_last_interval=True/False' does not have any effect. |
|
78
|
|
|
""" |
|
79
|
|
|
time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5] |
|
80
|
|
|
|
|
81
|
|
|
es = solph.EnergySystem( |
|
82
|
|
|
timeincrement=time_increments, infer_last_interval=True |
|
83
|
|
|
) |
|
84
|
|
|
assert pd.Series(es.timeincrement).sum() == 7.0 |
|
85
|
|
|
assert es.timeincrement[0] == 1 |
|
86
|
|
|
assert es.timeincrement[6] == 0.5 |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
def test_energysystem_with_numeric_index_non_equidistant(): |
|
90
|
|
|
""" |
|
91
|
|
|
Test EnergySystem with DatetimeIndex (non-equidistant) |
|
92
|
|
|
'infer_last_interval=True/False' does not have any effect. |
|
93
|
|
|
""" |
|
94
|
|
|
time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5] |
|
95
|
|
|
es = solph.EnergySystem( |
|
96
|
|
|
timeincrement=time_increments, infer_last_interval=False |
|
97
|
|
|
) |
|
98
|
|
|
assert pd.Series(es.timeincrement).sum() == 7.0 |
|
99
|
|
|
assert es.timeincrement[0] == 1 |
|
100
|
|
|
assert es.timeincrement[8] == 0.25 |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def test_model_timeincrement_with_valid_timeindex(): |
|
104
|
|
|
datetimeindex = pd.date_range("1/1/2012", periods=5, freq="H") |
|
105
|
|
|
es = solph.EnergySystem(timeindex=datetimeindex) |
|
106
|
|
|
m = solph._models.BaseModel(es) |
|
107
|
|
|
assert es.timeincrement.sum() == 5 |
|
108
|
|
|
assert m.timeincrement.sum() == 5 |
|
109
|
|
|
assert m.timeincrement[2] == 1 |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
def test_timeincrement_with_non_valid_timeindex(): |
|
113
|
|
|
with pytest.raises( |
|
114
|
|
|
TypeError, match="Parameter 'timeindex' has to be of type" |
|
115
|
|
|
): |
|
116
|
|
|
solph.EnergySystem(timeindex=4) |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def test_conflicting_time_index(): |
|
120
|
|
|
msg = ( |
|
121
|
|
|
"Specifying the timeincrement and the timeindex parameter at the same " |
|
122
|
|
|
"time is not allowed" |
|
123
|
|
|
) |
|
124
|
|
|
with pytest.raises(AttributeError, match=msg): |
|
125
|
|
|
solph.EnergySystem( |
|
126
|
|
|
timeindex=pd.date_range("1/1/2012", periods=2, freq="H"), |
|
127
|
|
|
timeincrement=[1, 2, 3, 4], |
|
128
|
|
|
) |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
def test_missing_timeincrement(): |
|
132
|
|
|
msg = ( |
|
133
|
|
|
"The EnergySystem needs to have a valid 'timeincrement' attribute to " |
|
134
|
|
|
"build a model." |
|
135
|
|
|
) |
|
136
|
|
|
es = solph.EnergySystem() |
|
137
|
|
|
with pytest.raises(AttributeError, match=msg): |
|
138
|
|
|
solph.Model(es) |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
def test_overwrite_timeincrement(): |
|
142
|
|
|
es = solph.EnergySystem( |
|
143
|
|
|
timeindex=pd.date_range("1/1/2012", periods=2, freq="H") |
|
144
|
|
|
) |
|
145
|
|
|
assert es.timeincrement[0] == 1 |
|
146
|
|
|
m = solph._models.BaseModel(es, timeincrement=[3]) |
|
147
|
|
|
assert m.timeincrement[0] == 3 |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
def test_model_timeincrement_list(): |
|
151
|
|
|
es = solph.EnergySystem() |
|
152
|
|
|
m = solph._models.BaseModel(es, timeincrement=[0, 1, 2, 3]) |
|
153
|
|
|
assert m.timeincrement[3] == 3 |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
def test_nonequ_inconsistent_timeindex(): |
|
157
|
|
|
# with pytest.raises(IndexError): |
|
158
|
|
|
timeindex_one = pd.date_range("1/1/2019", periods=1, freq="H") |
|
159
|
|
|
timeindex_hourly = pd.date_range("1/1/2019", periods=3, freq="H") |
|
160
|
|
|
timeindex_45mins = pd.date_range("1/1/2019", periods=2, freq="45min") |
|
161
|
|
|
timeindex1 = timeindex_one.append(timeindex_hourly) |
|
162
|
|
|
timeindex2 = timeindex_hourly.append([timeindex_45mins]) |
|
163
|
|
|
with pytest.raises(TypeError): |
|
164
|
|
|
solph.EnergySystem(timeindex=timeindex1, infer_last_interval=False) |
|
165
|
|
|
with pytest.raises(TypeError): |
|
166
|
|
|
solph.EnergySystem(timeindex=timeindex2, infer_last_interval=False) |
|
167
|
|
|
|