forked from PyPSA/PyPSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_components.py
235 lines (173 loc) · 6.65 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
# -*- coding: utf-8 -*-
import numpy as np
import pytest
import pypsa
@pytest.fixture
def empty_network_5_buses():
# Set up empty network with 5 buses.
network = pypsa.Network()
n_buses = 5
for i in range(n_buses):
network.add("Bus", f"bus_{i}")
return network
def test_mremove(ac_dc_network):
"""
GIVEN the AC DC exemplary pypsa network.
WHEN two components of Generator are removed with mremove
THEN the generator dataframe and the time-dependent generator
dataframe should not contain the removed elements.
"""
network = ac_dc_network
generators = {"Manchester Wind", "Frankfurt Wind"}
network.mremove("Generator", generators)
assert not generators.issubset(network.generators.index)
assert not generators.issubset(network.generators_t.p_max_pu.columns)
def test_mremove_misspelled_component(ac_dc_network, caplog):
"""
GIVEN the AC DC exemplary pypsa network.
WHEN a misspelled component is removed with mremove
THEN the function should not change anything in the Line
component dataframe and an error should be logged.
"""
network = ac_dc_network
len_lines = len(network.lines.index)
network.mremove("Liness", ["0", "1"])
assert len_lines == len(network.lines.index)
assert caplog.records[-1].levelname == "ERROR"
def test_madd_static(empty_network_5_buses):
"""
GIVEN an empty PyPSA network with 5 buses.
WHEN multiple components of Load are added to the network with
madd and attribute p_set
THEN the corresponding load components should be in the index of
the static load dataframe. Also the column p_set should contain any
value greater than 0.
"""
buses = empty_network_5_buses.buses.index
# Add load components at every bus with attribute p_set.
load_names = "load_" + buses
empty_network_5_buses.madd(
"Load",
load_names,
bus=buses,
p_set=3,
)
assert load_names.equals(empty_network_5_buses.loads.index)
assert (empty_network_5_buses.loads.p_set == 3).all()
def test_madd_t(empty_network_5_buses):
"""
GIVEN an empty PyPSA network with 5 buses and 7 snapshots.
WHEN multiple components of Load are added to the network with
madd and attribute p_set
THEN the corresponding load components should be in the columns
of the time-dependent load_t dataframe. Also, the shape of the
dataframe should resemble 7 snapshots x 5 buses.
"""
# Set up empty network with 5 buses and 7 snapshots.
snapshots = range(7)
empty_network_5_buses.set_snapshots(snapshots)
buses = empty_network_5_buses.buses.index
# Add load component at every bus with time-dependent attribute p_set.
load_names = "load_" + buses
empty_network_5_buses.madd(
"Load",
load_names,
bus=buses,
p_set=np.random.rand(len(snapshots), len(buses)),
)
assert load_names.equals(empty_network_5_buses.loads_t.p_set.columns)
assert empty_network_5_buses.loads_t.p_set.shape == (len(snapshots), len(buses))
def test_madd_misspelled_component(empty_network_5_buses, caplog):
"""
GIVEN an empty PyPSA network with 5 buses.
WHEN multiple components of a misspelled component are added
THEN the function should not change anything and an error should
be logged.
"""
misspelled_component = "Generatro"
empty_network_5_buses.madd(
misspelled_component,
["g_1", "g_2"],
bus=["bus_1", "bus_2"],
)
assert empty_network_5_buses.generators.empty
assert caplog.records[-1].levelname == "ERROR"
assert caplog.records[-1].message == (
f"Component class {misspelled_component} not found"
)
def test_madd_duplicated_index(empty_network_5_buses, caplog):
"""
GIVEN an empty PyPSA network with 5 buses.
WHEN adding generators with the same name
THEN the function should fail and an error should be logged.
"""
empty_network_5_buses.madd(
"Generator",
["g_1", "g_1"],
bus=["bus_1", "bus_2"],
)
assert caplog.records[-1].levelname == "ERROR"
assert caplog.records[-1].message == (
"Error, new components for Generator are not unique"
)
def test_madd_defaults(empty_network_5_buses):
"""
GIVEN an empty PyPSA network with 5 buses.
WHEN adding multiple components of Generator and Load with madd
THEN the defaults should be set correctly according to
n.component_attrs.
"""
gen_names = ["g_1", "g_2"]
empty_network_5_buses.madd(
"Generator",
gen_names,
bus=["bus_1", "bus_2"],
)
line_names = ["l_1", "l_2"]
empty_network_5_buses.madd(
"Load",
line_names,
bus=["bus_1", "bus_2"],
)
assert empty_network_5_buses.generators.loc[gen_names[0], "control"] == (
empty_network_5_buses.component_attrs.Generator.loc["control", "default"]
)
assert empty_network_5_buses.loads.loc[line_names[0], "p_set"] == (
empty_network_5_buses.component_attrs.Load.loc["p_set", "default"]
)
def test_copy_default_behavior(ac_dc_network):
"""
GIVEN the AC DC exemplary pypsa network.
WHEN copying the network with timestamps
THEN the copied network should have the same generators, loads
and timestamps.
"""
snapshot = ac_dc_network.snapshots[2]
copied_network = ac_dc_network.copy()
loads = ac_dc_network.loads.index.tolist()
generators = ac_dc_network.generators.index.tolist()
copied_loads = copied_network.loads.index.tolist()
copied_generators = copied_network.generators.index.tolist()
assert loads == copied_loads
assert generators == copied_generators
assert not copied_network.snapshots.empty
assert snapshot in copied_network.snapshots
def test_copy_deep_copy_behavior(ac_dc_network):
"""
GIVEN the AC DC exemplary pypsa network.
WHEN copying the network and changing a component
THEN the original network should have not changed.
"""
copied_network = ac_dc_network.copy()
copied_network.loads.rename(index={"London": "Berlin"}, inplace=True)
assert ac_dc_network.loads.index[0] != copied_network.loads.index[0]
def test_copy_no_snapshot(ac_dc_network):
"""
GIVEN the AC DC exemplary pypsa network.
WHEN copying the network without snapshots
THEN the copied network should only have the current time index.
"""
snapshot = ac_dc_network.snapshots[2]
copied_network = ac_dc_network.copy(with_time=False, snapshots=snapshot)
assert copied_network.snapshots.size == 1
assert snapshot not in copied_network.snapshots