% Delivered to the 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.
% 

% This script auto-generates a steady state design point model using an
% excel input file

global connections
global n_connections
global vertex_state
global gas_path
global disconnected_mechanical_path
global n
global b_count
global bifurcation
global current_search_id

[component_num, component_name, component_type]  = textread('Enumeration_1_NT.txt','%f%s%f');
[connection_num, connection_o, connection_d, connection_type] = textread('Enumeration_1_LT.txt', '%f%f%f%f','headerlines',1);

connections = [connection_o connection_d connection_type];

n_components = size(component_num);
n_components = n_components(1);

n_connections = size(connection_num);
n_connections = n_connections(1);

% Connection Types
gas_id = 1;         % Gas connection type Identifier
mechanical_id = 2;  % Mechanical connection type Identifier
lubrication_id = 3; % Lubrication connection type Identifier 

% Turbomachinery Component Types 
s_p = 1;            % Shaft Power Provider
s_c = 2;            % Shaft Power Consumer
s_n = 3;            % Shaft Power Neutral

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Overall Connectivity

% Initialize DSM-s 
Gas_DSM = zeros(n_components,n_components);
Mechanical_DSM = zeros(n_components, n_components);
Lubrication_DSM = zeros(n_components, n_components);

for count = 1:n_connections
    
    if connections(count,3) == gas_id
        
        Gas_DSM(connections(count,1),connections(count,2)) = 1;
    
    elseif connections(count,3) == mechanical_id
        
        Mechanical_DSM(connections(count,1),connections(count,2)) = 1;
        
    elseif connections(count,3) == lubrication_id
        
        Lubrication_DSM(connections(count,1),connections(count,2)) = 1;

    end
    
end

% Gas Connectivity Analysis

% Find Bifurcations in Gas Path
b_count = 0;            %   Initialize Bifurcation Count


for count1 = 1:n_components
    if sum(Gas_DSM(count1,:))>1
        l_count = 0;
        local_connections = 0;
        b_count = b_count+1;
        
        for count2 = 1:n_components
            if Gas_DSM(count1,count2) == 1
                l_count = l_count+1;
                local_connections(l_count) = component_num(count2);
            end
        end
        
        bifurcation(b_count,1:l_count+1) =[component_num(count1) local_connections];
    end
end
      
% Conduct a Depth First Search to Find Disconnected Subgraphs
current_search_id  = gas_id;                            % Initialize Sought Connection Type
vertex_state = [component_num ones(n_components,1)];    % Initialize Visited State
gas_path = 0;                                           % Initialize Spool Count
tree_structure = [];                                    % Initialize Spool Component Matrix

for c = 1:n_components 

    if vertex_state(component_num(c),2) ==1  
        
        n=1;
        gas_path = gas_path +1;
        [vertex_state, tree_structure] = DFS_gas(component_num(c), tree_structure);
        
    end
    
end

gas_tree_structure = tree_structure;
gas_tree_size = size(gas_tree_structure,2);

% Add bifurcation source to all 
for count1 = 1:gas_path
    for count2 = 1:b_count
    if gas_tree_structure(count1,1) == bifurcation(count2,2) || gas_tree_structure(count1,1) == bifurcation(count2,3)
    gas_tree_structure(count1,1:gas_tree_size+1) = [bifurcation(count2,1) gas_tree_structure(count1,1:gas_tree_size)]; 
    end
    end
end

% Mechanical Connectivity Analysis
current_search_id  = mechanical_id;                     % Initialize Sought Connection Type
vertex_state = [component_num ones(n_components,1)];    % Initialize Visited State
disconnected_mechanical_path = 0;                       % Initialize Spool Count
tree_structure = [];                                    % Initialize Spool Component Matrix

for c = 1:n_components 

    if vertex_state(component_num(c),2) ==1
        n=1;
        disconnected_mechanical_path = disconnected_mechanical_path +1;
        [vertex_state, tree_structure] = DFS_mechanical(component_num(c), tree_structure);
        
        if n<2 
            tree_structure(disconnected_mechanical_path,:) = [];   % No connections equals no spool
            disconnected_mechanical_path = disconnected_mechanical_path-1;
        end
        
        
    end
    
    mechanical_tree_structure = tree_structure;
end