SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
emissionsDrivingCycle_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Main for an emissions map writer
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #ifdef HAVE_VERSION_H
32 #include <version.h>
33 #endif
34 
36 #include <iostream>
37 #include <string>
38 #include <ctime>
40 #include <utils/options/Option.h>
46 #include <utils/common/ToString.h>
47 #include <utils/xml/XMLSubSys.h>
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 
59 
60 // ===========================================================================
61 // functions
62 // ===========================================================================
63 
64 
65 /* -------------------------------------------------------------------------
66  * main
67  * ----------------------------------------------------------------------- */
68 int
69 main(int argc, char** argv) {
70  // build options
72  // give some application descriptions
73  oc.setApplicationDescription("Computes emission by driving a time line.");
74  oc.setApplicationName("emissionsTimeline", "SUMO emissionsTimeline Version " + (std::string)VERSION_STRING);
75  // add options
76 
77  oc.addOptionSubTopic("Input");
78  oc.doRegister("timeline-file", 't', new Option_FileName());
79  oc.addSynonyme("timeline", "timeline-file");
80  oc.addDescription("timeline-file", "Input", "Defines the file to read the driving cycle from.");
81 
82  oc.doRegister("emission-class", 'e', new Option_String());
83  oc.addDescription("emission-class", "Input", "Defines for which emission class the emissions shall be generated. ");
84 
85 
86  oc.addOptionSubTopic("Processing");
87  oc.doRegister("compute-a", 'a', new Option_Bool(false));
88  oc.addDescription("compute-a", "Processing", "If set, the acceleration is computed instead of being read from the file. ");
89 
90  oc.doRegister("skip-first", 's', new Option_Bool(false));
91  oc.addDescription("skip-first", "Processing", "If set, the first line of the read file is skipped.");
92 
93  oc.doRegister("kmh", new Option_Bool(false));
94  oc.addDescription("kmh", "Processing", "If set, the given speed is interpreted as being given in km/h.");
95 
96  oc.doRegister("have-slope", new Option_Bool(false));
97  oc.addDescription("have-slope", "Processing", "If set, the fourth column is read and used as slope (in [°]).");
98 
99  oc.doRegister("slope", new Option_Float(0));
100  oc.addDescription("slope", "Processing", "Sets a global slope (in [°]) that is used if the file does not contain slope information.");
101 
102  oc.addOptionSubTopic("Output");
103  oc.doRegister("output-file", 'o', new Option_String());
104  oc.addSynonyme("output", "output-file");
105  oc.addDescription("emission-class", "Output", "Defines the file to write the emission cycle results into. ");
106 
107  oc.addOptionSubTopic("Emissions");
108  oc.doRegister("phemlight-path", 'p', new Option_FileName("./PHEMlight/"));
109  oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from.");
110 
111  oc.addOptionSubTopic("Report");
112  oc.doRegister("quiet", 'q', new Option_Bool(false));
113  oc.addDescription("quiet", "Report", "Not writing anything.");
114  oc.doRegister("help", '?', new Option_Bool(false));
115  oc.addDescription("help", "Report", "Writes a help screen.");
116 
117  // run
118  int ret = 0;
119  bool quiet = false;
120  try {
121  // initialise the application system (messaging, xml, options)
122  XMLSubSys::init();
123  OptionsIO::getOptions(true, argc, argv);
125  if (oc.processMetaOptions(argc < 2)) {
127  return 0;
128  }
129 
130  quiet = oc.getBool("quiet");
131  if (!oc.isSet("timeline-file")) {
132  throw ProcessError("The timeline file must be given.");
133  }
134  if (!oc.isSet("output-file")) {
135  throw ProcessError("The output file must be given.");
136  }
137 
138  bool skipFirst = oc.getBool("skip-first");
139  bool computeA = oc.getBool("compute-a");
140  bool inKMH = oc.getBool("kmh");
141  bool haveSlope = oc.getBool("have-slope");
142 
143  SUMOReal sumCO, sumCO2, sumHC, sumNOx, sumPMx, sumFuel;
144  sumCO = sumCO2 = sumHC = sumNOx = sumPMx = sumFuel = 0;
145  SUMOReal l = 0;
146 
147 
148  SUMOEmissionClass c = getVehicleEmissionTypeID(oc.getString("emission-class"));
149  std::ofstream o(oc.getString("output-file").c_str());
150  LineReader lr(oc.getString("timeline-file"));
151  SUMOReal lastV = 0;
152  while (lr.hasMore()) {
153  std::string line = lr.readLine();
154  if (skipFirst) {
155  skipFirst = false;
156  continue;
157  }
158  StringTokenizer st(StringUtils::prune(line), ";");
159  if(st.size()<2) {
160  throw ProcessError("Each line must at least include the time and the speed.");
161  }
162  try {
163  SUMOReal t = TplConvert::_2SUMOReal<char>(st.next().c_str());
164  SUMOReal v = TplConvert::_2SUMOReal<char>(st.next().c_str());
165  if (inKMH) {
166  v = v / 3.6;
167  }
168  l += v;
169  SUMOReal a = 0;
170  if (!computeA) {
171  if(!st.hasNext()) {
172  throw ProcessError("Acceleration information is missing; try running with --compute-a.");
173  }
174  a = TplConvert::_2SUMOReal<char>(st.next().c_str());
175  } else {
176  a = v - lastV;
177  }
178  lastV = v;
179  SUMOReal s = oc.getFloat("slope");
180  if (haveSlope) {
181  s = TplConvert::_2SUMOReal<char>(st.next().c_str());
182  }
183 
184  SUMOReal aCO = PollutantsInterface::computeCO(c, v, a, s);
185  SUMOReal aCO2 = PollutantsInterface::computeCO2(c, v, a, s);
186  SUMOReal aHC = PollutantsInterface::computeHC(c, v, a, s);
187  SUMOReal aNOx = PollutantsInterface::computeNOx(c, v, a, s);
188  SUMOReal aPMx = PollutantsInterface::computePMx(c, v, a, s);
189  SUMOReal aFuel = PollutantsInterface::computeFuel(c, v, a, s);
190  sumCO += aCO;
191  sumCO2 += aCO2;
192  sumHC += aHC;
193  sumNOx += aNOx;
194  sumPMx += aPMx;
195  sumFuel += aFuel;
196  o << t << ";" << v << ";" << a << ";" << s
197  << ";" << aCO << ";" << aCO2 << ";" << aHC << ";" << aPMx << ";" << aNOx << ";" << aFuel << std::endl;
198  } catch (EmptyData&) {
199  throw ProcessError("Missing an entry in line '" + line + "'.");
200  } catch (NumberFormatException&) {
201  throw ProcessError("Not numeric entry in line '" + line + "'.");
202  }
203  }
204  if(!quiet) {
205  std::cout << "sums" << std::endl
206  << "length:" << l << std::endl
207  << "CO:" << sumCO << std::endl
208  << "CO2:" << sumCO2 << std::endl
209  << "HC:" << sumHC << std::endl
210  << "NOx:" << sumNOx << std::endl
211  << "PMx:" << sumPMx << std::endl
212  << "fuel:" << sumFuel << std::endl;
213  }
214  } catch (InvalidArgument& e) {
216  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
217  ret = 1;
218  } catch (ProcessError& e) {
219  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
221  }
222  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
223  ret = 1;
224 #ifndef _DEBUG
225  } catch (...) {
226  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
227  ret = 1;
228 #endif
229  }
231  if (ret == 0 && !quiet) {
232  std::cout << "Success." << std::endl;
233  }
234  return ret;
235 }
236 
237 
238 
239 /****************************************************************************/
240 
SUMOEmissionClass getVehicleEmissionTypeID(const std::string &name)
Returns the class id of the emission class given by its name.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
std::string next()
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:58
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
int main(int argc, char **argv)
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
static void close()
Closes all of an applications subsystems.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
static SUMOReal computeFuel(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of consumed fuel given the vehicle type and state (in ml/s)
SUMOEmissionClass
Definition of vehicle emission classes.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static SUMOReal computeCO2(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted CO2 given the vehicle type and state (in mg/s)
static SUMOReal computeNOx(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted NOx given the vehicle type and state (in mg/s)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static SUMOReal computeHC(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted HC given the vehicle type and state (in mg/s)
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
size_t size() const
#define VERSION_STRING
Definition: config.h:227
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
static SUMOReal computeCO(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted CO given the vehicle type and state (in mg/s)
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
#define SUMOReal
Definition: config.h:215
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static SUMOReal computePMx(SUMOEmissionClass c, double v, double a, double slope)
Returns the amount of emitted PMx given the vehicle type and state (in mg/s)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.