From af0fa9f8a08783e18de7cae7ba80e49daab83244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6rl?= Date: Fri, 10 Nov 2023 22:22:46 +0100 Subject: [PATCH] fix: order of drt discharge events --- .../ev/discharging/DischargingModule.java | 1 + .../discharging/DriveDischargingHandler.java | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DischargingModule.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DischargingModule.java index 96078d1eab1..da402c58ef8 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DischargingModule.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DischargingModule.java @@ -41,6 +41,7 @@ public void install() { protected void configureQSim() { this.bind(DriveDischargingHandler.class).in( Singleton.class ); addMobsimScopeEventHandlerBinding().to(DriveDischargingHandler.class); + addMobsimListenerBinding().to(DriveDischargingHandler.class); // event handlers are not qsim components this.bind(IdleDischargingHandler.class).in( Singleton.class ); diff --git a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java index ca8508a0e74..74e4c9d29f1 100644 --- a/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java +++ b/contribs/ev/src/main/java/org/matsim/contrib/ev/discharging/DriveDischargingHandler.java @@ -20,9 +20,12 @@ package org.matsim.contrib.ev.discharging; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.events.Event; import org.matsim.api.core.v01.events.LinkLeaveEvent; import org.matsim.api.core.v01.events.VehicleEntersTrafficEvent; import org.matsim.api.core.v01.events.VehicleLeavesTrafficEvent; @@ -35,6 +38,8 @@ import org.matsim.contrib.ev.fleet.ElectricVehicle; import org.matsim.core.api.experimental.events.EventsManager; import org.matsim.core.events.MobsimScopeEventHandler; +import org.matsim.core.mobsim.framework.events.MobsimAfterSimStepEvent; +import org.matsim.core.mobsim.framework.listeners.MobsimAfterSimStepListener; import org.matsim.vehicles.Vehicle; import com.google.inject.Inject; @@ -45,7 +50,8 @@ * idle discharge process (see {@link IdleDischargingHandler}). */ public final class DriveDischargingHandler - implements LinkLeaveEventHandler, VehicleEntersTrafficEventHandler, VehicleLeavesTrafficEventHandler, MobsimScopeEventHandler { + implements LinkLeaveEventHandler, VehicleEntersTrafficEventHandler, VehicleLeavesTrafficEventHandler, MobsimScopeEventHandler, + MobsimAfterSimStepListener { private static class EvDrive { private final Id vehicleId; private final ElectricVehicle ev; @@ -66,6 +72,7 @@ private boolean isOnFirstLink() { private final EventsManager eventsManager; private final Map, ? extends ElectricVehicle> eVehicles; private final Map, EvDrive> evDrives; + private final List eventQueue = new LinkedList<>(); @Inject DriveDischargingHandler(ElectricFleet data, Network network, EventsManager eventsManager) { @@ -104,6 +111,7 @@ public void handleEvent(VehicleLeavesTrafficEvent event) { // (for instance, AUX discharging and battery charging modifies charge outside event handling // (as MobsimAfterSimStepListeners) //TODO In the long term, it will be safer to move the discharging procedure to a MobsimAfterSimStepListener + // -> has been implemented now as a hack /sebhoerl private EvDrive dischargeVehicle(Id vehicleId, Id linkId, double eventTime) { EvDrive evDrive = evDrives.get(vehicleId); if (evDrive != null && !evDrive.isOnFirstLink()) {// handle only our EVs, except for the first link @@ -115,9 +123,15 @@ private EvDrive dischargeVehicle(Id vehicleId, Id linkId, double //Energy consumption may be negative on links with negative slope ev.getBattery() .dischargeEnergy(energy, - missingEnergy -> eventsManager.processEvent(new MissingEnergyEvent(eventTime, ev.getId(), link.getId(), missingEnergy))); - eventsManager.processEvent(new DrivingEnergyConsumptionEvent(eventTime, vehicleId, linkId, energy, ev.getBattery().getCharge())); + missingEnergy -> eventQueue.add(new MissingEnergyEvent(eventTime, ev.getId(), link.getId(), missingEnergy))); + eventQueue.add(new DrivingEnergyConsumptionEvent(eventTime, vehicleId, linkId, energy, ev.getBattery().getCharge())); } return evDrive; } + + @Override + public void notifyMobsimAfterSimStep(MobsimAfterSimStepEvent e) { + eventQueue.forEach(eventsManager::processEvent); + eventQueue.clear(); + } }