clear all, clc, clf, close all
d0=fileparts([pwd,filesep]);
d0=d0(1:strfind(d0,'trunk')+4);
force = true;
root = 'DOnumPap';

files = dir(sprintf('%s/Save/%s/Cylinder_RE*',d0,root));
%%
for iii = 1:length(files)
   load(sprintf('%s/Save/%s/%s/param.mat',d0,root,files(iii).name)) ;
   app = rmfield(app,'savemat');
   file2 = dir(sprintf('%s/Save/%s/%s/0*.mat',d0,root,files(iii).name));
   k = 0;
   Nbcs = 5;
   CylinderPlots
   %%
   for jjj = length(file2):-floor(length(file2)/2):1
       %%
       if ~exist(sprintf('%s/Save/%s/%s/%05d.png',d0,root,...
                files(iii).name,jjj),'file') || force
       clf 
       load(sprintf('%s/Save/%s/%s/%s',d0,root,files(iii).name,file2(jjj).name)) ;
       CylinderPlots
       figure(1)
       print('-dpng','-r400',sprintf('%s/Save/%s/%s/%05d',d0,root,files(iii).name,jjj));
       fprintf('%s/Save/%s/%s/%05d\n',d0,root,files(iii).name,jjj)
       if jjj == length(file2)
           figure(2)
           print('-dpng','-r400',sprintf('%s/Save/%s/%s/enerfnl',d0,root,files(iii).name));
           print('-depsc',sprintf('%s/Save/%s/%s/enerfnl',d0,root,files(iii).name));
           fprintf('%s/Save/%s/%s/%05d\n',d0,root,files(iii).name,jjj)
       end
       k=1;
       %% Plot 2 realizations
       clf
       i=0;
       subplot(NPLOTSx,NPLOTSy, 5 * i + 1: 5 * (i + 1) - 1)
       [dum ids] = max(YYt(:,1));
       u1 = u + ui * YYt(ids,:)';
       v1 = v + vi * YYt(ids,:)';
       P1 = P + Pi * YYt(ids,:)';
       CylinderPlotsRealiz;
       
       i=2;
       [dum ids] = min(YYt(:,1));
       u1 = u + ui * YYt(ids,:)';
       v1 = v + vi * YYt(ids,:)';
       P1 = P + Pi * YYt(ids,:)';
       CylinderPlotsRealiz;
       
       print('-dpng','-r400',sprintf('%s/Save/%s/%s/real_%05d',d0,root,files(iii).name,jjj));
       fprintf('%s/Save/%s/%s/%05d\n',d0,root,files(iii).name,jjj)
       end
   end
end

%% 
close all, clear all, clf ,clc, close all
d0=fileparts([pwd,filesep]);
d0=d0(1:strfind(d0,'trunk')+4);
force = true;
root = 'DOnumPap';

files = dir(sprintf('%s/Save/%s/Cylinder_RE*',d0,root));
MC = [1e4];
MCe = [1e5];
LINE = {'-','--','-.',':','-^'};
LINE2 = {'-.','--','-','.-','-^'};
III = 1;
III2 = 1;
NN = [1 2 3 5 10];
NN2 = [1 2 3 5 10];
for iii = 1:length(files)
    file2 = dir(sprintf('%s/Save/%s/%s/0*.mat',d0,root,files(iii).name));
    load(sprintf('%s/Save/%s/%s/param.mat',d0,root,files(iii).name),'app') ;
    load(sprintf('%s/Save/%s/%s/%s',d0,root,files(iii).name,file2(end).name),'EIGPLOT','T');
    if files(iii).name(end) == '4' && app.MC == MCe
        Exact = load(sprintf('%s/Save/%s/%s/%s',d0,root,files(iii).name,file2(end).name),'EIGPLOT','T');
    end
    if files(iii).name(end) == '4'
        figure(1)
        subplot(211)
        semilogy(linspace(0,T,size(EIGPLOT,2)), EIGPLOT(1,:)', LINE{III});
        LEG{III} = num2str(app.MC);
        III = III + 1;
        hold on
    end
    if app.MC == MC
        figure(1)
        subplot(212)
        semilogy(linspace(0,T,size(EIGPLOT,2)), EIGPLOT(1,:)', LINE2{III2});  
        LEG2{III2} = files(iii).name(end);
        III2 = III2 + 1;
        hold on
    end
end

III = 1;
III2 = 1;
for iii = 1:length(files)
    file2 = dir(sprintf('%s/Save/%s/%s/0*.mat',d0,root,files(iii).name));
    load(sprintf('%s/Save/%s/%s/param.mat',d0,root,files(iii).name),'app') ;
    load(sprintf('%s/Save/%s/%s/%s',d0,root,files(iii).name,file2(end).name),'EIGPLOT','T');
  
    if files(iii).name(end) == '4'
        figure(1)
        subplot(211)
        semilogy(linspace(0,T,size(EIGPLOT,2)), EIGPLOT(NN,:)', LINE{III});
        LEG{III} = sprintf('Samples: %0.0e',app.MC);
        III = III + 1;
        hold on
%         figure(2)
%         subplot(211)
%         plot(linspace(0,T,size(EIGPLOT,2)), abs(EIGPLOT(1:NN,:)' - Exact.EIGPLOT(1:NN,:)'), LINE{III});
%         LEG{III} = sprintf('Samples: %0.0e',app.MC);
%         III = III + 1;
%         hold on
    end
    if app.MC == MC
        figure(1)
        subplot(212)
        semilogy(linspace(0,T,size(EIGPLOT,2)), EIGPLOT(NN2,:)', LINE2{III2});  
        LEG2{III2} = ['Order: ',files(iii).name(end)];
        III2 = III2 + 1;
        hold on
%         figure(2)
%         subplot(212)
%         plot(linspace(0,T,size(EIGPLOT,2)), abs(EIGPLOT(1:NN2,:)'-Exact.EIGPLOT(1:NN2,:)'), LINE{III2});  
%         LEG2{III2} = ['Order: ',files(iii).name(end)];
%         III2 = III2 + 1;
%         hold on
    end
end
figure(1)
subplot(211)
legend(LEG,'Location','SouthWest')
hold off
% print('-dpng','-r300',sprintf('%s/Save/%s/enerfnl_MC',d0,root));
subplot(212)
legend(LEG2,'Location','SouthWest')
hold off
print('-dpng','-r400',sprintf('%s/Save/%s/enerfnl_OrdMC',d0,root));
print('-depsc',sprintf('%s/Save/%s/enerfnl_OrdMC',d0,root));

% figure(2)
% subplot(211)
% hold off
% % print('-dpng','-r300',sprintf('%s/Save/%s/enerfnl_MC',d0,root));
% subplot(212)
% hold off
% print('-dpng','-r300',sprintf('%s/Save/%s/enerfnl_ERROrdMC',d0,root));