LCOV - code coverage report
Current view: top level - src/microsim/trigger - MSChargingStation.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 87 107 81.3 %
Date: 2024-05-07 15:28:01 Functions: 12 17 70.6 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2001-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    MSChargingStation.cpp
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Tamas Kurczveil
      17             : /// @author  Pablo Alvarez Lopez
      18             : /// @date    20-12-13
      19             : ///
      20             : // Chargin Station for Electric vehicles
      21             : /****************************************************************************/
      22             : #include <config.h>
      23             : 
      24             : #include <cassert>
      25             : #include <utils/common/StringUtils.h>
      26             : #include <utils/vehicle/SUMOVehicle.h>
      27             : #include <microsim/MSParkingArea.h>
      28             : #include <microsim/MSVehicleType.h>
      29             : #include <microsim/MSStoppingPlace.h>
      30             : #include <microsim/devices/MSDevice_Battery.h>
      31             : #include <microsim/MSNet.h>
      32             : #include "MSChargingStation.h"
      33             : 
      34             : 
      35             : // ===========================================================================
      36             : // member method definitions
      37             : // ===========================================================================
      38             : 
      39        5959 : MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
      40             :                                      const std::string& name, double chargingPower, double efficency, bool chargeInTransit,
      41        5959 :                                      SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
      42        5959 :     MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
      43       11918 :     myChargeInTransit(chargeInTransit) {
      44        5959 :     if (chargingPower < 0) {
      45           0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
      46             :     } else {
      47        5959 :         myChargingPower = chargingPower;
      48             :     }
      49        5959 :     if (efficency < 0 || efficency > 1) {
      50          10 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
      51             :     } else {
      52        5957 :         myEfficiency = efficency;
      53             :     }
      54        5959 :     if (chargeDelay < 0) {
      55           0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
      56             :     } else {
      57        5959 :         myChargeDelay = chargeDelay;
      58             :     }
      59        5980 :     if ((chargeType != "normal") && (chargeType != "electric") && (chargeType != "fuel")) {
      60           0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGETYPE), getID(), chargeType))
      61             :     } else {
      62        5959 :         myChargeDelay = chargeDelay;
      63             :     }
      64        5959 :     if (waitingTime < 0) {
      65           0 :         WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
      66             :     } else {
      67        5959 :         myChargeDelay = chargeDelay;
      68             :     }
      69        5959 :     if (getBeginLanePosition() > getEndLanePosition()) {
      70           0 :         WRITE_WARNING(TLF("ChargingStation with ID='getID()' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
      71             :     }
      72        5959 : }
      73             : 
      74             : 
      75           0 : MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
      76           0 :                                      double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
      77           0 :     MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
      78           0 :                       name, chargingPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
      79           0 :     myParkingArea = parkingArea;
      80           0 : }
      81             : 
      82             : 
      83       11641 : MSChargingStation::~MSChargingStation() {
      84       17544 : }
      85             : 
      86             : 
      87             : double
      88        5092 : MSChargingStation::getChargingPower(bool usingFuel) const {
      89        5092 :     if (usingFuel) {
      90         344 :         return myChargingPower;
      91             :     } else {
      92             :         // Convert from [Ws] to [Wh] (3600s / 1h):
      93        4748 :         return myChargingPower / 3600;
      94             :     }
      95             : }
      96             : 
      97             : 
      98             : double
      99        5084 : MSChargingStation::getEfficency() const {
     100        5084 :     return myEfficiency;
     101             : }
     102             : 
     103             : 
     104             : bool
     105        8020 : MSChargingStation::getChargeInTransit() const {
     106        8020 :     return myChargeInTransit;
     107             : }
     108             : 
     109             : 
     110             : SUMOTime
     111        5792 : MSChargingStation::getChargeDelay() const {
     112        5792 :     return myChargeDelay;
     113             : }
     114             : 
     115             : 
     116             : const std::string&
     117           0 : MSChargingStation::getChargeType() const {
     118           0 :     return myChargeType;
     119             : }
     120             : 
     121             : 
     122             : SUMOTime
     123           0 : MSChargingStation::getWaitingTime() const {
     124           0 :     return myWaitingTime;
     125             : }
     126             : 
     127             : 
     128        7189 : const MSParkingArea* MSChargingStation::getParkingArea() const {
     129        7189 :     return myParkingArea;
     130             : }
     131             : 
     132             : 
     133             : void
     134        7054 : MSChargingStation::setChargingVehicle(bool value) {
     135        7054 :     myChargingVehicle = value;
     136        7054 : }
     137             : 
     138             : 
     139             : bool
     140           0 : MSChargingStation::vehicleIsInside(const double position) const {
     141           0 :     if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
     142             :         return true;
     143             :     } else {
     144           0 :         return false;
     145             :     }
     146             : }
     147             : 
     148             : 
     149             : bool
     150           0 : MSChargingStation::isCharging() const {
     151           0 :     return myChargingVehicle;
     152             : }
     153             : 
     154             : 
     155             : void
     156        5792 : MSChargingStation::addChargeValueForOutput(double WCharged, MSDevice_Battery* battery) {
     157        5792 :     std::string status = "";
     158        5792 :     if (battery->getChargingStartTime() > myChargeDelay) {
     159        5084 :         if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
     160             :             status = "chargingStopped";
     161         567 :         } else if (myChargeInTransit) {
     162             :             status = "chargingInTransit";
     163             :         } else {
     164             :             status = "noCharging";
     165             :         }
     166             :     } else {
     167         708 :         if (myChargeInTransit) {
     168             :             status = "waitingChargeInTransit";
     169         320 :         } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
     170             :             status = "waitingChargeStopped";
     171             :         } else {
     172             :             status = "noWaitingCharge";
     173             :         }
     174             :     }
     175             :     // update total charge
     176        5792 :     myTotalCharge += WCharged;
     177             :     // create charge row and insert it in myChargeValues
     178             :     const std::string vehID = battery->getHolder().getID();
     179             :     if (myChargeValues.count(vehID) == 0) {
     180         327 :         myChargedVehicles.push_back(vehID);
     181             :     }
     182        5792 :     Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
     183             :              status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
     184       20698 :              myChargingPower, myEfficiency, myTotalCharge);
     185        5792 :     myChargeValues[vehID].push_back(C);
     186       11584 : }
     187             : 
     188             : 
     189             : void
     190         182 : MSChargingStation::writeChargingStationOutput(OutputDevice& output) {
     191         182 :     int chargingSteps = 0;
     192         333 :     for (const auto& item : myChargeValues) {
     193         151 :         chargingSteps += (int)item.second.size();
     194             :     }
     195         182 :     output.openTag(SUMO_TAG_CHARGING_STATION);
     196         182 :     output.writeAttr(SUMO_ATTR_ID, myID);
     197         182 :     output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED, myTotalCharge);
     198             :     output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
     199             :     // start writting
     200         182 :     if (myChargeValues.size() > 0) {
     201         215 :         for (const std::string& vehID : myChargedVehicles) {
     202             :             int iStart = 0;
     203         151 :             const auto& chargeSteps = myChargeValues[vehID];
     204         303 :             while (iStart < (int)chargeSteps.size()) {
     205         152 :                 int iEnd = iStart + 1;
     206         152 :                 double charged = chargeSteps[iStart].WCharged;
     207        4138 :                 while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
     208        3986 :                     charged += chargeSteps[iEnd].WCharged;
     209        3986 :                     iEnd++;
     210             :                 }
     211         152 :                 writeVehicle(output, chargeSteps, iStart, iEnd, charged);
     212             :                 iStart = iEnd;
     213             :             }
     214             :         }
     215             :     }
     216             :     // close charging station tag
     217         182 :     output.closeTag();
     218         182 : }
     219             : 
     220             : void
     221         152 : MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
     222         152 :     const Charge& first = chargeSteps[iStart];
     223         152 :     out.openTag(SUMO_TAG_VEHICLE);
     224         152 :     out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
     225         152 :     out.writeAttr(SUMO_ATTR_TYPE, first.vehicleType);
     226             :     out.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
     227         152 :     out.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(first.timeStep));
     228         152 :     out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
     229        4290 :     for (int i = iStart; i < iEnd; i++) {
     230        4138 :         const Charge& c = chargeSteps[i];
     231        4138 :         out.openTag(SUMO_TAG_STEP);
     232        4138 :         out.writeAttr(SUMO_ATTR_TIME, time2string(c.timeStep));
     233             :         // charge values
     234        4138 :         out.writeAttr(SUMO_ATTR_CHARGING_STATUS, c.status);
     235        4138 :         out.writeAttr(SUMO_ATTR_ENERGYCHARGED, c.WCharged);
     236        4138 :         out.writeAttr(SUMO_ATTR_PARTIALCHARGE, c.totalEnergyCharged);
     237             :         // charging values of charging station in this timestep
     238        4138 :         out.writeAttr(SUMO_ATTR_CHARGINGPOWER, c.chargingPower);
     239        4138 :         out.writeAttr(SUMO_ATTR_EFFICIENCY, c.chargingEfficiency);
     240             :         // battery status of vehicle
     241        4138 :         out.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, c.actualBatteryCapacity);
     242        4138 :         out.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, c.maxBatteryCapacity);
     243             :         // close tag timestep
     244        8276 :         out.closeTag();
     245             :     }
     246         152 :     out.closeTag();
     247         152 : }
     248             : 
     249             : 
     250             : /****************************************************************************/

Generated by: LCOV version 1.14