%close all
%clear all


batchfilename = 'new_batch.m';

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


ratio = 4;
b = 8000;
dir = 'Batch6Results\'

PTO_Results = NaN(groups,pergroup);  %Array to hold PTO results,  one row for each Hs, one column for each A.
u_mean_Results = NaN(groups,pergroup);  %Array to hold u_mean results,  one row for each Hs, one column for each A.
u_force_mean_Results = NaN(groups,pergroup);  %Array to hold u_force_mean results,  one row for each Hs, one column for each A.
prop_power_mean_Results = NaN(groups,pergroup);  %Array to hold prop_power_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 = 2, A2
Hs(2,1) = 2;
A(2,1) = 2;
ddir{2,1} = dir;
names{2,1} = 'BuoyA2';
Ls(2,1) = ratio*A(1,1)/Lc(1,1);


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


%Hs = 2, A20
Hs(2,3) = 2;
A(2,3) = 20;
ddir{2,3} = dir;
names{2,3} = 'BuoyA20';
Ls(2,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 = 4, A2
Hs(4,1) = 4;
A(4,1) = 2;
ddir{4,1} = dir;
names{4,1} = 'BuoyA2';
Ls(4,1) = ratio*A(3,1)/Lc(3,1);


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


%Hs = 4, A20
Hs(4,3) = 4;
A(4,3) = 20;
ddir{4,3} = dir;
names{4,3} = 'BuoyA20';
Ls(4,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);


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


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


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


for i = 1:groups
    for j = 1:pergroup
      name = names{i,j};
      u_mean = -1; pto_mean = -1; pow_disp_mean = -1;
      dtt = dt(i,j);
       if(~isnan(Hs(i,j)))
        while (pto_mean < 0)
            
          [u_mean u_force_mean pto_mean pow_disp_mean prop_power_mean] = Compute_FlappingFoil_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);
            u_mean = NaN;
            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
            u_mean_Results(i,j) = u_mean;
            u_force_mean_Results(i,j) = u_force_mean; 
            PTO_Results(i,j) = pto_mean; 
            prop_power_mean_Results(i,j) = prop_power_mean; 
          end
        end
      end
    end
end

save [dir 'Summary_Results_b' num2str(b) '_ratio' num2str(ratio)] PTO_Results u_mean_Results u_force_mean_Results prop_power_mean_Results Hs;
    
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(['Flapping\_Foil   Power Generated:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlappingFoil_pto_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlappingFoil_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(u_mean_Results,'group')
xlabel('Hs (m)');
ylabel('Forward Speed (m/s)');
legend(names{1,:},'Location','NorthWest');
title(['Flapping\_Foil   Forward Speed:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlappingFoil_u_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlappingFoil_u_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(u_force_mean_Results/4.45,'group')
xlabel('Hs (m)');
ylabel('Mean Propulsion Force (lbs))');
legend(names{1,:},'Location','NorthWest');
title(['Flapping\_Foil   Mean Propulsion Force:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlappingFoil_u_force_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlappingFoil_u_force_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(prop_power_mean_Results,'group')
xlabel('Hs (m)');
ylabel('Mean Propulsion Power (Watts)');
legend(names{1,:},'Location','NorthWest');
title(['Flapping\_Foil   Mean Propulsion Power:    ratio = ' num2str(ratio) '    b = ' num2str(b) ' N/m/s']);
print('-dpdf',[dir 'Summary_FlappingFoil_prop_power_mean_b' num2str(b) '_ratio' num2str(ratio)]);
print('-dill',[dir 'Summary_FlappingFoil_prop_power_mean_b' num2str(b) '_ratio' num2str(ratio)]);
