Skip to content

Commit

Permalink
fix: order of drt discharge events
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Nov 10, 2023
1 parent a39662a commit af0fa9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Vehicle> vehicleId;
private final ElectricVehicle ev;
Expand All @@ -66,6 +72,7 @@ private boolean isOnFirstLink() {
private final EventsManager eventsManager;
private final Map<Id<Vehicle>, ? extends ElectricVehicle> eVehicles;
private final Map<Id<Vehicle>, EvDrive> evDrives;
private final List<Event> eventQueue = new LinkedList<>();

@Inject
DriveDischargingHandler(ElectricFleet data, Network network, EventsManager eventsManager) {
Expand Down Expand Up @@ -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<Vehicle> vehicleId, Id<Link> linkId, double eventTime) {
EvDrive evDrive = evDrives.get(vehicleId);
if (evDrive != null && !evDrive.isOnFirstLink()) {// handle only our EVs, except for the first link
Expand All @@ -115,9 +123,15 @@ private EvDrive dischargeVehicle(Id<Vehicle> vehicleId, Id<Link> 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();
}
}

0 comments on commit af0fa9f

Please sign in to comment.