%
%  This code solves the nonlinear, dispersive, diffusive wave equation:
%
%		u  + c u + b u u  - g u  + d u    = 0
%		 t      x       x      xx     xxx
%
%  For c =  0.0;  b =  6.0;  g =  0.0; d =  1.0; i.e. the KdV equation:
%
%		u  + 6 u u  +  u    = 0
%		 t        x     xxx
%
%  using a spectral method over the interval [0:L]
%  For PRIODIC SOLUTIONS USING CN-WAVES
%
%  Ricardo Carretero (30/10/02) [based on David Muraki's code]
%
%  Note: The initial condition HAS TO BE PERIODIC on the box [0:L]
%

set(groot,'defaulttextinterpreter','latex');
set(groot,'defaultAxesTickLabelInterpreter','latex');
set(groot,'defaultLegendInterpreter','latex');
set(groot,'defaultAxesFontSize',14);

global ik
clear('i')

%%%%%%%%%%%%%%%%%%%%%
% Equation Parameters
%%%%%%%%%%%%%%%%%%%%%
c =  0.0;  b = 6.0;  g =  0.0; d =  1.0;  

par = [c,b,g,d];

%%%%%%%
% Setup
%%%%%%%
%%For fixed a1, a2, a3
%a1=-1;
%a2=a1-2.99999;
%a2=a1-0.01;
%a3=-4;
%k=sqrt((a2-a3)/(a1-a3))
%vel=-2*(a1+a2+a3);

%For fixed k, a2, v
v=10;
k=0.999999;
a2 = -1;
a1 = -1/2*(-v-4*a2+k^2*v+2*k^2*a2)/(2*k^2-1);
a3 = -1/2*(k^2*v+2*a2+2*k^2*a2)/(2*k^2-1);

[T,E] = ellipke(k^2);
T=2*T/sqrt((a1-a3)/2);
Nsol=3;				% Number of solitary waves to fit in L;
L=Nsol*T;			% Domain length
N = 256;			% Number of grid points
tfin=1.0;			% Final time
dt = 0.002;			% Time step (if run becomes untable reduce dt)
%
Nsnaps = 25; 			% Number of snapshots to display on screen
Nsaves = 500;			% Number of solutions to save on allu
lw=2;				% linewidth for display

tsnaps = tfin/Nsnaps;		% Time between screen snapshots
tsaves = tfin/Nsaves;		% Time between saved solutions

fprintf('k=%f, v=%f, a1=%f, a2=%f, a3=%f, Nsol=%f, L=%f\n',k,v,a1,a2,a3,Nsol,L);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% makes sure that dt divides tsnaps and tsaves
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(tsnaps/dt~=fix(tsnaps/dt))
 fprintf('dt=%f does not divide tsnaps=%f',dt,tsnaps);
 error('incompatible value for dt');
end
if(tsaves/dt~=fix(tsaves/dt))
 fprintf('dt=%f does not divide tsaves=%f',dt,tsaves);
 error('incompatible value for dt');
end

blurb = '\bf Wave equation: $u_t + c u_x + \beta u u_x - \gamma u_{xx} + \delta u_{xxx} = 0$';
disp_param = ['$c=' num2str(c) '$, $\beta=' num2str(b) '$, $\gamma=' num2str(g) '$, $\delta=' num2str(d) '$, $k=' num2str(k) '$, $v=' num2str(v) '$' ];


%%%%%%%%%%%%%%%%%%%
%  set grid vectors
%%%%%%%%%%%%%%%%%%%
ik = i*(2*pi/L)*(-N/2:N/2-1)';
fact = exp(dt*(c*ik-g*ik.^2+d*ik.^3));
dx = L/N;  xg = (0:dx:L-dx)';  x = [xg ; L];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial conditions and plotting window
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x0=T/2;
[Sn,Cn,Dn]=ellipj((xg-x0)*sqrt((a1-a3)/2),k^2);
ug  = -a2 + (a2-a3)*Cn.^2;
Mu=max(max(ug));
mu=min(min(ug));
pmin = mu-0.1*(Mu-mu);
pmax = Mu+0.1*(Mu-mu);

t = 0; 				% initial time
vg = fftshift(fft(ug))/N;	% FFT(initial condition)
allu = ug;			% this vector stores the saved snapshots
allt = 0;			% this vector stores the saving times
tsa = 0;			% saving loop-time
tsn = 0;			% displaying loop-time
u = [ug ; ug(1)];

%%%%%%%%%%%%%%%%%%%%%%%%
% Plot initial condition
%%%%%%%%%%%%%%%%%%%%%%%%
figure(1)
clf;
plot(x,u,'r',x,u,'k.','markersize',7);
axis([0 L pmin pmax]);
title(blurb);  
xlabel('\bf $x$-axis');  ylabel('\bf $u$-axis')
text(min(min(x))+L/20,pmax-0.1*(pmax-pmin)/2,disp_param,'FontSize',13);
text(max(max(x))-L/8,pmax-0.1*(pmax-pmin)/2,['t=' num2str(t)],'FontSize',13);
pause(0.001)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quick check that initial condition is indeed periodic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% this routine detects if the mas change of u happens at the boundary
if(abs(ug(1)-ug(N))>max(abs(diff(ug))))
 fprintf('I think your IC is not PERIODIC on the box [0:L] => Check plot!!!');
 error('Non-periodic IC');
end

input('press any key to continue');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  timestep loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t = dt:dt:tfin

  [vg,tj] = wave_integrator_rk(vg,0,dt,par);	% pseudo-spectrol + Runge-Kutta method
  vg = vg./fact;
  ug = real(ifft(fftshift(vg))*N);

  tsa = tsa + dt;
  tsn = tsn + dt;
  if(tsa+dt/2>tsaves)		% it is time to save solution
   tsa=0;
   allu = [allu,ug];
   allt = [allt,t];
  end
  if(tsn+dt/2>tsnaps)		% it is time to display solution
   tsn=0;
   figure(1); clf;
   plot(x,[ug ; ug(1)],'b','LineWidth',lw);%  plot(x,[ug ; ug(1)],'k.');  
   axis([0 L pmin pmax]);
   title(blurb);  xlabel('\bf $x$-axis');  ylabel('\bf $u$-axis')
   text(min(min(x))+L/20,pmax-0.1*(pmax-pmin)/2,disp_param,'FontSize',13);
   text(max(max(x))-L/8,pmax-0.1*(pmax-pmin)/2,['t=' num2str(t)],'FontSize',13);
   pause(0.001)
  end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% end of timestep loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot final condition together with initial
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on
plot(x,[allu(:,1);allu(1:1)],'r--');  
plot(x,[allu(:,1);allu(1:1)],'k.','markersize',7);
title(blurb);  xlabel('\bf $x$-axis');  ylabel('\bf $u$-axis')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calls the ploting subroutine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
input('press ANOTHER key to continue');
wave_plotsurf(allt,xg,allu,L,1,Nsaves+1,'no')


