def smooth(self, path='', parameters=None, span=0. ): """ Smooths SPECFEM2D kernels by convolving them with a Gaussian """ from seisflows.tools.array import meshsmooth, stack #assert parameters == self.parameters # implementing nproc > 1 would be straightforward, but a bit tedious #assert self.mesh.nproc == 1 kernels = self.load(path, suffix='_kernel') if not span: return kernels # set up grid x = sem.read(PATH.MODEL_INIT, 'x', 0) z = sem.read(PATH.MODEL_INIT, 'z', 0) mesh = stack(x, z) for key in parameters or self.parameters: kernels[key] = [meshsmooth(kernels[key][0], mesh, span)] unix.rm(path + '_nosmooth') unix.mv(path, path + '_nosmooth') self.save(path, kernels, suffix='_kernel')
def getxz(self): model_path = PATH.OUTPUT +'/'+ 'model_init' try: m = solver.load(model_path) x = m['x'][0] z = m['z'][0] except: from seisflows.plugins.io.sem import read x = read(model_path, 'x', 0) z = read(model_path, 'z', 0) return x,z
def apply_smoothing(self, parameter): from seisflows.tools.array import meshsmooth, stack coords = [] for key in ['x', 'z']: coords += [sem.read(PATH.MODEL_INIT, key, 0)] mesh = stack(*coords) for suffix in ['_nu']: path = PATH.MUMFORD_SHAH_OUTPUT if PAR.SMOOTH_EDGES > 0.: # backup original kernel = sem.read(path, parameter+suffix, 0) sem.write(kernel, path, parameter+suffix+'_nosmooth', 0) # apply smoothing operator kernel = meshsmooth(kernel, mesh, PAR.SMOOTH_EDGES) sem.write(kernel, path, parameter+suffix, 0)
def write_damping_term(self, parameter): path_coords = PATH.OUTPUT+'/'+'model_init' path_input = PATH.GRAD+'/'+'model' path_output = PATH.MUMFORD_SHAH_OUTPUT path_run = PATH.SUBMIT x = sem.read(path_coords, 'x', 0) z = sem.read(path_coords, 'z', 0) mesh = stack(x,z) m, grid = mesh2grid(sem.read(path_input, parameter, 0), mesh) nu, _ = mesh2grid(sem.read(path_output, parameter+'_nu', 0), mesh) grad_m = grad(m) grad_nu = grad(nu) V = -2.*(grad_m[0]*grad_nu[0] + grad_m[1]*grad_nu[1]) +\ -nu**2 * nabla2(m) v = grid2mesh(V, grid, mesh) sem.write(v, path_output, parameter+'_dm', 0)
def smooth(self, path='', parameters=None, span=0.): """ For a long time SPECFEM2D lacked its own smoothing utility; this method was intended only as a crude workaround """ from seisflows.tools import array assert self.mesh_properties.nproc == 1,\ msg.SmoothingError_SPECFEM2D kernels = self.load(path, suffix='_kernel') if not span: return kernels # set up grid x = sem.read(PATH.MODEL_INIT, 'x', 0) z = sem.read(PATH.MODEL_INIT, 'z', 0) mesh = array.stack(x, z) for key in parameters or self.parameters: kernels[key] = [array.meshsmooth(kernels[key][0], mesh, span)] unix.rm(path + '_nosmooth') unix.mv(path, path + '_nosmooth') self.save(path, kernels, suffix='_kernel')
def write_gradient(self, path): super(mumford_shah, self).write_gradient(path) g = solver.load(path +'/'+ 'gradient', suffix='_kernel') m = solver.load(path +'/'+ 'model') mesh = self.getmesh() for parameter in solver.parameters: for iproc in range(PAR.NPROC): g[parameter][iproc] += PAR.GAMMA *\ sem.read(PATH.MUMFORD_SHAH_OUTPUT, parameter+'_dm', iproc) # save gradient self.save(path, solver.merge(g), backup='noregularize') # save edges src = PATH.MUMFORD_SHAH_OUTPUT dst = PATH.OUTPUT+'/'+'mumford_shah'+('_%04d' % optimize.iter) unix.mv(src, dst)
def load(self, path, prefix='', suffix='', verbose=False): """ reads SPECFEM model or kernels Models are stored in Fortran binary format and separated into multiple files according to material parameter and processor rank. """ minmax = Minmax(self.parameters) model = Model(self.parameters) for iproc in range(self.mesh_properties.nproc): for key in self.parameters: model[key] += [sem.read(path, prefix+key+suffix, iproc)] # keep track of min, max #minmax.update(key, model[key][iproc]) #if verbose: # minmax.write(path, logpath=PATH.SUBMIT) return model
def check_mesh_properties(self, path=None, parameters=None): if not hasattr(self, '_mesh_properties'): if not path: path = PATH.MODEL_INIT if not parameters: parameters = self.parameters nproc = 0 ngll = [] while True: dummy = sem.read(path, parameters[0], nproc) ngll += [len(dummy)] nproc += 1 if not exists('%s/proc%06d_%s.bin' % (path, nproc, parameters[0])): break self._mesh_properties = Struct([['nproc', nproc], ['ngll', ngll]]) return self._mesh_properties