Eclipse SUMO - Simulation of Urban MObility
emissionsMap_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // Main for an emissions map writer
20 /****************************************************************************/
21 #include <config.h>
22 
23 #ifdef HAVE_VERSION_H
24 #include <version.h>
25 #endif
26 
28 #include <iostream>
29 #include <string>
30 #include <ctime>
32 #include <utils/options/Option.h>
38 #include <utils/common/ToString.h>
39 #include <utils/xml/XMLSubSys.h>
42 
43 
44 // ===========================================================================
45 // functions
46 // ===========================================================================
47 
48 
49 /* -------------------------------------------------------------------------
50  * main
51  * ----------------------------------------------------------------------- */
52 void single(const std::string& of, const std::string& className, SUMOEmissionClass c,
53  double vMin, double vMax, double vStep,
54  double aMin, double aMax, double aStep,
55  double sMin, double sMax, double sStep,
56  bool verbose) {
57  if (verbose) {
58  WRITE_MESSAGE("Writing map of '" + className + "' into '" + of + "'.");
59  }
60  std::ofstream o(of.c_str());
61  if (!o.good()) {
62  throw ProcessError("Could not open file '" + of + "' for writing.");
63  }
64  for (double v = vMin; v <= vMax; v += vStep) {
65  for (double a = aMin; a <= aMax; a += aStep) {
66  for (double s = sMin; s <= sMax; s += sStep) {
68  o << v << ";" << a << ";" << s << ";" << "CO" << ";" << result.CO << std::endl;
69  o << v << ";" << a << ";" << s << ";" << "CO2" << ";" << result.CO2 << std::endl;
70  o << v << ";" << a << ";" << s << ";" << "HC" << ";" << result.HC << std::endl;
71  o << v << ";" << a << ";" << s << ";" << "PMx" << ";" << result.PMx << std::endl;
72  o << v << ";" << a << ";" << s << ";" << "NOx" << ";" << result.NOx << std::endl;
73  o << v << ";" << a << ";" << s << ";" << "fuel" << ";" << result.fuel << std::endl;
74  o << v << ";" << a << ";" << s << ";" << "electricity" << ";" << result.electricity << std::endl;
75  }
76  }
77  }
78 }
79 
80 
81 
82 
83 int
84 main(int argc, char** argv) {
85  // build options
87  // give some application descriptions
88  oc.setApplicationDescription("Builds and writes an emissions map for SUMO's emission models.");
89  oc.setApplicationName("emissionsMap", "Eclipse SUMO emissionsMap Version " VERSION_STRING);
90  // add options
92  oc.addOptionSubTopic("Processing");
93  oc.doRegister("iterate", 'i', new Option_Bool(false));
94  oc.addDescription("iterate", "Processing", "If set, maps for all available emissions are written.");
95 
96  oc.doRegister("emission-class", 'e', new Option_String());
97  oc.addDescription("emission-class", "Processing", "Defines the name of the emission class to generate the map for.");
98 
99  oc.doRegister("v-min", new Option_Float(0.));
100  oc.addDescription("v-min", "Processing", "Defines the minimum velocity boundary of the map to generate (in m/s).");
101  oc.doRegister("v-max", new Option_Float(50.));
102  oc.addDescription("v-max", "Processing", "Defines the maximum velocity boundary of the map to generate (in m/s).");
103  oc.doRegister("v-step", new Option_Float(2.));
104  oc.addDescription("v-step", "Processing", "Defines the velocity step size (in m/s).");
105  oc.doRegister("a-min", new Option_Float(-4.));
106  oc.addDescription("a-min", "Processing", "Defines the minimum acceleration boundary of the map to generate (in m/s^2).");
107  oc.doRegister("a-max", new Option_Float(4.));
108  oc.addDescription("a-max", "Processing", "Defines the maximum acceleration boundary of the map to generate (in m/s^2).");
109  oc.doRegister("a-step", new Option_Float(.5));
110  oc.addDescription("a-step", "Processing", "Defines the acceleration step size (in m/s^2).");
111  oc.doRegister("s-min", new Option_Float(-10.));
112  oc.addDescription("s-min", "Processing", "Defines the minimum slope boundary of the map to generate (in deg).");
113  oc.doRegister("s-max", new Option_Float(10.));
114  oc.addDescription("s-max", "Processing", "Defines the maximum slope boundary of the map to generate (in deg).");
115  oc.doRegister("s-step", new Option_Float(1.));
116  oc.addDescription("s-step", "Processing", "Defines the slope step size (in deg).");
117 
118  oc.addOptionSubTopic("Output");
119  oc.doRegister("output-file", 'o', new Option_String());
120  oc.addSynonyme("output", "output-file");
121  oc.addDescription("output", "Output", "Defines the file (or the path if --iterate was set) to write the map(s) into.");
122 
123  oc.addOptionSubTopic("Emissions");
124  oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
125  oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from.");
126 
128 
129  // run
130  int ret = 0;
131  try {
132  // initialise the application system (messaging, xml, options)
133  XMLSubSys::init();
134  OptionsIO::setArgs(argc, argv);
136  if (oc.processMetaOptions(argc < 2)) {
138  return 0;
139  }
140 
141  double vMin = oc.getFloat("v-min");
142  double vMax = oc.getFloat("v-max");
143  double vStep = oc.getFloat("v-step");
144  double aMin = oc.getFloat("a-min");
145  double aMax = oc.getFloat("a-max");
146  double aStep = oc.getFloat("a-step");
147  double sMin = oc.getFloat("s-min");
148  double sMax = oc.getFloat("s-max");
149  double sStep = oc.getFloat("s-step");
150  if (!oc.getBool("iterate")) {
151  if (!oc.isSet("emission-class")) {
152  throw ProcessError("The emission class (-e) must be given.");
153  }
154  if (!oc.isSet("output-file")) {
155  throw ProcessError("The output file (-o) must be given.");
156  }
157  const SUMOEmissionClass c = PollutantsInterface::getClassByName(oc.getString("emission-class"));
158  single(oc.getString("output-file"), oc.getString("emission-class"),
159  c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
160  } else {
161  if (!oc.isSet("output-file")) {
162  oc.set("output-file", "./");
163  }
164  const std::vector<SUMOEmissionClass> classes = PollutantsInterface::getAllClasses();
165  for (std::vector<SUMOEmissionClass>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
166  SUMOEmissionClass c = *ci;
168  c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
169  }
170  }
171  } catch (InvalidArgument& e) {
173  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
174  ret = 1;
175  } catch (ProcessError& e) {
176  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
178  }
179  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
180  ret = 1;
181 #ifndef _DEBUG
182  } catch (...) {
183  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
184  ret = 1;
185 #endif
186  }
188  if (ret == 0) {
189  std::cout << "Success." << std::endl;
190  }
191  return ret;
192 }
193 
194 
195 /****************************************************************************/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:282
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:43
int SUMOEmissionClass
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:85
static std::string getName(const SUMOEmissionClass c)
Checks whether the string describes a known vehicle class.
static const std::vector< SUMOEmissionClass > getAllClasses()
Checks whether the string describes a known vehicle class.
static Emissions computeAll(const SUMOEmissionClass c, const double v, const double a, const double slope, const EnergyParams *param=0)
Returns the amount of all emitted pollutants given the vehicle type and state (in mg/s or ml/s for fu...
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:38
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:63
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
int main(int argc, char **argv)
void single(const std::string &of, const std::string &className, SUMOEmissionClass c, double vMin, double vMax, double vStep, double aMin, double aMax, double aStep, double sMin, double sMax, double sStep, bool verbose)
Storage for collected values of all emission types.