forked from oemof/oemof-solph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_components.py
294 lines (253 loc) · 9.3 KB
/
test_components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -
"""Tests of the component module.
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
by the contributors recorded in the version control history of the file,
available from its original location oemof/tests/test_components.py
SPDX-License-Identifier: MIT
"""
import warnings
import pytest
from oemof.tools.debugging import SuspiciousUsageWarning
from oemof.solph import Investment
from oemof.solph import NonConvex
from oemof.solph import components
from oemof.solph.buses import Bus
from oemof.solph.flows import Flow
# ********* GenericStorage *********
def test_generic_storage_1():
"""Duplicate definition inflow."""
bel = Bus()
with pytest.raises(AttributeError, match="Overdetermined."):
components.GenericStorage(
label="storage1",
inputs={bel: Flow(variable_costs=10e10)},
outputs={bel: Flow(variable_costs=10e10)},
loss_rate=0.00,
initial_storage_level=0,
invest_relation_input_output=1,
invest_relation_output_capacity=1,
invest_relation_input_capacity=1,
nominal_storage_capacity=Investment(),
inflow_conversion_factor=1,
outflow_conversion_factor=0.8,
)
def test_generic_storage_2():
"""Nominal value defined with investment model."""
bel = Bus()
with pytest.raises(
AttributeError,
match="For backward compatibility, the option investment overwrites",
):
components.GenericStorage(
label="storage3",
nominal_storage_capacity=45,
inputs={bel: Flow(variable_costs=10e10)},
outputs={bel: Flow(variable_costs=10e10)},
loss_rate=0.00,
initial_storage_level=0,
invest_relation_input_capacity=1 / 6,
invest_relation_output_capacity=1 / 6,
inflow_conversion_factor=1,
outflow_conversion_factor=0.8,
investment=Investment(ep_costs=23),
)
def test_generic_storage_3():
"""Nominal value defined with investment model."""
bel = Bus()
components.GenericStorage(
label="storage4",
nominal_storage_capacity=45,
inputs={bel: Flow(nominal_value=23, variable_costs=10e10)},
outputs={bel: Flow(nominal_value=7.5, variable_costs=10e10)},
loss_rate=0.00,
initial_storage_level=0,
inflow_conversion_factor=1,
outflow_conversion_factor=0.8,
)
def test_generic_storage_4():
"""Infeasible parameter combination for initial_storage_level"""
bel = Bus()
with pytest.raises(
ValueError, match="initial_storage_level must be greater"
):
components.GenericStorage(
label="storage4",
nominal_storage_capacity=10,
inputs={bel: Flow(variable_costs=10e10)},
outputs={bel: Flow(variable_costs=10e10)},
loss_rate=0.00,
initial_storage_level=0,
min_storage_level=0.1,
invest_relation_input_capacity=1 / 6,
invest_relation_output_capacity=1 / 6,
inflow_conversion_factor=1,
outflow_conversion_factor=0.8,
)
def test_generic_storage_with_non_convex_investment():
"""Tests error if `offset` and `existing` attribute are given."""
with pytest.raises(
AttributeError, match=r"Values for 'offset' and 'existing' are given"
):
bel = Bus()
components.GenericStorage(
label="storage4",
inputs={bel: Flow()},
outputs={bel: Flow()},
invest_relation_input_capacity=1 / 6,
invest_relation_output_capacity=1 / 6,
nominal_value=Investment(nonconvex=True, existing=5, maximum=25),
)
def test_generic_storage_with_non_convex_invest_maximum():
"""No investment maximum at nonconvex investment."""
with pytest.raises(
AttributeError, match=r"Please provide a maximum investment value"
):
bel = Bus()
components.GenericStorage(
label="storage6",
inputs={bel: Flow()},
outputs={bel: Flow()},
invest_relation_input_capacity=1 / 6,
invest_relation_output_capacity=1 / 6,
nominal_storage_capacity=Investment(nonconvex=True),
)
def test_generic_storage_with_convex_invest_offset():
"""Offset value is given and nonconvex is False."""
with pytest.raises(
AttributeError, match=r"If `nonconvex` is `False`, the `offset`"
):
bel = Bus()
components.GenericStorage(
label="storage6",
inputs={bel: Flow()},
outputs={bel: Flow()},
invest_relation_input_capacity=1 / 6,
invest_relation_output_capacity=1 / 6,
nominal_storage_capacity=Investment(offset=10),
)
def test_generic_storage_invest_warning():
with pytest.warns(FutureWarning):
bel = Bus()
components.GenericStorage(
label="storage7",
inputs={bel: Flow()},
outputs={bel: Flow()},
investment=Investment(),
)
def test_generic_storage_with_invest_and_fixed_losses_absolute():
"""
Storage with fixed losses in the investment mode but no minimum or existing
value is set an AttributeError is raised because this may result in storage
with zero capacity but fixed losses.
"""
msg = (
r"With fixed_losses_absolute > 0, either investment.existing or"
" investment.minimum has to be non-zero."
)
with pytest.raises(AttributeError, match=msg):
bel = Bus()
components.GenericStorage(
label="storage4",
inputs={bel: Flow()},
outputs={bel: Flow()},
nominal_storage_capacity=Investment(
ep_costs=23, minimum=0, existing=0
),
fixed_losses_absolute=[0, 0, 4],
)
def test_generic_storage_without_inputs():
with pytest.warns(SuspiciousUsageWarning):
components.GenericStorage(label="storage5")
def test_generic_storage_too_many_inputs():
msg = r"Only one input flow allowed in the GenericStorage storage6"
bel1 = Bus()
bel2 = Bus()
with pytest.raises(AttributeError, match=msg):
components.GenericStorage(
label="storage6",
inputs={bel1: Flow(), bel2: Flow()},
outputs={bel2: Flow()},
)
def test_generic_storage_too_many_outputs():
msg = r"Only one output flow allowed in the GenericStorage storage7"
bel1 = Bus()
bel2 = Bus()
with pytest.raises(AttributeError, match=msg):
components.GenericStorage(
label="storage7",
inputs={bel1: Flow()},
outputs={bel1: Flow(), bel2: Flow()},
)
# ********* OffsetConverter *********
def test_offsetconverter_without_nonconvex():
"""No NonConvex attribute is defined for any of the attached flows."""
with pytest.raises(
ValueError,
match=(
"Exactly one flow of the `OffsetConverter` must have the "
"`NonConvex` attribute."
),
):
b_diesel = Bus(label="bus_diesel")
b_el = Bus(label="bus_electricity")
components.OffsetConverter(
label="diesel_genset",
inputs={b_diesel: Flow()},
outputs={b_el: Flow()},
)
def test_offsetconverter_multiple_nonconvex():
"""NonConvex attribute is defined for more than one flow."""
with pytest.raises(
ValueError,
match=(
"Exactly one flow of the `OffsetConverter` must have the "
"`NonConvex` attribute."
),
):
b_diesel = Bus(label="bus_diesel")
b_heat = Bus(label="bus_heat")
components.OffsetConverter(
inputs={b_diesel: Flow(nonconvex=NonConvex())},
outputs={b_heat: Flow(nonconvex=NonConvex())},
)
def test_offsetconverter_investment_not_on_nonconvex():
"""Investment attribute is defined for a not NonConvex flow."""
with pytest.raises(
TypeError,
match=(
"`Investment` attribute must be defined only for the NonConvex "
"flow!"
),
):
b_diesel = Bus(label="bus_diesel")
b_heat = Bus(label="bus_heat")
components.OffsetConverter(
inputs={b_diesel: Flow(nominal_value=Investment(maximum=1))},
outputs={b_heat: Flow(nonconvex=NonConvex())},
)
# ********* GenericCHP *********
def test_generic_chp_without_warning():
warnings.filterwarnings("error", category=SuspiciousUsageWarning)
bel = Bus(label="electricityBus")
bth = Bus(label="heatBus")
bgas = Bus(label="commodityBus")
components.GenericCHP(
label="combined_cycle_extraction_turbine",
fuel_input={
bgas: Flow(custom_attributes={"H_L_FG_share_max": [0.183]})
},
electrical_output={
bel: Flow(
custom_attributes={
"P_max_woDH": [155.946],
"P_min_woDH": [68.787],
"Eta_el_max_woDH": [0.525],
"Eta_el_min_woDH": [0.444],
}
)
},
heat_output={bth: Flow(custom_attributes={"Q_CW_min": [10.552]})},
beta=[0.122],
back_pressure=False,
)
warnings.filterwarnings("always", category=SuspiciousUsageWarning)