% 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 spool 
% turbojet 

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

G_fuel_design = 0.4235;   % Design point fuel flow
fuel_percentage = 15;     % Percentage increase and decrease of fuel during simulation

I_low = 5;                % Low Spool Inertia ISA units
I_high = 1;               % High Spool Inertia ISA units

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

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;       % Beta perturbation value for Jacobian calculation
tolerance = 1e-9;        

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

% Diffuser
[T1 P1] = Diffuser(T_amb,P_amb, Mach);

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

% Initiate iteration count 
    k = 1;

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

% Count iterations
k = k+1;


% LPC equations
[T2, P2, G1, Speed_LPC_current] = LPC(T1, P1, w_low(timer), beta_new(1));

% HPC equations
[T3, P3, G2, Speed_HPC_current] = HPC(T2, P2, w_high(timer), beta_new(2));

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

% HPT
[T5, P5, G3, Speed_HPT_current] = HPT(T4, P4, w_high(timer), beta_new(3));

% LPT
[T6, P6, G4, Speed_LPT_current] = LPT(T5, P5, w_low(timer), beta_new(4));

% Nozzle          
[T7, G5] = Nozzle(T6, P6, P_amb);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Error calculations
Error(1,k) = G5-G1;
Error(2,k) = G5-G2;
Error(3,k) = G5-G3;
Error(4,k) = G5-G4;

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

for perturb = 1:4

% Perturb Apropriate Beta Coordinate and Calculate All Changes
beta_new(perturb) = beta_new(perturb)+d_constant;

% LPC equations
[T2_p, P2_p, G1_p, Speed] = LPC(T1, P1, w_low(timer), beta_new(1));

% HPC equations
[T3_p, P3_p, G2_p, Speed] = HPC(T2_p, P2_p, w_high(timer), beta_new(2));

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

% HPT
[T5_p, P5_p, G3_p, Speed] = HPT(T4_p, P4_p, w_high(timer), beta_new(3));

% LPT
[T6_p, P6_p, G4_p, Speed] = LPT(T5_p, P5_p, w_low(timer), beta_new(4));

% Nozzle          
[T7_p, G5_p] = Nozzle(T6_p, P6_p, P_amb);

% Calculate mass flow vector error for each perturbation
Error_change(1,perturb) = G5_p-G1_p;
Error_change(2,perturb) = G5_p-G2_p;
Error_change(3,perturb) = G5_p-G3_p;
Error_change(4,perturb) = G5_p-G4_p;

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

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

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

% Store Map Input Data
mem_beta(1:4,timer) = beta_new;             % Auxiliary beta map coordinate         
mem_rcrs(1,timer) = Speed_LPC_current;      % Relative corrected speed 
mem_rcrs(2,timer) = Speed_HPC_current;      
mem_rcrs(3,timer) = Speed_HPT_current;
mem_rcrs(4,timer) = Speed_LPT_current;

% Store Temperature Data
T(1,timer) = T1;
T(2,timer) = T2;
T(3,timer) = T3;
T(4,timer) = T4;
T(5,timer) = T5;
T(6,timer) = T6;
T(7,timer) = T7;

% Store Pressure Data
P(1,timer) = P1;
P(2,timer) = P2;
P(3,timer) = P3;
P(4,timer) = P4;
P(5,timer) = P5;
P(6,timer) = P6;
P(7,timer) = P_amb;

% Store Mass Flow Data
G(timer) = G1;

% Store Iteration Data
mem_k(timer) = k;

% Low Spool Torque
% LPC
torque_a_LPC(timer) =cp_cold*(T1-T2)*G1/w_low(timer);

% LPT
torque_a_LPT(timer) = cp_hot*(T5-T6)*G1/w_low(timer);

torque_low(timer) = torque_a_LPT(timer)+torque_a_LPC(timer);

% High Spool Torque
% HPC
torque_a_HPC(timer) =cp_cold*(T2-T3)*G1/w_high(timer);

% HPT
torque_a_HPT(timer) = cp_hot*(T4-T5)*G1/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;
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;
w_high(timer+1) = w_high(timer)+w_der_high(timer)*(time(timer+1)-time(timer));
end
disp(time(timer));
end

t_DF = toc;

% Normalize rotational speeds with respect to design point
w_low = w_low/1500;
w_high = w_high/2900;

Total_time = toc(Start_time);