% Code to plot simulation results from sscfluids_engine_cooling_system
%% Plot Description:
%
% This plot shows the density of the coolant at different locations in the
% cooling system over time.  The density of the coolant varies throughout
% the network based on the local temperature and pressure.

% Copyright 2015-2018 The MathWorks, Inc.

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

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

plotRho(simlog_sscfluids_engine_cooling_system)

% Create plots from simulation results
function plotRho(simlog)

% Get simulation results
t = simlog.Hose.Hose.pipe_model.rho.series.time;
hose_rho = simlog.Hose.Hose.pipe_model.rho.series.values('kg/m^3');
bypass_rho = simlog.Bypass_Hose.Bypass_Hose.pipe_model.rho.series.values('kg/m^3');
radiator_rho = simlog.Radiator.thermal_liquid_model.rho.series.values('kg/m^3');

% Plot results
plot(t, hose_rho, 'LineWidth', 1)
hold on
plot(t, bypass_rho, 'LineWidth', 1)
plot(t, radiator_rho, 'LineWidth', 1)
hold off
grid on
xlabel('Time (s)')
ylabel('Density (kg/m^3)')
title('Coolant Density')
legend({'Hose', 'Bypass Hose', 'Radiator'}, 'Location', 'Best')

end
