LCOV - code coverage report
Current view: top level - src/tools - emissionsMap_main.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 79 101 78.2 %
Date: 2024-04-26 15:39:30 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.14