From a38116d172e220452372a129513130d0b22e692d Mon Sep 17 00:00:00 2001 From: Peter Powers <pmpowers@usgs.gov> Date: Thu, 21 Nov 2024 15:35:41 -0700 Subject: [PATCH] updated combined gmm implementation --- .../earthquake/nshmp/gmm/CombinedGmm.java | 30 +++++-------------- .../earthquake/nshmp/gmm/GroundMotions.java | 2 +- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/main/java/gov/usgs/earthquake/nshmp/gmm/CombinedGmm.java b/src/main/java/gov/usgs/earthquake/nshmp/gmm/CombinedGmm.java index b1e30047..c790609e 100644 --- a/src/main/java/gov/usgs/earthquake/nshmp/gmm/CombinedGmm.java +++ b/src/main/java/gov/usgs/earthquake/nshmp/gmm/CombinedGmm.java @@ -76,12 +76,12 @@ import static gov.usgs.earthquake.nshmp.gmm.Gmm.ZHAO_06_INTERFACE_BASIN_M9; import static gov.usgs.earthquake.nshmp.gmm.Gmm.ZHAO_06_SLAB_BASIN; import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; +import java.util.Optional; import gov.usgs.earthquake.nshmp.data.DoubleData; import gov.usgs.earthquake.nshmp.gmm.GmmInput.Constraints; import gov.usgs.earthquake.nshmp.tree.LogicTree; +import gov.usgs.earthquake.nshmp.tree.Trees; /** * Convenience class for combined GMM implementations that compute weight @@ -92,12 +92,11 @@ import gov.usgs.earthquake.nshmp.tree.LogicTree; class CombinedGmm implements GroundMotionModel { private static final String NAME = "Combined: "; - private final Map<Gmm, Double> gmms; + private final LogicTree<GroundMotionModel> gmmTree; private final Imt imt; - /* Supply map of ground motion models initialized to the required IMT. */ private CombinedGmm(Imt imt, Map<Gmm, Double> gmms) { - this.gmms = gmms; + this.gmmTree = GmmUtils.weightMapToInstanceTree(gmms, imt); this.imt = imt; double[] weights = gmms.values().stream() @@ -113,23 +112,10 @@ class CombinedGmm implements GroundMotionModel { @Override public LogicTree<GroundMotion> calc(GmmInput in) { - - LogicTree.Builder<GroundMotion> builder = LogicTree.builder(NAME); - gmms.keySet().forEach(key -> builder.addBranch( - key.name(), - GroundMotions.combine(key.instance(imt).calc(in)), - gmms.get(key))); - return builder.build(); - } - - static Map<Gmm, GroundMotionModel> instancesForImt( - Imt imt, - Map<Gmm, Double> gmms) { - - return gmms.entrySet().stream() - .collect(Collectors.toMap( - Entry::getKey, - entry -> entry.getKey().instance(imt))); + return Trees.transform( + gmmTree, + gmm -> GroundMotions.combine(gmm.calc(in)), + Optional.of(GroundMotions.TREE_NAME)); } /* diff --git a/src/main/java/gov/usgs/earthquake/nshmp/gmm/GroundMotions.java b/src/main/java/gov/usgs/earthquake/nshmp/gmm/GroundMotions.java index 0ce4dc9e..690fa33a 100644 --- a/src/main/java/gov/usgs/earthquake/nshmp/gmm/GroundMotions.java +++ b/src/main/java/gov/usgs/earthquake/nshmp/gmm/GroundMotions.java @@ -80,7 +80,7 @@ public class GroundMotions { return new MultiMeanMultiSigma(μIds, μs, μWts, σIds, σs, σWts); } - private static final String TREE_NAME = "ground-motion-tree"; + static final String TREE_NAME = "ground-motion-tree"; public static final String EPI_LO = "epi-lo"; public static final String EPI_OFF = "epi-off"; public static final String EPI_HI = "epi-hi"; -- GitLab