Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-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/****************************************************************************/
21// Charging Station for Electric vehicles
22/****************************************************************************/
23#include <config.h>
24
25#include <cassert>
34#include <microsim/MSNet.h>
35#include "MSChargingStation.h"
36
37
38// ===========================================================================
39// member method definitions
40// ===========================================================================
41
42MSChargingStation::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 SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
45 MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
46 myChargeInTransit(chargeInTransit), myChargeType(stringToChargeType(chargeType)), myTotalPowerCheckEvent(nullptr) {
47 if (chargingPower < 0) {
48 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
49 } else {
50 myNominalChargingPower = chargingPower;
51 myTotalChargingPower = totalPower;
52 }
53 if (efficency < 0 || efficency > 1) {
54 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
55 } else {
56 myEfficiency = efficency;
57 }
58 if (chargeDelay < 0) {
59 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
60 } else {
61 myChargeDelay = chargeDelay;
62 }
63 if (waitingTime < 0) {
64 WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
65 } else {
66 myWaitingTime = waitingTime;
67 }
69 WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
70 }
71}
72
73
74MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
75 double totalPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
76 MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
77 name, chargingPower, totalPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
78 myParkingArea = parkingArea;
79}
80
81
84
85
86double
88 if (usingFuel) {
90 } else {
91 // Convert from [Ws] to [Wh] (3600s / 1h):
92 return myNominalChargingPower / 3600;
93 }
94}
95
96
97double
101
102
103bool
107
108
113
114
119
120
125
126
127const MSParkingArea*
131
132
133double
137
138
139void
141 myNominalChargingPower = chargingPower;
142}
143
144
145void
147 myEfficiency = efficiency;
148}
149
150
151void
155
156
157void
165
166
167void
175
176
177void
179 if (totalPower <= 0) {
180 return;
181 }
182 const bool hadLimit = myTotalChargingPower > 0;
183 myTotalChargingPower = totalPower;
184 if (!hadLimit && (myChargeInTransit || myChargingVehicle) && myTotalPowerCheckEvent == nullptr) {
187 }
188}
189
190
194 myTotalPowerCheckEvent = nullptr;
195 myChargedBatteries.clear();
196 return 0;
197 }
198 double sumReqWh = 0;
199 std::vector<Charge*> thisStepCharges;
200 for (auto& kv : myChargeValues) {
201 if (MSNet::getInstance()->getVehicleControl().getVehicle(kv.first) == nullptr) {
202 continue;
203 }
204 Charge& lastcharge = kv.second.back();
205 if (lastcharge.timeStep == currentTime) {
206 sumReqWh += lastcharge.WCharged;
207 thisStepCharges.push_back(&lastcharge);
208 }
209 }
210 if (thisStepCharges.size() < 2) {
211 return DELTA_T;
212 }
213 const double capWh = myTotalChargingPower * myEfficiency /*W*/ * TS /*s*/ / 3600.0; // convert to Wh
214#ifdef DEBUG_SIMSTEP
215 std::cout << "checkTotalPower: CS="
216 << this->myID << " currentTime=" << currentTime << " myTotalChargingPower=" << myTotalChargingPower;
217 if (sumReqWh > capWh && sumReqWh > 0) {
218 std::cout << " exceeded, needs rebalancing!";
219 }
220 std::cout << std::endl;
221#endif
222 if (sumReqWh > capWh && sumReqWh > 0) {
223 const double ratio = capWh / sumReqWh;
224 for (auto* charge : thisStepCharges) {
225 MSDevice_Battery* battery = myChargedBatteries[charge->vehicleID];
226 double abc = battery->getActualBatteryCapacity();
227
228 const double deliveredWh = charge->WCharged * ratio;
229 const double excessWh = charge->WCharged - deliveredWh;
230 charge->WCharged = deliveredWh;
231 if (charge->chargingEfficiency > 0 && TS > 0) {
232 // derive power [W] from energy [Wh]: Power = (Energy [Wh] * 3600) [Ws] / (efficiency * TS [s])
233 // ergo we are doing [Ws/s -> W]
234 charge->chargingPower = (deliveredWh * 3600.0) / (charge->chargingEfficiency * TS);
235 }
236 charge->actualBatteryCapacity = abc - excessWh;
237 charge->totalEnergyCharged -= excessWh;
238
239 // inform also battery device
240 battery->setActualBatteryCapacity(abc - excessWh);
241 battery->setEnergyCharged(deliveredWh);
242 myTotalCharge -= excessWh;
243
244#ifdef DEBUG_SIMSTEP
245 std::cout << "time=" << time2string(currentTime)
246 << " vehID=" << charge->vehicleID
247 << " requestedWh=" << (deliveredWh + excessWh)
248 << " deliveredWh=" << deliveredWh
249 << " deliveredW=" << charge->chargingPower
250 << " ratio=" << ratio << std::endl;
251#endif
252 }
253#ifdef DEBUG_SIMSTEP
254 std::cout << "===============\n\n";
255#endif
256 }
257 return DELTA_T;
258}
259
260
261bool
262MSChargingStation::vehicleIsInside(const double position) const {
263 if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
264 return true;
265 } else {
266 return false;
267 }
268}
269
270
271bool
275
276
277void
279 if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
280 return;
281 }
282 std::string status = "";
283 if (battery->getChargingStartTime() > myChargeDelay) {
284 if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
285 status = "chargingStopped";
286 } else if (myChargeInTransit) {
287 status = "chargingInTransit";
288 } else {
289 status = "noCharging";
290 }
291 } else {
292 if (myChargeInTransit) {
293 status = "waitingChargeInTransit";
294 } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
295 status = "waitingChargeStopped";
296 } else {
297 status = "noWaitingCharge";
298 }
299 }
300 // update total charge
301 myTotalCharge += WCharged;
302 // create charge row and insert it in myChargeValues
303 const std::string vehID = battery->getHolder().getID();
304 if (myChargeValues.count(vehID) == 0) {
305 myChargedVehicles.push_back(vehID);
306 myChargedBatteries[vehID] = battery;
307 }
308 Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
309 status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
311 myChargeValues[vehID].push_back(C);
312}
313
314
315void
317 int chargingSteps = 0;
318 for (const auto& item : myChargeValues) {
319 chargingSteps += (int)item.second.size();
320 }
322 output.writeAttr(SUMO_ATTR_ID, myID);
324 output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
325 // start writing
326 if (myChargeValues.size() > 0) {
327 for (const std::string& vehID : myChargedVehicles) {
328 int iStart = 0;
329 const auto& chargeSteps = myChargeValues[vehID];
330 while (iStart < (int)chargeSteps.size()) {
331 int iEnd = iStart + 1;
332 double charged = chargeSteps[iStart].WCharged;
333 while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
334 charged += chargeSteps[iEnd].WCharged;
335 iEnd++;
336 }
337 writeVehicle(output, chargeSteps, iStart, iEnd, charged);
338 iStart = iEnd;
339 }
340 }
341 }
342 // close charging station tag
343 output.closeTag();
344}
345
346
347void
349 std::vector<std::string> terminatedChargers;
350 for (const auto& item : myChargeValues) {
351 const Charge& lastCharge = item.second.back();
352 // no charge during the last time step == has stopped charging
353 bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
354 if (finished || includeUnfinished) {
355 if (finished) {
356 terminatedChargers.push_back(item.first);
357 }
358 // aggregate values
359 double charged = 0.;
360 double minPower = lastCharge.chargingPower;
361 double maxPower = lastCharge.chargingPower;
362 double minCharge = lastCharge.WCharged;
363 double maxCharge = lastCharge.WCharged;
364 double minEfficiency = lastCharge.chargingEfficiency;
365 double maxEfficiency = lastCharge.chargingEfficiency;
366 for (const auto& charge : item.second) {
367 charged += charge.WCharged;
368 if (charge.chargingPower < minPower) {
369 minPower = charge.chargingPower;
370 }
371 if (charge.chargingPower > maxPower) {
372 maxPower = charge.chargingPower;
373 }
374 if (charge.WCharged < minCharge) {
375 minCharge = charge.WCharged;
376 }
377 if (charge.WCharged > maxCharge) {
378 maxCharge = charge.WCharged;
379 }
380 if (charge.chargingEfficiency < minEfficiency) {
381 minEfficiency = charge.chargingEfficiency;
382 }
383 if (charge.chargingEfficiency > maxEfficiency) {
384 maxEfficiency = charge.chargingEfficiency;
385 }
386 }
387 // actually write the data
390 output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
391 output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
393 output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
394 if (finished) {
396 }
399 output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
400 output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
401 output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
402 output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
403 output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
404 output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
405 output.closeTag();
406 }
407 }
408
409 // clear charging data of vehicles which terminated charging
410 for (auto vehID : terminatedChargers) {
411 myChargeValues.erase(vehID);
412 }
413}
414
415
416void
417MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
418 const Charge& first = chargeSteps[iStart];
420 out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
424 out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
425 for (int i = iStart; i < iEnd; i++) {
426 const Charge& c = chargeSteps[i];
429 // charge values
433 // charging values of charging station in this timestep
436 // battery status of vehicle
439 // close tag timestep
440 out.closeTag();
441 }
442 out.closeTag();
443}
444
445
446/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TLF(string,...)
Definition MsgHandler.h:306
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:91
#define SIMSTEP
Definition SUMOTime.h:64
#define TS
Definition SUMOTime.h:45
@ 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_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:49
void setChargeInTransit(bool value)
set charging in transit
void writeChargingStationOutput(OutputDevice &output)
write charging station values
void setTotalChargingPower(double totalPower)
set charging station's total power
double myTotalCharge
total energy charged by this charging station
double getTotalChargingPower() const
Get charging station's total power.
static void writeVehicle(OutputDevice &out, const std::vector< Charge > &chargeSteps, int iStart, int iEnd, double charged)
std::map< std::string, MSDevice_Battery * > myChargedBatteries
map with the Batteries charged by this charging station (key = vehicleID)
double getChargingPower(bool usingFuel) const
Get charging station's charging power.
const ChargeType myChargeType
charge type
bool getChargeInTransit() const
Get chargeInTransit.
void setChargingVehicle(bool value)
enable or disable charging vehicle
std::vector< std::string > myChargedVehicles
order vehicles by time of first charge
MSChargingStation(const std::string &chargingStationID, MSLane &lane, double startPos, double endPos, const std::string &name, double chargingPower, double totalPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string &chargeType, SUMOTime waitingTime)
constructor
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
Command * myTotalPowerCheckEvent
Event for checking at every time-step if myTotalPower has been exceeded.
double myTotalChargingPower
The maximal charging power available to serve all charging vehicles (value <= 0 take no effect)
SUMOTime myWaitingTime
waiting time
double myNominalChargingPower
Charging station's nominal charging power per vehicle.
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
SUMOTime getChargeDelay() const
Get Charge Delay.
void writeAggregatedChargingStationOutput(OutputDevice &output, bool includeUnfinished=false)
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.
ChargeType getChargeType() const
Get charge type.
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.
SUMOTime checkTotalPower(SUMOTime currentTime)
update the delivered power to all charging vehicles after all requests are known
const MSParkingArea * getParkingArea() const
Get the parking area the charging station is placed on.
Battery device for electric vehicles.
void setEnergyCharged(const double energyCharged)
Set Energy Charged in Wh.
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.
void setActualBatteryCapacity(const double actualBatteryCapacity)
Set actual vehicle's Battery Capacity in kWh.
double getStoppingThreshold() const
Get stopping threshold.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
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:187
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition MSNet.h:495
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:392
A lane area vehicles can halt at.
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 * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
SUMOVehicle & getHolder() const
Returns the vehicle that holds this device.
const std::string & getID() const
Returns the name of the vehicle type.
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.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual double getSpeed() const =0
Returns the object's current speed.
A wrapper for a Command function.
Definition json.hpp:4471
struct to save information for the chargingStation output