Re = 500;
LidDrivenCavityConvergeScript_num(Re,false)

d0=fileparts([pwd,filesep]);
d0=d0(1:strfind(d0,'trunk')+4);

app.nu = 1/Re;
app.dt = 1/512;
app.savemat = ['DOnumPap/Lid',num2str(Re),'_',num2str(1/app.dt)];

load(sprintf('%s/Save/%s/RE%d_dt%03d_Final.mat',d0,app.savemat,1/app.nu,1/app.dt))

%Create L2 table
JJ = 1;
Matrix = [PL2f(2:4,JJ), orderL2f(2:end,1,JJ);
    uL2f(2:4,JJ), orderL2f(2:end,5);
    vL2f(2:4,JJ), orderL2f(2:end,9);
    YYtL2f(1,2:4,JJ)'*0, 0*orderL2f(2:end,9)];
for I = 1:3
    Matrix = [Matrix,...
        [PiL2f(I,2:4,JJ)', orderL2f(2:end,1+I,JJ);
        uiL2f(I,2:4,JJ)', orderL2f(2:end,5+I,JJ);
        viL2f(I,2:4,JJ)', orderL2f(2:end,9+I,JJ);
        YYtL2f(I,2:4,JJ)', orderL2f(2:end,17+I,JJ)]];
end

  
clab = {'Component','Grid','Mean',',','Mode 1','','Mode 2','','Mode 3',''};
rlab = {'$P$ & 256',' & 128',' & 64',...
    '$u$ & 256',' & 128',' & 64',...
    '$v$ & 256',' & 128',' & 64',...
    '$Y_i$ & 256',' & 128',' & 64'};
fprintf('\n\n')
tex_table(Matrix,clab,rlab)

%% Now to the same for time
%Create L2 table
II = 1; JJ = 2:3
log(PL2f(2:end,j) ./ PL2f(1:end-1,j)) / log(2);
Matrix = [PL2f(II,JJ)', log(PL2f(II,JJ+1) ./ PL2f(II,JJ))' / log(2);
    uL2f(II,JJ)', log(uL2f(II,JJ+1) ./ uL2f(II,JJ))' / log(2);
    vL2f(II,JJ)', log(vL2f(II,JJ+1) ./ vL2f(II,JJ))' / log(2);
    0*squeeze(YYtL2f(1,II,JJ)),0*squeeze(YYtL2f(1,II,JJ))]
for I = 1:3
    Matrix = [Matrix,...
        [squeeze(PiL2f(I,II,JJ)),...
        squeeze(log(PiL2f(I, II,JJ+1) ./ PiL2f(I, II,JJ)) / log(2)); 
        squeeze(uiL2f(I,II,JJ)),...
        squeeze(log(uiL2f(I, II,JJ+1) ./ uiL2f(I, II,JJ)) / log(2));
        squeeze(viL2f(I,II,JJ)),...
        squeeze(log(viL2f(I, II,JJ+1) ./ viL2f(I, II,JJ)) / log(2));
        squeeze(YYtL2f(I,II,JJ)),...
        squeeze(log(YYtL2f(I, II,JJ+1) ./ YYtL2f(I, II,JJ)) / log(2))]];
end

  
clab = {'Component','Grid','Mean',',','Mode 1','','Mode 2','','Mode 3',''};
rlab = {'$P$ & 2048',' & 1024',...
    '$u$ & 2048',' & 1024',...
    '$v$ & 2048',' & 1024',...
    '$Y_i$ & 2048',' & 1024'};

tex_table(Matrix,clab,rlab)
%% Figures
figure,
% semilogy(PL2f,'-','linewidth',2)
% legend('\Delta t','2 \Delta t','4 \Delta t','8 \Delta t','Location','NorthWest')

semilogy(PL2f(2:end,1)','o-','linewidth',2,'Markersize',10),hold on
semilogy(reshape(sum(PiL2f(:,2:end,1))/3,1,4),'o--','linewidth',2,'Markersize',10,'MarkerFaceColor','b')
% 
semilogy((uL2f(2:end,1) + vL2f(2:end,1))/2,'s-g','linewidth',2,'Markersize',10)
semilogy(reshape(sum(uiL2f(:,2:end,1) + viL2f(:,2:end,1))/6,1,4),'s--g','linewidth',2,'Markersize',10,'MarkerFaceColor','g')

%Old Plots plotted every value of Delta T. Added a new plot below. 
% semilogy(PL2f,'o-','linewidth',2,'Markersize',10)
% semilogy(reshape(sum(PiL2f)/3,4,4),'.--','linewidth',2,'Markersize',10)
% % 
% semilogy((uL2f + vL2f)/2,'s-','linewidth',2,'Markersize',10)
% semilogy(reshape(sum(uiL2f + viL2f)/6,4,4),'x--','linewidth',2,'Markersize',10)
%
% semilogy(reshape(sum(YYtL2f)/3,4,3),'<--','linewidth',2,'Markersize',10)
%
tmp = reshape(PL2f(2:end,:),4,4);
tmp1 = max(tmp(:,1));
tmp = tmp1;
for i = 1:3
%     tmp = [tmp, tmp(end)*4];
    tmp = [tmp(1)/4, tmp];
end
% semilogy([3:4],tmp(3:4)/2,'k','linewidth',1)
semilogy([1:4],tmp(1:4),'k','linewidth',1)

tmp = sum(PiL2f(:,2:end,1))/3;
tmp1 = max(tmp);
tmp = tmp1;
for i = 1:3
%     tmp = [tmp, tmp(end)*4];
    tmp = [tmp(1)/4, tmp];
end

semilogy([1:4],tmp(1:4),'k','linewidth',1)


tmp = PL2f(:,1);
tmp1 = max(tmp(1,:));
tmp = tmp1;
for i = 1:3
%     tmp = [tmp(1)/4, tmp];
    tmp = [tmp, tmp(end)*4];
end
% semilogy(tmp,'k','linewidth',1)
set(gca,'XTick',[1:4])
ylim([3e-5 7e-2]);
set(gca,'Fontsize',18)
legend('Pressure mean', 'Pressure modes', 'Velocity means','Velocity modes',...
    'Optimal Convergence', 'Location','NorthWest')
print('-dpng','-r400','img/LidConv')
print('-depsc','img/LidConv')

%%
figure
semilogy(PL2f(1,2:end),'o-','linewidth',2,'Markersize',10), hold on
semilogy(squeeze(sum(PiL2f(:,1,2:end))/3),'o--','linewidth',2,'Markersize',10,'MarkerFaceColor','b')
% 
semilogy((uL2f(1,2:end) + vL2f(1,2:end))/2,'sg-','linewidth',2,'Markersize',10), hold on
semilogy(squeeze(sum(uiL2f(:,1,2:end) + viL2f(:,1,2:end))/6),'sg--','linewidth',2,'Markersize',10,'MarkerFaceColor','g')

tmp = (uL2f(1,2:end) + vL2f(1,2:end))/2;
tmp1 = max(tmp);
tmp = tmp1;
for i = 1:3
%     tmp = [tmp, tmp(end)*4];
    tmp = [tmp(1)/2, tmp];
end
semilogy([2:3],tmp(2:3)/2,'k','linewidth',1)
% semilogy(tmp,'k','linewidth',1)


tmp = PL2f(1,:)
tmp1 = min(tmp(1,:));
tmp = tmp1;
for i = 1:3
%     tmp = [tmp(1)/4, tmp];
    tmp = [tmp, tmp(end)*2];
end
% semilogy([2:3],tmp(2:3),'k','linewidth',1)
set(gca,'XTick',[1:4])
% ylim([0.0003 0.02]);
%ylim([0.0002 0.11]);
ylim([3e-5 7e-2]);
legend('Pressure mean', 'Pressure modes', 'Velocity means','Velocity modes',...
    'Optimal Convergence', 'Location','NorthWest')
set(gca,'Fontsize',18)
print('-dpng','-r400','img/LidConvTime')
print('-depsc','img/LidConvTime')

