Skip to content

Commit

Permalink
bump to PR3466 + add test for noise analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Sep 10, 2024
1 parent 3f7c8d2 commit 743c398
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!--<version>14.0-PR1452</version>-->

<!-- snapshot == not recommended: rather use PR-labelled release!-->
<version>2025.0-PR3444</version>
<version>2025.0-PR3466</version>
</parent>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
name = "simwrapper",
description = "Run additional analysis and create SimWrapper dashboard for existing run output."
)
final class LausitzSimWrapperRunner implements MATSimAppCommand {
public final class LausitzSimWrapperRunner implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(LausitzSimWrapperRunner.class);

Expand All @@ -56,7 +56,8 @@ final class LausitzSimWrapperRunner implements MATSimAppCommand {
private boolean noise;


private LausitzSimWrapperRunner(){
public LausitzSimWrapperRunner(){
// public constructor needed for testing purposes.
}

@Override
Expand All @@ -81,7 +82,7 @@ public Integer call() throws Exception {
simwrapperCfg.defaultDashboards = SimWrapperConfigGroup.Mode.disabled;

//add dashboards according to command line parameters
// TODO: if more dashboards are to be added here, we need to check if noise==true before adding noise dashboard here
// if more dashboards are to be added here, we need to check if noise==true before adding noise dashboard here
sw.addDashboard(Dashboard.customize(new NoiseDashboard()).context("noise"));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.matsim.core.population.PersonUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.dashboards.LausitzSimWrapperRunner;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.testcases.MatsimTestUtils;

Expand All @@ -27,54 +28,28 @@

import static org.matsim.application.ApplicationUtils.globFile;

class EmissionAnalysisOutputTest {
class EmissionAndNoiseAnalysisOutputTest {

@RegisterExtension
public MatsimTestUtils utils = new MatsimTestUtils();

@TempDir
public Path p;

private final static Id<Person> ptPersonId = Id.createPersonId("Hoyerswerda-Cottbus_CAR");
private final static Id<Person> carPersonId = Id.createPersonId("Hoyerswerda-Cottbus_CAR");

private static Config config = ConfigUtils.loadConfig(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION));

@Disabled("Test is used to secure functionality of emission analysis. As the analysis needs" +
"a lot of RAM, it is disabled and only run manually. -sme0924")
@Test
void runEmissionAnalysisOutputTest() throws IOException {
Config config = ConfigUtils.loadConfig(String.format("input/v%s/lausitz-v%s-10pct.config.xml", LausitzScenario.VERSION, LausitzScenario.VERSION));
// Here, we do want dashboards in general (emission dashboard), but do not want to waste computation time on the standard dashboards
ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class).exclude = Set.of("TripDashboard", "OverviewDashboard", "StuckAgentDashboard",
"TrafficCountsDashboard", "TrafficDashboard", "PublicTransitDashboard");

Path inputPath = p.resolve("emissions-test-population.xml.gz");

Population population = PopulationUtils.createPopulation(config);
PopulationFactory fac = population.getFactory();
Person person = fac.createPerson(ptPersonId);
Plan plan = PopulationUtils.createPlan(person);

// home in hoyerswerda, nearest link 28922425#0
Activity home = fac.createActivityFromCoord("home_2400", new Coord(863538.13,5711028.24));
home.setEndTime(8 * 3600);
Activity home2 = fac.createActivityFromCoord("home_2400", new Coord(863538.13,5711028.24));
home2.setEndTime(19 * 3600);
// work in hoyerswerda, nearest link(s): 686055693#1, -686055693#1
Activity work = fac.createActivityFromCoord("work_2400", new Coord(863866.47,5710961.86));
work.setEndTime(17 * 3600 + 25 * 60);

Leg leg = fac.createLeg(TransportMode.car);

plan.addActivity(home);
plan.addLeg(leg);
plan.addActivity(work);
plan.addLeg(leg);
plan.addActivity(home2);

person.addPlan(plan);
PersonUtils.setIncome(person, 1000.);
person.getAttributes().putAttribute("subpopulation", "person");
population.addPerson(person);

new PopulationWriter(population).write(inputPath.toString());
Path inputPath = p.resolve("test-population.xml.gz");
createTestPopulation(inputPath);

assert MATSimApplication.execute(LausitzScenario.class, config,
"--1pct",
Expand Down Expand Up @@ -118,5 +93,94 @@ void runEmissionAnalysisOutputTest() throws IOException {
Assertions.assertFalse(nonZeroLinks.isEmpty());
Assertions.assertTrue(nonZeroLinks.containsKey("28922425#0"));
Assertions.assertTrue(nonZeroLinks.containsKey("-686055693#1"));
// CO
Assertions.assertEquals(830.9171, nonZeroLinks.get("28922425#0")[0], 0.0001);
Assertions.assertEquals(700.632, nonZeroLinks.get("-686055693#1")[0], 0.001);
// CO2_TOTAL
Assertions.assertEquals(8029.8788, nonZeroLinks.get("28922425#0")[1], 0.0001);
Assertions.assertEquals(6114.849, nonZeroLinks.get("-686055693#1")[1], 0.001);
}

@Disabled("Test is used to secure functionality of noise analysis. As the analysis needs" +
"a lot of RAM, it is disabled and only run manually. -sme0924")
@Test
void runNoiseAnalysisOutputTest() throws IOException {
// Here, we do want dashboards in general (emission dashboard), but do not want to waste computation time on the standard dashboards
ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class).defaultDashboards = SimWrapperConfigGroup.Mode.disabled;

Path inputPath = p.resolve("test-population.xml.gz");
createTestPopulation(inputPath);

assert MATSimApplication.execute(LausitzScenario.class, config,
"--1pct",
"--iterations", "0",
"--output", utils.getOutputDirectory(),
"--config:plans.inputPlansFile", inputPath.toString(),
"--config:controller.overwriteFiles=deleteDirectoryIfExists") == 0 : "Must return non error code";

new LausitzSimWrapperRunner().execute(utils.getOutputDirectory(), "--noise", "--shp", "../../../../../../../input/shp/lausitz.shp");

Path csvPath = globFile(Path.of(utils.getOutputDirectory() + "/analysis/noise-noise"), "*emission_per_day.csv*");

Map<String, Double> nonZeroLinks = new HashMap<>();

try {
BufferedReader reader = IOUtils.getBufferedReader(csvPath.toUri().toURL());
String line;

// skip header
reader.readLine();

while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");

if (Double.parseDouble(parts[1]) == 0.) {
continue;
}

double value = Double.parseDouble(parts[1]);

nonZeroLinks.put(parts[0], value);
}
} finally {

}

Assertions.assertFalse(nonZeroLinks.isEmpty());
Assertions.assertTrue(nonZeroLinks.containsKey("28922425#0"));
Assertions.assertTrue(nonZeroLinks.containsKey("-686055693#1"));
Assertions.assertEquals(71.21, Math.round(nonZeroLinks.get("28922425#0") * 100.) / 100. , 0.001);
Assertions.assertEquals(69.72, Math.round(nonZeroLinks.get("-686055693#1") * 100.) / 100., 0.001);
}

private void createTestPopulation(Path inputPath) {
Population population = PopulationUtils.createPopulation(config);
PopulationFactory fac = population.getFactory();
Person person = fac.createPerson(carPersonId);
Plan plan = PopulationUtils.createPlan(person);

// home in hoyerswerda, nearest link 28922425#0
Activity home = fac.createActivityFromCoord("home_2400", new Coord(863538.13,5711028.24));
home.setEndTime(8 * 3600);
Activity home2 = fac.createActivityFromCoord("home_2400", new Coord(863538.13,5711028.24));
home2.setEndTime(19 * 3600);
// work in hoyerswerda, nearest link(s): 686055693#1, -686055693#1
Activity work = fac.createActivityFromCoord("work_2400", new Coord(863866.47,5710961.86));
work.setEndTime(17 * 3600 + 25 * 60);

Leg leg = fac.createLeg(TransportMode.car);

plan.addActivity(home);
plan.addLeg(leg);
plan.addActivity(work);
plan.addLeg(leg);
plan.addActivity(home2);

person.addPlan(plan);
PersonUtils.setIncome(person, 1000.);
person.getAttributes().putAttribute("subpopulation", "person");
population.addPerson(person);

new PopulationWriter(population).write(inputPath.toString());
}
}

0 comments on commit 743c398

Please sign in to comment.