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-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>
32#include <microsim/MSNet.h>
33#include "MSChargingStation.h"
34
35
36// ===========================================================================
37// member method definitions
38// ===========================================================================
39
40MSChargingStation::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
76MSChargingStation::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
86
87
88double
90 if (usingFuel) {
91 return myChargingPower;
92 } else {
93 // Convert from [Ws] to [Wh] (3600s / 1h):
94 return myChargingPower / 3600;
95 }
96}
97
98
99double
103
104
105bool
109
110
115
116
117const std::string&
121
122
127
128
129const MSParkingArea*
133
134
135void
137 myChargingPower = chargingPower;
138}
139
140
141void
143 myEfficiency = efficiency;
144}
145
146
147void
151
152
153void
157
158
159void
163
164
165bool
166MSChargingStation::vehicleIsInside(const double position) const {
167 if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
168 return true;
169 } else {
170 return false;
171 }
172}
173
174
175bool
179
180
181void
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
218void
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
250void
252 std::vector<std::string> terminatedChargers;
253 for (const auto& item : myChargeValues) {
254 const Charge& lastCharge = item.second.back();
255 // no charge during the last time step == has stopped charging
256 bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
257 if (finished || includeUnfinished) {
258 if (finished) {
259 terminatedChargers.push_back(item.first);
260 }
261 // aggregate values
262 double charged = 0.;
263 double minPower = lastCharge.chargingPower;
264 double maxPower = lastCharge.chargingPower;
265 double minCharge = lastCharge.WCharged;
266 double maxCharge = lastCharge.WCharged;
267 double minEfficiency = lastCharge.chargingEfficiency;
268 double maxEfficiency = lastCharge.chargingEfficiency;
269 for (const auto& charge : item.second) {
270 charged += charge.WCharged;
271 if (charge.chargingPower < minPower) {
272 minPower = charge.chargingPower;
273 }
274 if (charge.chargingPower > maxPower) {
275 maxPower = charge.chargingPower;
276 }
277 if (charge.WCharged < minCharge) {
278 minCharge = charge.WCharged;
279 }
280 if (charge.WCharged > maxCharge) {
281 maxCharge = charge.WCharged;
282 }
283 if (charge.chargingEfficiency < minEfficiency) {
284 minEfficiency = charge.chargingEfficiency;
285 }
286 if (charge.chargingEfficiency > maxEfficiency) {
287 maxEfficiency = charge.chargingEfficiency;
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));
297 if (finished) {
299 }
302 output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
303 output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
304 output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
305 output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
306 output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
307 output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
308 output.closeTag();
309 }
310 }
311
312 // clear charging data of vehicles which terminated charging
313 for (auto vehID : terminatedChargers) {
314 myChargeValues.erase(vehID);
315 }
316}
317
318
319void
320MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
321 const Charge& first = chargeSteps[iStart];
323 out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
327 out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
328 for (int i = iStart; i < iEnd; i++) {
329 const Charge& c = chargeSteps[i];
332 // charge values
336 // charging values of charging station in this timestep
339 // battery status of vehicle
342 // close tag timestep
343 out.closeTag();
344 }
345 out.closeTag();
346}
347
348
349/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#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, 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.
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:185
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.
Definition json.hpp:4471
struct to save information for the chargingStation output