From 743c398738f8e18c6c1bd272442e7f4aeb5c01d5 Mon Sep 17 00:00:00 2001 From: sime94 Date: Tue, 10 Sep 2024 13:18:50 +0200 Subject: [PATCH] bump to PR3466 + add test for noise analysis --- pom.xml | 2 +- .../dashboards/LausitzSimWrapperRunner.java | 7 +- ...> EmissionAndNoiseAnalysisOutputTest.java} | 130 +++++++++++++----- 3 files changed, 102 insertions(+), 37 deletions(-) rename src/test/java/org/matsim/run/{EmissionAnalysisOutputTest.java => EmissionAndNoiseAnalysisOutputTest.java} (57%) diff --git a/pom.xml b/pom.xml index 4db5185..8c4c859 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ - 2025.0-PR3444 + 2025.0-PR3466 diff --git a/src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java b/src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java index bb61bda..29e09b6 100644 --- a/src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java +++ b/src/main/java/org/matsim/dashboards/LausitzSimWrapperRunner.java @@ -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); @@ -56,7 +56,8 @@ final class LausitzSimWrapperRunner implements MATSimAppCommand { private boolean noise; - private LausitzSimWrapperRunner(){ + public LausitzSimWrapperRunner(){ +// public constructor needed for testing purposes. } @Override @@ -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")); diff --git a/src/test/java/org/matsim/run/EmissionAnalysisOutputTest.java b/src/test/java/org/matsim/run/EmissionAndNoiseAnalysisOutputTest.java similarity index 57% rename from src/test/java/org/matsim/run/EmissionAnalysisOutputTest.java rename to src/test/java/org/matsim/run/EmissionAndNoiseAnalysisOutputTest.java index 0abb1c2..f4cd898 100644 --- a/src/test/java/org/matsim/run/EmissionAnalysisOutputTest.java +++ b/src/test/java/org/matsim/run/EmissionAndNoiseAnalysisOutputTest.java @@ -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; @@ -27,7 +28,7 @@ import static org.matsim.application.ApplicationUtils.globFile; -class EmissionAnalysisOutputTest { +class EmissionAndNoiseAnalysisOutputTest { @RegisterExtension public MatsimTestUtils utils = new MatsimTestUtils(); @@ -35,46 +36,20 @@ class EmissionAnalysisOutputTest { @TempDir public Path p; - private final static Id ptPersonId = Id.createPersonId("Hoyerswerda-Cottbus_CAR"); + private final static Id 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", @@ -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 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()); } }