Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
emissionsDrivingCycle_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-2026 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 calculator
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>
30#include <memory>
38#include <utils/xml/XMLSubSys.h>
46#include "TrajectoriesHandler.h"
47#include "VTypesHandler.h"
48
49
50// ===========================================================================
51// functions
52// ===========================================================================
53
54
55/* -------------------------------------------------------------------------
56 * main
57 * ----------------------------------------------------------------------- */
58int
59main(int argc, char** argv) {
61 oc.setApplicationDescription(TL("Computes emissions by driving a time line using SUMO's emission models."));
62 oc.setApplicationName("emissionsDrivingCycle", "Eclipse SUMO emissionsDrivingCycle " VERSION_STRING);
63
64 // add options
66 oc.addOptionSubTopic("Input");
67 oc.doRegister("timeline-file", 't', new Option_FileName());
68 oc.addSynonyme("timeline", "timeline-file");
69 oc.addDescription("timeline-file", "Input", TL("Defines the file to read the driving cycle from."));
70
71 oc.doRegister("timeline-file.skip", new Option_Integer(0));
72 oc.addSynonyme("timeline.skip", "timeline-file.skip");
73 oc.addDescription("timeline-file.skip", "Input", TL("Skips the first NUM lines."));
74
75 oc.doRegister("timeline-file.separator", new Option_String(";"));
76 oc.addSynonyme("timeline.separator", "timeline-file.separator");
77 oc.addDescription("timeline-file.separator", "Input", TL("Defines the entry separator."));
78
79 oc.doRegister("netstate-file", 'n', new Option_FileName());
80 oc.addSynonyme("netstate", "netstate-file");
81 oc.addSynonyme("amitran", "netstate-file");
82 oc.addDescription("netstate-file", "Input", TL("Defines the netstate, route and trajectory files to read the driving cycles from."));
83
84 oc.doRegister("additional-files", new Option_FileName());
85 oc.addDescription("additional-files", "Input", TL("Load emission parameters (vTypes) from FILE(s)"));
86
87 oc.doRegister("emission-class", 'e', new Option_String("HBEFA4/default"));
88 oc.addDescription("emission-class", "Input", TL("Defines for which emission class the emissions shall be generated. "));
89
90 oc.doRegister("vtype", new Option_String());
91 oc.addDescription("vtype", "Input", TL("Defines the vehicle type to use for emission parameters."));
92
93 oc.addOptionSubTopic("Processing");
94 oc.doRegister("compute-a", 'a', new Option_Bool(false));
95 oc.addDescription("compute-a", "Processing", TL("If set, the acceleration is computed instead of being read from the file. "));
96
97 oc.doRegister("compute-a.forward", new Option_Bool(false));
98 oc.addDescription("compute-a.forward", "Processing", TL("If set, the acceleration for time t is computed from v(t+1) - v(t) instead of v(t) - v(t-1). "));
99
100 oc.doRegister("compute-a.zero-correction", new Option_Bool(false));
101 oc.addDescription("compute-a.zero-correction", "Processing", TL("If set, the acceleration for time t is set to 0 if the speed is 0. "));
102
103 oc.doRegister("skip-first", 's', new Option_Bool(false));
104 oc.addDescription("skip-first", "Processing", TL("If set, the first line of the read file is skipped."));
105
106 oc.doRegister("kmh", new Option_Bool(false));
107 oc.addDescription("kmh", "Processing", TL("If set, the given speed is interpreted as being given in km/h."));
108
109 oc.doRegister("have-slope", new Option_Bool(false));
110 oc.addDescription("have-slope", "Processing", TL("If set, the fourth column is read and used as slope (in deg)."));
111
112 oc.doRegister("slope", new Option_Float(0));
113 oc.addDescription("slope", "Processing", TL("Sets a global slope (in deg) that is used if the file does not contain slope information."));
114
115 oc.addOptionSubTopic("Output");
116 oc.doRegister("output-file", 'o', new Option_String());
117 oc.addSynonyme("output", "output-file");
118 oc.addDescription("output", "Output", TL("Defines the file to write the emission cycle results into."));
119
120 oc.doRegister("output.attributes", new Option_StringVector());
121 oc.addDescription("output.attributes", "Output", TL("Defines the attributes to write."));
122
123 oc.doRegister("emission-output", new Option_FileName());
124 oc.addDescription("emission-output", "Output", TL("Save the emission values of each vehicle in XML"));
125
126 oc.doRegister("sum-output", new Option_FileName());
127 oc.addSynonyme("sum", "sum-output");
128 oc.addDescription("sum-output", "Output", TL("Save the aggregated and normed emission values of each vehicle in CSV"));
129
130 oc.addOptionSubTopic("Emissions");
131 oc.doRegister("emissions.volumetric-fuel", new Option_Bool(false));
132 oc.addDescription("emissions.volumetric-fuel", "Emissions", TL("Return fuel consumption values in (legacy) unit l instead of mg"));
133
134 oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
135 oc.addDescription("phemlight-path", "Emissions", TL("Determines where to load PHEMlight definitions from"));
136
137 oc.doRegister("phemlight-year", new Option_Integer(0));
138 oc.addDescription("phemlight-year", "Emissions", TL("Enable fleet age modeling with the given reference year in PHEMlight5"));
139
140 oc.doRegister("phemlight-temperature", new Option_Float(INVALID_DOUBLE));
141 oc.addDescription("phemlight-temperature", "Emissions", TL("Set ambient temperature to correct NOx emissions in PHEMlight5"));
142
143 oc.doRegister("begin", new Option_String("0", "TIME"));
144 oc.addDescription("begin", "Processing", TL("Defines the begin time in seconds;"));
145
146 oc.doRegister("end", new Option_String("-1", "TIME"));
147 oc.addDescription("end", "Processing", TL("Defines the end time in seconds;"));
148
150 oc.doRegister("quiet", 'q', new Option_Bool(false));
151 oc.addDescription("quiet", "Report", TL("Not writing anything."));
152
153 // run
154 int ret = 0;
155 bool quiet = false;
156 try {
157 // initialise the application system (messaging, xml, options)
159 OptionsIO::setArgs(argc, argv);
161 if (oc.processMetaOptions(argc < 2)) {
163 return 0;
164 }
167
168 quiet = oc.getBool("quiet");
169 if (!oc.isSet("timeline-file") && !oc.isSet("netstate-file")) {
170 throw ProcessError(TL("Either a timeline or a netstate / amitran file must be given."));
171 }
172 if (!oc.isSet("output-file") && (oc.isSet("timeline-file") || !oc.isSet("emission-output"))) {
173 throw ProcessError(TL("The output file must be given."));
174 }
175 std::unique_ptr<std::ostream> outOwner;
176 std::ostream* out = nullptr;
177 if (oc.isSet("output-file")) {
178 outOwner.reset(new std::ofstream(oc.getString("output-file").c_str()));
179 out = outOwner.get();
180 }
181 long long int attributes = 0;
182 if (oc.isSet("output.attributes")) {
183 for (std::string attrName : oc.getStringVector("output.attributes")) {
184 if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
185 if (attrName == "all") {
186 attributes = std::numeric_limits<long long int>::max() - 1;
187 } else {
188 WRITE_ERRORF(TL("Unknown attribute '%' to write in output."), attrName);
189 }
190 continue;
191 }
192 int attr = SUMOXMLDefinitions::Attrs.get(attrName);
193 assert(attr < 63);
194 attributes |= ((long long int)1 << attr);
195 }
196 } else {
197 attributes = ~(((long long int)1 << SUMO_ATTR_AMOUNT));
198 }
199 OptionsCont::createDeviceByOption("emission-output", "emission-export", "emission_file.xsd");
200 OutputDevice* xmlOut = nullptr;
201 if (oc.isSet("emission-output")) {
202 xmlOut = &OptionsCont::getDeviceByOption("emission-output");
203 } else if (out == nullptr) {
204 out = &std::cout;
205 }
206 std::unique_ptr<std::ostream> sumOut(nullptr);
207 if (oc.isSet("sum-output")) {
208 sumOut.reset(new std::ofstream(oc.getString("sum-output").c_str()));
209 (*sumOut) << "Vehicle,Cycle,Time,Speed,Gradient,Acceleration,FC,FCel,CO2,NOx,CO,HC,PM" << std::endl;
210 }
211
212 SUMOEmissionClass emissionClass = PollutantsInterface::getClassByName(oc.getString("emission-class"));
213 std::unique_ptr<EnergyParams> energyParams;
214 std::map<std::string, SUMOVTypeParameter*> vTypes;
215 if (oc.isSet("vtype") || oc.isSet("additional-files")) {
216 if (!oc.isSet("additional-files")) {
217 throw ProcessError(TL("Option --vtype requires option --additional-files for loading vehicle types"));
218 }
219 if (!oc.isUsableFileList("additional-files")) {
220 throw ProcessError();
221 }
222 for (const std::string& file : oc.getStringVector("additional-files")) {
223 VTypesHandler typesHandler(file, vTypes);
224 if (!XMLSubSys::runParser(typesHandler, file)) {
225 throw ProcessError(TLF("Loading of % failed.", file));
226 }
227 }
228 if (!oc.isSet("vtype") && vTypes.size() != 1) {
229 throw ProcessError(TL("Vehicle type is not unique."));
230 }
231 const auto vTypeIt = oc.isSet("vtype") ? vTypes.find(oc.getString("vtype")) : vTypes.begin();
232 if (vTypeIt == vTypes.end()) {
233 throw ProcessError(TLF("Vehicle type '%' is not defined.", oc.getString("vtype")));
234 }
235 if (oc.isDefault("emission-class")) {
236 emissionClass = vTypeIt->second->emissionClass;
237 }
238 energyParams = std::unique_ptr<EnergyParams>(new EnergyParams(vTypeIt->second));
239 } else {
240 energyParams = std::unique_ptr<EnergyParams>(new EnergyParams());
241 }
242 for (auto& vt : vTypes) {
243 delete vt.second;
244 }
245
246 const bool computeA = oc.getBool("compute-a") || oc.getBool("compute-a.forward");
247 TrajectoriesHandler handler(computeA, oc.getBool("compute-a.forward"), oc.getBool("compute-a.zero-correction"), emissionClass, energyParams.get(), attributes, oc.getFloat("slope"), out, xmlOut);
248
249 if (oc.isSet("timeline-file")) {
250 int skip = oc.getBool("skip-first") ? 1 : oc.getInt("timeline-file.skip");
251 const bool inKMH = oc.getBool("kmh");
252 const bool haveSlope = oc.getBool("have-slope");
253 double l = 0;
254 double totalA = 0;
255 double totalS = 0;
256 int time = 0;
257
258 LineReader lr(oc.getString("timeline-file"));
259 if (!lr.good()) {
260 throw ProcessError(TLF("Unreadable file '%'.", lr.getFileName()));
261 }
262 while (lr.hasMore()) {
263 std::string line = lr.readLine();
264 if (skip > 0) {
265 skip--;
266 continue;
267 }
268 StringTokenizer st(StringUtils::prune(line), oc.getString("timeline-file.separator"));
269 if (st.hasNext()) {
270 try {
271 double t = StringUtils::toDouble(st.next());
272 double v = 0;
273 if (st.hasNext()) {
274 v = StringUtils::toDouble(st.next());
275 } else {
276 v = t;
277 t = time;
278 }
279 if (inKMH) {
280 v /= 3.6;
281 }
282 double a = !computeA && st.hasNext() ? StringUtils::toDouble(st.next()) : TrajectoriesHandler::INVALID_VALUE;
283 double s = haveSlope && st.hasNext() ? StringUtils::toDouble(st.next()) : TrajectoriesHandler::INVALID_VALUE;
284 if (handler.writeEmissions(*out, "", emissionClass, energyParams.get(), attributes, t, v, a, s)) {
285 l += v;
286 totalA += a;
287 totalS += s;
288 time++;
289 }
290 } catch (EmptyData&) {
291 throw ProcessError(TLF("Missing an entry in line '%'.", line));
292 } catch (NumberFormatException&) {
293 throw ProcessError(TLF("Not numeric entry in line '%'.", line));
294 }
295 }
296 }
297 if (!quiet) {
298 std::cout << "sums" << std::endl
299 << "length:" << l << std::endl;
300 }
301 if (sumOut != nullptr) {
302 (*sumOut) << oc.getString("emission-class") << "," << lr.getFileName() << "," << time << ","
303 << (l / time * 3.6) << "," << (totalS / time) << "," << (totalA / time) << ",";
304 handler.writeNormedSums(*sumOut, "", l);
305 }
306 }
307 if (oc.isSet("netstate-file")) {
308 XMLSubSys::runParser(handler, oc.getString("netstate-file"));
309 }
310 if (!quiet) {
311 handler.writeSums(std::cout, "");
312 }
313 } catch (InvalidArgument& e) {
315 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
316 ret = 1;
317 } catch (ProcessError& e) {
318 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
320 }
321 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
322 ret = 1;
323#ifndef _DEBUG
324 } catch (...) {
325 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
326 ret = 1;
327#endif
328 }
330 if (ret == 0 && !quiet) {
331 std::cout << "Success." << std::endl;
332 }
333 return ret;
334}
335
336
337/****************************************************************************/
#define WRITE_ERRORF(...)
Definition MsgHandler.h:308
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition Option.h:42
int SUMOEmissionClass
@ SUMO_ATTR_AMOUNT
MSMeanData_Amitran.
const double INVALID_DOUBLE
invalid double
Definition StdDefs.h:68
An upper class for objects with additional parameters.
Retrieves a file linewise and reports the lines to a handler.
Definition LineReader.h:48
bool good() const
Returns the information whether the stream is readable.
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
std::string getFileName() const
Returns the name of the used file.
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:92
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)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the 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)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="", const int maximumDepth=2)
Creates the device using the output definition stored in the named option.
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)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file)
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition OptionsIO.cpp:59
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:75
Static storage of an output device and its base (abstract) implementation.
static SUMOEmissionClass getClassByName(const std::string &eClass, const SUMOVehicleClass vc=SVC_IGNORING)
Checks whether the string describes a known vehicle class.
static SequentialStringBijection Attrs
The names of SUMO-XML attributes for use in netbuild.
int get(const std::string &str) const
bool hasNext()
returns the information whether further substrings exist
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static void close()
Closes all of an applications subsystems.
static bool checkOptions(OptionsCont &oc)
checks shared options and sets StdDefs
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 initOutputOptions()
init message output (console and log files) according to the options
An XML-Handler for amitran and netstate trajectories.
void writeNormedSums(std::ostream &o, const std::string id, const double factor)
void writeSums(std::ostream &o, const std::string id)
static const int INVALID_VALUE
bool writeEmissions(std::ostream &o, const std::string id, const SUMOEmissionClass c, EnergyParams *params, long long int attributes, double t, double &v, double &a, double &s)
An XML-Handler for amitran and netstate trajectories.
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:60
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
int main(int argc, char **argv)