%% EML4140 Sandvik Oil Cooler: Fan Power & Noise Limit Compliance
clear; clc; close all;

%Engineering Inputs & Design Constraints
% Project Constraints
HP_LIMIT = 50.0;            %Maximum allowable fan motor power (HP)
DBA_LIMIT = 115.0;          %Maximum allowable acoustic ceiling (dBA)

%Core Volumetric Airflow Parameters
A_front = 1.827;            %Frontal core face area (m^2)
u_mean = 5.0;               %Reference air design velocity (m/s)
V_dot_air = A_front * u_mean; %Volumetric flow rate (m^3/s) (~19,360 CFM)

%Fan Mechanical Settings
eta_fan = 0.60;             %Conservative industrial fan static efficiency (60%)
%Baseline reference operating point for a clean, unrestricted 55-inch industrial fan:
ref_DBA = 92.0;             
ref_DP = 36.7;              %Using the smooth rectangular plate pressure drop as baseline

%Design Case Data Vectors
% Geometries: 1 = Rectangular, 2 = Louvered, 3 = Pin Fin Array
design_names = {'Baseline Rectangular', 'Advanced Louvered', 'Staggered Pin Fin'};
DP_cases = [36.7, 51.0, 167.2]; % Calculated core static pressure drops (Pa)

%Pre-allocate evaluation variables
fan_power_W = zeros(1,3);
fan_power_HP = zeros(1,3);
noise_dBA = zeros(1,3);

%Compliance Math Execution Loop
for i = 1:3
    %Calculate Mechanical Shaft Power required to overcome core restriction
    %Fluid Power = V_dot * Delta_P
    fan_power_W(i) = (V_dot_air * DP_cases(i)) / eta_fan;
    fan_power_HP(i) = fan_power_W(i) / 745.7; %Watts to Horsepower conversion
    
    %Estimate Aeroacoustic Noise scaling using pressure-drop tracking
    noise_dBA(i) = ref_DBA + 20 * log10(DP_cases(i) / ref_DP);
end

%Command Window Summary & Status Flag Printout
fprintf('========================================================================\n');
fprintf('                EML4140 DESIGN OPTION COMPLIANCE AUDIT                  \n');
fprintf('========================================================================\n');
fprintf('%-22s |  Pressure Drop  |  Fan Power  |  Estimated Noise  \n', 'Fin Geometry Strategy');
fprintf('------------------------------------------------------------------------\n');

for i = 1:3
    fprintf('%-22s |    %6.1f Pa   |  %5.2f HP   |     %5.1f dBA     \n', ...
        design_names{i}, DP_cases(i), fan_power_HP(i), noise_dBA(i));
end
fprintf('========================================================================\n\n');

%Print explicit alert flags for presentation slides
fprintf('--- CRITICAL DESIGN BOUNDARY VERIFICATION ---\n');
for i = 1:3
    fprintf('\n[%s Fin Core Style]:\n', design_names{i});
    
    %Power Limit Verification
    if fan_power_HP(i) <= HP_LIMIT
        fprintf('  -> Motor Power Limit:   PASSED (Margin: +%.2f HP)\n', HP_LIMIT - fan_power_HP(i));
    else
        fprintf('  -> MOTOR POWER LIMIT:   !! FAILED !! (Exceeded by %.2f HP)\n', fan_power_HP(i) - HP_LIMIT);
    end
    
    %Noise Limit Verification
    if noise_dBA(i) <= DBA_LIMIT
        fprintf('  -> Noise Level Limit:   PASSED (Margin: +%.1f dBA)\n', DBA_LIMIT - noise_dBA(i));
    else
        fprintf('  -> ACOUSTIC NOISE LEVEL: !! FAILED !! (Exceeded by %.1f dBA)\n', noise_dBA(i) - DBA_LIMIT);
    end
end
fprintf('========================================================================\n');

%Generating Presentation Comparison Charts
figure('Color', [1 1 1], 'Position', [100, 100, 900, 500]);

%Subplot A: Horsepower Budget Tracking
subplot(1,2,1);
b1 = bar(categorical(design_names), fan_power_HP, 0.5, 'FaceColor', [0.2 0.6 0.8]);
hold on;
yline(HP_LIMIT, 'r--', 'LineWidth', 2.0, 'Label', 'Max Motor Limit (50 HP)');
grid on;
ylabel('Required Fan Brake Horsepower (HP)');
title('Fan Power Consumption vs Motor Budget');
ylim([0 60]);

%Subplot B: Acoustic Budget Tracking
subplot(1,2,2);
b2 = bar(categorical(design_names), noise_dBA, 0.5, 'FaceColor', [0.8 0.5 0.2]);
hold on;
yline(DBA_LIMIT, 'r--', 'LineWidth', 2.0, 'Label', 'OSHA / Sandvik Ceiling (115 dBA)');
grid on;
ylabel('Estimated Sound Pressure Level (dBA)');
title('Aeroacoustic Noise Emissions vs Safe Envelope');
ylim([0 130]);