clear all
close all
clc


file = fopen('C:\School_work\ESM_4714\Element_Node_numbers.txt')

FE_node_list = dlmread('C:\School_work\ESM_4714\Node_loc_for_ansys.txt');


%This is just a list of the node numbers that are used in the FE model

node_list = FE_node_list(:,1);
clear FE_node_list


% We know that the largets element number is 2156 but there are only 1936 Elements

for n=1:2:1936*2;
        
        eval(sprintf('E = str2num(fgetl(file));'));
        eval(sprintf('N = str2num(fgetl(file));'));

        x = (n+1)/2;

        if E(2)==95 % 8 Node Shell Elements
            Elem_no_type_nodes(x,:) = [E(1) E(2) N];
        elseif E(2) == 121% 2 Node Rigid Bars 
            Elem_no_type_nodes(x,:) = [E(1) E(2) N 0 0 0 0 0 0 ];
        elseif E(2) == 136% 2 Node Translational Springs
            Elem_no_type_nodes(x,:) = [E(1) E(2) N 0 0 0 0 0 0 ];
        end
    
        
end      
  
% Looking for which elements contain a certain node?

nodes = [3535 3523 3509 3497 4553 4997 5515 3875 3845 4859]

%This will find the element and tell you which position the node is in on the element, 
% for used with another program to find the strain at a node, but the strain are given per element.
% So we need to know what elements contain certain nodes and what position that node is, in the element.

for k = 1:length(nodes)
         counter= 0;
    for i = 1:length(Elem_no_type_nodes)
        
        for j=3:10
                
          if Elem_no_type_nodes(i,j) == nodes(k)
              
                counter = counter+1;
                
                position(counter,:) = [Elem_no_type_nodes(i,1) j-2]
                
                Node_Elem_Pos(length(position)*(k-1)+counter,:) = [nodes(k) position(counter,:)]
               
            end
            
        end
        
    end    
    
end


%  dlmwrite('C:\School_work\ESM_4714\Elem_and_nodes_for_ansys.txt',Elem_no_type_nodes,'\t')
% % 
%  dlmwrite('C:\School_work\ESM_4714\Node_Elem_Position_in_Elem.txt',Node_Elem_Pos,'\t')
% 


%This will find the element and tell you which position the node is in on the element, 
% for used with another program to find the strain at a node, but the strain are given per element.
% So we need to know what elements contain certain nodes and what position that node is, in the element.


for k = 1:length(node_list)
    counter= 0;
    for i = 1:length(Elem_no_type_nodes)
        
        for j=3:10
            
            if Elem_no_type_nodes(i,j) == node_list(k)
                
                counter = counter+1;
                
                position(counter,:) = [Elem_no_type_nodes(i,1) j-2];
                
                All_Node_Elem_Pos(k,counter,:) = [node_list(k) position(counter,:)];
                
                % So this comes out in the form of (Element Number, Nodes Participating in the Element, Position in Element)
            end
            
        end
        
    end    
    
end

%save All_Node_Elem_Pos

% save C:\\School_work\\ESM_4714\\All_Nodes_Elems_Position_in_Elems All_Node_Elem_Pos 
 
% So this comes out in the form of (Element Number, Nodes Participating in the Element, Position in Element)