clear all
close all

dt = 0.1;       %timestep size
tf = 200;
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',1121.734,'S',3.83274,'disp',1.475,'g',9.80665);
Buoy = set(Buoy,'x0',[0 0 0]);  %Equilibrium Position in global coordinates.
Buoy = set(Buoy,'xG',[0 0 -0.63]);
Buoy = set(Buoy,'xB',[0 0 -0.29]);
Buoy = set(Buoy,'S11',0.2208,'S22',.2208);  %S11 = S22 = (-1)(-1)(3.14159) m^4
I = zeros(3);
I(1,1) = 1998.19; I(2,2) = 1947.66; I(3,3) = 466.13;      
Buoy = set(Buoy,'I',I);
clear I;
Buoy = set(Buoy,'Area',[0.1414 0.1414 3.83274]);
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.

filename = 'test25';
disp('Reading frequency domain hydrodynamic coefficients');
FD_coefs = ReadWamitFD(filename);
for i = 1:6 
   for j = 1:6 
      mu_inf(i,j) = 1025*FD_coefs.X{i,j}(2); 
   end
end
Buoy = set(Buoy,'mu_inf',mu_inf);
clear mu_inf;

%Compute Impulse Response Functions from Frequency Domain Results

disp('Computing impulse response functions'); 
L = ComputeWamitIRF_coefs(filename);


%Set up "Clump"
radius = 0.3556;  %in m ....Spherical shell of 28 inches diameters
mass = 532.971;  %kg

Clump = RigidBody;
Clump = set(Clump,'name','Clump');
Clump = set(Clump,'x0',[0  0 -7]);  %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',mass,'S',0,'disp',(4/3)*pi*radius^3,'g',9.81);% thin square plate 2m and thickness = 0.0254 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


%Sphere

I = zeros(3);
I(1,1) = 26.958; I(2,2) = 26.958; I(3,3) = 26.958;
Clump = set(Clump,'I',I);
clear I;
Clump = set(Clump,'Area',[0.3973  0.3973  0.3973]);     %projected area in x,y,z plane = pi.r^2
Clump = set(Clump,'Cd',[0.5 0.5 0.5]);

mu_inf = zeros(6,6);     
mu_inf(1,1) = (4/3)*3220.1325*(0.3556)^3;            %m11 = (4/3).pi.rho.r^3
mu_inf(2,2) = (4/3)*3220.1325*(0.3556)^3;            %m22 = (4/3).pi.rho.r^3
mu_inf(3,3) = (4/3)*3220.1325*(0.3556)^3;            %m33 = (4/3).pi.rho.r^3
mu_inf(5,5) = (8/15)*3220.1325*(0.3556)^5;           %m55 = (8/15).pi.rho.r^5

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

%Compute incident wave profile

%default values
A = 0.0;%Significant wave height = 2.0;
%T = 2;
%Type = 1 ( = monochromatic)

t = get(Buoy,'t');

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

disp('Computing incident wave profile');
t1= dt:dt:L{7,1}.breaks(end);
eta0 = eta(wave,0,[t t(end)+t1]);

% 
% figure('Position', [100 100 1000 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.09];  %Tether attachment point on Buoy (local CoordSys).
p_aC = [0.0  0.0  0.3556];  %Tether attachment point on Clump (local CoordSys).

gamma = 1.4;
[Force_Coeffs] = AccumSpringForce(gamma);

Buoy = add_Force_fcn(Buoy,@(body,RB,n) Hydraulic_Tether(body,RB,n,'Clump',p_aB,p_aC,Force_Coeffs));
Clump = add_Force_fcn(Clump,@(body,RB,n) Hydraulic_Tether(body,RB,n,'Surface Buoy',p_aC,p_aB,Force_Coeffs));


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

% Buoy = set_pos(Buoy,[0 0 0.3048 0 0 0],1);
% Clump = set_pos(Clump,[0 0 -0.656+0.3048 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', [50 50 1200 800])
set(gcf,'color','white');

subplot(3,1,1),plot(t,x(:,3),t,x(:,5));
title('MOOS Buoy, 1111 kg ','FontName','times','FontSize', 12);
legend('heave(m)','pitch(rad)');
ylim([-0.1 0.1])

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

subplot(3,1,3),plot(t,xddot(:,1),t,xddot(:,3),t,xddot(:,5));
legend('surge accel (m/s^2)','heave accel (m/s^2)','pitch accel (m/s^2)');
xlabel('time (s)');


%plate kinematics
figure('Position', [50 50 1200 800])
set(gcf,'color','white');

subplot(3,1,1), plot(t,y(:,3),t,y(:,5));
title('Solid Sphere r = 28 inches, mass 533 kg','FontName','times','FontSize', 12);
legend('heave(m)','pitch(rad)');

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

subplot(3,1,3), plot(t,yddot(:,1),t,yddot(:,3),t,yddot(:,5));
legend('surge(m/s^2)','heave accel (m/s^2)','pitch accel (m/s^2)');
xlabel('time (s)');


%Calculate Elongation

xa = p_aB(1);      
za = p_aB(3);

xb = p_aC(1);
zb = p_aC(3);

xoa = get(Buoy,'x0');
xob = get(Clump,'x0');

xoa1 = xoa(1);
xoa2 = xoa(3);

xob1 = xob(1);
xob2 = xob(3);

x1 = x(:,1);
z1 = x(:,3);

x2 = y(:,1);
z2 = y(:,3);

alpha = x(:,5);
theta = y(:,5);

%Initial position relative to GCS

pointA = [xoa1 + xa     xoa2 + za]; 
pointB = [xob1 + xb     xob2 + zb];

AB = pointB -pointA;

%Position After rotation and translation relative to GCS
vecr = [xa*cos(alpha) + za*sin(alpha)     za*cos(alpha) - xa*sin(alpha)];

pointAp = [xoa1 + x1 + xa*cos(alpha) + za*sin(alpha)    xoa2 + z1 + za*cos(alpha) - xa*sin(alpha)]; 
pointBp = [xob1 + x2 + xb*cos(theta) + zb*sin(theta)    xob2 + z2 + zb*cos(theta) - xb*sin(theta)]; 

ApBp = pointBp - pointAp;


for i=1:length(t);
elon(i) =(ApBp(i,1)^2 + ApBp(i,2)^2)^0.5 -(AB(1)^2 +AB(2)^2)^0.5;
tension(i)=ppval(Force_Coeffs,elon(i));
end


%Relative motion

%relative heave displacement
dv = v2-v1; %relative heave velocity

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

subplot(3,1,1), plot(t,eta0(1:N));
legend('Incident wave profile (m)');

subplot(3,1,2), plot(t,elon);
legend('Elongation (m)');

subplot(3,1,3), plot(t,dv);
legend('Relative Heave vel (m/s)');
xlabel('time (s)');


%plots for presentation
%Buoy Motion

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


subplot(2,1,1), plot(t,eta0(1:N));
L1=legend('Incident wave profile (m)');
set(L1,'FontName','times','FontSize', 20,'FontWeight','Bold');
ylim([-1 1])
set(gca,'FontName','times','FontSize',20,'FontWeight','Bold');

subplot(2,1,2),plot(t,x(:,3),t,x(:,5),'r');
L2=legend('buoy heave(m)','buoy pitch(rad)');
set(L2,'FontName','times','FontSize', 20,'FontWeight','Bold');
ylim([-1 1])
xlabel('time (s)','FontName','times','FontSize', 20,'FontWeight','Bold');
set(gca,'FontName','times','FontSize', 20,'FontWeight','Bold');

%Forces
figure('Position', [50 50 1200 800])
set(gcf,'color','white');

subplot(2,1,1), plot(t,elon);
L3=legend('Elongation (m)');
set(L3,'FontName','times','FontSize', 16,'FontWeight','Bold');
ylim([0.5 1.0])
set(gca,'FontName','times','FontSize',16,'FontWeight','Bold');

subplot(2,1,2), plot(t,tension/4.45);
L4=legend('Tether Tension (lbf)');
set(L4,'FontName','times','FontSize', 16,'FontWeight','Bold');
ylim([0 2000])
xlabel('time (s)','FontName','times','FontSize', 16,'FontWeight','Bold');
set(gca,'FontName','times','FontSize',16,'FontWeight','Bold');

