close all
clear all


results_path = 'Results\';

name = 'CCE2_hamilton_v1';


%1 = Catenary w/ stretch hose
%4 = Catenary w/ stretch hose and buoyant ESP
%2 = Catenary w/ EM Chain
%3 = Taut w/ stretch hose
   LAYOUT.config = 1;  
   
%Conditions
  condition = 2;

Conditions{1}.xwaveamp = 1;
Conditions{1}.xwaveperiod = 12;
Conditions{1}.xwavephase = 0;
Conditions{1}.xwind = 8;
Conditions{1}.xcurrent = [0 0.515];
Conditions{1}.currentscale = 1;

Conditions{2}.xwaveamp = 4;
Conditions{2}.xwaveperiod = 15;
Conditions{2}.xwavephase = 0;
Conditions{2}.xwind = 21;
Conditions{2}.xcurrent = [0 0.515];
Conditions{2}.currentscale = 1;

Conditions{3}.xwaveamp = 1;
Conditions{3}.xwaveperiod = 12;
Conditions{3}.xwavephase = 0;
Conditions{3}.xwind = 8;
Conditions{3}.xcurrent = [0 0.515];
Conditions{3}.currentscale = 0.5;

Conditions{4}.xwaveamp = 4;
Conditions{4}.xwaveperiod = 15;
Conditions{4}.xwavephase = 0;
Conditions{4}.xwind = 21;
Conditions{4}.xcurrent = [0 0.515];
Conditions{4}.currentscale = 0.5;


ENV = Conditions{condition};

  LAYOUT.title = name;
  
  ENV.depth = 770;
  ENV.tf = 330; 
  ENV.dt = .02;
  ENV.ramptime = 30;
  
   
  ENV.dynamiclambda = -0.5;

 
  ENV.forcing_type =  'wave-follower'; %'velocity';  % -or- 'wave-follower'
 
  ENV.input_type = 'random';  % regular -or- 'random'

 

 filenm = [results_path make_filename(ENV,LAYOUT,name)]

 load([filenm '.mat']);
  idx = find(t>30);
  t = t(idx)-t(idx(1));
  Mb_t = Mb_t(idx,:);
  Sn_t = Sn_t(idx,:);
  phi_t = phi_t(idx,:);
  u_t = u_t(idx,:);
  v_t = v_t(idx,:);
  x_t = x_t(idx,:);
  z_t = z_t(idx,:);
  T_t = T_t(idx,:);
  
  Buoy_Node = size(x_t,2);
  ESP_Top_Node = size(x_t,2)-1;
  ESP_Bottom_Node = size(x_t,2)-2;
  Release_Node = 4;
  
  
  % Compute Force Vs Elongatoin of Stretch Hose (or chain)
switch LAYOUT.config
    case 1
        L0 = 10;
    case 4
        L0 = 10;
    case 3
        L0 = 8;
end
e = 0:.01:1;
TT = 11675*e.^2-25086*e.^3+19235*e.^4;
TT = TT/4.45;
if(LAYOUT.config == 2)
    TT = NaN(1,length(e));  %Don't plot this for Chain
end
L = ( (x_t(:,Buoy_Node)-x_t(:,ESP_Top_Node)).^2 + (z_t(:,Buoy_Node)-z_t(:,ESP_Top_Node)).^2 ).^.5;
F = (T_t(:,ESP_Top_Node)+T_t(:,Buoy_Node))/2;
  

figure('Position',[1000 246 1028 1252],'PaperType','usletter','PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[0 0 8.5 11]);

subplot(4,4,1)
  plot(x_s,z_s-ENV.depth)
  xlabel('m');
  ylabel('m');
  ylim([-ENV.depth*1.05 0.05*ENV.depth]);
  axis_limits = axis;
  %hold all
  %plot(axis_limits(1:2),[0 0],'k--');
  %axis equal;
  title('Mooring Location')
  xpos = 0.5*(axis_limits(2)-axis_limits(1));
  ypos = 0.35*1.1*ENV.depth;
text(xpos,ypos,['CCE2 Configuration ' num2str(LAYOUT.config) '     Hs = ' num2str(2*ENV.xwaveamp) ' m   Tp = ' num2str(ENV.xwaveperiod) ' s   Wind = ' num2str(ENV.xwind/.515,3) ' kts    Surface Current = ' num2str(ENV.currentscale*ENV.xcurrent(1,2)/.515) ' kt'])

subplot(4,4,2)
  plot(x_t(:,ESP_Top_Node),z_t(:,ESP_Top_Node)-ENV.depth)
  xlabel('m');
  ylabel('m');
  title('ESP Location')
  axis equal;

subplot(4,4,3)
  plot(s,T)
  xlabel('distance along mooring from anchor (m)');
  ylabel('Static Tension (lbs)');
  
subplot(4,4,4)  
  plot(e,TT,(L-L0)/L0,F,'rx');
  title('Stretch Hose Behavior');
  xlabel('Strain');
  ylabel('Stretch Hose Tension (lbs)');
      
subplot(4,4,5:8)
plot(t,T_t(:,Buoy_Node),t,T_t(:,ESP_Bottom_Node),t,T_t(:,Release_Node));
xlabel('time (s)');
ylabel('Tension (lbs)');
legend('Tension Below Buoy','Tension Below ESP','Tension at Release');

ESP_Depth = z_t(:,ESP_Top_Node)-ENV.depth;  
%ESP_Depth = (mean(z_t(:,ESP_Top_Node))+detrend(z_t(:,ESP_Top_Node)))-ENV.depth;
subplot(4,4,9:12)
plot(t,ESP_Depth);
xlabel('time (s)');
ylabel('ESP depth (m)');
legend('ESP depth');


at_t = diff(u_t)/(t(2)-t(1));
an_t = diff(v_t)/(t(2)-t(1));

subplot(4,4,13:16)
plot(t(1:end-1),at_t(:,ESP_Top_Node)/9.81,t(1:end-1),an_t(:,ESP_Top_Node)/9.81);
xlabel('time (s)');
ylabel('ESP Acceleration (g''s)');
legend('Tangential Acceleration of ESP','Normal Acceleration of ESP');

print('-dpdf',[filenm '_ResultsSummary.pdf']);