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