📄 readinit.cpp
字号:
/* This routine reads initial values for AC voltage phasors.
The input format is in "i V Ang" format, i.e., bus number
and Voltage magnitude and angle (terminate list with a 0). */
#include <limits.h>
#include "readdata.h"
/* --------------------- ReadInit ---------------------------------- */
#ifdef ANSIPROTO
BOOLEAN ReadInit(void)
#else
BOOLEAN ReadInit()
#endif
/* Main routine. */
{
ACbusData *ACptr;
DCbusData *DCptr;
AreaData *Aptr;
INDEX N;
VALUETYPE V,d,SPg=0,DPg,Sum,Pn,Qn,Pz,Qz,val,PgMax,Vmax,Vmin;
VALUETYPE Ra,Xd,Xq,IaMax,EqMax,EqMin,Smax;
VALUETYPE Pg,Qg,Eq,dg,Vr,Vi,Ir,Ii,Vq,Vd,Iq,Id,Ia,apr,api;
char *Name,BusName[BUFLEN],Line[BUFLEN],*ptr,Vars[BUFLEN],variable[3];
FILE *InputFile;
int i,count,num;
num=6;
/* -------------------- Generation and Load Factors --------------- */
Name=NameParameter('K');
Sum=0;
if (!NullName(Name) && (InputFile=OpenInput(Name))!=NULL) {
for (;;) {
DPg=Pn=Qn=Pz=Qz=PgMax=Vmax=Vmin=0;
if (fgets(Line,BUFLEN,InputFile)==NULL) break;
if (Line[0]!='C') {
if (sscanf(Line,"%d %s",&N,BusName)!=2) count=0;
else if (BusName[0]=='\"'||BusName[0]=='\'') {
count=2;
for(ptr=Line;*ptr!='\"'&&*ptr!='\'';ptr++); ptr++;
for(i=0;*ptr!='\"'&&*ptr!='\''&&*ptr!='\n';BusName[i]= *ptr,i++,ptr++);
BusName[i]='\0';
ptr++;
count+=sscanf(ptr,"%lf %lf %lf %lf %lf %lf %lf %lf",&DPg,&Pn,&Qn,&PgMax,&Smax,&Vmax,&Vmin,&Pz,&Qz);
} else {
count=sscanf(Line,"%d %s %lf %lf %lf %lf %lf %lf %lf %lf",&N,BusName,&DPg,&Pn,&Qn,&PgMax,&Smax,&Vmax,&Vmin,&Pz,&Qz);
}
if (!strcmp(BusName,"0")) BusName[0]='\n';
if (count>=num){
for (ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next) {
if(N==ACptr->Num||!strncmp(ACptr->Name,BusName,strlen(BusName))) break;
}
if(ACptr!=NULL){
if (PgMax>ACptr->Pg && PgMax>0) ACptr->PgMax=PgMax;
if (Smax>=ACptr->PgMax && Smax>0) ACptr->Smax=Smax;
if (Vmax>0 && Vmin>0 && Vmax>Vmin) { ACptr->Vlmax=Vmax; ACptr->Vlmin=Vmin; }
ACptr->DPG=ACptr->DPg=DPg;
if (Narea>1 && Acont) ACptr->Area->Slack->Area->SPg+=DPg*DPg;
else SPg+=DPg*DPg;
ACptr->Pnl=Pn; ACptr->Qnl=Qn;
ACptr->Pzl=Pz; ACptr->Qzl=Qz;
Sum+=Pn+Qn+Pz+Qz;
}
} else ACptr=NULL;
if (ACptr==NULL) {
fCustomPrint(stderr,"***Warning: Line-> %s",Line);
fCustomPrint(stderr," will be ignored in file %s.\n",Name);
}
}
}
if (Narea>1 && Acont)
for(Aptr=dataPtr->Area;Aptr!=NULL;Aptr=Aptr->Next) {
if(!Aptr->SPg) Aptr->Slack->DPg=Aptr->SPg=1;
}
else if (!ExistParameter('6') || NullName(NameParameter('6'))) {
if (!SPg) {
for(ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next) if(strpbrk(ACptr->Type,"S")) break;
ACptr->DPg=SPg=1;
}
for(ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next){
if(Narea>1 && Acont) ACptr->DPg=ACptr->DPg/sqrt(ACptr->Area->Slack->Area->SPg);
else ACptr->DPg=ACptr->DPg/sqrt(SPg);
}
}
fclose(InputFile);
}
if (!flagKdirection) {
if (ExistParameter('v') && !Sum) {
fCustomPrint(stderr,"ERROR: The -v option will yield a singular Jacobian in voltage collapse\n");
fCustomPrint(stderr," studies since Pnl, Qnl, Pzl, and Qzl are zero in all load buses.\n");
stopExecute(ERROREXIT);
} else if (ExistParameter('L') && !Sum) {
fCustomPrint(stderr,"***Warning: The loading factor lambda will not yield different results\n");
fCustomPrint(stderr," from the base case since Pnl, Qnl, Pzl, and Qzl are zero\n");
fCustomPrint(stderr," in all load buses.\n");
} else if ((ExistParameter('H') || ExistParameter('c')) && !Sum) {
fCustomPrint(stderr,"ERROR: The Homotopy Continuation Method will not yield different results\n");
fCustomPrint(stderr," from the base case since Pnl, Qnl, Pzl, and Qzl are zero in all\n");
fCustomPrint(stderr," load buses.\n");
stopExecute(ERROREXIT);
} else if (ExistParameter('C') && !Sum) {
fCustomPrint(stderr,"ERROR: The Point of Collapse Method will not yield different results\n");
fCustomPrint(stderr," from the base case since Pnl, Qnl, Pzl, and Qzl are zero in\n");
fCustomPrint(stderr," all load buses.\n");
stopExecute(ERROREXIT);
}
}
/* -------------------- Initialize AC/DC Variables -------------------------- */
if (ExistParameter('V')){
Name=NameParameter('V');
if (!NullName(Name) && (InputFile=OpenInput(Name))!=NULL) {
for (;;) {
if (fgets(Line,BUFLEN,InputFile)==NULL) break;
if (Line[0]!='C') {
if ((count=sscanf(Line,"%d %s",&N,BusName))!=2) count=0;
else if (BusName[0]=='\"'||BusName[0]=='\'') {
count=2;
for(ptr=Line;*ptr!='\"'&&*ptr!='\'';ptr++); ptr++;
for(i=0;*ptr!='\"'&&*ptr!='\''&&*ptr!='\n';BusName[i]= *ptr,i++,ptr++);
BusName[i]='\0';
ptr++;
} else {
for (i=1,ptr=Line;i<=2;i++) {
if (ptr!=NULL && *ptr!='\0') for(;*ptr==' '&&*ptr!='\n';ptr++);
if (ptr!=NULL && *ptr!='\0') for(;*ptr!=' '&&*ptr!='\n';ptr++);
}
}
if (count){
for (ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next)
if(N==ACptr->Num||!strncmp(ACptr->Name,BusName,strlen(BusName))) break;
if(ACptr!=NULL){
if (ptr!=NULL) count=sscanf(ptr,"%lf %lf",&V,&d);
else count=0;
if (count==2) {
if (V>0 && ACptr->Cont!=NULL) ACptr->V=V;
ACptr->Ang=d*K3;
}
}
for (DCptr=dataPtr->DCbus;DCptr!=NULL;DCptr=DCptr->Next)
if(!strncmp(DCptr->Name,BusName,strlen(BusName))) break;
if(DCptr!=NULL){
if (ptr!=NULL) count=sscanf(ptr,"%s",Vars);
else count=0;
val=0;
if(ExistParameter('d')) CustomPrint("Read initial values for DC bus %s -> %s\n",DCptr->Name,Vars);
if (count) for(i=1;i<=strlen(Vars) && count;i=i+2) {
GetStr(Vars,i,2,2,variable);
if (ptr!=NULL && *ptr!='\0') for(;*ptr==' '&&*ptr!='\n';ptr++);
if (ptr!=NULL && *ptr!='\0') for(;*ptr!=' '&&*ptr!='\n';ptr++);
if (ptr!=NULL) {
count=sscanf(ptr,"%lf",&val);
if(ExistParameter('d')) CustomPrint(" %lf\n",val);
} else count=0;
if(!strcmp(variable,"PA") && strcmp(DCptr->Cont1,"PA") && strcmp(DCptr->Cont2,"PA")) DCptr->P=val;
if(!strcmp(variable,"AL") && strcmp(DCptr->Cont1,"AL") && strcmp(DCptr->Cont2,"AL")) DCptr->Alfa=val;
if(!strcmp(variable,"GA") && strcmp(DCptr->Cont1,"GA") && strcmp(DCptr->Cont2,"GA")) DCptr->Gamma=val;
if(!strcmp(variable,"VD") && strcmp(DCptr->Cont1,"VD") && strcmp(DCptr->Cont2,"VD")) DCptr->Vd=val;
if(!strcmp(variable,"ID") && strcmp(DCptr->Cont1,"ID") && strcmp(DCptr->Cont2,"ID")) DCptr->Id=val;
if(!strcmp(variable,"QA") && strcmp(DCptr->Cont1,"QA") && strcmp(DCptr->Cont2,"QA")) DCptr->Q=val;
if(!strcmp(variable,"AT") && strcmp(DCptr->Cont1,"AT") && strcmp(DCptr->Cont2,"AT")) DCptr->Tap=val;
}
}
} else { ACptr=NULL; DCptr=NULL;}
if (ACptr==NULL && DCptr==NULL) {
fCustomPrint(stderr,"***Warning: Line-> %s",Line);
fCustomPrint(stderr," will be ignored in file %s.\n",Name);
}
}
}
fclose(InputFile);
}
else if (NullName(Name)) {
for (ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next) {
if (ACptr->Cont!=NULL) ACptr->V=1;
ACptr->Ang=0;
}
}
}
/* -------------------- Read Generator Steady-State Data -------------------- */
Name=NameParameter('3');
Ngen=0;
if (!NullName(Name) && (InputFile=OpenInput(Name))!=NULL) {
for (;;) {
Ra=Xd=Xq=IaMax=EqMax=EqMin=0;
if (fgets(Line,BUFLEN,InputFile)==NULL) break;
if (Line[0]!='C') {
if (sscanf(Line,"%d %s",&N,BusName)!=2) count=0;
else if (BusName[0]=='\"'||BusName[0]=='\'') {
count=2;
for(ptr=Line;*ptr!='\"'&&*ptr!='\'';ptr++); ptr++;
for(i=0;*ptr!='\"'&&*ptr!='\''&&*ptr!='\n';BusName[i]= *ptr,i++,ptr++);
BusName[i]='\0';
ptr++;
count+=sscanf(ptr,"%lf %lf %lf %lf %lf %lf",&Ra,&Xd,&Xq,&IaMax,&EqMax,&EqMin);
} else {
count=sscanf(Line,"%d %s %lf %lf %lf %lf %lf %lf",&N,BusName,&Ra,&Xd,&Xq,&IaMax,&EqMax,&EqMin);
}
if (!strcmp(BusName,"0")) BusName[0]='\n';
if (count==8){
for (ACptr=dataPtr->ACbus;ACptr!=NULL;ACptr=ACptr->Next) {
if(N==ACptr->Num||!strncmp(ACptr->Name,BusName,strlen(BusName))) break;
}
if(ACptr!=NULL){
if (strpbrk(ACptr->Type,"G,Q,E,S,V")==NULL || Xd<=0) {
fCustomPrint(stderr,"***Warning: Line-> %s",Line);
fCustomPrint(stderr," will be ignored in file %s.\n",Name);
if (Xd<=0) fCustomPrint(stderr," The value of Xd is less than or equal to zero.\n");
else fCustomPrint(stderr," The bus is not a generator type (BQ,BG,BE,BV,BS).\n");
}
else {
#ifdef WINDOWS
ACptr->Gen=new GenModel;
#else
ACptr->Gen=(GenModel *) malloc (sizeof(GenModel));
if (ACptr->Gen==NULL) {
fclose(InputFile);
ErrorHalt("Insufficient memory to allocate steady-state Generator data.");
stopExecute(ERROREXIT);
}
#endif
ACptr->Gen->Ra=fabs(Ra);
if (Xd<Ra) {
fCustomPrint(stderr,"***Warning: The generator steady-state data for bus:%s\n",ACptr->Name);
fCustomPrint(stderr," has Ra > Xd.\n");
}
ACptr->Gen->Xd=Xd;
if (Xq==0) Xq=Xd;
if (Xq>Xd) {
fCustomPrint(stderr,"***Warning: The generator steady-state data for bus:%s\n",ACptr->Name);
fCustomPrint(stderr," has Xq > Xd. The program will force Xq=Xd.\n");
Xq=Xd;
}
ACptr->Gen->Xq=fabs(Xq);
/*
if (IaMax!=0 && strpbrk(ACptr->Type,"S")) {
fCustomPrint(stderr,"***Warning: The IaMax limit in slack generator:%s\n",ACptr->Name);
fCustomPrint(stderr," will be assumed large.\n");
IaMax=9999999999.;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -