From 334498a5e38b7836acfe3ebc36bb2a35d455ebf9 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Mon, 15 May 2017 13:49:28 +0200
Subject: [PATCH] fix testcases: mock gearbox has to return geearinfo, measured
 speed / pwheel mode cope with EngineSpeedTooHigh response

---
 .../SimulationComponent/Impl/CombustionEngine.cs   | 13 ++++++++++---
 .../Impl/EngineOnlyCombustionEngine.cs             |  5 +++++
 .../Impl/MeasuredSpeedDrivingCycle.cs              | 14 ++++++++++++--
 .../Impl/PowertrainDrivingCycle.cs                 | 10 +++++++++-
 .../EngineOnlyCycle/EngineOnlyCycleTest.cs         |  1 +
 .../SimulationComponent/CombustionEngineTest.cs    |  5 ++++-
 VectoCore/VectoCoreTest/Utils/MockGearbox.cs       |  4 ++--
 7 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index 64e98f3945..fc9023c56f 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -181,8 +181,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 			var avgEngineSpeed = (PreviousState.EngineSpeed + angularVelocity) / 2.0;
 
-			var engineSpeedLimit = VectoMath.Min(DataBus.GetGearData(DataBus.Gear).MaxSpeed,
-				ModelData.FullLoadCurves[0].N95hSpeed);
+			var engineSpeedLimit = GetEngineSpeedLimit();
 			if (!dryRun && avgEngineSpeed.IsGreater(engineSpeedLimit, Constants.SimulationSettings.LineSearchTolerance)) {
 				return new ResponseEngineSpeedTooHigh() { DeltaEngineSpeed = avgEngineSpeed - engineSpeedLimit };
 			}
@@ -294,6 +293,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			};
 		}
 
+		protected virtual PerSecond GetEngineSpeedLimit()
+		{
+			return DataBus.Gear == 0
+				? ModelData.FullLoadCurves[0].N95hSpeed
+				: VectoMath.Min(DataBus.GetGearData(DataBus.Gear).MaxSpeed,
+					ModelData.FullLoadCurves[0].N95hSpeed);
+		}
+
 		private NewtonMeter ComputeDelta(NewtonMeter torqueOut, NewtonMeter totalTorqueDemand, NewtonMeter maxEngineTorque)
 		{
 			var deltaEngine = totalTorqueDemand - maxEngineTorque;
@@ -577,7 +584,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				nextAngularSpeed = velocitySlope < 0
 					? VectoMath.LimitTo(VectoMath.Max(_engineTargetSpeed, nextAngularSpeed), _engine.EngineIdleSpeed, engineMaxSpeed)
 					: VectoMath.LimitTo(VectoMath.Min(_engineTargetSpeed, nextAngularSpeed), _engine.EngineIdleSpeed, engineMaxSpeed);
-				
+
 
 				var retVal = RequestPort.Request(absTime, dt, 0.SI<NewtonMeter>(), nextAngularSpeed);
 				retVal.Switch().
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs
index dd481bd07e..9837ac690f 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs
@@ -109,5 +109,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			}
 			return requestedEngineTorque;
 		}
+
+		protected override PerSecond GetEngineSpeedLimit()
+		{
+			return ModelData.FullLoadCurves[0].N95hSpeed;
+		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs
index 948ab95cd8..d10714a91d 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs
@@ -211,8 +211,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 									y => ((ResponseDryRun)y).DeltaFullLoad.Value());
 							Log.Info(
 								"Found operating point for driver acceleration. absTime: {0}, dt: {1}, acceleration: {2}, gradient: {3}",
-								absTime,
-								dt, acceleration, gradient);
+								absTime, dt, acceleration, gradient);
 						} else {
 							DataBus.BrakePower = SearchAlgorithm.Search(DataBus.BrakePower, r.Delta, -r.Delta,
 								getYValue: result => DataBus.ClutchClosed(absTime)
@@ -243,6 +242,17 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 						}
 						response = NextComponent.Request(absTime, dt, acceleration, gradient);
 					})
+					.Case<ResponseEngineSpeedTooHigh>(r => {
+						acceleration = SearchAlgorithm.Search(acceleration, r.DeltaEngineSpeed,
+							Constants.SimulationSettings.OperatingPointInitialSearchIntervalAccelerating,
+							getYValue: result => ((ResponseDryRun)result).DeltaEngineSpeed,
+							evaluateFunction: x => NextComponent.Request(absTime, dt, x, gradient, true),
+							criterion:
+								y => ((ResponseDryRun)y).DeltaEngineSpeed.Value());
+						Log.Info(
+							"Found operating point for driver acceleration. absTime: {0}, dt: {1}, acceleration: {2}, gradient: {3}",
+							absTime, dt, acceleration, gradient);
+					})
 					.Case<ResponseFailTimeInterval>(r => { dt = r.DeltaT; })
 					.Case<ResponseSuccess>()
 					.Default(
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs
index 4832ec5296..65ce768343 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs
@@ -35,6 +35,7 @@ using System.Linq;
 using TUGraz.VectoCommon.Exceptions;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
+using TUGraz.VectoCore.Configuration;
 using TUGraz.VectoCore.Models.Connector.Ports;
 using TUGraz.VectoCore.Models.Connector.Ports.Impl;
 using TUGraz.VectoCore.Models.Simulation;
@@ -117,7 +118,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				debug.Add(response);
 				response.Switch()
 					.Case<ResponseGearShift>(
-						() => response = NextComponent.Request(absTime, dt, CycleIterator.LeftSample.Torque, angularVelocity))
+						() => response = NextComponent.Request(absTime, dt, CurrentState.InTorque, angularVelocity))
 					.Case<ResponseUnderload>(r => {
 						var torqueInterval = -r.Delta / (angularVelocity.IsEqual(0) ? 10.RPMtoRad() : angularVelocity);
 						var torque = SearchAlgorithm.Search(CycleIterator.LeftSample.Torque, r.Delta, torqueInterval,
@@ -135,6 +136,13 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 						response = NextComponent.Request(absTime, dt, torque, angularVelocity);
 						CurrentState.InAngularVelocity = angularVelocity;
 					})
+					.Case<ResponseEngineSpeedTooHigh>(r => {
+						angularVelocity = SearchAlgorithm.Search(angularVelocity, r.DeltaEngineSpeed,
+							1.RPMtoRad(),
+							getYValue: result => ((ResponseDryRun)result).DeltaEngineSpeed,
+							evaluateFunction: x => NextComponent.Request(absTime, dt, CurrentState.InTorque, x, true),
+							criterion: y => ((ResponseDryRun)y).DeltaEngineSpeed.Value());
+					})
 					.Case<ResponseFailTimeInterval>(r => { dt = r.DeltaT; })
 					.Case<ResponseSuccess>(() => { })
 					.Default(
diff --git a/VectoCore/VectoCoreTest/Integration/EngineOnlyCycle/EngineOnlyCycleTest.cs b/VectoCore/VectoCoreTest/Integration/EngineOnlyCycle/EngineOnlyCycleTest.cs
index 526abde344..d42b318eaa 100644
--- a/VectoCore/VectoCoreTest/Integration/EngineOnlyCycle/EngineOnlyCycleTest.cs
+++ b/VectoCore/VectoCoreTest/Integration/EngineOnlyCycle/EngineOnlyCycleTest.cs
@@ -115,6 +115,7 @@ namespace TUGraz.VectoCore.Tests.Integration.EngineOnlyCycle
 			var vehicleContainer = new VehicleContainer(ExecutionMode.Engineering);
 
 			var engine = new CombustionEngine(vehicleContainer, MockSimulationDataFactory.CreateEngineDataFromFile(EngineFile, 0));
+			var gbx = new MockGearbox(vehicleContainer) { Gear = 0 };
 
 			var absTime = 0.SI<Second>();
 			var dt = 1.SI<Second>();
diff --git a/VectoCore/VectoCoreTest/Models/SimulationComponent/CombustionEngineTest.cs b/VectoCore/VectoCoreTest/Models/SimulationComponent/CombustionEngineTest.cs
index 2a146114e3..78407f3749 100644
--- a/VectoCore/VectoCoreTest/Models/SimulationComponent/CombustionEngineTest.cs
+++ b/VectoCore/VectoCoreTest/Models/SimulationComponent/CombustionEngineTest.cs
@@ -78,6 +78,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 			var vehicle = new VehicleContainer(ExecutionMode.Engineering);
 			var engineData = MockSimulationDataFactory.CreateEngineDataFromFile(CoachEngine, 0);
 			var engine = new CombustionEngine(vehicle, engineData);
+			var gearbox = new MockGearbox(vehicle) { Gear = 0 };
 
 			var port = engine.OutPort();
 
@@ -94,8 +95,9 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 		public void TestSimpleModalData()
 		{
 			var vehicle = new VehicleContainer(ExecutionMode.Engineering);
-			var engineData = MockSimulationDataFactory.CreateEngineDataFromFile(CoachEngine,0);
+			var engineData = MockSimulationDataFactory.CreateEngineDataFromFile(CoachEngine, 0);
 			var engine = new CombustionEngine(vehicle, engineData);
+			var gearbox = new MockGearbox(vehicle) { Gear = 0 };
 			var port = engine.OutPort();
 
 			var absTime = 0.SI<Second>();
@@ -220,6 +222,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 			var vehicleContainer = new VehicleContainer(ExecutionMode.Engineering);
 			var engineData = MockSimulationDataFactory.CreateEngineDataFromFile(engineFile, 0);
 			var engine = new CombustionEngine(vehicleContainer, engineData);
+			var gearbox = new MockGearbox(vehicleContainer) { Gear = 0 };
 
 			var expectedResults = VectoCSVFile.Read(resultFile);
 
diff --git a/VectoCore/VectoCoreTest/Utils/MockGearbox.cs b/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
index 00395fdcde..b3ca339253 100644
--- a/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
+++ b/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
@@ -70,7 +70,7 @@ namespace TUGraz.VectoCore.Tests.Utils
 			get { return 1.SI<Second>(); }
 		}
 
-		public uint NumGears { get;  set; }
+		public uint NumGears { get; set; }
 
 		public MeterPerSecond StartSpeed
 		{
@@ -96,7 +96,7 @@ namespace TUGraz.VectoCore.Tests.Utils
 
 		public GearData GetGearData(uint gear)
 		{
-			throw new NotImplementedException();
+			return new GearData();
 		}
 
 		public void Connect(ITnOutPort other)
-- 
GitLab