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>
39#include <utils/xml/XMLSubSys.h>
42
43
44// ===========================================================================
45// functions
46// ===========================================================================
47
48
49/* -------------------------------------------------------------------------
50 * main
51 * ----------------------------------------------------------------------- */
52void 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) {
67 const PollutantsInterface::Emissions result = PollutantsInterface::computeAll(c, v, a, s, nullptr);
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
83int
84main(int argc, char** argv) {
86 // build options
88 // give some application descriptions
89 oc.setApplicationDescription("Builds and writes an emissions map for SUMO's emission models.");
90 oc.setApplicationName("emissionsMap", "Eclipse SUMO emissionsMap Version " VERSION_STRING);
91 // add options
93 oc.addOptionSubTopic("Processing");
94 oc.doRegister("iterate", 'i', new Option_Bool(false));
95 oc.addDescription("iterate", "Processing", "If set, maps for all available emissions are written.");
96
97 oc.doRegister("emission-class", 'e', new Option_String());
98 oc.addDescription("emission-class", "Processing", "Defines the name of the emission class to generate the map for.");
99
100 oc.doRegister("v-min", new Option_Float(0.));
101 oc.addDescription("v-min", "Processing", "Defines the minimum velocity boundary of the map to generate (in m/s).");
102 oc.doRegister("v-max", new Option_Float(50.));
103 oc.addDescription("v-max", "Processing", "Defines the maximum velocity boundary of the map to generate (in m/s).");
104 oc.doRegister("v-step", new Option_Float(2.));
105 oc.addDescription("v-step", "Processing", "Defines the velocity step size (in m/s).");
106 oc.doRegister("a-min", new Option_Float(-4.));
107 oc.addDescription("a-min", "Processing", "Defines the minimum acceleration boundary of the map to generate (in m/s^2).");
108 oc.doRegister("a-max", new Option_Float(4.));
109 oc.addDescription("a-max", "Processing", "Defines the maximum acceleration boundary of the map to generate (in m/s^2).");
110 oc.doRegister("a-step", new Option_Float(.5));
111 oc.addDescription("a-step", "Processing", "Defines the acceleration step size (in m/s^2).");
112 oc.doRegister("s-min", new Option_Float(-10.));
113 oc.addDescription("s-min", "Processing", "Defines the minimum slope boundary of the map to generate (in deg).");
114 oc.doRegister("s-max", new Option_Float(10.));
115 oc.addDescription("s-max", "Processing", "Defines the maximum slope boundary of the map to generate (in deg).");
116 oc.doRegister("s-step", new Option_Float(1.));
117 oc.addDescription("s-step", "Processing", "Defines the slope step size (in deg).");
118
119 oc.addOptionSubTopic("Output");
120 oc.doRegister("output-file", 'o', new Option_String());
121 oc.addSynonyme("output", "output-file");
122 oc.addDescription("output", "Output", "Defines the file (or the path if --iterate was set) to write the map(s) into.");
123
124 oc.addOptionSubTopic("Emissions");
125 oc.doRegister("emissions.volumetric-fuel", new Option_Bool(false));
126 oc.addDescription("emissions.volumetric-fuel", "Emissions", "Return fuel consumption values in (legacy) unit l instead of mg");
127
128 oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
129 oc.addDescription("phemlight-path", "Emissions", "Determines where to load PHEMlight definitions from");
130
131 oc.doRegister("phemlight-year", new Option_Integer(0));
132 oc.addDescription("phemlight-year", "Emissions", "Enable fleet age modelling with the given reference year in PHEMlight5");
133
134 oc.doRegister("phemlight-temperature", new Option_Float(INVALID_DOUBLE));
135 oc.addDescription("phemlight-temperature", "Emissions", "Set ambient temperature to correct NOx emissions in PHEMlight5");
136
138
139 // run
140 int ret = 0;
141 try {
142 // initialise the application system (messaging, xml, options)
144 OptionsIO::setArgs(argc, argv);
146 if (oc.processMetaOptions(argc < 2)) {
148 return 0;
149 }
150
151 double vMin = oc.getFloat("v-min");
152 double vMax = oc.getFloat("v-max");
153 double vStep = oc.getFloat("v-step");
154 double aMin = oc.getFloat("a-min");
155 double aMax = oc.getFloat("a-max");
156 double aStep = oc.getFloat("a-step");
157 double sMin = oc.getFloat("s-min");
158 double sMax = oc.getFloat("s-max");
159 double sStep = oc.getFloat("s-step");
160 if (!oc.getBool("iterate")) {
161 if (!oc.isSet("emission-class")) {
162 throw ProcessError("The emission class (-e) must be given.");
163 }
164 if (!oc.isSet("output-file")) {
165 throw ProcessError("The output file (-o) must be given.");
166 }
168 single(oc.getString("output-file"), oc.getString("emission-class"),
169 c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
170 } else {
171 if (!oc.isSet("output-file")) {
172 oc.set("output-file", "./");
173 }
174 const std::vector<SUMOEmissionClass> classes = PollutantsInterface::getAllClasses();
175 for (std::vector<SUMOEmissionClass>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
176 SUMOEmissionClass c = *ci;
178 c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
179 }
180 }
181 } catch (InvalidArgument& e) {
183 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
184 ret = 1;
185 } catch (ProcessError& e) {
186 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
188 }
189 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
190 ret = 1;
191#ifndef _DEBUG
192 } catch (...) {
193 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
194 ret = 1;
195#endif
196 }
198 if (ret == 0) {
199 std::cout << "Success." << std::endl;
200 }
201 return ret;
202}
203
204
205/****************************************************************************/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:267
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:43
int SUMOEmissionClass
const double INVALID_DOUBLE
Definition: StdDefs.h:60
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:79
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:116
static void setupI18n(const std::string &locale="")
set up gettext stuff
Definition: MsgHandler.cpp:228
An integer-option.
Definition: Option.h:289
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:76
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)
Definition: OptionsCont.cpp:97
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:59
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: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.