% This script is for loading a MUCE simulation file and 1. calculating the distance from the
% surface float distance from the anchor, 2. bending factor above cable floats, 3. mean tension below buoy 4. mean tension below anchor
% float, 4. wear factor at cable float
% and loading the data in a matrix to allow import into Excel. The array is
% named SimDataSummary

close all
clear all
num = 0
for dir_num = [5 6 7 8 9 11 12]

   if(dir_num == 5)
       scope = 1.25;
       wave = 1.3;
       period = 9.5;
        elseif(dir_num == 6)
       scope = 1.35;
       wave = 1.3;
       period = 9.5;
          elseif(dir_num == 7)
       scope = 1.25;
       wave = 1.3;
       period = 9.5;
          elseif(dir_num == 8)
       scope = 1.25;
       wave = 1.75;
       period = 10.6;
          elseif(dir_num == 9)
       scope = 1.35;
      wave = 1.75;
       period = 10.6;
          elseif(dir_num == 10)
       scope = 1.25;
       wave = 1.75;
       period = 10.6;
          elseif(dir_num == 11)
       scope = 1.25;
       wave = 2;
       period = 11.18;
          elseif(dir_num == 12)
       scope = 1.35;
      wave = 2;
       period = 11.18;
         elseif(dir_num == 13)
       scope = 1.25;
      wave = 2;
       period = 11.18;
   end
       if(dir_num < 10)
    
        mypath = ['./MUCE-600lbs-200d-24mSnubber-Inflection0',num2str(dir_num),'/'];
    
    else mypath = ['./MUCE-600lbs-200d-24mSnubber-Inflection',num2str(dir_num),'/'];
    
   end
    
   for run_num = [0 1 2 3]
    
 num = num + 1
 
        datafile = [mypath,'Res-',num2str(run_num),'c-',num2str(scope),'s-200d-',num2str(wave),'A-',num2str(period),'T-7W-8SL'];
   
    
% Load appropriate file
% Directory of first set of data files to load

load([datafile,'.mat']);
load([datafile,'-spec.mat']);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. 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
% For WEAR FACTOR, create tension time history of specified node
tension_f = (T_t(:,Bend_c));

% Calculate mean value of surface float tension
mean_t = mean(tension_t);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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)
SimData(num,:) = [dir_val,xDis,bending_f,mean_t,wear_f]

   end
end
   