From e1473aa5313b32101c59a3200a3a25c13e197547 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Mon, 7 Dec 2020 15:31:51 +0100
Subject: [PATCH] bugfix em-off signal, em-off time share, ice-off time share,
 max startgear selection

---
 .../Simulation/Data/ModalResultField.cs       |  2 +-
 .../SimulationComponent/Impl/ElectricMotor.cs |  6 +--
 .../Impl/HybridController.cs                  |  3 +-
 .../Strategies/HybridStrategy.cs              | 38 +++++++++----------
 .../OutputData/IModalDataContainer.cs         |  4 +-
 5 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs b/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
index bd64deffcb..28b329b9c3 100644
--- a/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
@@ -385,7 +385,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Data
 
 		[ModalResultField(typeof(SI), caption: "T_{0}-em_drive_max [Nm]")] T_EM_electricMotor_drive_max_,
 		[ModalResultField(typeof(SI), caption: "T_{0}-em_gen_max [Nm]")] T_EM_electricMotor_gen_max_,
-		[ModalResultField(typeof(SI), caption: "P_{0}-em_gen_max_ [kW]", outputFactor: 1e-3)] P_EM_electricMotor_gen_max_,
+		[ModalResultField(typeof(SI), caption: "P_{0}-em_gen_max [kW]", outputFactor: 1e-3)] P_EM_electricMotor_gen_max_,
 		[ModalResultField(typeof(SI), caption: "P_{0}-em_drive_max [kW]", outputFactor: 1e-3)] P_EM_electricMotor_drive_max_,
 		
 		[ModalResultField(typeof(SI), caption: "P_{0}-em_mech [kW]", outputFactor: 1e-3)] P_EM_electricMotor_em_mech_,
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
index 6a34b07168..2cebd5bfbd 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
@@ -299,7 +299,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			
 			if (!dryRun) {
 				CurrentState.EMSpeed = emSpeed;
-				CurrentState.EMTorque = emTorque;
+				CurrentState.EMTorque = emOff ? null : emTorque;
 				CurrentState.EmTorqueMap = emTorqueMap;
 				CurrentState.DragMax = maxRecuperationTorqueEmMap;
 				CurrentState.DriveMax = maxDriveTorqueEmMap;
@@ -391,7 +391,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			container[ModalResultField.P_EM_electricMotor_gen_max_, Position] = (CurrentState.DragMax ?? 0.SI<NewtonMeter>()) * avgEMSpeed;
 			container[ModalResultField.P_EM_electricMotor_drive_max_, Position] = (CurrentState.DriveMax ?? 0.SI<NewtonMeter>()) * avgEMSpeed;
 			
-			container[ModalResultField.P_EM_electricMotor_em_mech_, Position] = CurrentState.EMTorque * avgEMSpeed;
+			container[ModalResultField.P_EM_electricMotor_em_mech_, Position] = (CurrentState.EMTorque ?? 0.SI<NewtonMeter>() ) * avgEMSpeed;
 			container[ModalResultField.P_EM_electricMotor_em_mech_map_, Position] = (CurrentState.EmTorqueMap ?? 0.SI<NewtonMeter>()) * avgEMSpeed;
 
 
@@ -419,7 +419,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		protected override void DoCommitSimulationStep(Second time, Second simulationInterval)
 		{
 			var avgSpeed = (PreviousState.EMSpeed + CurrentState.EMSpeed) / 2;
-			var losses = CurrentState.EMTorque * avgSpeed - CurrentState.ElectricPowerToBattery;
+			var losses = (CurrentState.EMTorque ?? 0.SI<NewtonMeter>()) * avgSpeed - CurrentState.ElectricPowerToBattery;
 			ThermalBuffer += (losses - ContinuousPowerLoss) * simulationInterval;
 			if (ThermalBuffer < 0) {
 				ThermalBuffer = 0.SI<Joule>();
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/HybridController.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/HybridController.cs
index ae1aeb50aa..2cfc50a669 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/HybridController.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/HybridController.cs
@@ -318,7 +318,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 										runData.VehicleData.DynamicTyreRadius;
 				var minEngineSpeed = (runData.EngineData.FullLoadCurves[0].RatedSpeed - runData.EngineData.IdleSpeed) *
 					Constants.SimulationSettings.ClutchClosingSpeedNorm + runData.EngineData.IdleSpeed;
-				foreach (var gear in GearList.Reverse()) {
+                MaxStartGear = GearList.First();
+                foreach (var gear in GearList.Reverse()) {
 					var gearData = GearboxModelData.Gears[gear.Gear];
 					if (GearshiftParams.StartSpeed * transmissionRatio * gearData.Ratio <= minEngineSpeed)
 						continue;
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
index e790d82f2d..e05f8ef8b7 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
@@ -1529,25 +1529,25 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 			container[ModalResultField.HybridStrategyScore] = (CurrentState.Solution?.Score ?? 0)/1e3;
 			container[ModalResultField.HybridStrategySolution] = CurrentState.Solution?.U ?? -100;
 
-			//if (CurrentState.Evaluations != null) {
-			//	container.SetDataValue(
-			//		"HybridStrategyEvaluation",
-			//		string.Join(
-			//			" | ", CurrentState.Evaluations.Select(
-			//				x => {
-			//					var foo = string.Join(" ",  x.Setting.MechanicalAssistPower.Select(e => $"{e.Key.GetName()} - {e.Value}"));
-			//					var ice = "====";
-			//					if (x.Response != null) {
-			//						ice =
-			//							$"{x.Response.Engine.TorqueOutDemand}, {x.Response.Engine.TotalTorqueDemand}, {x.Response.Engine.DynamicFullLoadTorque}";
-			//					}
-			//					return
-			//						$"{x.U:F2}: {x.Score:F2}; G{x.Gear}; ({x.FuelCosts:F2} + {x.EqualityFactor:F2} * ({x.BatCosts:F2} + {x.ICEStartPenalty1:F2}) * {x.SoCPenalty:F2} + {x.ICEStartPenalty2:F2}) / {x.GearshiftPenalty:F2} = {x.Score:F2} ({foo} ICE: {ice}); {x.IgnoreReason.HumanReadable()}";
-			//				})
-			//			)
-			//		);
-			//}
-		}
+            //if (CurrentState.Evaluations != null) {
+            //    container.SetDataValue(
+            //        "HybridStrategyEvaluation",
+            //        string.Join(
+            //            " | ", CurrentState.Evaluations.Select(
+            //                x => {
+            //                    var foo = string.Join(" ", x.Setting.MechanicalAssistPower.Select(e => $"{e.Key.GetName()} - {e.Value}"));
+            //                    var ice = "====";
+            //                    if (x.Response != null) {
+            //                        ice =
+            //                            $"{x.Response.Engine.TorqueOutDemand}, {x.Response.Engine.TotalTorqueDemand}, {x.Response.Engine.DynamicFullLoadTorque}";
+            //                    }
+            //                    return
+            //                        $"{x.U:F2}: {x.Score:F2}; G{x.Gear}; ({x.FuelCosts:F2} + {x.EqualityFactor:F2} * ({x.BatCosts:F2} + {x.ICEStartPenalty1:F2}) * {x.SoCPenalty:F2} + {x.ICEStartPenalty2:F2}) / {x.GearshiftPenalty:F2} = {x.Score:F2} ({foo} ICE: {ice}); {x.IgnoreReason.HumanReadable()}";
+            //                })
+            //            )
+            //        );
+            //}
+        }
 
 		
 	}
diff --git a/VectoCore/VectoCore/OutputData/IModalDataContainer.cs b/VectoCore/VectoCore/OutputData/IModalDataContainer.cs
index 10cffcb251..5ce09c8f97 100644
--- a/VectoCore/VectoCore/OutputData/IModalDataContainer.cs
+++ b/VectoCore/VectoCore/OutputData/IModalDataContainer.cs
@@ -552,7 +552,7 @@ namespace TUGraz.VectoCore.OutputData
 		public static Scalar ICEOffTimeShare(this IModalDataContainer data)
 		{
 			var iceOn = data.GetValues(x => new {
-				dt = x[ModalResultField.ICEOn.GetName()] is DBNull
+				dt = x[ModalResultField.ICEOn.GetName()] is DBNull || !x.Field<Boolean>(ModalResultField.ICEOn.GetName())
 					? 0.SI<Second>()
 					: x.Field<Second>(ModalResultField.simulationInterval.GetName())
 			}).Sum(x => x.dt) ?? 0.SI<Second>();
@@ -562,7 +562,7 @@ namespace TUGraz.VectoCore.OutputData
 		public static Scalar ElectricMotorOffTimeShare(this IModalDataContainer data, PowertrainPosition pos)
 		{
 			var emOff = data.GetValues(x => new {
-				dt = x[string.Format(ModalResultField.EM_Off_.GetCaption(), pos.GetName())] is DBNull
+				dt = x[string.Format(ModalResultField.EM_Off_.GetCaption(), pos.GetName())] is DBNull || !x.Field<Scalar>(string.Format(ModalResultField.EM_Off_.GetCaption(), pos.GetName())).IsEqual(1)
 					? 0.SI<Second>()
 					: x.Field<Second>(ModalResultField.simulationInterval.GetName())
 			}).Sum(x => x.dt) ?? 0.SI<Second>();
-- 
GitLab