Eclipse SUMO - Simulation of Urban MObility
dll_code/CEPHandler.cs
Go to the documentation of this file.
1 #define FLEET
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 using System.IO;
8 using System.Globalization;
9 
10 namespace PHEMlightdll
11 {
12  public class CEPHandler
13  {
14  //--------------------------------------------------------------------------------------------------
15  // Constructors
16  //--------------------------------------------------------------------------------------------------
17 
18  #region Constructor
19  public CEPHandler()
20  {
21  _ceps = new Dictionary<string, CEP>();
22  }
23  #endregion
24 
25  //--------------------------------------------------------------------------------------------------
26  // Members
27  //--------------------------------------------------------------------------------------------------
28 
29  #region CEPS
30  private Dictionary<string, CEP> _ceps;
31  public Dictionary<string, CEP> CEPS
32  {
33  get
34  {
35  return _ceps;
36  }
37  }
38  #endregion
39 
40  #if FLEET
41  #region FleetShares
42  private Dictionary<string, Dictionary<string, double>> _fleetShares;
43  public Dictionary<string, Dictionary<string, double>> FleetShares
44  {
45  get
46  {
47  return _fleetShares;
48  }
49  }
50  #endregion
51  #endif
52 
53  //--------------------------------------------------------------------------------------------------
54  // Methods
55  //--------------------------------------------------------------------------------------------------
56 
57  #region GetCEP
58  public bool GetCEP(string DataPath, Helpers Helper)
59  {
60  if (!CEPS.ContainsKey(Helper.gClass))
61  {
62  if (!Load(DataPath, Helper))
63  {
64  return false;
65  }
66  }
67  return true;
68  }
69  #endregion
70 
71  #if FLEET
72  #region GetFleetCEP
73  public bool GetFleetCEP(string DataPath, string AggClass, Helpers Helper)
74  {
75  if (!CEPS.ContainsKey(Helper.gClass))
76  {
77  if (Constants.AGGREGATED_VEHICLECLASSES.Contains(AggClass))
78  {
79  List<CEP> weightedCEPS = new List<CEP>();
80 
81  if (FleetShares.ContainsKey(AggClass))
82  {
83  foreach (string aggVehClass in FleetShares[AggClass].Keys)
84  {
85  if (!Helper.setclass(aggVehClass))
86  {
87  return false;
88  }
89  if (!CEPS.ContainsKey(aggVehClass) && !Load(DataPath, Helper))
90  {
91  return false;
92  }
93  weightedCEPS.Add(CEPS[aggVehClass] * FleetShares[AggClass][aggVehClass]);
94  }
95  _ceps.Add(AggClass, CEP.AddRangeCeps(weightedCEPS.ToArray(), Helper));
96 
97  //Set the vehicle class back
98  Helper.gClass = AggClass;
99  }
100  else
101  {
102  Helper.ErrMsg = "The aggregated vehicle class (" + AggClass + ") is not available in the FleetShare file!";
103  return false;
104  }
105  }
106  else
107  {
108  Helper.ErrMsg = "The aggregated vehicle class (" + AggClass + ") is a unknown class!";
109  return false;
110  }
111  }
112  return true;
113  }
114  #endregion
115  #endif
116 
117  //--------------------------------------------------------------------------------------------------
118  // Methods
119  //--------------------------------------------------------------------------------------------------
120 
121  #region Load
122  private bool Load(string DataPath, Helpers Helper)
123  {
124  //Deklaration
125  // get string identifier for PHEM emission class
126  string emissionRep = Helper.gClass.ToString();
127 
128  // to hold everything.
129  List<List<double>> matrixSpeedInertiaTable;
130  List<List<double>> normedTragTableSpeedInertiaTable;
131  List<List<double>> matrixFC;
132  List<List<double>> matrixPollutants;
133  List<double> idlingValuesFC;
134  List<double> idlingValuesPollutants;
135  List<string> headerFC;
136  List<string> headerPollutants;
137 
138  double vehicleMass;
139  double vehicleLoading;
140  double vehicleMassRot;
141  double crosssectionalArea;
142  double cwValue;
143  double f0;
144  double f1;
145  double f2;
146  double f3;
147  double f4;
148  double axleRatio;
149  List<double> transmissionGearRatios;
150  double auxPower;
151  double ratedPower;
152  double engineIdlingSpeed;
153  double engineRatedSpeed;
154  double effectiveWhellDiameter;
155  string vehicleMassType;
156  string vehicleFuelType;
157  double pNormV0;
158  double pNormP0;
159  double pNormV1;
160  double pNormP1;
161 
162  if (!ReadVehicleFile(DataPath,
163  emissionRep,
164  Helper,
165  out vehicleMass,
166  out vehicleLoading,
167  out vehicleMassRot,
168  out crosssectionalArea,
169  out cwValue,
170  out f0,
171  out f1,
172  out f2,
173  out f3,
174  out f4,
175  out axleRatio,
176  out auxPower,
177  out ratedPower,
178  out engineIdlingSpeed,
179  out engineRatedSpeed,
180  out effectiveWhellDiameter,
181  out transmissionGearRatios,
182  out vehicleMassType,
183  out vehicleFuelType,
184  out pNormV0,
185  out pNormP0,
186  out pNormV1,
187  out pNormP1,
188  out matrixSpeedInertiaTable,
189  out normedTragTableSpeedInertiaTable))
190  return false;
191 
192  if (!ReadEmissionData(true, DataPath, emissionRep, Helper, out headerFC, out matrixFC, out idlingValuesFC))
193  return false;
194 
195  if (!ReadEmissionData(false, DataPath, emissionRep, Helper, out headerPollutants, out matrixPollutants, out idlingValuesPollutants))
196  return false;
197 
198  _ceps.Add(Helper.gClass, new CEP(vehicleMassType == Constants.HeavyVehicle,
199  vehicleMass,
200  vehicleLoading,
201  vehicleMassRot,
202  crosssectionalArea,
203  cwValue,
204  f0,
205  f1,
206  f2,
207  f3,
208  f4,
209  axleRatio,
210  transmissionGearRatios,
211  auxPower,
212  ratedPower,
213  engineIdlingSpeed,
214  engineRatedSpeed,
215  effectiveWhellDiameter,
216  pNormV0,
217  pNormP0,
218  pNormV1,
219  pNormP1,
220  vehicleFuelType,
221  matrixFC,
222  headerPollutants,
223  matrixPollutants,
224  matrixSpeedInertiaTable,
225  normedTragTableSpeedInertiaTable,
226  idlingValuesFC.First(),
227  idlingValuesPollutants));
228 
229  return true;
230  }
231  #endregion
232 
233  #region ReadVehicleFile
234  private bool ReadVehicleFile(string DataPath,
235  string emissionClass,
236  Helpers Helper,
237  out double vehicleMass,
238  out double vehicleLoading,
239  out double vehicleMassRot,
240  out double crossArea,
241  out double cWValue,
242  out double f0,
243  out double f1,
244  out double f2,
245  out double f3,
246  out double f4,
247  out double axleRatio,
248  out double auxPower,
249  out double ratedPower,
250  out double engineIdlingSpeed,
251  out double engineRatedSpeed,
252  out double effectiveWheelDiameter,
253  out List<double> transmissionGearRatios,
254  out string vehicleMassType,
255  out string vehicleFuelType,
256  out double pNormV0,
257  out double pNormP0,
258  out double pNormV1,
259  out double pNormP1,
260  out List<List<double>> matrixSpeedInertiaTable,
261  out List<List<double>> normedDragTable)
262  {
263  vehicleMass = 0;
264  vehicleLoading = 0;
265  vehicleMassRot = 0;
266  crossArea = 0;
267  cWValue = 0;
268  f0 = 0;
269  f1 = 0;
270  f2 = 0;
271  f3 = 0;
272  f4 = 0;
273  axleRatio = 0;
274  ratedPower = 0;
275  auxPower = 0;
276  engineIdlingSpeed = 0;
277  engineRatedSpeed = 0;
278  effectiveWheelDiameter = 0;
279  vehicleMassType = "";
280  vehicleFuelType = "";
281  pNormV0 = 0;
282  pNormP0 = 0;
283  pNormV1 = 0;
284  pNormP1 = 0;
285  transmissionGearRatios = new List<double>();
286  matrixSpeedInertiaTable = new List<List<double>>();
287  normedDragTable = new List<List<double>>();
288  string line;
289  string cell;
290  int dataCount = 0;
291 
292  //Open file
293  string path = DataPath + @"\" + emissionClass + ".PHEMLight.veh";
294  if (!File.Exists(@path))
295  {
296  Helper.ErrMsg = "File do not exist! (" + path + ")";
297  return false;
298  }
299  StreamReader vehicleReader = File.OpenText(@path);
300 
301  // skip header
302  ReadLine(vehicleReader);
303 
304  while ((line = ReadLine(vehicleReader)) != null && dataCount <= 49)
305  {
306  if (line.Substring(0, 1) == Helper.CommentPrefix)
307  {
308  continue;
309  }
310  else
311  {
312  dataCount++;
313  }
314 
315  cell = split(line, ',')[0];
316 
317  // reading Mass
318  if (dataCount == 1)
319  vehicleMass = todouble(cell);
320 
321  // reading vehicle loading
322  if (dataCount == 2)
323  vehicleLoading = todouble(cell);
324 
325  // reading cWValue
326  if (dataCount == 3)
327  cWValue = todouble(cell);
328 
329  // reading crossectional area
330  if (dataCount == 4)
331  crossArea = todouble(cell);
332 
333  // reading vehicle mass rotational
334  if (dataCount == 7)
335  vehicleMassRot = todouble(cell);
336 
337  // reading rated power
338  if (dataCount == 9)
339  auxPower = todouble(cell);
340 
341  // reading rated power
342  if (dataCount == 10)
343  ratedPower = todouble(cell);
344 
345  // reading engine rated speed
346  if (dataCount == 11)
347  engineRatedSpeed = todouble(cell);
348 
349  // reading engine idling speed
350  if (dataCount == 12)
351  engineIdlingSpeed = todouble(cell);
352 
353  // reading f0
354  if (dataCount == 14)
355  f0 = todouble(cell);
356 
357  // reading f1
358  if (dataCount == 15)
359  f1 = todouble(cell);
360 
361  // reading f2
362  if (dataCount == 16)
363  f2 = todouble(cell);
364 
365  // reading f3
366  if (dataCount == 17)
367  f3 = todouble(cell);
368 
369  // reading f4
370  if (dataCount == 18)
371  f4 = todouble(cell);
372 
373  // reading axleRatio
374  if (dataCount == 21)
375  axleRatio = todouble(cell);
376 
377  // reading effective wheel diameter
378  if (dataCount == 22)
379  effectiveWheelDiameter = todouble(cell);
380 
381  if (dataCount >= 23 && dataCount <= 40)
382  transmissionGearRatios.Add(todouble(cell));
383 
384  // reading vehicleMassType
385  if (dataCount == 45)
386  vehicleMassType = cell;
387 
388  // reading vehicleFuelType
389  if (dataCount == 46)
390  vehicleFuelType = cell;
391 
392  // reading pNormV0
393  if (dataCount == 47)
394  pNormV0 = todouble(cell);
395 
396  // reading pNormP0
397  if (dataCount == 48)
398  pNormP0 = todouble(cell);
399 
400  // reading pNormV1
401  if (dataCount == 49)
402  pNormV1 = todouble(cell);
403 
404  // reading pNormP1
405  if (dataCount == 50)
406  pNormP1 = todouble(cell);
407  }
408 
409  while ((line = ReadLine(vehicleReader)) != null && line.Substring(0, 1) != Helper.CommentPrefix)
410  {
411  if (line.Substring(0, 1) == Helper.CommentPrefix)
412  continue;
413 
414  matrixSpeedInertiaTable.Add(todoubleList(split(line, ',')));
415  }
416 
417  while ((line = ReadLine(vehicleReader)) != null)
418  {
419  if (line.Substring(0, 1) == Helper.CommentPrefix)
420  continue;
421 
422  normedDragTable.Add(todoubleList(split(line, ',')));
423  }
424 
425  vehicleReader.Close();
426  return true;
427  }
428  #endregion
429 
430  #region ReadEmissionData
431  private bool ReadEmissionData(bool readFC,
432  string DataPath,
433  string emissionClass,
434  Helpers Helper,
435  out List<string> header,
436  out List<List<double>> matrix,
437  out List<double> idlingValues)
438  {
439  // declare file stream
440  string line;
441  header = new List<string>();
442  matrix = new List<List<double>>();
443  idlingValues = new List<double>();
444 
445  string pollutantExtension = "";
446  if (readFC)
447  pollutantExtension += "_FC";
448 
449  string path = DataPath + @"\" + emissionClass + pollutantExtension + ".csv";
450  if (!File.Exists(path))
451  {
452  Helper.ErrMsg = "File do not exist! (" + path + ")";
453  return false;
454  }
455  StreamReader fileReader = File.OpenText(@path);
456 
457  // read header line for pollutant identifiers
458  if ((line = ReadLine(fileReader)) != null)
459  {
460  List<string> entries = split(line, ',');
461  // skip first entry "Pe"
462  for (int i = 1; i < entries.Count; i++)
463  {
464  header.Add(entries[i]);
465  }
466  }
467 
468  // skip units
469  ReadLine(fileReader);
470 
471  // skip comment
472  ReadLine(fileReader);
473 
474  //readIdlingValues
475  line = ReadLine(fileReader);
476 
477  List<string> stringIdlings = split(line, ',').ToList();
478  stringIdlings.RemoveAt(0);
479 
480  idlingValues = todoubleList(stringIdlings);
481 
482  while ((line = ReadLine(fileReader)) != null)
483  {
484  matrix.Add(todoubleList(split(line, ',')));
485  }
486  fileReader.Close();
487  return true;
488  }
489  #endregion
490 
491  #if FLEET
492  #region ReadFleetShares
493  public bool ReadFleetShares(string DataPath, Helpers Helper)
494  {
495  //Declaration
496  string line;
497  string path = DataPath + @"\FleetShares.csv";
498  if (!File.Exists(@path))
499  {
500  Helper.ErrMsg = "FleetShares file does not exist! (" + path + ")";
501  return false;
502  }
503  StreamReader shareReader = File.OpenText(@path);
504 
505  _fleetShares = new Dictionary<string, Dictionary<string, double>>();
506 
507  while ((line = ReadLine(shareReader)) != null)
508  {
509  if (line.Substring(0, 1) == Helper.CommentPrefix)
510  continue;
511 
512  List<string> splitLine = split(line, ',');
513  string aggregateClass = splitLine[0];
514 
515  if (!FleetShares.ContainsKey(aggregateClass))
516  FleetShares.Add(aggregateClass, new Dictionary<string, double>());
517 
518  string subClass = splitLine[1];
519 
520  if (!FleetShares[aggregateClass].ContainsKey(subClass))
521  FleetShares[aggregateClass].Add(subClass, todouble(splitLine[2]));
522  }
523  return true;
524  }
525  #endregion
526  #endif
527 
528  //--------------------------------------------------------------------------------------------------
529  // Functions
530  //--------------------------------------------------------------------------------------------------
531 
532  #region Functions
533  //Split the string
534  private List<string> split(string s, char delim)
535  {
536  return s.Split(delim).ToList();
537  }
538 
539  //Convert string to double
540  private double todouble(string s)
541  {
542  return double.Parse(s, CultureInfo.InvariantCulture);
543  }
544 
545  //Convert string to double list
546  private List<double> todoubleList(List<string> s)
547  {
548  return s.Select(p => todouble(p)).Cast<double>().ToList();
549  }
550 
551  //Read a line from file
552  private string ReadLine(StreamReader s)
553  {
554  return s.ReadLine();
555  }
556  #endregion
557  }
558 }
bool ReadFleetShares(string DataPath, Helpers Helper)
std::vector< std::string > split(const std::string &s, char delim)
Dictionary< string, Dictionary< string, double > > FleetShares
Dictionary< string, CEP > CEPS
bool Load(const std::vector< std::string > &DataPath, Helpers *Helper)
List< string > split(string s, char delim)
bool ReadVehicleFile(string DataPath, string emissionClass, Helpers Helper, out double vehicleMass, out double vehicleLoading, out double vehicleMassRot, out double crossArea, out double cWValue, out double f0, out double f1, out double f2, out double f3, out double f4, out double axleRatio, out double auxPower, out double ratedPower, out double engineIdlingSpeed, out double engineRatedSpeed, out double effectiveWheelDiameter, out List< double > transmissionGearRatios, out string vehicleMassType, out string vehicleFuelType, out double pNormV0, out double pNormP0, out double pNormV1, out double pNormP1, out List< List< double >> matrixSpeedInertiaTable, out List< List< double >> normedDragTable)
List< double > todoubleList(List< string > s)
bool GetFleetCEP(string DataPath, string AggClass, Helpers Helper)
bool Load(string DataPath, 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)
std::map< std::string, CEP * > _ceps
Dictionary< string, CEP > _ceps
string ReadLine(StreamReader s)
std::vector< double > todoubleList(const std::vector< std::string > &s)
Dictionary< string, Dictionary< string, double > > _fleetShares
bool GetCEP(string DataPath, Helpers Helper)
bool ReadEmissionData(bool readFC, string DataPath, string emissionClass, Helpers Helper, out List< string > header, out List< List< double >> matrix, out List< double > idlingValues)
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)
static CEP AddRangeCeps(CEP[] cps, Helpers Helper)
static string[] AGGREGATED_VEHICLECLASSES
static const std::string HeavyVehicle
Definition: cpp/Constants.h:44
bool setclass(const std::string &VEH)