-
Notifications
You must be signed in to change notification settings - Fork 465
/
consistency_checks.py
212 lines (171 loc) · 9.74 KB
/
consistency_checks.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
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.
import pandas as pd
from numpy import allclose, isclose
from pandapower.pf.runpp_3ph import runpp_3ph
from pandapower.results import get_relevant_elements
import pandapower as pp
def runpp_with_consistency_checks(net, **kwargs):
pp.runpp(net, **kwargs)
consistency_checks(net)
return True
def runpp_3ph_with_consistency_checks(net, **kwargs):
runpp_3ph(net, **kwargs)
consistency_checks_3ph(net)
return True
def rundcpp_with_consistency_checks(net, **kwargs):
pp.rundcpp(net, **kwargs)
consistency_checks(net, test_q=False)
return True
def consistency_checks(net, rtol=1e-3, test_q=True):
indices_consistent(net)
branch_loss_consistent_with_bus_feed_in(net, rtol)
element_power_consistent_with_bus_power(net, rtol, test_q)
def indices_consistent(net):
elements = get_relevant_elements()
for element in elements:
e_idx = net[element].index
res_idx = net["res_" + element].index
assert len(e_idx) == len(res_idx), "length of %s bus and res_%s indices do not match"%(element, element)
assert all(e_idx == res_idx), "%s bus and res_%s indices do not match"%(element, element)
def branch_loss_consistent_with_bus_feed_in(net, atol=1e-2):
"""
The surpluss of bus feed summed over all buses always has to be equal to the sum of losses in
all branches.
"""
# Active Power
bus_surplus_p = -net.res_bus.p_mw.sum()
bus_surplus_q = -net.res_bus.q_mvar.sum()
branch_loss_p = net.res_line.pl_mw.values.sum() + net.res_trafo.pl_mw.values.sum() + \
net.res_trafo3w.pl_mw.values.sum() + net.res_impedance.pl_mw.values.sum() + \
net.res_dcline.pl_mw.values.sum()
branch_loss_q = net.res_line.ql_mvar.values.sum() + net.res_trafo.ql_mvar.values.sum() + \
net.res_trafo3w.ql_mvar.values.sum() + net.res_impedance.ql_mvar.values.sum() + \
net.res_dcline.q_to_mvar.values.sum() + net.res_dcline.q_from_mvar.values.sum()
try:
assert isclose(bus_surplus_p, branch_loss_p, atol=atol)
except AssertionError:
raise AssertionError("Branch losses are %.4f MW, but power generation at the buses exceeds the feedin by %.4f MW"%(branch_loss_p, bus_surplus_p))
try:
assert isclose(bus_surplus_q, branch_loss_q, atol=atol)
except AssertionError:
raise AssertionError("Branch losses are %.4f MVar, but power generation at the buses exceeds the feedin by %.4f MVar"%(branch_loss_q, bus_surplus_q))
def element_power_consistent_with_bus_power(net, rtol=1e-2, test_q=True):
"""
The bus feed-in at each node has to be equal to the sum of the element feed ins at each node.
"""
bus_p = pd.Series(data=0., index=net.bus.index)
bus_q = pd.Series(data=0., index=net.bus.index)
for idx, tab in net.ext_grid.iterrows():
if tab.in_service:
bus_p.at[tab.bus] -= net.res_ext_grid.p_mw.at[idx]
bus_q.at[tab.bus] -= net.res_ext_grid.q_mvar.at[idx]
for idx, tab in net.gen.iterrows():
if tab.in_service:
bus_p.at[tab.bus] -= net.res_gen.p_mw.at[idx]
bus_q.at[tab.bus] -= net.res_gen.q_mvar.at[idx]
for idx, tab in net.load.iterrows():
bus_p.at[tab.bus] += net.res_load.p_mw.at[idx]
bus_q.at[tab.bus] += net.res_load.q_mvar.at[idx]
for idx, tab in net.sgen.iterrows():
bus_p.at[tab.bus] -= net.res_sgen.p_mw.at[idx]
bus_q.at[tab.bus] -= net.res_sgen.q_mvar.at[idx]
for idx, tab in net.storage.iterrows():
bus_p.at[tab.bus] += net.res_storage.p_mw.at[idx]
bus_q.at[tab.bus] += net.res_storage.q_mvar.at[idx]
for idx, tab in net.shunt.iterrows():
bus_p.at[tab.bus] += net.res_shunt.p_mw.at[idx]
bus_q.at[tab.bus] += net.res_shunt.q_mvar.at[idx]
for idx, tab in net.ward.iterrows():
bus_p.at[tab.bus] += net.res_ward.p_mw.at[idx]
bus_q.at[tab.bus] += net.res_ward.q_mvar.at[idx]
for idx, tab in net.xward.iterrows():
bus_p.at[tab.bus] += net.res_xward.p_mw.at[idx]
bus_q.at[tab.bus] += net.res_xward.q_mvar.at[idx]
assert allclose(net.res_bus.p_mw.values, bus_p.values, equal_nan=True, rtol=rtol)
if test_q:
assert allclose(net.res_bus.q_mvar.values, bus_q.values, equal_nan=True, rtol=rtol)
def consistency_checks_3ph(net, rtol=2e-3):
indices_consistent_3ph(net)
branch_loss_consistent_with_bus_feed_in_3ph(net, rtol)
element_power_consistent_with_bus_power_3ph(net, rtol)
def indices_consistent_3ph(net):
elements = get_relevant_elements("pf_3ph")
for element in elements:
e_idx = net[element].index
res_idx = net["res_" + element+"_3ph"].index
assert len(e_idx) == len(res_idx), "length of %s bus and res_%s indices do not match"%(element, element)
assert all(e_idx == res_idx), "%s bus and res_%s indices do not match"%(element, element)
def branch_loss_consistent_with_bus_feed_in_3ph(net, atol=1e-2):
"""
The surpluss of bus feed summed over all buses always has to be equal to the sum of losses in
all branches.
"""
bus_surplus_p = -net.res_bus_3ph[["p_a_mw", "p_b_mw", "p_c_mw"]].sum().sum()
bus_surplus_q = -net.res_bus_3ph[["q_a_mvar", "q_b_mvar", "q_c_mvar"]].sum().sum()
branch_loss_p = net.res_line_3ph.p_a_l_mw.sum() + net.res_trafo_3ph.p_a_l_mw.sum() + \
net.res_line_3ph.p_b_l_mw.sum() + net.res_trafo_3ph.p_b_l_mw.sum() + \
net.res_line_3ph.p_c_l_mw.sum() + net.res_trafo_3ph.p_c_l_mw.sum()
branch_loss_q = net.res_line_3ph.q_a_l_mvar.sum() + net.res_trafo_3ph.q_a_l_mvar.sum() + \
net.res_line_3ph.q_b_l_mvar.sum() + net.res_trafo_3ph.q_b_l_mvar.sum() + \
net.res_line_3ph.q_c_l_mvar.sum() + net.res_trafo_3ph.q_c_l_mvar.sum()
try:
assert isclose(bus_surplus_p, branch_loss_p, atol=atol)
except AssertionError:
raise AssertionError("Branch losses are %.4f MW, but power generation at the buses exceeds the feedin by %.4f MW"%(branch_loss_p, bus_surplus_p))
try:
assert isclose(bus_surplus_q, branch_loss_q, atol=atol)
except AssertionError:
raise AssertionError("Branch losses are %.4f MVar, but power generation at the buses exceeds the feedin by %.4f MVar"%(branch_loss_q, bus_surplus_q))
def element_power_consistent_with_bus_power_3ph(net, rtol=1e-2):
"""
The bus feed-in at each node has to be equal to the sum of the element feed ins at each node.
"""
bus_p_a = pd.Series(data=0., index=net.bus.index)
bus_q_a = pd.Series(data=0., index=net.bus.index)
bus_p_b = pd.Series(data=0., index=net.bus.index)
bus_q_b = pd.Series(data=0., index=net.bus.index)
bus_p_c = pd.Series(data=0., index=net.bus.index)
bus_q_c = pd.Series(data=0., index=net.bus.index)
for idx, tab in net.ext_grid.iterrows():
bus_p_a.at[tab.bus] -= net.res_ext_grid_3ph.p_a_mw.at[idx]
bus_q_a.at[tab.bus] -= net.res_ext_grid_3ph.q_a_mvar.at[idx]
bus_p_b.at[tab.bus] -= net.res_ext_grid_3ph.p_b_mw.at[idx]
bus_q_b.at[tab.bus] -= net.res_ext_grid_3ph.q_b_mvar.at[idx]
bus_p_c.at[tab.bus] -= net.res_ext_grid_3ph.p_c_mw.at[idx]
bus_q_c.at[tab.bus] -= net.res_ext_grid_3ph.q_c_mvar.at[idx]
for idx, tab in net.load.iterrows():
bus_p_a.at[tab.bus] += net.res_load_3ph.p_mw.at[idx]/3
bus_q_a.at[tab.bus] += net.res_load_3ph.q_mvar.at[idx] /3
bus_p_b.at[tab.bus] += net.res_load_3ph.p_mw.at[idx]/3
bus_q_b.at[tab.bus] += net.res_load_3ph.q_mvar.at[idx] /3
bus_p_c.at[tab.bus] += net.res_load_3ph.p_mw.at[idx]/3
bus_q_c.at[tab.bus] += net.res_load_3ph.q_mvar.at[idx] /3
for idx, tab in net.asymmetric_load.iterrows():
bus_p_a.at[tab.bus] += net.res_asymmetric_load_3ph.p_a_mw.at[idx]
bus_q_a.at[tab.bus] += net.res_asymmetric_load_3ph.q_a_mvar.at[idx]
bus_p_b.at[tab.bus] += net.res_asymmetric_load_3ph.p_b_mw.at[idx]
bus_q_b.at[tab.bus] += net.res_asymmetric_load_3ph.q_b_mvar.at[idx]
bus_p_c.at[tab.bus] += net.res_asymmetric_load_3ph.p_c_mw.at[idx]
bus_q_c.at[tab.bus] += net.res_asymmetric_load_3ph.q_c_mvar.at[idx]
for idx, tab in net.asymmetric_sgen.iterrows():
bus_p_a.at[tab.bus] -= net.res_asymmetric_sgen_3ph.p_a_mw.at[idx]
bus_q_a.at[tab.bus] -= net.res_asymmetric_sgen_3ph.q_a_mvar.at[idx]
bus_p_b.at[tab.bus] -= net.res_asymmetric_sgen_3ph.p_b_mw.at[idx]
bus_q_b.at[tab.bus] -= net.res_asymmetric_sgen_3ph.q_b_mvar.at[idx]
bus_p_c.at[tab.bus] -= net.res_asymmetric_sgen_3ph.p_c_mw.at[idx]
bus_q_c.at[tab.bus] -= net.res_asymmetric_sgen_3ph.q_c_mvar.at[idx]
for idx, tab in net.sgen.iterrows():
bus_p_a.at[tab.bus] -= net.res_sgen_3ph.p_mw.at[idx] / 3
bus_q_a.at[tab.bus] -= net.res_sgen_3ph.q_mvar.at[idx] / 3
bus_p_b.at[tab.bus] -= net.res_sgen_3ph.p_mw.at[idx] / 3
bus_q_b.at[tab.bus] -= net.res_sgen_3ph.q_mvar.at[idx] / 3
bus_p_c.at[tab.bus] -= net.res_sgen_3ph.p_mw.at[idx] / 3
bus_q_c.at[tab.bus] -= net.res_sgen_3ph.q_mvar.at[idx] / 3
assert allclose(net.res_bus_3ph.p_a_mw.values, bus_p_a.values, equal_nan=True, rtol=rtol)
assert allclose(net.res_bus_3ph.q_a_mvar.values, bus_q_a.values, equal_nan=True, rtol=rtol)
assert allclose(net.res_bus_3ph.p_b_mw.values, bus_p_b.values, equal_nan=True, rtol=rtol)
assert allclose(net.res_bus_3ph.q_b_mvar.values, bus_q_b.values, equal_nan=True, rtol=rtol)
assert allclose(net.res_bus_3ph.p_c_mw.values, bus_p_c.values, equal_nan=True, rtol=rtol)
assert allclose(net.res_bus_3ph.q_c_mvar.values, bus_q_c.values, equal_nan=True, rtol=rtol)