def test_amplify_by_tf(self): #---------- Case 1: An artificial transfer function ------------------- gm = GM('./files/sample_accel.txt', unit='gal') ratio_benchmark = 2.76 freq = np.arange(0.01, 50, step=0.01) tf = ratio_benchmark * np.ones_like(freq) transfer_function = Frequency_Spectrum(np.column_stack((freq, tf))) new_gm = gm.amplify_by_tf(transfer_function, show_fig=False) ratio = new_gm.accel[:, 1] / gm.accel[:, 1] self.assertTrue(np.allclose(ratio, ratio_benchmark)) #---------- Case 2: A transfer function from a Vs profile ------------- vs_prof = Vs_Profile('./files/profile_FKSH14.txt') tf_RO, tf_BH, _ = vs_prof.get_transfer_function() gm_with_tf_RO = gm.amplify_by_tf(tf_RO) gm_with_tf_BH = gm.amplify_by_tf(tf_BH) gm_with_tf_RO_ = gm.amplify(vs_prof, boundary='elastic') gm_with_tf_BH_ = gm.amplify(vs_prof, boundary='rigid') # Assert that `amplify_by_tf()` and `amplify()` can generate # nearly identical results self.assertTrue( self.nearly_identical(gm_with_tf_RO.accel, gm_with_tf_RO_.accel)) self.assertTrue( self.nearly_identical(gm_with_tf_BH.accel, gm_with_tf_BH_.accel))
def test_scale_motion(self): data = np.array([1, 3, 7, -2, -10, 0]) gm = GM(data, unit='g', dt=0.1) gm_scaled_1 = gm.scale_motion(factor=2.0) # scale by 2.0 gm_scaled_2 = gm.scale_motion(target_PGA_in_g=5.0) # scale by 0.5 self.assertTrue(np.allclose(gm.accel[:, 1] * 2, gm_scaled_1.accel[:, 1])) self.assertTrue(np.allclose(gm.accel[:, 1] * 0.5, gm_scaled_2.accel[:, 1]))
def test_plot(self): # Test that the desired data are correctly imported to the object accel_in = Ground_Motion(_join(f_dir, 'sample_accel.txt'), unit='m/s/s') accel_tmp = accel_in.accel.copy() accel_tmp[:, 1] *= 5.0 accel_out = Ground_Motion(accel_tmp, unit='m/s/s') vs_profile = Vs_Profile(_join(f_dir, 'profile_FKSH14.txt')) thk = vs_profile._thk depth_bound = sr.thk2dep(thk, midpoint=False) depth_midpoint = sr.thk2dep(thk, midpoint=True) max_a_v_d = np.column_stack( (depth_bound, depth_bound * 1, depth_bound * 2, depth_bound * 3)) max_gamma_tau = np.column_stack( (depth_midpoint, depth_midpoint * 1, depth_midpoint * 2)) tf_RO, _, _ = vs_profile.get_transfer_function() sim_results = Simulation_Results(accel_in, accel_out, vs_profile, max_a_v_d=max_a_v_d, max_strain_stress=max_gamma_tau, trans_func=tf_RO) sim_results.plot(save_fig=False) # Test that it can produce a plot without max profiles and trans. func. sim_results_ = Simulation_Results(accel_in, accel_out, vs_profile) sim_results_.plot(save_fig=False)
def test_amplify_by_tf__case_1_an_artificial_transfer_function(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='gal') ratio_benchmark = 2.76 freq = np.arange(0.01, 50, step=0.01) tf = ratio_benchmark * np.ones_like(freq) transfer_function = Frequency_Spectrum(np.column_stack((freq, tf))) new_gm = gm.amplify_by_tf(transfer_function, show_fig=False) ratio = new_gm.accel[:, 1] / gm.accel[:, 1] self.assertTrue(np.allclose(ratio, ratio_benchmark))
def test_unit_convert(self): data = np.array([1, 3, 7, -2, -10, 0]) gm = GM(data, unit='m', dt=0.1) accel = gm.accel[:, 1] accel_in_m = gm._unit_convert(unit='m/s/s')[:, 1] accel_in_gal = gm._unit_convert(unit='gal')[:, 1] accel_in_g = gm._unit_convert(unit='g')[:, 1] self.assertTrue(np.allclose(accel_in_m, accel)) self.assertTrue(np.allclose(accel_in_gal, accel * 100)) self.assertTrue(np.allclose(accel_in_g, accel / 9.81))
def test_filters(self): gm = GM('./files/sample_accel.txt', unit='m') hp = gm.highpass(cutoff_freq=1.0, show_fig=True) lp = gm.lowpass(cutoff_freq=1.0, show_fig=True) bp = gm.bandpass(cutoff_freq=[0.5, 8], show_fig=True) bs = gm.bandstop(cutoff_freq=[0.5, 8], show_fig=True) self.assertTrue(isinstance(hp, GM)) self.assertTrue(isinstance(lp, GM)) self.assertTrue(isinstance(bp, GM)) self.assertTrue(isinstance(bs, GM))
def test_fourier_transform(self): gm = GM(_join(f_dir, 'two_column_data_example.txt'), unit='m/s/s') freq, spec = gm.get_Fourier_spectrum(real_val=False).raw_data.T freq_bench = [0.6667, 1.3333, 2.0000, 2.6667, 3.3333, 4.0000, 4.6667, 5.3333] FS_bench = [60.0000 + 0.0000j, -1.5000 + 7.0569j, -1.5000 + 3.3691j, -7.5000 +10.3229j, -1.5000 + 1.3506j, -1.5000 + 0.8660j, -7.5000 + 2.4369j, -1.5000 + 0.1577j] self.assertTrue(np.allclose(freq, freq_bench, atol=0.0001, rtol=0.0)) self.assertTrue(np.allclose(spec, FS_bench, atol=0.0001, rtol=0.0))
def test_plot(self): filename = _join(f_dir, 'sample_accel.txt') gm = GM(filename, unit='m') fig, axes = gm.plot() # automatically generate fig/ax objects self.assertTrue(isinstance(axes, tuple)) self.assertEqual(len(axes), 3) self.assertEqual(axes[0].title.get_text(), os.path.split(filename)[1]) fig2 = plt.figure(figsize=(8, 8)) fig2_, axes = gm.plot(fig=fig2) # feed an external figure object self.assertTrue(np.allclose(fig2_.get_size_inches(), (8, 8)))
def test_equiv_linear(self): soil_profile = Vs_Profile('./files/profile_FKSH14.txt') input_motion = Ground_Motion('./files/sample_accel.txt', unit='gal') curves = Multiple_GGmax_Damping_Curves(data='./files/curve_FKSH14.txt') equiv_lin_sim = Equiv_Linear_Simulation(soil_profile, input_motion, curves, boundary='elastic') output = equiv_lin_sim.run(show_fig=True) max_v = output.max_a_v_d[:, 2] max_v_benchmark = [ 0.404085, 0.403931, 0.402998, 0.399842, 0.390005, 0.389336, 0.388176, 0.386563, 0.384449, 0.381578, 0.377044, 0.371955, 0.366454, 0.36505, 0.363536, 0.361912, 0.360214, 0.358405, 0.356464, 0.354368, 0.352104, 0.349671, 0.347186, 0.344585, 0.342999, 0.344762, 0.345623, 0.346219, 0.34607, 0.344739, 0.342724, 0.339295, 0.334776, 0.32954, 0.324481, 0.319054, 0.317311, 0.316842, 0.318159, 0.319668, 0.321421, 0.323498, 0.325626, 0.326861, 0.328234, 0.328466, 0.327704, 0.326466, 0.323216, 0.322324, 0.320209, 0.316914, 0.312529, 0.308906, 0.304708, 0.300202, 0.295505, 0.29226, 0.289536, 0.287653, 0.287429, 0.290265, 0.292502 ] # from MATLAB SeismoSoil tol = 0.01 # FFT of scipy and MATLAB are different, hence a lenient tolerance self.assertTrue(np.allclose(max_v, max_v_benchmark, rtol=tol, atol=0.0))
def test_run__normal_case(self): gm_in = Ground_Motion(_join(f_dir, 'sample_accel.txt'), unit='gal') vs30 = 207 z1 = 892 sea = Site_Effect_Adjustment(gm_in, vs30, z1) gm_out = sea.run(show_fig=True, dpi=150) self.assertTrue(isinstance(gm_out, Ground_Motion))
def test_run__out_of_bound_z1_lenient_case(self): gm_in = Ground_Motion(_join(f_dir, 'sample_accel.txt'), unit='gal') sea1 = Site_Effect_Adjustment(gm_in, 360, 927, lenient=True) sea2 = Site_Effect_Adjustment(gm_in, 360, 900) motion_out1 = sea1.run() motion_out2 = sea2.run() self.assertTrue(np.allclose(motion_out1.accel, motion_out2.accel))
def test_init(self): # Case 1: Not a list with self.assertRaises(TypeError, msg='`list_of_simulations` should be a list.'): Batch_Simulation(1.4) # Case 2: A list of 0 length with self.assertRaises(ValueError, msg='should have at least one element'): Batch_Simulation([]) # Case 3: Wrong type with self.assertRaises( TypeError, msg='Elements of `list_of_simulations` should be of type'): Batch_Simulation([1, 2, 3]) # Case 4: Inhomogeneous element type with self.assertRaises(TypeError, msg='should be of the same type'): gm = Ground_Motion('./files/sample_accel.txt', unit='gal') prof = Vs_Profile('./files/profile_FKSH14.txt') mgdc = Multiple_GGmax_Damping_Curves(data='./files/curve_P001.txt') lin_sim = Linear_Simulation(gm, prof) equiv_sim = Equiv_Linear_Simulation(gm, prof, mgdc) Batch_Simulation([lin_sim, equiv_sim])
def test_amplify_by_tf__case_2_a_transfer_function_from_a_Vs_profile(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='gal') vs_prof = Vs_Profile(_join(f_dir, 'profile_FKSH14.txt')) tf_RO, tf_BH, _ = vs_prof.get_transfer_function() gm_with_tf_RO = gm.amplify_by_tf(tf_RO) gm_with_tf_BH = gm.amplify_by_tf(tf_BH) gm_with_tf_RO_ = gm.amplify(vs_prof, boundary='elastic') gm_with_tf_BH_ = gm.amplify(vs_prof, boundary='rigid') # Assert that `amplify_by_tf()` and `amplify()` can generate # nearly identical results self.assertTrue(self.nearly_identical(gm_with_tf_RO.accel, gm_with_tf_RO_.accel)) self.assertTrue(self.nearly_identical(gm_with_tf_BH.accel, gm_with_tf_BH_.accel))
def test_deconvolution(self): ''' Test that `deconvolve()` and `amplify()` are reverse operations to each other. ''' gm = GM(_join(f_dir, 'sample_accel.txt'), unit='m') vs_prof = Vs_Profile(_join(f_dir, 'profile_FKSH14.txt')) for boundary in ['elastic', 'rigid']: deconv_motion = gm.deconvolve(vs_prof, boundary=boundary) output_motion = deconv_motion.amplify(vs_prof, boundary=boundary) self.assertTrue(self.nearly_identical(gm.accel, output_motion.accel)) amplified_motion = gm.amplify(vs_prof, boundary=boundary) output_motion = amplified_motion.deconvolve(vs_prof, boundary=boundary) self.assertTrue(self.nearly_identical(gm.accel, output_motion.accel))
def test_nonlinear_init(self): input_motion = Ground_Motion('./files/sample_accel.txt', unit='m') soil_profile = Vs_Profile('./files/profile_FKSH14.txt') HH_G = HH_Param_Multi_Layer('./files/HH_G_FKSH14.txt') HH_x = HH_Param_Multi_Layer('./files/HH_X_FKSH14.txt') # this should succeed Nonlinear_Simulation(soil_profile, input_motion, G_param=HH_G, xi_param=HH_x, boundary='elastic') # this should fail with ValueError HH_G_data = HH_G.param_list HH_x_data = HH_x.param_list HH_G_ = HH_Param_Multi_Layer(HH_G_data[:-1]) # exclude one layer HH_x_ = HH_Param_Multi_Layer(HH_x_data[:-1]) # exclude one layer with self.assertRaises(ValueError, msg='Not enough sets of parameters'): Nonlinear_Simulation(soil_profile, input_motion, G_param=HH_G_, xi_param=HH_x) with self.assertRaises(ValueError, msg='Not enough sets of parameters'): Nonlinear_Simulation(soil_profile, input_motion, G_param=HH_G, xi_param=HH_x_)
def test_init__case_4_inhomogeneous_element_type(self): with self.assertRaisesRegex(TypeError, 'should be of the same type'): gm = Ground_Motion(_join(f_dir, 'sample_accel.txt'), unit='gal') prof = Vs_Profile(_join(f_dir, 'profile_FKSH14.txt')) mgdc = Multiple_GGmax_Damping_Curves( data=_join(f_dir, 'curve_FKSH14.txt')) lin_sim = Linear_Simulation(prof, gm) equiv_sim = Equiv_Linear_Simulation(prof, gm, mgdc) Batch_Simulation([lin_sim, equiv_sim])
def test_equiv_linear(self): gm_raw = Ground_Motion('./files/sample_accel.txt', unit='gal') # Make a very weak motion to speed up equivalent linear calculation gm = gm_raw.scale_motion(target_PGA_in_g=0.001) prof_2 = Vs_Profile('./files/profile_P001.txt') mgdc_2 = Multiple_GGmax_Damping_Curves(data='./files/curve_P001.txt') sim_2 = Equiv_Linear_Simulation(prof_2, gm, mgdc_2, boundary='elastic') sim_list = [sim_2] batch_sim = Batch_Simulation(sim_list) options = dict(show_fig=False, save_txt=False, verbose=True) non_par_results = batch_sim.run(parallel=False, options=options) par_results = batch_sim.run(parallel=True, n_cores=2, options=options) accel_out_0_non_par = non_par_results[0].accel_on_surface.accel accel_out_0_par = par_results[0].accel_on_surface.accel self.assertTrue( np.allclose(accel_out_0_non_par, accel_out_0_par, atol=0.0, rtol=1e-3))
def test_linear(self): input_motion = Ground_Motion('./files/sample_accel.txt', unit='m') soil_profile = Vs_Profile('./files/profile_FKSH14.txt') ls = Linear_Simulation(soil_profile, input_motion) sim_result = ls.run(every_layer=True, show_fig=True) output = sim_result.accel_on_surface self.assertEqual(output.accel.shape, input_motion.accel.shape) self.assertEqual(output.dt, input_motion.dt) self.assertEqual(output.npts, input_motion.npts) sim_result_ = ls.run(every_layer=False, show_fig=False) output_ = sim_result_.accel_on_surface # Check that two algorithms produce nearly identical results nearly_identical = Test_Class_Ground_Motion.nearly_identical self.assertTrue( nearly_identical(output.accel, output_.accel, thres=0.99))
def test_run(self): gm_in = Ground_Motion('./files/sample_accel.txt', unit='gal') vs30 = 207 z1 = 892 sea = Site_Effect_Adjustment(gm_in, vs30, z1) gm_out = sea.run(show_fig=True, dpi=150) self.assertTrue(isinstance(gm_out, Ground_Motion)) # Test out-of-bound Vs30 (lenient cases) sea1 = Site_Effect_Adjustment(gm_in, 170, 75, lenient=True) sea2 = Site_Effect_Adjustment(gm_in, 175, 75) motion_out1 = sea1.run() motion_out2 = sea2.run() self.assertTrue(np.allclose(motion_out1.accel, motion_out2.accel)) # Test out-of-bound z1 (lenient cases) sea1 = Site_Effect_Adjustment(gm_in, 360, 927, lenient=True) sea2 = Site_Effect_Adjustment(gm_in, 360, 900) motion_out1 = sea1.run() motion_out2 = sea2.run() self.assertTrue(np.allclose(motion_out1.accel, motion_out2.accel))
def test_nonlinear(self): accel_data = np.genfromtxt('./files/sample_accel.txt') accel_downsample = accel_data[::50] # for faster testing speed gm = Ground_Motion(accel_downsample, unit='gal') prof = Vs_Profile('./files/profile_FKSH14.txt') hh_g = HH_Param_Multi_Layer('./files/HH_G_FKSH14.txt') hh_x = HH_Param_Multi_Layer('./files/HH_X_FKSH14.txt') sim = Nonlinear_Simulation(prof, gm, G_param=hh_g, xi_param=hh_x) batch_sim = Batch_Simulation([sim]) options = dict(show_fig=False, save_txt=False, remove_sim_dir=True) non_par_results = batch_sim.run(parallel=False, options=options) par_results = batch_sim.run(parallel=True, n_cores=2, options=options) accel_out_0_non_par = non_par_results[0].accel_on_surface.accel accel_out_0_par = par_results[0].accel_on_surface.accel self.assertTrue( np.allclose(accel_out_0_non_par, accel_out_0_par, atol=0.0, rtol=1e-3))
def test_linear(self): gm = Ground_Motion('./files/sample_accel.txt', unit='gal') prof_1 = Vs_Profile('./files/profile_FKSH14.txt') prof_2 = Vs_Profile('./files/profile_P001.txt') sim_1 = Linear_Simulation(prof_1, gm, boundary='elastic') sim_2 = Linear_Simulation(prof_2, gm, boundary='elastic') sim_list = [sim_1, sim_2] batch_sim = Batch_Simulation(sim_list) options = dict(show_fig=False, save_txt=False, verbose=True) non_par_results = batch_sim.run(parallel=False, n_cores=2, options=options) par_results = batch_sim.run(parallel=True, options=options) accel_out_1_non_par = non_par_results[1].accel_on_surface.accel accel_out_1_par = par_results[1].accel_on_surface.accel self.assertTrue( np.allclose(accel_out_1_non_par, accel_out_1_par, atol=0.0, rtol=1e-3))
def test_low_pass_filter(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='m') lp = gm.lowpass(cutoff_freq=1.0, show_fig=True) self.assertTrue(isinstance(lp, GM))
def test_init(self): gm = Ground_Motion('./files/sample_accel.txt', unit='gal') vs30 = 250 z1 = 150 Site_Effect_Adjustment(gm, vs30, z1)
def test_init(self): gm = Ground_Motion(_join(f_dir, 'sample_accel.txt'), unit='gal') vs30 = 250 z1 = 150 Site_Effect_Adjustment(gm, vs30, z1)
def test_baseline_correction(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='m/s/s') corrected = gm.baseline_correct(show_fig=True) self.assertTrue(isinstance(corrected, GM))
def test_amplify_via_profile(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='m') vs_prof = Vs_Profile(_join(f_dir, 'profile_FKSH14.txt')) output_motion = gm.amplify(vs_prof, boundary='elastic') self.assertTrue(isinstance(output_motion, GM))
def test_band_stop_filter(self): gm = GM(_join(f_dir, 'sample_accel.txt'), unit='m') bs = gm.bandstop(cutoff_freq=[0.5, 8], show_fig=True) self.assertTrue(isinstance(bs, GM))
def test_baseline_correction(self): gm = GM('./files/sample_accel.txt', unit='m/s/s') corrected = gm.baseline_correct(show_fig=True) self.assertTrue(isinstance(corrected, GM))