function [u_mean u_force_mean pto_mean pow_disp_mean prop_power_mean] = Compute_FlappingFoil_Performance(dir,name,Hs,Ls,Lc,dt,tf,b)

results_filenm = create_filenm([dir 'FlappingFoil_' name],Hs,Ls,Lc,dt,tf,b)

if(exist([results_filenm '.mat']))
  load([results_filenm '.mat']);
else
  u_mean = -1;
  pto_mean = -1;
  pow_disp_mean = -1;
  prop_power_mean = -1;
  u_force_mean = -1;
  return;
end

t = get(Buoy,'t');

%Get Buoy motion results
x = get(Buoy,'x');
xdot = get(Buoy,'xdot');
xddot = get(Buoy,'xddot');
 
%Get Foil motion results
y = get(Foil,'x');
ydot = get(Foil,'xdot');
yddot = get(Foil,'xddot');

%plotting results

Bforce = get(Buoy,'Forces');
Fforce = get(Foil,'Forces');


idx = find(isnan(x(:,1)));
if(~isempty(idx))
    disp('WARNING:  NaN detected');
    u_mean = NaN;
    pto_mean = NaN;
    pow_disp_mean = NaN;
    prop_power_mean = NaN;
    u_force_mean = NaN;
    return;
end

N = length(t);

xoB = get(Buoy,'x0');
xoF = get(Foil,'x0');

S = get(Foil,'Area');

Elongation = zeros(N,1);
ElongationRate = zeros(N,1);
SpringTension = zeros(N,1);
DampingTension = zeros(N,1);
s = zeros(N,3);
for i = 1:N
    [Elongation(i),ElongationRate(i),s(i,:)] = TetherElongations(xoB,x(i,:)',xdot(i,:)',p_aB,xoF,y(i,:)',ydot(i,:)',p_aF);
    if(Elongation(i) > 0)  %If ram is fully retracted, tether is slack and no force is exerted.
        SpringTension(i) = ppval(Spring_Force_Coeffs,Elongation(i));
        DampingTension(i) = ppval(Damping_Force_Coeffs,ElongationRate(i));
    else
        Elongation(i) = 0;
        ElongationRate(i) = 0;
    end
end

%Compute and display auxillary results

pto_power = ElongationRate.*DampingTension;
disp_power = -ydot(1:N,3).*Fforce{3}(1:N,3);
prop_power = -xdot(1:N,1).*Bforce{end}(1:N,1);
u_force = Bforce{end}(1:N,1);

u_force_mean = mean(u_force);
u_mean = mean(xdot(1:N,1));
pto_mean = mean(pto_power);
pow_disp_mean = mean(disp_power);
prop_power_mean = u_mean*u_force_mean;

disp(['Mean Forward Speed = ' num2str(u_mean) ' m/s']);
disp(['Mean Horizontal Force = ' num2str(u_force_mean/4.45) ' lbs']);
disp(['Mean PTO Power = ' num2str(pto_mean) ' Watts']);
disp(['Mean Power Dissipated = ' num2str(pow_disp_mean) ' Watts']);
disp(['Mean Propulsion Power = ' num2str(prop_power_mean) ' Watts']);
disp(['Peak Tether Tension = ' num2str(max(SpringTension+DampingTension)/4.45) ' lbs']);
disp(['Mean Tether Tension = ' num2str(mean(SpringTension+DampingTension)/4.45) ' lbs']);
disp(['Flat Foil Surface area = ' num2str(S(3)) ' m^2']);
disp(['Max Foil Pressure = ' num2str((max(SpringTension+DampingTension)/4.45)/(S(3)*39.4^2)) ' psi']);



