def test_wrong_imt(self): err = 'imt must be an instance of IMT subclass' self._assert_value_error(self._get_poes, err, imt='something') self._assert_value_error(self._disaggregate_poe, err, imt='something') err = 'imt PGV is not supported by FakeGSIM' self._assert_value_error(self._get_poes, err, imt=PGV()) self._assert_value_error(self._disaggregate_poe, err, imt=PGV())
def test_mearn_nearfault_distance_taper(self): rupture = self.make_rupture() site1 = Site(location=Point(27.9, 41), vs30=1200., vs30measured=True, z1pt0=2.36, z2pt5=2.) site2 = Site(location=Point(28.1, 41), vs30=1200., vs30measured=True, z1pt0=2.36, z2pt5=2.) sites = SiteCollection([site1, site2]) fields = ground_motion_fields( rupture, sites, [PGV()], ChiouYoungs2014NearFaultEffect(), truncation_level=0, realizations=1) gmf = fields[PGV()] self.assertAlmostEquals(2.27395, gmf[0], delta=1e-4) self.assertAlmostEquals(3.38409, gmf[1], delta=1e-4)
def _compute_mean(self, C, g, mag, hypo_depth, dists, imt): """ Compute mean according to equation on Table 2, page 2275. """ delta = 0.00750 * 10**(0.507 * mag) # computing R for different values of mag if mag < 6.5: R = np.sqrt(dists.rhypo**2 + delta**2) else: R = np.sqrt(dists.rrup**2 + delta**2) mean = ( # 1st term C['c1'] + C['c2'] * mag + # 2nd term C['c3'] * R - # 3rd term C['c4'] * np.log10(R) + # 4th term C['c5'] * hypo_depth) # convert from base 10 to base e if imt == PGV(): mean = np.log(10**mean) else: # convert from cm/s**2 to g mean = np.log((10**mean) * 1e-2 / g) return mean
def __init__(self): self._pga = PGA() self._pgv = PGV() self._sa03 = SA(0.3) self._sa10 = SA(1.0) self._sa30 = SA(3.0) self.DEFINED_FOR_INTENSITY_MEASURE_TYPES = set()
def test_mag5(self): m = 5.0 imts = [ PGA(), SA(0.05), SA(0.1), SA(0.15), SA(0.2), SA(0.3), SA(0.4), SA(0.5), SA(0.75), SA(1.00), SA(2.00), SA(3.00), SA(4.00), PGV() ] computed = [] for imt in imts: coeffs = BindiEtAl2011.COEFFS_DELTA[imt] dlt = _get_delta(coeffs, imt, m) computed.append(dlt) expected = np.array([ 0.4, 0.366, 0.381, 0.362, 0.421, 0.384, 0.406, 0.428, 0.413, 0.39, 0.353, 0.363, 0.416, 0.345 ]) np.testing.assert_array_almost_equal(computed, expected)
def _getParamsFromIMT(self, imt): """ Helper function to return (possibly interpolated) conversion parameters for a given IMT. Args: imt (OpenQuake IMT): The intensity measure type of the input ground motions. Valid IMTs are PGA, PGV, and SA. Returns: (float, float, float, float, float): Coeffients for conversion. """ if imt == PGA(): sigma = self.pars['sigma'][0] c0 = self.pars['c0smooth'][0] r1 = self.pars['r1smooth'][0] m1 = self.pars['m1smooth'][0] m2 = self.pars['m2smooth'][0] elif imt == PGV(): sigma = self.pars['sigma'][1] c0 = self.pars['c0smooth'][1] r1 = self.pars['r1smooth'][1] m1 = self.pars['m1smooth'][1] m2 = self.pars['m2smooth'][1] elif 'SA' in imt: imt_per = imt.period pa = self.pars['per'][2:] sigma = np.interp(imt_per, pa, self.pars['sigma'][2:]) c0 = np.interp(imt_per, pa, self.pars['c0smooth'][2:]) r1 = np.interp(imt_per, pa, self.pars['r1smooth'][2:]) m1 = np.interp(imt_per, pa, self.pars['m1smooth'][2:]) m2 = np.interp(imt_per, pa, self.pars['m2smooth'][2:]) else: raise ValueError("Unknown IMT: %s" % str(imt)) return (sigma, c0, r1, m1, m2)
def hawaii_adjust(mean, ctx, imt): # Defining frequency if imt == PGA(): freq = 50.0 elif imt == PGV(): freq = 2.0 else: freq = 1./imt.period # Equation 3 of Atkinson (2010) x1 = np.min([-0.18+0.17*np.log10(freq), 0]) # Equation 4 a-b-c of Atkinson (2010) if ctx.hypo_depth < 20.0: x0 = np.max([0.217 - 0.321 * np.log10(freq), 0]) elif ctx.hypo_depth > 35.0: x0 = np.min([0.263 + 0.0924 * np.log10(freq), 0.35]) else: x0 = 0.2 # Limiting calculation distance to 1km # (as suggested by C. Bruce Worden) rjb = [d if d > 1 else 1 for d in ctx.rjb] # Equation 2 and 5 of Atkinson (2010) mean += (x0 + x1*np.log10(rjb)) / np.log10(np.e)
def test_mag7pt2(self): m = 7.2 gmm = BindiEtAl2011Ita19Low() imts = [ PGA(), SA(0.05), SA(0.1), SA(0.15), SA(0.2), SA(0.3), SA(0.4), SA(0.5), SA(0.75), SA(1.00), SA(2.00), SA(3.00), SA(4.00), PGV() ] computed = [] for imt in imts: dlt = gmm._get_delta(imt, m) computed.append(dlt) expected = np.array([ 0.61164, 0.5662, 0.56228, 0.61236, 0.707, 0.65284, 0.62512, 0.64052, 0.59472, 0.54224, 0.32704, 0.3498, 0.46264, 0.48624 ]) np.testing.assert_array_almost_equal(computed, expected)
def _get_mean(self, vs30, mag, rrup, imt, scale_fac): """ Compute and return mean """ C_HR, C_BC, C_SR, SC = self._extract_coeffs(imt) rrup = self._clip_distances(rrup) f0 = self._compute_f0_factor(rrup) f1 = self._compute_f1_factor(rrup) f2 = self._compute_f2_factor(rrup) pga_bc = self._get_pga_bc( f0, f1, f2, SC, mag, rrup, vs30, scale_fac ) # compute mean values for hard-rock sites (vs30 >= 2000), # and non-hard-rock sites (vs30 < 2000) and add soil amplification # term mean = np.zeros_like(vs30) self._compute_mean(C_HR, f0, f1, f2, SC, mag, rrup, vs30 >= 2000.0, mean, scale_fac) self._compute_mean(C_BC, f0, f1, f2, SC, mag, rrup, vs30 < 2000.0, mean, scale_fac) self._compute_soil_amplification(C_SR, vs30, pga_bc, mean) # convert from base 10 to base e if imt == PGV(): mean = np.log(10 ** mean) else: # convert from cm/s**2 to g mean = np.log((10 ** mean) * 1e-2 / g) return mean
def test_table_string_instantiation(self): # Check that the table instantiates in the conventional way table1 = CoeffsTable(sa_damping=5, table=self.coefficient_string) self.assertDictEqual(table1.non_sa_coeffs, { PGV(): { "a": 0.1, "b": 0.2 }, PGA(): { "a": 0.05, "b": 0.1 } }) self.assertDictEqual( table1.sa_coeffs, { SA(period=0.1, damping=5): { "a": 1.0, "b": 2.0 }, SA(period=1.0, damping=5): { "a": 5.0, "b": 10.0 }, SA(period=10.0, damping=5): { "a": 10.0, "b": 20.0 } })
def _compute_mean(C, g, ctx, imt, imc): """ Return the mean value based on the selected intensity measure component """ mag = ctx.mag dis = ctx.rrup if mag > 6.5 else ctx.rhypo # near-source saturation term delta = 0.0075 * 10**(0.507 * mag) # depth scaling H = min(ctx.hypo_depth, 75) - 50 # average distance to the fault surface R = np.sqrt(dis**2 + delta**2) if imc == const.IMC.VERTICAL_TO_HORIZONTAL_RATIO: # Computes the mean for the 'V/H ratio' according to equation 4, # page 1306. The equation predicts the mean value for PGA, PGV, # and SA in terms of the natural logarithm. mean = (C['c1'] + C['c2'] * mag + C['c3'] * R) else: # Computes the mean for the 'horizontal or vertical component' # according to equation 1, page 1304. The equation # predicts the mean value for PGA, PGV, and SA in terms of # the natural logarithm. mean = (C['c1'] + C['c2'] * mag + C['c3'] * np.log(R) + C['c4'] * R + C['c5'] * H) # For PGA and SA, the values are convert from cm/s**2 to 'g' if imt != PGV(): mean = np.log(np.exp(mean) * 1e-2 / g) return mean
def manage_imts(imt_str): if imt_str is None: return None, None elif imt_str == 'PGA': imt_obj = PGA() imt_label = 'PGA (%g)' elif imt_str == 'PGV': imt_obj = PGV() imt_label = 'PGV (cm/s)' elif 'SA' in imt_str: prd = float(imt_str.split('(')[1].split(')')[0]) imt_obj = SA(prd) imt_label = str(imt_obj) + ' (%g)' elif 'FAS' in imt_str: prd = float(imt_str.split('(')[1].split(')')[0]) imt_obj = None imt_label = str(imt_obj) + ' (%g)' elif imt_str == 'DURATION': imt_obj = RSD595() imt_label = 'Duration (s)' elif imt_str == 'ARIAS': imt_obj = IA() imt_label = 'Arias intensity (cm/s)' else: raise ValueError('IMT %s is not supported' % imt_str) return imt_obj, imt_label
def test_mag5(self): m = 5.0 gmm = BindiEtAl2011Ita19Low() imts = [ PGA(), SA(0.05), SA(0.1), SA(0.15), SA(0.2), SA(0.3), SA(0.4), SA(0.5), SA(0.75), SA(1.00), SA(2.00), SA(3.00), SA(4.00), PGV() ] computed = [] for imt in imts: dlt = gmm._get_delta(imt, m) computed.append(dlt) expected = np.array([ 0.4, 0.366, 0.381, 0.362, 0.421, 0.384, 0.406, 0.428, 0.413, 0.39, 0.353, 0.363, 0.416, 0.345 ]) np.testing.assert_array_almost_equal(computed, expected)
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ Using a frequency dependent correction for the mean ground motion. Standard deviation is fixed. """ mean, stddevs = super().get_mean_and_stddevs(sites, rup, dists, imt, stddev_types) # Defining frequency if imt == PGA(): freq = 50.0 elif imt == PGV(): freq = 2.0 else: freq = 1./imt.period # Equation 3 of Atkinson (2010) x1 = np.min([-0.18+0.17*np.log10(freq), 0]) # Equation 4 a-b-c of Atkinson (2010) if rup.hypo_depth < 20.0: x0 = np.max([0.217-0.321*np.log10(freq), 0]) elif rup.hypo_depth > 35.0: x0 = np.min([0.263+0.0924*np.log10(freq), 0.35]) else: x0 = 0.2 # Limiting calculation distance to 1km # (as suggested by C. Bruce Worden) rjb = [d if d > 1 else 1 for d in dists.rjb] # Equation 2 and 5 of Atkinson (2010) mean += (x0 + x1*np.log10(rjb))/np.log10(np.e) return mean, stddevs
def __init__(self): self._pga = PGA() self._pgv = PGV() self._pgd = PGD() self._ia = IA() self._ih = IH() self._sa03 = SA(0.3) self._sa10 = SA(1.0) self._sa30 = SA(3.0)
def filter_gmpe_list(gmpes, wts, imt): """ Method to remove GMPEs from the GMPE list that are not applicable to a specific IMT. Rescales the weights to sum to one. Args: gmpes (list): List of GMPE instances. wts (list): List of floats indicating the weight of the GMPEs. imt (IMT): OQ IMT to filter GMPE list for. Returns: tuple: List of GMPE instances and list of weights. """ if wts is None: n = len(gmpes) wts = [1 / n] * n per_max = [np.max(get_gmpe_sa_periods(g)) for g in gmpes] per_min = [np.min(get_gmpe_sa_periods(g)) for g in gmpes] if imt == PGA(): sgmpe = [ g for g in gmpes if imt in get_gmpe_coef_table(g).non_sa_coeffs ] swts = [ w for g, w in zip(gmpes, wts) if imt in get_gmpe_coef_table(g).non_sa_coeffs ] elif imt == PGV(): sgmpe = [] swts = [] for i in range(len(gmpes)): if (imt in get_gmpe_coef_table(gmpes[i]).non_sa_coeffs) or\ (per_max[i] >= 1.0 and per_min[i] <= 1.0): sgmpe.append(gmpes[i]) swts.append(wts[i]) else: per = imt.period sgmpe = [] swts = [] for i in range(len(gmpes)): if (per_max[i] >= per and per_min[i] <= per): sgmpe.append(gmpes[i]) swts.append(wts[i]) if len(sgmpe) == 0: raise Exception('No applicable GMPEs from GMPE list for %s' % imt) # Scale weights to sum to one swts = np.array(swts) swts = swts / np.sum(swts) return sgmpe, swts
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ # extract dictionaries of coefficients specific to required # intensity measure type. C_HR = self.COEFFS_HARD_ROCK[imt] C_BC = self.COEFFS_BC[imt] C_SR = self.COEFFS_SOIL_RESPONSE[imt] # clip distances to avoid singularity at 0 rrup = self._clip_distances(dists) # compute factors required for mean value calculation f0 = self._compute_f0_factor(rrup) f1 = self._compute_f1_factor(rrup) f2 = self._compute_f2_factor(rrup) # compute pga for BC boundary (required for soil amplification # calculation on non-hard-rock sites) pga_bc = np.zeros_like(sites.vs30) self._compute_mean(self.COEFFS_BC[PGA()], f0, f1, f2, rup.mag, rrup, sites, sites.vs30 < 2000.0, pga_bc) pga_bc = (10**pga_bc) * 1e-2 / g # compute mean values for hard-rock sites (vs30 >= 2000), # and non-hard-rock sites (vs30 < 2000) and add soil amplification # term mean = np.zeros_like(sites.vs30) self._compute_mean(C_HR, f0, f1, f2, rup.mag, rrup, sites, sites.vs30 >= 2000.0, mean) self._compute_mean(C_BC, f0, f1, f2, rup.mag, rrup, sites, sites.vs30 < 2000.0, mean) self._compute_soil_amplification(C_SR, sites, pga_bc, mean) # convert from base 10 to base e if imt == PGV(): mean = np.log(10**mean) else: # convert from cm/s**2 to g mean = np.log((10**mean) * 1e-2 / g) stddevs = self._get_stddevs(stddev_types, num_sites=len(sites.vs30)) return mean, stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): gmpe_imt = PGV() mean, std_devs = self._gmpe.get_mean_and_stddevs( sites, rup, dists, gmpe_imt, stddev_types) # convert to intensity according to Kaestli & Faeh (2006) soil_correction = np.zeros_like(sites.vs30) # correct felt intensities on soft soils soil_correction[sites.vs30 > 0] = .47 # mean comes back in log(cm/s). convert to m/s mean = np.exp(mean) / 100. mean_mmi = 1.8 * np.log10(mean) + 8.69 + soil_correction # use constant sigma of 0.71 for GMICE std_devs = np.hypot(std_devs, 0.71) return mean_mmi, std_devs
def fromFuncs(cls, gmpe, gmice): """ Creates a new VirtualIPE object with the specified MultiGMPE and GMICE. There is no default constructor, you must use this method. Args: gmpe: An instance of the MultiGMPE object. gmice: An instance of a GMICE object. Returns: :class:`VirtualIPE`: A new instance of a VirtualIPE object. """ self = cls() self.gmpe = gmpe self.gmice = gmice if (gmpe.ALL_GMPES_HAVE_PGV is True and PGV in gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES): self.imt = PGV() elif (PGA in gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES and PGA in gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES): self.imt = PGA() elif (SA in gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES and SA in gmice.DEFINED_FOR_INTENSITY_MEASURE_TYPES): self.imt = SA(1.0) else: raise ShakeLibException( 'The supplied GMPE and GMICE do not have a common IMT' ) self.DEFINED_FOR_STANDARD_DEVIATION_TYPES = \ gmpe.DEFINED_FOR_STANDARD_DEVIATION_TYPES.copy() self.REQUIRES_DISTANCES = gmpe.REQUIRES_DISTANCES.copy() self.REQUIRES_RUPTURE_PARAMETERS = \ gmpe.REQUIRES_RUPTURE_PARAMETERS.copy() self.REQUIRES_SITES_PARAMETERS = \ gmpe.REQUIRES_SITES_PARAMETERS.copy() self.DEFINED_FOR_INTENSITY_MEASURE_COMPONENT = \ copy.copy(gmpe.DEFINED_FOR_INTENSITY_MEASURE_COMPONENT) self.DEFINED_FOR_TECTONIC_REGION_TYPE = \ copy.copy(gmpe.DEFINED_FOR_TECTONIC_REGION_TYPE) return self
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ C = self.COEFFS[imt] M = rup.mag - 6 R = np.sqrt(dists.rjb**2 + C['h']**2) # In the original formulation of the GMPE, distinction is only made # between rock and soil sites, which I assumed separated by the Vs30 # value of 910m/s (see equation 5 of the paper) gamma = np.array([0 if v > 910. else 1 for v in sites.vs30]) mean = np.zeros_like(R) mean += C['b1'] + \ C['b2'] * M + \ C['b3'] * M ** 2 + \ C['b5'] * np.log10(R) + \ C['b6'] * gamma # Convert from base 10 to base e mean /= np.log10(np.e) # Converting PSV to PSA if imt != PGA() and imt != PGV(): omega = 2. * np.pi / imt.period mean += np.log(omega / (gravity * 100)) # Computing standard deviation stddevs = self._get_stddevs(C, stddev_types, dists.rjb.shape[0]) # Convert from base 10 to base e stddevs = [sd / np.log10(np.e) for sd in stddevs] return mean, stddevs
def compute(self, ctx, imts, mean, sig, tau, phi): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.compute>` for spec of input and result values. """ for m, imt in enumerate(imts): C = self.COEFFS[imt] M = ctx.mag - 6 R = np.sqrt(ctx.rjb**2 + C['h']**2) # In the original formulation of the GMPE, distinction is only made # between rock and soil ctx, which I assumed separated by the Vs30 # value of 910m/s (see equation 5 of the paper) gamma = np.array([0 if v > 910. else 1 for v in ctx.vs30]) mean[m] = (C['b1'] + C['b2'] * M + C['b3'] * M**2 + C['b5'] * np.log10(R) + C['b6'] * gamma) # Convert from base 10 to base e mean[m] /= np.log10(np.e) # Converting PSV to PSA if imt != PGA() and imt != PGV(): omega = 2. * np.pi / imt.period mean[m] += np.log(omega / (gravity * 100)) # Computing standard deviation if (self.DEFINED_FOR_INTENSITY_MEASURE_COMPONENT == 'Random horizontal'): # Using equation 8 of the paper, # corrected as indicated in the erratum Sr = np.sqrt(C['SlZ']**2 + (C['S3'] / np.sqrt(2))**2) else: Sr = C['SlZ'] # Convert from base 10 to base e sig[m] = Sr / np.log10(np.e)
def test_ak07(): gmice = AK07() mi, dmda = gmice.getMIfromGM(amps_in, PGA(), dists=None, mag=None) mi_target = np.array( [4.02841992026, 4.69161846432, 6.23592624018, 7.46713892245, 8.18735217199, 8.69835160472, 9.09471355792]) np.testing.assert_allclose(mi, mi_target) mi, dmda = gmice.getMIfromGM( amps_in + np.log(100), PGV(), dists=dists, mag=None) mi_target = np.array( [4.37, 4.99980005623, 6.57, 7.48212088686, 8.0156774018, 8.39424177372, 8.68787911314]) np.testing.assert_allclose(mi, mi_target) mi, dmda = gmice.getMIfromGM(amps_in, SA(0.3), dists=None, mag=3.0) mi_target = np.array( [3.74866985004, 4.39755475646, 5.26034166627, 6.33200845084, 6.95889333307, 7.4036752354, 7.74867488171]) np.testing.assert_allclose(mi, mi_target) mi, dmda = gmice.getMIfromGM(amps_in, SA(1.0), dists=dists, mag=7.5) mi_target = np.array( [3.52702354769, 4.07617250928, 5.55842357177, 6.46666805811, 7.00909852304, 7.39358539638, 7.67946993475]) np.testing.assert_allclose(mi, mi_target) mi, dmda = gmice.getMIfromGM(amps_in, SA(3.0), dists=None, mag=None) mi_target = np.array( [4.99925301952, 6.3963707863, 7.96500702214, 8.86809700913, 9.3963707863, 9.77118699612, 10.]) np.testing.assert_allclose(mi, mi_target) amps, dadm = gmice.getGMfromMI(mmi_in, PGA(), dists=None, mag=7.0) amps_target = np.log(np.array( [0.000347300247926, 0.00182024377197, 0.00954012388176, 0.0498674941261, 0.0979977440572, 0.182039122534, 0.285603651352])) np.testing.assert_allclose(amps, amps_target) amps, dadm = gmice.getGMfromMI(mmi_in, PGV(), dists=dists, mag=None) amps_target = np.log(np.array( [0.0160156826445, 0.0916476244071, 0.524441401963, 3.03283082017, 7.54897155245, 17.4150246057, 31.9853049046])) np.testing.assert_allclose(amps, amps_target) amps, dadm = gmice.getGMfromMI(mmi_in, SA(0.3), dists=None, mag=6.0) amps_target = np.log(np.array( [0.000517861166862, 0.00281518839281, 0.0153038810286, 0.0845026480232, 0.183632256171, 0.374056955014, 0.627562284541])) np.testing.assert_allclose(amps, amps_target) amps, dadm = gmice.getGMfromMI(mmi_in, SA(1.0), dists=dists, mag=8.0) amps_target = np.log(np.array( [0.000508056775718, 0.00367375892063, 0.0258564132795, 0.0636582041631, 0.159533013524, 0.371822895172, 0.694260679175])) np.testing.assert_allclose(amps, amps_target) amps, dadm = gmice.getGMfromMI(mmi_in, SA(3.0), dists=None, mag=5.0) amps_target = np.log(np.array( [0.0000473148708829, 0.000281962568289, 0.00168029392098, 0.0102722203276, 0.0258026508623, 0.0600248374471, 0.110916883717])) np.testing.assert_allclose(amps, amps_target) amps, dadm = gmice.getGMfromMI(mmi_in, PGA(), dists=None, mag=7.0) amps_target = np.log(np.array( [0.000347300247926, 0.00182024377197, 0.00954012388176, 0.0498674941261, 0.0979977440572, 0.182039122534, 0.285603651352])) # print(repr(100 * np.exp(amps))) np.testing.assert_allclose(amps, amps_target) sdd = gmice.getGM2MIsd() assert sdd[PGA()] == 0.89 assert sdd[PGV()] == 0.76 assert sdd[SA(0.3)] == 0.79 assert sdd[SA(1.0)] == 0.73 assert sdd[SA(3.0)] == 0.72 sdd = gmice.getMI2GMsd() assert abs(sdd[PGA()] - 1.312473503006606) < 0.0000001 assert abs(sdd[PGV()] - 1.197344248356904) < 0.0000001 assert abs(sdd[SA(0.3)] - 1.450628608586249) < 0.0000001 assert abs(sdd[SA(1.0)] - 1.312473503006606) < 0.0000001 assert abs(sdd[SA(3.0)] - 1.865093925325177) < 0.0000001 nm = gmice.getName() assert nm == 'Atkinson and Kaka (2007)' sc = gmice.getScale() assert sc == 'scale_ak07.ps' mm = gmice.getMinMax() assert mm == (1.0, 10.0) dt = gmice.getDistanceType() assert dt == 'rrup' # # This should fail # with pytest.raises(ValueError) as e: # noqa mi, dmda = gmice.getMIfromGM(amps_in, MMI(), dists=None, mag=None)
def test_goda_atkinson_2010(): d = np.linspace(0, 10, 101) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, PGA()) cor_target = np.array([ 0., 0.39235049, 0.43205433, 0.45704225, 0.47560355, 0.490493, 0.50298594, 0.51378292, 0.52331214, 0.53185541, 0.5396085, 0.54671319, 0.5532756, 0.55937735, 0.56508259, 0.57044268, 0.57549947, 0.58028746, 0.5848355, 0.58916792, 0.59330547, 0.59726599, 0.60106493, 0.60471575, 0.60823028, 0.61161894, 0.61489101, 0.61805471, 0.62111745, 0.62408587, 0.62696595, 0.62976314, 0.63248236, 0.63512811, 0.6377045, 0.64021531, 0.64266398, 0.64505373, 0.64738748, 0.64966797, 0.65189773, 0.6540791, 0.65621427, 0.65830527, 0.66035401, 0.66236228, 0.66433173, 0.66626394, 0.66816036, 0.6700224, 0.67185134, 0.67364841, 0.67541479, 0.67715155, 0.67885975, 0.68054036, 0.68219431, 0.6838225, 0.68542575, 0.68700487, 0.68856062, 0.69009373, 0.69160488, 0.69309473, 0.69456391, 0.69601302, 0.69744265, 0.69885332, 0.70024558, 0.70161993, 0.70297685, 0.7043168, 0.70564023, 0.70694757, 0.70823922, 0.70951559, 0.71077704, 0.71202396, 0.71325668, 0.71447555, 0.71568089, 0.71687303, 0.71805226, 0.71921887, 0.72037316, 0.72151539, 0.72264583, 0.72376474, 0.72487235, 0.72596892, 0.72705467, 0.72812983, 0.72919461, 0.73024923, 0.73129388, 0.73232877, 0.73335409, 0.73437003, 0.73537675, 0.73637445, 0.73736329 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, PGV()) cor_target = np.array([ 0., 0.3575817, 0.39874653, 0.42491472, 0.44447585, 0.4602403, 0.47351603, 0.48502441, 0.4952078, 0.50435814, 0.51267872, 0.52031708, 0.52738388, 0.53396433, 0.54012554, 0.54592134, 0.55139559, 0.55658456, 0.56151855, 0.56622319, 0.57072035, 0.57502884, 0.57916497, 0.58314296, 0.58697532, 0.5906731, 0.59424609, 0.59770305, 0.6010518, 0.60429941, 0.60745223, 0.61051605, 0.6134961, 0.61639717, 0.61922365, 0.62197954, 0.62466854, 0.62729404, 0.62985921, 0.63236693, 0.63481993, 0.63722069, 0.63957157, 0.64187473, 0.64413223, 0.64634596, 0.6485177, 0.65064914, 0.65274185, 0.65479732, 0.65681693, 0.65880202, 0.66075382, 0.66267351, 0.66456221, 0.66642097, 0.66825079, 0.67005263, 0.67182739, 0.67357593, 0.67529906, 0.67699757, 0.6786722, 0.68032367, 0.68195264, 0.68355976, 0.68514567, 0.68671094, 0.68825614, 0.68978183, 0.69128852, 0.69277671, 0.69424688, 0.6956995, 0.697135, 0.69855383, 0.69995637, 0.70134304, 0.70271421, 0.70407025, 0.70541151, 0.70673833, 0.70805105, 0.70934997, 0.71063541, 0.71190767, 0.71316703, 0.71441376, 0.71564814, 0.71687042, 0.71808086, 0.7192797, 0.72046717, 0.72164351, 0.72280893, 0.72396365, 0.72510787, 0.72624181, 0.72736565, 0.72847959, 0.72958381 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(0.3)) cor_target = np.array([ 0., 0.50305951, 0.5415847, 0.56538633, 0.58286091, 0.59675844, 0.60833979, 0.61829248, 0.62703425, 0.63483861, 0.64189473, 0.6483391, 0.65427353, 0.65977604, 0.6649078, 0.66971766, 0.67424532, 0.67852342, 0.68257921, 0.68643562, 0.69011218, 0.69362563, 0.69699045, 0.70021924, 0.70332305, 0.70631162, 0.70919357, 0.71197657, 0.71466749, 0.71727249, 0.71979714, 0.72224644, 0.72462498, 0.7269369, 0.72918599, 0.73137573, 0.73350931, 0.73558966, 0.73761949, 0.73960131, 0.74153743, 0.74343001, 0.74528104, 0.7470924, 0.74886581, 0.75060292, 0.75230523, 0.75397419, 0.75561112, 0.75721728, 0.75879388, 0.76034201, 0.76186273, 0.76335705, 0.7648259, 0.76627017, 0.76769071, 0.76908832, 0.77046376, 0.77181775, 0.77315099, 0.77446412, 0.77575778, 0.77703255, 0.778289, 0.77952767, 0.78074909, 0.78195374, 0.7831421, 0.78431463, 0.78547176, 0.78661391, 0.78774147, 0.78885483, 0.78995437, 0.79104042, 0.79211334, 0.79317345, 0.79422107, 0.7952565, 0.79628003, 0.79729195, 0.79829253, 0.79928202, 0.80026069, 0.80122878, 0.80218652, 0.80313415, 0.80407187, 0.80499992, 0.80591848, 0.80682778, 0.80772798, 0.80861929, 0.80950189, 0.81037595, 0.81124164, 0.81209913, 0.81294858, 0.81379014, 0.81462397 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(1.0)) cor_target = np.array([ 0., 0.34369983, 0.38462439, 0.4107154, 0.4302548, 0.44602318, 0.45931659, 0.47085065, 0.48106461, 0.49024857, 0.4986047, 0.50627981, 0.51338405, 0.52000232, 0.52620149, 0.53203522, 0.53754726, 0.54277378, 0.54774504, 0.55248663, 0.55702037, 0.56136506, 0.56553699, 0.56955039, 0.57341775, 0.57715013, 0.58075732, 0.58424807, 0.58763023, 0.59091084, 0.5940963, 0.59719237, 0.60020431, 0.6031369, 0.60599455, 0.60878126, 0.61150075, 0.61415641, 0.6167514, 0.61928864, 0.62177084, 0.6242005, 0.62657999, 0.62891147, 0.631197, 0.63343848, 0.63563771, 0.63779637, 0.63991605, 0.64199823, 0.64404431, 0.64605561, 0.64803339, 0.64997884, 0.65189306, 0.65377712, 0.65563203, 0.65745874, 0.65925816, 0.66103115, 0.66277854, 0.6645011, 0.6661996, 0.66787473, 0.66952719, 0.67115762, 0.67276665, 0.67435486, 0.67592285, 0.67747114, 0.67900026, 0.68051073, 0.68200301, 0.68347759, 0.68493489, 0.68637536, 0.6877994, 0.68920742, 0.69059979, 0.69197689, 0.69333907, 0.69468667, 0.69602003, 0.69733946, 0.69864528, 0.69993779, 0.70121726, 0.70248399, 0.70373824, 0.70498027, 0.70621033, 0.70742868, 0.70863554, 0.70983115, 0.71101573, 0.7121895, 0.71335267, 0.71450544, 0.715648, 0.71678055, 0.71790329 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(3.0)) cor_target = np.array([ 0., 0.15200493, 0.19124798, 0.21858541, 0.24021333, 0.25837222, 0.27415936, 0.28820397, 0.30090441, 0.31253071, 0.32327539, 0.33328108, 0.34265674, 0.35148774, 0.35984232, 0.367776, 0.37533465, 0.38255664, 0.3894744, 0.39611569, 0.40250439, 0.40866127, 0.4146045, 0.42035008, 0.42591217, 0.4313034, 0.43653505, 0.44161725, 0.44655917, 0.45136909, 0.45605456, 0.46062244, 0.46507902, 0.46943008, 0.47368092, 0.47783644, 0.48190116, 0.48587928, 0.4897747, 0.49359102, 0.49733164, 0.5009997, 0.50459816, 0.50812977, 0.51159714, 0.5150027, 0.51834874, 0.52163742, 0.5248708, 0.52805078, 0.5311792, 0.53425779, 0.53728817, 0.54027191, 0.54321047, 0.54610526, 0.54895762, 0.55176881, 0.55454006, 0.55727251, 0.55996727, 0.5626254, 0.56524791, 0.56783576, 0.57038989, 0.57291117, 0.57540046, 0.57785857, 0.5802863, 0.58268439, 0.58505357, 0.58739454, 0.58970796, 0.59199449, 0.59425474, 0.59648932, 0.59869881, 0.60088376, 0.60304471, 0.60518219, 0.6072967, 0.60938872, 0.61145873, 0.61350718, 0.61553452, 0.61754116, 0.61952752, 0.62149401, 0.623441, 0.62536889, 0.62727802, 0.62916877, 0.63104147, 0.63289645, 0.63473404, 0.63655456, 0.6383583, 0.64014558, 0.64191668, 0.64367188, 0.64541145 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(0.1)) cor_target = np.array([ 0., 0.4019066, 0.44148923, 0.46635119, 0.48479565, 0.49957763, 0.51197127, 0.5226759, 0.5321187, 0.54058065, 0.54825684, 0.55528853, 0.56178139, 0.56781666, 0.57345817, 0.57875705, 0.58375488, 0.588486, 0.59297907, 0.59725829, 0.60134427, 0.60525472, 0.60900501, 0.61260849, 0.61607691, 0.61942064, 0.62264885, 0.62576973, 0.62879062, 0.63171811, 0.63455814, 0.6373161, 0.63999689, 0.64260496, 0.64514439, 0.64761892, 0.65003198, 0.65238674, 0.65468611, 0.65693279, 0.6591293, 0.66127796, 0.66338093, 0.66544024, 0.66745776, 0.66943527, 0.6713744, 0.67327672, 0.67514369, 0.67697665, 0.67877692, 0.6805457, 0.68228415, 0.68399334, 0.68567431, 0.68732803, 0.68895541, 0.69055735, 0.69213466, 0.69368813, 0.69521853, 0.69672657, 0.69821292, 0.69967825, 0.70112317, 0.70254828, 0.70395415, 0.70534131, 0.7067103, 0.7080616, 0.70939571, 0.71071306, 0.71201412, 0.71329929, 0.71456899, 0.7158236, 0.7170635, 0.71828905, 0.7195006, 0.72069849, 0.72188303, 0.72305454, 0.72421333, 0.72535967, 0.72649385, 0.72761614, 0.7287268, 0.72982608, 0.73091423, 0.73199149, 0.73305808, 0.73411423, 0.73516014, 0.73619604, 0.73722211, 0.73823855, 0.73924556, 0.74024332, 0.741232, 0.74221179, 0.74318283 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(0.2)) cor_target = np.array([ 0., 0.44945291, 0.48885314, 0.51340023, 0.53151766, 0.54598262, 0.55807405, 0.56849164, 0.5776617, 0.58586399, 0.59329238, 0.60008701, 0.60635255, 0.61216937, 0.61760052, 0.62269645, 0.62749817, 0.63203947, 0.63634856, 0.64044921, 0.64436167, 0.64810333, 0.65168924, 0.6551325, 0.65844461, 0.6616357, 0.66471476, 0.6676898, 0.67056796, 0.67335569, 0.67605879, 0.67868251, 0.68123163, 0.68371048, 0.68612303, 0.68847293, 0.69076352, 0.69299786, 0.69517881, 0.69730898, 0.6993908, 0.70142654, 0.7034183, 0.70536804, 0.70727758, 0.70914864, 0.71098282, 0.71278161, 0.71454642, 0.7162786, 0.71797937, 0.71964992, 0.72129137, 0.72290475, 0.72449106, 0.72605124, 0.72758619, 0.72909675, 0.73058372, 0.73204786, 0.73348991, 0.73491055, 0.73631044, 0.73769021, 0.73905046, 0.74039177, 0.74171468, 0.74301971, 0.74430737, 0.74557813, 0.74683246, 0.7480708, 0.74929357, 0.75050118, 0.75169402, 0.75287246, 0.75403687, 0.75518759, 0.75632496, 0.7574493, 0.75856091, 0.75966011, 0.76074718, 0.76182239, 0.76288601, 0.76393831, 0.76497954, 0.76600993, 0.76702973, 0.76803916, 0.76903843, 0.77002777, 0.77100737, 0.77197744, 0.77293817, 0.77388975, 0.77483235, 0.77576616, 0.77669135, 0.77760808, 0.7785165 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(0.5)) cor_target = np.array([ 0., 0.44945291, 0.48885314, 0.51340023, 0.53151766, 0.54598262, 0.55807405, 0.56849164, 0.5776617, 0.58586399, 0.59329238, 0.60008701, 0.60635255, 0.61216937, 0.61760052, 0.62269645, 0.62749817, 0.63203947, 0.63634856, 0.64044921, 0.64436167, 0.64810333, 0.65168924, 0.6551325, 0.65844461, 0.6616357, 0.66471476, 0.6676898, 0.67056796, 0.67335569, 0.67605879, 0.67868251, 0.68123163, 0.68371048, 0.68612303, 0.68847293, 0.69076352, 0.69299786, 0.69517881, 0.69730898, 0.6993908, 0.70142654, 0.7034183, 0.70536804, 0.70727758, 0.70914864, 0.71098282, 0.71278161, 0.71454642, 0.7162786, 0.71797937, 0.71964992, 0.72129137, 0.72290475, 0.72449106, 0.72605124, 0.72758619, 0.72909675, 0.73058372, 0.73204786, 0.73348991, 0.73491055, 0.73631044, 0.73769021, 0.73905046, 0.74039177, 0.74171468, 0.74301971, 0.74430737, 0.74557813, 0.74683246, 0.7480708, 0.74929357, 0.75050118, 0.75169402, 0.75287246, 0.75403687, 0.75518759, 0.75632496, 0.7574493, 0.75856091, 0.75966011, 0.76074718, 0.76182239, 0.76288601, 0.76393831, 0.76497954, 0.76600993, 0.76702973, 0.76803916, 0.76903843, 0.77002777, 0.77100737, 0.77197744, 0.77293817, 0.77388975, 0.77483235, 0.77576616, 0.77669135, 0.77760808, 0.7785165 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(2.0)) cor_target = np.array([ 0., 0.26347348, 0.30426801, 0.3309032, 0.35114805, 0.36766262, 0.38170302, 0.39396967, 0.40489599, 0.41477014, 0.42379413, 0.43211541, 0.43984514, 0.44706931, 0.45385595, 0.46025983, 0.46632574, 0.47209084, 0.47758629, 0.48283851, 0.4878701, 0.49270056, 0.49734684, 0.50182377, 0.5061444, 0.51032028, 0.5143617, 0.51827783, 0.52207694, 0.52576645, 0.52935309, 0.53284297, 0.53624166, 0.53955422, 0.54278532, 0.54593924, 0.5490199, 0.55203095, 0.55497573, 0.55785739, 0.56067879, 0.56344265, 0.56615146, 0.56880758, 0.5714132, 0.57397038, 0.57648104, 0.578947, 0.58136995, 0.58375152, 0.5860932, 0.58839644, 0.59066258, 0.59289291, 0.59508863, 0.59725091, 0.59938082, 0.60147942, 0.60354768, 0.60558654, 0.60759691, 0.60957963, 0.61153552, 0.61346536, 0.61536989, 0.61724982, 0.61910584, 0.6209386, 0.62274871, 0.62453679, 0.6263034, 0.6280491, 0.62977441, 0.63147986, 0.63316592, 0.63483307, 0.63648177, 0.63811245, 0.63972554, 0.64132143, 0.64290053, 0.64446321, 0.64600984, 0.64754077, 0.64905634, 0.65055688, 0.65204271, 0.65351415, 0.65497148, 0.65641499, 0.65784498, 0.6592617, 0.66066543, 0.66205641, 0.6634349, 0.66480113, 0.66615534, 0.66749775, 0.66882859, 0.67014807, 0.67145639 ]) np.testing.assert_allclose(cor, cor_target) cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, SA(5.0)) cor_target = np.array([ 0., 0.12402312, 0.15999168, 0.18557647, 0.20609443, 0.22349771, 0.23875233, 0.25241667, 0.26484659, 0.27628465, 0.28690463, 0.29683585, 0.3061775, 0.31500756, 0.32338858, 0.33137161, 0.33899893, 0.34630602, 0.35332297, 0.36007552, 0.36658596, 0.37287365, 0.37895559, 0.38484677, 0.3905605, 0.39610864, 0.40150184, 0.40674965, 0.41186075, 0.41684298, 0.42170349, 0.42644882, 0.43108494, 0.43561736, 0.44005113, 0.44439094, 0.44864112, 0.45280568, 0.45688836, 0.46089264, 0.46482176, 0.46867876, 0.47246648, 0.4761876, 0.47984461, 0.48343988, 0.48697562, 0.49045392, 0.49387678, 0.49724606, 0.50056352, 0.50383086, 0.50704967, 0.51022145, 0.51334766, 0.51642965, 0.51946874, 0.52246616, 0.52542312, 0.52834073, 0.53122009, 0.53406223, 0.53686815, 0.53963879, 0.54237507, 0.54507786, 0.547748, 0.5503863, 0.55299353, 0.55557043, 0.55811773, 0.56063611, 0.56312625, 0.56558877, 0.56802429, 0.57043342, 0.57281673, 0.57517477, 0.57750808, 0.57981718, 0.58210257, 0.58436474, 0.58660415, 0.58882126, 0.59101651, 0.59319033, 0.59534313, 0.59747531, 0.59958726, 0.60167935, 0.60375196, 0.60580544, 0.60784014, 0.60985639, 0.61185451, 0.61383483, 0.61579765, 0.61774327, 0.61967199, 0.62158409, 0.62347984 ]) np.testing.assert_allclose(cor, cor_target) # Undefined: cor = ga10.GodaAtkinson2010.getSpatialCorrelation(d, 'PGD') cor_target = np.array([ 0., 0.3575817, 0.39874653, 0.42491472, 0.44447585, 0.4602403, 0.47351603, 0.48502441, 0.4952078, 0.50435814, 0.51267872, 0.52031708, 0.52738388, 0.53396433, 0.54012554, 0.54592134, 0.55139559, 0.55658456, 0.56151855, 0.56622319, 0.57072035, 0.57502884, 0.57916497, 0.58314296, 0.58697532, 0.5906731, 0.59424609, 0.59770305, 0.6010518, 0.60429941, 0.60745223, 0.61051605, 0.6134961, 0.61639717, 0.61922365, 0.62197954, 0.62466854, 0.62729404, 0.62985921, 0.63236693, 0.63481993, 0.63722069, 0.63957157, 0.64187473, 0.64413223, 0.64634596, 0.6485177, 0.65064914, 0.65274185, 0.65479732, 0.65681693, 0.65880202, 0.66075382, 0.66267351, 0.66456221, 0.66642097, 0.66825079, 0.67005263, 0.67182739, 0.67357593, 0.67529906, 0.67699757, 0.6786722, 0.68032367, 0.68195264, 0.68355976, 0.68514567, 0.68671094, 0.68825614, 0.68978183, 0.69128852, 0.69277671, 0.69424688, 0.6956995, 0.697135, 0.69855383, 0.69995637, 0.70134304, 0.70271421, 0.70407025, 0.70541151, 0.70673833, 0.70805105, 0.70934997, 0.71063541, 0.71190767, 0.71316703, 0.71441376, 0.71564814, 0.71687042, 0.71808086, 0.7192797, 0.72046717, 0.72164351, 0.72280893, 0.72396365, 0.72510787, 0.72624181, 0.72736565, 0.72847959, 0.72958381 ]) np.testing.assert_allclose(cor, cor_target)
from shakelib.gmice.ak07 import AK07 homedir = os.path.dirname(os.path.abspath(__file__)) # where is this script? shakedir = os.path.abspath(os.path.join(homedir, '..', '..', '..')) sys.path.insert(0, shakedir) # Inputs amps_in = np.log(np.array([0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5])) mmi_in = np.array([2., 3., 4., 5., 6.2, 7.3, 8.1]) dists = np.array([22.2, 10., 1.1, 32., 120., 300.0, 450.5]) lfact = np.log10(np.e) dmda_target = {PGA(): set([x * lfact for x in [1.55, 3.70]]), PGV(): set([x * lfact for x in [1.47, 3.16]]), SA(0.3): set([x * lfact for x in [1.69, 4.14]]), SA(1.0): set([x * lfact for x in [1.51, 2.90]]), SA(3.0): set([x * lfact for x in [1.17, 3.01]])} dadm_target = {PGA(): set([1.0 / (x * lfact) for x in [1.55, 3.70]]), PGV(): set([1.0 / (x * lfact) for x in [1.47, 3.16]]), SA(0.3): set([1.0 / (x * lfact) for x in [1.69, 4.14]]), SA(1.0): set([1.0 / (x * lfact) for x in [1.51, 2.90]]), SA(3.0): set([1.0 / (x * lfact) for x in [1.17, 3.01]])} def test_ak07(): gmice = AK07() mi, dmda = gmice.getMIfromGM(amps_in, PGA(), dists=None, mag=None) mi_target = np.array(
def _parse_csv_line(headers, values): """ Parse a single line from data file. :param headers: A list of header names, the strings from the first line of csv file. :param values: A list of values of a single row to parse. :returns: A tuple of the following values (in specified order): sctx An instance of :class:`openquake.hazardlib.gsim.base.SitesContext` with attributes populated by the information from in row in a form of single-element numpy arrays. rctx An instance of :class:`openquake.hazardlib.gsim.base.RuptureContext`. dctx An instance of :class:`openquake.hazardlib.gsim.base.DistancesContext`. stddev_types An empty list, if the ``result_type`` column says "MEAN" for that row, otherwise it is a list with one item -- a requested standard deviation type. expected_results A dictionary mapping IMT-objects to one-element arrays of expected result values. Those results represent either standard deviation or mean value of corresponding IMT depending on ``result_type``. result_type A string literal, one of ``'STDDEV'`` or ``'MEAN'``. Value is taken from column ``result_type``. """ rctx = RuptureContext() sctx = SitesContext() dctx = DistancesContext() expected_results = {} stddev_types = result_type = damping = None for param, value in zip(headers, values): if param == 'result_type': value = value.upper() if value.endswith('_STDDEV'): # the row defines expected stddev results result_type = 'STDDEV' stddev_types = [getattr(const.StdDev, value[:-len('_STDDEV')])] else: # the row defines expected exponents of mean values assert value == 'MEAN' stddev_types = [] result_type = 'MEAN' elif param == 'damping': damping = float(value) elif param.startswith('site_'): # value is sites context object attribute if (param == 'site_vs30measured') or (param == 'site_backarc'): value = float(value) != 0 else: value = float(value) setattr(sctx, param[len('site_'):], numpy.array([value])) elif param.startswith('dist_'): # value is a distance measure value = float(value) setattr(dctx, param[len('dist_'):], numpy.array([value])) elif param.startswith('rup_'): # value is a rupture context attribute value = float(value) setattr(rctx, param[len('rup_'):], value) elif param == 'component_type': pass else: # value is the expected result (of result_type type) value = float(value) if param == 'pga': imt = PGA() elif param == 'pgv': imt = PGV() elif param == 'pgd': imt = PGD() elif param == 'cav': imt = CAV() else: period = float(param) assert damping is not None imt = SA(period, damping) expected_results[imt] = numpy.array([value]) assert result_type is not None return sctx, rctx, dctx, stddev_types, expected_results, result_type
def test_wald99(): gmice = Wald99() df = {'PGA': amps_in, 'PGV': amps_in + np.log(100)} mi = gmice.getPreferredMI(df) mi_target = np.array( [3.18167182, 4.23133858, 5.68946656, 6.84666436, 7.47561075, 7.90914817, 8.24542592, 9.29]) if do_test is True: np.testing.assert_allclose(mi, mi_target) else: print(repr(mi)) pga_amps_in_nan = np.log(np.array( [0.01, 0.03, 0.1, np.nan, 0.3, 0.4, 0.5, 1.0])) pgv_amps_in_nan = np.log(np.array( [0.01, 0.03, 0.1, 0.2, np.nan, 0.4, 0.5, 1.0])) + np.log(100) pga_amps_in_nan[5] = np.nan pgv_amps_in_nan[5] = np.nan df = {'PGA': pga_amps_in_nan, 'PGV': pgv_amps_in_nan} mi = gmice.getPreferredMI(df) mi_target = np.array( [3.18167182, 4.23133858, 5.68946656, 6.86457408, 7.37577236, np.nan, 8.24542592, 9.29]) if do_test is True: np.testing.assert_allclose(mi, mi_target) else: print(repr(mi)) mi, dmda = gmice.getMIfromGM(amps_in, PGA(), dists=None, mag=None) mi_target = np.array( [3.18167182, 4.23133858, 5.62950857, 6.73127835, 7.37577236, 7.83304814, 8.18773878, 9.28950857]) if do_test is True: np.testing.assert_allclose(mi, mi_target) assert((set(dmda) - dmda_target[PGA()]) == set()) else: print(repr(mi)) mi, dmda = gmice.getMIfromGM( amps_in + np.log(100), PGV(), dists=None, mag=None) mi_target = np.array( [3.4, 4.40195463, 5.82, 6.86457408, 7.47561075, 7.90914817, 8.24542592, 9.29]) if do_test is True: np.testing.assert_allclose(mi, mi_target) assert((set(dmda) - dmda_target[PGV()]) == set()) else: print(repr(mi)) amps, dadm = gmice.getGMfromMI(mmi_in, PGA(), dists=None, mag=None) amps_target = np.array( [-5.84194287, -4.79531328, -3.7486837, -2.69862254, -1.9436766, -1.25164283, -0.74834554, -0.1821361]) if do_test is True: np.testing.assert_allclose(amps, amps_target) assert((set(dadm) - dadm_target[PGA()]) == set()) else: print(repr(amps)) amps, dadm = gmice.getGMfromMI(mmi_in, PGV(), dists=None, mag=None) amps_target = np.array( [-1.53505673, -0.43858764, 0.65788146, 1.75845836, 2.55474139, 3.2846675, 3.81552285, 4.41273512]) if do_test is True: np.testing.assert_allclose(amps, amps_target) assert((set(dadm) - dadm_target[PGV()]) == set()) else: print(repr(amps)) if do_test is True: sdd = gmice.getGM2MIsd() assert sdd[PGA()] == 1.08 assert sdd[PGV()] == 0.98 sdd = gmice.getMI2GMsd() lnten = np.log(10.0) np.testing.assert_allclose(sdd[PGA()], 0.295 * lnten) np.testing.assert_allclose(sdd[PGV()], 0.282 * lnten) nm = gmice.getName() assert nm == 'Wald et al. (1999)' sc = gmice.getScale() assert sc == 'scale_wald99.ps' mm = gmice.getMinMax() assert mm == (1.0, 10.0) dt = gmice.getDistanceType() assert dt == 'rrup' # # This should fail because MMI() is not a valid argument to # getMIfromGM # with pytest.raises(ValueError) as e: # noqa mi, dmda = gmice.getMIfromGM(amps_in, MMI(), dists=None, mag=None)
import pytest from openquake.hazardlib.imt import PGA, PGV, MMI from shakelib.gmice.wald99 import Wald99 do_test = True # Inputs amps_in = np.log(np.array([0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0])) mmi_in = np.array([2., 3., 4., 5., 6.2, 7.3, 8.1, 9.0]) lfact = np.log10(np.e) dmda_target = {PGA(): set([x * lfact for x in [3.66, 2.20]]), PGV(): set([x * lfact for x in [3.47, 2.10]])} dadm_target = {PGA(): set([1.0 / (x * lfact) for x in [3.66, 2.20]]), PGV(): set([1.0 / (x * lfact) for x in [3.47, 2.10]])} def test_wald99(): gmice = Wald99() df = {'PGA': amps_in, 'PGV': amps_in + np.log(100)} mi = gmice.getPreferredMI(df) mi_target = np.array( [3.18167182, 4.23133858, 5.68946656, 6.84666436, 7.47561075, 7.90914817, 8.24542592, 9.29])
def test_wgrw12(): gmice = WGRW12() mi = gmice.getMIfromGM(amps_in, imt=PGA(), dists=None, mag=None) mi_target = np.array([ 3.31708696, 4.05662491, 5.76917533, 6.88298631, 7.53452397, 7.9967973, 8.35536434 ]) np.testing.assert_allclose(mi, mi_target) mi = gmice.getMIfromGM(amps_in, imt=PGV(), dists=dists, mag=None) mi_target = np.array([ 3.78, 4.48136824, 6.05, 7.00125479, 7.55770316, 7.95250957, 8.25874521 ]) np.testing.assert_allclose(mi, mi_target) mi = gmice.getMIfromGM(amps_in, imt=SA(0.3), dists=None, mag=3.0) mi_target = np.array([ 2.93592062, 3.74225554, 4.62592062, 5.34177387, 6.07079169, 6.58803805, 6.98924551 ]) np.testing.assert_allclose(mi, mi_target) mi = gmice.getMIfromGM(amps_in, imt=SA(1.0), dists=dists, mag=7.5) mi_target = np.array([ 3.49070724, 4.3808733, 5.63884012, 6.26430362, 6.49369295, 6.66102468, 6.94206372 ]) np.testing.assert_allclose(mi, mi_target) mi = gmice.getMIfromGM(amps_in, imt=SA(3.0), dists=None, mag=None) mi_target = np.array([ 4.97492371, 6.41105869, 7.98492371, 8.891024, 9.42105869, 9.79712429, 10. ]) np.testing.assert_allclose(mi, mi_target) amps = gmice.getGMfromMI(mmi_in, imt=PGA(), dists=None, mag=7.0) amps_target = np.array([ 0.14134045, 0.6243495, 2.75796695, 6.19604804, 13.07492182, 25.92605261, 42.65329774 ]) np.testing.assert_allclose(amps, amps_target) amps = gmice.getGMfromMI(mmi_in, imt=PGV(), dists=dists, mag=None) amps_target = np.array([ 0.06153407, 0.29470517, 1.41143169, 4.65287643, 11.15496866, 24.86392117, 44.53835548 ]) np.testing.assert_allclose(amps, amps_target) amps = gmice.getGMfromMI(mmi_in, imt=SA(0.3), dists=None, mag=6.0) amps_target = np.array([ 0.27938354, 1.09123124, 4.26218963, 16.53773087, 32.23524628, 59.43352318, 92.74023471 ]) np.testing.assert_allclose(amps, amps_target) amps = gmice.getGMfromMI(mmi_in, imt=SA(1.0), dists=dists, mag=8.0) amps_target = np.array([ 1.02985637e-01, 3.65288187e-01, 1.67836836e+00, 7.32931235e+00, 2.37600759e+01, 6.64349034e+01, 1.25388693e+02 ]) np.testing.assert_allclose(amps, amps_target) amps = gmice.getGMfromMI(mmi_in, imt=SA(3.0), dists=None, mag=5.0) amps_target = np.array([ 7.84170397e-03, 4.01844000e-02, 2.87579777e-01, 1.59413407e+00, 4.95042961e+00, 1.33312946e+01, 2.45840289e+01 ]) np.testing.assert_allclose(amps, amps_target) amps = gmice.getGMfromMI(mmi_in, imt=PGA(), dists=None, mag=7.0) amps_target = np.array([ 0.30003924, 1.03002347, 4.10133824, 8.91444065, 22.40984121, 50.16909188, 82.53771776 ]) np.testing.assert_allclose(amps, amps_target) sdd = gmice.getGM2MIsd() assert sdd['pga'] == 0.66 assert sdd['pgv'] == 0.63 assert sdd['psa03'] == 0.82 assert sdd['psa10'] == 0.75 assert sdd['psa30'] == 0.89 sdd = gmice.getMI2GMsd() np.testing.assert_allclose(sdd['pga'], 2.238721) np.testing.assert_allclose(sdd['pgv'], 2.39883291) np.testing.assert_allclose(sdd['psa03'], 2.7542287) np.testing.assert_allclose(sdd['psa10'], 2.951209) np.testing.assert_allclose(sdd['psa30'], 4.365158) nm = gmice.getName() assert nm == 'Worden et al. (2012)' sc = gmice.getScale() assert sc == 'scale_wgrw12.ps' mm = gmice.getMinMax() assert mm == (1.0, 10.0) dt = gmice.getDistanceType() assert dt == 'rrup'
do_test = True amps_in = np.log(np.array([0.05, 0.1, 0.2, 0.4, 0.8, 1.6])) sigmas_in = np.array([0.5, 0.55, 0.6, 0.65, 0.61, 0.7]) rrup_in = np.array([100.0, 50.0, 25.0, 12.0, 6.0, 1.0]) imc_in = [ const.IMC.RotD50, const.IMC.RotD50, const.IMC.RotD100, const.IMC.RotD50, const.IMC.RotD100, const.IMC.GMRotI50, const.IMC.AVERAGE_HORIZONTAL ] imc_out = [ const.IMC.GMRotI50, const.IMC.AVERAGE_HORIZONTAL, const.IMC.RotD50, const.IMC.GREATER_OF_TWO_HORIZONTAL, const.IMC.GREATER_OF_TWO_HORIZONTAL, const.IMC.GREATER_OF_TWO_HORIZONTAL, const.IMC.GREATER_OF_TWO_HORIZONTAL ] mags_in = np.array([5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0]) imt_in = [PGA(), PGV(), SA(0.3), SA(1.0), SA(3.05)] amps_target = np.array([[ -2.99751949, -2.30634141, -1.61516334, -0.92410122, -0.23292315, 0.45513398 ], [ -3.00428338, -2.31469074, -1.6250981, -0.9357148, -0.24612216, 0.43783666 ], [ -3.01181065, -2.32300028, -1.6341899, -0.94563493, -0.25682456, 0.42511215 ], [ -3.01695413, -2.3278757, -1.63879727, -0.94995846, -0.26088004, 0.42174957 ], [ -3.01848387, -2.32967754, -1.64087122, -0.95232054, -0.26351421, 0.41841202 ], [ -3.00567931, -2.31596465, -1.62624999, -0.93673749, -0.24702283, 0.43725142 ], [ -3.01424793, -2.32494632, -1.63564472, -0.94656959, -0.25726798, 0.42593853
class WGRW12(object): """ Implements the ground motion intensity conversion equations (GMICE) of Worden et al. (2012). References: Worden, C. B., Gerstenberger, M. C., Rhoades, D. A., & Wald, D. J. (2012). Probabilistic relationships between ground‐motion parameters and modified Mercalli intensity in California. Bulletin of the Seismological Society of America, 102(1), 204-221. """ # ----------------------------------------------------------------------- # # MMI = c2->C1 + c2->C2 * log(Y) for log(Y) <= c2->T1 # MMI = C1 + C2 * log(Y) for c2->T1 < log(Y) <= T1 # MMI = C3 + C4 * log(Y) for log(Y) > T1 # # or # # MMI = c2->C1 + c2->C2 * log(Y) + C5 + C6 * log(D) + C7 * M # for log(Y) <= c2->T1 # MMI = C1 + C2 * log(Y) + C5 + C6 * log(D) + C7 * M # for c2->T1 < log(Y) <= T1 # MMI = C3 + C4 * log(Y) + C5 + C6 * log(D) + C7 * M for log(Y) > T1 # # Limit the distance residuals to between 10 and 300 km. # Limit the magnitude residuals to between M3.0 and M7.3. # # ----------------------------------------------------------------------- __pga = PGA() __pgv = PGV() __sa03 = SA(0.3) __sa10 = SA(1.0) __sa30 = SA(3.0) __constants = { __pga: { 'C1': 1.78, 'C2': 1.55, 'C3': -1.60, 'C4': 3.70, 'C5': -0.91, 'C6': 1.02, 'C7': -0.17, 'T1': 1.57, 'T2': 4.22, 'SMMI': 0.66, 'SPGM': 0.35 }, __pgv: { 'C1': 3.78, 'C2': 1.47, 'C3': 2.89, 'C4': 3.16, 'C5': 0.90, 'C6': 0.00, 'C7': -0.18, 'T1': 0.53, 'T2': 4.56, 'SMMI': 0.63, 'SPGM': 0.38 }, __sa03: { 'C1': 1.26, 'C2': 1.69, 'C3': -4.15, 'C4': 4.14, 'C5': -1.05, 'C6': 0.60, 'C7': 0.00, 'T1': 2.21, 'T2': 4.99, 'SMMI': 0.82, 'SPGM': 0.44 }, __sa10: { 'C1': 2.50, 'C2': 1.51, 'C3': 0.20, 'C4': 2.90, 'C5': 2.27, 'C6': -0.49, 'C7': -0.29, 'T1': 1.65, 'T2': 4.98, 'SMMI': 0.75, 'SPGM': 0.47 }, __sa30: { 'C1': 3.81, 'C2': 1.17, 'C3': 1.99, 'C4': 3.01, 'C5': 1.91, 'C6': -0.57, 'C7': -0.21, 'T1': 0.99, 'T2': 4.96, 'SMMI': 0.89, 'SPGM': 0.64 } } __constants2 = { __pga: { 'C1': 1.71, 'C2': 2.08, 'T1': 0.14, 'T2': 2.0 }, __pgv: { 'C1': 4.62, 'C2': 2.17, 'T1': -1.21, 'T2': 2.0 }, __sa03: { 'C1': 1.15, 'C2': 1.92, 'T1': 0.44, 'T2': 2.0 }, __sa10: { 'C1': 2.71, 'C2': 2.17, 'T1': -0.33, 'T2': 2.0 }, __sa30: { 'C1': 7.35, 'C2': 3.45, 'T1': -1.55, 'T2': 2.0 } } DEFINED_FOR_INTENSITY_MEASURE_TYPES = set([PGA, PGV, SA]) DEFINED_FOR_SA_PERIODS = set([0.3, 1.0, 3.0]) def getMIfromGM(self, amps, imt, dists=None, mag=None): """ Function to compute macroseismic intensity from ground-motion intensity. Supported ground-motion IMTs are PGA, PGV and PSA at 0.3, 1.0, and 3.0 sec periods. Args: amps (ndarray): Ground motion amplitude; natural log units; g for PGA and PSA, cm/s for PGV. imt (OpenQuake IMT): Type the input amps (must be one of PGA, PGV, or SA). Supported SA periods are 0.3, 1.0, and 3.0 sec. `[link] <http://docs.openquake.org/oq-hazardlib/master/imt.html>` dists (ndarray): Numpy array of distances from rupture (km). mag (float): Earthquake magnitude. Returns: ndarray of Modified Mercalli Intensity and ndarray of dMMI / dln(amp) (i.e., the slope of the relationship at the point in question). """ # noqa lfact = np.log10(np.e) c, c2 = self.__getConsts(imt) if dists is not None and mag is not None: doresid = True ldd = np.log10(np.clip(dists, 10, 300)) lmm = np.clip(mag, 3.0, 7.3) else: doresid = False # # Convert (for accelerations) from ln(g) to cm/s^2 # then take the log10 # if imt != self.__pgv: units = 981.0 else: units = 1.0 # # Math: log10(981 * exp(amps)) = log10(981) + log10(exp(amps)) # = log10(981) + amps * log10(e) # For PGV, just convert ln(amp) to log10(amp) by multiplying # by log10(e) # lamps = np.log10(units) + amps * lfact mmi = np.zeros_like(amps) dmmi_damp = np.zeros_like(amps) # # This is the MMI 1 to 2 range that is discussed in the paper but not # specifically implemented there # idx = lamps < c2['T1'] mmi[idx] = c2['C1'] + c2['C2'] * lamps[idx] dmmi_damp[idx] = c2['C2'] * lfact # # This is the lower segment of the bi-linear fit # idx = (lamps >= c2['T1']) & (lamps < c['T1']) mmi[idx] = c['C1'] + c['C2'] * lamps[idx] dmmi_damp[idx] = c['C2'] * lfact # # This is the upper segment of the bi-linear fit # idx = lamps >= c['T1'] mmi[idx] = c['C3'] + c['C4'] * lamps[idx] dmmi_damp[idx] = c['C4'] * lfact if doresid: mmi += c['C5'] + c['C6'] * ldd + c['C7'] * lmm mmi = np.clip(mmi, 1.0, 10.0) return mmi, dmmi_damp def getGMfromMI(self, mmi, imt, dists=None, mag=None): """ Function to tcompute ground-motion intensity from macroseismic intensity. Supported IMTs are PGA, PGV and PSA for 0.3, 1.0, and 3.0 sec periods. Args: mmi (ndarray): Macroseismic intensity. imt (OpenQuake IMT): IMT of the requested ground-motions intensities (must be one of PGA, PGV, or SA). `[link] <http://docs.openquake.org/oq-hazardlib/master/imt.html>` dists (ndarray): Rupture distances (km) to the corresponding MMIs. mag (float): Earthquake magnitude. Returns: Ndarray of ground motion intensity in natural log of g for PGA and PSA, and natural log cm/s for PGV; ndarray of dln(amp) / dMMI (i.e., the slope of the relationship at the point in question). """ # noqa lfact = np.log10(np.e) c, c2 = self.__getConsts(imt) mmi = mmi.copy() ix_nan = np.isnan(mmi) mmi[ix_nan] = 1.0 if dists is not None and mag is not None: doresid = True ldd = np.log10(np.clip(dists, 10, 300)) lmm = np.clip(mag, 3.0, 7.3) else: doresid = False if doresid: mmi -= c['C5'] + c['C6'] * ldd + c['C7'] * lmm pgm = np.zeros_like(mmi) dpgm_dmmi = np.zeros_like(mmi) # # MMI 1 to 2 # idx = mmi < 2.0 pgm[idx] = np.power(10, (mmi[idx] - c2['C1']) / c2['C2']) dpgm_dmmi[idx] = 1.0 / (c2['C2'] * lfact) # # Lower segment of bi-linear relationship # idx = (mmi >= 2.0) & (mmi < c['T2']) pgm[idx] = np.power(10, (mmi[idx] - c['C1']) / c['C2']) dpgm_dmmi[idx] = 1.0 / (c['C2'] * lfact) # # Upper segment of bi-linear relationship # idx = mmi >= c['T2'] pgm[idx] = np.power(10, (mmi[idx] - c['C3']) / c['C4']) dpgm_dmmi[idx] = 1.0 / (c['C4'] * lfact) if imt != self.__pgv: units = 981.0 else: units = 1.0 pgm /= units pgm = np.log(pgm) pgm[ix_nan] = np.nan dpgm_dmmi[ix_nan] = np.nan return pgm, dpgm_dmmi def getGM2MIsd(self): """ Return a dictionary of standard deviations for the ground-motion to MMI conversion. The keys are the ground motion types. Returns: Dictionary of GM to MI sigmas (in MMI units). """ return { self.__pga: self.__constants[self.__pga]['SMMI'], self.__pgv: self.__constants[self.__pgv]['SMMI'], self.__sa03: self.__constants[self.__sa03]['SMMI'], self.__sa10: self.__constants[self.__sa10]['SMMI'], self.__sa30: self.__constants[self.__sa30]['SMMI'] } def getMI2GMsd(self): """ Return a dictionary of standard deviations for the MMI to ground-motion conversion. The keys are the ground motion types. Returns: Dictionary of MI to GM sigmas (ln(PGM) units). """ # # Need to convert log10 to ln units # lfact = np.log(10.0) return { self.__pga: lfact * self.__constants[self.__pga]['SPGM'], self.__pgv: lfact * self.__constants[self.__pgv]['SPGM'], self.__sa03: lfact * self.__constants[self.__sa03]['SPGM'], self.__sa10: lfact * self.__constants[self.__sa10]['SPGM'], self.__sa30: lfact * self.__constants[self.__sa30]['SPGM'] } @staticmethod def getName(): """ Get the name of this GMICE. Returns: String containing name of this GMICE. """ return 'Worden et al. (2012)' @staticmethod def getScale(): """ Get the name of the PostScript file containing this GMICE's scale. Returns: Name of GMICE scale file. """ return 'scale_wgrw12.ps' @staticmethod def getMinMax(): """ Get the minimum and maximum MMI values produced by this GMICE. Returns: Tuple of min and max values of GMICE. """ return (1.0, 10.0) @staticmethod def getDistanceType(): return 'rrup' def __getConsts(self, imt): """ Helper function to get the constants. """ if (imt != self.__pga and imt != self.__pgv and imt != self.__sa03 and imt != self.__sa10 and imt != self.__sa30): raise ValueError("Invalid IMT " + str(imt)) c = self.__constants[imt] c2 = self.__constants2[imt] return (c, c2)