close all
clear all

batchfilename = 'new_batch4.m';
dir = 'Batch4Results\'

ratio = [.25 .5 1 2 4 6 8];
names = {'BuoyA2','BuoyA10','BuoyA20'};
%names{1} = 'BuoyA2';
areas = [2,10,20];
%areas = 2;

b = 0:1000:12000;
Lc = 0.4;
tf = 500;
dt = 0.05;

for i = 1:length(names)
  for Hs = [1 3 5]
      name = names{i}; A = areas(i);
      pto_mean  = -ones(length(ratio),length(b));
      pow_disp_mean = -ones(length(ratio),length(b));

      for j = 1:length(ratio)
          Ls = ratio(j)*A/Lc
          for k = 1:length(b)
            dtt = dt;
            while (pto_mean(j,k) < 0)
               [pto_mean(j,k) pow_disp_mean(j,k)] = Compute_FlatPlate_Performance(dir,name,Hs,Ls,Lc,dtt,tf,b(k));

               if(pto_mean(j,k) == -1)  %Results filen not found
                   fid = fopen(batchfilename,'a');
                   fprintf(fid, 'clear all;close all;Hs=%f;Ls=%f;Lc=%f;dt=%f;tf=%f;b=%f;TD_FlatPlate_%s\n',Hs,Ls,Lc,dtt,tf,b(k),name);
                   fclose(fid);
                   pto_mean(j,k) = NaN;
                   pow_disp_mean(j,k) = NaN;
                   break;
               end
               if(isnan(pto_mean(j,k)))  %Results not converged, look for finer timestep results
                   dtt = dtt/2;
                   pto_mean(j,k) = -1;
               end
            end
          end
      end
      
    figure('Position', [55 100 1175 844],'PaperOrientation','landscape','PaperPosition',[0 0 11 8.5])
    subplot(3,1,1)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    plot(ratio,pto_mean)
    title([name ' : Hs = ' num2str(Hs)]);
    ylabel('Mean PTO (watts)','FontSize',14);
    legend(['b = ' num2str(b(1)) ' N/m/s'],num2str(b(2)),num2str(b(3)),num2str(b(4)),num2str(b(5)),num2str(b(6)),num2str(b(7)),num2str(b(8)),num2str(b(9)),num2str(b(10)),num2str(b(11)),num2str(b(12)),num2str(b(13)),'FontSize',10);
    set(gca,'FontSize',14);
    subplot(3,1,2)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    plot(ratio,pow_disp_mean)
    ylabel('mean power Dissipated (watts)','FontSize',14);
    subplot(3,1,3)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    plot(ratio,pto_mean./pow_disp_mean)
    ylabel('Power  Ratio','FontSize',14);
    xlabel('Foil to Buoy Area Ratio'); 
    
    print('-dpdf',[dir 'FlatPlate_' name '_Hs' num2str(Hs) '_Vr'])
    print('-dill',[dir 'FlatPlate_' name '_Hs' num2str(Hs) '_Vr'])   
    
    figure('Position', [55 100 1175 844],'PaperOrientation','landscape','PaperPosition',[0 0 11 8.5])
    subplot(3,1,1)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    plot(b,pto_mean')
    title([name ' : Hs = ' num2str(Hs)]);
    ylabel('Mean PTO (watts)','FontSize',14);
    legend(['ratio = ' num2str(ratio(1))],num2str(ratio(2)),num2str(ratio(3)),num2str(ratio(4)),num2str(ratio(5)),num2str(ratio(6)),num2str(ratio(7)),'FontSize',10);
    set(gca,'FontSize',14);
    subplot(3,1,2)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    set(gca,'FontSize',14);
    plot(b,pow_disp_mean')
    ylabel('mean power Dissipated (watts)','FontSize',14);
    subplot(3,1,3)
    set(gca,'NextPlot','replacechildren')
    set(gca,'LineStyleOrder',{'-x',':x'})
    set(gca,'FontSize',14);
    plot(b,pto_mean'./pow_disp_mean')
    ylabel('Power  Ratio','FontSize',14);
    xlabel('damping coeff N/(m/s)'); 
    print('-dpdf',[dir 'FlatPlate_' name '_Hs' num2str(Hs) '_Vb'])
    print('-dill',[dir 'FlatPlate_' name '_Hs' num2str(Hs) '_Vb'])
  end
end

