Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2023 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_MESSAGEF(TL("Writing map of '%' into '%'."), className, of);
59 }
60 std::ofstream o(of.c_str());
61 if (!o.good()) {
62 throw ProcessError(TLF("Could not open file '%' for writing.", of));
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 oc.setApplicationDescription(TL("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", TL("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", TL("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", TL("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", TL("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", TL("Defines the velocity step size (in m/s)."));
103 oc.doRegister("a-min", new Option_Float(-4.));
104 oc.addDescription("a-min", "Processing", TL("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", TL("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", TL("Defines the acceleration step size (in m/s^2)."));
109 oc.doRegister("s-min", new Option_Float(-10.));
110 oc.addDescription("s-min", "Processing", TL("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", TL("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", TL("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", TL("Defines the file (or the path if --iterate was set) to write the map(s) into."));
120
121 oc.addOptionSubTopic("Emissions");
122 oc.doRegister("emissions.volumetric-fuel", new Option_Bool(false));
123 oc.addDescription("emissions.volumetric-fuel", "Emissions", TL("Return fuel consumption values in (legacy) unit l instead of mg"));
124
125 oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
126 oc.addDescription("phemlight-path", "Emissions", TL("Determines where to load PHEMlight definitions from"));
127
128 oc.doRegister("phemlight-year", new Option_Integer(0));
129 oc.addDescription("phemlight-year", "Emissions", TL("Enable fleet age modelling with the given reference year in PHEMlight5"));
130
131 oc.doRegister("phemlight-temperature", new Option_Float(INVALID_DOUBLE));
132 oc.addDescription("phemlight-temperature", "Emissions", TL("Set ambient temperature to correct NOx emissions in PHEMlight5"));
133
135
136 // run
137 int ret = 0;
138 try {
139 // initialise the application system (messaging, xml, options)
141 OptionsIO::setArgs(argc, argv);
143 if (oc.processMetaOptions(argc < 2)) {
145 return 0;
146 }
147
148 double vMin = oc.getFloat("v-min");
149 double vMax = oc.getFloat("v-max");
150 double vStep = oc.getFloat("v-step");
151 double aMin = oc.getFloat("a-min");
152 double aMax = oc.getFloat("a-max");
153 double aStep = oc.getFloat("a-step");
154 double sMin = oc.getFloat("s-min");
155 double sMax = oc.getFloat("s-max");
156 double sStep = oc.getFloat("s-step");
157 if (!oc.getBool("iterate")) {
158 if (!oc.isSet("emission-class")) {
159 throw ProcessError(TL("The emission class (-e) must be given."));
160 }
161 if (!oc.isSet("output-file")) {
162 throw ProcessError(TL("The output file (-o) must be given."));
163 }
165 single(oc.getString("output-file"), oc.getString("emission-class"),
166 c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
167 } else {
168 if (!oc.isSet("output-file")) {
169 oc.set("output-file", "./");
170 }
171 const std::vector<SUMOEmissionClass> classes = PollutantsInterface::getAllClasses();
172 for (std::vector<SUMOEmissionClass>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
173 SUMOEmissionClass c = *ci;
175 c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
176 }
177 }
178 } catch (InvalidArgument& e) {
180 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
181 ret = 1;
182 } catch (ProcessError& e) {
183 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
185 }
186 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
187 ret = 1;
188#ifndef _DEBUG
189 } catch (...) {
190 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
191 ret = 1;
192#endif
193 }
195 if (ret == 0) {
196 std::cout << "Success." << std::endl;
197 }
198 return ret;
199}
200
201
202/****************************************************************************/
#define WRITE_MESSAGEF(...)
Definition MsgHandler.h:273
#define TL(string)
Definition MsgHandler.h:287
#define TLF(string,...)
Definition MsgHandler.h:288
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
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
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.
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.
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.
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:55
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.