%close all
%clear all


batchfilename = 'new_batch4.m';

groups = 5;  %Number of possible Hs's
pergroup = 3;  %Number of Bouys per Hs.


ratio = 2;
b = 8000;
dir = 'Batch4Results\'

PTO_Results = NaN(groups,pergroup);  %Array to hold PTO results,  one row for each Hs, one column for each A.
prow_disp_mean_Results = NaN(groups,pergroup);  %Array to hold pow_disp_mean results,  one row for each Hs, one column for each A.


Hs = NaN(groups,pergroup);
ddir = cell(groups,pergroup);
Lc = 0.4*ones(groups,pergroup);
Ls = NaN(groups,pergroup);
bb = b*ones(groups,pergroup);
A = NaN(groups,pergroup);
dt = 0.05*ones(groups,pergroup);
tf = 500*ones(groups,pergroup);
names = cell(groups,pergroup);

%Hs = 1, A2
Hs(1,1) = 1;
A(1,1) = 2;
ddir{1,1} = dir;
names{1,1} = 'BuoyA2';
Ls(1,1) = ratio*A(1,1)/Lc(1,1);


%Hs = 1, A10
Hs(1,2) = 1;
A(1,2) = 10;
ddir{1,2} = dir;
names{1,2} = 'BuoyA10';
Ls(1,2) = ratio*A(1,2)/Lc(1,2);


%Hs = 1, A20
Hs(1,3) = 1;
A(1,3) = 20;
ddir{1,3} = dir;
names{1,3} = 'BuoyA20';
Ls(1,3) = ratio*A(1,3)/Lc(1,3);


%Hs = 3, A2
Hs(3,1) = 3;
A(3,1) = 2;
ddir{3,1} = dir;
names{3,1} = 'BuoyA2';
Ls(3,1) = ratio*A(3,1)/Lc(3,1);


%Hs = 3, A10
Hs(3,2) = 3;
A(3,2) = 10;
ddir{3,2} = dir;
names{3,2} = 'BuoyA10';
Ls(3,2) = ratio*A(3,2)/Lc(3,2);


%Hs = 3, A20
Hs(3,3) = 3;
A(3,3) = 20;
ddir{3,3} = dir;
names{3,3} = 'BuoyA20';
Ls(3,3) = ratio*A(3,3)/Lc(3,3);


%Hs = 5, A2
Hs(5,1) = 5;
A(5,1) = 2;
ddir{5,1} = dir;
names{5,1} = 'BuoyA2';
Ls(5,1) = ratio*A(5,1)/Lc(5,1);


%Hs = 5, A10
Hs(5,2) = 5;
A(5,2) = 10;
ddir{5,2} = dir;
names{5,2} = 'BuoyA10';
Ls(5,2) = ratio*A(5,2)/Lc(5,2);


%Hs = 5, A20
Hs(5,3) = 5;
A(5,3) = 20;
ddir{5,3} = dir;
names{5,3} = 'BuoyA20';
Ls(5,3) = ratio*A(5,3)/Lc(5,3);


for i = 1:groups
    for j = 1:pergroup
      name = names{i,j};
      pto_mean = -1; pow_disp_mean = -1;
      dtt = dt(i,j);
       if(~isnan(Hs(i,j)))
        while (pto_mean < 0)
            
          [pto_mean pow_disp_mean] = Compute_FlatPlate_Performance(ddir{i,j},names{i,j},Hs(i,j),Ls(i,j),Lc(i,j),dtt,tf(i,j),bb(i,j));
          
          if(pto_mean == -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_FlappingFoil_%s\n',Hs(i,j),Ls(i,j),Lc(i,j),dtt,tf(i,j),bb(i,j),names{i,j});
            fclose(fid);
            pto_mean = NaN;
            pow_disp_mean = NaN;
            break;
          end
          if(isnan(pto_mean))  %Results not converged, look for finer timestep results
            dtt = dtt/2;
            pto_mean = -1;
          else
            PTO_Results(i,j) = pto_mean; 
            pow_disp_mean_Results(i,j) = pow_disp_mean; 
          end
        end
      end
    end
end

PTO_Results
    
figure('Position', [55 100 1175 844],'PaperOrientation','landscape','PaperPosition',[0 0 11 8.5])
set(gca,'FontSize',18);
bar(PTO_Results,'group')
xlabel('Hs (m)');
ylabel('Power Generated (Watts)');
legend(names{1,:},'Location','NorthWest');
title(['Flat\_Plate   Power Generated:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlatPlate_pto_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlatPlate_pto_mean_b' num2str(b) '_ratio' num2str(ratio)]);


figure('Position', [55 100 1175 844],'PaperOrientation','landscape','PaperPosition',[0 0 11 8.5])
set(gca,'FontSize',18);
bar(pow_disp_mean_Results,'group')
xlabel('Hs (m)');
ylabel('Power Dissipated (Watts)');
legend(names{1,:},'Location','NorthWest');
title(['Flat\_Plate   Power Dissipated:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlatPlate_pow_disp_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlatPlate_pow_disp_mean_b' num2str(b) '_ratio' num2str(ratio)]);

