% Code to plot simulation results from sdl_hybrid_parallel
%% Plot Description:
%
% The plot below shows the electrical losses from the motor and battery as
% the vehicle accelerates and decelerates.  

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_hybrid_parallel.Engine.Engine.P.series.time;
simlog_lossMotor = simlog_sdl_hybrid_parallel.Motor.PMSM.Motor_Equations.PS_Term_Losses.I.series.values;
simlog_lossBatt = simlog_sdl_hybrid_parallel.Battery.Calculations.R.O.series.values;
simlog_vVeh = simlog_sdl_hybrid_parallel.Vehicle_Body.Vehicle_Body.V.series.values('km/hr');

temp_colorOrder = get(groot,'defaultAxesColorOrder');

% Plot results
yyaxis left
plot(simlog_t, (simlog_lossBatt+simlog_lossMotor)/1000, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_lossMotor/1000, 'LineWidth', 1)
plot(simlog_t, simlog_lossBatt/1000,'-','LineWidth', 1,'Color',temp_colorOrder(5,:));
ylabel('Electrical Losses (kW)')
yyaxis right
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Speed (km/hr)')
grid on
title('Electrical Losses and Vehicle Speed')
legend({'Total Losses','Motor Losses','Battery Losses','Vehicle Speed'},'Location','NorthEast');
xlabel('Time (s)')

% Remove temporary variables
clear simlog_t temp_colorOrder
clear simlog_lossBatt simlog_lossMotor 
clear simlog_vVeh
