clear
close all
clc

figure;
set(gcf, 'position',[200,60,900,900])

 subplot('position',[0.035, 0.02 0.96, 0.96])

 N_c = 5000;
 t_c =  [0:N_c]/N_c*2*pi;
 
 N_x = 100;
 t = [1:N_x]/N_x*2*pi;
 ds = 25;
 R = ds*N_x/ pi/2;
 
 total_length = N_x*ds;
 
 R_in = R-10;
 R_out = R+10;
 x_in = R_in*cos(t_c);
 y_in = R_in*sin(t_c);
 x_out = R_out*cos(t_c);
 y_out = R_out*sin(t_c);
 
 x = R*cos(t);
 y = R*sin(t);
 
 Car_index = [-N_x/2:N_x/2-1]*6;
 Car_difference = zeros(size(Car_index));
 
 
 
%  plot(x,y,'rs','MarkerFaceColor','r') 
%  hold on
%  plot(x_in,y_in,'k','LineWidth',2)
%  plot(x_out,y_out,'k','LineWidth',2)
%  axis equal
%  axis off
%  
 T = 500;
dt = 0.1;
N_T = floor(T/dt)+1;
 U = zeros(N_T, N_x);
 V = U;
 

  
 u_0 = zeros(1,N_x);
 v_0 = u_0;
 piece_u = 20;
 piece_v = 10; 
 
 delta_space = 5;
 delta_speed = 5;
 
 %different initial conditions
 
 
%  for i = 0: N_x/ piece_u-1
%  u_0(i*piece_u+1:i*piece_u+piece_u/2) = delta_space*[1:piece_u/2];
%  u_0(i*piece_u+piece_u/2+1:(i+1)*piece_u) = u_0(i*piece_u+piece_u/2)-delta_space*[1:piece_u/2];
%  end
%    
%   for i = 0: N_x/ piece_v-1
%  v_0(i*piece_v+1:i*piece_v+piece_v/2) = delta_speed;
%  v_0(i*piece_v+piece_v/2+1:(i+1)*piece_v) = -delta_speed;
%   end
   
%  u_0(1:N_x/2) = delta_space*[1:N_x/2];
%  u_0(N_x/2+1:N_x) = u_0(N_x/2) -delta_space*[1:N_x/2];
%  v_0(1:N_x/2) =delta_speed;
%  v_0(N_x/2+1:N_x) =-delta_speed;

u_0= rand(1,N_x)*delta_space*2-delta_space;
u_0=u_0-mean(u_0);

v_0= rand(1,N_x)*delta_speed*2-delta_speed;
v_0=v_0-mean(v_0);
 
% u_0(1)= delta_space;
% 
% u_0(2) = u_0(1)- delta_space;
% 
% u_0(51:65)= delta_space*[1:15];
% 
% u_0(66:80) = u_0(65)- delta_space*[1:15];

  v_ref =25;
  U_ref = v_ref*dt;
  
  U(1,:) = u_0;
  V(1,:) = v_0;
  
  x = R*cos(t+u_0/R);
  y = R*sin(t+u_0/R);
  
  Car_difference(2:end) = diff(u_0);
  Car_difference(1) = u_0(1)-u_0(end) ;
  
N_omega = N_x ;
d_omega = pi/ N_omega;
omega =  [0:N_omega-1]*d_omega;
k_d =0.1;
k_v = 0.4;
c_omega = 2*sqrt(k_d)*sin(omega);

Dmin_1 = zeros(1,N_T);
Dmin_1(1) = min(Car_difference);
Dmin_2= Dmin_1;

s_re = -2*k_v*(sin(omega)).^2;
s_im = 2*sin(omega).*sqrt(k_v^2*(sin(omega)).^2-k_d);
s1 = s_re + s_im;
s2 = s_re - s_im;
u_fft = fft(u_0);
v_fft = fft(v_0);

A = u_fft;
B = zeros(size(A));
B(2:end) = v_fft(2:end)./(c_omega(2:end));
  

A_s = zeros(size(A)); 
B_s = A_s;
A_s(2:end) = (u_fft(2:end).*s2(2:end)-v_fft(2:end))./(s2(2:end)-s1(2:end));
B_s(2:end) = (-u_fft(2:end).*s1(2:end)+v_fft(2:end))./(s2(2:end)-s1(2:end));
U_s = U;
V_s = V;

 plot(x,y,'ks','MarkerFaceColor','r') 
 hold on
 plot(x([1, N_x/2,N_x/2+1, end]),y([1, N_x/2,N_x/2+1, end]),'ks','MarkerFaceColor','k') 
 plot(x_in,y_in,'k','LineWidth',2)
 plot(x_out,y_out,'k','LineWidth',2)
 plot(Car_index, Car_difference*4+100, 'k.')
  plot(Car_index, V(1,:)*4+100, 'r.')
 axis equal
 text(-75,340,'Black Point: u[i]-u[i-1]','FontSize',14)
 text(-60,320,'Red Points: v[i]','FontSize',14,'Color','r')
 text(-160,280, sprintf('The minimum (u[i]-u[i-1]) is: %2.2f', min(Car_difference)),'FontSize',16)
 line([-305, -305],[0,200],'Color','k','LineWidth',2)
 line([300, 300],[0,200],'Color','k','LineWidth',2)
 line([-305, 300],[0,0],'Color','k','LineWidth',2)
 line([-305, 300],[20,20],'LineStyle','--')
 line([-305, 300],[200,200],'Color','k','LineWidth',2)
 text(-320,100,'0','FontSize',14)
  text(-320,120,'5','FontSize',14)  
  text(-330,80,'-5','FontSize',14)
   text(-340,0,'-25','FontSize',14)
    text(-340,20,'-20','FontSize',14,'Color','b')
    text(-328,200,'25','FontSize',14)
 
    text(-308,-15,'1','FontSize',14)
    text(290, -15,'100','FontSize',14)
    text(-10, -15,'50','FontSize',14)
      text(-90, -35,'Car index (from 1 to 100)','FontSize',14)
    
%  text(-210,300,'To see it clear, all u[i] and v[i] are multiplied by 4.','FontSize',14)
%  text(-150,-100,'The analytical solution of Method of Lines','FontSize',14)
 text(-150,-120,sprintf('Total Car Number: %d', N_x),'FontSize',14)
 text(-150,-140,sprintf('Time is : %2.2f (second)', 0),'FontSize',14)
%  text(-150,-160,sprintf('k_d is : %2.2f, and k_v is : 0', k_d),'FontSize',14)
 text(-150,-160,sprintf('Average space betwen cars is: %d (meter)', ds),'FontSize',14)
 text(-150,-180,sprintf('Average speed of the cars is %d (m/s)', v_ref),'FontSize',14)
 text(-150,-220,'The initial condition is:','FontSize',14)
 text(-150,-240,sprintf('First %d cars are spaced by %d (meter).', N_x/2,delta_space+ds),'FontSize',14)
 text(-150,-260,sprintf('Last %d cars are spaced by %d (meter).', N_x/2,ds-delta_space),'FontSize',14)
 text(-150,-280,sprintf('Speed of the first %d cars are: %d (m/s).', N_x/2, v_ref+delta_speed),'FontSize',14)
 text(-150,-300,sprintf('Last %d cars are spaced by %d (m/s).', N_x/2, v_ref-delta_speed),'FontSize',14)
 
 axis off
 hold off
%  
%  M(1) = getframe(gcf);
% print(gcf,'-depsc2','initial_condition_without damper.eps')

for i = 2:N_T
  
f_s = A_s.*exp(s1*(i-1)*dt)+B_s.*exp(s2*(i-1)*dt);
U_s(i,:) = real(ifft(f_s));
  Car_difference(2:end) = diff(U_s(i,:));
  Car_difference(1) = U_s(i,1)-U_s(i,end);
  Dmin_2(i) = min(Car_difference);



     
    f = A.*cos(c_omega*(i-1)*dt)+B.*sin(c_omega*(i-1)*dt);
    
    U(i,:) = real(ifft(f));
%     
     f = (A.*sin(c_omega*(i-1)*dt)+B.*cos(c_omega*(i-1)*dt)).*c_omega;
     
    V(i,:) = real(ifft(f));

  Car_difference(2:end) = diff(U(i,:));
  Car_difference(1) = U(i,1)-U(i,end);
  Dmin_1(i) = min(Car_difference);

      g = U(i,:) + U_ref*(i-1);
  x = R*cos(t+g/R);
  y = R*sin(t+g/R);  
  
  
%  plot(x,y,'ks','MarkerFaceColor','r') 
%  hold on
%  plot(x([1, N_x/2,N_x/2+1, end]),y([1, N_x/2,N_x/2+1, end]),'ks','MarkerFaceColor','k') 
%  plot(x_in,y_in,'k','LineWidth',2)
%  plot(x_out,y_out,'k','LineWidth',2)
%  plot(Car_index, Car_difference*4+100, 'k.')
%  plot(Car_index, V(i,:)*4+100, 'r.')
%  axis equal
% %   text(-150,340,'Red Points: speed of the i-th car','FontSize',14)
% %  text(-180,320,'Black Points: space ahead of the i-th car','FontSize',14)
% %  text(-210,300,'To see it clear, all u[i] and v[i] are multiplied by 4.','FontSize',14)
% %  text(-150,-100,'The analytical solution of Method of Lines','FontSize',14)
% %  text(-150,-120,sprintf('Total Car Number: %d', N_x),'FontSize',14)
% %  text(-150,-140,sprintf('Time is : %2.2f (second)', (i-1)*dt),'FontSize',14)
% %  text(-150,-160,sprintf('k_d is : %2.2f, and k_v is : 0', k_d),'FontSize',14)
% %  text(-150,-180,sprintf('Average space betwen cars is: %d (meter)', ds),'FontSize',14)
% %  text(-150,-200,sprintf('Average speed of the cars is %d (m/s)', v_ref),'FontSize',14)
% %  text(-150,-220,'The initial condition is:','FontSize',14)
% %  text(-150,-240,sprintf('First %d cars are spaced by %d.', N_x/2,delta_space+ds),'FontSize',14)
% %  text(-150,-260,sprintf('Last %d cars are spaced by %d.', N_x/2,ds-delta_space),'FontSize',14)
% %   text(-150,-280,sprintf('Speed of the first %d cars are: %d.', N_x/2, v_ref+delta_speed),'FontSize',14)
% %  text(-150,-300,sprintf('Last %d cars are spaced by %d.', N_x/2, v_ref-delta_speed),'FontSize',14)
% 
%  text(-75,340,'Black Point: u[i]-u[i-1]','FontSize',14)
%  text(-60,320,'Red Points: v[i]','FontSize',14,'Color','r')
%  text(-160,280, sprintf('The minimum (u[i]-u[i-1]) is: %2.2f', min(Car_difference)),'FontSize',16)
%  line([-305, -305],[0,200],'Color','k','LineWidth',2)
%  line([300, 300],[0,200],'Color','k','LineWidth',2)
%  line([-305, 300],[0,0],'Color','k','LineWidth',2)
%  line([-305, 300],[20,20],'LineStyle','--')
%  line([-305, 300],[200,200],'Color','k','LineWidth',2)
%  text(-320,100,'0','FontSize',14)
%   text(-320,120,'5','FontSize',14)  
%   text(-330,80,'-5','FontSize',14)
%    text(-340,0,'-25','FontSize',14)
%     text(-340,20,'-20','FontSize',14,'Color','b')
%     text(-328,200,'25','FontSize',14)
%  
%     text(-308,-15,'1','FontSize',14)
%     text(290, -15,'100','FontSize',14)
%     text(-10, -15,'50','FontSize',14)
%       text(-90, -35,'Car index (from 1 to 100)','FontSize',14)
%  
% %  text(-210,300,'To see it clear, all u[i] and v[i] are multiplied by 4.','FontSize',14)
% %  text(-150,-100,'The analytical solution of Method of Lines','FontSize',14)
%  text(-150,-120,sprintf('Total Car Number: %d', N_x),'FontSize',14)
%  text(-150,-140,sprintf('Time is : %2.2f (second)', (i-1)*dt),'FontSize',14)
% %  text(-150,-160,sprintf('k_d is : %2.2f, and k_v is : 0', k_d),'FontSize',14)
%  text(-150,-160,sprintf('Average space betwen cars is: %d (meter)', ds),'FontSize',14)
%  text(-150,-180,sprintf('Average speed of the cars is %d (m/s)', v_ref),'FontSize',14)
%  text(-150,-220,'The initial condition is:','FontSize',14)
%  text(-150,-240,sprintf('First %d cars are spaced by %d (meter).', N_x/2,delta_space+ds),'FontSize',14)
%  text(-150,-260,sprintf('Last %d cars are spaced by %d (meter).', N_x/2,ds-delta_space),'FontSize',14)
%  text(-150,-280,sprintf('Speed of the first %d cars are: %d (m/s).', N_x/2, v_ref+delta_speed),'FontSize',14)
%  text(-150,-300,sprintf('Last %d cars are spaced by %d (m/s).', N_x/2, v_ref-delta_speed),'FontSize',14)
%   axis off
%     hold off
    
% 
%     M(i) = getframe(gcf);
   
end

figure
set(gcf, 'position',[60,60,400,600])
hold on
T_axis = [0:N_T-1]*dt;
for i = 1: N_x
    plot(U(:,i)+(i-1)*ds,T_axis,'k')
    
end
axis tight
xlabel('Space (meter)');
ylabel('Time (second)');

figure
set(gcf, 'position',[500,60,400,600])
hold on
T_axis = [0:N_T-1]*dt;
for i = 1: N_x
    plot(U_s(:,i)+(i-1)*ds,T_axis,'k')
    
end
axis tight
xlabel('Space (meter)');
ylabel('Time (second)');

figure
set(gcf, 'position',[60,60,600,300])
plot(T_axis, Dmin_1,'k','LineWidth', 2)
hold on
plot(T_axis, Dmin_2,'r','LineWidth', 2)
xlabel('Time (second)');
ylabel('minimum (u[k]-u[k-1]) (meter)');
legend(sprintf('k_d=%1.1f, k_v=0',k_d),sprintf('k_d=%1.1f, k_v=%1.1f',k_d,k_v))

%  writegif('semi_discrete_method.gif',M,0.1);
% writerObj = VideoWriter('MOL_circle_speed_is_one_absolute_reference_small_kd');
% open(writerObj);
% writeVideo(writerObj,M);
% close(writerObj);