From b2291b1f742b608f515b6db1221f59096fbbc6a0 Mon Sep 17 00:00:00 2001
From: Stefanos Doumpoulakis <dubulak@gmail.com>
Date: Wed, 12 Oct 2022 12:20:25 +0300
Subject: [PATCH] fixed stop-start functionality for VTP

---
 .../VectoCore/Configuration/Constants.cs      |   6 +
 .../Simulation/Impl/PowertrainBuilder.cs      |   3 +-
 .../Impl/VTPCombustionEngine.cs               |  14 +-
 .../Impl/VTPnoSScombustionEngine.cs           | 224 ------------------
 VectoCore/VectoCore/VectoCore.csproj          |   1 -
 5 files changed, 12 insertions(+), 236 deletions(-)
 delete mode 100644 VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPnoSScombustionEngine.cs

diff --git a/VectoCore/VectoCore/Configuration/Constants.cs b/VectoCore/VectoCore/Configuration/Constants.cs
index 1cfb81011c..69f96b0a9e 100644
--- a/VectoCore/VectoCore/Configuration/Constants.cs
+++ b/VectoCore/VectoCore/Configuration/Constants.cs
@@ -333,6 +333,12 @@ namespace TUGraz.VectoCore.Configuration
 			/// from the measured positive energy of the vdri, a warning is issued.
 			/// </summary>
 			public const double VTPEngineWorkDeviationThreshold = 0.1;
+
+			/// <summary>
+			/// When the engine speed (in VTP) is higher than idle speed times this factor,
+			/// the combustion engine is set to on.
+			/// </summary>
+			public const double VTPIdleSpeedDetectionFactor = 0.85;
 		}
 
 		public static class XML
diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/PowertrainBuilder.cs b/VectoCore/VectoCore/Models/Simulation/Impl/PowertrainBuilder.cs
index 947e99f65b..5fddfe68e3 100644
--- a/VectoCore/VectoCore/Models/Simulation/Impl/PowertrainBuilder.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Impl/PowertrainBuilder.cs
@@ -180,8 +180,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 				.AddComponent(gearbox, data.Retarder, container)
 				.AddComponent(new Clutch(container, data.EngineData));
 			var engine = new VTPCombustionEngine(container, data.EngineData, pt1Disabled: true);
-			//var engine = new VTPnoSScombustionEngine(container, data.EngineData, pt1Disabled: true);
-
+			
 			var aux = CreateSpeedDependentAuxiliaries(data, container);
 			var engineFan = new EngineFanAuxiliary(data.FanData.FanCoefficients, data.FanData.FanDiameter);
 			aux.AddCycle(Constants.Auxiliaries.IDs.Fan, cycleEntry => engineFan.PowerDemand(cycleEntry));
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
index 7d2acc51ce..1b1f73f87f 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
@@ -78,11 +78,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public override IResponse Request(Second absTime, Second dt, NewtonMeter outTorque, PerSecond outAngularVelocity, bool dryRun)
 		{
-			// Engine speed and fuel consumption determine whether we consider the engine is on or off.
+			// Engine speed determines whether we consider the engine is on or off.
 			// This cut-off point affects calculations and Response (stop-start functionality, see base.Request)
-			var fuelConsumption = DataBus.DrivingCycleInfo.CycleData.LeftSample.Fuelconsumption.Sum(c => c.Value);
-			
-			CombustionEngineOn = (DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed >= EngineIdleSpeed) || fuelConsumption.IsGreater(0);
+
+			CombustionEngineOn = DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed >= 
+				(EngineIdleSpeed * Constants.SimulationSettings.VTPIdleSpeedDetectionFactor);
 
 			return base.Request(absTime, dt, outTorque, outAngularVelocity, dryRun);
 		}
@@ -208,11 +208,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		protected override PerSecond GetEngineSpeed(PerSecond angularSpeed)
         {
-			// When speed is below idle, clip to idle to avoid crashing.
-			// Returning zero causes crashes (divide by zero, etc).
-			return (DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed < EngineIdleSpeed) 
-				? EngineIdleSpeed 
-				: DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed;
+			return DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed;
         }
 
         protected override double WHTCCorrectionFactor(IFuelProperties fuel)
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPnoSScombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPnoSScombustionEngine.cs
deleted file mode 100644
index 0787a40f62..0000000000
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPnoSScombustionEngine.cs
+++ /dev/null
@@ -1,224 +0,0 @@
-using System;
-using System.Linq;
-using TUGraz.VectoCommon.BusAuxiliaries;
-using TUGraz.VectoCommon.Models;
-using TUGraz.VectoCommon.Utils;
-using TUGraz.VectoCore.Configuration;
-using TUGraz.VectoCore.Models.Connector.Ports.Impl;
-using TUGraz.VectoCore.Models.Simulation;
-using TUGraz.VectoCore.Models.Simulation.Data;
-using TUGraz.VectoCore.Models.SimulationComponent.Data;
-using TUGraz.VectoCore.OutputData;
-using TUGraz.VectoCore.Utils;
-
-namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
-{
-    public class VTPnoSScombustionEngine : CombustionEngine
-    {
-        private bool firstInit = true;
-
-        public VTPnoSScombustionEngine(IVehicleContainer container, CombustionEngineData modelData, bool pt1Disabled = false) : base(container, modelData, pt1Disabled) { }
-
-		public override IResponse Initialize(NewtonMeter outTorque, PerSecond outAngularVelocity)
-		{
-			if (outAngularVelocity == null) {
-				outAngularVelocity = EngineIdleSpeed;
-			}
-			var auxDemand = EngineAux == null ? 0.SI<NewtonMeter>() : EngineAux.Initialize(outTorque, outAngularVelocity);
-			if (firstInit) {
-				PreviousState = new EngineState {
-					EngineSpeed = outAngularVelocity,
-					dt = 1.SI<Second>(),
-					InertiaTorqueLoss = 0.SI<NewtonMeter>(),
-					StationaryFullLoadTorque = ModelData.FullLoadCurves[DataBus.GearboxInfo.Gear.Gear].FullLoadStationaryTorque(outAngularVelocity),
-					FullDragTorque = ModelData.FullLoadCurves[DataBus.GearboxInfo.Gear.Gear].DragLoadStationaryTorque(outAngularVelocity),
-					EngineTorque = outTorque + auxDemand,
-					EnginePower = (outTorque + auxDemand) * outAngularVelocity,
-				};
-				PreviousState.DynamicFullLoadTorque = PreviousState.StationaryFullLoadTorque;
-			}
-
-			return new ResponseSuccess(this) {
-				Engine = {
-					PowerRequest = PreviousState.EnginePower,
-					EngineSpeed = outAngularVelocity
-				}
-			};
-		}
-
-		protected override IResponse DoHandleRequest(Second absTime, Second dt, NewtonMeter torqueReq,
-			PerSecond angularVelocity, bool dryRun)
-		{
-			firstInit = false;
-			var powerDemand = angularVelocity * torqueReq;
-
-			var avgEngineSpeed = GetEngineSpeed(angularVelocity);
-			var torqueOut = powerDemand / avgEngineSpeed;
-
-
-			var fullDragTorque = ModelData.FullLoadCurves[DataBus.GearboxInfo.Gear.Gear].DragLoadStationaryTorque(avgEngineSpeed);
-			var fullLoadTorque = ModelData.FullLoadCurves[DataBus.GearboxInfo.Gear.Gear].FullLoadStationaryTorque(avgEngineSpeed);
-			
-			var inertiaTorqueLoss =
-				Formulas.InertiaPower(angularVelocity, PreviousState.EngineSpeed, ModelData.Inertia, dt) /
-				avgEngineSpeed;
-
-			if (EngineAux != null) {
-				EngineAux.Initialize(0.SI<NewtonMeter>(), avgEngineSpeed);
-			}
-			var auxTorqueDemand = EngineAux == null
-				? 0.SI<NewtonMeter>()
-				: EngineAux.TorqueDemand(absTime, dt, torqueOut, avgEngineSpeed, dryRun);
-			// compute the torque the engine has to provide. powertrain + aux + its own inertia
-			var totalTorqueDemand = torqueOut + auxTorqueDemand + inertiaTorqueLoss;
-
-			Log.Debug("EngineInertiaTorque: {0}", inertiaTorqueLoss);
-			Log.Debug("Drag Curve: torque: {0}, power: {1}", fullDragTorque, fullDragTorque * avgEngineSpeed);
-
-			var deltaFull = totalTorqueDemand - fullLoadTorque;
-			var deltaDrag = totalTorqueDemand - fullDragTorque;
-
-			if (dryRun) {
-				return new ResponseDryRun(this) {
-					DeltaFullLoad = deltaFull * avgEngineSpeed,
-					DeltaDragLoad = deltaDrag * avgEngineSpeed,
-					DeltaEngineSpeed = 0.RPMtoRad(),
-					Engine = {
-						PowerRequest = torqueOut * avgEngineSpeed,
-						DynamicFullLoadPower = fullLoadTorque * avgEngineSpeed,
-						DragPower = fullDragTorque * avgEngineSpeed,
-						EngineSpeed = angularVelocity,
-						AuxiliariesPowerDemand = auxTorqueDemand * avgEngineSpeed,
-					},
-				};
-			}
-			CurrentState.dt = dt;
-			CurrentState.EngineSpeed = angularVelocity;
-			CurrentState.EngineTorqueOut = torqueOut;
-			CurrentState.FullDragTorque = fullDragTorque;
-			CurrentState.DynamicFullLoadTorque = fullLoadTorque;
-			CurrentState.StationaryFullLoadTorque = fullLoadTorque;
-			CurrentState.InertiaTorqueLoss = inertiaTorqueLoss;
-
-			if ((deltaFull * avgEngineSpeed).IsGreater(0.SI<Watt>(), Constants.SimulationSettings.LineSearchTolerance) &&
-				(deltaDrag * avgEngineSpeed).IsSmaller(0.SI<Watt>(), Constants.SimulationSettings.LineSearchTolerance)) {
-				//throw new VectoSimulationException(
-				Log.Error(
-					"Unexpected condition: requested torque_out is above gearbox full-load and engine is below drag load! deltaFull: {0}, deltaDrag: {1}",
-					deltaFull, deltaDrag);
-			}
-
-			var minTorque = CurrentState.FullDragTorque;
-			var maxTorque = CurrentState.DynamicFullLoadTorque;
-
-			try {
-				CurrentState.EngineTorque = totalTorqueDemand.LimitTo(minTorque, maxTorque);
-			} catch (Exception) {
-				var extrapolated = avgEngineSpeed > ModelData.FullLoadCurves[0].FullLoadEntries.Last().EngineSpeed;
-				Log.Error("Engine full-load torque is below drag torque. max_torque: {0}, drag_torque: {1}, extrapolated: {2}", maxTorque, minTorque, extrapolated);
-				throw;
-			}
-			CurrentState.EnginePower = CurrentState.EngineTorque * avgEngineSpeed;
-
-			if (totalTorqueDemand.IsGreater(0) &&
-				(deltaFull * avgEngineSpeed).IsGreater(0, Constants.SimulationSettings.LineSearchTolerance)) {
-				Log.Debug("requested engine power exceeds fullload power: delta: {0}", deltaFull);
-				return new ResponseOverload(this) {
-					AbsTime = absTime,
-					Delta = deltaFull * avgEngineSpeed,
-					Engine = {
-						PowerRequest = totalTorqueDemand * avgEngineSpeed,
-						DynamicFullLoadPower = fullLoadTorque * avgEngineSpeed,
-						DragPower = CurrentState.FullDragTorque * avgEngineSpeed,
-						EngineSpeed = angularVelocity,
-						AuxiliariesPowerDemand = auxTorqueDemand * avgEngineSpeed,
-					},
-				};
-			}
-
-			if (totalTorqueDemand.IsSmaller(0) &&
-				(deltaDrag * avgEngineSpeed).IsSmaller(0, Constants.SimulationSettings.LineSearchTolerance)) {
-				Log.Debug("requested engine power is below drag power: delta: {0}", deltaDrag);
-				return new ResponseUnderload(this) {
-					AbsTime = absTime,
-					Delta = deltaDrag * avgEngineSpeed,
-					Engine = {
-						PowerRequest = totalTorqueDemand * avgEngineSpeed,
-						DynamicFullLoadPower = fullLoadTorque * avgEngineSpeed,
-						DragPower = CurrentState.FullDragTorque * avgEngineSpeed,
-						EngineSpeed = angularVelocity,
-						AuxiliariesPowerDemand = auxTorqueDemand * avgEngineSpeed,
-					},
-				};
-			}
-
-			//UpdateEngineState(CurrentState.EnginePower, avgEngineSpeed);
-
-			return new ResponseSuccess(this) {
-				Engine = {
-					PowerRequest = totalTorqueDemand * avgEngineSpeed,
-					DynamicFullLoadPower = fullLoadTorque * avgEngineSpeed,
-					DragPower = CurrentState.FullDragTorque * avgEngineSpeed,
-					EngineSpeed = angularVelocity,
-					AuxiliariesPowerDemand = auxTorqueDemand * avgEngineSpeed,
-				},
-			};
-		}
-
-		protected override PerSecond GetEngineSpeed(PerSecond angularSpeed)
-        {
-			// When speed is below idle, clip to idle to avoid crashing.
-			// Returning zero causes crashes (divide by zero, etc).
-			return (DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed < EngineIdleSpeed) 
-				? EngineIdleSpeed 
-				: DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed;
-        }
-
-        protected override void DoWriteModalResults(Second time, Second simulationInterval, IModalDataContainer container)
-        {
-			base.DoWriteModalResults(time, simulationInterval, container);
-
-			var avgEngineSpeed = GetEngineSpeed(CurrentState.EngineSpeed);
-
-			foreach (var fuel in ModelData.Fuels) {
-				var result = fuel.ConsumptionMap.GetFuelConsumption(
-					CurrentState.EngineTorque, avgEngineSpeed,
-					DataBus.ExecutionMode != ExecutionMode.Declaration);
-
-				var fuelData = fuel.FuelData;
-				
-				bool aboveIdleSpeed = (DataBus.DrivingCycleInfo.CycleData.LeftSample.EngineSpeed >= EngineIdleSpeed);
-				var fc = aboveIdleSpeed ? result.Value : 0.SI<KilogramPerSecond>();
-				var fcNCVcorr = fc * fuelData.HeatingValueCorrection;
-
-				var fcWHTC = fcNCVcorr * WHTCCorrectionFactor(fuel.FuelData);
-				
-				var advancedAux = EngineAux as BusAuxiliariesAdapter;
-				if (advancedAux != null) {
-					advancedAux.DoWriteModalResultsICE(time, simulationInterval, container);
-					
-				}
-				var fcFinal = fcWHTC;
-
-				container[ModalResultField.FCMap, fuelData] = fc;
-				container[ModalResultField.FCNCVc, fuel.FuelData] = fcNCVcorr;
-				container[ModalResultField.FCWHTCc, fuel.FuelData] = fcWHTC;
-				container[ModalResultField.FCFinal, fuel.FuelData] = fcFinal;
-			}
-        }
-
-        protected override double WHTCCorrectionFactor(IFuelProperties fuel)
-		{
-			var selected = ModelData.Fuels.First(x => x.FuelData.FuelType == fuel.FuelType);
-			
-			if (DataBus.DrivingCycleInfo.CycleData.LeftSample.VehicleTargetSpeed >= Constants.SimulationSettings.HighwaySpeedThreshold) {
-				return selected.WHTCMotorway;
-			}
-			if (DataBus.DrivingCycleInfo.CycleData.LeftSample.VehicleTargetSpeed >= Constants.SimulationSettings.RuralSpeedThreshold) {
-				return selected.WHTCRural;
-			}
-			return selected.WHTCUrban;
-		}
-
-    }
-}
diff --git a/VectoCore/VectoCore/VectoCore.csproj b/VectoCore/VectoCore/VectoCore.csproj
index 32d71c81cc..bf9dc27651 100644
--- a/VectoCore/VectoCore/VectoCore.csproj
+++ b/VectoCore/VectoCore/VectoCore.csproj
@@ -356,7 +356,6 @@
     <Compile Include="Models\SimulationComponent\Impl\StopStartCombustionEngine.cs" />
     <Compile Include="Models\SimulationComponent\Impl\VelocityRollingLookup.cs" />
     <Compile Include="Models\SimulationComponent\Impl\VelocitySpeedGearshiftPreprocessor.cs" />
-    <Compile Include="Models\SimulationComponent\Impl\VTPnoSScombustionEngine.cs" />
     <Compile Include="Models\Simulation\DataBus\IEngineControl.cs" />
     <Compile Include="Models\Simulation\DataBus\IGearboxControl.cs" />
     <Compile Include="Models\Simulation\Data\ShiftStrategyParameters.cs" />
-- 
GitLab