0001 function [NewGenBus,Link]=MoveExGen(mpcreduced_gen,ExBus,ExBusGen,BCIRC,acflag)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 BranchRec = [mpcreduced_gen.branch(:,[1,2]),BCIRC,mpcreduced_gen.branch(:,[3,4])];
0040 if acflag==0
0041 BranchRec(:,4)=0;
0042 end
0043
0044 BusNo = mpcreduced_gen.bus(:,1);
0045
0046 NewBusNo = (1:length(BusNo))';
0047 BusRec(:,1) = NewBusNo;
0048 BusRec = sortrows(BusRec,1);
0049
0050 tf = ismember(ExBus,ExBusGen);
0051 ExBus = ExBus(tf==0);
0052 ExBus=interp1(BusNo,NewBusNo,ExBus');
0053 tf = ismember(BusRec(:,1),ExBus);
0054 IntBus = BusRec(tf==0,1);
0055 BranchRec(:,1) = interp1(BusNo,NewBusNo,BranchRec(:,1));
0056 BranchRec(:,2) = interp1(BusNo,NewBusNo,BranchRec(:,2));
0057 BranchRec = sortrows(BranchRec,[1,2,3]);
0058
0059 Gen = mpcreduced_gen.gen;
0060 Gen(:,1)=interp1(BusNo,NewBusNo,Gen(:,1));
0061 Gen = sortrows(Gen,1);
0062
0063
0064
0065 [ignore,I]=unique(BranchRec(:,[1 2]),'rows','first');
0066 idx = find(diff(I)~=1);
0067 idx_del = [];
0068 for k = idx',
0069 z = complex(BranchRec(I(k),4),BranchRec(I(k),5));
0070 for kk = I(k)+1:I(k+1)-1
0071 z1 = complex(BranchRec(kk,4),BranchRec(kk,5));
0072 z = 1/(1/z+1/z1);
0073 end
0074 BranchRec(I(k),4) = real(z);
0075 BranchRec(I(k),5) = imag(z);
0076 idx_del = [idx_del I(k)+1:I(k+1)-1];
0077 end
0078 BranchRec(idx_del,:) = [];
0079
0080
0081 GenNum = Gen(:,1);
0082 tf = ismember(GenNum,IntBus);
0083 GenNum(tf==1)=[];
0084 LinkedBus = zeros(size(BusNo));
0085 LinkedBra = zeros(size(BusNo));
0086
0087 Level = -ones(size(BusNo));
0088 Level(IntBus) = 0;
0089
0090 Dist = inf*ones(size(BusNo));
0091 Dist(IntBus) = 0;
0092 BranchZ = sqrt(BranchRec(:,4).^2+BranchRec(:,5).^2);
0093
0094 BusPrevLayer = IntBus;
0095 BusTBD = GenNum;
0096
0097 for lev=1:1000,
0098 tf1 = ismember(BranchRec(:,1), BusPrevLayer);
0099 tf2 = ismember(BranchRec(:,2), BusTBD);
0100 ind = find(tf1&tf2);
0101 for k=ind',
0102 pi = BranchRec(k,1);
0103 gi = BranchRec(k,2);
0104 if Dist(gi)> BranchZ(k)+Dist(pi),
0105 Dist(gi) = BranchZ(k)+Dist(pi);
0106 LinkedBus(gi) = pi;
0107 LinkedBra(gi) = k;
0108 Level(gi) = Level(pi)+1;
0109 end
0110 end
0111 tf1 = ismember(BranchRec(:,2), BusPrevLayer);
0112 tf2 = ismember(BranchRec(:,1), BusTBD);
0113 ind = find(tf1&tf2);
0114 for k=ind',
0115 pi = BranchRec(k,2);
0116 gi = BranchRec(k,1);
0117 if Dist(gi)> BranchZ(k)+Dist(pi),
0118 Dist(gi) = BranchZ(k)+Dist(pi);
0119 LinkedBus(gi) = pi;
0120 LinkedBra(gi) = k;
0121 Level(gi) = Level(pi)+1;
0122 end
0123 end
0124
0125
0126
0127
0128 tf1 = ismember(BranchRec(:,1), BusTBD);
0129 tf2 = ismember(BranchRec(:,2), BusTBD);
0130 ind = find(tf1&tf2);
0131
0132
0133
0134 for k=ind',
0135 pi = BranchRec(k,1);
0136 gi = BranchRec(k,2);
0137
0138 if Dist(gi)> BranchZ(k)+Dist(pi),
0139 Level(gi) = -1;
0140 elseif Dist(pi)> BranchZ(k)+Dist(gi),
0141 Level(pi) = -1;
0142 end
0143 end
0144
0145 BusPrevLayer = find(Level==lev);
0146 if isempty(BusPrevLayer),
0147 maxLevel = lev-1;
0148 break
0149 end
0150 BusTBD = find(Level==-1);
0151 if isempty(BusTBD),
0152 maxLevel = lev;
0153 break
0154 end
0155 end
0156
0157
0158 LinkedBus(IntBus)=-1;
0159 islanded_Bus=BusNo(find(LinkedBus==0));
0160 LinkedBus(find(LinkedBus==0))=9999999;
0161
0162
0163 for i=1:length(LinkedBus)
0164 if LinkedBus(i)==-1
0165 LinkedBus(i)=i;
0166 end
0167 end
0168
0169 for lev=max(Level):-1:1
0170 ind=find(Level==lev);
0171 Level(ind)=Level(ind)-1;
0172
0173 for i=ind',
0174 p=LinkedBus(i);
0175 LinkedBus(i)=LinkedBus(p);
0176 end
0177 end
0178
0179 BusNo=[BusNo;9999999];
0180 NewBusNo=[NewBusNo;9999999];
0181 islandflag = 1;
0182 if isempty(LinkedBus(LinkedBus==9999999))
0183 islandflag = 0;
0184 LinkedBus = [LinkedBus;9999999];
0185 end
0186 LinkedBus = interp1(NewBusNo,BusNo,LinkedBus);
0187
0188 NewGenBus=interp1(BusNo,LinkedBus,mpcreduced_gen.gen(:,1));
0189 if ~islandflag
0190 LinkedBus(LinkedBus==9999999)=[];
0191 end
0192 Link = [mpcreduced_gen.bus(:,1),LinkedBus];
0193
0194 end