close all
clear all

name = 'DelMar_hamilton_v2';


  ENV.dynamiclambda = -0.5;
  ENV.forcing_type =  'wave-follower'; %'velocity';  % -or- 'wave-follower'
  ENV.input_type = 'random';  % regular -or- 'random'
  ENV.dt = 0.01;
  ENV.ramptime = 30;

ESP_wetweight = [-220  760 1550];  %Newtons
pigmass = [200 100 20];  %kg
net = (pigmass*9.81+ESP_wetweight)/4.45

%Current Profile Estimates from ADCP data
%profile_z = [0 -5 -10 -20 -40 -60 -80 -102];
%mean_profile = [17 17 17 15.5 12.5 10 9 9];   % Mean Profile
%max_obs_profile = [92 92 92 75 65 65 65 65];  % Max Observed Profile

  waveAmp = [4.8/2 2/2]
  wavePer = [9 14];
  seastate = 1;
  
  
  LAYOUT.title = name;

  
  ENV.depth = 100;
  ENV.tf = 300; 


  LAYOUT.stretch_hose_length = 7.0;
  

  currentscale = [2];
  
  hose = [1 2 3]; %1 = Soft  2 = Nominal  3 = Stiff
  hose_str = {'Soft','Nominal','Stiff'};
  
for i = 1:length(currentscale)
  
  for seastate = 1:2
     
  ENV.xwaveamp = waveAmp(seastate);
  ENV.xwaveperiod = wavePer(seastate);
  
    for j = 1:length(hose)  
        
     figure('Position',[1000 246 1028 1252],'PaperType','usletter','PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[0 0 8.5 11]);
        for config = 1:3
        
      LAYOUT.pigmass = pigmass(config);
      LAYOUT.ESP_wetweight = ESP_wetweight(config);
 
      ENV.currentscale = currentscale(i);
      LAYOUT.hose = hose(j);
      ENV.xwind = 5*ENV.currentscale;

      filenm = make_filename(ENV,LAYOUT,name)
      
      [segment n_length n_nodes nodes] = read_bom(filenm);

      
      
      StretchHose_Nodes = nodes(2)+1:nodes(1);  %196:225;
      ESP_Nodes = nodes(3)+1:nodes(2);  %192:195;
      PigWeight_Nodes =  nodes(7)+1:nodes(6);     %109:111;
      
      PlotNodes = [8 ... %Above ESP
                   7 ...  %Below ESP
                   4 ...  %Above Pig Weight
                   3  ... %Below Pig Weight
                   ];
          

      load([filenm '.mat']);
      ESP_x(i,j) = x(ESP_Nodes(end-1));
      ESP_z(i,j) = z(ESP_Nodes(end-1));
      ESP_waterdepth(i,j) = depth;

      BuoyDraft(i,j) = depth-z(end);
      HoseTension(i,j) = mean(T(StretchHose_Nodes))
      HoseLength(i,j) = ((x(StretchHose_Nodes(1))-x(StretchHose_Nodes(end))).^2+(z(StretchHose_Nodes(1))-z(StretchHose_Nodes(end))).^2).^.5;
      HoseStrain(i,j) = (HoseLength(i,j)-LAYOUT.stretch_hose_length)/LAYOUT.stretch_hose_length;
      TensionAboveESP(i,j) = T(ESP_Nodes(end));
      TensionBelowESP(i,j) = T(ESP_Nodes(1));
      TensionAbovePig(i,j) = T(PigWeight_Nodes(end));
      TensionBelowPig(i,j) = T(PigWeight_Nodes(1));
      TensionBelowBuoy(i,j) = T(end);

      leg_str = {['Above ESP (mean = ' num2str(mean(T_t(:,PlotNodes(1))),4) 'lbs    std dev = ' num2str(std(T_t(:,PlotNodes(1))),3) 'lbs)'],...
                 ['Below ESP (mean = ' num2str(mean(T_t(:,PlotNodes(2))),4) 'lbs    std dev = ' num2str(std(T_t(:,PlotNodes(2))),3) 'lbs)'],...
                 ['Above Pig Weight (mean = ' num2str(mean(T_t(:,PlotNodes(3))),4) 'lbs    std dev = ' num2str(std(T_t(:,PlotNodes(3))),3) 'lbs)'],...
                 ['Below Pig Weight (mean = ' num2str(mean(T_t(:,PlotNodes(4))),4) 'lbs    std dev = ' num2str(std(T_t(:,PlotNodes(4))),3) 'lbs)']};         

      
      ah(config) = subplot(3,1,config)
      plot(t,T_t(:,PlotNodes));  
      legend(leg_str)
         title(['Dynamic Tension Results for ' hose_str{hose(j)} ' Hose   ' num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight' ...
             '    H_s = ' num2str(waveAmp(seastate)*2) ' m    Period = ' num2str(wavePer(seastate)) ' s' ...
             ],'FontSize',9);
      hold all;
      if(seastate == 2)
          ylim([0 1000]);
      end
      if(seastate == 1)
          ylim([0 2500]);
      end
     end

  linkaxes(ah,'x');
  print_filenm = ['DynTensions_' hose_str{hose(j)} 'Hose' ...
             '_Hs' num2str(waveAmp(seastate)*2) '_T' num2str(wavePer(seastate)) '-hl'   num2str(LAYOUT.stretch_hose_length) ...
             ];
  print('-dpdf',[print_filenm '.pdf']);
  print('-dpng',[print_filenm '.png']);
 
  
  
  figure('Position',[1000 246 1028 1252],'PaperType','usletter','PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[0 0 8.5 11]);
      for config = 1:3
        
      LAYOUT.pigmass = pigmass(config);
      LAYOUT.ESP_wetweight = ESP_wetweight(config);
 
      ENV.currentscale = currentscale(i);
      LAYOUT.hose = hose(j);
      ENV.xwind = 5*ENV.currentscale;

      filenm = make_filename(ENV,LAYOUT,name)
      
      [segment n_length n_nodes nodes] = read_bom(filenm);

      
      
      StretchHose_Nodes = nodes(2)+1:nodes(1);  %196:225;
      ESP_Nodes = nodes(3)+1:nodes(2);  %192:195;
      PigWeight_Nodes =  nodes(7)+1:nodes(6);     %109:111;
      
            load([filenm '.mat']);
      

      ah(config) = subplot(3,1,config)
      ESP_z = z_t(1,8)+detrend(z_t(:,8))-0.5;  %The 0.5m offset is to account for the sample intake being 0.5m below the top of the ESP frame in this case.  That's how the static results report "ESP depth".
      plot(t,depth-ESP_z);  
         title(['ESP Depth Results for ' hose_str{hose(j)} ' Hose   ' num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight' ...
             '    H_s = ' num2str(waveAmp(seastate)*2) ' m    Period = ' num2str(wavePer(seastate)) ' s' ...
             ],'FontSize',9);
      xlabel('time (s)');
      ylabel('ESP depth (m)');
      
      ylim([10 20]);
      legend(['ESP depth:  mean = ' num2str(mean(depth-ESP_z),4) ' m   Std Dev = ' num2str(std(depth-ESP_z),3) ' m']);
   
     end

  linkaxes(ah,'x');

  print_filenm = ['DynMotions_' hose_str{hose(j)} 'Hose' ...
             '_Hs' num2str(waveAmp(seastate)*2) '_T' num2str(wavePer(seastate)) '-hl'   num2str(LAYOUT.stretch_hose_length) ...
             ]
  print('-dpdf',[print_filenm '.pdf']);
  print('-dpng',[print_filenm '.png']);
  
  
  
  
  
    end
  
  end
  end





