Skip to content

Commit

Permalink
use EmissionsByPollutant, back to use Id<Vehicle> as key
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Mar 4, 2024
1 parent 35f6445 commit 4f05a42
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,27 @@

package org.matsim.vsp.emissions;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.analysis.time.TimeBinMap;
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.contrib.emissions.analysis.EmissionsByPollutant;
import org.matsim.contrib.emissions.events.ColdEmissionEvent;
import org.matsim.contrib.emissions.events.ColdEmissionEventHandler;
import org.matsim.contrib.emissions.events.WarmEmissionEvent;
import org.matsim.contrib.emissions.events.WarmEmissionEventHandler;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;

/**
* Collects Warm- and Cold-Emission-Events and returns them either
* by time bin and link-id, or only by link-id.
*/
public class EmissionsPerVehicleEventHandler implements WarmEmissionEventHandler, ColdEmissionEventHandler {

private static final String FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1 =
"freight_rewe_VERBRAUCHERMARKT_TROCKEN_veh_medium18t_electro_160444_1";
//TODO: Use EmissionByPollutant?
private final Map<String, Map<Pollutant, Double>> vehicle2pollutants = new HashMap<>();
private static final Id<Vehicle> FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1 =
Id.createVehicleId("freight_rewe_VERBRAUCHERMARKT_TROCKEN_veh_medium18t_electro_160444_1");

private final Map<Id<Vehicle>, EmissionsByPollutant> vehicle2pollutants = new LinkedHashMap<>();
// private final Map<Id<VehicleType>, Map<Pollutant, Double>> vehicleType2pollutants = new HashMap<>();

private Double tempValue = 0.;
Expand All @@ -61,9 +58,9 @@ public EmissionsPerVehicleEventHandler() {}
*
* @return Total emissions per pollutant by vehicle id
*/
public Map<String, Map<Pollutant, Double>> getVehicle2pollutants() {
public Map<Id<Vehicle>, EmissionsByPollutant> getVehicle2pollutants() {
System.out.println("#### Vehicle2Pollutant ABRUF: " +vehicle2pollutants.get(
FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).toString() );
FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).getEmissions().toString() );
return vehicle2pollutants; }

/**
Expand All @@ -81,31 +78,34 @@ public void reset(int iteration) {

@Override
public void handleEvent(WarmEmissionEvent event) {
handleEmissionEvent(event.getTime(), event.getVehicleId(), event.getWarmEmissions());
handleEmissionEvent(event.getVehicleId(), event.getWarmEmissions());
}

@Override
public void handleEvent(ColdEmissionEvent event) {
handleEmissionEvent(event.getTime(), event.getVehicleId(), event.getColdEmissions());
handleEmissionEvent(event.getVehicleId(), event.getColdEmissions());
}

private void handleEmissionEvent(double time, Id<Vehicle> vehicleId, Map<Pollutant, Double> emissions) {
private void handleEmissionEvent(Id<Vehicle> vehicleId, Map<Pollutant, Double> emissions) {

EmissionsByPollutant emissionsByPollutant = new EmissionsByPollutant(emissions);

//Sum up ver VehicleId
if (vehicle2pollutants.get(vehicleId.toString()) == null) { vehicle2pollutants.put(vehicleId.toString(), emissions); }
if (vehicle2pollutants.get(vehicleId) == null) {
vehicle2pollutants.put(vehicleId, emissionsByPollutant); }
else {
for (Pollutant key : emissions.keySet()) {
vehicle2pollutants.get(vehicleId.toString()).merge(key, emissions.get(key), Double::sum);
}
// for (Pollutant key : emissions.keySet()) {
vehicle2pollutants.get(vehicleId).addEmissions(emissions);
// }
}
// if (vehicleId.toString().equals(FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1)){
// System.out.println("### vehId: " + vehicleId + "; emissions: "+ emissions.toString());
// System.out.println("### vehicle2Pollutants " + vehicle2pollutants.get(vehicleId.toString()).toString());
// }

if ( vehicle2pollutants.get(FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).get(Pollutant.CO).doubleValue() != tempValue.doubleValue()){
System.out.println("JETZT wurde was modifiziert: " + time + "vehid: " + vehicleId + "; emissions: "+ emissions.toString());
tempValue = vehicle2pollutants.get(FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).get(Pollutant.CO);
}
// if ( vehicle2pollutants.get(FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).get(Pollutant.CO).doubleValue() != tempValue.doubleValue()){
// System.out.println("JETZT wurde was modifiziert: " + time + "vehid: " + vehicleId + "; emissions: "+ emissions.toString());
// tempValue = vehicle2pollutants.get(FREIGHT_REWE_VERBRAUCHERMARKT_TROCKEN_VEH_MEDIUM_18_T_ELECTRO_160444_1).get(Pollutant.CO);
// }
}
}
39 changes: 19 additions & 20 deletions src/main/java/org/matsim/vsp/emissions/EmissionsWriterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
Expand All @@ -14,6 +15,7 @@
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.contrib.emissions.analysis.EmissionsByPollutant;
import org.matsim.contrib.emissions.analysis.EmissionsOnLinkEventHandler;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;
Expand All @@ -26,6 +28,7 @@ public class EmissionsWriterUtils {

private static final Logger log = LogManager.getLogger(EmissionsWriterUtils.class);
private static final String DELIMITER = ";";
private static final int NU_DIGITS = 5;

/**
* Schreibt CSV Dateien raus mit Emission pro LINK - einmal als Summe für den Link -> kann genutzt
Expand All @@ -45,7 +48,7 @@ static void writePerLinkOutput(String linkEmissionAnalysisFile,
log.info("Writing output per LINK...");

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(4);
nf.setMaximumFractionDigits(NU_DIGITS);
nf.setGroupingUsed(false);

{
Expand Down Expand Up @@ -119,7 +122,7 @@ static void writePerVehicleOutput(String vehicleEmissionsAnalysisFile,
log.info("Writing output per VEHICLE (TYPE)...");

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(4);
nf.setMaximumFractionDigits(NU_DIGITS);
nf.setGroupingUsed(false);

{
Expand All @@ -132,9 +135,9 @@ static void writePerVehicleOutput(String vehicleEmissionsAnalysisFile,
vehicleWriter.write("vehicleId" + DELIMITER + "vehicleTypeId");
vehicleTypeWriter.write("vehicleTypeId");

Map<String, Map<Pollutant, Double>> vehicle2pollutants = emissionsEventHandler.getVehicle2pollutants();
log.warn("#### Vehicle2 Pollutant vor dem schreiben: " +vehicle2pollutants.get("freight_rewe_VERBRAUCHERMARKT_TROCKEN_veh_medium18t_electro_160444_1").toString());
Map<Id<VehicleType>, Map<Pollutant, Double>> vehicleType2pollutants = new HashMap<>();
Map<Id<Vehicle>, EmissionsByPollutant> vehicle2pollutants = emissionsEventHandler.getVehicle2pollutants();
log.warn("#### Vehicle2 Pollutant vor dem schreiben: " +vehicle2pollutants.get(Id.createVehicleId("freight_rewe_VERBRAUCHERMARKT_TROCKEN_veh_medium18t_electro_160444_1")).getEmissions().toString());
Map<Id<VehicleType>, EmissionsByPollutant> vehicleType2pollutants = new LinkedHashMap<>();

for (Pollutant pollutant : Pollutant.values()) {
vehicleWriter.write(DELIMITER + pollutant);
Expand All @@ -144,46 +147,42 @@ static void writePerVehicleOutput(String vehicleEmissionsAnalysisFile,
vehicleWriter.newLine();
vehicleTypeWriter.newLine();

for (String vehicleId : vehicle2pollutants.keySet()) {
for (Id<Vehicle> vehicleId : vehicle2pollutants.keySet()) {
final Id<VehicleType> vehicleTypeId = VehicleUtils.findVehicle(
Id.createVehicleId(vehicleId), scenario)
vehicleId, scenario)
.getType().getId();

vehicleWriter.write(vehicleId.toString());
vehicleWriter.write(DELIMITER + vehicleTypeId.toString());

for (Pollutant pollutant : Pollutant.values()) {
double emissionValue = 0.;
if (vehicle2pollutants.get(vehicleId).get(pollutant) != null) {
emissionValue = vehicle2pollutants.get(vehicleId).get(pollutant);
}
// if (vehicle2pollutants.get(vehicleId).getEmission(pollutant) != null) {
emissionValue = vehicle2pollutants.get(vehicleId).getEmission(pollutant);
// }
vehicleWriter.write(DELIMITER + nf.format(emissionValue));
}
vehicleWriter.newLine();

{
//Sum up per VehicleType
EmissionsByPollutant emissionsByPollutant = vehicle2pollutants.get(vehicleId);
if (vehicleType2pollutants.get(vehicleTypeId) == null) {
vehicleType2pollutants.put(vehicleTypeId, vehicle2pollutants.get(vehicleId));
vehicleType2pollutants.put(vehicleTypeId, emissionsByPollutant);
} else {
var emissions = vehicle2pollutants.get(vehicleId);
for (Pollutant pollutant : emissions.keySet()) {
vehicleType2pollutants.get(vehicleTypeId)
.merge(pollutant, emissions.get(pollutant), Double::sum);
}
vehicleType2pollutants.get(vehicleTypeId).addEmissions(emissionsByPollutant.getEmissions());
}
}

}
// write it out per VehicleType.
for (Id<VehicleType> vehicleTypeId : vehicleType2pollutants.keySet()) {
vehicleTypeWriter.write(vehicleTypeId.toString());

for (Pollutant pollutant : Pollutant.values()) {
double emissionValue = 0.;
if (vehicleType2pollutants.get(vehicleTypeId).get(pollutant) != null) {
emissionValue = vehicleType2pollutants.get(vehicleTypeId).get(pollutant);
}
// if (vehicleType2pollutants.get(vehicleTypeId).getEmission(pollutant) != null) {
emissionValue = vehicleType2pollutants.get(vehicleTypeId).getEmission(pollutant);
// }
vehicleTypeWriter.write(DELIMITER + nf.format(emissionValue));
}
vehicleTypeWriter.newLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void run() throws IOException {
config.global().setCoordinateSystem("EPSG:31468");
// config.global().setCoordinateSystem("GK4");
config.plans().setInputFile(null);
config.eventsManager().setNumberOfThreads(null);
config.eventsManager().setNumberOfThreads(1);
config.eventsManager().setEstimatedNumberOfEvents(null);
// config.parallelEventHandling().setNumberOfThreads(null);
// config.parallelEventHandling().setEstimatedNumberOfEvents(null);
Expand Down Expand Up @@ -97,7 +97,7 @@ void run() throws IOException {
final String linkEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerLink.csv";
final String linkEmissionPerMAnalysisFile = analysisOutputDirectory + "/emissionsPerLinkPerM.csv";
final String vehicleTypeFile = analysisOutputDirectory + "/emissionVehicleInformation.csv";
final String vehicleEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerVerhicle.csv";
final String vehicleEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerVehicle.csv";
final String vehicleTypeEmissionAnalysisFile = analysisOutputDirectory + "/emissionsPerVehicleType.csv";

Scenario scenario = ScenarioUtils.loadScenario(config);
Expand Down

0 comments on commit 4f05a42

Please sign in to comment.