From 1dafac782cab5c879be9cbfa026afe000d425262 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Wed, 30 Sep 2015 15:40:24 +0200
Subject: [PATCH] SI Class comparison: tolerance has to have same SI unit

---
 .../Impl/CombustionEngine.cs                  |  5 ++--
 .../Impl/DefaultDriverStrategy.cs             |  4 +--
 .../Models/SimulationComponent/Impl/Driver.cs |  6 ++---
 .../SimulationComponent/Impl/Gearbox.cs       |  4 +--
 .../SimulationComponent/Impl/Vehicle.cs       |  2 +-
 VectoCore/Utils/SI.cs                         | 25 +++++++++++--------
 .../Models/SimulationComponent/DriverTest.cs  |  4 +--
 7 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index 88716cb13e..33ca3d940e 100644
--- a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -104,7 +104,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		IResponse ITnOutPort.Request(Second absTime, Second dt, NewtonMeter torque, PerSecond angularVelocity, bool dryRun)
 		{
-			Log.Debug("Engine Power Request: torque: {0}, angularVelocity: {1}, power: {2}", torque, angularVelocity, torque * angularVelocity);
+			Log.Debug("Engine Power Request: torque: {0}, angularVelocity: {1}, power: {2}", torque, angularVelocity,
+				torque * angularVelocity);
 			return DoHandleRequest(absTime, dt, torque, angularVelocity, dryRun);
 		}
 
@@ -550,7 +551,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					var response = (ResponseDryRun)RequestPort.Request(absTime, dt, torque, nextAngularSpeed, true);
 					delta = response.DeltaDragLoad;
 					debug.Add(new { engineSpeed = nextAngularSpeed, searchInterval, delta });
-					if (delta.IsEqual(0, Constants.SimulationSettings.EnginePowerSearchTolerance)) {
+					if (delta.IsEqual(0.SI<Watt>(), Constants.SimulationSettings.EnginePowerSearchTolerance)) {
 						Log.Debug("found operating point in {0} iterations. engine speed: {1}, delta: {2}", retryCount, nextAngularSpeed,
 							delta);
 						return RequestPort.Request(absTime, dt, torque, nextAngularSpeed);
diff --git a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
index ec6f6416aa..cf98e2d3ed 100644
--- a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
@@ -55,7 +55,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					nextAction = GetNextDrivingAction(currentDistance);
 					if (nextAction != null) {
 						if (currentDistance.IsEqual(nextAction.ActionDistance,
-							Constants.SimulationSettings.DriverActionDistanceTolerance.Value())) {
+							Constants.SimulationSettings.DriverActionDistanceTolerance)) {
 							CurrentDrivingMode = DrivingMode.DrivingModeBrake;
 							DrivingModes[CurrentDrivingMode].ResetMode();
 							Log.Debug("Switching to DrivingMode BRAKE");
@@ -111,7 +111,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			// if the distance at the end of the simulation interval is smaller than the new ActionDistance
 			// we are safe - go ahead...
 			if ((Driver.DataBus.Distance + ds).IsSmallerOrEqual(nextAction.TriggerDistance - coastingDistance,
-				Constants.SimulationSettings.DriverActionDistanceTolerance.Value())) {
+				Constants.SimulationSettings.DriverActionDistanceTolerance)) {
 				return retVal;
 			}
 
diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
index 732d008c3f..af45a2cd3a 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
@@ -368,7 +368,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			operatingPoint = SearchBrakingPower(absTime, operatingPoint.SimulationDistance, gradient,
 				operatingPoint.Acceleration, response);
 
-			if (!ds.IsEqual(operatingPoint.SimulationDistance, 1E-15)) {
+			if (!ds.IsEqual(operatingPoint.SimulationDistance, 1E-15.SI<Meter>())) {
 				Log.Info(
 					"SearchOperatingPoint Breaking reduced the max. distance: {0} -> {1}. Issue new request from driving cycle!",
 					operatingPoint.SimulationDistance, ds);
@@ -488,7 +488,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 						NextComponent.Request(absTime, operatingPoint.SimulationInterval, operatingPoint.Acceleration, gradient, true);
 				delta = DataBus.ClutchClosed(absTime) ? response.DeltaDragLoad : response.GearboxPowerRequest;
 
-				if (delta.IsEqual(0, Constants.SimulationSettings.EnginePowerSearchTolerance)) {
+				if (delta.IsEqual(0.SI<Watt>(), Constants.SimulationSettings.EnginePowerSearchTolerance)) {
 					LogManager.EnableLogging();
 					Log.Debug("found operating point in {0} iterations, delta: {1}", debug.Count, delta);
 					return operatingPoint;
@@ -602,7 +602,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					(ResponseDryRun)NextComponent.Request(absTime, retVal.SimulationInterval, retVal.Acceleration, gradient, true);
 				delta = actionRoll ? response.GearboxPowerRequest : (coasting ? response.DeltaDragLoad : response.DeltaFullLoad);
 
-				if (delta.IsEqual(0, Constants.SimulationSettings.EnginePowerSearchTolerance)) {
+				if (delta.IsEqual(0.SI<Watt>(), Constants.SimulationSettings.EnginePowerSearchTolerance)) {
 					LogManager.EnableLogging();
 					Log.Debug("found operating point in {0} iterations. Engine Power req: {2}, Gearbox Power req: {3} delta: {1}",
 						debug.Count, delta, response.EnginePowerRequest, response.GearboxPowerRequest);
diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
index 258cbb63c7..8a64ff02d9 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
@@ -232,7 +232,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				};
 			}
 
-			if (outTorque.IsGreater(0, Constants.SimulationSettings.EnginePowerSearchTolerance)) {
+			if ((outTorque * outAngularVelocity).IsGreater(0.SI<Watt>(), Constants.SimulationSettings.EnginePowerSearchTolerance)) {
 				return new ResponseOverload {
 					Source = this,
 					Delta = outTorque * outAngularVelocity,
@@ -240,7 +240,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				};
 			}
 
-			if (outTorque.IsSmaller(0, Constants.SimulationSettings.EnginePowerSearchTolerance)) {
+			if ((outTorque * outAngularVelocity).IsSmaller(0.SI<Watt>(), Constants.SimulationSettings.EnginePowerSearchTolerance)) {
 				return new ResponseUnderload {
 					Source = this,
 					Delta = outTorque * outAngularVelocity,
diff --git a/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs b/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
index 6b676356b3..5f61519d80 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
@@ -149,7 +149,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				Log.Warn("distance field is not set!");
 			} else {
 				var distance = (SI)writer[ModalResultField.dist];
-				if (!distance.IsEqual(_currentState.Distance, 1e-12)) {
+				if (!distance.IsEqual(_currentState.Distance, 1e-12.SI<Meter>())) {
 					Log.Warn("distance diverges: {0}, distance: {1}", (distance - _currentState.Distance).Value(), distance);
 				}
 			}
diff --git a/VectoCore/Utils/SI.cs b/VectoCore/Utils/SI.cs
index bef29889a3..7d3b876fcb 100644
--- a/VectoCore/Utils/SI.cs
+++ b/VectoCore/Utils/SI.cs
@@ -1599,9 +1599,10 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="si">The si.</param>
 		/// <param name="tolerance">The tolerance.</param>
 		/// <returns></returns>
-		public bool IsEqual(SI si, double tolerance = DoubleExtensionMethods.Tolerance)
+		public bool IsEqual(SI si, SI tolerance = null)
 		{
-			return HasEqualUnit(si) && Val.IsEqual(si.Val, tolerance);
+			return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) &&
+					Val.IsEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value());
 		}
 
 		/// <summary>
@@ -1621,9 +1622,10 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="si">The si.</param>
 		/// <param name="tolerance">The tolerance.</param>
 		/// <returns></returns>
-		public bool IsSmaller(SI si, double tolerance = DoubleExtensionMethods.Tolerance)
+		public bool IsSmaller(SI si, SI tolerance = null)
 		{
-			return HasEqualUnit(si) && Val.IsSmaller(si.Val, tolerance);
+			return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) &&
+					Val.IsSmaller(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value());
 		}
 
 		/// <summary>
@@ -1632,9 +1634,10 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="si">The si.</param>
 		/// <param name="tolerance">The tolerance.</param>
 		/// <returns></returns>
-		public bool IsSmallerOrEqual(SI si, double tolerance = DoubleExtensionMethods.Tolerance)
+		public bool IsSmallerOrEqual(SI si, SI tolerance = null)
 		{
-			return HasEqualUnit(si) && Val.IsSmallerOrEqual(si.Val, tolerance);
+			return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) &&
+					Val.IsSmallerOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value());
 		}
 
 		/// <summary>
@@ -1643,9 +1646,10 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="si">The si.</param>
 		/// <param name="tolerance">The tolerance.</param>
 		/// <returns></returns>
-		public bool IsGreater(SI si, double tolerance = DoubleExtensionMethods.Tolerance)
+		public bool IsGreater(SI si, SI tolerance = null)
 		{
-			return HasEqualUnit(si) && Val.IsGreater(si.Val, tolerance);
+			return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) &&
+					Val.IsGreater(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value());
 		}
 
 		/// <summary>
@@ -1654,9 +1658,10 @@ namespace TUGraz.VectoCore.Utils
 		/// <param name="si">The si.</param>
 		/// <param name="tolerance">The tolerance.</param>
 		/// <returns></returns>
-		public bool IsGreaterOrEqual(SI si, double tolerance = DoubleExtensionMethods.Tolerance)
+		public bool IsGreaterOrEqual(SI si, SI tolerance = null)
 		{
-			return HasEqualUnit(si) && Val.IsGreaterOrEqual(si.Val, tolerance);
+			return (tolerance == null || HasEqualUnit(tolerance)) && HasEqualUnit(si) &&
+					Val.IsGreaterOrEqual(si.Val, tolerance == null ? DoubleExtensionMethods.Tolerance : tolerance.Value());
 		}
 
 		/// <summary>
diff --git a/VectoCoreTest/Models/SimulationComponent/DriverTest.cs b/VectoCoreTest/Models/SimulationComponent/DriverTest.cs
index 1a8d15854c..03e87a4fb4 100644
--- a/VectoCoreTest/Models/SimulationComponent/DriverTest.cs
+++ b/VectoCoreTest/Models/SimulationComponent/DriverTest.cs
@@ -73,7 +73,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 			Assert.AreEqual(4.9812, vehicleContainer.VehicleSpeed.Value(), Tolerance);
 			Assert.AreEqual(0.2004, response.SimulationInterval.Value(), Tolerance);
 			Assert.AreEqual(engine.PreviousState.FullDragPower.Value(), engine.PreviousState.EnginePower.Value(),
-				Constants.SimulationSettings.EnginePowerSearchTolerance);
+				Constants.SimulationSettings.EnginePowerSearchTolerance.Value());
 
 			while (vehicleContainer.VehicleSpeed > 1) {
 				response = driver.DrivingActionCoast(absTime, 1.SI<Meter>(), velocity, 0.SI<Radian>());
@@ -132,7 +132,7 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponent
 			Assert.AreEqual(4.9812, vehicleContainer.VehicleSpeed.Value(), Tolerance);
 			Assert.AreEqual(0.2004, response.SimulationInterval.Value(), Tolerance);
 			Assert.AreEqual(engine.PreviousState.FullDragPower.Value(), engine.PreviousState.EnginePower.Value(),
-				Constants.SimulationSettings.EnginePowerSearchTolerance);
+				Constants.SimulationSettings.EnginePowerSearchTolerance.Value());
 
 			while (vehicleContainer.VehicleSpeed > 1) {
 				response = driver.DrivingActionCoast(absTime, 1.SI<Meter>(), velocity, gradient);
-- 
GitLab