From 2010c8c41f18127480c37469864d06a7251d53b6 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Mon, 20 Nov 2017 10:41:19 +0100
Subject: [PATCH] more expressive error message if extrapolated full-load is
 lower than drag load

---
 .../SimulationComponent/Impl/CombustionEngine.cs      |  9 ++++++++-
 .../SimulationComponent/Impl/VTPCombustionEngine.cs   | 11 +++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index a48aebc58f..78cba098ce 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -30,6 +30,7 @@
 */
 
 using System;
+using System.Linq;
 using TUGraz.VectoCommon.Exceptions;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
@@ -250,7 +251,13 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var minTorque = CurrentState.FullDragTorque;
 			var maxTorque = CurrentState.DynamicFullLoadTorque;
 
-			CurrentState.EngineTorque = totalTorqueDemand.LimitTo(minTorque, maxTorque);
+			try {
+				CurrentState.EngineTorque = totalTorqueDemand.LimitTo(minTorque, maxTorque);
+			} catch (Exception) {
+				var extrapolated = avgEngineSpeed > ModelData.FullLoadCurves[0].FullLoadEntries.Last().EngineSpeed;
+				Log.Error("Engine full-load torque is below drag torque. max_torque: {0}, drag_torque: {1}, extrapolated: {2}", maxTorque, minTorque, extrapolated);
+				throw;
+			}
 			CurrentState.EnginePower = CurrentState.EngineTorque * avgEngineSpeed;
 
 			if (totalTorqueDemand.IsGreater(0) &&
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
index 0b73e0df8e..801f334873 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/VTPCombustionEngine.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
 using TUGraz.VectoCore.Configuration;
@@ -25,7 +26,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 
 			var fullDragTorque = ModelData.FullLoadCurves[DataBus.Gear].DragLoadStationaryTorque(avgEngineSpeed);
-			var fullLoadTorque = ModelData.FullLoadCurves[DataBus.Gear].FullLoadStationaryTorque(angularVelocity);
+			var fullLoadTorque = ModelData.FullLoadCurves[DataBus.Gear].FullLoadStationaryTorque(avgEngineSpeed);
 			
 			var inertiaTorqueLoss =
 				Formulas.InertiaPower(angularVelocity, PreviousState.EngineSpeed, ModelData.Inertia, dt) /
@@ -76,7 +77,13 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var minTorque = CurrentState.FullDragTorque;
 			var maxTorque = CurrentState.DynamicFullLoadTorque;
 
-			CurrentState.EngineTorque = totalTorqueDemand.LimitTo(minTorque, maxTorque);
+			try {
+				CurrentState.EngineTorque = totalTorqueDemand.LimitTo(minTorque, maxTorque);
+			} catch (Exception) {
+				var extrapolated = avgEngineSpeed > ModelData.FullLoadCurves[0].FullLoadEntries.Last().EngineSpeed;
+				Log.Error("Engine full-load torque is below drag torque. max_torque: {0}, drag_torque: {1}, extrapolated: {2}", maxTorque, minTorque, extrapolated);
+				throw;
+			}
 			CurrentState.EnginePower = CurrentState.EngineTorque * avgEngineSpeed;
 
 			if (totalTorqueDemand.IsGreater(0) &&
-- 
GitLab