% 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 2.5 spool 
% turbofan, equivalent to a GTF with gear ratio 1
Start_time = tic;

% 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_high = 1;               % High Spool Inertia ISA units

fuel_percentage = 5; %15;      % Fuel step percentage increase and decrease 
G_fuel_design = 0.3025;   % Design point fuel flow 

time_step = 0.25; %0.001;
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 constant
tolerance = 1e-9;

beta_new(1:5,1) = 0.5;          % Initialize Beta          
Error(1:5,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);

w_low(1) = 1500;        % Low Spool Initial Speed (design point)
w_high(1) = 2900;       % High Spool Initial Speed (design point)

fprintf('Initial Speed Low Spool (Rad/s):             %9.2f \n', w_low(1));
fprintf('Initial Speed High Spool 3 (Rad/s):          %9.2f \n', w_high(1));

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:5,1)=0.5;
end

Error(1:5) = 10*tolerance;      % Initialize Error
k = 1;                          % Initialize iteration count

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

k = k+1;        % Count the 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 equations
[T2, P2, G2, Speed_LPC_current, Power_LPC] = LPC(T1, P1, w_low(timer), beta_new(2));

% HPC equations
[T3, P3, G3, Speed_HPC_current, Power_HPC] = HPC(T2, P2, w_high(timer), beta_new(3));

% Burner equations
[T4 P4] =  Burner(T3, P3, G3, G_fuel(timer));

% HPT
[T5, P5, G4, Speed_HPT_current, Power_HPT] = HPT(T4, P4, w_high(timer), beta_new(4));

% LPT
[T6, P6, G5, Speed_LPT_current, Power_LPT] = LPT(T5, P5, w_low(timer), beta_new(5));

% Core Nozzle          
[T_core, P_core, G_core] = Nozzle(T6, P6, 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 derivative calculations

for perturb = 1:5

% 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, Power_Fan_p] = Fan(T0, P0, w_low(timer), beta_new(1));

% LPC equations
[T2_p, P2_p, G2_p, Speed_LPC_current, Power_LPC_p] = LPC(T1_p, P1_p, w_low(timer), beta_new(2));

% HPC equations
[T3_p, P3_p, G3_p, Speed_HPC_current, Power_HPC_p] = HPC(T2_p, P2_p, w_high(timer), beta_new(3));

% Burner equations
[T4_p, P4_p] =  Burner(T3_p, P3_p, G3_p, G_fuel(timer));

% HPT
[T5_p, P5_p, G4_p, Speed_HPT_current, Power_HPT_p] = HPT(T4_p, P4_p, w_high(timer), beta_new(4));

% LPT
[T6_p, P6_p, G5_p, Speed_LPT_current, Power_LPT_p] = LPT(T5_p, P5_p, w_low(timer), beta_new(5));

% Core Nozzle          
[T_core_p, P_core_p, G_core_p] = Nozzle(T6_p, P6_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;

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


for c1 = 1:5
    for c2 = 1:5
    
    % 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 the 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:5,timer) = beta_new;
mem_rcrs(1,timer) = Speed_Fan_current;
mem_rcrs(2,timer) = Speed_LPC_current;
mem_rcrs(3,timer) = Speed_HPC_current;
mem_rcrs(4,timer) = Speed_HPT_current;
mem_rcrs(5,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) = 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) = P_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:5, timer) = [Power_Fan; Power_LPC; Power_HPC; Power_HPT; Power_LPT];

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

% LPC torque
torque_a_LPC(timer) =Power_LPC/w_low(timer);

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

% Resultant torque
torque_low(timer) = torque_a_Fan(timer)+ torque_a_LPT(timer)+torque_a_LPC(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);

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;     % Low spool acceleration
w_low(timer+1) = w_low(timer)+w_der_low(timer)*(time(timer+1)-time(timer));

% High Spool Dynamics
w_der_high(timer) = torque_high(timer)/I_high;  % High spool 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;


w_low =w_low/1500;              % Normalize with design speed
w_high = w_high/2900;           % Normailize with design speed
Total_time = toc(Start_time);



