% 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 3 spool 
% turbofan
Total_start = tic;


I_low = 20;               % Low Spool Inertia ISA units
I_inter = 2;              % Intermediate Spool Inertia
I_high = 1;               % High Spool Inertia ISA units

fuel_percentage = 10;     % Fuel step percentage increase and decrease
G_fuel_design = 0.3079;   % Design Point Fuel Flow

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

% Allocate Memory
w_low = zeros(1,n_timesteps);
w_inter = zeros(1,n_timesteps);
w_high = zeros(1,n_timesteps);

w_der_low = zeros(n_timesteps);
w_der_inter=  zeros(n_timesteps);
w_der_high = zeros(n_timesteps);

mem_beta =  zeros(6,n_timesteps);
mem_rcrs = zeros(6,n_timesteps);
mem_G_bypass = zeros(n_timesteps);
mem_G_core = zeros(n_timesteps);
mem_k = zeros(n_timesteps);
mem_power = zeros(6,n_timesteps);

torque_a_Fan = zeros(n_timesteps);
torque_a_LPC = zeros(n_timesteps);
torque_a_HPC = zeros(n_timesteps);
torque_a_HPT = zeros(n_timesteps);
torque_a_IPT = zeros(n_timesteps);
torque_a_LPT = zeros(n_timesteps);
torque_low = zeros(n_timesteps);
torque_inter = zeros(n_timesteps);
torque_high = zeros(n_timesteps);

T = zeros(10,n_timesteps);
P = zeros(10,n_timesteps);



w_low(1) = 1500;          % Low Spool Initial Speed (design point)
w_inter(1) = 2000;        % Intermediate Spool Initial Speed (design point)
w_high(1) = 3000;         % High Spool Initial Speed (design point)

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

d_constant = 1e-2;         %  Auxiliary beta coordinate perturbation constant
tolerance = 1e-9;

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

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:6,1)=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), Mass_Fan_Tr, PR_Fan_Tr, Efficiency_Fan_Tr, D_Fan);

% LPC equations
[T2, P2, G2, Speed_LPC_current, Power_LPC] = LPC(T1, P1, w_inter(timer), beta_new(2), Mass_LPC_Tr, PR_LPC_Tr, Efficiency_LPC_Tr, D_LPC);

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

% 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), Mass_HPT_Tr, PR_HPT_Tr, Efficiency_HPT_Tr, D_HPT);

% IPT
[T6, P6, G5, Speed_IPT_current, Power_IPT] = IPT(T5, P5, w_inter(timer), beta_new(5), Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_IPT);

% LPT
[T7, P7, G6, Speed_LPT_current, Power_LPT] = LPT(T6, P6, w_low(timer), beta_new(6), Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_LPT);

% Core Nozzle          
[T_core, P_core, G_core] = Nozzle(T7, P7, P_amb, D_N);

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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 derivative calculations
for perturb = 1:6

% 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), Mass_Fan_Tr, PR_Fan_Tr, Efficiency_Fan_Tr, D_Fan);

% LPC equations
[T2_p, P2_p, G2_p, Speed_LPC_current, Power_LPC_p] = LPC(T1_p, P1_p, w_inter(timer), beta_new(2), Mass_LPC_Tr, PR_LPC_Tr, Efficiency_LPC_Tr, D_LPC);

% 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), Mass_HPC_Tr, PR_HPC_Tr, Efficiency_HPC_Tr, D_HPC);

% 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), Mass_HPT_Tr, PR_HPT_Tr, Efficiency_HPT_Tr, D_HPT);

% IPT
[T6_p, P6_p, G5_p, Speed_IPT_current, Power_IPT_p] = IPT(T5_p, P5_p, w_inter(timer), beta_new(5), Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_IPT);

% LPT
[T7_p, P7_p, G6_p, Speed_LPT_current, Power_LPT_p] = LPT(T6_p, P6_p, w_low(timer), beta_new(6), Mass_LPT_Tr, PR_LPT_Tr, Efficiency_LPT_Tr, D_LPT);

% Core Nozzle          
[T_core_p, P_core_p, G_core_p] = Nozzle(T7_p, P7_p, P_amb, D_N);

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

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

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


for c1 = 1:6
    for c2 = 1:6
     
    % 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:6,timer) = beta_new;            % Auxiliary beta coordinates
mem_rcrs(1,timer) = Speed_Fan_current;     % Relative corrected speeds
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_IPT_current;
mem_rcrs(6,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) = 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) = 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:6, timer) = [Power_Fan; Power_LPC; Power_HPC; Power_HPT; Power_IPT; 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 Torque
% LPC torque
torque_a_LPC(timer) =Power_LPC/w_inter(timer);

% IPT torque
torque_a_IPT(timer) =Power_IPT/w_inter(timer);

% Total torque
torque_inter(timer) = torque_a_IPT(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);

% 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 Dynamics
% Low Spool Dynamics
w_der_inter(timer) = torque_inter(timer)/I_inter;   % Acceleration
w_inter(timer+1) = w_inter(timer)+w_der_inter(timer)*(time(timer+1)-time(timer));

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

disp(time(timer));
end

% Normalize spool speeds at design point 
w_low = w_low/1500;
w_inter=  w_inter/2000;
w_high = w_high/3000;

Total_time = toc(Total_start);




