def test_nine_positions(self): def v2p(*vectors): # "vectors to points" return [Point(*coords) for coords in zip(*geo_utils.cartesian_to_spherical( numpy.array(vectors, dtype=float) ))] corners = v2p([6370, 0, -0.5], [6370, 0, 0.5], [6369, 2, 0.5], [6369, 2, -0.5]) surface = PlanarSurface(2, 3, *corners) # first three positions: point projection is above the top edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6371, 0, -1.5], [6371, 0, 1.5], [6371, 0, 0.33]) )) aac(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4) # next three positions: point projection is below the bottom edge dists = surface.get_min_distance(Mesh.from_points_list( v2p([6368, 2, -1.5], [6368, 2, 1.5], [6368, 2, -0.45]) )) aac(dists, [2 ** 0.5, 2 ** 0.5, 1.0], atol=1e-4) # next three positions: point projection is left to rectangle, # right to it or lies inside dists = surface.get_min_distance(Mesh.from_points_list( v2p([6369.5, 1, -1.5], [6369.5, 1, 1.5], [6369.5, 1, -0.1]) )) aac(dists, [1, 1, 0], atol=1e-4)
def test_many_points(self): lons = numpy.array([0.7, 0.6, 0.4, 0.6, 0.3, 0.9, 0.5, 0.4]) lats = numpy.array([0.8, 0.5, 0.2, 0.7, 0.2, 0.4, 0.9, 0.4]) mesh = Mesh(lons, lats, None) polygon = mesh.get_convex_hull() elons = [0.4, 0.3, 0.5, 0.7, 0.9] elats = [0.2, 0.2, 0.9, 0.8, 0.4] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_two_points(self): mesh = Mesh(numpy.array([-10., -11.]), numpy.array([-12., -13.]), None) polygon = mesh.get_convex_hull() self.assertIsInstance(polygon, Polygon) elons = [-10.99996704, -11.0000323, -11.00003296, -10.00003295, -9.99996795, -9.99996705] elats = [-13.00003147, -13.00003212, -12.99996853, -11.99996865, -11.99996776, -12.00003135] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_distance_to_2d_mesh(self): lons = numpy.array([[0., 1.], [0., 1.]]) lats = numpy.array([[1., 1.], [0., 0.]]) mesh = Mesh(lons, lats) target_lons = numpy.array([[0.25, 0.75], [0.25, 0.75]]) target_lats = numpy.array([[0.75, 0.75], [0.25, 0.25]]) target_mesh = Mesh(target_lons, target_lats) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = numpy.zeros((2, 2)) numpy.testing.assert_equal(dists, expected_dists)
def test(self): mesh = Mesh(numpy.array([0., 1., 2., 3.]), numpy.zeros(4), None) matrix = mesh.get_distance_matrix() aaae = numpy.testing.assert_array_almost_equal aaae(matrix[0], [[0, 111.2, 222.4, 333.6]], decimal=1) aaae(matrix[1], [[111.2, 0, 111.2, 222.4]], decimal=1) aaae(matrix[2], [[222.4, 111.2, 0, 111.2]], decimal=1) aaae(matrix[3], [[333.6, 222.4, 111.2, 0]], decimal=1) for i in range(4): for j in range(i, 4): self.assertEqual(matrix[i, j], matrix[j, i])
def test8_strike_of_255_degrees(self): corners = [[(0.05, 0.05, 8), (-0.05, -0.05, 8)], [(0.05, 0.05, 9), (-0.05, -0.05, 9)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], -3.9313415355436705, places=4)
def test_left_or_right_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.24, -0.08, 0.55), Point(0.17, 0.07, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1], decimal=5) aae(res.lats, [-0.08, 0.07], decimal=3) aae(res.depths, [0.20679306, 1.69185737])
def test_top_or_bottom_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.04, -0.28, 0), Point(0.033, 0.15, 0)]) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.04, 0.033], decimal=5) aae(res.lats, [-0.1, 0.1], decimal=5) aae(res.depths, [0, 2], decimal=2)
def test_point_outside(self): corners = [Point(0.1, -0.1, 1), Point(-0.1, -0.1, 1), Point(-0.1, 0.1, 2), Point(0.1, 0.1, 2)] surface = PlanarSurface(1, 270, 45, *corners) sites = Mesh.from_points_list( [ Point(-0.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(0.8, 0.01), Point(0.2, -0.15), Point(0.02, -0.12), Point(-0.14, 0), Point(-3, 3), Point(0.05, 0.15, 10), ] ) dists = surface.get_joyner_boore_distance(sites) expected_dists = [ Point(-0.2, -0.2).distance(Point(-0.1, -0.1)), Point(1, 1).distance(Point(0.1, 0.1)), Point(4, 5).distance(Point(0.1, 0.1)), Point(0.8, 0.01).distance(Point(0.1, 0.01)), Point(0.2, -0.15).distance(Point(0.1, -0.1)), Point(0.02, -0.12).distance(Point(0.02, -0.1)), Point(-0.14, 0).distance(Point(-0.1, 0)), Point(-3, 3).distance(Point(-0.1, 0.1)), Point(0.05, 0.15).distance(Point(0.05, 0.1)), ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.05))
def test8_strike_of_45_degrees(self): corners = [Point(-0.05, -0.05, 8), Point(0.05, 0.05, 8), Point(0.05, 0.05, 9), Point(-0.05, -0.05, 9)] surface = PlanarSurface(45, 60, *corners) sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], 3.9313415355436705, places=4)
def test_point_inside(self): corners = [Point(-1, -1, 1), Point(1, -1, 1), Point(1, 1, 2), Point(-1, 1, 2)] surface = PlanarSurface(10, 90, 45, *corners) sites = Mesh.from_points_list([Point(0, 0), Point(0, 0, 20), Point(0.1, 0.3)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_left_or_right_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.24, -0.08, 0.55), Point(0.17, 0.07, 0)]) res = self.surface.get_closest_points(sites) aac(res.lons, [-0.1, 0.1], atol=1E-4) aac(res.lats, [-0.08, 0.07], rtol=5E-2) aac(res.depths, [0.20679306, 1.69185737])
def test4_site_along_strike(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.2, 0), Point(67.6, 0), Point(90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 aac(dists, expected_dists)
def test_one_point(self): mesh = Mesh.from_points_list([Point(7, 7)]) polygon = mesh.get_convex_hull() elons = [7.0000453, 7., 6.9999547, 7] elats = [7., 6.99995503, 7., 7.00004497] numpy.testing.assert_allclose(polygon.lons, elons) numpy.testing.assert_allclose(polygon.lats, elats)
def test_top_or_bottom_edge_is_closest(self): sites = Mesh.from_points_list([Point(-0.04, -0.28, 0), Point(0.033, 0.15, 0)]) res = self.surface.get_closest_points(sites) aac(res.lons, [-0.04, 0.033], atol=1E-4) aac(res.lats, [-0.1, 0.1], atol=1E-4) aac(res.depths, [0, 2], atol=1E-2)
def test_simple(self): lons = numpy.array([numpy.arange(-1, 1.2, 0.2)] * 11) lats = lons.transpose() + 1 depths = lats + 10 mesh = RectangularMesh(lons, lats, depths) check = lambda lon, lat, depth, expected_distance, **kwargs: \ self.assertAlmostEqual( mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(lon, lat, depth)]) )[0], expected_distance, **kwargs ) check(lon=0, lat=0.5, depth=0, expected_distance=0) check(lon=1, lat=1, depth=0, expected_distance=0) check(lon=0.6, lat=-1, depth=0, expected_distance=Point(0.6, -1).distance(Point(0.6, 0)), delta=0.1) check(lon=-0.8, lat=2.1, depth=10, expected_distance=Point(-0.8, 2.1).distance(Point(-0.8, 2)), delta=0.02) check(lon=0.75, lat=2.3, depth=3, expected_distance=Point(0.75, 2.3).distance(Point(0.75, 2)), delta=0.04)
def _get_point_rates(self, source, mmin, mmax=np.inf): """ Adds the rates for a point source :param source: Point source as instance of :class: openquake.hazardlib.source.point.PointSource :param float mmin: Minimum Magnitude :param float mmax: Maximum Magnitude """ src_mesh = Mesh.from_points_list([source.location]) in_poly = self.limits.intersects(src_mesh)[0] if not in_poly: return else: for (mag, rate) in source.get_annual_occurrence_rates(): if (mag < mmin) or (mag > mmax): return else: for (prob, depth) in source.hypocenter_distribution.data: if (depth < self.upper_depth) or (depth > self.lower_depth): continue else: self.rates += prob * rate
def test_point_above_surface(self): sites = Mesh.from_points_list([Point(0, 0), Point(-0.03, 0.05, 0.5)]) res = self.surface.get_closest_points(sites) self.assertIsInstance(res, Mesh) aac(res.lons, [0, -0.03], atol=.001) aac(res.lats, [-0.00081824, 0.04919223], atol=1e-6) aac(res.depths, [1.0113781, 1.50822185], atol=1e-6)
def test_point_on_the_border(self): corners = [Point(0.1, -0.1, 1), Point(-0.1, -0.1, 1), Point(-0.1, 0.1, 2), Point(0.1, 0.1, 2)] surface = PlanarSurface(1, 270, 45, *corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists))
def test5_site_opposite_to_strike_direction(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(-0.2, 0), Point(-67.6, 0), Point(-90.33, 0)]) dists = surface.get_rx_distance(sites) expected_dists = [0] * 3 aac(dists, expected_dists)
def test_point_above_surface_topo(self): sites = Mesh.from_points_list([Point(0, 0, -2), Point(-0.03, 0.05, -1.5)]) res = self.surface_topo.get_closest_points(sites) self.assertIsInstance(res, Mesh) aac(res.lons, [0, -0.03], atol=1E-4) aac(res.lats, [-0.00081824, 0.04919223], atol=1E-4) aac(res.depths, [1.0113781-2., 1.50822185-2.], atol=1E-4)
def test_point_on_the_border(self): corners = [[(0.1, -0.1, 1), (-0.1, -0.1, 1)], [(0.1, 0.1, 2), (-0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(-0.1, 0.04), Point(0.1, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 2 self.assertTrue(numpy.allclose(dists, expected_dists, atol=1e-4))
def test_point_above_surface(self): sites = Mesh.from_points_list([Point(0, 0), Point(-0.03, 0.05, 0.5)]) res = self.surface.get_closest_points(sites) self.assertIsInstance(res, Mesh) aae = numpy.testing.assert_almost_equal aae(res.lons, [0, -0.03], decimal=4) aae(res.lats, [-0.00081824, 0.04919223]) aae(res.depths, [1.0113781, 1.50822185])
def test_from_points_list_no_depth(self): points = [Point(0, 1), Point(2, 3), Point(5, 7)] mesh = Mesh.from_points_list(points) self.assertTrue((mesh.lons == [0, 2, 5]).all()) self.assertTrue((mesh.lats == [1, 3, 7]).all()) self.assertEqual(mesh.lons.dtype, numpy.float) self.assertEqual(mesh.lats.dtype, numpy.float) self.assertIs(mesh.depths, None)
def test_corner_is_closest(self): sites = Mesh.from_points_list( [Point(-0.11, 0.11), Point(0.14, -0.12, 10), Point(0.3, 0.2, 0.5), Point(-0.6, -0.6, 0.3)] ) res = self.surface.get_closest_points(sites) aae = numpy.testing.assert_almost_equal aae(res.lons, [-0.1, 0.1, 0.1, -0.1], decimal=4) aae(res.lats, [0.1, -0.1, 0.1, -0.1]) aae(res.depths, [2, 0, 2, 0], decimal=5)
def test_corner_is_closest(self): sites = Mesh.from_points_list( [Point(-0.11, 0.11), Point(0.14, -0.12, 10), Point(0.3, 0.2, 0.5), Point(-0.6, -0.6, 0.3)] ) res = self.surface.get_closest_points(sites) aac(res.lons, [-0.1, 0.1, 0.1, -0.1], atol=1E-4) aac(res.lats, [0.1, -0.1, 0.1, -0.1], atol=1E-4) aac(res.depths, [2, 0, 2, 0], atol=1E-5)
def test_point_inside(self): corners = [[(-0.1, -0.1, 1), (0.1, -0.1, 1)], [(-0.1, 0.1, 2), (0.1, 0.1, 2)]] surface = DummySurface(corners) sites = Mesh.from_points_list([Point(0, 0), Point(0, 0, 20), Point(0.01, 0.03)]) dists = surface.get_joyner_boore_distance(sites) expected_dists = [0] * 3 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_mesh_of_one_point(self): lons = numpy.array([[1.]]) lats = numpy.array([[0.]]) depths = numpy.array([[1.]]) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(1, 0), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [0, Point(0.5, 0).distance(Point(1, 0))] self.assertTrue(numpy.allclose(dists, expected_dists, atol=0.2))
def test2_sites_at_one_degree_distance(self): surface = self._test_rectangular_surface() sites = Mesh.from_points_list([Point(+1.0, 0.0), Point(+1.0, -1.0), Point(+1.0, 1.0), Point(-1.1, +0.0), Point(-1.1, 1.0), Point(-1.1, -1.0)]) dists = surface.get_ry0_distance(sites) expected_dists = [111.19505230826488, 111.177990689, 111.177990689, 111.19505230826488, 111.177990689, 111.177990689] self.assertTrue(numpy.allclose(dists, expected_dists))
def test_mesh_of_two_points(self): lons = numpy.array([[0, 0.5, 1]], float) lats = numpy.array([[0, 0, 0]], float) depths = numpy.array([[1, 0, 1]], float) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(0.5, 1), Point(0.5, 0)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [Point(0.5, 1).distance(Point(0.5, 0)), 0] numpy.testing.assert_almost_equal(dists, expected_dists)
def test_several_sites(self): surface = DummySurface(_planar_test_data.TEST_7_RUPTURE_2_MESH) sites = Mesh.from_points_list([Point(0, 0), Point(-0.3, 0.4)]) dists = surface.get_min_distance(sites) expected_dists = [7.01186304977, 55.6159556] self.assertTrue(numpy.allclose(dists, expected_dists))
def test_1d(self): mesh = Mesh(numpy.array([1, 2, 3, 5]), numpy.array([-1, -2, 4, 0])) self.assertEqual(len(mesh), 4) mesh = Mesh(numpy.array([1, 2]), numpy.array([0, 0]), numpy.array([10, 10])) self.assertEqual(len(mesh), 2)
class MultiPointSource(ParametricSeismicSource): """ MultiPointSource class, used to describe point sources with different MFDs and the same rupture_mesh_spacing, magnitude_scaling_relationship, rupture_aspect_ratio, temporal_occurrence_model, upper_seismogenic_depth, lower_seismogenic_depth, nodal_plane_distribution, hypocenter_distribution """ code = b'M' MODIFICATIONS = set(()) def __init__( self, source_id, name, tectonic_region_type, mfd, magnitude_scaling_relationship, rupture_aspect_ratio, # point-specific parameters (excluding location) upper_seismogenic_depth, lower_seismogenic_depth, nodal_plane_distribution, hypocenter_distribution, mesh, temporal_occurrence_model=None): assert len(mfd) == len(mesh), (len(mfd), len(mesh)) rupture_mesh_spacing = None super().__init__(source_id, name, tectonic_region_type, mfd, rupture_mesh_spacing, magnitude_scaling_relationship, rupture_aspect_ratio, temporal_occurrence_model) self.upper_seismogenic_depth = upper_seismogenic_depth self.lower_seismogenic_depth = lower_seismogenic_depth self.nodal_plane_distribution = nodal_plane_distribution self.hypocenter_distribution = hypocenter_distribution self.mesh = mesh def __iter__(self): for i, (mfd, point) in enumerate(zip(self.mfd, self.mesh)): name = '%s:%s' % (self.source_id, i) ps = PointSource(name, name, self.tectonic_region_type, mfd, self.rupture_mesh_spacing, self.magnitude_scaling_relationship, get(self.rupture_aspect_ratio, i), self.temporal_occurrence_model, get(self.upper_seismogenic_depth, i), get(self.lower_seismogenic_depth, i), point, self.nodal_plane_distribution, self.hypocenter_distribution) ps.num_ruptures = ps.count_ruptures() ps.scaling_rate = getattr(self, 'scaling_rate', 1) yield ps def __len__(self): return len(self.mfd) def iter_ruptures(self, **kwargs): """ Yield the ruptures of the underlying point sources """ for ps in self: for rupture in ps.iter_ruptures(**kwargs): yield rupture def count_ruptures(self): """ See :meth:`openquake.hazardlib.source.base.BaseSeismicSource.count_ruptures` for description of parameters and return value. """ return (len(self.get_annual_occurrence_rates()) * len(self.nodal_plane_distribution.data) * len(self.hypocenter_distribution.data)) def get_bounding_box(self, maxdist): """ Bounding box containing all the point sources, enlarged by the maximum distance. """ return utils.get_bounding_box([ps.location for ps in self], maxdist) @property def polygon(self): """ The polygon containing all points """ return self.mesh.get_convex_hull() def __toh5__(self): npd = [(prob, np.strike, np.dip, np.rake) for prob, np in self.nodal_plane_distribution.data] hdd = self.hypocenter_distribution.data points = [(p.x, p.y) for p in self.mesh] mfd = self.mfd.kwargs.copy() for k, vals in mfd.items(): if k in ('occurRates', 'magnitudes'): mfd[k] = [numpy.array(lst, F32) for lst in vals] else: mfd[k] = numpy.array(vals, F32) dic = { 'nodal_plane_distribution': numpy.array(npd, npd_dt), 'hypocenter_distribution': numpy.array(hdd, hdd_dt), 'mesh': numpy.array(points, mesh_dt), 'rupture_aspect_ratio': self.rupture_aspect_ratio, 'upper_seismogenic_depth': self.upper_seismogenic_depth, 'lower_seismogenic_depth': self.lower_seismogenic_depth, self.mfd.kind: mfd } attrs = { 'source_id': self.source_id, 'name': self.name, 'magnitude_scaling_relationship': self.magnitude_scaling_relationship.__class__.__name__, 'tectonic_region_type': self.tectonic_region_type } return dic, attrs def __fromh5__(self, dic, attrs): self.source_id = attrs['source_id'] self.name = attrs['name'] self.tectonic_region_type = attrs['tectonic_region_type'] self.magnitude_scaling_relationship = SCALEREL[ attrs['magnitude_scaling_relationship']] npd = dic.pop('nodal_plane_distribution').value hdd = dic.pop('hypocenter_distribution').value mesh = dic.pop('mesh').value self.rupture_aspect_ratio = dic.pop('rupture_aspect_ratio').value self.lower_seismogenic_depth = dic.pop('lower_seismogenic_depth').value self.upper_seismogenic_depth = dic.pop('upper_seismogenic_depth').value [(mfd_kind, mfd)] = dic.items() self.nodal_plane_distribution = PMF([ (prob, NodalPlane(strike, dip, rake)) for prob, strike, dip, rake in npd ]) self.hypocenter_distribution = PMF(hdd) self.mesh = Mesh(mesh['lon'], mesh['lat']) kw = {k: dset.value for k, dset in mfd.items()} kw['size'] = len(mesh) kw['kind'] = mfd_kind self.mfd = MultiMFD(**kw) def wkt(self): """ :returns: the geometry as a wkt string """ return self.mesh.get_convex_hull().wkt
def test_one_point(self): co = numpy.array([0]) mesh = Mesh(co, co, co) self.assertEqual(len(mesh), 1)
def _parse_distance_data(self, event, site, metadata): """ Read in the distance related metadata and return an instance of the :class: smtk.sm_database.RecordDistance """ # Compute various distance metrics # Add calculation of Repi, Rhypo from event and station localizations (latitudes, longitudes, depth, elevation)? target_site = Mesh(np.array([site.longitude]), np.array([site.latitude]), np.array([0.0])) # Warning ratio fixed to 1.5 ratio=1.5 surface_modeled = rcfg.create_planar_surface( Point(event.longitude, event.latitude, event.depth), event.mechanism.nodal_planes.nodal_plane_1['strike'], event.mechanism.nodal_planes.nodal_plane_1['dip'], event.rupture.area, ratio) hypocenter = rcfg.get_hypocentre_on_planar_surface( surface_modeled, event.rupture.hypo_loc) # Rhypo Rhypo = get_float(metadata["HypD (km)"]) if Rhypo is None: Rhypo = hypocenter.distance_to_mesh(target_site) # Repi Repi = get_float(metadata["EpiD (km)"]) if Repi is None: Repi= hypocenter.distance_to_mesh(target_site, with_depths=False) # Rrup Rrup = get_float(metadata["Campbell R Dist. (km)"]) if Rrup is None: Rrup = surface_modeled.get_min_distance(target_site) # Rjb Rjb = get_float(metadata["Joyner-Boore Dist. (km)"]) if Rjb is None: Rjb = surface_modeled.get_joyner_boore_distance(target_site) # Need to check if Rx and Ry0 are consistant with the other metrics # when those are coming from the flatfile? # Rx Rx = surface_modeled.get_rx_distance(target_site) # Ry0 Ry0 = surface_modeled.get_ry0_distance(target_site) distance = RecordDistance( repi = float(Repi), rhypo = float(Rhypo), rjb = float(Rjb), rrup = float(Rrup), r_x = float(Rx), ry0 = float(Ry0)) distance.azimuth = get_float(metadata["Source to Site Azimuth (deg)"]) distance.hanging_wall = get_float(metadata["FW/HW Indicator"]) # distance = RecordDistance( # get_float(metadata["EpiD (km)"]), # get_float(metadata["HypD (km)"]), # get_float(metadata["Joyner-Boore Dist. (km)"]), # get_float(metadata["Campbell R Dist. (km)"])) # distance.azimuth = get_float(metadata["Source to Site Azimuth (deg)"]) # distance.hanging_wall = get_float(metadata["FW/HW Indicator"]) return distance
def test_4(self): surface = PlanarSurface(1, 2, 3, *test_data.TEST_7_RUPTURE_2_CORNERS) sites = Mesh.from_points_list([Point(-0.3, 0.4)]) self.assertAlmostEqual(55.6159556, surface.get_min_distance(sites)[0], delta=0.6)
def test6_one_degree_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -1), Point(20, 1)]) dists = surface.get_rx_distance(sites) expected_dists = [111.19505230826488, -111.19505230826488] self.assertTrue(numpy.allclose(dists, expected_dists))
def test7_ten_degrees_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0, -10), Point(-15, 10)]) dists = surface.get_rx_distance(sites) expected_dists = [1111.9505230826488, -1111.9505230826488] self.assertTrue(numpy.allclose(dists, expected_dists))
def test_2(self): surface = PlanarSurface(1, 2, 3, *test_data.TEST_7_RUPTURE_6_CORNERS) sites = Mesh.from_points_list([Point(-0.25, 0.25)]) self.assertAlmostEqual(40.1213468, surface.get_min_distance(sites)[0], places=1)
def test6_one_degree_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -1), Point(20, 1)]) dists = surface.get_rx_distance(sites) expected_dists = [-111.19505230826488, +111.19505230826488] numpy.testing.assert_allclose(dists, expected_dists, rtol=.01)
def test3_site_on_centroid(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, 0)]) self.assertAlmostEqual(surface.get_rx_distance(sites)[0], 0)
def test_mesh_and_point_on_surface(self): self._test(Mesh.from_points_list( [Point(0, 0), Point(0, 1), Point(0, 2)]), Mesh.from_points_list([Point(1, 1), Point(-1, 0)]), expected_distance_indices=[1, 0])
def mesh(self): """Return a mesh with the given lons and lats""" return Mesh(self.lons, self.lats, depths=None)
def mesh(self): """Return a mesh with the given lons, lats, and depths""" return Mesh(self.lons, self.lats, self.depths)
def test2_site_on_the_hanging_wall(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -0.05), Point(-140, -0.05)]) dists = surface.get_rx_distance(sites) expected_dists = [5.559752615413244] * 2 self.assertTrue(numpy.allclose(dists, expected_dists))
def test_3(self): surface = PlanarSurface(1, 2, 3, *test_data.TEST_7_RUPTURE_2_CORNERS) sites = Mesh.from_points_list([Point(0, 0)]) self.assertAlmostEqual(7.01186304977, surface.get_min_distance(sites)[0], places=2)
def test_point_on_surface(self): self._test(Mesh.from_points_list( [Point(0, 0, 1), Point(0, 1, 2), Point(0, 2, 3)]), Mesh.from_points_list([Point(0.5, 1.5)]), expected_distance_indices=[1])
def mesh(self): """Return a mesh with the given lons, lats, and depths""" return Mesh(self['lon'], self['lat'], self['depth'])
def test_zeroes(self): mesh = Mesh(numpy.zeros(1000), numpy.zeros(1000), None) matrix = mesh.get_distance_matrix() self.assertIsInstance(matrix, numpy.matrix) self.assertEqual(matrix.shape, (1000, 1000)) self.assertTrue((matrix == 0).all())
def test1_site_on_the_edges(self): surface = self._test_rectangular_surface() sites = Mesh.from_points_list([Point(0.0, 0.05), Point(0.0, -0.05)]) dists = surface.get_ry0_distance(sites) expected_dists = [0, 0] numpy.testing.assert_allclose(dists, expected_dists)
def test_4(self): surface = FakeSurface(_planar_test_data.TEST_7_RUPTURE_2_MESH) sites = Mesh.from_points_list([Point(-0.3, 0.4)]) self.assertAlmostEqual(55.58568426746, surface.get_min_distance(sites)[0], places=4)
def test_from_points_list_with_depth(self): points = [Point(0, 1, 2), Point(2, 3, 4), Point(5, 7, 10)] mesh = Mesh.from_points_list(points) self.assertTrue((mesh.depths == [2, 4, 10]).all()) self.assertEqual(mesh.depths.dtype, numpy.float)
def test_several_sites(self): surface = FakeSurface(_planar_test_data.TEST_7_RUPTURE_2_MESH) sites = Mesh.from_points_list([Point(0, 0), Point(-0.3, 0.4)]) dists = surface.get_min_distance(sites) expected_dists = [7.01186301, 55.58568427] numpy.testing.assert_allclose(dists, expected_dists)
def test_mesh_on_surface(self): self._test(Mesh.from_points_list( [Point(0, 0), Point(0, 1), Point(0, 2)]), Mesh.from_points_list([Point(-1, -1, 3.4), Point(2, 5)]), expected_distance_indices=[0, 2])
def test7_ten_degrees_distance(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0, -10), Point(-15, 10)]) dists = surface.get_rx_distance(sites) expected_dists = [-1111.9505230826488, +1111.9505230826488] numpy.testing.assert_allclose(dists, expected_dists, rtol=.01)
def main(cfg_file): startTime = datetime.now() cfg = configparser.ConfigParser() cfg.read(cfg_file) (oq_param, source_model_file, matrixMagsMin, matrixMagsMax, matrixMagsStep, matrixDistsMin, matrixDistsMax, matrixDistsStep, limitIM, imt_filtering, trunc_level, im_filter, gmf_file, gmf_file_gmpe_rate, rup_mesh_spac, complex_mesh_spac, mfd_bin, area_discre, limit_max_mag, limit_min_mag) = read_config_file(cfg) # Set up the source model configuration conv1 = SourceConverter(1.0, # Investigation time rup_mesh_spac, # Rupture mesh spacing complex_fault_mesh_spacing=complex_mesh_spac, width_of_mfd_bin=mfd_bin, area_source_discretization=area_discre) # Parse the source Model if source_model_file: # only one source model file source_model = to_python(source_model_file, conv1) else: # source model has many files (in this case 2 - adapt for more) source_model_file2 = "demo_data/SA_RA_CATAL1_05.xml" source_model2 = to_python(source_model_file2, conv1) source_model = source_model+source_model2 # Calculate total number of ruptures in the erf # num_rup = 0 # rate_rup = [] # for a in range(len(source_model)): # model_trt = source_model[a] # for b in range(len(model_trt)): # num_rup = num_rup + len(list(model_trt[b].iter_ruptures())) # for rup in model_trt[b].iter_ruptures(): # rate_rup.append(rup.occurrence_rate) # print(num_rup) # print(sum(rate_rup)) # print(rate_rup[0:10]) # If exposure model is provided: haz_sitecol = get_site_collection(oq_param) sites, assets_by_site, _ = get_sitecol_assetcol(oq_param, haz_sitecol) # print(list(sites)[0:10]) # np.savetxt('sites.csv',list(zip(sites.lons, sites.lats))) # If region coordinates are provided: # sites = get_site_collection(oq_param) gsimlt = get_gsim_lt(oq_param) gsim_list = [br.uncertainty for br in gsimlt.branches] GMPEmatrix = build_gmpe_table(matrixMagsMin, matrixMagsMax, matrixMagsStep, matrixDistsMin, matrixDistsMax, matrixDistsStep, imt_filtering, limitIM, gsim_list, limit_max_mag, limit_min_mag) # Calculate minimum distance between rupture and assets # Import exposure from .ini file depths = np.zeros(len(sites)) exposureCoords = Mesh(sites.lons, sites.lats, depths) # To calculate Joyner Boore distance: exposurePoints = (exposureCoords, exposureCoords) recMeshExposure = RectangularMesh.from_points_list(exposurePoints) imts = ['PGA', 'SA(0.3)'] cmake = ContextMaker(gsim_list) filter1 = SourceFilter(sites, oq_param.maximum_distance) if im_filter == 'True': # Here we consider the IM and the MaxDist filter gmfs_median = calculate_gmfs_filter(source_model, gsimlt, filter1, cmake, gsim_list, recMeshExposure, matrixMagsMin, matrixMagsStep, matrixDistsMin, matrixDistsStep, GMPEmatrix, imts, trunc_level) else: # No IM filter, just the MAxDist filter gmfs_median = calc_gmfs_no_IM_filter(source_model, imts, gsim_list, trunc_level, gsimlt, filter1, cmake) print("%s Ground Motion Fields" % len(gmfs_median)) save_gmfs(gmf_file, gmf_file_gmpe_rate, gmfs_median, exposureCoords, gsim_list, imts) print(datetime.now() - startTime)
def test_2(self): surface = FakeSurface(_planar_test_data.TEST_7_RUPTURE_6_MESH) sites = Mesh.from_points_list([Point(-0.25, 0.25)]) self.assertAlmostEqual(40.09707543926, surface.get_min_distance(sites)[0], places=4)
def test_3(self): surface = FakeSurface(_planar_test_data.TEST_7_RUPTURE_2_MESH) sites = Mesh.from_points_list([Point(0, 0)]) self.assertAlmostEqual(7.01186304977, surface.get_min_distance(sites)[0])
def test2_site_on_the_foot_wall(self): surface = self._test1to7surface() sites = Mesh.from_points_list([Point(0.05, -0.05), Point(-140, -0.05)]) dists = surface.get_rx_distance(sites) expected_dists = [-5.559752615413244] * 2 numpy.testing.assert_allclose(dists, expected_dists, rtol=.01)
def test_1(self): surface = DummySurface(_planar_test_data.TEST_7_RUPTURE_6_MESH) sites = Mesh.from_points_list([Point(0, 0)]) self.assertAlmostEqual(8.01185807319, surface.get_min_distance(sites)[0])