%
%  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]
%  Ricardo Carretero (30/10/02) [based on David Muraki's code]
%
%  Note: The initial condition HAS TO BE PERIODIC on the box [0:L]
%

global ik
clear('i')

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

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

%%%%%%%
% Setup
%%%%%%%
N = 512;			% Number of grid points

%KdV Dirac delta
L=1000;				% Domain length
tfin=1*1000.0;			% Final time
dt = 0.5;			% Time step (if run becomes untable reduce dt)

Nsnaps = 50; 			% Number of snapshots to display on screen
Nsaves = 200;			% 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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% makes sure that dt divides tsnaps and tsaves
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(tsnaps/abs(dt)~=fix(tsnaps/abs(dt)))
 fprintf('dt=%f does not divide tsnaps=%f',dt,tsnaps);
 error('incompatible value for dt');
end
if(tsaves/abs(dt)~=fix(tsaves/abs(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) ];


%%%%%%%%%%%%%%%%%%%
%  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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%KdV Dirac delta

d0 = 0.1;
bb=d0^2*pi;
ug = d0*exp(-bb*(xg-L/2).^2);
pmin = -0.25*d0;
pmax = 1.2*d0;
%ug=uuu;

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.92,disp_param);
text(max(max(x))-L/10,pmax*0.92,['t=' num2str(t)]);
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 = abs(dt):abs(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 + abs(dt);
  tsn = tsn + abs(dt);
  if(tsa+abs(dt)/2>tsaves)		% it is time to save solution
   tsa=0;
   allu = [allu,ug];
   allt = [allt,t];
  end
  if(tsn+abs(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.92,disp_param);
   text(max(max(x))-L/10,pmax*0.92,['t=' num2str(t)]);
   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,log(0.001+allu-min(min(allu))),L,1,Nsaves+1,'no')


