clear all
close all

dt = 0.1;       %timestep size
tf = 300;
N = tf/dt;      %Number of timesteps to compute

%Set up "Buoy"


Buoy = RigidBody;
Buoy = set(Buoy,'name','Surface Buoy');
Buoy = set(Buoy,'dt',dt,'N',N);  %Set timesteps
Buoy = set(Buoy,'dof',6);  %With 6 DOF.
Buoy = set(Buoy,'Fixed',0);  %Set free to move
Buoy = set(Buoy,'mass', 6440.265,'S',3.14159,'disp',6.2832,'g',9.81);
Buoy = set(Buoy,'x0',[0 0 0]);  %Equilibrium Position in global coordinates.
Buoy = set(Buoy,'xG',[0 0 -1.0]);
Buoy = set(Buoy,'xB',[0 0 -1.0]);
Buoy = set(Buoy,'S11',3.14159,'S22',3.14159);  %S11 = S22 = (-1)(-1)(3.14159) m^4
I = zeros(3);
I(1,1) = 10197.09; I(2,2) = 10197.09; I(3,3) =  3220.13;
Buoy = set(Buoy,'I',I);
clear I;
Buoy = set(Buoy,'Area',[4 4 pi]);
Buoy = set(Buoy,'Cd',[1 1 0]);
Buoy = set(Buoy,'AreaW',[1 1 0]);
Buoy = set(Buoy,'CdW',[1 1 0]);

%Read Frequency Domain Hydrodynamic Data from file.
disp('Reading frequency domain hydrodynamic coefficients'); 
FD_coefs = ReadData('tcyl3.out');
for i = 1:6 
   mu_inf(i) = real(FD_coefs.F{i,i}(end)); 
end
Buoy = set(Buoy,'mu_inf',mu_inf);
clear mu_inf;

%Compute Impulse Response Functions from Frequency Domain Results
tau = 0:dt:15;
disp('Computing impulse response functions'); 
L = ComputeIRF_coefs(FD_coefs.w,FD_coefs.F,tau);


%Set up "Clump"
Clump = RigidBody;
Clump = set(Clump,'name','Clump');
Clump = set(Clump,'x0',[0  0 -20]);  %Equilibrium Position in global coordinates.
Clump = set(Clump,'dt',dt,'N',N);  %Set timesteps
Clump = set(Clump,'dof',6);  %With 6 DOF.
Clump = set(Clump,'Fixed',0);  %Set free to move
Clump = set(Clump,'mass',603.315,'S',0,'disp',0.5886,'g',9.81);% thin square plate 2m and thickness = 0.0654 m 

Clump = set(Clump,'xG',[0 0 0]);
Clump = set(Clump,'xB',[0 0 0]);
Clump = set(Clump,'S11',0.0,'S22',0.0);  %m^4

I = zeros(3);
I(1,1) = 452.7; I(2,2) = 452.7; I(3,3) = 904.97;
Clump = set(Clump,'I',I);
clear I;

Clump = set(Clump,'Area',[9  9  9]);
Clump = set(Clump,'Cd',[0.01 0.01 1.17]);



mu_inf = zeros(1,6);     
mu_inf(1) = 0;                     %m11 = (pi.rho.a^2)times thickness, a = 1m
mu_inf(3) = 3220.1325*(1.5^2)*3;
mu_inf(5) = 402.5166*(1.5^4)*3;            %m55 = (1/8pi.rho.a^2)times thickness, a = 1m

Clump = set(Clump,'mu_inf',mu_inf);
clear mu_inf;

%Compute incident wave profile

%default values
%A = 1;
%T = 2;
%Type = 1 ( = monochromatic)

t = get(Buoy,'t');

Type = 0; %Type refers monochromatic(Type = 1) or PM spectrum (Type = 0) wave
T = 11.0;  %wave period or peak wave period in seconds
%SigH = 2.5;
wave = IncWave;
wave = set(wave,'T',T);
wave = set(wave,'Type',Type);

disp('Computing incident wave profile'); 
eta0 = eta(wave,0,[t t(end)+tau]);

% figure('Position', [100 100 900 600])
% set(gcf,'color','white');
% 
% plot(eta0)

%Add forces to Buoy
Buoy = add_Force_fcn(Buoy,@F_HydroStatic);
Buoy = add_Force_fcn(Buoy,@F_Weight);
Buoy = add_Force_fcn(Buoy,@(body,RB,n) Mem_Radiation(body,RB,n,L));
Buoy = add_Force_fcn(Buoy,@(body,RB,n) F_Exciting(body,RB,n,L,eta0));
Buoy = add_Force_fcn(Buoy,@(body,RB,n) F_Drag_Inc(body,RB,n,'Surface Buoy',wave));
Buoy = add_Force_fcn(Buoy,@(body,RB,n) F_Wind(body,RB,n,[-10 0 0]));

%Add forces to Clump
Clump = add_Force_fcn(Clump,@F_HydroStatic);
Clump = add_Force_fcn(Clump,@F_Weight);
Clump = add_Force_fcn(Clump,@(body,RB,n) F_Drag_Inc(body,RB,n,'Surface Buoy',wave));



%Add forces that act on two bodies
p_aB = [0.0  0.0  -2.0];  %Tether attachment point on Buoy (local CoordSys).
p_aC = [0.0  0.0  0.0];  %Tether attachment point on Clump (local CoordSys).

k = 5000;
b = 2000;

Buoy = add_Force_fcn(Buoy,@(body,RB,n) SD_Tether(body,RB,n,'Clump',p_aB,p_aC,k,b));
Clump = add_Force_fcn(Clump,@(body,RB,n) SD_Tether(body,RB,n,'Surface Buoy',p_aC,p_aB,k,b));


%Set Initial Conditions  %Offset position from equilibrium position.
Buoy = set_pos(Buoy,[0 0 0 0 0 0],1);
Clump = set_pos(Clump,[0 0 0 0 0 0],1);


RB{1} = Buoy;
RB{2} = Clump;


%Solve for motion
RB = Solve_RK4(RB);


Buoy = RB{1};
Clump = RB{2};

%Plot Buoy motion results
x = get(Buoy,'x');
xdot = get(Buoy,'xdot');
xddot = get(Buoy,'xddot');

v1 = xdot(:,3);

%Plot Clump motion results
y = get(Clump,'x');
ydot = get(Clump,'xdot');
yddot = get(Clump,'xddot');

v2 = ydot(:,3);

%plotting results

Bforce = get(Buoy,'Forces');
CForce = get(Clump,'Forces');


%Cylinder kinematics

figure('Position', [100 100 1000 600])
set(gcf,'color','white');

subplot(3,1,1),plot(t,x(:,1),t,x(:,3),t,x(:,5));
title('Cylinder radius 1m and height 2m, 6440.265 kg ','FontName','times','FontSize', 12);
legend('surge(m)','heave(m)','pitch(rad)');
ylim([-50 10])

subplot(3,1,2),plot(t,xdot(:,1),t,xdot(:,3));
legend('surge vel (m/s)','heave vel (m/s)');
ylim([-2 2])

subplot(3,1,3),plot(t,xddot(:,1),t,xddot(:,3));
legend('surge accel (m/s^2)','heave accel (m/s^2)');
xlabel('time (s)');
ylim([-4 4])


%plate kinematics
figure('Position', [100 100 1000 600])
set(gcf,'color','white');

subplot(3,1,1), plot(t,y(:,1),t,y(:,3),t,y(:,5));
title('Thin Square Plate 3 m  mass 603.315 Kg','FontName','times','FontSize', 12);
legend('surge(m)','heave(m)','pitch(rad)');
ylim([-50 10])

subplot(3,1,2), plot(t,ydot(:,1),t,ydot(:,3));
legend('surge vel (m/s)','heave vel (m/s)');
ylim([-2 2])

subplot(3,1,3), plot(t,yddot(:,1),t,yddot(:,3));
legend('surge accel (m/s^2)','heave accel (m/s^2)');
xlabel('time (s)');
ylim([-1.5 1.5])


dv = v2-v1; %relative velocity

%Forces
figure('Position', [100 100 1000 600])
set(gcf,'color','white');

subplot(3,1,1), plot(t,CForce{4}(:,3));
legend('Tension (N)');

subplot(3,1,2), plot(t,CForce{3}(:,3));
legend('Drag on plate (N)');

subplot(3,1,3), plot(t,dv);
title('Relative velocity (m/s)','FontName','times','FontSize', 12);
xlabel('time (s)');

%power extraction 




for i =1:N;
    pow(i) =dv(i,1)*dv(i,1);
    pow(i) = 0.001*b*pow(i);
    
    p_drag(i) = -0.001*v2(i)*CForce{3}(i,3);
end



%Compare power values
%Ignore first 5 seconds

delt = 5/dt; delt = delt+1;

meanpow = mean(pow(delt:N));
meandrag = mean(p_drag(delt:N));
rdwave = 0.001*ComputeWorkDone(Buoy,[3 4])./t';
mrdwav = mean(rdwave(delt:N));

disp(['Mean Power dissipated-drag = ',num2str(meandrag),' kW'])
disp(['Mean Power take-off at damper = ',num2str(meanpow),' kW'])
disp(['Average Wave Radiation/Diffraction Power = ',num2str(mrdwav),' kW']) 


figure('Position', [100 100 1000 600])
set(gcf,'color','white');

subplot(3,1,1), plot(t,rdwave);
title('Average Wave Radiation/Diffraction Power (kW)','FontName','times','FontSize', 12);

subplot(3,1,2), plot(t,pow);
title('Power dissipated at damper (kW)','FontName','times','FontSize', 12);

subplot(3,1,3),plot(t,p_drag);
title('Power dissipated due to drag (kW)','FontName','times','FontSize', 12);








