From 6a8a780e7dde595bedcec1cc7e844a1ca02ef733 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <quaritsch@ivt.tugraz.at>
Date: Tue, 6 Dec 2022 20:24:00 +0100
Subject: [PATCH] adding gear disabling in declaration mode (again?)

---
 .../AbstractSimulationDataAdapter.cs          | 35 +---------------
 .../EngineeringDataAdapter.cs                 |  4 +-
 .../SimulationComponents/EngineDataAdapter.cs |  9 ++--
 .../GearBoxDataAdapter.cs                     | 41 ++++++++++++++++++-
 .../GenericModelData/GenericBusEngineData.cs  |  2 +-
 .../Models/Simulation/Data/VectoRunData.cs    |  3 +-
 6 files changed, 50 insertions(+), 44 deletions(-)

diff --git a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/AbstractSimulationDataAdapter.cs b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/AbstractSimulationDataAdapter.cs
index a50a9d6625..01c90a9f8b 100644
--- a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/AbstractSimulationDataAdapter.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/AbstractSimulationDataAdapter.cs
@@ -335,40 +335,7 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
 		}
 
 
-		/// <summary>
-		/// Filters the gears based on disabling rule: Disable either the last 1 or 2 gears by setting their vehicle-level torque limit to 0.
-		/// </summary>
-		internal static IList<ITransmissionInputData> FilterDisabledGears(IList<ITorqueLimitInputData> torqueLimits, IGearboxDeclarationInputData gearboxData) {
-			if (torqueLimits == null || torqueLimits.Count == 0){
-				return gearboxData?.Gears ?? new List<ITransmissionInputData>();
-			}
-
-			if (gearboxData == null) {
-				return new List<ITransmissionInputData>();
-			}
-
-			var gearsInput = gearboxData.Gears;
-			var lastGearNumber = gearsInput.Max(g => g.Gear);
-			var toRemove = torqueLimits
-				.Where(tqLimit => tqLimit.MaxTorque.IsEqual(0))
-				.Select(tqLimit => gearsInput.FirstOrDefault(g => g.Gear == tqLimit.Gear))
-				.Where(g => g != default)
-				.OrderBy(g => g.Gear)
-				.ToList();
-			
-			if ((toRemove.Count == 1 && toRemove[0].Gear != lastGearNumber)
-				|| (toRemove.Count == 2 && (toRemove[0].Gear != lastGearNumber-1 || toRemove[1].Gear != lastGearNumber))
-				|| toRemove.Count > 2) {
-				throw new VectoException("Only the last 1 or 2 gears can be disabled. Disabling gear {0} for a {1}-speed gearbox is not allowed.", 
-					toRemove.Min(g => g.Gear), gearsInput.Count);
-			}
-
-			foreach (var entry in toRemove) {
-				gearsInput.Remove(entry);
-			}
-			
-			return gearsInput;
-		}
+		
 	}
 }
 
diff --git a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
index c36a8a6738..d5dc21c5c0 100644
--- a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/EngineeringDataAdapter.cs
@@ -217,7 +217,7 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
 			retVal.EngineStartTime = engine.EngineStartTime ?? DeclarationData.Engine.DefaultEngineStartTime;
 			var limits = vehicle.TorqueLimits.ToDictionary(e => e.Gear);
 
-			var gears = FilterDisabledGears(vehicle.TorqueLimits, gbx);
+			var gears = GearboxDataAdapterBase.FilterDisabledGears(vehicle.TorqueLimits, gbx);
 			var fullLoadCurves = new Dictionary<uint, EngineFullLoadCurve>(gears.Count + 1);
 			fullLoadCurves[0] = FullLoadCurveReader.Create(engine.EngineModes.First().FullLoadCurve);
 			fullLoadCurves[0].EngineData = retVal;
@@ -331,7 +331,7 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter
 				throw new VectoSimulationException("At least two Gear-Entries must be defined in Gearbox!");
 			}
 
-			var gearsInput = FilterDisabledGears(inputData.JobInputData.Vehicle.TorqueLimits, gearbox);
+			var gearsInput = GearboxDataAdapterBase.FilterDisabledGears(inputData.JobInputData.Vehicle.TorqueLimits, gearbox);
 			
 			SetEngineeringData(gearbox, gearshiftData, retVal);
 
diff --git a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/EngineDataAdapter.cs b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/EngineDataAdapter.cs
index 39c0521b2b..1865f8f787 100644
--- a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/EngineDataAdapter.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/EngineDataAdapter.cs
@@ -204,15 +204,16 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter.SimulationComponen
 			retVal.EngineStartTime = DeclarationData.Engine.DefaultEngineStartTime;
 			var limits = vehicle.TorqueLimits?.ToDictionary(e => e.Gear) ??
 						new Dictionary<int, ITorqueLimitInputData>();
-			var numGears = gearbox?.Gears.Count ?? 0;
-			var fullLoadCurves = new Dictionary<uint, EngineFullLoadCurve>(numGears + 1);
+			//var numGears = gearbox?.Gears.Count ?? 0;
+			var gears = GearboxDataAdapterBase.FilterDisabledGears(vehicle.TorqueLimits, gearbox);
+			var fullLoadCurves = new Dictionary<uint, EngineFullLoadCurve>(gears.Count + 1);
 			fullLoadCurves[0] = FullLoadCurveReader.Create(mode.FullLoadCurve, true);
 			fullLoadCurves[0].EngineData = retVal;
 			foreach (var gear in gearbox?.Gears ?? new List<ITransmissionInputData>(0))
 			{
 				var maxTorque = VectoMath.Min(
-					GearboxDataAdapterBase.GbxMaxTorque(gear, numGears, fullLoadCurves[0].MaxTorque),
-					VehicleDataAdapter.VehMaxTorque(gear, numGears, limits, fullLoadCurves[0].MaxTorque));
+					GearboxDataAdapterBase.GbxMaxTorque(gear, gears.Count + 1, fullLoadCurves[0].MaxTorque),
+					VehicleDataAdapter.VehMaxTorque(gear, gears.Count + 1, limits, fullLoadCurves[0].MaxTorque));
 				fullLoadCurves[(uint)gear.Gear] = IntersectFullLoadCurves(fullLoadCurves[0], maxTorque);
 			}
 
diff --git a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/GearBoxDataAdapter.cs b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/GearBoxDataAdapter.cs
index 7c3558e49c..bb0d5caf56 100644
--- a/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/GearBoxDataAdapter.cs
+++ b/VectoCore/VectoCore/InputData/Reader/DataObjectAdapter/SimulationComponents/GearBoxDataAdapter.cs
@@ -231,6 +231,42 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter.SimulationComponen
 
 			return retVal;
 		}
+
+		/// <summary>
+		/// Filters the gears based on disabling rule: Disable either the last 1 or 2 gears by setting their vehicle-level torque limit to 0.
+		/// </summary>
+		internal static IList<ITransmissionInputData> FilterDisabledGears(IList<ITorqueLimitInputData> torqueLimits, IGearboxDeclarationInputData gearboxData)
+		{
+			if (torqueLimits == null || torqueLimits.Count == 0) {
+				return gearboxData?.Gears ?? new List<ITransmissionInputData>();
+			}
+
+			if (gearboxData == null) {
+				return new List<ITransmissionInputData>();
+			}
+
+			var gearsInput = gearboxData.Gears;
+			var lastGearNumber = gearsInput.Max(g => g.Gear);
+			var toRemove = torqueLimits
+				.Where(tqLimit => tqLimit.MaxTorque.IsEqual(0))
+				.Select(tqLimit => gearsInput.FirstOrDefault(g => g.Gear == tqLimit.Gear))
+				.Where(g => g != default)
+				.OrderBy(g => g.Gear)
+				.ToList();
+
+			if ((toRemove.Count == 1 && toRemove[0].Gear != lastGearNumber)
+				|| (toRemove.Count == 2 && (toRemove[0].Gear != lastGearNumber - 1 || toRemove[1].Gear != lastGearNumber))
+				|| toRemove.Count > 2) {
+				throw new VectoException("Only the last 1 or 2 gears can be disabled. Disabling gear {0} for a {1}-speed gearbox is not allowed.",
+					toRemove.Min(g => g.Gear), gearsInput.Count);
+			}
+
+			foreach (var entry in toRemove) {
+				gearsInput.Remove(entry);
+			}
+
+			return gearsInput;
+		}
 	}
 
 	public class GearboxDataAdapter : GearboxDataAdapterBase
@@ -272,13 +308,13 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter.SimulationComponen
 				throw new VectoSimulationException("Unsupported gearbox type: {0}!", retVal.Type);
 			}
 
-			var gearsInput = gearbox.Gears;
+			var gearsInput = FilterDisabledGears(inputData.TorqueLimits, gearbox);  //gearbox.Gears;
 			if (gearsInput.Count < 1)
 			{
 				throw new VectoSimulationException(
 					"At least one Gear-Entry must be defined in Gearbox!");
 			}
-
+			
 			SetDeclarationData(retVal);
 			
 			var gearDifferenceRatio = gearbox.Type.AutomaticTransmission() && gearbox.Gears.Count > 2
@@ -381,6 +417,7 @@ namespace TUGraz.VectoCore.InputData.Reader.DataObjectAdapter.SimulationComponen
 		}
 
 		#endregion
+
 	}
 
 	public class GenericCompletedBusGearboxDataAdapter : GearboxDataAdapter
diff --git a/VectoCore/VectoCore/Models/GenericModelData/GenericBusEngineData.cs b/VectoCore/VectoCore/Models/GenericModelData/GenericBusEngineData.cs
index c79fa613cd..9c0506ea33 100644
--- a/VectoCore/VectoCore/Models/GenericModelData/GenericBusEngineData.cs
+++ b/VectoCore/VectoCore/Models/GenericModelData/GenericBusEngineData.cs
@@ -64,7 +64,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 			};
 
 			var limits = primaryVehicle.TorqueLimits.ToDictionary(e => e.Gear);
-			var gears = AbstractSimulationDataAdapter.FilterDisabledGears(primaryVehicle.TorqueLimits, gearbox);
+			var gears = GearboxDataAdapterBase.FilterDisabledGears(primaryVehicle.TorqueLimits, gearbox);
 			
 			var numGears = gears.Count;
 			var fullLoadCurves = new Dictionary<uint, EngineFullLoadCurve>(numGears + 1);
diff --git a/VectoCore/VectoCore/Models/Simulation/Data/VectoRunData.cs b/VectoCore/VectoCore/Models/Simulation/Data/VectoRunData.cs
index 3f9b180a3b..32449a484c 100644
--- a/VectoCore/VectoCore/Models/Simulation/Data/VectoRunData.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Data/VectoRunData.cs
@@ -43,6 +43,7 @@ using TUGraz.VectoCommon.Utils;
 using TUGraz.VectoCore.Configuration;
 using TUGraz.VectoCore.InputData.Reader.ComponentData;
 using TUGraz.VectoCore.InputData.Reader.DataObjectAdapter;
+using TUGraz.VectoCore.InputData.Reader.DataObjectAdapter.SimulationComponents;
 using TUGraz.VectoCore.InputData.Reader.Impl;
 using TUGraz.VectoCore.Models.Declaration;
 using TUGraz.VectoCore.Models.Simulation.DataBus;
@@ -303,7 +304,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Data
 								angledriveRatio * dynamicTyreRadius;
 			var maxSpeed = VectoMath.Min(vehicleMaxSpeed, (runData.VehicleDesignSpeed ?? 90.KMPHtoMeterPerSecond()) + (runData.DriverData?.OverSpeed?.OverSpeed ?? 0.KMPHtoMeterPerSecond()));
 
-			var gearsInput = AbstractSimulationDataAdapter.FilterDisabledGears(runData.VehicleData.InputData.TorqueLimits, gearboxData.InputData);
+			var gearsInput = GearboxDataAdapterBase.FilterDisabledGears(runData.VehicleData.InputData.TorqueLimits, gearboxData.InputData);
 			var gears = gearboxData.Gears.Where(f => gearsInput.Any(g => f.Key == g.Gear)).ToList();
 			
 			if (gears.Count + 1 != engineData.FullLoadCurves.Count) {
-- 
GitLab