I2E_DATA Converts data from internal to external indexing. VAL = I2E_DATA(MPC, VAL, OLDVAL, ORDERING) VAL = I2E_DATA(MPC, VAL, OLDVAL, ORDERING, DIM) For a case struct using internal indexing, this function can be used to convert other data structures as well by passing in 3 or 4 extra parameters in addition to the case struct. If the value passed in the 2nd argument (VAL) is a column vector or cell array, it will be converted according to the ordering specified by the 4th argument (ORDERING, described below). If VAL is an n-dimensional matrix or cell array, then the optional 5th argument (DIM, default = 1) can be used to specify which dimension to reorder. The 3rd argument (OLDVAL) is used to initialize the return value before converting VAL to external indexing. In particular, any data corresponding to off-line gens or branches or isolated buses or any connected gens or branches will be taken from OLDVAL, with VAL supplying the rest of the returned data. The ORDERING argument is used to indicate whether the data corresponds to bus-, gen- or branch-ordered data. It can be one of the following three strings: 'bus', 'gen' or 'branch'. For data structures with multiple blocks of data, ordered by bus, gen or branch, they can be converted with a single call by specifying ORDERING as a cell array of strings. Any extra elements, rows, columns, etc. beyond those indicated in ORDERING, are not disturbed. Examples: A_ext = i2e_data(mpc, A_int, A_orig, {'bus','bus','gen','gen'}, 2); Converts an A matrix for user-supplied OPF constraints from internal to external ordering, where the columns of the A matrix correspond to bus voltage angles, then voltage magnitudes, then generator real power injections and finally generator reactive power injections. gencost_ext = i2e_data(mpc, gencost_int, gencost_orig, {'gen','gen'}, 1); Converts a GENCOST matrix that has both real and reactive power costs (in rows 1--ng and ng+1--2*ng, respectively). See also E2I_DATA, I2E_FIELD, INT2EXT.
0001 function newval = i2e_data(mpc, val, oldval, ordering, dim) 0002 %I2E_DATA Converts data from internal to external indexing. 0003 % 0004 % VAL = I2E_DATA(MPC, VAL, OLDVAL, ORDERING) 0005 % VAL = I2E_DATA(MPC, VAL, OLDVAL, ORDERING, DIM) 0006 % 0007 % For a case struct using internal indexing, this function can be 0008 % used to convert other data structures as well by passing in 3 or 4 0009 % extra parameters in addition to the case struct. If the value passed 0010 % in the 2nd argument (VAL) is a column vector or cell array, it will 0011 % be converted according to the ordering specified by the 4th argument 0012 % (ORDERING, described below). If VAL is an n-dimensional matrix or 0013 % cell array, then the optional 5th argument (DIM, default = 1) can be 0014 % used to specify which dimension to reorder. The 3rd argument (OLDVAL) 0015 % is used to initialize the return value before converting VAL to 0016 % external indexing. In particular, any data corresponding to off-line 0017 % gens or branches or isolated buses or any connected gens or branches 0018 % will be taken from OLDVAL, with VAL supplying the rest of the 0019 % returned data. 0020 % 0021 % The ORDERING argument is used to indicate whether the data 0022 % corresponds to bus-, gen- or branch-ordered data. It can be one 0023 % of the following three strings: 'bus', 'gen' or 'branch'. For 0024 % data structures with multiple blocks of data, ordered by bus, 0025 % gen or branch, they can be converted with a single call by 0026 % specifying ORDERING as a cell array of strings. 0027 % 0028 % Any extra elements, rows, columns, etc. beyond those indicated 0029 % in ORDERING, are not disturbed. 0030 % 0031 % Examples: 0032 % A_ext = i2e_data(mpc, A_int, A_orig, {'bus','bus','gen','gen'}, 2); 0033 % 0034 % Converts an A matrix for user-supplied OPF constraints from 0035 % internal to external ordering, where the columns of the A 0036 % matrix correspond to bus voltage angles, then voltage 0037 % magnitudes, then generator real power injections and finally 0038 % generator reactive power injections. 0039 % 0040 % gencost_ext = i2e_data(mpc, gencost_int, gencost_orig, {'gen','gen'}, 1); 0041 % 0042 % Converts a GENCOST matrix that has both real and reactive power 0043 % costs (in rows 1--ng and ng+1--2*ng, respectively). 0044 % 0045 % See also E2I_DATA, I2E_FIELD, INT2EXT. 0046 0047 % MATPOWER 0048 % Copyright (c) 2009-2016, Power Systems Engineering Research Center (PSERC) 0049 % by Ray Zimmerman, PSERC Cornell 0050 % 0051 % This file is part of MATPOWER. 0052 % Covered by the 3-clause BSD License (see LICENSE file for details). 0053 % See https://matpower.org for more info. 0054 0055 if ~isfield(mpc, 'order') 0056 error('i2e_data: mpc does not have the ''order'' field required for conversion back to external numbering.'); 0057 end 0058 o = mpc.order; 0059 if o.state ~= 'i' 0060 error('i2e_data: mpc does not appear to be in internal order'); 0061 end 0062 if nargin < 5 0063 dim = 1; 0064 end 0065 if ischar(ordering) %% single set 0066 if strcmp(ordering, 'gen') 0067 v = get_reorder(val, o.(ordering).e2i, dim); 0068 else 0069 v = val; 0070 end 0071 newval = set_reorder(oldval, v, o.(ordering).status.on, dim); 0072 else %% multiple sets 0073 be = 0; %% base, external indexing 0074 bi = 0; %% base, internal indexing 0075 for k = 1:length(ordering) 0076 ne = size(o.ext.(ordering{k}), 1); 0077 ni = size(mpc.(ordering{k}), 1); 0078 v = get_reorder(val, bi+(1:ni), dim); 0079 oldv = get_reorder(oldval, be+(1:ne), dim); 0080 new_v{k} = i2e_data(mpc, v, oldv, ordering{k}, dim); 0081 be = be + ne; 0082 bi = bi + ni; 0083 end 0084 ni = size(val, dim); 0085 if ni > bi %% the rest 0086 v = get_reorder(val, bi+1:ni, dim); 0087 new_v{length(new_v)+1} = v; 0088 end 0089 newval = cat(dim, new_v{:}); 0090 end