%% EML4140 Sandvik Oil Cooler: Rectangular vs Pin Fin Performance Analysis
clear; clc; close all;

%Geometric Parameters & Constants
%Core Parameters (Sourced from team brainstorming notes)
L = 5.667 * 0.0254;          %Flow length (inches to meters)
W_core = 48 * 0.0254;        %Core width (inches to meters)
N_tubes = 40;                %Total number of vertical tubes
D_tube_ext = 0.25 * 0.0254;  %Tube external diameter/width (inches to meters)
FPI = 12;                    %Fins Per Inch baseline for plate channels
F_p = 1 / (FPI * 39.37);     %Fin pitch distance (meters)

%Derived Core Dimensions
S_T_core = W_core / N_tubes;  %Transverse tube pitch (meters)
F_h = S_T_core - D_tube_ext;  %Fin height / vertical gap between tubes (meters)

%Pin Fin Configuration Settings
D_pin = 2.0 / 1000;          %Pin cylinder diameter (2 mm to meters)
S_T_pin = 4.0 / 1000;        %Transverse pin pitch (4 mm to meters)
S_L_pin = 4.0 / 1000;        %Longitudinal pin pitch (4 mm to meters)
N_L = L / S_L_pin;           %Number of pin rows deep across the core depth

%Air Properties at Mean Film Temperature (~300 K)
rho = 1.161;                 %Density (kg/m^3)
nu = 1.589e-5;               %Kinematic viscosity (m^2/s)
k_a = 0.0263;                %Thermal conductivity (W/m*K)
Pr = 0.707;                  %Prandtl number

%Velocity Sweep for Project Trade-Off Curves
%Sweep face velocity from 1 to 10 m/s to analyze matching fan curves
u_max = 1:0.5:10; 

%Pre-allocate arrays for calculation arrays
h_rect = zeros(size(u_max));
h_pin = zeros(size(u_max));
DP_rect = zeros(size(u_max));
DP_pin = zeros(size(u_max));

%Engineering Calculations Loop
%Baseline Channel Geometry (Common Rectangular Slot parameters)
D_h = (4 * F_h * F_p) / (2 * (F_h + F_p)); % Hydraulic diameter

for i = 1:length(u_max)
    v = u_max(i);
    
    %CASE A: Continuous Rectangular Plate Fins
    Re_Dh = (v * D_h) / nu;
    
    %Analytical laminar duct solution constant for high aspect ratio slot (b/a ~ 11.4)
    Nu_rect = 6.0; 
    h_rect(i) = (Nu_rect * k_a) / D_h;
    
    %Fully developed laminar friction factor constant (f*Re = 84)
    f_rect = 84 / Re_Dh;
    DP_rect(i) = f_rect * (L / D_h) * (rho * v^2 / 2);
    
    %CASE B: Advanced Pin Fin Array
    Re_D = (v * D_pin) / nu; % Reynolds number based on pin diameter
    
    %Zukauskas Staggered Tube Bank Correlation Constants (S_T/S_L < 2 row)
    %Valid for Re_D range 100 - 1000 matching our design flow profiles
    C = 0.35;
    m = 0.60;
    
    Nu_pin = C * (Re_D^m) * (Pr^0.36);
    h_pin(i) = (Nu_pin * k_a) / D_pin;
    
    %Pressure Drop across deep staggered cylinder arrays 
    f_k = 0.32; % Friction coefficient for staggered layout at this Re
    chi = 1.0;  % Correction factor for symmetric layout
    DP_pin(i) = N_L * chi * f_k * (rho * v^2 / 2);
end

%Command Window Summary Printout (At Design Velocity = 5 m/s)
[~, ref_idx] = min(abs(u_max - 5.0)); % Find index closest to 5 m/s
fprintf('====================================================\n');
fprintf('     EML4140 CORE PERFORMANCE COMPARISON (5 m/s)\n');
fprintf('====================================================\n');
fprintf('Rectangular Fin Convective Coeff (h):   %6.2f W/m^2*K\n', h_rect(ref_idx));
fprintf('Pin Fin Array Convective Coeff (h):     %6.2f W/m^2*K\n', h_pin(ref_idx));
fprintf('Performance Boost Multiplier:          %6.1fx Increase\n', h_pin(ref_idx)/h_rect(ref_idx));
fprintf('----------------------------------------------------\n');
fprintf('Rectangular Fin Air Pressure Drop (DP): %6.2f Pa\n', DP_rect(ref_idx));
fprintf('Pin Fin Array Air Pressure Drop (DP):    %6.2f Pa\n', DP_pin(ref_idx));
fprintf('Pressure Drop Penalty Multiplier:       %6.1fx Increase\n', DP_pin(ref_idx)/DP_rect(ref_idx));
fprintf('====================================================\n');

%Generating Performance Curve Plots for Presentations
figure('Color', [1 1 1]);

%Plot 1:Convective Heat Transfer Coefficients
subplot(2,1,1);
plot(u_max, h_pin, 'g-', 'LineWidth', 2.5); hold on;
plot(u_max, h_rect, 'b-', 'LineWidth', 2.5);
grid on;
title('Air-Side Convective Heat Transfer Coefficient Comparison');
xlabel('Maximum Local Fluid Velocity (m/s)');
ylabel('Convection Coefficient, h (W/m^2\cdotK)');
legend('Advanced Pin Fin Array (Staggered)', 'Baseline Rectangular Plate Fin', 'Location', 'NorthWest');

%Plot 2:Pressure Drop Penalties
subplot(2,1,2);
plot(u_max, DP_pin, 'g-', 'LineWidth', 2.5); hold on;
plot(u_max, DP_rect, 'b-', 'LineWidth', 2.5);
grid on;
title('Air-Side Static Core Pressure Drop Penalty');
xlabel('Maximum Local Fluid Velocity (m/s)');
ylabel('Static Pressure Drop, \DeltaP (Pa)');
legend('Advanced Pin Fin Array (Staggered)', 'Baseline Rectangular Plate Fin', 'Location', 'NorthWest');