% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

% This function computes the design and off design steady state performance
% of the Fan

function [Gas_out, Mech_out] = Fan(Gas_in, Speed, Beta, Mass_Fan_Tr,PR_Fan_Tr, Efficiency_Fan_Tr, D_Fan)

% Declare Global Viariable Data
gam_cold = 1.4;
cp_cold = 1005;
R = 287;

T1 = Gas_in(1);
P1 = Gas_in(2);
G1 = Gas_in(3);

% Fan Design Parameters
Speed_design_Fan = D_Fan(1);
Mass_design_Fan = D_Fan(2);
PR_design_Fan = D_Fan(3);
Efficiency_design_Fan = D_Fan(4);
T_design_Fan = D_Fan(6);
P_design_Fan = D_Fan(7);

% Fan equations
w = Speed*Speed_design_Fan/sqrt(T_design_Fan/T1);                       % Relative Corrected Speed         

Mass_nd = Mass_Fan_Tr(Beta, Speed);                                     % Relative Corrected Mass Flow
PR_nd = PR_Fan_Tr(Beta, Speed);                                         % Pressure Ratio
Efficiency_nd = Efficiency_Fan_Tr(Beta, Speed);                         % Efficiency 

G2 = Mass_nd*Mass_design_Fan*(sqrt(T_design_Fan/T1)*(P1/P_design_Fan));  % Mass Flow  
P2 = ((PR_nd-1)*(PR_design_Fan-1)+1)*P1;                                % Pressure
T2s = T1*(P2/P1)^((gam_cold-1)/gam_cold);                               % Isentropic Temperature        
T2 = (T2s-T1)/(Efficiency_nd*Efficiency_design_Fan)+T1;                 % True Temperature 
Power = G2*cp_cold*(T1-T2);

Gas_out(1:3) = [ T2, P2, G2]; 
Mech_out =[w, Power];
end