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

function [Gas_out, Mech_out] = IPT(Gas_in, Speed, Beta, Mass_LPT_Tr,PR_LPT_Tr, Efficiency_LPT_Tr, D_IPT)

% Declare Global Viariable Data
gam_hot = 1.3;      % Ratio of Specific Heats Estimated from (R. Sonntag et al, Fundamentals of Thermodynamics)
cp_hot = 1244;      % 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)


% IPT Design Parameters
Speed_design_IPT = D_IPT(1);        % Design Rotational Speed (rad/s) 
Mass_design_IPT = D_IPT(2);         % Design Mass Flow (kg/s)
PR_design_IPT = D_IPT(3);           % Design Pressure Ratio
Efficiency_design_IPT = D_IPT(4);   % Design Efficiency
T_design_IPT = D_IPT(6);            % Design Temperature at Inlet (Kelvin)
P_design_IPT = D_IPT(7);            % Design Pressure at Inlet (Pascal)

% IPT equations
w = Speed*Speed_design_IPT/sqrt(T_design_IPT/T1);                       % Relative Corrected Speed         

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


G2 = Mass_nd*Mass_design_IPT*(sqrt(T_design_IPT/T1)*(P1/P_design_IPT)); % Mass Flow (kg/s) Calculation (Based on Gasturb11 by Dr. Joachim Kurzke) 
P2 = P1/((PR_nd-1)*(PR_design_IPT-1)+1);                                % Pressure Calculation (Pascal) (Based on Gasturb11 by Dr. Joachim Kurzke)
T2s = T1*(P2/P1)^((gam_hot-1)/gam_hot);                                 % Isentropic Temperature (Kelvin) (Anderson, Fundamentals of Aerodynamics)          
T2 = T1+(T2s-T1)*(Efficiency_nd*Efficiency_design_IPT);                 % Actual Temperature (Kelvin) (Anderson, Fundamentals of Aerodynamics)
Power = G2*cp_hot*(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