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

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

% Declare Global Viariable Data
gam_cold = 1.4;      % Ratio of Specific Heats Estimated from (R. Sonntag et al, Fundamentals of Thermodynamics)     
cp_cold = 1005;      % Specific Heat at Constant Pressure Estimated from (R. Sonntag et al, Fundamentals of Thermodynamics)

T1 = Gas_in(1);      % Temperature In (Kelvin)
P1 = Gas_in(2);      % Pressure In (Pascal)
G1 = Gas_in(3);      % Mass Flow In (kg/s)

% Fan Design Parameters
Speed_design_Fan = D_Fan(1);        % Design Rotational Speed (rad/s)                                           
Mass_design_Fan = D_Fan(2);         % Design Mass Flow (kg/s)
PR_design_Fan = D_Fan(3);           % Design Pressure Ratio
Efficiency_design_Fan = D_Fan(4);   % Design Efficiency
T_design_Fan = D_Fan(6);            % Design Temperature at Inlet (Kelvin)
P_design_Fan = D_Fan(7);            % Design Pressure at Inlet (Pascal)

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

Mass_nd = Mass_Fan_Tr(Beta, Speed);                                     % Corrected Normalized Mass Flow (Lookup Table processed using Smooth C8.2 by Dr. Joachim Kurzke)
PR_nd = PR_Fan_Tr(Beta, Speed);                                         % Normalized Pressure Ratio (Lookup Table processed using Smooth C8.2 by Dr. Joachim Kurzke)
Efficiency_nd = Efficiency_Fan_Tr(Beta, Speed);                         % Normalized Efficiency (Lookup Table processed using Smooth C8.2 by Dr. Joachim Kurzke)

G2 = Mass_nd*Mass_design_Fan*(sqrt(T_design_Fan/T1)*(P1/P_design_Fan)); % Mass Flow Calculation (Based on Gasturb11 by Dr. Joachim Kurzke) 
P2 = ((PR_nd-1)*(PR_design_Fan-1)+1)*P1;                                % Pressure Calculation (Pascal) (Based on Gasturb11 by Dr. Joachim Kurzke)
T2s = T1*(P2/P1)^((gam_cold-1)/gam_cold);                               % Isentropic Temperature (Anderson, Fundamentals of Aerodynamics)       
T2 = (T2s-T1)/(Efficiency_nd*Efficiency_design_Fan)+T1;                 % Actual Temperature (Anderson, Fundamentals of Aerodynamics)
Power = G2*cp_cold*(T1-T2);

Gas_out(1:3) = [ T2, P2, G2];                                           % Save Gas Outputs
Mech_out =[w, Power];                                                   % Save Mechanical Outputs
end