% Code to plot simulation results from sscfluids_engine_cooling_system
%% Plot Description:
%
% These plots show the instantaneous vehicle speed, engine speed and torque 
% input profiles. The vehicle starts from rest accelerating to near
% its maximum speed. Subsequently, the vehicle decelerates till it fully 
% stops.

% 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('h3_sscfluids_engine_cooling_system', 'var') || ...
        ~isgraphics(h3_sscfluids_engine_cooling_system, 'figure')
    h3_sscfluids_engine_cooling_system = figure('Name', 'sscfluids_engine_cooling_system');
end
figure(h3_sscfluids_engine_cooling_system)
clf(h3_sscfluids_engine_cooling_system)

plotSpeed(logsout_sscfluids_engine_cooling_system)

function plotSpeed(logsout)

% Get simulation results
VehSpd = logsout.get('VehSpd');
EngSpd = logsout.get('EngSpd');
EngTrq = logsout.get('EngTrq');

% Plot results
handles(1) = subplot(2, 1, 1);
yyaxis left
plot(EngSpd.Values.Time, EngSpd.Values.Data, 'LineWidth', 1)
ylabel('Speed (RPM)')
grid on
yyaxis right
plot(VehSpd.Values.Time, VehSpd.Values.Data, 'LineWidth', 1)
ylabel('Speed (km/h)')
title('Engine and Vehicle Speed')

handles(2) = subplot(2, 1, 2);
plot(EngTrq.Values.Time, EngTrq.Values.Data, 'LineWidth', 1)
grid on
ylabel('Torque (N*m)')
title('Engine Torque');
xlabel('Time (s)')

linkaxes(handles, 'x')

end