% This script is for loading a MUCE simulation file and populating with: 1.run num., 2. calculating the distance from the
% surface float distance from the anchor, 3. bending factor above cable floats, 4. mean tension below buoy 4. mean tension below anchor
% float, 5. Mean tension below anchor float. 6. Wear factor at anchor float
% and loading the data in a matrix to allow import into Excel. The array is
% named SimDataSummary

close all
clear all
% Define run parameters so proper file is loaded

           H_S = [ 2.5 2.5 3.5 3.5 4 4 4.5 4.5 ];  %meters
           PER = [ 7.9 7.9 9.3 9.3 10 10 10.6 10.6 ];  %Seconds
           SCOPES = [1.25 1.35 1.25 1.35 1.25 1.35 1.25 1.35];
          
    num = 0
    
for dir_num = [1 2 3 4 5 6 7 8]
   
    suffix = ['MUCE-600lbs-200d-24mSnubber-AsBuilt',num2str(dir_num)];  
 
  path = ['./',suffix,'/']; 

    scope = SCOPES(dir_num);
    wave_per = PER(dir_num);
    wave_amp = H_S(dir_num)/2 ;
    
      
for run_num = [1 2 3 4]
    
    
    
% Load appropriate file
datafile = ['Res-',num2str(run_num),'c-',num2str(scope),'s-200d-',num2str(wave_amp),'A-',num2str(wave_per),'T-7W-8SL'];
load([path,datafile,'.mat']);
load([datafile,'-spec.mat']);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2. Calculate distance from anchor for surface float
xDis = x(end,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3. Calculate bend factor at node above cable floats
% Enter node number
%Bend_n where bending appears most severe in cable, approx. 1.9 meters past
%the end of the floated cable section.

bend_n = [last_FC_node+17]

% Create array x_b of the actual node numbers of bending nodes of interest
 x_b = [first_FC_node-10:first_UC_node+150]; % For floated section

% Create subset array of the actual Lagrangian position for bending nodes of interest 
s_b = s(x_b)';

% Create array of Lagrangian distance between bending nodes of interest, s_diff
s_diff = diff(s_b')';

% Create array of the nodes_of_interest array numbers that corrospond to
% the actual node numbers
[c, nodes_bending, ib] = intersect(nodes,x_b);

% Create array of angle differences between section of nodes_of_interest, deg_diff_t
% for section of interest nodes_bending
rad_diff_t = diff(phi_t(:,nodes_bending)')';

% Calculate degrees per meter by dividing deg_diff by s_diff (=node
% spacing). s_diff array is expanded to match size of deg_diff_t for
% function to work OK.
rad_m = rad_diff_t./(ones(length(rad_diff_t),1)*s_diff);

% Convert to degrees
deg_m = rad_m(:,:)*(180/pi);

% Find the full resolution deg_m array number that corrosponds to
% the actual node number bend_n.

[c, node_b, ib] = intersect(x_b,bend_n);

% Find the Lagrangiam position that corrosponds to
% the actual node number bend_n.

node_s = s(bend_n);

bending_s = ['Lagrangian Position: ',num2str(node_s,3)];

% Create bending time history of specific node
bending_t = (deg_m(:,node_b));

% Calculate local extrema in the signal time history
bending_e = sig2ext(bending_t);

% Rainflow analysis
rf = rainflow(bending_e);

% Calculate "total bending travel" at specific node based on 2 X the sum of the rainflow bending cycle
% amplitudes in rf row 1 X mean tension

bending_f = 2 * sum(rf(1,:));
bending_str = ['Total Bending Travel: ',num2str(bending_f,3)];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4. Calculate mean tension below surface float
% Use the end node -1

node_f = size(T_t) % find size of tension matrix
tension_f = (T_t(:,node_f(1,2))); % use size of matrix -1 to identify node. NO! Changed to last node
% because nodes of interest was broken and short last 136 nodes.

% Calculate mean value of surface float tension
mean_tf = mean(tension_f);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 5. Calculate mean tension and wear factor at node below anchor float
% Chain node is 6 and anchor node is 7
Bend_c = 6 ;% for chain node
Bend_f = 7 ;% for anchor float

% Create array of angle differences between chain and anchor nodes
rad_diff_t = diff(phi_t(:,Bend_c:Bend_f)')' ;

% Convert Radians to degree angle between nodes
deg_t = rad_diff_t(:,:)*(180/pi);

% Calculate local extrema in the signal time history
bending_e = sig2ext(deg_t);

% Rainflow analysis
rf = rainflow(bending_e);

% For WEAR FACTOR, create tension time history of specified node
tension_t = (T_t(:,Bend_c));

% Calculate mean value of tension
mean_t = mean(tension_t);

% Calculate "wear factor" at specific node based on 2 X the sum of the rainflow bending cycle
% amplitudes in rf row 1 X mean tension

wear_f = mean_t * 2 * sum(rf(1,:));

%create name for current data set

run_str = [num2str(dir_num),num2str(run_num)]
dir_val = str2num(run_str)

% load array
SimData(num,:) = [dir_val,xDis,bending_f,mean_tf,mean_t,wear_f]

   end
end
   