def get_model_syntest1_prem(): types = [ParameterType.VSH] radii = np.array([3480. + 100 * i for i in range(5)]) model_params = ModelParameters(types, radii, mesh_type='boxcar') model = SeismicModel.prem().boxcar_mesh(model_params) values = np.array( [.2 * (-1)**i if i < 2 else 0. for i in range(model_params.get_n_grd_params())]) values_dict = {param_type: values for param_type in types} values_mat = model_params.get_values_matrix(values_dict) model_updated = model.multiply(values_mat) return model_updated
def get_model_syntest1(): model_ref = SeismicModel.ak135() types = [ParameterType.VSH] radii = np.array([3479.5, 3680., 3880.], dtype='float') model_params = ModelParameters(types, radii, mesh_type='boxcar') model, mesh = model_ref.boxcar_mesh(model_params) values = np.array( [.2 * (-1)**i for i in range(model_params.get_n_grd_params())]) values_dict = {param_type: values for param_type in types} values_mat = model_params.get_values_matrix(values_dict) model_updated = mesh.multiply(values_mat) return model_updated
def get_model_syntest1(): model_ref = SeismicModel.ak135() types = [ParameterType.VSH] radii = 6371. - np.array([493.33, 410.]) model_params = ModelParameters(types, radii, mesh_type='boxcar') model, mesh = model_ref.boxcar_mesh(model_params) values = np.array( [.2 * (-1)**i for i in range(model_params._n_grd_params)]) values_dict = {param_type: values for param_type in types} values_mat = model_params.get_values_matrix(values_dict) mesh_mul = mesh.multiply(model_params.get_nodes(), values_mat) model_mul = model + mesh_mul return model_mul
def get_model(n_upper_mantle=20, n_mtz=10, n_lower_mantle=12, types=[ParameterType.VSH], verbose=0): '''Boxcar mesh using ak135 as reference model for the structure of the upper mantle and transition zone down to 1000 km depth. ''' ak135 = SeismicModel.ak135() # model parameters depth_moho = 6371. - 6336.6 depth_410 = 410. depth_660 = 660. depth_max = 1000. rs_upper_mantle = np.linspace(depth_410, depth_moho, n_upper_mantle + 1) rs_mtz = np.linspace(depth_660, depth_410, n_mtz, endpoint=False) rs_lower_mantle = np.linspace(depth_max, depth_660, n_lower_mantle, endpoint=False) radii = 6371. - np.round( np.hstack((rs_lower_mantle, rs_mtz, rs_upper_mantle)), 4) if verbose > 1: print('dr_um={}, dr_mtz={}, dr_lm={}'.format( rs_upper_mantle[1] - rs_upper_mantle[0], rs_mtz[1] - rs_mtz[0], rs_lower_mantle[1] - rs_lower_mantle[0])) model_params = ModelParameters(types, radii, mesh_type='boxcar') # mesh # model, mesh = ak135.boxcar_mesh(model_params) return ak135, model_params
def get_model_syntest_cmb_topo(): model_ref = SeismicModel.ak135() types = [ParameterType.VSV, ParameterType.RADIUS] radii = np.array([3479.5, 3679.5], dtype='float') model_params = ModelParameters( types, radii, mesh_type='lininterp') model = model_ref.lininterp_mesh( model_params, discontinuous=True) # set D'' layer to constant velocity and density depth_dpp = 2691.5 idpp = model.get_zone(3479.5) for p_type in [ ParameterType.VSH, ParameterType.VSV, ParameterType.VPH, ParameterType.VPV, ParameterType.RHO]: v_dpp = model.get_value_at(6371.-depth_dpp, p_type) model.set_value( idpp, p_type, np.array([v_dpp, 0., 0., 0.])) values = np.array([0.2, 0.2]) values_dict = { ParameterType.VSV: values, ParameterType.RADIUS: np.array([-8., 0.])} model_mul = model.build_model(model, model_params, values_dict) return model_mul
def std_boxcar_mesh( n_upper_mantle=20, n_mtz=10, n_lower_mantle=12, n_dpp=6, types=[ParameterType.VSH], verbose=0): '''Boxcar mesh using ak135 as reference model for the structure of the upper mantle and transition zone down to 1000 km depth. ''' model_ref = SeismicModel.prem() # model parameters depth_moho = 6371. - 6336.6 depth_410 = 410. depth_660 = 660. depth_dpp = 2491. depth_cmb = 2891. rs_upper_mantle = np.linspace(depth_410, depth_moho, n_upper_mantle) rs_mtz = np.linspace(depth_660, depth_410, n_mtz, endpoint=(n_upper_mantle==0)) rs_lower_mantle = np.linspace( depth_dpp, depth_660, n_lower_mantle, endpoint=(n_mtz==0)) rs_dpp = np.linspace( depth_cmb, depth_dpp, n_dpp, endpoint=(n_lower_mantle==0)) radii = 6371. - np.round( np.hstack((rs_lower_mantle, rs_mtz, rs_upper_mantle, rs_dpp)), 4) if verbose > 1: print('dr_um={}, dr_mtz={}, dr_lm={}'.format( rs_upper_mantle[1] - rs_upper_mantle[0], rs_mtz[1] - rs_mtz[0], rs_lower_mantle[1] - rs_lower_mantle[0])) model_params = ModelParameters(types, radii, mesh_type='boxcar') # mesh # model, mesh = ak135.boxcar_mesh(model_params) return model_ref, model_params
def get_model_syntest_vshvsv_4(): model_ref = SeismicModel.prem() types = [ParameterType.VSH, ParameterType.VSV] radii = np.linspace(3480., 3980., 3, endpoint=True) model_params = ModelParameters( types, radii, mesh_type='lininterp') model = model_ref.lininterp_mesh( model_params) values = np.zeros(model_params.get_n_grd_params()) values[2] = model_ref.get_value_at(radii[1], ParameterType.VSH) * 0.03 value_dict = { ParameterType.VSH: values, ParameterType.VSV: -values } model_updated = model.build_model( model, model_params, value_dict) return model_updated
def get_model_syntest1_prem_vshvsv(): types = [ParameterType.VSH, ParameterType.VSV] radii = np.array([3480. + 20 * i for i in range(21)]) model_params = ModelParameters(types, radii, mesh_type='boxcar') model = SeismicModel.prem().boxcar_mesh(model_params) values_vsh = np.array( [0.2 * (-1)**(i//5) if i <= 9 else 0. for i in range(model_params.get_n_grd_params())]) values_vsv = np.array( [-0.2 * (-1) ** (i // 5) if i <= 4 else 0. for i in range(model_params.get_n_grd_params())] ) values_dict = { ParameterType.VSH: values_vsh, ParameterType.VSV: values_vsv } values_mat = model_params.get_values_matrix(values_dict) model_updated = model.multiply(values_mat) return model_updated
def single_layer_dpp(): """Create objects for a single-layer D'' model. Returns: SeismicModel: reference model ModelParameters: model parameters dict: range dict """ # model parameters model_ref = ak135_lin_dpp() types = [ParameterType.VSH, ParameterType.RADIUS] radii = np.array([3479.5, 3679.5]) model_params = ModelParameters(types, radii, mesh_type='lininterp') # Defines constraints to model parameters mask_dict = dict() mask_dict[ParameterType.VSH] = np.ones(model_params._n_grd_params, dtype='bool') mask_dict[ParameterType.RADIUS] = np.ones(model_params._n_grd_params, dtype='bool') mask_dict[ParameterType.RADIUS][[0, 1]] = False equal_dict = dict() equal_dict[ParameterType.VSH] = np.arange(model_params._n_grd_params, dtype='int') equal_dict[ParameterType.VSH][2] = 0 equal_dict[ParameterType.VSH][3] = 1 discon_arr = np.zeros(model_params._n_nodes, dtype='bool') discon_arr[1] = True model_params.set_constraints(mask_dict=mask_dict, equal_dict=equal_dict, discon_arr=discon_arr) # Defines value range for model parameters delta_dict = {ParameterType.VSH: 0.5, ParameterType.RADIUS: 190.} range_dict = get_range_dict(model_params, delta_dict) return model_ref, model_params, range_dict
def get_model_lininterp(n_mtz=10, n_lower_mantle=12, types=[ParameterType.VSH], discontinuous=True, verbose=0): """Boxcar mesh using ak135 as reference model for the structure of the upper mantle and transition zone down to 1000 km depth. """ ak135 = SeismicModel.ak135() radii = np.array([5371, 5611, 5711, 5836, 5961, 6161, 6251, 6336.6]) model_params = ModelParameters(types, radii, mesh_type='lininterp') # mesh model = ak135.lininterp_mesh(model_params, discontinuous=discontinuous) return model, model_params
def get_model_lininterp( n_upper_mantle=20, n_mtz=10, n_lower_mantle=12, n_dpp=8, types=[ParameterType.VSH], discontinuous=False, verbose=0): """Boxcar mesh using ak135 as reference model for the structure of the upper mantle and transition zone down to 1000 km depth. """ ak135 = SeismicModel.ak135() # model parameters depth_moho = 6371. - 6336.6 depth_410 = 410. depth_660 = 660. depth_dpp = 2691.5 depth_cmb = 2891.5 rs_upper_mantle = np.linspace(depth_410, depth_moho, n_upper_mantle) rs_mtz = np.linspace(depth_660, depth_410, n_mtz, endpoint=(n_upper_mantle==0)) rs_lower_mantle = np.linspace( depth_dpp, depth_660, n_lower_mantle, endpoint=(n_mtz==0)) rs_dpp = np.linspace( depth_cmb, depth_dpp, n_dpp, endpoint=(n_lower_mantle==0)) radii = 6371. - np.round( np.hstack((rs_dpp, rs_lower_mantle, rs_mtz, rs_upper_mantle)), 4) if verbose > 1: print('dr_um={}, dr_mtz={}, dr_lm={}, dr_dpp={}'.format( rs_upper_mantle[1] - rs_upper_mantle[0], rs_mtz[1] - rs_mtz[0], rs_lower_mantle[1] - rs_lower_mantle[0], rs_dpp[1] - rs_dpp[0])) model_params = ModelParameters(types, radii, mesh_type='lininterp') # mesh model = ak135.lininterp_mesh(model_params, discontinuous=discontinuous) # set D'' layer to constant velocity and density idpp = model.get_zone(3479.5) for p_type in [ ParameterType.VSH, ParameterType.VSV, ParameterType.VPH, ParameterType.VPV, ParameterType.RHO]: v_dpp = model.get_value_at(6371.-depth_dpp, p_type) model.set_value( idpp, p_type, np.array([v_dpp, 0., 0., 0.])) return model, model_params
def get_model_syntest4(): model_ref = SeismicModel.ak135() types = [ParameterType.VSH] radii = np.array([3479.5, 3489.5], dtype='float') model_params = ModelParameters( types, radii, mesh_type='lininterp') model = model_ref.lininterp_mesh( model_params, discontinuous=True) izone = model.get_zone(3479.5) vsh_dpp = model.get_value_at(radii[-1], ParameterType.VSH) model._vsh[:, izone] = np.array([vsh_dpp, 0., 0., 0.]) # model._vsh[:, izone+1] = np.array([vsh_dpp, 0., 0., 0.]) values_p = np.array([0.2, 0.2]) values_dict_p = {param_type: values_p for param_type in types} values_m = np.array([0., 0.]) values_dict_m = {param_type: values_m for param_type in types} model_mul = model.build_model( model, model_params, values_dict_p) return model_mul
else: ratio = np.nan return variance, corr, ratio def check_data(data, syn, var, corr, ratio): var_, corr_, ratio_ = misfits(data, syn) return (np.isfinite(var_) and np.isfinite(corr_) and np.isfinite(ratio_) and var_ <= var and corr_ >= corr and 1 / ratio <= ratio_ <= ratio) if __name__ == '__main__': types = [ParameterType.VSH] radii = [3480. + 20 * i for i in range(41)] model_params = ModelParameters(types=types, radii=radii, mesh_type='boxcar') model = SeismicModel.prem().boxcar_mesh(model_params) n_param = model_params.get_n_params() its, itypes, igrds = model_params.get_free_all_indices() order_types = [model_params.get_types()[itypes[i]] for i in its] order_radii = [model_params.get_grd_params()[igrds[i]] for i in its] # sac_files = glob.glob(os.path.join(root_sac, '*[RZT]')) # dataset = Dataset.dataset_from_sac( # sac_files, headonly=False) t_before = 20. t_after = 40. sampling_hz = 20
depth_410, n_mtz, endpoint=(n_upper_mantle == 0)) rs_lower_mantle = np.linspace(depth_max, depth_660, n_lower_mantle, endpoint=(n_mtz == 0)) radii = 6371. - np.round( np.hstack((rs_lower_mantle, rs_mtz, rs_upper_mantle)), 4) # print('dr_um={}, dr_mtz={}, dr_lm={}'.format( # rs_upper_mantle[1] - rs_upper_mantle[0], # rs_mtz[1] - rs_mtz[0], # rs_lower_mantle[1] - rs_lower_mantle[0])) # radii = np.insert(radii, 2, 5700.) model_params = ModelParameters(types, radii, mesh_type='lininterp') model_ = ak135.lininterp_mesh(model_params, discontinuous=True) values_m = np.array( [0. if i % 2 == 1 else 0. for i in range(model_params._n_grd_params)]) values = np.array( [0. if i % 2 == 1 else 0. for i in range(model_params._n_grd_params)]) values[2] = -0.1 values[3] = 0 values_r = np.zeros(model_params._n_grd_params, dtype=np.float64) values_r[2] = -20 values_r[3] = -0 values_dict = {ParameterType.VSH: values, ParameterType.RADIUS: values_r} values_dict_m = { ParameterType.VSH: values_m,
arr1 = np.sin(x) shifts = [i * np.pi / 10 for i in range(11)] arrs = [np.sin(x + s) for s in shifts] roll_vars = [ rolling_variance(arr1, arrs[i], 500, 250) for i in range(len(arrs))] vars = [variance(arr1, arrs[i]) for i in range(len(arrs))] plt.plot(shifts, roll_vars) plt.plot(shifts, vars) plt.show() types = [ParameterType.VSH, ParameterType.VPH] radii = np.array([5701., 5971.]) model_params = ModelParameters(types, radii) model = SeismicModel.prem() range_dict = {ParameterType.VSH: [-0.15, 0.15], ParameterType.VPH: [-0.1, 0.1]} umc = UniformMonteCarlo( model, model_params, range_dict, mesh_type='triangle', seed=0) sample_models = umc.sample_models(50) fig, ax = sample_models[0].plot( types=[ParameterType.VSH, ParameterType.VPH]) for sample in sample_models[1:]: sample.plot( ax=ax, types=[ParameterType.VSH, ParameterType.VPH]) ax.get_legend().remove()
import numpy as np from mpi4py import MPI import os import sys comm = MPI.COMM_WORLD n_core = comm.Get_size() rank = comm.Get_rank() input_file = sys.argv[1] # model parameters n_nodes = 3 types = [ParameterType.VSH, ParameterType.VSV, ParameterType.RADIUS] radii = np.linspace(3480., 3980., n_nodes, endpoint=True) model_params = ModelParameters(types, radii, mesh_type='lininterp') model = SeismicModel.prem().lininterp_mesh(model_params) # set D'' layer to constant velocity and density for r in radii[:-1]: izone = model.get_zone(r) for p_type in ParameterType.structure_types(): v_dpp = model.get_value_at(radii[-1], p_type) model.set_value( izone, p_type, np.array([v_dpp, 0., 0., 0.])) # constraints to parameters mask_dict = dict() for p_type in ParameterType.structure_types(): mask_dict[p_type] = np.zeros( model_params.get_n_grd_params(), dtype='bool')
"""Test script for building seismic models using a discontinuous linear spline parametrization. """ from dsmpy.modelparameters import ModelParameters, ParameterType from dsmpy.seismicmodel import SeismicModel import numpy as np import matplotlib.pyplot as plt if __name__ == '__main__': types = [ParameterType.VSH, ParameterType.RADIUS] radii = 6371. - np.array([2891.5, 2691.5]) model_ref = SeismicModel.ak135() model_params = ModelParameters(types, radii, mesh_type='lininterp') model = model_ref.lininterp_mesh(model_params, discontinuous=True) # constraints to parameters mask_dict = dict() mask_dict[ParameterType.VSH] = np.ones(model_params._n_grd_params, dtype='bool') mask_dict[ParameterType.RADIUS] = np.ones(model_params._n_grd_params, dtype='bool') mask_dict[ParameterType.RADIUS][[0, 1]] = False equal_dict = dict() equal_dict[ParameterType.VSH] = np.arange(model_params._n_grd_params, dtype='int') equal_dict[ParameterType.VSH][2] = 0 equal_dict[ParameterType.VSH][3] = 1
get_model_syntest1_prem_vshvsv().plot(types=types, ax=ax, label='target') model.plot(types=self.model_params.get_types(), ax=ax, label='update') nodes = model_params.get_nodes() ax.set_ylim([nodes.min(), nodes.max()]) ax.set_xlim([6.5, 8]) ax.legend() fig.savefig(figname, bbox_inches='tight') if __name__ == '__main__': types = [ParameterType.VSH, ParameterType.VSV] radii = [3480. + 20 * i for i in range(21)] model_params = ModelParameters(types=types, radii=radii, mesh_type='boxcar') model_ref = SeismicModel.prem().boxcar_mesh(model_params) t_before = 20. t_after = 40. sampling_hz = 20 mode = 2 freq = 0.01 freq2 = 0.04 phases = ['ScS'] window_file = 'windows.pkl' dataset, output = get_dataset(get_model_syntest1_prem_vshvsv(), tlen=1638.4, nspc=256,