Line data Source code
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 : /****************************************************************************/
14 : /// @file emissionsMap_main.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Wed, 21.08.2013
18 : ///
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>
30 : #include <memory>
31 : #include <utils/common/MsgHandler.h>
32 : #include <utils/common/StringUtils.h>
33 : #include <utils/options/Option.h>
34 : #include <utils/options/OptionsCont.h>
35 : #include <utils/options/OptionsIO.h>
36 : #include <utils/common/UtilExceptions.h>
37 : #include <utils/emissions/EnergyParams.h>
38 : #include <utils/emissions/PollutantsInterface.h>
39 : #include <utils/common/SystemFrame.h>
40 : #include <utils/common/ToString.h>
41 : #include <utils/xml/XMLSubSys.h>
42 : #include <utils/common/FileHelpers.h>
43 : #include <utils/iodevices/OutputDevice.h>
44 : #include "VTypesHandler.h"
45 :
46 :
47 : // ===========================================================================
48 : // functions
49 : // ===========================================================================
50 951 : void single(const OptionsCont& oc, const std::string& of, const std::string& className, SUMOEmissionClass c,
51 : double vMin, double vMax, double vStep,
52 : double aMin, double aMax, double aStep,
53 : double sMin, double sMax, double sStep,
54 : bool verbose) {
55 951 : if (verbose) {
56 0 : WRITE_MESSAGEF(TL("Writing map of '%' into '%'."), className, of);
57 : }
58 951 : std::ofstream o(of.c_str());
59 951 : if (!o.good()) {
60 0 : throw ProcessError(TLF("Could not open file '%' for writing.", of));
61 : }
62 :
63 951 : std::unique_ptr<EnergyParams> energyParams;
64 : std::map<std::string, SUMOVTypeParameter*> vTypes;
65 1901 : if (oc.isSet("vtype") || oc.isSet("additional-files")) {
66 2 : if (!oc.isSet("additional-files")) {
67 0 : throw ProcessError(TL("Option --vtype requires option --additional-files for loading vehicle types"));
68 : }
69 2 : if (!oc.isUsableFileList("additional-files")) {
70 0 : throw ProcessError();
71 : }
72 3 : for (const std::string& file : oc.getStringVector("additional-files")) {
73 1 : VTypesHandler typesHandler(file, vTypes);
74 1 : if (!XMLSubSys::runParser(typesHandler, file)) {
75 0 : throw ProcessError(TLF("Loading of % failed.", file));
76 : }
77 1 : }
78 2 : if (!oc.isSet("vtype") && vTypes.size() != 1) {
79 0 : throw ProcessError(TL("Vehicle type is not unique."));
80 : }
81 4 : const auto vTypeIt = oc.isSet("vtype") ? vTypes.find(oc.getString("vtype")) : vTypes.begin();
82 1 : if (vTypeIt == vTypes.end()) {
83 0 : throw ProcessError(TLF("Vehicle type '%' is not defined.", oc.getString("vtype")));
84 : }
85 2 : if (oc.isDefault("emission-class")) {
86 0 : c = vTypeIt->second->emissionClass;
87 : }
88 2 : energyParams = std::unique_ptr<EnergyParams>(new EnergyParams(vTypeIt->second));
89 : } else {
90 1900 : energyParams = std::unique_ptr<EnergyParams>(new EnergyParams());
91 : }
92 7608 : for (double v = vMin; v <= vMax; v += vStep) {
93 66570 : for (double a = aMin; a <= aMax; a += aStep) {
94 119826 : for (double s = sMin; s <= sMax; s += sStep) {
95 59913 : const PollutantsInterface::Emissions result = PollutantsInterface::computeAll(c, v, a, s, energyParams.get());
96 59913 : o << v << ";" << a << ";" << s << ";" << "CO" << ";" << result.CO << std::endl;
97 59913 : o << v << ";" << a << ";" << s << ";" << "CO2" << ";" << result.CO2 << std::endl;
98 59913 : o << v << ";" << a << ";" << s << ";" << "HC" << ";" << result.HC << std::endl;
99 59913 : o << v << ";" << a << ";" << s << ";" << "PMx" << ";" << result.PMx << std::endl;
100 59913 : o << v << ";" << a << ";" << s << ";" << "NOx" << ";" << result.NOx << std::endl;
101 59913 : o << v << ";" << a << ";" << s << ";" << "fuel" << ";" << result.fuel << std::endl;
102 59913 : o << v << ";" << a << ";" << s << ";" << "electricity" << ";" << result.electricity << std::endl;
103 : }
104 : }
105 : }
106 951 : }
107 :
108 :
109 :
110 :
111 : int
112 951 : main(int argc, char** argv) {
113 951 : OptionsCont& oc = OptionsCont::getOptions();
114 951 : oc.setApplicationDescription(TL("Builds and writes an emissions map for SUMO's emission models."));
115 1902 : oc.setApplicationName("emissionsMap", "Eclipse SUMO emissionsMap Version " VERSION_STRING);
116 : // add options
117 951 : SystemFrame::addConfigurationOptions(oc);
118 951 : oc.addOptionSubTopic("Input");
119 951 : oc.addOptionSubTopic("Processing");
120 951 : oc.doRegister("iterate", 'i', new Option_Bool(false));
121 1902 : oc.addDescription("iterate", "Processing", TL("If set, maps for all available emissions are written."));
122 :
123 951 : oc.doRegister("emission-class", 'e', new Option_String());
124 1902 : oc.addDescription("emission-class", "Processing", TL("Defines the name of the emission class to generate the map for."));
125 :
126 951 : oc.doRegister("additional-files", new Option_FileName());
127 1902 : oc.addDescription("additional-files", "Input", TL("Load emission parameters (vTypes) from FILE(s)"));
128 :
129 951 : oc.doRegister("vtype", new Option_String());
130 1902 : oc.addDescription("vtype", "Input", TL("Defines the vehicle type to use for emission parameters."));
131 :
132 951 : oc.doRegister("v-min", new Option_Float(0.));
133 1902 : oc.addDescription("v-min", "Processing", TL("Defines the minimum velocity boundary of the map to generate (in m/s)."));
134 951 : oc.doRegister("v-max", new Option_Float(50.));
135 1902 : oc.addDescription("v-max", "Processing", TL("Defines the maximum velocity boundary of the map to generate (in m/s)."));
136 951 : oc.doRegister("v-step", new Option_Float(2.));
137 1902 : oc.addDescription("v-step", "Processing", TL("Defines the velocity step size (in m/s)."));
138 951 : oc.doRegister("a-min", new Option_Float(-4.));
139 1902 : oc.addDescription("a-min", "Processing", TL("Defines the minimum acceleration boundary of the map to generate (in m/s^2)."));
140 951 : oc.doRegister("a-max", new Option_Float(4.));
141 1902 : oc.addDescription("a-max", "Processing", TL("Defines the maximum acceleration boundary of the map to generate (in m/s^2)."));
142 951 : oc.doRegister("a-step", new Option_Float(.5));
143 1902 : oc.addDescription("a-step", "Processing", TL("Defines the acceleration step size (in m/s^2)."));
144 951 : oc.doRegister("s-min", new Option_Float(-10.));
145 1902 : oc.addDescription("s-min", "Processing", TL("Defines the minimum slope boundary of the map to generate (in deg)."));
146 951 : oc.doRegister("s-max", new Option_Float(10.));
147 1902 : oc.addDescription("s-max", "Processing", TL("Defines the maximum slope boundary of the map to generate (in deg)."));
148 951 : oc.doRegister("s-step", new Option_Float(1.));
149 1902 : oc.addDescription("s-step", "Processing", TL("Defines the slope step size (in deg)."));
150 :
151 951 : oc.addOptionSubTopic("Output");
152 951 : oc.doRegister("output-file", 'o', new Option_String());
153 1902 : oc.addSynonyme("output", "output-file");
154 1902 : oc.addDescription("output", "Output", TL("Defines the file (or the path if --iterate was set) to write the map(s) into."));
155 :
156 951 : oc.addOptionSubTopic("Emissions");
157 951 : oc.doRegister("emissions.volumetric-fuel", new Option_Bool(false));
158 1902 : oc.addDescription("emissions.volumetric-fuel", "Emissions", TL("Return fuel consumption values in (legacy) unit l instead of mg"));
159 :
160 3804 : oc.doRegister("phemlight-path", new Option_FileName(StringVector({ "./PHEMlight/" })));
161 1902 : oc.addDescription("phemlight-path", "Emissions", TL("Determines where to load PHEMlight definitions from"));
162 :
163 951 : oc.doRegister("phemlight-year", new Option_Integer(0));
164 1902 : oc.addDescription("phemlight-year", "Emissions", TL("Enable fleet age modelling with the given reference year in PHEMlight5"));
165 :
166 951 : oc.doRegister("phemlight-temperature", new Option_Float(INVALID_DOUBLE));
167 1902 : oc.addDescription("phemlight-temperature", "Emissions", TL("Set ambient temperature to correct NOx emissions in PHEMlight5"));
168 :
169 951 : SystemFrame::addReportOptions(oc);
170 :
171 : // run
172 : int ret = 0;
173 : try {
174 : // initialise the application system (messaging, xml, options)
175 951 : XMLSubSys::init();
176 951 : OptionsIO::setArgs(argc, argv);
177 951 : OptionsIO::getOptions();
178 951 : if (oc.processMetaOptions(argc < 2)) {
179 0 : SystemFrame::close();
180 : return 0;
181 : }
182 :
183 951 : double vMin = oc.getFloat("v-min");
184 951 : double vMax = oc.getFloat("v-max");
185 951 : double vStep = oc.getFloat("v-step");
186 951 : double aMin = oc.getFloat("a-min");
187 951 : double aMax = oc.getFloat("a-max");
188 951 : double aStep = oc.getFloat("a-step");
189 951 : double sMin = oc.getFloat("s-min");
190 951 : double sMax = oc.getFloat("s-max");
191 951 : double sStep = oc.getFloat("s-step");
192 1902 : if (!oc.getBool("iterate")) {
193 1902 : if (!oc.isSet("emission-class")) {
194 0 : throw ProcessError(TL("The emission class (-e) must be given."));
195 : }
196 1902 : if (!oc.isSet("output-file")) {
197 0 : throw ProcessError(TL("The output file (-o) must be given."));
198 : }
199 951 : const SUMOEmissionClass c = PollutantsInterface::getClassByName(oc.getString("emission-class"));
200 2853 : single(oc, oc.getString("output-file"), oc.getString("emission-class"),
201 951 : c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
202 : } else {
203 0 : if (!oc.isSet("output-file")) {
204 0 : oc.set("output-file", "./");
205 : }
206 0 : const std::vector<SUMOEmissionClass> classes = PollutantsInterface::getAllClasses();
207 0 : for (std::vector<SUMOEmissionClass>::const_iterator ci = classes.begin(); ci != classes.end(); ++ci) {
208 0 : SUMOEmissionClass c = *ci;
209 0 : single(oc, oc.getString("output-file") + PollutantsInterface::getName(c) + ".csv",
210 0 : PollutantsInterface::getName(c),
211 0 : c, vMin, vMax, vStep, aMin, aMax, aStep, sMin, sMax, sStep, oc.getBool("verbose"));
212 : }
213 0 : }
214 0 : } catch (InvalidArgument& e) {
215 0 : MsgHandler::getErrorInstance()->inform(e.what());
216 0 : MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
217 : ret = 1;
218 0 : } catch (ProcessError& e) {
219 0 : if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
220 0 : MsgHandler::getErrorInstance()->inform(e.what());
221 : }
222 0 : MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
223 : ret = 1;
224 : #ifndef _DEBUG
225 0 : } catch (...) {
226 0 : MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
227 : ret = 1;
228 : #endif
229 0 : }
230 951 : SystemFrame::close();
231 : if (ret == 0) {
232 : std::cout << "Success." << std::endl;
233 : }
234 : return ret;
235 1902 : }
236 :
237 :
238 : /****************************************************************************/
|