00001
00010 #include <stdlib.h>
00011 #include <stdio.h>
00012 #include <errno.h>
00013 #include <math.h>
00014
00015 #include "capacitor.h"
00016
00018
00020 CLASS* capacitor::oclass = NULL;
00021 CLASS* capacitor::pclass = NULL;
00022
00029 capacitor::capacitor(MODULE *mod):node(mod)
00030 {
00031 if(oclass == NULL)
00032 {
00033 pclass = node::oclass;
00034
00035 oclass = gl_register_class(mod,"capacitor",sizeof(capacitor),PC_PRETOPDOWN|PC_BOTTOMUP|PC_POSTTOPDOWN|PC_UNSAFE_OVERRIDE_OMIT);
00036 if(oclass == NULL)
00037 GL_THROW("unable to register object class implemented by %s",__FILE__);
00038
00039 if(gl_publish_variable(oclass,
00040 PT_INHERIT, "node",
00041 PT_set, "pt_phase", PADDR(pt_phase),
00042 PT_KEYWORD, "A",(set)PHASE_A,
00043 PT_KEYWORD, "B",(set)PHASE_B,
00044 PT_KEYWORD, "C",(set)PHASE_C,
00045 PT_KEYWORD, "D",(set)PHASE_D,
00046 PT_KEYWORD, "N",(set)PHASE_N,
00047 PT_set, "phases_connected", PADDR(phases_connected),
00048 PT_KEYWORD, "A",(set)PHASE_A,
00049 PT_KEYWORD, "B",(set)PHASE_B,
00050 PT_KEYWORD, "C",(set)PHASE_C,
00051 PT_KEYWORD, "D",(set)PHASE_D,
00052 PT_KEYWORD, "N",(set)PHASE_N,
00053 PT_enumeration, "switchA", PADDR(switchA_state),
00054 PT_KEYWORD, "OPEN", OPEN,
00055 PT_KEYWORD, "CLOSED", CLOSED,
00056 PT_enumeration, "switchB", PADDR(switchB_state),
00057 PT_KEYWORD, "OPEN", OPEN,
00058 PT_KEYWORD, "CLOSED", CLOSED,
00059 PT_enumeration, "switchC", PADDR(switchC_state),
00060 PT_KEYWORD, "OPEN", OPEN,
00061 PT_KEYWORD, "CLOSED", CLOSED,
00062 PT_enumeration, "control", PADDR(control),
00063 PT_KEYWORD, "MANUAL", MANUAL,
00064 PT_KEYWORD, "VAR", VAR,
00065 PT_KEYWORD, "VOLT", VOLT,
00066 PT_KEYWORD, "VARVOLT", VARVOLT,
00067 PT_KEYWORD, "CURRENT", CURRENT,
00068 PT_double, "voltage_set_high[V]", PADDR(voltage_set_high),
00069 PT_double, "voltage_set_low[V]", PADDR(voltage_set_low),
00070 PT_double, "VAr_set_high[VAr]", PADDR(VAr_set_high),
00071 PT_double, "VAr_set_low[VAr]", PADDR(VAr_set_low),
00072 PT_double, "current_set_low[A]", PADDR(current_set_low),
00073 PT_double, "current_set_high[A]", PADDR(current_set_high),
00074 PT_double, "capacitor_A[VAr]", PADDR(capacitor_A),
00075 PT_double, "capacitor_B[VAr]", PADDR(capacitor_B),
00076 PT_double, "capacitor_C[VAr]", PADDR(capacitor_C),
00077 PT_double, "cap_nominal_voltage[V]", PADDR(cap_nominal_voltage),
00078 PT_double, "time_delay[s]", PADDR(time_delay),
00079 PT_double, "dwell_time[s]", PADDR(dwell_time),
00080 PT_double, "lockout_time[s]", PADDR(lockout_time),
00081 PT_object, "remote_sense",PADDR(RemoteSensor),
00082 PT_object, "remote_sense_B", PADDR(SecondaryRemote),
00083 PT_enumeration, "control_level", PADDR(control_level),
00084 PT_KEYWORD, "BANK", BANK,
00085 PT_KEYWORD, "INDIVIDUAL", INDIVIDUAL,
00086 NULL) < 1) GL_THROW("unable to publish properties in %s",__FILE__);
00087 }
00088 }
00089
00090 int capacitor::create()
00091 {
00092 int result = node::create();
00093
00094
00095 switchA_state = OPEN;
00096 switchB_state = OPEN;
00097 switchC_state = OPEN;
00098 switchA_state_Next = OPEN;
00099 switchB_state_Next = OPEN;
00100 switchC_state_Next = OPEN;
00101 switchA_state_Prev = OPEN;
00102 switchB_state_Prev = OPEN;
00103 switchC_state_Prev = OPEN;
00104 switchA_state_Req_Next = OPEN;
00105 switchB_state_Req_Next = OPEN;
00106 switchC_state_Req_Next = OPEN;
00107 control = MANUAL;
00108 control_level = INDIVIDUAL;
00109 voltage_set_high = 0.0;
00110 voltage_set_low = 0.0;
00111 VAr_set_high = 0.0;
00112 VAr_set_low = 0.0;
00113 current_set_low = 0.0;
00114 current_set_high = 0.0;
00115 time_delay = 0.0;
00116 dwell_time = 0.0;
00117 lockout_time = 0.0;
00118 time_to_change = 0;
00119 dwell_time_left = 0;
00120 lockout_time_left_A = 0;
00121 lockout_time_left_B = 0;
00122 lockout_time_left_C = 0;
00123 last_time = 0;
00124 cap_nominal_voltage = 0.0;
00125 RemoteSensor = NULL;
00126 SecondaryRemote=NULL;
00127 RNode = NULL;
00128 RLink = NULL;
00129 VArVals[0] = VArVals[1] = VArVals[2] = 0.0;
00130 CurrentVals[0] = CurrentVals[1] = CurrentVals[2] = 0.0;
00131
00132 NotFirstIteration=false;
00133 Iteration_Toggle = false;
00134
00135 Return_Time = 0;
00136
00137 return result;
00138 }
00139
00140 int capacitor::init(OBJECT *parent)
00141 {
00142 int result = node::init();
00143
00144 OBJECT *obj = OBJECTHDR(this);
00145
00146 if ((control == VARVOLT) && (SecondaryRemote!=NULL))
00147 {
00148 if (RemoteSensor==NULL)
00149 {
00150 GL_THROW("Please set the remote sensing location on capacitor:%d under \"remote_sense\"",obj->id);
00151
00152
00153
00154
00155 }
00156 else
00157 {
00158 if ((gl_object_isa(RemoteSensor,"node","powerflow")) && (gl_object_isa(SecondaryRemote,"link","powerflow")))
00159 {
00160 RNode = OBJECTDATA(RemoteSensor,node);
00161 RLink = OBJECTDATA(SecondaryRemote,link);
00162 }
00163 else if ((gl_object_isa(RemoteSensor,"link","powerflow")) && (gl_object_isa(SecondaryRemote,"node","powerflow")))
00164 {
00165 RNode = OBJECTDATA(SecondaryRemote,node);
00166 RLink = OBJECTDATA(RemoteSensor,link);
00167 }
00168 else
00169 {
00170 GL_THROW("For two remote sensors, Capacitor:%d requires one link and one node object specified.",obj->id);
00171
00172
00173
00174
00175 }
00176 }
00177 }
00178 else if (((control==VARVOLT) || (control==CURRENT)) && (SecondaryRemote==NULL) && (RemoteSensor != NULL) && gl_object_isa(RemoteSensor,"link","powerflow"))
00179 {
00180 RLink = OBJECTDATA(RemoteSensor,link);
00181 }
00182 else if (SecondaryRemote != NULL)
00183 {
00184 gl_warning("Capacitor:%d has a secondary sensor specified, but is not in VARVOLT control. This will be ignored.",obj->id);
00185
00186
00187
00188
00189 }
00190
00191 if ((RemoteSensor != NULL) && (control != VARVOLT))
00192 {
00193 if (gl_object_isa(RemoteSensor,"node","powerflow"))
00194 {
00195 RNode = OBJECTDATA(RemoteSensor,node);
00196 }
00197 else if (gl_object_isa(RemoteSensor,"link","powerflow"))
00198 {
00199 RLink = OBJECTDATA(RemoteSensor,link);
00200 }
00201 }
00202
00203 if ((capacitor_A == 0.0) && (capacitor_B == 0.0) && (capacitor_C == 0.0))
00204 gl_error("Capacitor:%d does not have any capacitance values defined!",obj->id);
00205
00206
00207
00208
00209
00210
00211 if (cap_nominal_voltage==0.0)
00212 {
00213 cap_nominal_voltage=nominal_voltage;
00214 }
00215
00216 if ((cap_nominal_voltage==0.0) && (nominal_voltage==0.0))
00217 GL_THROW("Capcitor:%d does not have a node nominal or capacitor nominal voltage specified.",obj->id);
00218
00219
00220
00221
00222
00223
00224 cap_value[0] = complex(0,capacitor_A/(cap_nominal_voltage * cap_nominal_voltage));
00225 cap_value[1] = complex(0,capacitor_B/(cap_nominal_voltage * cap_nominal_voltage));
00226 cap_value[2] = complex(0,capacitor_C/(cap_nominal_voltage * cap_nominal_voltage));
00227
00228 if ((control == VOLT) && ((voltage_set_high == 0) || (voltage_set_low == 0)))
00229 gl_warning("Capacitor:%d does not have one or both of its voltage set points set.",obj->id);
00230
00231
00232
00233
00234
00235 if ((control == VARVOLT) && (voltage_set_high == 0))
00236 gl_warning("Capacitor:%d does not have its upper voltage limit set.",obj->id);
00237
00238
00239
00240
00241
00242 if (((control == VAR) || (control == VARVOLT) ) && ((VAr_set_high == 0) || (VAr_set_low == 0)))
00243 gl_warning("Capacitor:%d does not have one or both of its VAr set points set.",obj->id);
00244
00245
00246
00247
00248
00249 if ((control==CURRENT) && ((current_set_high == 0) || (current_set_low == 0)))
00250 gl_warning("Capacitor:%d does not have one or both of its current set points set.",obj->id);
00251
00252
00253
00254
00255
00256 if (((control == VAR) || (control == VARVOLT) || (control==CURRENT)) && (RLink == NULL))
00257 GL_THROW("VAR, VARVOLT, or CURRENT control on capacitor:%d requires a remote link to monitor.",obj->id);
00258
00259
00260
00261
00262
00263 if (((control==VAR) || (control==VARVOLT)) && (VAr_set_low > VAr_set_high))
00264 GL_THROW("The lower VAr limit of capacitor:%d is larger than the high limit setpoint!",obj->id);
00265
00266
00267
00268
00269 if (((control==VOLT) || (control==VARVOLT)) && (voltage_set_low > voltage_set_high))
00270 GL_THROW("The lower voltage limit of capacitor:%d is larger than the high limit setpoint!",obj->id);
00271
00272
00273
00274
00275 if ((control==CURRENT) && (current_set_low > current_set_high))
00276 GL_THROW("The lower current limit of capacitor:%d is larger than the high limit setpoint!",obj->id);
00277
00278
00279
00280
00281 if ((control != MANUAL) && (time_delay == 0) && (dwell_time==0))
00282 gl_warning("Automatic controls can oscillate to the iteration limit with no time delays. To prevent this, ensure your switching limits are reasonable.");
00283
00284
00285
00286
00287
00288 if ((control == VARVOLT) && (lockout_time==0))
00289 gl_warning("No lockout time specified for capacitor:%d's VAR-VOLT control scheme. May switch excessively.",obj->id);
00290
00291
00292
00293
00294 if ((control != MANUAL) && ((pt_phase & (PHASE_A | PHASE_B | PHASE_C)) == 0))
00295 GL_THROW("Capacitor:%d is set to an automatic scheme, but is not monitoring any phases.",obj->id);
00296
00297
00298
00299
00300
00301
00302 if ((control == MANUAL) && (control_level == BANK) && ((pt_phase & (PHASE_A | PHASE_B | PHASE_C)) == 0))
00303 gl_warning("Capacitor:%d is set to manual bank mode, but does not know which phase to monitor.",obj->id);
00304
00305
00306
00307
00308
00309 if ((phases_connected & (PHASE_A | PHASE_B | PHASE_C)) == 0)
00310 {
00311 gl_warning("No capacitor phase connection information is available for capacitor:%d. Defaulting to the phases property.",obj->id);
00312
00313
00314
00315
00316
00317 phases_connected = phases;
00318 }
00319
00320
00321 switchA_state_Req_Next = switchA_state_Prev = switchA_state_Next = switchA_state;
00322 switchB_state_Req_Next = switchB_state_Prev = switchB_state_Next = switchB_state;
00323 switchC_state_Req_Next = switchC_state_Prev = switchC_state_Next = switchC_state;
00324
00325
00326 if (((control!=MANUAL) && (control!=VOLT)) && ((RLink->phases & pt_phase) != pt_phase))
00327 {
00328 GL_THROW("One of the monitored remote link phases for capacitor:%d does not exist",obj->id);
00329
00330
00331
00332
00333
00334 }
00335 else if (((control==VOLT) || (control==VARVOLT)) && (RNode != NULL) && ((RNode->phases & pt_phase) != pt_phase))
00336 {
00337 GL_THROW("One of the monitored remote node phases for capacitor:%d does not exist",obj->id);
00338
00339
00340
00341
00342
00343 }
00344 else if (((control==VOLT) || (control==VARVOLT)) && (RNode == NULL) && ((phases & pt_phase) != pt_phase))
00345 {
00346 GL_THROW("One of the monitored node phases for capacitor:%d does not exist",obj->id);
00347
00348
00349
00350
00351
00352 }
00353
00354 return result;
00355 }
00356 TIMESTAMP capacitor::sync(TIMESTAMP t0)
00357 {
00358 complex VoltVals[3];
00359 complex temp_shunt[3];
00360 bool Phase_Mismatch = false;
00361 bool Good_Cycle = false;
00362 TIMESTAMP result;
00363
00364 if (solver_method==SM_NR)
00365 {
00366 Good_Cycle=!NR_cycle;
00367 }
00368 else
00369 {
00370 Good_Cycle=true;
00371 }
00372
00373
00374 if (Good_Cycle)
00375 {
00376
00377 time_to_change -= (t0 - last_time);
00378 dwell_time_left -= (t0 - last_time);
00379 lockout_time_left_A -= (t0 - last_time);
00380 lockout_time_left_B -= (t0 - last_time);
00381 lockout_time_left_C -= (t0 - last_time);
00382
00383 if (last_time!=t0)
00384 {
00385 last_time = t0;
00386 }
00387
00388 if (control==MANUAL)
00389 {
00390 if (switchA_state != switchA_state_Prev)
00391 {
00392 if ((control_level == BANK) && ((pt_phase & PHASE_A) != PHASE_A) && (time_delay==0))
00393 {
00394 switchA_state = switchA_state_Next;
00395 }
00396 else
00397 {
00398 switchA_state_Next = switchA_state;
00399 switchA_state = switchA_state_Prev;
00400 }
00401 time_to_change=(int64)time_delay;
00402 }
00403
00404 if (switchB_state != switchB_state_Prev)
00405 {
00406 if ((control_level == BANK) && ((pt_phase & PHASE_B) != PHASE_B) && (time_delay==0))
00407 {
00408 switchB_state = switchB_state_Next;
00409 }
00410 else
00411 {
00412 switchB_state_Next = switchB_state;
00413 switchB_state = switchB_state_Prev;
00414 }
00415 time_to_change=(int64)time_delay;
00416 }
00417
00418 if (switchC_state != switchC_state_Prev)
00419 {
00420 if ((control_level == BANK) && ((pt_phase & PHASE_C) != PHASE_C) && (time_delay==0))
00421 {
00422 switchC_state = switchC_state_Next;
00423 }
00424 else
00425 {
00426 switchC_state_Next = switchC_state;
00427 switchC_state = switchC_state_Prev;
00428 }
00429 time_to_change=(int64)time_delay;
00430 }
00431 }
00432
00433 if ((time_to_change<=0) && NotFirstIteration)
00434 {
00435
00436 if ((phases_connected & (PHASE_A)) == PHASE_A)
00437 switchA_state=switchA_state_Next;
00438
00439 if ((phases_connected & (PHASE_B)) == PHASE_B)
00440 switchB_state=switchB_state_Next;
00441
00442 if ((phases_connected & (PHASE_C)) == PHASE_C)
00443 switchC_state=switchC_state_Next;
00444
00445
00446 if ((control==VOLT) || (control==VARVOLT))
00447 {
00448 if ((pt_phase & PHASE_D) != (PHASE_D))
00449 {
00450 if (RNode == NULL)
00451 {
00452 VoltVals[0] = voltage[0];
00453 VoltVals[1] = voltage[1];
00454 VoltVals[2] = voltage[2];
00455 }
00456 else
00457 {
00458 LOCK_OBJECT(OBJECTHDR(RNode));
00459 VoltVals[0] = RNode->voltage[0];
00460 VoltVals[1] = RNode->voltage[1];
00461 VoltVals[2] = RNode->voltage[2];
00462 UNLOCK_OBJECT(OBJECTHDR(RNode));
00463
00464 }
00465 }
00466 else
00467 {
00468 if (RNode == NULL)
00469 {
00470 VoltVals[0] = voltaged[0];
00471 VoltVals[1] = voltaged[1];
00472 VoltVals[2] = voltaged[2];
00473 }
00474 else
00475 {
00476 LOCK_OBJECT(OBJECTHDR(RNode));
00477 VoltVals[0] = RNode->voltaged[0];
00478 VoltVals[1] = RNode->voltaged[1];
00479 VoltVals[2] = RNode->voltaged[2];
00480 UNLOCK_OBJECT(OBJECTHDR(RNode));
00481 }
00482 }
00483 }
00484 else;
00485
00486
00487 switch (control) {
00488 case MANUAL:
00489 {
00490 switchA_state_Prev = switchA_state_Next;
00491 switchB_state_Prev = switchB_state_Next;
00492 switchC_state_Prev = switchC_state_Next;
00493 }
00494 break;
00495 case VARVOLT:
00496 case VAR:
00497 {
00498
00499 if ((pt_phase & PHASE_A) == PHASE_A)
00500 {
00501 if (VAr_set_low >= VArVals[0])
00502 switchA_state_Req_Next=OPEN;
00503 else if (VAr_set_high <= VArVals[0])
00504 switchA_state_Req_Next=CLOSED;
00505 else;
00506 }
00507
00508 if ((pt_phase & PHASE_B) == PHASE_B)
00509 {
00510 if (VAr_set_low >= VArVals[1])
00511 switchB_state_Req_Next=OPEN;
00512 else if (VAr_set_high <= VArVals[1])
00513 switchB_state_Req_Next=CLOSED;
00514 else;
00515 }
00516
00517 if ((pt_phase & PHASE_C) == PHASE_C)
00518 {
00519 if (VAr_set_low >= VArVals[2])
00520 switchC_state_Req_Next=OPEN;
00521 else if (VAr_set_high <= VArVals[2])
00522 switchC_state_Req_Next=CLOSED;
00523 else;
00524 }
00525
00526
00527 if (control == VARVOLT)
00528 {
00529 bool lockout_state_change[3];
00530 lockout_state_change[0] = lockout_state_change[1] = lockout_state_change[2] = false;
00531
00532 if ((pt_phase & PHASE_D) != (PHASE_D))
00533 {
00534 if (lockout_time_left_A <= 0)
00535 {
00536 if (((pt_phase & PHASE_A) == PHASE_A) && (VoltVals[0].Mag() >= voltage_set_high))
00537 {
00538 switchA_state_Req_Next=OPEN;
00539 lockout_state_change[0] = true;
00540 }
00541 }
00542 else
00543 {
00544 switchA_state_Req_Next=OPEN;
00545 }
00546
00547 if (lockout_time_left_B <= 0)
00548 {
00549 if (((pt_phase & PHASE_B) == PHASE_B) && (VoltVals[1].Mag() >= voltage_set_high))
00550 {
00551 switchB_state_Req_Next=OPEN;
00552 lockout_state_change[1] = true;
00553 }
00554 }
00555 else
00556 {
00557 switchB_state_Req_Next=OPEN;
00558 }
00559
00560 if (lockout_time_left_C <= 0)
00561 {
00562 if (((pt_phase & PHASE_C) == PHASE_C) && (VoltVals[2].Mag() >= voltage_set_high))
00563 {
00564 switchC_state_Req_Next=OPEN;
00565 lockout_state_change[2] = true;
00566 }
00567 }
00568 else
00569 {
00570 switchC_state_Req_Next=OPEN;
00571 }
00572 }
00573 else
00574 {
00575 if (lockout_time_left_A <= 0)
00576 {
00577 if (((pt_phase & (PHASE_A | PHASE_B)) == (PHASE_A | PHASE_B)) && (VoltVals[0].Mag() >= voltage_set_high))
00578 {
00579 switchA_state_Req_Next=OPEN;
00580 lockout_state_change[0] = true;
00581 }
00582 }
00583 else
00584 {
00585 switchA_state_Req_Next=OPEN;
00586 }
00587
00588 if (lockout_time_left_B <= 0)
00589 {
00590 if (((pt_phase & (PHASE_B | PHASE_C)) == (PHASE_B | PHASE_C)) && (VoltVals[1].Mag() >= voltage_set_high))
00591 {
00592 switchB_state_Req_Next=OPEN;
00593 lockout_state_change[1] = true;
00594 }
00595 }
00596 else
00597 {
00598 switchB_state_Req_Next=OPEN;
00599 }
00600
00601 if (lockout_time_left_C <= 0)
00602 {
00603 if (((pt_phase & (PHASE_C | PHASE_A)) == (PHASE_C | PHASE_A)) && (VoltVals[2].Mag() >= voltage_set_high))
00604 {
00605 switchC_state_Req_Next=OPEN;
00606 lockout_state_change[2] = true;
00607 }
00608 }
00609 else
00610 {
00611 switchC_state_Req_Next=OPEN;
00612 }
00613 }
00614
00615 if ((control_level == BANK) && (lockout_state_change[0] | lockout_state_change[1] | lockout_state_change[2]))
00616 {
00617 lockout_time_left_A = lockout_time_left_B = lockout_time_left_C = (int64)lockout_time;
00618 }
00619 else if ((control_level == INDIVIDUAL) && (lockout_state_change[0] | lockout_state_change[1] | lockout_state_change[2]))
00620 {
00621 if (lockout_state_change[0]==true)
00622 lockout_time_left_A = (int64)lockout_time;
00623
00624 if (lockout_state_change[1]==true)
00625 lockout_time_left_B = (int64)lockout_time;
00626
00627 if (lockout_state_change[2]==true)
00628 lockout_time_left_C = (int64)lockout_time;
00629 }
00630 else;
00631 }
00632 }
00633 break;
00634 case VOLT:
00635 {
00636 if ((pt_phase & PHASE_D) != (PHASE_D))
00637 {
00638 if ((pt_phase & PHASE_A) == PHASE_A)
00639 {
00640 if (voltage_set_low >= VoltVals[0].Mag())
00641 switchA_state_Req_Next=CLOSED;
00642 else if (voltage_set_high <= VoltVals[0].Mag())
00643 switchA_state_Req_Next=OPEN;
00644 else;
00645 }
00646
00647 if ((pt_phase & PHASE_B) == PHASE_B)
00648 {
00649 if (voltage_set_low >= VoltVals[1].Mag())
00650 switchB_state_Req_Next=CLOSED;
00651 else if (voltage_set_high <= VoltVals[1].Mag())
00652 switchB_state_Req_Next=OPEN;
00653 else;
00654 }
00655
00656 if ((pt_phase & PHASE_C) == PHASE_C)
00657 {
00658 if (voltage_set_low >= VoltVals[2].Mag())
00659 switchC_state_Req_Next=CLOSED;
00660 else if (voltage_set_high <= VoltVals[2].Mag())
00661 switchC_state_Req_Next=OPEN;
00662 else;
00663 }
00664 }
00665 else
00666 {
00667 if ((pt_phase & (PHASE_A | PHASE_B)) == (PHASE_A | PHASE_B))
00668 {
00669 if (voltage_set_low >= VoltVals[0].Mag())
00670 switchA_state_Req_Next=CLOSED;
00671 else if (voltage_set_high <= VoltVals[0].Mag())
00672 switchA_state_Req_Next=OPEN;
00673 else;
00674 }
00675
00676 if ((pt_phase & (PHASE_B | PHASE_C)) == (PHASE_B | PHASE_C))
00677 {
00678 if (voltage_set_low >= VoltVals[1].Mag())
00679 switchB_state_Req_Next=CLOSED;
00680 else if (voltage_set_high <= VoltVals[1].Mag())
00681 switchB_state_Req_Next=OPEN;
00682 else;
00683 }
00684
00685 if ((pt_phase & (PHASE_C | PHASE_A)) == (PHASE_C | PHASE_A))
00686 {
00687 if (voltage_set_low >= VoltVals[2].Mag())
00688 switchC_state_Req_Next=CLOSED;
00689 else if (voltage_set_high <= VoltVals[2].Mag())
00690 switchC_state_Req_Next=OPEN;
00691 else;
00692 }
00693 }
00694 }
00695 break;
00696 case CURRENT:
00697 {
00698 if ((pt_phase & PHASE_A) == PHASE_A)
00699 {
00700 if (current_set_low >= CurrentVals[0])
00701 switchA_state_Req_Next=OPEN;
00702 else if (current_set_high <= CurrentVals[0])
00703 switchA_state_Req_Next=CLOSED;
00704 else;
00705 }
00706
00707 if ((pt_phase & PHASE_B) == PHASE_B)
00708 {
00709 if (current_set_low >= CurrentVals[1])
00710 switchB_state_Req_Next=OPEN;
00711 else if (current_set_high <= CurrentVals[1])
00712 switchB_state_Req_Next=CLOSED;
00713 else;
00714 }
00715
00716 if ((pt_phase & PHASE_C) == PHASE_C)
00717 {
00718 if (current_set_low >= CurrentVals[2])
00719 switchC_state_Req_Next=OPEN;
00720 else if (current_set_high <= CurrentVals[2])
00721 switchC_state_Req_Next=CLOSED;
00722 else;
00723 }
00724 }
00725 break;
00726 default:
00727 break;
00728 }
00729
00730
00731 if (((switchA_state_Prev != switchA_state_Req_Next) || (switchB_state_Prev != switchB_state_Req_Next) || (switchC_state_Prev != switchC_state_Req_Next)) && (control != MANUAL))
00732 {
00733 dwell_time_left = (int64)dwell_time;
00734 switchA_state_Prev = switchA_state_Req_Next;
00735 switchB_state_Prev = switchB_state_Req_Next;
00736 switchC_state_Prev = switchC_state_Req_Next;
00737 }
00738
00739 if ((dwell_time_left <= 0) && (control!=MANUAL))
00740 {
00741 switchA_state_Next = switchA_state_Req_Next;
00742 switchB_state_Next = switchB_state_Req_Next;
00743 switchC_state_Next = switchC_state_Req_Next;
00744 }
00745
00746 if (control_level == BANK)
00747 {
00748 bool bank_A, bank_B, bank_C;
00749
00750 if ((pt_phase & PHASE_D) != (PHASE_D))
00751 {
00752 bank_A = (((pt_phase & PHASE_A) == PHASE_A) && (switchA_state_Next == CLOSED));
00753 bank_B = (((pt_phase & PHASE_B) == PHASE_B) && (switchB_state_Next == CLOSED));
00754 bank_C = (((pt_phase & PHASE_C) == PHASE_C) && (switchC_state_Next == CLOSED));
00755 }
00756 else
00757 {
00758 bank_A = (((pt_phase & (PHASE_A | PHASE_B)) == (PHASE_A | PHASE_B)) && (switchA_state_Next == CLOSED));
00759 bank_B = (((pt_phase & (PHASE_B | PHASE_C)) == (PHASE_B | PHASE_C)) && (switchB_state_Next == CLOSED));
00760 bank_C = (((pt_phase & (PHASE_C | PHASE_A)) == (PHASE_C | PHASE_A)) && (switchC_state_Next == CLOSED));
00761 }
00762
00763 if (control==MANUAL)
00764 {
00765 if ((bank_A | bank_B | bank_C) == true)
00766 switchA_state_Next = switchB_state_Next = switchC_state_Next = CLOSED;
00767 else
00768 switchA_state_Next = switchB_state_Next = switchC_state_Next = OPEN;
00769
00770 switchA_state_Prev = switchB_state_Prev = switchC_state_Prev = switchA_state_Next;
00771 }
00772 else
00773 {
00774 if ((bank_A | bank_B | bank_C) == true)
00775 switchA_state_Next = switchB_state_Next = switchC_state_Next = CLOSED;
00776 else
00777 switchA_state_Next = switchB_state_Next = switchC_state_Next = OPEN;
00778
00779 switchA_state_Req_Next = switchB_state_Req_Next = switchC_state_Req_Next = switchA_state_Next;
00780 }
00781 }
00782
00783
00784
00785 if ((switchA_state != switchA_state_Next) | (switchB_state != switchB_state_Next) | (switchC_state != switchC_state_Next))
00786 time_to_change=(int64)time_delay;
00787 else
00788 time_to_change=-1;
00789
00790 }
00791
00792
00793
00794 if ((((phases_connected & PHASE_D) != PHASE_D) && ((phases & PHASE_D) != PHASE_D)) || ((phases_connected & PHASE_D) == PHASE_D) && ((phases & PHASE_D) == PHASE_D))
00795 {
00796 temp_shunt[0] = cap_value[0];
00797 temp_shunt[1] = cap_value[1];
00798 temp_shunt[2] = cap_value[2];
00799 }
00800 else if (((phases_connected & PHASE_D) != PHASE_D) && ((phases & PHASE_D) == PHASE_D))
00801 {
00802 GL_THROW("Capacitor:%d is Wye-connected on a Delta-connected node. This is not supported at this time.",OBJECTHDR(this)->id);
00803
00804
00805
00806
00807 }
00808 else if (((phases_connected & PHASE_D) == PHASE_D) && ((phases & PHASE_D) != PHASE_D))
00809 {
00810 complex cap_temp[3];
00811 complex numer;
00812
00813 cap_temp[0] = cap_temp[1] = cap_temp[2] = 0.0;
00814
00815
00816 if (((phases_connected & PHASE_A) == PHASE_A) && (switchA_state==CLOSED))
00817 cap_temp[0] = cap_value[0];
00818
00819 if (((phases_connected & PHASE_B) == PHASE_B) && (switchB_state==CLOSED))
00820 cap_temp[1] = cap_value[1];
00821
00822 if (((phases_connected & PHASE_C) == PHASE_C) && (switchC_state==CLOSED))
00823 cap_temp[2] = cap_value[2];
00824
00825
00826 temp_shunt[0] = (voltaged[0]*cap_temp[0] - voltaged[2]*cap_temp[2]) / voltage[0];
00827 temp_shunt[1] = (voltaged[1]*cap_temp[1] - voltaged[0]*cap_temp[0]) / voltage[1];
00828 temp_shunt[2] = (voltaged[2]*cap_temp[2] - voltaged[1]*cap_temp[1]) / voltage[2];
00829
00830 Phase_Mismatch = true;
00831 }
00832 else
00833 GL_THROW("Unable to determine connection for capacitor:%d",OBJECTHDR(this)->id);
00834
00835
00836
00837
00838
00839 if (Phase_Mismatch==true)
00840 {
00841 shunt[0] = temp_shunt[0];
00842 shunt[1] = temp_shunt[1];
00843 shunt[2] = temp_shunt[2];
00844 }
00845 else
00846 {
00847
00848 if ((phases_connected & (PHASE_A)) == PHASE_A)
00849 shunt[0] = switchA_state==CLOSED ? temp_shunt[0] : complex(0.0);
00850
00851 if ((phases_connected & (PHASE_B)) == PHASE_B)
00852 shunt[1] = switchB_state==CLOSED ? temp_shunt[1] : complex(0.0);
00853
00854 if ((phases_connected & (PHASE_C)) == PHASE_C)
00855 shunt[2] = switchC_state==CLOSED ? temp_shunt[2] : complex(0.0);
00856 }
00857
00858
00859 result = node::sync(t0);
00860
00861 if (dwell_time_left>0)
00862 result = t0 + dwell_time_left;
00863 else if (time_to_change>0)
00864 result = t0 + time_to_change;
00865 else;
00866
00867 if ((NotFirstIteration==false) && (solver_method==SM_NR))
00868 Return_Time = t0;
00869 else if ((NotFirstIteration==false) && (solver_method==SM_FBS))
00870 {
00871 Return_Time = t0;
00872 time_to_change = -1;
00873 }
00874 else
00875 Return_Time = result;
00876
00877 NotFirstIteration=true;
00878 }
00879 else
00880 {
00881 result = node::sync(t0);
00882 result = Return_Time;
00883 }
00884
00885 if (result != TS_NEVER)
00886 {
00887 if (result==t0)
00888 return result;
00889 else
00890 return -result;
00891 }
00892 else
00893 return TS_NEVER;
00894 }
00895
00896 TIMESTAMP capacitor::postsync(TIMESTAMP t0)
00897 {
00898 TIMESTAMP result;
00899
00900 if ((solver_method==SM_NR) && (NR_cycle==true))
00901 {
00902 Iteration_Toggle = !Iteration_Toggle;
00903 }
00904
00905 if ((control==VAR) || (control==VARVOLT))
00906 {
00907 if ((solver_method==SM_FBS) || (solver_method==SM_NR))
00908 {
00909 LOCK_OBJECT(OBJECTHDR(RLink));
00910
00911
00912 RLink->calculate_power();
00913
00914
00915 VArVals[0] = RLink->indiv_power_in[0].Im();
00916 VArVals[1] = RLink->indiv_power_in[1].Im();
00917 VArVals[2] = RLink->indiv_power_in[2].Im();
00918
00919 UNLOCK_OBJECT(OBJECTHDR(RLink));
00920 }
00921 }
00922 else if (control==CURRENT)
00923 {
00924 if ((solver_method==SM_FBS) || (solver_method==SM_NR))
00925 {
00926
00927 LOCK_OBJECT(OBJECTHDR(RLink));
00928
00929
00930 CurrentVals[0]=RLink->current_in[0].Mag();
00931 CurrentVals[1]=RLink->current_in[1].Mag();
00932 CurrentVals[2]=RLink->current_in[2].Mag();
00933
00934 UNLOCK_OBJECT(OBJECTHDR(RLink));
00935 }
00936 }
00937
00938 result=node::postsync(t0);
00939
00940
00941 if (solver_method==SM_NR)
00942 {
00943 if (Iteration_Toggle)
00944 return result;
00945 else
00946 return t0;
00947 }
00948 else
00949 {
00950 return result;
00951 }
00952 }
00953
00954
00955
00956 void capacitor::toggle_bank_status(bool des_status){
00957
00958 if (des_status==true)
00959 {
00960 if ((phases_connected & PHASE_A) == PHASE_A)
00961 {
00962 switchA_state = switchA_state_Next = switchA_state_Prev = CLOSED;
00963 }
00964
00965 if ((phases_connected & PHASE_B) == PHASE_B)
00966 {
00967 switchB_state = switchB_state_Next = switchB_state_Prev = CLOSED;
00968 }
00969
00970 if ((phases_connected & PHASE_C) == PHASE_C)
00971 {
00972 switchC_state = switchC_state_Next = switchC_state_Prev = CLOSED;
00973 }
00974 }
00975 else
00976 {
00977 if ((phases_connected & PHASE_A) == PHASE_A)
00978 {
00979 switchA_state = switchA_state_Next = switchA_state_Prev = OPEN;
00980 }
00981
00982 if ((phases_connected & PHASE_B) == PHASE_B)
00983 {
00984 switchB_state = switchB_state_Next = switchB_state_Prev = OPEN;
00985 }
00986
00987 if ((phases_connected & PHASE_C) == PHASE_C)
00988 {
00989 switchC_state = switchC_state_Next = switchC_state_Prev = OPEN;
00990 }
00991 }
00992
00993
00994 time_to_change = dwell_time_left = 0;
00995 }
00996
00997 int capacitor::isa(char *classname)
00998 {
00999 return strcmp(classname,"capacitor")==0 || node::isa(classname);
01000 }
01001
01003
01005
01013 EXPORT int create_capacitor(OBJECT **obj, OBJECT *parent)
01014 {
01015 try
01016 {
01017 *obj = gl_create_object(capacitor::oclass);
01018 if (*obj!=NULL)
01019 {
01020 capacitor *my = OBJECTDATA(*obj,capacitor);
01021 gl_set_parent(*obj,parent);
01022 return my->create();
01023 }
01024 }
01025 catch (const char *msg)
01026 {
01027 gl_error("create_capacitor: %s", msg);
01028 }
01029 return 0;
01030 }
01031
01032
01033
01040 EXPORT int init_capacitor(OBJECT *obj)
01041 {
01042 capacitor *my = OBJECTDATA(obj,capacitor);
01043 try {
01044 return my->init(obj->parent);
01045 }
01046 catch (const char *msg)
01047 {
01048 GL_THROW("%s (capacitor:%d): %s", my->get_name(), my->get_id(), msg);
01049 return 0;
01050 }
01051 }
01052
01061 EXPORT TIMESTAMP sync_capacitor(OBJECT *obj, TIMESTAMP t0, PASSCONFIG pass)
01062 {
01063 capacitor *pObj = OBJECTDATA(obj,capacitor);
01064 try {
01065 TIMESTAMP t1 = TS_NEVER;
01066 switch (pass) {
01067 case PC_PRETOPDOWN:
01068 return pObj->presync(t0);
01069 case PC_BOTTOMUP:
01070 return pObj->sync(t0);
01071 case PC_POSTTOPDOWN:
01072 t1 = pObj->postsync(t0);
01073 obj->clock = t0;
01074 return t1;
01075 default:
01076 throw "invalid pass request";
01077 }
01078 } catch (const char *error) {
01079 GL_THROW("%s (capacitor:%d): %s", pObj->get_name(), pObj->get_id(), error);
01080 return 0;
01081 } catch (...) {
01082 GL_THROW("%s (capacitor:%d): %s", pObj->get_name(), pObj->get_id(), "unknown exception");
01083 return 0;
01084 }
01085 }
01086
01095 EXPORT int isa_capacitor(OBJECT *obj, char *classname)
01096 {
01097 return OBJECTDATA(obj,capacitor)->isa(classname);
01098 }
01099