From 5ec1a1908b47fbe52207720de8f4fdbed8710525 Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Thu, 22 Dec 2016 18:11:35 +0100
Subject: [PATCH] Allow more values for AirDrag in DryRun (in Search)

---
 VectoCommon/VectoCommon/Utils/VectoMath.cs         | 14 ++++++++++++--
 .../SimulationComponent/Impl/CombustionEngine.cs   |  4 ++--
 .../Models/SimulationComponent/Impl/Vehicle.cs     | 10 +++++++++-
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/VectoCommon/VectoCommon/Utils/VectoMath.cs b/VectoCommon/VectoCommon/Utils/VectoMath.cs
index 52e7dda65c..5e2960b828 100644
--- a/VectoCommon/VectoCommon/Utils/VectoMath.cs
+++ b/VectoCommon/VectoCommon/Utils/VectoMath.cs
@@ -164,12 +164,22 @@ namespace TUGraz.VectoCommon.Utils
 		[MethodImpl(MethodImplOptions.AggressiveInlining)]
 		public static T Max<T>(T c1, T c2) where T : IComparable
 		{
-			return c1.CompareTo(c2) >= 0 ? c1 : c2;
+			return c1.CompareTo(c2) > 0 ? c1 : c2;
+		}
+
+		/// <summary>
+		/// Returns the maximum of two values.
+		/// </summary>
+		[DebuggerStepThrough]
+		[MethodImpl(MethodImplOptions.AggressiveInlining)]
+		public static T Max<T>(double c1, T c2) where T : SIBase<T>
+		{
+			return c1 > c2.Value() ? c1.SI<T>() : c2;
 		}
 
 		[DebuggerStepThrough]
 		[MethodImpl(MethodImplOptions.AggressiveInlining)]
-		public static T Max<T>(T c1, T c2, T c3) where T : IComparable
+		public static T Max<T>(T c1, T c2, T c3) where T : SIBase<T>
 		{
 			return Max(Max(c1, c2), c3);
 		}
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index d4e0793639..807d96a3d4 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -605,8 +605,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				var prevEngineSpeed = _engine.PreviousState.EngineSpeed;
 				var dragLoad = _engine.ModelData.FullLoadCurve.DragLoadStationaryPower(prevEngineSpeed);
 
-				var nextEnginePower = (_lastEnginePower - dragLoad) *
-									VectoMath.Max(idleTime.Value() * PeDropSlope + PeDropOffset, 0) + dragLoad;
+				var nextEnginePower = (_lastEnginePower - dragLoad) * Math.Max(0, idleTime.Value() * PeDropSlope + PeDropOffset) +
+									dragLoad;
 
 				var auxDemandResponse = RequestPort.Request(absTime, dt, 0.SI<NewtonMeter>(), prevEngineSpeed, true);
 
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
index f5abf10e10..cc93a43539 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
@@ -30,6 +30,7 @@
 */
 
 using System;
+using TUGraz.VectoCommon.Exceptions;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
 using TUGraz.VectoCore.Configuration;
@@ -102,7 +103,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 			CurrentState.DriverAcceleration = DriverAcceleration(acceleration);
 			CurrentState.RollingResistance = RollingResistance(gradient);
-			CurrentState.AirDragResistance = AirDragResistance(PreviousState.Velocity, CurrentState.Velocity);
+			try {
+				CurrentState.AirDragResistance = AirDragResistance(PreviousState.Velocity, CurrentState.Velocity);
+			} catch (VectoException ex) {
+				Log.Warn("Exception during calculation of AirDragResistance: absTime: {0}, dist: {1}, v: {2}. {3}", absTime,
+					CurrentState.Distance, CurrentState.Velocity, ex);
+				CurrentState.AirDragResistance = AirDragResistance(VectoMath.Max(0, PreviousState.Velocity),
+					VectoMath.Max(0, CurrentState.Velocity));
+			}
 			CurrentState.SlopeResistance = SlopeResistance(gradient);
 
 			// DriverAcceleration = vehicleTractionForce - RollingResistance - AirDragResistance - SlopeResistance
-- 
GitLab