clear all
close all

dt = 0.1;       %timestep size
tf = 5000;
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', 6140.265,'S',3.14159,'disp',6.2832,'g',9.81);
Buoy = set(Buoy,'x0',[0 0 0]);
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) = 9722.09; I(2,2) = 9722.09; I(3,3) = 3070.13;
Buoy = set(Buoy,'I',I);
clear I;




%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:10;
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',300,'S',0,'disp',0.0654,'g',9.81);% thin plate 1m x 1m 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) =  25.11; I(2,2) = 25.11; I(3,3) = 50.0;
Clump = set(Clump,'I',I);
clear I;


mu_inf = zeros(1,6);
%mu_inf(3) = 805.03*0.25; %len = 1m       m33 = (pi.rho.len^2)/4
mu_inf(3) = 805.03;
mu_inf(5) = 25.16;                        %m55 = (pi.rho.len^4)/8
Clump = set(Clump,'mu_inf',mu_inf);
clear mu_inf;

%Compute incident wave profile
t = get(Buoy,'t');
T = 6.0;  %seconds
A = 1.0;
wave = IncWave;
wave = set(wave,'T',T);
wave = set(wave,'A',A);
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));

%Add forces to Clump
Clump = add_Force_fcn(Clump,@F_HydroStatic);
Clump = add_Force_fcn(Clump,@F_Weight);
Clump = add_Force_fcn(Clump,@F_Drag);

%Add forces that act on two bodies
p_a1 = [0 0 0];  %Tether attachment point on Buoy (local CoordSys).
p_a2 = [0 0 0];  %Tether attachment point on Clump (local CoordSys).

k = 2000;
b = 20;

Buoy = add_Force_fcn(Buoy,@(body,RB,n) SD_Tether(body,RB,n,'Clump',p_a1,p_a2,k,b));
Clump = add_Force_fcn(Clump,@(body,RB,n) SD_Tether(body,RB,n,'Surface Buoy',p_a1,p_a2,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

%Cylinder kinematics

figure('Position', [100 100 1000 600])
set(gcf,'color','white');
subplot(3,1,1),plot(t,x(:,3));
title('Cylinder radius 1m and height 2m, 6140 kg ','FontName','times','FontSize', 12);
legend('pos (m)');
xlabel('time (s)');
ylim([-5  5])


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


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

%plate kinematics

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

subplot(3,1,1),plot(t,y(:,3));
title('Thin Plate 1 m x 1 m mass = 300 Kg','FontName','times','FontSize', 12);
legend('pos (m)');
xlabel('time (s)');
ylim([-8  8])

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

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



% % heave oscillation
% 
% figure('Position', [100 100 800 800])
% set(gcf,'color','white');
% subplot(2,1,1),plot(t,x(:,3));
% title('Heave oscillation (m)','FontSize', 20,'FontWeight','bold');
% set(gca, 'FontSize',20,'FontWeight','bold');
% legend('Buoy');
% %xlabel('time (s)');
% ylim([-10 10])
% 
% subplot(2,1,2),plot(t,y(:,3));
% set(gca, 'FontSize',20,'FontWeight','bold');
% legend('Plate');
% xlabel('time (s)');
% ylim([-10 10])

%Calculating drag force on the plate

len = 1.0;
rho = 1025;
area = len^2;


%Calculating power


dv = v2-v1;


for i =1:N;
    pow(i) =dv(i,1)*dv(i,1);
    pow(i) = 0.001*b*pow(i);
    
    
    F_tether(i) = -k*(x(i,3)-y(i,3))-b*(v1(i)-v2(i));
    
    %flat plate Cd = 1.17
 
    F_drag(i) = 0.5*1.17*rho*area*(v2(i))*abs(v2(i));
    P_drag(i) = 0.001*v2(i)*F_drag(i);
end

meanpow = mean(pow);
meandrag = mean(P_drag);


figure('Position', [50 50 800 900])
set(gcf,'color','white');

subplot(3,1,1),plot(t,dv);
set(gca, 'FontSize',20,'FontWeight','bold');
title('Relative heave velocity (m/s)','FontSize',20,'FontWeight','bold');
ylim([-5 5])

for i=1:N;

    pow_wav(i) = 1.025*9.81*(2*2)*56*2.0/48; %in kW
    pow_ext(i) = meanpow;
    pow_diss(i) = meandrag;
end


subplot(3,1,2), plot(t,F_drag/1000);
set(gca, 'FontSize',20,'FontWeight','bold');
title('Drag Force (N)','FontSize', 20,'FontWeight','bold');
ylim ([-8  8])

subplot(3,1,3),plot(t,P_drag);
set(gca, 'FontSize',20,'FontWeight','bold');
title(' Power dissipated - Drag (kW)','FontSize', 20,'FontWeight','bold');
xlabel('time (s)');
ylim ([0 5])



