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(1, 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]) )) self.assertTrue(numpy.allclose(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]) )) self.assertTrue(numpy.allclose(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]) )) self.assertTrue(numpy.allclose(dists, [1, 1, 0], atol=1e-4))
def _test(self, mesh_points, target_points, expected_distance_indexes): mesh = Mesh.from_points_list(mesh_points) target_mesh = Mesh.from_points_list(target_points) dists = mesh.get_min_distance(target_mesh) expected_dists = [mesh_points[mi].distance(target_points[ti]) for ti, mi in enumerate(expected_distance_indexes)] self.assertEqual(list(dists), expected_dists)
def test_mesh_and_point_not_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, 1.5, 3), Point(0, 1.5, 0.9)]), expected_distance_indices=[2, 1])
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 self.assertTrue(numpy.allclose(dists, expected_dists))
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(1, 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 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 self.assertTrue(numpy.allclose(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_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 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_inside(self): corners = [[(-1, -1, 1), (1, -1, 1)], [(-1, 1, 2), (1, 1, 2)]] surface = DummySurface(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) 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_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 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 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_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_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_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_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_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 _test(self, points, site, expected_distance): lons, lats, depths = numpy.array(points).transpose() lons = lons.transpose() lats = lats.transpose() depths = depths.transpose() mesh = RectangularMesh(lons, lats, depths) distance = mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(*site)]))[0] self.assertAlmostEqual(distance, expected_distance, delta=0.02)
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 _test(self, points, site, expected_distance): lons, lats, depths = numpy.array(points).transpose() lons = lons.transpose() lats = lats.transpose() depths = depths.transpose() mesh = RectangularMesh(lons, lats, depths) distance = mesh.get_joyner_boore_distance( Mesh.from_points_list([Point(*site)]) )[0] self.assertAlmostEqual(distance, expected_distance, delta=0.02)
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_vertical_mesh(self): lons = numpy.array([[0, 1, 2], [0, 1, 2]]) lats = numpy.array([[0, 0, 0], [0, 0, 0]]) depths = numpy.array([[1, 1, 1], [2, 2, 2]]) mesh = RectangularMesh(lons, lats, depths) target_mesh = Mesh.from_points_list([Point(0.5, 0), Point(0.5, 1), Point(0.5, 5)]) dists = mesh.get_joyner_boore_distance(target_mesh) expected_dists = [ 0, Point(0.5, 1).distance(Point(0.5, 0)), Point(0.5, 5).distance(Point(0.5, 0)) ] self.assertTrue(numpy.allclose(dists, expected_dists, atol=3))
def test_point_outside(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.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(8, 10.4), 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(8, 10.4).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.2))
def test_point_outside(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.2, -0.2), Point(1, 1, 1), Point(4, 5), Point(8, 10.4), 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(8, 10.4).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.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 test_several_sites(self): surface = DummySurface(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_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 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_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 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 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_4(self): surface = DummySurface(test_data.TEST_7_RUPTURE_2_MESH) sites = Mesh.from_points_list([Point(-0.3, 0.4)]) self.assertAlmostEqual(55.6159556, surface.get_min_distance(sites)[0], places=4)
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_1(self): surface = DummySurface(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])
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 test_2(self): surface = DummySurface(test_data.TEST_7_RUPTURE_6_MESH) sites = Mesh.from_points_list([Point(-0.25, 0.25)]) self.assertAlmostEqual(40.1213468, 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_3(self): surface = DummySurface(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])