LCOV - code coverage report
Current view: top level - src/microsim/trigger - MSChargingStation.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 91.7 % 204 187
Test Date: 2026-03-26 16:31:35 Functions: 87.0 % 23 20

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-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              : /****************************************************************************/
      14              : /// @file    MSChargingStation.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Tamas Kurczveil
      17              : /// @author  Pablo Alvarez Lopez
      18              : /// @author  Mirko Barthauer
      19              : /// @date    20-12-13
      20              : ///
      21              : // Charging Station for Electric vehicles
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <cassert>
      26              : #include <utils/common/StringUtils.h>
      27              : #include <utils/common/WrappingCommand.h>
      28              : #include <utils/vehicle/SUMOVehicle.h>
      29              : #include <microsim/MSEventControl.h>
      30              : #include <microsim/MSParkingArea.h>
      31              : #include <microsim/MSVehicleType.h>
      32              : #include <microsim/MSStoppingPlace.h>
      33              : #include <microsim/devices/MSDevice_Battery.h>
      34              : #include <microsim/MSNet.h>
      35              : #include "MSChargingStation.h"
      36              : 
      37              : 
      38              : // ===========================================================================
      39              : // member method definitions
      40              : // ===========================================================================
      41              : 
      42        15229 : MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
      43              :                                      const std::string& name, double chargingPower, double totalPower, double efficency, bool chargeInTransit,
      44        15229 :                                      SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
      45        15229 :     MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
      46        30458 :     myChargeInTransit(chargeInTransit), myChargeType(stringToChargeType(chargeType)), myTotalPowerCheckEvent(nullptr) {
      47        15229 :     if (chargingPower < 0) {
      48            0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
      49              :     } else {
      50        15229 :         myNominalChargingPower = chargingPower;
      51        15229 :         myTotalChargingPower = totalPower;
      52              :     }
      53        15229 :     if (efficency < 0 || efficency > 1) {
      54            6 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
      55              :     } else {
      56        15227 :         myEfficiency = efficency;
      57              :     }
      58        15229 :     if (chargeDelay < 0) {
      59            0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
      60              :     } else {
      61        15229 :         myChargeDelay = chargeDelay;
      62              :     }
      63        15229 :     if (waitingTime < 0) {
      64            0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
      65              :     } else {
      66        15229 :         myWaitingTime = waitingTime;
      67              :     }
      68        15229 :     if (getBeginLanePosition() > getEndLanePosition()) {
      69            0 :         WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
      70              :     }
      71        15229 : }
      72              : 
      73              : 
      74           17 : MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
      75           17 :                                      double totalPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
      76           17 :     MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
      77           34 :                       name, chargingPower, totalPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
      78           17 :     myParkingArea = parkingArea;
      79           17 : }
      80              : 
      81              : 
      82        30067 : MSChargingStation::~MSChargingStation() {
      83        45249 : }
      84              : 
      85              : 
      86              : double
      87       101310 : MSChargingStation::getChargingPower(bool usingFuel) const {
      88       101310 :     if (usingFuel) {
      89         2333 :         return myNominalChargingPower;
      90              :     } else {
      91              :         // Convert from [Ws] to [Wh] (3600s / 1h):
      92        98977 :         return myNominalChargingPower / 3600;
      93              :     }
      94              : }
      95              : 
      96              : 
      97              : double
      98       100871 : MSChargingStation::getEfficency() const {
      99       100871 :     return myEfficiency;
     100              : }
     101              : 
     102              : 
     103              : bool
     104       104020 : MSChargingStation::getChargeInTransit() const {
     105       104020 :     return myChargeInTransit;
     106              : }
     107              : 
     108              : 
     109              : SUMOTime
     110       101024 : MSChargingStation::getChargeDelay() const {
     111       101024 :     return myChargeDelay;
     112              : }
     113              : 
     114              : 
     115              : MSChargingStation::ChargeType
     116       100956 : MSChargingStation::getChargeType() const {
     117       100956 :     return myChargeType;
     118              : }
     119              : 
     120              : 
     121              : SUMOTime
     122            0 : MSChargingStation::getWaitingTime() const {
     123            0 :     return myWaitingTime;
     124              : }
     125              : 
     126              : 
     127              : const MSParkingArea*
     128       107428 : MSChargingStation::getParkingArea() const {
     129       107428 :     return myParkingArea;
     130              : }
     131              : 
     132              : 
     133              : void
     134          511 : MSChargingStation::setChargingPower(double chargingPower) {
     135          511 :     myNominalChargingPower = chargingPower;
     136          511 : }
     137              : 
     138              : 
     139              : void
     140          511 : MSChargingStation::setEfficiency(double efficiency) {
     141          511 :     myEfficiency = efficiency;
     142          511 : }
     143              : 
     144              : 
     145              : void
     146           10 : MSChargingStation::setChargeDelay(SUMOTime delay) {
     147           10 :     myChargeDelay = delay;
     148           10 : }
     149              : 
     150              : 
     151              : void
     152           10 : MSChargingStation::setChargeInTransit(bool value) {
     153           10 :     myChargeInTransit = value;
     154           10 :     if (myTotalChargingPower > 0 && myChargeInTransit && myTotalPowerCheckEvent == nullptr) {
     155            0 :         myTotalPowerCheckEvent = new WrappingCommand<MSChargingStation>(this, &MSChargingStation::checkTotalPower);
     156            0 :         MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myTotalPowerCheckEvent);
     157              :     }
     158           10 : }
     159              : 
     160              : 
     161              : void
     162       102744 : MSChargingStation::setChargingVehicle(bool value) {
     163       102744 :     myChargingVehicle = value;
     164       102744 :     if (myTotalChargingPower > 0 && myChargingVehicle && myTotalPowerCheckEvent == nullptr) {
     165            8 :         myTotalPowerCheckEvent = new WrappingCommand<MSChargingStation>(this, &MSChargingStation::checkTotalPower);
     166            8 :         MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myTotalPowerCheckEvent);
     167              :     }
     168       102744 : }
     169              : 
     170              : 
     171              : SUMOTime
     172         1282 : MSChargingStation::checkTotalPower(SUMOTime currentTime) {
     173         1282 :     if (!myChargeInTransit && !myChargingVehicle) {
     174            4 :         myTotalPowerCheckEvent = nullptr;
     175              :         myChargedBatteries.clear();
     176            4 :         return 0;
     177              :     }
     178              :     double sumReqWh = 0;
     179              :     std::vector<Charge*> thisStepCharges;
     180         3784 :     for (auto& kv : myChargeValues) {
     181         2506 :         if (MSNet::getInstance()->getVehicleControl().getVehicle(kv.first) == nullptr) {
     182           17 :             continue;
     183              :         }
     184              :         Charge& lastcharge = kv.second.back();
     185         2489 :         if (lastcharge.timeStep == currentTime) {
     186          296 :             sumReqWh += lastcharge.WCharged;
     187          296 :             thisStepCharges.push_back(&lastcharge);
     188              :         }
     189              :     }
     190         1278 :     if (thisStepCharges.size() < 2) {
     191         1174 :         return DELTA_T;
     192              :     }
     193          104 :     const double capWh = myTotalChargingPower * myEfficiency /*W*/ * TS /*s*/ / 3600.0; // convert to Wh
     194              : #ifdef DEBUG_SIMSTEP
     195              :     std::cout << "checkTotalPower: CS="
     196              :               << this->myID << " currentTime=" << currentTime << " myTotalChargingPower=" << myTotalChargingPower;
     197              :     if (sumReqWh > capWh && sumReqWh > 0) {
     198              :         std::cout << " exceeded, needs rebalancing!";
     199              :     }
     200              :     std::cout << std::endl;
     201              : #endif
     202          104 :     if (sumReqWh > capWh && sumReqWh > 0) {
     203          100 :         const double ratio = capWh / sumReqWh;
     204          300 :         for (auto* charge : thisStepCharges) {
     205          200 :             MSDevice_Battery* battery = myChargedBatteries[charge->vehicleID];
     206          200 :             double abc = battery->getActualBatteryCapacity();
     207              : 
     208          200 :             const double deliveredWh = charge->WCharged * ratio;
     209          200 :             const double excessWh = charge->WCharged - deliveredWh;
     210          200 :             charge->WCharged = deliveredWh;
     211          200 :             if (charge->chargingEfficiency > 0 && TS > 0) {
     212              :                 // derive power [W] from energy [Wh]: Power = (Energy [Wh] * 3600) [Ws] / (efficiency * TS [s])
     213              :                 // ergo we are doing [Ws/s -> W]
     214          200 :                 charge->chargingPower = (deliveredWh * 3600.0) / (charge->chargingEfficiency * TS);
     215              :             }
     216          200 :             charge->actualBatteryCapacity = abc - excessWh;
     217          200 :             charge->totalEnergyCharged -= excessWh;
     218              : 
     219              :             //  inform also battery device
     220          200 :             battery->setActualBatteryCapacity(abc - excessWh);
     221          200 :             battery->setEnergyCharged(deliveredWh);
     222          200 :             myTotalCharge -= excessWh;
     223              : 
     224              : #ifdef DEBUG_SIMSTEP
     225              :             std::cout << "time=" << time2string(currentTime)
     226              :                       << " vehID=" << charge->vehicleID
     227              :                       << " requestedWh=" << (deliveredWh + excessWh)
     228              :                       << " deliveredWh=" << deliveredWh
     229              :                       << " deliveredW="  << charge->chargingPower
     230              :                       << " ratio=" << ratio << std::endl;
     231              : #endif
     232              :         }
     233              : #ifdef DEBUG_SIMSTEP
     234              :         std::cout << "===============\n\n";
     235              : #endif
     236              :     }
     237          104 :     return DELTA_T;
     238         1278 : }
     239              : 
     240              : 
     241              : bool
     242            0 : MSChargingStation::vehicleIsInside(const double position) const {
     243            0 :     if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
     244              :         return true;
     245              :     } else {
     246            0 :         return false;
     247              :     }
     248              : }
     249              : 
     250              : 
     251              : bool
     252            0 : MSChargingStation::isCharging() const {
     253            0 :     return myChargingVehicle;
     254              : }
     255              : 
     256              : 
     257              : void
     258       100460 : MSChargingStation::addChargeValueForOutput(double WCharged, MSDevice_Battery* battery) {
     259       200920 :     if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
     260         1727 :         return;
     261              :     }
     262        98733 :     std::string status = "";
     263        98733 :     if (battery->getChargingStartTime() > myChargeDelay) {
     264        98021 :         if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
     265              :             status = "chargingStopped";
     266          842 :         } else if (myChargeInTransit) {
     267              :             status = "chargingInTransit";
     268              :         } else {
     269              :             status = "noCharging";
     270              :         }
     271              :     } else {
     272          712 :         if (myChargeInTransit) {
     273              :             status = "waitingChargeInTransit";
     274          344 :         } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
     275              :             status = "waitingChargeStopped";
     276              :         } else {
     277              :             status = "noWaitingCharge";
     278              :         }
     279              :     }
     280              :     // update total charge
     281        98733 :     myTotalCharge += WCharged;
     282              :     // create charge row and insert it in myChargeValues
     283              :     const std::string vehID = battery->getHolder().getID();
     284              :     if (myChargeValues.count(vehID) == 0) {
     285          389 :         myChargedVehicles.push_back(vehID);
     286          389 :         myChargedBatteries[vehID] = battery;
     287              :     }
     288        98733 :     Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
     289              :              status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
     290       296199 :              myNominalChargingPower, myEfficiency, myTotalCharge);
     291        98733 :     myChargeValues[vehID].push_back(C);
     292        98733 : }
     293              : 
     294              : 
     295              : void
     296          443 : MSChargingStation::writeChargingStationOutput(OutputDevice& output) {
     297          443 :     int chargingSteps = 0;
     298          724 :     for (const auto& item : myChargeValues) {
     299          281 :         chargingSteps += (int)item.second.size();
     300              :     }
     301          443 :     output.openTag(SUMO_TAG_CHARGING_STATION);
     302          443 :     output.writeAttr(SUMO_ATTR_ID, myID);
     303          443 :     output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED, myTotalCharge);
     304          443 :     output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
     305              :     // start writing
     306          443 :     if (myChargeValues.size() > 0) {
     307          427 :         for (const std::string& vehID : myChargedVehicles) {
     308              :             int iStart = 0;
     309          281 :             const auto& chargeSteps = myChargeValues[vehID];
     310          562 :             while (iStart < (int)chargeSteps.size()) {
     311          281 :                 int iEnd = iStart + 1;
     312          281 :                 double charged = chargeSteps[iStart].WCharged;
     313        93715 :                 while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
     314        93434 :                     charged += chargeSteps[iEnd].WCharged;
     315        93434 :                     iEnd++;
     316              :                 }
     317          281 :                 writeVehicle(output, chargeSteps, iStart, iEnd, charged);
     318              :                 iStart = iEnd;
     319              :             }
     320              :         }
     321              :     }
     322              :     // close charging station tag
     323          443 :     output.closeTag();
     324          443 : }
     325              : 
     326              : 
     327              : void
     328        52074 : MSChargingStation::writeAggregatedChargingStationOutput(OutputDevice& output, bool includeUnfinished) {
     329              :     std::vector<std::string> terminatedChargers;
     330        57304 :     for (const auto& item : myChargeValues) {
     331              :         const Charge& lastCharge = item.second.back();
     332              :         // no charge during the last time step == has stopped charging
     333         5230 :         bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
     334         5230 :         if (finished || includeUnfinished) {
     335          108 :             if (finished) {
     336          104 :                 terminatedChargers.push_back(item.first);
     337              :             }
     338              :             // aggregate values
     339          108 :             double charged = 0.;
     340          108 :             double minPower = lastCharge.chargingPower;
     341          108 :             double maxPower = lastCharge.chargingPower;
     342          108 :             double minCharge = lastCharge.WCharged;
     343          108 :             double maxCharge = lastCharge.WCharged;
     344          108 :             double minEfficiency = lastCharge.chargingEfficiency;
     345          108 :             double maxEfficiency = lastCharge.chargingEfficiency;
     346         5126 :             for (const auto& charge : item.second) {
     347         5018 :                 charged += charge.WCharged;
     348         5018 :                 if (charge.chargingPower < minPower) {
     349            0 :                     minPower = charge.chargingPower;
     350              :                 }
     351         5018 :                 if (charge.chargingPower > maxPower) {
     352            0 :                     maxPower = charge.chargingPower;
     353              :                 }
     354         5018 :                 if (charge.WCharged < minCharge) {
     355           32 :                     minCharge = charge.WCharged;
     356              :                 }
     357         5018 :                 if (charge.WCharged > maxCharge) {
     358            4 :                     maxCharge = charge.WCharged;
     359              :                 }
     360         5018 :                 if (charge.chargingEfficiency < minEfficiency) {
     361            0 :                     minEfficiency = charge.chargingEfficiency;
     362              :                 }
     363         5018 :                 if (charge.chargingEfficiency > maxEfficiency) {
     364            0 :                     maxEfficiency = charge.chargingEfficiency;
     365              :                 }
     366              :             }
     367              :             // actually write the data
     368          108 :             output.openTag(SUMO_TAG_CHARGING_EVENT);
     369          108 :             output.writeAttr(SUMO_ATTR_CHARGINGSTATIONID, myID);
     370          108 :             output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
     371          108 :             output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
     372          108 :             output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
     373          108 :             output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
     374          108 :             if (finished) {
     375          104 :                 output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(lastCharge.timeStep));
     376              :             }
     377          108 :             output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, lastCharge.actualBatteryCapacity);
     378          108 :             output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, lastCharge.maxBatteryCapacity);
     379          108 :             output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
     380          108 :             output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
     381          108 :             output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
     382          108 :             output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
     383          108 :             output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
     384          108 :             output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
     385          216 :             output.closeTag();
     386              :         }
     387              :     }
     388              : 
     389              :     // clear charging data of vehicles which terminated charging
     390        52178 :     for (auto vehID : terminatedChargers) {
     391              :         myChargeValues.erase(vehID);
     392              :     }
     393        52074 : }
     394              : 
     395              : 
     396              : void
     397          281 : MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
     398          281 :     const Charge& first = chargeSteps[iStart];
     399          281 :     out.openTag(SUMO_TAG_VEHICLE);
     400          281 :     out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
     401          281 :     out.writeAttr(SUMO_ATTR_TYPE, first.vehicleType);
     402          281 :     out.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
     403          281 :     out.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(first.timeStep));
     404          281 :     out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
     405        93996 :     for (int i = iStart; i < iEnd; i++) {
     406        93715 :         const Charge& c = chargeSteps[i];
     407        93715 :         out.openTag(SUMO_TAG_STEP);
     408        93715 :         out.writeAttr(SUMO_ATTR_TIME, time2string(c.timeStep));
     409              :         // charge values
     410        93715 :         out.writeAttr(SUMO_ATTR_CHARGING_STATUS, c.status);
     411        93715 :         out.writeAttr(SUMO_ATTR_ENERGYCHARGED, c.WCharged);
     412        93715 :         out.writeAttr(SUMO_ATTR_PARTIALCHARGE, c.totalEnergyCharged);
     413              :         // charging values of charging station in this timestep
     414        93715 :         out.writeAttr(SUMO_ATTR_CHARGINGPOWER, c.chargingPower);
     415        93715 :         out.writeAttr(SUMO_ATTR_EFFICIENCY, c.chargingEfficiency);
     416              :         // battery status of vehicle
     417        93715 :         out.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, c.actualBatteryCapacity);
     418        93715 :         out.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, c.maxBatteryCapacity);
     419              :         // close tag timestep
     420       187430 :         out.closeTag();
     421              :     }
     422          281 :     out.closeTag();
     423          281 : }
     424              : 
     425              : 
     426              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1