Generator power calculation

clear% Clear memory variables
format long
jc=0;
%--------------------------Node raw data module-------- ------------------------%
JieDianShuJu=[1 1.0600 0.0000 0.0000 0.0000 0.0000 0.0000;
              2 1.0450 0.0000 0.4000 0.4000 0.2170 0.1270;
              3 1.0100 0.0000 0.0000 0.2000 0.9420 0.1900;
              4 1.0000 0.0000 0.0000 0.0000 0.4780 -0.0390;
              5 1.0000 0.0000 0.0000 0.0000 0.0760 0.0160;
              6 1.0700 0.0000 0.0000 0.3000 0.1120 0.0750;
              7 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000;
              8 1.0900 0.0000 0.0000 0.3000 0.0000 0.0000;
              9 1.0000 0.0000 0.0000 0.0000 0.2950 0.1660 ;
              10 1.0000 0.0000 0.0000 0.0000 0.0900 0.0580;
              11 1.0000 0.0000 0.0000 0.0000 0.0350 0.0180;
              12 1.0000 0.0000 0.0000 0.0000 0.0610 0.0160;
              13 1.0000 0.0000 0.0000 0.0000 0.1350 0.0580;
              14 1.0000 0.0000 0.0000 0.0000 0.1490 0.0500);% input node data
JieDianZongShu=size(JieDianShuJu ,1);%Total number of nodes
PVJieDianShu=4;%
Number of PV nodes PQJieDianShu=JieDianZongShu-PVJieDianShu-1;%PQ number of nodes
PVI=[0 1 1 0 0 1 0 1 0 0 0 0 0 0];% indicates a node The matrix of whether it is a PV node
PQI=[0 0 0 1 1 0 1 0 1 1 1 1 1 1];% A matrix indicating whether a node is a PQ node
PVR=[1 2 3 6 8];% slack node and PV Node number
PV=[2 3 6 8];%PV node number
PQ=[4 5 7 9 10 11 12 13 14];%PQ node number
SC=[1 2 5 7]; Required in %N,L,M Deleted rows and columns
U=JieDianShuJu(:,2);% voltage effective value
Theta=JieDianShuJu(:,3);% voltage phase
PG=JieDianShuJu(:,4);% generator active power
QG=JieDianShuJu(:,5);% generator reactive power
PL=JieDianShuJu(:,6);% load Active
QL=JieDianShuJu(:,7);% load reactive power
DetP=zeros(JieDianZongShu,1);% node active power error vector
DetQ=zeros(JieDianZongShu,1);% node reactive power error vector
sump=0;% Temporary variable
sumq=0;%temporary variable
DetTheta=0;%temporary variable
%========================== node original data module=== =============================%
%
% ------------------ --------Define the Jacobian matrix and the correction vector---------------------------%
XiuZhengXiangLiang=zeros(JieDianZongShu+PQJieDianShu -1,1);% define the correction vector
ThetaXiuZheng=XiuZhengXiangLiang(1:JieDianZongShu-1);% define the argument correction vector
UXiuZheng=XiuZhengXiangLiang(JieDianZongShu:end);% define the voltage correction vector
J=zeros(JieDianZongShu*2-2 );% defines the Jacobian matrix
H=zeros(JieDianZongShu-1);%initialize H matrix
N=zeros(JieDianZongShu-1);%initialize N matrix
L=zeros(JieDianZongShu-1);%initialize L matrix
M=zeros(JieDianZongShu-1);%initialize M matrix
H1=H; N1=N; M1=M; L1=L;
%========================== define the Jacobian matrix sum correction vectors ===========================%
%
% ------------------ -------- Branch raw data module--------------------------------%
ZhiLuShuJu=[- 1 4 7 0.00000 0.20912 0.00000 0.978;
            -2 4 9 0.00000 0.55678 0.00000 0.969;
            -3 5 6 0.00000 0.25202 0.00000 0.932;
             0 0 9 0.00000 0.00000 0.01900 0.000
             1 1 2 0.01938 0.05917 0.02640 0.000;
             2 2 3 0.04699 0.19797 0.02190 0.000;
             3 2 4 0.05811 0.17632 0.01870 0.000;
             4 1 5 0.05403 0.22304 0.02460 0.000;
             5 2 5 0.05695 0.17388 0.01700 0.000;
             6 3 4 0.06701 0.17103 0.01730 0.000;
             7 4 5 0.01335 0.04211 0.00640 0.000;
             8 7 8 0.00000 0.17615 0.00000 0.000;
             9 7 9 0.00000 0.11001 0.00000 0.000;
            10 9 10 0.03181 0.08450 0.00000 0.000;
            11 6 11 0.09498 0.19890 0.00000 0.000;
            12 6 12 0.12291 0.15581 0.00000 0.000;
            13 6 13 0.06615 0.13027 0.00000 0.000;
            14 9 14 0.12711 0.27038 0.00000 0.000;
            15 10 11 0.08205 0.19207 0.00000 0.000;
            16 12 13 0.22092 0.19988 0.00000 0.000;
            17 13 14 0.17093 0.34802 0.00000 0.000];% input branch original data
ZhiLuShu=size(ZhiLuShuJu,1);% total branch number
%================================================================================================================================================================================================================================================================= ===== branch raw data module ================================%
%
% ----- ---------------------Iso-admittance parameter module----------------------- ---------%
DengZhiDaoNaCanShu=zeros(ZhiLuShu,3);
for k=1:ZhiLuShu
    if ZhiLuShuJu(k,1)<0% The branch label is negative, that is, the transformer branch
        DengZhiDaoNaCanShu(k,1 )=1/(ZhiLuShuJu(k,7)*j*ZhiLuShuJu(k,5));
        DengZhiDaoNaCanShu(k,2)=DengZhiDaoNaCanShu(k,1)*(ZhiLuShuJu(k,7)-1);
        DengZhiDaoNaCanShu(k ,3)=-DengZhiDaoNaCanShu(k,2)/ZhiLuShuJu(k,7);
    elseif ZhiLuShuJu(k,1)>0% The branch label is positive, that is, the transmission line branch
        DengZhiDaoNaCanShu(k,1)=1/( ZhiLuShuJu(k,4)+j*ZhiLuShuJu(k,5));
        DengZhiDaoNaCanShu(k,2)=j*ZhiLuShuJu(k,6);
        DengZhiDaoNaCanShu(k,3)=j*ZhiLuShuJu(k,6);
    else% Parallel branch
        DengZhiDaoNaCanShu(k,3)=j*ZhiLuShuJu(k, 6);
    end
end
%===================================================================================================================================================================================================================================================================================================== ====================%
%
% -------------------------- node Self-admittance calculation module ------------------------------%
JieDianZiDaoNa=zeros(JieDianZongShu,1);% define node self-guide The nano matrix
for k=1: JieDianZongShu
    for l=1: ZhiLuShu
        if ZhiLuShuJu(l,2)==k%k is the first node of the branch
            JieDianZiDaoNa(k)=JieDianZiDaoNa(k)+DengZhiDaoNaCanShu(l,1)+DengZhiDaoNaCanShu(l,1) l,2);
        elseif ZhiLuShuJu(l,3)==k%k is the end node of the branch
            JieDianZiDaoNa(k)=JieDianZiDaoNa(k)+DengZhiDaoNaCanShu(l,1)+DengZhiDaoNaCanShu(l,3);
        end
    end
end
%===================== ==== from node admittance calculation module ===============================%
%
% ------ --------------------Node Mutual Admittance Calculation Module------------------------ ------%
JieDianHuDaoNa=zeros(JieDianZongShu);% initialize the node mutual admittance matrix
for k=1: ZhiLuShu
    if ZhiLuShuJu(k,1)==0;
        continue;
    end
    JieDianHuDaoNa(ZhiLuShuJu(k,2), ZhiLuShuJu(k,3))=-DengZhiDaoNaCanShu(k,1);
    JieDianHuDaoNa(ZhiLuShuJu(k,3),ZhiLuShuJu(k,2))=-DengZhiDaoNaCanShu(k,1);
end
%======= ====================Node mutual admittance calculation module================================================================================================================================================================================================================================================================================= ======%
%
%--------------------------Synthetic node admittance matrix------------------ --------------%
Y=JieDianHuDaoNa;
for k=1:JieDianZongShu
    Y(k,k)=JieDianZiDaoNa(k);
end
G=real(Y);% Generate node conductance matrix
B=imag(Y);%Generate nodal admittance matrix
%========================synthetic nodal admittance matrix======================================================================================================================================================================================================================================================= ============================%
%
% ------------------- -------Generate node adjacency matrix---------------------------------%
LinJieZhen=zeros(JieDianZongShu );
for k=1:ZhiLuShu
    if ZhiLuShuJu(k,1)==0;
        continue;
    end
    LinJieZhen(ZhiLuShuJu(k,2),ZhiLuShuJu(k,3))=1;
    LinJieZhen(ZhiLuShuJu(k,3), ZhiLuShuJu(k,2))=1;
end
for k=1:JieDianZongShu
    LinJieZhen(k,k)=1;
end
%========================= Generate node adjacency matrix =================== ==============%
%
% --------------------------------- ---Newton iteration loop---------------------------------------%
for DieDaiCiShu=1 : 10
    %------------------------------Power error calculation module------------- ------------%
    for k=2:JieDianZongShu
        sump=0;
        sumq=0;
        for l=1:JieDianZongShu
            DetTheta=Theta(k)-Theta(l);
            sump=sump+LinJieZhen (k,l)*U(l)*(G(k,l)*cos(DetTheta)+B(k,l)*sin(DetTheta));
            sumq=sumq+LinJieZhen(k,l)*U( l)*(G(k,l)*sin(DetTheta)-B(k,l)*cos(DetTheta));
        end
        sump=sump*U(k);
        sumq=sumq*U(k);
        DetP( k)=PG(k)-PL(k)-sump;
        DetQ(k)=QG(k)-QL(k)-sumq;
    end
    YouGongWuCha=DetP;
    WuGongWuCha=DetQ;
    YouGongWuCha(1)=[];
    WuGongWuCha(PVR,:)=[];
    %===== ========================= Power error calculation module =============================================================================================================================================================================================================================================================================== ====%
    %
    % ------------------------------ --------- iteration of the loop termination condition ————%
    if max(abs(YouGongWuCha))<=1e-20&max(abs(WuGongWuCha))<=1e-20
        jc=1;
        break;
    end
    %============= ================ iteration loop termination condition ==========================%
    %
    % - ---------------------------- Jacobian calculation module ----------------- --------%
    for k=2:JieDianZongShu
        for l=2:JieDianZongShu
            DetTheta=Theta(k)-Theta(l);
            H(k-1,l-1)=-U(k)*U(l)*(G(k,l)*sin(DetTheta)-B(k ,l)*cos(DetTheta));
            N(k-1,l-1)=-U(k)*U(l)*(G(k,l)*cos(DetTheta)+B(k,l )*sin(DetTheta));
        end
    end
    L=H;
    M=-N;
    for k=2:JieDianZongShu
        H(k-1,k-1)=U(k)*U(k)*B(k, k)+(QG(k)-QL(k));
        N(k-1,k-1)=-U(k)*U(k)*G(k,k)-(PG(k)- PL(k));
        M(k-1,k-1)=U(k)*U(k)*G(k,k)-(PG(k)-PL(k));
        L(k- 1,k-1)=U(k)*U(k)*B(k,k)-(QG(k)-QL(k));
    end
    H1=H;N1=N;M1=M;L1 =L;
    N1(:,SC)=[];% delete the column corresponding to the PV node
    M1(SC,:)=[];% delete the row corresponding to the PV node
    L1(:,SC)=[];% Delete the column corresponding to the PV node
    L1(SC,:)=[];% delete the row corresponding to the PV node
    J=[H1,N1;M1,L1];% Synthetic Jacobian matrix
    %============================ Jacobian matrix calculation module ==========================%
    %
    % ------------------- ----------Modified data solution and data update module -----------------%
    XiuZhengXiangLiang=-inv(J)*[YouGongWuCha;WuGongWuCha];
    ThetaXiuZheng =XiuZhengXiangLiang(1:JieDianZongShu-1);
    UXiuZheng=XiuZhengXiangLiang(JieDianZongShu:end);
    Theta(2:end)=Theta(2:end)+ThetaXiuZheng;
    U(PQ,:)=U(PQ,:)+U (PQ,:).*UXiuZheng;
    %============================ Modified data solution and data update module==== =============%
end
%================================= Newton Iterative loop ===================================%
JiaoDu=Theta/pi*180;
wc1=max (abs(YouGongWuCha));
wc2=max(abs(WuGongWuCha));
wc=max(wc1,wc2)
%
%--------------------------Generator power calculation module--------- ----------------------%
sump=0;
sumq=0;
for k=1:JieDianZongShu
    DetTheta=Theta(1)-Theta(k);
    sump =sump+LinJieZhen(1,k)*U(k)*(G(1,k)*cos(DetTheta)+B(1,k)*sin(DetTheta));
    sumq=sumq+LinJieZhen(1,k )*U(k)*(G(1,k)*sin(DetTheta)-B(1,k)*cos(DetTheta));
end
sump=sump*U(1);
sumq=sumq*U(1 );
PG(1)=PL(1)+sump;% balance node generator active power
sump
QG(1)=QL(1)+sumq;% balance node generator reactive power
for k=1:PVJieDianShu%PV Node generator reactive power
        sumq=0;
        for l=1: JieDianZongShu
            DetTheta=Theta(PV(k))-Theta(l);
            sumq = sumq + LinJieZhen (PV (k), l) * U (l) * (G (PV (k), l) * sin (DetTheta) -B (PV (k), l) * cos (DetTheta)) ;
        end
        sumq = sumq * U (PV (k));
        QG (PV (k)) = QL (PV (k)) + sumq;
end
[JieDianShuJu (:, 1) U JiaoDu PG QG]


    
    
    
    
    
    

Guess you like

Origin blog.csdn.net/ccsss22/article/details/113823098