diff --git a/internal/Cal_locl_er.m b/internal/Cal_locl_er.m new file mode 100644 index 0000000..a2e7ee4 --- /dev/null +++ b/internal/Cal_locl_er.m @@ -0,0 +1,69 @@ +function [LTE,LTE2,LTE3] = Cal_locl_er(x_ori,k,h) +% Calculation of local error +% +% FUNCTION Cal_locl_er +% +% Author: Kaiyang Huang +% +% Copyright (C) 2023, UChicago Argonne, LLC. All rights reserved. +% +% OPEN SOURCE LICENSE +% +% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +% +% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +% +% +% ****************************************************************************************************** +% DISCLAIMER +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% *************************************************************************************************** +% +% INPUT + +% x_ori - Initial system state +% xNew - Updated system state (algebraic variables to be solved) +% SysDataNew - (optional) updated system data +% +% OUTPUT +% Vx, Wx, Qx, fx - Last HE coefficients of algebraic variables +% xx - Solved updated system state +% finalAlpha - The ending length of this segment of simulation +% alphas - Record of alphas +% diffRec - A list of errors +% + + x = x_ori(:,k-3:k); + Xh = x.*h.^(k-4:k-1); + x_new = polyvalVec(x_ori,h); + eta = 0.001; + infcof = max(abs(Xh(:,end))./(abs(x_new)+eta)); + trc = infcof^(1/k); + LTE = infcof/(1-trc); + + LTE3 = max(abs(Xh(:,end-1)./(x_new+eta))); + + % relative error used to change order + e1 = abs(Xh(:,end-1)./Xh(:,end)); + e2 = abs(Xh(:,end-2)./Xh(:,end)); + e3 = abs(Xh(:,end-3)./Xh(:,end-1)); + % delete nan induced by zero/zero + f1 = find(Xh(:,end)==0); + f2 = find(Xh(:,end-1)==0); + f2 = f2(:); + e1(f1) = 0; + e2(f1) = 0; + e3(f2) = 0; + LTE2 = infcof/min([norm(e1,Inf), sqrt(norm(e2,Inf)), sqrt(norm(e3,Inf))]); + +end + diff --git a/internal/Copy_of_hemMachinePFSalientcontinueDyn.m b/internal/Copy_of_hemMachinePFSalientcontinueDyn.m new file mode 100644 index 0000000..305db70 --- /dev/null +++ b/internal/Copy_of_hemMachinePFSalientcontinueDyn.m @@ -0,0 +1,1221 @@ +function [V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrf,Vavrref,tgovg,tgovm,Tmech,f,dpg,qplt,vg]=... + hemMachinePFSalientcontinueDyn(SimData,SysData,SysPara,x0) +% Core HE algorithm for solving DAEs (dynamic simulation) +% +% FUNCTION hemMachinePFSalientcontinueDyn +% +% Author: Rui Yao +% +% Copyright (C) 2021, UChicago Argonne, LLC. All rights reserved. +% +% OPEN SOURCE LICENSE +% +% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +% +% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +% +% +% ****************************************************************************************************** +% DISCLAIMER +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% *************************************************************************************************** +% +% INPUT +% SimData - Simulation parameters +% SysData - System data for simulation +% SysPara - Parameters representing the events happening in the system +% x0 - Initial system state +% +% OUTPUT - (will be consolidated in a future version) +% +% TODO % Modify the output arguments +% + +global IS_OCTAVE; + + + +% import system data +[bus,sw,pv,pq,shunt,line,ind,zip,syn,exc,tg,agc,cac,cluster]=unfoldSysData(SysData); +% nbus:total number of buses +nbus=size(bus,1); +nline=size(line,1); + +%determine islanding +if isfield(SysPara,'nIslands')&&isfield(SysPara,'islands')&&isfield(SysPara,'refs') + nIslands=SysPara.nIslands;islands=SysPara.islands;refs=SysPara.refs; +else + [nIslands,islands,refs]=searchIslands(bus(:,1),line(:,1:2)); +end + +% improt initial condition +[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 +[~,~,~,nlvl,taylorN,~,~,~,~]=unfoldSimData(SimData); +% import system parameters +[pqIncr,pvIncr,Rind0,Rind1,Reind0,Reind1,Rzip0,Rzip1,Ytr0,Ytr1,Ysh0,Ysh1,VspSq2,~,~,~,~,Tmech1,Varref1,Ef1,Pm1,Eq11]=unfoldSysPara(SysPara); +% + +% It seems that PQ incremental is given for every pq bus. +% need to figure how to utilize them +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); +% why additional PQ is given +if size(pqIncr,2)>=4 + Pls(pq(:,1),2)=pqIncr(:,3); + Qls(pq(:,1),2)=pqIncr(:,4); +end + +% formatting Ymatrix, here the default value of fault is empty +if isempty(Ytr0) + [Y,Ytr0,Ysh,ytrfr,ytrto,yshfr,yshto]=getYMatrix(nbus,line); +end + + +% reshape the pv, pq shunt and swing buses if they are empty +busType=zeros(nbus,1); +if isempty(pv) + pv=zeros(0,6); +end +if isempty(pq) + pq=zeros(0,6); +end +if isempty(shunt) + shunt=zeros(0,7); +end +if isempty(sw) + sw=zeros(0,13); +end + +% label pv and swing buses +% 1: PV bus, 0: PQ bus +busType(pv(:,1))=1; +busType(sw(:,1))=2; +% zip(busType(zip(:,1))~=0,10)=0; + +% index of swing bus (isw), pv bus(ipv), and pq bus(ipq) +% is given: isw, ipv, and ipq +% Additionally, number of pv and pq buses are given: +%npv, npq respectively +isw=find(busType==2); +ipv=find(busType==1); +ipq=find(busType==0); +npq=size(ipq,1); +npv=size(ipv,1); + +% shunt capacitator is initialized as yShunt which is a complex number. +% for every bus +yShunt=zeros(nbus,1); +yShunt(shunt(:,1))=shunt(:,5)+1j*shunt(:,6); + +% check if zip load exists in the system +% and initialized zip load +if ~isempty(zip)%zipMode=0 + Ysh0=Ysh0+accumarray(zip(:,1),Rzip0.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]); + Ysh1=Ysh1+accumarray(zip(:,1),Rzip1.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]); +end + +% now zip load + shunt +Ysh0=Ysh0+yShunt; +% Y=Y+sparse(1:nbus,1:nbus,yShunt,nbus,nbus); + +% now zip load + shunt+ network Y matrix +Y=Ytr0+sparse(1:nbus,1:nbus,Ysh0,nbus,nbus); + +%initialize P and Q for every bus +pVec=zeros(nbus,1); +qVec=zeros(nbus,1); +% vSp=zeros(nbus,1); + +% need to figure out the meaning of index 1, 4,5 +% based Kaiyang's understanding, 1 is load side and 4&5 are generators' +% output +pVec(pv(:,1))=pVec(pv(:,1))+pv(:,4); +pVec(pq(:,1))=pVec(pq(:,1))-pq(:,4); +qVec(pq(:,1))=qVec(pq(:,1))-pq(:,5); +% account the zip load, i.e dynamic load +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]); + qVec=qVec-accumarray(zip(:,1),Rzip0.*zip(:,10).*zip(:,12),[nbus,1]); +end +% qVec(ipv)=qVec(ipv)+Q0(ipv); +% vSp(ipv)=pv(:,5); + +% so far, initialization of PQ for every bus and Y matrix is ready + + +% initiliza voltage V and W = 1/V for every bus +V=zeros(nbus,nlvl+1); +V(:,1)=V0; +W=zeros(nbus,nlvl+1); +W(:,1)=1./V0; + + +% initiliza magnitude of voltage V for every bus +Vmag=zeros(nbus,nlvl+1); +Vmag(:,1)=abs(V0); + +% Power is initilized as we already cooked pVec and qVec +P=zeros(nbus,nlvl+1); +P(:,1)=pVec; +% 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 +Q=zeros(nbus,nlvl+1); +Qxtra=zeros(size(Q)); +Q(:,1)=Q0; +Qxtra(:,1)=qVec; + +% Also, the meaning of Pls and Qls need to be verified +P(:,2:(size(Pls,2)+1))=-Pls; +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. +if ~isempty(zip) + 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]); +end +% Qxtra(busType~=0,2:end)=Q(busType~=0,2:end); +% Q(busType~=0,2:end)=0; + + +% seperate real and image part of voltages and their inverse +% here V = C+1i*D +% and W = 1./V = E + 1i*F +C0=real(V(:,1)); +D0=imag(V(:,1)); +E0=real(W(:,1)); +F0=imag(W(:,1)); + +% Construct sparse matrix individually for C,D,E,F,P,Q +% Notice that Q = Q(:,1)+Qxtra(:,1) which is different from P +C0M=sparse(1:nbus,1:nbus,C0,nbus,nbus); +D0M=sparse(1:nbus,1:nbus,D0,nbus,nbus); +E0M=sparse(1:nbus,1:nbus,E0,nbus,nbus); +F0M=sparse(1:nbus,1:nbus,F0,nbus,nbus); +P0M=sparse(1:nbus,1:nbus,P(:,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 +G=real(Y); +B=imag(Y); +% so Y = G + 1i*B + + +%-------------------------------------------------------------- +% 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 +freqKeptTag=zeros(nbus,1); +frefs=refs; +fswTag=zeros(nbus,1); +fsynTag=zeros(nbus,1); +fswTag(isw)=1; +fswTagxD=fswTag; +fsynTag(syn(:,1))=1; +for isl=1:nIslands + if isempty(find(fswTag(islands==isl)==1, 1)) + if isempty(find(fsynTag(islands==isl)==1, 1)) + freqTypeTag(isl)=2; + busesInIsland=find(islands==isl); + [~,imin]=min(abs(D0(busesInIsland))); + frefs(isl)=busesInIsland(imin(1)); + fswTagxD(frefs(isl))=1; + freqKeptTag(busesInIsland)=1; + else + freqTypeTag(isl)=1; + end + end +end +freqKeptTagxRef=freqKeptTag; +freqKeptTagxRef(frefs)=0; +nFreqKept=sum(freqKeptTag); +%----------------------------------------------------------- + + + + +%------------------------------------------------------------------ +% this part is for initialling inductor +if ~isempty(ind) % check if there is any inductor + nInd=size(ind,1); % determine the number of inductors + indIdx=ind(:,1); % store the index of inductors among all buses + + s=zeros(nInd,nlvl+1); % slip + s(:,1)=s0; % initialize slip + IL=zeros(nInd,nlvl+1); % | + IR=zeros(nInd,nlvl+1); % | + Vm=zeros(nInd,nlvl+1); % initialization finished 0 value + + + %-----------------parameters of inductors--------- + %-----------------START---------------- + R1=ind(:,7); + X1=ind(:,8); + Z1=ind(:,7)+1j*ind(:,8); + Ze=1j*ind(:,13); + R2=ind(:,9); + X2=ind(:,10); + T0=ind(:,15)+ind(:,16)+ind(:,17); + T1=-ind(:,16)-2*ind(:,17); + T2=ind(:,17); + Hm=ind(:,14); + %-----------------parameters of inductors--------- + %-----------------END---------------- + + + Rm=zeros(nInd,1); + + Am=sparse(indIdx,(1:nInd)',ones(1,nInd),nbus,nInd); + + % first order value of induction motor IL,VM,IR + IL(:,1)=V0(indIdx)./(Z1+Ze.*(R2+1j*X2.*s0)./(R2.*Reind0+(1j*X2.*Reind0+Ze).*s0)); + Vm(:,1)=V0(indIdx)-IL(:,1).*Z1; + IR(:,1)=Vm(:,1).*s0./(R2+1j*X2.*s0); + + J0=real(IR(:,1)); + K0=imag(IR(:,1)); + JL0=real(IL(:,1)); + KL0=imag(IL(:,1)); + + % prepare the algebric matrix + Yeind0=Reind0./Ze; + Yeind1=Reind1./Ze; + Ye1ind0=Reind0.*Z1./Ze; + Ye1ind1=Reind1.*Z1./Ze; + Ge=real(Yeind0); + Be=imag(Yeind0); + kg1e=real(Ye1ind0); + kb1e=imag(Ye1ind0); + Ge1=real(Yeind1); + Be1=imag(Yeind1); + kg1e1=real(Ye1ind1); + kb1e1=imag(Ye1ind1); + + % LHS_MatInd_Shr_sqz=zeros(nInd,4); + % RHS_C_Shr_sqz=zeros(nInd,8); + % LHS_MatInd_Shr2_sqz=zeros(nInd,8); + % + % LHS_MatInd_Shr=zeros(nInd,2,2); + % RHS_C_Shr=cell(nInd,1); + % LHS_MatInd_Shr2=cell(nInd,1); % A^-1B + % LHS_MatInd_Shr3=cell(nInd,1); % A^-1 + % + % for i=1:nInd + % LHS_MatInd=[R2(i),-X2(i)*s0(i),R1(i)*s0(i),-X1(i)*s0(i),-s0(i),0;... + % X2(i)*s0(i), R2(i),X1(i)*s0(i), R1(i)*s0(i),0,-s0(i);... + % -1,0,1+kg1e(i),-kb1e(i),-Ge(i), Be(i);... + % 0,-1,kb1e(i), 1+kg1e(i),-Be(i),-Ge(i);]; + % temp0=LHS_MatInd([3,4],[1,2])\eye(2); % A^-1 + % LHS_MatInd_Shr2{i}=temp0*LHS_MatInd([3,4],[3,4,5,6]); % A^-1B + % LHS_MatInd_Shr3{i}=temp0; % A^-1 + % temp1=LHS_MatInd([1,2],[1,2])/LHS_MatInd([3,4],[1,2]); % CA^-1 + % temp2=LHS_MatInd([1,2],[3,4,5,6])-temp1*LHS_MatInd([3,4],[3,4,5,6]); % L=D-CA^-1B + % LHS_MatInd_Shr(i,:,:)=-temp2(:,[1,2])\temp2(:,[3,4]); % -R\S + % RHS_C_Shr{i}=temp2(:,[1,2])\[eye(2),-temp1]; % R\[I,-CA^-1] + % + % LHS_MatInd_Shr_sqz(i,:)=reshape(LHS_MatInd_Shr(i,:,:),[1,4]); + % RHS_C_Shr_sqz(i,:)=reshape(RHS_C_Shr{i},[1,8]); + % LHS_MatInd_Shr2_sqz(i,:)=reshape(LHS_MatInd_Shr2{i},[1,8]); + % end + % LHS_MatInd_Bus=zeros(nbus,2,2); % \sum{-R\S} by buses + % LHS_MatInd_Bus(:,1,1)=accumarray(indIdx,LHS_MatInd_Shr(:,1,1),[nbus,1]); + % LHS_MatInd_Bus(:,1,2)=accumarray(indIdx,LHS_MatInd_Shr(:,1,2),[nbus,1]); + % LHS_MatInd_Bus(:,2,1)=accumarray(indIdx,LHS_MatInd_Shr(:,2,1),[nbus,1]); + % LHS_MatInd_Bus(:,2,2)=accumarray(indIdx,LHS_MatInd_Shr(:,2,2),[nbus,1]); + + MInd0=zeros(nInd,1); + MInd1=ones(nInd,1); + LHS_MatInd_sqz=[R2,X2.*s0,-MInd1,MInd0,... + -X2.*s0,R2,MInd0,-MInd1,... + R1.*s0,X1.*s0,MInd1+kg1e,kb1e,... + -X1.*s0,R1.*s0,-kb1e,MInd1+kg1e,... + -s0,MInd0,-Ge,-Be,... + MInd0,-s0,Be,-Ge]; % 4*6 matrix [C,D;A,B] + LHS_MatInd_idx=reshape((1:24)',[4,6]); + temp0inv_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[1,2]),1,[])); + temp0inv_sqz_det=temp0inv_sqz(:,1).*temp0inv_sqz(:,4)-temp0inv_sqz(:,2).*temp0inv_sqz(:,3); + temp0_sqz=[temp0inv_sqz(:,4),-temp0inv_sqz(:,2),-temp0inv_sqz(:,3),temp0inv_sqz(:,1)]./repmat(temp0inv_sqz_det,[1,4]);% A^-1 + indB_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[3,4,5,6]),1,[])); + LHS_MatInd_Shr2_sqz=[temp0_sqz(:,1).*indB_sqz(:,1)+temp0_sqz(:,3).*indB_sqz(:,2),temp0_sqz(:,2).*indB_sqz(:,1)+temp0_sqz(:,4).*indB_sqz(:,2),... + temp0_sqz(:,1).*indB_sqz(:,3)+temp0_sqz(:,3).*indB_sqz(:,4),temp0_sqz(:,2).*indB_sqz(:,3)+temp0_sqz(:,4).*indB_sqz(:,4),... + temp0_sqz(:,1).*indB_sqz(:,5)+temp0_sqz(:,3).*indB_sqz(:,6),temp0_sqz(:,2).*indB_sqz(:,5)+temp0_sqz(:,4).*indB_sqz(:,6),... + temp0_sqz(:,1).*indB_sqz(:,7)+temp0_sqz(:,3).*indB_sqz(:,8),temp0_sqz(:,2).*indB_sqz(:,7)+temp0_sqz(:,4).*indB_sqz(:,8)];% A^-1B + indC_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[1,2]),1,[])); + temp1_sqz=[indC_sqz(:,1).*temp0_sqz(:,1)+indC_sqz(:,3).*temp0_sqz(:,2),indC_sqz(:,2).*temp0_sqz(:,1)+indC_sqz(:,4).*temp0_sqz(:,2),... + indC_sqz(:,1).*temp0_sqz(:,3)+indC_sqz(:,3).*temp0_sqz(:,4),indC_sqz(:,2).*temp0_sqz(:,3)+indC_sqz(:,4).*temp0_sqz(:,4)];% CA^-1 + temp2_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[3,4,5,6]),1,[]))-... + [temp1_sqz(:,1).*indB_sqz(:,1)+temp1_sqz(:,3).*indB_sqz(:,2),temp1_sqz(:,2).*indB_sqz(:,1)+temp1_sqz(:,4).*indB_sqz(:,2),... + temp1_sqz(:,1).*indB_sqz(:,3)+temp1_sqz(:,3).*indB_sqz(:,4),temp1_sqz(:,2).*indB_sqz(:,3)+temp1_sqz(:,4).*indB_sqz(:,4),... + temp1_sqz(:,1).*indB_sqz(:,5)+temp1_sqz(:,3).*indB_sqz(:,6),temp1_sqz(:,2).*indB_sqz(:,5)+temp1_sqz(:,4).*indB_sqz(:,6),... + temp1_sqz(:,1).*indB_sqz(:,7)+temp1_sqz(:,3).*indB_sqz(:,8),temp1_sqz(:,2).*indB_sqz(:,7)+temp1_sqz(:,4).*indB_sqz(:,8)];% L=D-CA^-1B=[R,S] + temp2_c12_sqz=temp2_sqz(:,1:4); + temp2_c34_sqz=temp2_sqz(:,5:8); + temp2_c12_sqz_det=temp2_c12_sqz(:,1).*temp2_c12_sqz(:,4)-temp2_c12_sqz(:,2).*temp2_c12_sqz(:,3); + temp2_c12_inv_sqz=[temp2_c12_sqz(:,4),-temp2_c12_sqz(:,2),-temp2_c12_sqz(:,3),temp2_c12_sqz(:,1)]./repmat(temp2_c12_sqz_det,[1,4]); + LHS_MatInd_Shr_sqz=-[temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,2),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,2),... + temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,4),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,4)];% -R\S + 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(:,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 + 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(:,2)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,2),[nbus,1]); + LHS_MatInd_Bus_sqz(:,3)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,3),[nbus,1]); + LHS_MatInd_Bus_sqz(:,4)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,4),[nbus,1]); +else + s=zeros(0,nlvl+1); +end +% Initialization of inductors is finished +%------------------------------------------------------------------ + + +%------------------------------Initialization of ZIP load--------- +if ~isempty(zip) + nZip=size(zip,1); + zipIdx=zip(:,1); + IiL=zeros(nZip,nlvl+1); + BiL=zeros(nZip,nlvl+1); + + % prepare the necessary matrix by blocks + Bi0=abs(V0(zipIdx)); + JI=zip(:,6); + KI=-zip(:,9); + % current + Ii0L=Rzip0.*(JI+1j*KI).*V0(zipIdx)./Bi0; + Ji0L=real(Ii0L); + Ki0L=imag(Ii0L); + + IiL(:,1)=Ii0L; + BiL(:,1)=Bi0; + % voltage + Ci0=real(V0(zipIdx)); + Di0=imag(V0(zipIdx)); + + LHS_MatZip=[Rzip0.*JI./Bi0-Ci0.*Ji0L./Bi0./Bi0,-Rzip0.*KI./Bi0-Di0.*Ji0L./Bi0./Bi0,... + Rzip0.*KI./Bi0-Ci0.*Ki0L./Bi0./Bi0,Rzip0.*JI./Bi0-Di0.*Ki0L./Bi0./Bi0]; + Mat_BZip=[Ci0./Bi0,Di0./Bi0]; +else + IiL=zeros(0,nlvl+1); +end +%------------------------------Initialization of ZIP load------------------ +%------------------------------Initialization of ZIP load is finished---------------- + + +%------------------------------Initialization of GEN------------------ +%------------------------------Start------------------------ +nSyn=size(syn,1); +if ~isempty(syn) + synIdx =syn(:,1);% index number of Generators + wgb =syn(:,4);% maybe the base value + modSyn =syn(:,5);% the order of generator models + Xgl =syn(:,6); + Rga =syn(:,7); + Xgd =syn(:,8); + Xgd1 =syn(:,9); + Xgd2 =syn(:,10); + Tgd1 =syn(:,11); + Tgd2 =syn(:,12); + Xgq =syn(:,13); + Xgq1 =syn(:,14); + Xgq2 =syn(:,15); + Tgq1 =syn(:,16); + Tgq2 =syn(:,17); + Mg =syn(:,18); + Dg =syn(:,19); + TgAA =syn(:,24); + gammad =Tgd2./Tgd1.*Xgd2./Xgd1.*(Xgd-Xgd1); + gammaq =Tgq2./Tgq1.*Xgq2./Xgq1.*(Xgq-Xgq1); + + d=zeros(nSyn,nlvl+1); % delta + w=zeros(nSyn,nlvl+1); % omega + eq1=zeros(nSyn,nlvl+1); %eq' + eq2=zeros(nSyn,nlvl+1); %eq'' + ed1=zeros(nSyn,nlvl+1); %ed' + ed2=zeros(nSyn,nlvl+1); %ed'' + psiq=zeros(nSyn,nlvl+1); % not sure, only in 8th order model + psid=zeros(nSyn,nlvl+1); % not sure, only in 8th order model + JG=zeros(nSyn,nlvl+1); + KG=zeros(nSyn,nlvl+1); + IGq=zeros(nSyn,nlvl+1); + IGd=zeros(nSyn,nlvl+1); + VGq=zeros(nSyn,nlvl+1); + VGd=zeros(nSyn,nlvl+1); + Cd=zeros(nSyn,nlvl+1); + Sd=zeros(nSyn,nlvl+1); + Ef=zeros(nSyn,nlvl+1); + Pm=zeros(nSyn,nlvl+1); + + cosd=cos(d0); + sind=sin(d0); + CG0=C0(synIdx); + DG0=D0(synIdx); + % the first value is given here, notice all are 8th order model + d(:,1)=d0; + w(:,1)=w0; + eq1(:,1)=eq10; + eq2(:,1)=eq20; + ed1(:,1)=ed10; + ed2(:,1)=ed20; + psiq(:,1)=psiq0; + psid(:,1)=psid0; + + % transform between grid side and dq side + + VGd(:,1)=sind.*CG0-cosd.*DG0; + VGq(:,1)=cosd.*CG0+sind.*DG0; + % now they are under dq side + + Cd(:,1)=cosd; % first order of cos(delta) + Sd(:,1)=sind; % first order of sin(delta) + Ef(:,1)=Ef0; + Pm(:,1)=Pm0; + + %check if controller exists + if ~isempty(Ef1) + Ef(:,2)=Ef1; + end + if ~isempty(Eq11) + eq1(:,2)=Eq11; + end + if ~isempty(Pm1) + Pm(:,2)=Pm1; + end + + % notice that here truncated taylor is applied + % and this is the key differnet from Dt rule + % Here only at most 5 th order taylor series are considered for sin + % and cos function + [cosp,sinp,taylorN]=getTaylorPolynomials(d0,taylorN); % taylorN may be truncated + + Mats=zeros(nSyn,4); + MatsR=zeros(nSyn,4); + MatsRs=zeros(nSyn,4); + + % count the number for different kinds models + % ex: modelTag = [ 0 0 0 0 0 10 0 0].' + % ex: there are 10 gens using 6th order model + modelTag=accumarray(modSyn,ones(nSyn,1),[8,1]); + + % determine the order of the model + % Do we really need for loop? + % the answer is yes since different gen may use different + % order model + for i=1:nSyn + % 8th order, no need to change + if modSyn(i)==8 + IGd(i,1)=(eq20(i)-psid0(i))/Xgd2(i); + IGq(i,1)=(-ed20(i)-psiq0(i))/Xgq2(i); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + % 6th order + elseif modSyn(i)==6 + % algebric equation to solve Id, Iq + 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)); + % transform matrix (inverse version) + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + % Here matrix is the inverse matrix, to understand this + % We have A*Ixy+B*Vxy = f => MatsR = A^-1, MatsRs = A^-1*B = MatsRs*B + % so Ixy = MatsR*f-MatsRs*Vxy, which is used later to + % eliminate Ixy when disturbance happens + 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)); + 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)]; + % 5th order + 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)); + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(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)); + 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)]; + % 4th order + 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)); + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq1(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq1(i)+sind(i)*Rga(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),... + MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)]; + % 3rd order + 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)); + IGq(i,1)=(-(-VGd(i,1))*Xgd1(i)+(eq10(i)-VGq(i,1))*Rga(i))/(Rga(i)*Rga(i)+Xgd1(i)*Xgq(i)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq(i)+sind(i)*Rga(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),... + MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)]; + % classical model + 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)); + IGq(i,1)=(-(-VGd(i,1))*Xgd(i)+(Ef0(i)-VGq(i,1))*Rga(i))/(Rga(i)*Rga(i)+Xgd(i)*Xgq(i)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd(i),-cosd(i)*Xgq(i)+sind(i)*Rga(i)]/... + (Rga(i)*Rga(i)+Xgd(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),... + MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)]; + end + end + % not sure how to use them now, but they are zeroth order of Ix and Iy + JG(:,1)= sind.*IGd(:,1)+cosd.*IGq(:,1); + KG(:,1)=-cosd.*IGd(:,1)+sind.*IGq(:,1); + + % put previous matrix in a right place in all buses instead of only + % generator buses + 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)]; +else + d=zeros(0,nlvl+1); + w=zeros(0,nlvl+1); + eq1=zeros(0,nlvl+1); + eq2=zeros(0,nlvl+1); + ed1=zeros(0,nlvl+1); + ed2=zeros(0,nlvl+1); + psiq=zeros(0,nlvl+1); + psid=zeros(0,nlvl+1); + JG=zeros(0,nlvl+1); + KG=zeros(0,nlvl+1); + IGq=zeros(0,nlvl+1); + IGd=zeros(0,nlvl+1); + VGq=zeros(0,nlvl+1); + VGd=zeros(0,nlvl+1); + Cd=zeros(0,nlvl+1); + Sd=zeros(0,nlvl+1); + Ef=zeros(0,nlvl+1); + Pm=zeros(0,nlvl+1); +end +%------------------------------Initialization of GEN------------------ +%------------------------------EnD------------------------------------ + + + +%------------------------------Initialization of Exciter------------------ +%------------------------------START------------------------------------ +if ~isempty(exc) + nExc =size(exc,1); + % All Type 3 AVR, a 3rd order controller + % for Type 3 AVR, avr0(:,1:3) are Vavrm, Vavrr, Vavrf, + % and avr0(:,4) is reference Vref (input for secondary voltage control). + excIdx = exc(:,1); + VavrMax = exc(:,3); + VavrMin = exc(:,4); + muavr0 = exc(:,5); + Tavr1 = exc(:,7); + Tavr2 = exc(:,6); + vavrf0 = exc(:,8); + Vavr0 = exc(:,9); + Tavre = exc(:,10); + Tavrr = exc(:,11); + + %here I need to check why Vavrref is time varing instead of constant + % memory is given to state variables of EXC + Vavrm = zeros(nExc,nlvl+1); + Vavrr = zeros(nExc,nlvl+1); + Vavrf = zeros(nExc,nlvl+1); + Vavrref= zeros(nExc,nlvl+1); + + % zeroth order value is given + Vavrm(:,1)=real(Vavrm0); + Vavrr(:,1)=real(Vavrr0); + Vavrf(:,1)=real(Vavrf0); + Vavrref(:,1)=real(Vavrref0); + + % here Varref1 is given with syspara + if ~isempty(Varref1) + Vavrref(:,2)=Varref1; + end + + % non-windup limiter, check the limit + tavrMaxDiff=Vavrf(:,1)-VavrMax; + tavrMinDiff=Vavrf(:,1)-VavrMin; + + % label values in different interval + avrSt=zeros(nExc,1); + avrSt(tavrMaxDiff>0)=1; + avrSt(tavrMinDiff<0)=-1; + + % output after the limiter + Ef(excIdx(avrSt==-1),1)=VavrMin(avrSt==-1); + Ef(excIdx(avrSt== 1),1)=VavrMax(avrSt== 1); + Ef(excIdx(avrSt== 0),1)=Vavrf(avrSt==0,1); + +else + Vavrm=zeros(0,nlvl+1); + Vavrr=zeros(0,nlvl+1); + Vavrf=zeros(0,nlvl+1); + Vavrref=zeros(0,nlvl+1); +end +%------------------------------Initialization of Exciter------------------ +%------------------------------END------------------------------------ + + + + +%------------------------------Initialization of Turbing Governor------------------ +%------------------------------START------------------------------------ +if ~isempty(tg) + nTg = size(tg,1); + % Type 2 Turbing governor. + % one DE, one AE and one limiter + tgIdx = tg(:,1); + + wtgref = tg(:,3); + Rtg = tg(:,4); + Ttgmax = tg(:,5); + Ttgmin = tg(:,6); + Ttg2 = tg(:,7); + Ttg1 = tg(:,8); + + tgovg = zeros(nTg,nlvl+1); % tg + tgovm = zeros(nTg,nlvl+1); % Tmi* + Tmech = zeros(nTg,nlvl+1); % Tmi0 + + % zeroth value is given + tgovg(:,1)=real(tgovg0); + tgovm(:,1)=real(tgovm0); + Tmech(:,1)=real(tgovmech0); + + if ~isempty(Tmech1) + Tmech(:,2)=Tmech1; + end + + % check if limit is approached + tgovMaxDiff=tgovm(:,1)-Ttgmax; + tgovMinDiff=tgovm(:,1)-Ttgmin; + + govSt=zeros(nTg,1); + govSt(tgovMaxDiff>0)=1; + govSt(tgovMinDiff<0)=-1; + % if limit is approached, set Pm to constant value + Pm(tgIdx(govSt==0),1)=tgovm(govSt==0,1); + Pm(tgIdx(govSt==1),1)=Ttgmax(govSt==1,1); + Pm(tgIdx(govSt==-1),1)=Ttgmin(govSt==-1,1); +else + tgovg=zeros(0,nlvl+1); + tgovm=zeros(0,nlvl+1); + Tmech=zeros(0,nlvl+1); +end +%------------------------------Initialization of Turbing Governor------------------ +%------------------------------END------------------------------------ + +% this part i don't quite understand. It looks like f denotes frequency +% on every bus, is it relevant with frequency dependant load? +% now i find that this is for dynamics of agc +f=zeros(nbus,nlvl+1); +f(:,1)=f0; +synTag=zeros(nbus,1); +synTag(syn(:,1))=1:nSyn; +numSynOnBus=accumarray(syn(:,1),1,[nbus,1]); +dpgTag=ones(nbus,1); +for islIdx=1:nIslands + busIsland=find(islands==islIdx); + synTagIsland=synTag(busIsland); + wIsland=w(synTagIsland(synTagIsland~=0),1); + if ~isempty(wIsland) + f(busIsland,1)=mean(wIsland); % note that here the freq can be different + dpgTag(busIsland)=0; + end +end + +%AGC part +if ~isempty(agc) + agcExt=zeros(nbus,size(agc,2)); + agcExt(agc(:,1),:)=agc; + dpg=zeros(nbus,nlvl+1); + dpg(:,1)=dpg0; + fdk=agcExt(:,2)+agcExt(:,3); %1/R+D +else + dpg=zeros(nbus,nlvl+1); + fdk=zeros(nbus,1); +end + +% this is long term dynamic, it seems that not considered here +if ~isempty(cac)&&~isempty(cluster) + +else + qplt=zeros(0,nlvl+1); + vg=zeros(0,nlvl+1); +end + +% freq relevant part induced by AGC +FreqReal=sparse(1:nbus,1:nbus,-freqKeptTag.*fdk.*E0,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); + +Y11=-G;Y12=B;Y21=-B;Y22=-G; +% Influence to Origianl Power flow +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 +if ~isempty(zip) + 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); + Y21=Y21-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,3),[nbus,1]),nbus,nbus); + Y22=Y22-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,4),[nbus,1]),nbus,nbus); +end +YLHS=[Y11,Y12;Y21,Y22]; + +% Counting influence of Motors into small Y matrix +if ~isempty(ind) + 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(:,2),nbus,nbus),sparse(1:nbus,1:nbus,LHS_MatInd_Bus_sqz(:,4),nbus,nbus)]; +end + +% Counting influence of generators into small Y matrix +if ~isempty(syn) + YLHS=YLHS+MatGCD; +end + +idxNonSw=find(busType~=2); +idxNonSwxD=find(fswTagxD==0); +idxNonSwD=find(busType~=2&fswTagxD==1); + +% This is the left hand side matrix totally +LHS_mat=[YLHS([idxNonSw;idxNonSw+nbus],[idxNonSw;idxNonSw+nbus]),... + [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)];... + C0M(ipv,idxNonSw),D0M(ipv,idxNonSw),sparse(npv,2*npq+3*npv+nFreqKept);... + E0M(idxNonSw,idxNonSw),-F0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),-D0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);... + F0M(idxNonSw,idxNonSw),E0M(idxNonSw,idxNonSw),D0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);... + sparse(sum(freqKeptTagxRef),size(idxNonSw,1)+size(idxNonSw,1)+2*npq+3*npv),Freq2freq(freqKeptTagxRef==1,freqKeptTag==1);... + sparse(size(idxNonSwD,1),size(idxNonSw,1)),sparse(1:size(idxNonSwD,1),idxNonSwD,ones(size(idxNonSwD,1),1),size(idxNonSwD,1),size(idxNonSw,1)),sparse(size(idxNonSwD,1),2*npq+3*npv+nFreqKept)]; + +% if nbus<=500 +% [L_LHS_mat,U_LHS_mat,p_LHS_mat]=lu(LHS_mat,'vector'); +% end + + +% deterine if we use LU factoration +% for this part, i assume the system algebrac equation is under a good +% dcondition number and the dimension is not very high, otherwise LU will +% be time consuming +useLU=isfield(SysPara,'iden')&&isfield(SysPara,'p_amd'); + +if useLU + if isempty(SysPara.p_amd) + p_amd = colamd (LHS_mat) ; + save([SysPara.iden,'.mat'],'p_amd'); + else + p_amd=SysPara.p_amd; + end + MxI = speye (size(LHS_mat)) ; + MxQ = MxI (:, p_amd) ; + if IS_OCTAVE + [MxL,MxU,MxP,MxQx] = lu (LHS_mat*MxQ) ; + else + [MxL,MxU,MxP] = lu (LHS_mat*MxQ) ; + end +end + + +%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%this is the recursive part for computing high order of time series%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% strat interations nlvl: order of Taylor series +for i=1:nlvl + % 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) + seq2=getseq(i,2); + seq2p=getseq(i+1,2); + seq3=getseq(i,3); + idxSeq2=sum(seq2==i,2); + idxSeq2x=sum(seq2(:,2)==i,2); + idxSeq2p=sum(seq2p>=i,2); + idxSeq3=sum(seq3==i,2); + idxSeq3x=sum(seq3(:,[2,3])==i,2); + + % 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 + seq2R=seq2(idxSeq2==0,:); + seq2x=seq2(idxSeq2x==0,:); + seq2m=getseq(i-1,2); + seq2mm=getseq(i-2,2); + + RHSILr=zeros(nbus,1); + RHSILi=zeros(nbus,1); + + % This part is for induction motor + if ~isempty(ind) + % package right hand side vector at every iteration + rhsM=sum(Vm(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,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+... + % (T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2)).*Rm./R2; + + % s(:,i+1)=(Rind0.*(T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2))... + % -real(sum(IR(:,seq2m(:,1)+1).*conj(IR(:,seq2m(:,2)+1)),2)).*R2-2*Hm.*sum(repmat(seq2R(:,1)',nInd,1).*s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2))... + % ./(2*Hm.*s(:,1)*i); + % if i>=2 + % s(:,i+1)=s(:,i+1)+... + % Rind1.*(T0.*s(:,i-1)+T1.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2)+T2.*sum(s(:,seq3mm(:,1)+1).*s(:,seq3mm(:,2)+1).*s(:,seq3mm(:,3)+1),2))... + % ./(2*Hm.*s(:,1)*i); + % end + + % update the high order of slip, a special setting is required for + % low order when i<2 + 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 + s(:,i+1)=s(:,i+1)+... + Rind1.*(T1.*s(:,i-1)+T2.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2))... + ./(2*Hm*i); + end + if i==1 + s(:,i+1)=s(:,i+1)+Rind0.*T0./(2*Hm*i); + end + if i==2 + s(:,i+1)=s(:,i+1)+Rind1.*T0./(2*Hm*i); + end + % for dynamic model, Right hand side vector is required a update + addenRhs=Vm(:,1).*s(:,i+1)-1j*X2.*IR(:,1).*s(:,i+1); + + % rhsBus=zeros(2,nInd); + % for j=1:nInd + % rhsBus(:,j)=RHS_C_Shr{j}*[real(rhsM(j)+addenRhs(j));imag(rhsM(j)+addenRhs(j));0;0]; + % end + + % count the influence of dynamic of slip into rigt hand side vector + 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)]'; + + %accumulate currents IL + RHSILr=accumarray(indIdx,rhsBus(1,:)',[nbus,1]); + RHSILi=accumarray(indIdx,rhsBus(2,:)',[nbus,1]); + + % rhsBus=zeros(5,nInd); + % rhsM=sum(Vm(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2); + % rhsImod=Rind1.*(T1.*s(:,i)+T2.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2))+Rind0.*T2.*sum(s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-... + % real(sum(V(indIdx,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2))+... + % real(sum(IL(:,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2).*Z1); + % if i==1 + % rhsImod=rhsImod+Rind1.*T0; + % end + % rhsIL=V(indIdx,i).*Yeind1-IL(:,i).*Ye1ind1; + % for j=1:nInd + % rhsBus(:,j)=squeeze(RHS_C_Shr(j,:,:))*[real(rhsM(j));imag(rhsM(j));rhsImod(j);real(rhsIL(j));imag(rhsIL(j))]; + % end + % RHSILr=accumarray(indIdx,rhsBus(3,:)',[nbus,1]); + % RHSILi=accumarray(indIdx,rhsBus(4,:)',[nbus,1]); + end + + % strat update ZIP load into currents + RHSIiLr=zeros(nbus,1); + RHSIiLi=zeros(nbus,1); + if ~isempty(zip) + RHS_BZip=(real(sum(V(zipIdx,seq2R(:,1)+1).*conj(V(zipIdx,seq2R(:,2)+1)),2))-sum(BiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2))./Bi0/2; + RHZ_BIConv=sum(IiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2); + RHSILr_full=Rzip1.*(JI.*real(V(zipIdx,i))-KI.*imag(V(zipIdx,i)))./Bi0-real(RHZ_BIConv)./Bi0-Ji0L.*RHS_BZip./Bi0; + RHSILi_full=Rzip1.*(KI.*real(V(zipIdx,i))+JI.*imag(V(zipIdx,i)))./Bi0-imag(RHZ_BIConv)./Bi0-Ki0L.*RHS_BZip./Bi0; + RHSIiLr=accumarray(zipIdx,RHSILr_full,[nbus,1]); + RHSIiLi=accumarray(zipIdx,RHSILi_full,[nbus,1]); + end + + % Start uodate generators + RHSIGr=zeros(nbus,1); + RHSIGi=zeros(nbus,1); + if ~isempty(syn) + RhsEd=zeros(nSyn,1); + RhsEq=zeros(nSyn,1); + IGdAdd=zeros(nSyn,1); + IGqAdd=zeros(nSyn,1); + % select different models for generators + if modelTag(8)>0 + d(modSyn==8,i+1)=(wgb(modSyn==8).*w(modSyn==8,i))/i; + w(modSyn==8,i+1)=(Pm(modSyn==8,i)-... + (sum(psid(modSyn==8,seq2m(:,1)+1).*IGq(modSyn==8,seq2m(:,2)+1),2)-sum(psiq(modSyn==8,seq2m(:,1)+1).*IGd(modSyn==8,seq2m(:,2)+1),2))-... + Dg(modSyn==8).*w(modSyn==8,i))./Mg(modSyn==8)/i; + psid(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGd(modSyn==8,i)+psiq(modSyn==8,i)+VGd(modSyn==8,i))/i; + psiq(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGq(modSyn==8,i)-psid(modSyn==8,i)+VGq(modSyn==8,i))/i; + eq1(modSyn==8,i+1)=(-eq1(modSyn==8,i)-(Xgd(modSyn==8)-Xgd1(modSyn==8)-gammad(modSyn==8)).*IGd(modSyn==8,i)+(1-TgAA(modSyn==8)./Tgd1(modSyn==8)).*Ef(modSyn==8,i))./Tgd1(modSyn==8)/i; + ed1(modSyn==8,i+1)=(-ed1(modSyn==8,i)+(Xgq(modSyn==8)-Xgq1(modSyn==8)-gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq1(modSyn==8)/i; + eq2(modSyn==8,i+1)=(-eq2(modSyn==8,i)+eq1(modSyn==8,i)-(Xgd1(modSyn==8)-Xgd2(modSyn==8)+gammad(modSyn==8)).*IGd(modSyn==8,i)+TgAA(modSyn==8)./Tgd1(modSyn==8).*Ef(modSyn==8,i))./Tgd2(modSyn==8)/i; + ed2(modSyn==8,i+1)=(-ed2(modSyn==8,i)+ed1(modSyn==8,i)+(Xgq1(modSyn==8)-Xgq2(modSyn==8)+gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq2(modSyn==8)/i; + IGdAdd(modSyn==8)=(eq2(modSyn==8,i+1)-psid(modSyn==8,i+1))./Xgd2(modSyn==8); + IGqAdd(modSyn==8)=(-ed2(modSyn==8,i+1)-psiq(modSyn==8,i+1))./Xgq2(modSyn==8); + end + if modelTag(6)>0 + d(modSyn==6,i+1)=(wgb(modSyn==6).*w(modSyn==6,i))/i; + w(modSyn==6,i+1)=(Pm(modSyn==6,i)-... + (sum(VGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(VGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)+... + Rga(modSyn==6).*(sum(IGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(IGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)))-... + Dg(modSyn==6).*w(modSyn==6,i))./Mg(modSyn==6)/i; + eq1(modSyn==6,i+1)=(-eq1(modSyn==6,i)-(Xgd(modSyn==6)-Xgd1(modSyn==6)-gammad(modSyn==6)).*IGd(modSyn==6,i)+(1-TgAA(modSyn==6)./Tgd1(modSyn==6)).*Ef(modSyn==6,i))./Tgd1(modSyn==6)/i; + ed1(modSyn==6,i+1)=(-ed1(modSyn==6,i)+(Xgq(modSyn==6)-Xgq1(modSyn==6)-gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq1(modSyn==6)/i; + eq2(modSyn==6,i+1)=(-eq2(modSyn==6,i)+eq1(modSyn==6,i)-(Xgd1(modSyn==6)-Xgd2(modSyn==6)+gammad(modSyn==6)).*IGd(modSyn==6,i)+TgAA(modSyn==6)./Tgd1(modSyn==6).*Ef(modSyn==6,i))./Tgd2(modSyn==6)/i; + ed2(modSyn==6,i+1)=(-ed2(modSyn==6,i)+ed1(modSyn==6,i)+(Xgq1(modSyn==6)-Xgq2(modSyn==6)+gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq2(modSyn==6)/i; + RhsEd(modSyn==6)=ed2(modSyn==6,i+1); + RhsEq(modSyn==6)=eq2(modSyn==6,i+1); + end + if modelTag(5)>0 + d(modSyn==5,i+1)=(wgb(modSyn==5).*w(modSyn==5,i))/i; + w(modSyn==5,i+1)=(Pm(modSyn==5,i)-... + (sum(VGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(VGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)+... + Rga(modSyn==5).*(sum(IGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(IGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)))-... + Dg(modSyn==5).*w(modSyn==5,i))./Mg(modSyn==5)/i; + eq1(modSyn==5,i+1)=(-eq1(modSyn==5,i)-(Xgd(modSyn==5)-Xgd1(modSyn==5)-gammad(modSyn==5)).*IGd(modSyn==5,i)+(1-TgAA(modSyn==5)./Tgd1(modSyn==5)).*Ef(modSyn==5,i))./Tgd1(modSyn==5)/i; + eq2(modSyn==5,i+1)=(-eq2(modSyn==5,i)+eq1(modSyn==5,i)-(Xgd1(modSyn==5)-Xgd2(modSyn==5)+gammad(modSyn==5)).*IGd(modSyn==5,i)+TgAA(modSyn==5)./Tgd1(modSyn==5).*Ef(modSyn==5,i))./Tgd2(modSyn==5)/i; + ed2(modSyn==5,i+1)=(-ed2(modSyn==5,i)+(Xgq(modSyn==5)-Xgq2(modSyn==5)).*IGq(modSyn==5,i))./Tgq2(modSyn==5)/i; + RhsEd(modSyn==5)=ed2(modSyn==5,i+1); + RhsEq(modSyn==5)=eq2(modSyn==5,i+1); + end + if modelTag(4)>0 + d(modSyn==4,i+1)=(wgb(modSyn==4).*w(modSyn==4,i))/i; + w(modSyn==4,i+1)=(Pm(modSyn==4,i)-... + (sum(VGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(VGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)+... + Rga(modSyn==4).*(sum(IGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(IGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)))-... + Dg(modSyn==4).*w(modSyn==4,i))./Mg(modSyn==4)/i; + eq1(modSyn==4,i+1)=(-eq1(modSyn==4,i)-(Xgd(modSyn==4)-Xgd1(modSyn==4)).*IGd(modSyn==4,i)+Ef(modSyn==4,i))./Tgd1(modSyn==4)/i; + ed1(modSyn==4,i+1)=(-ed1(modSyn==4,i)+(Xgq(modSyn==4)-Xgq1(modSyn==4)).*IGq(modSyn==4,i))./Tgq1(modSyn==4)/i; + RhsEd(modSyn==4)=ed1(modSyn==4,i+1); + RhsEq(modSyn==4)=eq1(modSyn==4,i+1); + end + if modelTag(3)>0 + d(modSyn==3,i+1)=(wgb(modSyn==3).*w(modSyn==3,i))/i; + w(modSyn==3,i+1)=(Pm(modSyn==3,i)-... + (sum(VGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(VGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)+... + Rga(modSyn==3).*(sum(IGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(IGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)))-... + Dg(modSyn==3).*w(modSyn==3,i))./Mg(modSyn==3)/i; + eq1(modSyn==3,i+1)=(-eq1(modSyn==3,i)-(Xgd(modSyn==3)-Xgd1(modSyn==3)).*IGd(modSyn==3,i)+Ef(modSyn==3,i))./Tgd1(modSyn==3)/i; + RhsEd(modSyn==3)=0; + RhsEq(modSyn==3)=eq1(modSyn==3,i+1); + end + if modelTag(2)>0 + d(modSyn==2,i+1)=(wgb(modSyn==2).*w(modSyn==2,i))/i; + w(modSyn==2,i+1)=(Pm(modSyn==2,i)-... + (sum(VGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(VGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)+... + Rga(modSyn==2).*(sum(IGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(IGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)))-... + Dg(modSyn==2).*w(modSyn==2,i))./Mg(modSyn==2)/i; + RhsEd(modSyn==2)=0; + RhsEq(modSyn==2)=eq1(modSyn==2,i+1); + end + + % + %DTsin(:,i+1) = 1/(i)*sum(repmat([i:-1:1],GLO_NDOF,1).*DTcos(:,1:i).*DTdta(:,k+2:-1:2),2); + %DTcos(:,i+1) =-1/(i)*sum(repmat([i:-1:1],GLO_NDOF,1).*DTsin(:,1:i).*DTdta(:,k+2:-1:2),2); + + %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); + + % this part may be different from DT + AG0=cosp(:,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 + if taylorN>=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); + end + if taylorN>=3 + AG0=AG0+cosp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2); + BG0=BG0+sinp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2); + end + if taylorN>=4 + seq4=getseq(i,4); + AG0=AG0+cosp(:,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 + + % high order coefficients of cos(delta) and sin(delta) + Cd(:,i+1)=AG0; + Sd(:,i+1)=BG0; + + VGdCr=sum(Cd(:,seq2x(:,1)+1).*VGd(:,seq2x(:,2)+1),2);% Vd*cosdta + VGqCr=sum(Cd(:,seq2x(:,1)+1).*VGq(:,seq2x(:,2)+1),2);% Vq*cosdta + VGdSr=sum(Sd(:,seq2x(:,1)+1).*VGd(:,seq2x(:,2)+1),2);% Vd*sindta + VGqSr=sum(Sd(:,seq2x(:,1)+1).*VGq(:,seq2x(:,2)+1),2);% Vq*sindta + JCr=sum(Cd(:,seq2x(:,1)+1).*JG(:,seq2x(:,2)+1),2);% similar, for currents + KCr=sum(Cd(:,seq2x(:,1)+1).*KG(:,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); + + RHSIGxr=-(MatsRs(:,1).*(-VGdSr-VGqCr)+MatsRs(:,2).*(VGdCr-VGqSr))+... + (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))+... + (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 + RHSIGr=accumarray(synIdx,RHSIGxr,[nbus,1]); + RHSIGi=accumarray(synIdx,RHSIGxi,[nbus,1]); + end + % update exciter, 3 state variables + if ~isempty(exc) + 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; + Vavrf(:,i+1)=((vavrf0.*Vmag(synIdx(excIdx),i)+... + sum(Vavrr(:,seq2m(:,1)+1).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2)+... + muavr0.*Tavr1./Tavr2.*sum((Vavrref(:,seq2m(:,1)+1)-Vavrm(:,seq2m(:,1)+1)).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2))./Vavr0-Vavrf(:,i))./Tavre/i; + Ef(excIdx(avrSt==-1),i+1)=0; + Ef(excIdx(avrSt== 1),i+1)=0; + Ef(excIdx(avrSt== 0),i+1)=Vavrf(avrSt==0,i+1); + end + + % update agc, one state variables + if ~isempty(agc) + dpg(:,i+1)=-f(:,i).*agcExt(:,4)/i; + for islIdx=1:nIslands + busIsland=find(islands==islIdx); + synTagIsland=synTag(busIsland); + wIsland=w(synTagIsland(synTagIsland~=0),i+1); + if ~isempty(wIsland) + f(busIsland,i+1)=mean(wIsland); % note that here the freq can be different + end + end % TODO: steady-state model + + % update generator participation part from agc + if ~isempty(syn) %dynamic model (synchronous generators) + if ~isempty(tg) + Tmech(:,i+1)=Tmech(:,i+1)+dpg(syn(tg(:,1),1),i+1)./numSynOnBus(syn(tg(:,1),1)); + end + Pm(:,i+1)=Pm(:,i+1)+dpg(syn(:,1),i+1)./numSynOnBus(syn(:,1)); + end + end + % update Turbine, 2 state variables + if ~isempty(tg) + 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); + + Pm(tgIdx(govSt==0),i+1)=tgovm(govSt==0,i+1); + Pm(tgIdx(govSt==1),i+1)=0; + Pm(tgIdx(govSt==-1),i+1)=0; + end + + % HEM Body + RHS1=sum((-P(:,seq2(:,1)+1)+1j*(Q(:,seq2(:,1)+1)+Qxtra(:,seq2(:,1)+1))).*conj(W(:,seq2(:,2)+1)),2)+... + freqKeptTag.*sum(-dpg(:,seq2(:,1)+1).*conj(W(:,seq2(:,2)+1)),2)+... + freqKeptTag.*fdk.*sum(f(:,seq2R(:,1)+1).*conj(W(:,seq2R(:,2)+1)),2)+Ysh1.*V(:,i)+Ytr1*V(:,i); + 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); + + + if i==1 + RHS2=RHS2+0.5*VspSq2(:,2); + end + + compactRHS1=RHS1(busType~=2); + compactRHS1=compactRHS1+Y(busType~=2,isw)*real(V(isw,i+1)); + % combine all current injection involing Motor, zip load, and Generators + RHS=[real(compactRHS1)+RHSILr(busType~=2)+RHSIiLr(busType~=2)-RHSIGr(busType~=2);... + imag(compactRHS1)+RHSILi(busType~=2)+RHSIiLi(busType~=2)-RHSIGi(busType~=2);... + RHS2(ipv);... + real(RHS3(busType~=2));... + imag(RHS3(busType~=2));... + zeros(sum(freqKeptTagxRef),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 + if useLU + if IS_OCTAVE + x = real(MxQ * MxQx* (MxU \ (MxL \ (MxP * RHS)))) ; + else + x =real( MxQ * (MxU \ (MxL \ (MxP * RHS)))) ; + end + else + x=real(LHS_mat\RHS); + end + + % x= [V;W;Q_pv;f] + xC=real(V(:,i+1)); + xD=imag(V(:,i+1)); + xC(idxNonSw)=x(1:(npq+npv)); + xD(idxNonSw)=x(((npq+npv)+1):(2*(npq+npv))); + V(:,i+1)=xC+1j*xD; + W(busType~=2,i+1)=x((2*(npq+npv)+1):(3*(npq+npv)))+... + 1j*x((3*(npq+npv)+1):(4*(npq+npv))); + Q(ipv,i+1)=x((4*(npq+npv)+1):(4*(npq+npv)+npv)); + f(freqKeptTag==1,i+1)=x((4*(npq+npv)+npv+1):end); + + 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 + if ~isempty(ind) + % for j=1:nInd + % tempIL=squeeze(LHS_MatInd_Shr(j,:,:))*[real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]+rhsBus(:,j); + % tempIRs=-LHS_MatInd_Shr2{j}*[tempIL;real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]; + % IL(j,i+1)=tempIL(1)+1j*tempIL(2); + % IR(j,i+1)=tempIRs(1)+1j*tempIRs(2); + % Vm(j,i+1)=V(indIdx(j),i+1)-IL(j,i+1)*Z1(j); + % end + tempILvr=LHS_MatInd_Shr_sqz(:,1).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,3).*imag(V(indIdx,i+1))+rhsBus(1,:)'; + tempILvi=LHS_MatInd_Shr_sqz(:,2).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,4).*imag(V(indIdx,i+1))+rhsBus(2,:)'; + tempIRsvr=-sum(LHS_MatInd_Shr2_sqz(:,[1,3,5,7]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2); + tempIRsvi=-sum(LHS_MatInd_Shr2_sqz(:,[2,4,6,8]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2); + IL(:,i+1)=tempILvr+1j*tempILvi; + IR(:,i+1)=tempIRsvr+1j*tempIRsvi; + Vm(:,i+1)=V(indIdx,i+1)-IL(:,i+1).*Z1; + end + + % now update the Algebric variables for ZIP loads + if ~isempty(zip) + IiL(:,i+1)=(LHS_MatZip(:,1)+1j*LHS_MatZip(:,3)).*real(V(zipIdx,i+1))+(LHS_MatZip(:,2)+1j*LHS_MatZip(:,4)).*imag(V(zipIdx,i+1))+(RHSILr_full+1j*RHSILi_full); + BiL(:,i+1)=Mat_BZip(:,1).*real(V(zipIdx,i+1))+Mat_BZip(:,2).*imag(V(zipIdx,i+1))+RHS_BZip; + end + + % now update the Algebric variables for Generators: Vd,Vq, Id, Iq + if ~isempty(syn) + 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; + IGd(:,i+1)=JSr-KCr+sind.*JG(:,i+1)-cosd.*KG(:,i+1); + IGq(:,i+1)=JCr+KSr+cosd.*JG(:,i+1)+sind.*KG(:,i+1); + tempVGC=real(V(synIdx,i+1))-VGdSr-VGqCr; + tempVGD=imag(V(synIdx,i+1))+VGdCr-VGqSr; + VGd(:,i+1)=sind.*tempVGC-cosd.*tempVGD; + VGq(:,i+1)=cosd.*tempVGC+sind.*tempVGD; + end +end + +% Output value: coefficients for every order. +Q=real(Q); +s=real(s); +d=real(d); +w=real(w); +eq1=real(eq1); +eq2=real(eq2); +ed1=real(ed1); +ed2=real(ed2); +psid=real(psid); +psiq=real(psiq); +Pm=real(Pm); +Ef=real(Ef); +Vavrm=real(Vavrm); +Vavrr=real(Vavrr); +Vavrf=real(Vavrf); +Vavrref=real(Vavrref); +tgovg=real(tgovg); +tgovm=real(tgovm); +Tmech=real(Tmech); +f=real(f); +dpg=real(dpg); +qplt=real(qplt); +vg=real(vg); + +if ~isempty(exc) + avr={Vavrm,Vavrr,Vavrf}; +end + +if ~isempty(tg) + gov={tgovg,tgovm}; +end +end \ No newline at end of file diff --git a/internal/VSOO.m b/internal/VSOO.m new file mode 100644 index 0000000..61da7e7 --- /dev/null +++ b/internal/VSOO.m @@ -0,0 +1,143 @@ +function [outputArg1,outputArg2,outputArg3] = VSOO(LTE,LTE2,LTE3,SimData,h,ratio_b4) +%UNTITLED5 Summary of this function goes here +% Detailed explanation goes here + tol = 10^-4; + theta_max = 2; + Kmax = 30; + Kmin = 10; + p = 1; + fac1 = 1; + fac2 = 1; + hmax = SimData.maxAlpha; + hmin = 1*SimData.dAlpha; + + + + + K = SimData.nlvl; + ratio = LTE/h; + K_I = 0.03/K; + K_p = 0.04/K; + pf1 = 1; + pf2 = 0; + pf3 = 0; + if isnan(ratio_b4) + else + theta = ((tol/ratio)^(K_I))*((ratio_b4/ratio)^(K_p)); + Lin = (pf1*(K+p)^2+pf2*(K+p)+pf3)/(pf1*(K)^2+pf2*(K)+pf3); + Lde = (pf1*(K-p)^2+pf2*(K-p)+pf3)/(pf1*(K)^2+pf2*(K)+pf3); + + + trc_in =(LTE2)/(1-LTE2^((K+p))); + trc_de= (LTE3)/(1-LTE3^((K-p))); + + tol_in = tol; + tol_de = tol; + + theta_in =0.9*(h*tol_in/trc_in)^(1/(K+p)); + theta_de =0.9*(h*tol_de/trc_de)^(1/(K-p)); + + + if theta> theta_max + h_can = theta_max*h; + else + h_can = theta*h; + end + + + if h_can > hmax + h_can = hmax; + + elseif h < hmin + h_can = hmin; + + end + + if theta_in> theta_max + theta_in = theta_max; + end + if theta_de> theta_max + theta_de = theta_max; + end + + + + if h*theta_in > hmax + h_inp = hmax; + + elseif h*theta_in < hmin + h_inp = hmin; + else + + h_inp = h*theta_in; + end + + + if h*theta_de > hmax + h_dep = hmax; + + elseif h*theta_de < hmin + h_dep = hmin; + else + h_dep = h*theta_de; + end + + + if (Lde=K)))&&((h>= h_can||(h==hmax&&KK(1,ite+1)<=K))&&Lin = h_can||(h_can==hmax))&&Lin theta_max + h = theta_max*h; + else + h = theta*h; + end + + + end + + if h > hmax; h = hmax; end + if h < hmin; h = hmin; end + if K > Kmax; K = Kmax; end + if K < Kmin; K = Kmin; end +% ratio_b4 = ratio; + +outputArg1 = K; +outputArg2 = h; +outputArg3 = ratio; +end + diff --git a/internal/dtmMachinePFSalientcontinueDyn.m b/internal/dtmMachinePFSalientcontinueDyn.m new file mode 100644 index 0000000..99fcd2d --- /dev/null +++ b/internal/dtmMachinePFSalientcontinueDyn.m @@ -0,0 +1,1228 @@ +function [V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrf,Vavrref,tgovg,tgovm,Tmech,f,dpg,qplt,vg]=... + dtmMachinePFSalientcontinueDyn(SimData,SysData,SysPara,x0) +% Core HE algorithm for solving DAEs (dynamic simulation) +% +% FUNCTION hemMachinePFSalientcontinueDyn +% +% Author: Rui Yao +% +% Copyright (C) 2021, UChicago Argonne, LLC. All rights reserved. +% +% OPEN SOURCE LICENSE +% +% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +% +% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +% +% +% ****************************************************************************************************** +% DISCLAIMER +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% *************************************************************************************************** +% +% INPUT +% SimData - Simulation parameters +% SysData - System data for simulation +% SysPara - Parameters representing the events happening in the system +% x0 - Initial system state +% +% OUTPUT - (will be consolidated in a future version) +% +% TODO % Modify the output arguments +% + +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); +% nbus:Total number of buses khuang, 8 Jul +nbus=size(bus,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') + nIslands=SysPara.nIslands;islands=SysPara.islands;refs=SysPara.refs; +else + [nIslands,islands,refs]=searchIslands(bus(:,1),line(:,1:2)); +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); +% import simualtion data khuang 8 JUl +[~,~,~,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); +% + +% 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 +Qls=zeros(nbus,2);Qls(pq(:,1),1)=pqIncr(:,2); +if size(pqIncr,2)>=4 + Pls(pq(:,1),2)=pqIncr(:,3); + Qls(pq(:,1),2)=pqIncr(:,4); +end + +% formatting Ymatrix, here the default value of fault is empty, khuang 8 JUL +if isempty(Ytr0) + [Y,Ytr0,Ysh,ytrfr,ytrto,yshfr,yshto]=getYMatrix(nbus,line); +end + + +% reshape the pv, pq shunt and swing buses as zeros if they are empty +% khuang 8JUL +busType=zeros(nbus,1); +if isempty(pv) + pv=zeros(0,6); +end +if isempty(pq) + pq=zeros(0,6); +end +if isempty(shunt) + shunt=zeros(0,7); +end +if isempty(sw) + sw=zeros(0,13); +end + +% label pv and swing buses khuang 8 JUL +% 1: PV bus, 0: PQ bus +busType(pv(:,1))=1; +busType(sw(:,1))=2; +% 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); +ipv=find(busType==1); +ipq=find(busType==0); +npq=size(ipq,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(shunt(:,1))=shunt(:,5)+1j*shunt(:,6); + +% check if zip load exists in the system +% and initialized zip load khuang 8 JUL +if ~isempty(zip)%zipMode=0 + Ysh0=Ysh0+accumarray(zip(:,1),Rzip0.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]); + Ysh1=Ysh1+accumarray(zip(:,1),Rzip1.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]); +end + +% now zip load + shunt khuang 8 JUL +Ysh0=Ysh0+yShunt; +% 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); + +%initialize P and Q for every bus khuang 8 JUL +pVec=zeros(nbus,1); +qVec=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(pq(:,1))=pVec(pq(:,1))-pq(:,4); +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 + pVec=pVec-accumarray(zip(:,1),Rzip0.*zip(:,7).*zip(:,12),[nbus,1]); + qVec=qVec-accumarray(zip(:,1),Rzip0.*zip(:,10).*zip(:,12),[nbus,1]); +end +% qVec(ipv)=qVec(ipv)+Q0(ipv); +% 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(:,1)=V0; +W=zeros(nbus,nlvl+1); +W(:,1)=1./V0; + + +% initiliza magnitude of voltage V for every bus khuang 8 JUL +Vmag=zeros(nbus,nlvl+1); +Vmag(:,1)=abs(V0); + +% Power is initilized as we already cooked pVec and qVec khuang 8 JUL +P=zeros(nbus,nlvl+1); +P(:,1)=pVec; +% 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); +Qxtra=zeros(size(Q)); +Q(:,1)=Q0; +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; +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) + 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]); +end +% Qxtra(busType~=0,2:end)=Q(busType~=0,2:end); +% 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)); +D0=imag(V(:,1)); +E0=real(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); +D0M=sparse(1:nbus,1:nbus,D0,nbus,nbus); +E0M=sparse(1:nbus,1:nbus,E0,nbus,nbus); +F0M=sparse(1:nbus,1:nbus,F0,nbus,nbus); +P0M=sparse(1:nbus,1:nbus,P(:,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); +B=imag(Y); +% so Y = G + 1i*B. khuang 8 JUL + +% 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 +freqKeptTag=zeros(nbus,1); +frefs=refs; +fswTag=zeros(nbus,1); +fsynTag=zeros(nbus,1); +fswTag(isw)=1; +fswTagxD=fswTag; +fsynTag(syn(:,1))=1; +for isl=1:nIslands + if isempty(find(fswTag(islands==isl)==1, 1)) + if isempty(find(fsynTag(islands==isl)==1, 1)) + freqTypeTag(isl)=2; + busesInIsland=find(islands==isl); + [~,imin]=min(abs(D0(busesInIsland))); + frefs(isl)=busesInIsland(imin(1)); + fswTagxD(frefs(isl))=1; + freqKeptTag(busesInIsland)=1; + else + freqTypeTag(isl)=1; + end + end +end +freqKeptTagxRef=freqKeptTag; +freqKeptTagxRef(frefs)=0; +nFreqKept=sum(freqKeptTag); +%----------------------------------------------------------- + + + + +%------------------------------------------------------------------ +% this part is for initialling inductor. khuang 8 JUL +if ~isempty(ind) % check if there is any inductor% khuang 8 JUL + nInd=size(ind,1); % determine the number of inductors % khuang 8 JUL + 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); + X1=ind(:,8); + Z1=ind(:,7)+1j*ind(:,8); + Ze=1j*ind(:,13); + R2=ind(:,9); + X2=ind(:,10); + T0=ind(:,15)+ind(:,16)+ind(:,17); + T1=-ind(:,16)-2*ind(:,17); + T2=ind(:,17); + Hm=ind(:,14); + %-----------------parameters of inductors---------% khuang 8 JUL + %-----------------END----------------% khuang 8 JUL + + + Rm=zeros(nInd,1); + + 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+1j*X2.*s0)./(R2.*Reind0+(1j*X2.*Reind0+Ze).*s0)); + Vm(:,1)=V0(indIdx)-IL(:,1).*Z1; + IR(:,1)=Vm(:,1).*s0./(R2+1j*X2.*s0); + + J0=real(IR(:,1)); + K0=imag(IR(:,1)); + JL0=real(IL(:,1)); + KL0=imag(IL(:,1)); + + % prepare the algebric matrix % khuang 8 JUL + Yeind0=Reind0./Ze; + Yeind1=Reind1./Ze; + Ye1ind0=Reind0.*Z1./Ze; + Ye1ind1=Reind1.*Z1./Ze; + Ge=real(Yeind0); + Be=imag(Yeind0); + kg1e=real(Ye1ind0); + kb1e=imag(Ye1ind0); + Ge1=real(Yeind1); + Be1=imag(Yeind1); + kg1e1=real(Ye1ind1); + kb1e1=imag(Ye1ind1); + + % LHS_MatInd_Shr_sqz=zeros(nInd,4); + % RHS_C_Shr_sqz=zeros(nInd,8); + % LHS_MatInd_Shr2_sqz=zeros(nInd,8); + % + % LHS_MatInd_Shr=zeros(nInd,2,2); + % RHS_C_Shr=cell(nInd,1); + % LHS_MatInd_Shr2=cell(nInd,1); % A^-1B + % LHS_MatInd_Shr3=cell(nInd,1); % A^-1 + % + % for i=1:nInd + % LHS_MatInd=[R2(i),-X2(i)*s0(i),R1(i)*s0(i),-X1(i)*s0(i),-s0(i),0;... + % X2(i)*s0(i), R2(i),X1(i)*s0(i), R1(i)*s0(i),0,-s0(i);... + % -1,0,1+kg1e(i),-kb1e(i),-Ge(i), Be(i);... + % 0,-1,kb1e(i), 1+kg1e(i),-Be(i),-Ge(i);]; + % temp0=LHS_MatInd([3,4],[1,2])\eye(2); % A^-1 + % LHS_MatInd_Shr2{i}=temp0*LHS_MatInd([3,4],[3,4,5,6]); % A^-1B + % LHS_MatInd_Shr3{i}=temp0; % A^-1 + % temp1=LHS_MatInd([1,2],[1,2])/LHS_MatInd([3,4],[1,2]); % CA^-1 + % temp2=LHS_MatInd([1,2],[3,4,5,6])-temp1*LHS_MatInd([3,4],[3,4,5,6]); % L=D-CA^-1B + % LHS_MatInd_Shr(i,:,:)=-temp2(:,[1,2])\temp2(:,[3,4]); % -R\S + % RHS_C_Shr{i}=temp2(:,[1,2])\[eye(2),-temp1]; % R\[I,-CA^-1] + % + % LHS_MatInd_Shr_sqz(i,:)=reshape(LHS_MatInd_Shr(i,:,:),[1,4]); + % RHS_C_Shr_sqz(i,:)=reshape(RHS_C_Shr{i},[1,8]); + % LHS_MatInd_Shr2_sqz(i,:)=reshape(LHS_MatInd_Shr2{i},[1,8]); + % end + % LHS_MatInd_Bus=zeros(nbus,2,2); % \sum{-R\S} by buses + % LHS_MatInd_Bus(:,1,1)=accumarray(indIdx,LHS_MatInd_Shr(:,1,1),[nbus,1]); + % LHS_MatInd_Bus(:,1,2)=accumarray(indIdx,LHS_MatInd_Shr(:,1,2),[nbus,1]); + % LHS_MatInd_Bus(:,2,1)=accumarray(indIdx,LHS_MatInd_Shr(:,2,1),[nbus,1]); + % LHS_MatInd_Bus(:,2,2)=accumarray(indIdx,LHS_MatInd_Shr(:,2,2),[nbus,1]); + + MInd0=zeros(nInd,1); + MInd1=ones(nInd,1); + LHS_MatInd_sqz=[R2,X2.*s0,-MInd1,MInd0,... + -X2.*s0,R2,MInd0,-MInd1,... + R1.*s0,X1.*s0,MInd1+kg1e,kb1e,... + -X1.*s0,R1.*s0,-kb1e,MInd1+kg1e,... + -s0,MInd0,-Ge,-Be,... + MInd0,-s0,Be,-Ge]; % 4*6 matrix [C,D;A,B] + LHS_MatInd_idx=reshape((1:24)',[4,6]); + temp0inv_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[1,2]),1,[])); + temp0inv_sqz_det=temp0inv_sqz(:,1).*temp0inv_sqz(:,4)-temp0inv_sqz(:,2).*temp0inv_sqz(:,3); + temp0_sqz=[temp0inv_sqz(:,4),-temp0inv_sqz(:,2),-temp0inv_sqz(:,3),temp0inv_sqz(:,1)]./repmat(temp0inv_sqz_det,[1,4]);% A^-1 + indB_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[3,4,5,6]),1,[])); + LHS_MatInd_Shr2_sqz=[temp0_sqz(:,1).*indB_sqz(:,1)+temp0_sqz(:,3).*indB_sqz(:,2),temp0_sqz(:,2).*indB_sqz(:,1)+temp0_sqz(:,4).*indB_sqz(:,2),... + temp0_sqz(:,1).*indB_sqz(:,3)+temp0_sqz(:,3).*indB_sqz(:,4),temp0_sqz(:,2).*indB_sqz(:,3)+temp0_sqz(:,4).*indB_sqz(:,4),... + temp0_sqz(:,1).*indB_sqz(:,5)+temp0_sqz(:,3).*indB_sqz(:,6),temp0_sqz(:,2).*indB_sqz(:,5)+temp0_sqz(:,4).*indB_sqz(:,6),... + temp0_sqz(:,1).*indB_sqz(:,7)+temp0_sqz(:,3).*indB_sqz(:,8),temp0_sqz(:,2).*indB_sqz(:,7)+temp0_sqz(:,4).*indB_sqz(:,8)];% A^-1B + indC_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[1,2]),1,[])); + temp1_sqz=[indC_sqz(:,1).*temp0_sqz(:,1)+indC_sqz(:,3).*temp0_sqz(:,2),indC_sqz(:,2).*temp0_sqz(:,1)+indC_sqz(:,4).*temp0_sqz(:,2),... + indC_sqz(:,1).*temp0_sqz(:,3)+indC_sqz(:,3).*temp0_sqz(:,4),indC_sqz(:,2).*temp0_sqz(:,3)+indC_sqz(:,4).*temp0_sqz(:,4)];% CA^-1 + temp2_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[3,4,5,6]),1,[]))-... + [temp1_sqz(:,1).*indB_sqz(:,1)+temp1_sqz(:,3).*indB_sqz(:,2),temp1_sqz(:,2).*indB_sqz(:,1)+temp1_sqz(:,4).*indB_sqz(:,2),... + temp1_sqz(:,1).*indB_sqz(:,3)+temp1_sqz(:,3).*indB_sqz(:,4),temp1_sqz(:,2).*indB_sqz(:,3)+temp1_sqz(:,4).*indB_sqz(:,4),... + temp1_sqz(:,1).*indB_sqz(:,5)+temp1_sqz(:,3).*indB_sqz(:,6),temp1_sqz(:,2).*indB_sqz(:,5)+temp1_sqz(:,4).*indB_sqz(:,6),... + temp1_sqz(:,1).*indB_sqz(:,7)+temp1_sqz(:,3).*indB_sqz(:,8),temp1_sqz(:,2).*indB_sqz(:,7)+temp1_sqz(:,4).*indB_sqz(:,8)];% L=D-CA^-1B=[R,S] + temp2_c12_sqz=temp2_sqz(:,1:4); + temp2_c34_sqz=temp2_sqz(:,5:8); + temp2_c12_sqz_det=temp2_c12_sqz(:,1).*temp2_c12_sqz(:,4)-temp2_c12_sqz(:,2).*temp2_c12_sqz(:,3); + temp2_c12_inv_sqz=[temp2_c12_sqz(:,4),-temp2_c12_sqz(:,2),-temp2_c12_sqz(:,3),temp2_c12_sqz(:,1)]./repmat(temp2_c12_sqz_det,[1,4]); + LHS_MatInd_Shr_sqz=-[temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,2),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,2),... + temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,4),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,4)];% -R\S + 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(:,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(:,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(:,3)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,3),[nbus,1]); + LHS_MatInd_Bus_sqz(:,4)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,4),[nbus,1]); +else + s=zeros(0,nlvl+1); +end +% Initialization of inductors is finished % khuang 8 JUL +%------------------------------------------------------------------ + + +%------------------------------Initialization of ZIP load---------% khuang 8 JUL +if ~isempty(zip) + nZip=size(zip,1); + zipIdx=zip(:,1); + IiL=zeros(nZip,nlvl+1); + BiL=zeros(nZip,nlvl+1); + + % prepare the necessary matrix by blocks% khuang 8 JUL + Bi0=abs(V0(zipIdx)); + JI=zip(:,6); + KI=-zip(:,9); + % current % khuang 8 JUL + Ii0L=Rzip0.*(JI+1j*KI).*V0(zipIdx)./Bi0; + Ji0L=real(Ii0L); + Ki0L=imag(Ii0L); + + IiL(:,1)=Ii0L; + BiL(:,1)=Bi0; + % voltage% khuang 8 JUL + Ci0=real(V0(zipIdx)); + Di0=imag(V0(zipIdx)); + + LHS_MatZip=[Rzip0.*JI./Bi0-Ci0.*Ji0L./Bi0./Bi0,-Rzip0.*KI./Bi0-Di0.*Ji0L./Bi0./Bi0,... + Rzip0.*KI./Bi0-Ci0.*Ki0L./Bi0./Bi0,Rzip0.*JI./Bi0-Di0.*Ki0L./Bi0./Bi0]; + Mat_BZip=[Ci0./Bi0,Di0./Bi0]; +else + IiL=zeros(0,nlvl+1); +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); +if ~isempty(syn) + synIdx =syn(:,1);% index number of Generators% khuang 8 JUL + wgb =syn(:,4);% maybe the base value% khuang 8 JUL + modSyn =syn(:,5);% the order of generator models% khuang 8 JUL + Xgl =syn(:,6); + Rga =syn(:,7); + Xgd =syn(:,8); + Xgd1 =syn(:,9); + Xgd2 =syn(:,10); + Tgd1 =syn(:,11); + Tgd2 =syn(:,12); + Xgq =syn(:,13); + Xgq1 =syn(:,14); + Xgq2 =syn(:,15); + Tgq1 =syn(:,16); + Tgq2 =syn(:,17); + Mg =syn(:,18); + Dg =syn(:,19); + TgAA =syn(:,24); + gammad =Tgd2./Tgd1.*Xgd2./Xgd1.*(Xgd-Xgd1); + gammaq =Tgq2./Tgq1.*Xgq2./Xgq1.*(Xgq-Xgq1); + + d=zeros(nSyn,nlvl+1); % delta% khuang 8 JUL + w=zeros(nSyn,nlvl+1); % omega% khuang 8 JUL + eq1=zeros(nSyn,nlvl+1); %eq'% khuang 8 JUL + eq2=zeros(nSyn,nlvl+1); %eq''% khuang 8 JUL + ed1=zeros(nSyn,nlvl+1); %ed'% khuang 8 JUL + ed2=zeros(nSyn,nlvl+1); %ed''% khuang 8 JUL + psiq=zeros(nSyn,nlvl+1); % not sure, only in 8th order model% khuang 8 JUL + psid=zeros(nSyn,nlvl+1); % not sure, only in 8th order model% khuang 8 JUL + JG=zeros(nSyn,nlvl+1); + KG=zeros(nSyn,nlvl+1); + IGq=zeros(nSyn,nlvl+1); + IGd=zeros(nSyn,nlvl+1); + VGq=zeros(nSyn,nlvl+1); + VGd=zeros(nSyn,nlvl+1); + Cd=zeros(nSyn,nlvl+1); + Sd=zeros(nSyn,nlvl+1); + Ef=zeros(nSyn,nlvl+1); + Pm=zeros(nSyn,nlvl+1); + + cosd=cos(d0); + sind=sin(d0); + CG0=C0(synIdx); + DG0=D0(synIdx); + % the first value is given here, notice all are 8th order model% khuang 8 JUL + d(:,1)=d0; + w(:,1)=w0; + eq1(:,1)=eq10; + eq2(:,1)=eq20; + ed1(:,1)=ed10; + ed2(:,1)=ed20; + psiq(:,1)=psiq0; + psid(:,1)=psid0; + + % transform between grid side and dq side% khuang 8 JUL + + VGd(:,1)=sind.*CG0-cosd.*DG0; + VGq(:,1)=cosd.*CG0+sind.*DG0; + % now they are under dq side% khuang 8 JUL + + 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; + Pm(:,1)=Pm0; + + %check if controller exists% khuang 8 JUL + if ~isempty(Ef1) + Ef(:,2)=Ef1; + end + if ~isempty(Eq11) + eq1(:,2)=Eq11; + end + if ~isempty(Pm1) + Pm(:,2)=Pm1; + 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 + + Mats=zeros(nSyn,4); + MatsR=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]); + + % 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 + % 8th order, no need to change% khuang 8 JUL + if modSyn(i)==8 + IGd(i,1)=(eq20(i)-psid0(i))/Xgd2(i); + IGq(i,1)=(-ed20(i)-psiq0(i))/Xgq2(i); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + % 6th order% khuang 8 JUL + 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)); + 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)]; + % 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)]/... + (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),... + 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 + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(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)); + 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)]; + % 4th order% khuang 8 JUL + 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)); + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq1(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq1(i)+sind(i)*Rga(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),... + 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 + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq(i)+sind(i)*Rga(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),... + 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 + 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)); + Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)]; + MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd(i),-cosd(i)*Xgq(i)+sind(i)*Rga(i)]/... + (Rga(i)*Rga(i)+Xgd(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),... + MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)]; + end + end + % 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); + + % 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);... + sparse(synIdx,synIdx,MatsRs(:,3),nbus,nbus),sparse(synIdx,synIdx,MatsRs(:,4),nbus,nbus)]; +else + d=zeros(0,nlvl+1); + w=zeros(0,nlvl+1); + eq1=zeros(0,nlvl+1); + eq2=zeros(0,nlvl+1); + ed1=zeros(0,nlvl+1); + ed2=zeros(0,nlvl+1); + psiq=zeros(0,nlvl+1); + psid=zeros(0,nlvl+1); + JG=zeros(0,nlvl+1); + KG=zeros(0,nlvl+1); + IGq=zeros(0,nlvl+1); + IGd=zeros(0,nlvl+1); + VGq=zeros(0,nlvl+1); + VGd=zeros(0,nlvl+1); + Cd=zeros(0,nlvl+1); + Sd=zeros(0,nlvl+1); + Ef=zeros(0,nlvl+1); + Pm=zeros(0,nlvl+1); +end +%------------------------------Initialization of GEN------------------% khuang 8 JUL +%------------------------------EnD------------------------------------% khuang 8 JUL + + + +%------------------------------Initialization of Exciter------------------% khuang 8 JUL +%------------------------------START------------------------------------% khuang 8 JUL +if ~isempty(exc) + nExc =size(exc,1); + % All Type 3 AVR, a 3rd order controller + % for Type 3 AVR, avr0(:,1:3) are Vavrm, Vavrr, Vavrf, + % and avr0(:,4) is reference Vref (input for secondary voltage control). + excIdx = exc(:,1); + VavrMax = exc(:,3); + VavrMin = exc(:,4); + muavr0 = exc(:,5); + Tavr1 = exc(:,7); + Tavr2 = exc(:,6); + vavrf0 = exc(:,8); + Vavr0 = exc(:,9); + Tavre = exc(:,10); + Tavrr = exc(:,11); + + %here I need to check why Vavrref is time varing instead of constant% khuang 8 JUL + % memory is given to state variables of EXC% khuang 8 JUL + Vavrm = 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); + Vavrr(:,1)=real(Vavrr0); + Vavrf(:,1)=real(Vavrf0); + Vavrref(:,1)=real(Vavrref0); + + % here Varref1 is given with syspara.% khuang 8 JUL + if ~isempty(Varref1) + Vavrref(:,2)=Varref1; + end + + % non-windup limiter, check the limit.% khuang 8 JUL + tavrMaxDiff=Vavrf(:,1)-VavrMax; + tavrMinDiff=Vavrf(:,1)-VavrMin; + + % label values in different interval.% khuang 8 JUL + avrSt=zeros(nExc,1); + avrSt(tavrMaxDiff>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)=VavrMax(avrSt== 1); + Ef(excIdx(avrSt== 0),1)=Vavrf(avrSt==0,1); + +else + Vavrm=zeros(0,nlvl+1); + Vavrr=zeros(0,nlvl+1); + Vavrf=zeros(0,nlvl+1); + Vavrref=zeros(0,nlvl+1); +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) + nTg = size(tg,1); + % Type 2 Turbing governor. + % one DE, one AE and one limiter + tgIdx = tg(:,1); + + wtgref = tg(:,3); + Rtg = tg(:,4); + Ttgmax = tg(:,5); + Ttgmin = tg(:,6); + Ttg2 = tg(:,7); + Ttg1 = tg(:,8); + + tgovg = zeros(nTg,nlvl+1); % tg % khuang 8 JUL + tgovm = zeros(nTg,nlvl+1); % Tmi* % khuang 8 JUL + Tmech = zeros(nTg,nlvl+1); % Tmi0 % khuang 8 JUL + + % zeroth value is given % khuang 8 JUL + tgovg(:,1)=real(tgovg0); + tgovm(:,1)=real(tgovm0); + Tmech(:,1)=real(tgovmech0); + + if ~isempty(Tmech1) + Tmech(:,2)=Tmech1; + end + + % check if limit is approached % khuang 8 JUL + tgovMaxDiff=tgovm(:,1)-Ttgmax; + tgovMinDiff=tgovm(:,1)-Ttgmin; + + govSt=zeros(nTg,1); + govSt(tgovMaxDiff>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==1),1)=Ttgmax(govSt==1,1); + Pm(tgIdx(govSt==-1),1)=Ttgmin(govSt==-1,1); +else + tgovg=zeros(0,nlvl+1); + tgovm=zeros(0,nlvl+1); + Tmech=zeros(0,nlvl+1); +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(:,1)=f0; +synTag=zeros(nbus,1); +synTag(syn(:,1))=1:nSyn; +numSynOnBus=accumarray(syn(:,1),1,[nbus,1]); +dpgTag=ones(nbus,1); +for islIdx=1:nIslands + busIsland=find(islands==islIdx); + synTagIsland=synTag(busIsland); + wIsland=w(synTagIsland(synTagIsland~=0),1); + if ~isempty(wIsland) + f(busIsland,1)=mean(wIsland); % note that here the freq can be different + dpgTag(busIsland)=0; + end +end + +%AGC part % khuang 8 JUL +if ~isempty(agc) + agcExt=zeros(nbus,size(agc,2)); + agcExt(agc(:,1),:)=agc; + dpg=zeros(nbus,nlvl+1); + dpg(:,1)=dpg0; + fdk=agcExt(:,2)+agcExt(:,3); %1/R+D +else + dpg=zeros(nbus,nlvl+1); + fdk=zeros(nbus,1); +end + +% this is for long term dynamic, it seems that not considered here % khuang 8 JUL +if ~isempty(cac)&&~isempty(cluster) + +else + qplt=zeros(0,nlvl+1); + vg=zeros(0,nlvl+1); +end + +% freq relevant part induced by AGC % khuang 8 JUL +FreqReal=sparse(1:nbus,1:nbus,-freqKeptTag.*fdk.*E0,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); + +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); + +% Counting influence of ZIP load into Y matrix. % khuang 8 JUL +if ~isempty(zip) + 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); + Y21=Y21-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,3),[nbus,1]),nbus,nbus); + Y22=Y22-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,4),[nbus,1]),nbus,nbus); +end +YLHS=[Y11,Y12;Y21,Y22]; + +% Counting influence of Motors into small Y matrix % khuang 8 JUL +if ~isempty(ind) + 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(:,2),nbus,nbus),sparse(1:nbus,1:nbus,LHS_MatInd_Bus_sqz(:,4),nbus,nbus)]; +end + +% Counting influence of generators into small Y matrix % khuang 8 JUL +if ~isempty(syn) + YLHS=YLHS+MatGCD; +end + +idxNonSw=find(busType~=2); +idxNonSwxD=find(fswTagxD==0); +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]),... + [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)];... + C0M(ipv,idxNonSw),D0M(ipv,idxNonSw),sparse(npv,2*npq+3*npv+nFreqKept);... + E0M(idxNonSw,idxNonSw),-F0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),-D0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);... + F0M(idxNonSw,idxNonSw),E0M(idxNonSw,idxNonSw),D0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);... + sparse(sum(freqKeptTagxRef),size(idxNonSw,1)+size(idxNonSw,1)+2*npq+3*npv),Freq2freq(freqKeptTagxRef==1,freqKeptTag==1);... + sparse(size(idxNonSwD,1),size(idxNonSw,1)),sparse(1:size(idxNonSwD,1),idxNonSwD,ones(size(idxNonSwD,1),1),size(idxNonSwD,1),size(idxNonSw,1)),sparse(size(idxNonSwD,1),2*npq+3*npv+nFreqKept)]; + +% if nbus<=500 +% [L_LHS_mat,U_LHS_mat,p_LHS_mat]=lu(LHS_mat,'vector'); +% 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'); + +if useLU + if isempty(SysPara.p_amd) + p_amd = colamd (LHS_mat) ; + save([SysPara.iden,'.mat'],'p_amd'); + else + p_amd=SysPara.p_amd; + end + MxI = speye (size(LHS_mat)) ; + MxQ = MxI (:, p_amd) ; + if IS_OCTAVE + [MxL,MxU,MxP,MxQx] = lu (LHS_mat*MxQ) ; + else + [MxL,MxU,MxP] = lu (LHS_mat*MxQ) ; + 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 + + % 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); + seq2p=getseq(i+1,2); + seq3=getseq(i,3); + idxSeq2=sum(seq2==i,2); + idxSeq2x=sum(seq2(:,2)==i,2); + idxSeq2p=sum(seq2p>=i,2); + idxSeq3=sum(seq3==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,:); + seq2x=seq2(idxSeq2x==0,:); + seq2m=getseq(i-1,2); + seq2mm=getseq(i-2,2); + + RHSILr=zeros(nbus,1); + RHSILi=zeros(nbus,1); + + % This part is for induction motor % khuang 8 JUL + 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)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,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+... + % (T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2)).*Rm./R2; + + % s(:,i+1)=(Rind0.*(T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2))... + % -real(sum(IR(:,seq2m(:,1)+1).*conj(IR(:,seq2m(:,2)+1)),2)).*R2-2*Hm.*sum(repmat(seq2R(:,1)',nInd,1).*s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2))... + % ./(2*Hm.*s(:,1)*i); + % if i>=2 + % s(:,i+1)=s(:,i+1)+... + % Rind1.*(T0.*s(:,i-1)+T1.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2)+T2.*sum(s(:,seq3mm(:,1)+1).*s(:,seq3mm(:,2)+1).*s(:,seq3mm(:,3)+1),2))... + % ./(2*Hm.*s(:,1)*i); + % 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); + if i>=2 + s(:,i+1)=s(:,i+1)+... + Rind1.*(T1.*s(:,i-1)+T2.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2))... + ./(2*Hm*i); + end + if i==1 + s(:,i+1)=s(:,i+1)+Rind0.*T0./(2*Hm*i); + end + if i==2 + s(:,i+1)=s(:,i+1)+Rind1.*T0./(2*Hm*i); + end + % for dynamic model, Right hand side vector is required a update. % khuang 8 JUL + addenRhs=Vm(:,1).*s(:,i+1)-1j*X2.*IR(:,1).*s(:,i+1); + + % rhsBus=zeros(2,nInd); + % for j=1:nInd + % rhsBus(:,j)=RHS_C_Shr{j}*[real(rhsM(j)+addenRhs(j));imag(rhsM(j)+addenRhs(j));0;0]; + % end + + % count the influence of dynamic of slip into rigt hand side + % vector.% khuang 8 JUL + 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)]'; + + %accumulate currents IL.% khuang 8 JUL + RHSILr=accumarray(indIdx,rhsBus(1,:)',[nbus,1]); + RHSILi=accumarray(indIdx,rhsBus(2,:)',[nbus,1]); + + % rhsBus=zeros(5,nInd); + % rhsM=sum(Vm(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2); + % rhsImod=Rind1.*(T1.*s(:,i)+T2.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2))+Rind0.*T2.*sum(s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-... + % real(sum(V(indIdx,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2))+... + % real(sum(IL(:,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2).*Z1); + % if i==1 + % rhsImod=rhsImod+Rind1.*T0; + % end + % rhsIL=V(indIdx,i).*Yeind1-IL(:,i).*Ye1ind1; + % for j=1:nInd + % rhsBus(:,j)=squeeze(RHS_C_Shr(j,:,:))*[real(rhsM(j));imag(rhsM(j));rhsImod(j);real(rhsIL(j));imag(rhsIL(j))]; + % end + % RHSILr=accumarray(indIdx,rhsBus(3,:)',[nbus,1]); + % RHSILi=accumarray(indIdx,rhsBus(4,:)',[nbus,1]); + end + + % strat update ZIP load into currents.% khuang 8 JUL + RHSIiLr=zeros(nbus,1); + RHSIiLi=zeros(nbus,1); + if ~isempty(zip) + RHS_BZip=(real(sum(V(zipIdx,seq2R(:,1)+1).*conj(V(zipIdx,seq2R(:,2)+1)),2))-sum(BiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2))./Bi0/2; + RHZ_BIConv=sum(IiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2); + RHSILr_full=Rzip1.*(JI.*real(V(zipIdx,i))-KI.*imag(V(zipIdx,i)))./Bi0-real(RHZ_BIConv)./Bi0-Ji0L.*RHS_BZip./Bi0; + RHSILi_full=Rzip1.*(KI.*real(V(zipIdx,i))+JI.*imag(V(zipIdx,i)))./Bi0-imag(RHZ_BIConv)./Bi0-Ki0L.*RHS_BZip./Bi0; + RHSIiLr=accumarray(zipIdx,RHSILr_full,[nbus,1]); + RHSIiLi=accumarray(zipIdx,RHSILi_full,[nbus,1]); + end + + % Start update generators.% khuang 8 JUL + RHSIGr=zeros(nbus,1); + RHSIGi=zeros(nbus,1); + if ~isempty(syn) + RhsEd=zeros(nSyn,1); + RhsEq=zeros(nSyn,1); + IGdAdd=zeros(nSyn,1); + IGqAdd=zeros(nSyn,1); + % select different models for generators.% khuang 8 JUL + if modelTag(8)>0 + d(modSyn==8,i+1)=(wgb(modSyn==8).*w(modSyn==8,i))/i; + w(modSyn==8,i+1)=(Pm(modSyn==8,i)-... + (sum(psid(modSyn==8,seq2m(:,1)+1).*IGq(modSyn==8,seq2m(:,2)+1),2)-sum(psiq(modSyn==8,seq2m(:,1)+1).*IGd(modSyn==8,seq2m(:,2)+1),2))-... + Dg(modSyn==8).*w(modSyn==8,i))./Mg(modSyn==8)/i; + psid(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGd(modSyn==8,i)+psiq(modSyn==8,i)+VGd(modSyn==8,i))/i; + psiq(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGq(modSyn==8,i)-psid(modSyn==8,i)+VGq(modSyn==8,i))/i; + eq1(modSyn==8,i+1)=(-eq1(modSyn==8,i)-(Xgd(modSyn==8)-Xgd1(modSyn==8)-gammad(modSyn==8)).*IGd(modSyn==8,i)+(1-TgAA(modSyn==8)./Tgd1(modSyn==8)).*Ef(modSyn==8,i))./Tgd1(modSyn==8)/i; + ed1(modSyn==8,i+1)=(-ed1(modSyn==8,i)+(Xgq(modSyn==8)-Xgq1(modSyn==8)-gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq1(modSyn==8)/i; + eq2(modSyn==8,i+1)=(-eq2(modSyn==8,i)+eq1(modSyn==8,i)-(Xgd1(modSyn==8)-Xgd2(modSyn==8)+gammad(modSyn==8)).*IGd(modSyn==8,i)+TgAA(modSyn==8)./Tgd1(modSyn==8).*Ef(modSyn==8,i))./Tgd2(modSyn==8)/i; + ed2(modSyn==8,i+1)=(-ed2(modSyn==8,i)+ed1(modSyn==8,i)+(Xgq1(modSyn==8)-Xgq2(modSyn==8)+gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq2(modSyn==8)/i; + IGdAdd(modSyn==8)=(eq2(modSyn==8,i+1)-psid(modSyn==8,i+1))./Xgd2(modSyn==8); + IGqAdd(modSyn==8)=(-ed2(modSyn==8,i+1)-psiq(modSyn==8,i+1))./Xgq2(modSyn==8); + end + if modelTag(6)>0 + d(modSyn==6,i+1)=(wgb(modSyn==6).*w(modSyn==6,i))/i; + w(modSyn==6,i+1)=(Pm(modSyn==6,i)-... + (sum(VGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(VGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)+... + Rga(modSyn==6).*(sum(IGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(IGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)))-... + Dg(modSyn==6).*w(modSyn==6,i))./Mg(modSyn==6)/i; + eq1(modSyn==6,i+1)=(-eq1(modSyn==6,i)-(Xgd(modSyn==6)-Xgd1(modSyn==6)-gammad(modSyn==6)).*IGd(modSyn==6,i)+(1-TgAA(modSyn==6)./Tgd1(modSyn==6)).*Ef(modSyn==6,i))./Tgd1(modSyn==6)/i; + ed1(modSyn==6,i+1)=(-ed1(modSyn==6,i)+(Xgq(modSyn==6)-Xgq1(modSyn==6)-gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq1(modSyn==6)/i; + eq2(modSyn==6,i+1)=(-eq2(modSyn==6,i)+eq1(modSyn==6,i)-(Xgd1(modSyn==6)-Xgd2(modSyn==6)+gammad(modSyn==6)).*IGd(modSyn==6,i)+TgAA(modSyn==6)./Tgd1(modSyn==6).*Ef(modSyn==6,i))./Tgd2(modSyn==6)/i; + ed2(modSyn==6,i+1)=(-ed2(modSyn==6,i)+ed1(modSyn==6,i)+(Xgq1(modSyn==6)-Xgq2(modSyn==6)+gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq2(modSyn==6)/i; + RhsEd(modSyn==6)=ed2(modSyn==6,i+1); + RhsEq(modSyn==6)=eq2(modSyn==6,i+1); + end + if modelTag(5)>0 + d(modSyn==5,i+1)=(wgb(modSyn==5).*w(modSyn==5,i))/i; + w(modSyn==5,i+1)=(Pm(modSyn==5,i)-... + (sum(VGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(VGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)+... + Rga(modSyn==5).*(sum(IGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(IGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)))-... + Dg(modSyn==5).*w(modSyn==5,i))./Mg(modSyn==5)/i; + eq1(modSyn==5,i+1)=(-eq1(modSyn==5,i)-(Xgd(modSyn==5)-Xgd1(modSyn==5)-gammad(modSyn==5)).*IGd(modSyn==5,i)+(1-TgAA(modSyn==5)./Tgd1(modSyn==5)).*Ef(modSyn==5,i))./Tgd1(modSyn==5)/i; + eq2(modSyn==5,i+1)=(-eq2(modSyn==5,i)+eq1(modSyn==5,i)-(Xgd1(modSyn==5)-Xgd2(modSyn==5)+gammad(modSyn==5)).*IGd(modSyn==5,i)+TgAA(modSyn==5)./Tgd1(modSyn==5).*Ef(modSyn==5,i))./Tgd2(modSyn==5)/i; + ed2(modSyn==5,i+1)=(-ed2(modSyn==5,i)+(Xgq(modSyn==5)-Xgq2(modSyn==5)).*IGq(modSyn==5,i))./Tgq2(modSyn==5)/i; + RhsEd(modSyn==5)=ed2(modSyn==5,i+1); + RhsEq(modSyn==5)=eq2(modSyn==5,i+1); + end + if modelTag(4)>0 + d(modSyn==4,i+1)=(wgb(modSyn==4).*w(modSyn==4,i))/i; + w(modSyn==4,i+1)=(Pm(modSyn==4,i)-... + (sum(VGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(VGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)+... + Rga(modSyn==4).*(sum(IGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(IGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)))-... + Dg(modSyn==4).*w(modSyn==4,i))./Mg(modSyn==4)/i; + eq1(modSyn==4,i+1)=(-eq1(modSyn==4,i)-(Xgd(modSyn==4)-Xgd1(modSyn==4)).*IGd(modSyn==4,i)+Ef(modSyn==4,i))./Tgd1(modSyn==4)/i; + ed1(modSyn==4,i+1)=(-ed1(modSyn==4,i)+(Xgq(modSyn==4)-Xgq1(modSyn==4)).*IGq(modSyn==4,i))./Tgq1(modSyn==4)/i; + RhsEd(modSyn==4)=ed1(modSyn==4,i+1); + RhsEq(modSyn==4)=eq1(modSyn==4,i+1); + end + if modelTag(3)>0 + d(modSyn==3,i+1)=(wgb(modSyn==3).*w(modSyn==3,i))/i; + w(modSyn==3,i+1)=(Pm(modSyn==3,i)-... + (sum(VGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(VGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)+... + Rga(modSyn==3).*(sum(IGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(IGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)))-... + Dg(modSyn==3).*w(modSyn==3,i))./Mg(modSyn==3)/i; + eq1(modSyn==3,i+1)=(-eq1(modSyn==3,i)-(Xgd(modSyn==3)-Xgd1(modSyn==3)).*IGd(modSyn==3,i)+Ef(modSyn==3,i))./Tgd1(modSyn==3)/i; + RhsEd(modSyn==3)=0; + RhsEq(modSyn==3)=eq1(modSyn==3,i+1); + end + if modelTag(2)>0 + d(modSyn==2,i+1)=(wgb(modSyn==2).*w(modSyn==2,i))/i; + w(modSyn==2,i+1)=(Pm(modSyn==2,i)-... + (sum(VGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(VGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)+... + Rga(modSyn==2).*(sum(IGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(IGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)))-... + Dg(modSyn==2).*w(modSyn==2,i))./Mg(modSyn==2)/i; + RhsEd(modSyn==2)=0; + RhsEq(modSyn==2)=eq1(modSyn==2,i+1); + end + % this part may be different from DT.% khuang 8 JUL +% AG0=cosp(:,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 +% 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); +% end +% if taylorN>=3 +% AG0=AG0+cosp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2); +% BG0=BG0+sinp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2); +% end +% if taylorN>=4 +% seq4=getseq(i,4); +% AG0=AG0+cosp(:,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 + + % now Sd store high order terms of sin(dta) and Cd for cos(dta), + % 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);% Vd*cosdta% khuang 8 JUL + 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);% Vd*sindta% khuang 8 JUL + 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);% similar, for currents% khuang 8 JUL + KCr=sum(Cd(:,seq2x(:,1)+1).*KG(:,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); + + RHSIGxr=-(MatsRs(:,1).*(-VGdSr-VGqCr)+MatsRs(:,2).*(VGdCr-VGqSr))+... + (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))+... + (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]); + RHSIGi=accumarray(synIdx,RHSIGxi,[nbus,1]); + end + % update exciter, 3 state variables.% khuang 8 JUL + if ~isempty(exc) + 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; + Vavrf(:,i+1)=((vavrf0.*Vmag(synIdx(excIdx),i)+... + sum(Vavrr(:,seq2m(:,1)+1).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2)+... + muavr0.*Tavr1./Tavr2.*sum((Vavrref(:,seq2m(:,1)+1)-Vavrm(:,seq2m(:,1)+1)).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2))./Vavr0-Vavrf(:,i))./Tavre/i; + Ef(excIdx(avrSt==-1),i+1)=0; + Ef(excIdx(avrSt== 1),i+1)=0; + Ef(excIdx(avrSt== 0),i+1)=Vavrf(avrSt==0,i+1); + end + + % update agc, one state variables.% khuang 8 JUL + if ~isempty(agc) + dpg(:,i+1)=-f(:,i).*agcExt(:,4)/i; + for islIdx=1:nIslands + busIsland=find(islands==islIdx); + synTagIsland=synTag(busIsland); + wIsland=w(synTagIsland(synTagIsland~=0),i+1); + if ~isempty(wIsland) + f(busIsland,i+1)=mean(wIsland); % note that here the freq can be different + end + end % TODO: steady-state model + + % update generator participation part from agc.% khuang 8 JUL + if ~isempty(syn) %dynamic model (synchronous generators) + if ~isempty(tg) + Tmech(:,i+1)=Tmech(:,i+1)+dpg(syn(tg(:,1),1),i+1)./numSynOnBus(syn(tg(:,1),1)); + end + Pm(:,i+1)=Pm(:,i+1)+dpg(syn(:,1),i+1)./numSynOnBus(syn(:,1)); + end + end + % update Turbine, 2 state variables.% khuang 8 JUL + if ~isempty(tg) + 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); + + Pm(tgIdx(govSt==0),i+1)=tgovm(govSt==0,i+1); + Pm(tgIdx(govSt==1),i+1)=0; + Pm(tgIdx(govSt==-1),i+1)=0; + end + + % HEM Body + RHS1=sum((-P(:,seq2(:,1)+1)+1j*(Q(:,seq2(:,1)+1)+Qxtra(:,seq2(:,1)+1))).*conj(W(:,seq2(:,2)+1)),2)+... + freqKeptTag.*sum(-dpg(:,seq2(:,1)+1).*conj(W(:,seq2(:,2)+1)),2)+... + freqKeptTag.*fdk.*sum(f(:,seq2R(:,1)+1).*conj(W(:,seq2R(:,2)+1)),2)+Ysh1.*V(:,i)+Ytr1*V(:,i); + 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); + + + if i==1 + RHS2=RHS2+0.5*VspSq2(:,2); + end + + compactRHS1=RHS1(busType~=2); + 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);... + imag(compactRHS1)+RHSILi(busType~=2)+RHSIiLi(busType~=2)-RHSIGi(busType~=2);... + RHS2(ipv);... + real(RHS3(busType~=2));... + imag(RHS3(busType~=2));... + zeros(sum(freqKeptTagxRef),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 IS_OCTAVE + x = real(MxQ * MxQx* (MxU \ (MxL \ (MxP * RHS)))) ; + else + x =real( MxQ * (MxU \ (MxL \ (MxP * RHS)))) ; + end + else + x=real(LHS_mat\RHS); + end + + % x= [V;W;Q_pv;f] + xC=real(V(:,i+1)); + xD=imag(V(:,i+1)); + xC(idxNonSw)=x(1:(npq+npv)); + xD(idxNonSw)=x(((npq+npv)+1):(2*(npq+npv))); + V(:,i+1)=xC+1j*xD; + W(busType~=2,i+1)=x((2*(npq+npv)+1):(3*(npq+npv)))+... + 1j*x((3*(npq+npv)+1):(4*(npq+npv))); + Q(ipv,i+1)=x((4*(npq+npv)+1):(4*(npq+npv)+npv)); + f(freqKeptTag==1,i+1)=x((4*(npq+npv)+npv+1):end); + + 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) + % for j=1:nInd + % tempIL=squeeze(LHS_MatInd_Shr(j,:,:))*[real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]+rhsBus(:,j); + % tempIRs=-LHS_MatInd_Shr2{j}*[tempIL;real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]; + % IL(j,i+1)=tempIL(1)+1j*tempIL(2); + % IR(j,i+1)=tempIRs(1)+1j*tempIRs(2); + % Vm(j,i+1)=V(indIdx(j),i+1)-IL(j,i+1)*Z1(j); + % end + tempILvr=LHS_MatInd_Shr_sqz(:,1).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,3).*imag(V(indIdx,i+1))+rhsBus(1,:)'; + tempILvi=LHS_MatInd_Shr_sqz(:,2).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,4).*imag(V(indIdx,i+1))+rhsBus(2,:)'; + tempIRsvr=-sum(LHS_MatInd_Shr2_sqz(:,[1,3,5,7]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2); + tempIRsvi=-sum(LHS_MatInd_Shr2_sqz(:,[2,4,6,8]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2); + IL(:,i+1)=tempILvr+1j*tempILvi; + IR(:,i+1)=tempIRsvr+1j*tempIRsvi; + Vm(:,i+1)=V(indIdx,i+1)-IL(:,i+1).*Z1; + end + + % now update the Algebric variables for ZIP loads.% khuang 8 JUL + if ~isempty(zip) + IiL(:,i+1)=(LHS_MatZip(:,1)+1j*LHS_MatZip(:,3)).*real(V(zipIdx,i+1))+(LHS_MatZip(:,2)+1j*LHS_MatZip(:,4)).*imag(V(zipIdx,i+1))+(RHSILr_full+1j*RHSILi_full); + BiL(:,i+1)=Mat_BZip(:,1).*real(V(zipIdx,i+1))+Mat_BZip(:,2).*imag(V(zipIdx,i+1))+RHS_BZip; + end + + % now update the Algebric variables for Generators: Vd,Vq,Id,Iq.% khuang 8 JUL + if ~isempty(syn) + 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; + IGd(:,i+1)=JSr-KCr+sind.*JG(:,i+1)-cosd.*KG(:,i+1); + IGq(:,i+1)=JCr+KSr+cosd.*JG(:,i+1)+sind.*KG(:,i+1); + tempVGC=real(V(synIdx,i+1))-VGdSr-VGqCr; + tempVGD=imag(V(synIdx,i+1))+VGdCr-VGqSr; + VGd(:,i+1)=sind.*tempVGC-cosd.*tempVGD; + VGq(:,i+1)=cosd.*tempVGC+sind.*tempVGD; + end +end + +% Output value: coefficients for every order.% khuang 8 JUL +Q=real(Q); +s=real(s); +d=real(d); +w=real(w); +eq1=real(eq1); +eq2=real(eq2); +ed1=real(ed1); +ed2=real(ed2); +psid=real(psid); +psiq=real(psiq); +Pm=real(Pm); +Ef=real(Ef); +Vavrm=real(Vavrm); +Vavrr=real(Vavrr); +Vavrf=real(Vavrf); +Vavrref=real(Vavrref); +tgovg=real(tgovg); +tgovm=real(tgovm); +Tmech=real(Tmech); +f=real(f); +dpg=real(dpg); +qplt=real(qplt); +vg=real(vg); + +if ~isempty(exc) + avr={Vavrm,Vavrr,Vavrf}; +end + +if ~isempty(tg) + gov={tgovg,tgovm}; +end +end \ No newline at end of file diff --git a/internal/dtmMachinePFmultiStageDyn.m b/internal/dtmMachinePFmultiStageDyn.m new file mode 100644 index 0000000..a0c934f --- /dev/null +++ b/internal/dtmMachinePFmultiStageDyn.m @@ -0,0 +1,1084 @@ +function [t,stateCurve,finalAlpha,alphaList,diffList,exitFlag]=hemMachinePFmultiStageDyn(SimData,SysData,SysPara,x0) +% General interface for invoking dynamic simulation for a segment of time +% +% FUNCTION generalDynSimulation +% +% Author: Rui Yao +% +% Copyright (C) 2021, UChicago Argonne, LLC. All rights reserved. +% +% OPEN SOURCE LICENSE +% +% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +% +% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +% +% +% ****************************************************************************************************** +% DISCLAIMER +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% *************************************************************************************************** +% +% INPUT +% SimData - Simulation parameters +% SysData - System data for simulation +% SysPara - Parameters representing the events happening in the system +% x0 - Initial system state +% +% OUTPUT +% t - A list of time points (starting with 0) +% stateCurve - A list of states in the order of time +% finalAlpha - The ending length of this segment of simulation +% alphaList - Equivalent to t +% diffList - A list of errors +% exitFlag - +% 0 - Success and normally exit +% -1 - Fail to finish (due to computation errors or failures) +% 1 - System in absolute steady state +% 2 - Generator transients faded away +% 3 - Suggest using error reduction mode +% +global TaskID TaskNum + +DynSimFlag=getDynSimFlags(); +exitFlag=DynSimFlag.NORMAL; +% +[bus,sw,pv,pq,shunt,line,ind,zip,syn,exc,tg,agc,cac,cluster]=unfoldSysData(SysData); +[nState,idxs]... + =getIndexDyn(SysData); +[nIslands,islands,refs]=searchIslands(bus(:,1),line(:,1:2)); +nbus=size(bus,1); +nline=size(line,1); +[V0,Q0,s0,d0,w0,eq10,eq20,ed10,ed20,psid0,psiq0,Pm0,Ef0,Vavrm0,Vavrr0,Vavrf0,Vavrref0,tgovg0,tgovm0,Tmech0,f0,dpg0,qplt0,vg0]=unfoldX(x0,SysData); +[maxAlpha,segAlpha,dAlpha,nlvl,taylorN,alphaTol,diffTol,diffTolMax,~,varOpt]=unfoldSimData(SimData); +alphaTolOrig=alphaTol; +[pqIncr,pvIncr,Rind0,Rind1,Reind0,Reind1,Rzip0,Rzip1,Ytr0,Ytr1,Ysh0,Ysh1,VspSq2,~,~,~,~,Tmech1,Varref1,Ef1,Pm1,Eq11]=unfoldSysPara(SysPara); + +busType=zeros(nbus,1); +busType(pv(:,1))=1; +busType(sw(:,1))=2; +% zip(busType(zip(:,1))~=0,10)=0; + +agcExt=zeros(nbus,4); +if ~isempty(agc) + agcExt(agc(:,1),:)=agc; +end +agcRByIsland=accumarray(islands,agcExt(:,4),[nIslands,1]); +isAgcActiveByIsland=agcRByIsland~=0; +isFreqApchZero=isAgcActiveByIsland(islands); +% +pqx=pq; +pvx=pv; +shuntx=shunt; +indx=ind; +zipx=zip; +% +V0x=V0; +s0x=s0; +Q0x=Q0; +Rzip0x=Rzip0; +Rind0x=Rind0; +Reind0x=Reind0; +Ysh0x=Ysh0; +Ytr0x=Ytr0; +Vsp0x=real(V0.*conj(V0)); +d0x=d0;w0x=w0;eq10x=eq10;eq20x=eq20;ed10x=ed10;ed20x=ed20;psid0x=psid0;psiq0x=psiq0; +Pm0x=Pm0;Ef0x=Ef0;Vavrm0x=Vavrm0;Vavrr0x=Vavrr0;Vavrf0x=Vavrf0;Vavrref0x=Vavrref0; +tgovg0x=tgovg0;tgovm0x=tgovm0;Tmech0x=Tmech0; +f0x=f0;dpg0x=dpg0;qplt0x=qplt0;vg0x=vg0; + +VSol=V0; +QSol=Q0; +sSol=s0; +dSol=d0; +wSol=w0; +eq1Sol=eq10; +eq2Sol=eq20; +ed1Sol=ed10; +ed2Sol=ed20; +psiqSol=psiq0; +psidSol=psid0; +PmSol=Pm0; +EfSol=Ef0; +VavrmSol=Vavrm0; +VavrrSol=Vavrr0; +VavrfSol=Vavrf0; +VavrrefSol=Vavrref0; +tgovgSol=tgovg0; +tgovmSol=tgovm0; +TmechSol=Tmech0; +fSol=f0; +dpgSol=dpg0; +qpltSol=qplt0; +vgSol=vg0; + +t=0; + +alphaConfirm=0; + +busType=zeros(nbus,1); +if ~isempty(pv);busType(pv(:,1))=1;end +if ~isempty(sw);busType(sw(:,1))=2;end + +nSyn=size(syn,1); +nInd=size(ind,1); +nZip=size(zip,1); + +% vc=zeros(nbus,1); +% qc=zeros(size(Q0,1),1); +% sc=zeros(nInd,1);dsc=zeros(nInd,1); +% dc=zeros(nSyn,1);ddc=zeros(nSyn,1); +% wc=zeros(nSyn,1);dwc=zeros(nSyn,1); +% eq1c=zeros(nSyn,1);deq1c=zeros(nSyn,1); +% eq2c=zeros(nSyn,1);deq2c=zeros(nSyn,1); +% ed1c=zeros(nSyn,1);ded1c=zeros(nSyn,1); +% ed2c=zeros(nSyn,1);ded2c=zeros(nSyn,1); +% psidc=zeros(nSyn,1);dpsidc=zeros(nSyn,1); +% psiqc=zeros(nSyn,1);dpsiqc=zeros(nSyn,1); + +alphaList=0; +ratiob4 = NaN; +diffList=0; +diffMul=1.02; +alphaPreMult=0.91; +noMoveCnt=0; +absNoMoveCnt=0; +maxNoMoveCnt=3; +maxAbsNoMoveCnt=5; + +% generate unique identifier (colamd) +if exist('TaskID','var')&&exist('TaskNum','var')&&~isempty(TaskID)&&~isempty(TaskNum) +taskTag=num2str(TaskID(1)); +else +taskTag=''; +end + +iden=[datestr(clock,30),'_',taskTag,'K',num2str(randi([100000,899999])),'_init']; + +jac=getJacobianMod(nbus,nline,bus,sw,pvx,pqx,shuntx,line,zipx,indx,s0,V0,ones(size(ind,1),1)); +initJacCond=condest(jac); + +if ~isempty(varOpt) && isfield(varOpt,'allowModelSwitch') + allowModelSwitch=varOpt.allowModelSwitch; +else + allowModelSwitch=0; +end + +if ~isempty(varOpt) && isfield(varOpt,'absT') + useAbsT=1; + absT=varOpt.absT; +else + useAbsT=0; +end + +if ~isempty(varOpt) && isfield(varOpt,'allowExit1') + allowExit1=varOpt.allowExit1; +else + allowExit1=1; +end + +if ~isempty(varOpt) && isfield(varOpt,'useDiffCtrl') && isfield(varOpt,'diffTolCtrl') + useDiffCtrl=varOpt.useDiffCtrl; + diffTolCtrl=varOpt.diffTolCtrl; +else + useDiffCtrl=0; + diffTolCtrl=0; +end + +if ~isempty(varOpt) && isfield(varOpt,'useNomoveCtrl') + useNomoveCtrl=varOpt.useNomoveCtrl; +else + useNomoveCtrl=1; +end +alphab4 = NaN; +while alphaConfirmmax([diffTol/2,diffTolCtrl]) % refine initial point + SysParaNR=foldSysPara(zeros(size(pqx,1),2),zeros(size(pvx,1),1),ones(nInd,1),zeros(nInd,1),ones(nInd,1),zeros(nInd,1),ones(nZip,1),zeros(nZip,1),Ytr0x,0*Ytr0x,Ysh0x,0*Ysh0x,[Vsp0x,zeros(size(Vsp0x))],MatGV0,[],MatGRhs0,[]); + SimDataNR=foldSimData(maxAlpha,segAlpha,dAlpha,nlvl,taylorN,alphaTol,diffTol/10,diffTolMax,[]); + [stateNew,flag,diffRec,loop]=solveAlgebraicNR(SimDataNR,SysDataInit,SysParaNR,xxx,xxx); + if flag==0 + V0x=stateNew(idxs.vIdx); + Q0x=stateNew(idxs.qIdx); + f0x=stateNew(idxs.fIdx); + end + end + end + + xx=foldX(SysDatax,V0x,Q0x,s0x,d0x,w0x,eq10x,eq20x,ed10x,ed20x,psid0x,psiq0x,Pm0x,Ef0x,Vavrm0x,Vavrr0x,Vavrf0x,Vavrref0x,tgovg0x,tgovm0x,Tmech0x,f0x,dpg0x,qplt0x,vg0x); + pqIncrx=pqIncr; + if size(pqIncrx,2)>=4 + pqIncrx(:,1:2)=pqIncr(:,1:2)+pqIncr(:,3:4)*2*alphaConfirm; + end + SysParax=foldSysPara(pqIncrx,pvIncr,Rind0x,Rind1,Reind0x,Reind1,Rzip0x,Rzip1,Ytr0x,Ytr1,Ysh0x,Ysh1,[Vsp0x,VspSq2(:,2)],[],[],[],[],Tmech1,Varref1,Ef1,Pm1,Eq11); + SysParax.iden=iden; + if exist([iden,'.mat'],'file') + if ~exist('p_amd','var') + load([iden,'.mat']); + end + SysParax.p_amd=p_amd; + else + SysParax.p_amd=[]; + end + SysParax.nIslands=nIslands; + SysParax.islands=islands; + SysParax.refs=refs; + + [V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrf,Vavrref,tgovg,tgovm,Tmech,f,dpg,qplt,vg]=... + dtmMachinePFSalientcontinueDyn(SimData,SysDatax,SysParax,xx); + + + + + %check the dirivative of every state variables w.r.t. time + ds=s(:,2:end).*repmat(1:(size(s,2)-1),size(s,1),1); + dd=d(:,2:end).*repmat(1:(size(d,2)-1),size(d,1),1); + dw=w(:,2:end).*repmat(1:(size(w,2)-1),size(w,1),1); + deq1=eq1(:,2:end).*repmat(1:(size(eq1,2)-1),size(eq1,1),1); + deq2=eq2(:,2:end).*repmat(1:(size(eq2,2)-1),size(eq2,1),1); + ded1=ed1(:,2:end).*repmat(1:(size(ed1,2)-1),size(ed1,1),1); + ded2=ed2(:,2:end).*repmat(1:(size(ed2,2)-1),size(ed2,1),1); + dpsid=psid(:,2:end).*repmat(1:(size(psid,2)-1),size(psid,1),1); + dpsiq=psiq(:,2:end).*repmat(1:(size(psiq,2)-1),size(psiq,1),1); + dPm=Pm(:,2:end).*repmat(1:(size(Pm,2)-1),size(Pm,1),1); + + dVavrm=Vavrm(:,2:end).*repmat(1:(size(Vavrm,2)-1),size(Vavrm,1),1); + dVavrr=Vavrr(:,2:end).*repmat(1:(size(Vavrr,2)-1),size(Vavrr,1),1); + dVavrf=Vavrf(:,2:end).*repmat(1:(size(Vavrf,2)-1),size(Vavrf,1),1); + dtgovg=tgovg(:,2:end).*repmat(1:(size(tgovg,2)-1),size(tgovg,1),1); + dTmech=Tmech(:,2:end).*repmat(1:(size(Tmech,2)-1),size(Tmech,1),1); + + df=f(:,2:end).*repmat(1:(size(f,2)-1),size(f,1),1); %agc + ddpg=dpg(:,2:end).*repmat(1:(size(dpg,2)-1),size(dpg,1),1); %agc + % TODO: avc +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% main part of pade +% [Va,Vb]=getPadeApproxHelm(V,0); +% idxNanV=find(isnan(Va(:,1))|isnan(Vb(:,1)));Va(idxNanV,:)=0;Vb(idxNanV,:)=0;Va(idxNanV,1:3)=V(idxNanV,1:3); +% [Qa,Qb]=getPadeApproxHelm(Q,find(busType==0)); +% idxNanQ=find(isnan(Qa(:,1))|isnan(Qb(:,1)));Qa(idxNanQ,:)=0;Qb(idxNanQ,:)=0;Qa(idxNanQ,1:3)=Q(idxNanQ,1:3); +% +% [sa,sb]=getPadeApproxHelm(s,0); +% [dsa,dsb]=getPadeApproxHelm(ds,0); +% [da,db]=getPadeApproxHelm(d,0); +% [dda,ddb]=getPadeApproxHelm(dd,0); +% [wa,wb]=getPadeApproxHelm(w,0); +% [dwa,dwb]=getPadeApproxHelm(dw,0); +% [eq1a,eq1b]=getPadeApproxHelm(eq1,0); +% [deq1a,deq1b]=getPadeApproxHelm(deq1,0); +% [eq2a,eq2b]=getPadeApproxHelm(eq2,0); +% [deq2a,deq2b]=getPadeApproxHelm(deq2,0); +% [ed1a,ed1b]=getPadeApproxHelm(ed1,0); +% [ded1a,ded1b]=getPadeApproxHelm(ded1,0); +% [ed2a,ed2b]=getPadeApproxHelm(ed2,0); +% [ded2a,ded2b]=getPadeApproxHelm(ded2,0); +% [psida,psidb]=getPadeApproxHelm(psid,0); +% [dpsida,dpsidb]=getPadeApproxHelm(dpsid,0); +% [psiqa,psiqb]=getPadeApproxHelm(psiq,0); +% [dpsiqa,dpsiqb]=getPadeApproxHelm(dpsiq,0); +% +% [Efa,Efb]=getPadeApproxHelm(Ef,0); +% [Pma,Pmb]=getPadeApproxHelm(Pm,0); +% [dPma,dPmb]=getPadeApproxHelm(dPm,0); +% [Vavrma,Vavrmb]=getPadeApproxHelm(Vavrm,0); +% [dVavrma,dVavrmb]=getPadeApproxHelm(dVavrm,0); +% [Vavrra,Vavrrb]=getPadeApproxHelm(Vavrr,0); +% [dVavrra,dVavrrb]=getPadeApproxHelm(dVavrr,0); +% [Vavrfa,Vavrfb]=getPadeApproxHelm(Vavrf,0); +% [dVavrfa,dVavrfb]=getPadeApproxHelm(dVavrf,0); +% [Vavrrefa,Vavrrefb]=getPadeApproxHelm(Vavrref,0); +% [tgovga,tgovgb]=getPadeApproxHelm(tgovg,0); +% [dtgovga,dtgovgb]=getPadeApproxHelm(dtgovg,0); +% [tgovma,tgovmb]=getPadeApproxHelm(tgovm,0); +% [Tmecha,Tmechb]=getPadeApproxHelm(Tmech,0); +% [dTmecha,dTmechb]=getPadeApproxHelm(dTmech,0); +% +% [fa,fb]=getPadeApproxHelm(f,0); % agc +% [dfa,dfb]=getPadeApproxHelm(df,0); % agc +% [dpga,dpgb]=getPadeApproxHelm(dpg,0); % agc +% [ddpga,ddpgb]=getPadeApproxHelm(ddpg,0); % agc +% +% [qplta,qpltb]=getPadeApproxHelm(qplt,0); +% [vga,vgb]=getPadeApproxHelm(vg,0); +% main part of pade +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% if ~isempty(ind) +% idxNanS=find(isnan(sa(:,1))|isnan(sb(:,1)));sa(idxNanS,:)=0;sb(idxNanS,:)=0;sa(idxNanS,1:2)=s(idxNanS,1:2); +% idxNandS=find(isnan(dsa(:,1))|isnan(dsb(:,1)));dsa(idxNandS,:)=0;dsb(idxNandS,:)=0;dsa(idxNandS,1:2)=ds(idxNandS,1:2); +% end +% if ~isempty(syn) +% idxNand=find(isnan(da(:,1))|isnan(db(:,1)));da(idxNand,:)=0;db(idxNand,:)=0;da(idxNand,1:2)=d(idxNand,1:2); +% idxNandd=find(isnan(dda(:,1))|isnan(ddb(:,1)));dda(idxNandd,:)=0;ddb(idxNandd,:)=0;dda(idxNandd,1:2)=dd(idxNandd,1:2); +% idxNanw=find(isnan(wa(:,1))|isnan(wb(:,1)));wa(idxNanw,:)=0;wb(idxNanw,:)=0;wa(idxNanw,1:2)=w(idxNanw,1:2); +% idxNandw=find(isnan(dwa(:,1))|isnan(dwb(:,1)));dwa(idxNandw,:)=0;dwb(idxNandw,:)=0;dwa(idxNandw,1:2)=dw(idxNandw,1:2); +% idxNaneq1=find(isnan(eq1a(:,1))|isnan(eq1b(:,1)));eq1a(idxNaneq1,:)=0;eq1b(idxNaneq1,:)=0;eq1a(idxNaneq1,1:2)=eq1(idxNaneq1,1:2); +% idxNandeq1=find(isnan(deq1a(:,1))|isnan(deq1b(:,1)));deq1a(idxNandeq1,:)=0;deq1b(idxNandeq1,:)=0;deq1a(idxNandeq1,1:2)=deq1(idxNandeq1,1:2); +% idxNaneq2=find(isnan(eq2a(:,1))|isnan(eq2b(:,1)));eq2a(idxNaneq2,:)=0;eq2b(idxNaneq2,:)=0;eq2a(idxNaneq2,1:2)=eq2(idxNaneq2,1:2); +% idxNandeq2=find(isnan(deq2a(:,1))|isnan(deq2b(:,1)));deq2a(idxNandeq2,:)=0;deq2b(idxNandeq2,:)=0;deq2a(idxNandeq2,1:2)=deq2(idxNandeq2,1:2); +% idxNaned1=find(isnan(ed1a(:,1))|isnan(ed1b(:,1)));ed1a(idxNaned1,:)=0;ed1b(idxNaned1,:)=0;ed1a(idxNaned1,1:2)=ed1(idxNaned1,1:2); +% idxNanded1=find(isnan(ded1a(:,1))|isnan(ded1b(:,1)));ded1a(idxNanded1,:)=0;ded1b(idxNanded1,:)=0;ded1a(idxNanded1,1:2)=ded1(idxNanded1,1:2); +% idxNaned2=find(isnan(ed2a(:,1))|isnan(ed2b(:,1)));ed2a(idxNaned2,:)=0;ed2b(idxNaned2,:)=0;ed2a(idxNaned2,1:2)=ed2(idxNaned2,1:2); +% idxNanded2=find(isnan(ded2a(:,1))|isnan(ded2b(:,1)));ded2a(idxNanded2,:)=0;ded2b(idxNanded2,:)=0;ded2a(idxNanded2,1:2)=ded2(idxNanded2,1:2); +% idxNanpsid=find(isnan(psida(:,1))|isnan(psidb(:,1)));psida(idxNanpsid,:)=0;psidb(idxNanpsid,:)=0;psida(idxNanpsid,1:2)=psid(idxNanpsid,1:2); +% idxNandpsid=find(isnan(dpsida(:,1))|isnan(dpsidb(:,1)));dpsida(idxNandpsid,:)=0;dpsidb(idxNandpsid,:)=0;dpsida(idxNandpsid,1:2)=dpsid(idxNandpsid,1:2); +% idxNanpsiq=find(isnan(psiqa(:,1))|isnan(psiqb(:,1)));psiqa(idxNanpsiq,:)=0;psiqb(idxNanpsiq,:)=0;psiqa(idxNanpsiq,1:2)=psiq(idxNanpsiq,1:2); +% idxNandpsiq=find(isnan(dpsiqa(:,1))|isnan(dpsiqb(:,1)));dpsiqa(idxNandpsiq,:)=0;dpsiqb(idxNandpsiq,:)=0;dpsiqa(idxNandpsiq,1:2)=dpsiq(idxNandpsiq,1:2); +% idxNanEf=find(isnan(Efa(:,1))|isnan(Efb(:,1)));Efa(idxNanEf,:)=0;Efb(idxNanEf,:)=0;Efa(idxNanEf,1:2)=Ef(idxNanEf,1:2); +% idxNanPm=find(isnan(Pma(:,1))|isnan(Pmb(:,1)));Pma(idxNanPm,:)=0;Pmb(idxNanPm,:)=0;Pma(idxNanPm,1:2)=Pm(idxNanPm,1:2); +% idxNandPm=find(isnan(dPma(:,1))|isnan(dPmb(:,1)));dPma(idxNandPm,:)=0;dPmb(idxNandPm,:)=0;dPma(idxNandPm,1:2)=dPm(idxNandPm,1:2); +% end +% if ~isempty(exc) +% idxNanVavrm=find(isnan(Vavrma(:,1))|isnan(Vavrmb(:,1)));Vavrma(idxNanVavrm,:)=0;Vavrmb(idxNanVavrm,:)=0;Vavrma(idxNanVavrm,1:2)=Vavrm(idxNanVavrm,1:2); +% idxNandVavrm=find(isnan(dVavrma(:,1))|isnan(dVavrmb(:,1)));dVavrma(idxNandVavrm,:)=0;dVavrmb(idxNandVavrm,:)=0;dVavrma(idxNandVavrm,1:2)=dVavrm(idxNandVavrm,1:2); +% idxNanVavrr=find(isnan(Vavrra(:,1))|isnan(Vavrrb(:,1)));Vavrra(idxNanVavrr,:)=0;Vavrrb(idxNanVavrr,:)=0;Vavrra(idxNanVavrr,1:2)=Vavrr(idxNanVavrr,1:2); +% idxNandVavrr=find(isnan(dVavrra(:,1))|isnan(dVavrrb(:,1)));dVavrra(idxNandVavrr,:)=0;dVavrrb(idxNandVavrr,:)=0;dVavrra(idxNandVavrr,1:2)=dVavrr(idxNandVavrr,1:2); +% idxNanVavrf=find(isnan(Vavrfa(:,1))|isnan(Vavrfb(:,1)));Vavrfa(idxNanVavrf,:)=0;Vavrfb(idxNanVavrf,:)=0;Vavrfa(idxNanVavrf,1:2)=Vavrf(idxNanVavrf,1:2); +% idxNandVavrf=find(isnan(dVavrfa(:,1))|isnan(dVavrfb(:,1)));dVavrfa(idxNandVavrf,:)=0;dVavrfb(idxNandVavrf,:)=0;dVavrfa(idxNandVavrf,1:2)=dVavrf(idxNandVavrf,1:2); +% idxNanVavrref=find(isnan(Vavrrefa(:,1))|isnan(Vavrrefb(:,1)));Vavrrefa(idxNanVavrref,:)=0;Vavrrefb(idxNanVavrref,:)=0;Vavrrefa(idxNanVavrref,1:2)=Vavrref(idxNanVavrref,1:2); +% end +% if ~isempty(tg) +% idxNantgovg=find(isnan(tgovga(:,1))|isnan(tgovgb(:,1)));tgovga(idxNantgovg,:)=0;tgovgb(idxNantgovg,:)=0;tgovga(idxNantgovg,1:2)=tgovg(idxNantgovg,1:2); +% idxNandtgovg=find(isnan(dtgovga(:,1))|isnan(dtgovgb(:,1)));dtgovga(idxNandtgovg,:)=0;dtgovgb(idxNandtgovg,:)=0;dtgovga(idxNandtgovg,1:2)=dtgovg(idxNandtgovg,1:2); +% idxNantgovm=find(isnan(tgovma(:,1))|isnan(tgovmb(:,1)));tgovma(idxNantgovm,:)=0;tgovmb(idxNantgovm,:)=0;tgovma(idxNantgovm,1:2)=tgovm(idxNantgovm,1:2); +% idxNanTmech=find(isnan(Tmecha(:,1))|isnan(Tmechb(:,1)));Tmecha(idxNanTmech,:)=0;Tmechb(idxNanTmech,:)=0;Tmecha(idxNanTmech,1:2)=Tmech(idxNanTmech,1:2); +% idxNandTmech=find(isnan(dTmecha(:,1))|isnan(dTmechb(:,1)));dTmecha(idxNandTmech,:)=0;dTmechb(idxNandTmech,:)=0;dTmecha(idxNandTmech,1:2)=dTmech(idxNandTmech,1:2); +% end +% +% idxNanf=find(isnan(fa(:,1))|isnan(fb(:,1)));fa(idxNanf,:)=0;fb(idxNanf,:)=0;fa(idxNanf,1:2)=f(idxNanf,1:2); +% idxNandf=find(isnan(dfa(:,1))|isnan(dfb(:,1)));dfa(idxNandf,:)=0;dfb(idxNandf,:)=0;dfa(idxNandf,1:2)=df(idxNandf,1:2); +% idxNandpg=find(isnan(dpga(:,1))|isnan(dpgb(:,1)));dpga(idxNandpg,:)=0;dpgb(idxNandpg,:)=0;dpga(idxNandpg,1:2)=dpg(idxNandpg,1:2); +% idxNanddpg=find(isnan(ddpga(:,1))|isnan(ddpgb(:,1)));ddpga(idxNanddpg,:)=0;ddpgb(idxNanddpg,:)=0;ddpga(idxNanddpg,1:2)=ddpg(idxNanddpg,1:2); +% +% if ~isempty(cac)&&~isempty(cluster) +% +% end +% if ~isempty(ind) +% idxNanS=find(isnan(sa(:,1)));s(idxNanS,:)=0;sa(idxNanS,1:2)=s(idxNanS,1:2); +% idxNandS=find(isnan(dsa(:,1))|isnan(dsb(:,1)));dsa(idxNandS,:)=0;dsb(idxNandS,:)=0;dsa(idxNandS,1:2)=ds(idxNandS,1:2); +% end +% if ~isempty(syn) +% idxNand=find(isnan(da(:,1))|isnan(db(:,1)));da(idxNand,:)=0;db(idxNand,:)=0;da(idxNand,1:2)=d(idxNand,1:2); +% idxNandd=find(isnan(dda(:,1))|isnan(ddb(:,1)));dda(idxNandd,:)=0;ddb(idxNandd,:)=0;dda(idxNandd,1:2)=dd(idxNandd,1:2); +% idxNanw=find(isnan(wa(:,1))|isnan(wb(:,1)));wa(idxNanw,:)=0;wb(idxNanw,:)=0;wa(idxNanw,1:2)=w(idxNanw,1:2); +% idxNandw=find(isnan(dwa(:,1))|isnan(dwb(:,1)));dwa(idxNandw,:)=0;dwb(idxNandw,:)=0;dwa(idxNandw,1:2)=dw(idxNandw,1:2); +% idxNaneq1=find(isnan(eq1a(:,1))|isnan(eq1b(:,1)));eq1a(idxNaneq1,:)=0;eq1b(idxNaneq1,:)=0;eq1a(idxNaneq1,1:2)=eq1(idxNaneq1,1:2); +% idxNandeq1=find(isnan(deq1a(:,1))|isnan(deq1b(:,1)));deq1a(idxNandeq1,:)=0;deq1b(idxNandeq1,:)=0;deq1a(idxNandeq1,1:2)=deq1(idxNandeq1,1:2); +% idxNaneq2=find(isnan(eq2a(:,1))|isnan(eq2b(:,1)));eq2a(idxNaneq2,:)=0;eq2b(idxNaneq2,:)=0;eq2a(idxNaneq2,1:2)=eq2(idxNaneq2,1:2); +% idxNandeq2=find(isnan(deq2a(:,1))|isnan(deq2b(:,1)));deq2a(idxNandeq2,:)=0;deq2b(idxNandeq2,:)=0;deq2a(idxNandeq2,1:2)=deq2(idxNandeq2,1:2); +% idxNaned1=find(isnan(ed1a(:,1))|isnan(ed1b(:,1)));ed1a(idxNaned1,:)=0;ed1b(idxNaned1,:)=0;ed1a(idxNaned1,1:2)=ed1(idxNaned1,1:2); +% idxNanded1=find(isnan(ded1a(:,1))|isnan(ded1b(:,1)));ded1a(idxNanded1,:)=0;ded1b(idxNanded1,:)=0;ded1a(idxNanded1,1:2)=ded1(idxNanded1,1:2); +% idxNaned2=find(isnan(ed2a(:,1))|isnan(ed2b(:,1)));ed2a(idxNaned2,:)=0;ed2b(idxNaned2,:)=0;ed2a(idxNaned2,1:2)=ed2(idxNaned2,1:2); +% idxNanded2=find(isnan(ded2a(:,1))|isnan(ded2b(:,1)));ded2a(idxNanded2,:)=0;ded2b(idxNanded2,:)=0;ded2a(idxNanded2,1:2)=ded2(idxNanded2,1:2); +% idxNanpsid=find(isnan(psida(:,1))|isnan(psidb(:,1)));psida(idxNanpsid,:)=0;psidb(idxNanpsid,:)=0;psida(idxNanpsid,1:2)=psid(idxNanpsid,1:2); +% idxNandpsid=find(isnan(dpsida(:,1))|isnan(dpsidb(:,1)));dpsida(idxNandpsid,:)=0;dpsidb(idxNandpsid,:)=0;dpsida(idxNandpsid,1:2)=dpsid(idxNandpsid,1:2); +% idxNanpsiq=find(isnan(psiqa(:,1))|isnan(psiqb(:,1)));psiqa(idxNanpsiq,:)=0;psiqb(idxNanpsiq,:)=0;psiqa(idxNanpsiq,1:2)=psiq(idxNanpsiq,1:2); +% idxNandpsiq=find(isnan(dpsiqa(:,1))|isnan(dpsiqb(:,1)));dpsiqa(idxNandpsiq,:)=0;dpsiqb(idxNandpsiq,:)=0;dpsiqa(idxNandpsiq,1:2)=dpsiq(idxNandpsiq,1:2); +% idxNanEf=find(isnan(Efa(:,1))|isnan(Efb(:,1)));Efa(idxNanEf,:)=0;Efb(idxNanEf,:)=0;Efa(idxNanEf,1:2)=Ef(idxNanEf,1:2); +% idxNanPm=find(isnan(Pma(:,1))|isnan(Pmb(:,1)));Pma(idxNanPm,:)=0;Pmb(idxNanPm,:)=0;Pma(idxNanPm,1:2)=Pm(idxNanPm,1:2); +% idxNandPm=find(isnan(dPma(:,1))|isnan(dPmb(:,1)));dPma(idxNandPm,:)=0;dPmb(idxNandPm,:)=0;dPma(idxNandPm,1:2)=dPm(idxNandPm,1:2); +% end +% if ~isempty(exc) +% idxNanVavrm=find(isnan(Vavrma(:,1))|isnan(Vavrmb(:,1)));Vavrma(idxNanVavrm,:)=0;Vavrmb(idxNanVavrm,:)=0;Vavrma(idxNanVavrm,1:2)=Vavrm(idxNanVavrm,1:2); +% idxNandVavrm=find(isnan(dVavrma(:,1))|isnan(dVavrmb(:,1)));dVavrma(idxNandVavrm,:)=0;dVavrmb(idxNandVavrm,:)=0;dVavrma(idxNandVavrm,1:2)=dVavrm(idxNandVavrm,1:2); +% idxNanVavrr=find(isnan(Vavrra(:,1))|isnan(Vavrrb(:,1)));Vavrra(idxNanVavrr,:)=0;Vavrrb(idxNanVavrr,:)=0;Vavrra(idxNanVavrr,1:2)=Vavrr(idxNanVavrr,1:2); +% idxNandVavrr=find(isnan(dVavrra(:,1))|isnan(dVavrrb(:,1)));dVavrra(idxNandVavrr,:)=0;dVavrrb(idxNandVavrr,:)=0;dVavrra(idxNandVavrr,1:2)=dVavrr(idxNandVavrr,1:2); +% idxNanVavrf=find(isnan(Vavrfa(:,1))|isnan(Vavrfb(:,1)));Vavrfa(idxNanVavrf,:)=0;Vavrfb(idxNanVavrf,:)=0;Vavrfa(idxNanVavrf,1:2)=Vavrf(idxNanVavrf,1:2); +% idxNandVavrf=find(isnan(dVavrfa(:,1))|isnan(dVavrfb(:,1)));dVavrfa(idxNandVavrf,:)=0;dVavrfb(idxNandVavrf,:)=0;dVavrfa(idxNandVavrf,1:2)=dVavrf(idxNandVavrf,1:2); +% idxNanVavrref=find(isnan(Vavrrefa(:,1))|isnan(Vavrrefb(:,1)));Vavrrefa(idxNanVavrref,:)=0;Vavrrefb(idxNanVavrref,:)=0;Vavrrefa(idxNanVavrref,1:2)=Vavrref(idxNanVavrref,1:2); +% end +% if ~isempty(tg) +% idxNantgovg=find(isnan(tgovga(:,1))|isnan(tgovgb(:,1)));tgovga(idxNantgovg,:)=0;tgovgb(idxNantgovg,:)=0;tgovga(idxNantgovg,1:2)=tgovg(idxNantgovg,1:2); +% idxNandtgovg=find(isnan(dtgovga(:,1))|isnan(dtgovgb(:,1)));dtgovga(idxNandtgovg,:)=0;dtgovgb(idxNandtgovg,:)=0;dtgovga(idxNandtgovg,1:2)=dtgovg(idxNandtgovg,1:2); +% idxNantgovm=find(isnan(tgovma(:,1))|isnan(tgovmb(:,1)));tgovma(idxNantgovm,:)=0;tgovmb(idxNantgovm,:)=0;tgovma(idxNantgovm,1:2)=tgovm(idxNantgovm,1:2); +% idxNanTmech=find(isnan(Tmecha(:,1))|isnan(Tmechb(:,1)));Tmecha(idxNanTmech,:)=0;Tmechb(idxNanTmech,:)=0;Tmecha(idxNanTmech,1:2)=Tmech(idxNanTmech,1:2); +% idxNandTmech=find(isnan(dTmecha(:,1))|isnan(dTmechb(:,1)));dTmecha(idxNandTmech,:)=0;dTmechb(idxNandTmech,:)=0;dTmecha(idxNandTmech,1:2)=dTmech(idxNandTmech,1:2); +% end +% +% idxNanf=find(isnan(fa(:,1))|isnan(fb(:,1)));fa(idxNanf,:)=0;fb(idxNanf,:)=0;fa(idxNanf,1:2)=f(idxNanf,1:2); +% idxNandf=find(isnan(dfa(:,1))|isnan(dfb(:,1)));dfa(idxNandf,:)=0;dfb(idxNandf,:)=0;dfa(idxNandf,1:2)=df(idxNandf,1:2); +% idxNandpg=find(isnan(dpga(:,1))|isnan(dpgb(:,1)));dpga(idxNandpg,:)=0;dpgb(idxNandpg,:)=0;dpga(idxNandpg,1:2)=dpg(idxNandpg,1:2); +% idxNanddpg=find(isnan(ddpga(:,1))|isnan(ddpgb(:,1)));ddpga(idxNanddpg,:)=0;ddpgb(idxNanddpg,:)=0;ddpga(idxNanddpg,1:2)=ddpg(idxNanddpg,1:2); +% +% if ~isempty(cac)&&~isempty(cluster) +% +% end +% + alphaLeft=0; + alphaRight=alpha/alphaPreMult; + + + %khuangJune14 + % here we utilize PI control strategy instead of bisection search. + % local truncation error is required from all state variable + %------------------------------------------------------------------------------------------- + + +% local_x=foldX(SysDatax,V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrr,Vavrref,tgovg,tgovm,Tmech,f,dpg,qplt,vg); +% if alphax>0 +% if size(alphaList,2)>1&&~isnan(alphab4) + +% [K,h,~] =VSOO(LTE,LTE2,LTE3,SimData,alphab4,ratiob4); +% if isnan(h) +% h +% end +% SimData.nlvl = K; +% alphax = h; +% alphaRight = h ; +% alphaLeft = 0.8*h; + +% % K +% % h +% end +% else +% alphaRight = 0; +% end +% % LTE=0; + %------------------------------------------------------------------------------------------ + + + + + + + + + + + + + % alphax=0.2; + while 1 + %pade +% vc=polyvalVec(Va(:,end:-1:1),alphax)./polyvalVec([Vb(:,end:-1:1),ones(size(Vb,1),1)],alphax); +% +% qc=polyvalVec(Qa(:,end:-1:1),alphax)./polyvalVec([Qb(:,end:-1:1),ones(size(Qb,1),1)],alphax); +% qc(busType==0,:)=0; +% +% sc=polyvalVec(sa(:,end:-1:1),alphax)./polyvalVec([sb(:,end:-1:1),ones(size(sb,1),1)],alphax); +% dsc=polyvalVec(dsa(:,end:-1:1),alphax)./polyvalVec([dsb(:,end:-1:1),ones(size(dsb,1),1)],alphax); +% +% dc=polyvalVec(da(:,end:-1:1),alphax)./polyvalVec([db(:,end:-1:1),ones(size(db,1),1)],alphax); +% ddc=polyvalVec(dda(:,end:-1:1),alphax)./polyvalVec([ddb(:,end:-1:1),ones(size(ddb,1),1)],alphax); +% wc=polyvalVec(wa(:,end:-1:1),alphax)./polyvalVec([wb(:,end:-1:1),ones(size(wb,1),1)],alphax); +% dwc=polyvalVec(dwa(:,end:-1:1),alphax)./polyvalVec([dwb(:,end:-1:1),ones(size(dwb,1),1)],alphax); +% eq1c=polyvalVec(eq1a(:,end:-1:1),alphax)./polyvalVec([eq1b(:,end:-1:1),ones(size(eq1b,1),1)],alphax); +% deq1c=polyvalVec(deq1a(:,end:-1:1),alphax)./polyvalVec([deq1b(:,end:-1:1),ones(size(deq1b,1),1)],alphax); +% eq2c=polyvalVec(eq2a(:,end:-1:1),alphax)./polyvalVec([eq2b(:,end:-1:1),ones(size(eq2b,1),1)],alphax); +% deq2c=polyvalVec(deq2a(:,end:-1:1),alphax)./polyvalVec([deq2b(:,end:-1:1),ones(size(deq2b,1),1)],alphax); +% ed1c=polyvalVec(ed1a(:,end:-1:1),alphax)./polyvalVec([ed1b(:,end:-1:1),ones(size(ed1b,1),1)],alphax); +% ded1c=polyvalVec(ded1a(:,end:-1:1),alphax)./polyvalVec([ded1b(:,end:-1:1),ones(size(ded1b,1),1)],alphax); +% ed2c=polyvalVec(ed2a(:,end:-1:1),alphax)./polyvalVec([ed2b(:,end:-1:1),ones(size(ed2b,1),1)],alphax); +% ded2c=polyvalVec(ded2a(:,end:-1:1),alphax)./polyvalVec([ded2b(:,end:-1:1),ones(size(ded2b,1),1)],alphax); +% psidc=polyvalVec(psida(:,end:-1:1),alphax)./polyvalVec([psidb(:,end:-1:1),ones(size(psidb,1),1)],alphax); +% dpsidc=polyvalVec(dpsida(:,end:-1:1),alphax)./polyvalVec([dpsidb(:,end:-1:1),ones(size(dpsidb,1),1)],alphax); +% psiqc=polyvalVec(psiqa(:,end:-1:1),alphax)./polyvalVec([psiqb(:,end:-1:1),ones(size(psiqb,1),1)],alphax); +% dpsiqc=polyvalVec(dpsiqa(:,end:-1:1),alphax)./polyvalVec([dpsiqb(:,end:-1:1),ones(size(dpsiqb,1),1)],alphax); +% +% Pmc=polyvalVec(Pma(:,end:-1:1),alphax)./polyvalVec([Pmb(:,end:-1:1),ones(size(Pmb,1),1)],alphax); +% dPmc=polyvalVec(dPma(:,end:-1:1),alphax)./polyvalVec([dPmb(:,end:-1:1),ones(size(dPmb,1),1)],alphax); +% Efc=polyvalVec(Efa(:,end:-1:1),alphax)./polyvalVec([Efb(:,end:-1:1),ones(size(Efb,1),1)],alphax); +% Vavrmc=polyvalVec(Vavrma(:,end:-1:1),alphax)./polyvalVec([Vavrmb(:,end:-1:1),ones(size(Vavrmb,1),1)],alphax); +% dVavrmc=polyvalVec(dVavrma(:,end:-1:1),alphax)./polyvalVec([dVavrmb(:,end:-1:1),ones(size(dVavrmb,1),1)],alphax); +% Vavrrc=polyvalVec(Vavrra(:,end:-1:1),alphax)./polyvalVec([Vavrrb(:,end:-1:1),ones(size(Vavrrb,1),1)],alphax); +% dVavrrc=polyvalVec(dVavrra(:,end:-1:1),alphax)./polyvalVec([dVavrrb(:,end:-1:1),ones(size(dVavrrb,1),1)],alphax); +% Vavrfc=polyvalVec(Vavrfa(:,end:-1:1),alphax)./polyvalVec([Vavrfb(:,end:-1:1),ones(size(Vavrfb,1),1)],alphax); +% dVavrfc=polyvalVec(dVavrfa(:,end:-1:1),alphax)./polyvalVec([dVavrfb(:,end:-1:1),ones(size(dVavrfb,1),1)],alphax); +% Vavrrefc=polyvalVec(Vavrrefa(:,end:-1:1),alphax)./polyvalVec([Vavrrefb(:,end:-1:1),ones(size(Vavrrefb,1),1)],alphax); +% tgovgc=polyvalVec(tgovga(:,end:-1:1),alphax)./polyvalVec([tgovgb(:,end:-1:1),ones(size(tgovgb,1),1)],alphax); +% dtgovgc=polyvalVec(dtgovga(:,end:-1:1),alphax)./polyvalVec([dtgovgb(:,end:-1:1),ones(size(dtgovgb,1),1)],alphax); +% tgovmc=polyvalVec(tgovma(:,end:-1:1),alphax)./polyvalVec([tgovmb(:,end:-1:1),ones(size(tgovmb,1),1)],alphax); +% Tmechc=polyvalVec(Tmecha(:,end:-1:1),alphax)./polyvalVec([Tmechb(:,end:-1:1),ones(size(Tmechb,1),1)],alphax); +% dTmechc=polyvalVec(dTmecha(:,end:-1:1),alphax)./polyvalVec([dTmechb(:,end:-1:1),ones(size(dTmechb,1),1)],alphax); +% +% fc=polyvalVec(fa(:,end:-1:1),alphax)./polyvalVec([fb(:,end:-1:1),ones(size(fb,1),1)],alphax); +% dfc=polyvalVec(dfa(:,end:-1:1),alphax)./polyvalVec([dfb(:,end:-1:1),ones(size(dfb,1),1)],alphax); +% dpgc=polyvalVec(dpga(:,end:-1:1),alphax)./polyvalVec([dpgb(:,end:-1:1),ones(size(dpgb,1),1)],alphax); +% ddpgc=polyvalVec(ddpga(:,end:-1:1),alphax)./polyvalVec([ddpgb(:,end:-1:1),ones(size(ddpgb,1),1)],alphax); +% +% qpltc=polyvalVec(qplta(:,end:-1:1),alphax)./polyvalVec([qpltb(:,end:-1:1),ones(size(qplt,1),1)],alphax); +% vgc=polyvalVec(vga(:,end:-1:1),alphax)./polyvalVec([vgb(:,end:-1:1),ones(size(vg,1),1)],alphax); +% Y=Ytr0x+alphax*Ytr1+sparse(1:nbus,1:nbus,Ysh0x+Ysh1*alphax,nbus,nbus); + %pade b4 + vc=polyvalVec(V(:,end:-1:1),alphax); + + qc=polyvalVec(Q(:,end:-1:1),alphax); + qc(busType==0,:)=0; + + sc=polyvalVec(s(:,end:-1:1),alphax); + dsc=polyvalVec(ds(:,end:-1:1),alphax); + + dc=polyvalVec(d(:,end:-1:1),alphax); + ddc=polyvalVec(dd(:,end:-1:1),alphax); + wc=polyvalVec(w(:,end:-1:1),alphax); + dwc=polyvalVec(dw(:,end:-1:1),alphax); + eq1c=polyvalVec(eq1(:,end:-1:1),alphax); + deq1c=polyvalVec(deq1(:,end:-1:1),alphax); + eq2c=polyvalVec(eq2(:,end:-1:1),alphax); + deq2c=polyvalVec(deq2(:,end:-1:1),alphax); + ed1c=polyvalVec(ed1(:,end:-1:1),alphax); + ded1c=polyvalVec(ded1(:,end:-1:1),alphax); + ed2c=polyvalVec(ed2(:,end:-1:1),alphax); + ded2c=polyvalVec(ded2(:,end:-1:1),alphax); + psidc=polyvalVec(psid(:,end:-1:1),alphax); + dpsidc=polyvalVec(dpsid(:,end:-1:1),alphax); + psiqc=polyvalVec(psiq(:,end:-1:1),alphax); + dpsiqc=polyvalVec(dpsiq(:,end:-1:1),alphax); + + Pmc=polyvalVec(Pm(:,end:-1:1),alphax); + dPmc=polyvalVec(dPm(:,end:-1:1),alphax); + Efc=polyvalVec(Ef(:,end:-1:1),alphax); + Vavrmc=polyvalVec(Vavrm(:,end:-1:1),alphax); + dVavrmc=polyvalVec(dVavrm(:,end:-1:1),alphax); + Vavrrc=polyvalVec(Vavrr(:,end:-1:1),alphax); + dVavrrc=polyvalVec(dVavrr(:,end:-1:1),alphax); + Vavrfc=polyvalVec(Vavrf(:,end:-1:1),alphax); + dVavrfc=polyvalVec(dVavrf(:,end:-1:1),alphax); + Vavrrefc=polyvalVec(Vavrref(:,end:-1:1),alphax); + tgovgc=polyvalVec(tgovg(:,end:-1:1),alphax); + dtgovgc=polyvalVec(dtgovg(:,end:-1:1),alphax); + tgovmc=polyvalVec(tgovm(:,end:-1:1),alphax); + Tmechc=polyvalVec(Tmech(:,end:-1:1),alphax); + dTmechc=polyvalVec(dTmech(:,end:-1:1),alphax); + + fc=polyvalVec(f(:,end:-1:1),alphax); + dfc=polyvalVec(df(:,end:-1:1),alphax); + dpgc=polyvalVec(dpg(:,end:-1:1),alphax); + ddpgc=polyvalVec(ddpg(:,end:-1:1),alphax); + + qpltc=polyvalVec(qplt(:,end:-1:1),alphax); + vgc=polyvalVec(vg(:,end:-1:1),alphax); + + Vsp2=Vsp0x+alphax*VspSq2(:,2); + + pqxx=pqx; + pqxx(:,[4,5])=pqx(:,[4,5])+alphax*pqIncrx(:,1:2); + if size(pqIncrx,2)>=4 + pqxx(:,[4,5])=pqxx(:,[4,5])+alphax*alphax*pqIncrx(:,3:4); + end + pvxx=pvx; + if ~isempty(pvx);pvxx(:,4)=pvx(:,4)+alphax*pvIncr;end + + zipxx=zipx; + if ~isempty(zipxx) + zipxx(:,5:10)=repmat(Rzip0x+alphax*Rzip1,1,6).*zipx(:,5:10); + end + + indxx=indx; + if ~isempty(indxx) + indxx(:,15)=(Rind0x+alphax*Rind1).*indx(:,15); + indxx(:,16)=(Rind0x+alphax*Rind1).*indx(:,16); + indxx(:,17)=(Rind0x+alphax*Rind1).*indx(:,17); + end + + SysDataxx=foldSysData(bus,sw,pvxx,pqxx,shunt,line,indxx,zipxx,syn,exc,tg,agc,cac,cluster); + xc=foldX(SysDataxx,vc,qc,sc,dc,wc,eq1c,eq2c,ed1c,ed2c,psidc,psiqc,Pmc,Efc,Vavrmc,Vavrrc,Vavrfc,Vavrrefc,tgovgc,tgovmc,Tmechc,fc,dpgc,qpltc,vgc); + dxc=foldX(SysDataxx,0*vc,0*qc,dsc,ddc,dwc,deq1c,deq2c,ded1c,ded2c,dpsidc,dpsiqc,dPmc,0*Efc,dVavrmc,dVavrrc,dVavrfc,0*Vavrrefc,dtgovgc,0*tgovmc,dTmechc,dfc,ddpgc,0*qpltc,0*vgc); + SysParaxx=foldSysPara([],[],[],[],[],[],[],[],Ytr0x+alphax*Ytr1,[],Ysh0x+Ysh1*alphax,[],[Vsp2,zeros(size(Vsp2))],[],[],[],[],Tmech1,Varref1,Ef1,Pm1,Eq11); + SysParaxx.nIslands=nIslands; + SysParaxx.islands=islands; + SysParaxx.refs=refs; + + diff=checkEquationBalanceSynDyn(SysDataxx,SysParaxx,xc,dxc); + absDiff=max(abs(diff)); + %bisection search +% if absDiff=4 + pqxx(:,[4,5])=pqxx(:,[4,5])+alphax*alphax*pqIncrx(:,3:4); + end + pvxx=pvx; + if ~isempty(pvx);pvxx(:,4)=pvx(:,4)+alphax*pvIncr;end + + zipxx=zipx; + if ~isempty(zipxx) + zipxx(:,5:10)=repmat(Rzip0x+alphax*Rzip1,1,6).*zipx(:,5:10); + end + + indxx=indx; + if ~isempty(indxx) + indxx(:,15)=(Rind0x+alphax*Rind1).*indx(:,15); + indxx(:,16)=(Rind0x+alphax*Rind1).*indx(:,16); + indxx(:,17)=(Rind0x+alphax*Rind1).*indx(:,17); + end + + SysDataxx=foldSysData(bus,sw,pvxx,pqxx,shunt,line,indxx,zipxx,syn,exc,tg,agc,cac,cluster); + xc=foldX(SysDataxx,vc,qc,sc,dc,wc,eq1c,eq2c,ed1c,ed2c,psidc,psiqc,Pmc,Efc,Vavrmc,Vavrrc,Vavrfc,Vavrrefc,tgovgc,tgovmc,Tmechc,fc,dpgc,qpltc,vgc); + dxc=foldX(SysDataxx,0*vc,0*qc,dsc,ddc,dwc,deq1c,deq2c,ded1c,ded2c,dpsidc,dpsiqc,dPmc,0*Efc,dVavrmc,dVavrrc,dVavrfc,0*Vavrrefc,dtgovgc,0*tgovmc,dTmechc,dfc,ddpgc,0*qpltc,0*vgc); + SysParaxx=foldSysPara([],[],[],[],[],[],[],[],Ytr0x+alphax*Ytr1,[],Ysh0x+Ysh1*alphax,[],[Vsp2,zeros(size(Vsp2))],[],[],[],[],Tmech1,Varref1,Ef1,Pm1,Eq11); + SysParaxx.nIslands=nIslands; + SysParaxx.islands=islands; + SysParaxx.refs=refs; + + diff=checkEquationBalanceSynDyn(SysDataxx,SysParaxx,xc,dxc); + if max(abs(diff))<1.05*diffParadigm + break; + end + alphax=alphax*alphaPreMult; + countCheckAlpha=countCheckAlpha+1; + end + + if countCheckAlpha>=maxCount;alphax=alphax/alphaPreMult;end + alpha=alphax; + + pqx(:,[4,5])=pqx(:,[4,5])+alpha*pqIncrx(:,1:2); + if size(pqIncrx,2)>=4 + pqx(:,[4,5])=pqx(:,[4,5])+alpha*alpha*pqIncrx(:,3:4); + end + if ~isempty(pvx);pvx(:,4)=pvx(:,4)+alpha*pvIncr;end + Rzip0x=Rzip0x+alpha*Rzip1; + Rind0x=Rind0x+alpha*Rind1; + Reind0x=Reind0x+alpha*Reind1; + Ysh0x=Ysh0x+alpha*Ysh1; + Vsp0x=Vsp0x+alpha*VspSq2(:,2); + Ytr0x=Ytr0x+alpha*Ytr1; + + alphaConfirm=alphaConfirm+alpha; + if useAbsT + strPre=['T=',num2str(absT+alphaConfirm,'%7.5f'),', ']; + else + strPre=['Step=',num2str(alphaConfirm,'%7.5f'),', ']; + end + addLog([strPre,'dt=',num2str(alpha,'%7.5f'),', K=',num2str(SimData.nlvl,'%7.1f'),', (maxdiff<',num2str(diffTol),').'],'INFO'); +% addLog(['Fluctuation=',num2str(max(fluctuation)),' rate=',num2str(max(fluctuation)/maxTcoeff),'.'],'INFO'); + + if alpha==0 + noMoveCnt=noMoveCnt+1; + absNoMoveCnt=absNoMoveCnt+1; + addLog(['Step did not move! (MaxDiff=',num2str(max(abs(diff))),')' ],'INFO'); + if diffTol>=diffTolMax + addLog('Max DiffTol reached and not move, exit!','INFO'); + exitFlag=DynSimFlag.FAIL; + break; + end + + if noMoveCnt>=maxNoMoveCnt + zipxx=zipx; + if ~isempty(zipxx) + zipxx(:,5:10)=repmat(Rzip0x,1,6).*zipx(:,5:10); + end + jac=getJacobianMod(nbus,nline,bus,sw,pvx,pqx,shuntx,line,zipxx,indx,sc,vc,ones(size(ind,1),1)); + jacCond=condest(jac); + addLog(['Cond of Jacobian=',num2str(jacCond), ', relative cond=',num2str(jacCond/initJacCond)],'INFO'); + if jacCond>50*initJacCond + addLog('Singular likely, exit!','INFO'); + exitFlag=DynSimFlag.FAIL; + break; + end + end + + if allowModelSwitch && useNomoveCtrl && absNoMoveCnt>maxAbsNoMoveCnt && alphaConfirm>0 + addLog('Too many times of no move!','INFO'); + exitFlag=DynSimFlag.NOMOVE_CTRL; + break; + end + + alphaTol=alphaTol/2; + if alphaToldiffTolMax + diffTol=diffTolMax; + end + addLog(['Enlarge tol! (Tol=',num2str(diffTol),')'],'INFO'); + else % step moves + if alphaTolalphaTolOrig + alphaTol=alphaTolOrig; + end + + if useDiffCtrl&&(length(diffList)>1)&&(diffList(2)diffTolMax/2) + exitFlag=3; + end + + if useAbsT && isfield(varOpt,'lastSwitchT') && isfield(varOpt,'switchDelay') + if absT+alphaConfirm>varOpt.lastSwitchT+varOpt.switchDelay + allowSwitchByDelay=1; + else + allowSwitchByDelay=0; + end + else + allowSwitchByDelay=0; + end + if allowModelSwitch && allowSwitchByDelay + CC=real(V); + DD=imag(V); + V2=zeros(size(CC)); + for lvl=1:size(V2,2) + V2(:,lvl)=sum(CC(:,1:lvl).*CC(:,lvl:-1:1),2)+sum(DD(:,1:lvl).*DD(:,lvl:-1:1),2); + end + + if ~isempty(d) + xd=d(2:end,:)-repmat(d(1,:),size(d,1)-1,1); + else + xd=d; + end + if ~isempty(w) + islandSyn=islands(syn(:,1)); + refSynTag=zeros(size(syn,1),1); + for isl=1:nIslands + synInIsland=find(islandSyn==isl); + if ~isempty(synInIsland) + refSynTag(islandSyn==isl)=synInIsland(1); + end + end + xw=w-w(refSynTag,:); + else + xw=w; + end +% [V2a,V2b]=getPadeApproxHelm(V2,0); +% idxNanV2=find(isnan(V2a(:,1))|isnan(V2b(:,1)));V2a(idxNanV2,:)=0;V2b(idxNanV2,:)=0;V2a(idxNanV2,1:2)=V2(idxNanV2,1:2); +% [xda,xdb]=getPadeApproxHelm(xd,0); +% idxNanxd=find(isnan(xda(:,1))|isnan(xdb(:,1)));xda(idxNanxd,:)=0;xdb(idxNanxd,:)=0;xda(idxNanxd,1:2)=xd(idxNanxd,1:2); +% [xwa,xwb]=getPadeApproxHelm(xw,0); +% idxNanxw=find(isnan(xwa(:,1))|isnan(xwb(:,1)));xwa(idxNanxw,:)=0;xwb(idxNanxw,:)=0;xwa(idxNanxw,1:2)=xw(idxNanxw,1:2); + + % heCoeffsToCheck=[xd;w;s;eq1;eq2;ed1;ed2;psid;psiq;Ef;Pm;Vavrm;Vavrr;Vavrf;Vavrref;tgovg;tgovm;Tmech;f;dpg;qplt;vg]; + % maxTcoeff=alpha; + % fluctuations=calcFluctuationHE(heCoeffsToCheck,maxTcoeff); + % flucTag=getFluctuationTag(fluctuations,maxTcoeff); + +% heCoeffsToCheckA=[V2a;xda;wa;sa;eq1a;eq2a;ed1a;ed2a;psida;psiqa;Efa;Pma;Vavrma;Vavrra;Vavrfa;Vavrrefa;tgovga;tgovma;Tmecha;fa;dpga;qplta;vga]; +% heCoeffsToCheckB=[V2b;xdb;wb;sb;eq1b;eq2b;ed1b;ed2b;psidb;psiqb;Efb;Pmb;Vavrmb;Vavrrb;Vavrfb;Vavrrefb;tgovgb;tgovmb;Tmechb;fb;dpgb;qpltb;vgb]; +% heCoeffsToCheckB=[ones(size(heCoeffsToCheckB,1),1),heCoeffsToCheckB]; +% flucTag=getFluctuationTagPade(real(heCoeffsToCheckA),real(heCoeffsToCheckB),0.99*alpha,0.001); + flucTag = 1; + if flucTag && max(abs(isAgcActiveByIsland.*fc))<1e-4&&allowExit1 + addLog('Dyn model likely reached steady state!(PS)','INFO'); + exitFlag=1; + else + exitFlag=0; + end + + if ~isempty(xw) && exitFlag==0 + % heCoeffsToCheckw=[xd;xw;s;eq1;eq2;ed1;ed2;psid;psiq;Ef;Pm;Vavrm;Vavrr;Vavrf;Vavrref;tgovg;tgovm;Tmech;f;dpg;qplt;vg]; + % maxTcoeffw=alpha; + % fluctuationsw=calcFluctuationHE(heCoeffsToCheckw,maxTcoeffw); + % flucTagw=getFluctuationTag(fluctuationsw,maxTcoeffw); +% heCoeffsToCheck=[V2;xw;s;eq1;eq2;ed1;ed2;psid;psiq;Ef;Vavrm;Vavrr;Vavrf;Vavrref;qplt;vg]; +% heCoeffsToCheckAw=[V2a;xwa;sa;eq1a;eq2a;ed1a;ed2a;psida;psiqa;Efa;Vavrma;Vavrra;Vavrfa;Vavrrefa;qplta;vga]; +% heCoeffsToCheckBw=[V2b;xwb;sb;eq1b;eq2b;ed1b;ed2b;psidb;psiqb;Efb;Vavrmb;Vavrrb;Vavrfb;Vavrrefb;qpltb;vgb]; +% heCoeffsToCheckBw=[ones(size(heCoeffsToCheckBw,1),1),heCoeffsToCheckBw]; +% fThreshold=0.0001; +% flucTagw=getFluctuationTagPade(real(heCoeffsToCheckAw),real(heCoeffsToCheckBw),0.99*alpha,fThreshold); +% [fluctRateMod,flucTagMod]=getFluctuationTagPadeMod(real(heCoeffsToCheckAw),real(heCoeffsToCheckBw),0.99*alpha,fThreshold); +% [ubc,lbc]=findPolynomialBounds(real(heCoeffsToCheck(:,2:end)),0.99*alpha); +% fluctRatePs=min(abs([ubc,lbc]),[],2); + flucTagw = 0 ; + if flucTagw + addLog('Inter-rotor transients approximately fades away!(PS)','INFO'); + exitFlag=2; + else + exitFlag=0; + end + end + end +% heCoeffsToChecka=[xda;wa;sa;eq1a;eq2a;ed1a;ed2a;psida;psiqa;Efa;Pma;Vavrma;Vavrra;Vavrfa;Vavrrefa;tgovga;tgovma;Tmecha;fa;qplta;vga]; +% heCoeffsToCheckb=[xdb;wb;sb;eq1b;eq2b;ed1b;ed2b;psidb;psiqb;Efb;Pmb;Vavrmb;Vavrrb;Vavrfb;Vavrrefb;tgovgb;tgovmb;Tmechb;fb;qpltb;vgb]; +% fluctuationsPade=calcFluctuationHEpade(heCoeffsToChecka,heCoeffsToCheckb,maxTcoeff); +% flucTagPade=getFluctuationTag(fluctuationsPade,maxTcoeff); +% if flucTagPade +% addLog('Dyn model likely reached steady state!(Pade)','INFO'); +% end + + noMoveCnt=0; + + tt=[dAlpha:dAlpha:(alpha-alphaTol/10),alpha]; + vcx=polyvalVec(V(:,end:-1:1),tt); + qcx=polyvalVec(Q(:,end:-1:1),tt); + qcx(busType==0,:)=0; + scx=polyvalVec(s(:,end:-1:1),tt); + dcx=polyvalVec(d(:,end:-1:1),tt); + wcx=polyvalVec(w(:,end:-1:1),tt); + eq1cx=polyvalVec(eq1(:,end:-1:1),tt); + eq2cx=polyvalVec(eq2(:,end:-1:1),tt); + ed1cx=polyvalVec(ed1(:,end:-1:1),tt); + ed2cx=polyvalVec(ed2(:,end:-1:1),tt); + psidcx=polyvalVec(psid(:,end:-1:1),tt); + psiqcx=polyvalVec(psiq(:,end:-1:1),tt); + + Pmcx=polyvalVec(Pm(:,end:-1:1),tt); + Efcx=polyvalVec(Ef(:,end:-1:1),tt); + Vavrmcx=polyvalVec(Vavrm(:,end:-1:1),tt); + Vavrrcx=polyvalVec(Vavrr(:,end:-1:1),tt); + Vavrfcx=polyvalVec(Vavrf(:,end:-1:1),tt); + Vavrrefcx=polyvalVec(Vavrref(:,end:-1:1),tt); + tgovgcx=polyvalVec(tgovg(:,end:-1:1),tt); + tgovmcx=polyvalVec(tgovm(:,end:-1:1),tt); + Tmechcx=polyvalVec(Tmech(:,end:-1:1),tt); + + fcx=polyvalVec(f(:,end:-1:1),tt); + dpgcx=polyvalVec(dpg(:,end:-1:1),tt); + qpltcx=polyvalVec(qplt(:,end:-1:1),tt); + vgcx=polyvalVec(vg(:,end:-1:1),tt); +% dtm = 199815 + % +% load('2bus_x.mat'); +% if isempty(tend);tlastend=0;else tlastend=tend(end);end +% E=sw(1,4); +% r=line(1,8); +% x=line(1,9); +% tstart=[tstart;tlastend]; +% tend=[tend;tt(end)+tlastend]; +% istart=[istart;((real(Va(2,1))-E)*(real(Va(2,1))-E)+imag(Va(2,1))*imag(Va(2,1)))/(r*r+x*x)]; +% iend=[iend;((real(vcx(2,end))-E)*(real(vcx(2,end))-E)+imag(vcx(2,end))*imag(vcx(2,end)))/(r*r+x*x)]; +% V2=[V2;V(2,:)]; +% save('2bus_x.mat','tstart','tend','istart','iend','V2'); +% + t=[t,(tt+t(end))]; + VSol=[VSol,vcx]; + QSol=[QSol,qcx]; + sSol=[sSol,scx]; + dSol=[dSol,dcx]; + wSol=[wSol,wcx]; + eq1Sol=[eq1Sol,eq1cx]; + eq2Sol=[eq2Sol,eq2cx]; + ed1Sol=[ed1Sol,ed1cx]; + ed2Sol=[ed2Sol,ed2cx]; + psiqSol=[psiqSol,psiqcx]; + psidSol=[psidSol,psidcx]; + PmSol=[PmSol,Pmcx]; + EfSol=[EfSol,Efcx]; + VavrmSol=[VavrmSol,Vavrmcx]; + VavrrSol=[VavrrSol,Vavrrcx]; + VavrfSol=[VavrfSol,Vavrfcx]; + VavrrefSol=[VavrrefSol,Vavrrefcx]; + tgovgSol=[tgovgSol,tgovgcx]; + tgovmSol=[tgovmSol,tgovmcx]; + TmechSol=[TmechSol,Tmechcx]; + + fSol=[fSol,fcx]; + dpgSol=[dpgSol,dpgcx]; + qpltSol=[qpltSol,qpltcx]; + vgSol=[vgSol,vgcx]; + + V0x=vcx(:,end); + Q0x=qcx(:,end); + s0x=scx(:,end); + d0x=dcx(:,end);w0x=wcx(:,end);eq10x=eq1cx(:,end);eq20x=eq2cx(:,end);ed10x=ed1cx(:,end);ed20x=ed2cx(:,end);psid0x=psidcx(:,end);psiq0x=psiqcx(:,end); + Pm0x=Pmcx(:,end);Ef0x=Efcx(:,end);Vavrm0x=Vavrmcx(:,end);Vavrr0x=Vavrrcx(:,end);Vavrf0x=Vavrfcx(:,end); + Vavrref0x=Vavrrefcx(:,end);tgovg0x=tgovgcx(:,end);tgovm0x=tgovmcx(:,end);Tmech0x=Tmechcx(:,end); + f0x=fcx(:,end);dpg0x=dpgcx(:,end);qplt0x=qpltcx(:,end);vg0x=vgcx(:,end); + + +% +% [LTE,LTE2,LTE3] = Cal_locl_er(local_x,SimData.nlvl,alpha); +% ratiob4 = LTE./alpha; +% alphab4 = alpha; +% alphaList=[alphaList,alpha]; +% diffList=[diffList,max(abs(diff))]; + + if allowModelSwitch + if exitFlag==DynSimFlag.STEADY||exitFlag==DynSimFlag.QSS + break; + end + end + if exitFlag==DynSimFlag.DIFF_CTRL + break; + end + end +end +if length(diffList)>1;diffList(1)=diffList(2);end +stateCurve=[... +VSol;QSol;sSol;dSol;wSol;eq1Sol;eq2Sol;ed1Sol;ed2Sol;psidSol;psiqSol; +PmSol;EfSol;VavrmSol;VavrrSol;VavrfSol;VavrrefSol;tgovgSol;tgovmSol;TmechSol;fSol;dpgSol;qpltSol;vgSol +]; +finalAlpha=alphaConfirm; + +if exist([iden,'.mat'],'file') + delete([iden,'.mat']); +end +end diff --git a/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.html b/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.html new file mode 100644 index 0000000..16de489 --- /dev/null +++ b/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.html @@ -0,0 +1,2498 @@ + + + + + Copy_of_hemMachinePFSalientcontinueDyn
function [V,Q,s,d,w,eq1,eq2,ed1,ed2,psid,psiq,Pm,Ef,Vavrm,Vavrr,Vavrf,Vavrref,tgovg,tgovm,Tmech,f,dpg,qplt,vg]=...
+    hemMachinePFSalientcontinueDyn(SimData,SysData,SysPara,x0)
+
% Core HE algorithm for solving DAEs (dynamic simulation)
+%
+% FUNCTION hemMachinePFSalientcontinueDyn
+%
+% Author: Rui Yao <ruiyao@ieee.org>
+%
+% Copyright (C) 2021, UChicago Argonne, LLC. All rights reserved.
+%
+% OPEN SOURCE LICENSE
+%
+% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+%
+% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+%
+%
+% ******************************************************************************************************
+% DISCLAIMER
+%
+% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
+% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+% ***************************************************************************************************
+%
+% INPUT
+%   SimData - Simulation parameters
+%   SysData - System data for simulation
+%   SysPara - Parameters representing the events happening in the system
+%   x0 - Initial system state
+%
+% OUTPUT - (will be consolidated in a future version)
+%
+% TODO % Modify the output arguments
+%
+
+global IS_OCTAVE;
+
+
+
+% import system data
+[bus,sw,pv,pq,shunt,line,ind,zip,syn,exc,tg,agc,cac,cluster]=unfoldSysData(SysData);
+% nbus:total number of buses
+nbus=size(bus,1);
+nline=size(line,1);
+
+%determine islanding
+if isfield(SysPara,'nIslands')&&isfield(SysPara,'islands')&&isfield(SysPara,'refs')
+    nIslands=SysPara.nIslands;islands=SysPara.islands;refs=SysPara.refs;
+else
+    [nIslands,islands,refs]=searchIslands(bus(:,1),line(:,1:2));
+end
+
+% improt initial condition
+[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
+[~,~,~,nlvl,taylorN,~,~,~,~]=unfoldSimData(SimData);
+% import system parameters
+[pqIncr,pvIncr,Rind0,Rind1,Reind0,Reind1,Rzip0,Rzip1,Ytr0,Ytr1,Ysh0,Ysh1,VspSq2,~,~,~,~,Tmech1,Varref1,Ef1,Pm1,Eq11]=unfoldSysPara(SysPara);
+%
+
+% It seems that PQ incremental is given for every pq bus.
+% need to figure how to utilize them
+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);
+% why additional PQ is given
+if size(pqIncr,2)>=4
+    Pls(pq(:,1),2)=pqIncr(:,3);
+    Qls(pq(:,1),2)=pqIncr(:,4);
+end
+
+% formatting Ymatrix, here the default value of fault is empty
+if isempty(Ytr0)
+    [Y,Ytr0,Ysh,ytrfr,ytrto,yshfr,yshto]=getYMatrix(nbus,line);
+end
+
+
+% reshape the pv, pq shunt and swing buses if they are empty
+busType=zeros(nbus,1);
+if isempty(pv)
+    pv=zeros(0,6);
+end
+if isempty(pq)
+    pq=zeros(0,6);
+end
+if isempty(shunt)
+    shunt=zeros(0,7);
+end
+if isempty(sw)
+    sw=zeros(0,13);
+end
+
+% label pv and swing buses
+% 1: PV bus, 0: PQ bus
+busType(pv(:,1))=1;
+busType(sw(:,1))=2;
+% zip(busType(zip(:,1))~=0,10)=0;
+
+% index of swing bus (isw), pv bus(ipv), and pq bus(ipq)
+% is given: isw, ipv, and ipq
+% Additionally, number of pv and pq buses are given:
+%npv, npq respectively
+isw=find(busType==2);
+ipv=find(busType==1);
+ipq=find(busType==0);
+npq=size(ipq,1);
+npv=size(ipv,1);
+
+% shunt capacitator is initialized as yShunt which is a complex number.
+% for every bus
+yShunt=zeros(nbus,1);
+yShunt(shunt(:,1))=shunt(:,5)+1j*shunt(:,6);
+
+% check if zip load exists in the system
+% and initialized zip load
+if ~isempty(zip)%zipMode=0
+    Ysh0=Ysh0+accumarray(zip(:,1),Rzip0.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]);
+    Ysh1=Ysh1+accumarray(zip(:,1),Rzip1.*(zip(:,5)+1j*zip(:,8)).*zip(:,12),[nbus,1]);
+end
+
+% now zip load + shunt
+Ysh0=Ysh0+yShunt;
+%     Y=Y+sparse(1:nbus,1:nbus,yShunt,nbus,nbus);
+
+% now zip load + shunt+ network Y matrix
+Y=Ytr0+sparse(1:nbus,1:nbus,Ysh0,nbus,nbus);
+
+%initialize P and Q for every bus
+pVec=zeros(nbus,1);
+qVec=zeros(nbus,1);
+%     vSp=zeros(nbus,1);
+
+% need to figure out the meaning of index 1, 4,5
+% based Kaiyang's understanding, 1 is load side and 4&5 are generators'
+% output
+pVec(pv(:,1))=pVec(pv(:,1))+pv(:,4);
+pVec(pq(:,1))=pVec(pq(:,1))-pq(:,4);
+qVec(pq(:,1))=qVec(pq(:,1))-pq(:,5);
+% account the zip load, i.e dynamic load
+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]);
+    qVec=qVec-accumarray(zip(:,1),Rzip0.*zip(:,10).*zip(:,12),[nbus,1]);
+end
+% qVec(ipv)=qVec(ipv)+Q0(ipv);
+%     vSp(ipv)=pv(:,5);
+
+% so far, initialization of PQ for every bus and Y matrix is ready
+
+
+% initiliza voltage V and W = 1/V for every bus
+V=zeros(nbus,nlvl+1);
+V(:,1)=V0;
+W=zeros(nbus,nlvl+1);
+W(:,1)=1./V0;
+
+
+% initiliza magnitude of voltage V for every bus
+Vmag=zeros(nbus,nlvl+1);
+Vmag(:,1)=abs(V0);
+
+% Power is initilized as we already cooked pVec and qVec
+P=zeros(nbus,nlvl+1);
+P(:,1)=pVec;
+%     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
+Q=zeros(nbus,nlvl+1);
+Qxtra=zeros(size(Q));
+Q(:,1)=Q0;
+Qxtra(:,1)=qVec;
+
+% Also, the meaning of Pls and Qls need to be verified
+P(:,2:(size(Pls,2)+1))=-Pls;
+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.
+if ~isempty(zip)
+    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]);
+end
+% Qxtra(busType~=0,2:end)=Q(busType~=0,2:end);
+% Q(busType~=0,2:end)=0;
+
+
+% seperate real and image part of voltages and their inverse
+% here V = C+1i*D
+% and  W = 1./V = E + 1i*F
+C0=real(V(:,1));
+D0=imag(V(:,1));
+E0=real(W(:,1));
+F0=imag(W(:,1));
+
+% Construct sparse matrix individually for C,D,E,F,P,Q
+% Notice that Q = Q(:,1)+Qxtra(:,1) which is different from P
+C0M=sparse(1:nbus,1:nbus,C0,nbus,nbus);
+D0M=sparse(1:nbus,1:nbus,D0,nbus,nbus);
+E0M=sparse(1:nbus,1:nbus,E0,nbus,nbus);
+F0M=sparse(1:nbus,1:nbus,F0,nbus,nbus);
+P0M=sparse(1:nbus,1:nbus,P(:,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
+G=real(Y);
+B=imag(Y);
+% so Y = G + 1i*B
+
+
+%--------------------------------------------------------------
+% 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
+freqKeptTag=zeros(nbus,1);
+frefs=refs;
+fswTag=zeros(nbus,1);
+fsynTag=zeros(nbus,1);
+fswTag(isw)=1;
+fswTagxD=fswTag;
+fsynTag(syn(:,1))=1;
+for isl=1:nIslands
+    if isempty(find(fswTag(islands==isl)==1, 1))
+        if isempty(find(fsynTag(islands==isl)==1, 1))
+            freqTypeTag(isl)=2;
+            busesInIsland=find(islands==isl);
+            [~,imin]=min(abs(D0(busesInIsland)));
+            frefs(isl)=busesInIsland(imin(1));
+            fswTagxD(frefs(isl))=1;
+            freqKeptTag(busesInIsland)=1;
+        else
+            freqTypeTag(isl)=1;
+        end
+    end
+end
+freqKeptTagxRef=freqKeptTag;
+freqKeptTagxRef(frefs)=0;
+nFreqKept=sum(freqKeptTag);
+%-----------------------------------------------------------
+
+
+
+
+%------------------------------------------------------------------
+% this part is for initialling inductor
+if ~isempty(ind)            % check if there is any inductor
+    nInd=size(ind,1);       % determine the number of inductors
+    indIdx=ind(:,1);        % store the index of inductors among all buses
+
+    s=zeros(nInd,nlvl+1);   % slip
+    s(:,1)=s0;              % initialize slip
+    IL=zeros(nInd,nlvl+1);  %  |
+    IR=zeros(nInd,nlvl+1);  %  |
+    Vm=zeros(nInd,nlvl+1);  % initialization finished 0 value
+
+
+   %-----------------parameters of inductors---------
+   %-----------------START----------------
+    R1=ind(:,7);
+    X1=ind(:,8);
+    Z1=ind(:,7)+1j*ind(:,8);
+    Ze=1j*ind(:,13);
+    R2=ind(:,9);
+    X2=ind(:,10);
+    T0=ind(:,15)+ind(:,16)+ind(:,17);
+    T1=-ind(:,16)-2*ind(:,17);
+    T2=ind(:,17);
+    Hm=ind(:,14);
+   %-----------------parameters of inductors---------
+   %-----------------END----------------
+
+
+    Rm=zeros(nInd,1);
+
+    Am=sparse(indIdx,(1:nInd)',ones(1,nInd),nbus,nInd);
+
+    % first order value of induction motor IL,VM,IR
+    IL(:,1)=V0(indIdx)./(Z1+Ze.*(R2+1j*X2.*s0)./(R2.*Reind0+(1j*X2.*Reind0+Ze).*s0));
+    Vm(:,1)=V0(indIdx)-IL(:,1).*Z1;
+    IR(:,1)=Vm(:,1).*s0./(R2+1j*X2.*s0);
+
+    J0=real(IR(:,1));
+    K0=imag(IR(:,1));
+    JL0=real(IL(:,1));
+    KL0=imag(IL(:,1));
+
+    % prepare the algebric matrix
+    Yeind0=Reind0./Ze;
+    Yeind1=Reind1./Ze;
+    Ye1ind0=Reind0.*Z1./Ze;
+    Ye1ind1=Reind1.*Z1./Ze;
+    Ge=real(Yeind0);
+    Be=imag(Yeind0);
+    kg1e=real(Ye1ind0);
+    kb1e=imag(Ye1ind0);
+    Ge1=real(Yeind1);
+    Be1=imag(Yeind1);
+    kg1e1=real(Ye1ind1);
+    kb1e1=imag(Ye1ind1);
+
+    %     LHS_MatInd_Shr_sqz=zeros(nInd,4);
+    %     RHS_C_Shr_sqz=zeros(nInd,8);
+    %     LHS_MatInd_Shr2_sqz=zeros(nInd,8);
+    %
+    %     LHS_MatInd_Shr=zeros(nInd,2,2);
+    %     RHS_C_Shr=cell(nInd,1);
+    %     LHS_MatInd_Shr2=cell(nInd,1); % A^-1B
+    %     LHS_MatInd_Shr3=cell(nInd,1); % A^-1
+    %
+    %     for i=1:nInd
+    %         LHS_MatInd=[R2(i),-X2(i)*s0(i),R1(i)*s0(i),-X1(i)*s0(i),-s0(i),0;...
+    %                     X2(i)*s0(i), R2(i),X1(i)*s0(i), R1(i)*s0(i),0,-s0(i);...
+    %                     -1,0,1+kg1e(i),-kb1e(i),-Ge(i), Be(i);...
+    %                     0,-1,kb1e(i), 1+kg1e(i),-Be(i),-Ge(i);];
+    %         temp0=LHS_MatInd([3,4],[1,2])\eye(2);                       % A^-1
+    %         LHS_MatInd_Shr2{i}=temp0*LHS_MatInd([3,4],[3,4,5,6]);       % A^-1B
+    %         LHS_MatInd_Shr3{i}=temp0;                                % A^-1
+    %         temp1=LHS_MatInd([1,2],[1,2])/LHS_MatInd([3,4],[1,2]);       % CA^-1
+    %         temp2=LHS_MatInd([1,2],[3,4,5,6])-temp1*LHS_MatInd([3,4],[3,4,5,6]); % L=D-CA^-1B
+    %         LHS_MatInd_Shr(i,:,:)=-temp2(:,[1,2])\temp2(:,[3,4]);        % -R\S
+    %         RHS_C_Shr{i}=temp2(:,[1,2])\[eye(2),-temp1];             % R\[I,-CA^-1]
+    %
+    %         LHS_MatInd_Shr_sqz(i,:)=reshape(LHS_MatInd_Shr(i,:,:),[1,4]);
+    %         RHS_C_Shr_sqz(i,:)=reshape(RHS_C_Shr{i},[1,8]);
+    %         LHS_MatInd_Shr2_sqz(i,:)=reshape(LHS_MatInd_Shr2{i},[1,8]);
+    %     end
+    %     LHS_MatInd_Bus=zeros(nbus,2,2);                                  % \sum{-R\S} by buses
+    %     LHS_MatInd_Bus(:,1,1)=accumarray(indIdx,LHS_MatInd_Shr(:,1,1),[nbus,1]);
+    %     LHS_MatInd_Bus(:,1,2)=accumarray(indIdx,LHS_MatInd_Shr(:,1,2),[nbus,1]);
+    %     LHS_MatInd_Bus(:,2,1)=accumarray(indIdx,LHS_MatInd_Shr(:,2,1),[nbus,1]);
+    %     LHS_MatInd_Bus(:,2,2)=accumarray(indIdx,LHS_MatInd_Shr(:,2,2),[nbus,1]);
+
+    MInd0=zeros(nInd,1);
+    MInd1=ones(nInd,1);
+    LHS_MatInd_sqz=[R2,X2.*s0,-MInd1,MInd0,...
+        -X2.*s0,R2,MInd0,-MInd1,...
+        R1.*s0,X1.*s0,MInd1+kg1e,kb1e,...
+        -X1.*s0,R1.*s0,-kb1e,MInd1+kg1e,...
+        -s0,MInd0,-Ge,-Be,...
+        MInd0,-s0,Be,-Ge];   % 4*6 matrix [C,D;A,B]
+    LHS_MatInd_idx=reshape((1:24)',[4,6]);
+    temp0inv_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[1,2]),1,[]));
+    temp0inv_sqz_det=temp0inv_sqz(:,1).*temp0inv_sqz(:,4)-temp0inv_sqz(:,2).*temp0inv_sqz(:,3);
+    temp0_sqz=[temp0inv_sqz(:,4),-temp0inv_sqz(:,2),-temp0inv_sqz(:,3),temp0inv_sqz(:,1)]./repmat(temp0inv_sqz_det,[1,4]);% A^-1
+    indB_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([3,4],[3,4,5,6]),1,[]));
+    LHS_MatInd_Shr2_sqz=[temp0_sqz(:,1).*indB_sqz(:,1)+temp0_sqz(:,3).*indB_sqz(:,2),temp0_sqz(:,2).*indB_sqz(:,1)+temp0_sqz(:,4).*indB_sqz(:,2),...
+        temp0_sqz(:,1).*indB_sqz(:,3)+temp0_sqz(:,3).*indB_sqz(:,4),temp0_sqz(:,2).*indB_sqz(:,3)+temp0_sqz(:,4).*indB_sqz(:,4),...
+        temp0_sqz(:,1).*indB_sqz(:,5)+temp0_sqz(:,3).*indB_sqz(:,6),temp0_sqz(:,2).*indB_sqz(:,5)+temp0_sqz(:,4).*indB_sqz(:,6),...
+        temp0_sqz(:,1).*indB_sqz(:,7)+temp0_sqz(:,3).*indB_sqz(:,8),temp0_sqz(:,2).*indB_sqz(:,7)+temp0_sqz(:,4).*indB_sqz(:,8)];% A^-1B
+    indC_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[1,2]),1,[]));
+    temp1_sqz=[indC_sqz(:,1).*temp0_sqz(:,1)+indC_sqz(:,3).*temp0_sqz(:,2),indC_sqz(:,2).*temp0_sqz(:,1)+indC_sqz(:,4).*temp0_sqz(:,2),...
+        indC_sqz(:,1).*temp0_sqz(:,3)+indC_sqz(:,3).*temp0_sqz(:,4),indC_sqz(:,2).*temp0_sqz(:,3)+indC_sqz(:,4).*temp0_sqz(:,4)];% CA^-1
+    temp2_sqz=LHS_MatInd_sqz(:,reshape(LHS_MatInd_idx([1,2],[3,4,5,6]),1,[]))-...
+        [temp1_sqz(:,1).*indB_sqz(:,1)+temp1_sqz(:,3).*indB_sqz(:,2),temp1_sqz(:,2).*indB_sqz(:,1)+temp1_sqz(:,4).*indB_sqz(:,2),...
+        temp1_sqz(:,1).*indB_sqz(:,3)+temp1_sqz(:,3).*indB_sqz(:,4),temp1_sqz(:,2).*indB_sqz(:,3)+temp1_sqz(:,4).*indB_sqz(:,4),...
+        temp1_sqz(:,1).*indB_sqz(:,5)+temp1_sqz(:,3).*indB_sqz(:,6),temp1_sqz(:,2).*indB_sqz(:,5)+temp1_sqz(:,4).*indB_sqz(:,6),...
+        temp1_sqz(:,1).*indB_sqz(:,7)+temp1_sqz(:,3).*indB_sqz(:,8),temp1_sqz(:,2).*indB_sqz(:,7)+temp1_sqz(:,4).*indB_sqz(:,8)];% L=D-CA^-1B=[R,S]
+    temp2_c12_sqz=temp2_sqz(:,1:4);
+    temp2_c34_sqz=temp2_sqz(:,5:8);
+    temp2_c12_sqz_det=temp2_c12_sqz(:,1).*temp2_c12_sqz(:,4)-temp2_c12_sqz(:,2).*temp2_c12_sqz(:,3);
+    temp2_c12_inv_sqz=[temp2_c12_sqz(:,4),-temp2_c12_sqz(:,2),-temp2_c12_sqz(:,3),temp2_c12_sqz(:,1)]./repmat(temp2_c12_sqz_det,[1,4]);
+    LHS_MatInd_Shr_sqz=-[temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,2),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,1)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,2),...
+        temp2_c12_inv_sqz(:,1).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,3).*temp2_c34_sqz(:,4),temp2_c12_inv_sqz(:,2).*temp2_c34_sqz(:,3)+temp2_c12_inv_sqz(:,4).*temp2_c34_sqz(:,4)];% -R\S
+    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(:,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
+    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(:,2)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,2),[nbus,1]);
+    LHS_MatInd_Bus_sqz(:,3)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,3),[nbus,1]);
+    LHS_MatInd_Bus_sqz(:,4)=accumarray(indIdx,LHS_MatInd_Shr_sqz(:,4),[nbus,1]);
+else
+    s=zeros(0,nlvl+1);
+end
+% Initialization of inductors is finished
+%------------------------------------------------------------------
+
+
+%------------------------------Initialization of ZIP load---------
+if ~isempty(zip)
+    nZip=size(zip,1);
+    zipIdx=zip(:,1);
+    IiL=zeros(nZip,nlvl+1);
+    BiL=zeros(nZip,nlvl+1);
+
+    % prepare the necessary matrix by blocks
+    Bi0=abs(V0(zipIdx));
+    JI=zip(:,6);
+    KI=-zip(:,9);
+    % current
+    Ii0L=Rzip0.*(JI+1j*KI).*V0(zipIdx)./Bi0;
+    Ji0L=real(Ii0L);
+    Ki0L=imag(Ii0L);
+
+    IiL(:,1)=Ii0L;
+    BiL(:,1)=Bi0;
+    % voltage
+    Ci0=real(V0(zipIdx));
+    Di0=imag(V0(zipIdx));
+
+    LHS_MatZip=[Rzip0.*JI./Bi0-Ci0.*Ji0L./Bi0./Bi0,-Rzip0.*KI./Bi0-Di0.*Ji0L./Bi0./Bi0,...
+        Rzip0.*KI./Bi0-Ci0.*Ki0L./Bi0./Bi0,Rzip0.*JI./Bi0-Di0.*Ki0L./Bi0./Bi0];
+    Mat_BZip=[Ci0./Bi0,Di0./Bi0];
+else
+    IiL=zeros(0,nlvl+1);
+end
+%------------------------------Initialization of ZIP load------------------
+%------------------------------Initialization of ZIP load is finished----------------
+
+
+%------------------------------Initialization of GEN------------------
+%------------------------------Start------------------------
+nSyn=size(syn,1);
+if ~isempty(syn)
+    synIdx =syn(:,1);% index number of Generators
+    wgb    =syn(:,4);% maybe the base value
+    modSyn =syn(:,5);% the order of generator models
+    Xgl    =syn(:,6);
+    Rga    =syn(:,7);
+    Xgd    =syn(:,8);
+    Xgd1   =syn(:,9);
+    Xgd2   =syn(:,10);
+    Tgd1   =syn(:,11);
+    Tgd2   =syn(:,12);
+    Xgq    =syn(:,13);
+    Xgq1   =syn(:,14);
+    Xgq2   =syn(:,15);
+    Tgq1   =syn(:,16);
+    Tgq2   =syn(:,17);
+    Mg     =syn(:,18);
+    Dg     =syn(:,19);
+    TgAA   =syn(:,24);
+    gammad =Tgd2./Tgd1.*Xgd2./Xgd1.*(Xgd-Xgd1);
+    gammaq =Tgq2./Tgq1.*Xgq2./Xgq1.*(Xgq-Xgq1);
+
+    d=zeros(nSyn,nlvl+1); % delta
+    w=zeros(nSyn,nlvl+1); % omega
+    eq1=zeros(nSyn,nlvl+1); %eq'
+    eq2=zeros(nSyn,nlvl+1); %eq''
+    ed1=zeros(nSyn,nlvl+1); %ed'
+    ed2=zeros(nSyn,nlvl+1); %ed''
+    psiq=zeros(nSyn,nlvl+1); % not sure, only in 8th order model
+    psid=zeros(nSyn,nlvl+1); % not sure, only in 8th order model
+    JG=zeros(nSyn,nlvl+1);
+    KG=zeros(nSyn,nlvl+1);
+    IGq=zeros(nSyn,nlvl+1);
+    IGd=zeros(nSyn,nlvl+1);
+    VGq=zeros(nSyn,nlvl+1);
+    VGd=zeros(nSyn,nlvl+1);
+    Cd=zeros(nSyn,nlvl+1);
+    Sd=zeros(nSyn,nlvl+1);
+    Ef=zeros(nSyn,nlvl+1);
+    Pm=zeros(nSyn,nlvl+1);
+
+    cosd=cos(d0);
+    sind=sin(d0);
+    CG0=C0(synIdx);
+    DG0=D0(synIdx);
+    % the first value is given here, notice all are 8th order model
+    d(:,1)=d0;
+    w(:,1)=w0;
+    eq1(:,1)=eq10;
+    eq2(:,1)=eq20;
+    ed1(:,1)=ed10;
+    ed2(:,1)=ed20;
+    psiq(:,1)=psiq0;
+    psid(:,1)=psid0;
+
+    % transform between grid side and dq side
+
+    VGd(:,1)=sind.*CG0-cosd.*DG0;
+    VGq(:,1)=cosd.*CG0+sind.*DG0;
+    % now they are under dq side
+
+    Cd(:,1)=cosd; %  first order of cos(delta)
+    Sd(:,1)=sind; %  first order of sin(delta)
+    Ef(:,1)=Ef0;
+    Pm(:,1)=Pm0;
+
+    %check if controller exists
+    if ~isempty(Ef1)
+        Ef(:,2)=Ef1;
+    end
+    if ~isempty(Eq11)
+        eq1(:,2)=Eq11;
+    end
+    if ~isempty(Pm1)
+        Pm(:,2)=Pm1;
+    end
+
+    % notice that here truncated taylor is applied
+    % and this is the key differnet from Dt rule
+    % Here only at most 5 th order taylor series are considered for sin
+    % and cos function
+    [cosp,sinp,taylorN]=getTaylorPolynomials(d0,taylorN); % taylorN may be truncated
+
+    Mats=zeros(nSyn,4);
+    MatsR=zeros(nSyn,4);
+    MatsRs=zeros(nSyn,4);
+
+    % count the number for different kinds models
+    % ex: modelTag = [ 0 0 0 0 0 10 0 0].'
+    % ex: there are 10 gens using 6th order model
+    modelTag=accumarray(modSyn,ones(nSyn,1),[8,1]);
+
+    % determine the order of the model
+    % Do we really need for loop?
+    % the answer is yes since different gen may use different
+    % order model
+    for i=1:nSyn
+        % 8th order, no need to change
+        if modSyn(i)==8
+            IGd(i,1)=(eq20(i)-psid0(i))/Xgd2(i);
+            IGq(i,1)=(-ed20(i)-psiq0(i))/Xgq2(i);
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)];
+        % 6th order
+        elseif modSyn(i)==6
+            % algebric equation to solve Id, Iq
+            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));
+            % transform matrix (inverse version)
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)];
+            % Here matrix is the inverse matrix, to understand this
+            % We have A*Ixy+B*Vxy = f => MatsR = A^-1, MatsRs = A^-1*B = MatsRs*B
+            % so Ixy = MatsR*f-MatsRs*Vxy, which is used later to
+            % eliminate Ixy when disturbance happens
+            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));
+            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)];
+        % 5th order
+        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));
+            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));
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(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));
+            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)];
+        % 4th order
+        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));
+            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));
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)];
+            MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq1(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq1(i)+sind(i)*Rga(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),...
+                MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)];
+        % 3rd order
+        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));
+            IGq(i,1)=(-(-VGd(i,1))*Xgd1(i)+(eq10(i)-VGq(i,1))*Rga(i))/(Rga(i)*Rga(i)+Xgd1(i)*Xgq(i));
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)];
+            MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd1(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd1(i),-cosd(i)*Xgq(i)+sind(i)*Rga(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),...
+                MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)];
+        % classical model
+        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));
+            IGq(i,1)=(-(-VGd(i,1))*Xgd(i)+(Ef0(i)-VGq(i,1))*Rga(i))/(Rga(i)*Rga(i)+Xgd(i)*Xgq(i));
+            Mats(i,:)=[sind(i),cosd(i),-cosd(i),sind(i)];
+            MatsR(i,:)=[sind(i)*Rga(i)-cosd(i)*Xgd(i),sind(i)*Xgq(i)+cosd(i)*Rga(i),-cosd(i)*Rga(i)-sind(i)*Xgd(i),-cosd(i)*Xgq(i)+sind(i)*Rga(i)]/...
+                (Rga(i)*Rga(i)+Xgd(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),...
+                MatsR(i,3)*sind(i)+MatsR(i,4)*cosd(i),-MatsR(i,3)*cosd(i)+MatsR(i,4)*sind(i)];
+        end
+    end
+    % not sure how to use them now, but they are zeroth order of Ix and Iy
+    JG(:,1)= sind.*IGd(:,1)+cosd.*IGq(:,1);
+    KG(:,1)=-cosd.*IGd(:,1)+sind.*IGq(:,1);
+
+    % put previous matrix in a right place in all buses instead of only
+    % generator buses
+    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)];
+else
+    d=zeros(0,nlvl+1);
+    w=zeros(0,nlvl+1);
+    eq1=zeros(0,nlvl+1);
+    eq2=zeros(0,nlvl+1);
+    ed1=zeros(0,nlvl+1);
+    ed2=zeros(0,nlvl+1);
+    psiq=zeros(0,nlvl+1);
+    psid=zeros(0,nlvl+1);
+    JG=zeros(0,nlvl+1);
+    KG=zeros(0,nlvl+1);
+    IGq=zeros(0,nlvl+1);
+    IGd=zeros(0,nlvl+1);
+    VGq=zeros(0,nlvl+1);
+    VGd=zeros(0,nlvl+1);
+    Cd=zeros(0,nlvl+1);
+    Sd=zeros(0,nlvl+1);
+    Ef=zeros(0,nlvl+1);
+    Pm=zeros(0,nlvl+1);
+end
+%------------------------------Initialization of GEN------------------
+%------------------------------EnD------------------------------------
+
+
+
+%------------------------------Initialization of Exciter------------------
+%------------------------------START------------------------------------
+if ~isempty(exc)
+    nExc    =size(exc,1);
+    % All Type 3 AVR, a 3rd order controller
+    % for Type 3 AVR, avr0(:,1:3) are Vavrm, Vavrr, Vavrf,
+    % and avr0(:,4) is reference Vref (input for secondary voltage control).
+    excIdx  = exc(:,1);
+    VavrMax = exc(:,3);
+    VavrMin = exc(:,4);
+    muavr0  = exc(:,5);
+    Tavr1   = exc(:,7);
+    Tavr2   = exc(:,6);
+    vavrf0  = exc(:,8);
+    Vavr0   = exc(:,9);
+    Tavre   = exc(:,10);
+    Tavrr   = exc(:,11);
+
+    %here I need to check why Vavrref is time varing instead of constant
+    % memory is given to state variables of EXC
+    Vavrm  = zeros(nExc,nlvl+1);
+    Vavrr  = zeros(nExc,nlvl+1);
+    Vavrf  = zeros(nExc,nlvl+1);
+    Vavrref= zeros(nExc,nlvl+1);
+
+    % zeroth order value is given
+    Vavrm(:,1)=real(Vavrm0);
+    Vavrr(:,1)=real(Vavrr0);
+    Vavrf(:,1)=real(Vavrf0);
+    Vavrref(:,1)=real(Vavrref0);
+
+    % here Varref1 is given with syspara
+    if ~isempty(Varref1)
+        Vavrref(:,2)=Varref1;
+    end
+
+    %  non-windup limiter, check the limit
+    tavrMaxDiff=Vavrf(:,1)-VavrMax;
+    tavrMinDiff=Vavrf(:,1)-VavrMin;
+
+    % label values in different interval
+    avrSt=zeros(nExc,1);
+    avrSt(tavrMaxDiff>0)=1;
+    avrSt(tavrMinDiff<0)=-1;
+
+    % output after the limiter
+    Ef(excIdx(avrSt==-1),1)=VavrMin(avrSt==-1);
+    Ef(excIdx(avrSt== 1),1)=VavrMax(avrSt== 1);
+    Ef(excIdx(avrSt== 0),1)=Vavrf(avrSt==0,1);
+
+else
+    Vavrm=zeros(0,nlvl+1);
+    Vavrr=zeros(0,nlvl+1);
+    Vavrf=zeros(0,nlvl+1);
+    Vavrref=zeros(0,nlvl+1);
+end
+%------------------------------Initialization of Exciter------------------
+%------------------------------END------------------------------------
+
+
+
+
+%------------------------------Initialization of Turbing Governor------------------
+%------------------------------START------------------------------------
+if ~isempty(tg)
+    nTg  = size(tg,1);
+    % Type 2 Turbing governor.
+    % one DE, one AE and one limiter
+    tgIdx  = tg(:,1);
+
+    wtgref = tg(:,3);
+    Rtg    = tg(:,4);
+    Ttgmax = tg(:,5);
+    Ttgmin = tg(:,6);
+    Ttg2   = tg(:,7);
+    Ttg1   = tg(:,8);
+
+    tgovg  = zeros(nTg,nlvl+1); % tg
+    tgovm  = zeros(nTg,nlvl+1); % Tmi*
+    Tmech  = zeros(nTg,nlvl+1); % Tmi0
+
+    % zeroth value is given
+    tgovg(:,1)=real(tgovg0);
+    tgovm(:,1)=real(tgovm0);
+    Tmech(:,1)=real(tgovmech0);
+
+    if ~isempty(Tmech1)
+        Tmech(:,2)=Tmech1;
+    end
+
+    % check if limit is approached
+    tgovMaxDiff=tgovm(:,1)-Ttgmax;
+    tgovMinDiff=tgovm(:,1)-Ttgmin;
+
+    govSt=zeros(nTg,1);
+    govSt(tgovMaxDiff>0)=1;
+    govSt(tgovMinDiff<0)=-1;
+    % if limit is approached, set Pm to constant value
+    Pm(tgIdx(govSt==0),1)=tgovm(govSt==0,1);
+    Pm(tgIdx(govSt==1),1)=Ttgmax(govSt==1,1);
+    Pm(tgIdx(govSt==-1),1)=Ttgmin(govSt==-1,1);
+else
+    tgovg=zeros(0,nlvl+1);
+    tgovm=zeros(0,nlvl+1);
+    Tmech=zeros(0,nlvl+1);
+end
+%------------------------------Initialization of Turbing Governor------------------
+%------------------------------END------------------------------------
+
+% this part i don't quite understand. It looks like f denotes frequency
+% on every bus, is it relevant with frequency dependant load?
+% now i find that this is for dynamics of agc
+f=zeros(nbus,nlvl+1);
+f(:,1)=f0;
+synTag=zeros(nbus,1);
+synTag(syn(:,1))=1:nSyn;
+numSynOnBus=accumarray(syn(:,1),1,[nbus,1]);
+dpgTag=ones(nbus,1);
+for islIdx=1:nIslands
+    busIsland=find(islands==islIdx);
+    synTagIsland=synTag(busIsland);
+    wIsland=w(synTagIsland(synTagIsland~=0),1);
+    if ~isempty(wIsland)
+        f(busIsland,1)=mean(wIsland); % note that here the freq can be different
+        dpgTag(busIsland)=0;
+    end
+end
+
+%AGC part
+if ~isempty(agc)
+    agcExt=zeros(nbus,size(agc,2));
+    agcExt(agc(:,1),:)=agc;
+    dpg=zeros(nbus,nlvl+1);
+    dpg(:,1)=dpg0;
+    fdk=agcExt(:,2)+agcExt(:,3); %1/R+D
+else
+    dpg=zeros(nbus,nlvl+1);
+    fdk=zeros(nbus,1);
+end
+
+% this is long term dynamic, it seems that not considered here
+if ~isempty(cac)&&~isempty(cluster)
+
+else
+    qplt=zeros(0,nlvl+1);
+    vg=zeros(0,nlvl+1);
+end
+
+% freq relevant part induced by AGC
+FreqReal=sparse(1:nbus,1:nbus,-freqKeptTag.*fdk.*E0,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);
+
+Y11=-G;Y12=B;Y21=-B;Y22=-G;
+% Influence to Origianl Power flow
+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
+if ~isempty(zip)
+    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);
+    Y21=Y21-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,3),[nbus,1]),nbus,nbus);
+    Y22=Y22-sparse(1:nbus,1:nbus,accumarray(zipIdx,LHS_MatZip(:,4),[nbus,1]),nbus,nbus);
+end
+YLHS=[Y11,Y12;Y21,Y22];
+
+% Counting influence of Motors into small Y matrix
+if ~isempty(ind)
+    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(:,2),nbus,nbus),sparse(1:nbus,1:nbus,LHS_MatInd_Bus_sqz(:,4),nbus,nbus)];
+end
+
+% Counting influence of generators into small Y matrix
+if ~isempty(syn)
+    YLHS=YLHS+MatGCD;
+end
+
+idxNonSw=find(busType~=2);
+idxNonSwxD=find(fswTagxD==0);
+idxNonSwD=find(busType~=2&fswTagxD==1);
+
+% This is the left hand side matrix totally
+LHS_mat=[YLHS([idxNonSw;idxNonSw+nbus],[idxNonSw;idxNonSw+nbus]),...
+    [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)];...
+    C0M(ipv,idxNonSw),D0M(ipv,idxNonSw),sparse(npv,2*npq+3*npv+nFreqKept);...
+    E0M(idxNonSw,idxNonSw),-F0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),-D0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);...
+    F0M(idxNonSw,idxNonSw),E0M(idxNonSw,idxNonSw),D0M(idxNonSw,idxNonSw),C0M(idxNonSw,idxNonSw),sparse(npq+npv,npv+nFreqKept);...
+    sparse(sum(freqKeptTagxRef),size(idxNonSw,1)+size(idxNonSw,1)+2*npq+3*npv),Freq2freq(freqKeptTagxRef==1,freqKeptTag==1);...
+    sparse(size(idxNonSwD,1),size(idxNonSw,1)),sparse(1:size(idxNonSwD,1),idxNonSwD,ones(size(idxNonSwD,1),1),size(idxNonSwD,1),size(idxNonSw,1)),sparse(size(idxNonSwD,1),2*npq+3*npv+nFreqKept)];
+
+% if nbus<=500
+%     [L_LHS_mat,U_LHS_mat,p_LHS_mat]=lu(LHS_mat,'vector');
+% end
+
+
+% deterine if we use LU factoration
+% for this part, i assume the system algebrac equation is under a good
+% dcondition number and the dimension is not very high, otherwise LU will
+% be time consuming
+useLU=isfield(SysPara,'iden')&&isfield(SysPara,'p_amd');
+
+if useLU
+    if isempty(SysPara.p_amd)
+        p_amd = colamd (LHS_mat) ;
+        save([SysPara.iden,'.mat'],'p_amd');
+    else
+        p_amd=SysPara.p_amd;
+    end
+    MxI = speye (size(LHS_mat)) ;
+    MxQ = MxI (:, p_amd) ;
+    if IS_OCTAVE
+        [MxL,MxU,MxP,MxQx] = lu (LHS_mat*MxQ) ;
+    else
+        [MxL,MxU,MxP] = lu (LHS_mat*MxQ) ;
+    end
+end
+
Not enough input arguments.
+
+Error in Copy_of_hemMachinePFSalientcontinueDyn (line 48)
+[bus,sw,pv,pq,shunt,line,ind,zip,syn,exc,tg,agc,cac,cluster]=unfoldSysData(SysData);
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%this is the recursive part for computing high order of time series%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% strat interations nlvl: order of Taylor series
+for i=1:nlvl
+    % 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)
+    seq2=getseq(i,2);
+    seq2p=getseq(i+1,2);
+    seq3=getseq(i,3);
+    idxSeq2=sum(seq2==i,2);
+    idxSeq2x=sum(seq2(:,2)==i,2);
+    idxSeq2p=sum(seq2p>=i,2);
+    idxSeq3=sum(seq3==i,2);
+    idxSeq3x=sum(seq3(:,[2,3])==i,2);
+
+    % 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
+    seq2R=seq2(idxSeq2==0,:);
+    seq2x=seq2(idxSeq2x==0,:);
+    seq2m=getseq(i-1,2);
+    seq2mm=getseq(i-2,2);
+
+    RHSILr=zeros(nbus,1);
+    RHSILi=zeros(nbus,1);
+
+    % This part is for induction motor
+    if ~isempty(ind)
+        % package right hand side vector at every iteration
+        rhsM=sum(Vm(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,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+...
+        %             (T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2)).*Rm./R2;
+
+        %         s(:,i+1)=(Rind0.*(T0.*s(:,i)+T1.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2)+T2.*sum(s(:,seq3m(:,1)+1).*s(:,seq3m(:,2)+1).*s(:,seq3m(:,3)+1),2))...
+        %             -real(sum(IR(:,seq2m(:,1)+1).*conj(IR(:,seq2m(:,2)+1)),2)).*R2-2*Hm.*sum(repmat(seq2R(:,1)',nInd,1).*s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2))...
+        %             ./(2*Hm.*s(:,1)*i);
+        %         if i>=2
+        %             s(:,i+1)=s(:,i+1)+...
+        %                 Rind1.*(T0.*s(:,i-1)+T1.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2)+T2.*sum(s(:,seq3mm(:,1)+1).*s(:,seq3mm(:,2)+1).*s(:,seq3mm(:,3)+1),2))...
+        %                 ./(2*Hm.*s(:,1)*i);
+        %         end
+
+        % update the high order of slip, a special setting is required for
+        % low order when i<2
+        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
+            s(:,i+1)=s(:,i+1)+...
+                Rind1.*(T1.*s(:,i-1)+T2.*sum(s(:,seq2mm(:,1)+1).*s(:,seq2mm(:,2)+1),2))...
+                ./(2*Hm*i);
+        end
+        if i==1
+            s(:,i+1)=s(:,i+1)+Rind0.*T0./(2*Hm*i);
+        end
+        if i==2
+            s(:,i+1)=s(:,i+1)+Rind1.*T0./(2*Hm*i);
+        end
+        % for dynamic model, Right hand side vector is required a update
+        addenRhs=Vm(:,1).*s(:,i+1)-1j*X2.*IR(:,1).*s(:,i+1);
+
+        %         rhsBus=zeros(2,nInd);
+        %         for j=1:nInd
+        %             rhsBus(:,j)=RHS_C_Shr{j}*[real(rhsM(j)+addenRhs(j));imag(rhsM(j)+addenRhs(j));0;0];
+        %         end
+
+        % count the influence of dynamic of slip into rigt hand side vector
+        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)]';
+
+        %accumulate currents IL
+        RHSILr=accumarray(indIdx,rhsBus(1,:)',[nbus,1]);
+        RHSILi=accumarray(indIdx,rhsBus(2,:)',[nbus,1]);
+
+        %             rhsBus=zeros(5,nInd);
+        %             rhsM=sum(Vm(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-1j*X2.*sum(IR(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2);
+        %             rhsImod=Rind1.*(T1.*s(:,i)+T2.*sum(s(:,seq2m(:,1)+1).*s(:,seq2m(:,2)+1),2))+Rind0.*T2.*sum(s(:,seq2R(:,1)+1).*s(:,seq2R(:,2)+1),2)-...
+        %                 real(sum(V(indIdx,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2))+...
+        %                 real(sum(IL(:,seq2R(:,1)+1).*conj(IR(:,seq2R(:,2)+1)),2).*Z1);
+        %             if i==1
+        %                 rhsImod=rhsImod+Rind1.*T0;
+        %             end
+        %             rhsIL=V(indIdx,i).*Yeind1-IL(:,i).*Ye1ind1;
+        %             for j=1:nInd
+        %                 rhsBus(:,j)=squeeze(RHS_C_Shr(j,:,:))*[real(rhsM(j));imag(rhsM(j));rhsImod(j);real(rhsIL(j));imag(rhsIL(j))];
+        %             end
+        %             RHSILr=accumarray(indIdx,rhsBus(3,:)',[nbus,1]);
+        %             RHSILi=accumarray(indIdx,rhsBus(4,:)',[nbus,1]);
+    end
+
+    % strat update ZIP load into currents
+    RHSIiLr=zeros(nbus,1);
+    RHSIiLi=zeros(nbus,1);
+    if ~isempty(zip)
+        RHS_BZip=(real(sum(V(zipIdx,seq2R(:,1)+1).*conj(V(zipIdx,seq2R(:,2)+1)),2))-sum(BiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2))./Bi0/2;
+        RHZ_BIConv=sum(IiL(:,seq2R(:,1)+1).*BiL(:,seq2R(:,2)+1),2);
+        RHSILr_full=Rzip1.*(JI.*real(V(zipIdx,i))-KI.*imag(V(zipIdx,i)))./Bi0-real(RHZ_BIConv)./Bi0-Ji0L.*RHS_BZip./Bi0;
+        RHSILi_full=Rzip1.*(KI.*real(V(zipIdx,i))+JI.*imag(V(zipIdx,i)))./Bi0-imag(RHZ_BIConv)./Bi0-Ki0L.*RHS_BZip./Bi0;
+        RHSIiLr=accumarray(zipIdx,RHSILr_full,[nbus,1]);
+        RHSIiLi=accumarray(zipIdx,RHSILi_full,[nbus,1]);
+    end
+
+    % Start uodate generators
+    RHSIGr=zeros(nbus,1);
+    RHSIGi=zeros(nbus,1);
+    if ~isempty(syn)
+        RhsEd=zeros(nSyn,1);
+        RhsEq=zeros(nSyn,1);
+        IGdAdd=zeros(nSyn,1);
+        IGqAdd=zeros(nSyn,1);
+        % select different models for generators
+        if modelTag(8)>0
+            d(modSyn==8,i+1)=(wgb(modSyn==8).*w(modSyn==8,i))/i;
+            w(modSyn==8,i+1)=(Pm(modSyn==8,i)-...
+                (sum(psid(modSyn==8,seq2m(:,1)+1).*IGq(modSyn==8,seq2m(:,2)+1),2)-sum(psiq(modSyn==8,seq2m(:,1)+1).*IGd(modSyn==8,seq2m(:,2)+1),2))-...
+                Dg(modSyn==8).*w(modSyn==8,i))./Mg(modSyn==8)/i;
+            psid(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGd(modSyn==8,i)+psiq(modSyn==8,i)+VGd(modSyn==8,i))/i;
+            psiq(modSyn==8,i+1)=wgb(modSyn==8).*(Rga(modSyn==8).*IGq(modSyn==8,i)-psid(modSyn==8,i)+VGq(modSyn==8,i))/i;
+            eq1(modSyn==8,i+1)=(-eq1(modSyn==8,i)-(Xgd(modSyn==8)-Xgd1(modSyn==8)-gammad(modSyn==8)).*IGd(modSyn==8,i)+(1-TgAA(modSyn==8)./Tgd1(modSyn==8)).*Ef(modSyn==8,i))./Tgd1(modSyn==8)/i;
+            ed1(modSyn==8,i+1)=(-ed1(modSyn==8,i)+(Xgq(modSyn==8)-Xgq1(modSyn==8)-gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq1(modSyn==8)/i;
+            eq2(modSyn==8,i+1)=(-eq2(modSyn==8,i)+eq1(modSyn==8,i)-(Xgd1(modSyn==8)-Xgd2(modSyn==8)+gammad(modSyn==8)).*IGd(modSyn==8,i)+TgAA(modSyn==8)./Tgd1(modSyn==8).*Ef(modSyn==8,i))./Tgd2(modSyn==8)/i;
+            ed2(modSyn==8,i+1)=(-ed2(modSyn==8,i)+ed1(modSyn==8,i)+(Xgq1(modSyn==8)-Xgq2(modSyn==8)+gammaq(modSyn==8)).*IGq(modSyn==8,i))./Tgq2(modSyn==8)/i;
+            IGdAdd(modSyn==8)=(eq2(modSyn==8,i+1)-psid(modSyn==8,i+1))./Xgd2(modSyn==8);
+            IGqAdd(modSyn==8)=(-ed2(modSyn==8,i+1)-psiq(modSyn==8,i+1))./Xgq2(modSyn==8);
+        end
+        if modelTag(6)>0
+            d(modSyn==6,i+1)=(wgb(modSyn==6).*w(modSyn==6,i))/i;
+            w(modSyn==6,i+1)=(Pm(modSyn==6,i)-...
+                (sum(VGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(VGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)+...
+                Rga(modSyn==6).*(sum(IGq(modSyn==6,seq2m(:,1)+1).*IGq(modSyn==6,seq2m(:,2)+1),2)+sum(IGd(modSyn==6,seq2m(:,1)+1).*IGd(modSyn==6,seq2m(:,2)+1),2)))-...
+                Dg(modSyn==6).*w(modSyn==6,i))./Mg(modSyn==6)/i;
+            eq1(modSyn==6,i+1)=(-eq1(modSyn==6,i)-(Xgd(modSyn==6)-Xgd1(modSyn==6)-gammad(modSyn==6)).*IGd(modSyn==6,i)+(1-TgAA(modSyn==6)./Tgd1(modSyn==6)).*Ef(modSyn==6,i))./Tgd1(modSyn==6)/i;
+            ed1(modSyn==6,i+1)=(-ed1(modSyn==6,i)+(Xgq(modSyn==6)-Xgq1(modSyn==6)-gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq1(modSyn==6)/i;
+            eq2(modSyn==6,i+1)=(-eq2(modSyn==6,i)+eq1(modSyn==6,i)-(Xgd1(modSyn==6)-Xgd2(modSyn==6)+gammad(modSyn==6)).*IGd(modSyn==6,i)+TgAA(modSyn==6)./Tgd1(modSyn==6).*Ef(modSyn==6,i))./Tgd2(modSyn==6)/i;
+            ed2(modSyn==6,i+1)=(-ed2(modSyn==6,i)+ed1(modSyn==6,i)+(Xgq1(modSyn==6)-Xgq2(modSyn==6)+gammaq(modSyn==6)).*IGq(modSyn==6,i))./Tgq2(modSyn==6)/i;
+            RhsEd(modSyn==6)=ed2(modSyn==6,i+1);
+            RhsEq(modSyn==6)=eq2(modSyn==6,i+1);
+        end
+        if modelTag(5)>0
+            d(modSyn==5,i+1)=(wgb(modSyn==5).*w(modSyn==5,i))/i;
+            w(modSyn==5,i+1)=(Pm(modSyn==5,i)-...
+                (sum(VGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(VGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)+...
+                Rga(modSyn==5).*(sum(IGq(modSyn==5,seq2m(:,1)+1).*IGq(modSyn==5,seq2m(:,2)+1),2)+sum(IGd(modSyn==5,seq2m(:,1)+1).*IGd(modSyn==5,seq2m(:,2)+1),2)))-...
+                Dg(modSyn==5).*w(modSyn==5,i))./Mg(modSyn==5)/i;
+            eq1(modSyn==5,i+1)=(-eq1(modSyn==5,i)-(Xgd(modSyn==5)-Xgd1(modSyn==5)-gammad(modSyn==5)).*IGd(modSyn==5,i)+(1-TgAA(modSyn==5)./Tgd1(modSyn==5)).*Ef(modSyn==5,i))./Tgd1(modSyn==5)/i;
+            eq2(modSyn==5,i+1)=(-eq2(modSyn==5,i)+eq1(modSyn==5,i)-(Xgd1(modSyn==5)-Xgd2(modSyn==5)+gammad(modSyn==5)).*IGd(modSyn==5,i)+TgAA(modSyn==5)./Tgd1(modSyn==5).*Ef(modSyn==5,i))./Tgd2(modSyn==5)/i;
+            ed2(modSyn==5,i+1)=(-ed2(modSyn==5,i)+(Xgq(modSyn==5)-Xgq2(modSyn==5)).*IGq(modSyn==5,i))./Tgq2(modSyn==5)/i;
+            RhsEd(modSyn==5)=ed2(modSyn==5,i+1);
+            RhsEq(modSyn==5)=eq2(modSyn==5,i+1);
+        end
+        if modelTag(4)>0
+            d(modSyn==4,i+1)=(wgb(modSyn==4).*w(modSyn==4,i))/i;
+            w(modSyn==4,i+1)=(Pm(modSyn==4,i)-...
+                (sum(VGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(VGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)+...
+                Rga(modSyn==4).*(sum(IGq(modSyn==4,seq2m(:,1)+1).*IGq(modSyn==4,seq2m(:,2)+1),2)+sum(IGd(modSyn==4,seq2m(:,1)+1).*IGd(modSyn==4,seq2m(:,2)+1),2)))-...
+                Dg(modSyn==4).*w(modSyn==4,i))./Mg(modSyn==4)/i;
+            eq1(modSyn==4,i+1)=(-eq1(modSyn==4,i)-(Xgd(modSyn==4)-Xgd1(modSyn==4)).*IGd(modSyn==4,i)+Ef(modSyn==4,i))./Tgd1(modSyn==4)/i;
+            ed1(modSyn==4,i+1)=(-ed1(modSyn==4,i)+(Xgq(modSyn==4)-Xgq1(modSyn==4)).*IGq(modSyn==4,i))./Tgq1(modSyn==4)/i;
+            RhsEd(modSyn==4)=ed1(modSyn==4,i+1);
+            RhsEq(modSyn==4)=eq1(modSyn==4,i+1);
+        end
+        if modelTag(3)>0
+            d(modSyn==3,i+1)=(wgb(modSyn==3).*w(modSyn==3,i))/i;
+            w(modSyn==3,i+1)=(Pm(modSyn==3,i)-...
+                (sum(VGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(VGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)+...
+                Rga(modSyn==3).*(sum(IGq(modSyn==3,seq2m(:,1)+1).*IGq(modSyn==3,seq2m(:,2)+1),2)+sum(IGd(modSyn==3,seq2m(:,1)+1).*IGd(modSyn==3,seq2m(:,2)+1),2)))-...
+                Dg(modSyn==3).*w(modSyn==3,i))./Mg(modSyn==3)/i;
+            eq1(modSyn==3,i+1)=(-eq1(modSyn==3,i)-(Xgd(modSyn==3)-Xgd1(modSyn==3)).*IGd(modSyn==3,i)+Ef(modSyn==3,i))./Tgd1(modSyn==3)/i;
+            RhsEd(modSyn==3)=0;
+            RhsEq(modSyn==3)=eq1(modSyn==3,i+1);
+        end
+        if modelTag(2)>0
+            d(modSyn==2,i+1)=(wgb(modSyn==2).*w(modSyn==2,i))/i;
+            w(modSyn==2,i+1)=(Pm(modSyn==2,i)-...
+                (sum(VGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(VGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)+...
+                Rga(modSyn==2).*(sum(IGq(modSyn==2,seq2m(:,1)+1).*IGq(modSyn==2,seq2m(:,2)+1),2)+sum(IGd(modSyn==2,seq2m(:,1)+1).*IGd(modSyn==2,seq2m(:,2)+1),2)))-...
+                Dg(modSyn==2).*w(modSyn==2,i))./Mg(modSyn==2)/i;
+            RhsEd(modSyn==2)=0;
+            RhsEq(modSyn==2)=eq1(modSyn==2,i+1);
+        end
+        % this part may be different from DT
+        AG0=cosp(:,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
+        if taylorN>=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);
+        end
+        if taylorN>=3
+            AG0=AG0+cosp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2);
+            BG0=BG0+sinp(:,4).*sum(d(:,seq3(:,1)+1).*d(:,seq3(:,2)+1).*d(:,seq3(:,3)+1),2);
+        end
+        if taylorN>=4
+            seq4=getseq(i,4);
+            AG0=AG0+cosp(:,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
+
+        % high order coefficients of cos(delta) and sin(delta)
+        Cd(:,i+1)=AG0;
+        Sd(:,i+1)=BG0;
+
+        VGdCr=sum(Cd(:,seq2x(:,1)+1).*VGd(:,seq2x(:,2)+1),2);% Vd*cosdta
+        VGqCr=sum(Cd(:,seq2x(:,1)+1).*VGq(:,seq2x(:,2)+1),2);% Vq*cosdta
+        VGdSr=sum(Sd(:,seq2x(:,1)+1).*VGd(:,seq2x(:,2)+1),2);% Vd*sindta
+        VGqSr=sum(Sd(:,seq2x(:,1)+1).*VGq(:,seq2x(:,2)+1),2);% Vq*sindta
+        JCr=sum(Cd(:,seq2x(:,1)+1).*JG(:,seq2x(:,2)+1),2);% similar, for currents
+        KCr=sum(Cd(:,seq2x(:,1)+1).*KG(:,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);
+
+        RHSIGxr=-(MatsRs(:,1).*(-VGdSr-VGqCr)+MatsRs(:,2).*(VGdCr-VGqSr))+...
+            (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))+...
+            (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
+        RHSIGr=accumarray(synIdx,RHSIGxr,[nbus,1]);
+        RHSIGi=accumarray(synIdx,RHSIGxi,[nbus,1]);
+    end
+    % update exciter, 3 state variables
+    if ~isempty(exc)
+        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;
+        Vavrf(:,i+1)=((vavrf0.*Vmag(synIdx(excIdx),i)+...
+            sum(Vavrr(:,seq2m(:,1)+1).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2)+...
+            muavr0.*Tavr1./Tavr2.*sum((Vavrref(:,seq2m(:,1)+1)-Vavrm(:,seq2m(:,1)+1)).*Vmag(synIdx(excIdx),seq2m(:,2)+1),2))./Vavr0-Vavrf(:,i))./Tavre/i;
+        Ef(excIdx(avrSt==-1),i+1)=0;
+        Ef(excIdx(avrSt== 1),i+1)=0;
+        Ef(excIdx(avrSt== 0),i+1)=Vavrf(avrSt==0,i+1);
+    end
+
+    % update agc, one state variables
+    if ~isempty(agc)
+        dpg(:,i+1)=-f(:,i).*agcExt(:,4)/i;
+        for islIdx=1:nIslands
+            busIsland=find(islands==islIdx);
+            synTagIsland=synTag(busIsland);
+            wIsland=w(synTagIsland(synTagIsland~=0),i+1);
+            if ~isempty(wIsland)
+                f(busIsland,i+1)=mean(wIsland); % note that here the freq can be different
+            end
+        end % TODO: steady-state model
+
+        % update generator participation part from agc
+        if ~isempty(syn) %dynamic model (synchronous generators)
+            if ~isempty(tg)
+                Tmech(:,i+1)=Tmech(:,i+1)+dpg(syn(tg(:,1),1),i+1)./numSynOnBus(syn(tg(:,1),1));
+            end
+            Pm(:,i+1)=Pm(:,i+1)+dpg(syn(:,1),i+1)./numSynOnBus(syn(:,1));
+        end
+    end
+    % update Turbine, 2 state variables
+    if ~isempty(tg)
+        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);
+
+        Pm(tgIdx(govSt==0),i+1)=tgovm(govSt==0,i+1);
+        Pm(tgIdx(govSt==1),i+1)=0;
+        Pm(tgIdx(govSt==-1),i+1)=0;
+    end
+
+    % HEM Body
+    RHS1=sum((-P(:,seq2(:,1)+1)+1j*(Q(:,seq2(:,1)+1)+Qxtra(:,seq2(:,1)+1))).*conj(W(:,seq2(:,2)+1)),2)+...
+        freqKeptTag.*sum(-dpg(:,seq2(:,1)+1).*conj(W(:,seq2(:,2)+1)),2)+...
+        freqKeptTag.*fdk.*sum(f(:,seq2R(:,1)+1).*conj(W(:,seq2R(:,2)+1)),2)+Ysh1.*V(:,i)+Ytr1*V(:,i);
+    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);
+
+
+    if i==1
+        RHS2=RHS2+0.5*VspSq2(:,2);
+    end
+
+    compactRHS1=RHS1(busType~=2);
+    compactRHS1=compactRHS1+Y(busType~=2,isw)*real(V(isw,i+1));
+    % combine all current injection involing Motor, zip load, and Generators
+    RHS=[real(compactRHS1)+RHSILr(busType~=2)+RHSIiLr(busType~=2)-RHSIGr(busType~=2);...
+        imag(compactRHS1)+RHSILi(busType~=2)+RHSIiLi(busType~=2)-RHSIGi(busType~=2);...
+        RHS2(ipv);...
+        real(RHS3(busType~=2));...
+        imag(RHS3(busType~=2));...
+        zeros(sum(freqKeptTagxRef),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
+    if useLU
+        if IS_OCTAVE
+            x = real(MxQ * MxQx* (MxU \ (MxL \ (MxP * RHS)))) ;
+        else
+            x =real( MxQ * (MxU \ (MxL \ (MxP * RHS)))) ;
+        end
+    else
+        x=real(LHS_mat\RHS);
+    end
+
+    % x= [V;W;Q_pv;f]
+    xC=real(V(:,i+1));
+    xD=imag(V(:,i+1));
+    xC(idxNonSw)=x(1:(npq+npv));
+    xD(idxNonSw)=x(((npq+npv)+1):(2*(npq+npv)));
+    V(:,i+1)=xC+1j*xD;
+    W(busType~=2,i+1)=x((2*(npq+npv)+1):(3*(npq+npv)))+...
+        1j*x((3*(npq+npv)+1):(4*(npq+npv)));
+    Q(ipv,i+1)=x((4*(npq+npv)+1):(4*(npq+npv)+npv));
+    f(freqKeptTag==1,i+1)=x((4*(npq+npv)+npv+1):end);
+
+    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
+    if ~isempty(ind)
+        %         for j=1:nInd
+        %             tempIL=squeeze(LHS_MatInd_Shr(j,:,:))*[real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))]+rhsBus(:,j);
+        %             tempIRs=-LHS_MatInd_Shr2{j}*[tempIL;real(V(indIdx(j),i+1));imag(V(indIdx(j),i+1))];
+        %             IL(j,i+1)=tempIL(1)+1j*tempIL(2);
+        %             IR(j,i+1)=tempIRs(1)+1j*tempIRs(2);
+        %             Vm(j,i+1)=V(indIdx(j),i+1)-IL(j,i+1)*Z1(j);
+        %         end
+        tempILvr=LHS_MatInd_Shr_sqz(:,1).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,3).*imag(V(indIdx,i+1))+rhsBus(1,:)';
+        tempILvi=LHS_MatInd_Shr_sqz(:,2).*real(V(indIdx,i+1))+LHS_MatInd_Shr_sqz(:,4).*imag(V(indIdx,i+1))+rhsBus(2,:)';
+        tempIRsvr=-sum(LHS_MatInd_Shr2_sqz(:,[1,3,5,7]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2);
+        tempIRsvi=-sum(LHS_MatInd_Shr2_sqz(:,[2,4,6,8]).*[tempILvr,tempILvi,real(V(indIdx,i+1)),imag(V(indIdx,i+1))],2);
+        IL(:,i+1)=tempILvr+1j*tempILvi;
+        IR(:,i+1)=tempIRsvr+1j*tempIRsvi;
+        Vm(:,i+1)=V(indIdx,i+1)-IL(:,i+1).*Z1;
+    end
+
+    % now update the Algebric variables for ZIP loads
+    if ~isempty(zip)
+        IiL(:,i+1)=(LHS_MatZip(:,1)+1j*LHS_MatZip(:,3)).*real(V(zipIdx,i+1))+(LHS_MatZip(:,2)+1j*LHS_MatZip(:,4)).*imag(V(zipIdx,i+1))+(RHSILr_full+1j*RHSILi_full);
+        BiL(:,i+1)=Mat_BZip(:,1).*real(V(zipIdx,i+1))+Mat_BZip(:,2).*imag(V(zipIdx,i+1))+RHS_BZip;
+    end
+
+    % now update the Algebric variables for Generators: Vd,Vq, Id, Iq
+    if ~isempty(syn)
+        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;
+        IGd(:,i+1)=JSr-KCr+sind.*JG(:,i+1)-cosd.*KG(:,i+1);
+        IGq(:,i+1)=JCr+KSr+cosd.*JG(:,i+1)+sind.*KG(:,i+1);
+        tempVGC=real(V(synIdx,i+1))-VGdSr-VGqCr;
+        tempVGD=imag(V(synIdx,i+1))+VGdCr-VGqSr;
+        VGd(:,i+1)=sind.*tempVGC-cosd.*tempVGD;
+        VGq(:,i+1)=cosd.*tempVGC+sind.*tempVGD;
+    end
+end
+
+% Output value: coefficients for every order.
+Q=real(Q);
+s=real(s);
+d=real(d);
+w=real(w);
+eq1=real(eq1);
+eq2=real(eq2);
+ed1=real(ed1);
+ed2=real(ed2);
+psid=real(psid);
+psiq=real(psiq);
+Pm=real(Pm);
+Ef=real(Ef);
+Vavrm=real(Vavrm);
+Vavrr=real(Vavrr);
+Vavrf=real(Vavrf);
+Vavrref=real(Vavrref);
+tgovg=real(tgovg);
+tgovm=real(tgovm);
+Tmech=real(Tmech);
+f=real(f);
+dpg=real(dpg);
+qplt=real(qplt);
+vg=real(vg);
+
+if ~isempty(exc)
+    avr={Vavrm,Vavrr,Vavrf};
+end
+
+if ~isempty(tg)
+    gov={tgovg,tgovm};
+end
+
end
+
\ No newline at end of file diff --git a/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.pdf b/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.pdf new file mode 100644 index 0000000..e97a284 Binary files /dev/null and b/internal/html/Copy_of_hemMachinePFSalientcontinueDyn.pdf differ diff --git a/internal/simulationTimeDomainDT.m b/internal/simulationTimeDomainDT.m new file mode 100644 index 0000000..bbf0f6f --- /dev/null +++ b/internal/simulationTimeDomainDT.m @@ -0,0 +1,138 @@ +function [stateCurve,t,finalAlpha,alphaList,diffList,exitFlag]=simulationTimeDomainHem(SimData,SysData,SysPara,x0) +% Numerical integration approach for dynamic simulation +% +%%FUNCTION simulationTimeDomainNI +% Author: Rui Yao +% +% Copyright (C) 2021, UChicago Argonne, LLC. All rights reserved. +% +% OPEN SOURCE LICENSE +% +% Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +% +% 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +% 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +% 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +% +% +% ****************************************************************************************************** +% DISCLAIMER +% +% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +% *************************************************************************************************** +% +% INPUT +% SimData - Simulation parameters +% SysData - System data for simulation +% SysPara - Parameters representing the events happening in the system +% x0 - Initial system state +% +% OUTPUT +% stateCurve - A list of states in the order of time +% t - A list of time points (starting with 0) +% finalAlpha - Final value of alpha +% alphaList - List of alpha (from multi-stage HE) +% exitFlag - +% 0 - Success and normally exit +% -1 - Fail to finish (due to computation errors or failures) +% 1 - System in absolute steady state +% 2 - Generator transients faded away +% 3 - Suggest using error reduction mode +% +% MODES (controlled by SimData.method) +% SimData.method has format X.YZ +% X - Method for solving differential equations (DE) +% 0 - HE +% 1 - Modified Euler +% 2 - RK4 +% 3 - Trapezoidal +% +% Y - Method for solving algebraic equations (AE) +% 0 - HE +% 1 - NR +% +% Z - Variable step (only applies to X>1) +% 0 - Fixed step +% 1 - Adaptive step +% +[bus,sw,pv,pq,shunt,line,ind,zip,syn,exc,tg,agc,cac,cluster]=unfoldSysData(SysData); +[nState,idxs]... + =getIndexDyn(SysData); +% [V0,Q0,s0,d0,w0,eq10,eq20,ed10,ed20,psid0,psiq0,Pm0,Ef0,Vavrm0,Vavrr0,Vavrf0,Vavrref0,tgovg0,tgovm0,tgovmech0]=unfoldX(x0,SysData); +% [~,~,sNew,dNew,~,eq1New,eq2New,ed1New,ed2New,psidNew,psiqNew,~,EfNew,~,~,~,~,~,~,~]=unfoldX(xNew,SysData); +[maxTime,segTime,dt,nlvl,taylorN,alphaTol,diffTol,diffTolMax,method,varOpt]=unfoldSimData(SimData); +[pqIncr,pvIncr,Rind0,Rind1,Reind0,Reind1,Rzip0,Rzip1,Ytr0,Ytr1,Ysh0,Ysh1,VspSq2,~,~,~,~,Tmech1,Varref1,Ef1,Pm1,Eq11]=unfoldSysPara(SysPara); + +nbus=size(bus,1); +nline=size(line,1); +nInd=size(ind,1); +nZip=size(zip,1); +nSyn=size(syn,1); +nTg=size(tg,1); +nExc=size(exc,1); + +if isempty(pqIncr);pqIncr=zeros(size(pq,1),2);end +if isempty(pvIncr);pvIncr=zeros(size(pv,1),1);end +if isempty(Rind0);Rind0=ones(nInd,1);end +if isempty(Rind1);Rind1=zeros(nInd,1);end +if isempty(Reind0);Reind0=ones(nInd,1);end +if isempty(Reind1);Reind1=zeros(nInd,1);end +if isempty(Rzip0);Rzip0=ones(nZip,1);end +if isempty(Rzip1);Rzip1=zeros(nZip,1);end +if isempty(Ytr0)||isempty(Ytr1)||isempty(Ysh0)||isempty(Ysh1) + [~,Ytr0x,Ysh0x,~,~,~,~]=getYMatrix(nbus,line); + if isempty(Ytr0);Ytr0=Ytr0x;end + if isempty(Ytr1);Ytr1=0*Ytr0;end + if isempty(Ysh0);Ysh0=Ysh0x;end + if isempty(Ysh1);Ysh1=0*Ysh0;end +end +if isempty(Tmech1);Tmech1=zeros(nTg,1);end +if isempty(Varref1);Varref1=zeros(nExc,1);end +if isempty(Ef1);Ef1=zeros(nSyn,1);end +if isempty(Pm1);Pm1=zeros(nSyn,1);end +if isempty(Eq11);Eq11=zeros(nSyn,1);end + +busType=zeros(nbus,1); +if isempty(pv) + pv=zeros(0,6); +end +if isempty(pq) + pq=zeros(0,6); +end +if isempty(shunt) + shunt=zeros(0,7); +end +if isempty(sw) + sw=zeros(0,13); +end +busType(pv(:,1))=1; +busType(sw(:,1))=2; + +isw=find(busType==2); +ipv=find(busType~=0); +ipq=find(busType==0); +npq=size(ipq,1); +npv=size(ipv,1); + +% yShunt=zeros(nbus,1); +% yShunt(shunt(:,1))=shunt(:,5)+1j*shunt(:,6); +% Ysh0=Ysh0+yShunt; +if isempty(VspSq2) + V0=x0(idxs.vIdx); + VspSq2=[abs(V0).*abs(V0),zeros(nbus,1)]; +end + +segAlpha=min([maxTime,segTime]); + +SysParax=foldSysPara(pqIncr,pvIncr,Rind0,Rind1,Reind0,Reind1,Rzip0,Rzip1,Ytr0,Ytr1,Ysh0,Ysh1,VspSq2,[],[],[],[],Tmech1,Varref1,Ef1,Pm1,Eq11); +SimDatax=foldSimData(maxTime,segAlpha,dt,nlvl,taylorN,alphaTol,diffTol,diffTolMax,method,varOpt); + +[t,stateCurve,finalAlpha,alphaList,diffList,exitFlag]=dtmMachinePFmultiStageDyn(SimDatax,SysData,SysParax,x0); + +end \ No newline at end of file