def export_kernels(self, path): unix.cd(self.kernel_databases) # work around conflicting name conventions files = [] files += glob('*proc??????_alpha_kernel.bin') files += glob('*proc??????_alpha[hv]_kernel.bin') files += glob('*proc??????_reg1_alpha_kernel.bin') files += glob('*proc??????_reg1_alpha[hv]_kernel.bin') unix.rename('alpha', 'vp', files) files = [] files += glob('*proc??????_beta_kernel.bin') files += glob('*proc??????_beta[hv]_kernel.bin') files += glob('*proc??????_reg1_beta_kernel.bin') files += glob('*proc??????_reg1_beta[hv]_kernel.bin') unix.rename('beta', 'vs', files) # hack to deal with problems on parallel filesystem unix.mkdir(join(path, 'kernels'), noexit=True) unix.mkdir(join(path, 'kernels', basename(self.getpath))) src = join(glob('*_kernel.bin')) dst = join(path, 'kernels', basename(self.getpath)) unix.mv(src, dst)
def clip(self, path='', parameters=[], minval=-np.inf, maxval=np.inf): """ Clips kernels by convolving them with a Gaussian. Wrapper over xclip_sem utility. """ assert exists(path) assert len(parameters) > 0 unix.cd(self.getpath) for name in self.parameters: self.mpirun( PATH.SPECFEM_BIN +'/'+ 'xclip_sem ' + str(minval) + ' ' + str(maxval) + ' ' + name + '_kernel' + ' ' + path + '/ ' + path + '/ ') # move input files src = path dst = path + '_noclip' unix.mkdir(dst) for name in self.parameters: unix.mv(glob(src+'/*'+name+'.bin'), dst) # rename output files unix.rename('_clip', '', glob(src+'/*'))
def generate_data(self, **model_kwargs): """ Generates data (perform meshing and database generation first) """ self.generate_mesh(**model_kwargs) unix.cd(self.cwd) setpar('SIMULATION_TYPE', '1') setpar('SAVE_FORWARD', '.false.') call_solver(system.mpiexec(), 'bin/xmeshfem2D', output='mesher.log') call_solver(system.mpiexec(), 'bin/xspecfem2D', output='solver.log') if PAR.FORMAT in ['SU', 'su']: src = glob('OUTPUT_FILES/*.su') # work around SPECFEM2D's different file names (depending on the # version used : # ?? junliu # _d? unix.rename('single_d.su', 'single.su', src) src = glob('OUTPUT_FILES/*.su') dst = 'traces/obs' unix.mv(src, dst) if PAR.SAVETRACES: self.export_traces(PATH.OUTPUT + '/' + 'traces/obs')
def smooth(self, path='', tag='gradient', span=0.): """ smooths SPECFEM3D kernels """ unix.cd(self.getpath) # list kernels kernels = [] for name in self.model_parameters: if name in self.inversion_parameters: flag = True else: flag = False kernels = kernels + [[name, flag]] # smooth kernels for name, flag in kernels: if flag: print ' smoothing', name self.mpirun(PATH.SOLVER_BINARIES + '/' + 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + ' ' + path + '/' + tag + '/ ' + path + '/' + tag + '/ ') # move kernels src = path + '/' + tag dst = path + '/' + tag + '_nosmooth' unix.mkdir(dst) for name, flag in kernels: if flag: unix.mv(glob(src + '/*' + name + '.bin'), dst) else: unix.cp(glob(src + '/*' + name + '.bin'), dst) unix.rename('_smooth', '', glob(src + '/*')) print '' unix.cd(path)
def smooth(self, input_path='', output_path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ if not exists(input_path): raise Exception if not exists(output_path): unix.mkdir(output_path) # apply smoothing operator unix.cd(self.cwd) for name in parameters or self.parameters: print ' smoothing', name call_solver(system.mpiexec(), PATH.SPECFEM_BIN + '/' + 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + input_path + '/ ' + output_path + '/ ', output='/dev/null') print '' # rename output files files = glob(output_path + '/*') unix.rename('_smooth', '', files)
def smooth(self, path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ assert exists(path) assert len(parameters) > 0 # apply smoothing operator unix.cd(self.getpath) for name in parameters or self.parameters: print ' smoothing', name call_solver( system.mpiexec(), PATH.SPECFEM_BIN +'/'+ 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + path + '/ ' + path + '/ ', output=self.getpath+'/'+'OUTPUT_FILES/output_smooth_sem.txt') print '' # move input files src = path dst = path + '_nosmooth' unix.mkdir(dst) for name in parameters or self.parameters: unix.mv(glob(src+'/*'+name+'_kernel.bin'), dst) # rename output files unix.rename('_smooth', '', glob(src+'/*'))
def clip(self, path='', parameters=[], minval=-np.inf, maxval=np.inf): """ Clips kernels by convolving them with a Gaussian. Wrapper over xclip_sem utility. """ assert exists(path) assert len(parameters) > 0 unix.cd(self.getpath) for name in parameters or self.parameters: call_solver( system.mpiexec, PATH.SPECFEM_BIN +'/'+ 'xclip_sem ' + str(minval) + ' ' + str(maxval) + ' ' + name + '_kernel' + ' ' + path + '/ ' + path + '/ ') # move input files src = path dst = path + '_noclip' unix.mkdir(dst) for name in parameters or self.parameters: unix.mv(glob(src+'/*'+name+'.bin'), dst) # rename output files unix.rename('_clip', '', glob(src+'/*'))
def smooth(self, input_path='', output_path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ if not exists(input_path): raise Exception if not exists(output_path): unix.mkdir(output_path) # apply smoothing operator unix.cd(self.cwd) for name in parameters or self.parameters: print ' smoothing', name call_solver( system.mpiexec(), PATH.SPECFEM_BIN +'/'+ 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + input_path + '/ ' + output_path + '/ ', output='/dev/null') print '' # rename output files files = glob(output_path+'/*') unix.rename('_smooth', '', files)
def smooth(self, path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ assert exists(path) assert len(parameters) > 0 # apply smoothing operator unix.cd(self.getpath) for name in parameters: print ' smoothing', name self.mpirun( PATH.SPECFEM_BIN +'/'+ 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + path + '/ ' + path + '/ ', output=self.getpath+'/'+'OUTPUT_FILES/output_smooth_sem.txt') print '' # move input files src = path dst = path + '_nosmooth' unix.mkdir(dst) for name in self.parameters: unix.mv(glob(src+'/*'+name+'.bin'), dst) # rename output files unix.rename('_smooth', '', glob(src+'/*'))
def smooth(self, input_path='', output_path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ if not exists(input_path): raise Exception if not exists(output_path): unix.mkdir(output_path) # apply smoothing operator unix.cd(self.cwd) files = [] files += glob(PATH.MODEL_INIT + '/proc??????_x.bin') files += glob(PATH.MODEL_INIT + '/proc??????_z.bin') files += glob(PATH.MODEL_INIT + '/proc??????_NSPEC_ibool.bin') files += glob(PATH.MODEL_INIT + '/proc??????_jacobian.bin') for file in files: unix.cp(file, input_path) for name in parameters or self.parameters: print(' smoothing', name) call_solver(system.mpiexec(), PATH.SPECFEM_BIN + '/' + 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + input_path + '/ ' + output_path + '/ F', output=output_path + '/smooth_' + name + '.log') print('') # rename output files files = glob(output_path + '/*') unix.rename('_smooth', '', files)
def initialize_adjoint_traces(self): super(specfem3d_globe, self).initialize_adjoint_traces() # workaround for SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['ASCII', 'ascii']: files = glob(self.cwd +'/'+ 'traces/adj/*sem.ascii') unix.rename('sem.ascii', 'adj', files)
def initialize_adjoint_traces(self): super(specfem3d_globe, self).initialize_adjoint_traces() # workaround for SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['ASCII', 'ascii']: files = glob(self.cwd + '/' + 'traces/adj/*sem.ascii') unix.rename('sem.ascii', 'adj', files)
def export_att_kernel(self, path): unix.cd(self.kernel_databases) # work around conflicting name conventions files = [] files += glob('*proc??????_c_acoustic_kernel.bin') unix.rename('c_acoustic', 'Qkappa', files) src = glob('*Qkappa_kernel.bin') dst = join(path, 'kernels', self.source_name) unix.mkdir(dst) unix.mv(src, dst)
def adjoint_att(self): """ Calls SPECFEM2D adjoint solver """ unix.rm('SEM') unix.ln('traces/adj_att', 'SEM') # hack to deal with different SPECFEM2D name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob('traces/adj_att/*.su') unix.rename('.su', '.su.adj', files) call_solver(system.mpiexec(), 'bin/xspecfem2D')
def adjoint(self): """ Calls SPECFEM2D adjoint solver """ setpar('SIMULATION_TYPE', '3') setpar('SAVE_FORWARD', '.false.') unix.rm('SEM') unix.ln('traces/adj', 'SEM') # hack to deal with different SPECFEM2D name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob('traces/adj/*.su') unix.rename('.su', '.su.adj', files) call_solver(system.mpiexec(), 'bin/xspecfem2D')
def adjoint(self): """ Calls SPECFEM2D adjoint solver """ setpar('SIMULATION_TYPE', '3') setpar('SAVE_FORWARD', '.false.') unix.rm('SEM') unix.ln('traces/adj', 'SEM') # hack to deal with different SPECFEM2D name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob('traces/adj/*.su') unix.rename('.su', '.su.adj', files) call_solver(system.mpiexec(), 'bin/xmeshfem2D') call_solver(system.mpiexec(), 'bin/xspecfem2D')
def rename_kernels(self): """ Works around conflicting kernel filename conventions """ files = [] files += glob('*proc??????_alpha_kernel.bin') files += glob('*proc??????_alpha[hv]_kernel.bin') files += glob('*proc??????_reg1_alpha_kernel.bin') files += glob('*proc??????_reg1_alpha[hv]_kernel.bin') unix.rename('alpha', 'vp', files) files = [] files += glob('*proc??????_beta_kernel.bin') files += glob('*proc??????_beta[hv]_kernel.bin') files += glob('*proc??????_reg1_beta_kernel.bin') files += glob('*proc??????_reg1_beta[hv]_kernel.bin') unix.rename('beta', 'vs', files)
def forward(self, path='traces/syn'): """ Calls SPECFEM2D forward solver """ setpar('SIMULATION_TYPE', '1') setpar('SAVE_FORWARD', '.true.') call_solver(system.mpiexec(), 'bin/xmeshfem2D') call_solver(system.mpiexec(), 'bin/xspecfem2D') if PAR.FORMAT in ['SU', 'su']: filenames = glob('OUTPUT_FILES/*.su') # work around SPECFEM2D's different file names (depending on the # version used : unix.rename('single_p.su', 'single.su', filenames) filenames = glob('OUTPUT_FILES/*.su') unix.mv(filenames, path)
def initialize_adjoint_traces(self): super(specfem3d, self).initialize_adjoint_traces() # workaround for SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob(self.cwd +'/'+ 'traces/adj/*SU') unix.rename('_SU', '_SU.adj', files) # workaround for SPECFEM3D's requirement that all components exist, # even ones not in use unix.cd(self.cwd +'/'+ 'traces/adj') for iproc in range(PAR.NPROC): for channel in ['x', 'y', 'z']: src = '%d_d%s_SU.adj' % (iproc, PAR.CHANNELS[0]) dst = '%d_d%s_SU.adj' % (iproc, channel) if not exists(dst): unix.cp(src, dst)
def initialize_adjoint_traces(self): super(specfem2d, self).initialize_adjoint_traces() # work around SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob('traces/adj/*.su') unix.rename('.su', '.su.adj', files) # work around SPECFEM2D's requirement that all components exist, # even ones not in use if PAR.FORMAT in ['SU', 'su']: unix.cd(self.cwd +'/'+ 'traces/adj') for channel in ['x', 'y', 'z', 'p']: src = 'U%s_file_single.su.adj' % PAR.CHANNELS[0] dst = 'U%s_file_single.su.adj' % channel if not exists(dst): unix.cp(src, dst)
def initialize_adjoint_traces(self): super(specfem3d_nz, self).initialize_adjoint_traces() # workaround for SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob(self.cwd + '/' + 'traces/adj/*SU') unix.rename('_SU', '_SU.adj', files) # workaround for SPECFEM3D's requirement that all components exist, # even ones not in use unix.cd(self.cwd + '/' + 'traces/adj') for iproc in range(PAR.NPROC): for channel in ['x', 'y', 'z']: src = '%d_d%s_SU.adj' % (iproc, PAR.CHANNELS[0]) dst = '%d_d%s_SU.adj' % (iproc, channel) if not exists(dst): unix.cp(src, dst)
def initialize_adjoint_traces(self): super(specfem2d, self).initialize_adjoint_traces() # work around SPECFEM2D's use of different name conventions for # regular traces and 'adjoint' traces if PAR.FORMAT in ['SU', 'su']: files = glob('traces/adj/*.su') unix.rename('.su', '.su.adj', files) # work around SPECFEM2D's requirement that all components exist, # even ones not in use if PAR.FORMAT in ['SU', 'su']: unix.cd(self.getpath + '/' + 'traces/adj') for channel in ['x', 'y', 'z', 'p']: src = 'U%s_file_single.su.adj' % PAR.CHANNELS[0] dst = 'U%s_file_single.su.adj' % channel if not exists(dst): unix.cp(src, dst)
def smooth(self, input_path='', output_path='', parameters=[], span=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. """ if not exists(input_path): raise Exception if not exists(output_path): unix.mkdir(output_path) # apply smoothing operator unix.cd(self.cwd) src = '/scratch/gpfs/etienneb/2D_inversion_80_elem/model_init80/proc000000_x.bin' dst = '/scratch/gpfs/etienneb/2D_inversion_80_elem/scratch/evalgrad/kernels/sum_nosmooth' unix.cp(src, dst) src = '/scratch/gpfs/etienneb/2D_inversion_80_elem/model_init80/proc000000_z.bin' unix.cp(src, dst) src = '/scratch/gpfs/etienneb/2D_inversion_80_elem/model_init80/proc000000_NSPEC_ibool.bin' unix.cp(src, dst) src = '/scratch/gpfs/etienneb/2D_inversion_80_elem/model_init80/proc000000_jacobian.bin' unix.cp(src, dst) print input_path for name in parameters or self.parameters: print ' smoothing', name call_solver( system.mpiexec(), PATH.SPECFEM_BIN +'/'+ 'xsmooth_sem ' + str(span) + ' ' + str(span) + ' ' + name + '_kernel' + ' ' + input_path + '/ ' + output_path + '/ T', output='/scratch/gpfs/etienneb/2D_inversion_80_elem/smooth_output.log') print '' # rename output files files = glob(output_path+'/*') unix.rename('_smooth', '', files)
def smooth(self, input_path='', output_path='', parameters=[], span_h=0., span_v=0.): """ Smooths kernels by convolving them with a Gaussian. Wrapper over xsmooth_sem utility. smooth() in base.py has the incorrect command line call, specfem requires that NPROC be specified """ if not exists(input_path): raise Exception if not exists(output_path): unix.mkdir(output_path) # apply smoothing operator unix.cd(self.cwd) print 'smoothing parameters ', self.parameters for name in parameters or self.parameters: print 'smoothing', name solver_call = " ".join([ PATH.SPECFEM_BIN + '/' + 'xsmooth_sem', # ./bin/xsmooth_sem str(span_h), # SIGMA_H str(span_v), # SIGMA_V name + '_kernel', # KERNEL_NAME input_path + '/', # INPUT_DIR output_path + '/', # OUTPUT_DIR '.false' # USE_GPU ]) call_solver(system.mpiexec(), solver_call) print '' # rename output files files = glob(output_path + '/*') unix.rename('_smooth', '', files)
def rename_kernels(self): """ Works around conflicting kernel filename conventions """ #files = [] #files += glob('*proc??????_alpha_acoustic_kernel.bin') #unix.rename('alpha_acoustic', 'vp', files) files = [] files += glob('*proc??????_alpha_kernel.bin') unix.rename('alpha', 'vp', files) files = [] files += glob('*proc??????_c_acoustic_kernel.bin') unix.rename('c_acoustic', 'vp', files) # unix.rename('c_acoustic', 'Qkappa', files) files = [] files += glob('*proc??????_rhop_acoustic_kernel.bin') unix.rename('rhop_acoustic', 'rho', files) files = [] files += glob('*proc??????_beta_kernel.bin') unix.rename('beta', 'vs', files)
def rename_kernels(self): """ Works around conflicting kernel filename conventions """ files = [] files += glob('*proc??????_c_acoustic_kernel.bin') files += glob('*proc??????_alpha_kernel.bin') files += glob('*proc??????_alpha[hv]_kernel.bin') files += glob('*proc??????_reg1_alpha_kernel.bin') files += glob('*proc??????_reg1_alpha[hv]_kernel.bin') # Replace 'alpha' by 'vp' is all files names : unix.rename('alpha', 'vp', files) # Replace 'c_acoustic' by 'vp' is all files names : unix.rename('c_acoustic', 'vp', files) files = [] files += glob('*proc??????_beta_kernel.bin') files += glob('*proc??????_beta[hv]_kernel.bin') files += glob('*proc??????_reg1_beta_kernel.bin') files += glob('*proc??????_reg1_beta[hv]_kernel.bin') # Replace 'beta' by 'vs' is all files names : unix.rename('beta', 'vs', files)
def rename_data(self): """ Works around conflicting data filename conventions """ files = glob(self.cwd +'/'+ 'traces/adj/*sem.ascii') unix.rename('sem.ascii', 'sem.ascii.adj', files)
def rename_data(self): """ Works around conflicting data filename conventions """ files = glob(self.cwd + '/' + 'traces/adj/*sem.ascii') unix.rename('sem.ascii', 'sem.ascii.adj', files)
def rename_data(self): """ Works around conflicting data filename conventions """ if PAR.FORMAT in ['SU', 'su']: files = glob(self.cwd + '/' + 'traces/adj/*SU') unix.rename('_SU', '_SU.adj', files)
def rename_data(self): """ Works around conflicting data filename conventions """ if PAR.FORMAT in ['SU', 'su']: files = glob(self.cwd +'/'+ 'traces/adj/*SU') unix.rename('_SU', '_SU.adj', files)