close all
clear all

batchfilename = 'new_batch6.m';
dir = 'Batch6Results\'

ratio = 2;
names = {'BuoyA2','BuoyA10','BuoyA20'};
areas = [2,10,20];
%names = {'BuoyA2'};
%areas = 2;
wave_heights = [1 3 5];


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


for i = 1:length(names)
  u_mean  = -ones(length(wave_heights),length(b));
  u_force_mean  = -ones(length(wave_heights),length(b));
  pto_mean  = -ones(length(wave_heights),length(b));
  pow_disp_mean = -ones(length(wave_heights),length(b));
  prop_power_mean = -ones(length(wave_heights),length(b));
    
    for j = 1:length(wave_heights)
      Hs = wave_heights(j);
      name = names{i}; A = areas(i);
          Ls = ratio*A/Lc
          for k = 1:length(b)
            dtt = dt;
            while (pto_mean(j,k) < 0)
               [u_mean(j,k) u_force_mean(j,k) pto_mean(j,k) pow_disp_mean(j,k) prop_power_mean(j,k)] = Compute_FlappingFoil_Performance(dir,name,Hs,Ls,Lc,dtt,tf,b(k));

               if(pto_mean(j,k) == -1)  %Results filenm 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_FlappingFoil_%s\n',Hs,Ls,Lc,dtt,tf,b(k),name);
                   fclose(fid);
                   u_mean(j,k) = NaN;
                   pto_mean(j,k) = NaN;
                   pow_disp_mean(j,k) = NaN;
                   prop_power_mean(j,k) = NaN;
                   u_force_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
    
    close all
    
    figure('Position', [55 100 1175 844],'PaperOrientation','landscape','PaperPosition',[0 0 11 8.5])
    subplot(4,1,1)
    set(gca,'NextPlot','replacechildren');
    set(gca,'LineStyleOrder',{'-x',':x'});
    set(gca,'FontSize',16);
    plot(b,pto_mean')
    legend(['Hs = ' num2str(wave_heights(1)) ' m'],['Hs = ' num2str(wave_heights(2)) ' m'],['Hs = ' num2str(wave_heights(3)) ' m'],'Location','NorthWest')
    title(['FlappingFoil\_' names{i}   '     ratio = ' num2str(ratio)]);
    ylabel('Mean PTO (W)','FontSize',12);
    subplot(4,1,2)
    set(gca,'NextPlot','replacechildren');
    set(gca,'LineStyleOrder',{'-x',':x'});
    set(gca,'FontSize',16);
    plot(b,u_force_mean'/4.45)
    ylabel('mean prop force (lbs)','FontSize',12);
    subplot(4,1,3)
    set(gca,'NextPlot','replacechildren');
    set(gca,'LineStyleOrder',{'-x',':x'});
    set(gca,'FontSize',16);
    plot(b,prop_power_mean')
    ylabel('mean prop power (W)','FontSize',12);
    subplot(4,1,4)
    set(gca,'NextPlot','replacechildren');
    set(gca,'LineStyleOrder',{'-x',':x'});
    set(gca,'FontSize',16);
    plot(b,u_mean')
    ylabel('Mean Speed (m/s)','FontSize',12);
    xlabel('damping coeff N/(m/s)'); 
    print('-dpdf',[dir 'FlappingFoil_' name '_ratio' num2str(ratio) '_Vb'])
    print('-dill',[dir 'FlappingFoil_' name '_ratio' num2str(ratio) '_Vb'])
end

