% Code to plot simulation results from sscfluids_simple_cpu_cooling
%% Plot Description:
%
% The plots below show the volumetric flow rate through and pressure rise
% across the CPU fan.

% Copyright 2018 The MathWorks, Inc.

% Generate simulation results if they don't exist
if(~exist('simlog_sscfluids_simple_cpu_cooling','var'))
    sim('sscfluids_simple_cpu_cooling')
end

% Reuse figure if it exists, else create new figure
if ~exist('h2_sscfluids_simple_cpu_cooling', 'var') || ...
        ~isgraphics(h2_sscfluids_simple_cpu_cooling, 'figure')
    h2_sscfluids_simple_cpu_cooling = figure('Name', 'sscfluids_simple_cpu_cooling');
end
figure(h2_sscfluids_simple_cpu_cooling)
clf(h2_sscfluids_simple_cpu_cooling)

plotFlowRate(simlog_sscfluids_simple_cpu_cooling)

% Create plots from simulation results
function plotFlowRate(simlog)

% Get simulation results
t = simlog.CPU_fan.mdot_A.series.time;
mdot = simlog.CPU_fan.mdot_A.series.values('kg/s');
dp = simlog.CPU_fan.pressure_differential.series.values('Pa');
rho = simlog.Air_Flow_Sensor.Thermodynamic_Properties_Sensor_G.RHO.series.values('kg/m^3');
volumetric_flow = mdot./rho*1000*60;

% Plot results
handles(1) = subplot(2,1,1);
plot(t, volumetric_flow, 'LineWidth', 1)
grid on
title('CPU Fan');
ylabel('Volumetric flow rate (l/min)');

handles(2) = subplot(2,1,2);
plot(t, dp, 'LineWidth', 1)
grid on
xlabel('Time (s)');
ylabel('Pressure rise (Pa)');

linkaxes(handles, 'x')

end
