Line data Source code
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 : /****************************************************************************/
14 : /// @file MSChargingStation.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Tamas Kurczveil
17 : /// @author Pablo Alvarez Lopez
18 : /// @author Mirko Barthauer
19 : /// @date 20-12-13
20 : ///
21 : // Charging Station for Electric vehicles
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <cassert>
26 : #include <utils/common/StringUtils.h>
27 : #include <utils/common/WrappingCommand.h>
28 : #include <utils/vehicle/SUMOVehicle.h>
29 : #include <microsim/MSEventControl.h>
30 : #include <microsim/MSParkingArea.h>
31 : #include <microsim/MSVehicleType.h>
32 : #include <microsim/MSStoppingPlace.h>
33 : #include <microsim/devices/MSDevice_Battery.h>
34 : #include <microsim/MSNet.h>
35 : #include "MSChargingStation.h"
36 :
37 :
38 : // ===========================================================================
39 : // member method definitions
40 : // ===========================================================================
41 :
42 12231 : MSChargingStation::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 12231 : SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
45 12231 : MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
46 24462 : myChargeInTransit(chargeInTransit), myChargeType(stringToChargeType(chargeType)), myTotalPowerCheckEvent(nullptr) {
47 12231 : if (chargingPower < 0) {
48 0 : WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGINGPOWER), getID(), toString(chargingPower)))
49 : } else {
50 12231 : myNominalChargingPower = chargingPower;
51 12231 : myTotalChargingPower = totalPower;
52 : }
53 12231 : if (efficency < 0 || efficency > 1) {
54 6 : WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_EFFICIENCY), getID(), toString(efficency)))
55 : } else {
56 12229 : myEfficiency = efficency;
57 : }
58 12231 : if (chargeDelay < 0) {
59 0 : WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_CHARGEDELAY), getID(), toString(chargeDelay)))
60 : } else {
61 12231 : myChargeDelay = chargeDelay;
62 : }
63 12231 : if (waitingTime < 0) {
64 0 : WRITE_WARNING(TLF("Attribute % for chargingStation with ID='%' is invalid (%).", toString(SUMO_ATTR_WAITINGTIME), getID(), toString(waitingTime)))
65 : } else {
66 12231 : myWaitingTime = waitingTime;
67 : }
68 12231 : if (getBeginLanePosition() > getEndLanePosition()) {
69 0 : WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
70 : }
71 12231 : }
72 :
73 :
74 17 : MSChargingStation::MSChargingStation(const std::string& chargingStationID, const MSParkingArea* parkingArea, const std::string& name, double chargingPower,
75 17 : double totalPower, double efficency, bool chargeInTransit, SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
76 17 : MSChargingStation(chargingStationID, const_cast<MSLane&>(parkingArea->getLane()), parkingArea->getBeginLanePosition(), parkingArea->getEndLanePosition(),
77 34 : name, chargingPower, totalPower, efficency, chargeInTransit, chargeDelay, chargeType, waitingTime) {
78 17 : myParkingArea = parkingArea;
79 17 : }
80 :
81 :
82 24092 : MSChargingStation::~MSChargingStation() {
83 36288 : }
84 :
85 :
86 : double
87 102206 : MSChargingStation::getChargingPower(bool usingFuel) const {
88 102206 : if (usingFuel) {
89 2333 : return myNominalChargingPower;
90 : } else {
91 : // Convert from [Ws] to [Wh] (3600s / 1h):
92 99873 : return myNominalChargingPower / 3600;
93 : }
94 : }
95 :
96 :
97 : double
98 101761 : MSChargingStation::getEfficency() const {
99 101761 : return myEfficiency;
100 : }
101 :
102 :
103 : bool
104 104909 : MSChargingStation::getChargeInTransit() const {
105 104909 : return myChargeInTransit;
106 : }
107 :
108 :
109 : SUMOTime
110 101908 : MSChargingStation::getChargeDelay() const {
111 101908 : return myChargeDelay;
112 : }
113 :
114 :
115 : MSChargingStation::ChargeType
116 101840 : MSChargingStation::getChargeType() const {
117 101840 : return myChargeType;
118 : }
119 :
120 :
121 : SUMOTime
122 0 : MSChargingStation::getWaitingTime() const {
123 0 : return myWaitingTime;
124 : }
125 :
126 :
127 : const MSParkingArea*
128 109442 : MSChargingStation::getParkingArea() const {
129 109442 : return myParkingArea;
130 : }
131 :
132 :
133 : void
134 511 : MSChargingStation::setChargingPower(double chargingPower) {
135 511 : myNominalChargingPower = chargingPower;
136 511 : }
137 :
138 :
139 : void
140 511 : MSChargingStation::setEfficiency(double efficiency) {
141 511 : myEfficiency = efficiency;
142 511 : }
143 :
144 :
145 : void
146 10 : MSChargingStation::setChargeDelay(SUMOTime delay) {
147 10 : myChargeDelay = delay;
148 10 : }
149 :
150 :
151 : void
152 10 : MSChargingStation::setChargeInTransit(bool value) {
153 10 : myChargeInTransit = value;
154 10 : if (myTotalChargingPower > 0 && myChargeInTransit && myTotalPowerCheckEvent == nullptr) {
155 0 : myTotalPowerCheckEvent = new WrappingCommand<MSChargingStation>(this, &MSChargingStation::checkTotalPower);
156 0 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myTotalPowerCheckEvent);
157 : }
158 10 : }
159 :
160 :
161 : void
162 103636 : MSChargingStation::setChargingVehicle(bool value) {
163 103636 : myChargingVehicle = value;
164 103636 : if (myTotalChargingPower > 0 && myChargingVehicle && myTotalPowerCheckEvent == nullptr) {
165 8 : myTotalPowerCheckEvent = new WrappingCommand<MSChargingStation>(this, &MSChargingStation::checkTotalPower);
166 8 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myTotalPowerCheckEvent);
167 : }
168 103636 : }
169 :
170 :
171 : SUMOTime
172 1282 : MSChargingStation::checkTotalPower(SUMOTime currentTime) {
173 1282 : if (!myChargeInTransit && !myChargingVehicle) {
174 4 : myTotalPowerCheckEvent = nullptr;
175 : myChargedBatteries.clear();
176 4 : return 0;
177 : }
178 : double sumReqWh = 0;
179 : std::vector<Charge*> thisStepCharges;
180 3784 : for (auto& kv : myChargeValues) {
181 : // auto& vid = kv.first;
182 : Charge& lastcharge = kv.second.back();
183 2506 : if (lastcharge.timeStep == currentTime) {
184 296 : sumReqWh += lastcharge.WCharged;
185 296 : thisStepCharges.push_back(&lastcharge);
186 : }
187 : }
188 1278 : if (thisStepCharges.size() < 2) {
189 1174 : return DELTA_T;
190 : }
191 104 : 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 104 : if (sumReqWh > capWh && sumReqWh > 0) {
200 100 : const double ratio = capWh / sumReqWh;
201 300 : for (auto* charge : thisStepCharges) {
202 200 : const double deliveredWh = charge->WCharged * ratio;
203 200 : const double excessWh = charge->WCharged - deliveredWh;
204 200 : charge->WCharged = deliveredWh;
205 :
206 : // inform also battery device
207 200 : MSDevice_Battery* battery = myChargedBatteries[charge->vehicleID];
208 200 : double abc = battery->getActualBatteryCapacity();
209 200 : battery->setActualBatteryCapacity(abc - excessWh);
210 200 : battery->setEnergyCharged(deliveredWh);
211 : }
212 : }
213 104 : return DELTA_T;
214 1278 : }
215 :
216 :
217 : bool
218 0 : MSChargingStation::vehicleIsInside(const double position) const {
219 0 : if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
220 : return true;
221 : } else {
222 0 : return false;
223 : }
224 : }
225 :
226 :
227 : bool
228 0 : MSChargingStation::isCharging() const {
229 0 : return myChargingVehicle;
230 : }
231 :
232 :
233 : void
234 101336 : MSChargingStation::addChargeValueForOutput(double WCharged, MSDevice_Battery* battery) {
235 202672 : if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
236 1727 : return;
237 : }
238 99609 : std::string status = "";
239 99609 : if (battery->getChargingStartTime() > myChargeDelay) {
240 98897 : if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
241 : status = "chargingStopped";
242 845 : } else if (myChargeInTransit) {
243 : status = "chargingInTransit";
244 : } else {
245 : status = "noCharging";
246 : }
247 : } else {
248 712 : if (myChargeInTransit) {
249 : status = "waitingChargeInTransit";
250 344 : } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
251 : status = "waitingChargeStopped";
252 : } else {
253 : status = "noWaitingCharge";
254 : }
255 : }
256 : // update total charge
257 99609 : 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 391 : myChargedVehicles.push_back(vehID);
262 391 : myChargedBatteries[vehID] = battery;
263 : }
264 99609 : Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
265 : status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
266 298827 : myNominalChargingPower, myEfficiency, myTotalCharge);
267 99609 : myChargeValues[vehID].push_back(C);
268 99609 : }
269 :
270 :
271 : void
272 448 : MSChargingStation::writeChargingStationOutput(OutputDevice& output) {
273 448 : int chargingSteps = 0;
274 731 : for (const auto& item : myChargeValues) {
275 283 : chargingSteps += (int)item.second.size();
276 : }
277 448 : output.openTag(SUMO_TAG_CHARGING_STATION);
278 448 : output.writeAttr(SUMO_ATTR_ID, myID);
279 448 : output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED, myTotalCharge);
280 448 : output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
281 : // start writing
282 448 : if (myChargeValues.size() > 0) {
283 431 : for (const std::string& vehID : myChargedVehicles) {
284 : int iStart = 0;
285 283 : const auto& chargeSteps = myChargeValues[vehID];
286 566 : while (iStart < (int)chargeSteps.size()) {
287 283 : int iEnd = iStart + 1;
288 283 : double charged = chargeSteps[iStart].WCharged;
289 94591 : while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
290 94308 : charged += chargeSteps[iEnd].WCharged;
291 94308 : iEnd++;
292 : }
293 283 : writeVehicle(output, chargeSteps, iStart, iEnd, charged);
294 : iStart = iEnd;
295 : }
296 : }
297 : }
298 : // close charging station tag
299 448 : output.closeTag();
300 448 : }
301 :
302 :
303 : void
304 52074 : MSChargingStation::writeAggregatedChargingStationOutput(OutputDevice& output, bool includeUnfinished) {
305 : std::vector<std::string> terminatedChargers;
306 57304 : for (const auto& item : myChargeValues) {
307 : const Charge& lastCharge = item.second.back();
308 : // no charge during the last time step == has stopped charging
309 5230 : bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
310 5230 : if (finished || includeUnfinished) {
311 108 : if (finished) {
312 104 : terminatedChargers.push_back(item.first);
313 : }
314 : // aggregate values
315 108 : double charged = 0.;
316 108 : double minPower = lastCharge.chargingPower;
317 108 : double maxPower = lastCharge.chargingPower;
318 108 : double minCharge = lastCharge.WCharged;
319 108 : double maxCharge = lastCharge.WCharged;
320 108 : double minEfficiency = lastCharge.chargingEfficiency;
321 108 : double maxEfficiency = lastCharge.chargingEfficiency;
322 5126 : for (const auto& charge : item.second) {
323 5018 : charged += charge.WCharged;
324 5018 : if (charge.chargingPower < minPower) {
325 0 : minPower = charge.chargingPower;
326 : }
327 5018 : if (charge.chargingPower > maxPower) {
328 0 : maxPower = charge.chargingPower;
329 : }
330 5018 : if (charge.WCharged < minCharge) {
331 32 : minCharge = charge.WCharged;
332 : }
333 5018 : if (charge.WCharged > maxCharge) {
334 4 : maxCharge = charge.WCharged;
335 : }
336 5018 : if (charge.chargingEfficiency < minEfficiency) {
337 0 : minEfficiency = charge.chargingEfficiency;
338 : }
339 5018 : if (charge.chargingEfficiency > maxEfficiency) {
340 0 : maxEfficiency = charge.chargingEfficiency;
341 : }
342 : }
343 : // actually write the data
344 108 : output.openTag(SUMO_TAG_CHARGING_EVENT);
345 108 : output.writeAttr(SUMO_ATTR_CHARGINGSTATIONID, myID);
346 108 : output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
347 108 : output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
348 108 : output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
349 108 : output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
350 108 : if (finished) {
351 208 : output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(lastCharge.timeStep));
352 : }
353 108 : output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, lastCharge.actualBatteryCapacity);
354 108 : output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, lastCharge.maxBatteryCapacity);
355 108 : output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
356 108 : output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
357 108 : output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
358 108 : output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
359 108 : output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
360 108 : output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
361 216 : output.closeTag();
362 : }
363 : }
364 :
365 : // clear charging data of vehicles which terminated charging
366 52178 : for (auto vehID : terminatedChargers) {
367 : myChargeValues.erase(vehID);
368 : }
369 52074 : }
370 :
371 :
372 : void
373 283 : MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
374 283 : const Charge& first = chargeSteps[iStart];
375 283 : out.openTag(SUMO_TAG_VEHICLE);
376 283 : out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
377 283 : out.writeAttr(SUMO_ATTR_TYPE, first.vehicleType);
378 283 : out.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
379 283 : out.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(first.timeStep));
380 283 : out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
381 94874 : for (int i = iStart; i < iEnd; i++) {
382 94591 : const Charge& c = chargeSteps[i];
383 94591 : out.openTag(SUMO_TAG_STEP);
384 94591 : out.writeAttr(SUMO_ATTR_TIME, time2string(c.timeStep));
385 : // charge values
386 94591 : out.writeAttr(SUMO_ATTR_CHARGING_STATUS, c.status);
387 94591 : out.writeAttr(SUMO_ATTR_ENERGYCHARGED, c.WCharged);
388 94591 : out.writeAttr(SUMO_ATTR_PARTIALCHARGE, c.totalEnergyCharged);
389 : // charging values of charging station in this timestep
390 94591 : out.writeAttr(SUMO_ATTR_CHARGINGPOWER, c.chargingPower);
391 94591 : out.writeAttr(SUMO_ATTR_EFFICIENCY, c.chargingEfficiency);
392 : // battery status of vehicle
393 94591 : out.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, c.actualBatteryCapacity);
394 94591 : out.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, c.maxBatteryCapacity);
395 : // close tag timestep
396 189182 : out.closeTag();
397 : }
398 283 : out.closeTag();
399 283 : }
400 :
401 :
402 : /****************************************************************************/
|