clear all;

load('plot_param.mat')
time=[];
EIGVL=[];
MEANEN=[];
skip_frm=1;
STDVORT=3;
for i=1:305
    
    
    load(['Re',num2str((i-1)*skip_frm),'.mat'])
time=[time,T];
EIGVL=[EIGVL,EIGPLOT(:,1+(i-1)*skip_frm)];
MEANEN=[MEANEN,MEAN_ENER(1+(i-1)*skip_frm)];
    set(gcf,'Color','w','Position', [50,150,1100,600],'paperpositionmode','auto')

        
%% Mean Plot

   subplot('Position',[0.02 0.48 0.28 0.46]) ;
     
    tmp1 = 0.5*(u(Nodeu(2:Ny+1,1:Nx))+u(Nodeu(2:Ny+1,2:Nx+1)));
    tmp2 = 0.5*(v(Nodev(2:Ny+1,2:Nx+1))+v(Nodev(1:Ny,2:Nx+1)));
    [dudx,dudy] = gradient(tmp1,dx,dy);
    [dvdx,dvdy] = gradient(tmp2,dx,dy);    
%     quiver(XP(:),YP(:),(tmp1(:)),(tmp2(:)),'k')
    vort = -dvdx-dudy;
    pcolor(XP,YP,vort); title('Mean flow'); shading interp
    axis([0 1 0 1]); box on; caxis([-std(vort(:)),std(vort(:))]*STDVORT+mean(vort(:)));
    set(gca,'xtick',[],'ytick',[]); box on;   % xlim(XLIM);ylim(YLIM)
 
         hold on

    h=streamslice(XP,YP,tmp1,tmp2);%,[a/Nx a/Nx a/Nx a/4 a/4],[b/2 5*b/8 3*b/8 b/4 3*b/4])
        set(h,'color','k');
           text('Position',[100.6 52.05],'String',['Time = ' num2str(time(i),'%10.2f'), '       Re = 10^8'],'fontsize',10);

      hold off;
%% Variance plot
      
      
    subplot('Position',[0.38 0.54 0.56 0.40]) ;  
    for j=1:S
        semilogy(time(1:i),(EIGVL(j,:))); axis([0 10^-5+time(i) 0 1.1*max([max(EIGVL),MEANEN])]) ; hold on;
    end
    box on;
    xlabel('Time'); ylabel('Variance / Energy')
    semilogy(time(1:i),(MEANEN(1:i)),'r','linewidth',1);
      
%% Modes
    offu=size(ui,1);
    offv=size(vi,1);
off=0.325;
for j=1:3
     [f,xi] = ksdensity((YYt(:,j)-mean(YYt(:,j)))/sqrt(cov(YYt(:,j)))); 
     subplot('Position',[0.02+(j-1)*off 0.05 0.21 .38]) ;
     
    tmp1 = 0.5*(ui((j-1)*offu+Nodeu(2:Ny+1,1:Nx))+ui((j-1)*offu+Nodeu(2:Ny+1,2:Nx+1)));
    tmp2 = 0.5*(vi((j-1)*offv+Nodev(2:Ny+1,2:Nx+1))+vi((j-1)*offv+Nodev(1:Ny,2:Nx+1)));
    [dudx,dudy] = gradient(tmp1,dx,dy);
    [dvdx,dvdy] = gradient(tmp2,dx,dy);    
%     quiver(XP(:),YP(:),(tmp1(:)),(tmp2(:)),'k')
    vort = -dvdx-dudy;
    pcolor(XP,YP,vort); shading interp;
    caxis([-std(vort(:)),std(vort(:))]*STDVORT+mean(vort(:)));
    axis([0 1 0 1]); box on;
    set(gca,'xtick',[],'ytick',[]); box on;   % xlim(XLIM);ylim(YLIM)
 
         hold on

    h=streamslice(XP,YP,tmp1,tmp2);
        set(h,'color','k');

      hold off;
     subplot('Position',[0.26+(j-1)*off 0.08 0.07 .32]);
     plot(xi,f); axis([min(xi) max(xi) 0 1.1*max(f)])
     set(gca,'fontsize',8); title(['Mode ',num2str(j)],'fontweight','bold')

end
   Frame(i)=getframe(gcf) ;
end

      movie2avi(Frame,'double_gyre.avi','compression','none')


     
