%
%This code solves the NLS+Potential using a fourth order sympletic integrator.
%
%       i u_t + (1/2) u_xx - alpha*|u|^2 u = V(x) u
%
%with:
%
%  V(x) = V0*tanh(x)^2;
%  alpha = 1 is defocusing
%  alpha =-1 is focusing
%  
%Ex:  NLS_potential(n,dt,tf,L,vel,V0,alpha,dispframes,saveframes)
%
% npts=2^n, dt:time step, tf:t_final, domain:[-L,L]
% vel=velocity of soliton, V0=heith of tanh^2 potential
%
%Example:
% global allt ally allu v ran; ran=1
% n=7;dt=0.1;tf=100;L=10;alpha=-1;vel=0.2;V0=0.1;dispframes=50;saveframes=200;
% NLS_potential(n,dt,tf,L,vel,V0,alpha,dispframes,saveframes)
% wave_plotsurf(allt,ally,abs(allu).^2,L,1,length(allt),'no')
%
%DON'T FORGET: 
% global allt ally allu v ran; ran=1

function [] = NLS_potential(n,dt,maxtime,L,vel,V0,alpha,dispframes,saveframes)

 global allt ally allu v;
 global ran;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Checking if variables are global
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if(isempty(ran)==1)
   fprintf('\n');
   error('Error: first do: global allt ally allu v ran; ran=1')
 end

 maxstep=maxtime/dt;
 if(exist('allt'))
  if(isglobal(allt)~=1)
   fprintf('\n');
   error('Error: first do: global allt ally allu v ran; ran=1')
  end
 end

 dispscreen= fix(maxstep/100); 

 if(dispframes>0) stopdisp= fix(maxstep/dispframes); 
 else stopdisp=9e+99;
 end
 if(saveframes>0)stopsave= fix(maxstep/saveframes);
 else stopsave=9e+99;
 end
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Spatial computational domain defined
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 N=2^n;
 dx=2*pi/N; x=-pi+(0:N-1)*dx;
 M = N/2; kvec = fftshift(-M:M-1);
 kvec=kvec.';
 x=x.'; 
 y=x*L/pi;
 ally = y;
 allt = 0:stopsave*dt:maxtime;

 plotrealimag=1;
 plotsquare=0;
 u=0; 

 strlength=0;
 kk=0;
 t0=0;
 lw=2;
 fs=16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GENERATING ICs and potential
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 x0 = 0;
 phi0=0;

 v = V0*tanh(y).^2;
 u = sqrt(1-V0)*sech((y-x0)).*exp(i*(vel*y+phi0));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 fprintf('\n');
 fprintf('L \t\t= %f\n',L);
 fprintf('alpha \t\t= %f\n',alpha);
 fprintf('V0 \t\t= %f\n',V0);
 fprintf('vel \t\t= %f\n',vel);
 fprintf('\phi_0 \t\t= %f\n',phi0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot initial data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 figure(1);clf;hold on;
 if(plotsquare)
  plot(y,abs(u).^2,'k',y,v,'b')
  high = max(max(abs(u).^2,v));
  low  = min(min(abs(u).^2,v));
 else
  plot(y,abs(u),'k',y,v,'b','LineWidth',lw)
  high = max(max(abs(u),v));
  low  = min(min(abs(u),v));
 end
 dhl = (high-low)*0.1;
 title('IC: u(x,t) (black), v(x) (blue)','FontSize',fs);
 axis([-L L -dhl high+dhl])
 if(plotrealimag)
  plot(y,real(u),'g',y,imag(u),'r',y,v,'b','LineWidth',lw)
  title('IC: u (black), Real (green) and Imag (red), v(x) (blue)','FontSize',fs);
  axis([-L L -high-dhl high+dhl])
 end

 text(L/1.75,high-dhl/2,['T='num2str(kk*dt) '/' num2str(maxtime)],'FontSize',fs);
 xlabel('x','FontSize',fs+2)
 ylabel('u(x,t)','FontSize',fs+2)
 set(gca,'FontSize',fs);
 allu = u;

 fprintf('\nt=%5.2f ',t0);
 fprintf('\nPress any key to contiue...');
 pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 beta = (1/3.0)*(2+2^(1/3)+2^(-1/3));
 ooN=1.0/N;
 rho = (pi/L)^2/2.0;
 symbol1 = exp(-i*rho*(kvec.*kvec)*beta*dt);
 symbol2 = exp(-i*rho*(kvec.*kvec)*(1-2*beta)*dt);
 idt1=-i*beta*dt/2.0;
 idt2=-i*(1-2*beta)*dt/2.0;

 for kk=1:maxstep

   [temp1] = NLS_potential_spectral(N,ooN,u,    v,symbol1,idt1,alpha);
   [temp1] = NLS_potential_spectral(N,ooN,temp1,v,symbol2,idt2,alpha);
   [u] =     NLS_potential_spectral(N,ooN,temp1,v,symbol1,idt1,alpha);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% include noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   noise=0;
   if(noise>0)
    u  = u+noise*(rand(N,1)-0.5);
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display time on screen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   if (fix(kk/dispscreen)==kk/dispscreen)
    fprintf('%5.2f ',t0+kk*dt);
    strlength=strlength+1;
    if strlength>=10
     strlength=0;
     fprintf('\n');
    end
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% store solution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   if (fix(kk/stopsave)==kk/stopsave)
    allu = [allu,u];
   end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   if(fix(kk/stopdisp)==kk/stopdisp&kk>1)
     figure(1); clf;
     if(plotsquare)
      plot(y,abs(u).^2,'k',y,v,'b','LineWidth',lw)
     else
      plot(y,abs(u),'k',y,v,'b','LineWidth',lw)
     end
     title('u(x,t) (black), v(x) (blue)','FontSize',fs);
     axis([-L L -dhl high+dhl])
     hold on
     if(plotrealimag)
      plot(y,real(u),'g',y,imag(u),'r',y,v,'b','LineWidth',lw)
      title('u (black), Real (green) and Imag (red), v(x) (blue)','FontSize',fs);
      axis([-L L -high-dhl high+dhl])
     end

     text(L/1.75,high-dhl/2,['T='num2str(kk*dt) '/' num2str(maxtime)],'FontSize',fs);
     xlabel('x','FontSize',fs+2)
     ylabel('u(x,t)','FontSize',fs+2)
     set(gca,'FontSize',fs);
     pause(0.01);
   end

 end

 ran=1;
 fprintf('\n');
 return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pseudo spectral integrator
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [u] = NLS_potential_spectral(N,ooN,u,v,symbol,idt,alpha)

                mag = alpha*u.*conj(u)+v;
                u = exp(idt*mag).*u;

                uh = fft(u)*ooN;
                udh = symbol.*uh;
                u = ifft(udh)*N;

                mag = alpha*u.*conj(u)+v;
                u = exp(idt*mag).*u;

return
