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