pq_decouple.C
上传用户:wxl722722
上传日期:2022-07-11
资源大小:10k
文件大小:21k
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- void Datain(int Nb,int Nl,int Ng,FILE *fp,struct Branch_Type *Branch,struct Load_Type *Load,struct Generator_Type *Generator,struct PVNode_Type *PVNode,int *Npv); /*从初始数据文件中读入数据*/
- void AdmittanceMatrix(int N,int Nb,struct Yii_Type *Yii,struct Yii_Type *Yiil,struct Yij_Type *Yij,struct Yij_Type *Yijl,struct Branch_Type *Branch,int *NYseq1); /*形成导纳矩阵函数*/
- void AdmittanceMatrixAdd(int Nb,struct Yii_Type *Yii,struct Yii_Type *Yiil,struct Yij_Type *Yij,struct Yij_Type *Yijl,struct Branch_Type *Branch); /*形成导纳矩阵追加接地支路函数*/
- void Factorial(int flag,int N,int Npv,struct PVNode_Type *PVNode,int *NUsum,struct Yii_Type *Yii,struct Yij_Type *Yij,int *NYseq,double *D,struct U_Type *U); /*形成因子表函数*/
- void NodePower(int flag,int N,struct NodalVol *NodeVol,struct NodalPow *NodePow,struct Yii_Type *Yii,struct Yij_Type *Yij,int *NYseq); /*节点功率计算函数*/
- void Iteration(int flag,struct Generator_Type *Generator,struct Load_Type *Load,struct PVNode_Type *PVNode,struct NodalVol *NodeVol,struct NodalPow *NodePow,struct GeneratorPower *GenPower,int N,double *DI,double *MaxError,int *ErrNode); /*迭代计算函数*/
- void FormulaSolution(int flag,struct U_Type *U,double *D,int *NUsum,double *DI,int N,struct NodalVol *NodeVol,double V0); /*线性方程组求解函数*/
- void NodeDataOutput(FILE *fp,struct NodalVol *NodeVol,struct Generator_Type *Generator,int N,struct GeneratorPower *GenPower,struct NodalPow *NodePow,struct Load_Type *Load,int Nl); /*节点数据输出函数*/
- void BranchDataOutput(FILE *fp,int Nb,struct Branch_Type *Branch,struct NodalVol *NodeVol); /*支路数据输出函数*/
- struct Branch_Type
- {
- int i,j;
- double R,X,YK;
- };
-
- struct Generator_Type
- {
- int i;
- double P,Q;
- double V;
- };
-
- struct Load_Type
- {
- int i;
- double P,Q;
- double V;
- };
-
- struct PVNode_Type
- {
- int i;
- double V;
- };
-
- struct Yii_Type
- {
- double G,B;
- };
-
- struct Yij_Type
- {
- double G,B;
- int j;
- };
-
- struct NodalPow
- {
- double P,Q;
- };
-
- struct NodalVol
- {
- double V,theta;
- };
-
- struct GeneratorPower
- {
- double P,Q;
- };
-
- struct U_Type
- {
- double value;
- int j;
- };
-
- main()
- {
- int N; /*系统节点总数*/
- int Nb; /*系统中支路数*/
- int Ng; /*发电机节点总数,不包括平衡机*/
- int Nl; /*负荷节点总数*/
- double V0; /*系统标称电压*/
- double epsilon; /*迭代收敛所要求的精确度*/
- struct Branch_Type *Branch; /*支路数据数组*/
- struct Generator_Type *Generator; /*发电机数据数组*/
- struct Load_Type *Load; /*负荷数据数组*/
- struct PVNode_Type *PVNode; /*PV节点数据数组*/
- int Npv=0; /*PV节点个数*/
- struct Yii_Type *Yii,*Yiil; /*导纳对角矩阵数组*/
- struct Yij_Type *Yij,*Yijl; /*导纳非对角矩阵数组*/
- int *NYseq; /*矩阵非对角元素记录数组*/
- struct NodalPow *NodePow; /*节点功率数组*/
- struct NodalVol *NodeVol; /*节点电压数组*/
- struct GeneratorPower *GenPower; /*发电机功率数组*/
- double *DI1,*DI2; /*存放误差项的数组*/
- double MaxError=0.0; /*每次迭代过程中最大误差项*/
- int ErrNode; /*最大误差项所在节点*/
- int Kp=1,Kq=1; /*有功迭代和无功迭代的标记*/
- int n,k,count; /*K为迭代次数*/
- int i;
- struct U_Type *U1,*U2; /*存放因子表非对角元素的数组*/
- double *D1,*D2; /*存放因子表对角元素的数组*/
- int *NUsum1,*NUsum2; /*矩阵非对角元素记录数组*/
- char FILENAME[20];
- FILE *fp;
- /****************打开初始数据原文件*****************/
- printf("Please Enter The Filename of The System:");
- gets(FILENAME);
- if((fp=fopen(FILENAME,"r"))==NULL)
- {
- printf("Cannot Find The File:%sn",FILENAME);
- printf("Press ENTER to Escape!");
- exit(0);
- }
- /********************读系统基本数据信息*********************/
- fscanf(fp,"%d,%d,%d,%d,%lf,%lf",&N,&Nb,&Ng,&Nl,&V0,&epsilon);
- /*************给支路、发电机、负荷、PV节点分配内存**********/
- Branch=malloc((Nb+1)*sizeof(struct Branch_Type));
- Generator=malloc((Ng+1)*sizeof(struct Generator_Type));
- Load=malloc((Nl+1)*sizeof(struct Load_Type));
- PVNode=malloc(N*sizeof(struct PVNode_Type));
- Datain(Nb,Nl,Ng,fp,Branch,Load,Generator,PVNode,&Npv); /*从初始文件中读入数据*/
- for(n=0;1;n++) /*建立计算结果输出文件*/
- {
- if(FILENAME[n]=='.')
- {
- FILENAME[n]=' ';
- strcat(FILENAME,"out.dat");
- break;
- }
- }
- if((fp=fopen(FILENAME,"w"))==NULL) /*打开计算结果输出文件*/
- {
- printf("Cannot Find The File:%sn",FILENAME);
- printf("Press ENTER to Escape!");
- exit(0);
- }
-
- /***********为导纳矩阵分配内存并形成导纳矩阵***************/
- Yii=malloc((N+1)*sizeof(struct Yii_Type));
- Yiil=malloc((N+1)*sizeof(struct Yii_Type));
- Yij=malloc((N+1)*sizeof(struct Yij_Type));
- Yijl=malloc((N+1)*sizeof(struct Yij_Type));
- NYseq=malloc((N+1)*sizeof(int));
-
- AdmittanceMatrix(N,Nb,Yii,Yiil,Yij,Yijl,Branch,NYseq); /*导纳矩阵形成函数*/
- /*****形成因子表,采用BX方案(B'中忽略充电电容和非标准变比,B"中忽略电阻)*****/
- U1=malloc((N-1)*(N-2)/2*sizeof(struct U_Type));
- U2=malloc((N-1)*(N-2)/2*sizeof(struct U_Type));
- D1=malloc(N*sizeof(double));
- D2=malloc(N*sizeof(double));
- NUsum1=malloc(N*sizeof(int));
- NUsum2=malloc(N*sizeof(int));
- Factorial(1,N,Npv,PVNode,NUsum1,Yii,Yij,NYseq,D1,U1); /*形成因子表B'*/
- AdmittanceMatrixAdd(Nb,Yii,Yiil,Yij,Yijl,Branch); /*导纳矩阵追加接地支路函数*/
- Factorial(2,N,Npv,PVNode,NUsum2,Yiil,Yijl,NYseq,D2,U2); /*形成因子表B"*/
- /****************下面利用所求得的因子表进行迭代求解******************/
- DI1=malloc(N*sizeof(double));
- DI2=malloc(N*sizeof(double));
- NodePow=malloc((N+1)*sizeof(struct NodalPow));
- NodeVol=malloc((N+1)*sizeof(struct NodalVol));
- /********************先送电压初值***********************/
- for(i=1;i<=N;i++)
- {
- NodeVol[i].V=V0;
- NodeVol[i].theta=0.0;
- }
- for(n=1;n<=Npv;n++)
- {
- i=PVNode[n].i;
- NodeVol[i].V=PVNode[n].V;
- }
-
- GenPower=malloc((N+1)*sizeof(struct GeneratorPower));
-
- fprintf(fp,"ttt系统潮流计算结果 n(1)迭代过程纪录:n迭代次数t t有功迭代tt无功迭代ntt ΔPmaxtP-Nodet ΔQmaxttQ-Noden");
-
- for(k=0;1;k++)
- {
- fprintf(fp," %2d:t",k);
- if(Kp==1)
- {
- NodePower(1,N,NodeVol,NodePow,Yii,Yij,NYseq); /*节点功率计算函数*/
- Iteration(1,Generator,Load,PVNode,NodeVol,NodePow,GenPower,N,DI1,&MaxError,&ErrNode); /*迭代计算函数*/
- fprintf(fp,"t%10.7lft %dt",MaxError,ErrNode);
- if(MaxError>=epsilon)
- FormulaSolution(1,U1,D1,NUsum1,DI1,N,NodeVol,V0); /*线性方程组求解函数*/
- else
- Kp=0;
- }
- else
- fprintf(fp,"tttt");
- if(Kq==1)
- {
- NodePower(2,N,NodeVol,NodePow,Yii,Yij,NYseq); /*节点功率计算函数*/
- Iteration(2,Generator,Load,PVNode,NodeVol,NodePow,GenPower,N,DI2,&MaxError,&ErrNode); /*迭代计算函数*/
- fprintf(fp,"%10.7lft %dn",MaxError,ErrNode);
- if(MaxError>=epsilon)
- FormulaSolution(2,U2,D2,NUsum2,DI2,N,NodeVol,V0); /*线性方程组求解函数*/
- else
- Kq=0;
- }
- else
- fprintf(fp,"n");
- if(Kp==0&&Kq==0)
- break;
- if(k>1000)
- {
- fprintf(fp,"n迭代次数超过1000次,系统不收敛!n");
- }
- printf("...");
- }
- fprintf(fp,"n总迭代次数为: %d 次!nn",k+1);
- fprintf(fp,"(2)潮流计算结果(节点电压):n NodettVttθttPttQn");
- NodeDataOutput(fp,NodeVol,Generator,N,GenPower,NodePow,Load,Nl);
- fprintf(fp,"n(3)潮流计算结果(支路功率):n BranchttPttQn");
- BranchDataOutput(fp,Nb,Branch,NodeVol);
- fprintf(fp,"nnn");
- fclose(fp);
- free(Branch);
- free(Generator);
- free(Load);
- free(PVNode);
- free(Yii);
- free(Yiil);
- free(Yij);
- free(Yijl);
- free(NYseq);
- free(U1);
- free(U2);
- free(D1);
- free(D2);
- free(NUsum1);
- free(NUsum2);
- free(DI1);
- free(DI2);
- free(NodePow);
- free(NodeVol);
- free(GenPower);
- }
- void Datain(int Nb,int Nl,int Ng,FILE *fp,struct Branch_Type *Branch,struct Load_Type *Load,struct Generator_Type *Generator,struct PVNode_Type *PVNode,int *Npv)
- {
- int n;
- /*********************从初始数据文件中读取支路、发电机、负荷数据************************/
- for(n=1;n<=Nb;n++)
- {
- fscanf(fp,"%d,%d,%lf,%lf,%lf",&(Branch[n].i),&(Branch[n].j),&(Branch[n].R),&(Branch[n].X),&(Branch[n].YK));
- }
- for(n=1;n<=Ng;n++)
- {
- fscanf(fp,"%d,%lf,%lf,%lf",&(Generator[n].i),&(Generator[n].P),&(Generator[n].Q),&(Generator[n].V));
- if((Generator[n].V)<0)
- {
- (*Npv)++;
- PVNode[(*Npv)].i=Generator[n].i;
- PVNode[(*Npv)].V=-(Generator[n].V);
- }
- }
- for(n=1;n<=Nl;n++)
- {
- fscanf(fp,"%d,%lf,%lf,%lf",&(Load[n].i),&(Load[n].P),&(Load[n].Q),&(Load[n].V));
- if((Load[n].V)<0)
- {
- (*Npv)++;
- PVNode[(*Npv)].i=Load[n].i;
- PVNode[(*Npv)].V=-(Load[n].V);
- }
- }
- fclose(fp);
- }
- void AdmittanceMatrix(int N,int Nb,struct Yii_Type *Yii,struct Yii_Type *Yiil,struct Yij_Type *Yij,struct Yij_Type *Yijl,struct Branch_Type *Branch,int *NYseq1)
- {
- /*****************不考虑接地支路时,形成节点导纳矩阵******************/
- int i,n;
- int *NYsum=malloc((N+1)*sizeof(int));
- int *NYseq=malloc((N+1)*sizeof(int));
- for(i=1;i<=N;i++)
- {
- Yii[i].G=0.0;
- Yii[i].B=0.0;
- Yiil[i].G=0.0;
- Yiil[i].B=0.0;
- NYsum[i]=0;
- }
- for(n=1;n<=Nb;n++)
- {
- int i=abs(Branch[n].i);
- int j=abs(Branch[n].j);
- double R=(Branch[n].R);
- double X=(Branch[n].X);
- double YK=(Branch[n].YK);
- double Zmag2=R*R+X*X;
- double Gij=R/Zmag2;
- double Bij=-X/Zmag2;
- double b_ij=-1.0/X;
- if((Branch[n].i<0)||(Branch[n].j<0))
- {
- Yij[n].G=-Gij/YK;
- Yij[n].B=-Bij/YK;
- Yijl[n].G=0.0;
- Yijl[n].B=-b_ij/YK;
- }
- else
- {
- Yij[n].G=-Gij;
- Yij[n].B=-Bij;
- Yijl[n].G=0.0;
- Yijl[n].B=-b_ij;
- }
- Yij[n].j=j;
- Yijl[n].j=j;
- if((Branch[n].i<0)||(Branch[n].j<0))
- {
- Yii[i].G=Yii[i].G+Gij/YK;
- Yii[i].B=Yii[i].B+Bij/YK;
- Yii[j].G=Yii[j].G+Gij/YK;
- Yii[j].B=Yii[j].B+Bij/YK;
- Yiil[i].B=Yiil[i].B+b_ij/YK;
- Yiil[j].B=Yiil[j].B+b_ij/YK;
- }
- else
- {
- Yii[i].G=Yii[i].G+Gij;
- Yii[i].B=Yii[i].B+Bij;
- Yii[j].G=Yii[j].G+Gij;
- Yii[j].B=Yii[j].B+Bij;
- Yiil[i].B=Yiil[i].B+b_ij;
- Yiil[j].B=Yiil[j].B+b_ij;
- }
- NYsum[i]+=1;
- }
- NYseq[1]=1;
- for(i=1;i<=N-1;i++)
- {
- NYseq[i+1]=NYseq[i]+NYsum[i];
- }
- for(n=1;n<=N;n++)
- {
- NYseq1[n]=NYseq[n];
- }
- }
- void AdmittanceMatrixAdd(int Nb,struct Yii_Type *Yii,struct Yii_Type *Yiil,struct Yij_Type *Yij,struct Yij_Type *Yijl,struct Branch_Type *Branch)
- {
- int n;
- /***********************在导纳矩阵中追加接地支路************************/
- for(n=1;n<=Nb;n++)
- {
- int i=Branch[n].i;
- int j=Branch[n].j;
- double YK=Branch[n].YK;
- if(!(i<0||j<0))
- {
- double Bij=YK/2.0;
- double b_ij=YK/2.0;
- Yii[i].B=Yii[i].B+Bij;
- Yii[j].B=Yii[j].B+Bij;
- Yiil[i].B=Yiil[i].B+b_ij;
- Yiil[j].B=Yiil[j].B+b_ij;
- }
- else
- {
- double Gij;
- double Bij;
- double b_ij;
- if(i<0)
- {
- i=abs(i);
- Gij=Yij[n].G;
- Bij=Yij[n].B;
- b_ij=Yijl[n].B;
- Yii[i].G=Yii[i].G+(1.0-1.0/YK)*Gij;
- Yii[i].B=Yii[i].B+(1.0-1.0/YK)*Bij;
- Yiil[i].B=Yiil[i].B+(1.0-1.0/YK)*b_ij;
- Yii[j].G=Yii[j].G+(1.0-YK)*Gij;
- Yii[j].B=Yii[j].B+(1.0-YK)*Bij;
- Yiil[j].B=Yiil[j].B+(1.0-YK)*b_ij;
- }
- else
- {
- j=abs(j);
- Gij=Yij[n].G;
- Bij=Yij[n].B;
- b_ij=Yijl[n].B;
- Yii[j].G=Yii[j].G+(1.0-1.0/YK)*Gij;
- Yii[j].B=Yii[j].B+(1.0-1.0/YK)*Bij;
- Yiil[j].B=Yiil[j].B+(1.0-1.0/YK)*b_ij;
- Yii[i].G=Yii[i].G+(1.0-YK)*Gij;
- Yii[i].B=Yii[i].B+(1.0-YK)*Bij;
- Yiil[i].B=Yiil[i].B+(1.0-YK)*b_ij;
- }
- }
- }
- }
- void Factorial(int flag,int N,int Npv,struct PVNode_Type *PVNode,int *NUsum,struct Yii_Type *Yii,struct Yij_Type *Yij,int *NYseq,double *D,struct U_Type *U)
- {
- /**************************因子表形成函数***************************/
- int n_pv,i_pv,j,n_u,i_above;
- int i,count;
- double *B=malloc((N+1)*sizeof(double));
- double Btemp;
- n_pv=1;
- i_pv=PVNode[1].i;
-
- for(i=1;i<N;i++)
- {
- if((flag==2)&&(i==i_pv))
- {
- n_pv++;
- i_pv=PVNode[n_pv].i;
- NUsum[i]=0;
- D[i]=0.0;
- continue;
- }
- else
- {
- for(count=i+1;count<N;count++)
- B[count]=0.0;
-
- B[i]=Yii[i].B;
-
- for(count=NYseq[i];count<NYseq[i+1];count++)
- {
- j=Yij[count].j;
- B[j]=Yij[count].B;
- }
-
- if(flag==2)
- {
- for(count=1;count<=Npv;count++)
- {
- j=PVNode[count].i;
- B[j]=0.0;
- }
-
- }
-
- n_u=1;
- i_above=1;
-
- while(i_above<=(i-1))
- {
- count=1;
-
- while(count<=NUsum[i_above])
- {
- if(U[n_u].j==i)
- {
-
- Btemp=U[n_u].value/D[i_above];
- while(count<=NUsum[i_above])
- {
- j=U[n_u].j;
- B[j]=B[j]-Btemp*U[n_u].value;
- count++;
- n_u++;
- }
- break;
- }
- count++;
- n_u++;
- }
- i_above++;
- }
-
- Btemp=1.0/B[i];
- D[i]=Btemp;
- count=0;
- for(j=i+1;j<N;j++)
- {
- if(B[j]!=0.0)
- {
- U[n_u].value=B[j]*Btemp;
- U[n_u].j=j;
- count++;
- n_u++;
- }
- }
- NUsum[i]=count;
- }
- }
- free(B);
- }
- void NodePower(int flag,int N,struct NodalVol *NodeVol,struct NodalPow *NodePow,struct Yii_Type *Yii,struct Yij_Type *Yij,int *NYseq)
- {
- /*********************计算各节点功率函数**********************/
- double A,B,Vi;
- int i,n,j;
- double VV,theta;
- for(i=1;i<=N;i++)
- {
- if(flag==1)
- NodePow[i].P=0.0;
- else
- NodePow[i].Q=0.0;
- }
- for(i=1;i<=N;i++)
- {
- Vi=NodeVol[i].V;
- if(flag==1)
- {
- A=Yii[i].G;
- }
- else
- {
- A=-Yii[i].B;
- }
-
- if(flag==1)
- NodePow[i].P+=Vi*Vi*A;
- else
- {NodePow[i].Q+=Vi*Vi*A;}
-
- if(i==N)
- {
- break;
- }
- else
- {
- for(n=NYseq[i];n<=NYseq[i+1]-1;n++)
- {
- if(flag==1)
- {
- A=Yij[n].G;
- B=Yij[n].B;
- }
- else
- {
- A=-Yij[n].B;
- B=Yij[n].G;
- }
- j=Yij[n].j;
- VV=Vi*NodeVol[j].V;
- theta=NodeVol[i].theta-NodeVol[j].theta;
- A=A*VV*cos(theta);
- B=B*VV*sin(theta);
- if(flag==1)
- {
- NodePow[i].P+=(A+B);
- NodePow[j].P+=(A-B);
- }
- else
- {
- NodePow[i].Q+=(A+B);
- NodePow[j].Q+=(A-B);
- }
- }
- }
- }
- }
- void Iteration(int flag,struct Generator_Type *Generator,struct Load_Type *Load,struct PVNode_Type *PVNode,struct NodalVol *NodeVol,struct NodalPow *NodePow,struct GeneratorPower *GenPower,int N,double *DI,double *MaxError,int *ErrNode)
- {
- /*****************迭代计算函数*****************/
- int i=1,n_g=1,n_l=1,n_pv=1,i_g=Generator[1].i,i_l=Load[1].i,i_pv=PVNode[1].i;
- double Vi,Wi,Wtemp;
- (*MaxError)=0.0;
-
- do
- {
- Vi=NodeVol[i].V;
- if(i==i_l)
- {
- if(flag==1)
- {
- Wi=Load[n_l].P;
- }
- else
- {
- Wi=Load[n_l].Q;
- }
- n_l+=1;
- i_l=Load[n_l].i;
- }
- else
- {
- Wi=0.0;
- }
- Wtemp=Wi;
- if(flag==1)
- Wi=Wi-NodePow[i].P;
- else
- Wi=Wi-NodePow[i].Q;
- if(i==i_g)
- {
- if(flag==1)
- {
- NodePow[i].P=Wtemp;
- GenPower[i_g].P=-Wi;
- }
- else
- {
- NodePow[i].Q=Wtemp;
- GenPower[i_g].Q=-Wi;
- }
- if(flag==1)
- {
- Wi+=Generator[n_g].P;
- }
- else
- {
- Wi+=Generator[n_g].Q;
- }
- n_g+=1;
- i_g=Generator[n_g].i;
- }
- if(i==N)
- {
- break;
- }
- else
- {
- if(flag==2&&i==i_pv)
- {
- n_pv+=1;
- i_pv=PVNode[n_pv].i;
- DI[i]=0.0;
- }
- else
- {
- if(fabs(Wi)>(*MaxError))
- {
- (*MaxError)=fabs(Wi);
- (*ErrNode)=i;
- }
-
- DI[i]=Wi/Vi;
- }
- }
- i+=1;
- }while(1);
- }
- void FormulaSolution(int flag,struct U_Type *U,double *D,int *NUsum,double *DI,int N,struct NodalVol *NodeVol,double V0)
- {
- /******************进行线性方程组的求解********************/
- int n_u;
- int i,count;
- int j;
- double DItemp,Dtheta,DV;
- n_u=1;
-
- for(i=1;i<=N-1;i++)
- {
- DItemp=DI[i];
- for(count=1;count<=NUsum[i];count++)
- {
- j=U[n_u].j;
- DI[j]=DI[j]-DItemp*U[n_u].value;
- n_u++;
- }
- DI[i]=DItemp*D[i];
- }
- for(i=N-1;i>=1;i--)
- {
- DItemp=DI[i];
- for(count=1;count<=NUsum[i];count++)
- {
- n_u-=1;
- j=U[n_u].j;
- DItemp=DItemp-DI[j]*U[n_u].value;
- }
- DI[i]=DItemp;
- }
- for(i=1;i<=N-1;i++)
- {
- if(flag==1)
- {
- Dtheta=DI[i]/V0;
- NodeVol[i].theta=NodeVol[i].theta-Dtheta;
- }
- else
- {
- DV=DI[i];
- NodeVol[i].V-=DV;
- }
- }
- }
- void NodeDataOutput(FILE *fp,struct NodalVol *NodeVol,struct Generator_Type *Generator,int N,struct GeneratorPower *GenPower,struct NodalPow *NodePow,struct Load_Type *Load,int Nl)
- {
- /***************节点数据输出**********************/
- double Vmin=NodeVol[1].V;
- double V,theta,P,Q;
- int i_g=Generator[1].i;
- int VminNode=1;
- int n_g=1;
- int i;
-
- for(i=1;i<=N;i++)
- {
- theta=NodeVol[i].theta/3.14159*180;
- V=NodeVol[i].V;
- if(V<Vmin)
- {
- Vmin=V;
- VminNode=i;
- }
- if(i==i_g)
- {
- P=GenPower[i].P;
- Q=GenPower[i].Q;
- n_g+=1;
- i_g=Generator[n_g].i;
- }
- else
- {
- P=0.0;
- Q=0.0;
- }
- if(i!=N)
- fprintf(fp," %dt %10.7lft %10.7lft %10.7lft %10.7lfn",i,V,theta,P,Q);
- else
- fprintf(fp," %dt %10.7lft %10.7lft %10.7lft %10.7lfn",i,V,theta,NodePow[i].P-Load[Nl].P,NodePow[i].Q-Load[Nl].Q);
- }
- fprintf(fp,"系统最低电压=%10.7lf,节点=%dn",Vmin,VminNode);
- }
- void BranchDataOutput(FILE *fp,int Nb,struct Branch_Type *Branch,struct NodalVol *NodeVol)
- {
- /***************支路数据输出***************/
- double PLoss=0.0,QLoss=0.0;
- int n;
- int i,j;
- double R,X,YK,Y,theta,Ei,Ej,Fi,Fj,Vi,Vj,DE,DF;
- double Zmag2,Ir,Ii;
- double Pij,Qij,Pji,Qji;
- for(n=1;n<=Nb;n++)
- {
- i=abs(Branch[n].i);
- j=abs(Branch[n].j);
- R=Branch[n].R;
- X=Branch[n].X;
- YK=Branch[n].YK;
- Vi=NodeVol[i].V;
- theta=NodeVol[i].theta;
- Ei=Vi*cos(theta);
- Fi=Vi*sin(theta);
- Vj=NodeVol[j].V;
- theta=NodeVol[j].theta;
- Ej=Vj*cos(theta);
- Fj=Vj*sin(theta);
-
- if(Branch[n].i<0||Branch[n].j<0)
- {
- if(Branch[n].i<0)
- {
- Ei=Ei/YK;
- Fi=Fi/YK;
- }
- else
- {
- Ej=Ej/YK;
- Fj=Fj/YK;
- }
- YK=0.0;
- }
-
- DE=Ei-Ej;
- DF=Fi-Fj;
- Zmag2=R*R+X*X;
- Ir=(DE*R+DF*X)/Zmag2;
- Ii=(DF*R-DE*X)/Zmag2;
-
- Pij=Ir*Ei+Ii*Fi;
- Qij=Ir*Fi-Ii*Ei;
-
- Pji=-(Ir*Ej+Ii*Fj);
- Qji=-(Ir*Fj-Ii*Ej);
-
- Qij-=(Vi*Vi*YK/2.0);
- Qji-=(Vj*Vj*YK/2.0);
-
- PLoss=PLoss+Pij+Pji;
- QLoss=QLoss+Qij+Qji;
-
- fprintf(fp," %3d->%3dt %10.7lft %10.7lfn %3d->%3dt %10.7lft %10.7lfn",i,j,Pij,Qij,j,i,Pji,Qji);
- }
- fprintf(fp," 损耗t %10.7lft %10.7lfn",PLoss,QLoss);
- }