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