SUMO - Simulation of Urban MObility
Start.cs
Go to the documentation of this file.
1 #define FLEET
2 using System;
3 using System.IO;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Reflection;
10 
11 namespace PHEMlightdll
12 {
13  public class Start
14  {
15  private string _DataPath;
17  public Helpers Helper = new Helpers();
18 
19  //******************* Parameters of Array or Single calculation *******************
20  //********************************* INPUT ******************************************
21  //*** DATATYP | UNIT | VARIBLE | Description ***
22  //string | [-] | VEH | Name of vehicle file (e.g. "PC_D_EU4" path neede if not in "Default Vehicles") or aggregated name (PC, HDV, BUS, TW) by FleetMix calculation
23  //List<double> / double | [s] | Time | Time signal
24  //List<double> / double | [m/s] | Velocity | Velocity signal
25  //double | [m/s^2]| acc | Acceleration (ONLY NEDDED BY SINGLE CALCULATION)
26  //List<double> / double | [%] | Gradient | Gradient of the route
27  //out List<VehicleResult> | [-] | VehicleResultsOrg | Returned result list
28  //bool | [-] | fleetMix = false | Optional parameter if fleetMix should be calculate
29  //string | [-] | PHEMDataV = "V4" | Optional parameter for "Default Vehicles"
30  //string | [-] | CommentPref = "c" | Optional parameter for comment prefix
31 
32  //********************************* OUPUT: VehicleResultsOrg **********************
33  //*** DATATYP | UNIT | VARIBLE | Description ***
34  //double | [s] | time | Time
35  //double | [m/s] | speed | Velocity
36  //double | [kW] | power | Calculated power at the engine (ICE for conventional and HEV vehicles, electric engine for BEVs) including engine inertia and auxiliaries; not limited for engine fullload and braking limitations
37  //double | [kW] | P_pos | Positive engine power limited with engine rated power
38  //double | [-] | pNormRated | Engine power normalised with rated engine power and limited with the power range (fullload and drag) as specified in the characteristic curve for fuel consumption
39  //double | [-] | pNormDrive | Engine power normalised with "P_drive" and limited with the power range (fullload and drag) as specified in the characteristic curve for emissions
40  //double | [m/s^2]| acc | Caclulated/given acceleration
41  //double | [g/h] | fc | Calculated fuel consumption (Different unit for BEV vehicles)
42  // | [kWh/h]| |
43  //double | [g/h] | cO2 | Calculated CO2 emissions
44  //double | [g/h] | NOx | Calculated NOx emissions
45  //double | [g/h] | HC | Calculated HC emissions
46  //double | [g/h] | PM | Calculated PM emissions
47  //double | [g/h] | CO | Calculated CO emissions
48 
49  #region calculate
50  //Calculate data from array
51  public bool CALC_Array(string VEH,
52  List<double> Time,
53  List<double> Velocity,
54  List<double> Gradient,
55  out List<VehicleResult> VehicleResultsOrg,
56  bool fleetMix = false,
57  string PHEMDataV = "V4",
58  string CommentPref = "c")
59  {
60  //Declaration
61  int i;
62  double acc;
63  List<VehicleResult> _VehicleResult = new List<VehicleResult>();
64 
65  //Initialisation
66  Helper.ErrMsg = null;
67 
68  //Borrow
69  Helper.CommentPrefix = CommentPref;
70  Helper.PHEMDataV = PHEMDataV;
71  if ((VEH.LastIndexOf(@"\")) >= 0)
72  _DataPath = VEH.Substring(0, VEH.LastIndexOf(@"\"));
73  else
74  _DataPath = Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf(@"\")) + @"\Default Vehicles\" + Helper.PHEMDataV;
75 
76  //Read the vehicle and emission data
77  #if FLEET
78  if (fleetMix)
79  {
80  //Set the vehicle class
81  Helper.gClass = VEH;
82 
83  //Generate the class
84  DataInput = new CEPHandler();
85 
86  //Read the FleetShares
87  if (!DataInput.ReadFleetShares(_DataPath, Helper))
88  {
89  VehicleResultsOrg = null;
90  return false;
91  }
92  //Read the vehicle and emission data
93  if (!DataInput.GetFleetCEP(_DataPath, VEH, Helper))
94  {
95  VehicleResultsOrg = null;
96  return false;
97  }
98  }
99  else
100  #endif
101  {
102  //Get vehicle string
103  if (!Helper.setclass(VEH))
104  {
105  VehicleResultsOrg = null;
106  return false;
107  }
108 
109  //Generate the class
110  DataInput = new CEPHandler();
111 
112  //Read the vehicle and emission data
113  if (!DataInput.GetCEP(_DataPath, Helper))
114  {
115  VehicleResultsOrg = null;
116  return false;
117  }
118  }
119 
120  //Calculate emissions per second
121  for (i = 1; i <= Time.Count - 1; i++)
122  {
123  //Calculate the acceleration
124  acc = (Velocity[i] - Velocity[i - 1]) / (Time[i] - Time[i - 1]);
125 
126  //Calculate and save the data in the List
127  _VehicleResult.Add(PHEMLight.CreateVehicleStateData(Helper,
128  DataInput.CEPS[Helper.gClass],
129  Time[i - 1],
130  Velocity[i - 1],
131  acc,
132  Gradient[i - 1]));
133  if (Helper.ErrMsg != null)
134  {
135  VehicleResultsOrg = null;
136  return false;
137  }
138  }
139  VehicleResultsOrg = _VehicleResult;
140  return true;
141  }
142 
143  //Calculate single data
144  public bool CALC_Single(string VEH,
145  double Time,
146  double Velocity,
147  double acc,
148  double Gradient,
149  out List<VehicleResult> VehicleResultsOrg,
150  bool fleetMix = false,
151  string PHEMDataV = "V4",
152  string CommentPref = "c")
153  {
154  //Declaration
155  List<VehicleResult> _VehicleResult = new List<VehicleResult>();
156  VehicleResultsOrg = _VehicleResult;
157 
158  //Borrow
159  Helper.CommentPrefix = CommentPref;
160  Helper.PHEMDataV = PHEMDataV;
161  if ((VEH.LastIndexOf(@"\")) >= 0)
162  _DataPath = VEH.Substring(0, VEH.LastIndexOf(@"\"));
163  else
164  _DataPath = Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf(@"\")) + @"\Default Vehicles\" + Helper.PHEMDataV;
165 
166  //Read the vehicle and emission data
167  #if FLEET
168  if (fleetMix)
169  {
170  //Set the vehicle class
171  Helper.gClass = "AggClass_" + VEH;
172 
173  //Generate the class
174  DataInput = new CEPHandler();
175 
176  //Read the FleetShares
177  if (!DataInput.ReadFleetShares(_DataPath, Helper))
178  {
179  VehicleResultsOrg = null;
180  return false;
181  }
182  //Read the vehicle and emission data
183  if (!DataInput.GetFleetCEP(_DataPath, VEH, Helper))
184  {
185  VehicleResultsOrg = null;
186  return false;
187  }
188  }
189  else
190  #endif
191  {
192  //Get vehicle string
193  if (!Helper.setclass(VEH))
194  {
195  VehicleResultsOrg = null;
196  return false;
197  }
198 
199  //Generate the class
200  DataInput = new CEPHandler();
201 
202  //Read the vehicle and emission data
203  if (!DataInput.GetCEP(_DataPath, Helper))
204  {
205  VehicleResultsOrg = null;
206  return false;
207  }
208  }
209 
210  //Calculate and save the data in the List
211  _VehicleResult.Add(PHEMLight.CreateVehicleStateData(Helper,
212  DataInput.CEPS[Helper.gClass],
213  Time,
214  Velocity,
215  acc,
216  Gradient));
217  VehicleResultsOrg = _VehicleResult;
218  return true;
219  }
220  #endregion
221 
222  #region ExportData
223  public bool ExportData(string path, string vehicle, List<VehicleResult> _VehicleResult)
224  {
225  if (path == null || vehicle == null || _VehicleResult == null || _VehicleResult.Count == 0) return false;
226 
227  //Write head
228  StringBuilder allLines = new StringBuilder();
229  string lineEnding = "\r\n";
230 
231  allLines.AppendLine("Vehicletype: ," + vehicle);
232  allLines.AppendLine("Time, Speed, Gradient, Accelaration, Engine power raw, P_pos, P_norm_rated, P_norm_drive, FC, FC el., CO2, NOx, CO, HC, PM");
233  allLines.AppendLine("[s], [m/s], [%], [m/s^2], [kW], [kW], [-], [-], [g/h], [kWh/h], [g/h], [g/h], [g/h], [g/h], [g/h]");
234 
235  //Write data
236  foreach (VehicleResult Result in _VehicleResult)
237  {
238  allLines.Append(Result.Time.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
239  allLines.Append(Result.Speed.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
240  allLines.Append(Result.Grad.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
241  allLines.Append(Result.Accelaration.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
242  allLines.Append(Result.Power.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
243  allLines.Append(Result.PPos.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
244  allLines.Append(Result.PNormRated.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
245  allLines.Append(Result.PNormDrive.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
246  allLines.Append(Result.EmissionData.FC.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
247  allLines.Append(Result.EmissionData.FCel.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
248  allLines.Append(Result.EmissionData.CO2.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
249  allLines.Append(Result.EmissionData.NOx.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
250  allLines.Append(Result.EmissionData.CO.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
251  allLines.Append(Result.EmissionData.HC.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
252  allLines.Append(Result.EmissionData.PM.ToString("0.0000", CultureInfo.InvariantCulture) + lineEnding);
253  }
254 
255  // Write the string to a file.
256  if (path.IndexOf(".", 0) < 0)
257  {
258  path = path + ".sta";
259  }
260  try
261  {
262  StreamWriter file = new StreamWriter(path);
263  file.WriteLine(allLines);
264  file.Close();
265  return true;
266  }
267  catch (Exception ex)
268  {
269  return false;
270  }
271  }
272 
273  public bool ExportSumData(string path, string vehicle, string cycle, VehicleResult _VehicleResult)
274  {
275  if (path == null || vehicle == null || cycle == null || _VehicleResult == null) return false;
276  StringBuilder allLines = new StringBuilder();
277 
278  if (path.IndexOf(".", 0) < 0)
279  {
280  path = path + ".erg";
281  }
282 
283  if (!File.Exists(path))
284  {
285  //Write head
286  allLines.AppendLine("PHEMLight Results");
287  allLines.AppendLine("");
288  allLines.AppendLine("Vehicle, Cycle, Time, Speed, Gradient, Accelaration, Engine power raw, P_pos, P_norm_rated, P_norm_drive, FC, FC el., CO2, NOx, CO, HC, PM");
289  allLines.AppendLine("[-], [-], [s], [km/h], [%], [m/s^2], [kW], [kW], [-], [-], [g/km], [kWh/km], [g/km], [g/km], [g/km], [g/km], [g/km]");
290  }
291 
292  //Write data
293  allLines.Append(vehicle + ",");
294  allLines.Append(cycle + ",");
295  allLines.Append(_VehicleResult.Time.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
296  allLines.Append(_VehicleResult.Speed.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
297  allLines.Append(_VehicleResult.Grad.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
298  allLines.Append(_VehicleResult.Accelaration.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
299  allLines.Append(_VehicleResult.Power.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
300  allLines.Append(_VehicleResult.PPos.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
301  allLines.Append(_VehicleResult.PNormRated.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
302  allLines.Append(_VehicleResult.PNormDrive.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
303  allLines.Append(_VehicleResult.EmissionData.FC.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
304  allLines.Append(_VehicleResult.EmissionData.FCel.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
305  allLines.Append(_VehicleResult.EmissionData.CO2.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
306  allLines.Append(_VehicleResult.EmissionData.NOx.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
307  allLines.Append(_VehicleResult.EmissionData.CO.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
308  allLines.Append(_VehicleResult.EmissionData.HC.ToString("0.0000", CultureInfo.InvariantCulture) + ",");
309  allLines.Append(_VehicleResult.EmissionData.PM.ToString("0.0000", CultureInfo.InvariantCulture));
310 
311  // Write the string to a file.
312  try
313  {
314  StreamWriter file = new StreamWriter(path, true);
315  file.WriteLine(allLines);
316  file.Close();
317  return true;
318  }
319  catch (Exception ex)
320  {
321  return false;
322  }
323  }
324 
325  public VehicleResult GenerateSumData(List<VehicleResult> _VehicleResult)
326  {
327  //Declaration
328  double sum_time = 0;
329  double sum_speed = 0;
330  double sum_grad = 0;
331  double sum_power = 0;
332  double sum_pPos = 0;
333  double sum_pNormRated = 0;
334  double sum_pNormDrive = 0;
335  double sum_acc = 0;
336  double sum_fc = 0;
337  double sum_fcel = 0;
338  double sum_cO2 = 0;
339  double sum_nOx = 0;
340  double sum_hC = 0;
341  double sum_pM = 0;
342  double sum_cO = 0;
343 
344  if (_VehicleResult == null || _VehicleResult.Count == 0) return new VehicleResult(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
345 
346  //Write data
347  foreach (VehicleResult Result in _VehicleResult)
348  {
349  sum_speed += Result.Speed * 3.6;
350  sum_power += Result.Power;
351  if (Result.PPos > 0) { sum_pPos += Result.PPos; }
352  sum_grad += Result.Grad;
353  sum_pNormRated += Result.PNormRated;
354  sum_pNormDrive += Result.PNormDrive;
355  sum_acc += Result.Accelaration;
356  sum_fcel += Result.EmissionData.FCel;
357  sum_fc += Result.EmissionData.FC;
358  sum_cO2 += Result.EmissionData.CO2;
359  sum_nOx += Result.EmissionData.NOx;
360  sum_hC += Result.EmissionData.HC;
361  sum_pM += Result.EmissionData.PM;
362  sum_cO += Result.EmissionData.CO;
363  }
364 
365  //Build average
366  sum_time = _VehicleResult[_VehicleResult.Count - 1].Time - _VehicleResult[0].Time;
367  sum_power /= _VehicleResult.Count;
368  sum_pPos /= _VehicleResult.Count;
369  sum_grad /= _VehicleResult.Count;
370  sum_pNormRated /= _VehicleResult.Count;
371  sum_pNormDrive /= _VehicleResult.Count;
372  sum_acc /= _VehicleResult.Count;
373  if (sum_speed > 0)
374  {
375  sum_fc /= sum_speed;
376  sum_fcel /= sum_speed;
377  sum_cO2 /= sum_speed;
378  sum_nOx /= sum_speed;
379  sum_hC /= sum_speed;
380  sum_pM /= sum_speed;
381  sum_cO /= sum_speed;
382  }
383  else
384  {
385  sum_fc = 0;
386  sum_fcel = 0;
387  sum_cO2 = 0;
388  sum_nOx = 0;
389  sum_hC = 0;
390  sum_pM = 0;
391  sum_cO = 0;
392  }
393  sum_speed /= _VehicleResult.Count;
394 
395  return new VehicleResult(sum_time,
396  sum_speed,
397  sum_grad,
398  sum_power,
399  sum_pPos,
400  sum_pNormRated,
401  sum_pNormDrive,
402  sum_acc,
403  sum_fc,
404  sum_fcel,
405  sum_cO2,
406  sum_nOx,
407  sum_hC,
408  sum_pM,
409  sum_cO);
410  }
411  #endregion
412  }
413 
414  //Calculation
415  class PHEMLight
416  {
417  #region CreateVehicleStateData
419  CEP currCep,
420  double time,
421  double inputSpeed,
422  double inputAcc,
423  double Gradient = 0)
424  {
425  //Declaration
426  double speed = Math.Max(inputSpeed, 0);
427  double acc;
428  double P_pos;
429 
430  //Speed/Acceleration limitation
431  if (speed == 0)
432  acc = 0;
433  else
434  acc = inputAcc;
435 
436  //Calculate the power
437  double power = currCep.CalcPower(speed, acc, Gradient);
438  double P_eng = currCep.CalcEngPower(power);
439 
440  //Power limitation
441  if (P_eng >= 0)
442  P_pos = power;
443  else
444  P_pos = 0;
445 
446  //Calculate the result values (BEV)
447  if (Helper.tClass == Constants.strBEV)
448  {
449  return new VehicleResult(time,
450  speed,
451  Gradient,
452  power,
453  P_pos,
454  P_eng / currCep.RatedPower,
455  P_eng / currCep.DrivingPower,
456  acc,
457  0,
458  currCep.GetEmission("FC", power, speed, Helper),
459  0,
460  0,
461  0,
462  0,
463  0);
464  }
465 
466  //Calculate the decel costing
467  double decelCoast = currCep.GetDecelCoast(speed, acc, Gradient);
468 
469  //Calculate the result values (Zero emissions by costing)
470  if (acc >= decelCoast)
471  {
472  return new VehicleResult(time,
473  speed,
474  Gradient,
475  power,
476  P_pos,
477  P_eng / currCep.RatedPower,
478  P_eng / currCep.DrivingPower,
479  acc,
480  currCep.GetEmission("FC", power, speed, Helper),
481  0,
482  currCep.GetCO2Emission(currCep.GetEmission("FC", power, speed, Helper), currCep.GetEmission("CO", power, speed, Helper), currCep.GetEmission("HC", power, speed, Helper), Helper),
483  currCep.GetEmission("NOx", power, speed, Helper),
484  currCep.GetEmission("HC", power, speed, Helper),
485  currCep.GetEmission("PM", power, speed, Helper),
486  currCep.GetEmission("CO", power, speed, Helper));
487  }
488  else
489  {
490  return new VehicleResult(time,
491  speed,
492  Gradient,
493  power,
494  P_pos,
495  power / currCep.RatedPower,
496  power / currCep.DrivingPower,
497  acc,
498  0,
499  0,
500  0,
501  0,
502  0,
503  0,
504  0);
505  }
506  }
507  #endregion
508  }
509 }
double GetEmission(const std::string &pollutant, double power, double speed, Helpers *VehicleClass)
Definition: CEP.cpp:230
bool setclass(const std::string &VEH)
Definition: Helpers.cpp:241
double DrivingPower
Definition: CEP.cs:314
bool GetFleetCEP(string DataPath, string AggClass, Helpers Helper)
Definition: CEPHandler.cs:73
Helpers Helper
Definition: Start.cs:17
bool GetCEP(const std::vector< std::string > &DataPath, Helpers *Helper)
Definition: CEPHandler.cpp:43
#define FLEET
Definition: Start.cs:1
bool ExportData(string path, string vehicle, List< VehicleResult > _VehicleResult)
Definition: Start.cs:223
bool ReadFleetShares(string DataPath, Helpers Helper)
Definition: CEPHandler.cs:493
double GetDecelCoast(double speed, double acc, double gradient)
Definition: CEP.cpp:321
double RatedPower
Definition: CEP.cs:288
CEPHandler DataInput
Definition: Start.cs:16
double CalcEngPower(double power)
Definition: CEP.cpp:219
VehicleResult GenerateSumData(List< VehicleResult > _VehicleResult)
Definition: Start.cs:325
bool ExportSumData(string path, string vehicle, string cycle, VehicleResult _VehicleResult)
Definition: Start.cs:273
static const std::string strBEV
Definition: Constants.h:65
string _DataPath
Definition: Start.cs:15
Dictionary< string, CEP > CEPS
Definition: CEPHandler.cs:32
EmissionData EmissionData
Definition: cResult.cs:153
double CalcPower(double speed, double acc, double gradient)
Definition: CEP.cpp:200
static VehicleResult CreateVehicleStateData(Helpers Helper, CEP currCep, double time, double inputSpeed, double inputAcc, double Gradient=0)
Definition: Start.cs:418
bool CALC_Single(string VEH, double Time, double Velocity, double acc, double Gradient, out List< VehicleResult > VehicleResultsOrg, bool fleetMix=false, string PHEMDataV="V4", string CommentPref="c")
Definition: Start.cs:144
string CommentPrefix
Definition: Helpers.cs:53
double GetCO2Emission(double _FC, double _CO, double _HC, Helpers *VehicleClass)
Definition: CEP.cpp:291
bool CALC_Array(string VEH, List< double > Time, List< double > Velocity, List< double > Gradient, out List< VehicleResult > VehicleResultsOrg, bool fleetMix=false, string PHEMDataV="V4", string CommentPref="c")
Definition: Start.cs:51