% Code to plot simulation results from sh_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 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_lubrication_system.Nozzle_Bypass_Valve.orifice_T.flow_rate.series.time;
simlog_pumpq = simlog_sh_lubrication_system.Pump_Unit.Pump.pump_model.pump_delivery.series.values('m^3/s');
simlog_HXBypq = simlog_sh_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_T.flow_rate.series.values('m^3/s');
simlog_HXManq = simlog_sh_lubrication_system.Heat_Exchanger_Bypass_Valve.orifice_P.flow_rate.series.values('m^3/s')*-1;
simlog_NozManq = simlog_sh_lubrication_system.Nozzle_Bypass_Valve.orifice_P.flow_rate.series.values('m^3/s')*-1;
simlog_NozBypq = simlog_sh_lubrication_system.Nozzle_Bypass_Valve.orifice_T.flow_rate.series.values('m^3/s');

% 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
