def smoother(A, x, b): x[:] = (cg(A, b, x0=x, tol=tol, maxiter=maxiter, M=M, callback=callback, residuals=residuals)[0]).reshape(x.shape)
def isoperimetric(A, ground=None, residuals=None): #from pyamg.graph import pseudo_peripheral_node #ground = pseudo_peripheral_node(A)[0] # select random ground 'dead' node if ground is None: seed() ground = randint(0, A.shape[0] - 1) coarse = numpy.arange(0, A.shape[0]) coarse = numpy.delete(coarse, ground, 0) # remove ground node row and column L = A[coarse, :][:, coarse] r = numpy.ones((L.shape[0], )) if residuals is None: res = [] # construct preconditioner ml = smoothed_aggregation_solver(L, coarse_solver='pinv2') M = ml.aspreconditioner() # solve system using cg (x, flag) = cg(L, r, residuals=res, tol=1e-12, M=M) # use the median of solution, x, as the separator vmed = numpy.median(x) vmin = numpy.min(x) P1 = coarse[numpy.where(x <= vmed)[0]] P2 = coarse[numpy.where(x > vmed)[0]] weights = numpy.zeros((A.shape[0], )) weights[P1] = x[numpy.where(x <= vmed)[0]] weights[P2] = x[numpy.where(x > vmed)[0]] weights[ground] = vmin - 1 P1 = numpy.append(P1, ground) return P1, P2, weights
def isoperimetric(A, ground=None, residuals=None) : #from pyamg.graph import pseudo_peripheral_node #ground = pseudo_peripheral_node(A)[0] # select random ground 'dead' node if ground is None : seed() ground = randint(0,A.shape[0]-1) coarse = numpy.arange(0,A.shape[0]) coarse = numpy.delete(coarse,ground,0) # remove ground node row and column L = A[coarse,:][:,coarse] r = numpy.ones((L.shape[0],)) if residuals is None : res = [] # construct preconditioner ml = smoothed_aggregation_solver(L,coarse_solver='pinv2') M = ml.aspreconditioner() # solve system using cg (x,flag) = cg(L,r,residuals=res,tol=1e-12,M=M) # use the median of solution, x, as the separator vmed = numpy.median(x) vmin = numpy.min(x) P1 = coarse[numpy.where(x<=vmed)[0]] P2 = coarse[numpy.where(x>vmed)[0]] weights = numpy.zeros((A.shape[0],)) weights[P1] = x[numpy.where(x<=vmed)[0]] weights[P2] = x[numpy.where(x>vmed)[0]] weights[ground] = vmin-1 P1 = numpy.append(P1,ground) return P1,P2,weights
def test_aspreconditioner(self): from pyamg import smoothed_aggregation_solver from scipy.sparse.linalg import cg from pyamg.krylov import fgmres A = poisson((50, 50), format='csr') b = rand(A.shape[0]) ml = smoothed_aggregation_solver(A) for cycle in ['V', 'W', 'F']: M = ml.aspreconditioner(cycle=cycle) x, info = cg(A, b, tol=1e-8, maxiter=30, M=M) # cg satisfies convergence in the preconditioner norm assert (precon_norm(b - A * x, ml) < 1e-8 * precon_norm(b, ml)) for cycle in ['AMLI']: M = ml.aspreconditioner(cycle=cycle) x, info = fgmres(A, b, tol=1e-8, maxiter=30, M=M) # fgmres satisfies convergence in the 2-norm assert (norm(b - A * x) < 1e-8 * norm(b))
def test_aspreconditioner(self): from pyamg import smoothed_aggregation_solver from scipy.sparse.linalg import cg from pyamg.krylov import fgmres A = poisson((50, 50), format='csr') b = rand(A.shape[0]) ml = smoothed_aggregation_solver(A) for cycle in ['V', 'W', 'F']: M = ml.aspreconditioner(cycle=cycle) x, info = cg(A, b, tol=1e-8, maxiter=30, M=M) # cg satisfies convergence in the preconditioner norm assert(precon_norm(b - A*x, ml) < 1e-8*precon_norm(b, ml)) for cycle in ['AMLI']: M = ml.aspreconditioner(cycle=cycle) x, info = fgmres(A, b, tol=1e-8, maxiter=30, M=M) # fgmres satisfies convergence in the 2-norm assert(norm(b - A*x) < 1e-8*norm(b))
def fn(A, b): return cg(A, b)[0]
def run_lsm(self, input=None): """run extended least squares reverse time migration """ krylov_maxiter = self.krylov_maxiter weight_matrix = self.weight_matrix regularization_value = self.regularization_value m0 = self.m0 if self.tools.solver.supports['equation_dynamics'] == "time": rhs = self.tools.migrate_shots_extend(self.shots, m0, self.simdata, self.max_sub_offset, self.h, self.imaging_period) else: rhs = self.tools.migrate_shots_extend( self.shots, m0, self.simdata, self.frequencies, self.max_sub_offset, self.h, return_parameters=['imaging_condition']) rhs.data = rhs.data * np.prod(m0.mesh.deltas) / self.tools.solver.dt rhs = rhs.data.reshape(-1) if self.parallel_wrap_shot.use_parallel: rhs_global = self.parallel_wrap_shot.comm.allreduce(rhs, op=MPI.SUM) rhs = rhs_global m1_extend = ExtendedModelingParameter2D(m0.mesh, self.max_sub_offset, self.h) if input is not None: x0 = input.data.reshape(-1) else: x0 = m1_extend.data.reshape(-1) def matvec(x): m1_extend.setter(x) if self.tools.solver.supports['equation_dynamics'] == "time": linfwdret = self.tools.linear_forward_model_extend( self.shots, m0, m1_extend, self.max_sub_offset, self.h, ['simdata']) lindatas = linfwdret['simdata'] for i in range(len(self.shots)): lindatas[i] = lindatas[i] #* self.tools.solver.dt m1_out = self.tools.migrate_shots_extend( self.shots, m0, lindatas, self.max_sub_offset, self.h, self.imaging_period) m1_out.data = m1_out.data * np.prod( m0.mesh.deltas) / self.tools.solver.dt #/ self.tools.solver.dt else: linfwdret = self.tools.linear_forward_model_extend( self.shots, m0, m1_extend, self.frequencies, self.max_sub_offset, self.h, ['simdata']) lindatas = linfwdret['simdata'] m1_out = self.tools.migrate_shots_extend( self.shots, m0, lindatas, self.frequencies, self.max_sub_offset, self.h, return_parameters=['imaging_condition']) m1_out.data = m1_out.data * np.prod(m0.mesh.deltas) if weight_matrix is not None: if regularization_value is None: raise TabError( 'A weight_matrix is passed, but not regularization_value is given. Please give a value to regularization_value' ) else: if weight_matrix == 'linear_h': sh_data = m1_out.sh_data max_sub_offset = m1_out.max_sub_offset weight_h = np.linspace(-max_sub_offset, max_sub_offset, sh_data[1]) for i in range(sh_data[1]): m1_out.data[:, i] += regularization_value * weight_h[i]**2.0 * \ m1_extend.data[:, i] / self.parallel_wrap_shot.size * \ np.prod(m0.mesh.deltas) xout_local = np.reshape(m1_out.data, (np.prod(m1_out.sh_data), 1)) if self.parallel_wrap_shot.use_parallel: xout_global = self.parallel_wrap_shot.comm.allreduce( xout_local, op=MPI.SUM) else: xout_global = xout_local return xout_global A_shape = (len(rhs), len(rhs)) A = LinearOperator(shape=A_shape, matvec=matvec, dtype=rhs.dtype) resid = [] rhs = np.reshape(rhs, (len(rhs), 1)) x0 = np.reshape(x0, (len(x0), 1)) # d, info = cg(A, rhs, maxiter=self.krylov_maxiter, residuals=resid) x_out, info = cg(A, rhs, x0=x0, maxiter=self.krylov_maxiter, residuals=resid) m1_extend.setter(x_out) self.m_out = m1_extend return m1_extend