1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Tests C{ext2int} and C{int2ext}.
18 """
19
20 from numpy import ones, delete, c_, r_
21
22 from pypower.loadcase import loadcase
23 from pypower.ext2int import ext2int
24 from pypower.int2ext import int2ext
25
26 from pypower.t.t_begin import t_begin
27 from pypower.t.t_end import t_end
28 from pypower.t.t_is import t_is
29
30 from pypower.t.t_case_ext import t_case_ext
31 from pypower.t.t_case_int import t_case_int
32
33
35 """Tests C{ext2int} and C{int2ext}.
36
37 @author: Ray Zimmerman (PSERC Cornell)
38 @author: Richard Lincoln
39 """
40 t_begin(85, quiet)
41
42
43 t = 'ppc = ext2int(ppc) : '
44 ppce = loadcase(t_case_ext())
45 ppci = loadcase(t_case_int())
46 ppc = ext2int(ppce)
47 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
48 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
49 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
50 t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost'])
51 t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas'])
52 t_is(ppc['A'], ppci['A'], 12, [t, 'A'])
53 t_is(ppc['N'], ppci['N'], 12, [t, 'N'])
54 t = 'ppc = ext2int(ppc) - repeat : '
55 ppc = ext2int(ppc)
56 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
57 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
58 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
59 t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost'])
60 t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas'])
61 t_is(ppc['A'], ppci['A'], 12, [t, 'A'])
62 t_is(ppc['N'], ppci['N'], 12, [t, 'N'])
63 t = 'ppc = int2ext(ppc) : '
64 ppc = int2ext(ppc)
65 t_is(ppc['bus'], ppce['bus'], 12, [t, 'bus'])
66 t_is(ppc['branch'], ppce['branch'], 12, [t, 'branch'])
67 t_is(ppc['gen'], ppce['gen'], 12, [t, 'gen'])
68 t_is(ppc['gencost'], ppce['gencost'], 12, [t, 'gencost'])
69 t_is(ppc['areas'], ppce['areas'], 12, [t, 'areas'])
70 t_is(ppc['A'], ppce['A'], 12, [t, 'A'])
71 t_is(ppc['N'], ppce['N'], 12, [t, 'N'])
72
73
74 t = 'val = ext2int(ppc, val, \'bus\')'
75 ppc = ext2int(ppce)
76 got = ext2int(ppc, ppce['xbus'], 'bus')
77 ex = ppce['xbus']
78 ex = delete(ex, 5, 0)
79 t_is(got, ex, 12, t)
80 t = 'val = int2ext(ppc, val, oldval, \'bus\')'
81 tmp = ones(ppce['xbus'].shape)
82 tmp[5, :] = ppce['xbus'][5, :]
83 got = int2ext(ppc, ex, tmp, 'bus')
84 t_is(got, ppce['xbus'], 12, t)
85
86 t = 'val = ext2int(ppc, val, \'bus\', 1)'
87 got = ext2int(ppc, ppce['xbus'], 'bus', 1)
88 ex = ppce['xbus']
89 ex = delete(ex, 5, 1)
90 t_is(got, ex, 12, t)
91 t = 'val = int2ext(ppc, val, oldval, \'bus\', 1)'
92 tmp = ones(ppce['xbus'].shape)
93 tmp[:, 5] = ppce['xbus'][:, 5]
94 got = int2ext(ppc, ex, tmp, 'bus', 1)
95 t_is(got, ppce['xbus'], 12, t)
96
97 t = 'val = ext2int(ppc, val, \'gen\')'
98 got = ext2int(ppc, ppce['xgen'], 'gen')
99 ex = ppce['xgen'][[3, 1, 0], :]
100 t_is(got, ex, 12, t)
101 t = 'val = int2ext(ppc, val, oldval, \'gen\')'
102 tmp = ones(ppce['xgen'].shape)
103 tmp[2, :] = ppce['xgen'][2, :]
104 got = int2ext(ppc, ex, tmp, 'gen')
105 t_is(got, ppce['xgen'], 12, t)
106
107 t = 'val = ext2int(ppc, val, \'gen\', 1)'
108 got = ext2int(ppc, ppce['xgen'], 'gen', 1)
109 ex = ppce['xgen'][:, [3, 1, 0]]
110 t_is(got, ex, 12, t)
111 t = 'val = int2ext(ppc, val, oldval, \'gen\', 1)'
112 tmp = ones(ppce['xgen'].shape)
113 tmp[:, 2] = ppce['xgen'][:, 2]
114 got = int2ext(ppc, ex, tmp, 'gen', 1)
115 t_is(got, ppce['xgen'], 12, t)
116
117 t = 'val = ext2int(ppc, val, \'branch\')'
118 got = ext2int(ppc, ppce['xbranch'], 'branch')
119 ex = ppce['xbranch']
120 ex = delete(ex, 6, 0)
121 t_is(got, ex, 12, t)
122 t = 'val = int2ext(ppc, val, oldval, \'branch\')'
123 tmp = ones(ppce['xbranch'].shape)
124 tmp[6, :] = ppce['xbranch'][6, :]
125 got = int2ext(ppc, ex, tmp, 'branch')
126 t_is(got, ppce['xbranch'], 12, t)
127
128 t = 'val = ext2int(ppc, val, \'branch\', 1)'
129 got = ext2int(ppc, ppce['xbranch'], 'branch', 1)
130 ex = ppce['xbranch']
131 ex = delete(ex, 6, 1)
132 t_is(got, ex, 12, t)
133 t = 'val = int2ext(ppc, val, oldval, \'branch\', 1)'
134 tmp = ones(ppce['xbranch'].shape)
135 tmp[:, 6] = ppce['xbranch'][:, 6]
136 got = int2ext(ppc, ex, tmp, 'branch', 1)
137 t_is(got, ppce['xbranch'], 12, t)
138
139 t = 'val = ext2int(ppc, val, {\'branch\', \'gen\', \'bus\'})'
140 got = ext2int(ppc, ppce['xrows'], ['branch', 'gen', 'bus'])
141 ex = r_[ppce['xbranch'][range(6) + range(7, 10), :4],
142 ppce['xgen'][[3, 1, 0], :],
143 ppce['xbus'][range(5) + range(6, 10), :4],
144 -1 * ones((2, 4))]
145 t_is(got, ex, 12, t)
146 t = 'val = int2ext(ppc, val, oldval, {\'branch\', \'gen\', \'bus\'})'
147 tmp1 = ones(ppce['xbranch'][:, :4].shape)
148 tmp1[6, :4] = ppce['xbranch'][6, :4]
149 tmp2 = ones(ppce['xgen'].shape)
150 tmp2[2, :] = ppce['xgen'][2, :]
151 tmp3 = ones(ppce['xbus'][:, :4].shape)
152 tmp3[5, :4] = ppce['xbus'][5, :4]
153 tmp = r_[tmp1, tmp2, tmp3]
154 got = int2ext(ppc, ex, tmp, ['branch', 'gen', 'bus'])
155 t_is(got, ppce['xrows'], 12, t)
156
157 t = 'val = ext2int(ppc, val, {\'branch\', \'gen\', \'bus\'}, 1)'
158 got = ext2int(ppc, ppce['xcols'], ['branch', 'gen', 'bus'], 1)
159 ex = r_[ppce['xbranch'][range(6) + range(7, 10), :4],
160 ppce['xgen'][[3, 1, 0], :],
161 ppce['xbus'][range(5) + range(6, 10), :4],
162 -1 * ones((2, 4))].T
163 t_is(got, ex, 12, t)
164 t = 'val = int2ext(ppc, val, oldval, {\'branch\', \'gen\', \'bus\'}, 1)'
165 tmp1 = ones(ppce['xbranch'][:, :4].shape)
166 tmp1[6, :4] = ppce['xbranch'][6, :4]
167 tmp2 = ones(ppce['xgen'].shape)
168 tmp2[2, :] = ppce['xgen'][2, :]
169 tmp3 = ones(ppce['xbus'][:, :4].shape)
170 tmp3[5, :4] = ppce['xbus'][5, :4]
171 tmp = r_[tmp1, tmp2, tmp3].T
172 got = int2ext(ppc, ex, tmp, ['branch', 'gen', 'bus'], 1)
173 t_is(got, ppce['xcols'], 12, t)
174
175
176 t = 'ppc = ext2int(ppc, field, \'bus\')'
177 ppc = ext2int(ppce)
178 ex = ppce['xbus']
179 ex = delete(ex, 5, 0)
180 got = ext2int(ppc, 'xbus', 'bus')
181 t_is(got['xbus'], ex, 12, t)
182 t = 'ppc = int2ext(ppc, field, \'bus\')'
183 got = int2ext(got, 'xbus', ordering='bus')
184 t_is(got['xbus'], ppce['xbus'], 12, t)
185
186 t = 'ppc = ext2int(ppc, field, \'bus\', 1)'
187 ex = ppce['xbus']
188 ex = delete(ex, 5, 1)
189 got = ext2int(ppc, 'xbus', 'bus', 1)
190 t_is(got['xbus'], ex, 12, t)
191 t = 'ppc = int2ext(ppc, field, \'bus\', 1)'
192 got = int2ext(got, 'xbus', ordering='bus', dim=1)
193 t_is(got['xbus'], ppce['xbus'], 12, t)
194
195 t = 'ppc = ext2int(ppc, field, \'gen\')'
196 ex = ppce['xgen'][[3, 1, 0], :]
197 got = ext2int(ppc, 'xgen', 'gen')
198 t_is(got['xgen'], ex, 12, t)
199 t = 'ppc = int2ext(ppc, field, \'gen\')'
200 got = int2ext(got, 'xgen', ordering='gen')
201 t_is(got['xgen'], ppce['xgen'], 12, t)
202
203 t = 'ppc = ext2int(ppc, field, \'gen\', 1)'
204 ex = ppce['xgen'][:, [3, 1, 0]]
205 got = ext2int(ppc, 'xgen', 'gen', 1)
206 t_is(got['xgen'], ex, 12, t)
207 t = 'ppc = int2ext(ppc, field, \'gen\', 1)'
208 got = int2ext(got, 'xgen', ordering='gen', dim=1)
209 t_is(got['xgen'], ppce['xgen'], 12, t)
210
211 t = 'ppc = ext2int(ppc, field, \'branch\')'
212 ex = ppce['xbranch']
213 ex = delete(ex, 6, 0)
214 got = ext2int(ppc, 'xbranch', 'branch')
215 t_is(got['xbranch'], ex, 12, t)
216 t = 'ppc = int2ext(ppc, field, \'branch\')'
217 got = int2ext(got, 'xbranch', ordering='branch')
218 t_is(got['xbranch'], ppce['xbranch'], 12, t)
219
220 t = 'ppc = ext2int(ppc, field, \'branch\', 1)'
221 ex = ppce['xbranch']
222 ex = delete(ex, 6, 1)
223 got = ext2int(ppc, 'xbranch', 'branch', 1)
224 t_is(got['xbranch'], ex, 12, t)
225 t = 'ppc = int2ext(ppc, field, \'branch\', 1)'
226 got = int2ext(got, 'xbranch', ordering='branch', dim=1)
227 t_is(got['xbranch'], ppce['xbranch'], 12, t)
228
229 t = 'ppc = ext2int(ppc, field, {\'branch\', \'gen\', \'bus\'})'
230 ex = r_[ppce['xbranch'][range(6) + range(7, 10), :4],
231 ppce['xgen'][[3, 1, 0], :],
232 ppce['xbus'][range(5) + range(6, 10), :4],
233 -1 * ones((2, 4))]
234 got = ext2int(ppc, 'xrows', ['branch', 'gen', 'bus'])
235 t_is(got['xrows'], ex, 12, t)
236 t = 'ppc = int2ext(ppc, field, {\'branch\', \'gen\', \'bus\'})'
237 got = int2ext(got, 'xrows', ordering=['branch', 'gen', 'bus'])
238 t_is(got['xrows'], ppce['xrows'], 12, t)
239
240 t = 'ppc = ext2int(ppc, field, {\'branch\', \'gen\', \'bus\'}, 1)'
241 ex = r_[ppce['xbranch'][range(6) + range(7, 10), :4],
242 ppce['xgen'][[3, 1, 0], :],
243 ppce['xbus'][range(5) + range(6, 10), :4],
244 -1 * ones((2, 4))].T
245 got = ext2int(ppc, 'xcols', ['branch', 'gen', 'bus'], 1)
246 t_is(got['xcols'], ex, 12, t)
247 t = 'ppc = int2ext(ppc, field, {\'branch\', \'gen\', \'bus\'})'
248 got = int2ext(got, 'xcols', ordering=['branch', 'gen', 'bus'], dim=1)
249 t_is(got['xcols'], ppce['xcols'], 12, t)
250
251 t = 'ppc = ext2int(ppc, {\'field1\', \'field2\'}, ordering)'
252 ex = ppce['x']['more'][[3, 1, 0], :]
253 got = ext2int(ppc, ['x', 'more'], 'gen')
254 t_is(got['x']['more'], ex, 12, t)
255 t = 'ppc = int2ext(ppc, {\'field1\', \'field2\'}, ordering)'
256 got = int2ext(got, ['x', 'more'], ordering='gen')
257 t_is(got['x']['more'], ppce['x']['more'], 12, t)
258
259 t = 'ppc = ext2int(ppc, {\'field1\', \'field2\'}, ordering, 1)'
260 ex = ppce['x']['more'][:, [3, 1, 0]]
261 got = ext2int(ppc, ['x', 'more'], 'gen', 1)
262 t_is(got['x']['more'], ex, 12, t)
263 t = 'ppc = int2ext(ppc, {\'field1\', \'field2\'}, ordering, 1)'
264 got = int2ext(got, ['x', 'more'], ordering='gen', dim=1)
265 t_is(got['x']['more'], ppce['x']['more'], 12, t)
266
267
268 t = 'ppc = ext2int(ppc) - bus/gen/branch only : '
269 ppce = loadcase(t_case_ext())
270 ppci = loadcase(t_case_int())
271 del ppce['gencost']
272 del ppce['areas']
273 del ppce['A']
274 del ppce['N']
275 del ppci['gencost']
276 del ppci['areas']
277 del ppci['A']
278 del ppci['N']
279 ppc = ext2int(ppce)
280 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
281 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
282 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
283
284 t = 'ppc = ext2int(ppc) - no areas/A : '
285 ppce = loadcase(t_case_ext())
286 ppci = loadcase(t_case_int())
287 del ppce['areas']
288 del ppce['A']
289 del ppci['areas']
290 del ppci['A']
291 ppc = ext2int(ppce)
292 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
293 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
294 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
295 t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost'])
296 t_is(ppc['N'], ppci['N'], 12, [t, 'N'])
297
298 t = 'ppc = ext2int(ppc) - Qg cost, no N : '
299 ppce = loadcase(t_case_ext())
300 ppci = loadcase(t_case_int())
301 del ppce['N']
302 del ppci['N']
303 ppce['gencost'] = c_[ppce['gencost'], ppce['gencost']]
304 ppci['gencost'] = c_[ppci['gencost'], ppci['gencost']]
305 ppc = ext2int(ppce)
306 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
307 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
308 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
309 t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost'])
310 t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas'])
311 t_is(ppc['A'], ppci['A'], 12, [t, 'A'])
312
313 t = 'ppc = ext2int(ppc) - A, N are DC sized : '
314 ppce = loadcase(t_case_ext())
315 ppci = loadcase(t_case_int())
316 eVmQgcols = range(10, 20) + range(24, 28)
317 iVmQgcols = range(9, 18) + range(21, 24)
318 ppce['A'] = delete(ppce['A'], eVmQgcols, 1)
319 ppce['N'] = delete(ppce['N'], eVmQgcols, 1)
320 ppci['A'] = delete(ppci['A'], iVmQgcols, 1)
321 ppci['N'] = delete(ppci['N'], iVmQgcols, 1)
322 ppc = ext2int(ppce)
323 t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus'])
324 t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch'])
325 t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen'])
326 t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost'])
327 t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas'])
328 t_is(ppc['A'], ppci['A'], 12, [t, 'A'])
329 t_is(ppc['N'], ppci['N'], 12, [t, 'N'])
330 t = 'ppc = int2ext(ppc) - A, N are DC sized : '
331 ppc = int2ext(ppc)
332 t_is(ppc['bus'], ppce['bus'], 12, [t, 'bus'])
333 t_is(ppc['branch'], ppce['branch'], 12, [t, 'branch'])
334 t_is(ppc['gen'], ppce['gen'], 12, [t, 'gen'])
335 t_is(ppc['gencost'], ppce['gencost'], 12, [t, 'gencost'])
336 t_is(ppc['areas'], ppce['areas'], 12, [t, 'areas'])
337 t_is(ppc['A'], ppce['A'], 12, [t, 'A'])
338 t_is(ppc['N'], ppce['N'], 12, [t, 'N'])
339
340 t_end()
341
342
343 if __name__ == '__main__':
344 t_ext2int2ext(False)
345