function [ret,x0,str,ts,xts]=tengine2(t,x,u,flag);
%TENGINE2	is the M-file description of the SIMULINK system named TENGINE2.
%	The block-diagram can be displayed by typing: TENGINE2.
%
%	SYS=TENGINE2(T,X,U,FLAG) returns depending on FLAG certain
%	system values given time point, T, current state vector, X,
%	and input vector, U.
%	FLAG is used to indicate the type of output to be returned in SYS.
%
%	Setting FLAG=1 causes TENGINE2 to return state derivatives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling TENGINE2 with a FLAG of zero:
%	[SIZES]=TENGINE2([],[],[],0),  returns a vector, SIZES, which
%	contains the sizes of the state vector and other parameters.
%		SIZES(1) number of states
%		SIZES(2) number of discrete states
%		SIZES(3) number of outputs
%		SIZES(4) number of inputs
%		SIZES(5) number of roots (currently unsupported)
%		SIZES(6) direct feedthrough flag
%		SIZES(7) number of sample times
%
%	For the definition of other parameters in SIZES, see SFUNC.
%	See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.

% Note: This M-file is only used for saving graphical information;
%       after the model is loaded into memory an internal model
%       representation is used.

% the system will take on the name of this mfile:
sys = mfilename;
new_system(sys)
simver(1.3)
if (0 == (nargin + nargout))
     set_param(sys,'Location',[55,53,808,602])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '1500')
set_param(sys,'Min step size', '0.5')
set_param(sys,'Max step size', '0.5')
set_param(sys,'Relative error','1e-6')
set_param(sys,'Return vars',   '[t,x,y]')
set_param(sys,'AssignWideVectorLines','on');

add_block('built-in/Gain',[sys,'/',['Stoichiometric',13,'Fuel']])
set_param([sys,'/',['Stoichiometric',13,'Fuel']],...
		'Gain','1/14.64',...
		'position',[65,348,135,372])

add_block('built-in/To Workspace',[sys,'/','To Workspace1'])
set_param([sys,'/','To Workspace1'],...
		'orientation',2,...
		'mat-name','speed',...
		'buffer','[3000 0 1]',...
		'position',[40,37,90,53])


%     Subsystem  'int'.

new_system([sys,'/','int'])
set_param([sys,'/','int'],'Location',[180,196,673,403])

add_block('built-in/Inport',[sys,'/','int/in_1'])
set_param([sys,'/','int/in_1'],...
		'position',[50,55,70,75])

add_block('built-in/Inport',[sys,'/',['int/ang speed',13,'in rad//s']])
set_param([sys,'/',['int/ang speed',13,'in rad//s']],...
		'Port','2',...
		'position',[50,105,70,125])

add_block('built-in/Fcn',[sys,'/',['int/conversion factor event',13,'based to continuous time']])
set_param([sys,'/',['int/conversion factor event',13,'based to continuous time']],...
		'Expr','30/u[1]',...
		'position',[195,107,265,123])

add_block('built-in/Product',[sys,'/','int/mullt'])
set_param([sys,'/','int/mullt'],...
		'position',[300,62,315,78])

add_block('built-in/Integrator',[sys,'/','int/integrator'])
set_param([sys,'/','int/integrator'],...
		'Initial','init',...
		'position',[335,60,355,80])

add_block('built-in/Outport',[sys,'/','int/out_1'])
set_param([sys,'/','int/out_1'],...
		'position',[385,60,405,80])

add_block('built-in/Gain',[sys,'/','int/N//rpm'])
set_param([sys,'/','int/N//rpm'],...
		'Gain','60/(2*pi)',...
		'position',[105,94,165,136])
add_line([sys,'/','int'],[320,70;330,70])
add_line([sys,'/','int'],[75,65;295,65])
add_line([sys,'/','int'],[360,70;380,70])
add_line([sys,'/','int'],[270,115;275,115;275,75;295,75])
add_line([sys,'/','int'],[75,115;100,115])
add_line([sys,'/','int'],[170,115;190,115])
set_param([sys,'/','int'],...
		'Mask Display','1/s\nevent\nbased',...
		'Mask Type','Event based time integrator',...
		'Mask Dialogue','Integrate in event based time | Initial integrator state',...
		'Mask Translate','init=@1;')
set_param([sys,'/','int'],...
		'Mask Help','Let t = continuous time, t''= no. of samples then input 2 should be k such that dt = k * dt'', k=60/(2*N) if sampling every 180 deg of cranshaft rotation',...
		'Mask Entries','86\/')


%     Finished composite block 'int'.

set_param([sys,'/','int'],...
		'position',[685,375,725,430])

add_block('built-in/To Workspace',[sys,'/','To Workspace2'])
set_param([sys,'/','To Workspace2'],...
		'mat-name','throttle',...
		'buffer','[3000 0 1]',...
		'position',[355,52,405,68])


%     Subsystem  ['Discrete PID',13,'for realization'].

new_system([sys,'/',['Discrete PID',13,'for realization']])
set_param([sys,'/',['Discrete PID',13,'for realization']],'Location',[165,336,913,587])

add_block('built-in/Sum',[sys,'/',['Discrete PID',13,'for realization/Sum3']])
set_param([sys,'/',['Discrete PID',13,'for realization/Sum3']],...
		'inputs','+-',...
		'position',[125,59,145,101])

add_block('built-in/Inport',[sys,'/',['Discrete PID',13,'for realization/speed']])
set_param([sys,'/',['Discrete PID',13,'for realization/speed']],...
		'Port','2',...
		'position',[55,90,75,110])

add_block('built-in/Inport',[sys,'/',['Discrete PID',13,'for realization/speed reference']])
set_param([sys,'/',['Discrete PID',13,'for realization/speed reference']],...
		'position',[55,50,75,70])

add_block('built-in/Discrete State-Space',[sys,'/',['Discrete PID',13,'for realization/controller']])
set_param([sys,'/',['Discrete PID',13,'for realization/controller']],...
		'A','[1.3679   -0.3679; 1 0]',...
		'B','[1;0]',...
		'C','[-0.0381 0.0385]',...
		'D','0.0671',...
		'position',[200,58,325,102])

add_block('built-in/Outport',[sys,'/',['Discrete PID',13,'for realization/throttle',13,'setting']])
set_param([sys,'/',['Discrete PID',13,'for realization/throttle',13,'setting']],...
		'position',[385,70,405,90])
add_line([sys,'/',['Discrete PID',13,'for realization']],[80,60;95,60;95,70;120,70])
add_line([sys,'/',['Discrete PID',13,'for realization']],[330,80;380,80])
add_line([sys,'/',['Discrete PID',13,'for realization']],[80,100;95,100;95,90;120,90])
add_line([sys,'/',['Discrete PID',13,'for realization']],[150,80;195,80])
set_param([sys,'/',['Discrete PID',13,'for realization']],...
		'Mask Display','Discrete\nPID controller',...
		'Mask Type','Discrete PID for realization')
set_param([sys,'/',['Discrete PID',13,'for realization']],...
		'Mask Help','Discrete PID controller with Integrator Anti-Windup and Output Saturation')


%     Finished composite block ['Discrete PID',13,'for realization'].

set_param([sys,'/',['Discrete PID',13,'for realization']],...
		'position',[170,71,250,149])

add_block('built-in/Step Fcn',[sys,'/',['Speed',13,'demand']])
set_param([sys,'/',['Speed',13,'demand']],...
		'Time','600',...
		'Before','120',...
		'After','130',...
		'position',[65,80,85,100])

add_block('built-in/To Workspace',[sys,'/','To Workspace3'])
set_param([sys,'/','To Workspace3'],...
		'mat-name','mdot',...
		'buffer','3000',...
		'position',[470,282,520,298])

add_block('built-in/Unit Delay',[sys,'/',['induction to',13,'powerstroke',13,'delay']])
set_param([sys,'/',['induction to',13,'powerstroke',13,'delay']],...
		'orientation',2,...
		'x0','0.108',...
		'position',[150,242,180,288])

add_block('built-in/Constant',[sys,'/',['Atmospheric',13,'Pressure, Pa',13,'(bar)']])
set_param([sys,'/',['Atmospheric',13,'Pressure, Pa',13,'(bar)']],...
		'Value','1.0',...
		'position',[325,170,345,190])


%     Subsystem  'Throttle'.

new_system([sys,'/','Throttle'])
set_param([sys,'/','Throttle'],'Location',[270,196,866,434])

add_block('built-in/Constant',[sys,'/','Throttle/Sonic Flow'])
set_param([sys,'/','Throttle/Sonic Flow'],...
		'Value','1.0',...
		'position',[240,160,260,180])

add_block('built-in/Outport',[sys,'/',['Throttle/Ouput Throttle',13,'Flow, mdot (g//s)']])
set_param([sys,'/',['Throttle/Ouput Throttle',13,'Flow, mdot (g//s)']],...
		'position',[475,110,495,130])

add_block('built-in/Inport',[sys,'/',['Throttle/Atmospheric Pressure,',13,'Pa, (bar)']])
set_param([sys,'/',['Throttle/Atmospheric Pressure,',13,'Pa, (bar)']],...
		'Port','3',...
		'position',[70,160,90,180])

add_block('built-in/Fcn',[sys,'/','Throttle/f(theta)'])
set_param([sys,'/','Throttle/f(theta)'],...
		'Expr','2.821-0.05231*u[1]+0.10299*u[1]*u[1]-0.00063*u[1]*u[1]*u[1]',...
		'position',[230,20,270,40])

add_block('built-in/Mux',[sys,'/','Throttle/Mux'])
set_param([sys,'/','Throttle/Mux'],...
		'inputs','2',...
		'position',[170,106,200,139])

add_block('built-in/Fcn',[sys,'/','Throttle/g(Pm)'])
set_param([sys,'/','Throttle/g(Pm)'],...
		'Expr','2/u[2]*sqrt(u[1]*u[2]-u[1]*u[1])',...
		'position',[230,60,270,80])

add_block('built-in/Fcn',[sys,'/','Throttle/Flow Regime'])
set_param([sys,'/','Throttle/Flow Regime'],...
		'Expr','u[1]/u[2]-0.5',...
		'position',[230,115,270,135])

add_block('built-in/Switch',[sys,'/','Throttle/Switch'])
set_param([sys,'/','Throttle/Switch'],...
		'position',[315,109,340,141])

add_block('built-in/Product',[sys,'/','Throttle/Product'])
set_param([sys,'/','Throttle/Product'],...
		'position',[390,110,415,130])

add_block('built-in/Inport',[sys,'/',['Throttle/Throttle Angle,',13,'theta (rad)']])
set_param([sys,'/',['Throttle/Throttle Angle,',13,'theta (rad)']],...
		'position',[70,20,90,40])

add_block('built-in/Inport',[sys,'/',['Throttle/Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Throttle/Manifold Pressure,',13,'Pm (bar)']],...
		'Port','2',...
		'position',[65,105,85,125])
add_line([sys,'/','Throttle'],[95,170;145,170;145,130;165,130])
add_line([sys,'/','Throttle'],[90,115;165,115])
add_line([sys,'/','Throttle'],[95,30;225,30])
add_line([sys,'/','Throttle'],[420,120;470,120])
add_line([sys,'/','Throttle'],[345,125;385,125])
add_line([sys,'/','Throttle'],[275,125;310,125])
add_line([sys,'/','Throttle'],[205,125;225,125])
add_line([sys,'/','Throttle'],[205,125;210,125;210,70;225,70])
add_line([sys,'/','Throttle'],[265,170;295,170;295,135;310,135])
add_line([sys,'/','Throttle'],[275,70;285,70;285,115;310,115])
add_line([sys,'/','Throttle'],[275,30;355,30;355,115;385,115])


%     Finished composite block 'Throttle'.

set_param([sys,'/','Throttle'],...
		'position',[395,94,425,196])

add_block('built-in/Note',[sys,'/',['mdot per',13,'Cylinder (g//s)']])
set_param([sys,'/',['mdot per',13,'Cylinder (g//s)']],...
		'position',[383,228,415,252])

add_block('built-in/Fcn',[sys,'/','Pumping'])
set_param([sys,'/','Pumping'],...
		'orientation',2,...
		'Expr','-.366+.08979*u[1]*u[2]-.0337*u[2]*u[1]*u[1]+.0001*u[1]*u[2]*u[2]',...
		'position',[470,236,530,264])

add_block('built-in/Mux',[sys,'/','Pm, N'])
set_param([sys,'/','Pm, N'],...
		'orientation',2,...
		'inputs','2',...
		'position',[565,230,595,265])

add_block('built-in/Fcn',[sys,'/','Torque'])
set_param([sys,'/','Torque'],...
		'Expr','-181.3+379.36*u[1]+21.91*u[1]/u[2]-(.85*u[1]*u[1])/(u[2]*u[2])+.26*u[3]-.0028*u[3]*u[3]+.027*u[4]-.000107*u[4]*u[4]+.00048*u[4]+u[3]+2.55*u[3]*u[1]-.05*u[3]*u[3]*u[1]',...
		'position',[270,365,320,385])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'inputs','+-',...
		'position',[490,364,510,411])

add_block('built-in/Step Fcn',[sys,'/',['Load Torque',13,'Step1']])
set_param([sys,'/',['Load Torque',13,'Step1']],...
		'Time','0',...
		'After','0',...
		'position',[375,440,395,460])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'position',[445,390,465,410])

add_block('built-in/Step Fcn',[sys,'/',['Load Torque',13,'Step']])
set_param([sys,'/',['Load Torque',13,'Step']],...
		'Time','0',...
		'Before','20',...
		'After','20',...
		'position',[375,385,395,405])

add_block('built-in/Gain',[sys,'/',['Inertia,',13,'J']])
set_param([sys,'/',['Inertia,',13,'J']],...
		'Gain','1/.14',...
		'position',[535,375,580,405])

add_block('built-in/Mux',[sys,'/',['Cylinder Air Mass',13,'Fuel Mass',13,'Spark Advance',13,'Engine Speed']])
set_param([sys,'/',['Cylinder Air Mass',13,'Fuel Mass',13,'Spark Advance',13,'Engine Speed']],...
		'position',[200,324,230,421])

add_block('built-in/Constant',[sys,'/',['Spark Advance',13,'(degrees BTDC)']])
set_param([sys,'/',['Spark Advance',13,'(degrees BTDC)']],...
		'Value','15.0',...
		'position',[45,391,75,409])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample']],'Location',[131,529,616,748])

add_block('built-in/Scope',[sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']],...
		'Vgain','0.150000',...
		'Hgain','0.260000',...
		'Vmax','0.300000',...
		'Hmax','5.200000',...
		'Window',[730,559,1063,811])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']],...
		'position',[400,127,420,153])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge']],...
		'position',[400,70,420,90])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/mdot (g//s)']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/mdot (g//s)']],...
		'position',[45,10,65,30])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/N (rad//s) ']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/N (rad//s) ']],...
		'Port','2',...
		'position',[50,115,70,135])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],'Location',[69,260,562,553])

add_block('built-in/Gain',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/N//rpm']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/N//rpm']],...
		'Gain','60/(2*pi)',...
		'position',[105,94,165,136])

add_block('built-in/Product',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/mullt']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/mullt']],...
		'position',[300,62,315,78])

add_block('built-in/Fcn',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/conversion factor event',13,'based to continuous time']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/conversion factor event',13,'based to continuous time']],...
		'Expr','30/u[1]',...
		'position',[195,107,265,123])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/ang speed',13,'in rad//s']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/ang speed',13,'in rad//s']],...
		'Port','2',...
		'position',[50,105,70,125])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/in_1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/in_1']],...
		'position',[50,55,70,75])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/out_1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/out_1']],...
		'position',[430,70,450,90])

add_block('built-in/Reset Integrator',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/Reset',13,'Integrator']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/Reset',13,'Integrator']],...
		'Initial','init',...
		'position',[375,65,405,95])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/zero']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/zero']],...
		'Value','0',...
		'position',[250,230,270,250])

add_block('built-in/Clock',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/Clock']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/Clock']],...
		'position',[200,183,220,207])

add_block('built-in/Fcn',[sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/TDC hit']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s/TDC hit']],...
		'Expr','(!( abs( u(1) - floor( u(1)+0.5 ) ) > 1e-4 ) )',...
		'position',[245,185,295,205])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[170,115;190,115])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[75,115;100,115])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[270,115;275,115;275,75;295,75])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[410,80;425,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[75,65;295,65])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[320,70;370,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[275,240;340,240;340,90;370,90])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[225,195;240,195])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],[300,195;330,195;330,80;370,80])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],...
		'Mask Display','1/s\nevent based\nwith reset\nat TDC',...
		'Mask Type','Event based time integrator with reset at TDC')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],...
		'Mask Dialogue','Integrate in event based time | Initial integrator state',...
		'Mask Translate','init=@1;')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],...
		'Mask Help','Let t = continuous time, t''= no. of samples then input 2 should be k such that dt = k * dt'', k=60/(2*N) if sampling every 180 deg of cranshaft rotation')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],...
		'Mask Entries','0.108\/')


%     Finished composite block ['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/ang speed',13,'in rad//s']],...
		'position',[170,41,250,119])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[255,80;395,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[70,20;110,20;110,60;165,60])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[75,125;115,125;115,100;165,100])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[255,80;350,80;350,140;395,140])


%     Finished composite block ['Integrate Mass Flow',13,'and Sample'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample']],...
		'orientation',2,...
		'position',[310,234,340,296])

add_block('built-in/To Workspace',[sys,'/','To Workspace4'])
set_param([sys,'/','To Workspace4'],...
		'mat-name','charge',...
		'buffer','3000',...
		'position',[310,327,360,343])

add_block('built-in/Note',[sys,'/','Engine Timing Model Synchronized to Crank Angle'])
set_param([sys,'/','Engine Timing Model Synchronized to Crank Angle'],...
		'Font Name','helvetica',...
		'Font Weight','bold',...
		'Font Angle','oblique',...
		'Font Size',18,...
		'position',[395,5,400,10])


%     Subsystem  'Subsystem'.

new_system([sys,'/','Subsystem'])
set_param([sys,'/','Subsystem'],'Location',[420,446,596,555])
set_param([sys,'/','Subsystem'],...
		'Mask Display','Double-click\nto run simulation')
set_param([sys,'/','Subsystem'],...
		'Mask Dialogue','eval(''[t,x,y]=rk45(''''tengine2'''',1500,[],[1e-6 0.5 0.5]);plot(speed);ylabel(''''Engine Speed (rad/sec)'''');xlabel(''''Crankshaft Revolutions/2, N/2'''');'')')


%     Finished composite block 'Subsystem'.

set_param([sys,'/','Subsystem'],...
		'BackGround',5,...
		'hide name',0,...
		'position',[333,500,440,546])

add_block('built-in/To Workspace',[sys,'/','To Workspace5'])
set_param([sys,'/','To Workspace5'],...
		'mat-name','load',...
		'buffer','[3000 0 1]',...
		'position',[490,437,540,453])


%     Subsystem  ['Intake',13,'Manifold'].

new_system([sys,'/',['Intake',13,'Manifold']])
set_param([sys,'/',['Intake',13,'Manifold']],'Location',[79,449,775,748])

add_block('built-in/Inport',[sys,'/',['Intake',13,'Manifold/speed in rad//s']])
set_param([sys,'/',['Intake',13,'Manifold/speed in rad//s']],...
		'Port','3',...
		'position',[30,135,50,155])


%     Subsystem  ['Intake',13,'Manifold/int'].

new_system([sys,'/',['Intake',13,'Manifold/int']])
set_param([sys,'/',['Intake',13,'Manifold/int']],'Location',[69,260,562,467])

add_block('built-in/Gain',[sys,'/',['Intake',13,'Manifold/int/N//rpm']])
set_param([sys,'/',['Intake',13,'Manifold/int/N//rpm']],...
		'Gain','60/(2*pi)',...
		'position',[105,94,165,136])

add_block('built-in/Outport',[sys,'/',['Intake',13,'Manifold/int/out_1']])
set_param([sys,'/',['Intake',13,'Manifold/int/out_1']],...
		'position',[385,60,405,80])

add_block('built-in/Integrator',[sys,'/',['Intake',13,'Manifold/int/integrator']])
set_param([sys,'/',['Intake',13,'Manifold/int/integrator']],...
		'Initial','init',...
		'position',[335,60,355,80])

add_block('built-in/Product',[sys,'/',['Intake',13,'Manifold/int/mullt']])
set_param([sys,'/',['Intake',13,'Manifold/int/mullt']],...
		'position',[300,62,315,78])

add_block('built-in/Fcn',[sys,'/',['Intake',13,'Manifold/int/conversion factor event',13,'based to continuous time']])
set_param([sys,'/',['Intake',13,'Manifold/int/conversion factor event',13,'based to continuous time']],...
		'Expr','30/u[1]',...
		'position',[195,107,265,123])

add_block('built-in/Inport',[sys,'/',['Intake',13,'Manifold/int/ang speed',13,'in rad//s']])
set_param([sys,'/',['Intake',13,'Manifold/int/ang speed',13,'in rad//s']],...
		'Port','2',...
		'position',[50,105,70,125])

add_block('built-in/Inport',[sys,'/',['Intake',13,'Manifold/int/in_1']])
set_param([sys,'/',['Intake',13,'Manifold/int/in_1']],...
		'position',[50,55,70,75])
add_line([sys,'/',['Intake',13,'Manifold/int']],[170,115;190,115])
add_line([sys,'/',['Intake',13,'Manifold/int']],[75,115;100,115])
add_line([sys,'/',['Intake',13,'Manifold/int']],[270,115;275,115;275,75;295,75])
add_line([sys,'/',['Intake',13,'Manifold/int']],[360,70;380,70])
add_line([sys,'/',['Intake',13,'Manifold/int']],[75,65;295,65])
add_line([sys,'/',['Intake',13,'Manifold/int']],[320,70;330,70])
set_param([sys,'/',['Intake',13,'Manifold/int']],...
		'Mask Display','1/s\nevent\nbased',...
		'Mask Type','Event based time integrator',...
		'Mask Dialogue','Integrate in event based time | Initial integrator state')
set_param([sys,'/',['Intake',13,'Manifold/int']],...
		'Mask Translate','init=@1;')
set_param([sys,'/',['Intake',13,'Manifold/int']],...
		'Mask Help','Let t = continuous time, t''= no. of samples then input 2 should be k such that dt = k * dt'', k=60/(2*N) if sampling every 180 deg of cranshaft rotation')
set_param([sys,'/',['Intake',13,'Manifold/int']],...
		'Mask Entries','0.46\/')


%     Finished composite block ['Intake',13,'Manifold/int'].

set_param([sys,'/',['Intake',13,'Manifold/int']],...
		'position',[410,40,450,95])

add_block('built-in/Outport',[sys,'/',['Intake',13,'Manifold/Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Intake',13,'Manifold/Manifold Pressure,',13,'Pm (bar)']],...
		'position',[535,60,555,80])

add_block('built-in/Gain',[sys,'/',['Intake',13,'Manifold/Ratio of',13,'specific heats, ',13,'gamma']])
set_param([sys,'/',['Intake',13,'Manifold/Ratio of',13,'specific heats, ',13,'gamma']],...
		'Gain','1.4',...
		'position',[115,15,155,35])

add_block('built-in/Gain',[sys,'/',['Intake',13,'Manifold/RT//Vm']])
set_param([sys,'/',['Intake',13,'Manifold/RT//Vm']],...
		'Gain','.41328',...
		'position',[280,36,360,74])

add_block('built-in/Sum',[sys,'/',['Intake',13,'Manifold/Sum']])
set_param([sys,'/',['Intake',13,'Manifold/Sum']],...
		'inputs','+-',...
		'position',[230,38,250,67])

add_block('built-in/Inport',[sys,'/',['Intake',13,'Manifold/mass flow out']])
set_param([sys,'/',['Intake',13,'Manifold/mass flow out']],...
		'Port','2',...
		'position',[30,80,50,100])

add_block('built-in/Inport',[sys,'/',['Intake',13,'Manifold/mass flow in']])
set_param([sys,'/',['Intake',13,'Manifold/mass flow in']],...
		'position',[30,15,50,35])
add_line([sys,'/',['Intake',13,'Manifold']],[55,145;375,145;375,80;405,80])
add_line([sys,'/',['Intake',13,'Manifold']],[255,55;275,55])
add_line([sys,'/',['Intake',13,'Manifold']],[160,25;205,25;205,45;225,45])
add_line([sys,'/',['Intake',13,'Manifold']],[55,25;110,25])
add_line([sys,'/',['Intake',13,'Manifold']],[55,90;215,90;225,60])
add_line([sys,'/',['Intake',13,'Manifold']],[455,70;530,70])
add_line([sys,'/',['Intake',13,'Manifold']],[365,55;405,55])


%     Finished composite block ['Intake',13,'Manifold'].

set_param([sys,'/',['Intake',13,'Manifold']],...
		'position',[565,134,595,186])

add_block('built-in/Note',[sys,'/',['mdot Throttle',13,'(g//s)']])
set_param([sys,'/',['mdot Throttle',13,'(g//s)']],...
		'position',[470,112,475,117])

add_block('built-in/Note',[sys,'/',['Note: Exhaust gas recirculation',13,'(EGR) is not included.']])
set_param([sys,'/',['Note: Exhaust gas recirculation',13,'(EGR) is not included.']],...
		'position',[611,86,616,91])

add_block('built-in/Note',[sys,'/',['Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Manifold Pressure,',13,'Pm (bar)']],...
		'position',[655,122,660,128])

add_block('built-in/Note',[sys,'/',['Based on tengine.m',13,'modified by David Maclay (8//95)',13,'Cambridge Control']])
set_param([sys,'/',['Based on tengine.m',13,'modified by David Maclay (8//95)',13,'Cambridge Control']],...
		'position',[100,180,105,185])
add_line(sys,[90,90;165,90])
add_line(sys,[255,110;280,110;280,60;350,60])
add_line(sys,[350,180;390,180])
add_line(sys,[560,250;535,250])
add_line(sys,[600,160;725,160;725,240;600,240])
add_line(sys,[235,375;265,375])
add_line(sys,[140,360;195,360])
add_line(sys,[80,400;150,400;150,385;195,385])
add_line(sys,[305,265;185,265])
add_line(sys,[600,160;725,160;725,87;355,87;355,145;390,145])
add_line(sys,[585,390;680,390])
add_line(sys,[325,375;485,375])
add_line(sys,[730,405;745,405;745,492;140,492;140,410;195,410])
add_line(sys,[470,400;485,400])
add_line(sys,[145,265;50,265;60,360])
add_line(sys,[515,390;530,390])
add_line(sys,[400,450;430,450;440,405])
add_line(sys,[400,395;440,395])
add_line(sys,[730,405;745,405;745,255;600,255])
add_line(sys,[745,327;410,327;410,280;345,280])
add_line(sys,[430,145;560,145])
add_line(sys,[465,250;345,250])
add_line(sys,[455,250;455,160;560,160])
add_line(sys,[730,405;745,405;745,467;640,467;640,415;680,415])
add_line(sys,[305,265;255,265;255,335;305,335])
add_line(sys,[465,250;455,250;465,290])
add_line(sys,[730,405;745,405;745,45;95,45])
add_line(sys,[730,405;745,405;745,45;115,45;115,130;165,130])
add_line(sys,[50,335;195,335])
add_line(sys,[255,110;390,110])
add_line(sys,[470,400;475,400;485,445])
add_line(sys,[745,218;540,218;540,175;560,175])

drawnow

% Return any arguments.
if (nargin | nargout)
	% Must use feval here to access system in memory
	if (nargin > 3)
		if (flag == 0)
			eval(['[ret,x0,str,ts,xts]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str,ts,xts] = feval(sys);
	end
else
	drawnow % Flash up the model and execute load callback
end
