%% EML4140 Sandvik Oil Cooler: Fin Geometry Convective Coefficient Analysis
clear; clc; close all;

%Input Parameters & Constants
%Core Geometry (Based on 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
F_p = 1 / (FPI * 39.37);     %Fin pitch (meters)
T_f = 0.15 / 1000;           %Fin thickness (0.15 mm to meters)

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

%Louvered Fin Geometry Additions
L_p = 1.5 / 1000;            % Louver pitch (1.5 mm to meters)
alpha = 25;                  % Louver angle (degrees)

%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
cp = 1007;                   %Specific heat (J/kg*K)

%Velocity Sweep for Comparative Performance Curves
% Sweep face air velocity from 1 to 10 m/s to analyze matching fan curves
u_max = 1:0.5:10; 

%Pre-allocate arrays for plotting
h_rect = zeros(size(u_max));
h_louver = zeros(size(u_max));
DP_rect = zeros(size(u_max));
DP_louver = 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: Louvered Fins
    Re_Lp = (v * L_p) / nu; % Reynolds number based on louver pitch
    
    %Chang and Wang Colburn j-factor empirical correlation
    j = 0.2671 * (Re_Lp^-0.19) * ((alpha/90)^0.4) * ((F_p/L_p)^-0.211) * ((F_h/L_p)^0.12);
    h_louver(i) = j * rho * v * cp * (Pr^(-2/3));
    
    %Chang and Wang Fanning friction factor correlation
    f_louver = 0.894 * (Re_Lp^-0.33) * ((alpha/90)^0.83) * ((F_p/L_p)^-0.42) * ((T_f/L_p)^0.17);
    
    %Matrix configuration ratio for air static pressure drop
    At_Ac_ratio = 4 * L / D_h; 
    DP_louver(i) = f_louver * At_Ac_ratio * (rho * v^2 / 2);
end

%%Data Command Window Display (At Reference 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 RESULTS AT DESIGN SPEED (5 m/s)\n');
fprintf('====================================================\n');
fprintf('Rectangular Fin Convective Coeff (h):   %6.2f W/m^2*K\n', h_rect(ref_idx));
fprintf('Louvered Fin Convective Coeff (h):      %6.2f W/m^2*K\n', h_louver(ref_idx));
fprintf('Performance Multiplier Factor:         %6.1fx Increase\n', h_louver(ref_idx)/h_rect(ref_idx));
fprintf('----------------------------------------------------\n');
fprintf('Rectangular Fin Air Pressure Drop (DP): %6.2f Pa\n', DP_rect(ref_idx));
fprintf('Louvered Fin Air Pressure Drop (DP):    %6.2f Pa\n', DP_louver(ref_idx));
fprintf('Pressure Drop Penalty Multiplier:       %6.1fx Increase\n', DP_louver(ref_idx)/DP_rect(ref_idx));
fprintf('====================================================\n');

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

%Plot Convective Coefficients
subplot(2,1,1);
plot(u_max, h_louver, 'r-', '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 Louvered Fin Strategy', 'Baseline Rectangular Plate Fin', 'Location', 'NorthWest');

%Plot Pressure Drops
subplot(2,1,2);
plot(u_max, DP_louver, 'r-', '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 Louvered Fin Strategy', 'Baseline Rectangular Plate Fin', 'Location', 'NorthWest');