% Code to plot simulation results from sscfluids_cyl_closed_circuit
%% Plot Description:
%
% This plot shows the pressures inside the cylinder chambers and the
% time-varying load on the piston.

% Copyright 2019 The MathWorks, Inc.

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

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

plotPressureLoad(simlog_sscfluids_cyl_closed_circuit)



% Plot cylinder pressures and piston load
function plotPressureLoad(simlog)

% Get simulation results
t = simlog.Cylinder.chamber_A.p_I.series.time;
p_A = simlog.Cylinder.chamber_A.p_I.series.values('MPa');
p_B = simlog.Cylinder.chamber_B.p_I.series.values('MPa');
F_load = simlog.Force_Source.S.series.values('N');

% Plot results
handles(1) = subplot(2, 1, 1);
plot(t, p_A, 'LineWidth', 1)
hold on
plot(t, p_B, 'LineWidth', 1)
hold off
grid on
legend('Side A', 'Side B', 'Location', 'best')
ylabel('Pressure (MPa)')
title('Cylinder Pressures')

handles(2) = subplot(2, 1, 2);
plot(t, F_load, 'LineWidth', 1)
grid on
ylabel('Force (N)')
xlabel('Time (s)')
title('Load Force')

linkaxes(handles, 'x')

end