From c74463e937069125ee51ffcfe7cc15030b58f014 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Fri, 10 Jun 2016 10:09:56 +0200
Subject: [PATCH] adding estimated axle losses (via databus), change gearbox
 losses to use value from previous state, refactoring of coastingdistance
 computation (renaming variables)

---
 .../Simulation/DataBus/IAxlegearInfo.cs       |  9 ++++
 .../Models/Simulation/DataBus/IDataBus.cs     |  3 +-
 .../Models/Simulation/DataBus/IGearboxInfo.cs |  2 +-
 .../Simulation/Impl/VehicleContainer.cs       | 11 +++-
 .../Models/SimulationComponent/IAxlegear.cs   |  6 +++
 .../SimulationComponent/Impl/AxleGear.cs      | 13 ++++-
 .../SimulationComponent/Impl/CycleGearbox.cs  | 11 ++--
 .../Impl/DefaultDriverStrategy.cs             | 52 ++++++++-----------
 .../SimulationComponent/Impl/Gearbox.cs       | 10 ++--
 VectoCore/VectoCore/VectoCore.csproj          |  2 +
 .../Models/Declaration/ShiftPolygonTest.cs    |  5 +-
 .../Simulation/LACDecisionFactorTest.cs       | 11 ++--
 VectoCore/VectoCoreTest/Utils/MockGearbox.cs  |  2 +-
 .../Utils/MockVairVehicleContainer.cs         |  7 ++-
 14 files changed, 93 insertions(+), 51 deletions(-)
 create mode 100644 VectoCore/VectoCore/Models/Simulation/DataBus/IAxlegearInfo.cs
 create mode 100644 VectoCore/VectoCore/Models/SimulationComponent/IAxlegear.cs

diff --git a/VectoCore/VectoCore/Models/Simulation/DataBus/IAxlegearInfo.cs b/VectoCore/VectoCore/Models/Simulation/DataBus/IAxlegearInfo.cs
new file mode 100644
index 0000000000..955d81583c
--- /dev/null
+++ b/VectoCore/VectoCore/Models/Simulation/DataBus/IAxlegearInfo.cs
@@ -0,0 +1,9 @@
+using TUGraz.VectoCommon.Utils;
+
+namespace TUGraz.VectoCore.Models.Simulation.DataBus
+{
+	public interface IAxlegearInfo
+	{
+		Watt AxlegearLoss();
+	}
+}
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/Simulation/DataBus/IDataBus.cs b/VectoCore/VectoCore/Models/Simulation/DataBus/IDataBus.cs
index 3f96821e2a..cba61a715b 100644
--- a/VectoCore/VectoCore/Models/Simulation/DataBus/IDataBus.cs
+++ b/VectoCore/VectoCore/Models/Simulation/DataBus/IDataBus.cs
@@ -37,7 +37,8 @@ namespace TUGraz.VectoCore.Models.Simulation.DataBus
 	/// <summary>
 	/// Defines interfaces for all different cockpits to access shared data of the powertrain.
 	/// </summary>
-	public interface IDataBus : IGearboxInfo, IEngineInfo, IVehicleInfo, IMileageCounter, IClutchInfo, IBrakes,
+	public interface IDataBus : IGearboxInfo, IAxlegearInfo, IEngineInfo, IVehicleInfo, IMileageCounter, IClutchInfo,
+		IBrakes,
 		IRoadLookAhead, IDriverInfo, IDrivingCycleInfo
 	{
 		ExecutionMode ExecutionMode { get; set; }
diff --git a/VectoCore/VectoCore/Models/Simulation/DataBus/IGearboxInfo.cs b/VectoCore/VectoCore/Models/Simulation/DataBus/IGearboxInfo.cs
index a675388706..3de4244ed2 100644
--- a/VectoCore/VectoCore/Models/Simulation/DataBus/IGearboxInfo.cs
+++ b/VectoCore/VectoCore/Models/Simulation/DataBus/IGearboxInfo.cs
@@ -51,6 +51,6 @@ namespace TUGraz.VectoCore.Models.Simulation.DataBus
 
 		FullLoadCurve GearFullLoadCurve { get; }
 
-		Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque);
+		Watt GearboxLoss();
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
index 3dd0227bf3..14d75f6d83 100644
--- a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
@@ -53,6 +53,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 
 		internal IEngineInfo Engine;
 		internal IGearboxInfo Gearbox;
+		internal IAxlegearInfo Axlegear;
 		internal IVehicleInfo Vehicle;
 		internal IBrakes Brakes;
 		internal IDriverInfo Driver;
@@ -116,9 +117,9 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 			get { return Gearbox != null ? Gearbox.GearFullLoadCurve : null; }
 		}
 
-		public Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque)
+		public Watt GearboxLoss()
 		{
-			return Gearbox.GearboxLoss(inAngularVelocity, inTorque);
+			return Gearbox.GearboxLoss();
 		}
 
 		#endregion
@@ -233,6 +234,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 					Gearbox = c;
 					commitPriority = 4;
 				})
+				.If<IAxlegearInfo>(c => Axlegear = c)
 				.If<IVehicleInfo>(c => {
 					Vehicle = c;
 					commitPriority = 5;
@@ -348,5 +350,10 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 		{
 			get { return DrivingCycle.Altitude; }
 		}
+
+		public Watt AxlegearLoss()
+		{
+			return Axlegear.AxlegearLoss();
+		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/IAxlegear.cs b/VectoCore/VectoCore/Models/SimulationComponent/IAxlegear.cs
new file mode 100644
index 0000000000..9fbe29b51c
--- /dev/null
+++ b/VectoCore/VectoCore/Models/SimulationComponent/IAxlegear.cs
@@ -0,0 +1,6 @@
+using TUGraz.VectoCore.Models.Simulation.DataBus;
+
+namespace TUGraz.VectoCore.Models.SimulationComponent
+{
+	public interface IAxlegear : IPowerTrainComponent, IAxlegearInfo {}
+}
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/AxleGear.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/AxleGear.cs
index f8fa67be3d..f8c66aa9c3 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/AxleGear.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/AxleGear.cs
@@ -40,7 +40,7 @@ using TUGraz.VectoCore.OutputData;
 
 namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 {
-	public class AxleGear : StatefulVectoSimulationComponent<SimpleComponentState>, IPowerTrainComponent, ITnInPort,
+	public class AxleGear : StatefulVectoSimulationComponent<AxleGear.AxlegearState>, IAxlegear, ITnInPort,
 		ITnOutPort
 	{
 		protected ITnOutPort NextComponent;
@@ -79,6 +79,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var inTorque = torque / ModelData.AxleGear.Ratio + torqueLoss;
 
 			CurrentState.SetState(inTorque, inAngularVelocity, torque, angularVelocity);
+			CurrentState.TransmissionTorqueLoss = torqueLoss;
 
 			var retVal = NextComponent.Request(absTime, dt, inTorque, inAngularVelocity, dryRun);
 
@@ -119,5 +120,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			}
 			AdvanceState();
 		}
+
+		public Watt AxlegearLoss()
+		{
+			return (PreviousState.TransmissionTorqueLoss) * PreviousState.InAngularVelocity;
+		}
+
+		public class AxlegearState : SimpleComponentState
+		{
+			public NewtonMeter TransmissionTorqueLoss = 0.SI<NewtonMeter>();
+		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CycleGearbox.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CycleGearbox.cs
index 6a32160a24..21a091f487 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CycleGearbox.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CycleGearbox.cs
@@ -112,12 +112,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			get { return Gear == 0 ? null : ModelData.Gears[Gear].FullLoadCurve; }
 		}
 
-		public Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque)
+		public Watt GearboxLoss()
 		{
-			var outTorque = ModelData.Gears[Gear].LossMap.GetOutTorque(inAngularVelocity, inTorque, true);
-			var torqueLoss = inTorque - outTorque * ModelData.Gears[Gear].Ratio;
+			//var outTorque = ModelData.Gears[Gear].LossMap.GetOutTorque(inAngularVelocity, inTorque, true);
+			//var torqueLoss = inTorque - outTorque * ModelData.Gears[Gear].Ratio;
 
-			return torqueLoss * inAngularVelocity;
+			//return torqueLoss * inAngularVelocity;
+
+			return (PreviousState.TransmissionTorqueLoss +
+					PreviousState.InertiaTorqueLossOut / ModelData.Gears[PreviousState.Gear].Ratio) * PreviousState.InAngularVelocity;
 		}
 
 		#endregion
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
index c2ebce67e7..da4bd3f849 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
@@ -330,47 +330,39 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			return nextBrakingAction;
 		}
 
-		protected internal virtual Meter ComputeCoastingDistance(MeterPerSecond v_veh,
+		protected internal virtual Meter ComputeCoastingDistance(MeterPerSecond vehicleSpeed,
 			DrivingCycleData.DrivingCycleEntry actionEntry)
 		{
-			var v_target = OverspeedAllowed(actionEntry.RoadGradient, actionEntry.VehicleTargetSpeed)
+			var targetSpeed = OverspeedAllowed(actionEntry.RoadGradient, actionEntry.VehicleTargetSpeed)
 				? actionEntry.VehicleTargetSpeed + Driver.DriverData.OverSpeedEcoRoll.OverSpeed
 				: actionEntry.VehicleTargetSpeed;
 
-			var m = Driver.DataBus.TotalMass;
-			var g = Physics.GravityAccelleration;
-			var h_target = actionEntry.Altitude; //dec.Altitude;
+			var vehicleMass = Driver.DataBus.TotalMass;
+			var targetAltitude = actionEntry.Altitude; //dec.Altitude;
 
-			var h_vehicle = Driver.DataBus.Altitude;
+			var vehicleAltitude = Driver.DataBus.Altitude;
 
-			var E_kin_veh = m * v_veh * v_veh / 2;
-			var E_kin_target = m * v_target * v_target / 2;
-			var E_pot_target = m * g * h_target;
-			var E_pot_veh = m * g * h_vehicle;
+			var targetEnergy = vehicleMass * Physics.GravityAccelleration * targetAltitude +
+								vehicleMass * targetSpeed * targetSpeed / 2;
+			var vehicleEnergy = vehicleMass * Physics.GravityAccelleration * vehicleAltitude +
+								vehicleMass * vehicleSpeed * vehicleSpeed / 2;
 
-			var delta_E = (E_kin_veh + E_pot_veh) - (E_kin_target + E_pot_target);
+			var energyDifference = vehicleEnergy - targetEnergy;
 
-			//var F_dec_average = delta_E / x_delta;
+			var airDragForce = Driver.DataBus.AirDragResistance(vehicleSpeed, targetSpeed);
+			var rollResistanceForce = Driver.DataBus.RollingResistance(
+				((targetAltitude - vehicleAltitude) / (actionEntry.Distance - Driver.DataBus.Distance)).Value().SI<Radian>());
+			var engineDragLoss = Driver.DataBus.EngineDragPower(Driver.DataBus.EngineSpeed);
+			var gearboxLoss = Driver.DataBus.GearboxLoss();
+			var axleLoss = Driver.DataBus.AxlegearLoss();
 
-			var F_air = Driver.DataBus.AirDragResistance(v_veh, v_target);
-			var F_roll =
-				Driver.DataBus.RollingResistance(
-					((h_target - h_vehicle) / (actionEntry.Distance - Driver.DataBus.Distance)).Value().SI<Radian>());
-			var F_enginedrag = Driver.DataBus.EngineDragPower(Driver.DataBus.EngineSpeed) / v_veh;
-			var F_loss_gb = Driver.DataBus.GearboxLoss(Driver.DataBus.EngineSpeed, Driver.DataBus.EngineTorque) / v_veh;
+			var coastingResistanceForce = airDragForce + rollResistanceForce +
+										(gearboxLoss + axleLoss - engineDragLoss) / vehicleSpeed;
 
-			// todo mk-2016-05-11 calculate ra loss
-			var F_loss_ra = 0.SI<Newton>();
-
-			var F_coasting = F_air + F_roll + F_loss_gb + F_loss_ra - F_enginedrag;
-
-			//var CDP = F_dec_average / -F_coasting;
-
-			var DF_coasting = Driver.DriverData.LookAheadCoasting.LookAheadDecisionFactor.Lookup(v_target, v_veh - v_target);
-
-			var delta_x = (delta_E / (DF_coasting * F_coasting)).Cast<Meter>();
-
-			return delta_x;
+			var coastingDecisionFactor = Driver.DriverData.LookAheadCoasting.LookAheadDecisionFactor.Lookup(targetSpeed,
+				vehicleSpeed - targetSpeed);
+			var coastingDistance = (energyDifference / (coastingDecisionFactor * coastingResistanceForce)).Cast<Meter>();
+			return coastingDistance;
 		}
 
 
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
index d20b7faee1..e5faa54d24 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
@@ -128,12 +128,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			get { return Gear == 0 ? null : ModelData.Gears[Gear].FullLoadCurve; }
 		}
 
-		public Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque)
+		public Watt GearboxLoss()
 		{
-			var outTorque = ModelData.Gears[Gear].LossMap.GetOutTorque(inAngularVelocity, inTorque, true);
-			var torqueLoss = inTorque - outTorque * ModelData.Gears[Gear].Ratio;
+			//var outTorque = ModelData.Gears[Gear].LossMap.GetOutTorque(inAngularVelocity, inTorque, true);
+			//var torqueLoss = inTorque - outTorque * ModelData.Gears[Gear].Ratio;
 
-			return torqueLoss * inAngularVelocity;
+			//return torqueLoss * inAngularVelocity;
+			return (PreviousState.TransmissionTorqueLoss +
+					PreviousState.InertiaTorqueLossOut / ModelData.Gears[PreviousState.Gear].Ratio) * PreviousState.InAngularVelocity;
 		}
 
 		#endregion
diff --git a/VectoCore/VectoCore/VectoCore.csproj b/VectoCore/VectoCore/VectoCore.csproj
index 5cea0c4b85..840900d991 100644
--- a/VectoCore/VectoCore/VectoCore.csproj
+++ b/VectoCore/VectoCore/VectoCore.csproj
@@ -162,6 +162,7 @@
     <Compile Include="Models\SimulationComponent\Data\ICrossWindCorrection.cs" />
     <Compile Include="Models\SimulationComponent\Data\CrosswindCorrectionVAirBeta.cs" />
     <Compile Include="Models\SimulationComponent\IAuxiliary.cs" />
+    <Compile Include="Models\SimulationComponent\IAxlegear.cs" />
     <Compile Include="Models\SimulationComponent\IBrakes.cs" />
     <Compile Include="Models\SimulationComponent\ICombustionEngineIdleController.cs" />
     <Compile Include="Models\SimulationComponent\IDriverActions.cs" />
@@ -178,6 +179,7 @@
     <Compile Include="Models\SimulationComponent\Impl\CycleClutch.cs" />
     <Compile Include="Models\SimulationComponent\IShiftStrategy.cs" />
     <Compile Include="Models\SimulationComponent\Impl\ShiftStrategy.cs" />
+    <Compile Include="Models\Simulation\DataBus\IAxlegearInfo.cs" />
     <Compile Include="Models\Simulation\DataBus\IClutchInfo.cs" />
     <Compile Include="Models\Simulation\DataBus\IDriverInfo.cs" />
     <Compile Include="Models\Simulation\Data\AuxiliaryDemandType.cs" />
diff --git a/VectoCore/VectoCoreTest/Models/Declaration/ShiftPolygonTest.cs b/VectoCore/VectoCoreTest/Models/Declaration/ShiftPolygonTest.cs
index 8f668c1e9d..99d82a1d7c 100644
--- a/VectoCore/VectoCoreTest/Models/Declaration/ShiftPolygonTest.cs
+++ b/VectoCore/VectoCoreTest/Models/Declaration/ShiftPolygonTest.cs
@@ -377,14 +377,15 @@ namespace TUGraz.VectoCore.Tests.Models.Declaration
 		{
 			//var engineFldFile = @"E:\QUAM\Downloads\EngineFLD\Map_375c_BB1390_modTUG_R49_375c_BB1386.vfld";
 			var engineFldFile = @"E:\QUAM\tmp\scania_fullload_shiftpolygon-test.csv";
-			var gearboxFile = @"TestData\Components\40t_Long_Haul_Truck.vgbx";
+			var gearboxFile = @"E:\QUAM\Downloads\TUG_dev_gbx\TUG_dev\GRS905R.vgbx";
+			//@"TestData\Components\40t_Long_Haul_Truck.vgbx";
 
 			if (!File.Exists(engineFldFile)) {
 				Assert.Inconclusive("Confidential File not found. Test cannot run without file.");
 			}
 
 			var rdyn = 0.4882675.SI<Meter>();
-			var axlegearRatio = 2.31; // 3.71; //2.59;
+			var axlegearRatio = 3.71; //2.31; // 3.71; //2.59;
 
 			var engineData = new CombustionEngineData() {
 				IdleSpeed = 509.RPMtoRad(),
diff --git a/VectoCore/VectoCoreTest/Models/Simulation/LACDecisionFactorTest.cs b/VectoCore/VectoCoreTest/Models/Simulation/LACDecisionFactorTest.cs
index 39a76e1eb8..c6ff1ef7dd 100644
--- a/VectoCore/VectoCoreTest/Models/Simulation/LACDecisionFactorTest.cs
+++ b/VectoCore/VectoCoreTest/Models/Simulation/LACDecisionFactorTest.cs
@@ -15,15 +15,18 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 				vVehicle < 100.KMPHtoMeterPerSecond();
 				vVehicle += 1.KMPHtoMeterPerSecond()) {
 				for (var vTarget = vVehicle; vTarget > 0; vTarget -= 1.KMPHtoMeterPerSecond()) {
-					var df_coast = LACDecisionFactor.Lookup(vTarget, vVehicle - vTarget);
-					if (vTarget < 48.KMPHtoMeterPerSecond())
+					var df_coast = new LACDecisionFactor().Lookup(vTarget, vVehicle - vTarget);
+					if (vTarget < 48.KMPHtoMeterPerSecond()) {
 						AssertHelper.AreRelativeEqual(df_coast, 2.5, string.Format("vVehicle: {0}, vTarget: {1}", vVehicle, vTarget));
+					}
 
-					if (vVehicle - vTarget > 11)
+					if (vVehicle - vTarget > 11) {
 						AssertHelper.AreRelativeEqual(df_coast, 2.5, string.Format("vVehicle: {0}, vTarget: {1}", vVehicle, vTarget));
+					}
 
-					if (vTarget > 52.KMPHtoMeterPerSecond() && vVehicle - vTarget < 9.KMPHtoMeterPerSecond())
+					if (vTarget > 52.KMPHtoMeterPerSecond() && vVehicle - vTarget < 9.KMPHtoMeterPerSecond()) {
 						AssertHelper.AreRelativeEqual(df_coast, 1.0, string.Format("vVehicle: {0}, vTarget: {1}", vVehicle, vTarget));
+					}
 				}
 			}
 		}
diff --git a/VectoCore/VectoCoreTest/Utils/MockGearbox.cs b/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
index 0cd19ce42f..f4cfe8eb35 100644
--- a/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
+++ b/VectoCore/VectoCoreTest/Utils/MockGearbox.cs
@@ -74,7 +74,7 @@ namespace TUGraz.VectoCore.Tests.Utils
 			get { return null; }
 		}
 
-		public Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque)
+		public Watt GearboxLoss()
 		{
 			return 0.SI<Watt>();
 		}
diff --git a/VectoCore/VectoCoreTest/Utils/MockVairVehicleContainer.cs b/VectoCore/VectoCoreTest/Utils/MockVairVehicleContainer.cs
index 84df8c4615..742cf7f41b 100644
--- a/VectoCore/VectoCoreTest/Utils/MockVairVehicleContainer.cs
+++ b/VectoCore/VectoCoreTest/Utils/MockVairVehicleContainer.cs
@@ -52,7 +52,7 @@ namespace TUGraz.VectoCore.Tests.Utils
 		public MeterPerSquareSecond StartAcceleration { get; private set; }
 		public FullLoadCurve GearFullLoadCurve { get; private set; }
 
-		public Watt GearboxLoss(PerSecond inAngularVelocity, NewtonMeter inTorque)
+		public Watt GearboxLoss()
 		{
 			throw new System.NotImplementedException();
 		}
@@ -136,5 +136,10 @@ namespace TUGraz.VectoCore.Tests.Utils
 		{
 			throw new System.NotImplementedException();
 		}
+
+		public Watt AxlegearLoss()
+		{
+			throw new System.NotImplementedException();
+		}
 	}
 }
\ No newline at end of file
-- 
GitLab