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_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))
Example #4
0
    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))
Example #5
0
 def test(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, 10.0), Point(0.0, 0.0, 10.0)]
     surface = PlanarSurface(1, 45, 90, *corners)
     self.assertTrue(
         Point(0.0, 0.044966, 5.0) == surface.get_middle_point()
     )
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
    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)
Example #9
0
 def test_inclined_surface(self):
     corners = [Point(-0.00317958, -0.00449661, 4.64644661),
                Point(-0.00317958, 0.00449661, 4.64644661),
                Point(0.00317958, 0.00449661, 5.35355339),
                Point(0.00317958, -0.00449661, 5.35355339)]
     surface = PlanarSurface(1, 0.0, 45.0, *corners)
     self.assertAlmostEqual(surface.get_width(), 1.0, places=3)
Example #10
0
 def test_inclined_surface(self):
     corners = [Point(-0.00317958, -0.00449661, 4.64644661),
                Point(-0.00317958, 0.00449661, 4.64644661),
                Point(0.00317958, 0.00449661, 5.35355339),
                Point(0.00317958, -0.00449661, 5.35355339)]
     surface = PlanarSurface(0.0, 45.0, *corners)
     self.assertAlmostEqual(surface.get_width(), 1.0, places=3)
Example #11
0
 def test(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, 10.0), Point(0.0, 0.0, 10.0)]
     surface = PlanarSurface(1, 45, 90, *corners)
     self.assertTrue(
         Point(0.0, 0.044966, 5.0) == surface.get_middle_point()
     )
Example #12
0
 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 make_non_parametric_source():
    surf1 = PlanarSurface(
        mesh_spacing=2., strike=0, dip=90,
        top_left=Point(0., -1., 0.), top_right=Point(0., 1., 0.),
        bottom_right=Point(0., 1., 10.), bottom_left=Point(0., -1., 10.)
    )
    surf2 = PlanarSurface(
        mesh_spacing=2., strike=90., dip=90.,
        top_left=Point(-1., 0., 0.), top_right=Point(1., 0., 0.),
        bottom_right=Point(1., 0., 10.), bottom_left=Point(-1., 0., 10.)
    )
    rup1 = Rupture(
        mag=5., rake=90., tectonic_region_type='ASC',
        hypocenter=Point(0., 0., 5.), surface=surf1, source_typology=None
    )
    rup2 = Rupture(
        mag=6, rake=0, tectonic_region_type='ASC',
        hypocenter=Point(0., 0., 5.), surface=surf2, source_typology=None
    )
    pmf1 = PMF([(Decimal('0.7'), 0), (Decimal('0.3'), 1)])
    pmf2 = PMF([(Decimal('0.7'), 0), (Decimal('0.2'), 1), (Decimal('0.1'), 2)])
    kwargs = {
        'source_id': 'source_id', 'name': 'source name',
        'tectonic_region_type': 'tectonic region',
        'data': [(rup1, pmf1), (rup2, pmf2)]
    }
    npss = NonParametricSeismicSource(**kwargs)
    assert_pickleable(npss)
    return npss, kwargs
Example #14
0
 def test_topo(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, -8.0), Point(0.0, 0.0, -8.0)]
     surface = PlanarSurface(1, 45, 90, *corners)
     midpoint = surface.get_middle_point()
     assert_aeq(midpoint.longitude, 0.0, decimal=5)
     assert_aeq(midpoint.latitude, 0.044966, decimal=5)
     assert_aeq(midpoint.depth, -4.0, decimal=5)
Example #15
0
 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))
Example #16
0
 def test_topo(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, -8.0), Point(0.0, 0.0, -8.0)]
     surface = PlanarSurface(45, 90, *corners)
     midpoint = surface.get_middle_point()
     aac(midpoint.longitude, 0.0, atol=1E-4)
     aac(midpoint.latitude, 0.044966, atol=1E-4)
     aac(midpoint.depth, -4.0, atol=1E-4)
Example #17
0
 def test_topo(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, -8.0), Point(0.0, 0.0, -8.0)]
     surface = PlanarSurface(45, 90, *corners)
     midpoint = surface.get_middle_point()
     aac(midpoint.longitude, 0.0, atol=1E-4)
     aac(midpoint.latitude, 0.044966, atol=1E-4)
     aac(midpoint.depth, -4.0, atol=1E-4)
Example #18
0
 def test(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)
     self.assertEqual(surface.get_top_edge_depth(), 8)
Example #19
0
 def test_vertical_surface(self):
     corners = [
         Point(-0.05, -0.05, 8),
         Point(0.05, 0.05, 8),
         Point(0.05, 0.05, 10),
         Point(-0.05, -0.05, 10)
     ]
     surface = PlanarSurface(1, 45, 60, *corners)
     self.assertAlmostEqual(surface.get_width(), 2.0, places=4)
Example #20
0
 def test_distance_to_2d_mesh(self):
     corners = [Point(0.0, 1.0), Point(1.0, 1.0),
                Point(1.0, 0.114341), Point(0.0, 0.114341)]
     surface = PlanarSurface(90.0, 10.0, *corners)
     sites = Mesh(numpy.array([[0.25, 0.75], [0.25, 0.75]]),
                  numpy.array([[0.75, 0.75], [0.25, 0.25]]),
                  None)
     dists = surface.get_joyner_boore_distance(sites)
     numpy.testing.assert_equal(dists, numpy.zeros((2, 2)))
Example #21
0
 def test(self):
     corners = [
         Point(0.0, 0.0, 0.0),
         Point(0.0, 0.089932, 0.0),
         Point(0.0, 0.089932, 10.0),
         Point(0.0, 0.0, 10.0)
     ]
     surface = PlanarSurface(1, 45, 90, *corners)
     self.assertAlmostEqual(surface.get_area(), 100.0, places=0)
Example #22
0
 def test_distance_to_2d_mesh(self):
     corners = [Point(0.0, 1.0), Point(1.0, 1.0),
                Point(1.0, 0.114341), Point(0.0, 0.114341)]
     surface = PlanarSurface(1, 90.0, 10.0, *corners)
     sites = Mesh(numpy.array([[0.25, 0.75], [0.25, 0.75]]),
                  numpy.array([[0.75, 0.75], [0.25, 0.25]]),
                  None)
     dists = surface.get_joyner_boore_distance(sites)
     numpy.testing.assert_equal(dists, numpy.zeros((2, 2)))
Example #23
0
 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))
Example #24
0
class PlanarSurfaceGetClosestPointsTestCase(unittest.TestCase):
    corners = [Point(-0.1, -0.1, 0), Point(0.1, -0.1, 0),
               Point(0.1, 0.1, 2), Point(-0.1, 0.1, 2)]
    surface = PlanarSurface(90, 45, *corners)

    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_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_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_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_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)
        aae = numpy.testing.assert_almost_equal
        aae(res.lons, [0, -0.03], decimal=5)
        aae(res.lats, [-0.00081824,  0.04919223], decimal=5)
        aae(res.depths, [1.0113781-2., 1.50822185-2.], decimal=5)

    corners_topo = [Point(-0.1, -0.1, -2), Point(0.1, -0.1, -2),
                    Point(0.1, 0.1, 0), Point(-0.1, 0.1, 0)]
    surface_topo = PlanarSurface(90, 45, *corners_topo)
Example #25
0
 def test(self):
     corners = [Point(-0.00317958, -0.00449661, 4.64644661),
                Point(-0.00317958, 0.00449661, 4.64644661),
                Point(0.00317958, 0.00449661, 5.35355339),
                Point(0.00317958, -0.00449661, 5.35355339)]
     surface = PlanarSurface(0.0, 45.0, *corners)
     west, east, north, south = surface.get_bounding_box()
     self.assertEqual(-0.00317958, west)
     self.assertEqual(0.00317958, east)
     self.assertEqual(0.00449661, north)
     self.assertEqual(-0.00449661, south)
Example #26
0
 def test(self):
     corners = [Point(-0.00317958, -0.00449661, 4.64644661),
                Point(-0.00317958, 0.00449661, 4.64644661),
                Point(0.00317958, 0.00449661, 5.35355339),
                Point(0.00317958, -0.00449661, 5.35355339)]
     surface = PlanarSurface(1, 0.0, 45.0, *corners)
     west, east, north, south = surface.get_bounding_box()
     self.assertEqual(-0.00317958, west)
     self.assertEqual(0.00317958, east)
     self.assertEqual(0.00449661, north)
     self.assertEqual(-0.00449661, south)
Example #27
0
 def test2(self):
     surface = PlanarSurface(10, 20, 30, Point(3.9, 2.2, 10),
                             Point(4.90402718, 3.19634248, 10),
                             Point(5.9, 2.2, 90),
                             Point(4.89746275, 1.20365263, 90))
     plons, plats, pdepths = [[4., 4.3, 3.1], [1.5, 1.7, 3.5],
                              [11., 12., 13.]]
     dists, xx, yy = surface._project(plons, plats, pdepths)
     lons, lats, depths = surface._project_back(dists, xx, yy)
     aaae = numpy.testing.assert_array_almost_equal
     aaae(lons, plons)
     aaae(lats, plats)
     aaae(depths, pdepths)
Example #28
0
 def test2(self):
     surface = PlanarSurface(
         20, 30,
         Point(3.9, 2.2, 10), Point(4.90402718, 3.19634248, 10),
         Point(5.9, 2.2, 90), Point(4.89746275, 1.20365263, 90))
     plons, plats, pdepths = [[4., 4.3, 3.1], [1.5, 1.7, 3.5],
                              [11., 12., 13.]]
     xyz = geo_utils.spherical_to_cartesian(plons, plats, pdepths)
     dists, xx, yy = surface._project(xyz)
     lons, lats, depths = surface._project_back(dists, xx, yy)
     aaae = numpy.testing.assert_array_almost_equal
     aaae(lons, plons)
     aaae(lats, plats)
     aaae(depths, pdepths)
Example #29
0
 def assert_successfull_creation(self, strike, dip, tl, tr, br, bl):
     surface1 = PlanarSurface(strike, dip, tl, tr, br, bl)
     translated = surface1.translate(tl, tr).translate(tr, tl)
     for surface in [surface1, translated]:
         self.assertIsInstance(surface, PlanarSurface)
         self.assertEqual(surface.top_left, tl)
         self.assertEqual(surface.top_right, tr)
         self.assertEqual(surface.bottom_left, bl)
         self.assertEqual(surface.bottom_right, br)
         self.assertEqual(surface.strike, strike)
         self.assertEqual(surface.get_strike(), strike)
         self.assertEqual(surface.dip, dip)
         self.assertEqual(surface.get_dip(), dip)
         self.assertAlmostEqual(surface.length, tl.distance(tr), delta=0.2)
         self.assertAlmostEqual(surface.width, tl.distance(bl), delta=0.2)
Example #30
0
 def assert_successfull_creation(self, strike, dip, tl, tr, br, bl):
     surface1 = PlanarSurface(strike, dip, tl, tr, br, bl)
     translated = surface1.translate(tl, tr).translate(tr, tl)
     for surface in [surface1, translated]:
         self.assertIsInstance(surface, PlanarSurface)
         self.assertEqual(surface.top_left, tl)
         self.assertEqual(surface.top_right, tr)
         self.assertEqual(surface.bottom_left, bl)
         self.assertEqual(surface.bottom_right, br)
         self.assertEqual(surface.strike, strike)
         self.assertEqual(surface.get_strike(), strike)
         self.assertEqual(surface.dip, dip)
         self.assertEqual(surface.get_dip(), dip)
         self.assertAlmostEqual(surface.length, tl.distance(tr), delta=0.2)
         self.assertAlmostEqual(surface.width, tl.distance(bl), delta=0.2)
Example #31
0
def make_rupture(rupture_class, **kwargs):
    default_arguments = {
        'mag':
        5.5,
        'rake':
        123.45,
        'tectonic_region_type':
        const.TRT.STABLE_CONTINENTAL,
        'hypocenter':
        Point(5, 6, 7),
        'surface':
        PlanarSurface(10, 11, 12, Point(0, 0, 1), Point(1, 0, 1),
                      Point(1, 0, 2), Point(0, 0, 2)),
        'source_typology':
        object()
    }
    default_arguments.update(kwargs)
    kwargs = default_arguments
    rupture = rupture_class(**kwargs)
    for key in kwargs:
        if key != 'pmf':
            # for pmf .pmf is a numpy array whereas pmf is a PMF instance
            assert getattr(rupture, key) is kwargs[key], (getattr(
                rupture, key), kwargs[key])
    return rupture
Example #32
0
    def test_against_mesh_to_mesh(self):
        corners = [Point(2.6, 3.7, 20), Point(2.90102155, 3.99961567, 20),
                   Point(3.2, 3.7, 75), Point(2.89905849, 3.40038407, 75)]
        surface = PlanarSurface(0.5, 45, 70, *corners)
        lons, lats = numpy.meshgrid(numpy.linspace(2.2, 3.6, 7),
                                    numpy.linspace(3.4, 4.2, 7))
        sites = Mesh(lons, lats, depths=None)

        res1 = surface.get_closest_points(sites)
        res2 = super(PlanarSurface, surface).get_closest_points(sites)

        aae = numpy.testing.assert_almost_equal
        # precision up to ~1 km
        aae(res1.lons, res2.lons, decimal=2)
        aae(res1.lats, res2.lats, decimal=2)
        aae(res1.depths, res2.depths, decimal=0)
Example #33
0
    def test_against_mesh_to_mesh(self):
        corners = [Point(2.6, 3.7, 20), Point(2.90102155, 3.99961567, 20),
                   Point(3.2, 3.7, 75), Point(2.89905849, 3.40038407, 75)]
        surface = PlanarSurface(0.5, 45, 70, *corners)
        lons, lats = numpy.meshgrid(numpy.linspace(2.2, 3.6, 7),
                                    numpy.linspace(3.4, 4.2, 7))
        sites = Mesh(lons, lats, depths=None)

        res1 = surface.get_closest_points(sites)
        res2 = super(PlanarSurface, surface).get_closest_points(sites)

        aae = numpy.testing.assert_almost_equal
        # precision up to ~1 km
        aae(res1.lons, res2.lons, decimal=2)
        aae(res1.lats, res2.lats, decimal=2)
        aae(res1.depths, res2.depths, decimal=0)
Example #34
0
    def setUpClass(self):
        cfg = helpers.get_data_path('simple_fault_demo_hazard/job.ini')
        job = helpers.get_job(cfg)
        output = models.Output.objects.create(oq_job=job,
                                              display_name='test',
                                              output_type='ses')
        ses_coll = models.SESCollection.create(output=output)
        self.mesh_lons = numpy.array([0.1 * x for x in range(16)]).reshape(
            (4, 4))
        self.mesh_lats = numpy.array([0.2 * x for x in range(16)]).reshape(
            (4, 4))
        self.mesh_depths = numpy.array([0.3 * x for x in range(16)]).reshape(
            (4, 4))

        sfs = SimpleFaultSurface(
            Mesh(self.mesh_lons, self.mesh_lats, self.mesh_depths))

        ps = PlanarSurface(10, 20, 30, Point(3.9, 2.2, 10),
                           Point(4.90402718, 3.19634248, 10),
                           Point(5.9, 2.2, 90),
                           Point(4.89746275, 1.20365263, 90))
        self.fault_rupture = models.ProbabilisticRupture.objects.create(
            ses_collection=ses_coll,
            magnitude=5,
            rake=0,
            surface=sfs,
            is_from_fault_source=True,
            is_multi_surface=False)
        self.source_rupture = models.ProbabilisticRupture.objects.create(
            ses_collection=ses_coll,
            magnitude=5,
            rake=0,
            surface=ps,
            is_from_fault_source=False,
            is_multi_surface=False)
Example #35
0
class PlanarSurfaceGetClosestPointsTestCase(unittest.TestCase):
    corners = [Point(-0.1, -0.1, 0), Point(0.1, -0.1, 0),
               Point(0.1, 0.1, 2), Point(-0.1, 0.1, 2)]
    surface = PlanarSurface(10, 90, 45, *corners)

    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_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_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_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_against_mesh_to_mesh(self):
        corners = [Point(2.6, 3.7, 20), Point(2.90102155, 3.99961567, 20),
                   Point(3.2, 3.7, 75), Point(2.89905849, 3.40038407, 75)]
        surface = PlanarSurface(0.5, 45, 70, *corners)
        lons, lats = numpy.meshgrid(numpy.linspace(2.2, 3.6, 7),
                                    numpy.linspace(3.4, 4.2, 7))
        sites = Mesh(lons, lats, depths=None)

        res1 = surface.get_closest_points(sites)
        res2 = super(PlanarSurface, surface).get_closest_points(sites)

        aae = numpy.testing.assert_almost_equal
        # precision up to ~1 km
        aae(res1.lons, res2.lons, decimal=2)
        aae(res1.lats, res2.lats, decimal=2)
        aae(res1.depths, res2.depths, decimal=0)
Example #36
0
 def test2(self):
     surface = PlanarSurface(
         10,
         20,
         30,
         Point(3.9, 2.2, 10),
         Point(4.90402718, 3.19634248, 10),
         Point(5.9, 2.2, 90),
         Point(4.89746275, 1.20365263, 90),
     )
     plons, plats, pdepths = [[4.0, 4.3, 3.1], [1.5, 1.7, 3.5], [11.0, 12.0, 13.0]]
     dists, xx, yy = surface._project(plons, plats, pdepths)
     lons, lats, depths = surface._project_back(dists, xx, yy)
     aaae = numpy.testing.assert_array_almost_equal
     aaae(lons, plons)
     aaae(lats, plats)
     aaae(depths, pdepths)
Example #37
0
 def _test1to7surface(self):
     corners = [
         Point(0, 0, 8),
         Point(-0.1, 0, 8),
         Point(-0.1, 0, 9),
         Point(0, 0, 9)
     ]
     surface = PlanarSurface(1, 270, 90, *corners)
     return surface
Example #38
0
 def test_vertical_surf_from_corner_points(self):
     # vertical surface pointing North
     surf = PlanarSurface.from_corner_points(
         Point(0, 0, 0), Point(0, 1, 0), Point(0, 1, 5), Point(0, 0, 5))
     self.assertAlmostEqual(surf.strike, 0.)
     self.assertAlmostEqual(surf.dip, 90.)
     self.assertEqual(surf.top_left, Point(0, 0, 0))
     self.assertEqual(surf.top_right, Point(0, 1, 0))
     self.assertEqual(surf.bottom_left, Point(0, 0, 5))
     self.assertEqual(surf.bottom_right, Point(0, 1, 5))
Example #39
0
 def test_vertical_surf_from_corner_points(self):
     # vertical surface pointing North
     surf = PlanarSurface.from_corner_points(Point(0, 0, 0), Point(0, 1, 0),
                                             Point(0, 1, 5), Point(0, 0, 5))
     self.assertAlmostEqual(surf.strike, 0.)
     self.assertAlmostEqual(surf.dip, 90.)
     self.assertEqual(surf.top_left, Point(0, 0, 0))
     self.assertEqual(surf.top_right, Point(0, 1, 0))
     self.assertEqual(surf.bottom_left, Point(0, 0, 5))
     self.assertEqual(surf.bottom_right, Point(0, 1, 5))
Example #40
0
    def test1(self):
        lons, lats, depths = geo_utils.cartesian_to_spherical(
            numpy.array([[60, -10, -10], [60, -10, 10],
                         [60, 10, 10], [60, 10, -10]], float)
        )
        surface = PlanarSurface(20, 30, *Mesh(lons, lats, depths))
        aaae = numpy.testing.assert_array_almost_equal

        xyz = numpy.array([[60, -10, -10], [59, 0, 0], [70, -11, -10]])
        plons, plats, pdepths = geo_utils.cartesian_to_spherical(xyz)

        dists, xx, yy = surface._project(xyz)
        aaae(xx, [0, 10, 0])
        aaae(yy, [0, 10, -1])
        aaae(dists, [0, 1, -10])

        lons, lats, depths = surface._project_back(dists, xx, yy)
        aaae(lons, plons)
        aaae(lats, plats)
        aaae(depths, pdepths)
Example #41
0
 def test_inclined_surf_from_corner_points_topo(self):
     # inclined surface (dip = 45) with 45 degrees strike
     surf = PlanarSurface.from_corner_points(
         Point(0, 0, -1), Point(0.5, 0.5, -1),
         Point(0.563593, 0.436408, 9.), Point(0.063592, -0.063592, 9))
     self.assertAlmostEqual(surf.strike, 45., delta=0.1)
     self.assertAlmostEqual(surf.dip, 45., delta=0.1)
     self.assertEqual(surf.top_left, Point(0, 0, -1))
     self.assertEqual(surf.top_right, Point(0.5, 0.5, -1))
     self.assertEqual(surf.bottom_left, Point(0.063592, -0.063592, 9))
     self.assertEqual(surf.bottom_right, Point(0.563593, 0.436408, 9.))
Example #42
0
 def test_inclined_surf_from_hypocenter(self):
     # inclined surface (dip = 45) with 45 degrees strike
     surf = PlanarSurface.from_hypocenter(Point(0, 0, 20), WC1994(), 7, 1,
                                          45, 45, 0)
     self.assertAlmostEqual(surf.strike, 45., delta=0.1)
     self.assertAlmostEqual(surf.dip, 45., delta=0.1)
     self.assertEqual(surf.top_left, Point(-0.149496, -0.025650, 10.2623))
     self.assertEqual(surf.top_right, Point(0.025650, 0.149496, 10.2623))
     self.assertEqual(surf.bottom_left, Point(-0.025650, -0.149496,
                                              29.7377))
     self.assertEqual(surf.bottom_right, Point(0.149496, 0.025650, 29.7377))
Example #43
0
 def test_inclined_surf_from_corner_points_topo(self):
     # inclined surface (dip = 45) with 45 degrees strike
     surf = PlanarSurface.from_corner_points(
         Point(0, 0, -1), Point(0.5, 0.5, -1),
         Point(0.563593, 0.436408, 9.), Point(0.063592, -0.063592, 9))
     self.assertAlmostEqual(surf.strike, 45., delta=0.1)
     self.assertAlmostEqual(surf.dip, 45., delta=0.1)
     self.assertEqual(surf.top_left, Point(0, 0, -1))
     self.assertEqual(surf.top_right, Point(0.5, 0.5, -1))
     self.assertEqual(surf.bottom_left, Point(0.063592, -0.063592, 9))
     self.assertEqual(surf.bottom_right, Point(0.563593, 0.436408, 9.))
Example #44
0
    def test1(self):
        lons, lats, depths = geo_utils.cartesian_to_spherical(
            numpy.array(
                [[60, -10, -10], [60, -10, 10], [60, 10, 10], [60, 10, -10]],
                float))
        surface = PlanarSurface(10, 20, 30, *Mesh(lons, lats, depths))
        aaae = numpy.testing.assert_array_almost_equal

        plons, plats, pdepths = geo_utils.cartesian_to_spherical(
            numpy.array([[60, -10, -10], [59, 0, 0], [70, -11, -10]], float))

        dists, xx, yy = surface._project(plons, plats, pdepths)
        aaae(xx, [0, 10, 0])
        aaae(yy, [0, 10, -1])
        aaae(dists, [0, 1, -10])

        lons, lats, depths = surface._project_back(dists, xx, yy)
        aaae(lons, plons)
        aaae(lats, plats)
        aaae(depths, pdepths)
Example #45
0
 def test_inclined_surf_from_corner_points(self):
     # inclined surface (dip = 45) with 45 degrees strike
     surf = PlanarSurface.from_corner_points(
         2.0, Point(0, 0, 0), Point(0.5, 0.5, 0), Point(0.563593, 0.436408, 10.0), Point(0.063592, -0.063592, 10)
     )
     self.assertEqual(surf.mesh_spacing, 2.0)
     self.assertAlmostEqual(surf.strike, 45.0, delta=0.1)
     self.assertAlmostEqual(surf.dip, 45.0, delta=0.1)
     self.assertEqual(surf.top_left, Point(0, 0, 0))
     self.assertEqual(surf.top_right, Point(0.5, 0.5, 0))
     self.assertEqual(surf.bottom_left, Point(0.063592, -0.063592, 10))
     self.assertEqual(surf.bottom_right, Point(0.563593, 0.436408, 10.0))
Example #46
0
 def test_vertical_surf_from_corner_points_topo(self):
     # vertical surface pointing North
     surf = PlanarSurface.from_corner_points(
         2., Point(0, 0, -1), Point(0, 1, -1), Point(0, 1, 4), Point(0, 0, 4)
     )
     self.assertEqual(surf.mesh_spacing, 2.)
     self.assertAlmostEqual(surf.strike, 0.)
     self.assertAlmostEqual(surf.dip, 90.)
     self.assertEqual(surf.top_left, Point(0, 0, -1))
     self.assertEqual(surf.top_right, Point(0, 1, -1))
     self.assertEqual(surf.bottom_left, Point(0, 0, 4))
     self.assertEqual(surf.bottom_right, Point(0, 1, 4))
Example #47
0
 def test_inclined_surf_from_corner_points(self):
     # inclined surface (dip = 45) with 45 degrees strike
     surf = PlanarSurface.from_corner_points(2., Point(0, 0, 0),
                                             Point(0.5, 0.5, 0),
                                             Point(0.563593, 0.436408, 10.),
                                             Point(0.063592, -0.063592, 10))
     self.assertEqual(surf.mesh_spacing, 2.)
     self.assertAlmostEqual(surf.strike, 45., delta=0.1)
     self.assertAlmostEqual(surf.dip, 45., delta=0.1)
     self.assertEqual(surf.top_left, Point(0, 0, 0))
     self.assertEqual(surf.top_right, Point(0.5, 0.5, 0))
     self.assertEqual(surf.bottom_left, Point(0.063592, -0.063592, 10))
     self.assertEqual(surf.bottom_right, Point(0.563593, 0.436408, 10.))
    def check_surface_validity(cls, edges):
        """
        Check validity of the surface.

        Project edge points to vertical plane anchored to surface upper left
        edge and with strike equal to top edge strike. Check that resulting
        polygon is valid.

        This method doesn't have to be called by hands before creating the
        surface object, because it is called from :meth:`from_fault_data`.
        """
        # extract coordinates of surface boundary (as defined from edges)
        full_boundary = []
        left_boundary = []
        right_boundary = []

        for i in range(1, len(edges) - 1):
            left_boundary.append(edges[i].points[0])
            right_boundary.append(edges[i].points[-1])

        full_boundary.extend(edges[0].points)
        full_boundary.extend(right_boundary)
        full_boundary.extend(edges[-1].points[::-1])
        full_boundary.extend(left_boundary[::-1])

        lons = [p.longitude for p in full_boundary]
        lats = [p.latitude for p in full_boundary]
        depths = [p.depth for p in full_boundary]

        # define reference plane. Corner points are separated by an arbitrary
        # distance of 10 km. The mesh spacing is set to 2 km. Both corner
        # distance and mesh spacing values do not affect the algorithm results.
        ul = edges[0].points[0]
        strike = ul.azimuth(edges[0].points[-1])
        dist = 10.
        mesh_spacing = 2.

        ur = ul.point_at(dist, 0, strike)
        bl = Point(ul.longitude, ul.latitude, ul.depth + dist)
        br = bl.point_at(dist, 0, strike)

        # project surface boundary to reference plane and check for
        # validity.
        ref_plane = PlanarSurface.from_corner_points(
            mesh_spacing, ul, ur, br, bl
        )
        _, xx, yy = ref_plane._project(lons, lats, depths)
        coords = [(x, y) for x, y in zip(xx, yy)]
        p = shapely.geometry.Polygon(coords)
        if not p.is_valid:
            raise ValueError('Edges points are not in the right order')
Example #49
0
    def check_surface_validity(cls, edges):
        """
        Check validity of the surface.

        Project edge points to vertical plane anchored to surface upper left
        edge and with strike equal to top edge strike. Check that resulting
        polygon is valid.

        This method doesn't have to be called by hands before creating the
        surface object, because it is called from :meth:`from_fault_data`.
        """
        # extract coordinates of surface boundary (as defined from edges)
        full_boundary = []
        left_boundary = []
        right_boundary = []

        for i in range(1, len(edges) - 1):
            left_boundary.append(edges[i].points[0])
            right_boundary.append(edges[i].points[-1])

        full_boundary.extend(edges[0].points)
        full_boundary.extend(right_boundary)
        full_boundary.extend(edges[-1].points[::-1])
        full_boundary.extend(left_boundary[::-1])

        lons = [p.longitude for p in full_boundary]
        lats = [p.latitude for p in full_boundary]
        depths = [p.depth for p in full_boundary]

        # define reference plane. Corner points are separated by an arbitrary
        # distance of 10 km. The mesh spacing is set to 2 km. Both corner
        # distance and mesh spacing values do not affect the algorithm results.
        ul = edges[0].points[0]
        strike = ul.azimuth(edges[0].points[-1])
        dist = 10.
        mesh_spacing = 2.

        ur = ul.point_at(dist, 0, strike)
        bl = Point(ul.longitude, ul.latitude, ul.depth + dist)
        br = bl.point_at(dist, 0, strike)

        # project surface boundary to reference plane and check for
        # validity.
        ref_plane = PlanarSurface.from_corner_points(mesh_spacing, ul, ur, br,
                                                     bl)
        _, xx, yy = ref_plane._project(lons, lats, depths)
        coords = [(x, y) for x, y in zip(xx, yy)]
        p = shapely.geometry.Polygon(coords)
        if not p.is_valid:
            raise ValueError('Edges points are not in the right order')
Example #50
0
def get_ucerf_rupture(hdf5, iloc, idx_set, tom, sites,
                      integration_distance, mesh_spacing=DEFAULT_MESH_SPACING,
                      trt=DEFAULT_TRT):
    """
    :param hdf5:
        Source Model hdf5 object as instance of :class: h5py.File
    :param int iloc:
        Location of the rupture plane in the hdf5 file
    :param dict idx_set:
        Set of indices for the branch
    Generates a rupture set from a sample of the background model
    :param tom:
        Temporal occurrence model as instance of :class:
        openquake.hazardlib.tom.TOM
    :param sites:
        Sites for consideration (can be None!)
    """
    ridx = hdf5[idx_set["geol_idx"] + "/RuptureIndex"][iloc]
    surface_set = []
    if not prefilter_ruptures(
            hdf5, ridx, idx_set, sites, integration_distance):
        return None, None
    for idx in ridx:
        # Build simple fault surface
        trace_idx = "{:s}/{:s}".format(idx_set["sec_idx"], str(idx))
        rup_plane = hdf5[trace_idx + "/RupturePlanes"][:].astype("float64")
        for jloc in range(0, rup_plane.shape[2]):
            top_left = Point(rup_plane[0, 0, jloc],
                             rup_plane[0, 1, jloc],
                             rup_plane[0, 2, jloc])
            top_right = Point(rup_plane[1, 0, jloc],
                              rup_plane[1, 1, jloc],
                              rup_plane[1, 2, jloc])
            bottom_right = Point(rup_plane[2, 0, jloc],
                                 rup_plane[2, 1, jloc],
                                 rup_plane[2, 2, jloc])
            bottom_left = Point(rup_plane[3, 0, jloc],
                                rup_plane[3, 1, jloc],
                                rup_plane[3, 2, jloc])
            try:
                surface_set.append(PlanarSurface.from_corner_points(
                    mesh_spacing,
                    top_left,
                    top_right,
                    bottom_right,
                    bottom_left))
            except ValueError as evl:
                raise ValueError(evl, trace_idx, top_left, top_right,
                                 bottom_right, bottom_left)

    rupture = ParametricProbabilisticRupture(
        hdf5[idx_set["mag_idx"]][iloc],  # Magnitude
        hdf5[idx_set["rake_idx"]][iloc],  # Rake
        trt,  # Tectonic Region Type
        surface_set[len(surface_set)/2].get_middle_point(),  # Hypocentre
        MultiSurface(surface_set),
        CharacteristicFaultSource,
        hdf5[idx_set["rate_idx"]][iloc],  # Rate of events
        tom)

    # Get rupture index code string
    ridx_string = "-".join(str(val) for val in ridx)
    return rupture, ridx_string
Example #51
0
 def test_1(self):
     surface = PlanarSurface(1, 2, 3, *test_data.TEST_7_RUPTURE_6_CORNERS)
     sites = Mesh.from_points_list([Point(0, 0)])
     self.assertAlmostEqual(8.01185807319,
                            surface.get_min_distance(sites)[0], places=2)
Example #52
0
 def test(self):
     corners = [Point(0.0, 0.0, 0.0), Point(0.0, 0.089932, 0.0),
                Point(0.0, 0.089932, 10.0), Point(0.0, 0.0, 10.0)]
     surface = PlanarSurface(45, 90, *corners)
     self.assertAlmostEqual(surface.get_area(), 100.0, places=0)
Example #53
0
 def test_vertical_surface(self):
     corners = [Point(-0.05, -0.05, 8), Point(0.05, 0.05, 8),
                Point(0.05, 0.05, 10), Point(-0.05, -0.05, 10)]
     surface = PlanarSurface(45, 60, *corners)
     self.assertAlmostEqual(surface.get_width(), 2.0, places=4)
Example #54
0
 def test(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)
     self.assertEqual(surface.get_top_edge_depth(), 8)
Example #55
0
 def test_2(self):
     surface = PlanarSurface(2, 3, *tdata.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)
Example #56
0
 def test_topo(self):
     surface = PlanarSurface(2, 3, *tdata.TEST_7_RUPTURE_9_CORNERS)
     sites = Mesh.from_points_list([Point(-0.3, 0.4, -8)])
     self.assertAlmostEqual(55.6159556,
                            surface.get_min_distance(sites)[0], delta=0.6)
Example #57
0
 def test_3(self):
     surface = PlanarSurface(2, 3, *tdata.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)