def ak135_lin_dpp(): model = SeismicModel.ak135() r0 = 4000. model = model._add_boundary(r0) izone = model.get_zone(r0 - 1e-5) vp_cmb = model.get_value_at(3479.5, ParameterType.VPH) vp_r0 = model.get_value_at(r0, ParameterType.VPH) vp_cmb_p = vp_cmb + 0.09 vs_cmb = model.get_value_at(3479.5, ParameterType.VSH) vs_r0 = model.get_value_at(r0, ParameterType.VSH) x_cmb = 3479.5 / 6371 x0 = r0 / 6371 poly_vp = model._lin_element(x_cmb, x0, vp_cmb_p, vp_r0) poly_vs = model._lin_element(x_cmb, x0, vs_cmb, vs_r0) for p_type in [ParameterType.VPH, ParameterType.VPV]: model.set_value(izone, p_type, poly_vp) model.set_value(izone - 1, p_type, poly_vp) for p_type in [ParameterType.VSH, ParameterType.VSV]: model.set_value(izone, p_type, poly_vs) model.set_value(izone - 1, p_type, poly_vs) return model
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_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 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_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_output(tlen=1638.4, nspc=64, sampling_hz=20, mode=0): catalog = read_catalog() event = Event.event_from_catalog(catalog, '200707211534A') stations = [ Station('{:03d}'.format(i), 'DSM', event.latitude, event.longitude + i) for i in range(12, 36) ] model = SeismicModel.ak135() pydsm_input = PyDSMInput.input_from_arrays(event, stations, model, tlen, nspc, sampling_hz) pydsm_output = compute(pydsm_input, mode=mode) pydsm_output.to_time_domain() return pydsm_output
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_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 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
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 if __name__ == '__main__': model_syntest2 = get_model_syntest2() fig, ax = model_syntest2.plot() SeismicModel.ak135().plot(ax=ax, label='ak135') plt.show() # plt.savefig('model_syntest2.pdf') # plt.show() # plt.close(fig) # fig, ax = get_model_syntest4().plot(types=[ParameterType.VSH]) # SeismicModel.ak135().plot(types=[ParameterType.VSH], ax=ax, label='ak135') # plt.show() model_, model_params = get_model_lininterp(0, 0, 0, 2, discontinuous=True) value_dict = { ParameterType.VSH: np.array([0.2, 0.2]), ParameterType.RADIUS: np.array([0., -8.])} value_dict_m = {
def main(path): """Compute synthetics in parallel. Args: path (str): path to an input file. """ comm = MPI.COMM_WORLD rank = comm.Get_rank() # open log file log = open('log', 'w', buffering=1) if rank == 0: input_file = PyDSMInputFile(path) params = input_file.read() seismic_model = SeismicModel.model_from_name(params['seismic_model']) tlen = params['tlen'] nspc = params['nspc'] sampling_hz = params['sampling_hz'] mode = params['mode'] verbose = params['verbose'] start_time = time.time() dataset = Dataset.dataset_from_sac(params['sac_files'], verbose=verbose) end_time = time.time() if verbose >= 1: log.write( 'Initalizing dataset finished in {} s\n'.format(end_time - start_time)) else: params = None dataset = None seismic_model = None tlen = None nspc = None sampling_hz = None mode = None verbose = None # run pydsm start_time = time.time() outputs = dsm.compute_dataset_parallel(dataset, seismic_model, tlen, nspc, sampling_hz, mode=mode, verbose=verbose, log=log) end_time = time.time() log.write('rank {}: DSM finished in {} s\n'.format(rank, end_time - start_time)) if rank == 0: start_time = time.time() for output in outputs: output.set_source_time_function(None) output.to_time_domain() output.write(params['output_folder'], format='sac') end_time = time.time() log.write('finished FFT and writing in {} s\n'.format( rank, end_time - start_time)) log.close() return "Done!"
from dsmpy.seismicmodel import SeismicModel import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec 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() result = InversionResult.load(sys.argv[1]) loaded = True model_ref = SeismicModel.ak135() types = [ParameterType.VSH, ParameterType.RADIUS] figscale = 1. # compute best models if rank == 0: indices_better = [0] for imod in range(1, result.meta['n_mod']): i_best = result.get_indices_best_models(n_best=1, n_mod=imod+1) if i_best != indices_better[-1]: indices_better.append(imod) models = [result.models[i] for i in indices_better] else: models = None outputs = result.compute_models(models)
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 window_npts = int((t_before + t_after) * 20) tlen = 1638.4
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() plt.show()
print('Results saved to \'{}\''.format(result_path)) fig, ax = dataset.plot_event(0, windows, component=Component.T, align_zero=True, color='black') cycler = plt.rcParams['axes.prop_cycle'] for imod, sty in enumerate(cycler[:n_block]): _, ax = outputs[imod][0].plot_component(Component.T, windows, ax=ax, align_zero=True, **sty) plt.savefig('profiles_syntest2.pdf') plt.close(fig) # plt.show() fig, ax = result.plot_models(types=[ParameterType.VSH], n_best=3) # fig, ax = work_parameters.get_model_syntest1().plot( # types=[ParameterType.VSH], ax=ax, color='red') fig, ax = work_parameters.get_model_syntest2().plot( types=[ParameterType.VSH], ax=ax, color='red') fig, ax = SeismicModel.ak135().plot(types=[ParameterType.VSH], ax=ax, color='blue') ax.set(ylim=[model_params._radii[0] - 100, 6371]) ax.get_legend().remove() plt.savefig('recovered_models_syntest2.pdf') # plt.show() plt.close(fig)
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') for p_type in types:
def get_dataset_ref(tlen=1638.4, nspc=256, sampling_hz=20, mode=0): return get_dataset( SeismicModel.ak135(), tlen, nspc, sampling_hz, mode)[0]
def get_dataset_syntest3(tlen=1638.4, nspc=256, sampling_hz=20, mode=0, add_noise=False, noise_normalized_std=1.): return get_dataset(get_model_syntest3(), tlen, nspc, sampling_hz, mode, add_noise, noise_normalized_std) if __name__ == '__main__': model, model_params = get_model_lininterp( types=[ParameterType.VSH, ParameterType.RADIUS]) fig, ax = get_model_syntest3().plot() SeismicModel.ak135().plot(ax=ax) plt.show() dataset, _ = get_dataset_syntest3(nspc=64, mode=2) dataset.plot_event(0) plt.show() fig, ax = SeismicModel.ak135().plot(types=[ParameterType.VSH], dr=.5, label='') mask_dict = dict() mask_dict[ParameterType.VSH] = np.ones(model_params._n_grd_params // 2, dtype='bool') mask_dict[ParameterType.RADIUS] = np.zeros(model_params._n_grd_params // 2, dtype='bool')