From 9cfe75b82a57d6641989b34c35c8f7d53462a2d4 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Thu, 15 Sep 2016 16:34:46 +0200
Subject: [PATCH] adding validation for gearbox: validate torque converter if
 AT gearbox

---
 .../SimulationComponent/Data/GearboxData.cs   | 19 ++++++++++++++++++-
 .../SimulationComponent/Data/VehicleData.cs   |  1 +
 .../Impl/CombustionEngine.cs                  | 12 ++++++++----
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/GearboxData.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/GearboxData.cs
index 83b07b0710..1f7ac5ff1b 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Data/GearboxData.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/GearboxData.cs
@@ -31,6 +31,7 @@
 
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
+using System.Linq;
 using System.Runtime.Serialization;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Utils;
@@ -42,7 +43,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
 	/// Class for Gearbox Data. Gears can be accessed via Gears-Dictionary and range from 1 upwards.
 	/// </summary>
 	/// <remarks>The Axle Gear has its own Property "AxleGearData" and is *not included* in the Gears-Dictionary.</remarks>
-	[DataContract]
+	[DataContract, CustomValidation(typeof(GearboxData), "ValidateGearboxData")]
 	public class GearboxData : SimulationComponentData
 	{
 		public GearboxType Type { get; internal set; }
@@ -112,5 +113,21 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
 
 		[Required, SIRange(0, double.MaxValue)]
 		public MeterPerSquareSecond UpshiftMinAcceleration { get; internal set; }
+
+		// ReSharper disable once UnusedMember.Global -- used via Validation
+		public static ValidationResult ValidateGearboxData(GearboxData gearboxData, ValidationContext validationContext)
+		{
+			var mode = GetExecutionMode(validationContext);
+
+			var result = new List<ValidationResult>();
+			if (gearboxData.Type.AutomaticTransmission()) {
+				result.AddRange(gearboxData.TorqueConverterData.Validate(mode));
+			}
+
+			if (result.Any()) {
+				return new ValidationResult("Validation of Gearbox Data failed", result.Select(x => x.ErrorMessage));
+			}
+			return ValidationResult.Success;
+		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/VehicleData.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/VehicleData.cs
index 4c32251d17..053a0a8fa5 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Data/VehicleData.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/VehicleData.cs
@@ -170,6 +170,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
 			WheelsInertia = wheelsInertia;
 		}
 
+		// ReSharper disable once UnusedMember.Global  -- used via Validation
 		public static ValidationResult ValidateVehicleData(VehicleData vehicleData, ValidationContext validationContext)
 		{
 			var mode = GetExecutionMode(validationContext);
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
index 5145b3c88a..6cdc927f7b 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs
@@ -207,8 +207,12 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			// get max. torque as limited by gearbox. gearbox only limits torqueOut!
 			var gearboxFullLoad = DataBus.GearMaxTorque;
 
-			var deltaFull = ComputeDelta(torqueOut, totalTorqueDemand + (CurrentState.InertiaTorqueLoss < 0 ? CurrentState.InertiaTorqueLoss : 0.SI<NewtonMeter>()), CurrentState.DynamicFullLoadTorque, gearboxFullLoad, true);
-			var deltaDrag = ComputeDelta(torqueOut, totalTorqueDemand - (CurrentState.InertiaTorqueLoss < 0 ? CurrentState.InertiaTorqueLoss : 0.SI<NewtonMeter>()), CurrentState.FullDragTorque,
+			var deltaFull = ComputeDelta(torqueOut,
+				totalTorqueDemand + (CurrentState.InertiaTorqueLoss < 0 ? CurrentState.InertiaTorqueLoss : 0.SI<NewtonMeter>()),
+				CurrentState.DynamicFullLoadTorque, gearboxFullLoad, true);
+			var deltaDrag = ComputeDelta(torqueOut,
+				totalTorqueDemand - (CurrentState.InertiaTorqueLoss < 0 ? CurrentState.InertiaTorqueLoss : 0.SI<NewtonMeter>()),
+				CurrentState.FullDragTorque,
 				gearboxFullLoad != null ? -gearboxFullLoad : null, false);
 
 			if (dryRun) {
@@ -332,12 +336,12 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		{
 			if (CurrentState.FullDragTorque >= 0 && torqueDemand < 0) {
 				throw new VectoSimulationException("P_engine_drag > 0! Tq_drag: {1}, Tq_eng: {2},  n_eng_avg: {0} [1/min] ",
-					CurrentState.EngineSpeed.Value().RadToRPM(), CurrentState.FullDragTorque, CurrentState.EngineTorque);
+					CurrentState.EngineSpeed.AsRPM, CurrentState.FullDragTorque, CurrentState.EngineTorque);
 			}
 
 			if (CurrentState.DynamicFullLoadTorque <= 0 && torqueDemand > 0) {
 				throw new VectoSimulationException("P_engine_full < 0! Tq_drag: {1}, Tq_eng: {2},  n_eng_avg: {0} [1/min] ",
-					CurrentState.EngineSpeed.Value().RadToRPM(), CurrentState.FullDragTorque, CurrentState.EngineTorque);
+					CurrentState.EngineSpeed.AsRPM, CurrentState.FullDragTorque, CurrentState.EngineTorque);
 			}
 		}
 
-- 
GitLab