0001 function mpc = order_radial(mpc)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 define_constants;
0017
0018 slack = mpc.bus(mpc.bus(:,BUS_TYPE) == REF, 1);
0019 [f, t] = deal(mpc.branch(:,F_BUS),mpc.branch(:,T_BUS));
0020 nl = size(mpc.branch,1);
0021 branch_number = (1:nl)';
0022 mpc.branch_order = [];
0023 mpc.loop = [];
0024 mpc.bus_order = slack;
0025
0026 iter = 1;
0027 while ~isempty(f) && iter <= nl
0028
0029
0030
0031
0032 mf = ismember(f,mpc.bus_order);
0033 mt = ismember(t,mpc.bus_order);
0034 is_loop = mf & mt;
0035
0036 if any(is_loop)
0037 mpc.loop = [mpc.loop; branch_number(is_loop)];
0038 mf(is_loop) = [];
0039
0040 f(is_loop) = [];
0041 t(is_loop) = [];
0042 branch_number(is_loop) = [];
0043 end
0044
0045 if any(mf)
0046 u = unique(t(mf));
0047 [junk,i] = intersect(t.*mf,u);
0048 mpc.bus_order = [mpc.bus_order; t(i)];
0049
0050 mpc.branch_order = [mpc.branch_order; branch_number(i)];
0051 mf(i) = [];
0052
0053 f(i) = [];
0054 t(i) = [];
0055 branch_number(i) = [];
0056 end
0057
0058 if any(mf)
0059 mpc.loop = [mpc.loop; branch_number(mf)];
0060 f(mf) = [];
0061 t(mf) = [];
0062 branch_number(mf) = [];
0063 end
0064
0065
0066
0067
0068 mf = ismember(f,mpc.bus_order);
0069 mt = ismember(t,mpc.bus_order);
0070 is_loop = mf & mt;
0071
0072 if any(is_loop)
0073 mpc.loop = [mpc.loop; branch_number(is_loop)];
0074 mt(is_loop) = [];
0075
0076 f(is_loop) = [];
0077 t(is_loop) = [];
0078 branch_number(is_loop) = [];
0079 end
0080
0081 if any(mt)
0082 u = unique(f(mt));
0083 [junk,i] = intersect(f.*mt,u);
0084 mpc.bus_order = [mpc.bus_order; f(i)];
0085
0086 mpc.branch_order = [mpc.branch_order; branch_number(i)];
0087
0088 mt(i) = [];
0089 f(i) = [];
0090 t(i) = [];
0091 branch_number(i) = [];
0092 end
0093
0094 if any(mt)
0095 mpc.loop = [mpc.loop; branch_number(mt)];
0096 f(mt) = [];
0097 t(mt) = [];
0098 branch_number(mt) = [];
0099 end
0100 iter = iter + 1;
0101 end
0102 if ~isempty(f)
0103 mpc.not_connected = branch_number;
0104 else
0105 mpc.not_connected = [];
0106 end
0107
0108 if isempty(mpc.loop)
0109
0110 mpc.branch = mpc.branch(mpc.branch_order,:);
0111
0112 mpc.bus_order_inv = sparse(mpc.bus_order,ones(nl+1,1),1:nl+1);
0113
0114 [f, t] = deal(mpc.branch(:,F_BUS),mpc.branch(:,T_BUS));
0115 f = mpc.bus_order_inv(f);
0116 t = mpc.bus_order_inv(t);
0117
0118 mpc.br_reverse = f > t;
0119 [f(mpc.br_reverse), t(mpc.br_reverse)] = deal(t(mpc.br_reverse), f(mpc.br_reverse));
0120
0121 mpc.branch(:,[F_BUS T_BUS]) = [f t];
0122
0123 mpc.branch_order_inv = sparse(mpc.branch_order,ones(nl,1),1:nl);
0124
0125 mpc.bus = mpc.bus(mpc.bus_order,:);
0126 mpc.bus(:,BUS_I) = mpc.bus_order_inv(mpc.bus(:,BUS_I));
0127
0128 mpc.gen(:,GEN_BUS) = mpc.bus_order_inv(mpc.gen(:,GEN_BUS));
0129 end