% Code to plot simulation results from sscfluids_lubrication_system
%% Plot Description:
%
% The plots below show the flow rates through different parts of the
% lubrication system.  As the control member within the different valves
% are adjusted, flow rates change within the system.

% Copyright 2015-2020 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t          = simlog_sscfluids_lubrication_system.Nozzle_Bypass_Valve.orifice_T.mdot_A.series.time;
simlog_pump_mdot  = simlog_sscfluids_lubrication_system.Pump_Unit.Pump.mdot_A.series.values('kg/s');
simlog_pump_rho   = simlog_sscfluids_lubrication_system.Pump_Unit.Pump.rho_avg.series.values('kg/m^3');
simlog_pumpq      = simlog_pump_mdot./simlog_pump_rho;
simlog_HXByp_mdot = simlog_sscfluids_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_T.mdot_A.series.values('kg/s');
simlog_HXByp_rho  = simlog_sscfluids_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_T.rho_avg.series.values('kg/m^3');
simlog_HXBypq     = simlog_HXByp_mdot./simlog_HXByp_rho;
simlog_HXMan_mdot = simlog_sscfluids_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_P.mdot_A.series.values('kg/s')*-1;
simlog_HXMan_rho  = simlog_sscfluids_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_P.rho_avg.series.values('kg/m^3');
simlog_HXManq     = simlog_HXMan_mdot./simlog_HXMan_rho;
simlog_NozMan_mdot= simlog_sscfluids_lubrication_system.Nozzle_Bypass_Valve.orifice_P.mdot_A.series.values('kg/s')*-1;
simlog_NozMan_rho = simlog_sscfluids_lubrication_system.Nozzle_Bypass_Valve.orifice_P.rho_avg.series.values('kg/m^3');
simlog_NozManq    = simlog_NozMan_mdot./simlog_NozMan_rho;
simlog_NozByp_mdot= simlog_sscfluids_lubrication_system.Nozzle_Bypass_Valve.orifice_T.mdot_A.series.values('kg/s');
simlog_NozByp_rho = simlog_sscfluids_lubrication_system.Nozzle_Bypass_Valve.orifice_T.rho_avg.series.values('kg/m^3');
simlog_NozBypq    = simlog_NozByp_mdot./simlog_NozByp_rho;

% Plot results
simlog_handles(1) = subplot(3, 1, 1);
temp_colororder = get(gca,'ColorOrder');
plot(simlog_t, simlog_pumpq, 'LineWidth', 1)
grid on
title('Pump Flow Rate')
ylabel('Flow Rate (m^3/s)')
legend({'Pump'},'Location','Best');
set(gca,'YLim',[0 3e-4])

simlog_handles(2) = subplot(3, 1, 2);
plot(simlog_t, simlog_HXManq, 'LineWidth', 1,'Color',temp_colororder(2,:))
hold on
plot(simlog_t, simlog_HXBypq, 'LineWidth', 1,'Color',temp_colororder(2,:),'LineStyle','--')
hold off
grid on
title('Heat Exchanger Flow Rates')
legend({'Manifold','Bypass'},'Location','Best');
ylabel('Flow Rate (m^3/s)')
set(gca,'YLim',[0 3e-4])

simlog_handles(3) = subplot(3, 1, 3);
plot(simlog_t, simlog_NozManq, 'LineWidth', 1,'Color',temp_colororder(3,:))
hold on
plot(simlog_t, simlog_NozBypq, 'LineWidth', 1,'Color',temp_colororder(3,:),'LineStyle','--')
hold off
grid on
title('Nozzle Flow Rates')
legend({'Manifold','Bypass'},'Location','Best');
ylabel('Flow Rate (m^3/s)')
xlabel('Time (s)')
set(gca,'YLim',[0 3e-4])
linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_pumpq simlog_HXBypq simlog_HXManq simlog_NozManq simlog_NozBypq temp_colororder simlog_handles
clear simlog_pump_mdot simlog_pump_rho simlog_HXByp_mdot simlog_HXByp_rho simlog_HXMan_mdot simlog_HXMan_rho
clear simlog_NozByp_mdot simlog_NozByp_rho simlog_NozMan_mdot simlog_NozMan_rho