%% Conventional Vehicle Spark-Ignition Engine Fuel Economy and Emissions
% This example shows how to calculate the city and highway fuel economy and
% the emissions for a conventional vehicle with a 1.5-L spark-ignition (SI)
% engine. To run this example, make sure you have the city (FTP75) and the
% highway (HWFET) drive cycles installed. After you open the conventional
% vehicle reference application, open the <docid:autoblks_ref#bu_3eb0 Drive
% Cycle Source> block and click *Install additional drive cycles*. For more
% information, see <docid:autoblks_ug#bvdvbj_ Install Drive Cycle Data>.  
%
% Copyright 2019 The MathWorks, Inc.
%%
setupconvehMPG;
%% Prepare the Conventional Vehicle Reference Application For Simulation
% Name the Drive Cycle Source block and Visualization subsystem.
model = 'SiCiPtReferenceApplication';
dcs = [model, '/Drive Cycle Source'];
vis_sys = [model, '/Visualization'];
%%
% In the Visualization subsystem, log the emissions signal data.
pt_set_logging([vis_sys, '/Performance Calculations'], 'US MPG', 'Fuel Economy [mpg]', 'both');
pt_set_logging([vis_sys, '/Emission Calculations'], 'TP HC Mass (g/mi)', 'HC [g/mi]', 'both');
pt_set_logging([vis_sys, '/Emission Calculations'], 'TP CO Mass (g/mi)', 'CO [g/mi]', 'both');
pt_set_logging([vis_sys, '/Emission Calculations'], 'TP NOx Mass (g/mi)', 'NOx [g/mi]', 'both');
pt_set_logging([vis_sys, '/Emission Calculations'], 'TP CO2 Mass (g/km)', 'CO2 [g/km]', 'both');
%% Run City Drive Cycle Simulation
% Configure the Drive Cycle Source block to run the city drive
% cycle (FTP75).
set_param(dcs,'cycleVar','FTP75');
%%
% Run a simulation of the city drive cycle. View the results in the
% Performance and FE Scope.
tfinal = get_param(dcs, 'tfinal');
tf = tfinal(1:strfind(tfinal,' '));
simout1 = sim(model,'ReturnWorkspaceOutputs','on', 'StopTime', tf);
open_system('SiCiPtReferenceApplication/Visualization/Performance and FE Scope')
%%
% The results indicate that the fuel economy is approximately 30 mpg at the
% end of the drive cycle. The scope also provides the target velocity,
% engine speed, and brake specific fuel consumption (BSFC).
%% Run Highway Drive Cycle Simulation
% Configure the Drive Cycle Source block to run the highway drive cycle
% (HWFET). Make sure that you have installed the highway drive cycle.
set_param(dcs,'cycleVar','HWFET');
%%
% Run a simulation of the highway drive cycle. View the results in the
% Performance and FE Scope.
tfinal = get_param(dcs, 'tfinal');
tf = tfinal(1:strfind(tfinal,' '));
simout2 = sim(model,'ReturnWorkspaceOutputs','on', 'StopTime', tf);
open_system('SiCiPtReferenceApplication/Visualization/Performance and FE Scope')
%%
% The results indicate that the fuel economy is approximately 34 mpg at the
% end of the drive cycle. The scope also provides the target velocity,
% engine speed, and brake specific fuel consumption (BSFC).
%% Extract Results
% Extract the city and highway fuel economy results for the city and
% highway drive cycles from the logged data.
logsout1 = simout1.get('logsout');
FE_urban = logsout1.get('Fuel Economy [mpg]').Values.Data(end);
logsout2 = simout2.get('logsout');
FE_hwy = logsout2.get('Fuel Economy [mpg]').Values.Data(end);
%%
% Use the city and highway fuel economy results to compute the combined
% sticker mpg.
FE_combined = 0.55*FE_urban + 0.45*FE_hwy;
%%
% Extract the tailpipe emissions from the city drive cycle.
HC = logsout1.get('HC [g/mi]').Values.Data(end);
CO = logsout1.get('CO [g/mi]').Values.Data(end);
NOx = logsout1.get('NOx [g/mi]').Values.Data(end);
CO2 = logsout1.get('CO2 [g/km]').Values.Data(end);
%%
% Display the fuel economy and city drive cycle tailpipe emissions results
% in the command window.
fprintf('\n***********************\n')
fprintf('FUEL ECONOMY\n');
fprintf('   City:     %4.2f mpg\n', FE_urban);
fprintf('   Highway:  %4.2f mpg\n', FE_hwy);
fprintf('   Combined: %4.2f mpg\n', FE_combined);
fprintf('\nTAILPIPE EMISSIONS\n');
fprintf('   HC:   %4.3f [g/mi]\n',HC);
fprintf('   CO:   %4.3f [g/mi]\n',CO);
fprintf('   NOx:  %4.3f [g/mi]\n',NOx);
fprintf('   CO2:  %4.1f [g/km]\n',CO2);
fprintf('   NMOG: %4.3f [g/mi]',HC+NOx);
fprintf('\n***********************\n');