From 4057653d41c0f4964d9270a48d40300fc112ecb8 Mon Sep 17 00:00:00 2001
From: David Amo <david.amo-gonzalez@ext.ec.europa.eu>
Date: Wed, 29 May 2024 10:32:47 +0100
Subject: [PATCH] Implement subgroup allocations for PEVs

---
 .../InputData/EngineeringInputData.cs         |   5 +
 .../Models/Declaration/DeclarationData.cs     |  55 +++--
 .../Models/Declaration/WeightingFactors.cs    |   7 +-
 .../Models/Declaration/WeightingGroups.cs     |  48 +++--
 .../VectoCore/OutputData/DeclarationReport.cs |  41 +++-
 .../AbstractCustomerReport.cs                 |   7 +-
 .../AbstractManufacturerReport.cs             |   9 +-
 .../VTPReport/XMLVTPReport.cs                 |   5 +
 .../OutputData/XML/XMLDeclarationReport.cs    |  56 ++++-
 .../XMLDeclarationReportCompletedVehicle.cs   |   5 +
 .../CO2Standards/WeightingGroups.csv          | 198 +++++++++---------
 11 files changed, 284 insertions(+), 152 deletions(-)

diff --git a/VectoCommon/VectoCommon/InputData/EngineeringInputData.cs b/VectoCommon/VectoCommon/InputData/EngineeringInputData.cs
index 9a3a655ee1..77855af972 100644
--- a/VectoCommon/VectoCommon/InputData/EngineeringInputData.cs
+++ b/VectoCommon/VectoCommon/InputData/EngineeringInputData.cs
@@ -73,6 +73,11 @@ namespace TUGraz.VectoCommon.InputData
 		public const string Hybrid = "Hybrid";
 		public const string PureElectric = "PureElectric";
 
+		public static bool IsBatteryElectric(this VectoSimulationJobType jobType)
+		{
+			return jobType == VectoSimulationJobType.BatteryElectricVehicle || jobType == VectoSimulationJobType.IEPC_E;
+		}
+
 		public static string GetPowertrainArchitectureType(this VectoSimulationJobType jobType)
 		{
 			switch (jobType) {
diff --git a/VectoCore/VectoCore/Models/Declaration/DeclarationData.cs b/VectoCore/VectoCore/Models/Declaration/DeclarationData.cs
index f78cafdbf9..20ae30bec0 100644
--- a/VectoCore/VectoCore/Models/Declaration/DeclarationData.cs
+++ b/VectoCore/VectoCore/Models/Declaration/DeclarationData.cs
@@ -180,27 +180,40 @@ namespace TUGraz.VectoCore.Models.Declaration
 				case VehicleCategory.RigidTruck:
 				case VehicleCategory.Tractor:
 					try {
-						var truckSegment = DeclarationData.TruckSegments.Lookup(vehicleData.VehicleCategory,
-							vehicleData.AxleConfiguration, vehicleData.GrossVehicleMassRating,
+						var truckSegment = DeclarationData.TruckSegments.Lookup(
+							vehicleData.VehicleCategory,
+							vehicleData.AxleConfiguration,
+							vehicleData.GrossVehicleMassRating,
 							vehicleData.CurbMassChassis,
 							vehicleData.VocationalVehicle);
+
 						return Tuple.Create(truckSegment.VehicleClass, (bool?)vehicleData.VocationalVehicle);
 					} catch (VectoException) {
-						var truckSegment = DeclarationData.TruckSegments.Lookup(vehicleData.VehicleCategory,
-							vehicleData.AxleConfiguration, vehicleData.GrossVehicleMassRating,
+						var truckSegment = DeclarationData.TruckSegments.Lookup(
+							vehicleData.VehicleCategory,
+							vehicleData.AxleConfiguration,
+							vehicleData.GrossVehicleMassRating,
 							vehicleData.CurbMassChassis,
 							false);
+
 						return Tuple.Create(truckSegment.VehicleClass, (bool?)false);
 					}
 				case VehicleCategory.HeavyBusPrimaryVehicle:
-					var primarySegment = DeclarationData.PrimaryBusSegments.Lookup(vehicleData.VehicleCategory,
-						vehicleData.AxleConfiguration, vehicleData.Articulated);
+					var primarySegment = DeclarationData.PrimaryBusSegments.Lookup(
+						vehicleData.VehicleCategory,
+						vehicleData.AxleConfiguration, 
+						vehicleData.Articulated);
+
 					return Tuple.Create(primarySegment.VehicleClass, (bool?)null);
 				case VehicleCategory.HeavyBusCompletedVehicle:
-					var segment = DeclarationData.CompletedBusSegments.Lookup(vehicleData.AxleConfiguration.NumAxles(),
+					var segment = DeclarationData.CompletedBusSegments.Lookup(
+						vehicleData.AxleConfiguration.NumAxles(),
 						vehicleData.VehicleCode,
-						vehicleData.RegisteredClass, vehicleData.NumberPassengerSeatsLowerDeck, vehicleData.Height,
+						vehicleData.RegisteredClass,
+						vehicleData.NumberPassengerSeatsLowerDeck, 
+						vehicleData.Height,
 						vehicleData.LowEntry);
+
 					return Tuple.Create(segment.VehicleClass, (bool?)null);
 			}
 
@@ -246,21 +259,39 @@ namespace TUGraz.VectoCore.Models.Declaration
 			public bool AllowVocational { get; set; }
 		}
 
+		/// <summary>
+		/// Checks whether the LH subgroup conditions are met, otherwise RD allocation needs to be carried out.
+		/// </summary>
+		/// <param name="result">Simulation cycle result entry.</param>
+		/// <returns>True if RD allocation is needed; false otherwise.</returns>
+		public static bool EvaluateLHSubgroupConditions(IResultEntry result)
+		{
+			Meter electricOprerationalRange = result.VectoRunData.JobType.IsBatteryElectric() ?
+				(result.ActualChargeDepletingRange ?? 0.SI<Meter>()) :
+				double.MaxValue.SI<Meter>();
+
+			return result.Mission == MissionType.LongHaul &&
+				result.LoadingType == LoadingType.ReferenceLoad &&
+				electricOprerationalRange < 350000.SI<Meter>();
+		}
 
-		public static WeightingGroup GetVehicleGroupCO2StandardsGroup(IVehicleDeclarationInputData vehicleData)
+		public static WeightingGroup GetVehicleGroupCO2StandardsGroup(IVehicleDeclarationInputData vehicleData, double? electricRange = null)
 		{
 			switch (vehicleData.VehicleCategory) {
 				case VehicleCategory.Van:
 				case VehicleCategory.RigidTruck:
 				case VehicleCategory.Tractor:
 					var vehicleGroup = GetVehicleGroupGroup(vehicleData);
-					var propulsionPower = GetReferencePropulsionPower(vehicleData);
-					var co2Group = WeightingGroup.Lookup(vehicleGroup.Item1, vehicleData.SleeperCab ?? false, propulsionPower);
+					var co2Group = WeightingGroup.Lookup(
+						vehicleGroup.Item1,
+						vehicleData.SleeperCab ?? false,
+						GetReferencePropulsionPower(vehicleData),
+						vehicleData.VehicleType.IsBatteryElectric(),
+						electricRange);
 					return co2Group;
 				default:
 					return Declaration.WeightingGroup.Unknown;
 			}
-			//throw new VectoException("No CO2 Group found for vehicle");
 		}
 
 		public static Watt GetReferencePropulsionPower(IVehicleDeclarationInputData vehicleData)
diff --git a/VectoCore/VectoCore/Models/Declaration/WeightingFactors.cs b/VectoCore/VectoCore/Models/Declaration/WeightingFactors.cs
index 81a862bdfc..2233fc296a 100644
--- a/VectoCore/VectoCore/Models/Declaration/WeightingFactors.cs
+++ b/VectoCore/VectoCore/Models/Declaration/WeightingFactors.cs
@@ -43,6 +43,9 @@ namespace TUGraz.VectoCore.Models.Declaration
 {
 	public sealed class WeightingFactors : LookupData
 	{
+		private const double VocationalFactorsSum = 2.0;
+		private const double NonVocationalFactorsSum = 1.0;
+
 		private readonly Dictionary<WeightingGroup, Dictionary<Tuple<MissionType, LoadingType>, double>> Data = new Dictionary<WeightingGroup, Dictionary<Tuple<MissionType, LoadingType>, double>>();
 
 		public IDictionary<Tuple<MissionType, LoadingType>, double> Lookup(WeightingGroup group)
@@ -91,8 +94,8 @@ namespace TUGraz.VectoCore.Models.Declaration
 			foreach (var entry in Data) {
 				var sum = entry.Value.Sum(item => item.Value);
 
-				bool hasVocationalWeights = sum.IsEqual(2.0, 1e-12);
-				bool isNormalWeights = sum.IsEqual(1.0, 1e-12);
+				bool hasVocationalWeights = sum.IsEqual(VocationalFactorsSum, 1e-12);
+				bool isNormalWeights = sum.IsEqual(NonVocationalFactorsSum, 1e-12);
 
 				if (!isNormalWeights && !hasVocationalWeights)
 				{
diff --git a/VectoCore/VectoCore/Models/Declaration/WeightingGroups.cs b/VectoCore/VectoCore/Models/Declaration/WeightingGroups.cs
index b50cec54ec..3f50e9c196 100644
--- a/VectoCore/VectoCore/Models/Declaration/WeightingGroups.cs
+++ b/VectoCore/VectoCore/Models/Declaration/WeightingGroups.cs
@@ -50,16 +50,12 @@ namespace TUGraz.VectoCore.Models.Declaration
         Group4UD,
 		Group4RD,
 		Group4LH,
-		Group4V,
 		Group5RD,
 		Group5LH,
-		Group5V,
 		Group9RD,
 		Group9LH,
-		Group9V,
 		Group10RD,
 		Group10LH,
-		Group10V,
 
 		Group11,
 		Group12,
@@ -160,16 +156,12 @@ namespace TUGraz.VectoCore.Models.Declaration
 				case WeightingGroup.Group4UD:
 				case WeightingGroup.Group4RD:
 				case WeightingGroup.Group4LH:
-				case WeightingGroup.Group4V:
 				case WeightingGroup.Group5RD:
 				case WeightingGroup.Group5LH:
-				case WeightingGroup.Group5V:
 				case WeightingGroup.Group9RD:
 				case WeightingGroup.Group9LH:
-				case WeightingGroup.Group9V:
 				case WeightingGroup.Group10RD:
 				case WeightingGroup.Group10LH:
-				case WeightingGroup.Group10V:
 					return Regex.Split(group.ToString().Replace(Prefix, ""), @"(\d+|\w+)").Where(x => !string.IsNullOrWhiteSpace(x)).Join("-");
 				default:
 					return Constants.NOT_AVAILABLE;
@@ -177,7 +169,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 		}
 	}
 
-	public class WeightingGroups : LookupData<VehicleClass, bool, Watt, WeightingGroup>
+	public class WeightingGroups : LookupData<VehicleClass, bool, Watt, bool, double?, WeightingGroup>
 	{
 		protected readonly List<Entry> Entries = new List<Entry>();
 
@@ -192,31 +184,55 @@ namespace TUGraz.VectoCore.Models.Declaration
 			foreach (DataRow row in table.Rows) {
 				Entries.Add(new Entry() {
 					VehicleGroup = VehicleClassHelper.Parse(row.Field<string>("vehiclegroup")),
+					IsElectric = "1".Equals(row.Field<string>("iselectric"), StringComparison.InvariantCultureIgnoreCase),
 					SleeperCab = "SleeperCab".Equals(row.Field<string>("cabintype"), StringComparison.InvariantCultureIgnoreCase),
 					RatedPowerMin = row.ParseDouble("engineratedpowermin").SI(Unit.SI.Kilo.Watt).Cast<Watt>(),
 					RatedPowerMax = row.ParseDouble("engineratedpowermax").SI(Unit.SI.Kilo.Watt).Cast<Watt>(),
-					WeightingGroup = WeightingGroupHelper.Parse(row.Field<string>("weightinggroup"))
+					ElectricRange = row.ParseDouble("electricrange").SI<Meter>(),
+					WeightingGroup = WeightingGroupHelper.Parse(row.Field<string>("weightinggroup")),
 				});
 			}
 		}
 
 
-		public override WeightingGroup Lookup(VehicleClass group, bool sleeperCab, Watt engineRatedPower)
+		public override WeightingGroup Lookup(VehicleClass group, bool sleeperCab, Watt engineRatedPower, bool isElectric = false,  double? electricRange = null)
 		{
-			var rows = Entries.FindAll(
-				x => x.VehicleGroup == group && x.SleeperCab == sleeperCab && engineRatedPower >= x.RatedPowerMin &&
-					engineRatedPower < x.RatedPowerMax);
-			return rows.Count == 0 ? WeightingGroup.Unknown : rows.First().WeightingGroup;
+			WeightingGroup Lookup()
+			{
+				var rows = Entries.FindAll(
+					e => e.VehicleGroup == group &&
+					e.IsElectric == false &&
+					e.SleeperCab == sleeperCab &&
+					engineRatedPower >= e.RatedPowerMin &&
+					engineRatedPower < e.RatedPowerMax);
+
+				return rows.Count == 0 ? WeightingGroup.Unknown : rows.First().WeightingGroup;
+			}
+
+			/// Never meet the conditions for Electric Range if null.
+			var operationalRange = electricRange == null ? double.MaxValue : electricRange.Value;
+
+			var electricRows = Entries.FindAll(
+				e => e.VehicleGroup == group &&
+				e.IsElectric == isElectric &&
+				e.SleeperCab == sleeperCab &&
+				engineRatedPower >= e.RatedPowerMin &&
+				engineRatedPower < e.RatedPowerMax &&
+				operationalRange < e.ElectricRange);
+
+			return electricRows.Count == 0 ? Lookup() : electricRows.First().WeightingGroup;
 		}
+
 		#endregion
 
 		protected class Entry
 		{
 			public VehicleClass VehicleGroup;
-			public bool Vocational;
+			public bool IsElectric;
 			public bool SleeperCab;
 			public Watt RatedPowerMin;
 			public Watt RatedPowerMax;
+			public Meter ElectricRange;
 			public WeightingGroup WeightingGroup;
 		}
 	}
diff --git a/VectoCore/VectoCore/OutputData/DeclarationReport.cs b/VectoCore/VectoCore/OutputData/DeclarationReport.cs
index 148730c494..c3c2571c79 100644
--- a/VectoCore/VectoCore/OutputData/DeclarationReport.cs
+++ b/VectoCore/VectoCore/OutputData/DeclarationReport.cs
@@ -29,6 +29,8 @@
 *   Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
 */
 
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.CompilerServices;
@@ -74,6 +76,8 @@ namespace TUGraz.VectoCore.OutputData
 	public interface IResultEntry
 	{
 		void Initialize(VectoRunData vectoRunData);
+		
+		void Initialize(VectoRunData vectoRunData, IModalDataContainer modalData);
 
 		VectoRunData VectoRunData { get; }
 
@@ -120,7 +124,7 @@ namespace TUGraz.VectoCore.OutputData
 
 		Watt MaxChargingPower { get; }
 
-		double WeightingFactor { get; }
+		double WeightingFactor { get; set; }
 
 		Meter ActualChargeDepletingRange { get; }
 
@@ -250,25 +254,29 @@ namespace TUGraz.VectoCore.OutputData
 			}
 		}
 
+		List<Tuple<T, VectoRunData, IModalDataContainer>> StoredResults = new List<Tuple<T, VectoRunData, IModalDataContainer>>();
+
 		public void AddResult(VectoRunData runData,
 			IModalDataContainer modData)
 		{
 			//return;
 			if (runData.Mission.MissionType != MissionType.ExemptedMission) {
 				var entry = new T();
-				entry.Initialize(runData);
+				entry.Initialize(runData, modData);
 				lock (Results) {
-					var exístingResult = Results.SingleOrDefault(e =>
-						e.Mission == entry.Mission && e.LoadingType == entry.LoadingType && e.OVCMode == entry.OVCMode && e.VehicleClass == entry.VehicleClass);
-					if (exístingResult != null) {
+					var existingResults = Results.Where(e =>
+						e.Mission == entry.Mission && e.LoadingType == entry.LoadingType && e.OVCMode == entry.OVCMode && e.VehicleClass == entry.VehicleClass).ToList();
+					bool areElectric = existingResults.All(e => e.LoadingType == LoadingType.ReferenceLoad);
+					if (existingResults.Count > 1)
+					{
 						//We already have a result for this run stored, this can happen with iterative runs, in this case we have to remove the old result
-						Results.Remove(exístingResult);
+						Results.RemoveRange(1, existingResults.Count - 1);
 					}
 
 					Results.Add(entry);
 				}
-				
-				DoStoreResult(entry, runData, modData);
+
+				StoredResults.Add(Tuple.Create(entry, runData, modData));
 			}
 
 			WriteResults();
@@ -297,8 +305,19 @@ namespace TUGraz.VectoCore.OutputData
 
 		protected internal virtual void DoWriteReport()
 		{
-			foreach (var result in OrderedResults) {
-				WriteResult(result);
+			/// Check if LH does not meet LH requierements, i.e. ReferenceLoad and OperationalRange > 350km.
+			var RDGroupEntry = StoredResults.SingleOrDefault(e => DeclarationData.EvaluateLHSubgroupConditions(e.Item1));
+
+			foreach (var resultEntry in OrderedResults)
+			{
+				var rdResultEntry = RDGroupEntry != null ? RDGroupEntry.Item1 : resultEntry;
+				var vectoRun = RDGroupEntry != null ? RDGroupEntry.Item2 : resultEntry.VectoRunData;
+
+				/// Set new weighting factors according to new RD group.
+				SetWeightingFactors(vectoRun, OrderedResults, rdResultEntry != null ? rdResultEntry.ActualChargeDepletingRange?.Value() : null);
+
+				/// Update results with newest weighting factors (WFs), i.e. RD WFs if updated otherwise if else.
+				WriteResult(resultEntry);
 			}
 
 			GenerateReports();
@@ -315,5 +334,7 @@ namespace TUGraz.VectoCore.OutputData
 		protected abstract void WriteResult(T result);
 
 		public abstract void InitializeReport(VectoRunData modelData);
+
+		public abstract void SetWeightingFactors(VectoRunData runData, IEnumerable<IResultEntry> orderedeResults, double? electricRange);
 	}
 }
diff --git a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/CustomerInformationFile/CustomerInformationFile_0_9/AbstractCustomerReport.cs b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/CustomerInformationFile/CustomerInformationFile_0_9/AbstractCustomerReport.cs
index dcd3e97da3..8ed477a261 100644
--- a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/CustomerInformationFile/CustomerInformationFile_0_9/AbstractCustomerReport.cs
+++ b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/CustomerInformationFile/CustomerInformationFile_0_9/AbstractCustomerReport.cs
@@ -10,6 +10,7 @@ using System.Xml.XPath;
 using TUGraz.VectoCommon.InputData;
 using TUGraz.VectoCommon.Models;
 using TUGraz.VectoCommon.Resources;
+using TUGraz.VectoCommon.Utils;
 using TUGraz.VectoCore.Models.Declaration;
 using TUGraz.VectoCore.Models.Simulation.Data;
 using TUGraz.VectoCore.Models.SimulationComponent;
@@ -96,9 +97,11 @@ namespace TUGraz.VectoCore.OutputData.XML.DeclarationReports.CustomerInformation
 				)
 			);
 
+			var RDGroupEntry = _results.SingleOrDefault(e => DeclarationData.EvaluateLHSubgroupConditions(e));
+
 			// ReSharper disable once PossibleNullReferenceException
-			Vehicle.XPathSelectElement($"//*[local-name()='{XMLNames.VehicleGroupCO2}']").Value = DeclarationData
-				.GetVehicleGroupCO2StandardsGroup(Input).ToXMLFormat();
+			Vehicle.XPathSelectElement($"//*[local-name()='{XMLNames.VehicleGroupCO2}']").Value =
+				DeclarationData.GetVehicleGroupCO2StandardsGroup(Input, RDGroupEntry != null ? RDGroupEntry.ActualChargeDepletingRange?.Value() : null).ToXMLFormat();
 
 			var stream = new MemoryStream();
 			var writer = new StreamWriter(stream);
diff --git a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/ManufacturerReport/ManufacturerReport_0_9/AbstractManufacturerReport.cs b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/ManufacturerReport/ManufacturerReport_0_9/AbstractManufacturerReport.cs
index f11a49b3a7..c66cf3b77b 100644
--- a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/ManufacturerReport/ManufacturerReport_0_9/AbstractManufacturerReport.cs
+++ b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/ManufacturerReport/ManufacturerReport_0_9/AbstractManufacturerReport.cs
@@ -112,10 +112,11 @@ namespace TUGraz.VectoCore.OutputData.XML.DeclarationReports.ManufacturerReport.
 				)
 			);
 
-			//var lh = _results.SingleOrDefault(res => res.Mission == MissionType.LongHaul && res.LoadingType == LoadingType.ReferenceLoad);
-			
-			Vehicle.XPathSelectElement($"//*[local-name()='{XMLNames.VehicleGroupCO2}']").Value = DeclarationData
-				.GetVehicleGroupCO2StandardsGroup(Input).ToXMLFormat();
+			var RDGroupEntry = _results.SingleOrDefault(e => DeclarationData.EvaluateLHSubgroupConditions(e));
+			double? LHOperationalRange = RDGroupEntry != null ? RDGroupEntry.ActualChargeDepletingRange?.Value() : null;
+
+			Vehicle.XPathSelectElement($"//*[local-name()='{XMLNames.VehicleGroupCO2}']").Value =
+				DeclarationData.GetVehicleGroupCO2StandardsGroup(Input, LHOperationalRange).ToXMLFormat();
 
 			var stream = new MemoryStream();
 			var writer = new StreamWriter(stream);
diff --git a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/VTPReport/XMLVTPReport.cs b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/VTPReport/XMLVTPReport.cs
index 0195929c95..6c156664c0 100644
--- a/VectoCore/VectoCore/OutputData/XML/DeclarationReports/VTPReport/XMLVTPReport.cs
+++ b/VectoCore/VectoCore/OutputData/XML/DeclarationReports/VTPReport/XMLVTPReport.cs
@@ -1000,6 +1000,11 @@ namespace TUGraz.VectoCore.OutputData.XML
 			};
 		}
 
+		public override void SetWeightingFactors(VectoRunData runData, IEnumerable<IResultEntry> orderedeResults, double? electricRange)
+		{
+			throw new NotImplementedException();
+		}
+
 		#region Implementation of IVTPReport
 
 		public IVectoHash InputDataHash { protected get; set; }
diff --git a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReport.cs b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReport.cs
index 3cf6892855..2acac19f5c 100644
--- a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReport.cs
+++ b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReport.cs
@@ -47,6 +47,7 @@ using TUGraz.VectoCore.Models.Simulation.Data;
 using TUGraz.VectoCore.Models.Simulation.Impl;
 using TUGraz.VectoCore.Models.SimulationComponent.Data.ElectricComponents.Battery;
 using TUGraz.VectoCore.OutputData.ModDataPostprocessing;
+using TUGraz.VectoCore.OutputData.XML.DeclarationReports.Common;
 using TUGraz.VectoCore.OutputData.XML.DeclarationReports.CustomerInformationFile;
 using TUGraz.VectoCore.OutputData.XML.DeclarationReports.CustomerInformationFile.CustomerInformationFile_0_9;
 using TUGraz.VectoCore.OutputData.XML.DeclarationReports.ManufacturerReport;
@@ -99,9 +100,25 @@ namespace TUGraz.VectoCore.OutputData.XML
 				BatteryData = runData.BatteryData;
 				OVCMode = runData.OVCMode;
 				VectoRunData = runData;
+			}
 
-			
-				//VehicleCode = runData.VehicleData.VehicleCode;
+			public void Initialize(VectoRunData runData, IModalDataContainer modalData)
+			{
+				Mission = runData.Mission.MissionType;
+				LoadingType = runData.Loading;
+				FuelMode = runData.EngineData?.FuelMode ?? 0;
+				FuelData = runData.EngineData?.Fuels.Select(x => x.FuelData).ToList() ?? new List<IFuelProperties>();
+				Payload = runData.VehicleData.Loading;
+				TotalVehicleMass = runData.VehicleData.TotalVehicleMass;
+				CargoVolume = runData.VehicleData.CargoVolume;
+				VehicleClass = runData.Mission?.BusParameter?.BusGroup ?? runData.VehicleData.VehicleClass;
+				PassengerCount = runData.VehicleData.PassengerCount;
+				MaxChargingPower = runData.MaxChargingPower;
+				BatteryData = runData.BatteryData;
+				OVCMode = runData.OVCMode;
+				VectoRunData = runData;
+
+				SetResultData(runData, modalData, 0.0);
 			}
 
 			public VectoRunData VectoRunData { get; private set; }
@@ -267,11 +284,7 @@ namespace TUGraz.VectoCore.OutputData.XML
 				}
 
 				WeightingFactor = weightingFactor;
-
-				PrimaryResult = runData.PrimaryResult;
-
 			}
-
 		}
 
 
@@ -312,6 +325,24 @@ namespace TUGraz.VectoCore.OutputData.XML
 			_monitoringReport.GenerateReport();
 		}
 
+		public override void SetWeightingFactors(VectoRunData runData, IEnumerable<IResultEntry> orderedeResults, double? electricRange)
+		{
+			WeightingGroup = DeclarationData.WeightingGroup.Lookup(
+				runData.VehicleData.VehicleClass,
+				runData.VehicleData.SleeperCab.Value,
+				DeclarationData.GetReferencePropulsionPower(runData.VehicleData.InputData),
+				runData.JobType.IsBatteryElectric(),
+				electricRange);
+
+			_weightingFactors = WeightingGroup == WeightingGroup.Unknown
+				? ZeroWeighting
+				: DeclarationData.WeightingFactors.Lookup(WeightingGroup);
+
+			foreach(var result in orderedeResults)
+			{
+				result.WeightingFactor = _weightingFactors[Tuple.Create(result.Mission, result.LoadingType)];
+			}
+		}
 
 		protected override void OutputReports()
 		{
@@ -332,15 +363,19 @@ namespace TUGraz.VectoCore.OutputData.XML
 
 		public override void InitializeReport(VectoRunData modelData)
 		{
-			if (modelData.Exempted) {
+			if (modelData.Exempted)
+			{
 				WeightingGroup = WeightingGroup.Unknown;
-			} else {
-				if (modelData.VehicleData.SleeperCab == null) {
+			}
+			else
+			{
+				if (modelData.VehicleData.SleeperCab == null)
+				{
 					throw new VectoException("SleeperCab parameter is required");
 				}
 
 				var propulsionPower = DeclarationData.GetReferencePropulsionPower(modelData.VehicleData.InputData);
-                WeightingGroup = DeclarationData.WeightingGroup.Lookup(
+				WeightingGroup = DeclarationData.WeightingGroup.Lookup(
 					modelData.VehicleData.VehicleClass,
 					modelData.VehicleData.SleeperCab.Value,
 					propulsionPower);
@@ -350,7 +385,6 @@ namespace TUGraz.VectoCore.OutputData.XML
 				? ZeroWeighting
 				: DeclarationData.WeightingFactors.Lookup(WeightingGroup);
 
-
 			InstantiateReports(modelData);
 
 			ManufacturerRpt.Initialize(modelData);
diff --git a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReportCompletedVehicle.cs b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReportCompletedVehicle.cs
index 0eccc41ecd..a88b51548c 100644
--- a/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReportCompletedVehicle.cs
+++ b/VectoCore/VectoCore/OutputData/XML/XMLDeclarationReportCompletedVehicle.cs
@@ -342,6 +342,11 @@ namespace TUGraz.VectoCore.OutputData.XML
                 throw new NotImplementedException();
             }
 
+			public void Initialize(VectoRunData vectoRunData, IModalDataContainer modalData)
+			{
+				throw new NotImplementedException();
+			}
+
 			public string Error { get; set; } 
 			public string StackTrace { get; set; }
             public BatterySystemData BatteryData
diff --git a/VectoCore/VectoCore/Resources/Declaration/CO2Standards/WeightingGroups.csv b/VectoCore/VectoCore/Resources/Declaration/CO2Standards/WeightingGroups.csv
index 0d3939ee62..2eeadceb06 100644
--- a/VectoCore/VectoCore/Resources/Declaration/CO2Standards/WeightingGroups.csv
+++ b/VectoCore/VectoCore/Resources/Declaration/CO2Standards/WeightingGroups.csv
@@ -1,97 +1,105 @@
-Vehicle Group  , Cabin Type , Engine Rated Power Min [kw] , Engine Rated Power Max [kW] , Weighting Group
-1s             , DayCab     , 0                           , 999999                      , 1s
-1s             , SleeperCab , 0                           , 999999                      , 1s
-1              , DayCab     , 0                           , 999999                      , 1
-1              , SleeperCab , 0                           , 999999                      , 1
-2              , DayCab     , 0                           , 999999                      , 2
-2              , SleeperCab , 0                           , 999999                      , 2
-3              , DayCab     , 0                           , 999999                      , 3
-3              , SleeperCab , 0                           , 999999                      , 3
-4              , DayCab     , 0                           , 170                         , 4-UD
-4              , SleeperCab , 0                           , 170                         , 4-UD
-4              , DayCab     , 170                         , 999999                      , 4-RD
-4              , SleeperCab , 170                         , 265                         , 4-RD
-4              , SleeperCab , 265                         , 999999                      , 4-LH
-5              , DayCab     , 0                           , 999999                      , 5-RD
-5              , SleeperCab , 0                           , 265                         , 5-RD
-5              , SleeperCab , 265                         , 999999                      , 5-LH
-9              , DayCab     , 0                           , 999999                      , 9-RD
-9              , SleeperCab , 0                           , 999999                      , 9-LH
-10             , DayCab     , 0                           , 999999                      , 10-RD
-10             , SleeperCab , 0                           , 999999                      , 10-LH
-11             , DayCab     , 0                           , 999999                      , 11
-11             , SleeperCab , 0                           , 999999                      , 11
-12             , DayCab     , 0                           , 999999                      , 12
-12             , SleeperCab , 0                           , 999999                      , 12
-16             , DayCab     , 0                           , 999999                      , 16
-16             , SleeperCab , 0                           , 999999                      , 16
+Vehicle Group  , Is Electric   , Cabin Type , Engine Rated Power Min [kw] , Engine Rated Power Max [kW] , Electric Range [m] , Weighting Group
+1s             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 1s
+1s             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 1s
+1              , 0             , DayCab     , 0                           , 999999                      , 999999999          , 1
+1              , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 1
+2              , 0             , DayCab     , 0                           , 999999                      , 999999999          , 2
+2              , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 2
+3              , 0             , DayCab     , 0                           , 999999                      , 999999999          , 3
+3              , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 3
+4              , 0             , DayCab     , 0                           , 170                         , 999999999          , 4-UD
+4              , 0             , SleeperCab , 0                           , 170                         , 999999999          , 4-UD
+4              , 0             , DayCab     , 170                         , 999999                      , 999999999          , 4-RD
+4              , 0             , SleeperCab , 170                         , 265                         , 350000             , 4-RD
+4              , 1             , SleeperCab , 170                         , 999999                      , 350000             , 4-RD
+4              , 0             , SleeperCab , 265                         , 999999                      , 999999999          , 4-LH
+4              , 1             , SleeperCab , 265                         , 999999                      , 999999999          , 4-LH
+5              , 0             , DayCab     , 0                           , 999999                      , 999999999          , 5-RD
+5              , 0             , SleeperCab , 0                           , 265                         , 350000             , 5-RD
+5              , 1             , SleeperCab , 0                           , 999999                      , 350000             , 5-RD
+5              , 0             , SleeperCab , 265                         , 999999                      , 999999999          , 5-LH
+5              , 1             , SleeperCab , 265                         , 999999                      , 999999999          , 5-LH
+9              , 0             , DayCab     , 0                           , 999999                      , 999999999          , 9-RD
+9              , 1             , SleeperCab , 0                           , 999999                      , 350000             , 9-RD
+9              , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 9-LH
+9              , 1             , SleeperCab , 0                           , 999999                      , 999999999          , 9-LH
+10             , 0             , DayCab     , 0                           , 999999                      , 350000             , 10-RD
+10             , 1             , DayCab     , 0                           , 999999                      , 350000             , 10-RD
+10             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 10-LH
+10             , 1             , SleeperCab , 0                           , 999999                      , 999999999          , 10-LH
+11             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 11
+11             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 11
+12             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 12
+12             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 12
+16             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 16
+16             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 16
 #
-51             , DayCab     , 0                           , 999999                      , 51
-51             , SleeperCab , 0                           , 999999                      , 51
-52             , DayCab     , 0                           , 999999                      , 52
-52             , SleeperCab , 0                           , 999999                      , 52
-53             , DayCab     , 0                           , 999999                      , 53
-53             , SleeperCab , 0                           , 999999                      , 53
-54             , DayCab     , 0                           , 999999                      , 54
-54             , SleeperCab , 0                           , 999999                      , 54
-55             , DayCab     , 0                           , 999999                      , 55
-55             , SleeperCab , 0                           , 999999                      , 55
-56             , DayCab     , 0                           , 999999                      , 56
-56             , SleeperCab , 0                           , 999999                      , 56
+51             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 51
+51             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 51
+52             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 52
+52             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 52
+53             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 53
+53             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 53
+54             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 54
+54             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 54
+55             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 55
+55             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 55
+56             , 0             , DayCab     , 0                           , 999999                      , 999999999          , 56
+56             , 0             , SleeperCab , 0                           , 999999                      , 999999999          , 56
 #
-31a            , DayCab     , 0                           , 999999                      , 31a
-31b1           , DayCab     , 0                           , 999999                      , 31b1
-31b2           , DayCab     , 0                           , 999999                      , 31b2
-31c            , DayCab     , 0                           , 999999                      , 31c
-31d            , DayCab     , 0                           , 999999                      , 31d
-31e            , DayCab     , 0                           , 999999                      , 31e
-32a            , DayCab     , 0                           , 999999                      , 32a
-32b            , DayCab     , 0                           , 999999                      , 32b
-32c            , DayCab     , 0                           , 999999                      , 32c
-32d            , DayCab     , 0                           , 999999                      , 32d
-32e            , DayCab     , 0                           , 999999                      , 32e
-32f            , DayCab     , 0                           , 999999                      , 32f
-33a            , DayCab     , 0                           , 999999                      , 33a
-33b1           , DayCab     , 0                           , 999999                      , 33b1
-33b2           , DayCab     , 0                           , 999999                      , 33b2
-33c            , DayCab     , 0                           , 999999                      , 33c
-33d            , DayCab     , 0                           , 999999                      , 33d
-33e            , DayCab     , 0                           , 999999                      , 33e
-34a            , DayCab     , 0                           , 999999                      , 34a
-34b            , DayCab     , 0                           , 999999                      , 34b
-34c            , DayCab     , 0                           , 999999                      , 34c
-34d            , DayCab     , 0                           , 999999                      , 34d
-34e            , DayCab     , 0                           , 999999                      , 34e
-34f            , DayCab     , 0                           , 999999                      , 34f
-35a            , DayCab     , 0                           , 999999                      , 35a
-35b1           , DayCab     , 0                           , 999999                      , 35b1
-35b2           , DayCab     , 0                           , 999999                      , 35b2
-35c            , DayCab     , 0                           , 999999                      , 35c
-36a            , DayCab     , 0                           , 999999                      , 36a
-36b            , DayCab     , 0                           , 999999                      , 36b
-36c            , DayCab     , 0                           , 999999                      , 36c
-36d            , DayCab     , 0                           , 999999                      , 36d
-36e            , DayCab     , 0                           , 999999                      , 36e
-36f            , DayCab     , 0                           , 999999                      , 36f
-37a            , DayCab     , 0                           , 999999                      , 37a
-37b1           , DayCab     , 0                           , 999999                      , 37b1
-37b2           , DayCab     , 0                           , 999999                      , 37b2
-37c            , DayCab     , 0                           , 999999                      , 37c
-37d            , DayCab     , 0                           , 999999                      , 37d
-37e            , DayCab     , 0                           , 999999                      , 37e
-38a            , DayCab     , 0                           , 999999                      , 38a
-38b            , DayCab     , 0                           , 999999                      , 38b
-38c            , DayCab     , 0                           , 999999                      , 38c
-38d            , DayCab     , 0                           , 999999                      , 38d
-38e            , DayCab     , 0                           , 999999                      , 38e
-38f            , DayCab     , 0                           , 999999                      , 38f
-39a            , DayCab     , 0                           , 999999                      , 39a
-39b1           , DayCab     , 0                           , 999999                      , 39b1
-39b2           , DayCab     , 0                           , 999999                      , 39b2
-39c            , DayCab     , 0                           , 999999                      , 39c
-40a            , DayCab     , 0                           , 999999                      , 40a
-40b            , DayCab     , 0                           , 999999                      , 40b
-40c            , DayCab     , 0                           , 999999                      , 40c
-40d            , DayCab     , 0                           , 999999                      , 40d
-40e            , DayCab     , 0                           , 999999                      , 40e
-40f            , DayCab     , 0                           , 999999                      , 40f
\ No newline at end of file
+31a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31a
+31b1           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31b1
+31b2           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31b2
+31c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31c
+31d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31d
+31e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 31e
+32a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32a
+32b            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32b
+32c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32c
+32d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32d
+32e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32e
+32f            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 32f
+33a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33a
+33b1           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33b1
+33b2           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33b2
+33c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33c
+33d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33d
+33e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 33e
+34a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34a
+34b            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34b
+34c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34c
+34d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34d
+34e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34e
+34f            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 34f
+35a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 35a
+35b1           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 35b1
+35b2           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 35b2
+35c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 35c
+36a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36a
+36b            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36b
+36c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36c
+36d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36d
+36e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36e
+36f            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 36f
+37a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37a
+37b1           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37b1
+37b2           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37b2
+37c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37c
+37d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37d
+37e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 37e
+38a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38a
+38b            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38b
+38c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38c
+38d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38d
+38e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38e
+38f            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 38f
+39a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 39a
+39b1           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 39b1
+39b2           , 0             , DayCab     , 0                           , 999999                      , 999999999          , 39b2
+39c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 39c
+40a            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40a
+40b            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40b
+40c            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40c
+40d            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40d
+40e            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40e
+40f            , 0             , DayCab     , 0                           , 999999                      , 999999999          , 40f
\ No newline at end of file
-- 
GitLab