% Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
% 
% (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
% including any software or models in source or binary form, as well as any drawings, 
% specifications, and documentation (collectively "the Data"), to deal in the Data without 
% restriction, including without limitation the rights to use, copy, modify, merge, publish,
% distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
% is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or
% substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
% BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
% USE OR OTHER DEALINGS IN THE DATA.

% Authors:  Fernando Rodriguez                      John Finn
%           Sr Engineer/Scientist                   Contractor (Summer 2011 Intern)
%           Power Electronics Systems               Embedded Systems & Networks
%           United Technologies Research Center     United Technologies Research Center
%           411 Silver Lane                         411 Silver Lane
%           East Hartford,CT 06108                  East Hartford,CT 06108
%           rodrigf@utrc.utc.com                    ccfinnje@utrc.utc.com
%           tel: (860) 610-7102                     tel: +1 860-610-7696
%%
function [newSource_data, newTL_data, newSW_data, newLoad_data,...
    newConv_data, EPS_Code, mapping] = reorder(LNK_data,...
    Source_data, Load_data, Conv_data, EPS_Code)
    %
    % Define the new, reordered topology variables.
    newSource_data = [zeros(length(Source_data(:,1)),1), Source_data(:,2:end)];
    newLNK_data = [LNK_data(:,1:2), zeros(length(LNK_data(:,1)),2), LNK_data(:,5:end)];
    newConv_data = Conv_data;
    newLoad_data = [zeros(length(Load_data(:,1)),1), Load_data(:,2:end)];
    
    % This algorithm traverses the topology using a Breadth First Search 
    % (BFS) algorithm re-numbering the nodes using the counter variable, 
    % count. Initialize count as 1. Assumes the topology is a tree meaning
    % all loops in the topology include the gound bus.
    count = 1;
    % Perform a BFS for each source.
    for u1 = 1 : length(Source_data(:,1))
        % Initialize the queue, Q, with the source node
        Q = Source_data(u1,1);
        % Update the source node to the current count value
        newSource_data(u1,:) = [count Source_data(u1,2:end)];
        % Initialize the loop control variable
        go = 1;
        % Start BFS for the current source
        while go % while the queue is not empty
            % Extract the next node on the Q
            v = Q(1,1);
            
            % Creat the mapping entry which shows the mapping from the
            % original topology to the re-ordered topology. 
            mapping(count,:) = [v, count];

            % Search LNK_data for v
            for u2 = 1 : length(LNK_data(:,1))
                if LNK_data(u2,3) == v              % Found the node in the links
                    % Only add the opposite node to Q if it is towards the load 
                    if LNK_data(u2,4) ~= -1         
                        Q = [Q, LNK_data(u2,4)];    % Append the Q with the opposite node
                    end
                    newLNK_data(u2,3) = count;      % Update the node to the current count value
                    LNK_data(u2,3) = -1;            % Mark the node as found
                elseif LNK_data(u2,4) == v          % Found the node in the links
                    % Only add the opposite node to Q if it is towards the load 
                    if LNK_data(u2,3) ~= -1
                        Q = [Q, LNK_data(u2,3)];    % Append the Q with the opposite node
                    end
                    newLNK_data(u2,4) = count;      % Update the node to the current count value
                    LNK_data(u2,4) = -1;            % Mark the node as found
                end
            end

            % Search Conv_data for v
            for u2 = 1 : length(Conv_data(:,1))
                if Conv_data(u2,2) == v             % Found the node in the Converters
                    % Only add the opposite node to Q if it is towards the load 
                    if Conv_data(u2,3) ~= -1
                        Q = [Q, Conv_data(u2,3)];   % Append the Q with the opposite node
                    end
                    newConv_data(u2,2) = count;     % Update the node to the current count value
                    Conv_data(u2,2) = -1;           % Mark the node as found
                elseif Conv_data(u2,3) == v         % Found the node in the Converters
                    % Only add the opposite node to Q if it is towards the load 
                    if Conv_data(u2,2) ~= -1
                        Q = [Q, Conv_data(u2,2)];   % Append the Q with the opposite node
                    end
                    newConv_data(u2,3) = count;     % Update the node to the current count value
                    Conv_data(u2,3) = -1;           % Mark the node as found
                end
            end       

            % Search Load_data for v
            for u2 = 1 : length(Load_data(:,1))     
                if Load_data(u2,1) == v             % Found the node in the Loads
                    newLoad_data(u2,1) = count;     % Update the node to the current count value
                end
            end 

            % If the Q is empty, stop the while loop
            % An empty Q will have one element since it has not been
            % removed yet.
            if length(Q(1,:)) == 1  % Empty Q
                go = 0;             % Stop the loop
                clear Q;            % Clear %
            else                    % The Q is not empty
                Q = Q(1,2:end);     % Remove the first item in the Q
            end
            count = count + 1;      % Increment the re-ording counter
        end
    end

    % Temporary code to separate the Switches and T-lines in newLNK_data
    u2 = 1;
    u3 = 1;
    for u1 = 1 : length(newLNK_data(:,1))
        if imag(newLNK_data(u1,5)) > 0
            newTL_data(u2,:) = newLNK_data(u1,:);
            u2 = u2 + 1;
        else
            newSW_data(u3,:) = newLNK_data(u1,:);
            u3 = u3 + 1;
        end
    end
end



