From b91f4440fb1bec3eb5bf1351fb3049e8dca938e2 Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Wed, 23 Sep 2015 16:38:44 +0200
Subject: [PATCH] test coverage increased

---
 .../Models/SimulationComponent/Impl/Clutch.cs |  3 +-
 .../Models/SimulationComponent/Impl/Driver.cs |  2 +-
 .../SimulationComponent/Impl/Gearbox.cs       | 15 +++--
 .../SimulationComponent/Impl/ShiftStrategy.cs | 67 ++++++++++---------
 .../Models/SimulationComponent/GearboxTest.cs |  4 +-
 VectoCoreTest/Utils/MockPorts.cs              |  8 ++-
 6 files changed, 56 insertions(+), 43 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Impl/Clutch.cs b/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
index 434635f0d3..7efc5dcbbb 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
@@ -65,7 +65,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			AddClutchLoss(torque, angularVelocity, out torqueIn, out engineSpeedIn);
 
 			var retVal = NextComponent.Request(absTime, dt, torqueIn, engineSpeedIn, dryRun);
-			retVal.ClutchPowerRequest = Formulas.TorqueToPower(torque, angularVelocity);
+			retVal.ClutchPowerRequest = torque * angularVelocity;
 			return retVal;
 		}
 
@@ -76,6 +76,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			AddClutchLoss(torque, angularVelocity, out torqueIn, out engineSpeedIn);
 
 			var retVal = NextComponent.Initialize(torqueIn, engineSpeedIn);
+			retVal.ClutchPowerRequest = torque * angularVelocity;
 			return retVal;
 		}
 
diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
index 907bf4ef08..9faf15efb5 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
@@ -76,7 +76,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			VehicleStopped = false;
 			Log.Debug("==== DRIVER Request ====");
 			Log.Debug(
-				"Request: absTime: {0},  ds: {1}, targetVelocity: {2}, gradient: {3} | distance: {4}, velocity: {5}, vehicle stopped: {7}",
+				"Request: absTime: {0},  ds: {1}, targetVelocity: {2}, gradient: {3} | distance: {4}, velocity: {5}, vehicle stopped: {6}",
 				absTime, ds, targetVelocity, gradient, DataBus.Distance, DataBus.VehicleSpeed, VehicleStopped);
 
 			var retVal = DriverStrategy.Request(absTime, ds, targetVelocity, gradient);
diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
index 0441e363e1..08897b6045 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
@@ -139,8 +139,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		internal ResponseDryRun Initialize(uint gear, NewtonMeter outTorque, PerSecond outAngularVelocity)
 		{
-			var inAngularVelocity = outAngularVelocity * Data.Gears[Gear].Ratio;
-			var inTorque = Data.Gears[Gear].LossMap.GearboxInTorque(inAngularVelocity, outTorque);
+			var inAngularVelocity = outAngularVelocity * Data.Gears[gear].Ratio;
+			var inTorque = Data.Gears[gear].LossMap.GearboxInTorque(inAngularVelocity, outTorque);
 
 			if (!inAngularVelocity.IsEqual(0)) {
 				var alpha = (Data.Inertia.IsEqual(0))
@@ -169,6 +169,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			return new ResponseDryRun {
 				Source = this,
 				EnginePowerRequest = response.EnginePowerRequest,
+				ClutchPowerRequest = response.ClutchPowerRequest,
+				GearboxPowerRequest = outTorque * outAngularVelocity,
 				DeltaFullLoad = response.EnginePowerRequest - fullLoad
 			};
 		}
@@ -289,10 +291,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			_powerLoss = inTorque * inEngineSpeed - outTorque * outEngineSpeed;
 
 			if (!inEngineSpeed.IsEqual(0)) {
-				var torqueLossInertia = Formulas.InertiaPower(inEngineSpeed, _previousInAngularSpeed, Data.Inertia, dt) /
-										inEngineSpeed;
-				_powerLossInertia = torqueLossInertia * inEngineSpeed;
-				inTorque += torqueLossInertia;
+				_powerLossInertia = Formulas.InertiaPower(inEngineSpeed, _previousInAngularSpeed, Data.Inertia, dt);
+				inTorque += _powerLossInertia / inEngineSpeed;
 			} else {
 				_powerLossInertia = 0.SI<Watt>();
 			}
@@ -304,7 +304,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			}
 
 			if (!inEngineSpeed.IsEqual(0)) {
-				if (_strategy.ShiftRequired(absTime, dt, outTorque, outEngineSpeed, inTorque, inEngineSpeed, Gear, _shiftTime)) {
+				if (_strategy.ShiftRequired(absTime, dt, outTorque, outEngineSpeed, inTorque, inEngineSpeed, Gear,
+					_shiftTime + Data.TractionInterruption)) {
 					_shiftTime = absTime + Data.TractionInterruption;
 
 					Log.Debug("Gearbox is shifting. absTime: {0}, dt: {1}, shiftTime: {2}, out: ({3}, {4}), in: ({5}, {6})", absTime,
diff --git a/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs b/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
index 027186755a..d70dd631ed 100644
--- a/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
@@ -99,36 +99,34 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			PreviousGear = 1;
 		}
 
-		private uint GetGear(Second absTime, Second dt, NewtonMeter outTorque, PerSecond outEngineSpeed, bool skipGears,
+		private uint GetGear(Second absTime, Second dt, NewtonMeter outTorque, PerSecond outAngularSpeed, bool skipGears,
 			double torqueReserve)
 		{
 			// maxGear ratio must not result in a angularSpeed below idle-speed
 			var maxGear = (uint)(skipGears ? Data.Gears.Count : Math.Min(PreviousGear + 1, Data.Gears.Count));
-			while (outEngineSpeed * Data.Gears[maxGear].Ratio < DataBus.EngineIdleSpeed && maxGear > 1) {
+			var minGear = skipGears ? 1 : Math.Max(PreviousGear - 1, 1);
+
+			while (outAngularSpeed * Data.Gears[maxGear].Ratio < DataBus.EngineIdleSpeed && maxGear > minGear) {
 				maxGear--;
 			}
 
 			// minGear ratio must not result in an angularSpeed above ratedspeed-range * 1.2
-			var minGear = skipGears ? 1 : Math.Max(PreviousGear - 1, 1);
-			while ((outEngineSpeed * Data.Gears[minGear].Ratio - DataBus.EngineIdleSpeed) /
-					(DataBus.EngineRatedSpeed - DataBus.EngineIdleSpeed) >= 1.2 && minGear < Data.Gears.Count) {
-				minGear++;
-			}
 
-			if (maxGear < minGear) {
-				throw new VectoSimulationException("ShiftStrategy couldn't find an appropriate gear.");
+			while ((outAngularSpeed * Data.Gears[minGear].Ratio - DataBus.EngineIdleSpeed) /
+					(DataBus.EngineRatedSpeed - DataBus.EngineIdleSpeed) >= 1.2 && minGear < maxGear) {
+				minGear++;
 			}
 
 			// loop only runs from maxGear to minGear+1 because minGear is returned afterwards anyway.
 			for (var gear = maxGear; gear > minGear; gear--) {
 				Gearbox.Gear = gear;
-				var response = (ResponseDryRun)Gearbox.Request(absTime, dt, outTorque, outEngineSpeed, true);
+				var response = (ResponseDryRun)Gearbox.Request(absTime, dt, outTorque, outAngularSpeed, true);
 				var currentPower = response.EnginePowerRequest;
 
 				var fullLoadPower = currentPower - response.DeltaFullLoad;
 				var reserve = 1 - (currentPower / fullLoadPower).Cast<Scalar>();
 
-				var inAngularSpeed = outEngineSpeed * Data.Gears[gear].Ratio;
+				var inAngularSpeed = outAngularSpeed * Data.Gears[gear].Ratio;
 				var inTorque = response.ClutchPowerRequest / inAngularSpeed;
 
 				// if in shift curve and torque reserve is provided: return the current gear
@@ -159,7 +157,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		public override bool ShiftRequired(Second absTime, Second dt, NewtonMeter outTorque, PerSecond outAngularVelocity,
 			NewtonMeter inTorque, PerSecond inAngularVelocity, uint gear, Second lastShiftTime)
 		{
-			// todo: During start (clutch slipping) no gear shift (cPower.vb::2077) still needed?
+			if (DataBus.VehicleStopped) {
+				return false;
+			}
+
+			var minimumShiftTimePassed = (lastShiftTime + Data.ShiftTime).IsSmallerOrEqual(absTime);
+			if (!minimumShiftTimePassed) {
+				return false;
+			}
 
 			var speedTooLowForEngine = inAngularVelocity < DataBus.EngineIdleSpeed;
 			var speedToHighForEngine = (inAngularVelocity * Data.Gears[gear].Ratio - DataBus.EngineIdleSpeed) /
@@ -170,36 +175,34 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				return true;
 			}
 
-			var minimumShiftTimePassed = (lastShiftTime + Data.ShiftTime).IsSmallerOrEqual(absTime);
-			if (minimumShiftTimePassed) {
-				// todo: simulate traction interruption power request change after shift 
-				// and only shift if simulated power request still fullfills the shift conditions.
+			// todo: simulate traction interruption power request change after shift 
+			// and only shift if simulated power request still fullfills the shift conditions.
 
-				if (IsBelowDownShiftCurve(gear, inTorque, inAngularVelocity) ||
-					IsAboveUpShiftCurve(gear, inTorque, inAngularVelocity)) {
-					return true;
-				}
+			if (IsBelowDownShiftCurve(gear, inTorque, inAngularVelocity) ||
+				IsAboveUpShiftCurve(gear, inTorque, inAngularVelocity)) {
+				return true;
+			}
 
-				if (Data.EarlyShiftUp) {
-					// try if next gear would provide enough torque reserve
-					var nextGear = gear + 1;
+			if (Data.EarlyShiftUp && gear < Data.Gears.Count) {
+				// try if next gear would provide enough torque reserve
+				var nextGear = gear + 1;
 
-					//todo: is initialize correct? shouldnt it be a dry run request? but gear has to be set in advance
-					var response = Gearbox.Initialize(nextGear, outTorque, outAngularVelocity);
+				//todo: is initialize correct? shouldnt it be a dry run request? but gear has to be set in advance
+				var response = Gearbox.Initialize(nextGear, outTorque, outAngularVelocity);
 
-					var nextAngularVelocity = Data.Gears[nextGear].Ratio * outAngularVelocity;
+				var nextAngularVelocity = Data.Gears[nextGear].Ratio * outAngularVelocity;
 
-					if (!IsBelowDownShiftCurve(nextGear, response.ClutchPowerRequest / nextAngularVelocity, nextAngularVelocity)) {
-						var fullLoadPower = response.EnginePowerRequest - response.DeltaFullLoad;
-						var reserve = 1 - (response.EnginePowerRequest / fullLoadPower).Cast<Scalar>();
+				if (!IsBelowDownShiftCurve(nextGear, response.ClutchPowerRequest / nextAngularVelocity, nextAngularVelocity)) {
+					var fullLoadPower = response.EnginePowerRequest - response.DeltaFullLoad;
+					var reserve = 1 - (response.EnginePowerRequest / fullLoadPower).Cast<Scalar>();
 
-						if (reserve >= Data.TorqueReserve) {
-							return true;
-						}
+					if (reserve >= Data.TorqueReserve) {
+						return true;
 					}
 				}
 			}
 
+
 			return false;
 		}
 
diff --git a/VectoCoreTest/Models/SimulationComponent/GearboxTest.cs b/VectoCoreTest/Models/SimulationComponent/GearboxTest.cs
index a0f15477d1..d3b6c7d158 100644
--- a/VectoCoreTest/Models/SimulationComponent/GearboxTest.cs
+++ b/VectoCoreTest/Models/SimulationComponent/GearboxTest.cs
@@ -199,6 +199,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 
 			var port = new MockTnOutPort();
 			gearbox.InPort().Connect(port);
+			container.Engine = port;
 
 			var ratios = new[] { 0.0, 6.38, 4.63, 3.44, 2.59, 1.86, 1.35, 1, 0.76 };
 			// the first element 0.0 is just a placeholder for axlegear, not used in this test
@@ -374,9 +375,10 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 			var absTime = 0.SI<Second>();
 			var dt = 2.SI<Second>();
 
+			//just for test driver
 			driver.VehicleStopped = true;
 
-			var response = gearbox.OutPort().Request(absTime, dt, 50.SI<NewtonMeter>(), 10000.SI<PerSecond>());
+			var response = gearbox.OutPort().Request(absTime, dt, 50.SI<NewtonMeter>(), 1000000.RPMtoRad());
 			Assert.IsInstanceOfType(response, typeof(ResponseSuccess));
 			AssertHelper.AreRelativeEqual(absTime, port.AbsTime);
 			AssertHelper.AreRelativeEqual(dt, port.Dt);
diff --git a/VectoCoreTest/Utils/MockPorts.cs b/VectoCoreTest/Utils/MockPorts.cs
index 185d7a58c6..fc73e9a0de 100644
--- a/VectoCoreTest/Utils/MockPorts.cs
+++ b/VectoCoreTest/Utils/MockPorts.cs
@@ -31,6 +31,7 @@ namespace TUGraz.VectoCore.Tests.Utils
 					Source = this,
 					GearboxPowerRequest = torque * angularVelocity,
 					EnginePowerRequest = torque * angularVelocity,
+					ClutchPowerRequest = torque * angularVelocity,
 					DeltaFullLoad = (torque - 2300.SI<NewtonMeter>()) * angularVelocity,
 					DeltaDragLoad = (torque - -100.SI<NewtonMeter>()) * angularVelocity
 				};
@@ -40,12 +41,17 @@ namespace TUGraz.VectoCore.Tests.Utils
 				Source = this,
 				GearboxPowerRequest = torque * angularVelocity,
 				EnginePowerRequest = torque * angularVelocity,
+				ClutchPowerRequest = torque * angularVelocity,
 			};
 		}
 
 		public IResponse Initialize(NewtonMeter torque, PerSecond angularVelocity)
 		{
-			return new ResponseSuccess { Source = this, EnginePowerRequest = torque * angularVelocity, };
+			return new ResponseSuccess {
+				Source = this, 
+				EnginePowerRequest = torque * angularVelocity, 
+				ClutchPowerRequest = torque * angularVelocity, 
+			};
 		}
 
 		public void DoCommitSimulationStep()
-- 
GitLab