From 8cc8380f31c722a0c63c38698ed2e21f32414c94 Mon Sep 17 00:00:00 2001
From: "VKMTHD\\haraldmartini" <harald.martini@student.tugraz.at>
Date: Wed, 10 May 2023 12:10:19 +0200
Subject: [PATCH] updated GenericIEPCData

---
 .../GenericModelData/GenericBusIEPCData.cs    | 128 +++++++++++++-----
 1 file changed, 97 insertions(+), 31 deletions(-)

diff --git a/VectoCore/VectoCore/Models/GenericModelData/GenericBusIEPCData.cs b/VectoCore/VectoCore/Models/GenericModelData/GenericBusIEPCData.cs
index 3ff291a221..4506ce8b2d 100644
--- a/VectoCore/VectoCore/Models/GenericModelData/GenericBusIEPCData.cs
+++ b/VectoCore/VectoCore/Models/GenericModelData/GenericBusIEPCData.cs
@@ -8,6 +8,8 @@ using TUGraz.VectoCore.InputData.Reader.ComponentData;
 using TUGraz.VectoCore.Models.Declaration;
 using TUGraz.VectoCore.Models.SimulationComponent.Data;
 using TUGraz.VectoCore.Models.SimulationComponent.Data.ElectricMotor;
+using TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox;
+using TUGraz.VectoCore.Models.SimulationComponent.Impl;
 using TUGraz.VectoCore.Utils;
 
 
@@ -17,14 +19,17 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 	{
 		#region Constant
 
-		public const double GearEfficiency = 0.95;
+		public const double GenericGearEfficiency = 0.96;
+		public const double GenericAxleGearEfficiency = 0.96;
 
 		#endregion
 
-		private double axleEfficiency;
-		private Dictionary<int, double> gearRatios;
-		private double axleRatio;
-		private KeyValuePair<int, double> gearRatioAtMeasurement;
+		private readonly GenericTransmissionComponentData _genericTransmission = new GenericTransmissionComponentData();
+
+		private double _axleEfficiency;
+		private Dictionary<int, double> _gearRatios;
+		private double _axleRatio;
+		private KeyValuePair<int, double> _gearRatioAtMeasurement;
 
 		public GenericBusIEPCData()
 		{
@@ -34,9 +39,9 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 				$"{DeclarationData.DeclarationDataResourcePrefix}.GenericBusData.EfficiencyMap_IEPC_PSM_normalized.vmap";
 		}
 
-		public IEPCElectricMotorData CreateIEPCElectricMotorData(IIEPCDeclarationInputData iepcData, IAxleGearInputData axleGearData)
+		public IEPCElectricMotorData CreateIEPCElectricMotorData(IIEPCDeclarationInputData iepcData)
 		{
-			InitData(iepcData, axleGearData);
+			InitData(iepcData);
 
 			var count = iepcData.DesignTypeWheelMotor && iepcData.NrOfDesignTypeWheelMotorMeasured == 1 ? 2 : 1; //? also for declaration mode valid
 			
@@ -44,33 +49,32 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 				IEPCDragCurves = GetIEPCDragCurves(iepcData, count),
 				EfficiencyData = GetIEPCVoltageLevelData(iepcData.VoltageLevels, count, iepcData.ElectricMachineType),
 				Inertia = iepcData.Inertia * count,
-				RatioPerGear = gearRatios.Select(x => x.Value).ToArray()
-				//iepcEM.OverloadRegenerationFactor
+				RatioPerGear = _gearRatios.Select(x => x.Value).ToArray(),
 				//iepcEM.RatioADC
-				//iepcEM.TransmissionLossMap
-				//iepcEM.EMDragCurve
-				//iepcEM.Overload
-			};
+                //iepcEM.TransmissionLossMap
+                //iepcEM.EMDragCurve
+                //iepcEM.Overload
+            };
 			
 			return iepcEM;
 		}
 
-		private void InitData(IIEPCDeclarationInputData iepcData, IAxleGearInputData axleGear)
+		private void InitData(IIEPCDeclarationInputData iepcData)
 		{
-			if (!iepcData.DifferentialIncluded) {
-				axleEfficiency = 1;
-				axleRatio = 1;
+			if (iepcData.DifferentialIncluded) {
+				_axleEfficiency = GenericAxleGearEfficiency;
+				_axleRatio = 1;
 			} else {
-				axleEfficiency = axleGear.Efficiency;
-				axleRatio = axleGear.Ratio;
+				_axleEfficiency = 1;
+				_axleRatio = 1;
 			}
 
-			gearRatios = new Dictionary<int, double>();
+			_gearRatios = new Dictionary<int, double>();
 			foreach (var gear in iepcData.Gears.OrderBy(x => x.GearNumber)) {
-				gearRatios.Add(gear.GearNumber, gear.Ratio);
+				_gearRatios.Add(gear.GearNumber, gear.Ratio);
 			}
 
-			gearRatioAtMeasurement = GetGearRatioAtMeasurement();
+			_gearRatioAtMeasurement = GetGearRatioAtMeasurement();
 		}
 
 		private VoltageLevelData GetIEPCVoltageLevelData(IList<IElectricMotorVoltageLevel> voltageLevels, int count, ElectricMachineType electricMachineType)
@@ -99,7 +103,7 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 		private ElectricMotorFullLoadCurve GetElectricMotorFullLoadCurve(IElectricMotorVoltageLevel voltageLevel,
 			int count)
 		{
-			return IEPCFullLoadCurveReader.Create(voltageLevel.FullLoadCurve, count, gearRatioAtMeasurement.Value);
+			return IEPCFullLoadCurveReader.Create(voltageLevel.FullLoadCurve, count, _gearRatioAtMeasurement.Value);
 		}
 
 		
@@ -108,12 +112,12 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 		{
 			var result = new Dictionary<uint, EfficiencyMap>();
 
-			foreach (var gearEntry in gearRatios) {
+			foreach (var gearEntry in _gearRatios) {
 
 				var gearRatio = gearEntry.Value;
 				
 				var ratedPoint = GenericRatedPointHelper.GetRatedPointOfFullLoadCurveAtIEPC(voltageLevel.FullLoadCurve,
-					axleRatio, gearRatioAtMeasurement.Value, GearEfficiency, axleEfficiency);
+					_axleRatio, _gearRatioAtMeasurement.Value, GenericGearEfficiency, _axleEfficiency);
 				
 				var deNormalizedMap = DeNormalizeData(GetNormalizedEfficiencyMap(electricMachineType), ratedPoint, gearRatio);
 				result.Add((uint) gearEntry.Key, IEPCMapReader.Create(deNormalizedMap, count, gearRatio, fullLoadCurve));
@@ -142,7 +146,7 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 
 		private KeyValuePair<int, double> GetGearRatioAtMeasurement()
 		{
-			var gear = gearRatios.Select(x => new {
+			var gear = _gearRatios.Select(x => new {
 				Gear = x.Key,
 				Ratio = x.Value,
 				Distance = Math.Abs(x.Value - 1)
@@ -161,13 +165,13 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 
 			foreach (DataRow row in normalizedMap.Rows) {
 				var torqueNormValue = row.ParseDouble(TorqueNorm);
-				var motorSpeed = row.ParseDouble(MotorSpeedNorm) * ratedPoint.NRated / gearRatio / axleRatio;
-				var torque = torqueNormValue * ratedPoint.TRated * gearRatio * axleRatio * 
-							( torqueNormValue > 0 ? GearEfficiency * axleEfficiency : 1 / GearEfficiency * axleEfficiency);
+				var motorSpeed = row.ParseDouble(MotorSpeedNorm) * ratedPoint.NRated / gearRatio / _axleRatio;
+				var torque = torqueNormValue * ratedPoint.TRated * gearRatio * _axleRatio * 
+							( torqueNormValue > 0 ? GenericGearEfficiency * _axleEfficiency : 1 / (GenericGearEfficiency * _axleEfficiency));
 				var powerElectrical = row.ParseDouble(PowerElectricalNorm) * ratedPoint.PRated;
 				
 				var newRow = result.NewRow();
-				newRow[ElectricMotorMapReader.Fields.MotorSpeed] = Math.Round(motorSpeed.Value(), 2, MidpointRounding.AwayFromZero).ToXMLFormat(2);
+				newRow[ElectricMotorMapReader.Fields.MotorSpeed] = Math.Round(motorSpeed.AsRPM, 2, MidpointRounding.AwayFromZero).ToXMLFormat(2);
 				newRow[ElectricMotorMapReader.Fields.Torque] = Math.Round(torque.Value(), 2, MidpointRounding.AwayFromZero).ToXMLFormat(2);
 				newRow[ElectricMotorMapReader.Fields.PowerElectrical] = Math.Round(powerElectrical.Value(), 2, MidpointRounding.AwayFromZero).ToXMLFormat(2);
 				result.Rows.Add(newRow);
@@ -175,5 +179,67 @@ namespace TUGraz.VectoCore.Models.GenericModelData
 
 			return result;
 		}
-	}
+
+		private OverloadData CalculateOverloadData(IIEPCDeclarationInputData iepc, int count,
+			VoltageLevelData voltageLevel, Volt averageVoltage, Tuple<uint, double> gearRatioUsedForMeasurement)
+		{
+
+			// if average voltage is outside of the voltage-level range, do not extrapolate but take the min voltage entry, or max voltage entry
+			if (averageVoltage < iepc.VoltageLevels.Min(x => x.VoltageLevel))
+			{
+				return CalculateOverloadBuffer(iepc.VoltageLevels.First(), count, voltageLevel, gearRatioUsedForMeasurement);
+			}
+			if (averageVoltage > iepc.VoltageLevels.Max(x => x.VoltageLevel))
+			{
+				return CalculateOverloadBuffer(iepc.VoltageLevels.Last(), count, voltageLevel, gearRatioUsedForMeasurement);
+			}
+
+			var (vLow, vHigh) = iepc.VoltageLevels.OrderBy(x => x.VoltageLevel).GetSection(x => x.VoltageLevel < averageVoltage);
+			var ovlLo = CalculateOverloadBuffer(vLow, count, voltageLevel, gearRatioUsedForMeasurement);
+			var ovlHi = CalculateOverloadBuffer(vHigh, count, voltageLevel, gearRatioUsedForMeasurement);
+
+			var retVal = new OverloadData()
+			{
+				OverloadBuffer = VectoMath.Interpolate(vLow.VoltageLevel, vHigh.VoltageLevel, ovlLo.OverloadBuffer, ovlHi.OverloadBuffer, averageVoltage),
+				ContinuousTorque = VectoMath.Interpolate(vLow.VoltageLevel, vHigh.VoltageLevel, ovlLo.ContinuousTorque, ovlHi.ContinuousTorque, averageVoltage),
+				ContinuousPowerLoss = VectoMath.Interpolate(vLow.VoltageLevel, vHigh.VoltageLevel, ovlLo.ContinuousPowerLoss, ovlHi.ContinuousPowerLoss, averageVoltage)
+			};
+			return retVal;
+		}
+
+
+        private OverloadData CalculateOverloadBuffer(IElectricMotorVoltageLevel voltageEntry,
+            int count, VoltageLevelData voltageLevel, Tuple<uint, double> gearUsedForMeasurement = null)
+        {
+            var gearRatioUsedForMeasurement = gearUsedForMeasurement?.Item2 ?? 1.0;
+            var gear = new GearshiftPosition(gearUsedForMeasurement?.Item1 ?? 1);
+            var continuousTorque = voltageEntry.ContinuousTorque * count / gearRatioUsedForMeasurement;
+            var continuousTorqueSpeed = voltageEntry.ContinuousTorqueSpeed * gearRatioUsedForMeasurement;
+            var overloadTorque = (voltageEntry.OverloadTorque ?? 0.SI<NewtonMeter>()) * count / gearRatioUsedForMeasurement;
+            var overloadTestSpeed = (voltageEntry.OverloadTestSpeed ?? 0.RPMtoRad()) * gearRatioUsedForMeasurement;
+
+
+            var peakElPwr = voltageLevel.LookupElectricPower(voltageEntry.VoltageLevel,
+                    overloadTestSpeed,
+                    -overloadTorque,
+                    gear,
+                    true)
+                    .ElectricalPower;
+            var peakPwrLoss = -peakElPwr - overloadTorque * overloadTestSpeed; // losses need to be positive
+
+            var contElPwr = voltageLevel.LookupElectricPower(voltageEntry.VoltageLevel, continuousTorqueSpeed,
+                                -continuousTorque, gear).ElectricalPower ??
+                            voltageLevel.LookupElectricPower(voltageEntry.VoltageLevel, continuousTorqueSpeed,
+                                voltageLevel.FullLoadDriveTorque(voltageEntry.VoltageLevel, continuousTorqueSpeed),
+                                gear, true).ElectricalPower;
+            var continuousPowerLoss = -contElPwr - continuousTorque * continuousTorqueSpeed; // loss needs to be positive
+            var overloadBuffer = (peakPwrLoss - continuousPowerLoss) * voltageEntry.OverloadTime;
+            return new OverloadData()
+            {
+                OverloadBuffer = overloadBuffer,
+                ContinuousTorque = continuousTorque,
+                ContinuousPowerLoss = continuousPowerLoss
+            };
+        }
+    }
 }
-- 
GitLab