close all
clear all

tf = 240;
dt = .05;

sw_density = 1025;


%Pierson Moskowitz Spectrum
           H_S = [1 2 3];  %meters
           PER = [6.0 7.5 9.0];  %Seconds
           SPD = [0 0 0];  %meters/sec
          

AMP = H_S/2;   %height -> amp

  
suffix = 'TEST';   
 
path = ['./',suffix,'/'];
  
PTO_k = 1750;
depth = 30;

run_num = 2;
wave_amp = AMP(run_num);
wave_per = PER(run_num);
wind_spd = SPD(run_num);

PTO_b = 14000;

results_filenm = [path,'Res-',num2str(depth),'d-',num2str(PTO_k),'k-',num2str(wave_amp),'A-',num2str(wave_per),'T-',num2str(wind_spd),'W-',num2str(PTO_b),'b']

load([results_filenm '.mat'])
load([results_filenm '-SPEC.mat'])


dL_t = sqrt(diff(x_t,1,2).^2+diff(z_t,1,2).^2);
e_t = dL_t/.2;

dL_PTO = sqrt((x_t(:,150)-x_t(:,125)).^2+(z_t(:,150)-z_t(:,125)).^2);


dLdot_PTO = [0; diff(dL_PTO)./diff(t)];
pow_PTO = PTO_b*dLdot_PTO.^2;

figure
plot(x_t',z_t',x_t(:,126)',z_t(:,126)','kx');



figure
plot(t,phi_t(:,120:126)*180/pi,t,phi_t(:,126:130)*180/pi,'-x')
xlabel('time (s)')
ylabel('Cable angle (degrees)');

figure('Position',[520   142   843   956],'PaperOrientation','landscape','PaperPositionMode','auto');
ah1 = subplot(3,1,1)
plot(t,180*(phi_t(:,127)-phi_t(:,125))/pi);
xlabel('time (s)')
ylabel('Cable bend at end of PTO (degrees)');
ah2 = subplot(3,1,2)
plot(t,T_t(:,126).*sin(phi_t(:,127)-phi_t(:,125)));
title('Side loading at end of PTO');
xlabel('time (s)');
ylabel('Side Load (lbs)');
ah3 = subplot(3,1,3)
plot(t,dL_PTO-5);
xlabel('time (s)');
ylabel('PTO Elongation (m)');

linkaxes([ah1 ah2 ah3],'x');

figure('Position',[520   142   843   956],'PaperOrientation','landscape','PaperPositionMode','auto');
plot(dL_PTO-5,T_t(:,126).*sin(phi_t(:,127)-phi_t(:,125)))
title('Side loading versus PTO elongation');
xlabel('Elongation (m)');
ylabel('Side Loading (lbs)');



figure
plot(abs(180*(phi_t(:,127)-phi_t(:,125))/pi),T_t(:,126));
title('Tension versus cable bend at end of PTO');
xlabel('Cable bend at end of PTO (degrees)');
ylabel('tension (lbs)');


figure
plot(t,z_t(:,1)-mean(z_t(:,1)),t,z_t(:,125)-mean(z_t(:,125)),t,z_t(:,150)-mean(z_t(:,150)))
xlabel('time (s)');
ylabel('Excursions from Mean Depth (m)');
legend('Plate','Bottom of PTO','Buoy');



figure
plot(t,dL_t)

figure
plot(t,e_t)


figure
plot(t,dLdot_PTO);

figure
plot(t,pow_PTO)
mean(pow_PTO)





