% 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 12 spool 
% turbofan

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_inter_1 = 1;            % Intermediate Spool 1 Inertia
I_inter_2 = 1;            % Intermediate Spool 2 Inertia
I_inter_3 = 1;            % Intermediate Spool 3 Inertia
I_inter_4 = 1;            % Intermediate Spool 4 Inertia
I_inter_5 = 1;            % Intermediate Spool 5 Inertia
I_inter_6 = 1;            % Intermediate Spool 6 Inertia
I_inter_7 = 1;            % Intermediate Spool 7 Inertia
I_inter_8 = 1;            % Intermediate Spool 8 Inertia
I_inter_9 = 1;            % Intermediate Spool 9 Inertia
I_inter_10 = 1;           % Intermediate Spool 10 Inertia
I_high = 1;               % High Spool Inertia ISA units

w_low(1) = 1500;          % Initial spool speeds
w_inter_1(1) = 2000;
w_inter_2(1) = 2250;
w_inter_3(1) = 2500;
w_inter_4(1) = 2750;
w_inter_5(1) = 3000;
w_inter_6(1) = 3250;
w_inter_7(1) = 3500;
w_inter_8(1) = 3750;
w_inter_9(1) = 4000;
w_inter_10(1) = 4250;
w_high(1) = 4500;

fuel_percentage = 15;   % Fuel step percentage increase decrease
global G_fuel_design    % Use design fuel mass flow

fprintf('Initial Speed Low Spool (Rad/s):             %9.2f \n', w_low(1));
fprintf('Initial Speed Intermediate Spool 1 (Rad/s):  %9.2f \n', w_inter_1(1));
fprintf('Initial Speed Intermediate Spool 2 (Rad/s):  %9.2f \n', w_inter_2(1));
fprintf('Initial Speed Intermediate Spool 3 (Rad/s):  %9.2f \n', w_inter_3(1));
fprintf('Initial Speed Intermediate Spool 4 (Rad/s):  %9.2f \n', w_inter_4(1));
fprintf('Initial Speed Intermediate Spool 5 (Rad/s):  %9.2f \n', w_inter_5(1));
fprintf('Initial Speed Intermediate Spool 6 (Rad/s):  %9.2f \n', w_inter_6(1));
fprintf('Initial Speed Intermediate Spool 7 (Rad/s):  %9.2f \n', w_inter_7(1));
fprintf('Initial Speed Intermediate Spool 8 (Rad/s):  %9.2f \n', w_inter_8(1));
fprintf('Initial Speed Intermediate Spool 9 (Rad/s):  %9.2f \n', w_inter_9(1));
fprintf('Initial Speed Intermediate Spool 10 (Rad/s): %9.2f \n',w_inter_10(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;               % Auxiliary beta coordinate perturbation
tolerance = 1e-9;

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

k = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

k = k+1;        % 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));

% LPC 4 equations
[T5, P5, G5, Speed_LPC_4_current, Power_LPC_4] = LPC_4(T4, P4, w_inter_4(timer), beta_new(5));

% LPC 5 equations
[T6, P6, G6, Speed_LPC_5_current, Power_LPC_5] = LPC_5(T5, P5, w_inter_5(timer), beta_new(6));

% LPC 6 equations
[T7, P7, G7, Speed_LPC_6_current, Power_LPC_6] = LPC_6(T6, P6, w_inter_6(timer), beta_new(7));

% LPC 7 equations
[T8, P8, G8, Speed_LPC_7_current, Power_LPC_7] = LPC_7(T7, P7, w_inter_7(timer), beta_new(8));

% LPC 8 equations
[T9, P9, G9, Speed_LPC_8_current, Power_LPC_8] = LPC_8(T8, P8, w_inter_8(timer), beta_new(9));

% LPC 9 equations
[T10, P10, G10, Speed_LPC_9_current, Power_LPC_9] = LPC_9(T9, P9, w_inter_9(timer), beta_new(10));

% LPC 10 equations
[T11, P11, G11, Speed_LPC_10_current, Power_LPC_10] = LPC_10(T10, P10, w_inter_10(timer), beta_new(11));

% HPC equations
[T12, P12, G12, Speed_HPC_current, Power_HPC] = HPC(T11, P11, w_high(timer), beta_new(12));

% Burner equations
[T13, P13] =  Burner(T12, P12, G2, G_fuel(timer));

% HPT
[T14, P14, G13, Speed_HPT_current, Power_HPT] = HPT(T13, P13, w_high(timer), beta_new(13));

% IPT 10
[T15, P15, G14, Speed_IPT_10_current, Power_IPT_10] = IPT_10(T14, P14, w_inter_10(timer), beta_new(14));

% IPT 9
[T16, P16, G15, Speed_IPT_9_current, Power_IPT_9] = IPT_9(T15, P15, w_inter_9(timer), beta_new(15));

% IPT 8
[T17, P17, G16, Speed_IPT_8_current, Power_IPT_8] = IPT_8(T16, P16, w_inter_8(timer), beta_new(16));

% IPT 7
[T18, P18, G17, Speed_IPT_7_current, Power_IPT_7] = IPT_7(T17, P17, w_inter_7(timer), beta_new(17));

% IPT 6
[T19, P19, G18, Speed_IPT_6_current, Power_IPT_6] = IPT_6(T18, P18, w_inter_6(timer), beta_new(18));

% IPT 5
[T20, P20, G19, Speed_IPT_5_current, Power_IPT_5] = IPT_5(T19, P19, w_inter_5(timer), beta_new(19));

% IPT 4
[T21, P21, G20, Speed_IPT_4_current, Power_IPT_4] = IPT_4(T20, P20, w_inter_4(timer), beta_new(20));

% IPT 3
[T22, P22, G21, Speed_IPT_3_current, Power_IPT_3] = IPT_3(T21, P21, w_inter_3(timer), beta_new(21));

% IPT 2
[T23, P23, G22, Speed_IPT_2_current, Power_IPT_2] = IPT_2(T22, P22, w_inter_2(timer), beta_new(22));

% IPT 1
[T24, P24, G23, Speed_IPT_1_current, Power_IPT_1] = IPT_1(T23, P23, w_inter_1(timer), beta_new(23));

% LPT
[T25, P25, G24, Speed_LPT_current, Power_LPT] = LPT(T24, P24, w_low(timer), beta_new(24));

% Core Nozzle          
[T_core, P_core, G_core] = Nozzle(T25, P25, 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(11,k) = G_core -G11;
Error(12,k) = G_core-G12;
Error(13,k) = G_core-G13;
Error(14,k) = G_core -G14;
Error(15,k) = G_core-G15;
Error(16,k) = G_core-G16;
Error(17,k) = G_core-G17;
Error(18,k) = G_core-G18;
Error(19,k) = G_core-G19;
Error(20,k) = G_core-G20;
Error(21,k) = G_core-G21;
Error(22,k) = G_core-G22;
Error(23,k) = G_core-G23;
Error(24,k) = G_core-G24;

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

for perturb = 1:24

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

% LPC 4 equations
[T5_p, P5_p, G5_p, Speed_LPC_4_current_p, Power_LPC_4_p] = LPC_4(T4_p, P4_p, w_inter_4(timer), beta_new(5));

% LPC 5 equations
[T6_p, P6_p, G6_p, Speed_LPC_5_current_p, Power_LPC_5_p] = LPC_5(T5_p, P5_p, w_inter_5(timer), beta_new(6));

% LPC 6 equations
[T7_p, P7_p, G7_p, Speed_LPC_6_current_p, Power_LPC_6_p] = LPC_6(T6_p, P6_p, w_inter_6(timer), beta_new(7));

% LPC 7 equations
[T8_p, P8_p, G8_p, Speed_LPC_7_current_p, Power_LPC_7_p] = LPC_7(T7_p, P7_p, w_inter_7(timer), beta_new(8));

% LPC 8 equations
[T9_p, P9_p, G9_p, Speed_LPC_8_current_p, Power_LPC_8_p] = LPC_8(T8_p, P8_p, w_inter_8(timer), beta_new(9));

% LPC 9 equations
[T10_p, P10_p, G10_p, Speed_LPC_9_current_p, Power_LPC_9_p] = LPC_9(T9_p, P9_p, w_inter_9(timer), beta_new(10));

% LPC 10 equations
[T11_p, P11_p, G11_p, Speed_LPC_10_current_p, Power_LPC_10_p] = LPC_10(T10_p, P10_p, w_inter_10(timer), beta_new(11));

% HPC equations
[T12_p, P12_p, G12_p, Speed_HPC_current_p, Power_HPC_p] = HPC(T11_p, P11_p, w_high(timer), beta_new(12));

% Burner equations
[T13_p, P13_p] =  Burner(T12_p, P12_p, G2_p, G_fuel(timer));

% HPT
[T14_p, P14_p, G13_p, Speed_HPT_current_p, Power_HPT_p] = HPT(T13_p, P13_p, w_high(timer), beta_new(13));

% IPT 10
[T15_p, P15_p, G14_p, Speed_IPT_10_current_p, Power_IPT_10_p] = IPT_10(T14_p, P14_p, w_inter_10(timer), beta_new(14));

% IPT 9
[T16_p, P16_p, G15_p, Speed_IPT_9_current_p, Power_IPT_9_p] = IPT_9(T15_p, P15_p, w_inter_9(timer), beta_new(15));

% IPT 8
[T17_p, P17_p, G16_p, Speed_IPT_8_current_p, Power_IPT_8_p] = IPT_8(T16_p, P16_p, w_inter_8(timer), beta_new(16));

% IPT 7
[T18_p, P18_p, G17_p, Speed_IPT_7_current_p, Power_IPT_7_p] = IPT_7(T17_p, P17_p, w_inter_7(timer), beta_new(17));

% IPT 6
[T19_p, P19_p, G18_p, Speed_IPT_6_current_p, Power_IPT_6_p] = IPT_6(T18_p, P18_p, w_inter_6(timer), beta_new(18));

% IPT 5
[T20_p, P20_p, G19_p, Speed_IPT_5_current_p, Power_IPT_5_p] = IPT_5(T19_p, P19_p, w_inter_5(timer), beta_new(19));

% IPT 4
[T21_p, P21_p, G20_p, Speed_IPT_4_current_p, Power_IPT_4_p] = IPT_4(T20_p, P20_p, w_inter_4(timer), beta_new(20));

% IPT 3
[T22_p, P22_p, G21_p, Speed_IPT_3_current_p, Power_IPT_3_p] = IPT_3(T21_p, P21_p, w_inter_3(timer), beta_new(21));

% IPT 2
[T23_p, P23_p, G22_p, Speed_IPT_2_current_p, Power_IPT_2_p] = IPT_2(T22_p, P22_p, w_inter_2(timer), beta_new(22));

% IPT 1
[T24_p, P24_p, G23_p, Speed_IPT_1_current_p, Power_IPT_1_p] = IPT_1(T23_p, P23_p, w_inter_1(timer), beta_new(23));

% LPT
[T25_p, P25_p, G24_p, Speed_LPT_current_p, Power_LPT_p] = LPT(T24_p, P24_p, w_low(timer), beta_new(24));

% Core Nozzle          
[T_core_p, P_core_p, G_core_p] = Nozzle(T25_p, P25_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;
Error_change(11,perturb) = G_core_p -G11_p;
Error_change(12,perturb) = G_core_p-G12_p;
Error_change(13,perturb) = G_core_p-G13_p;
Error_change(14,perturb) = G_core_p -G14_p;
Error_change(15,perturb) = G_core_p-G15_p;
Error_change(16,perturb) = G_core_p-G16_p;
Error_change(17,perturb) = G_core_p-G17_p;
Error_change(18,perturb) = G_core_p-G18_p;
Error_change(19,perturb) = G_core_p-G19_p;
Error_change(20,perturb) = G_core_p-G20_p;
Error_change(21,perturb) = G_core_p-G21_p;
Error_change(22,perturb) = G_core_p-G22_p;
Error_change(23,perturb) = G_core_p-G23_p;
Error_change(24,perturb) = G_core_p-G24_p;

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

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

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

end

% Store Map Input Data
mem_beta(1:24,timer) = beta_new;            % Auxiliary Beta Coordinates
mem_rcrs(1,timer) = Speed_Fan_current;      % Relative Corrected Speeds
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_LPC_4_current;
mem_rcrs(6,timer) = Speed_LPC_5_current;
mem_rcrs(7,timer) = Speed_LPC_6_current;
mem_rcrs(8,timer) = Speed_LPC_7_current;
mem_rcrs(9,timer) = Speed_LPC_8_current;
mem_rcrs(10,timer) = Speed_LPC_9_current;
mem_rcrs(11,timer) = Speed_LPC_10_current;
mem_rcrs(12,timer) = Speed_HPC_current;
mem_rcrs(13,timer) = Speed_HPT_current;
mem_rcrs(14,timer) = Speed_IPT_10_current;
mem_rcrs(15,timer) = Speed_IPT_9_current;
mem_rcrs(16,timer) = Speed_IPT_8_current;
mem_rcrs(17,timer) = Speed_IPT_7_current;
mem_rcrs(18,timer) = Speed_IPT_6_current;
mem_rcrs(19,timer) = Speed_IPT_5_current;
mem_rcrs(20,timer) = Speed_IPT_4_current;
mem_rcrs(21,timer) = Speed_IPT_3_current;
mem_rcrs(22,timer) = Speed_IPT_2_current;
mem_rcrs(23,timer) = Speed_IPT_1_current;
mem_rcrs(24,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) = T12;
T(15,timer) = T13;
T(16,timer) = T14;
T(17,timer) = T15;
T(18, timer) = T16;
T(19,timer) = T17;
T(20,timer) = T18;
T(21,timer) = T19;
T(22,timer) = T20;
T(23, timer) = T21;
T(24, timer) = T22;
T(25, timer) = T23;
T(26, timer) = T24;
T(27, timer) = T25;
T(28,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) = P12;
P(15,timer) = P13;
P(16,timer) = P14;
P(17,timer) = P15;
P(18, timer) = P16;
P(19,timer) = P17;
P(20,timer) = P18;
P(21,timer) = P19;
P(22,timer) = P20;
P(23, timer) = P21;
P(24, timer) = P22;
P(25, timer) = P23;
P(26, timer) = P24;
P(27, timer) = P25;
P(28,timer) = P_core;

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

% Store Iteration Data
mem_k(timer) = k;

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 4 Torque
% LPC 4 torque
torque_a_LPC_4(timer) =Power_LPC_4/w_inter_4(timer);

% IPT 4 torque
torque_a_IPT_4(timer) =Power_IPT_4/w_inter_4(timer);

% Total torque
torque_inter_4(timer) = torque_a_IPT_4(timer)+torque_a_LPC_4(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 5 Torque
% LPC 5 torque
torque_a_LPC_5(timer) =Power_LPC_5/w_inter_5(timer);

% IPT 5 torque
torque_a_IPT_5(timer) =Power_IPT_5/w_inter_5(timer);

% Total torque
torque_inter_5(timer) = torque_a_IPT_5(timer)+torque_a_LPC_5(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 6 Torque
% LPC 6 torque
torque_a_LPC_6(timer) =Power_LPC_6/w_inter_6(timer);

% IPT 6 torque
torque_a_IPT_6(timer) =Power_IPT_6/w_inter_6(timer);

% Total torque
torque_inter_6(timer) = torque_a_IPT_6(timer)+torque_a_LPC_6(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 7 Torque
% LPC 7 torque
torque_a_LPC_7(timer) =Power_LPC_7/w_inter_7(timer);

% IPT 7 torque
torque_a_IPT_7(timer) =Power_IPT_7/w_inter_7(timer);

% Total torque
torque_inter_7(timer) = torque_a_IPT_7(timer)+torque_a_LPC_7(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 8 Torque
% LPC 8 torque
torque_a_LPC_8(timer) =Power_LPC_8/w_inter_8(timer);

% IPT 8 torque
torque_a_IPT_8(timer) =Power_IPT_8/w_inter_8(timer);

% Total torque
torque_inter_8(timer) = torque_a_IPT_8(timer)+torque_a_LPC_8(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 9 Torque
% LPC 9 torque
torque_a_LPC_9(timer) =Power_LPC_9/w_inter_9(timer);

% IPT 9 torque
torque_a_IPT_9(timer) =Power_IPT_9/w_inter_9(timer);

% Total torque
torque_inter_9(timer) = torque_a_IPT_9(timer)+torque_a_LPC_9(timer);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 10 Torque
% LPC 10 torque
torque_a_LPC_10(timer) =Power_LPC_10/w_inter_10(timer);

% IPT 10 torque
torque_a_IPT_10(timer) =Power_IPT_10/w_inter_10(timer);

% Total torque
torque_inter_10(timer) = torque_a_IPT_10(timer)+torque_a_LPC_10(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));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 4 Dynamics
w_der_inter_4(timer) = torque_inter_4(timer)/I_inter_4; % Acceleration
w_inter_4(timer+1) = w_inter_4(timer)+w_der_inter_4(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 5 Dynamics
w_der_inter_5(timer) = torque_inter_5(timer)/I_inter_5; % Acceleration
w_inter_5(timer+1) = w_inter_5(timer)+w_der_inter_5(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 6 Dynamics
w_der_inter_6(timer) = torque_inter_6(timer)/I_inter_6; % Acceleration
w_inter_6(timer+1) = w_inter_6(timer)+w_der_inter_6(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 7 Dynamics
w_der_inter_7(timer) = torque_inter_7(timer)/I_inter_7; % Acceleration
w_inter_7(timer+1) = w_inter_7(timer)+w_der_inter_7(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 8 Dynamics
w_der_inter_8(timer) = torque_inter_8(timer)/I_inter_8; % Acceleration
w_inter_8(timer+1) = w_inter_8(timer)+w_der_inter_8(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 9 Dynamics
w_der_inter_9(timer) = torque_inter_9(timer)/I_inter_9; % Acceleration
w_inter_9(timer+1) = w_inter_9(timer)+w_der_inter_9(timer)*(time(timer+1)-time(timer));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intermediate Spool 10 Dynamics
w_der_inter_10(timer) = torque_inter_10(timer)/I_inter_10; % Acceleration
w_inter_10(timer+1) = w_inter_10(timer)+w_der_inter_10(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_LPC_4 Speed_design_LPC_5 Speed_design_LPC_6
global Speed_design_LPC_7 Speed_design_LPC_8 Speed_design_LPC_9
global Speed_design_LPC_10
global Speed_design_HPC

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_inter_4 = w_inter_4/Speed_design_LPC_4;
w_inter_5 = w_inter_5/Speed_design_LPC_5;
w_inter_6 = w_inter_6/Speed_design_LPC_6;
w_inter_7 = w_inter_7/Speed_design_LPC_7;
w_inter_8 = w_inter_8/Speed_design_LPC_8;
w_inter_9 = w_inter_9/Speed_design_LPC_9;
w_inter_10 = w_inter_10/Speed_design_LPC_10;
w_high = w_high/Speed_design_HPC;

Total_time = toc(Start_time);