% 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 reads the AE worksheet and outputs the DSM and nodes matrices
%for the RLC circuit example problem
%
%Written by:  Blake Moffitt 3/10/2011
%
%BEGIN USER INPUT
%DEFINE THE FILENAME AND SHEET FROM THE ARCH ENUM
%filename = 'DemoGGF DARPA META-II PI Mtg RLC_1R_V6a.xlsm';
%sheet = 'ArchNT';
filename = 'ArchNT.csv';    %saving ArchNT sheet as a csv file - Kaushik
%LABEL DEVICES AND NODES IN CELL ARRAY
Device_a = {'Vin', 'R1', 'L', 'C', 'Vout'};
nodes_a = {'a', 'b', 'c', 'd', 'e'};
%END User Input
%%

%READ THE FILE
%[num, ArchNTtxt] = xlsread(filename,sheet);
%[num, ArchNTtxt] = xlsread(filename);
ArchNTtxt = importdata(filename);   %using ArchNT.csv as input file and read in the strings - Kaushik
num_enumerated_architectures = size(ArchNTtxt,1)
DSM_size = size(Device_a,2)
node_size = size(nodes_a,2);

for i = 1:size(ArchNTtxt,1); %Loop through architectures
    fprintf('WRITING DSM #= %i\n',i);
    DSM = zeros(DSM_size);
    
    %create Dev_str  (cell array with node letters for each device)
    for j=1:DSM_size;
        Dev_str_length = size(Device_a{j},2);
        str_start = findstr(ArchNTtxt{i},Device_a{j}) + Dev_str_length+1; %1 for underscore
        str_end = str_start + 1;
        Dev_str{j} = ArchNTtxt{i}(str_start:str_end);
    end
    
    %fill out the DSM
    for j=1:DSM_size
        clear adjnode1 adjnode2;
        adjnode1 = strfind(Dev_str,Dev_str{j}(1)); %find devices adjacent to input node
        adjnode2 = strfind(Dev_str,Dev_str{j}(2)); %find devices adjacent to output node

        for k=1:DSM_size
            if isempty(adjnode1{k}) == 0; %if the string is not empty, put 1 in DSM
                DSM(j,k) = 1;
            end
            if isempty(adjnode2{k}) == 0; %if the string is not empty, put 1 in DSM
                DSM(j,k) = 1;
            end
        end
        DSM(j,j) = 0; %set diagonal to zero
    end
    
    %Fill out the nodes matrix, assume +1 for input, -1 for output
    node_mat = zeros(DSM_size);
    for j=1:DSM_size
        irowp = strmatch(Dev_str{j}(1),nodes_a);
        irowm = strmatch(Dev_str{j}(2),nodes_a);
        node_mat(irowp,j) = 1;
        node_mat(irowm,j) = -1;
    end
    
    %FILL OUT THE DSM WITH HEADERS
    for k=2:(DSM_size + 1)
      DSM_a{1,k} = Device_a{k-1};
      DSM_a{k,1}= Device_a{k-1};
      for m = 2:(DSM_size + 1)
          DSM_a{k,m} = DSM(k-1,m-1);
      end
    end
    
    %FILL OUT THE NODE MATRIX WITH HEADERS
    for k=2:(DSM_size + 1);
        node_mat_a{1,k} = Device_a{k-1};
        for m = 2:(node_size + 1);
            node_mat_a{m,1} = nodes_a{m-1};
            node_mat_a{k,m} = node_mat(k-1,m-1);
        end
    end

    %WRITE OUT THE EXCEL FILES
%     eval(['xlswrite(''Arch_RLC_',int2str(i),'.xls'',DSM_a,''DSM'');']);
%     eval(['xlswrite(''Arch_RLC_',int2str(i),'.xls'',node_mat_a,''Nodes'');']);
    
    %WRITE OUT DSM AND NODES as CSV Files
    eval(['fid = fopen(''Arch_RLC_DSM_',int2str(i),'.csv'',''w'');']);
    ndevices = size(Device_a,2);
    fprintf(fid,'%s',' '); fprintf(fid,',%s',Device_a{:}); fprintf(fid,'\n'); %HEADER
    for j=1:ndevices %BODY OF DSM
        fprintf(fid,'%s',Device_a{j});
        fprintf(fid,',%i',DSM(j,:));
        fprintf(fid,'\n');
    end
    fclose(fid);
    
    eval(['fid2 = fopen(''Arch_RLC_NODES_',int2str(i),'.csv'',''w'');']);
    fprintf(fid2,'%s',' '); fprintf(fid2,',%s',Device_a{:}); fprintf(fid2,'\n'); %HEADER
    for j = 1:node_size
        fprintf(fid2,'%s',nodes_a{j});
        fprintf(fid2,',%i',node_mat(j,:));
        fprintf(fid2,'\n');
    end
    fclose(fid2);
end


%fclose(fid);