MSDevice_HBEFA.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef _MSC_VER
00024 #include <windows_config.h>
00025 #else
00026 #include <config.h>
00027 #endif
00028
00029 #include "MSDevice_HBEFA.h"
00030 #include <microsim/MSNet.h>
00031 #include <microsim/MSLane.h>
00032 #include <utils/options/OptionsCont.h>
00033 #include <utils/common/WrappingCommand.h>
00034 #include <utils/common/StaticCommand.h>
00035 #include <utils/common/HelpersHBEFA.h>
00036 #include <utils/iodevices/OutputDevice.h>
00037
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041
00042
00043
00044
00045
00046 int MSDevice_HBEFA::myVehicleIndex = 0;
00047
00048
00049
00050
00051
00052
00053
00054
00055 void
00056 MSDevice_HBEFA::insertOptions() throw() {
00057 OptionsCont &oc = OptionsCont::getOptions();
00058 oc.addOptionSubTopic("Emissions");
00059
00060 oc.doRegister("device.hbefa.probability", new Option_Float(0.));
00061 oc.addDescription("device.hbefa.probability", "Emissions", "The probability for a vehicle to have an emission logging device");
00062
00063 oc.doRegister("device.hbefa.knownveh", new Option_String());
00064 oc.addDescription("device.hbefa.knownveh", "Emissions", "Assign a device to named vehicles");
00065
00066 oc.doRegister("device.hbefa.deterministic", new Option_Bool(false));
00067 oc.addDescription("device.hbefa.deterministic", "Emissions", "The devices are set deterministic using a fraction of 1000");
00068
00069 myVehicleIndex = 0;
00070 }
00071
00072
00073 void
00074 MSDevice_HBEFA::buildVehicleDevices(MSVehicle &v, std::vector<MSDevice*> &into) throw() {
00075 OptionsCont &oc = OptionsCont::getOptions();
00076 if (oc.getFloat("device.hbefa.probability")==0&&!oc.isSet("device.hbefa.knownveh")) {
00077
00078 return;
00079 }
00080
00081 bool haveByNumber = false;
00082 if (oc.getBool("device.hbefa.deterministic")) {
00083 haveByNumber = ((myVehicleIndex%1000) < (int)(oc.getFloat("device.hbefa.probability")*1000.));
00084 } else {
00085 haveByNumber = RandHelper::rand()<=oc.getFloat("device.hbefa.probability");
00086 }
00087 bool haveByName = oc.isSet("device.hbefa.knownveh") && OptionsCont::getOptions().isInStringVector("device.hbefa.knownveh", v.getID());
00088 if (haveByNumber||haveByName) {
00089
00090 MSDevice_HBEFA* device = new MSDevice_HBEFA(v, "hbefa_" + v.getID());
00091 into.push_back(device);
00092 }
00093 myVehicleIndex++;
00094 }
00095
00096
00097
00098
00099
00100 MSDevice_HBEFA::MSDevice_HBEFA(MSVehicle &holder, const std::string &id) throw()
00101 : MSDevice(holder, id), myComputeAndCollectCommand(0),
00102 myCO2(0), myCO(0), myHC(0), myPMx(0), myNOx(0), myFuel(0) {
00103 }
00104
00105
00106 MSDevice_HBEFA::~MSDevice_HBEFA() throw() {
00107
00108 if (myComputeAndCollectCommand!=0) {
00109 myComputeAndCollectCommand->deschedule();
00110 }
00111 }
00112
00113
00114 void
00115 MSDevice_HBEFA::enterLaneAtEmit(MSLane* enteredLane, const MSVehicle::State &) {
00116 if (myComputeAndCollectCommand!=0) {
00117 return;
00118 }
00119 myComputeAndCollectCommand = new WrappingCommand< MSDevice_HBEFA >(this, &MSDevice_HBEFA::wrappedComputeCommandExecute);
00120 MSNet::getInstance()->getEndOfTimestepEvents().addEvent(
00121 myComputeAndCollectCommand, MSNet::getInstance()->getCurrentTimeStep(),
00122 MSEventControl::ADAPT_AFTER_EXECUTION);
00123 }
00124
00125
00126 SUMOTime
00127 MSDevice_HBEFA::wrappedComputeCommandExecute(SUMOTime currentTime) throw(ProcessError) {
00128 if (!getHolder().isOnRoad()) {
00129 return 1;
00130 }
00131 SUMOEmissionClass c = getHolder().getVehicleType().getEmissionClass();
00132 SUMOReal v = getHolder().getSpeed();
00133 SUMOReal a = getHolder().getPreDawdleAcceleration();
00134 myCO2 += HelpersHBEFA::computeCO2(c, v, a);
00135 myCO += HelpersHBEFA::computeCO(c, v, a);
00136 myHC += HelpersHBEFA::computeHC(c, v, a);
00137 myPMx += HelpersHBEFA::computePMx(c, v, a);
00138 myNOx += HelpersHBEFA::computeNOx(c, v, a);
00139 myFuel += HelpersHBEFA::computeFuel(c, v, a);
00140 return DELTA_T;
00141 }
00142
00143
00144 void
00145 MSDevice_HBEFA::tripInfoOutput(OutputDevice &os) const throw(IOError) {
00146 os << resetiosflags(std::ios::floatfield);
00147 (os.openTag("emissions") <<
00148 " CO_abs=\"" << myCO <<
00149 "\" CO2_abs=\"" << myCO2 <<
00150 "\" HC_abs=\"" << myHC <<
00151 "\" PMx_abs=\""<< myPMx <<
00152 "\" NOx_abs=\""<< myNOx <<
00153 "\" fuel_abs=\""<< myFuel <<
00154 "\"").closeTag(true);
00155 os<<setiosflags(std::ios::fixed);
00156 }
00157
00158
00159
00160
00161