def test_depths_go_to_zero(self): # Depth information is zero when a :class:`Site` # contains only (lon, lat) s1 = Site(location=Point(10, 20), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) s2 = Site(location=Point(-1.2, -3.4), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) cll = SiteCollection([s1, s2]) cll_sites = list(cll) exp_s1 = Site(location=Point(10, 20, 0.0), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) exp_s2 = Site(location=Point(-1.2, -3.4, 0.0), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) for i, s in enumerate([exp_s1, exp_s2]): self.assertEqual(s, cll_sites[i]) # test equality of site collections sc = SiteCollection([exp_s1, exp_s2]) self.assertEqual(cll, sc)
class JB2009ApplyCorrelationTestCase(unittest.TestCase): SITECOL = SiteCollection([ Site(Point(2, -40), 1, 1, 1), Site(Point(2, -40.1), 1, 1, 1), Site(Point(2, -39.9), 1, 1, 1) ]) def test(self): numpy.random.seed(13) cormo = JB2009CorrelationModel(vs30_clustering=False) intra_residuals_sampled = numpy.random.normal(size=(3, 100000)) intra_residuals_correlated = cormo.apply_correlation( self.SITECOL, PGA(), intra_residuals_sampled) inferred_corrcoef = numpy.corrcoef(intra_residuals_correlated) mean = intra_residuals_correlated.mean() std = intra_residuals_correlated.std() self.assertAlmostEqual(mean, 0, delta=0.002) self.assertAlmostEqual(std, 1, delta=0.002) actual_corrcoef = cormo._get_correlation_matrix(self.SITECOL, PGA()) numpy.testing.assert_almost_equal(inferred_corrcoef, actual_corrcoef, decimal=2) def test_filtered_sitecol(self): filtered = self.SITECOL.filtered([0, 2]) numpy.random.seed(13) cormo = JB2009CorrelationModel(vs30_clustering=False) intra_residuals_sampled = numpy.random.normal(size=(2, 5)) intra_residuals_correlated = cormo.apply_correlation( filtered, PGA(), intra_residuals_sampled) aaae(intra_residuals_correlated, [[-0.71239066, 0.75376638, -0.04450308, 0.45181234, 1.34510171], [0.51816327, 1.36481251, 0.86016437, 1.48732124, -1.01860545]], decimal=6)
def test_simple_fault_surface_vertical(self): # Create the rupture mag = 10.0 rake = 90.0 trt = TRT.ACTIVE_SHALLOW_CRUST hypoc = Point(0.25, 0.0, 5) pmf = PMF([(0.8, 0), (0.2, 1)]) rup = NPPR(mag, rake, trt, hypoc, self.srfc1, pmf) # Compute distances param = 'closest_point' sites = SiteCollection( [Site(Point(0.25, -0.1, 0.0)), Site(Point(-0.1, 0.0, 0.0))]) dsts = get_distances(rup, sites, param) # Check first point msg = 'The longitude of the first point is wrong' self.assertTrue(abs(dsts[0, 0] - 0.25) < 1e-2, msg) msg = 'The latitude of the first point is wrong' self.assertTrue(abs(dsts[0][1] - 0.0) < 1e-2, msg) # Check second point msg = 'The longitude of the second point is wrong' self.assertTrue(abs(dsts[1, 0] - 0.0) < 1e-2, msg) msg = 'The latitude of the second point is wrong' self.assertTrue(abs(dsts[1][1] - 0.0) < 1e-2, msg)
def example_calc(apply): sitecol = SiteCollection([ Site(Point(30.0, 30.0), 760., 1.0, 1.0), Site(Point(30.25, 30.25), 760., 1.0, 1.0), Site(Point(30.4, 30.4), 760., 1.0, 1.0) ]) mfd_1 = TruncatedGRMFD(4.5, 8.0, 0.1, 4.0, 1.0) mfd_2 = TruncatedGRMFD(4.5, 7.5, 0.1, 3.5, 1.1) sources = [ PointSource('001', 'Point1', 'Active Shallow Crust', mfd_1, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])), PointSource('002', 'Point2', 'Active Shallow Crust', mfd_2, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])) ] imtls = { 'PGA': [0.01, 0.1, 0.2, 0.5, 0.8], 'SA(0.5)': [0.01, 0.1, 0.2, 0.5, 0.8] } gsims = {'Active Shallow Crust': AkkarBommer2010()} return calc_hazard_curves(sources, sitecol, imtls, gsims, apply=apply, filter_distance='rrup')
def test_no_correlation_mean_and_intra_respected(self): mean1 = 10 mean2 = 14 inter = 1e-300 intra1 = 0.2 intra2 = 1.6 p1 = Point(0, 0) p2 = Point(0, 0.3) sites = [ Site(p1, mean1, False, inter, intra1), Site(p2, mean2, False, inter, intra2) ] self.sites = SiteCollection(sites) numpy.random.seed(41) cormo = JB2009CorrelationModel(vs30_clustering=False) s1_intensity, s2_intensity = ground_motion_fields( self.rupture, self.sites, [self.imt1], self.gsim, truncation_level=None, realizations=6000, correlation_model=cormo, )[self.imt1] self.assertAlmostEqual(s1_intensity.mean(), mean1, delta=1e-3) self.assertAlmostEqual(s2_intensity.mean(), mean2, delta=1e-3) self.assertAlmostEqual(s1_intensity.std(), intra1, delta=2e-3) self.assertAlmostEqual(s2_intensity.std(), intra2, delta=1e-2)
def test_correlation_with_total_stddev(self): mean1 = 10 mean2 = 14 inter = 1e-300 intra1 = 0.2 intra2 = 1.6 p1 = Point(0, 0) p2 = Point(0, 0.3) sites = [ Site(p1, mean1, False, inter, intra1), Site(p2, mean2, False, inter, intra2) ] self.sites = SiteCollection(sites) numpy.random.seed(41) cormo = JB2009CorrelationModel(vs30_clustering=False) gsim = FakeGSIMTotalStdDev(self) gsim.expect_same_sitecol = False with self.assertRaises(CorrelationButNoInterIntraStdDevs): ground_motion_fields(self.rupture, self.sites, [self.imt1], gsim, truncation_level=None, realizations=6000, correlation_model=cormo)
def test_depths_go_to_zero(self): # Depth information is meant to be discarded when a site collection is # created. s1 = Site(location=Point(10, 20, 30), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) s2 = Site(location=Point(-1.2, -3.4, -5.6), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) cll = SiteCollection([s1, s2]) cll_sites = list(cll) exp_s1 = Site(location=Point(10, 20, 0.0), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) exp_s2 = Site(location=Point(-1.2, -3.4, 0.0), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) for i, s in enumerate([exp_s1, exp_s2]): self.assertEqual(s, cll_sites[i]) # test equality of site collections sc = SiteCollection([exp_s1, exp_s2]) self.assertEqual(cll, sc) # test nonequality of site collections # (see https://github.com/gem/oq-hazardlib/pull/403) sc._vs30 = numpy.array([numpy.nan, numpy.nan]) self.assertNotEqual(cll, sc)
def test_from_sites(self): s1 = Site(location=Point(10, 20, 30), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6, backarc=True) s2 = Site(location=Point(-1.2, -3.4, -5.6), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9, backarc=False) cll = SiteCollection([s1, s2]) self.assertTrue((cll.vs30 == [1.2, 55.4]).all()) self.assertTrue((cll.vs30measured == [True, False]).all()) self.assertTrue((cll.z1pt0 == [3.4, 66.7]).all()) self.assertTrue((cll.z2pt5 == [5.6, 88.9]).all()) self.assertTrue((cll.mesh.lons == [10, -1.2]).all()) self.assertTrue((cll.mesh.lats == [20, -3.4]).all()) self.assertTrue((cll.backarc == [True, False]).all()) self.assertIs(cll.mesh.depths, None) for arr in (cll.vs30, cll.z1pt0, cll.z2pt5): self.assertIsInstance(arr, numpy.ndarray) self.assertEqual(arr.flags.writeable, False) self.assertEqual(arr.dtype, float) for arr in (cll.vs30measured, cll.backarc): self.assertIsInstance(arr, numpy.ndarray) self.assertEqual(arr.flags.writeable, False) self.assertEqual(arr.dtype, bool) self.assertEqual(len(cll), 2) # test __toh5__ and __fromh5__ array, attrs = cll.__toh5__() newcll = cll.__new__(cll.__class__) newcll.__fromh5__(array, attrs) self.assertEqual(newcll, cll)
def test_from_sites(self): s1 = Site(location=Point(10, 20, 30), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) s2 = Site(location=Point(-1.2, -3.4, -5.6), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) cll = SiteCollection([s1, s2]) assert_eq(cll.vs30, [1.2, 55.4]) assert_eq(cll.vs30measured, [True, False]) assert_eq(cll.z1pt0, [3.4, 66.7]) assert_eq(cll.z2pt5, [5.6, 88.9]) assert_eq(cll.mesh.lons, [10, -1.2]) assert_eq(cll.mesh.lats, [20, -3.4]) assert_eq(cll.mesh.depths, [30, -5.6]) for arr in (cll.vs30, cll.z1pt0, cll.z2pt5): self.assertIsInstance(arr, numpy.ndarray) self.assertEqual(arr.flags.writeable, False) self.assertEqual(arr.dtype, float) for arr in (cll.vs30measured,): self.assertIsInstance(arr, numpy.ndarray) self.assertEqual(arr.flags.writeable, False) self.assertEqual(arr.dtype, bool) self.assertEqual(len(cll), 2) # test serialization to hdf5 fd, fpath = tempfile.mkstemp(suffix='.hdf5') os.close(fd) with hdf5.File(fpath, 'w') as f: f['folder'] = dict(sitecol=cll, b=[2, 3]) newcll = f['folder/sitecol'] self.assertEqual(newcll, cll) self.assertEqual(list(f['folder/b']), [2, 3]) os.remove(fpath)
def setUp(self): """ """ mfd_1 = TruncatedGRMFD(4.5, 8.0, 0.1, 4.0, 1.0) mfd_2 = TruncatedGRMFD(4.5, 7.5, 0.1, 3.5, 1.1) self.source_model = [ PointSource('001', 'Point1', 'Active Shallow Crust', mfd_1, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])), PointSource('002', 'Point2', 'Active Shallow Crust', mfd_2, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])) ] self.sites = SiteCollection([ Site(Point(30.0, 30.0), 760., True, 1.0, 1.0, 1), Site(Point(30.25, 30.25), 760., True, 1.0, 1.0, 2), Site(Point(30.4, 30.4), 760., True, 1.0, 1.0, 2) ]) self.gsims = {'Active Shallow Crust': 'AkkarBommer2010'} self.imts = ['PGA', 'SA(0.5)'] self.imls = [[0.01, 0.1, 0.2, 0.5, 0.8]]
def reference_psha_calculation_openquake(): """ Sets up the reference PSHA calculation calling OpenQuake directly. All subsequent implementations should match this example """ # Site model - 3 Sites site_model = SiteCollection([ Site(Point(30.0, 30.0), 760., True, 1.0, 1.0, 1), Site(Point(30.25, 30.25), 760., True, 1.0, 1.0, 2), Site(Point(30.4, 30.4), 760., True, 1.0, 1.0, 2) ]) # Source Model Two Point Sources mfd_1 = TruncatedGRMFD(4.5, 8.0, 0.1, 4.0, 1.0) mfd_2 = TruncatedGRMFD(4.5, 7.5, 0.1, 3.5, 1.1) source_model = [ PointSource('001', 'Point1', 'Active Shallow Crust', mfd_1, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])), PointSource('002', 'Point2', 'Active Shallow Crust', mfd_2, 1.0, WC1994(), 1.0, PoissonTOM(50.0), 0.0, 30.0, Point(30.0, 30.5), PMF([(1.0, NodalPlane(0.0, 90.0, 0.0))]), PMF([(1.0, 10.0)])) ] imts = { 'PGA': [0.01, 0.1, 0.2, 0.5, 0.8], 'SA(0.5)': [0.01, 0.1, 0.2, 0.5, 0.8] } # Akkar & Bommer (2010) GMPE gsims = {'Active Shallow Crust': gsim.akkar_bommer_2010.AkkarBommer2010()} truncation_level = None return calc_hazard_curves(source_model, site_model, imts, gsims, truncation_level)
def test_source_filter_filter_all_out(self): col = SiteCollection([Site(Point(10, 10), 1, True, 2, 3), Site(Point(11, 12), 2, True, 2, 3), Site(Point(13, 14), 1, True, 2, 3)]) for int_dist in (0, 1, 10, 100, 1000): filtered = self.source.filter_sites_by_distance_to_source( integration_distance=int_dist, sites=col ) self.assertIs(filtered, None) # all filtered
def get_target_sites_line(self, maximum_distance, spacing, vs30, line_azimuth=90., origin_point=(0.5, 0.5), as_log=False, vs30measured=True, z1pt0=None, z2pt5=None, backarc=False): """ Defines the target sites along a line with respect to the rupture """ #input_origin_point = deepcopy(origin_point) azimuth, origin_location, z1pt0, z2pt5 = _setup_site_peripherals( line_azimuth, origin_point, vs30, z1pt0, z2pt5, self.strike, self.surface) self.target_sites = [ Site(origin_location, vs30, vs30measured, z1pt0, z2pt5, backarc=backarc) ] spacings = self._define_line_spacing(maximum_distance, spacing, as_log) for offset in spacings: target_loc = origin_location.point_at(offset, 0., azimuth) # Get Rupture distance temp_mesh = Mesh(np.array(target_loc.longitude), np.array(target_loc.latitude), np.array(target_loc.depth)) distance = self.surface.get_min_distance(temp_mesh) self.target_sites.append( Site(target_loc, vs30, vs30measured, z1pt0, z2pt5, backarc=backarc)) self.target_sites_config = { "TYPE": "Line", "RMAX": maximum_distance, "SPACING": spacing, "AZIMUTH": line_azimuth, "ORIGIN": origin_point, "AS_LOG": as_log, "VS30": vs30, "VS30MEASURED": vs30measured, "Z1.0": z1pt0, "Z2.5": z2pt5, "BACKARC": backarc } self.target_sites = SiteCollection(self.target_sites) return self.target_sites
class JB2009LowerTriangleCorrelationMatrixTestCase(unittest.TestCase): SITECOL = SiteCollection([Site(Point(2, -40), 1, 1, 1), Site(Point(2, -40.1), 1, 1, 1), Site(Point(2, -39.9), 1, 1, 1)]) def test(self): cormo = JB2009CorrelationModel(vs30_clustering=False) lt = cormo.get_lower_triangle_correlation_matrix(self.SITECOL, PGA()) aaae(lt, [[1.0, 0.0, 0.0], [1.97514806e-02, 9.99804920e-01, 0.0], [1.97514806e-02, 5.42206860e-20, 9.99804920e-01]])
def test(self): s1 = Site(location=Point(10, 20), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6) s2 = Site(location=Point(-1.2, -3.4), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9) cll = SiteCollection([s1, s2]) cll_sites = list(cll) for i, s in enumerate([s1, s2]): self.assertEqual(s, cll_sites[i])
class HM2018ApplyCorrelationTestCase(unittest.TestCase): SITECOL = SiteCollection([ Site(Point(2, -40), 1, 1, 1), Site(Point(2, -40.1), 1, 1, 1), Site(Point(2, -39.95), 1, 1, 1) ]) def test_no_uncertainty(self): numpy.random.seed(1) Nsim = 100000 imt = SA(period=2.0, damping=5) stddev_intra = numpy.array([0.5, 0.6, 0.7]) cormo = HM2018CorrelationModel(uncertainty_multiplier=0) intra_residuals_sampled = numpy.random.multivariate_normal( numpy.zeros(3), numpy.diag(stddev_intra ** 2), Nsim).\ transpose(1, 0) intra_residuals_correlated = cormo.apply_correlation( self.SITECOL, imt, intra_residuals_sampled, stddev_intra) inferred_corrcoef = numpy.corrcoef(intra_residuals_correlated) mean = intra_residuals_correlated.mean(1) std = intra_residuals_correlated.std(1) aaae(numpy.squeeze(numpy.asarray(mean)), numpy.zeros(3), 2) aaae(numpy.squeeze(numpy.asarray(std)), stddev_intra, 2) actual_corrcoef = cormo._get_correlation_matrix(self.SITECOL, imt) aaae(inferred_corrcoef, actual_corrcoef, 2) def test_with_uncertainty(self): numpy.random.seed(1) Nsim = 100000 imt = SA(period=3.0, damping=5) stddev_intra = numpy.array([0.3, 0.6, 0.9]) cormo = HM2018CorrelationModel(uncertainty_multiplier=1) intra_residuals_sampled = numpy.random.multivariate_normal( numpy.zeros(3), numpy.diag(stddev_intra ** 2), Nsim).\ transpose(1, 0) intra_residuals_correlated = cormo.apply_correlation( self.SITECOL, imt, intra_residuals_sampled, stddev_intra) inferred_corrcoef = numpy.corrcoef(intra_residuals_correlated) mean = intra_residuals_correlated.mean(1) std = intra_residuals_correlated.std(1) aaae(numpy.squeeze(numpy.asarray(mean)), numpy.zeros(3), 2) aaae(numpy.squeeze(numpy.asarray(std)), stddev_intra, 2) aaae(inferred_corrcoef, [[1., 0.3807, 0.5066], [0.3807, 1., 0.3075], [0.5066, 0.3075, 1.]], 2)
def setUp(self): self.orig_make_contexts = ContextMaker.make_contexts ContextMaker.make_contexts = lambda self, sites, rupture: ( FakeSiteContext(sites), rupture, None) self.truncation_level = 3.4 self.imts = {'PGA': [1, 2, 3], 'PGD': [2, 4]} self.time_span = 49.2 rup11 = self.FakeRupture(0.23, const.TRT.ACTIVE_SHALLOW_CRUST) rup12 = self.FakeRupture(0.15, const.TRT.ACTIVE_SHALLOW_CRUST) rup21 = self.FakeRupture(0.04, const.TRT.VOLCANIC) self.source1 = self.FakeSource(1, [rup11, rup12], self.time_span, const.TRT.ACTIVE_SHALLOW_CRUST) self.source2 = self.FakeSource(2, [rup21], self.time_span, const.TRT.VOLCANIC) self.sources = [self.source1, self.source2] site1 = Site(Point(10, 20), 1, True, 2, 3) site2 = Site(Point(20, 30), 2, False, 4, 5) self.sites = SiteCollection([site1, site2]) gsim1 = self.FakeGSIM( self.truncation_level, self.imts, poes={ (site1.location.latitude, rup11, imt.PGA()): [0.1, 0.05, 0.03], (site2.location.latitude, rup11, imt.PGA()): [0.11, 0.051, 0.034], (site1.location.latitude, rup12, imt.PGA()): [0.12, 0.052, 0.035], (site2.location.latitude, rup12, imt.PGA()): [0.13, 0.053, 0.036], (site1.location.latitude, rup11, imt.PGD()): [0.4, 0.33], (site2.location.latitude, rup11, imt.PGD()): [0.39, 0.331], (site1.location.latitude, rup12, imt.PGD()): [0.38, 0.332], (site2.location.latitude, rup12, imt.PGD()): [0.37, 0.333], }) gsim2 = self.FakeGSIM(self.truncation_level, self.imts, poes={ (site1.location.latitude, rup21, imt.PGA()): [0.5, 0.3, 0.2], (site2.location.latitude, rup21, imt.PGA()): [0.4, 0.2, 0.1], (site1.location.latitude, rup21, imt.PGD()): [0.24, 0.08], (site2.location.latitude, rup21, imt.PGD()): [0.14, 0.09], }) self.gsims = { const.TRT.ACTIVE_SHALLOW_CRUST: gsim1, const.TRT.VOLCANIC: gsim2 }
def test_mearn_nearfault_distance_taper(self): rupture = self.make_rupture() site1 = Site(location=Point(27.9, 41), vs30=1200., vs30measured=True, z1pt0=2.36, z2pt5=2.) site2 = Site(location=Point(28.1, 41), vs30=1200., vs30measured=True, z1pt0=2.36, z2pt5=2.) sites = SiteCollection([site1, site2]) fields = ground_motion_fields( rupture, sites, [PGV()], ChiouYoungs2014NearFaultEffect(), truncation_level=0, realizations=1) gmf = fields[PGV()] self.assertAlmostEquals(2.27395, gmf[0], delta=1e-4) self.assertAlmostEquals(3.38409, gmf[1], delta=1e-4)
def test_two_sites(self): site1 = Site(Point(0, 0), vs30=760., z1pt0=48.0, z2pt5=0.607, vs30measured=True) site2 = Site(Point(0, 0.5), vs30=760., z1pt0=48.0, z2pt5=0.607, vs30measured=True) sitecol = SiteCollection([site1, site2]) srcfilter = SourceFilter(sitecol, IntegrationDistance.new('200')) imtls = {"PGA": [.123]} for period in numpy.arange(.1, .5, .1): imtls['SA(%.2f)' % period] = [.123] assert len(imtls) == 5 # 5 periods gsim_by_trt = {'Stable Continental Crust': ExampleA2021()} hcurves = calc_hazard_curves( [asource], srcfilter, DictArray(imtls), gsim_by_trt) print(hcurves)
def test_get_bounding_boxes(self): maxdist = IntegrationDistance({'default': [ (3, 30), (4, 40), (5, 100), (6, 200), (7, 300), (8, 400)]}) sitecol = SiteCollection([ Site(location=Point(10, 20, 30), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6, backarc=True), Site(location=Point(-1.2, -3.4, -5.6), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9, backarc=False)]) srcfilter = SourceFilter(sitecol, maxdist) bb1, bb2 = srcfilter.get_bounding_boxes(mag=4) # bounding boxes in the form min_lon, min_lat, max_lon, max_lat aae(bb1, (9.6171855, 19.640272, 10.3828145, 20.359728)) aae(bb2, (-1.5603623, -3.759728, -0.8396377, -3.040272))
def test_international_date_line(self): maxdist = IntegrationDistance({'default': [ (3, 30), (4, 40), (5, 100), (6, 200), (7, 300), (8, 400)]}) sitecol = SiteCollection([ Site(location=Point(179, 80), vs30=1.2, vs30measured=True, z1pt0=3.4, z2pt5=5.6, backarc=True), Site(location=Point(-179, 80), vs30=55.4, vs30measured=False, z1pt0=66.7, z2pt5=88.9, backarc=False)]) srcfilter = SourceFilter(sitecol, maxdist) bb1, bb2 = srcfilter.get_bounding_boxes(mag=4) # bounding boxes in the form min_lon, min_lat, max_lon, max_lat aae(bb1, (176.928409, 79.640272, 181.071591, 80.359728)) aae(bb2, (-181.071591, 79.640272, -176.928409, 80.359728))
def test(self): sitecol = SiteCollection([Site(Point(-65.13490, 0.0), vs30=760., z1pt0=48.0, z2pt5=0.607, vs30measured=True)]) mfd = ArbitraryMFD([6.0], [0.01604252]) trace = Line([Point(-65.0000, -0.11240), Point(-65.000, 0.11240)]) # 1.0 km Mesh Spacing mesh_spacing = 1.0 msr = PeerMSR() sources = [SimpleFaultSource("001", "PEER Fault Set 2.5", "Active Shallow Crust", mfd, mesh_spacing, msr, 2.0, PoissonTOM(1.0), 0.0, 12., trace, 90., 0.)] imtls = {"PGA": [0.001, 0.01, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.0]} gmpe = ChiouYoungs2014PEER() gmpe.mixture_model = {"factors": [0.8, 1.2], "weights": [0.5, 0.5]} hcm = calc_hazard_curves(sources, sitecol, imtls, {"Active Shallow Crust": gmpe}) # Match against the benchmark is not exact - but differences in the # log space should be on the order of less than 0.04% expected = numpy.array([-4.140470001, -4.140913368, -4.259457496, -4.724733842, -5.900747959, -7.734816415, -9.019329629, -10.03864778, -10.90333404, -11.83885783, -12.65826442, -14.05429951, -15.22535996, -16.23988897, -17.94685518, -19.36079032, -20.57460101, -21.64201335]) expected = numpy.around(expected, 5) hcm_lnpga = numpy.around(numpy.log(hcm["PGA"].flatten()), 5) perc_diff = 100.0 * ((hcm_lnpga / expected) - 1.0) numpy.testing.assert_allclose(perc_diff, numpy.zeros(len(perc_diff)), atol=0.04)
def test_ses_generation_from_parametric_source_with_filtering(self): # generate stochastic event set (SES) from 2 area sources (area1, # area2). However, by including a single site co-located with the # area1 center, and with source site filtering of 100 km (exactly # the radius of area1), the second source (area2), which is centered # at 5., 5. (that is about 500 km from center of area1), will be # excluded. the MFD from the SES will be therefore approximately equal # to the one of area1 only. numpy.random.seed(123) sites = SiteCollection([ Site(location=Point(0., 0.), vs30=760, vs30measured=True, z1pt0=40., z2pt5=2.) ]) ses = stochastic_event_set([self.area1, self.area2], filters.SourceFilter( sites, filters.MagDepDistance.new('100'))) rates = self._extract_rates(ses, time_span=self.time_span, bins=numpy.arange(5., 6.6, 0.1)) expect_rates = numpy.array( [r for m, r in self.mfd.get_annual_occurrence_rates()]) numpy.testing.assert_allclose(rates, expect_rates, rtol=0, atol=1e-4)
def test_magnitude_bins(self): """ Testing build disaggregation matrix """ fname = os.path.join(DATA_PATH, 'data', 'ssm.xml') converter = sourceconverter.SourceConverter(50., 1., 10, 0.1, 10) groups = to_python(fname, converter) sources = [] for g in groups: sources += g.sources site = Site(Point(172.63, -43.53), vs30=250, vs30measured=False, z1pt0=330) imt = SA(3.0) iml = 0.25612220 gsim_by_trt = {TRT.ACTIVE_SHALLOW_CRUST: Bradley2013()} truncation_level = 3.0 n_epsilons = 1 mag_bin_width = 0.1 dist_bin_width = 100. coord_bin_width = 100. # Compute the disaggregation matrix edges, mtx = disagg.disaggregation(sources, site, imt, iml, gsim_by_trt, truncation_level, n_epsilons, mag_bin_width, dist_bin_width, coord_bin_width) tm = disagg.mag_pmf(mtx[:, :, :, :, :, 0]) numpy.testing.assert_array_less(numpy.zeros_like(tm[2:]), tm[2:])
def to_openquake_site(self, missing_vs30=None): """ Returns the site as an instance of the :class: openquake.hazardlib.site.Site """ if self.vs30: vs30 = self.vs30 vs30_measured = self.vs30_measured else: vs30 = missing_vs30 vs30_measured = False if self.z1pt0: z1pt0 = self.z1pt0 else: z1pt0 = vs30_to_z1pt0_as08(vs30) if self.z2pt5: z2pt5 = self.z2pt5 else: z2pt5 = z1pt0_to_z2pt5(z1pt0) location = Point(self.longitude, self.latitude, -self.altitude / 1000.) # Elevation from m to km oq_site = Site(location, vs30, z1pt0, z2pt5, vs30measured=vs30_measured, backarc=self.backarc) setattr(oq_site, "id", self.id) return oq_site
def test_nankai(self): # source model for the Nankai region provided by M. Pagani source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml') # it has a single group containing 15 mutex sources [group] = nrml.to_python(source_model) aae(group.srcs_weights, [ 0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125, 0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1 ]) rup_serial = numpy.arange(group.tot_ruptures, dtype=numpy.uint32) start = 0 for i, src in enumerate(group): src.id = i nr = src.num_ruptures src.serial = rup_serial[start:start + nr] start += nr group.samples = 1 lonlat = 135.68, 35.68 site = Site(geo.Point(*lonlat), 800, True, z1pt0=100., z2pt5=1.) s_filter = SourceFilter(SiteCollection([site]), {}) param = dict(ses_per_logic_tree_path=10, seed=42) gsims = [SiMidorikawa1999SInter()] dic = sample_ruptures(group, s_filter, gsims, param) self.assertEqual(dic['num_ruptures'], 19) # total ruptures self.assertEqual(dic['num_events'], 16) self.assertEqual(len(dic['eb_ruptures']), 8) self.assertEqual(len(dic['calc_times']), 15) # mutex sources # test export mesh = numpy.array([lonlat], [('lon', float), ('lat', float)]) ebr = dic['eb_ruptures'][0] ebr.export(mesh)
def get_regular_site_collection(limits, vs30, z1pt0=100.0, z2pt5=1.0): """ Gets a collection of sites from a regularly spaced grid of points :param list limits: Limits of mesh as [lower_long, upper_long, long_spc, lower_lat, upper_lat, lat_spc] :param float vs30: Vs30 values :param float z1pt0: Depth (m) to the 1 km/s interface :param float z2pt5: Depth (km) to the 2.5 km/s interface """ xgrd = np.arange(limits[0], limits[1] + limits[2], limits[2]) ygrd = np.arange(limits[3], limits[4] + limits[5], limits[5]) gx, gy = np.meshgrid(xgrd, ygrd) nx, ny = np.shape(gx) ngp = nx * ny gx = np.reshape(gx, [ngp, 1]).flatten() gy = np.reshape(gy, [ngp, 1]).flatten() depths = np.zeros(ngp, dtype=float) return SiteCollection([ Site(Point(gx[i], gy[i]), vs30, True, z1pt0, z2pt5, i) for i in range(0, ngp) ])
def point_at_distance(self, model, distance, vs30, line_azimuth=90., origin_point=(0.5, 0.), vs30measured=True, z1pt0=None, z2pt5=None, backarc=False): """ Generates a point at a given hypocentral distance """ azimuth, origin_point, z1pt0, z2pt5 = _setup_site_peripherals( line_azimuth, origin_point, vs30, z1pt0, z2pt5, model.strike, model.surface) xdist = sqrt(distance**2. - model.hypocentre.depth**2.) return SiteCollection([ Site(model.hypocentre.point_at(xdist, -model.hypocentre.depth, azimuth), vs30, vs30measured, z1pt0, z2pt5, backarc=backarc) ])
def point_at_distance(self, model, distance, vs30, line_azimuth=90., origin_point=(0.5, 0.), vs30measured=True, z1pt0=None, z2pt5=None, backarc=False): """ Generates a site given a specified rupture distance from the rupture surface """ azimuth, origin_location, z1pt0, z2pt5 = _setup_site_peripherals( line_azimuth, origin_point, vs30, z1pt0, z2pt5, model.strike, model.surface) selected_point = _rup_to_point(distance, model.surface, origin_location, azimuth, 'rjb') target_sites = SiteCollection([ Site(selected_point, vs30, vs30measured, z1pt0, z2pt5, backarc=backarc) ]) return target_sites
def test_nankai(self): # source model for the Nankai region provided by M. Pagani source_model = os.path.join(os.path.dirname(__file__), 'nankai.xml') # it has a single group containing 15 mutex sources [group] = nrml.to_python(source_model) aae([src.mutex_weight for src in group], [0.0125, 0.0125, 0.0125, 0.0125, 0.1625, 0.1625, 0.0125, 0.0125, 0.025, 0.025, 0.05, 0.05, 0.325, 0.025, 0.1]) seed = 42 rup_serial = numpy.arange(seed, seed + group.tot_ruptures, dtype=numpy.uint32) start = 0 for i, src in enumerate(group): src.id = i nr = src.num_ruptures src.serial = rup_serial[start:start + nr] start += nr lonlat = 135.68, 35.68 site = Site(geo.Point(*lonlat), 800, z1pt0=100., z2pt5=1.) s_filter = SourceFilter(SiteCollection([site]), {}) param = dict(ses_per_logic_tree_path=10, filter_distance='rjb', gsims=[SiMidorikawa1999SInter()]) dic = sum(sample_ruptures(group, param, s_filter), {}) self.assertEqual(len(dic['rup_array']), 5) self.assertEqual(len(dic['calc_times']), 15) # mutex sources # test no filtering 1 ruptures = list(stochastic_event_set(group)) self.assertEqual(len(ruptures), 19) # test no filtering 2 ruptures = sum(sample_ruptures(group, param), {})['rup_array'] self.assertEqual(len(ruptures), 5)