'##############
'Engine Script
'##############

'Simulates basic engine behaviour, fuel cunsumption and cooling circuit

'(c) 23.03.2009 Rüdiger Hülsmann
'(c) 09.08.2009 Marcel Kuhnt

'Script Version: 2.1
'Omsi release: 1.0

'Needs:
'- Antrieb
'- Heizung


'Revision History:
'- Marcel Kuhnt		09.08.2009	Added Engine Failure
'- Marcel Kuhnt		10.08.2009	Added Revision History
'- Rüdiger Hülsmann	12.10.2009	Added possibility of idle speed control
'- Rüdiger Hülsmann	11.05.2010	Added "E-Gas" function (throttle override by stop brake)
'- Rüdiger Hülsmann	13.05.2010	Added engine temperature and cooling system
'- Rüdiger Hülsmann	08.07.2010	Bugfix E-Gas
'- Rüdiger Hülsmann	06.11.2010	Improved engine start and stop procedure 
'- Marcel Kuhnt		09.11.2010	refueling included
'- Rüdiger Hülsmann	21.11.2010	thermostat short circuit
'- Rüdiger Hülsmann	22.11.2010	engine coolable by fan heaters
'- Rüdiger Hülsmann	24.12.2010	optional ASR (traction control)
'- Rüdiger Hülsmann	29.12.2010	ASR debugged
'- Rüdiger Hülsmann	01.01.2011	ASR debugged again
'- Rüdiger Hülsmann	05.01.2011	20h-switch E-Gas
'- Marcel Kuhnt		07.01.2011	writing tank_percent variable
'- Rüdiger Hülsmann	10.01.2011	Internal throttle set to 0 during shutdown
'- Rüdiger Hülsmann	23.01.2011	ASR relais sound
'- Kyle Ortiz	        05.07.2011	Added turbochager
'- Kyle Ortiz	        30.07.2011	Added Speed Governer with RPM governer while the bus is in drive
'- Kyle Ortiz	        16.08.2011	Changed engine braking algorithm. 
'- Kyle Ortiz	        16.12.2011	Added nemeza's fuel code.
'- Kyle Ortiz	        20.8.2012-5.10.2012	Changed turbochager Characteristic
'- Kyle Ortiz	        11.4.2013	Changed the way the tempature is calculated. Takes current throttle into account versus torque for power calculate.  For example if the engine needs 25.54 KW to idle, 25.54 KW of power would be used and calculate for fuel consumpiton
'- Kyle Ortiz	        11.9.2013	Correction of engine output to compenstate for throttle governor and parasitic loss
'- Kyle Ortiz	        11.9.2013	Added throttle smoke regulator 
'- Kyle Ortiz	        21.9.2013	Added throttle smoke regulator 


'------------------------------------------------------------------------------------------


'----------------------
'	Trigger
'----------------------

{trigger:kw_m_engine_startbutton}
	(L.L.engine_on) ! (L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) && &&
	{if}
		1 (S.L.engine_on) (S.L.engine_injection_on)
		(T.L.ev_enginestart)
		1 (S.L.engine_idle_control)
	{endif}	
	1 (S.L.cp_taster_anlasser)
{end}

{trigger:kw_m_engine_startbutton_off}
	0 (S.L.cp_taster_anlasser)
{end}

{trigger:kw_m_enginestart}
	(L.L.engine_starter_antirepeat) !
	{if}
		(L.L.engine_on) !
		{if}	
			(L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) && 
			{if}
				1 (S.L.engine_on) (S.L.engine_injection_on)
				(T.L.ev_enginestart)
				1  (S.L.engine_idle_control)
			{endif}	
			1 (S.L.cp_taster_anlasser) (S.L.engine_starter_antirepeat)
		{else}
			(L.L.elec_busbar_main)
			{if}
				0 (S.L.engine_injection_on) (S.L.engine_idle_control)
				1 (S.L.engine_starter_antirepeat)

			{endif}
			1 (S.L.cp_taster_motorabstellung)
		{endif}
	{endif}

{end}

{trigger:kw_m_enginestart_off}
	0 (S.L.cp_taster_motorabstellung) (S.L.cp_taster_anlasser) (S.L.engine_starter_antirepeat)
	1 (S.L.engine_injection_on)
{end}


{trigger:kw_m_engineshutdown}
	(L.L.elec_busbar_main)
	{if}
		0 (S.L.engine_injection_on)
	{endif}



	1 (S.L.cp_taster_motorabstellung)
{end}

{trigger:kw_m_engineshutdown_off}
	0 (S.L.cp_taster_motorabstellung)
	1 (S.L.engine_injection_on)
{end}

{trigger:veh_tank}
	(L.L.engine_tank_content) (L.S.Timegap) 3 * + (C.L.engine_tank_capacity) min (S.L.engine_tank_content)
{end}

{macro:engine_init}

	50 50 (C.L.engine_tank_capacity) - random + (S.L.engine_tank_content)
	(L.S.Weather_Temperature) (S.L.engine_temperature) (S.L.engine_temperature_envir)
	0  (S.L.Torque_Governor_set)
{end}


{macro:engine_frame}

	(M.L.speed_governor)

	(L.L.engine_last_n) 300 >=
	(L.L.engine_n) 300 < &&
	{if}
		(M.L.engine_stall)
		0 (S.L.engine_on)
	{endif}
	
	
	(L.L.engine_n) (S.L.engine_last_n)

	
	(L.L.engine_n) 0 < (L.L.engine_injection_on) ! &&
	{if}
		0 (S.L.engine_n) 
	{endif}

	(L.L.bremse_kompressor)	(L.L.engine_on) &&
	{if}	
		10 (S.L.engine_M_additional_load)
	{else}
		0 (S.L.engine_M_additional_load)
	{endif}

	(M.L.engine_temperature)

	(M.L.engine_consumption)

	(M.L.turbochanger)

	    



 
	    (L.L.Torque_Governor_set)
	     {if}

		(L.L.engine_M) (L.L.Torque_Governor_set) / 0 max 1 min (S.L.engine_output)
   
	     {else}
		(L.L.engine_M) (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.engine_altload) - / 0 max 1 min (S.L.engine_output)

	      {endif}
			    
 (L.L.antrieb_getr_M_an) (L.L.engine_resultant_M) (L.L.engine_altload) abs - / 0 max 1 min (S.L.engine_load)
	
	 
	
	(L.L.antrieb_Torque_Governor_ADJ) (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.engine_internal_throttle)  * / 1 max s1


	(L.L.engine_idle_control)
	 {if}

		 (L.L.engine_n) (C.L.engine_governed_idle_RPM) - (F.L.idle_ctr)  (S.L.engine_additional_throttle)
	{else}
		(L.L.engine_additional_throttle) (L.S.Timegap) 0.05 * - 0 max (S.L.engine_additional_throttle)
	{endif}
	 		



'calculating engine brake theorim 




'(L.L.throttle) 1050 * (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.engine_n) (F.L.engine_M_minThrottle) abs 1 (L.L.throttle) - * - (L.L.engine_altload) abs - / 0 max 1 min  s2
	
	(L.L.antrieb_getr_aktugang) (C.L.throttle_type) 2 = && 
	{if}		      
		(L.L.throttle) 0.05  > 
		{if}
			(L.L.engine_n) (F.L.engine_M_minThrottle) abs (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.throttle) * + (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.engine_n) (F.L.engine_M_minThrottle) abs + / s6
		{else}
			0 s6
		{endif}

{else}

(L.L.throttle) s6

{endif}
	
			

	(L.L.antrieb_getr_aktugang) (C.L.throttle_type) 1 = ||
	{if}
		(C.L.rpmgov) s7
	 {else}
	(L.L.throttle) s1 (C.L.rpmgov) * 1 l1 - (C.L.engine_governed_idle_RPM) * + s7
	{endif}

	  l6 (L.L.throttle_active) *  (L.L.engine_n) l7 - (F.L.rpmgov_map) * (L.L.engine_additional_throttle) + 0 max 1 min (S.L.engine_internal_throttle)


'	Override durch Haltestellenbremse ODER Motorabstellung

	(L.L.bremse_halte_sw) (L.L.door_20h_sw) || (C.L.engine_e-gas) &&
	(L.L.engine_injection_on) ! || (L.L.antrieb_neutral_requested) ||
	{if}
		0 (S.L.throttle_active)
		{else}
		1 (S.L.throttle_active)
	{endif}


		(M.L.engine_speedcontrol)


'MCQ: calculating tank content percent and write into pre defined variable for publishing on red line:


'	Motormanagement durch ASR-Eingriff, sofern vorhanden

	(L.L.elec_busbar_main) &&
	{if}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 1 / 10 + abs >
		(L.L.Wheel_RotationSpeed_1_L) 1 > &&
		(L.L.Wheel_RotationSpeed_1_R) 1 > &&
		(L.L.engine_internal_throttle) 0 > &&
		(L.L.antrieb_getr_aktugang) &&
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{endif}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 2 / 8 + abs <
		{if}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}

		(L.L.engine_ASR_eingriff)
		{if}
			0 (S.L.engine_internal_throttle)
		{endif}

	{else}
'		Wenn ASR aus, dann Dauerleuchten
		(L.L.elec_busbar_main) 
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{else}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}
	{endif}


	
{end}


{macro:engine_moment}

	(L.L.Velocity) (C.L.speedgov) - (F.L.governor) (S.L.engine_spdgov)

	(L.L.antrieb_getr_aktugang) (L.L.antrieb_getr_fest) &&
	{if}
		(L.L.engine_n) (C.L.rpmgov) - (F.L.rpmgov_map) (S.L.RPM_Governor)
	{else}		      
		(L.L.engine_n) (C.L.rpmgov_unload) - (F.L.rpmgov_map) (S.L.RPM_Governor)
	{endif}

	(L.L.engine_internal_throttle) (L.L.engine_spdgov) (L.L.RPM_Governor) * min (S.L.engine_internal_throttle)

	 (L.L.Torque_Governor_set) 0 =

	 {if}
	(L.L.engine_n) (F.L.engine_M_maxThrottle) s5
	 {else}
	   (L.L.Torque_Governor_set) s5
	 {endif}

	    (L.L.Torque_Governor_set)
	     {if}

		   (L.L.Torque_Governor_set) (S.L.Torque_Governor_ADJ)
   
	     {else}
			(L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.Torque_Governor_ADJ) >
		     {if}

		   (L.L.engine_n) (F.L.engine_M_maxThrottle) (S.L.Torque_Governor_ADJ)

		    {endif}

	      {endif}
			      
'Calculate transmission settings 
(L.L.Torque_Governor_ADJ) (L.L.engine_temperature) (F.L.torquelimit) min (S.L.Torque_Governor_ADJ)


 	(L.L.Torque_Governor_ADJ) (L.L.Torque_Governor_Perm) >
	{if}
		(L.L.Torque_Governor_Perm) (L.S.Timegap) 800 * + (L.L.Torque_Governor_ADJ)  min (S.L.Torque_Governor_Perm)
	{else}
		(L.L.Torque_Governor_Perm) (L.S.Timegap) 800 * - (L.L.Torque_Governor_ADJ) max (S.L.Torque_Governor_Perm)
	{endif}

'calculating engine brake theorim 

(L.L.engine_throttle_injection) s1 (L.L.engine_n) s0 (F.L.engine_M_maxThrottle) * 1 l1 - l0 (F.L.engine_M_minThrottle) * + s1

(L.L.engine_throttle_injection) (L.L.engine_n) (F.L.engine_M_maxThrottle) * l1 - s1




	(L.L.Torque_Governor_Perm)   (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.engine_altload) l1 + -  / 0 max 1 min  (S.L.Torque_Governor)


     (L.L.turbo_psi) 5 <
     {if}
       0.4 s5

       {else}

       3 s5


       {endif}
	



	(C.L.turbochaged) 3 =  (L.L.turbo_psi) 15 < && 
	{if}
		0.5 s0
		{else}
		10 s0
	{endif}



'	Reaktionszeit der Einspritzpumpe
	(L.L.engine_internal_throttle) (L.L.engine_throttle_injection) >
	{if}
		(L.L.engine_throttle_injection) (L.S.Timegap) l0 * + (L.L.engine_internal_throttle)  min (S.L.engine_throttle_injection)
	{else}
		(L.L.engine_throttle_injection) (L.S.Timegap) 20 * - (L.L.engine_internal_throttle) max (S.L.engine_throttle_injection)
	{endif}



		(C.L.turbochaged) 2 =  (L.L.turbo_psi) 15 < && (L.L.engine_n) 1300 < && 
	{if}
		(C.L.smoke_limiter) s0    
		{else}
		1 s0
	{endif}
 	
	(L.L.engine_throttle_injection) (L.L.Torque_Governor) l0 * min (S.L.engine_throttle_injection)	
		
	
' Throttle Controller
	  	       
	(L.L.engine_injection_on) (L.L.engine_on) &&
	{if}

	(C.L.turbochaged) 
	{if}

'2.52 donates exhaust flow tempature - calculates exhaust flow


			(C.L.VGT_turbochager) 
		{if}	
			 (C.L.engine_CID) (L.L.engine_n) * (C.L.engine_volumetric_efficiency) * 5660 / 14.7 (L.L.turbo_psi) + 14.7 / *  (S.L.engine_airflow)
		{else}
				 (C.L.engine_CID) (L.L.engine_n) * (C.L.engine_volumetric_efficiency) * 5660 / 14.7 (L.L.turbo_psi) + 14.7 / * (L.L.engine_throttle_injection) *  (S.L.engine_airflow)
		{endif}


  	 (C.L.engine_CID) (L.L.engine_n) * (C.L.engine_volumetric_efficiency) * 5660 / 14.7 (C.L.turbo_max_psi)  + 14.7 / *   s2
	

		(L.L.engine_airflow) (L.L.engine_airflow_eni) > 
		{if}	
			
			(L.L.engine_airflow_eni) (L.S.Timegap) 1000 * +  (S.L.engine_airflow_eni)

		{endif}

		(L.L.engine_airflow) (L.L.engine_airflow_eni) <
		{if}	
			(L.L.engine_airflow_eni) (L.S.Timegap) 500 * -  (S.L.engine_airflow_eni)

		{endif}
							       
	 						       

		(L.L.engine_resultant_M) (L.L.engine_n) * pi * 30000 / s1

		(L.L.engine_airflow_eni) (F.L.turbo_exhaust_flow) (C.L.turbo_max_psi)  (L.L.engine_throttle_injection)  * min (S.L.turbo_limit_thorttle) 

'	Regulates the amount of turbocharger boost based on throttle position 

		(C.L.VGT_turbochager) 
		{if}	
			2 s3 
		{else}
			1 s3 
		{endif}

'		 (C.L.turbo_max_psi)  (C.L.turbo_compressor_wheel_size) sqr pi 0.5 * * 30 100 * / / l3 * 1 40 (L.L.engine_n) sqrt / / sqr * s1   
		 
	(C.L.turbo_max_psi) (C.L.turbo_compressor_wheel_size) sqr (L.L.turbo_psi) 14.7 + (C.L.engine_volumetric_efficiency) * / /  s1  
		 
		(C.L.turbo_max_psi)    (C.L.turbo_compressor_wheel_size) sqr 14.7 (L.L.turbo_psi) 1 max + 100 * / / 2 * s2 

	      	
		(L.L.throttle) 0.1 > (L.L.antrieb_getr_aktugang) &&  (L.L.throttle_active) &&
		{if}	
		
			(L.L.turbo_limit_thorttle) (L.L.turbo_psi) > 
			{if}	
				
				(L.L.turbo_psi) (L.S.Timegap) l1 (L.L.engine_throttle_injection) (L.L.turbo_psi) 2 / 1 min 0.5 max * * *  +  (C.L.turbo_max_psi)  (L.L.engine_throttle_injection) 0.995 (L.L.engine_n)  (F.L.turbo_boost_threshold) * * * min (S.L.turbo_psi)

			{endif}

			(L.L.turbo_limit_thorttle) (L.L.turbo_psi) <
			{if}	
				(L.L.turbo_psi) (L.S.Timegap) l2 * - (C.L.turbo_min_psi)  max (S.L.turbo_psi)

			{endif}

		{else}

			(L.L.turbo_psi) (L.S.Timegap) l2 * - (C.L.turbo_min_psi)  max (S.L.turbo_psi)

		{endif}

'Units are in English (PSI) not Metric 

'		Units are in Imperial (PSI) not Metric (Sorry)  Temp(in F) = (((Tin (in F) + 460) * (Pressure Ratio0.283)) - 460)

'		 Calculate Pressure Ratio 

		 (L.L.turbo_psi) 14.7 + 14.7 / s1
		
'	T IC drop = (T IC in - T ambient) * IC efficiency


' Temp = (85 + 460) * 2.020^.283 - 460 = 205F
'						^.283
	(L.S.Weather_Temperature) 1.8 * 32 + 460 + 1.22 * 460 - s1
	l1 (L.S.Weather_Temperature) - 0.7 / s1

' T IC drop = (T IC in - T ambient) * IC efficiency

l1 0.65 * s2

l1 l2 - s2

' Density ratio= ((Tempin + 460) / (Tempout + 460)) * (Pressureout / Pressurein)

		(L.S.Weather_Temperature) 460 + l2 460 + / (L.L.turbo_psi) 2 max 14.7 + 14.7 / * (S.L.engine_turbo_density) 

		(L.L.engine_turbo_density) (C.L.turbo_max_psi)  0.0689475729 * / 1 min (S.L.engine_turbo_PR) 


		 (L.L.engine_turbo_density)  (L.L.engine_turbo_PR) / s1

		     (L.L.turbo_psi) (C.L.turbo_max_psi)  / 0 max 1 min (S.L.turbo_debug_1)

		
		  
	

		    (L.L.engine_n) (F.L.engine_M_maxThrottle) 14.7 (C.L.turbo_max_psi)  14.7 + /  *  (S.L.enginena_M)


		    1 14.7 (C.L.turbo_max_psi)  14.7 + / - (S.L.turbo_M)
		   (L.L.turbo_debug_1) (L.L.engine_n) (F.L.engine_M_maxThrottle) (L.L.turbo_M) * *  (L.L.engine_turbo_PR) * (S.L.engineNA1_M)
		   (L.L.enginena_M) 1 (L.L.engine_turbo_PR) / * (S.L.engine_resultant_M)  
		{else}
			(C.L.turbo_max_psi)  (C.L.turbo_compressor_wheel_size) sqr (L.L.turbo_psi) 100 * / / s1 
			(L.L.turbo_psi) (L.S.Timegap) l1 * - (C.L.turbo_min_psi)  max (S.L.turbo_psi)

			
			0 (S.L.engineNA1_M) 
			1 (S.L.turbo_M)
			(L.L.engine_n) (F.L.engine_M_maxThrottle) (S.L.engine_resultant_M)
		{endif}
	
		
		 	 (L.L.engine_luefter) 
			 {if}


' calculate how much engine power is loss due to cooling system -  for those nitpickers it is an self created formula yet produces an realistic product.

			 2400 0.06 * 0.001 * s6

' 0.0028 yields a 35 KW fan at 2400 RPM
											  
			 9548 (L.L.engine_n) 0.00245 * d * * (L.L.engine_n) /  s7   

			 {else}
			   0 s7
			 {endif}

' Average alterator load of 2.3 KW for lights etc. 

				  (L.L.elec_busbar_main) (L.L.engine_n) 492 > &&
				   {if}
						9548 2.3 * (L.L.engine_n) /  s8
				    {else}

						0 s5
				  {endif}


' power steering loss

			9548 (L.L.engine_n) 0.00245 * * (L.L.engine_n) /  s8
			

			      l7 l5 + l8 + (S.L.engine_altload)


			 (L.L.engine_throttle_injection)  0.1 >
			  {if}
			      
				(L.L.engineNA1_M) s6
			       {else}
				  0 s6
				
			   {endif}

		


		
			(C.L.Exhaust_brake_enabled) 1 = (L.L.cp_retarder_sw) 0 > && (L.L.velocity) 5 > && (L.L.throttle) 0.1 < && (L.L.clutch) 0.05 < && (L.L.engine_n) 900 > &&
		{if}
			
			(L.L.engine_throttle_injection) s1 (L.L.engine_resultant_M) * 1 l1 - (L.L.engine_n) (F.L.engine_M_minThrottle) * + l6 + (L.L.engine_n) (F.L.Exhaust_brake) + (L.L.engine_altload) /-/ + (S.L.engine_M)
			1 (S.L.Exhaust_brake_enabled)
		{else}

	(L.L.engine_throttle_injection) s1 (L.L.engine_resultant_M) * 1 l1 - (L.L.engine_n) (F.L.engine_M_minThrottle) * + l6 + (L.L.engine_altload) /-/ + (S.L.engine_M)
			0 (S.L.Exhaust_brake_enabled)
		{endif}
	
	{else}
		(L.L.engine_n) 1 >
		{if}
			(C.L.engine_coldM) (S.L.engine_M)
		{else}
			0 (S.L.engine_M)
			0 (S.L.turbo_psi)
		{endif}
	{endif}

{end}


{macro:turbochanger}	


	

	
		
{end}


{macro:engine_acceleration}


'	Berechnung der Drehzahlbeschleunigung:
' 	Calculation of speed acceleration:

	(L.L.engine_M) (L.L.antrieb_getr_M_an) (L.L.engine_M_additional_load) + - (C.L.engine_J) /

'	... Berechnung der Motordrehzahl anhand der Drehzahlbeschleunigung:

	(L.S.Timegap) * 30 * pi / (L.L.engine_n) + (S.L.engine_n)
{end}





{macro:engine_consumption}
'					      	=2*PI/60


	(L.L.engine_throttle_injection) (L.L.engine_resultant_M) (L.L.engine_altload) - * (L.L.engine_throttle_injection) (L.L.engineNA1_M) * + (L.L.engine_altload) + (L.L.engine_n) * 0.10472 * s0
	(L.L.engine_on)
	{if}
		l0 (S.L.engine_power)
	{else}
		l0 (S.L.engine_power)
	{endif}
	
	(L.L.engine_energytransfer) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / + (S.L.engine_energytransfer)
	(L.L.engine_energy_used) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / (L.L.engine_n) (F.L.engine_efficiency_rpm) (L.L.engine_throttle_injection) (F.L.engine_efficiency_throttle) / / s0 + (S.L.engine_energy_used)

	(C.L.engine_consumption_curve_available)
	{if}
		(L.L.engine_n) (F.L.engine_consumption_per_rpm) 1000 / s2
		1 l2 / 0.845 * s2
		(L.L.engine_fuel_used) l0 l2 / s1 + (S.L.engine_fuel_used)
	{else}
		(L.L.engine_fuel_used) l0 (C.L.engine_fuel_value) / s1 + (S.L.engine_fuel_used)
	{endif}

		(L.L.engine_tank_content) (C.L.engine_tank_capacity) / (S.L.tank_percent)
	
	l1 (L.S.Timegap) / 3600 * (S.L.engine_consumption_h)

	(L.L.engine_tank_content) l1 - 0 max (S.L.engine_tank_content)
	0 <= 
	(L.L.engine_failure_general) ||
	{if}
		0 (S.L.engine_on)
	{endif}



	
{end}

{macro:engine_stall}
	
	(T.L.ev_engineshutdown)
	
	0 (S.L.engine_on) 
{end}

{macro:engine_speedcontrol}


{end}
{macro:speed_governor}



 
{end}


{macro:engine_temperature}


	(L.L.engine_power) (C.L.engine_Qrate_engine_factor) * (S.L.engine_Qrate_engine)


	(L.L.elec_busbar_main)
	{if}


			(L.L.engine_fans_on)
			 {if} 

			  (L.L.engine_luefter) (L.S.Timegap) 2000 * + (L.L.engine_n) min (S.L.engine_luefter)
			 {else}
			  (L.L.engine_luefter) (L.S.Timegap) 2000 * - 0 max (S.L.engine_luefter)
			 {endif}


		(L.L.engine_temperature) (C.L.luefterthermostat_opn) >
			       {if}

				1 (S.L.engine_fans_on)
				

		{endif}




		(L.L.engine_temperature) (C.L.luefterthermostat_cls) <
			{if}
				 0 (S.L.engine_fans_on)
				
			{endif}
	{else}
		(L.L.engine_luefter) (L.S.Timegap) 2000 * - 0 max (S.L.engine_luefter)
	{endif}

	(L.L.engine_temperature) (C.L.kuehlerthermostat_opn) > 
	{if}
		1 (S.L.engine_thermostat_open)
	{else}
		(L.L.engine_temperature) (C.L.kuehlerthermostat_cls) <
		{if}
			0 (S.L.engine_thermostat_open)
		{endif}
	{endif}

	
'	--------------------------------------------------
'	Berechnung der Raten:

	(L.L.engine_temperature) (L.L.engine_temperature_envir) - (C.L.engine_Qrate_cooling_factor) * (S.L.engine_Qrate_cooling)
	(L.L.engine_temperature_envir) (L.S.Weather_Temperature) - (C.L.engine_Qrate_Veloc_factor_basic) (C.L.engine_Qrate_veloc_factor) (L.L.velocity) abs * + * (S.L.engine_Qrate_veloc)

	(L.L.engine_temperature) (L.S.Weather_Temperature) - (C.L.engine_Qrate_fan_factor) * (L.L.engine_n) * (S.L.engine_Qrate_fan)


	(L.L.engine_thermostat_open)
	{if}
		(L.L.engine_luefter) !
		{if}	
'			Reduktion der Kühlwirkung durch nur leer mitdrehenden Lüfter
			(L.L.engine_Qrate_fan) 0.5 * (S.L.engine_Qrate_fan)	
		{endif}
	{else}			
'		Nullwirkung des Lüfters durch Kühlwasserkurzschluss	
		0 (S.L.engine_Qrate_fan)
	{endif}


'	--------------------------------------------------
'	Berechnung der Temperaturveränderungen:	    Calculation of the temperature changes:

	(L.L.engine_Qrate_engine) (L.L.engine_Qrate_cooling) - (L.L.engine_Qrate_fan) - (L.L.cabinair_Qrate_engine_fanheatcooling) - (C.L.engine_cvm_engine) / (L.S.Timegap) * (L.L.engine_temperature) + (S.L.engine_temperature) 
	(L.L.engine_Qrate_cooling) (L.L.engine_Qrate_veloc) - (C.L.engine_cvm_envir) / (L.S.Timegap) * (L.L.engine_temperature_envir) + (S.L.engine_temperature_envir) 


'--------------------------------------------------




'	Kochendes Kühlwasser
	(L.L.engine_temperature) (F.L.kuehlwassersmoke) (S.L.engine_kuehlwasser_freq)


	

	
{end}


