@ -42,20 +42,32 @@ function [V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrf,Vavrref,tg
global IS_OCTAVE ;
global IS_OCTAVE ;
% import system data khuang, 8 Jul
[ bus , sw , pv , pq , shunt , line , ind , zip , syn , exc , tg , agc , cac , cluster ] = unfoldSysData ( SysData ) ;
[ bus , sw , pv , pq , shunt , line , ind , zip , syn , exc , tg , agc , cac , cluster ] = unfoldSysData ( SysData ) ;
% nbus:Total number of buses khuang, 8 Jul
nbus = size ( bus , 1 ) ;
nbus = size ( bus , 1 ) ;
nline = size ( line , 1 ) ;
nline = size ( line , 1 ) ;
%determine islanding, this is for AGC part khuang 8 JUl
if isfield ( SysPara , ' nIslands' ) && isfield ( SysPara , ' islands' ) && isfield ( SysPara , ' refs' )
if isfield ( SysPara , ' nIslands' ) && isfield ( SysPara , ' islands' ) && isfield ( SysPara , ' refs' )
nIslands = SysPara . nIslands ; islands = SysPara . islands ; refs = SysPara . refs ;
nIslands = SysPara . nIslands ; islands = SysPara . islands ; refs = SysPara . refs ;
else
else
[ nIslands , islands , refs ] = searchIslands ( bus ( : , 1 ) , line ( : , 1 : 2 ) ) ;
[ nIslands , islands , refs ] = searchIslands ( bus ( : , 1 ) , line ( : , 1 : 2 ) ) ;
end
end
% improt initial condition of state variables khuang 8 JUl
[ V0 , Q0 , s0 , d0 , w0 , eq10 , eq20 , ed10 , ed20 , psid0 , psiq0 , Pm0 , Ef0 , Vavrm0 , Vavrr0 , Vavrf0 , Vavrref0 , tgovg0 , tgovm0 , tgovmech0 , f0 , dpg0 , qplt0 , vg0 ] = unfoldX ( x0 , SysData ) ;
[ V0 , Q0 , s0 , d0 , w0 , eq10 , eq20 , ed10 , ed20 , psid0 , psiq0 , Pm0 , Ef0 , Vavrm0 , Vavrr0 , Vavrf0 , Vavrref0 , tgovg0 , tgovm0 , tgovmech0 , f0 , dpg0 , qplt0 , vg0 ] = unfoldX ( x0 , SysData ) ;
% import simualtion data khuang 8 JUl
[ ~ , ~ , ~ , nlvl , taylorN , ~ , ~ , ~ , ~ ] = unfoldSimData ( SimData ) ;
[ ~ , ~ , ~ , nlvl , taylorN , ~ , ~ , ~ , ~ ] = unfoldSimData ( SimData ) ;
% import system parameters khuang 8 JUl
[ pqIncr , pvIncr , Rind0 , Rind1 , Reind0 , Reind1 , Rzip0 , Rzip1 , Ytr0 , Ytr1 , Ysh0 , Ysh1 , VspSq2 , ~ , ~ , ~ , ~ , Tmech1 , Varref1 , Ef1 , Pm1 , Eq11 ] = unfoldSysPara ( SysPara ) ;
[ pqIncr , pvIncr , Rind0 , Rind1 , Reind0 , Reind1 , Rzip0 , Rzip1 , Ytr0 , Ytr1 , Ysh0 , Ysh1 , VspSq2 , ~ , ~ , ~ , ~ , Tmech1 , Varref1 , Ef1 , Pm1 , Eq11 ] = unfoldSysPara ( SysPara ) ;
%
%
% PQ incremental is given for every pq bus.
% need to figure how to utilize them, now i suppose this part is for CPF
% problem khuang 8 JUL
Pls = zeros ( nbus , 2 ) ; Pls ( pq ( : , 1 ) , 1 ) = pqIncr ( : , 1 ) ; if ~ isempty ( pv ) ; Pls ( pv ( : , 1 ) , 1 ) = Pls ( pv ( : , 1 ) , 1 ) - pvIncr ; end
Pls = zeros ( nbus , 2 ) ; Pls ( pq ( : , 1 ) , 1 ) = pqIncr ( : , 1 ) ; if ~ isempty ( pv ) ; Pls ( pv ( : , 1 ) , 1 ) = Pls ( pv ( : , 1 ) , 1 ) - pvIncr ; end
Qls = zeros ( nbus , 2 ) ; Qls ( pq ( : , 1 ) , 1 ) = pqIncr ( : , 2 ) ;
Qls = zeros ( nbus , 2 ) ; Qls ( pq ( : , 1 ) , 1 ) = pqIncr ( : , 2 ) ;
if size ( pqIncr , 2 ) > = 4
if size ( pqIncr , 2 ) > = 4
@ -63,10 +75,14 @@ if size(pqIncr,2)>=4
Qls ( pq ( : , 1 ) , 2 ) = pqIncr ( : , 4 ) ;
Qls ( pq ( : , 1 ) , 2 ) = pqIncr ( : , 4 ) ;
end
end
% formatting Ymatrix, here the default value of fault is empty, khuang 8 JUL
if isempty ( Ytr0 )
if isempty ( Ytr0 )
[ Y , Ytr0 , Ysh , ytrfr , ytrto , yshfr , yshto ] = getYMatrix ( nbus , line ) ;
[ Y , Ytr0 , Ysh , ytrfr , ytrto , yshfr , yshto ] = getYMatrix ( nbus , line ) ;
end
end
% reshape the pv, pq shunt and swing buses as zeros if they are empty
% khuang 8JUL
busType = zeros ( nbus , 1 ) ;
busType = zeros ( nbus , 1 ) ;
if isempty ( pv )
if isempty ( pv )
pv = zeros ( 0 , 6 ) ;
pv = zeros ( 0 , 6 ) ;
@ -80,33 +96,54 @@ end
if isempty ( sw )
if isempty ( sw )
sw = zeros ( 0 , 13 ) ;
sw = zeros ( 0 , 13 ) ;
end
end
% label pv and swing buses khuang 8 JUL
% 1: PV bus, 0: PQ bus
busType ( pv ( : , 1 ) ) = 1 ;
busType ( pv ( : , 1 ) ) = 1 ;
busType ( sw ( : , 1 ) ) = 2 ;
busType ( sw ( : , 1 ) ) = 2 ;
% zip(busType(zip(:,1))~=0,10)=0;
% zip(busType(zip(:,1))~=0,10)=0;
% index of slack bus (isw), pv bus(ipv), and pq bus(ipq)
% is given: isw, ipv, and ipq khuang 8 JUL
% Additionally, number of pv and pq buses are given:
%npv, npq respectively khuang 8 JUL
isw = find ( busType == 2 ) ;
isw = find ( busType == 2 ) ;
ipv = find ( busType == 1 ) ;
ipv = find ( busType == 1 ) ;
ipq = find ( busType == 0 ) ;
ipq = find ( busType == 0 ) ;
npq = size ( ipq , 1 ) ;
npq = size ( ipq , 1 ) ;
npv = size ( ipv , 1 ) ;
npv = size ( ipv , 1 ) ;
% shunt capacitator is initialized as yShunt which is a complex number.
% for every bus khuang 8 JUL
yShunt = zeros ( nbus , 1 ) ;
yShunt = zeros ( nbus , 1 ) ;
yShunt ( shunt ( : , 1 ) ) = shunt ( : , 5 ) + 1 j * shunt ( : , 6 ) ;
yShunt ( shunt ( : , 1 ) ) = shunt ( : , 5 ) + 1 j * shunt ( : , 6 ) ;
% check if zip load exists in the system
% and initialized zip load khuang 8 JUL
if ~ isempty ( zip ) %zipMode=0
if ~ isempty ( zip ) %zipMode=0
Ysh0 = Ysh0 + accumarray ( zip ( : , 1 ) , Rzip0 .* ( zip ( : , 5 ) + 1 j * zip ( : , 8 ) ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
Ysh0 = Ysh0 + accumarray ( zip ( : , 1 ) , Rzip0 .* ( zip ( : , 5 ) + 1 j * zip ( : , 8 ) ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
Ysh1 = Ysh1 + accumarray ( zip ( : , 1 ) , Rzip1 .* ( zip ( : , 5 ) + 1 j * zip ( : , 8 ) ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
Ysh1 = Ysh1 + accumarray ( zip ( : , 1 ) , Rzip1 .* ( zip ( : , 5 ) + 1 j * zip ( : , 8 ) ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
end
end
% now zip load + shunt khuang 8 JUL
Ysh0 = Ysh0 + yShunt ;
Ysh0 = Ysh0 + yShunt ;
% Y=Y+sparse(1:nbus,1:nbus,yShunt,nbus,nbus);
% Y=Y+sparse(1:nbus,1:nbus,yShunt,nbus,nbus);
% now zip load + shunt+ network Y matrix khuang 8 JUL
Y = Ytr0 + sparse ( 1 : nbus , 1 : nbus , Ysh0 , nbus , nbus ) ;
Y = Ytr0 + sparse ( 1 : nbus , 1 : nbus , Ysh0 , nbus , nbus ) ;
%initialize P and Q for every bus khuang 8 JUL
pVec = zeros ( nbus , 1 ) ;
pVec = zeros ( nbus , 1 ) ;
qVec = zeros ( nbus , 1 ) ;
qVec = zeros ( nbus , 1 ) ;
% vSp=zeros(nbus,1);
% vSp=zeros(nbus,1);
% need to figure out the meaning of index 1, 4,5
% based on Kaiyang's understanding, 1 is load side and 4&5 are generators'
% output khuang 8 JUL
pVec ( pv ( : , 1 ) ) = pVec ( pv ( : , 1 ) ) + pv ( : , 4 ) ;
pVec ( pv ( : , 1 ) ) = pVec ( pv ( : , 1 ) ) + pv ( : , 4 ) ;
pVec ( pq ( : , 1 ) ) = pVec ( pq ( : , 1 ) ) - pq ( : , 4 ) ;
pVec ( pq ( : , 1 ) ) = pVec ( pq ( : , 1 ) ) - pq ( : , 4 ) ;
qVec ( pq ( : , 1 ) ) = qVec ( pq ( : , 1 ) ) - pq ( : , 5 ) ;
qVec ( pq ( : , 1 ) ) = qVec ( pq ( : , 1 ) ) - pq ( : , 5 ) ;
% account the zip load, i.e dynamic load khuang 8 JUL
if ~ isempty ( zip ) %zipMode=0, account for the PQ components in ZIP loads
if ~ isempty ( zip ) %zipMode=0, account for the PQ components in ZIP loads
pVec = pVec - accumarray ( zip ( : , 1 ) , Rzip0 .* zip ( : , 7 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
pVec = pVec - accumarray ( zip ( : , 1 ) , Rzip0 .* zip ( : , 7 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
qVec = qVec - accumarray ( zip ( : , 1 ) , Rzip0 .* zip ( : , 10 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
qVec = qVec - accumarray ( zip ( : , 1 ) , Rzip0 .* zip ( : , 10 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
@ -114,21 +151,39 @@ end
% qVec(ipv)=qVec(ipv)+Q0(ipv);
% qVec(ipv)=qVec(ipv)+Q0(ipv);
% vSp(ipv)=pv(:,5);
% vSp(ipv)=pv(:,5);
% so far, initialization of PQ for every bus and Y matrix is ready khuang 8 JUL
% initiliza voltage V and W = 1/V for every bus khuang 8 JUL
V = zeros ( nbus , nlvl + 1 ) ;
V = zeros ( nbus , nlvl + 1 ) ;
V ( : , 1 ) = V0 ;
V ( : , 1 ) = V0 ;
W = zeros ( nbus , nlvl + 1 ) ;
W = zeros ( nbus , nlvl + 1 ) ;
W ( : , 1 ) = 1. / V0 ;
W ( : , 1 ) = 1. / V0 ;
% initiliza magnitude of voltage V for every bus khuang 8 JUL
Vmag = zeros ( nbus , nlvl + 1 ) ;
Vmag = zeros ( nbus , nlvl + 1 ) ;
Vmag ( : , 1 ) = abs ( V0 ) ;
Vmag ( : , 1 ) = abs ( V0 ) ;
% Power is initilized as we already cooked pVec and qVec khuang 8 JUL
P = zeros ( nbus , nlvl + 1 ) ;
P = zeros ( nbus , nlvl + 1 ) ;
P ( : , 1 ) = pVec ;
P ( : , 1 ) = pVec ;
% P(isw,2:end)=0;
% P(isw,2:end)=0;
% here we need to figure out what Q extra mean, and difference from Q
% notice that Q0 is initialized with sysmdata but not P0 khuang 8 JUL
Q = zeros ( nbus , nlvl + 1 ) ;
Q = zeros ( nbus , nlvl + 1 ) ;
Qxtra = zeros ( size ( Q ) ) ;
Qxtra = zeros ( size ( Q ) ) ;
Q ( : , 1 ) = Q0 ;
Q ( : , 1 ) = Q0 ;
Qxtra ( : , 1 ) = qVec ;
Qxtra ( : , 1 ) = qVec ;
% Also, the meaning of Pls and Qls need to be verified, i assume they are
% for CPF khuang 8 JUL
P ( : , 2 : ( size ( Pls , 2 ) + 1 ) ) = - Pls ;
P ( : , 2 : ( size ( Pls , 2 ) + 1 ) ) = - Pls ;
Qxtra ( : , 2 : ( size ( Qls , 2 ) + 1 ) ) = - Qls ;
Qxtra ( : , 2 : ( size ( Qls , 2 ) + 1 ) ) = - Qls ;
% In the previous, pVec and qvec are considered zip load, here Pls and Qls
% are not, so we need to do it.khuang 8 JUL
if ~ isempty ( zip )
if ~ isempty ( zip )
P ( : , 2 ) = P ( : , 2 ) - accumarray ( zip ( : , 1 ) , Rzip1 .* zip ( : , 7 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
P ( : , 2 ) = P ( : , 2 ) - accumarray ( zip ( : , 1 ) , Rzip1 .* zip ( : , 7 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
Qxtra ( : , 2 ) = Qxtra ( : , 2 ) - accumarray ( zip ( : , 1 ) , Rzip1 .* zip ( : , 10 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
Qxtra ( : , 2 ) = Qxtra ( : , 2 ) - accumarray ( zip ( : , 1 ) , Rzip1 .* zip ( : , 10 ) .* zip ( : , 12 ) , [ nbus , 1 ] ) ;
@ -136,11 +191,17 @@ end
% Qxtra(busType~=0,2:end)=Q(busType~=0,2:end);
% Qxtra(busType~=0,2:end)=Q(busType~=0,2:end);
% Q(busType~=0,2:end)=0;
% Q(busType~=0,2:end)=0;
% seperate real and image part of voltages and their inverse khuang 8 JUL
% here V = C+1i*D khuang 8 JUL
% and W = 1./V = E + 1i*F khuang 8 JUL
C0 = real ( V ( : , 1 ) ) ;
C0 = real ( V ( : , 1 ) ) ;
D0 = imag ( V ( : , 1 ) ) ;
D0 = imag ( V ( : , 1 ) ) ;
E0 = real ( W ( : , 1 ) ) ;
E0 = real ( W ( : , 1 ) ) ;
F0 = imag ( W ( : , 1 ) ) ;
F0 = imag ( W ( : , 1 ) ) ;
% Construct sparse matrix individually for C,D,E,F,P,Q. khuang 8 JUL
% Notice that Q = Q(:,1)+Qxtra(:,1) which is different from P. khuang 8 JUL
C0M = sparse ( 1 : nbus , 1 : nbus , C0 , nbus , nbus ) ;
C0M = sparse ( 1 : nbus , 1 : nbus , C0 , nbus , nbus ) ;
D0M = sparse ( 1 : nbus , 1 : nbus , D0 , nbus , nbus ) ;
D0M = sparse ( 1 : nbus , 1 : nbus , D0 , nbus , nbus ) ;
E0M = sparse ( 1 : nbus , 1 : nbus , E0 , nbus , nbus ) ;
E0M = sparse ( 1 : nbus , 1 : nbus , E0 , nbus , nbus ) ;
@ -148,10 +209,15 @@ F0M=sparse(1:nbus,1:nbus,F0,nbus,nbus);
P0M = sparse ( 1 : nbus , 1 : nbus , P ( : , 1 ) , nbus , nbus ) ;
P0M = sparse ( 1 : nbus , 1 : nbus , P ( : , 1 ) , nbus , nbus ) ;
Q0M = sparse ( 1 : nbus , 1 : nbus , Q ( : , 1 ) + Qxtra ( : , 1 ) , nbus , nbus ) ;
Q0M = sparse ( 1 : nbus , 1 : nbus , Q ( : , 1 ) + Qxtra ( : , 1 ) , nbus , nbus ) ;
% get real part and image part of Y matrix, not sure why do this. khuang 8 JUL
G = real ( Y ) ;
G = real ( Y ) ;
B = imag ( Y ) ;
B = imag ( Y ) ;
% so Y = G + 1i*B. khuang 8 JUL
% Determine the frequency model of each island
% this part is for AGC khuang 8 JUL
%--------------------------------------------------------------
% Determine the frequency model of each island
% 0:sw,1:syn,2:steady-state f
freqTypeTag = zeros ( nIslands , 1 ) ; %0:sw,1:syn,2:steady-state f
freqTypeTag = zeros ( nIslands , 1 ) ; %0:sw,1:syn,2:steady-state f
freqKeptTag = zeros ( nbus , 1 ) ;
freqKeptTag = zeros ( nbus , 1 ) ;
frefs = refs ;
frefs = refs ;
@ -177,16 +243,26 @@ end
freqKeptTagxRef = freqKeptTag ;
freqKeptTagxRef = freqKeptTag ;
freqKeptTagxRef ( frefs ) = 0 ;
freqKeptTagxRef ( frefs ) = 0 ;
nFreqKept = sum ( freqKeptTag ) ;
nFreqKept = sum ( freqKeptTag ) ;
%-----------------------------------------------------------
if ~ isempty ( ind )
nInd = size ( ind , 1 ) ;
indIdx = ind ( : , 1 ) ;
s = zeros ( nInd , nlvl + 1 ) ;
%------------------------------------------------------------------
s ( : , 1 ) = s0 ;
% this part is for initialling inductor. khuang 8 JUL
IL = zeros ( nInd , nlvl + 1 ) ;
if ~ isempty ( ind ) % check if there is any inductor% khuang 8 JUL
IR = zeros ( nInd , nlvl + 1 ) ;
nInd = size ( ind , 1 ) ; % determine the number of inductors % khuang 8 JUL
Vm = zeros ( nInd , nlvl + 1 ) ;
indIdx = ind ( : , 1 ) ; % store the index of inductors among all buses% khuang 8 JUL
s = zeros ( nInd , nlvl + 1 ) ; % slip% khuang 8 JUL
s ( : , 1 ) = s0 ; % initialize slip% khuang 8 JUL
IL = zeros ( nInd , nlvl + 1 ) ; % |
IR = zeros ( nInd , nlvl + 1 ) ; % |
Vm = zeros ( nInd , nlvl + 1 ) ; % initialization finished 0 value% khuang 8 JUL
%-----------------parameters of inductors---------% khuang 8 JUL
%-----------------START----------------% khuang 8 JUL
R1 = ind ( : , 7 ) ;
R1 = ind ( : , 7 ) ;
X1 = ind ( : , 8 ) ;
X1 = ind ( : , 8 ) ;
Z1 = ind ( : , 7 ) + 1 j * ind ( : , 8 ) ;
Z1 = ind ( : , 7 ) + 1 j * ind ( : , 8 ) ;
@ -197,11 +273,15 @@ if ~isempty(ind)
T1 = - ind ( : , 16 ) - 2 * ind ( : , 17 ) ;
T1 = - ind ( : , 16 ) - 2 * ind ( : , 17 ) ;
T2 = ind ( : , 17 ) ;
T2 = ind ( : , 17 ) ;
Hm = ind ( : , 14 ) ;
Hm = ind ( : , 14 ) ;
%-----------------parameters of inductors---------% khuang 8 JUL
%-----------------END----------------% khuang 8 JUL
Rm = zeros ( nInd , 1 ) ;
Rm = zeros ( nInd , 1 ) ;
Am = sparse ( indIdx , ( 1 : nInd ) ' , ones ( 1 , nInd ) , nbus , nInd ) ;
Am = sparse ( indIdx , ( 1 : nInd ) ' , ones ( 1 , nInd ) , nbus , nInd ) ;
% first order value of induction motor IL,VM,IR % khuang 8 JUL
IL ( : , 1 ) = V0 ( indIdx ) ./ ( Z1 + Ze .* ( R2 + 1 j * X2 .* s0 ) ./ ( R2 .* Reind0 + ( 1 j * X2 .* Reind0 + Ze ) .* s0 ) ) ;
IL ( : , 1 ) = V0 ( indIdx ) ./ ( Z1 + Ze .* ( R2 + 1 j * X2 .* s0 ) ./ ( R2 .* Reind0 + ( 1 j * X2 .* Reind0 + Ze ) .* s0 ) ) ;
Vm ( : , 1 ) = V0 ( indIdx ) - IL ( : , 1 ) .* Z1 ;
Vm ( : , 1 ) = V0 ( indIdx ) - IL ( : , 1 ) .* Z1 ;
IR ( : , 1 ) = Vm ( : , 1 ) .* s0 ./ ( R2 + 1 j * X2 .* s0 ) ;
IR ( : , 1 ) = Vm ( : , 1 ) .* s0 ./ ( R2 + 1 j * X2 .* s0 ) ;
@ -211,6 +291,7 @@ if ~isempty(ind)
JL0 = real ( IL ( : , 1 ) ) ;
JL0 = real ( IL ( : , 1 ) ) ;
KL0 = imag ( IL ( : , 1 ) ) ;
KL0 = imag ( IL ( : , 1 ) ) ;
% prepare the algebric matrix % khuang 8 JUL
Yeind0 = Reind0 ./ Ze ;
Yeind0 = Reind0 ./ Ze ;
Yeind1 = Reind1 ./ Ze ;
Yeind1 = Reind1 ./ Ze ;
Ye1ind0 = Reind0 .* Z1 ./ Ze ;
Ye1ind0 = Reind0 .* Z1 ./ Ze ;
@ -290,7 +371,7 @@ if ~isempty(ind)
RHS_C_Shr_sqz = [ temp2_c12_inv_sqz , ...
RHS_C_Shr_sqz = [ temp2_c12_inv_sqz , ...
- [ temp2_c12_inv_sqz ( : , 1 ) .* temp1_sqz ( : , 1 ) + temp2_c12_inv_sqz ( : , 3 ) .* temp1_sqz ( : , 2 ) , temp2_c12_inv_sqz ( : , 2 ) .* temp1_sqz ( : , 1 ) + temp2_c12_inv_sqz ( : , 4 ) .* temp1_sqz ( : , 2 ) , ...
- [ temp2_c12_inv_sqz ( : , 1 ) .* temp1_sqz ( : , 1 ) + temp2_c12_inv_sqz ( : , 3 ) .* temp1_sqz ( : , 2 ) , temp2_c12_inv_sqz ( : , 2 ) .* temp1_sqz ( : , 1 ) + temp2_c12_inv_sqz ( : , 4 ) .* temp1_sqz ( : , 2 ) , ...
temp2_c12_inv_sqz ( : , 1 ) .* temp1_sqz ( : , 3 ) + temp2_c12_inv_sqz ( : , 3 ) .* temp1_sqz ( : , 4 ) , temp2_c12_inv_sqz ( : , 2 ) .* temp1_sqz ( : , 3 ) + temp2_c12_inv_sqz ( : , 4 ) .* temp1_sqz ( : , 4 ) ] ] ; % R\[I,-CA^-1]
temp2_c12_inv_sqz ( : , 1 ) .* temp1_sqz ( : , 3 ) + temp2_c12_inv_sqz ( : , 3 ) .* temp1_sqz ( : , 4 ) , temp2_c12_inv_sqz ( : , 2 ) .* temp1_sqz ( : , 3 ) + temp2_c12_inv_sqz ( : , 4 ) .* temp1_sqz ( : , 4 ) ] ] ; % R\[I,-CA^-1]
% will be used to calculate algebric variabls for motors% khuang 8 JUL
LHS_MatInd_Bus_sqz = zeros ( nbus , 4 ) ; % \sum{-R\S} by buses
LHS_MatInd_Bus_sqz = zeros ( nbus , 4 ) ; % \sum{-R\S} by buses
LHS_MatInd_Bus_sqz ( : , 1 ) = accumarray ( indIdx , LHS_MatInd_Shr_sqz ( : , 1 ) , [ nbus , 1 ] ) ;
LHS_MatInd_Bus_sqz ( : , 1 ) = accumarray ( indIdx , LHS_MatInd_Shr_sqz ( : , 1 ) , [ nbus , 1 ] ) ;
LHS_MatInd_Bus_sqz ( : , 2 ) = accumarray ( indIdx , LHS_MatInd_Shr_sqz ( : , 2 ) , [ nbus , 1 ] ) ;
LHS_MatInd_Bus_sqz ( : , 2 ) = accumarray ( indIdx , LHS_MatInd_Shr_sqz ( : , 2 ) , [ nbus , 1 ] ) ;
@ -299,24 +380,29 @@ if ~isempty(ind)
else
else
s = zeros ( 0 , nlvl + 1 ) ;
s = zeros ( 0 , nlvl + 1 ) ;
end
end
% Initialization of inductors is finished % khuang 8 JUL
%------------------------------------------------------------------
%------------------------------Initialization of ZIP load---------% khuang 8 JUL
if ~ isempty ( zip )
if ~ isempty ( zip )
nZip = size ( zip , 1 ) ;
nZip = size ( zip , 1 ) ;
zipIdx = zip ( : , 1 ) ;
zipIdx = zip ( : , 1 ) ;
IiL = zeros ( nZip , nlvl + 1 ) ;
IiL = zeros ( nZip , nlvl + 1 ) ;
BiL = zeros ( nZip , nlvl + 1 ) ;
BiL = zeros ( nZip , nlvl + 1 ) ;
% prepare the necessary matrix by blocks% khuang 8 JUL
Bi0 = abs ( V0 ( zipIdx ) ) ;
Bi0 = abs ( V0 ( zipIdx ) ) ;
JI = zip ( : , 6 ) ;
JI = zip ( : , 6 ) ;
KI = - zip ( : , 9 ) ;
KI = - zip ( : , 9 ) ;
% current % khuang 8 JUL
Ii0L = Rzip0 .* ( JI + 1 j * KI ) .* V0 ( zipIdx ) ./ Bi0 ;
Ii0L = Rzip0 .* ( JI + 1 j * KI ) .* V0 ( zipIdx ) ./ Bi0 ;
Ji0L = real ( Ii0L ) ;
Ji0L = real ( Ii0L ) ;
Ki0L = imag ( Ii0L ) ;
Ki0L = imag ( Ii0L ) ;
IiL ( : , 1 ) = Ii0L ;
IiL ( : , 1 ) = Ii0L ;
BiL ( : , 1 ) = Bi0 ;
BiL ( : , 1 ) = Bi0 ;
% voltage% khuang 8 JUL
Ci0 = real ( V0 ( zipIdx ) ) ;
Ci0 = real ( V0 ( zipIdx ) ) ;
Di0 = imag ( V0 ( zipIdx ) ) ;
Di0 = imag ( V0 ( zipIdx ) ) ;
@ -326,38 +412,43 @@ if ~isempty(zip)
else
else
IiL = zeros ( 0 , nlvl + 1 ) ;
IiL = zeros ( 0 , nlvl + 1 ) ;
end
end
%------------------------------Initialization of ZIP load------------------% khuang 8 JUL
%------------------------------Initialization of ZIP load is finished---------------- % khuang 8 JUL
%------------------------------Initialization of GEN------------------% khuang 8 JUL
%------------------------------Start------------------------% khuang 8 JUL
nSyn = size ( syn , 1 ) ;
nSyn = size ( syn , 1 ) ;
if ~ isempty ( syn )
if ~ isempty ( syn )
synIdx = syn ( : , 1 ) ;
synIdx = syn ( : , 1 ) ; % index number of Generators% khuang 8 JUL
wgb = syn ( : , 4 ) ;
wgb = syn ( : , 4 ) ; % maybe the base value% khuang 8 JUL
modSyn = syn ( : , 5 ) ;
modSyn = syn ( : , 5 ) ; % the order of generator models% khuang 8 JUL
Xgl = syn ( : , 6 ) ;
Xgl = syn ( : , 6 ) ;
Rga = syn ( : , 7 ) ;
Rga = syn ( : , 7 ) ;
Xgd = syn ( : , 8 ) ;
Xgd = syn ( : , 8 ) ;
Xgd1 = syn ( : , 9 ) ;
Xgd1 = syn ( : , 9 ) ;
Xgd2 = syn ( : , 10 ) ;
Xgd2 = syn ( : , 10 ) ;
Tgd1 = syn ( : , 11 ) ;
Tgd1 = syn ( : , 11 ) ;
Tgd2 = syn ( : , 12 ) ;
Tgd2 = syn ( : , 12 ) ;
Xgq = syn ( : , 13 ) ;
Xgq = syn ( : , 13 ) ;
Xgq1 = syn ( : , 14 ) ;
Xgq1 = syn ( : , 14 ) ;
Xgq2 = syn ( : , 15 ) ;
Xgq2 = syn ( : , 15 ) ;
Tgq1 = syn ( : , 16 ) ;
Tgq1 = syn ( : , 16 ) ;
Tgq2 = syn ( : , 17 ) ;
Tgq2 = syn ( : , 17 ) ;
Mg = syn ( : , 18 ) ;
Mg = syn ( : , 18 ) ;
Dg = syn ( : , 19 ) ;
Dg = syn ( : , 19 ) ;
TgAA = syn ( : , 24 ) ;
TgAA = syn ( : , 24 ) ;
gammad = Tgd2 ./ Tgd1 .* Xgd2 ./ Xgd1 .* ( Xgd - Xgd1 ) ;
gammad = Tgd2 ./ Tgd1 .* Xgd2 ./ Xgd1 .* ( Xgd - Xgd1 ) ;
gammaq = Tgq2 ./ Tgq1 .* Xgq2 ./ Xgq1 .* ( Xgq - Xgq1 ) ;
gammaq = Tgq2 ./ Tgq1 .* Xgq2 ./ Xgq1 .* ( Xgq - Xgq1 ) ;
d = zeros ( nSyn , nlvl + 1 ) ;
d = zeros ( nSyn , nlvl + 1 ) ; % delta% khuang 8 JUL
w = zeros ( nSyn , nlvl + 1 ) ;
w = zeros ( nSyn , nlvl + 1 ) ; % omega% khuang 8 JUL
eq1 = zeros ( nSyn , nlvl + 1 ) ;
eq1 = zeros ( nSyn , nlvl + 1 ) ; %eq'% khuang 8 JUL
eq2 = zeros ( nSyn , nlvl + 1 ) ;
eq2 = zeros ( nSyn , nlvl + 1 ) ; %eq''% khuang 8 JUL
ed1 = zeros ( nSyn , nlvl + 1 ) ;
ed1 = zeros ( nSyn , nlvl + 1 ) ; %ed'% khuang 8 JUL
ed2 = zeros ( nSyn , nlvl + 1 ) ;
ed2 = zeros ( nSyn , nlvl + 1 ) ; %ed''% khuang 8 JUL
psiq = zeros ( nSyn , nlvl + 1 ) ;
psiq = zeros ( nSyn , nlvl + 1 ) ; % not sure, only in 8th order model% khuang 8 JUL
psid = zeros ( nSyn , nlvl + 1 ) ;
psid = zeros ( nSyn , nlvl + 1 ) ; % not sure, only in 8th order model% khuang 8 JUL
JG = zeros ( nSyn , nlvl + 1 ) ;
JG = zeros ( nSyn , nlvl + 1 ) ;
KG = zeros ( nSyn , nlvl + 1 ) ;
KG = zeros ( nSyn , nlvl + 1 ) ;
IGq = zeros ( nSyn , nlvl + 1 ) ;
IGq = zeros ( nSyn , nlvl + 1 ) ;
@ -373,7 +464,7 @@ if ~isempty(syn)
sind = sin ( d0 ) ;
sind = sin ( d0 ) ;
CG0 = C0 ( synIdx ) ;
CG0 = C0 ( synIdx ) ;
DG0 = D0 ( synIdx ) ;
DG0 = D0 ( synIdx ) ;
% the first value is given here, notice all are 8th order model% khuang 8 JUL
d ( : , 1 ) = d0 ;
d ( : , 1 ) = d0 ;
w ( : , 1 ) = w0 ;
w ( : , 1 ) = w0 ;
eq1 ( : , 1 ) = eq10 ;
eq1 ( : , 1 ) = eq10 ;
@ -382,13 +473,19 @@ if ~isempty(syn)
ed2 ( : , 1 ) = ed20 ;
ed2 ( : , 1 ) = ed20 ;
psiq ( : , 1 ) = psiq0 ;
psiq ( : , 1 ) = psiq0 ;
psid ( : , 1 ) = psid0 ;
psid ( : , 1 ) = psid0 ;
% transform between grid side and dq side% khuang 8 JUL
VGd ( : , 1 ) = sind .* CG0 - cosd .* DG0 ;
VGd ( : , 1 ) = sind .* CG0 - cosd .* DG0 ;
VGq ( : , 1 ) = cosd .* CG0 + sind .* DG0 ;
VGq ( : , 1 ) = cosd .* CG0 + sind .* DG0 ;
Cd ( : , 1 ) = cosd ;
% now they are under dq side% khuang 8 JUL
Sd ( : , 1 ) = sind ;
Cd ( : , 1 ) = cosd ; % first order of cos(delta)% khuang 8 JUL
Sd ( : , 1 ) = sind ; % first order of sin(delta)% khuang 8 JUL
Ef ( : , 1 ) = Ef0 ;
Ef ( : , 1 ) = Ef0 ;
Pm ( : , 1 ) = Pm0 ;
Pm ( : , 1 ) = Pm0 ;
%check if controller exists% khuang 8 JUL
if ~ isempty ( Ef1 )
if ~ isempty ( Ef1 )
Ef ( : , 2 ) = Ef1 ;
Ef ( : , 2 ) = Ef1 ;
end
end
@ -399,26 +496,47 @@ if ~isempty(syn)
Pm ( : , 2 ) = Pm1 ;
Pm ( : , 2 ) = Pm1 ;
end
end
% notice that here truncated taylor is applied % khuang 8 JUL
% and this is the key differnet from Dt rule% khuang 8 JUL
% Here only at most 5 th order taylor series are considered for sin% khuang 8 JUL
% and cos function % khuang 8 JUL
[ cosp , sinp , taylorN ] = getTaylorPolynomials ( d0 , taylorN ) ; % taylorN may be truncated
[ cosp , sinp , taylorN ] = getTaylorPolynomials ( d0 , taylorN ) ; % taylorN may be truncated
Mats = zeros ( nSyn , 4 ) ;
Mats = zeros ( nSyn , 4 ) ;
MatsR = zeros ( nSyn , 4 ) ;
MatsR = zeros ( nSyn , 4 ) ;
MatsRs = zeros ( nSyn , 4 ) ;
MatsRs = zeros ( nSyn , 4 ) ;
% count the number for different kinds models % khuang 8 JUL
% ex: modelTag = [ 0 0 0 0 0 10 0 0].' % khuang 8 JUL
% ex: there are 10 gens using 6th order model % khuang 8 JUL
modelTag = accumarray ( modSyn , ones ( nSyn , 1 ) , [ 8 , 1 ] ) ;
modelTag = accumarray ( modSyn , ones ( nSyn , 1 ) , [ 8 , 1 ] ) ;
% determine the order of the model % khuang 8 JUL
% Do we really need for loop? % khuang 8 JUL
% the answer is yes since different gen may use different% khuang 8 JUL
% order model% khuang 8 JUL
for i = 1 : nSyn
for i = 1 : nSyn
% 8th order, no need to change% khuang 8 JUL
if modSyn ( i ) == 8
if modSyn ( i ) == 8
IGd ( i , 1 ) = ( eq20 ( i ) - psid0 ( i ) ) / Xgd2 ( i ) ;
IGd ( i , 1 ) = ( eq20 ( i ) - psid0 ( i ) ) / Xgd2 ( i ) ;
IGq ( i , 1 ) = ( - ed20 ( i ) - psiq0 ( i ) ) / Xgq2 ( i ) ;
IGq ( i , 1 ) = ( - ed20 ( i ) - psiq0 ( i ) ) / Xgq2 ( i ) ;
Mats ( i , : ) = [ sind ( i ) , cosd ( i ) , - cosd ( i ) , sind ( i ) ] ;
Mats ( i , : ) = [ sind ( i ) , cosd ( i ) , - cosd ( i ) , sind ( i ) ] ;
% 6th order% khuang 8 JUL
elseif modSyn ( i ) == 6
elseif modSyn ( i ) == 6
% algebric equation to solve Id, Iq% khuang 8 JUL
IGd ( i , 1 ) = ( ( ed20 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Xgq2 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGd ( i , 1 ) = ( ( ed20 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Xgq2 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed20 ( i ) - VGd ( i , 1 ) ) * Xgd2 ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed20 ( i ) - VGd ( i , 1 ) ) * Xgd2 ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
% transform matrix (inverse version)% khuang 8 JUL
Mats ( i , : ) = [ sind ( i ) , cosd ( i ) , - cosd ( i ) , sind ( i ) ] ;
Mats ( i , : ) = [ sind ( i ) , cosd ( i ) , - cosd ( i ) , sind ( i ) ] ;
% Here matrix is the inverse matrix, to understand this% khuang 8 JUL
% We have A*Ixy+B*Vxy = f => MatsR = A^-1, MatsRs = A^-1*B = MatsRs*B% khuang 8 JUL
% so Ixy = MatsR*f-MatsRs*Vxy, which is used later to% khuang 8 JUL
% eliminate Ixy when disturbance happens% khuang 8 JUL
MatsR ( i , : ) = [ sind ( i ) * Rga ( i ) - cosd ( i ) * Xgd2 ( i ) , sind ( i ) * Xgq2 ( i ) + cosd ( i ) * Rga ( i ) , - cosd ( i ) * Rga ( i ) - sind ( i ) * Xgd2 ( i ) , - cosd ( i ) * Xgq2 ( i ) + sind ( i ) * Rga ( i ) ] / ...
MatsR ( i , : ) = [ sind ( i ) * Rga ( i ) - cosd ( i ) * Xgd2 ( i ) , sind ( i ) * Xgq2 ( i ) + cosd ( i ) * Rga ( i ) , - cosd ( i ) * Rga ( i ) - sind ( i ) * Xgd2 ( i ) , - cosd ( i ) * Xgq2 ( i ) + sind ( i ) * Rga ( i ) ] / ...
( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
% 5th order% khuang 8 JUL
elseif modSyn ( i ) == 5
elseif modSyn ( i ) == 5
IGd ( i , 1 ) = ( ( ed20 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Xgq2 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGd ( i , 1 ) = ( ( ed20 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Xgq2 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed20 ( i ) - VGd ( i , 1 ) ) * Xgd2 ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed20 ( i ) - VGd ( i , 1 ) ) * Xgd2 ( i ) + ( eq20 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
@ -427,6 +545,7 @@ if ~isempty(syn)
( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
( Rga ( i ) * Rga ( i ) + Xgd2 ( i ) * Xgq2 ( i ) ) ;
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
% 4th order% khuang 8 JUL
elseif modSyn ( i ) == 4
elseif modSyn ( i ) == 4
IGd ( i , 1 ) = ( ( ed10 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Xgq1 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
IGd ( i , 1 ) = ( ( ed10 ( i ) - VGd ( i , 1 ) ) * Rga ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Xgq1 ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed10 ( i ) - VGd ( i , 1 ) ) * Xgd1 ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
IGq ( i , 1 ) = ( - ( ed10 ( i ) - VGd ( i , 1 ) ) * Xgd1 ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
@ -435,6 +554,7 @@ if ~isempty(syn)
( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq1 ( i ) ) ;
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
% 3rd order% khuang 8 JUL
elseif modSyn ( i ) == 3
elseif modSyn ( i ) == 3
IGd ( i , 1 ) = ( ( - VGd ( i , 1 ) ) * Rga ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Xgq ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
IGd ( i , 1 ) = ( ( - VGd ( i , 1 ) ) * Rga ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Xgq ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
IGq ( i , 1 ) = ( - ( - VGd ( i , 1 ) ) * Xgd1 ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
IGq ( i , 1 ) = ( - ( - VGd ( i , 1 ) ) * Xgd1 ( i ) + ( eq10 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
@ -443,6 +563,7 @@ if ~isempty(syn)
( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
( Rga ( i ) * Rga ( i ) + Xgd1 ( i ) * Xgq ( i ) ) ;
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsRs ( i , : ) = [ MatsR ( i , 1 ) * sind ( i ) + MatsR ( i , 2 ) * cosd ( i ) , - MatsR ( i , 1 ) * cosd ( i ) + MatsR ( i , 2 ) * sind ( i ) , ...
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
% classical model% khuang 8 JUL
elseif modSyn ( i ) == 2
elseif modSyn ( i ) == 2
IGd ( i , 1 ) = ( ( - VGd ( i , 1 ) ) * Rga ( i ) + ( Ef0 ( i ) - VGq ( i , 1 ) ) * Xgq ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd ( i ) * Xgq ( i ) ) ;
IGd ( i , 1 ) = ( ( - VGd ( i , 1 ) ) * Rga ( i ) + ( Ef0 ( i ) - VGq ( i , 1 ) ) * Xgq ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd ( i ) * Xgq ( i ) ) ;
IGq ( i , 1 ) = ( - ( - VGd ( i , 1 ) ) * Xgd ( i ) + ( Ef0 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd ( i ) * Xgq ( i ) ) ;
IGq ( i , 1 ) = ( - ( - VGd ( i , 1 ) ) * Xgd ( i ) + ( Ef0 ( i ) - VGq ( i , 1 ) ) * Rga ( i ) ) / ( Rga ( i ) * Rga ( i ) + Xgd ( i ) * Xgq ( i ) ) ;
@ -453,9 +574,12 @@ if ~isempty(syn)
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
MatsR ( i , 3 ) * sind ( i ) + MatsR ( i , 4 ) * cosd ( i ) , - MatsR ( i , 3 ) * cosd ( i ) + MatsR ( i , 4 ) * sind ( i ) ] ;
end
end
end
end
JG ( : , 1 ) = sind .* IGd ( : , 1 ) + cosd .* IGq ( : , 1 ) ;
% not sure how to use them now, but they are zeroth order of Ix and Iy% khuang 8 JUL
JG ( : , 1 ) = sind .* IGd ( : , 1 ) + cosd .* IGq ( : , 1 ) ;
KG ( : , 1 ) = - cosd .* IGd ( : , 1 ) + sind .* IGq ( : , 1 ) ;
KG ( : , 1 ) = - cosd .* IGd ( : , 1 ) + sind .* IGq ( : , 1 ) ;
% put previous matrix in a right place in all buses instead of only% khuang 8 JUL
% generator buses% khuang 8 JUL
MatGCD = - [ sparse ( synIdx , synIdx , MatsRs ( : , 1 ) , nbus , nbus ) , sparse ( synIdx , synIdx , MatsRs ( : , 2 ) , nbus , nbus ) ; ...
MatGCD = - [ sparse ( synIdx , synIdx , MatsRs ( : , 1 ) , nbus , nbus ) , sparse ( synIdx , synIdx , MatsRs ( : , 2 ) , nbus , nbus ) ; ...
sparse ( synIdx , synIdx , MatsRs ( : , 3 ) , nbus , nbus ) , sparse ( synIdx , synIdx , MatsRs ( : , 4 ) , nbus , nbus ) ] ;
sparse ( synIdx , synIdx , MatsRs ( : , 3 ) , nbus , nbus ) , sparse ( synIdx , synIdx , MatsRs ( : , 4 ) , nbus , nbus ) ] ;
else
else
@ -478,43 +602,57 @@ else
Ef = zeros ( 0 , nlvl + 1 ) ;
Ef = zeros ( 0 , nlvl + 1 ) ;
Pm = zeros ( 0 , nlvl + 1 ) ;
Pm = zeros ( 0 , nlvl + 1 ) ;
end
end
%------------------------------Initialization of GEN------------------% khuang 8 JUL
%------------------------------EnD------------------------------------% khuang 8 JUL
%------------------------------Initialization of Exciter------------------% khuang 8 JUL
%------------------------------START------------------------------------% khuang 8 JUL
if ~ isempty ( exc )
if ~ isempty ( exc )
nExc = size ( exc , 1 ) ;
nExc = size ( exc , 1 ) ;
% All Type 3 AVR
% All Type 3 AVR , a 3rd order controller
% for Type 3 AVR, avr0(:,1:3) are Vavrm, Vavrr, Vavrf,
% for Type 3 AVR, avr0(:,1:3) are Vavrm, Vavrr, Vavrf,
% and avr0(:,4) is reference Vref (input for secondary voltage control).
% and avr0(:,4) is reference Vref (input for secondary voltage control).
excIdx = exc ( : , 1 ) ;
excIdx = exc ( : , 1 ) ;
VavrMax = exc ( : , 3 ) ;
VavrMax = exc ( : , 3 ) ;
VavrMin = exc ( : , 4 ) ;
VavrMin = exc ( : , 4 ) ;
muavr0 = exc ( : , 5 ) ;
muavr0 = exc ( : , 5 ) ;
Tavr1 = exc ( : , 7 ) ;
Tavr1 = exc ( : , 7 ) ;
Tavr2 = exc ( : , 6 ) ;
Tavr2 = exc ( : , 6 ) ;
vavrf0 = exc ( : , 8 ) ;
vavrf0 = exc ( : , 8 ) ;
Vavr0 = exc ( : , 9 ) ;
Vavr0 = exc ( : , 9 ) ;
Tavre = exc ( : , 10 ) ;
Tavre = exc ( : , 10 ) ;
Tavrr = exc ( : , 11 ) ;
Tavrr = exc ( : , 11 ) ;
Vavrm = zeros ( nExc , nlvl + 1 ) ;
%here I need to check why Vavrref is time varing instead of constant% khuang 8 JUL
Vavrr = zeros ( nExc , nlvl + 1 ) ;
% memory is given to state variables of EXC% khuang 8 JUL
Vavrf = zeros ( nExc , nlvl + 1 ) ;
Vavrm = zeros ( nExc , nlvl + 1 ) ;
Vavrref = zeros ( nExc , nlvl + 1 ) ;
Vavrr = zeros ( nExc , nlvl + 1 ) ;
Vavrf = zeros ( nExc , nlvl + 1 ) ;
Vavrref = zeros ( nExc , nlvl + 1 ) ;
% zeroth order value is given% khuang 8 JUL
Vavrm ( : , 1 ) = real ( Vavrm0 ) ;
Vavrm ( : , 1 ) = real ( Vavrm0 ) ;
Vavrr ( : , 1 ) = real ( Vavrr0 ) ;
Vavrr ( : , 1 ) = real ( Vavrr0 ) ;
Vavrf ( : , 1 ) = real ( Vavrf0 ) ;
Vavrf ( : , 1 ) = real ( Vavrf0 ) ;
Vavrref ( : , 1 ) = real ( Vavrref0 ) ;
Vavrref ( : , 1 ) = real ( Vavrref0 ) ;
% here Varref1 is given with syspara.% khuang 8 JUL
if ~ isempty ( Varref1 )
if ~ isempty ( Varref1 )
Vavrref ( : , 2 ) = Varref1 ;
Vavrref ( : , 2 ) = Varref1 ;
end
end
% non-windup limiter, check the limit.% khuang 8 JUL
tavrMaxDiff = Vavrf ( : , 1 ) - VavrMax ;
tavrMaxDiff = Vavrf ( : , 1 ) - VavrMax ;
tavrMinDiff = Vavrf ( : , 1 ) - VavrMin ;
tavrMinDiff = Vavrf ( : , 1 ) - VavrMin ;
% label values in different interval.% khuang 8 JUL
avrSt = zeros ( nExc , 1 ) ;
avrSt = zeros ( nExc , 1 ) ;
avrSt ( tavrMaxDiff > 0 ) = 1 ;
avrSt ( tavrMaxDiff > 0 ) = 1 ;
avrSt ( tavrMinDiff < 0 ) = - 1 ;
avrSt ( tavrMinDiff < 0 ) = - 1 ;
% output after the limiter.% khuang 8 JUL
Ef ( excIdx ( avrSt == - 1 ) , 1 ) = VavrMin ( avrSt == - 1 ) ;
Ef ( excIdx ( avrSt == - 1 ) , 1 ) = VavrMin ( avrSt == - 1 ) ;
Ef ( excIdx ( avrSt == 1 ) , 1 ) = VavrMax ( avrSt == 1 ) ;
Ef ( excIdx ( avrSt == 1 ) , 1 ) = VavrMax ( avrSt == 1 ) ;
Ef ( excIdx ( avrSt == 0 ) , 1 ) = Vavrf ( avrSt == 0 , 1 ) ;
Ef ( excIdx ( avrSt == 0 ) , 1 ) = Vavrf ( avrSt == 0 , 1 ) ;
@ -525,37 +663,48 @@ else
Vavrf = zeros ( 0 , nlvl + 1 ) ;
Vavrf = zeros ( 0 , nlvl + 1 ) ;
Vavrref = zeros ( 0 , nlvl + 1 ) ;
Vavrref = zeros ( 0 , nlvl + 1 ) ;
end
end
%------------------------------Initialization of Exciter------------------% khuang 8 JUL
%------------------------------END------------------------------------% khuang 8 JUL
%------------------------------Initialization of Turbing Governor------------------% khuang 8 JUL
%------------------------------START------------------------------------% khuang 8 JUL
if ~ isempty ( tg )
if ~ isempty ( tg )
nTg = size ( tg , 1 ) ;
nTg = size ( tg , 1 ) ;
% Type 2 Turbing governor.
% Type 2 Turbing governor.
tgIdx = tg ( : , 1 ) ;
% one DE, one AE and one limiter
tgIdx = tg ( : , 1 ) ;
wtgref = tg ( : , 3 ) ;
wtgref = tg ( : , 3 ) ;
Rtg = tg ( : , 4 ) ;
Rtg = tg ( : , 4 ) ;
Ttgmax = tg ( : , 5 ) ;
Ttgmax = tg ( : , 5 ) ;
Ttgmin = tg ( : , 6 ) ;
Ttgmin = tg ( : , 6 ) ;
Ttg2 = tg ( : , 7 ) ;
Ttg2 = tg ( : , 7 ) ;
Ttg1 = tg ( : , 8 ) ;
Ttg1 = tg ( : , 8 ) ;
tgovg = zeros ( nTg , nlvl + 1 ) ;
tgovg = zeros ( nTg , nlvl + 1 ) ; % tg % khuang 8 JUL
tgovm = zeros ( nTg , nlvl + 1 ) ;
tgovm = zeros ( nTg , nlvl + 1 ) ; % Tmi* % khuang 8 JUL
Tmech = zeros ( nTg , nlvl + 1 ) ;
Tmech = zeros ( nTg , nlvl + 1 ) ; % Tmi0 % khuang 8 JUL
% zeroth value is given % khuang 8 JUL
tgovg ( : , 1 ) = real ( tgovg0 ) ;
tgovg ( : , 1 ) = real ( tgovg0 ) ;
tgovm ( : , 1 ) = real ( tgovm0 ) ;
tgovm ( : , 1 ) = real ( tgovm0 ) ;
Tmech ( : , 1 ) = real ( tgovmech0 ) ;
Tmech ( : , 1 ) = real ( tgovmech0 ) ;
if ~ isempty ( Tmech1 )
if ~ isempty ( Tmech1 )
Tmech ( : , 2 ) = Tmech1 ;
Tmech ( : , 2 ) = Tmech1 ;
end
end
% check if limit is approached % khuang 8 JUL
tgovMaxDiff = tgovm ( : , 1 ) - Ttgmax ;
tgovMaxDiff = tgovm ( : , 1 ) - Ttgmax ;
tgovMinDiff = tgovm ( : , 1 ) - Ttgmin ;
tgovMinDiff = tgovm ( : , 1 ) - Ttgmin ;
govSt = zeros ( nTg , 1 ) ;
govSt = zeros ( nTg , 1 ) ;
govSt ( tgovMaxDiff > 0 ) = 1 ;
govSt ( tgovMaxDiff > 0 ) = 1 ;
govSt ( tgovMinDiff < 0 ) = - 1 ;
govSt ( tgovMinDiff < 0 ) = - 1 ;
% if limit is approached, set Pm to constant value. % khuang 8 JUL
Pm ( tgIdx ( govSt == 0 ) , 1 ) = tgovm ( govSt == 0 , 1 ) ;
Pm ( tgIdx ( govSt == 0 ) , 1 ) = tgovm ( govSt == 0 , 1 ) ;
Pm ( tgIdx ( govSt == 1 ) , 1 ) = Ttgmax ( govSt == 1 , 1 ) ;
Pm ( tgIdx ( govSt == 1 ) , 1 ) = Ttgmax ( govSt == 1 , 1 ) ;
Pm ( tgIdx ( govSt == - 1 ) , 1 ) = Ttgmin ( govSt == - 1 , 1 ) ;
Pm ( tgIdx ( govSt == - 1 ) , 1 ) = Ttgmin ( govSt == - 1 , 1 ) ;
@ -564,7 +713,12 @@ else
tgovm = zeros ( 0 , nlvl + 1 ) ;
tgovm = zeros ( 0 , nlvl + 1 ) ;
Tmech = zeros ( 0 , nlvl + 1 ) ;
Tmech = zeros ( 0 , nlvl + 1 ) ;
end
end
%------------------------------Initialization of Turbing Governor------------------% khuang 8 JUL
%------------------------------END------------------------------------% khuang 8 JUL
% this part i don't quite understand. It looks like f denotes frequency % khuang 1 JUL
% on every bus, is it relevant with frequency dependant load? % khuang 1 JUL
% now i find that this is for dynamics of agc. % khuang 8 JUL
f = zeros ( nbus , nlvl + 1 ) ;
f = zeros ( nbus , nlvl + 1 ) ;
f ( : , 1 ) = f0 ;
f ( : , 1 ) = f0 ;
synTag = zeros ( nbus , 1 ) ;
synTag = zeros ( nbus , 1 ) ;
@ -581,6 +735,7 @@ for islIdx=1:nIslands
end
end
end
end
%AGC part % khuang 8 JUL
if ~ isempty ( agc )
if ~ isempty ( agc )
agcExt = zeros ( nbus , size ( agc , 2 ) ) ;
agcExt = zeros ( nbus , size ( agc , 2 ) ) ;
agcExt ( agc ( : , 1 ) , : ) = agc ;
agcExt ( agc ( : , 1 ) , : ) = agc ;
@ -592,6 +747,7 @@ else
fdk = zeros ( nbus , 1 ) ;
fdk = zeros ( nbus , 1 ) ;
end
end
% this is for long term dynamic, it seems that not considered here % khuang 8 JUL
if ~ isempty ( cac ) && ~ isempty ( cluster )
if ~ isempty ( cac ) && ~ isempty ( cluster )
else
else
@ -599,14 +755,16 @@ else
vg = zeros ( 0 , nlvl + 1 ) ;
vg = zeros ( 0 , nlvl + 1 ) ;
end
end
% freq
% freq relevant part induced by AGC % khuang 8 JUL
FreqReal = sparse ( 1 : nbus , 1 : nbus , - freqKeptTag .* fdk .* E0 , nbus , nbus ) ;
FreqReal = sparse ( 1 : nbus , 1 : nbus , - freqKeptTag .* fdk .* E0 , nbus , nbus ) ;
FreqImag = sparse ( 1 : nbus , 1 : nbus , - freqKeptTag .* fdk .* F0 , nbus , nbus ) ;
FreqImag = sparse ( 1 : nbus , 1 : nbus , - freqKeptTag .* fdk .* F0 , nbus , nbus ) ;
Freq2freq = sparse ( [ 1 : nbus , 1 : nbus ] , [ 1 : nbus , frefs ( islands ) ' ] , [ ones ( 1 , nbus ) , - ones ( 1 , nbus ) ] , nbus , nbus ) ;
Freq2freq = sparse ( [ 1 : nbus , 1 : nbus ] , [ 1 : nbus , frefs ( islands ) ' ] , [ ones ( 1 , nbus ) , - ones ( 1 , nbus ) ] , nbus , nbus ) ;
Y11 = - G ; Y12 = B ; Y21 = - B ; Y22 = - G ;
Y11 = - G ; Y12 = B ; Y21 = - B ; Y22 = - G ;
% Influence to Origianl Power flow % khuang 8 JUL
YEF11 = P0M + sparse ( 1 : nbus , 1 : nbus , freqKeptTag .* ( - fdk .* f0 + dpg0 ) , nbus , nbus ) ; YEF12 = - Q0M ; YEF21 = - Q0M ; YEF22 = - P0M - sparse ( 1 : nbus , 1 : nbus , freqKeptTag .* ( - fdk .* f0 + dpg0 ) , nbus , nbus ) ;
YEF11 = P0M + sparse ( 1 : nbus , 1 : nbus , freqKeptTag .* ( - fdk .* f0 + dpg0 ) , nbus , nbus ) ; YEF12 = - Q0M ; YEF21 = - Q0M ; YEF22 = - P0M - sparse ( 1 : nbus , 1 : nbus , freqKeptTag .* ( - fdk .* f0 + dpg0 ) , nbus , nbus ) ;
% Counting influence of ZIP load into Y matrix. % khuang 8 JUL
if ~ isempty ( zip )
if ~ isempty ( zip )
Y11 = Y11 - sparse ( 1 : nbus , 1 : nbus , accumarray ( zipIdx , LHS_MatZip ( : , 1 ) , [ nbus , 1 ] ) , nbus , nbus ) ;
Y11 = Y11 - sparse ( 1 : nbus , 1 : nbus , accumarray ( zipIdx , LHS_MatZip ( : , 1 ) , [ nbus , 1 ] ) , nbus , nbus ) ;
Y12 = Y12 - sparse ( 1 : nbus , 1 : nbus , accumarray ( zipIdx , LHS_MatZip ( : , 2 ) , [ nbus , 1 ] ) , nbus , nbus ) ;
Y12 = Y12 - sparse ( 1 : nbus , 1 : nbus , accumarray ( zipIdx , LHS_MatZip ( : , 2 ) , [ nbus , 1 ] ) , nbus , nbus ) ;
@ -615,12 +773,14 @@ if ~isempty(zip)
end
end
YLHS = [ Y11 , Y12 ; Y21 , Y22 ] ;
YLHS = [ Y11 , Y12 ; Y21 , Y22 ] ;
% Counting influence of Motors into small Y matrix % khuang 8 JUL
if ~ isempty ( ind )
if ~ isempty ( ind )
YLHS = YLHS - ...
YLHS = YLHS - ...
[ sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 1 ) , nbus , nbus ) , sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 3 ) , nbus , nbus ) ; ...
[ sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 1 ) , nbus , nbus ) , sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 3 ) , nbus , nbus ) ; ...
sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 2 ) , nbus , nbus ) , sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 4 ) , nbus , nbus ) ] ;
sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 2 ) , nbus , nbus ) , sparse ( 1 : nbus , 1 : nbus , LHS_MatInd_Bus_sqz ( : , 4 ) , nbus , nbus ) ] ;
end
end
% Counting influence of generators into small Y matrix % khuang 8 JUL
if ~ isempty ( syn )
if ~ isempty ( syn )
YLHS = YLHS + MatGCD ;
YLHS = YLHS + MatGCD ;
end
end
@ -629,6 +789,7 @@ idxNonSw=find(busType~=2);
idxNonSwxD = find ( fswTagxD == 0 ) ;
idxNonSwxD = find ( fswTagxD == 0 ) ;
idxNonSwD = find ( busType ~= 2 & fswTagxD == 1 ) ;
idxNonSwD = find ( busType ~= 2 & fswTagxD == 1 ) ;
% This is the left hand side matrix totally % khuang 8 JUL
LHS_mat = [ YLHS ( [ idxNonSw ; idxNonSw + nbus ] , [ idxNonSw ; idxNonSw + nbus ] ) , ...
LHS_mat = [ YLHS ( [ idxNonSw ; idxNonSw + nbus ] , [ idxNonSw ; idxNonSw + nbus ] ) , ...
[ YEF11 ( idxNonSw , idxNonSw ) , YEF12 ( idxNonSw , idxNonSw ) , - F0M ( idxNonSw , ipv ) , FreqReal ( idxNonSw , freqKeptTag == 1 ) ; ...
[ YEF11 ( idxNonSw , idxNonSw ) , YEF12 ( idxNonSw , idxNonSw ) , - F0M ( idxNonSw , ipv ) , FreqReal ( idxNonSw , freqKeptTag == 1 ) ; ...
YEF21 ( idxNonSw , idxNonSw ) , YEF22 ( idxNonSw , idxNonSw ) , - E0M ( idxNonSw , ipv ) , - FreqImag ( idxNonSw , freqKeptTag == 1 ) ] ; ...
YEF21 ( idxNonSw , idxNonSw ) , YEF22 ( idxNonSw , idxNonSw ) , - E0M ( idxNonSw , ipv ) , - FreqImag ( idxNonSw , freqKeptTag == 1 ) ] ; ...
@ -642,6 +803,11 @@ LHS_mat=[YLHS([idxNonSw;idxNonSw+nbus],[idxNonSw;idxNonSw+nbus]),...
% [L_LHS_mat,U_LHS_mat,p_LHS_mat]=lu(LHS_mat,'vector');
% [L_LHS_mat,U_LHS_mat,p_LHS_mat]=lu(LHS_mat,'vector');
% end
% end
% deterine if we use LU factoration
% for this part, i assume the system algebrac equation is under a good
% condition number and the dimension is not very high, otherwise LU will
% be time consuming % khuang 8 JUL
useLU = isfield ( SysPara , ' iden' ) && isfield ( SysPara , ' p_amd' ) ;
useLU = isfield ( SysPara , ' iden' ) && isfield ( SysPara , ' p_amd' ) ;
if useLU
if useLU
@ -660,8 +826,22 @@ if useLU
end
end
end
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%this is the recursive part for computing high order of time series%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% khuang 8 JUL
% strat interations nlvl: order of Taylor series. % khuang 8 JUL
for i = 1 : nlvl
for i = 1 : nlvl
% khuang 8 JUL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% seq2 provides two columns from 0 to i, and i to 0
% seq2p provides two columns from 0 to i+1, and i+1 to 0
% seq3 provides 3 columns, the summary of each row is equal to i(binominal coefficients)
% khuang 8 JUL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
seq2 = getseq ( i , 2 ) ;
seq2 = getseq ( i , 2 ) ;
seq2p = getseq ( i + 1 , 2 ) ;
seq2p = getseq ( i + 1 , 2 ) ;
seq3 = getseq ( i , 3 ) ;
seq3 = getseq ( i , 3 ) ;
@ -670,6 +850,15 @@ for i=1:nlvl
idxSeq2p = sum ( seq2p > = i , 2 ) ;
idxSeq2p = sum ( seq2p > = i , 2 ) ;
idxSeq3 = sum ( seq3 == i , 2 ) ;
idxSeq3 = sum ( seq3 == i , 2 ) ;
idxSeq3x = sum ( seq3 ( : , [ 2 , 3 ] ) == i , 2 ) ;
idxSeq3x = sum ( seq3 ( : , [ 2 , 3 ] ) == i , 2 ) ;
% khuang 8 JUL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% seq2R is usually used in constructing algebric equations
% seq2R provides two columns from 1 to i-1, and i-1 to 1
% seq2x provides two columns from 1 to i, and i-1 to 0
% seq2m provides two columns from 0 to i-1, and i-1 to 0
% seq2mm provides two columns from 0 to i-2, and i-2 to 0
% khuang 8 JUL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
seq2R = seq2 ( idxSeq2 == 0 , : ) ;
seq2R = seq2 ( idxSeq2 == 0 , : ) ;
seq2x = seq2 ( idxSeq2x == 0 , : ) ;
seq2x = seq2 ( idxSeq2x == 0 , : ) ;
seq2m = getseq ( i - 1 , 2 ) ;
seq2m = getseq ( i - 1 , 2 ) ;
@ -677,7 +866,10 @@ for i=1:nlvl
RHSILr = zeros ( nbus , 1 ) ;
RHSILr = zeros ( nbus , 1 ) ;
RHSILi = zeros ( nbus , 1 ) ;
RHSILi = zeros ( nbus , 1 ) ;
% This part is for induction motor % khuang 8 JUL
if ~ isempty ( ind )
if ~ isempty ( ind )
% package right hand side vector at every iteration. % khuang 8 JUL
rhsM = sum ( Vm ( : , seq2R ( : , 1 ) + 1 ) .* s ( : , seq2R ( : , 2 ) + 1 ) , 2 ) - 1 j * X2 .* sum ( IR ( : , seq2R ( : , 1 ) + 1 ) .* s ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ;
rhsM = sum ( Vm ( : , seq2R ( : , 1 ) + 1 ) .* s ( : , seq2R ( : , 2 ) + 1 ) , 2 ) - 1 j * X2 .* sum ( IR ( : , seq2R ( : , 1 ) + 1 ) .* s ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ;
% rhsI=-real(sum(IR(:,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2))+...
% rhsI=-real(sum(IR(:,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2))+...
% (T1.*sum(s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)+T2.*sum(s(:,seq3R(:,1)+1).*s(:,seq3R(:,2)+1).*s(:,seq3R(:,3)+1),2))./R2+...
% (T1.*sum(s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)+T2.*sum(s(:,seq3R(:,1)+1).*s(:,seq3R(:,2)+1).*s(:,seq3R(:,3)+1),2))./R2+...
@ -692,6 +884,8 @@ for i=1:nlvl
% ./(2*Hm.*s(:,1)*i);
% ./(2*Hm.*s(:,1)*i);
% end
% end
% update the high order of slip, a special setting is required for
% low order when i<2. % khuang 8 JUL
s ( : , i + 1 ) = ( Rind0 .* ( T1 .* s ( : , i ) + T2 .* sum ( s ( : , seq2m ( : , 1 ) + 1 ) .* s ( : , seq2m ( : , 2 ) + 1 ) , 2 ) ) - real ( sum ( Vm ( : , seq2m ( : , 1 ) + 1 ) .* conj ( IR ( : , seq2m ( : , 2 ) + 1 ) ) , 2 ) ) ) ./ ( 2 * Hm * i ) ;
s ( : , i + 1 ) = ( Rind0 .* ( T1 .* s ( : , i ) + T2 .* sum ( s ( : , seq2m ( : , 1 ) + 1 ) .* s ( : , seq2m ( : , 2 ) + 1 ) , 2 ) ) - real ( sum ( Vm ( : , seq2m ( : , 1 ) + 1 ) .* conj ( IR ( : , seq2m ( : , 2 ) + 1 ) ) , 2 ) ) ) ./ ( 2 * Hm * i ) ;
if i > = 2
if i > = 2
s ( : , i + 1 ) = s ( : , i + 1 ) + ...
s ( : , i + 1 ) = s ( : , i + 1 ) + ...
@ -704,6 +898,7 @@ for i=1:nlvl
if i == 2
if i == 2
s ( : , i + 1 ) = s ( : , i + 1 ) + Rind1 .* T0 ./ ( 2 * Hm * i ) ;
s ( : , i + 1 ) = s ( : , i + 1 ) + Rind1 .* T0 ./ ( 2 * Hm * i ) ;
end
end
% for dynamic model, Right hand side vector is required a update. % khuang 8 JUL
addenRhs = Vm ( : , 1 ) .* s ( : , i + 1 ) - 1 j * X2 .* IR ( : , 1 ) .* s ( : , i + 1 ) ;
addenRhs = Vm ( : , 1 ) .* s ( : , i + 1 ) - 1 j * X2 .* IR ( : , 1 ) .* s ( : , i + 1 ) ;
% rhsBus=zeros(2,nInd);
% rhsBus=zeros(2,nInd);
@ -711,10 +906,12 @@ for i=1:nlvl
% rhsBus(:,j)=RHS_C_Shr{j}*[real(rhsM(j)+addenRhs(j));imag(rhsM(j)+addenRhs(j));0;0];
% rhsBus(:,j)=RHS_C_Shr{j}*[real(rhsM(j)+addenRhs(j));imag(rhsM(j)+addenRhs(j));0;0];
% end
% end
% count the influence of dynamic of slip into rigt hand side
% vector.% khuang 8 JUL
tempRhsInd = rhsM + addenRhs ;
tempRhsInd = rhsM + addenRhs ;
rhsBus = [ RHS_C_Shr_sqz ( : , 1 ) .* real ( tempRhsInd ) + RHS_C_Shr_sqz ( : , 3 ) .* imag ( tempRhsInd ) , RHS_C_Shr_sqz ( : , 2 ) .* real ( tempRhsInd ) + RHS_C_Shr_sqz ( : , 4 ) .* imag ( tempRhsInd ) ] ' ;
rhsBus = [ RHS_C_Shr_sqz ( : , 1 ) .* real ( tempRhsInd ) + RHS_C_Shr_sqz ( : , 3 ) .* imag ( tempRhsInd ) , RHS_C_Shr_sqz ( : , 2 ) .* real ( tempRhsInd ) + RHS_C_Shr_sqz ( : , 4 ) .* imag ( tempRhsInd ) ] ' ;
%accumulate currents
%accumulate currents IL.% khuang 8 JUL
RHSILr = accumarray ( indIdx , rhsBus ( 1 , : ) ' , [ nbus , 1 ] ) ;
RHSILr = accumarray ( indIdx , rhsBus ( 1 , : ) ' , [ nbus , 1 ] ) ;
RHSILi = accumarray ( indIdx , rhsBus ( 2 , : ) ' , [ nbus , 1 ] ) ;
RHSILi = accumarray ( indIdx , rhsBus ( 2 , : ) ' , [ nbus , 1 ] ) ;
@ -734,6 +931,7 @@ for i=1:nlvl
% RHSILi=accumarray(indIdx,rhsBus(4,:)',[nbus,1]);
% RHSILi=accumarray(indIdx,rhsBus(4,:)',[nbus,1]);
end
end
% strat update ZIP load into currents.% khuang 8 JUL
RHSIiLr = zeros ( nbus , 1 ) ;
RHSIiLr = zeros ( nbus , 1 ) ;
RHSIiLi = zeros ( nbus , 1 ) ;
RHSIiLi = zeros ( nbus , 1 ) ;
if ~ isempty ( zip )
if ~ isempty ( zip )
@ -745,6 +943,7 @@ for i=1:nlvl
RHSIiLi = accumarray ( zipIdx , RHSILi_full , [ nbus , 1 ] ) ;
RHSIiLi = accumarray ( zipIdx , RHSILi_full , [ nbus , 1 ] ) ;
end
end
% Start update generators.% khuang 8 JUL
RHSIGr = zeros ( nbus , 1 ) ;
RHSIGr = zeros ( nbus , 1 ) ;
RHSIGi = zeros ( nbus , 1 ) ;
RHSIGi = zeros ( nbus , 1 ) ;
if ~ isempty ( syn )
if ~ isempty ( syn )
@ -752,7 +951,7 @@ for i=1:nlvl
RhsEq = zeros ( nSyn , 1 ) ;
RhsEq = zeros ( nSyn , 1 ) ;
IGdAdd = zeros ( nSyn , 1 ) ;
IGdAdd = zeros ( nSyn , 1 ) ;
IGqAdd = zeros ( nSyn , 1 ) ;
IGqAdd = zeros ( nSyn , 1 ) ;
% select different models for generators.% khuang 8 JUL
if modelTag ( 8 ) > 0
if modelTag ( 8 ) > 0
d ( modSyn == 8 , i + 1 ) = ( wgb ( modSyn == 8 ) .* w ( modSyn == 8 , i ) ) / i ;
d ( modSyn == 8 , i + 1 ) = ( wgb ( modSyn == 8 ) .* w ( modSyn == 8 , i ) ) / i ;
w ( modSyn == 8 , i + 1 ) = ( Pm ( modSyn == 8 , i ) - ...
w ( modSyn == 8 , i + 1 ) = ( Pm ( modSyn == 8 , i ) - ...
@ -822,10 +1021,11 @@ for i=1:nlvl
RhsEd ( modSyn == 2 ) = 0 ;
RhsEd ( modSyn == 2 ) = 0 ;
RhsEq ( modSyn == 2 ) = eq1 ( modSyn == 2 , i + 1 ) ;
RhsEq ( modSyn == 2 ) = eq1 ( modSyn == 2 , i + 1 ) ;
end
end
% this part may be different from DT.% khuang 8 JUL
AG0 = cosp ( : , 2 ) .* d ( : , i + 1 ) ;
AG0 = cosp ( : , 2 ) .* d ( : , i + 1 ) ;
BG0 = sinp ( : , 2 ) .* d ( : , i + 1 ) ;
BG0 = sinp ( : , 2 ) .* d ( : , i + 1 ) ;
% here multi-convolution is utilized as sine function is
% approxiamted as a taylor series of delta.% khuang 8 JUL
if taylorN > = 2
if taylorN > = 2
AG0 = AG0 + cosp ( : , 3 ) .* sum ( d ( : , seq2 ( : , 1 ) + 1 ) .* d ( : , seq2 ( : , 2 ) + 1 ) , 2 ) ;
AG0 = AG0 + cosp ( : , 3 ) .* sum ( d ( : , seq2 ( : , 1 ) + 1 ) .* d ( : , seq2 ( : , 2 ) + 1 ) , 2 ) ;
BG0 = BG0 + sinp ( : , 3 ) .* sum ( d ( : , seq2 ( : , 1 ) + 1 ) .* d ( : , seq2 ( : , 2 ) + 1 ) , 2 ) ;
BG0 = BG0 + sinp ( : , 3 ) .* sum ( d ( : , seq2 ( : , 1 ) + 1 ) .* d ( : , seq2 ( : , 2 ) + 1 ) , 2 ) ;
@ -840,14 +1040,19 @@ for i=1:nlvl
BG0 = BG0 + sinp ( : , 5 ) .* sum ( d ( : , seq4 ( : , 1 ) + 1 ) .* d ( : , seq4 ( : , 2 ) + 1 ) .* d ( : , seq4 ( : , 3 ) + 1 ) .* d ( : , seq4 ( : , 4 ) + 1 ) , 2 ) ;
BG0 = BG0 + sinp ( : , 5 ) .* sum ( d ( : , seq4 ( : , 1 ) + 1 ) .* d ( : , seq4 ( : , 2 ) + 1 ) .* d ( : , seq4 ( : , 3 ) + 1 ) .* d ( : , seq4 ( : , 4 ) + 1 ) , 2 ) ;
end
end
Cd ( : , i + 1 ) = AG0 ;
% now Sd store high order terms of sin(dta) and Cd for cos(dta),
Sd ( : , i + 1 ) = BG0 ;
% the following sentence is for DT, i commentde it for HE. % khuang 8 JUL
%Sd(:,i+1) = 1/(i)*sum(repmat([i:-1:1],nSyn,1).*Cd(:,1:i).*d(:,i+1:-1:2),2);
%Cd(:,i+1) =-1/(i)*sum(repmat([i:-1:1],nSyn,1).*Sd(:,1:i).*d(:,i+1:-1:2),2);
% high order coefficients of cos(delta) and sin(delta).% khuang 8 JUL
Cd ( : , i + 1 ) = AG0 ;
Sd ( : , i + 1 ) = BG0 ;
VGdCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* VGd ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
VGdCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* VGd ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ; % Vd*cosdta% khuang 8 JUL
VGqCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* VGq ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
VGqCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* VGq ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ; % Vq*cosdta% khuang 8 JUL
VGdSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* VGd ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
VGdSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* VGd ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ; % Vd*sindta% khuang 8 JUL
VGqSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* VGq ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
VGqSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* VGq ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ; % Vq*sindta% khuang 8 JUL
JCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* JG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
JCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* JG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ; % similar, for currents% khuang 8 JUL
KCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* KG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
KCr = sum ( Cd ( : , seq2x ( : , 1 ) + 1 ) .* KG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
JSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* JG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
JSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* JG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
KSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* KG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
KSr = sum ( Sd ( : , seq2x ( : , 1 ) + 1 ) .* KG ( : , seq2x ( : , 2 ) + 1 ) , 2 ) ;
@ -856,10 +1061,11 @@ for i=1:nlvl
( MatsR ( : , 1 ) .* RhsEd + MatsR ( : , 2 ) .* RhsEq ) - ( Mats ( : , 1 ) .* ( JSr - KCr ) + Mats ( : , 2 ) .* ( JCr + KSr ) ) + ( Mats ( : , 1 ) .* IGdAdd + Mats ( : , 2 ) .* IGqAdd ) ;
( MatsR ( : , 1 ) .* RhsEd + MatsR ( : , 2 ) .* RhsEq ) - ( Mats ( : , 1 ) .* ( JSr - KCr ) + Mats ( : , 2 ) .* ( JCr + KSr ) ) + ( Mats ( : , 1 ) .* IGdAdd + Mats ( : , 2 ) .* IGqAdd ) ;
RHSIGxi = - ( MatsRs ( : , 3 ) .* ( - VGdSr - VGqCr ) + MatsRs ( : , 4 ) .* ( VGdCr - VGqSr ) ) + ...
RHSIGxi = - ( MatsRs ( : , 3 ) .* ( - VGdSr - VGqCr ) + MatsRs ( : , 4 ) .* ( VGdCr - VGqSr ) ) + ...
( MatsR ( : , 3 ) .* RhsEd + MatsR ( : , 4 ) .* RhsEq ) - ( Mats ( : , 3 ) .* ( JSr - KCr ) + Mats ( : , 4 ) .* ( JCr + KSr ) ) + ( Mats ( : , 3 ) .* IGdAdd + Mats ( : , 4 ) .* IGqAdd ) ;
( MatsR ( : , 3 ) .* RhsEd + MatsR ( : , 4 ) .* RhsEq ) - ( Mats ( : , 3 ) .* ( JSr - KCr ) + Mats ( : , 4 ) .* ( JCr + KSr ) ) + ( Mats ( : , 3 ) .* IGdAdd + Mats ( : , 4 ) .* IGqAdd ) ;
% current injections from generators IG.% khuang 8 JUL
RHSIGr = accumarray ( synIdx , RHSIGxr , [ nbus , 1 ] ) ;
RHSIGr = accumarray ( synIdx , RHSIGxr , [ nbus , 1 ] ) ;
RHSIGi = accumarray ( synIdx , RHSIGxi , [ nbus , 1 ] ) ;
RHSIGi = accumarray ( synIdx , RHSIGxi , [ nbus , 1 ] ) ;
end
end
% update exciter, 3 state variables.% khuang 8 JUL
if ~ isempty ( exc )
if ~ isempty ( exc )
Vavrm ( : , i + 1 ) = ( Vmag ( synIdx ( excIdx ) , i ) - Vavrm ( : , i ) ) ./ Tavrr / i ;
Vavrm ( : , i + 1 ) = ( Vmag ( synIdx ( excIdx ) , i ) - Vavrm ( : , i ) ) ./ Tavrr / i ;
Vavrr ( : , i + 1 ) = ( muavr0 .* ( 1 - Tavr1 ./ Tavr2 ) .* ( Vavrref ( : , i ) - Vavrm ( : , i ) ) - Vavrr ( : , i ) ) ./ Tavr2 / i ;
Vavrr ( : , i + 1 ) = ( muavr0 .* ( 1 - Tavr1 ./ Tavr2 ) .* ( Vavrref ( : , i ) - Vavrm ( : , i ) ) - Vavrr ( : , i ) ) ./ Tavr2 / i ;
@ -871,6 +1077,7 @@ for i=1:nlvl
Ef ( excIdx ( avrSt == 0 ) , i + 1 ) = Vavrf ( avrSt == 0 , i + 1 ) ;
Ef ( excIdx ( avrSt == 0 ) , i + 1 ) = Vavrf ( avrSt == 0 , i + 1 ) ;
end
end
% update agc, one state variables.% khuang 8 JUL
if ~ isempty ( agc )
if ~ isempty ( agc )
dpg ( : , i + 1 ) = - f ( : , i ) .* agcExt ( : , 4 ) / i ;
dpg ( : , i + 1 ) = - f ( : , i ) .* agcExt ( : , 4 ) / i ;
for islIdx = 1 : nIslands
for islIdx = 1 : nIslands
@ -882,6 +1089,7 @@ for i=1:nlvl
end
end
end % TODO: steady-state model
end % TODO: steady-state model
% update generator participation part from agc.% khuang 8 JUL
if ~ isempty ( syn ) %dynamic model (synchronous generators)
if ~ isempty ( syn ) %dynamic model (synchronous generators)
if ~ isempty ( tg )
if ~ isempty ( tg )
Tmech ( : , i + 1 ) = Tmech ( : , i + 1 ) + dpg ( syn ( tg ( : , 1 ) , 1 ) , i + 1 ) ./ numSynOnBus ( syn ( tg ( : , 1 ) , 1 ) ) ;
Tmech ( : , i + 1 ) = Tmech ( : , i + 1 ) + dpg ( syn ( tg ( : , 1 ) , 1 ) , i + 1 ) ./ numSynOnBus ( syn ( tg ( : , 1 ) , 1 ) ) ;
@ -889,7 +1097,7 @@ for i=1:nlvl
Pm ( : , i + 1 ) = Pm ( : , i + 1 ) + dpg ( syn ( : , 1 ) , i + 1 ) ./ numSynOnBus ( syn ( : , 1 ) ) ;
Pm ( : , i + 1 ) = Pm ( : , i + 1 ) + dpg ( syn ( : , 1 ) , i + 1 ) ./ numSynOnBus ( syn ( : , 1 ) ) ;
end
end
end
end
% update Turbine, 2 state variables.% khuang 8 JUL
if ~ isempty ( tg )
if ~ isempty ( tg )
tgovg ( : , i + 1 ) = ( - ( 1 - Ttg1 ./ Ttg2 ) .* w ( tgIdx , i ) ./ Rtg - tgovg ( : , i ) ) ./ Ttg2 / i ;
tgovg ( : , i + 1 ) = ( - ( 1 - Ttg1 ./ Ttg2 ) .* w ( tgIdx , i ) ./ Rtg - tgovg ( : , i ) ) ./ Ttg2 / i ;
tgovm ( : , i + 1 ) = tgovg ( : , i + 1 ) - Ttg1 ./ Ttg2 .* w ( tgIdx , i + 1 ) ./ Rtg + Tmech ( : , i + 1 ) ;
tgovm ( : , i + 1 ) = tgovg ( : , i + 1 ) - Ttg1 ./ Ttg2 .* w ( tgIdx , i + 1 ) ./ Rtg + Tmech ( : , i + 1 ) ;
@ -906,12 +1114,15 @@ for i=1:nlvl
RHS2 = - 0.5 * real ( sum ( V ( : , seq2R ( : , 1 ) + 1 ) .* conj ( V ( : , seq2R ( : , 2 ) + 1 ) ) , 2 ) ) ;
RHS2 = - 0.5 * real ( sum ( V ( : , seq2R ( : , 1 ) + 1 ) .* conj ( V ( : , seq2R ( : , 2 ) + 1 ) ) , 2 ) ) ;
RHS3 = sum ( - W ( : , seq2R ( : , 1 ) + 1 ) .* V ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ;
RHS3 = sum ( - W ( : , seq2R ( : , 1 ) + 1 ) .* V ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ;
if i == 1
if i == 1
RHS2 = RHS2 + 0.5 * VspSq2 ( : , 2 ) ;
RHS2 = RHS2 + 0.5 * VspSq2 ( : , 2 ) ;
end
end
compactRHS1 = RHS1 ( busType ~= 2 ) ;
compactRHS1 = RHS1 ( busType ~= 2 ) ;
compactRHS1 = compactRHS1 + Y ( busType ~= 2 , isw ) * real ( V ( isw , i + 1 ) ) ;
compactRHS1 = compactRHS1 + Y ( busType ~= 2 , isw ) * real ( V ( isw , i + 1 ) ) ;
% combine all current injection involing Motor, zip load, and
% Generators.% khuang 8 JUL
RHS = [ real ( compactRHS1 ) + RHSILr ( busType ~= 2 ) + RHSIiLr ( busType ~= 2 ) - RHSIGr ( busType ~= 2 ) ; ...
RHS = [ real ( compactRHS1 ) + RHSILr ( busType ~= 2 ) + RHSIiLr ( busType ~= 2 ) - RHSIGr ( busType ~= 2 ) ; ...
imag ( compactRHS1 ) + RHSILi ( busType ~= 2 ) + RHSIiLi ( busType ~= 2 ) - RHSIGi ( busType ~= 2 ) ; ...
imag ( compactRHS1 ) + RHSILi ( busType ~= 2 ) + RHSIiLi ( busType ~= 2 ) - RHSIGi ( busType ~= 2 ) ; ...
RHS2 ( ipv ) ; ...
RHS2 ( ipv ) ; ...
@ -919,7 +1130,9 @@ for i=1:nlvl
imag ( RHS3 ( busType ~= 2 ) ) ; ...
imag ( RHS3 ( busType ~= 2 ) ) ; ...
zeros ( sum ( freqKeptTagxRef ) , 1 ) ; ...
zeros ( sum ( freqKeptTagxRef ) , 1 ) ; ...
zeros ( size ( idxNonSwD , 1 ) , 1 ) ] ;
zeros ( size ( idxNonSwD , 1 ) , 1 ) ] ;
% solve AE, notice that every time we need to solve Ax(k) =b(k), which
% means that A in invariant for every order. so we only need to rebulid
% b every iteration.% khuang 8 JUL
if useLU
if useLU
if IS_OCTAVE
if IS_OCTAVE
x = real ( MxQ * MxQx * ( MxU \ ( MxL \ ( MxP * RHS ) ) ) ) ;
x = real ( MxQ * MxQx * ( MxU \ ( MxL \ ( MxP * RHS ) ) ) ) ;
@ -930,6 +1143,7 @@ for i=1:nlvl
x = real ( LHS_mat \ RHS ) ;
x = real ( LHS_mat \ RHS ) ;
end
end
% x= [V;W;Q_pv;f]
xC = real ( V ( : , i + 1 ) ) ;
xC = real ( V ( : , i + 1 ) ) ;
xD = imag ( V ( : , i + 1 ) ) ;
xD = imag ( V ( : , i + 1 ) ) ;
xC ( idxNonSw ) = x ( 1 : ( npq + npv ) ) ;
xC ( idxNonSw ) = x ( 1 : ( npq + npv ) ) ;
@ -942,6 +1156,7 @@ for i=1:nlvl
Vmag ( : , i + 1 ) = ( sum ( V ( : , seq2 ( : , 1 ) + 1 ) .* conj ( V ( : , seq2 ( : , 2 ) + 1 ) ) , 2 ) - sum ( Vmag ( : , seq2R ( : , 1 ) + 1 ) .* Vmag ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ) ./ Vmag ( : , 1 ) / 2 ; % Calculate voltage magnitude
Vmag ( : , i + 1 ) = ( sum ( V ( : , seq2 ( : , 1 ) + 1 ) .* conj ( V ( : , seq2 ( : , 2 ) + 1 ) ) , 2 ) - sum ( Vmag ( : , seq2R ( : , 1 ) + 1 ) .* Vmag ( : , seq2R ( : , 2 ) + 1 ) , 2 ) ) ./ Vmag ( : , 1 ) / 2 ; % Calculate voltage magnitude
% now update the Algebric variables for motors:IL,IR,VM.% khuang 8 JUL
if ~ isempty ( ind )
if ~ isempty ( ind )
% for j=1:nInd
% for j=1:nInd
% tempIL=squeeze(LHS_MatInd_Shr(j,:,:))*[real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]+rhsBus(:,j);
% tempIL=squeeze(LHS_MatInd_Shr(j,:,:))*[real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]+rhsBus(:,j);
@ -959,11 +1174,13 @@ for i=1:nlvl
Vm ( : , i + 1 ) = V ( indIdx , i + 1 ) - IL ( : , i + 1 ) .* Z1 ;
Vm ( : , i + 1 ) = V ( indIdx , i + 1 ) - IL ( : , i + 1 ) .* Z1 ;
end
end
% now update the Algebric variables for ZIP loads.% khuang 8 JUL
if ~ isempty ( zip )
if ~ isempty ( zip )
IiL ( : , i + 1 ) = ( LHS_MatZip ( : , 1 ) + 1 j * LHS_MatZip ( : , 3 ) ) .* real ( V ( zipIdx , i + 1 ) ) + ( LHS_MatZip ( : , 2 ) + 1 j * LHS_MatZip ( : , 4 ) ) .* imag ( V ( zipIdx , i + 1 ) ) + ( RHSILr_full + 1 j * RHSILi_full ) ;
IiL ( : , i + 1 ) = ( LHS_MatZip ( : , 1 ) + 1 j * LHS_MatZip ( : , 3 ) ) .* real ( V ( zipIdx , i + 1 ) ) + ( LHS_MatZip ( : , 2 ) + 1 j * LHS_MatZip ( : , 4 ) ) .* imag ( V ( zipIdx , i + 1 ) ) + ( RHSILr_full + 1 j * RHSILi_full ) ;
BiL ( : , i + 1 ) = Mat_BZip ( : , 1 ) .* real ( V ( zipIdx , i + 1 ) ) + Mat_BZip ( : , 2 ) .* imag ( V ( zipIdx , i + 1 ) ) + RHS_BZip ;
BiL ( : , i + 1 ) = Mat_BZip ( : , 1 ) .* real ( V ( zipIdx , i + 1 ) ) + Mat_BZip ( : , 2 ) .* imag ( V ( zipIdx , i + 1 ) ) + RHS_BZip ;
end
end
% now update the Algebric variables for Generators: Vd,Vq,Id,Iq.% khuang 8 JUL
if ~ isempty ( syn )
if ~ isempty ( syn )
JG ( : , i + 1 ) = - MatsRs ( : , 1 ) .* real ( V ( synIdx , i + 1 ) ) - MatsRs ( : , 2 ) .* imag ( V ( synIdx , i + 1 ) ) + RHSIGxr ;
JG ( : , i + 1 ) = - MatsRs ( : , 1 ) .* real ( V ( synIdx , i + 1 ) ) - MatsRs ( : , 2 ) .* imag ( V ( synIdx , i + 1 ) ) + RHSIGxr ;
KG ( : , i + 1 ) = - MatsRs ( : , 3 ) .* real ( V ( synIdx , i + 1 ) ) - MatsRs ( : , 4 ) .* imag ( V ( synIdx , i + 1 ) ) + RHSIGxi ;
KG ( : , i + 1 ) = - MatsRs ( : , 3 ) .* real ( V ( synIdx , i + 1 ) ) - MatsRs ( : , 4 ) .* imag ( V ( synIdx , i + 1 ) ) + RHSIGxi ;
@ -976,6 +1193,7 @@ for i=1:nlvl
end
end
end
end
% Output value: coefficients for every order.% khuang 8 JUL
Q = real ( Q ) ;
Q = real ( Q ) ;
s = real ( s ) ;
s = real ( s ) ;
d = real ( d ) ;
d = real ( d ) ;