Skip to content

Commit

Permalink
Merge pull request #93 from matsim-scenarios/hbefa_road_type_mapping
Browse files Browse the repository at this point in the history
The Emission config has central infrastructure for HBEFA Road tags.
  • Loading branch information
Janek Laudan authored Mar 16, 2022
2 parents 9c514c4 + f426ebf commit 2f997f9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 166 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!--<matsim.version>13.0</matsim.version>-->

<!-- PR-labelled release -->
<matsim.version>14.0-PR1884</matsim.version>
<matsim.version>14.0-PR1859</matsim.version>

<!-- snapshot == not recommended: rather use PR-labelled release!-->
<!--<matsim.version>14.0-SNAPSHOT</matsim.version>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.emissions.EmissionModule;
import org.matsim.contrib.emissions.HbefaVehicleCategory;
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.contrib.emissions.VspHbefaRoadTypeMapping;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.DetailedVsAverageLookupBehavior;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.HbefaRoadTypeSource;
Expand Down Expand Up @@ -121,7 +124,6 @@ void run() throws IOException {
eConfig.setDetailedVsAverageLookupBehavior(DetailedVsAverageLookupBehavior.tryDetailedThenTechnologyAverageElseAbort);
eConfig.setDetailedColdEmissionFactorsFile(hbefaColdFile);
eConfig.setDetailedWarmEmissionFactorsFile(hbefaWarmFile);
eConfig.setHbefaRoadTypeSource(HbefaRoadTypeSource.fromLinkAttributes);
eConfig.setNonScenarioVehicles(NonScenarioVehicles.ignore);
eConfig.setWritingEmissionsEvents(true);

Expand All @@ -138,54 +140,7 @@ void run() throws IOException {
Scenario scenario = ScenarioUtils.loadScenario(config);

// network
for (Link link : scenario.getNetwork().getLinks().values()) {

double freespeed = Double.NaN;

if (link.getFreespeed() <= 13.888889) {
freespeed = link.getFreespeed() * 2;
// for non motorway roads, the free speed level was reduced
} else {
freespeed = link.getFreespeed();
// for motorways, the original speed levels seems ok.
}

if(freespeed <= 8.333333333){ //30kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/30");
} else if(freespeed <= 11.111111111){ //40kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/40");
} else if(freespeed <= 13.888888889){ //50kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/50");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Distr/50");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/50");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 16.666666667){ //60kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/60");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/60");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/60");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 19.444444444){ //70kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/70");
} else if(freespeed <= 22.222222222){ //80kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-Nat./80");
} else if(freespeed > 22.222222222){ //faster
link.getAttributes().putAttribute("hbefa_road_type", "RUR/MW/>130");
} else{
throw new RuntimeException("Link not considered...");
}
}
new VspHbefaRoadTypeMapping().addHbefaMappings(scenario.getNetwork());

// car vehicles

Expand Down Expand Up @@ -266,30 +221,19 @@ void run() throws IOException {
EngineInformation engineInformation = type.getEngineInformation();
VehicleUtils.setHbefaVehicleCategory( engineInformation, HbefaVehicleCategory.NON_HBEFA_VEHICLE.toString());
}

List<Id<Vehicle>> vehiclesToChangeToElectric = new ArrayList<>();

List<Id<Vehicle>> carVehiclesToChangeToSpecificType = new ArrayList<>();

final Random rnd = MatsimRandom.getLocalInstance();

int totalVehiclesCounter = 0;
// randomly change some vehicle types
for (Vehicle vehicle : scenario.getVehicles().getVehicles().values()) {
totalVehiclesCounter++;
if (vehicle.getId().toString().contains("freight")) {
// some freight vehicles have the type "car", skip them...

} else if (vehicle.getType().getId().toString().equals(defaultCarVehicleType.getId().toString())) {

carVehiclesToChangeToSpecificType.add(vehicle.getId());

if (rnd.nextDouble() < shareOfPrivateVehiclesChangedToElectric) {
vehiclesToChangeToElectric.add(vehicle.getId());
}
} else {
// ignore all other vehicles
}
}
List<Id<Vehicle>> vehiclesToChangeToElectric = scenario.getVehicles().getVehicles().values().stream()
.filter(vehicle -> vehicle.getType().getId().equals(defaultCarVehicleType.getId()))
.filter(vehicle -> !vehicle.getId().toString().contains("freight"))// some freight vehicles have the type "car", skip them...
.filter(vehicle -> rnd.nextDouble() < shareOfPrivateVehiclesChangedToElectric)
.map(Identifiable::getId)
.collect(Collectors.toList());

final double petrolShare = 0.512744724750519;
final double dieselShare = 0.462841421365738;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.matsim.contrib.emissions.EmissionModule;
import org.matsim.contrib.emissions.HbefaVehicleCategory;
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.contrib.emissions.VspHbefaRoadTypeMapping;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.DetailedVsAverageLookupBehavior;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.HbefaRoadTypeSource;
Expand Down Expand Up @@ -134,7 +135,6 @@ void run() throws IOException {
eConfig.setDetailedVsAverageLookupBehavior(DetailedVsAverageLookupBehavior.tryDetailedThenTechnologyAverageElseAbort);
eConfig.setDetailedColdEmissionFactorsFile(hbefaColdFile);
eConfig.setDetailedWarmEmissionFactorsFile(hbefaWarmFile);
eConfig.setHbefaRoadTypeSource(HbefaRoadTypeSource.fromLinkAttributes);
eConfig.setNonScenarioVehicles(NonScenarioVehicles.ignore);
eConfig.setWritingEmissionsEvents(true);

Expand All @@ -151,54 +151,7 @@ void run() throws IOException {
Scenario scenario = ScenarioUtils.loadScenario(config);

// network
for (Link link : scenario.getNetwork().getLinks().values()) {

double freespeed = Double.NaN;

if (link.getFreespeed() <= 13.888889) {
freespeed = link.getFreespeed() * 2;
// for non motorway roads, the free speed level was reduced
} else {
freespeed = link.getFreespeed();
// for motorways, the original speed levels seems ok.
}

if(freespeed <= 8.333333333){ //30kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/30");
} else if(freespeed <= 11.111111111){ //40kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/40");
} else if(freespeed <= 13.888888889){ //50kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/50");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Distr/50");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/50");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 16.666666667){ //60kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/60");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/60");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/60");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 19.444444444){ //70kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/70");
} else if(freespeed <= 22.222222222){ //80kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-Nat./80");
} else if(freespeed > 22.222222222){ //faster
link.getAttributes().putAttribute("hbefa_road_type", "RUR/MW/>130");
} else{
throw new RuntimeException("Link not considered...");
}
}
new VspHbefaRoadTypeMapping().addHbefaMappings(scenario.getNetwork());

// car vehicles

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.emissions.EmissionModule;
import org.matsim.contrib.emissions.HbefaVehicleCategory;
import org.matsim.contrib.emissions.VspHbefaRoadTypeMapping;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.DetailedVsAverageLookupBehavior;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup.HbefaRoadTypeSource;
Expand Down Expand Up @@ -105,7 +106,6 @@ void run() {
eConfig.setDetailedVsAverageLookupBehavior(DetailedVsAverageLookupBehavior.directlyTryAverageTable);
eConfig.setAverageColdEmissionFactorsFile(this.hbefaColdFile);
eConfig.setAverageWarmEmissionFactorsFile(this.hbefaWarmFile);
eConfig.setHbefaRoadTypeSource(HbefaRoadTypeSource.fromLinkAttributes);
eConfig.setNonScenarioVehicles(NonScenarioVehicles.ignore);

final String emissionEventOutputFile = analysisOutputDirectory + runId + ".emission.events.offline.xml.gz";
Expand All @@ -114,54 +114,7 @@ void run() {
Scenario scenario = ScenarioUtils.loadScenario(config);

// network
for (Link link : scenario.getNetwork().getLinks().values()) {

double freespeed = Double.NaN;

if (link.getFreespeed() <= 13.888889) {
freespeed = link.getFreespeed() * 2;
// for non motorway roads, the free speed level was reduced
} else {
freespeed = link.getFreespeed();
// for motorways, the original speed levels seems ok.
}

if(freespeed <= 8.333333333){ //30kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/30");
} else if(freespeed <= 11.111111111){ //40kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/Access/40");
} else if(freespeed <= 13.888888889){ //50kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/50");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Distr/50");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/50");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 16.666666667){ //60kmh
double lanes = link.getNumberOfLanes();
if(lanes <= 1.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Local/60");
} else if(lanes <= 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/Trunk-City/60");
} else if(lanes > 2.0){
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/60");
} else{
throw new RuntimeException("NoOfLanes not properly defined");
}
} else if(freespeed <= 19.444444444){ //70kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-City/70");
} else if(freespeed <= 22.222222222){ //80kmh
link.getAttributes().putAttribute("hbefa_road_type", "URB/MW-Nat./80");
} else if(freespeed > 22.222222222){ //faster
link.getAttributes().putAttribute("hbefa_road_type", "RUR/MW/>130");
} else{
throw new RuntimeException("Link not considered...");
}
}
new VspHbefaRoadTypeMapping().addHbefaMappings(scenario.getNetwork());

// vehicles

Expand Down

0 comments on commit 2f997f9

Please sign in to comment.