clear all
close all

dt = 0.05;       %timestep size
tf = 500;
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;


%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 -5]);  %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',210.6,'S',0,'disp',0.2055,'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) = 52.72; I(2,2) =  105.30; I(3,3) = 52.72;
Clump = set(Clump,'I',I);
clear I;


mu_inf = zeros(1,6);     
mu_inf(1) = 3220.1325*0.0654;                     %m11 = (pi.rho.a^2)times thickness, a = 1m
mu_inf(3) = 3220.1325*0.0654;                    
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_Skin);


%Add forces that act on two bodies
p_aB = [0.0  0.0  0.0];  %Tether attachment point on Buoy (local CoordSys).
p_aC = [0.0  0.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_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,[1 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(:,1),t,x(:,3),t,x(:,5));
title('Cylinder radius 1m and height 2m, 6440 kg ','FontName','times','FontSize', 12);
legend('surge(m)','heave(m)','pitch(rad)');



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


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


%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('Circular Plate 1 m radius mass = 210 Kg','FontName','times','FontSize', 12);
legend('surge(m)','heave(m)','pitch(rad)');


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


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

%Check Energy Balance

 velx = ydot(:,1);
 velz = ydot(:,3);
 zcyl = x(:,3);
 DCoefx = 1;
 DCoefz = 1;
 rho = 1025;
 area = pi*(1^2); 
 
for i=1:N;
    
    F_Skinx(i) =  -0.5*DCoefx*rho*area*velx(i)*abs(velx(i));
    F_Skinz(i) =  -0.5*DCoefz*rho*area*velz(i)*abs(velz(i));
    
    Intx(i) = velx(i)*F_Skinx(i);
    Intz(i) = velz(i)*F_Skinz(i);
    
end
    E_dissx = trapz(t,Intx);
    E_dissz = trapz(t,Intz);
    E_Res   = rho*9.81*pi*trapz(zcyl,zcyl);
    
    
    





