From 03d90590142ea088569273f0d2804ffb0d95069f Mon Sep 17 00:00:00 2001 From: Brandon Clayton <bclayton@usgs.gov> Date: Tue, 6 Dec 2022 09:42:56 -0700 Subject: [PATCH] move some to NshmTests --- .../earthquake/nshmp/model/NshmTestUtils.java | 92 ++++--------------- 1 file changed, 19 insertions(+), 73 deletions(-) diff --git a/src/test/java/gov/usgs/earthquake/nshmp/model/NshmTestUtils.java b/src/test/java/gov/usgs/earthquake/nshmp/model/NshmTestUtils.java index 05be8ae7..d34e167e 100644 --- a/src/test/java/gov/usgs/earthquake/nshmp/model/NshmTestUtils.java +++ b/src/test/java/gov/usgs/earthquake/nshmp/model/NshmTestUtils.java @@ -9,11 +9,8 @@ import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.EnumSet; -import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @@ -30,37 +27,12 @@ import gov.usgs.earthquake.nshmp.calc.Hazard; import gov.usgs.earthquake.nshmp.calc.HazardCalcs; import gov.usgs.earthquake.nshmp.calc.Site; import gov.usgs.earthquake.nshmp.data.XySequence; -import gov.usgs.earthquake.nshmp.gmm.Imt; -import gov.usgs.earthquake.nshmp.site.NshmpSite; +import gov.usgs.earthquake.nshmp.model.NshmTests.Nshm; /** * Utilities to run tests on a NSHM. */ class NshmTestUtils { - /* Alaska test sites */ - private static final List<NamedLocation> ALASKA_LOCATIONS = List.of( - NshmpSite.ANCHORAGE_AK); - - /* CONUS test sites */ - private static final List<NamedLocation> CONUS_LOCATIONS = List.of( - NshmpSite.LOS_ANGELES_CA, - NshmpSite.SAN_FRANCISCO_CA, - NshmpSite.SEATTLE_WA, - NshmpSite.SALT_LAKE_CITY_UT, - NshmpSite.RENO_NV, - NshmpSite.NEW_MADRID_MO, - NshmpSite.BOSTON_MA, - NshmpSite.NEW_YORK_NY, - NshmpSite.CHICAGO_IL); - - /* Hawaii test sites */ - private static final List<NamedLocation> HAWAII_LOCATIONS = List.of( - NshmpSite.HILO_HI, - NshmpSite.HONOLULU_HI, - NshmpSite.KAILUA_KONA_HI); - - private static final Set<Imt> IMTS = EnumSet.of(Imt.PGA, Imt.SA0P2, Imt.SA1P0, Imt.SA5P0); - private static final Path DATA_PATH = Paths.get("src/test/resources/e2e"); private static final double TOLERANCE = 1e-12; @@ -69,6 +41,11 @@ class NshmTestUtils { .setPrettyPrinting() .create(); + /** + * Generate results for all {@link NSHM}s + * + * Run "./gradlew nshms" to first download all NSHMs + */ public static void main(String[] args) throws IOException { for (Nshm nshm : Nshm.values()) { /* Initialize and shut down executor to generate results. */ @@ -86,7 +63,7 @@ class NshmTestUtils { static final void testNshm(Nshm nshm) { NshmModel nshmModel = loadModel(nshm); - for (NamedLocation location : nshm.locations) { + for (NamedLocation location : nshm.locations()) { compareCurves(nshmModel, location); } @@ -129,12 +106,13 @@ class NshmTestUtils { } } - private static Map<String, XySequence> generateActual(NshmModel nshmModel, + private static Map<String, XySequence> generateActual( + NshmModel nshmModel, NamedLocation location) { Site site = Site.builder().location(location.location()).build(); CalcConfig config = CalcConfig.copyOf(nshmModel.model.config()) - .imts(nshmModel.nshm.imts) + .imts(nshmModel.nshm.imts()) .build(); Hazard hazard = HazardCalcs.hazard( @@ -156,20 +134,14 @@ class NshmTestUtils { return new NshmModel( nshm, - ModelLoader.load(nshm.modelPath), + ModelLoader.load(nshm.modelPath()), Executors.newFixedThreadPool(cores)); } - private static String resultFilename( - String modelName, - int year, - NamedLocation loc) { - return modelName + "-" + year + "-" + loc.name() + ".json"; - } - private static Map<String, XySequence> readExpected(NshmModel nshmModel, NamedLocation loc) { - String filename = resultFilename(nshmModel.nshm.modelName, nshmModel.nshm.year, loc); - Path resultPath = DATA_PATH.resolve(nshmModel.nshm.modelName).resolve(filename); + Path resultPath = DATA_PATH + .resolve(nshmModel.nshm.modelName()) + .resolve(nshmModel.nshm.resultFilename(loc)); JsonObject obj = null; try (BufferedReader br = Files.newBufferedReader(resultPath)) { @@ -188,24 +160,22 @@ class NshmTestUtils { } private static void writeExpecteds(NshmModel nshmModel) throws IOException { - for (NamedLocation location : nshmModel.nshm.locations) { + for (NamedLocation location : nshmModel.nshm.locations()) { Map<String, XySequence> xyMap = generateActual(nshmModel, location); String json = GSON.toJson(xyMap); - writeExpected(nshmModel.nshm.modelName, nshmModel.nshm.year, location, json); + writeExpected(nshmModel.nshm, location, json); } } private static void writeExpected( - String modelName, - int year, + Nshm nshm, NamedLocation loc, String json) throws IOException { - String filename = resultFilename(modelName, year, loc); - Path modelDir = DATA_PATH.resolve(modelName); + Path modelDir = DATA_PATH.resolve(nshm.modelName()); if (!Files.exists(modelDir)) { Files.createDirectories(modelDir); } - Path resultPath = modelDir.resolve(filename); + Path resultPath = modelDir.resolve(nshm.resultFilename(loc)); Files.write(resultPath, json.getBytes()); } @@ -231,28 +201,4 @@ class NshmTestUtils { this.exec = exec; } } - - static enum Nshm { - ALASKA_2023("nshm-alaska-3.a.0", ALASKA_LOCATIONS, IMTS), - - CONUS_2018("nshm-conus-5.2.0", CONUS_LOCATIONS, IMTS), - - CONUS_2023("nshm-conus-6.a.3", CONUS_LOCATIONS, IMTS), - - HAWAII_2021("nshm-hawaii-2.0.2", HAWAII_LOCATIONS, IMTS); - - private final Path modelPath; - private final String modelName; - private final List<NamedLocation> locations; - private final Set<Imt> imts; - private final int year; - - Nshm(String modelName, List<NamedLocation> locations, Set<Imt> imts) { - this.locations = locations; - this.imts = imts; - this.modelName = modelName; - this.modelPath = Paths.get("nshms", modelName); - this.year = Integer.parseInt(name().split("_")[1]); - } - } } -- GitLab