Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    def test_total_stddev_only(self):
        truncation_level = None
        numpy.random.seed(37)
        realizations = 1000
        gmfs = ground_motion_fields(self.rupture, self.sites_total,
                                    [self.imt2],
                                    self.total_stddev_gsim,
                                    truncation_level,
                                    realizations=realizations,
                                    correlation_model=None)
        intensity = gmfs[self.imt2]

        assert_allclose((intensity[0].mean(), intensity[0].std()),
                        (self.mean1, self.total1), rtol=4e-2)
        assert_allclose((intensity[1].mean(), intensity[1].std()),
                        (self.mean2, self.total2), rtol=4e-2)
        assert_allclose((intensity[2].mean(), intensity[2].std()),
                        (self.mean3, self.total3), rtol=4e-2)

        assert_allclose((intensity[3].mean(), intensity[3].std()),
                        (self.mean4567, self.total45), rtol=4e-2)
        assert_allclose((intensity[4].mean(), intensity[4].std()),
                        (self.mean4567, self.total45), rtol=4e-2)

        assert_allclose((intensity[5].mean(), intensity[5].std()),
                        (self.mean4567, self.total67), rtol=4e-2)
        assert_allclose((intensity[6].mean(), intensity[6].std()),
                        (self.mean4567, self.total67), rtol=4e-2)
Ejemplo n.º 5
0
    def test_total_stddev_only(self):
        truncation_level = None
        numpy.random.seed(37)
        realizations = 1000
        gmfs = ground_motion_fields(self.rupture,
                                    self.sites_total, [self.imt2],
                                    self.total_stddev_gsim,
                                    truncation_level,
                                    realizations=realizations,
                                    correlation_model=None)
        intensity = gmfs[self.imt2]

        assert_allclose((intensity[0].mean(), intensity[0].std()),
                        (self.mean1, self.total1),
                        rtol=4e-2)
        assert_allclose((intensity[1].mean(), intensity[1].std()),
                        (self.mean2, self.total2),
                        rtol=4e-2)
        assert_allclose((intensity[2].mean(), intensity[2].std()),
                        (self.mean3, self.total3),
                        rtol=4e-2)

        assert_allclose((intensity[3].mean(), intensity[3].std()),
                        (self.mean4567, self.total45),
                        rtol=4e-2)
        assert_allclose((intensity[4].mean(), intensity[4].std()),
                        (self.mean4567, self.total45),
                        rtol=4e-2)

        assert_allclose((intensity[5].mean(), intensity[5].std()),
                        (self.mean4567, self.total67),
                        rtol=4e-2)
        assert_allclose((intensity[6].mean(), intensity[6].std()),
                        (self.mean4567, self.total67),
                        rtol=4e-2)
Ejemplo n.º 6
0
    def test_no_filtering_no_truncation(self):
        truncation_level = None
        numpy.random.seed(3)
        realizations = 2000
        gmfs = ground_motion_fields(self.rupture, self.sites,
                                    [self.imt2], self.gsim,
                                    truncation_level,
                                    realizations=realizations)
        intensity = gmfs[self.imt2]

        assert_allclose((intensity[0].mean(), intensity[0].std()),
                        (self.mean1, self.stddev1), rtol=4e-2)
        assert_allclose((intensity[1].mean(), intensity[1].std()),
                        (self.mean2, self.stddev2), rtol=4e-2)
        assert_allclose((intensity[2].mean(), intensity[2].std()),
                        (self.mean3, self.stddev3), rtol=4e-2)

        assert_allclose((intensity[3].mean(), intensity[3].std()),
                        (self.mean4567, self.stddev45), rtol=4e-2)
        assert_allclose((intensity[4].mean(), intensity[4].std()),
                        (self.mean4567, self.stddev45), rtol=4e-2)

        assert_allclose((intensity[5].mean(), intensity[5].std()),
                        (self.mean4567, self.stddev67), rtol=4e-2)
        assert_allclose((intensity[6].mean(), intensity[6].std()),
                        (self.mean4567, self.stddev67), rtol=4e-2)

        # sites with zero intra-event stddev, should give exactly the same
        # result, since inter-event distribution is sampled only once
        assert_array_equal(intensity[5], intensity[6])

        self.assertFalse((intensity[3] == intensity[4]).all())
Ejemplo n.º 7
0
    def test_rupture_site_filtering(self):
        mean = 10
        inter = 2
        intra = 3
        points = [Point(0, 0), Point(0, 0.05)]
        sites = [Site(point, mean, False, inter, intra) for point in points]
        self.sites = SiteCollection(sites)

        def rupture_site_filter(rupture_sites):
            [(rupture, sites)] = rupture_sites
            yield rupture, sites.filter(sites.mesh.lats == 0)

        self.gsim.expect_same_sitecol = False

        numpy.random.seed(37)
        cormo = JB2009CorrelationModel(vs30_clustering=False)
        gmfs = ground_motion_fields(
            self.rupture, self.sites, [self.imt1], self.gsim,
            truncation_level=None, realizations=1,
            correlation_model=cormo,
            rupture_site_filter=rupture_site_filter
        )

        s1gmf, s2gmf = gmfs[self.imt1]
        numpy.testing.assert_array_equal(s2gmf, 0)
        numpy.testing.assert_array_almost_equal(s1gmf, 11.1852253)
Ejemplo n.º 8
0
def calc_gmfs_fast(oqparam, sitecol):
    """
    Build all the ground motion fields for the whole site collection in
    a single step.
    """
    max_dist = oqparam.maximum_distance
    correl_model = get_correl_model(oqparam)
    seed = oqparam.random_seed
    imts = get_imts(oqparam)
    [gsim] = get_gsims(oqparam)
    trunc_level = oqparam.truncation_level
    n_gmfs = oqparam.number_of_ground_motion_fields
    rupture = get_rupture(oqparam)
    res = gmf.ground_motion_fields(
        rupture,
        sitecol,
        imts,
        gsim,
        trunc_level,
        n_gmfs,
        correl_model,
        filters.rupture_site_distance_filter(max_dist),
        seed,
    )
    return {str(imt): matrix for imt, matrix in res.items()}
Ejemplo n.º 9
0
    def test_no_truncation(self):
        mean = 10
        inter = 1e-300
        intra = 3
        points = [
            Point(0, 0),
            Point(0, 0.05),
            Point(0.06, 0.025),
            Point(0, 1.0),
            Point(-10, -10)
        ]
        sites = [Site(point, mean, False, inter, intra) for point in points]
        self.sites = SiteCollection(sites)

        numpy.random.seed(23)
        cormo = JB2009CorrelationModel(vs30_clustering=False)
        corma = cormo._get_correlation_matrix(self.sites, self.imt1)
        gmfs = ground_motion_fields(self.rupture,
                                    self.sites, [self.imt1],
                                    self.gsim,
                                    truncation_level=None,
                                    realizations=6000,
                                    correlation_model=cormo)

        sampled_corma = numpy.corrcoef(gmfs[self.imt1])
        assert_allclose(corma, sampled_corma, rtol=0, atol=0.02)
Ejemplo n.º 10
0
    def test_rupture_site_filtering(self):
        mean = 10
        inter = 2
        intra = 3
        points = [Point(0, 0), Point(0, 0.05)]
        sites = [Site(point, mean, False, inter, intra) for point in points]
        self.sites = SiteCollection(sites)

        def rupture_site_filter(rupture_sites):
            [(rupture, sites)] = rupture_sites
            yield rupture, sites.filter(sites.mesh.lats == 0)

        self.gsim.expect_same_sitecol = False

        numpy.random.seed(37)
        cormo = JB2009CorrelationModel(vs30_clustering=False)
        gmfs = ground_motion_fields(self.rupture,
                                    self.sites, [self.imt1],
                                    self.gsim,
                                    truncation_level=None,
                                    realizations=1,
                                    correlation_model=cormo,
                                    rupture_site_filter=rupture_site_filter)

        s1gmf, s2gmf = gmfs[self.imt1]
        numpy.testing.assert_array_equal(s2gmf, 0)
        numpy.testing.assert_array_almost_equal(s1gmf, 11.1852253)
Ejemplo n.º 11
0
 def execute(self):
     gmfs_by_imt = ground_motion_fields(
         self.rupture, self.sitecol, self.imts, self.gsim,
         self.oqparam.truncation_level,
         self.oqparam.number_of_ground_motion_fields,
         correlation_model=readinput.get_correl_model(self.oqparam),
         seed=self.oqparam.random_seed)
     return gmfs_by_imt
Ejemplo n.º 12
0
 def execute(self):
     gmfs_by_imt = ground_motion_fields(
         self.rupture, self.sitecol, self.imts, self.gsim,
         self.oqparam.truncation_level,
         self.oqparam.number_of_ground_motion_fields,
         correlation_model=readinput.get_correl_model(self.oqparam),
         seed=self.oqparam.random_seed)
     return gmfs_by_imt
Ejemplo n.º 13
0
    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)
        with self.assertRaises(CorrelationButNoInterIntraStdDevs):
            ground_motion_fields(
                self.rupture, self.sites, [self.imt1], gsim,
                truncation_level=None, realizations=6000,
                correlation_model=cormo)
Ejemplo n.º 14
0
 def test_filter_all_out(self):
     def rupture_site_filter(rupture_site):
         return []
     for truncation_level in (None, 0, 1.3):
         gmfs = ground_motion_fields(
             self.rupture, self.sites, [self.imt1, self.imt2], self.gsim,
             truncation_level=truncation_level,
             realizations=123,
             rupture_site_filter=rupture_site_filter
         )
         self.assertEqual(gmfs[self.imt1].shape, (7, 123))
         self.assertEqual(gmfs[self.imt2].shape, (7, 123))
         assert_array_equal(gmfs[self.imt1], 0)
         assert_array_equal(gmfs[self.imt2], 0)
Ejemplo n.º 15
0
    def test_mean_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.assertAlmostEqual(2.27395, gmf[0], delta=1e-4)
        self.assertAlmostEqual(3.38409, gmf[1], delta=1e-4)
Ejemplo n.º 16
0
def _compute_gmf(params, imt, gsims, site_coll, ruptures, rupture_seeds):
    """
    Compute a ground motion field value for each rupture, for all the
    points affected by that rupture, for the given IMT. Returns a
    dictionary with the nonzero contributions to each site id, and a dictionary
    with the ids of the contributing ruptures for each site id.
    assert len(ruptures) == len(rupture_seeds)

    :param params:
        a dictionary containing the keys
        correl_model, truncation_level, maximum_distance
    :param imt:
        a hazardlib IMT instance
    :param gsims:
        a dictionary {tectonic region type -> GSIM instance}
    :param site_coll:
        a SiteCollection instance
    :param ruptures:
        a list of SESRupture objects
    :param rupture_seeds:
        a list with the seeds associated to the ruptures
    """
    gmvs_per_site = collections.defaultdict(list)
    ruptures_per_site = collections.defaultdict(list)

    # Compute and save ground motion fields
    for i, rupture in enumerate(ruptures):
        gmf_calc_kwargs = {
            'rupture': rupture.rupture,
            'sites': site_coll,
            'imts': [imt],
            'gsim': gsims[rupture.rupture.tectonic_region_type],
            'truncation_level': params['truncation_level'],
            'realizations': DEFAULT_GMF_REALIZATIONS,
            'correlation_model': params['correl_model'],
            'rupture_site_filter': filters.rupture_site_distance_filter(
                params['maximum_distance']),
        }
        numpy.random.seed(rupture_seeds[i])
        # there is a single imt => a single entry in the return dict
        [gmf_1_realiz] = gmf.ground_motion_fields(**gmf_calc_kwargs).values()
        # since DEFAULT_GMF_REALIZATIONS is 1, gmf_1_realiz is a matrix
        # with n_sites rows and 1 column
        for site, gmv in zip(site_coll, gmf_1_realiz):
            gmv = float(gmv)  # convert a 1x1 matrix into a float
            if gmv:  # nonzero contribution to site
                gmvs_per_site[site.id].append(gmv)
                ruptures_per_site[site.id].append(rupture.id)
    return gmvs_per_site, ruptures_per_site
Ejemplo n.º 17
0
    def test_filter_all_out(self):
        def rupture_site_filter(rupture_site):
            return []

        for truncation_level in (None, 0, 1.3):
            gmfs = ground_motion_fields(
                self.rupture,
                self.sites, [self.imt1, self.imt2],
                self.gsim,
                truncation_level=truncation_level,
                realizations=123,
                rupture_site_filter=rupture_site_filter)
            self.assertEqual(gmfs[self.imt1].shape, (7, 123))
            self.assertEqual(gmfs[self.imt2].shape, (7, 123))
            assert_array_equal(gmfs[self.imt1], 0)
            assert_array_equal(gmfs[self.imt2], 0)
Ejemplo n.º 18
0
def calc_gmfs_fast(oqparam, sitecol):
    """
    Build all the ground motion fields for the whole site collection in
    a single step.
    """
    max_dist = oqparam.maximum_distance
    correl_model = get_correl_model(oqparam)
    seed = oqparam.random_seed
    imts = get_imts(oqparam)
    [gsim] = get_gsims(oqparam)
    trunc_level = oqparam.truncation_level
    n_gmfs = oqparam.number_of_ground_motion_fields
    rupture = get_rupture(oqparam)
    res = gmf.ground_motion_fields(
        rupture, sitecol, imts, gsim, trunc_level, n_gmfs, correl_model,
        filters.rupture_site_distance_filter(max_dist), seed)
    return {str(imt): matrix for imt, matrix in res.items()}
Ejemplo n.º 19
0
 def test_no_filtering_zero_truncation(self):
     truncation_level = 0
     self.gsim.expect_stddevs = False
     gmfs = ground_motion_fields(self.rupture, self.sites,
                                 [self.imt1, self.imt2], self.gsim,
                                 realizations=100,
                                 truncation_level=truncation_level)
     for intensity in gmfs[self.imt1], gmfs[self.imt2]:
         for i in xrange(7):
             self.assertEqual(intensity[i].std(), 0)
         self.assertEqual(intensity[0].mean(), self.mean1)
         self.assertEqual(intensity[1].mean(), self.mean2)
         self.assertEqual(intensity[2].mean(), self.mean3)
         self.assertEqual(intensity[3].mean(), self.mean4567)
         self.assertEqual(intensity[4].mean(), self.mean4567)
         self.assertEqual(intensity[5].mean(), self.mean4567)
         self.assertEqual(intensity[6].mean(), self.mean4567)
Ejemplo n.º 20
0
    def test_no_filtering_with_truncation(self):
        truncation_level = 1.9
        numpy.random.seed(11)
        realizations = 400
        gmfs = ground_motion_fields(self.rupture,
                                    self.sites, [self.imt1],
                                    self.gsim,
                                    realizations=realizations,
                                    truncation_level=truncation_level)
        intensity = gmfs[self.imt1]

        max_deviation1 = (self.inter1 + self.intra1) * truncation_level
        max_deviation2 = (self.inter2 + self.intra2) * truncation_level
        max_deviation3 = (self.inter3 + self.intra3) * truncation_level
        max_deviation4567 = truncation_level
        self.assertLessEqual(intensity[0].max(), self.mean1 + max_deviation1)
        self.assertGreaterEqual(intensity[0].min(),
                                self.mean1 - max_deviation1)
        self.assertLessEqual(intensity[1].max(), self.mean2 + max_deviation2)
        self.assertGreaterEqual(intensity[1].min(),
                                self.mean2 - max_deviation2)
        self.assertLessEqual(intensity[2].max(), self.mean3 + max_deviation3)
        self.assertGreaterEqual(intensity[2].min(),
                                self.mean3 - max_deviation3)

        for i in (3, 4, 5, 6):
            self.assertLessEqual(intensity[i].max(),
                                 self.mean4567 + max_deviation4567)
            self.assertGreaterEqual(intensity[i].min(),
                                    self.mean4567 - max_deviation4567)

        assert_allclose(intensity.mean(axis=1),
                        [self.mean1, self.mean2, self.mean3] +
                        [self.mean4567] * 4,
                        rtol=5e-2)

        self.assertLess(intensity[0].std(), self.stddev1)
        self.assertLess(intensity[1].std(), self.stddev2)
        self.assertLess(intensity[2].std(), self.stddev3)
        self.assertLess(intensity[3].std(), self.stddev45)
        self.assertLess(intensity[4].std(), self.stddev45)
        self.assertLess(intensity[5].std(), self.stddev67)
        self.assertLess(intensity[6].std(), self.stddev67)
        for i in range(7):
            self.assertGreater(intensity[i].std(), 0)
Ejemplo n.º 21
0
    def test_filtered_no_truncation(self):
        numpy.random.seed(17)
        realizations = 50
        gmfs = ground_motion_fields(
            self.rupture,
            self.sites, [self.imt1, self.imt2],
            self.gsim,
            truncation_level=None,
            realizations=realizations,
            rupture_site_filter=self.rupture_site_filter)

        for imt in [self.imt1, self.imt2]:
            intensity = gmfs[imt]
            self.assertEqual(intensity.shape, (7, realizations))
            assert_array_equal(
                intensity[(1 - self.sites.vs30measured).nonzero()], 0)
            self.assertFalse(
                (intensity[self.sites.vs30measured.nonzero()] == 0).any())
Ejemplo n.º 22
0
 def test_no_filtering_zero_truncation(self):
     truncation_level = 0
     self.gsim.expect_stddevs = False
     gmfs = ground_motion_fields(self.rupture,
                                 self.sites, [self.imt1, self.imt2],
                                 self.gsim,
                                 realizations=100,
                                 truncation_level=truncation_level)
     for intensity in gmfs[self.imt1], gmfs[self.imt2]:
         for i in range(7):
             self.assertEqual(intensity[i].std(), 0)
         self.assertEqual(intensity[0].mean(), self.mean1)
         self.assertEqual(intensity[1].mean(), self.mean2)
         self.assertEqual(intensity[2].mean(), self.mean3)
         self.assertEqual(intensity[3].mean(), self.mean4567)
         self.assertEqual(intensity[4].mean(), self.mean4567)
         self.assertEqual(intensity[5].mean(), self.mean4567)
         self.assertEqual(intensity[6].mean(), self.mean4567)
Ejemplo n.º 23
0
    def test_no_filtering_with_truncation(self):
        truncation_level = 1.9
        numpy.random.seed(11)
        realizations = 400
        gmfs = ground_motion_fields(self.rupture, self.sites,
                                    [self.imt1], self.gsim,
                                    realizations=realizations,
                                    truncation_level=truncation_level)
        intensity = gmfs[self.imt1]

        max_deviation1 = (self.inter1 + self.intra1) * truncation_level
        max_deviation2 = (self.inter2 + self.intra2) * truncation_level
        max_deviation3 = (self.inter3 + self.intra3) * truncation_level
        max_deviation4567 = truncation_level
        self.assertLessEqual(intensity[0].max(), self.mean1 + max_deviation1)
        self.assertGreaterEqual(intensity[0].min(),
                                self.mean1 - max_deviation1)
        self.assertLessEqual(intensity[1].max(), self.mean2 + max_deviation2)
        self.assertGreaterEqual(intensity[1].min(),
                                self.mean2 - max_deviation2)
        self.assertLessEqual(intensity[2].max(), self.mean3 + max_deviation3)
        self.assertGreaterEqual(intensity[2].min(),
                                self.mean3 - max_deviation3)

        for i in (3, 4, 5, 6):
            self.assertLessEqual(intensity[i].max(),
                                 self.mean4567 + max_deviation4567)
            self.assertGreaterEqual(intensity[i].min(),
                                    self.mean4567 - max_deviation4567)

        assert_allclose(intensity.mean(axis=1),
                        [self.mean1, self.mean2, self.mean3] +
                        [self.mean4567] * 4,
                        rtol=5e-2)

        self.assertLess(intensity[0].std(), self.stddev1)
        self.assertLess(intensity[1].std(), self.stddev2)
        self.assertLess(intensity[2].std(), self.stddev3)
        self.assertLess(intensity[3].std(), self.stddev45)
        self.assertLess(intensity[4].std(), self.stddev45)
        self.assertLess(intensity[5].std(), self.stddev67)
        self.assertLess(intensity[6].std(), self.stddev67)
        for i in xrange(7):
            self.assertGreater(intensity[i].std(), 0)
Ejemplo n.º 24
0
    def test_filtered_zero_truncation(self):
        self.gsim.expect_stddevs = False
        self.gsim.expect_same_sitecol = False
        gmfs = ground_motion_fields(
            self.rupture, self.sites, [self.imt1, self.imt2], self.gsim,
            truncation_level=0, rupture_site_filter=self.rupture_site_filter,
            realizations=100
        )
        for intensity in gmfs[self.imt1], gmfs[self.imt2]:
            for i in range(7):
                self.assertEqual(intensity[i].std(), 0)

            self.assertEqual(intensity[0].mean(), 0)
            self.assertEqual(intensity[1].mean(), self.mean2)
            self.assertEqual(intensity[2].mean(), 0)
            self.assertEqual(intensity[3].mean(), self.mean4567)
            self.assertEqual(intensity[4].mean(), 0)
            self.assertEqual(intensity[5].mean(), self.mean4567)
            self.assertEqual(intensity[6].mean(), 0)
Ejemplo n.º 25
0
    def test_filtered_no_truncation(self):
        numpy.random.seed(17)
        realizations = 50
        gmfs = ground_motion_fields(
            self.rupture, self.sites, [self.imt1, self.imt2],
            self.gsim, truncation_level=None,
            realizations=realizations,
            rupture_site_filter=self.rupture_site_filter
        )

        for imt in [self.imt1, self.imt2]:
            intensity = gmfs[imt]
            self.assertEqual(intensity.shape, (7, realizations))
            assert_array_equal(
                intensity[(1 - self.sites.vs30measured).nonzero()], 0
            )
            self.assertFalse(
                (intensity[self.sites.vs30measured.nonzero()] == 0).any()
            )
Ejemplo n.º 26
0
    def test_no_truncation(self):
        mean = 10
        inter = 1e-300
        intra = 3
        points = [Point(0, 0), Point(0, 0.05), Point(0.06, 0.025),
                  Point(0, 1.0), Point(-10, -10)]
        sites = [Site(point, mean, False, inter, intra) for point in points]
        self.sites = SiteCollection(sites)

        numpy.random.seed(23)
        cormo = JB2009CorrelationModel(vs30_clustering=False)
        corma = cormo._get_correlation_matrix(self.sites, self.imt1)
        gmfs = ground_motion_fields(
            self.rupture, self.sites, [self.imt1], self.gsim,
            truncation_level=None, realizations=6000,
            correlation_model=cormo
        )

        sampled_corma = numpy.corrcoef(gmfs[self.imt1])
        assert_allclose(corma, sampled_corma, rtol=0, atol=0.02)
Ejemplo n.º 27
0
def compute_gmf_cache(hc, gsims, ruptures, rupture_ids):
    """
    Compute a ground motion field value for each rupture, for all the
    points affected by that rupture, for all IMTs.
    """
    imts = [haz_general.imt_to_hazardlib(x)
            for x in hc.intensity_measure_types]
    correl_model = None
    if hc.ground_motion_correlation_model is not None:
        correl_model = haz_general.get_correl_model(hc)

    n_points = len(hc.site_collection)

    # initialize gmf_cache, a dict imt -> {gmvs, rupture_ids}
    gmf_cache = dict((imt, dict(gmvs=numpy.empty((n_points, 0)),
                                rupture_ids=[]))
                     for imt in imts)

    for rupture, rupture_id in zip(ruptures, rupture_ids):

        # Compute and save ground motion fields
        gmf_calc_kwargs = {
            'rupture': rupture,
            'sites': hc.site_collection,
            'imts': imts,
            'gsim': gsims[rupture.tectonic_region_type],
            'truncation_level': hc.truncation_level,
            'realizations': DEFAULT_GMF_REALIZATIONS,
            'correlation_model': correl_model,
            'rupture_site_filter': filters.rupture_site_distance_filter(
                hc.maximum_distance),
        }
        gmf_dict = gmf.ground_motion_fields(**gmf_calc_kwargs)

        # update the gmf cache:
        for imt_key, v in gmf_dict.iteritems():
            gmf_cache[imt_key]['gmvs'] = numpy.append(
                gmf_cache[imt_key]['gmvs'], v, axis=1)
            gmf_cache[imt_key]['rupture_ids'].append(rupture_id)

    return gmf_cache
Ejemplo n.º 28
0
    def test_filtered_zero_truncation(self):
        self.gsim.expect_stddevs = False
        self.gsim.expect_same_sitecol = False
        gmfs = ground_motion_fields(
            self.rupture,
            self.sites, [self.imt1, self.imt2],
            self.gsim,
            truncation_level=0,
            rupture_site_filter=self.rupture_site_filter,
            realizations=100)
        for intensity in gmfs[self.imt1], gmfs[self.imt2]:
            for i in xrange(7):
                self.assertEqual(intensity[i].std(), 0)

            self.assertEqual(intensity[0].mean(), 0)
            self.assertEqual(intensity[1].mean(), self.mean2)
            self.assertEqual(intensity[2].mean(), 0)
            self.assertEqual(intensity[3].mean(), self.mean4567)
            self.assertEqual(intensity[4].mean(), 0)
            self.assertEqual(intensity[5].mean(), self.mean4567)
            self.assertEqual(intensity[6].mean(), 0)
Ejemplo n.º 29
0
    def test_no_filtering_no_truncation(self):
        truncation_level = None
        numpy.random.seed(3)
        realizations = 2000
        gmfs = ground_motion_fields(self.rupture,
                                    self.sites, [self.imt2],
                                    self.gsim,
                                    truncation_level,
                                    realizations=realizations)
        intensity = gmfs[self.imt2]

        assert_allclose((intensity[0].mean(), intensity[0].std()),
                        (self.mean1, self.stddev1),
                        rtol=4e-2)
        assert_allclose((intensity[1].mean(), intensity[1].std()),
                        (self.mean2, self.stddev2),
                        rtol=4e-2)
        assert_allclose((intensity[2].mean(), intensity[2].std()),
                        (self.mean3, self.stddev3),
                        rtol=4e-2)

        assert_allclose((intensity[3].mean(), intensity[3].std()),
                        (self.mean4567, self.stddev45),
                        rtol=4e-2)
        assert_allclose((intensity[4].mean(), intensity[4].std()),
                        (self.mean4567, self.stddev45),
                        rtol=4e-2)

        assert_allclose((intensity[5].mean(), intensity[5].std()),
                        (self.mean4567, self.stddev67),
                        rtol=4e-2)
        assert_allclose((intensity[6].mean(), intensity[6].std()),
                        (self.mean4567, self.stddev67),
                        rtol=4e-2)

        # sites with zero intra-event stddev, should give exactly the same
        # result, since inter-event distribution is sampled only once
        assert_array_equal(intensity[5], intensity[6])

        self.assertFalse((intensity[3] == intensity[4]).all())
Ejemplo n.º 30
0
    def calc_gmf(self, r_sites, rupture, rupture_id, rupture_seed):
        """
        Compute the GMF generated by the given rupture on the given
        sites and collect the values in the dictionaries
        .gmvs_per_site and .ruptures_per_site.

        :param r_sites:
            the collection of sites affected by the rupture
        :param rupture:
            an `openquake.hazardlib.source.rupture.
                ParametricProbabilisticRupture` instance
        :param id:
            the id of an `openquake.engine.db.models.SESRupture` instance
        :param seed:
            an integer to be used as stochastic seed
        """
        for rlz, gsim in self.gsim_by_rlz.items():
            gmf_calc_kwargs = {
                'rupture': rupture,
                'sites': r_sites,
                'imts': self.imts,
                'gsim': gsim,
                'truncation_level': self.params['truncation_level'],
                'realizations': DEFAULT_GMF_REALIZATIONS,
                'correlation_model': self.params['correl_model'],
            }
            numpy.random.seed(rupture_seed)
            gmf_dict = gmf.ground_motion_fields(**gmf_calc_kwargs)
            for imt, gmf_1_realiz in gmf_dict.iteritems():
                # since DEFAULT_GMF_REALIZATIONS is 1, gmf_1_realiz is a matrix
                # with n_sites rows and 1 column
                for site_id, gmv in zip(r_sites.sids, gmf_1_realiz):
                    # convert a 1x1 matrix into a float
                    gmv = float(gmv)
                    if gmv:
                        self.gmvs_per_site[rlz, imt, site_id].append(gmv)
                        self.ruptures_per_site[rlz, imt, site_id].append(
                            rupture_id)
Ejemplo n.º 31
0
def ses_and_gmfs(job_id, src_ids, lt_rlz_id, task_seed, result_grp_ordinal):
    """
    Celery task for the stochastic event set calculator.

    Samples logic trees and calls the stochastic event set calculator.

    Once stochastic event sets are calculated, results will be saved to the
    database. See :class:`openquake.engine.db.models.SESCollection`.

    Optionally (specified in the job configuration using the
    `ground_motion_fields` parameter), GMFs can be computed from each rupture
    in each stochastic event set. GMFs are also saved to the database.

    Once all of this work is complete, a signal will be sent via AMQP to let
    the control noe know that the work is complete. (If there is any work left
    to be dispatched, this signal will indicate to the control node that more
    work can be enqueued.)

    :param int job_id:
        ID of the currently running job.
    :param src_ids:
        List of ids of parsed source models from which we will generate
        stochastic event sets/ruptures.
    :param lt_rlz_id:
        Id of logic tree realization model to calculate for.
    :param int task_seed:
        Value for seeding numpy/scipy in the computation of stochastic event
        sets and ground motion fields.
    :param int result_grp_ordinal:
        The result group in which the calculation results will be placed.
        This ID basically corresponds to the sequence number of the task,
        in the context of the entire calculation.
    """
    logs.LOG.debug(('> starting `stochastic_event_sets` task: job_id=%s, '
                    'lt_realization_id=%s') % (job_id, lt_rlz_id))
    numpy.random.seed(task_seed)

    hc = models.HazardCalculation.objects.get(oqjob=job_id)

    cmplt_lt_ses = None
    if hc.complete_logic_tree_ses:
        cmplt_lt_ses = models.SES.objects.get(
            ses_collection__output__oq_job=job_id,
            complete_logic_tree_ses=True)

    if hc.ground_motion_fields:
        # For ground motion field calculation, we need the points of interest
        # for the calculation.
        points_to_compute = hc.points_to_compute()

        imts = [haz_general.imt_to_hazardlib(x)
                for x in hc.intensity_measure_types]

        correl_model = None
        if hc.ground_motion_correlation_model is not None:
            correl_model = haz_general.get_correl_model(hc)

    lt_rlz = models.LtRealization.objects.get(id=lt_rlz_id)
    ltp = logictree.LogicTreeProcessor(hc.id)

    apply_uncertainties = ltp.parse_source_model_logictree_path(
            lt_rlz.sm_lt_path)
    gsims = ltp.parse_gmpe_logictree_path(lt_rlz.gsim_lt_path)

    sources = list(haz_general.gen_sources(
        src_ids, apply_uncertainties, hc.rupture_mesh_spacing,
        hc.width_of_mfd_bin, hc.area_source_discretization))

    # Compute stochastic event sets
    # For each rupture generated, we can optionally calculate a GMF
    for ses_rlz_n in xrange(1, hc.ses_per_logic_tree_path + 1):
        logs.LOG.debug('> computing stochastic event set %s of %s'
                       % (ses_rlz_n, hc.ses_per_logic_tree_path))

        # This is the container for all ruptures for this stochastic event set
        # (specified by `ordinal` and the logic tree realization).
        # NOTE: Many tasks can contribute ruptures to this SES.
        ses = models.SES.objects.get(
            ses_collection__lt_realization=lt_rlz, ordinal=ses_rlz_n)

        sources_sites = ((src, hc.site_collection) for src in sources)
        ssd_filter = filters.source_site_distance_filter(hc.maximum_distance)
        # Get the filtered sources, ignore the site collection:
        filtered_sources = (src for src, _ in ssd_filter(sources_sites))
        # Calculate stochastic event sets:
        logs.LOG.debug('> computing stochastic event sets')
        if hc.ground_motion_fields:
            gmf_cache = _create_gmf_cache(len(points_to_compute), imts)

            logs.LOG.debug('> computing also ground motion fields')
            # This will be the "container" for all computed ground motion field
            # results for this stochastic event set.
            gmf_set = models.GmfSet.objects.get(
                gmf_collection__lt_realization=lt_rlz, ses_ordinal=ses_rlz_n)

        ses_poissonian = stochastic.stochastic_event_set_poissonian(
            filtered_sources, hc.investigation_time)

        logs.LOG.debug('> looping over ruptures')
        rupture_ordinal = 0
        for rupture in ses_poissonian:
            rupture_ordinal += 1

            # Prepare and save SES ruptures to the db:
            logs.LOG.debug('> saving SES rupture to DB')
            _save_ses_rupture(
                ses, rupture, cmplt_lt_ses, result_grp_ordinal,
                rupture_ordinal)
            logs.LOG.debug('> done saving SES rupture to DB')

            # Compute ground motion fields (if requested)
            logs.LOG.debug('compute ground motion fields?  %s'
                           % hc.ground_motion_fields)
            if hc.ground_motion_fields:
                # Compute and save ground motion fields

                gmf_calc_kwargs = {
                    'rupture': rupture,
                    'sites': hc.site_collection,
                    'imts': imts,
                    'gsim': gsims[rupture.tectonic_region_type],
                    'truncation_level': hc.truncation_level,
                    'realizations': DEFAULT_GMF_REALIZATIONS,
                    'correlation_model': correl_model,
                    'rupture_site_filter':
                        filters.rupture_site_distance_filter(
                            hc.maximum_distance),
                }
                logs.LOG.debug('> computing ground motion fields')
                gmf_dict = gmf_calc.ground_motion_fields(**gmf_calc_kwargs)
                logs.LOG.debug('< done computing ground motion fields')

                # update the gmf cache:
                for k, v in gmf_dict.iteritems():
                    gmf_cache[k] = numpy.append(
                        gmf_cache[k], v, axis=1)

        logs.LOG.debug('< Done looping over ruptures')
        logs.LOG.debug('%s ruptures computed for SES realization %s of %s'
                       % (rupture_ordinal, ses_rlz_n,
                          hc.ses_per_logic_tree_path))
        logs.LOG.debug('< done computing stochastic event set %s of %s'
                       % (ses_rlz_n, hc.ses_per_logic_tree_path))

        if hc.ground_motion_fields:
            # save the GMFs to the DB
            logs.LOG.debug('> saving GMF results to DB')
            _save_gmfs(
                gmf_set, gmf_cache, points_to_compute, result_grp_ordinal)
            logs.LOG.debug('< done saving GMF results to DB')

    logs.LOG.debug('< task complete, signalling completion')
    base.signal_task_complete(job_id=job_id, num_items=len(src_ids))