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

function [Gas_out, Mech_out] = HPC(Gas_in, Speed, Beta, Mass_HPC_Tr,PR_HPC_Tr, Efficiency_HPC_Tr, D_HPC)

% 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)

% HPC Design Parameters
Speed_design_HPC = D_HPC(1);        % Design Rotational Speed (rad/s)       
Mass_design_HPC = D_HPC(2);         % Design Mass Flow (kg/s)
PR_design_HPC = D_HPC(3);           % Design Pressure Ratio
Efficiency_design_HPC = D_HPC(4);   % Design Efficiency
T_design_HPC = D_HPC(6);            % Design Temperature at Inlet (Kelvin)
P_design_HPC = D_HPC(7);            % Design Pressure at Inlet (Pascal)

% HPC equations
w = Speed*Speed_design_HPC/sqrt(T_design_HPC/T1);                       % Relative Corrected Speed         

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

G2 = Mass_nd*Mass_design_HPC*(sqrt(T_design_HPC/T1)*(P1/P_design_HPC)); % Mass Flow (kg/s) Calculation (Based on Gasturb11 by Dr. Joachim Kurzke)   
P2 = ((PR_nd-1)*(PR_design_HPC-1)+1)*P1;                                % Pressure Calculation (Pascal) (Based on Gasturb11 by Dr. Joachim Kurzke)
T2s = T1*(P2/P1)^((gam_cold-1)/gam_cold);                               % Isentropic Temperature (Kelvin) (Anderson, Fundamentals of Aerodynamics)      
T2 = (T2s-T1)/(Efficiency_nd*Efficiency_design_HPC)+T1;                 % Actual Temperature (Kelvin) (Anderson, Fundamentals of Aerodynamics) 
Power = G2*cp_cold*(T1-T2);                                             % Shaft power output (Watts) (Anderson, Funadementals of Aerodynamics)

Gas_out = [T2 P2 G2];   % Save Gas Outputs
Mech_out =[w, Power];   % Save Mechanical Outputs

end