Eclipse SUMO - Simulation of Urban MObility
MSChargingStation.cpp
Go to the documentation of this file.
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 /****************************************************************************/
21 // Charging Station for Electric vehicles
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <cassert>
28 #include <microsim/MSParkingArea.h>
29 #include <microsim/MSVehicleType.h>
32 #include <microsim/MSNet.h>
33 #include "MSChargingStation.h"
34 
35 
36 // ===========================================================================
37 // member method definitions
38 // ===========================================================================
39 
40 MSChargingStation::MSChargingStation(const std::string& chargingStationID, MSLane& lane, double startPos, double endPos,
41  const std::string& name, double chargingPower, double efficency, bool chargeInTransit,
42  SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
43  MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
44  myChargeInTransit(chargeInTransit) {
45  if (chargingPower < 0) {
46  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
47  } else {
48  myChargingPower = chargingPower;
49  }
50  if (efficency < 0 || efficency > 1) {
51  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
52  } else {
53  myEfficiency = efficency;
54  }
55  if (chargeDelay < 0) {
56  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
57  } else {
58  myChargeDelay = chargeDelay;
59  }
60  if ((chargeType != "normal") && (chargeType != "electric") && (chargeType != "fuel")) {
61  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGETYPE), getID(), chargeType))
62  } else {
63  myChargeDelay = chargeDelay;
64  }
65  if (waitingTime < 0) {
66  WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
67  } else {
68  myChargeDelay = chargeDelay;
69  }
71  WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
72  }
73 }
74 
75 
76 MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
77  double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
78  MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
79  name, chargingPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
80  myParkingArea = parkingArea;
81 }
82 
83 
85 }
86 
87 
88 double
89 MSChargingStation::getChargingPower(bool usingFuel) const {
90  if (usingFuel) {
91  return myChargingPower;
92  } else {
93  // Convert from [Ws] to [Wh] (3600s / 1h):
94  return myChargingPower / 3600;
95  }
96 }
97 
98 
99 double
101  return myEfficiency;
102 }
103 
104 
105 bool
107  return myChargeInTransit;
108 }
109 
110 
111 SUMOTime
113  return myChargeDelay;
114 }
115 
116 
117 const std::string&
119  return myChargeType;
120 }
121 
122 
123 SUMOTime
125  return myWaitingTime;
126 }
127 
128 
129 const MSParkingArea*
131  return myParkingArea;
132 }
133 
134 
135 void
136 MSChargingStation::setChargingPower(double chargingPower) {
137  myChargingPower = chargingPower;
138 }
139 
140 
141 void
143  myEfficiency = efficiency;
144 }
145 
146 
147 void
149  myChargeDelay = delay;
150 }
151 
152 
153 void
155  myChargeInTransit = value;
156 }
157 
158 
159 void
161  myChargingVehicle = value;
162 }
163 
164 
165 bool
166 MSChargingStation::vehicleIsInside(const double position) const {
167  if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
168  return true;
169  } else {
170  return false;
171  }
172 }
173 
174 
175 bool
177  return myChargingVehicle;
178 }
179 
180 
181 void
183  if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
184  return;
185  }
186  std::string status = "";
187  if (battery->getChargingStartTime() > myChargeDelay) {
188  if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
189  status = "chargingStopped";
190  } else if (myChargeInTransit) {
191  status = "chargingInTransit";
192  } else {
193  status = "noCharging";
194  }
195  } else {
196  if (myChargeInTransit) {
197  status = "waitingChargeInTransit";
198  } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
199  status = "waitingChargeStopped";
200  } else {
201  status = "noWaitingCharge";
202  }
203  }
204  // update total charge
205  myTotalCharge += WCharged;
206  // create charge row and insert it in myChargeValues
207  const std::string vehID = battery->getHolder().getID();
208  if (myChargeValues.count(vehID) == 0) {
209  myChargedVehicles.push_back(vehID);
210  }
211  Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
212  status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
214  myChargeValues[vehID].push_back(C);
215 }
216 
217 
218 void
220  int chargingSteps = 0;
221  for (const auto& item : myChargeValues) {
222  chargingSteps += (int)item.second.size();
223  }
225  output.writeAttr(SUMO_ATTR_ID, myID);
227  output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
228  // start writing
229  if (myChargeValues.size() > 0) {
230  for (const std::string& vehID : myChargedVehicles) {
231  int iStart = 0;
232  const auto& chargeSteps = myChargeValues[vehID];
233  while (iStart < (int)chargeSteps.size()) {
234  int iEnd = iStart + 1;
235  double charged = chargeSteps[iStart].WCharged;
236  while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
237  charged += chargeSteps[iEnd].WCharged;
238  iEnd++;
239  }
240  writeVehicle(output, chargeSteps, iStart, iEnd, charged);
241  iStart = iEnd;
242  }
243  }
244  }
245  // close charging station tag
246  output.closeTag();
247 }
248 
249 
250 void
252  std::vector<std::string> terminatedChargers;
253  for (const auto& item : myChargeValues) {
254  const Charge& lastCharge = item.second.back();
255  if (lastCharge.timeStep < SIMSTEP - DELTA_T) {
256  // no charge during the last time step == has stopped charging
257  terminatedChargers.push_back(item.first);
258 
259  // aggregate values
260  double charged = 0.;
261  double minPower = lastCharge.chargingPower;
262  double maxPower = lastCharge.chargingPower;
263  double minCharge = lastCharge.WCharged;
264  double maxCharge = lastCharge.WCharged;
265  double minEfficiency = lastCharge.chargingEfficiency;
266  double maxEfficiency = lastCharge.chargingEfficiency;
267 
268  for (const auto& charge : item.second) {
269  charged += charge.WCharged;
270  if (charge.chargingPower < minPower) {
271  minPower = charge.chargingPower;
272  }
273  if (charge.chargingPower > maxPower) {
274  maxPower = charge.chargingPower;
275  }
276  if (charge.WCharged < minCharge) {
277  minCharge = charge.WCharged;
278  }
279  if (charge.WCharged > maxCharge) {
280  maxCharge = charge.WCharged;
281  }
282  if (charge.chargingEfficiency < minEfficiency) {
283  minEfficiency = charge.chargingEfficiency;
284  }
285  if (charge.chargingEfficiency > maxEfficiency) {
286  maxEfficiency = charge.chargingEfficiency;
287  }
288  }
289 
290  // actually write the data
293  output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
294  output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
296  output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
300  output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
301  output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
302  output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
303  output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
304  output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
305  output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
306  output.closeTag();
307  }
308  }
309 
310  // clear charging data of vehicles which terminated charging
311  for (auto vehID : terminatedChargers) {
312  myChargeValues.erase(vehID);
313  }
314 }
315 
316 
317 void
318 MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
319  const Charge& first = chargeSteps[iStart];
321  out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
325  out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
326  for (int i = iStart; i < iEnd; i++) {
327  const Charge& c = chargeSteps[i];
328  out.openTag(SUMO_TAG_STEP);
330  // charge values
334  // charging values of charging station in this timestep
337  // battery status of vehicle
340  // close tag timestep
341  out.closeTag();
342  }
343  out.closeTag();
344 }
345 
346 
347 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:295
#define TLF(string,...)
Definition: MsgHandler.h:317
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define SIMSTEP
Definition: SUMOTime.h:61
@ SUMO_TAG_CHARGING_STATION
A Charging Station.
@ SUMO_TAG_CHARGING_EVENT
A charging event (charging a vehicle at a charging station)
@ SUMO_TAG_STEP
trigger: a step description
@ SUMO_TAG_VEHICLE
description of a vehicle
@ SUMO_ATTR_PARTIALCHARGE
energy provided by charging station at certain timestep
@ SUMO_ATTR_TOTALENERGYCHARGED
@ SUMO_ATTR_MINPOWER
minimum charging power encountered during the charging event
@ SUMO_ATTR_WAITINGTIME
@ SUMO_ATTR_CHARGETYPE
Charge type (fuel or electric)
@ SUMO_ATTR_MAXIMUMBATTERYCAPACITY
Maxium battery capacity.
@ SUMO_ATTR_MINEFFICIENCY
minimum charging efficiency encountered during the charging event
@ SUMO_ATTR_MAXCHARGE
maximum energy charged during one time step of the charging event
@ SUMO_ATTR_MAXPOWER
@ SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE
total energy charged into a single vehicle
@ SUMO_ATTR_CHARGINGSTATIONID
Charging Station ID.
@ SUMO_ATTR_ACTUALBATTERYCAPACITY
@ SUMO_ATTR_ENERGYCHARGED
tgotal of Energy charged
@ SUMO_ATTR_CHARGINGPOWER
@ SUMO_ATTR_MAXEFFICIENCY
maximum charging efficiency encountered during the charging event
@ SUMO_ATTR_VEHICLE
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_CHARGINGSTEPS
number of steps that a vehicle is charging
@ SUMO_ATTR_CHARGINGEND
timesteps in which charging ends
@ SUMO_ATTR_EFFICIENCY
Eficiency of the charge in Charging Stations.
@ SUMO_ATTR_CHARGINGBEGIN
timestep in which charging begins
@ SUMO_ATTR_ID
@ SUMO_ATTR_MINCHARGE
minimum energy charged during one time step of the charging event
@ SUMO_ATTR_CHARGEDELAY
Delay in the charge of charging stations (different of waiting time)
@ SUMO_ATTR_TIME
trigger: the time of the step
@ SUMO_ATTR_CHARGING_STATUS
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double myChargingPower
Charging station's charging power.
void setChargeInTransit(bool value)
set charging in transit
void writeChargingStationOutput(OutputDevice &output)
write charging station values
double myTotalCharge
total energy charged by this charging station
static void writeVehicle(OutputDevice &out, const std::vector< Charge > &chargeSteps, int iStart, int iEnd, double charged)
double getChargingPower(bool usingFuel) const
Get charging station's charging power.
bool getChargeInTransit() const
Get chargeInTransit.
const std::string & getChargeType() const
Get charge type.
void setChargingVehicle(bool value)
enable or disable charging vehicle
const std::string myChargeType
charge type
std::vector< std::string > myChargedVehicles
order vehicles by time of first charge
double myEfficiency
Efficiency of the charging station.
void setEfficiency(double efficiency)
set efficiency of the charging station
void setChargingPower(double chargingPower)
set charging station's charging power
SUMOTime myWaitingTime
waiting time
bool myChargeInTransit
Allow charge in transit.
bool vehicleIsInside(const double position) const
Check if a vehicle is inside in the Charge Station.
const MSParkingArea * myParkingArea
parkingArea the charging station is placed on
std::map< std::string, std::vector< Charge > > myChargeValues
map with the charges of this charging station (key = vehicleID)
void addChargeValueForOutput(double WCharged, MSDevice_Battery *battery)
add charge value for output
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string &chargeType, SUMOTime waitingTime)
constructor
SUMOTime getChargeDelay() const
Get Charge Delay.
void writeAggregatedChargingStationOutput(OutputDevice &output)
write ungrouped output (flush data after writing)
void setChargeDelay(SUMOTime delay)
set charging delay of the charging station
double getEfficency() const
Get efficiency of the charging station.
SUMOTime myChargeDelay
Charge Delay.
~MSChargingStation()
destructor
bool myChargingVehicle
Check if in the current TimeStep chargingStation is charging a vehicle.
bool isCharging() const
Return true if in the current time step charging station is charging a vehicle.
SUMOTime getWaitingTime() const
Get waiting time.
const MSParkingArea * getParkingArea() const
Get the parking area the charging station is placed on.
Battery device for electric vehicles.
SUMOTime getChargingStartTime() const
Get charging start time.
double getActualBatteryCapacity() const
Get the actual vehicle's Battery Capacity in Wh.
double getMaximumBatteryCapacity() const
Get the total vehicle's Battery Capacity in Wh.
double getStoppingThreshold() const
Get stopping threshold.
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:184
A lane area vehicles can halt at.
Definition: MSParkingArea.h:60
A lane area vehicles can halt at.
double getBeginLanePosition() const
Returns the begin position of this stop.
double getEndLanePosition() const
Returns the end position of this stop.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:91
std::string myID
The name of the object.
Definition: Named.h:125
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
Definition: json.hpp:4471
struct to save information for the chargingStation output