def test_get_mfd(self):
        '''
        Tests the main function to get the magnitude frequency distribution
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}
        self.model.setUp(self.config)
        # Same fault case as for test_cumulative_value - now comparing incremenetal
        # rates
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        output1 = self.model.get_mfd(1.0, 7200.)
        np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2], output1[2])

        # Test case for when b > 1.5 - should produce array of NaN
        self.model = YoungsCoppersmithExponential()
        self.config['b_value'] = [1.6, 0.1]
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        _, _, _ = self.model.get_mfd(1.0, 7200.)
        self.assertTrue(np.all(np.isnan(self.model.occurrence_rate)))
    def test_get_mmax(self):
        '''
        Tests the function to get Mmax
        Values come from WC1994 (tested in openquake.hazardlib) - only
        functionality is tested for here!
        '''
        # Case 1 MMmax and uncertainty specified in config
        self.config['Maximum_Magnitude'] = 8.0
        self.config['Maximum_Magnitude_Uncertainty'] = 0.2

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 8.0)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.2)

        # Case 2: Mmax and uncertainty not specified in config
        self.config['Maximum_Magnitude'] = None
        self.config['Maximum_Magnitude_Uncertainty'] = None

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 7.9880073)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.23)
    def test_cumulative_value(self):
        '''
        Tests the calculation of the cumulative rate M > Mref
        Data and tests taken from Bungum (2007). Current parameters should
        reproduce Model 4 line in Figure 1 of Bungum (2007)
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}

        # Test case 1 - Fault of 120 km length and 60 km width (7200 km ** 2 area)
        # b-value 1, slip = 1 mm/yr
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        moment_rate = (30. * 1E9) * (7200. * 1E6) * (1.0 / 1000.)
        momax = _scale_moment(self.model.mmax, in_nm=True)
        beta = log(10.)
        expected_result = YC_EXP_DATA[:, 1]
        mags = np.arange(5.0, 8.1, 0.1)
        for iloc, mag in enumerate(mags):
            self.assertAlmostEqual(
                expected_result[iloc],
                self.model.cumulative_value(mag, moment_rate, beta, momax))
 def setUp(self):
     '''
     '''
     self.model = YoungsCoppersmithExponential()
     self.msr = WC1994()
     self.config = {'MFD_spacing': 0.1,
                    'Maximum_Magnitude': 8.0,
                    'Maximum_Magnitude_Uncertainty': None,
                    'Minimum_Magnitude': 5.0,
                    'Model_Weight': 1.0,
                    'b_value': [1.0, 0.1]}
 def test_return_oq_mfd(self):
     """
     Tests the function to return the mfd as an instance of the openquake
     EvenlyDiscretizedMFD class
     """
     self.model = YoungsCoppersmithExponential()
     self.config = {'MFD_spacing': 0.1,
                    'Maximum_Magnitude': 8.0,
                    'Maximum_Magnitude_Uncertainty': None,
                    'Minimum_Magnitude': 5.0,
                    'Model_Weight': 1.0,
                    'b_value': [1.0, 0.1]}
     self.model.setUp(self.config)
     # Same fault case as for test_cumulative_value - now comparing incremenetal
     # rates
     self.model.get_mmax(self.config, self.msr, 0., 7200.)
     _ = self.model.get_mfd(1.0, 7200.)
     oq_mfd = self.model.to_evenly_discretized_mfd()
     self.assertTrue(isinstance(oq_mfd, EvenlyDiscretizedMFD))
     self.assertAlmostEqual(oq_mfd.min_mag, 5.05)
     self.assertAlmostEqual(oq_mfd.bin_width, 0.1)
     np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2],
                                          np.array(oq_mfd.occurrence_rates))
def build_fault_model(area, slip, mmax, config={}, mu=30.0, coupling=1.0):
    """
    
    """
    if not config:
        # Simple Dirac function
        geological_moment = coupling * (mu * 1.0E9) * (area * 1.0E6) * (slip *
                                                                        1.0E-3)
        mmax_moment = moment_function(mmax)
        rate = geological_moment / mmax_moment
        mf_dist = EvenlyDiscretizedMFD(mmax, 0.1, [rate])
        return get_incremental_cumulative(mf_dist)

    d_m = config["MFD_spacing"]
    config["Model_Weight"] = 1.0

    if config["model"] == "Characteristic":
        # Try characteristic
        for key in ["MFD_spacing", "Lower_Bound", "Upper_Bound", "Sigma"]:
            # Verify everything is present
            assert key in config and config[key] is not None
        rec_model = Characteristic()
        rec_model.setUp(config)
        rec_model.mmax = mmax
    elif config["model"] == "Hybrid":
        # Try Hybrid
        for key in ["Minimum_Magnitude", "b_value"]:
            # Verify everything is present
            assert key in config and config[key] is not None
        rec_model = YoungsCoppersmithCharacteristic()
        rec_model.setUp(config)
        rec_model.mmax = mmax
    elif config["model"] == "Exponential":
        # Try exponential
        for key in ["Minimum_Magnitude", "b_value"]:
            # Verify everything is present
            assert key in config and config[key] is not None
        rec_model = YoungsCoppersmithExponential()
        rec_model.setUp(config)
        rec_model.mmax = mmax
        rec_model.mmin += (config["MFD_spacing"] / 2.)
    else:
        raise ValueError("Model %s not recognised" % config["model"])

    mmin, bin_width, rate = rec_model.get_mfd(slip, area, mu)
    mf_dist = EvenlyDiscretizedMFD(mmin, bin_width, rate.tolist())
    return get_incremental_cumulative(mf_dist)
 def test_return_oq_mfd(self):
     """
     Tests the function to return the mfd as an instance of the openquake
     EvenlyDiscretizedMFD class
     """
     self.model = YoungsCoppersmithExponential()
     self.config = {'MFD_spacing': 0.1,
                    'Maximum_Magnitude': 8.0,
                    'Maximum_Magnitude_Uncertainty': None,
                    'Minimum_Magnitude': 5.0,
                    'Model_Weight': 1.0,
                    'b_value': [1.0, 0.1]}
     self.model.setUp(self.config)
     # Same fault case as for test_cumulative_value - now comparing incremenetal
     # rates
     self.model.get_mmax(self.config, self.msr, 0., 7200.)
     _ = self.model.get_mfd(1.0, 7200.)
     oq_mfd = self.model.to_evenly_discretized_mfd()
     self.assertTrue(isinstance(oq_mfd, EvenlyDiscretizedMFD))
     self.assertAlmostEqual(oq_mfd.min_mag, 5.05)
     self.assertAlmostEqual(oq_mfd.bin_width, 0.1)
     np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2],
                                          np.array(oq_mfd.occurrence_rates))
Пример #8
0
class TestYoungsCoppersmithExponential(unittest.TestCase):
    '''
    Tests the :class: YoungsCoppersmithExponential
    '''
    def setUp(self):
        '''
        '''
        self.model = YoungsCoppersmithExponential()
        self.msr = WC1994()
        self.config = {
            'MFD_spacing': 0.1,
            'Maximum_Magnitude': 8.0,
            'Maximum_Magnitude_Uncertainty': None,
            'Minimum_Magnitude': 5.0,
            'Model_Weight': 1.0,
            'b_value': [1.0, 0.1]
        }

    def test_class_setup(self):
        '''
        Tests the basic setup of the class
        '''
        expected_dict = {
            'b_value': 1.0,
            'b_value_sigma': 0.1,
            'bin_width': 0.1,
            'mfd_model': 'Youngs & Coppersmith Exponential',
            'mfd_weight': 1.0,
            'mmax': None,
            'mmax_sigma': None,
            'mmin': 5.0,
            'occurrence_rate': None
        }

        self.model.setUp(self.config)
        self.assertDictEqual(expected_dict, self.model.__dict__)

    def test_get_mmax(self):
        '''
        Tests the function to get Mmax
        Values come from WC1994 (tested in openquake.hazardlib) - only
        functionality is tested for here!
        '''
        # Case 1 MMmax and uncertainty specified in config
        self.config['Maximum_Magnitude'] = 8.0
        self.config['Maximum_Magnitude_Uncertainty'] = 0.2

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 8.0)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.2)

        # Case 2: Mmax and uncertainty not specified in config
        self.config['Maximum_Magnitude'] = None
        self.config['Maximum_Magnitude_Uncertainty'] = None

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 7.9880073)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.23)

    def test_cumulative_value(self):
        '''
        Tests the calculation of the cumulative rate M > Mref
        Data and tests taken from Bungum (2007). Current parameters should
        reproduce Model 4 line in Figure 1 of Bungum (2007)
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {
            'MFD_spacing': 0.1,
            'Maximum_Magnitude': 8.0,
            'Maximum_Magnitude_Uncertainty': None,
            'Minimum_Magnitude': 5.0,
            'Model_Weight': 1.0,
            'b_value': [1.0, 0.1]
        }

        # Test case 1 - Fault of 120 km length and 60 km width (7200 km ** 2 area)
        # b-value 1, slip = 1 mm/yr
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        moment_rate = (30. * 1E9) * (7200. * 1E6) * (1.0 / 1000.)
        momax = _scale_moment(self.model.mmax, in_nm=True)
        beta = log(10.)
        expected_result = YC_EXP_DATA[:, 1]
        mags = np.arange(5.0, 8.1, 0.1)
        for iloc, mag in enumerate(mags):
            self.assertAlmostEqual(
                expected_result[iloc],
                self.model.cumulative_value(mag, moment_rate, beta, momax))

    def test_get_mfd(self):
        '''
        Tests the main function to get the magnitude frequency distribution
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {
            'MFD_spacing': 0.1,
            'Maximum_Magnitude': 8.0,
            'Maximum_Magnitude_Uncertainty': None,
            'Minimum_Magnitude': 5.0,
            'Model_Weight': 1.0,
            'b_value': [1.0, 0.1]
        }
        self.model.setUp(self.config)
        # Same fault case as for test_cumulative_value - now comparing incremenetal
        # rates
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        output1 = self.model.get_mfd(1.0, 7200.)
        np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2], output1[2])

        # Test case for when b > 1.5 - should produce array of NaN
        self.model = YoungsCoppersmithExponential()
        self.config['b_value'] = [1.6, 0.1]
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        _, _, _ = self.model.get_mfd(1.0, 7200.)
        self.assertTrue(np.all(np.isnan(self.model.occurrence_rate)))

    def test_return_oq_mfd(self):
        """
        Tests the function to return the mfd as an instance of the openquake
        EvenlyDiscretizedMFD class
        """
        self.model = YoungsCoppersmithExponential()
        self.config = {
            'MFD_spacing': 0.1,
            'Maximum_Magnitude': 8.0,
            'Maximum_Magnitude_Uncertainty': None,
            'Minimum_Magnitude': 5.0,
            'Model_Weight': 1.0,
            'b_value': [1.0, 0.1]
        }
        self.model.setUp(self.config)
        # Same fault case as for test_cumulative_value - now comparing incremenetal
        # rates
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        _ = self.model.get_mfd(1.0, 7200.)
        oq_mfd = self.model.to_evenly_discretized_mfd()
        self.assertTrue(isinstance(oq_mfd, EvenlyDiscretizedMFD))
        self.assertAlmostEqual(oq_mfd.min_mag, 5.05)
        self.assertAlmostEqual(oq_mfd.bin_width, 0.1)
        np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2],
                                             np.array(oq_mfd.occurrence_rates))
class TestYoungsCoppersmithExponential(unittest.TestCase):
    '''
    Tests the :class: YoungsCoppersmithExponential
    '''

    def setUp(self):
        '''
        '''
        self.model = YoungsCoppersmithExponential()
        self.msr = WC1994()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}

    def test_class_setup(self):
        '''
        Tests the basic setup of the class
        '''
        expected_dict = {'b_value': 1.0,
                         'b_value_sigma': 0.1,
                         'bin_width': 0.1,
                         'mfd_model': 'Youngs & Coppersmith Exponential',
                         'mfd_weight': 1.0,
                         'mmax': None,
                         'mmax_sigma': None,
                         'mmin': 5.0,
                         'occurrence_rate': None}

        self.model.setUp(self.config)
        self.assertDictEqual(expected_dict, self.model.__dict__)

    def test_get_mmax(self):
        '''
        Tests the function to get Mmax
        Values come from WC1994 (tested in openquake.hazardlib) - only
        functionality is tested for here!
        '''
        # Case 1 MMmax and uncertainty specified in config
        self.config['Maximum_Magnitude'] = 8.0
        self.config['Maximum_Magnitude_Uncertainty'] = 0.2

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 8.0)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.2)

        # Case 2: Mmax and uncertainty not specified in config
        self.config['Maximum_Magnitude'] = None
        self.config['Maximum_Magnitude_Uncertainty'] = None

        self.model = YoungsCoppersmithExponential()
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 8500.)
        self.assertAlmostEqual(self.model.mmax, 7.9880073)
        self.assertAlmostEqual(self.model.mmax_sigma, 0.23)

    def test_cumulative_value(self):
        '''
        Tests the calculation of the cumulative rate M > Mref
        Data and tests taken from Bungum (2007). Current parameters should
        reproduce Model 4 line in Figure 1 of Bungum (2007)
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}

        # Test case 1 - Fault of 120 km length and 60 km width (7200 km ** 2 area)
        # b-value 1, slip = 1 mm/yr
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        moment_rate = (30. * 1E9) * (7200. * 1E6) * (1.0 / 1000.)
        momax = _scale_moment(self.model.mmax, in_nm=True)
        beta = log(10.)
        expected_result = YC_EXP_DATA[:, 1]
        mags = np.arange(5.0, 8.1, 0.1)
        for iloc, mag in enumerate(mags):
            self.assertAlmostEqual(
                expected_result[iloc],
                self.model.cumulative_value(mag, moment_rate, beta, momax))

    def test_get_mfd(self):
        '''
        Tests the main function to get the magnitude frequency distribution
        '''
        self.model = YoungsCoppersmithExponential()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}
        self.model.setUp(self.config)
        # Same fault case as for test_cumulative_value - now comparing incremenetal
        # rates
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        output1 = self.model.get_mfd(1.0, 7200.)
        np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2], output1[2])

        # Test case for when b > 1.5 - should produce array of NaN
        self.model = YoungsCoppersmithExponential()
        self.config['b_value'] = [1.6, 0.1]
        self.model.setUp(self.config)
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        _, _, _ = self.model.get_mfd(1.0, 7200.)
        self.assertTrue(np.all(np.isnan(self.model.occurrence_rate)))

    def test_return_oq_mfd(self):
        """
        Tests the function to return the mfd as an instance of the openquake
        EvenlyDiscretizedMFD class
        """
        self.model = YoungsCoppersmithExponential()
        self.config = {'MFD_spacing': 0.1,
                       'Maximum_Magnitude': 8.0,
                       'Maximum_Magnitude_Uncertainty': None,
                       'Minimum_Magnitude': 5.0,
                       'Model_Weight': 1.0,
                       'b_value': [1.0, 0.1]}
        self.model.setUp(self.config)
        # Same fault case as for test_cumulative_value - now comparing incremenetal
        # rates
        self.model.get_mmax(self.config, self.msr, 0., 7200.)
        _ = self.model.get_mfd(1.0, 7200.)
        oq_mfd = self.model.to_evenly_discretized_mfd()
        self.assertTrue(isinstance(oq_mfd, EvenlyDiscretizedMFD))
        self.assertAlmostEqual(oq_mfd.min_mag, 5.05)
        self.assertAlmostEqual(oq_mfd.bin_width, 0.1)
        np.testing.assert_array_almost_equal(YC_EXP_DATA[:, 2],
                                             np.array(oq_mfd.occurrence_rates))