% Code to plot simulation results from sh_flow_rectifier
%% Plot Description:
%
% The plots below show the flow rate through various components in the
% system.  Though the flow rate from the source (controlled by the 4-Way
% Valve) changes direction, the flow rate in the load (Flow Control Valve)
% remains in the same direction.  The flow rates through the check valves
% are plotted to show how the flow proceeds through the flow rectifier.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_flow_rectifier.Flow_Rate_Source.q.series.time;
simlog_qchkV1 = simlog_sh_flow_rectifier.Check_Valve1.flow_rate.series.values('m^3/s');
simlog_qchkV2 = simlog_sh_flow_rectifier.Check_Valve2.flow_rate.series.values('m^3/s');
simlog_qchkV3 = simlog_sh_flow_rectifier.Check_Valve3.flow_rate.series.values('m^3/s');
simlog_qchkV4 = simlog_sh_flow_rectifier.Check_Valve4.flow_rate.series.values('m^3/s');
simlog_qLoad = simlog_sh_flow_rectifier.Flow_Control_Valve.orif.flow_rate.series.values('m^3/s');
simlog_qSrc = simlog_sh_flow_rectifier.Flow_Sensor_Source.Flow_Rate_Sensor.q.series.values('m^3/s');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_qLoad, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_qSrc, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Flow Rates (Source and Load)')
ylabel('Flow Rate (m^3/s)')
legend({'Source','Load'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_qchkV1, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_qchkV4, 'LineWidth', 1,'LineStyle','--')
plot(simlog_t, simlog_qchkV2, 'LineWidth', 1)
plot(simlog_t, simlog_qchkV3, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Flow Rate through Check Valves')
ylabel('Flow Rate (m^3/s)')
xlabel('Time (s)')
legend({'Check Valve 1','Check Valve 4','Check Valve 2','Check Valve 3'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_qchkV1 simlog_qchkV2 simlog_qchkV3 simlog_qchkV4 simlog_qLoad simlog_qSrc simlog_handles
