%
% utility to generate plots based on input text file
% generated by mlmc_test_100.m
%
% mlmc_plot_100(filename)
%

function mlmc_plot_100(filename)

close all;

%
% read in data
%

fid = fopen([filename '.txt'],'r');

line = '    ';
while (length(line)<20) | (strcmp(line(1:4),'*** ')==0)
  line = [ fgetl(fid) '    ' ];
end
file_version = sscanf(line(27:34),'%f');
if isempty(file_version)
  file_version = 0.8;
end

for i=1:8
  line = fgetl(fid);
end

value = sscanf(line(15:end),'%f');
novalue = isempty(value);

line = fgetl(fid);

l = 0;
while ~isnumeric(line)
  l = l+1;
  line = fgetl(fid);
  Eps(l) = sscanf(line(7:end),'%f');

  line = fgetl(fid);

  for j=1:5:100
    line = fgetl(fid);
    data(l,j:j+4) = sscanf(line,'%f');
  end

  line = fgetl(fid);
end

if novalue
  err  = data - sum(data(1,:))/100;
  %  err  = data - repmat(sum(data,2)/100,1,100);
else
  err  = data - value;
end

ave = sum(err,2)/100;
rms = sqrt((sum(err.^2,2)/100 - ave.^2));
Eps = Eps';

%
% plot results
%

figs(1) = figure; 
pos=get(gcf,'pos'); pos(3:4)=pos(3:4).*[1.0 0.75]; set(gcf,'pos',pos);

set(0,'DefaultAxesColorOrder',[0 0 0]);
set(0,'DefaultAxesLineStyleOrder','-*|--*|-.*')

subplot(1,2,1)
loglog(Eps,3*Eps,'--',Eps,Eps,'-.')
hold on;
for l = 1:length(Eps)
  loglog(Eps(l),abs(err(l,:)),'o');
end
legend('3*Tol','  Tol','Location','NorthWest');
xlabel('accuracy $\varepsilon$','Interpreter','latex'); 
ylabel('Error');

subplot(1,2,2)
if novalue
    %  semilogx(Eps,rms./Eps)
    %  legend('MC error','Location','SouthWest');
  semilogx(Eps,sqrt(rms.^2+ave.^2)./Eps,Eps,rms./Eps,Eps,abs(ave./Eps))
  legend('RMS error','MC error','weak error (est)','Location','SouthWest');
else
  semilogx(Eps,sqrt(rms.^2+ave.^2)./Eps,Eps,rms./Eps,Eps,abs(ave./Eps))
  legend('RMS error','MC error','weak error','Location','SouthWest');
end
lims = axis; lims(3)=0; lims(4)=max(lims(4),1); axis(lims)
xlabel('accuracy $\varepsilon$','Interpreter','latex'); 
ylabel('Error / $\varepsilon$','Interpreter','latex');
