2using System.Collections.Generic;
5using System.Globalization;
10 public class CEPHandler
17 private Dictionary<string, CEP>
_ceps;
18 public Dictionary<string, CEP>
CEPS =>
_ceps;
23 private Dictionary<string, Dictionary<string, double>>
_fleetShares;
29 public bool GetCEP(List<string> DataPath, Helpers Helper, Correction DataCor)
31 if (!
CEPS.ContainsKey(Helper.gClass))
33 if (!
Load(DataPath, Helper, DataCor))
42 public bool GetFleetCEP(List<string> DataPath,
string AggClass, Helpers Helper, Correction DataCor)
44 if (!
CEPS.ContainsKey(Helper.gClass))
46 if (Constants.AGGREGATED_VEHICLECLASSES.Contains(AggClass))
48 List<CEP> weightedCEPS =
new List<CEP>();
52 foreach (
string aggVehClass
in FleetShares[AggClass].Keys)
54 if (!Helper.setclass(aggVehClass))
58 if (!
CEPS.ContainsKey(aggVehClass) && !
Load(DataPath, Helper, DataCor,
true))
62 weightedCEPS.Add(
CEPS[aggVehClass] *
FleetShares[AggClass][aggVehClass]);
64 if (CEP.CheckClass(weightedCEPS.ToArray()))
66 _ceps.Add(AggClass, CEP.AddRangeCeps(weightedCEPS.ToArray(), Helper));
70 Helper.ErrMsg =
"The aggregated vehicle class (" + AggClass +
") includes heavy and non heavy vehicles! This is not allowed. Aggregated vehicle class not added.";
75 Helper.gClass = AggClass;
76 Helper.pClass =
_ceps[
_ceps.Last().Key].FuelType;
80 Helper.ErrMsg =
"The aggregated vehicle class (" + AggClass +
") is not available in the FleetShare file!";
86 Helper.ErrMsg =
"The aggregated vehicle class (" + AggClass +
") is a unknown class!";
96 private bool CalcCorrection(Correction DataCor, Helpers Helper, VEHPHEMLightJSON.Vehicle_Data vehicle_Data)
100 DataCor.VehMileage = -1;
101 if (vehicle_Data.Mileage.HasValue) DataCor.VehMileage = vehicle_Data.Mileage.Value;
103 if (!DataCor.IniDETfactor(Helper))
108 if (!DataCor.IniTNOxfactor(Helper))
118 private bool Load(List<string> DataPath, Helpers Helper, Correction DataCor,
bool fleetMix =
false)
122 List<List<double>> matrixFCvalues;
123 List<List<double>> matrixPollutants;
124 List<double> idlingValuesFCvalues;
125 List<double> idlingValuesPollutants;
126 List<string> headerFCvalues;
127 List<string> headerPollutants;
138 if (!CalcCorrection(DataCor, Helper,
Vehicle.VehicleData))
142 if (!
ReadEmissionData(
true, DataPath[1], Helper, fleetMix, DataCor, out headerFCvalues, out matrixFCvalues, out idlingValuesFCvalues))
144 if (!
ReadEmissionData(
false, DataPath[2], Helper, fleetMix, DataCor, out headerPollutants, out matrixPollutants, out idlingValuesPollutants))
152 idlingValuesFCvalues,
153 idlingValuesPollutants));
158 #region ReadVehicleFile
162 out VEHPHEMLightJSON.VEH
Vehicle)
165 Vehicle =
new VEHPHEMLightJSON.VEH();
169 path = DataPath +
@"\" + Helper.gClass +
".PHEMLight.veh";
173 if (!File.Exists(@path))
175 Helper.ErrMsg =
"File do not exist! (" + path +
")";
180 using (StreamReader r =
new StreamReader(path))
184 string json = r.ReadToEnd();
185 Vehicle = JsonConvert.DeserializeObject<VEHPHEMLightJSON.VEH>(json);
189 Helper.ErrMsg =
"Error during file read! (" + path +
")";
196 if (
Vehicle.VehicleData.MassType ==
null) Vehicle.VehicleData.MassType =
"LV";
197 if (
Vehicle.VehicleData.FuelType ==
null) Vehicle.VehicleData.FuelType =
"D";
198 if (
Vehicle.VehicleData.CalcType ==
null) Vehicle.VehicleData.CalcType =
"Conv";
199 if (
Vehicle.VehicleData.Mass ==
null) Vehicle.VehicleData.Mass = 0;
200 if (
Vehicle.VehicleData.Loading ==
null) Vehicle.VehicleData.Loading = 0;
201 if (
Vehicle.VehicleData.RedMassWheel ==
null) Vehicle.VehicleData.RedMassWheel = 0;
202 if (
Vehicle.VehicleData.WheelDiameter ==
null) Vehicle.VehicleData.WheelDiameter = 0;
203 if (
Vehicle.VehicleData.Cw ==
null) Vehicle.VehicleData.Cw = 0;
204 if (
Vehicle.VehicleData.A ==
null) Vehicle.VehicleData.A = 0;
207 if (
Vehicle.AuxiliariesData.Pauxnorm ==
null) Vehicle.AuxiliariesData.Pauxnorm = 0;
210 if (
Vehicle.EngineData.ICEData.Prated ==
null) Vehicle.EngineData.ICEData.Prated = 0;
211 if (
Vehicle.EngineData.ICEData.nrated ==
null) Vehicle.EngineData.ICEData.nrated = 0;
212 if (
Vehicle.EngineData.ICEData.Idling ==
null) Vehicle.EngineData.ICEData.Idling = 0;
213 if (
Vehicle.EngineData.EMData.Prated ==
null) Vehicle.EngineData.EMData.Prated = 0;
214 if (
Vehicle.EngineData.EMData.nrated ==
null) Vehicle.EngineData.EMData.nrated = 0;
217 if (
Vehicle.RollingResData.Fr0 ==
null) Vehicle.RollingResData.Fr0 = 0;
218 if (
Vehicle.RollingResData.Fr1 ==
null) Vehicle.RollingResData.Fr1 = 0;
219 if (
Vehicle.RollingResData.Fr2 ==
null) Vehicle.RollingResData.Fr2 = 0;
220 if (
Vehicle.RollingResData.Fr3 ==
null) Vehicle.RollingResData.Fr3 = 0;
221 if (
Vehicle.RollingResData.Fr4 ==
null) Vehicle.RollingResData.Fr4 = 0;
224 if (
Vehicle.TransmissionData.AxelRatio ==
null) Vehicle.TransmissionData.AxelRatio = 0;
225 if (
Vehicle.TransmissionData.Transm ==
null)
227 Helper.ErrMsg =
"Transmission ratios missing in vehicle file! Calculation stopped! (" + path +
")";
232 if (!
Vehicle.TransmissionData.Transm.ContainsKey(
"Speed"))
234 Helper.ErrMsg =
"No Speed signal in transmission data given! Calculation stopped! (" + path +
")";
237 if (!
Vehicle.TransmissionData.Transm.ContainsKey(
"GearRatio"))
239 Helper.ErrMsg =
"No GearRatio signal in transmission data given! Calculation stopped! (" + path +
")";
242 if (!
Vehicle.TransmissionData.Transm.ContainsKey(
"RotMassF"))
244 Helper.ErrMsg =
"No RotMassF signal in transmission data given! Calculation stopped! (" + path +
")";
250 if (
Vehicle.FLDData.P_n_max_v0 ==
null) Vehicle.FLDData.P_n_max_v0 = 0;
251 if (
Vehicle.FLDData.P_n_max_p0 ==
null) Vehicle.FLDData.P_n_max_p0 = 0;
252 if (
Vehicle.FLDData.P_n_max_v1 ==
null) Vehicle.FLDData.P_n_max_v1 = 0;
253 if (
Vehicle.FLDData.P_n_max_p1 ==
null) Vehicle.FLDData.P_n_max_p1 = 0;
254 if (
Vehicle.FLDData.DragCurve ==
null)
256 Helper.ErrMsg =
"Drag curve missing in vehicle file! Calculation stopped! (" + path +
")";
261 if (!
Vehicle.FLDData.DragCurve.ContainsKey(
"n_norm"))
263 Helper.ErrMsg =
"No n_norm signal in drag curve data given! Calculation stopped! (" + path +
")";
266 if (!
Vehicle.FLDData.DragCurve.ContainsKey(
"pe_drag_norm"))
268 Helper.ErrMsg =
"No pe_drag_norm signal in drag curve data given! Calculation stopped! (" + path +
")";
277 #region ReadEmissionData
283 out List<string> header,
284 out List<List<double>> matrix,
285 out List<double> idlingValues)
290 header =
new List<string>();
291 matrix =
new List<List<double>>();
292 idlingValues =
new List<double>();
297 path = DataPath +
@"\" + Helper.gClass +
"_FC.csv";
299 path = DataPath +
@"\" + Helper.gClass +
".csv";
306 if (!File.Exists(path))
308 Helper.ErrMsg =
"File do not exist! (" + path +
")";
311 StreamReader fileReader = File.OpenText(@path);
314 if ((line =
ReadLine(fileReader)) !=
null)
316 List<string> entries =
split(line,
',');
318 for (
int i = 1; i < entries.Count; i++)
320 header.Add(entries[i]);
333 List<string> stringIdlings =
split(line,
',').ToList();
334 stringIdlings.RemoveAt(0);
338 while ((line =
ReadLine(fileReader)) !=
null)
345 if (!CorrectEmissionData(DataCor, ref header, ref matrix, ref idlingValues))
347 Helper.ErrMsg =
"Error in correction calculation";
355 private bool CorrectEmissionData(Correction DataCor,
356 ref List<string> header,
357 ref List<List<double>> matrix,
358 ref List<double> idlingValues)
360 for (
int i = 0; i < header.Count; i++)
362 double CorF = GetDetTempCor(DataCor, header[i]);
365 for (
int j = 0; j < matrix.Count; j++)
367 matrix[j][i + 1] *= CorF;
369 idlingValues[i] *= CorF;
378 private double GetDetTempCor(Correction DataCor,
string Emi)
387 foreach (
string Key
in DataCor.DETFactors.Keys)
389 if (Emi.ToUpper() == Key.ToUpper())
391 CorF += (DataCor.DETFactors[Key] - 1);
398 if (Emi.ToUpper().Contains(
"NOX")) CorF += (DataCor.TNOxFactor - 1);
408 #region ReadFleetShares
409 public bool ReadFleetShares(
string DataPath, Helpers Helper)
413 string path = DataPath;
414 if (!File.Exists(@path))
416 Helper.ErrMsg =
"FleetShares file does not exist! (" + path +
")";
419 StreamReader shareReader = File.OpenText(@path);
421 _fleetShares =
new Dictionary<string, Dictionary<string, double>>();
423 while ((line =
ReadLine(shareReader)) !=
null)
425 if (line.Substring(0, 1) == Helper.CommentPrefix)
428 List<string> splitLine =
split(line,
',');
429 string aggregateClass = splitLine[0];
432 FleetShares.Add(aggregateClass,
new Dictionary<string, double>());
434 string subClass = splitLine[1];
436 if (!
FleetShares[aggregateClass].ContainsKey(subClass))
446 private List<string>
split(
string s,
char delim)
448 return s.Split(delim).ToList();
454 return double.Parse(s, CultureInfo.InvariantCulture);
460 return s.Select(p =>
todouble(p)).Cast<
double>().ToList();
464 private string ReadLine(StreamReader s)
472 public class VEHPHEMLightJSON
477 public string Type {
get;
set; }
478 public string Version {
get;
set; }
479 public Vehicle_Data VehicleData {
get;
set; }
480 public Aux_Data AuxiliariesData {
get;
set; }
481 public Engine_Data EngineData {
get;
set; }
482 public Rollres_Data RollingResData {
get;
set; }
483 public FullLoadDrag_Data FLDData {
get;
set; }
484 public Transmission_Data TransmissionData {
get;
set; }
488 VehicleData =
new Vehicle_Data();
489 RollingResData =
new Rollres_Data();
490 EngineData =
new Engine_Data();
491 AuxiliariesData =
new Aux_Data();
492 FLDData =
new FullLoadDrag_Data();
493 TransmissionData =
new Transmission_Data();
499 public class Vehicle_Data
501 public string MassType {
get;
set; }
502 public string FuelType {
get;
set; }
503 public string CalcType {
get;
set; }
504 public double? Mass {
get;
set; }
505 public double? Loading {
get;
set; }
506 public double? RedMassWheel {
get;
set; }
507 public double? WheelDiameter {
get;
set; }
508 public double? Cw {
get;
set; }
509 public double? A {
get;
set; }
510 public double? Mileage {
get;
set; }
514 #region Rolling Resistance
516 public class Rollres_Data
518 public double? Fr0 {
get;
set; }
519 public double? Fr1 {
get;
set; }
520 public double? Fr2 {
get;
set; }
521 public double? Fr3 {
get;
set; }
522 public double? Fr4 {
get;
set; }
528 public class Engine_Data
530 public ICE_Data ICEData {
get;
set; }
531 public EM_Data EMData {
get;
set; }
535 ICEData =
new ICE_Data();
536 EMData =
new EM_Data();
541 public class ICE_Data
543 public double? Prated {
get;
set; }
544 public double? nrated {
get;
set; }
545 public double? Idling {
get;
set; }
551 public double? Prated {
get;
set; }
552 public double? nrated {
get;
set; }
558 public class Aux_Data
560 public double? Pauxnorm {
get;
set; }
564 #region Full load and Drag data
566 public class FullLoadDrag_Data
568 public double? P_n_max_v0 {
get;
set; }
569 public double? P_n_max_p0 {
get;
set; }
570 public double? P_n_max_v1 {
get;
set; }
571 public double? P_n_max_p1 {
get;
set; }
572 public Dictionary<string, List<double>> DragCurve {
get;
set; }
574 public FullLoadDrag_Data()
576 DragCurve =
new Dictionary<string, List<double>>();
583 public class Transmission_Data
585 public double? AxelRatio {
get;
set; }
586 public Dictionary<string, List<double>> Transm {
get;
set; }
588 public Transmission_Data()
590 Transm =
new Dictionary<string, List<double>>();
std::map< std::string, CEP * > _ceps
std::vector< std::string > split(const std::string &s, char delim)
bool Load(const std::vector< std::string > &DataPath, Helpers *Helper)
Dictionary< string, CEP > CEPS
Dictionary< string, Dictionary< string, double > > _fleetShares
bool GetFleetCEP(string DataPath, string AggClass, Helpers Helper)
bool ReadEmissionData(bool readFC, const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, std::vector< std::string > &header, std::vector< std::vector< double > > &matrix, std::vector< double > &idlingValues)
double todouble(const std::string &s)
std::string ReadLine(std::ifstream &s)
bool GetCEP(const std::vector< std::string > &DataPath, Helpers *Helper)
std::vector< double > todoubleList(const std::vector< std::string > &s)
Dictionary< string, Dictionary< string, double > > FleetShares
bool ReadVehicleFile(const std::vector< std::string > &DataPath, const std::string &emissionClass, Helpers *Helper, double &vehicleMass, double &vehicleLoading, double &vehicleMassRot, double &crossArea, double &cWValue, double &f0, double &f1, double &f2, double &f3, double &f4, double &axleRatio, double &auxPower, double &ratedPower, double &engineIdlingSpeed, double &engineRatedSpeed, double &effectiveWheelDiameter, std::vector< double > &transmissionGearRatios, std::string &vehicleMassType, std::string &vehicleFuelType, double &pNormV0, double &pNormP0, double &pNormV1, double &pNormP1, std::vector< std::vector< double > > &matrixSpeedInertiaTable, std::vector< std::vector< double > > &normedDragTable)
C++ TraCI client API implementation.