% 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 uses multi-dimensional Newton-Raphson along with the first order
% backward Euler method to compute the transient performance of a 5 spool 
% turbofan

% Set Air Constants (ISA)
global cp_cold cp_hot R gam_cold gam_hot

cp_cold = 1045;           % Specific Heat at Constant Pressure Cold Section
cp_hot = 1244;            % Specific Heat at Constant Pressure Hot Section
gam_cold = 1.4;           % Ratio of Specific Heats Cold Section
gam_hot = 1.3;            % Ratio of Specific Heats Hot Section
R = 287;                  % Gas Constant

I_low = 20;               % Low Spool Inertia ISA units
I_inter_1 = 1;            % Intermediate Spool 1 Inertia
I_inter_2 = 1;            % Intermediate Spool 2 Inertia
I_inter_3 = 1;            % Intermediate Spool 3 Inertia
I_high = 1;               % High Spool Inertia ISA units

w_low(1) = 1500;          % Low Spool Initial Speed (design point)
w_inter_1(1) = 2000;      % Intermediate Spool 1 Initial Speed (design point)
w_inter_2(1) = 2500;      % Intermediate Spool 2 Initial Speed (design point)
w_inter_3(1) = 3000;      % Intermediate Spool 3 Initial Speed (design point)
w_high(1) = 3500;         % High Spool Initial Speed (design point)


fuel_percentage = 15;     % Fuel step percentage increase and decrease
global G_fuel_design      % Take design point fuel data

fprintf('Initial Speed Low Spool (Rad/s):             %8.2f \n', w_low(1));
fprintf('Initial Speed Intermediate Spool 1 (Rad/s):  %8.2f \n', w_inter_1(1));
fprintf('Initial Speed Intermediate Spool 2 (Rad/s):  %8.2f \n', w_inter_2(1));
fprintf('Initial Speed Intermediate Spool 3 (Rad/s):  %8.2f \n', w_inter_3(1));
fprintf('Initial Speed High Spool 3 (Rad/s):          %8.2f \n', w_high(1));


time_step = 0.25;
max_time = 150;
time = 0:time_step:max_time;
n_timesteps = floor(max_time/time_step+1);

d_constant = 1e-2;               % Auxiliary beta coordinate perturbation value
tolerance = 1e-3;

beta_new(1:10,1) = 0.5;          % Initialize Beta           
Error(1:10,1) = 10*tolerance;    % Initialize Error
torque(1) = 0;                   % Initialize Torque

n_turbo = max(size(beta_new));   % Size of Jacobian

T_amb = 216.15;                  % Ambient conditions
P_amb = 22729;
Mach = 0.8;

% Fuel flow profile has up step and down step at 50 and 100 seconds
G_fuel(1:floor(n_timesteps/3)) = G_fuel_design; 
G_fuel(floor(n_timesteps/3):floor(2*n_timesteps/3))= G_fuel_design*(1+fuel_percentage/100);
G_fuel(ceil(2*n_timesteps/3):n_timesteps)= G_fuel_design*(1-fuel_percentage/100);

t_DF = 0;
tic;

for timer = 1:n_timesteps

% Reset beta map variable to middle of map if fuel flow changes
if timer>1 &&(abs(G_fuel(timer)/G_fuel(timer-1))-1)>tolerance
beta_new(1:10)=0.5;
end

% Initialize iteration count
k = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while max(abs(Error(:,k)))>tolerance   

k = k+1;        % Count number of Iterations

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

% Fan Equations
[T1, P1, G1, Speed_Fan_current, Power_Fan] = Fan(T0, P0, w_low(timer), beta_new(1));

% LPC 1 equations
[T2, P2, G2, Speed_LPC_1_current, Power_LPC_1] = LPC_1(T1, P1, w_inter_1(timer), beta_new(2));

% LPC 2 equations
[T3, P3, G3, Speed_LPC_2_current, Power_LPC_2] = LPC_2(T2, P2, w_inter_2(timer), beta_new(3));

% LPC 3 equations
[T4, P4, G4, Speed_LPC_3_current, Power_LPC_3] = LPC_3(T3, P3, w_inter_3(timer), beta_new(4));

% HPC equations
[T5, P5, G5, Speed_HPC_current, Power_HPC] = HPC(T4, P4, w_high(timer), beta_new(5));


% Burner equations
[T6, P6] =  Burner(T5, P5, G5, G_fuel(timer));

% HPT
[T7, P7, G6, Speed_HPT_current, Power_HPT] = HPT(T6, P6, w_high(timer), beta_new(6));

% IPT 3
[T8, P8, G7, Speed_IPT_3_current, Power_IPT_3] = IPT_3(T7, P7, w_inter_3(timer), beta_new(7));

% IPT 2
[T9, P9, G8, Speed_IPT_2_current, Power_IPT_2] = IPT_2(T8, P8, w_inter_2(timer), beta_new(8));

% IPT 1
[T10, P10, G9, Speed_IPT_1_current, Power_IPT_1] = IPT_1(T9, P9, w_inter_1(timer), beta_new(9));

% LPT
[T11, P11, G10, Speed_LPT_current, Power_LPT] = LPT(T10, P10, w_low(timer), beta_new(10));

% Core Nozzle          
[T_core, P_core, G_core] = Nozzle(T11, P11, P_amb);

% Bypass Nozzle 
[T_bypass, P_bypass, G_bypass] = Bypass_Nozzle(T1,P1, P_amb);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Error calculations
Error(1,k) = G_bypass-G1;
Error(2,k) = G_core-G2;
Error(3,k) = G_core-G3;
Error(4,k) = G_core-G4;
Error(5,k) = G_core-G5;
Error(6,k) = G_core-G6;
Error(7,k) = G_core-G7;
Error(8,k) = G_core-G8;
Error(9,k) = G_core-G9;
Error(10,k) = G_core-G10;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Error derivative calculations

for perturb = 1:10

% Perturb Apropriate Beta Coordinate and Calculate All Changes

beta_new(perturb) = beta_new(perturb)+d_constant;

% Fan Equations
[T1_p, P1_p, G1_p, Speed_Fan_current_p, Power_Fan_p] = Fan(T0, P0, w_low(timer), beta_new(1));

% LPC 1 equations
[T2_p, P2_p, G2_p, Speed_LPC_1_current_p, Power_LPC_1_p] = LPC_1(T1_p, P1_p, w_inter_1(timer), beta_new(2));

% LPC 2 equations
[T3_p, P3_p, G3_p, Speed_LPC_2_current_p, Power_LPC_2_p] = LPC_2(T2_p, P2_p, w_inter_2(timer), beta_new(3));

% LPC 3 equations
[T4_p, P4_p, G4_p, Speed_LPC_3_current_p, Power_LPC_3_p] = LPC_3(T3_p, P3_p, w_inter_3(timer), beta_new(4));

% HPC equations
[T5_p, P5_p, G5_p, Speed_HPC_current_p, Power_HPC_p] = HPC(T4_p, P4_p, w_high(timer), beta_new(5));


% Burner equations
[T6_p, P6_p] =  Burner(T5_p, P5_p, G5_p, G_fuel(timer));

% HPT
[T7_p, P7_p, G6_p, Speed_HPT_current_p, Power_HPT_p] = HPT(T6_p, P6_p, w_high(timer), beta_new(6));

% IPT 3
[T8_p, P8_p, G7_p, Speed_IPT_3_current_p, Power_IPT_3_p] = IPT_3(T7_p, P7_p, w_inter_3(timer), beta_new(7));

% IPT 2
[T9_p, P9_p, G8_p, Speed_IPT_2_current_p, Power_IPT_2_p] = IPT_2(T8_p, P8_p, w_inter_2(timer), beta_new(8));

% IPT 1
[T10_p, P10_p, G9_p, Speed_IPT_1_current_p, Power_IPT_1_p] = IPT_1(T9_p, P9_p, w_inter_1(timer), beta_new(9));

% LPT
[T11_p, P11_p, G10_p, Speed_LPT_current_p, Power_LPT_p] = LPT(T10_p, P10_p, w_low(timer), beta_new(10));

% Core Nozzle          
[T_core_p, P_core_p, G_core_p] = Nozzle(T11_p, P11_p, P_amb);

% Bypass Nozzle 
[T_bypass_p, P_bypass_p, G_bypass_p] = Bypass_Nozzle(T1_p,P1_p, P_amb);

% Calculate mass flow vector error for each perturbation
Error_change(1,perturb) = G_bypass_p-G1_p;
Error_change(2,perturb) = G_core_p-G2_p;
Error_change(3,perturb) = G_core_p-G3_p;
Error_change(4,perturb) = G_core_p-G4_p;
Error_change(5,perturb) = G_core_p-G5_p;
Error_change(6,perturb) = G_core_p-G6_p;
Error_change(7,perturb) = G_core_p-G7_p;
Error_change(8,perturb) = G_core_p-G8_p;
Error_change(9,perturb) = G_core_p-G9_p;
Error_change(10,perturb) = G_core_p-G10_p;

beta_new(perturb) = beta_new(perturb)-d_constant;   % Reset back to original value
end


for c1 = 1:10
    for c2 = 1:10
     
    % Jacobian calculation        
    J(c1,c2) = (Error_change(c1,c2)-Error(c1,k))/(d_constant);
    
    end
end

% Memorize Jacobian
mem_J(k,1:n_turbo,1:n_turbo) = J;
% Memorize Error
Current_Error = Error(:,k);
% Calculate the new beta variable for Newton Raphson
beta_new = beta_new-J^(-1)*Current_Error;
end

% Store Map Input Data
mem_beta(1:10,timer) = beta_new;
mem_rcrs(1,timer) = Speed_Fan_current;
mem_rcrs(2,timer) = Speed_LPC_1_current;
mem_rcrs(3,timer) = Speed_LPC_2_current;
mem_rcrs(4,timer) = Speed_LPC_3_current;
mem_rcrs(5,timer) = Speed_HPC_current;
mem_rcrs(6,timer) = Speed_HPT_current;
mem_rcrs(7,timer) = Speed_IPT_3_current;
mem_rcrs(8,timer) = Speed_IPT_2_current;
mem_rcrs(9,timer) = Speed_IPT_1_current;
mem_rcrs(10,timer) = Speed_LPT_current;

% Store Temperature Data
T(1,timer) = T_amb;
T(2,timer) = T0;
T(3,timer) = T1;
T(4,timer) = T2;
T(5,timer) = T3;
T(6,timer) = T4;
T(7,timer) = T5;
T(8,timer) = T6;
T(9,timer) = T7;
T(10,timer) = T8;
T(11,timer) = T9;
T(12,timer) = T10;
T(13, timer) = T11;
T(14,timer) = T_core;

% Store Pressure Data
P(1,timer) = P_amb;
P(2,timer) = P0;
P(3,timer) = P1;
P(4,timer) = P2;
P(5,timer) = P3;
P(6,timer) = P4;
P(7,timer) = P5;
P(8,timer) = P6;
P(9,timer) = P7;
P(10,timer) = P8;
P(11,timer) = P9;
P(12,timer) = P10;
P(13, timer) = P11;
P(14,timer) = T_core;

% Store Mass Flow Data
mem_G_bypass(timer) = G1;
mem_G_core(timer) = G2;

% Store Iteration Data
mem_k(timer) = k;

% Store Power Data
mem_power(1:10, timer) = [Power_Fan; Power_LPC_1; Power_LPC_2; Power_LPC_3; Power_HPC; Power_HPT; Power_IPT_3; Power_IPT_2; Power_IPT_1; Power_LPT];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Low Spool Torque
% Fan torque
torque_a_Fan(timer) = Power_Fan/w_low(timer);

% LPT torque
torque_a_LPT(timer) =Power_LPT/w_low(timer);

% Total torque
torque_low(timer) = torque_a_Fan(timer)+ torque_a_LPT(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 1 Torque
% LPC 1 torque
torque_a_LPC_1(timer) =Power_LPC_1/w_inter_1(timer);

% IPT 1 torque
torque_a_IPT_1(timer) =Power_IPT_1/w_inter_1(timer);

% Total torque
torque_inter_1(timer) = torque_a_IPT_1(timer)+torque_a_LPC_1(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 2 Torque
% LPC 2 torque
torque_a_LPC_2(timer) =Power_LPC_2/w_inter_2(timer);

% IPT 2 torque
torque_a_IPT_2(timer) =Power_IPT_2/w_inter_2(timer);

% Total torque
torque_inter_2(timer) = torque_a_IPT_2(timer)+torque_a_LPC_2(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 3 Torque
% LPC 3 torque
torque_a_LPC_3(timer) =Power_LPC_3/w_inter_3(timer);

% IPT 3 torque
torque_a_IPT_3(timer) =Power_IPT_3/w_inter_3(timer);

% Total torque
torque_inter_3(timer) = torque_a_IPT_3(timer)+torque_a_LPC_3(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% High Spool Torque
% HPC torque
torque_a_HPC(timer) =Power_HPC/w_high(timer);

% HPT torque
torque_a_HPT(timer) = Power_HPT/w_high(timer);

% Total torque
torque_high(timer) = torque_a_HPT(timer)+torque_a_HPC(timer);

if timer < n_timesteps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Low Spool Dynamics
w_der_low(timer) = torque_low(timer)/I_low;             % Acceleration
w_low(timer+1) = w_low(timer)+w_der_low(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 1 Dynamics
w_der_inter_1(timer) = torque_inter_1(timer)/I_inter_1; % Acceleration
w_inter_1(timer+1) = w_inter_1(timer)+w_der_inter_1(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 2 Dynamics
w_der_inter_2(timer) = torque_inter_2(timer)/I_inter_2; % Acceleration
w_inter_2(timer+1) = w_inter_2(timer)+w_der_inter_2(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 3 Dynamics
w_der_inter_3(timer) = torque_inter_3(timer)/I_inter_3; % Acceleration
w_inter_3(timer+1) = w_inter_3(timer)+w_der_inter_3(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% High Spool Dynamics
w_der_high(timer) = torque_high(timer)/I_high;          % Acceleration
w_high(timer+1) = w_high(timer)+w_der_high(timer)*(time(timer+1)-time(timer));
end
disp(time(timer));
end

t_DF = toc;

global Speed_design_Fan
global Speed_design_LPC_1 Speed_design_LPC_2 Speed_design_LPC_3
global Speed_design_HPC

% Normalize with design speeds
w_low = w_low/Speed_design_Fan;
w_inter_1=  w_inter_1/Speed_design_LPC_1;
w_inter_2=  w_inter_2/Speed_design_LPC_2;
w_inter_3=  w_inter_3/Speed_design_LPC_3;
w_high = w_high/Speed_design_HPC;
