close all
clear all



results_path = 'Results\';

name = 'DelMar_hamilton_v1';



  LAYOUT.title = name;
  
  ENV.depth = 100;
  ENV.tf = 330; 
  ENV.ramptime = 30;
  ENV.xwaveamp = 1.5;
  ENV.xwaveperiod = 10;
  ENV.xwavephase = 0;
  ENV.xwind = 8;

  ENV.dynamiclambda = -0.5;

  ENV.currentscale = 1;
  ENV.xcurrent = [0 0.515];
 
  ENV.forcing_type =  'wave-follower'; %'velocity';  % -or- 'wave-follower'
 
  ENV.input_type = 'random';  % regular -or- 'random'

  %1 = Catenary w/ stretch hose
  %2 = Catenary w/ EM Chain
  %3 = Taut w/ stretch hose
  LAYOUT.config = 3; 
              
              
              
              
  if(LAYOUT.config == 2)
     ENV.dt = 0.01;
  else
     ENV.dt = 0.04;
  end 

   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;
  PigWeight_Node = 4;

 % figure
 % subplot(2,2,1)
 % test=imread('ChainCatWStretchHose.png');  
 % image(test);

% Compute Force Vs Elongatoin of Stretch Hose (or chain)
switch LAYOUT.config
    case 1
        L0 = 8.5;
    case 2
        L0 = 13;
    case 3
        L0 = 9;
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.2*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.45*1.2*ENV.depth;
text(xpos,ypos,['Del Mar 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.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('dist. along mooring from anchor (m)');
  title('Static Tension')
  ylabel('(lbs)');

  subplot(4,4,4)  
  plot(e,TT,(L-L0)/L0,F,'rx');
  if(LAYOUT.config == 2)
      title('Chain Stretch Behavior');
      xlabel('Strain');
      ylabel('Chain Tension (lbs)');
  else
      title('Stretch Hose Behavior');
      xlabel('Strain');
      ylabel('Stretch Hose Tension (lbs)');
  end
  
subplot(4,4,5:8)
plot(t,T_t(:,Buoy_Node),t,T_t(:,ESP_Bottom_Node),t,T_t(:,PigWeight_Node));
xlabel('time (s)');
ylabel('Tension (lbs)');
legend('Tension Below Buoy','Tension Below ESP','Tension at Pig Weight');
title('Dynamic Tension');

if(LAYOUT.config == 2)
 ESP_Depth = (mean(z_t(:,ESP_Top_Node))+detrend(z_t(:,ESP_Top_Node)))-ENV.depth;
else
 ESP_Depth = z_t(:,ESP_Top_Node)-ENV.depth;  
end

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']);







