Skip to content

Commit

Permalink
add emissions dashboard as option for post procession creation of das…
Browse files Browse the repository at this point in the history
…hboards
  • Loading branch information
simei94 committed Oct 16, 2024
1 parent 9367897 commit 5b46dba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
43 changes: 40 additions & 3 deletions src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.application.ApplicationUtils;
import org.matsim.application.MATSimAppCommand;
import org.matsim.application.options.ShpOptions;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.run.prepare.PrepareNetwork;
import org.matsim.run.scenarios.LausitzScenario;
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.SimWrapper;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.dashboard.EmissionsDashboard;
import org.matsim.simwrapper.dashboard.NoiseDashboard;
import org.matsim.simwrapper.dashboard.TripDashboard;
import org.matsim.vehicles.MatsimVehicleWriter;
import picocli.CommandLine;

import java.io.IOException;
Expand All @@ -57,6 +64,8 @@ public final class LausitzSimWrapperRunner implements MATSimAppCommand {
private boolean noise;
@CommandLine.Option(names = "--trips", defaultValue = "false", description = "create trips dashboard")
private boolean trips;
@CommandLine.Option(names = "--emissions", defaultValue = "false", description = "create emission dashboard")
private boolean emissions;


public LausitzSimWrapperRunner(){
Expand All @@ -66,15 +75,15 @@ public LausitzSimWrapperRunner(){
@Override
public Integer call() throws Exception {

if (!noise && !trips){
if (!noise && !trips && !emissions){
throw new IllegalArgumentException("you have not configured any dashboard to be created! Please use command line parameters!");
}

for (Path runDirectory : inputPaths) {
log.info("Running on {}", runDirectory);

Path configPath = ApplicationUtils.matchInput("config.xml", runDirectory);
Config config = ConfigUtils.loadConfig(configPath.toString());
String configPath = ApplicationUtils.matchInput("config.xml", runDirectory).toString();
Config config = ConfigUtils.loadConfig(configPath);
SimWrapper sw = SimWrapper.create(config);

SimWrapperConfigGroup simwrapperCfg = ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class);
Expand All @@ -99,6 +108,34 @@ public Integer call() throws Exception {
.withDistanceDistribution("lausitz_mode_share_distance_distribution.csv"));
}

if (emissions) {
sw.addDashboard(Dashboard.customize(new EmissionsDashboard(config.global().getCoordinateSystem())).context("emissions"));

LausitzScenario.setEmissionsConfigs(config);
ConfigUtils.writeConfig(config, configPath);

Config dummyConfig = new Config();

String networkPath = ApplicationUtils.matchInput("output_network.xml.gz", runDirectory).toString();
String vehiclesPath = ApplicationUtils.matchInput("output_vehicles.xml.gz", runDirectory).toString();
String transitVehiclesPath = ApplicationUtils.matchInput("output_transitVehicles.xml.gz", runDirectory).toString();

dummyConfig.network().setInputFile(networkPath);
dummyConfig.vehicles().setVehiclesFile(vehiclesPath);
dummyConfig.transit().setVehiclesFile(transitVehiclesPath);

Scenario scenario = ScenarioUtils.loadScenario(dummyConfig);

// adapt network and veh types for emissions analysis like in LausitzScenario base run class
PrepareNetwork.prepareEmissionsAttributes(scenario.getNetwork());
LausitzScenario.prepareVehicleTypesForEmissionAnalysis(scenario);

// overwrite outputs with adapted files
NetworkUtils.writeNetwork(scenario.getNetwork(), networkPath);
new MatsimVehicleWriter(scenario.getVehicles()).writeFile(vehiclesPath);
new MatsimVehicleWriter(scenario.getTransitVehicles()).writeFile(transitVehiclesPath);
}


try {
sw.generate(runDirectory, true);
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/org/matsim/run/scenarios/LausitzScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,7 @@ public Config prepareConfig(Config config) {

if (emissions == EmissionAnalysisHandling.PERFORM_EMISSIONS_ANALYSIS) {
// set hbefa input files for emission analysis
EmissionsConfigGroup eConfig = ConfigUtils.addOrGetModule(config, EmissionsConfigGroup.class);
eConfig.setDetailedColdEmissionFactorsFile(HBEFA_FILE_COLD_DETAILED);
eConfig.setDetailedWarmEmissionFactorsFile(HBEFA_FILE_WARM_DETAILED);
eConfig.setAverageColdEmissionFactorsFile(HBEFA_FILE_COLD_AVERAGE);
eConfig.setAverageWarmEmissionFactorsFile(HBEFA_FILE_WARM_AVERAGE);
eConfig.setHbefaTableConsistencyCheckingLevel(EmissionsConfigGroup.HbefaTableConsistencyCheckingLevel.consistent);
eConfig.setDetailedVsAverageLookupBehavior(EmissionsConfigGroup.DetailedVsAverageLookupBehavior.tryDetailedThenTechnologyAverageThenAverageTable);
setEmissionsConfigs(config);
}
return config;
}
Expand Down Expand Up @@ -259,7 +253,20 @@ public static void prepareCommercialTrafficConfig(Config config) {
}
}

private static void prepareVehicleTypesForEmissionAnalysis(Scenario scenario) {
public static void setEmissionsConfigs(Config config) {
EmissionsConfigGroup eConfig = ConfigUtils.addOrGetModule(config, EmissionsConfigGroup.class);
eConfig.setDetailedColdEmissionFactorsFile(HBEFA_FILE_COLD_DETAILED);
eConfig.setDetailedWarmEmissionFactorsFile(HBEFA_FILE_WARM_DETAILED);
eConfig.setAverageColdEmissionFactorsFile(HBEFA_FILE_COLD_AVERAGE);
eConfig.setAverageWarmEmissionFactorsFile(HBEFA_FILE_WARM_AVERAGE);
eConfig.setHbefaTableConsistencyCheckingLevel(EmissionsConfigGroup.HbefaTableConsistencyCheckingLevel.consistent);
eConfig.setDetailedVsAverageLookupBehavior(EmissionsConfigGroup.DetailedVsAverageLookupBehavior.tryDetailedThenTechnologyAverageThenAverageTable);
}

/**
* Prepare vehicle types with necessary HBEFA information for emission analysis.
*/
public static void prepareVehicleTypesForEmissionAnalysis(Scenario scenario) {
for (VehicleType type : scenario.getVehicles().getVehicleTypes().values()) {
EngineInformation engineInformation = type.getEngineInformation();

Expand Down

0 comments on commit 5b46dba

Please sign in to comment.