Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: order of drt discharge events #2926

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

@michalmac michalmac Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be replaced with:
this.addQSimComponentBinding(EvModule.EV_COMPONENT).to(DriveDischargingHandler.class);

Right now I doubt there is any event processed. Maybe this is why so may tests failed.

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this change will definitely help with the order of events (which is good!), I was thinking about updating the SOC in the main thread (outside the event handling threads) to avoid race conditions (and also to have events deterministically ordered).

I will create a PR for moving the whole logic outside the event handling.

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();
}
}
Loading