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-2025 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
133void
135 myNominalChargingPower = chargingPower;
136}
137
138
139void
141 myEfficiency = efficiency;
142}
143
144
145void
149
150
151void
159
160
161void
169
170
174 myTotalPowerCheckEvent = nullptr;
175 myChargedBatteries.clear();
176 return 0;
177 }
178 double sumReqWh = 0;
179 std::vector<Charge*> thisStepCharges;
180 for (auto& kv : myChargeValues) {
181 // auto& vid = kv.first;
182 Charge& lastcharge = kv.second.back();
183 if (lastcharge.timeStep == currentTime) {
184 sumReqWh += lastcharge.WCharged;
185 thisStepCharges.push_back(&lastcharge);
186 }
187 }
188 if (thisStepCharges.size() < 2) {
189 return DELTA_T;
190 }
191 const double capWh = myTotalChargingPower * myEfficiency /*W*/ * TS /*s*/ / 3600.0; // convert to Wh
192#ifdef DEBUG_SIMSTEP
193 std::cout << "checkTotalPower: CS="
194 << this->myID << " currentTime=" << currentTime << " myTotalChargingPower=" << myTotalChargingPower;
195 if (sumReqWh > capWh && sumReqWh > 0)
196 std::cout << " exceeded, needs rebalancing!";
197 std::cout << std::endl;
198#endif
199 if (sumReqWh > capWh && sumReqWh > 0) {
200 const double ratio = capWh / sumReqWh;
201 for (auto* charge : thisStepCharges) {
202 const double deliveredWh = charge->WCharged * ratio;
203 const double excessWh = charge->WCharged - deliveredWh;
204 charge->WCharged = deliveredWh;
205
206 // inform also battery device
207 MSDevice_Battery* battery = myChargedBatteries[charge->vehicleID];
208 double abc = battery->getActualBatteryCapacity();
209 battery->setActualBatteryCapacity(abc - excessWh);
210 battery->setEnergyCharged(deliveredWh);
211 }
212 }
213 return DELTA_T;
214}
215
216
217bool
218MSChargingStation::vehicleIsInside(const double position) const {
219 if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
220 return true;
221 } else {
222 return false;
223 }
224}
225
226
227bool
231
232
233void
235 if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
236 return;
237 }
238 std::string status = "";
239 if (battery->getChargingStartTime() > myChargeDelay) {
240 if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
241 status = "chargingStopped";
242 } else if (myChargeInTransit) {
243 status = "chargingInTransit";
244 } else {
245 status = "noCharging";
246 }
247 } else {
248 if (myChargeInTransit) {
249 status = "waitingChargeInTransit";
250 } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
251 status = "waitingChargeStopped";
252 } else {
253 status = "noWaitingCharge";
254 }
255 }
256 // update total charge
257 myTotalCharge += WCharged;
258 // create charge row and insert it in myChargeValues
259 const std::string vehID = battery->getHolder().getID();
260 if (myChargeValues.count(vehID) == 0) {
261 myChargedVehicles.push_back(vehID);
262 myChargedBatteries[vehID] = battery;
263 }
264 Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
265 status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
267 myChargeValues[vehID].push_back(C);
268}
269
270
271void
273 int chargingSteps = 0;
274 for (const auto& item : myChargeValues) {
275 chargingSteps += (int)item.second.size();
276 }
278 output.writeAttr(SUMO_ATTR_ID, myID);
280 output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
281 // start writing
282 if (myChargeValues.size() > 0) {
283 for (const std::string& vehID : myChargedVehicles) {
284 int iStart = 0;
285 const auto& chargeSteps = myChargeValues[vehID];
286 while (iStart < (int)chargeSteps.size()) {
287 int iEnd = iStart + 1;
288 double charged = chargeSteps[iStart].WCharged;
289 while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
290 charged += chargeSteps[iEnd].WCharged;
291 iEnd++;
292 }
293 writeVehicle(output, chargeSteps, iStart, iEnd, charged);
294 iStart = iEnd;
295 }
296 }
297 }
298 // close charging station tag
299 output.closeTag();
300}
301
302
303void
305 std::vector<std::string> terminatedChargers;
306 for (const auto& item : myChargeValues) {
307 const Charge& lastCharge = item.second.back();
308 // no charge during the last time step == has stopped charging
309 bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
310 if (finished || includeUnfinished) {
311 if (finished) {
312 terminatedChargers.push_back(item.first);
313 }
314 // aggregate values
315 double charged = 0.;
316 double minPower = lastCharge.chargingPower;
317 double maxPower = lastCharge.chargingPower;
318 double minCharge = lastCharge.WCharged;
319 double maxCharge = lastCharge.WCharged;
320 double minEfficiency = lastCharge.chargingEfficiency;
321 double maxEfficiency = lastCharge.chargingEfficiency;
322 for (const auto& charge : item.second) {
323 charged += charge.WCharged;
324 if (charge.chargingPower < minPower) {
325 minPower = charge.chargingPower;
326 }
327 if (charge.chargingPower > maxPower) {
328 maxPower = charge.chargingPower;
329 }
330 if (charge.WCharged < minCharge) {
331 minCharge = charge.WCharged;
332 }
333 if (charge.WCharged > maxCharge) {
334 maxCharge = charge.WCharged;
335 }
336 if (charge.chargingEfficiency < minEfficiency) {
337 minEfficiency = charge.chargingEfficiency;
338 }
339 if (charge.chargingEfficiency > maxEfficiency) {
340 maxEfficiency = charge.chargingEfficiency;
341 }
342 }
343 // actually write the data
346 output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
347 output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
349 output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
350 if (finished) {
352 }
355 output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
356 output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
357 output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
358 output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
359 output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
360 output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
361 output.closeTag();
362 }
363 }
364
365 // clear charging data of vehicles which terminated charging
366 for (auto vehID : terminatedChargers) {
367 myChargeValues.erase(vehID);
368 }
369}
370
371
372void
373MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
374 const Charge& first = chargeSteps[iStart];
376 out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
380 out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
381 for (int i = iStart; i < iEnd; i++) {
382 const Charge& c = chargeSteps[i];
385 // charge values
389 // charging values of charging station in this timestep
392 // battery status of vehicle
395 // close tag timestep
396 out.closeTag();
397 }
398 out.closeTag();
399}
400
401
402/****************************************************************************/
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:61
#define TS
Definition SUMOTime.h:42
@ 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:46
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)
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
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 & 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 & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
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