From 7562374946677cf2a1a40b53d62238ad4d4fcfb2 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <quaritsch@ivt.tugraz.at>
Date: Fri, 21 Apr 2023 09:43:47 +0200
Subject: [PATCH] combustion engine: avoid throwing an exception in case PT1
 cannot be calculated - rewrite with ifs

---
 .../Impl/CombustionEngine.cs                   | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index d6fa80ae44..7619043b39 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -562,8 +562,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var stationaryFullLoadPower = stationaryFullLoadTorque * avgAngularVelocity;
 			Watt dynFullPowerCalculated;
 
-			
-
 			// disable pt1 behaviour if PT1Disabled is true, or if the previous enginepower is greater than the current stationary fullload power (in this case the pt1 calculation fails)
 			if (PT1Disabled || PreviousState.EnginePower.IsGreaterOrEqual(stationaryFullLoadPower)) {
 				dynFullPowerCalculated = stationaryFullLoadPower;
@@ -571,9 +569,19 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				try {
 					var pt1 = ModelData.FullLoadCurves[DataBus.GearboxInfo.Gear.Gear].PT1(avgAngularVelocity).Value.Value();
 					var powerRatio = (PreviousState.EnginePower / stationaryFullLoadPower).Value();
-					var tStarPrev = pt1 * Math.Log(1.0 / (1 - powerRatio), Math.E).SI<Second>();
-					var tStar = tStarPrev + PreviousState.dt;
-					dynFullPowerCalculated = stationaryFullLoadPower * (pt1.IsEqual(0) ? 1 : 1 - Math.Exp((-tStar / pt1).Value()));
+					var tStarPrev = pt1 * Math.Log(1.0 / (1 - powerRatio), Math.E);
+					if (!double.IsNaN(tStarPrev)) {
+						var tStar = tStarPrev.SI<Second>() + PreviousState.dt;
+						dynFullPowerCalculated = stationaryFullLoadPower * (pt1.IsEqual(0) ? 1 : 1 - Math.Exp((-tStar / pt1).Value()));
+					} else {
+						if (dryRun) {
+							Log.Info("PT1 calculation failed (dryRun: {0})", dryRun);
+							dynFullPowerCalculated = stationaryFullLoadPower;
+                        } else {
+							Log.Warn("PT1 calculation failed (dryRun: {0})", dryRun);
+							throw new VectoException("PT1 calculation failed!");
+                        }
+					}
 				} catch (VectoException e) {
 					if (dryRun) {
 						Log.Info("PT1 calculation failed (dryRun: {0}): {1}", dryRun, e.Message);
-- 
GitLab