% 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 Intern 2011)
%           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
%% Script description
% Graph_reduction.m
% Reduces an EPS topology to components that are powered by a source.
function [LNK_data, Source_data, Load_data, Conv_data, EPS_Code]= Graph_reduction...
    (Source_data, TL_data, SW_data, Load_data, Conv_data, EPS_Code)
%% -------------Remove OFF SW's from SW_data------------------
u2 = 0; u3 = 0;
for u1=1:length(SW_data(:,1))           %Step thru SW_data
    if SW_data(u1,5) == 0               %If SW is OFF
        u2 = u2+1;
        SW_OFF(u2,:) = SW_data(u1,:);   %Save to SW_OFF
    else                                %If SW is ON
        u3 = u3+1;
        SW_ON(u3,:) = SW_data(u1,:);    %Save to SW_ON
    end
end
clear SW_data u1 u2 u3;
SW_data = SW_ON;                        %SW_data only include ON SW's
%
%% -----------------------Create LNK_data----------------------
LNK_data = [SW_data; TL_data]; clear SW_data TL_data;
%
%% ----------------Save Source buses to Start_bus--------------
Start_bus = Source_data(:,1);
%
%% ---------------Start Search for powered LNK's---------------
u3 = 1;     % u3: LNK_Match index
u6 = 1;     % u6: End_bus index
while length(Start_bus) ~=0
    for  u1=1:length(Start_bus)              %For every Start_bus
        u5 = 1;
        while u5 ~= 0
            match = 0; clear LNK_row LNK_bus
            for u2 = 1:length(LNK_data(:,1)) %Search LNK_data for matching bus#
                Bus_one = LNK_data(u2,3);
                Bus_two = LNK_data(u2,4);
                if Bus_one == Start_bus(u1)
                    match = match + 1;
                    LNK_row(match) = u2;           %save row number
                    LNK_bus(match) = Bus_two;      %save bus number
                elseif Bus_two == Start_bus(u1)
                    match = match + 1;
                    LNK_row(match) = u2;           %save row number
                    LNK_bus(match) = Bus_one;      %save bus number
                end
            end

            %% How many matching LNKs were found?
            if match == 1               %ONLY 1 MATCH FOUND----------------
                LNK_Match(u3,:) = LNK_data(LNK_row,:); u3 = u3+1; %save LNK to LNK_Match
                LNK_data(LNK_row,:) = LNK_data(end,:);            %replace match with last row
                u4 = length(LNK_data(:,1))-1;                     %new length of LNK_data
                LNK_data =  LNK_data(1:u4,:);                     %remove from LNK_data
                Start_bus(u1) = LNK_bus;                          %set Start_bus to LNK_bus: return to u2 for loop
            elseif match == 0           %ZERO MATCHES FOUND----------------
                End_bus(u6) = Start_bus(u1);    u6 = u6+1;        %record bus# to End_bus
                u5 = 0;                                           %Break "u5" while loop                
            elseif match >1             %MORE THAN 1 MATCH FOUND-----------
                u8 = length(Start_bus);
                u4 = length(LNK_data(:,1));
                for u7=1:match 
                    LNK_Match(u3,:) = LNK_data(LNK_row(u7),:);    %Save LNK to LNK_Match
                    u3 = u3+1;
                    LNK_data(LNK_row(u7),1) =0;
                    Start_bus(u8+u7) = LNK_bus(u7);               %add buses to Start_bus
                end              
                u9 =1; clear temp                                 %temp holds non-matching LNKs
                for u7=1:length(LNK_data(:,1))
                    if LNK_data(u7,1) ~= 0
                        temp(u9,:)=LNK_data(u7,:); u9 = u9+1;     %write non-matching LNKs to temp
                    end
                end
                LNK_data = temp;                                  %re-size LNK_data
                u5 = 0;                                           %break while loop
            end
        end
    end
    %Remove original set in Start_bus
    u8 = length(Start_bus);
    Start_bus = Start_bus(u1+1:u8);
end
clear u1 u2 u3 u4 u5 u6 u7 u8 Bus_one Bus_two match temp
%% Save remaining LNK's to LNK_OFF
LNK_OFF = LNK_data;
LNK_data = LNK_Match;
%% If Source bus matches an END_bus then source is OFF, remove and save to Source_OFF
Source_bus = Source_data(:,1); u3 = 1; u4 =1; 
for u1 = 1:length(Source_bus)
    u5 = 1; %Indicates that Source is assumed ON
    for u2 = 1:length(End_bus)
        if Source_bus(u1) == End_bus(u2)
            Source_OFF(u3,:) = Source_data(u1,:); u3 = u3+1;
            u5 = 0; %Indicates that Source is OFF
            break
        end
    end
    if u5 == 1  % If source is ON
        Source_ON(u4,:) = Source_data(u1,:); u4 = u4+1;
    end
    if exist('Source_OFF') == 0
        Source_OFF = Source_data(2:1,:);
    end
end
Source_data = Source_ON;
clear u1 u2 u3 u4 u5
%% If Load bus does not match an END_bus, then source is OFF, remove and save to Load_OFF
Load_bus = Load_data(:,1); u3 = 1; u4 =1; 
for u1 = 1:length(Load_bus)
    u5 = 0; %Indicates that Load is assumed OFF
    for u2 = 1:length(End_bus)
        if Load_bus(u1) == End_bus(u2)
            Load_ON(u3,:) = Load_data(u1,:); u3 = u3+1;
            u5 = 1; %Indicates that Load is ON
            break
        end
    end
    if u5 == 0  % If source is OFF
        Load_OFF(u4,:) = Load_data(u1,:);
        u4 = u4+1;
    else
        Load_OFF = Load_data(2:1,:); % Sets to Empty matrix: 0-by-5
    end
end
Load_data = Load_ON;
clear u1 u2 u3 u4 u5 u9
end
