Ejemplo n.º 1
0
 def test_prem(self):
     model_data = TestFunctionTaup.gen_test_model("prem")
     model_prem = SeisModel(model=model_data)
     source_prem = SourceModel(sdep=16.5)
     # * note, use larger distance will integrate more, the waveform of only calculating 10km and calculating to 100km will be grealy different
     config_prem = Config(model=model_prem,
                          source=source_prem,
                          npt=512,
                          dt=0.1,
                          receiver_distance=np.arange(10, 20, 10))
     gf = calculate_gf(config_prem)
     # # * calculate sync_prem_gcmt for the event
     event = obspy.read_events(
         join(dirname(__file__), "../data/sync_prem_gcmt/test_gcmt"))[0]
     source_prem.update_source_mechanism(event)
     # # * generate a source time function
     source_time_function = generate_source_time_function(
         4, 0.5, gf[0][0].stats.delta)
     sync_result = calculate_sync(gf, config_prem, 30, source_time_function)
     # * test if the cc value is large enough
     for index, component in enumerate(["z", "r", "t"]):
         sac_path = join(dirname(__file__),
                         f"../data/sync_prem_gcmt/prem.{component}")
         sac_wave = obspy.read(sac_path)[0].data
         coef = np.corrcoef(
             sac_wave,
             sync_result[0][index].data,
         )[0, 1]
         assert coef > 0.9999
Ejemplo n.º 2
0
    def test_free_surface(self):
        test_model_data = np.array([
            [10.5, 3.64, 6.301, 2.55, 700, 1300],
        ])
        test_model = SeisModel(test_model_data,
                               flattening=False,
                               use_kappa=False)
        test_source = SourceModel(sdep=-12, srcType="dc")
        with pytest.raises(PyfkError) as execinfo:
            _ = Config(model=test_model,
                       source=test_source,
                       receiver_distance=[10, 20, 30])
        assert str(execinfo.value
                   ) == "The source or receivers are located in the air."

        test_model_data = np.array([[5.5, 3.18, 5.501, 2.53, 600, 1100],
                                    [10.5, 3.64, 6.301, 2.55, 700, 1300],
                                    [16.0, 3.87, 6.699, 2.59, 800, 1600],
                                    [90.0, 4.50, 7.799, 2.6, 900, 1800]])
        test_model = SeisModel(test_model_data,
                               flattening=False,
                               use_kappa=False)
        test_source = SourceModel(sdep=-12, srcType="dc")
        with pytest.raises(PyfkError) as execinfo:
            _ = Config(model=test_model,
                       source=test_source,
                       receiver_distance=[10, 20, 30])
        assert str(execinfo.value
                   ) == "The source or receivers are located in the air."
Ejemplo n.º 3
0
 def test_earth_models(self):
     for earth_model_name in ["prem", "ak135f_no_mud", "1066a"]:
         earthmodel = TauPyModel(model=earth_model_name)
         for source_depth in [12, 50, 200]:
             model_data = self.gen_test_model(earth_model_name)
             test_model = SeisModel(model_data,
                                    flattening=True,
                                    use_kappa=False)
             test_source = SourceModel(sdep=source_depth, srcType="dc")
             receiver_distance = [1, 10, 50]
             test_config = Config(model=test_model,
                                  source=test_source,
                                  receiver_distance=receiver_distance,
                                  degrees=True)
             t0_list, _, _, _ = taup(
                 test_config.src_layer, test_config.rcv_layer,
                 test_config.model.th.astype(np.float64),
                 test_config.model.vp.astype(np.float64),
                 test_config.receiver_distance.astype(np.float64))
             for index, each_distance in enumerate(receiver_distance):
                 arrivals = earthmodel.get_travel_times(
                     source_depth_in_km=source_depth,
                     distance_in_degree=each_distance,
                     phase_list=["p", "P"])
                 assert np.allclose(arrivals[0].time,
                                    t0_list[index],
                                    rtol=0.01)
Ejemplo n.º 4
0
    def test_hk():
        # * perl fk.pl -Mhk/15/k -N512/0.1 10 20 30
        model_path = join(dirname(__file__), f"../data/hk")
        model_data = np.loadtxt(model_path)
        model_hk = SeisModel(model=model_data, use_kappa=True)
        source_hk = SourceModel(sdep=15)
        config_hk = Config(model=model_hk,
                           source=source_hk,
                           npt=512,
                           dt=0.1,
                           receiver_distance=[10, 20, 30])

        result = calculate_gf(config_hk)
        # * for all the gf in data/hk_gf, test if the results are close (in FK, it uses float but we are using double)
        for irec, each_rec in enumerate([10, 20, 30]):
            for icomn in range(9):
                hk_gf_data = obspy.read(
                    join(dirname(__file__),
                         f"../data/hk_gf/{each_rec}.grn.{icomn}"))[0]
                coef = np.corrcoef(
                    hk_gf_data.data,
                    result[irec][icomn].data,
                )[0, 1]
                if np.isnan(coef):
                    coef = 1.0
                assert coef > 0.99
Ejemplo n.º 5
0
 def test_topo(self):
     test_model_data = np.array([
         [-5.5, 3.18, 5.501, 2.53, 600, 1100],
         [10.5, 3.64, 6.301, 2.55, 700, 1300],
         [16.0, 3.87, 6.699, 2.59, 800, 1600],
         [90.0, 4.50, 7.799, 2.6, 900, 1800]
     ])
     test_model = SeisModel(
         test_model_data, flattening=False, use_kappa=False)
     test_source = SourceModel(sdep=12, srcType="dc")
     test_config = Config(
         model=test_model,
         source=test_source,
         receiver_distance=[
             10,
             20,
             30])
     newmodel = np.array([
         [0., 3.18, 5.501, 2.53, 600, 1100],
         [5.5, 3.64, 6.301, 2.55, 700, 1300],
         [5, 3.64, 6.301, 2.55, 700, 1300],
         [7.0, 3.87, 6.699, 2.59, 800, 1600],
         [9.0, 3.87, 6.699, 2.59, 800, 1600],
         [0., 4.50, 7.799, 2.6, 900, 1800]
     ])
     assert np.all(test_config.model.model_values == newmodel)
Ejemplo n.º 6
0
 def test_big_array(self):
     # model_data = TestFunctionTaup.gen_test_model("prem")
     # there is a possibility that we write x=f(x) where x is a memoryview in the code
     # this might cause segmentation fault
     model_data = np.loadtxt(join(dirname(__file__), f"../data/hk"))
     model_hk = SeisModel(model=model_data)
     source_hk = SourceModel(sdep=16.5)
     config_hk = Config(model=model_hk,
                        source=source_hk,
                        npt=512,
                        dt=0.1,
                        receiver_distance=np.arange(10, 40, 10))
     _ = calculate_gf(config_hk)
Ejemplo n.º 7
0
 def test__flattening(self):
     test_model = SeisModel(TestClassSeisModel.test_model_6column,
                            flattening=True,
                            use_kappa=False)
     test_source = SourceModel(sdep=12, srcType="dc")
     test_config = Config(model=test_model,
                          source=test_source,
                          receiver_distance=[10, 20, 30],
                          rdep=22)
     assert test_config.source.sdep == R_EARTH * \
         np.log(R_EARTH / (R_EARTH - 12))
     assert test_config.rdep == R_EARTH * \
         np.log(R_EARTH / (R_EARTH - 22))
Ejemplo n.º 8
0
 def test_exceptions(self):
     model_data = TestFunctionTaup.gen_test_model("prem")
     model_prem = SeisModel(model=model_data)
     source_prem = SourceModel(sdep=16.5)
     # * note, use larger distance will integrate more, the waveform of only calculating 10km and calculating to 100km will be grealy different
     # ! note receiver_distance can not be 0
     config_prem = Config(model=model_prem,
                          source=source_prem,
                          npt=512,
                          dt=0.1,
                          receiver_distance=np.arange(1, 10))
     gf = calculate_gf(config_prem)
     event = obspy.read_events(
         join(dirname(__file__), "../data/sync_prem_gcmt/test_gcmt"))[0]
     source_prem.update_source_mechanism(event)
     source_time_function = generate_source_time_function(
         4, 0.5, gf[0][0].stats.delta)
     # * the main tests
     with pytest.raises(PyfkError) as execinfo:
         _ = calculate_sync(gf, config_prem, [30], source_time_function)
     assert str(execinfo.value) == "az must be a number"
     with pytest.raises(PyfkError) as execinfo:
         _ = calculate_sync(gf, config_prem, 30, None)
     assert str(execinfo.value) == "must provide a source time function"
     with pytest.raises(PyfkError) as execinfo:
         source_time_function_abnormal = generate_source_time_function(
             4, 0.5, 1.2)
         _ = calculate_sync(None, config_prem, 30,
                            source_time_function_abnormal)
     assert str(execinfo.value) == "check input Green's function"
     with pytest.raises(PyfkError) as execinfo:
         source_time_function_abnormal = generate_source_time_function(
             4, 0.5, 1.2)
         _ = calculate_sync(gf, config_prem, 30,
                            source_time_function_abnormal)
     assert str(
         execinfo.value
     ) == "delta for the source time function and the Green's function should be the same"
Ejemplo n.º 9
0
 def test_catch_warning(self):
     test_model = SeisModel(TestClassSeisModel.test_model_6column,
                            flattening=False,
                            use_kappa=False)
     test_source = SourceModel(sdep=12, srcType="dc")
     # dk
     with pytest.warns(PyfkWarning) as execinfo:
         _ = Config(model=test_model,
                    source=test_source,
                    receiver_distance=[10, 20, 30],
                    dk=0.05)
     assert len(execinfo[0].message.args) == 1
     assert execinfo[0].message.args[
         0] == "dk is recommended to be within (0.1,0.4)"
Ejemplo n.º 10
0
 def test_static_source(self):
     model_data = TestFunctionTaup.gen_test_model("prem")
     model_prem = SeisModel(model=model_data)
     source_prem = SourceModel(sdep=16.5, srcType="dc")
     config_prem = Config(model=model_prem,
                          source=source_prem,
                          npt=1,
                          dt=1,
                          receiver_distance=[50])
     gf = calculate_gf(config_prem)
     ref_gf = [
         -0.242E-06, -0.103E-05, 0.000E+00, 0.236E-06, 0.118E-05,
         -0.548E-07, -0.942E-07, -0.156E-05, 0.285E-06
     ]
     coef = np.corrcoef(
         gf,
         ref_gf,
     )[0, 1]
     assert coef > 0.99999
Ejemplo n.º 11
0
 def test_prem_sf(self):
     model_data = TestFunctionTaup.gen_test_model("prem")
     model_prem = SeisModel(model=model_data)
     source_prem = SourceModel(sdep=16.5, srcType="sf")
     config_prem = Config(model=model_prem,
                          source=source_prem,
                          npt=512,
                          dt=1,
                          receiver_distance=[50])
     gf = calculate_gf(config_prem)
     for index, comnname in enumerate(range(6)):
         gf_data = obspy.read(
             join(dirname(__file__),
                  f"../data/sync_prem_sf/50.grn.{comnname}"))[0]
         coef = np.corrcoef(
             gf_data.data,
             gf[0][index].data,
         )[0, 1]
         if np.isnan(coef):
             coef = 1.
         assert coef > 0.99
Ejemplo n.º 12
0
 def test_radiation_pattern(self):
     with pytest.raises(PyfkError) as execinfo:
         _ = SourceModel(sdep=12, srcType="dc", source_mechanism=[1])
     assert str(
         execinfo.value) == "length of source_mechanism is not correct"
     with pytest.raises(PyfkError) as execinfo:
         _ = SourceModel(sdep=12, srcType="sf", source_mechanism=[1, 1])
     assert str(
         execinfo.value) == "length of source_mechanism is not correct"
     with pytest.raises(PyfkError) as execinfo:
         _ = SourceModel(sdep=12, srcType="sf",
                         source_mechanism=[[1, 2], [3, 4]])
     assert str(
         execinfo.value) == "source_mechanism should be a 1D array"
     with pytest.raises(PyfkError) as execinfo:
         _ = SourceModel(sdep=12, srcType="sf",
                         source_mechanism=(1, 2, 3, 4))
     assert str(
         execinfo.value) == "source_mechanism must be None, a list or numpy.ndarray"
     with pytest.raises(PyfkError) as execinfo:
         test_source = SourceModel(sdep=12, srcType="sf")
         test_source.update_source_mechanism(None)
     assert str(
         execinfo.value) == "source mechanism couldn't be None"
     # * test the case of 1, 3, 4
     test_source = SourceModel(sdep=12, srcType="ep", source_mechanism=[1])
     test_source.calculate_radiation_pattern(30.)
     test_source = SourceModel(
         sdep=12,
         srcType="sf",
         source_mechanism=[
             1,
             1,
             1])
     test_source.calculate_radiation_pattern(30.)
     test_source = SourceModel(
         sdep=12, srcType="dc", source_mechanism=[
             1, 1, 1, 1])
     test_source.calculate_radiation_pattern(30.)
     test_source = SourceModel(
         sdep=12, srcType="dc", source_mechanism=[
             1, 1, 1, 1, 1, 1, 1])
Ejemplo n.º 13
0
 def test_catch_exceptions(self):
     test_model = SeisModel(
         TestClassSeisModel.test_model_6column,
         flattening=False,
         use_kappa=False)
     test_source = SourceModel(sdep=12, srcType="dc")
     # receiver_distance
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model, source=test_source)
     assert str(
         execinfo.value) == "Must provide a list of receiver distance"
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model, source=test_source, receiver_distance=np.arange(10))
     assert str(
         execinfo.value) == "Can't set receiver distance as 0, please consider to use a small value instead"
     # taper
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             taper=-0.2)
     assert str(
         execinfo.value) == "Taper must be with (0,1)"
     # npt
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             npt=-4)
     assert str(
         execinfo.value) == "npt should be positive."
     # dt
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             dt=-0.4)
     assert str(
         execinfo.value) == "dt should be positive."
     # dk
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             dk=0.7)
     assert str(
         execinfo.value) == "dk should be within (0,0.5)"
     # smth
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             smth=0)
     assert str(
         execinfo.value) == "smth should be positive."
     # pmin
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             pmin=1.2)
     assert str(
         execinfo.value) == "pmin should be within [0,1]"
     # pmax
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             pmax=1.2)
     assert str(
         execinfo.value) == "pmax should be within [0,1]"
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             pmax=0.3,
             pmin=0.8)
     assert str(
         execinfo.value) == "pmin should be smaller than pmax"
     # kmax
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             kmax=1.2)
     assert str(
         execinfo.value) == "kmax should be larger or equal to 10"
     # updn
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             updn="bbq")
     assert str(
         execinfo.value) == "the selection of phases should be either 'up', 'down' or 'all'"
     # samples_before_first_arrival
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source,
             receiver_distance=[
                 10,
                 20,
                 30],
             samples_before_first_arrival=-
             12)
     assert str(
         execinfo.value) == "samples_before_first_arrival should be positive"
     # source and receiver
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             source=test_source, receiver_distance=[10, 20, 30])
     assert str(
         execinfo.value) == "Must provide a seisModel"
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=1, source=test_source, receiver_distance=[10, 20, 30])
     assert str(
         execinfo.value) == "Must provide a seisModel"
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model, receiver_distance=[10, 20, 30])
     assert str(
         execinfo.value) == "Must provide a source"
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             source=1, model=test_model, receiver_distance=[10, 20, 30])
     assert str(
         execinfo.value) == "Must provide a source"
     # source located at real interface
     test_source_interface = SourceModel(sdep=16, srcType="dc")
     with pytest.raises(PyfkError) as execinfo:
         _ = Config(
             model=test_model,
             source=test_source_interface,
             receiver_distance=[
                 10,
                 20,
                 30])
     assert str(
         execinfo.value) == "The source is located at a real interface."
Ejemplo n.º 14
0
    def test_init(self):
        test_model = SeisModel(
            TestClassSeisModel.test_model_6column,
            flattening=False,
            use_kappa=False)
        test_source = SourceModel(sdep=12, srcType="dc")
        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30])
        newmodel = np.array([
            [5.5, 3.18, 5.501, 2.53, 600, 1100],
            [6.5, 3.64, 6.301, 2.55, 700, 1300],
            [4, 3.64, 6.301, 2.55, 700, 1300],
            [16.0, 3.87, 6.699, 2.59, 800, 1600],
            [0, 4.50, 7.799, 2.6, 900, 1800]
        ])
        assert np.all(test_config.model.model_values == newmodel)

        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30],
            rdep=16,
            degrees=True)
        receiver_distance_km = [degrees2kilometers(
            10), degrees2kilometers(20), degrees2kilometers(30)]
        assert np.allclose(test_config.receiver_distance, receiver_distance_km)

        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30],
            rdep=16)
        assert np.all(test_config.model.model_values == newmodel)

        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30],
            rdep=30)
        newmodel = np.array([
            [5.5, 3.18, 5.501, 2.53, 600, 1100],
            [6.5, 3.64, 6.301, 2.55, 700, 1300],
            [4, 3.64, 6.301, 2.55, 700, 1300],
            [14.0, 3.87, 6.699, 2.59, 800, 1600],
            [2.0, 3.87, 6.699, 2.59, 800, 1600],
            [0, 4.50, 7.799, 2.6, 900, 1800]
        ])
        assert np.all(test_config.model.model_values == newmodel)

        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30],
            rdep=13)
        newmodel = np.array([
            [5.5, 3.18, 5.501, 2.53, 600, 1100],
            [6.5, 3.64, 6.301, 2.55, 700, 1300],
            [1, 3.64, 6.301, 2.55, 700, 1300],
            [3, 3.64, 6.301, 2.55, 700, 1300],
            [16.0, 3.87, 6.699, 2.59, 800, 1600],
            [0, 4.50, 7.799, 2.6, 900, 1800]
        ])
        assert np.all(test_config.model.model_values == newmodel)

        test_config = Config(
            model=test_model,
            source=test_source,
            receiver_distance=[
                10,
                20,
                30],
            rdep=7)
        newmodel = np.array([
            [5.5, 3.18, 5.501, 2.53, 600, 1100],
            [1.5, 3.64, 6.301, 2.55, 700, 1300],
            [5, 3.64, 6.301, 2.55, 700, 1300],
            [4, 3.64, 6.301, 2.55, 700, 1300],
            [16.0, 3.87, 6.699, 2.59, 800, 1600],
            [0, 4.50, 7.799, 2.6, 900, 1800]
        ])
        assert np.all(test_config.model.model_values == newmodel)
Ejemplo n.º 15
0
 def test_catch_exceptions(self):
     with pytest.raises(PyfkError) as execinfo:
         _ = SourceModel(sdep=12, srcType="bbq")
     assert str(
         execinfo.value) == "Source type should be one of 'dc', 'sf', or 'ep'."
Ejemplo n.º 16
0
 def test_set_sdep(self):
     test_model = SourceModel(sdep=12, srcType="dc")
     test_model.sdep = 13
     assert test_model._sdep == 13
     assert test_model.sdep == 13
Ejemplo n.º 17
0
 def test_init(self):
     test_model = SourceModel(sdep=12, srcType="dc")
     assert test_model.sdep == 12
     assert test_model.srcType == "dc"