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.dev/sumo
3 // Copyright (C) 2013-2024 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 
27 #include <iostream>
28 #include <string>
29 #include <ctime>
32 #include <utils/options/Option.h>
39 #include <utils/common/ToString.h>
40 #include <utils/xml/XMLSubSys.h>
43 
44 
45 // ===========================================================================
46 // functions
47 // ===========================================================================
48 void single(const std::string& of, const std::string& className, SUMOEmissionClass c,
49  double vMin, double vMax, double vStep,
50  double aMin, double aMax, double aStep,
51  double sMin, double sMax, double sStep,
52  bool verbose) {
53  if (verbose) {
54  WRITE_MESSAGEF(TL("Writing map of '%' into '%'."), className, of);
55  }
56  std::ofstream o(of.c_str());
57  if (!o.good()) {
58  throw ProcessError(TLF("Could not open file '%' for writing.", of));
59  }
60  EnergyParams energyParams(c);
61  for (double v = vMin; v <= vMax; v += vStep) {
62  for (double a = aMin; a <= aMax; a += aStep) {
63  for (double s = sMin; s <= sMax; s += sStep) {
64  const PollutantsInterface::Emissions result = PollutantsInterface::computeAll(c, v, a, s, &energyParams);
65  o << v << ";" << a << ";" << s << ";" << "CO" << ";" << result.CO << std::endl;
66  o << v << ";" << a << ";" << s << ";" << "CO2" << ";" << result.CO2 << std::endl;
67  o << v << ";" << a << ";" << s << ";" << "HC" << ";" << result.HC << std::endl;
68  o << v << ";" << a << ";" << s << ";" << "PMx" << ";" << result.PMx << std::endl;
69  o << v << ";" << a << ";" << s << ";" << "NOx" << ";" << result.NOx << std::endl;
70  o << v << ";" << a << ";" << s << ";" << "fuel" << ";" << result.fuel << std::endl;
71  o << v << ";" << a << ";" << s << ";" << "electricity" << ";" << result.electricity << std::endl;
72  }
73  }
74  }
75 }
76 
77 
78 
79 
80 int
81 main(int argc, char** argv) {
83  oc.setApplicationDescription(TL("Builds and writes an emissions map for SUMO's emission models."));
84  oc.setApplicationName("emissionsMap", "Eclipse SUMO emissionsMap Version " VERSION_STRING);
85  // add options
87  oc.addOptionSubTopic("Processing");
88  oc.doRegister("iterate", 'i', new Option_Bool(false));
89  oc.addDescription("iterate", "Processing", TL("If set, maps for all available emissions are written."));
90 
91  oc.doRegister("emission-class", 'e', new Option_String());
92  oc.addDescription("emission-class", "Processing", TL("Defines the name of the emission class to generate the map for."));
93 
94  oc.doRegister("v-min", new Option_Float(0.));
95  oc.addDescription("v-min", "Processing", TL("Defines the minimum velocity boundary of the map to generate (in m/s)."));
96  oc.doRegister("v-max", new Option_Float(50.));
97  oc.addDescription("v-max", "Processing", TL("Defines the maximum velocity boundary of the map to generate (in m/s)."));
98  oc.doRegister("v-step", new Option_Float(2.));
99  oc.addDescription("v-step", "Processing", TL("Defines the velocity step size (in m/s)."));
100  oc.doRegister("a-min", new Option_Float(-4.));
101  oc.addDescription("a-min", "Processing", TL("Defines the minimum acceleration boundary of the map to generate (in m/s^2)."));
102  oc.doRegister("a-max", new Option_Float(4.));
103  oc.addDescription("a-max", "Processing", TL("Defines the maximum acceleration boundary of the map to generate (in m/s^2)."));
104  oc.doRegister("a-step", new Option_Float(.5));
105  oc.addDescription("a-step", "Processing", TL("Defines the acceleration step size (in m/s^2)."));
106  oc.doRegister("s-min", new Option_Float(-10.));
107  oc.addDescription("s-min", "Processing", TL("Defines the minimum slope boundary of the map to generate (in deg)."));
108  oc.doRegister("s-max", new Option_Float(10.));
109  oc.addDescription("s-max", "Processing", TL("Defines the maximum slope boundary of the map to generate (in deg)."));
110  oc.doRegister("s-step", new Option_Float(1.));
111  oc.addDescription("s-step", "Processing", TL("Defines the slope step size (in deg)."));
112 
113  oc.addOptionSubTopic("Output");
114  oc.doRegister("output-file", 'o', new Option_String());
115  oc.addSynonyme("output", "output-file");
116  oc.addDescription("output", "Output", TL("Defines the file (or the path if --iterate was set) to write the map(s) into."));
117 
118  oc.addOptionSubTopic("Emissions");
119  oc.doRegister("emissions.volumetric-fuel", new Option_Bool(false));
120  oc.addDescription("emissions.volumetric-fuel", "Emissions", TL("Return fuel consumption values in (legacy) unit l instead of mg"));
121 
122  oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
123  oc.addDescription("phemlight-path", "Emissions", TL("Determines where to load PHEMlight definitions from"));
124 
125  oc.doRegister("phemlight-year", new Option_Integer(0));
126  oc.addDescription("phemlight-year", "Emissions", TL("Enable fleet age modelling with the given reference year in PHEMlight5"));
127 
128  oc.doRegister("phemlight-temperature", new Option_Float(INVALID_DOUBLE));
129  oc.addDescription("phemlight-temperature", "Emissions", TL("Set ambient temperature to correct NOx emissions in PHEMlight5"));
130 
132 
133  // run
134  int ret = 0;
135  try {
136  // initialise the application system (messaging, xml, options)
137  XMLSubSys::init();
138  OptionsIO::setArgs(argc, argv);
140  if (oc.processMetaOptions(argc < 2)) {
142  return 0;
143  }
144 
145  double vMin = oc.getFloat("v-min");
146  double vMax = oc.getFloat("v-max");
147  double vStep = oc.getFloat("v-step");
148  double aMin = oc.getFloat("a-min");
149  double aMax = oc.getFloat("a-max");
150  double aStep = oc.getFloat("a-step");
151  double sMin = oc.getFloat("s-min");
152  double sMax = oc.getFloat("s-max");
153  double sStep = oc.getFloat("s-step");
154  if (!oc.getBool("iterate")) {
155  if (!oc.isSet("emission-class")) {
156  throw ProcessError(TL("The emission class (-e) must be given."));
157  }
158  if (!oc.isSet("output-file")) {
159  throw ProcessError(TL("The output file (-o) must be given."));
160  }
161  const SUMOEmissionClass c = PollutantsInterface::getClassByName(oc.getString("emission-class"));
162  single(oc.getString("output-file"), oc.getString("emission-class"),
163  c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
164  } else {
165  if (!oc.isSet("output-file")) {
166  oc.set("output-file", "./");
167  }
168  const std::vector<SUMOEmissionClass> classes = PollutantsInterface::getAllClasses();
169  for (std::vector<SUMOEmissionClass>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
170  SUMOEmissionClass c = *ci;
172  c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
173  }
174  }
175  } catch (InvalidArgument& e) {
177  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
178  ret = 1;
179  } catch (ProcessError& e) {
180  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
182  }
183  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
184  ret = 1;
185 #ifndef _DEBUG
186  } catch (...) {
187  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
188  ret = 1;
189 #endif
190  }
192  if (ret == 0) {
193  std::cout << "Success." << std::endl;
194  }
195  return ret;
196 }
197 
198 
199 /****************************************************************************/
#define WRITE_MESSAGEF(...)
Definition: MsgHandler.h:298
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:42
int SUMOEmissionClass
const double INVALID_DOUBLE
invalid double
Definition: StdDefs.h:64
An upper class for objects with additional parameters.
Definition: EnergyParams.h:43
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:92
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:154
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.
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)
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)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
Definition: OptionsCont.cpp:76
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool set(const std::string &name, const std::string &value, const bool append=false)
Sets the given value for the named option.
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:60
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:74
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)
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:67
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:56
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.