Line data Source code
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 : /****************************************************************************/
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 15240 : 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 15240 : SUMOTime chargeDelay, const std::string& chargeType, SUMOTime waitingTime) :
45 15240 : MSStoppingPlace(chargingStationID, SUMO_TAG_CHARGING_STATION, std::vector<std::string>(), lane, startPos, endPos, name),
46 30480 : myChargeInTransit(chargeInTransit), myChargeType(stringToChargeType(chargeType)), myTotalPowerCheckEvent(nullptr) {
47 15240 : 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 15240 : myNominalChargingPower = chargingPower;
51 15240 : myTotalChargingPower = totalPower;
52 : }
53 15240 : 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 15238 : myEfficiency = efficency;
57 : }
58 15240 : 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 15240 : myChargeDelay = chargeDelay;
62 : }
63 15240 : 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 15240 : myWaitingTime = waitingTime;
67 : }
68 15240 : if (getBeginLanePosition() > getEndLanePosition()) {
69 0 : WRITE_WARNING(TLF("ChargingStation with ID='%' doesn't have a valid position (% < %).", getID(), toString(getBeginLanePosition()), toString(getEndLanePosition())));
70 : }
71 15240 : }
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 30086 : MSChargingStation::~MSChargingStation() {
83 45279 : }
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 108372 : MSChargingStation::getParkingArea() const {
129 108372 : 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 2506 : if (MSNet::getInstance()->getVehicleControl().getVehicle(kv.first) == nullptr) {
182 17 : continue;
183 : }
184 : Charge& lastcharge = kv.second.back();
185 2489 : if (lastcharge.timeStep == currentTime) {
186 296 : sumReqWh += lastcharge.WCharged;
187 296 : thisStepCharges.push_back(&lastcharge);
188 : }
189 : }
190 1278 : if (thisStepCharges.size() < 2) {
191 1174 : return DELTA_T;
192 : }
193 104 : const double capWh = myTotalChargingPower * myEfficiency /*W*/ * TS /*s*/ / 3600.0; // convert to Wh
194 : #ifdef DEBUG_SIMSTEP
195 : std::cout << "checkTotalPower: CS="
196 : << this->myID << " currentTime=" << currentTime << " myTotalChargingPower=" << myTotalChargingPower;
197 : if (sumReqWh > capWh && sumReqWh > 0) {
198 : std::cout << " exceeded, needs rebalancing!";
199 : }
200 : std::cout << std::endl;
201 : #endif
202 104 : if (sumReqWh > capWh && sumReqWh > 0) {
203 100 : const double ratio = capWh / sumReqWh;
204 300 : for (auto* charge : thisStepCharges) {
205 200 : const double deliveredWh = charge->WCharged * ratio;
206 200 : const double excessWh = charge->WCharged - deliveredWh;
207 200 : charge->WCharged = deliveredWh;
208 :
209 : // inform also battery device
210 200 : MSDevice_Battery* battery = myChargedBatteries[charge->vehicleID];
211 200 : double abc = battery->getActualBatteryCapacity();
212 200 : battery->setActualBatteryCapacity(abc - excessWh);
213 200 : battery->setEnergyCharged(deliveredWh);
214 : }
215 : }
216 104 : return DELTA_T;
217 1278 : }
218 :
219 :
220 : bool
221 0 : MSChargingStation::vehicleIsInside(const double position) const {
222 0 : if ((position >= getBeginLanePosition()) && (position <= getEndLanePosition())) {
223 : return true;
224 : } else {
225 0 : return false;
226 : }
227 : }
228 :
229 :
230 : bool
231 0 : MSChargingStation::isCharging() const {
232 0 : return myChargingVehicle;
233 : }
234 :
235 :
236 : void
237 101336 : MSChargingStation::addChargeValueForOutput(double WCharged, MSDevice_Battery* battery) {
238 202672 : if (!OptionsCont::getOptions().isSet("chargingstations-output")) {
239 1727 : return;
240 : }
241 99609 : std::string status = "";
242 99609 : if (battery->getChargingStartTime() > myChargeDelay) {
243 98897 : if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
244 : status = "chargingStopped";
245 845 : } else if (myChargeInTransit) {
246 : status = "chargingInTransit";
247 : } else {
248 : status = "noCharging";
249 : }
250 : } else {
251 712 : if (myChargeInTransit) {
252 : status = "waitingChargeInTransit";
253 344 : } else if (battery->getHolder().getSpeed() < battery->getStoppingThreshold()) {
254 : status = "waitingChargeStopped";
255 : } else {
256 : status = "noWaitingCharge";
257 : }
258 : }
259 : // update total charge
260 99609 : myTotalCharge += WCharged;
261 : // create charge row and insert it in myChargeValues
262 : const std::string vehID = battery->getHolder().getID();
263 : if (myChargeValues.count(vehID) == 0) {
264 391 : myChargedVehicles.push_back(vehID);
265 391 : myChargedBatteries[vehID] = battery;
266 : }
267 99609 : Charge C(MSNet::getInstance()->getCurrentTimeStep(), vehID, battery->getHolder().getVehicleType().getID(),
268 : status, WCharged, battery->getActualBatteryCapacity(), battery->getMaximumBatteryCapacity(),
269 298827 : myNominalChargingPower, myEfficiency, myTotalCharge);
270 99609 : myChargeValues[vehID].push_back(C);
271 99609 : }
272 :
273 :
274 : void
275 448 : MSChargingStation::writeChargingStationOutput(OutputDevice& output) {
276 448 : int chargingSteps = 0;
277 731 : for (const auto& item : myChargeValues) {
278 283 : chargingSteps += (int)item.second.size();
279 : }
280 448 : output.openTag(SUMO_TAG_CHARGING_STATION);
281 448 : output.writeAttr(SUMO_ATTR_ID, myID);
282 448 : output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED, myTotalCharge);
283 448 : output.writeAttr(SUMO_ATTR_CHARGINGSTEPS, chargingSteps);
284 : // start writing
285 448 : if (myChargeValues.size() > 0) {
286 431 : for (const std::string& vehID : myChargedVehicles) {
287 : int iStart = 0;
288 283 : const auto& chargeSteps = myChargeValues[vehID];
289 566 : while (iStart < (int)chargeSteps.size()) {
290 283 : int iEnd = iStart + 1;
291 283 : double charged = chargeSteps[iStart].WCharged;
292 94591 : while (iEnd < (int)chargeSteps.size() && chargeSteps[iEnd].timeStep == chargeSteps[iEnd - 1].timeStep + DELTA_T) {
293 94308 : charged += chargeSteps[iEnd].WCharged;
294 94308 : iEnd++;
295 : }
296 283 : writeVehicle(output, chargeSteps, iStart, iEnd, charged);
297 : iStart = iEnd;
298 : }
299 : }
300 : }
301 : // close charging station tag
302 448 : output.closeTag();
303 448 : }
304 :
305 :
306 : void
307 52074 : MSChargingStation::writeAggregatedChargingStationOutput(OutputDevice& output, bool includeUnfinished) {
308 : std::vector<std::string> terminatedChargers;
309 57304 : for (const auto& item : myChargeValues) {
310 : const Charge& lastCharge = item.second.back();
311 : // no charge during the last time step == has stopped charging
312 5230 : bool finished = lastCharge.timeStep < SIMSTEP - DELTA_T;
313 5230 : if (finished || includeUnfinished) {
314 108 : if (finished) {
315 104 : terminatedChargers.push_back(item.first);
316 : }
317 : // aggregate values
318 108 : double charged = 0.;
319 108 : double minPower = lastCharge.chargingPower;
320 108 : double maxPower = lastCharge.chargingPower;
321 108 : double minCharge = lastCharge.WCharged;
322 108 : double maxCharge = lastCharge.WCharged;
323 108 : double minEfficiency = lastCharge.chargingEfficiency;
324 108 : double maxEfficiency = lastCharge.chargingEfficiency;
325 5126 : for (const auto& charge : item.second) {
326 5018 : charged += charge.WCharged;
327 5018 : if (charge.chargingPower < minPower) {
328 0 : minPower = charge.chargingPower;
329 : }
330 5018 : if (charge.chargingPower > maxPower) {
331 0 : maxPower = charge.chargingPower;
332 : }
333 5018 : if (charge.WCharged < minCharge) {
334 32 : minCharge = charge.WCharged;
335 : }
336 5018 : if (charge.WCharged > maxCharge) {
337 4 : maxCharge = charge.WCharged;
338 : }
339 5018 : if (charge.chargingEfficiency < minEfficiency) {
340 0 : minEfficiency = charge.chargingEfficiency;
341 : }
342 5018 : if (charge.chargingEfficiency > maxEfficiency) {
343 0 : maxEfficiency = charge.chargingEfficiency;
344 : }
345 : }
346 : // actually write the data
347 108 : output.openTag(SUMO_TAG_CHARGING_EVENT);
348 108 : output.writeAttr(SUMO_ATTR_CHARGINGSTATIONID, myID);
349 108 : output.writeAttr(SUMO_ATTR_VEHICLE, lastCharge.vehicleID);
350 108 : output.writeAttr(SUMO_ATTR_TYPE, lastCharge.vehicleType);
351 108 : output.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
352 108 : output.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(item.second.at(0).timeStep));
353 108 : if (finished) {
354 208 : output.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(lastCharge.timeStep));
355 : }
356 108 : output.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, lastCharge.actualBatteryCapacity);
357 108 : output.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, lastCharge.maxBatteryCapacity);
358 108 : output.writeAttr(SUMO_ATTR_MINPOWER, minPower);
359 108 : output.writeAttr(SUMO_ATTR_MAXPOWER, maxPower);
360 108 : output.writeAttr(SUMO_ATTR_MINCHARGE, minCharge);
361 108 : output.writeAttr(SUMO_ATTR_MAXCHARGE, maxCharge);
362 108 : output.writeAttr(SUMO_ATTR_MINEFFICIENCY, minEfficiency);
363 108 : output.writeAttr(SUMO_ATTR_MAXEFFICIENCY, maxEfficiency);
364 216 : output.closeTag();
365 : }
366 : }
367 :
368 : // clear charging data of vehicles which terminated charging
369 52178 : for (auto vehID : terminatedChargers) {
370 : myChargeValues.erase(vehID);
371 : }
372 52074 : }
373 :
374 :
375 : void
376 283 : MSChargingStation::writeVehicle(OutputDevice& out, const std::vector<Charge>& chargeSteps, int iStart, int iEnd, double charged) {
377 283 : const Charge& first = chargeSteps[iStart];
378 283 : out.openTag(SUMO_TAG_VEHICLE);
379 283 : out.writeAttr(SUMO_ATTR_ID, first.vehicleID);
380 283 : out.writeAttr(SUMO_ATTR_TYPE, first.vehicleType);
381 283 : out.writeAttr(SUMO_ATTR_TOTALENERGYCHARGED_VEHICLE, charged);
382 283 : out.writeAttr(SUMO_ATTR_CHARGINGBEGIN, time2string(first.timeStep));
383 283 : out.writeAttr(SUMO_ATTR_CHARGINGEND, time2string(chargeSteps[iEnd - 1].timeStep));
384 94874 : for (int i = iStart; i < iEnd; i++) {
385 94591 : const Charge& c = chargeSteps[i];
386 94591 : out.openTag(SUMO_TAG_STEP);
387 94591 : out.writeAttr(SUMO_ATTR_TIME, time2string(c.timeStep));
388 : // charge values
389 94591 : out.writeAttr(SUMO_ATTR_CHARGING_STATUS, c.status);
390 94591 : out.writeAttr(SUMO_ATTR_ENERGYCHARGED, c.WCharged);
391 94591 : out.writeAttr(SUMO_ATTR_PARTIALCHARGE, c.totalEnergyCharged);
392 : // charging values of charging station in this timestep
393 94591 : out.writeAttr(SUMO_ATTR_CHARGINGPOWER, c.chargingPower);
394 94591 : out.writeAttr(SUMO_ATTR_EFFICIENCY, c.chargingEfficiency);
395 : // battery status of vehicle
396 94591 : out.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, c.actualBatteryCapacity);
397 94591 : out.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, c.maxBatteryCapacity);
398 : // close tag timestep
399 189182 : out.closeTag();
400 : }
401 283 : out.closeTag();
402 283 : }
403 :
404 :
405 : /****************************************************************************/
|