From 5d85a7fd212f762f533346b728b1b3d4e5b7496f Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Wed, 12 Aug 2020 17:57:57 +0200
Subject: [PATCH] fix testcases reading hybrid job more work on P3 hybrid: em
 torque during braking phases - during gearshift, if battery is already full,
 ... electric motor map: set initial search orientation to avoid finding a
 solution outside allowed torque range

---
 .../Data/ElectricMotor/EfficiencyMap.cs         |  2 +-
 .../Impl/CombustionEngine.cs                    |  4 ++--
 .../SimulationComponent/Impl/ElectricMotor.cs   |  2 +-
 .../Impl/StopStartCombustionEngine.cs           |  7 +++++--
 .../Strategies/HybridStrategy.cs                | 17 ++++++++++++-----
 .../Strategies/TestPowertrain.cs                |  4 ++++
 .../VectoCoreTest/FileIO/JsonReadHybridTest.cs  |  4 ++--
 .../Integration/Hybrid/ParallelHybridTest.cs    |  2 +-
 .../HybridStrategyParams.vhctl                  |  5 ++++-
 9 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
index 2876372d97..2d0724cb45 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
@@ -133,7 +133,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data {
 				return 0.SI<NewtonMeter>();
 			}
 			var retVal = SearchAlgorithm.Search(
-				maxEmTorque, elPowerMaxEM.ElectricalPower, maxEmTorque * 0.1,
+				maxEmTorque, elPowerMaxEM.ElectricalPower, maxEmTorque * 0.1 * (maxEmTorque > 0 ? -1 : 1),
 				getYValue: x => {
 					var myX = (EfficiencyResult)x;
 					return myX.ElectricalPower - batPower;
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index ba92c1e518..b87419fdc4 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -650,8 +650,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				if (outAngularVelocity != null) {
 					throw new VectoException("IdleController can only handle idle requests, i.e. angularVelocity == null!");
 				}
-				if (!outTorque.IsEqual(0, 1e-2)) {
-					throw new VectoException("Torque has to be 0 for idle requests!");
+				if (!outTorque.IsEqual(0, 5e-2)) {
+					throw new VectoException("Torque has to be 0 for idle requests! {0}", outTorque);
 				}
 
 				return DoHandleRequest(absTime, dt, outTorque, outAngularVelocity);
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
index 489f428e10..d42a2e6b7c 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
@@ -100,7 +100,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var electricSystemResponse = ElectricPower.Request(absTime, dt, 0.SI<Watt>(), true);
 			var maxBatPower = electricSystemResponse.MaxPowerDrag;
 
-			var maxBatRecuperationTorque = maxBatPower.IsEqual(0) ? 0.SI<NewtonMeter>() : ModelData.EfficiencyMap.LookupTorque(maxBatPower, avgSpeed, maxEmTorque);
+			var maxBatRecuperationTorque = maxBatPower.IsEqual(0) ? ModelData.DragCurve.Lookup(avgSpeed) : ModelData.EfficiencyMap.LookupTorque(maxBatPower, avgSpeed, maxEmTorque);
 			var maxTorqueRecuperate = VectoMath.Min(maxEmTorque, maxBatRecuperationTorque);
 			return maxTorqueRecuperate < 0 ? null : maxTorqueRecuperate;
 		}
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/StopStartCombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/StopStartCombustionEngine.cs
index 23a35b3c88..8c646c9859 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/StopStartCombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/StopStartCombustionEngine.cs
@@ -90,12 +90,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl {
 					DeltaFullLoad = 0.SI<Watt>(),
 					DeltaDragLoad = 0.SI<Watt>(),
 					Engine = {
-						TotalTorqueDemand = 0.SI<NewtonMeter>(),
-						PowerRequest = 0.SI<Watt>(),
+						TorqueOutDemand = outTorque,
+						PowerRequest = outTorque * outAngularVelocity,
 						DynamicFullLoadPower = 0.SI<Watt>(),
 						DragPower = 0.SI<Watt>(),
 						EngineSpeed = 0.RPMtoRad(),
 						AuxiliariesPowerDemand = 0.SI<Watt>(),
+						TotalTorqueDemand = 0.SI<NewtonMeter>(),
+						DragTorque = 0.SI<NewtonMeter>()
 					},
 					DeltaEngineSpeed = 0.RPMtoRad(),
 				};
@@ -103,6 +105,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl {
 
 			return new ResponseSuccess(this) {
 				Engine = {
+					TorqueOutDemand = outTorque,
 					PowerRequest = 0.SI<Watt>(),
 					DynamicFullLoadPower = 0.SI<Watt>(),
 					TotalTorqueDemand = 0.SI<NewtonMeter>(),
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
index e3c3a3e82c..4e756f7347 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
@@ -214,7 +214,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 
 				var emPos = ModelData.ElectricMachinesData.First().Item1;
 				var currentGear = !DataBus.GearboxInfo.GearEngaged(absTime)
-					? Controller.ShiftStrategy.NextGear.Gear
+					? 0
 					: (PreviousState.GearboxEngaged
 						? DataBus.GearboxInfo.Gear
 						: Controller.ShiftStrategy.NextGear.Gear);
@@ -270,8 +270,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 				var maxRecuperationResponse = RequestDryRun(
 					absTime, dt, outTorque, outAngularVelocity, currentGear, maxRecuperation);
 
-				if (maxRecuperationResponse.DeltaDragLoad.IsSmaller(0)) {
-					// even with full recuperation (and no braking) the operating point is below the drag curve - use full recuperation
+				if (maxRecuperationResponse.DeltaDragLoad.IsSmaller(0) && 
+					maxRecuperationResponse.ElectricSystem.BatteryPowerDemand.IsBetween(maxRecuperationResponse.ElectricSystem.MaxPowerDrag, maxRecuperationResponse.ElectricSystem.MaxPowerDrive)) {
+					// even with full recuperation (and no braking) the operating point is below the drag curve (and the battery can handle it) - use full recuperation
 					eval.Add(
 						new HybridResultEntry() {
 							ICEOff = !DataBus.EngineInfo.EngineOn,
@@ -303,7 +304,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 								{ emPos, emTq }
 							}
 						};
-						return RequestDryRun(absTime, dt, outTorque, outAngularVelocity, currentGear, cfg);
+						return RequestDryRun(absTime, dt, outTorque, outAngularVelocity, DataBus.GearboxInfo.GearEngaged(absTime) ? currentGear : 0, cfg);
 					},
 					criterion: r => {
 						var response = r as ResponseDryRun;
@@ -801,6 +802,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 		private ResponseDryRun RequestDryRun(Second absTime, Second dt, NewtonMeter outTorque, PerSecond outAngularVelocity, uint nextGear, HybridStrategyResponse cfg)
 		{
 			TestPowertrain.Gearbox.Gear = PreviousState.GearboxEngaged ? DataBus.GearboxInfo.Gear : Controller.ShiftStrategy.NextGear.Gear;
+			TestPowertrain.Gearbox.Disengaged = nextGear == 0;
 			TestPowertrain.Gearbox.DisengageGearbox = nextGear == 0;
 			TestPowertrain.Container.VehiclePort.Initialize(DataBus.VehicleInfo.VehicleSpeed, DataBus.DrivingCycleInfo.RoadGradient ?? 0.SI<Radian>());
 			TestPowertrain.HybridController.ApplyStrategySettings(cfg);
@@ -834,6 +836,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 
 			if (nextGear == 0) {
 				TestPowertrain.Gearbox._nextGear = Controller.ShiftStrategy.NextGear;
+				TestPowertrain.Gearbox.Disengaged = nextGear == 0;
 			}
 
 			//if (!PreviousState.GearboxEngaged) {
@@ -862,7 +865,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 				TestPowertrain.ElectricMotorP2.PreviousState.OutAngularVelocity =
 					DataBus.ElectricMotorInfo(PowertrainPosition.HybridP2).ElectricMotorSpeed;
 			}
-			
+			if (/*nextGear != DataBus.GearboxInfo.Gear && */TestPowertrain.ElectricMotorP3 != null) {
+				TestPowertrain.ElectricMotorP3.PreviousState.OutAngularVelocity =
+					DataBus.ElectricMotorInfo(PowertrainPosition.HybridP3).ElectricMotorSpeed;
+			}
+
 			var retVal = TestPowertrain.HybridController.NextComponent.Request(absTime, dt, outTorque, outAngularVelocity, true);
 
 			return retVal as ResponseDryRun;
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/TestPowertrain.cs b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/TestPowertrain.cs
index bf51156043..aa4e9d7bd2 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/TestPowertrain.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/TestPowertrain.cs
@@ -24,6 +24,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies {
 
 		public StopStartCombustionEngine CombustionEngine;
 		public ElectricMotor ElectricMotorP2;
+		public ElectricMotor ElectricMotorP3;
 
 		public TestPowertrain(SimplePowertrainContainer container, IDataBus realContainer)
 		{
@@ -36,6 +37,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies {
 			ElectricMotorP2 = container.ElectricMotors.ContainsKey(PowertrainPosition.HybridP2)
 				? container.ElectricMotors[PowertrainPosition.HybridP2] as ElectricMotor
 				: null;
+			ElectricMotorP3 = container.ElectricMotors.ContainsKey(PowertrainPosition.HybridP3)
+				? container.ElectricMotors[PowertrainPosition.HybridP3] as ElectricMotor
+				: null;
 			if (Gearbox == null) {
 				throw new VectoException("Unknown gearboxtype in TestContainer: {0}", Container.GearboxCtl.GetType().FullName);
 			}
diff --git a/VectoCore/VectoCoreTest/FileIO/JsonReadHybridTest.cs b/VectoCore/VectoCoreTest/FileIO/JsonReadHybridTest.cs
index 330796592c..c5a7004c98 100644
--- a/VectoCore/VectoCoreTest/FileIO/JsonReadHybridTest.cs
+++ b/VectoCore/VectoCoreTest/FileIO/JsonReadHybridTest.cs
@@ -125,7 +125,7 @@ namespace TUGraz.VectoCore.Tests.FileIO
 			Assert.NotNull(bat);
 			Assert.AreEqual(2, bat.Count);
 			Assert.AreEqual(50, bat.BatteryPack.MaxCurrentFactor);
-			Assert.AreEqual(0.4986666, ri.Lookup(0.5).Value());
+			Assert.AreEqual(0.04, ri.Lookup(0.5).Value());
 
 			var em = engineering.JobInputData.Vehicle.Components.ElectricMachines;
 
@@ -134,7 +134,7 @@ namespace TUGraz.VectoCore.Tests.FileIO
 
 			Assert.AreEqual(PowertrainPosition.HybridP2, em.Entries[0].Position);
 
-			Assert.AreEqual(0.15, em.Entries[0].ElectricMachine.Inertia.Value());
+			Assert.AreEqual(0.2, em.Entries[0].ElectricMachine.Inertia.Value());
 
 		}
 
diff --git a/VectoCore/VectoCoreTest/Integration/Hybrid/ParallelHybridTest.cs b/VectoCore/VectoCoreTest/Integration/Hybrid/ParallelHybridTest.cs
index c77d1e1d10..cf7e465fc5 100644
--- a/VectoCore/VectoCoreTest/Integration/Hybrid/ParallelHybridTest.cs
+++ b/VectoCore/VectoCoreTest/Integration/Hybrid/ParallelHybridTest.cs
@@ -586,7 +586,7 @@ namespace TUGraz.VectoCore.Tests.Integration.Hybrid
 			var sumData = new SummaryDataContainer(fileWriter);
 			var jobContainer = new JobContainer(sumData);
             var container = CreateParallelHybridPowerTrain(
-				cycleData, Path.GetFileNameWithoutExtension(modFileName), initialSoc, largeMotor, sumData, pAuxEl, pos, ratio, payload);
+				cycleData,modFileName, initialSoc, largeMotor, sumData, pAuxEl, pos, ratio, payload);
 			var run = new DistanceRun(container);
 			jobContainer.AddRun(run);
 			return jobContainer;
diff --git a/VectoCore/VectoCoreTest/TestData/Hybrids/GenericVehicle_Group2_P2/HybridStrategyParams.vhctl b/VectoCore/VectoCoreTest/TestData/Hybrids/GenericVehicle_Group2_P2/HybridStrategyParams.vhctl
index d63963762a..e38a9d4f64 100644
--- a/VectoCore/VectoCoreTest/TestData/Hybrids/GenericVehicle_Group2_P2/HybridStrategyParams.vhctl
+++ b/VectoCore/VectoCoreTest/TestData/Hybrids/GenericVehicle_Group2_P2/HybridStrategyParams.vhctl
@@ -9,6 +9,9 @@
     "EquivalenceFactor": 2.5,
     "MinSoC": 20.0,
     "MaxSoC": 80.0,
-    "TargetSoC": 50.0
+    "TargetSoC": 50.0,
+    "MinICEOnTime": 3,
+    "AuxBufferTime": 5,
+    "AuxBufferChgTime":  3 
   }
 }
\ No newline at end of file
-- 
GitLab