-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpower-topology.c
331 lines (295 loc) · 11.2 KB
/
xpower-topology.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
** Copyright (C) 2011 XXX all rights reserved.
**
** This file defines Dispatcher Training Simulator (DTS) topology sturctures
** and routines
**
** Created by chuck-liu@qq.com on 01/11/2011
**
*/
#include "xpower-array.h"
#include "xpower-topology.h"
/*
* set breaker status to _XTTUE (changed) if breaker status
* changed
*/
static inline void _xtopology_update_changed_breaker(void){
int i;
_xlist *address;
_xmodel_section *section;
_xmodel_breaker *breaker;
for (i=0; i<_xused_of(section); i++){
section = _xpointer_of(section,i);
address = section->breakers;
while (address){
breaker = _xcontainer_of(address,_xmodel_breaker, velement);
if (breaker->status.close != breaker->old.close){
section->status.changed = _XTRUE;
breaker->old = breaker->status;
}
address = address->next;
}
}
}
/*
* equalize the topology bus number within a section
*/
static inline void _xtopology_equalize_section(_xmodel_section *section){
int more;
_xlist *address;
do {
more = _XFALSE;
address = section ? section->breakers : (_xlist *)NULL;
while (address){
_xmodel_breaker *breaker = _xcontainer_of(address,_xmodel_breaker,velement);
if (breaker->status.close){
_xmodel_node *fnode = _xpointer_of(node,breaker->fnode - 1);
_xmodel_node *tnode = _xpointer_of(node,breaker->tnode - 1);
if (!fnode || !tnode)
_xerror(_XE_NULL,"_xtopology_equalize_section");
if (tnode->assign != fnode->assign){
tnode->assign = _xmin(tnode->assign,fnode->assign);
fnode->assign = tnode->assign;
more = _XTRUE;
}
}
address = address->next;
}
} while (more);
}
/*
* assign the breaker's from-to nodes topology bus number
*/
static inline void _xtopology_assign_breaker_node(_xmodel_breaker *breaker, int *bus){
_xmodel_node *fnode = _xpointer_of(node,breaker->fnode - 1);
_xmodel_node *tnode = _xpointer_of(node,breaker->tnode - 1);
if (!fnode || !tnode)
_xerror(_XE_NULL,"_xtopology_assign_node");
else if (fnode->assign)
tnode->assign = fnode->assign;
else if (tnode->assign)
fnode->assign = tnode->assign;
else {
fnode->assign = ++(*bus);
tnode->assign = *bus;
}
}
int _xtopology_cold = _XTRUE;
/*!
* initialize section's topology bus information, return bus count
*/
int _xtopology_update_section(void){
int i,j,bus;
_xlist *address;
_xmodel_node *node;
_xmodel_breaker *breaker;
_xmodel_section *section;
_xtopology_update_changed_breaker();
for (i=0; i<_xused_of(section); i++){
section = _xpointer_of(section,i);
if (section->nodes && (section->status.changed || _xtopology_cold)){
bus = 0;
section->status.changed = _XFALSE;
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
node->assign = 0;
}
_xlist_for_each(address,section->breakers){
breaker = _xcontainer_of(address,_xmodel_breaker,velement);
if (breaker->status.close)
_xtopology_assign_breaker_node(breaker,&bus);
}
_xtopology_equalize_section(section);
bus = 0;
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
bus = _xmax(bus,node->assign);
}
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
if (node->assign == 0)
node->assign = ++bus;
}
section->buses = bus;
}
}/* for loop */
bus = 0; /* max buses */
for (i=0; i<_xused_of(section); i++){
section = _xpointer_of(section,i);
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
if (node->assign > 0){
_xlist *atmp;_xmodel_node *ntmp;
bus++;
j = node->assign;
_xlist_for_each(atmp,address){
ntmp = _xcontainer_of(atmp,_xmodel_node,velement);
if (ntmp->assign == j)
ntmp->assign = -bus;
}
}
}
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
node->assign = abs(node->assign);
}
}
for (i=0; i<_xused_of(section); i++){/* non-busbar node */
section = _xpointer_of(section,i);
_xlist_for_each(address,section->nodes){
node = _xcontainer_of(address,_xmodel_node,velement);
if (node->busbar <= 0){
_xlist *atmp;_xmodel_node *ntmp;
_xlist_for_each(atmp,section->nodes){
ntmp = _xcontainer_of(atmp,_xmodel_node,velement);
if (ntmp->assign == node->assign && ntmp->busbar){
node->busbar = -abs(ntmp->busbar);
break;
}
}
}
}
}
return bus;
}
/*!
* create topology bus used for power flow calculation
*/
int _xtopology_create_bus(int capacity){
int i,*flag = NULL;
_xlist *address;
_xmodel_section *section;
_xmodel_node *fnode,*tnode;
if (capacity <= 0 || capacity > _xcapacity_of(tpbus))
return _XFALSE;
for (i=0; i<_xused_of(section); i++){
section = _xpointer_of(section,i);
address = section->shunts;
while (address){/* process shunt */
_xmodel_shunt *shunt = _xcontainer_of(address,_xmodel_shunt,velement);
shunt->status.outage = _XFALSE;
if (shunt->breaker){
_xmodel_breaker *breaker = _xpointer_of(breaker,shunt->breaker - 1);
fnode = _xpointer_of(node,breaker->fnode - 1);
tnode = _xpointer_of(node,breaker->tnode - 1);
if (fnode->assign != tnode->assign){
shunt->status.outage = _XTRUE;
if (breaker->fnode == shunt->node)
fnode->assign = tnode->assign;
else
tnode->assign = fnode->assign;
}
}
address = address->next;
}/* shunt while loop */
}
for (i=0; i<_xused_of(capacitor); i++){/* process capacitor */
_xmodel_capacitor *capacitor = _xpointer_of(capacitor,i);
capacitor->status.outage = _XTRUE;
if (1){/* for local variables */
_xmodel_capsegment *capsegment = NULL;
_xmodel_node *snode = _xpointer_of(node,capacitor->snode - 1);
_xmodel_node *lnode = _xpointer_of(node,capacitor->lnode - 1);
if (lnode->assign != snode->assign){
int lconn,sconn;
lconn = sconn = _XFALSE;
address = capacitor->capsegments;
while (address){
capsegment = _xcontainer_of(address,_xmodel_capsegment,celement);
fnode = _xpointer_of(node,capsegment->fnode - 1);
tnode = _xpointer_of(node,capsegment->tnode - 1);
if (fnode->assign == lnode->assign ||
tnode->assign == lnode->assign)
lconn = _XTRUE;
if (fnode->assign == snode->assign ||
tnode->assign == snode->assign)
sconn = _XTRUE;
if (lconn && sconn){
capacitor->status.outage = _XFALSE;
break;
}
address = address->next;
}/*while loop*/
}
if (!capsegment && !capacitor->status.outage)
lnode->assign = snode->assign;
address = capacitor->capsegments;
while (address){
capsegment = _xcontainer_of(address,_xmodel_capsegment,celement);
fnode = _xpointer_of(node,capsegment->fnode - 1);
tnode = _xpointer_of(node,capsegment->tnode - 1);
capsegment->status.outage = _XTRUE;
if (!capacitor->status.outage){
if (fnode->assign != tnode->assign)
capsegment->status.outage = _XFALSE;
fnode->assign = snode->assign;
tnode->assign = snode->assign;
}
address = address->next;
}/*while loop*/
}
}
/* create bus */
_xused_of(tpbus) = 0;
_xmem_zero(_xpointer_of(tpbus,0),_xcapacity_of(tpbus)*sizeof(_xtopology_bus));
flag = _xnew_a(capacity + 1, int);
for (i=0; i<_xused_of(section); i++){/*node*/
section = _xpointer_of(section,i);
if (section->nodes){
address = section->nodes;
while (address){
fnode = _xcontainer_of(address,_xmodel_node,velement);
if (!flag[fnode->assign]){/* populate topology bus*/
_xtopology_bus *pbus = _xpointer_of(tpbus,_xused_of(tpbus));
flag[fnode->assign] = ++(_xused_of(tpbus));/*index + 1*/
pbus->base_kV = section->nominal;
pbus->bus_i = fnode->assign;
if (_xtopology_cold || fnode->busbar <= 0){
pbus->Va = (_xreal)0.0;
pbus->Vm = (_xreal)1.05;
}
else {
_xmodel_voltage *pvolt = _xpointer_of(voltage,abs(fnode->busbar)-1);
pbus->Va = pvolt->angle;
pbus->Vm = pvolt->voltage;
}
}
fnode->tphook = flag[fnode->assign];
address = address->next;
}/* while loop*/
}
}/* for loop*/
_xfree(flag);flag = NULL;
return _XTRUE;
}
int _xtopology_create_branch(void){
int i;
_xmodel_branch *branch;
_xmodel_node *fnode,*tnode;
_xtopology_bus *fbus,*tbus;
_xtopology_branch *tpbranch;
for (i=0; i<_xused_of(branch); i++){
branch = _xpointer_of(branch,i);
fnode = _xpointer_of(node,branch->fnode-1);
tnode = _xpointer_of(node,branch->tnode-1);
if (!fnode || !tnode)
return _XFALSE;
fbus = _xpointer_of(tpbus,fnode->tphook-1);
tbus = _xpointer_of(tpbus,tnode->tphook-1);
if (!fbus || !tbus)
return _XFALSE;
tpbranch = _xpointer_of(tpbranch,_xused_of(tpbranch));
tpbranch->model = i+1;
tpbranch->f_bus = fbus->bus_i;
tpbranch->t_bus = tbus->bus_i;
tpbranch->br_r = branch->r;
tpbranch->br_x = branch->x;
tpbranch->br_b = branch->bc_a + branch->bc_b;
tpbranch->br_status = branch->status.in_service;
_xused_of(tpbranch)++;
}
return _XTRUE;
}
int _xtopology_create_island(void){
return _XFALSE;
}