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

function [Gas_out, Mech_out] = LPC(Gas_in, Speed, Beta, Mass_LPC_Tr,PR_LPC_Tr, Efficiency_LPC_Tr, D_LPC)

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

% LPC Design Parameters
Speed_design_LPC = D_LPC(1);        % Design Rotational Speed (rad/s)
Mass_design_LPC = D_LPC(2);         % Design Mass Flow (kg/s)
PR_design_LPC = D_LPC(3);           % Design Pressure Ratio
Efficiency_design_LPC = D_LPC(4);   % Design Efficiency
T_design_LPC = D_LPC(6);            % Design Temperature at Inlet (Kelvin)
P_design_LPC = D_LPC(7);            % Design Pressure at Inlet (Pascal)


% LPC equations
w = Speed*Speed_design_LPC/sqrt(T_design_LPC/T1);                       % Relative Corrected Speed         

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

G2 = Mass_nd*Mass_design_LPC*(sqrt(T_design_LPC/T1)*(P1/P_design_LPC)); % Mass Flow (kg/s) Calculation (Based on Gasturb11 by Dr. Joachim Kurzke) 
P2 = ((PR_nd-1)*(PR_design_LPC-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_LPC)+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