% 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 script sizes turbines based on the design of the cold section
% The assumption is that design point performance occurs at beta = 0.5
% A more accurate description of this will be provided using Dr. Joachim
% Kurzke's Smooth T8.2 and Smooth C8.2 map scaling softwares.
start = tic;
% Design Pressure, Temperature and Mach number
P_amb = 22729;                   % Cruise Ambient Pressure at 36000ft (Pascals)
T_amb = 216.5;                   % Cruise Ambient Temperature at 36000ft (Kelvin)
Mach  = 0.8;                     % Cruise Mach Number


% Gas Properties
global cp_cold cp_hot R gam_cold gam_hot
cp_cold = 1045;
cp_hot = 1244;
R = 287;
gam_cold= 1.4;
gam_hot = 1.3;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Cold Section

% Design Data for Fan
global Speed_design_Fan;         % Design Speed
global Mass_design_Fan;          % Design Mass Flow
global PR_design_Fan;            % Design Pressure Ratio
global Efficiency_design_Fan;    % Design Efficiency

Speed_design_Fan = 1500;         % Design Speed
Mass_design_Fan = 220;           % Design Mass Flow
PR_design_Fan = 1.2;             % Design Pressure Ratio
Efficiency_design_Fan = 0.9;     % Design Efficiency
Beta_Fan = 0.5;                  % Design Beta Value


% Design Data for Low Pressure Compressor
global Speed_design_LPC;         % Design Speed
global Mass_design_LPC;          % Design Mass Flow
global PR_design_LPC;            % Design Pressure Ratio
global Efficiency_design_LPC;    % Design Efficiency

Speed_design_LPC = 2000;         % Design Speed
Mass_design_LPC = 15.134;        % Design Mass Flow
PR_design_LPC = 4;               % Design Pressure Ratio
Efficiency_design_LPC = 0.9;     % Design Efficiency
Beta_LPC = 0.5;                  % Design Beta Value



% Design Data for High Pressure Compressor
global Speed_design_HPC;         % Design Speed
global Mass_design_HPC;          % Design Mass Flow
global PR_design_HPC;            % Design Pressure Ratio
global Efficiency_design_HPC;    % Design Efficiency

Speed_design_HPC = 3000;         % Design Speed
Mass_design_HPC = 15.134;        % Design Mass Flow
PR_design_HPC = 14;              % Design Pressure Ratio
Efficiency_design_HPC = 0.9;     % Design Efficiency
Beta_HPC = 0.5;                  % Design Beta Value


% Burner Maximum Temperature
T4 = 1600;                       % Kelvin 
PR_Burner= 0.9;                  % Burner Pressure Ratio

% Hot Section

%Design Data for High Pressure Turbine
global Speed_design_HPT;         % Design Speed
global Mass_design_HPT;          % Design Mass Flow
global Efficiency_design_HPT;    % Design Efficiency

Speed_design_HPT = 3000;         % Design Speed
Mass_design_HPT = 15.134;        % Design Mass Flow
Efficiency_design_HPT = 0.9;     % Design Efficiency
Beta_HPT = 0.5;                  % Design Beta Value

% Design Data for Intermediate Pressure Turbine
global Speed_design_IPT;         % Design Speed
global Mass_design_IPT;          % Design Mass Flow
global Efficiency_design_IPT;    % Design Efficiency

Speed_design_IPT = 2000;         % Design Speed
Mass_design_IPT = 15.134;        % Design Mass Flow
Efficiency_design_IPT = 0.9;     % Design Efficiency
Beta_HPT = 0.5;                  % Design Beta Value


% Design Data for Low Pressure Turbine
global Speed_design_LPT;         % Design Speed
global Mass_design_LPT;          % Design Mass Flow
global Efficiency_design_LPT;    % Design Efficiency

Speed_design_LPT = 1500;         % Design Speed
Mass_design_LPT = 15.134;        % Design Mass Flow
Efficiency_design_LPT = 0.9;     % Design Efficiency
Beta_HPT = 0.5;                  % Design Beta Value


% Nozzle
% Area will be calculated Automatically

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Size Hot Section to Match Cold Section

% Diffuser
[T0, P0] = Design_Diffuser(T_amb,P_amb, Mach);

% Fan
[T1, P1, G1, Power_1] = Design_Fan(T0, P0);

% LPC
[T2, P2, G2, Power_2] = Design_LPC(T1, P1);

% HPC
[T3, P3, G3, Power_3] = Design_HPC(T2, P2);

Power_spool_1 = Power_1;                % Low Spool Power
Power_spool_2 = Power_2;                % Intermediate Spool Power
Power_spool_3 = Power_3;                % High Spool Power

% Burner
[G_fuel_design, P4] = Design_Burner(T3, T4, P3, G2, PR_Burner);

% HPT
[T5, P5, PR_design_HPT] = Design_HPT(T4, P4, G2, Power_spool_3);

%IPT
[T6, P6, PR_design_IPT] = Design_IPT(T5, P5, G2, Power_spool_2);

% LPT
[T7, P7, PR_design_LPT] = Design_LPT(T6, P6, G2, Power_spool_1);

% Nozzle          
[T_core,P_core A_Core] = Design_Nozzle(T7,P7,P_amb, G2);

% Bypass Nozzle
[T_bypass, P_bypass, A_Bypass] = Design_Nozzle(T1,P1,P_amb, G1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Design Output

% Fan
T_design_Fan = T0;
P_design_Fan = P0;

% LPC
T_design_LPC = T1;
P_design_LPC = P1;

% HPC
T_design_HPC = T2;
P_design_HPC = P2;

% Burner

% HPT
T_design_HPT = T4;
P_design_HPT = P4;

% IPT
T_design_IPT = T5;
P_design_IPT = P5;

% LPT
T_design_LPT = T6;
P_design_LPT = P6;

end_time = toc(start);
% Nozzle

BPR = Mass_design_Fan/Mass_design_LPC-1;
BPR_AOHEX = 0.01;
BPR = BPR-BPR*BPR_AOHEX;
u_core = sqrt(2*cp_hot*(T7-T_core));
u_bypass = sqrt(2*cp_cold*(T1-T_bypass));
u_flight = 0.8*sqrt(1.4*287*216.5);
f = G_fuel_design/Mass_design_LPC;

eta_total = (u_core+BPR*u_bypass-(1+BPR)*u_flight+f*u_core)*u_flight/((1+f)*cp_hot*T4-cp_hot*T3);
spt = (u_core+BPR*u_bypass-(1+BPR)*u_bypass+f*u_core)/(1+BPR);

clear('G1', 'G2', 'G3', 'R', 'gam_hot', 'gam_cold', 'cp_hot', 'cp_cold')
clear('Power_1', 'Power_2', 'Power_3')
clear('T0', 'T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7')
clear('P0', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7')
clear('Beta_Fan', 'Beta_LPC', 'Beta_HPC', 'Beta_HPT', 'Beta_LPT'); 