def _solve_(self, L, x, b): diag = L.takeDiagonal() maxdiag = max(numerix.absolute(diag)) L = L * (1 / maxdiag) b = b * (1 / maxdiag) LU = superlu.factorize(L.matrix.to_csr()) if DEBUG: import sys print(L.matrix, file=sys.stderr) error0 = numerix.sqrt(numerix.sum((L * x - b)**2)) for iteration in range(self.iterations): errorVector = L * x - b if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) <= self.tolerance: break xError = numerix.zeros(len(b), 'd') LU.solve(errorVector, xError) x[:] = x - xError if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (iteration+1, self.iterations)) PRINT('residual:', numerix.sqrt(numerix.sum(errorVector**2)))
def _solve_(self, L, x, b): """ `_solve_` is only for use by solvers which may use preconditioning. If you are writing a solver which doesn't use preconditioning, this must be overridden. :Parameters: - `L`: a `fipy.matrices.pysparseMatrix._PysparseMeshMatrix`. - `x`: a `numpy.ndarray`. - `b`: a `numpy.ndarray`. """ A = L.matrix if self.preconditioner is None: P = None else: P, A = self.preconditioner._applyToMatrix(A) info, iter, relres = self.solveFnc(A, b, x, self.tolerance, self.iterations, P) self._raiseWarning(info, iter, relres) if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (iter, self.iterations)) if info < 0: PRINT('failure', self._warningList[info].__class__.__name__) PRINT('relres:', relres)
def _solve_(self, L, x, b): diag = L.takeDiagonal() maxdiag = max(numerix.absolute(diag)) L = L * (1 / maxdiag) b = b * (1 / maxdiag) LU = splu(L.matrix.asformat("csc"), diag_pivot_thresh=1., drop_tol=0., relax=1, panel_size=10, permc_spec=3) error0 = numerix.sqrt(numerix.sum((L * x - b)**2)) for iteration in range(min(self.iterations, 10)): errorVector = L * x - b if (numerix.sqrt(numerix.sum(errorVector**2)) / error0) <= self.tolerance: break xError = LU.solve(errorVector) x[:] = x - xError if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (iteration + 1, self.iterations)) PRINT('residual:', numerix.sqrt(numerix.sum(errorVector**2))) return x
def _solve_(self, L, x, b): for iteration in range(self.iterations): # errorVector = L*x - b errorVector = Epetra.Vector(L.RangeMap()) L.Multiply(False, x, errorVector) # If A is an Epetra.Vector with map M # and B is an Epetra.Vector with map M # and C = A - B # then C is an Epetra.Vector with *no map* !!!?!?! errorVector -= b tol = errorVector.Norm1() if iteration == 0: tol0 = tol if (tol / tol0) <= self.tolerance: break xError = Epetra.Vector(L.RowMap()) Problem = Epetra.LinearProblem(L, xError, errorVector) Solver = self.Factory.Create("Klu", Problem) Solver.Solve() x[:] = x - xError if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (iteration + 1, self.iterations)) PRINT('residual:', errorVector.Norm2())
def _solve_(self, L, x, b): ksp = PETSc.KSP() ksp.create(PETSc.COMM_WORLD) ksp.setType("preonly") ksp.getPC().setType(self.preconditioner) # TODO: SuperLU invoked with PCFactorSetMatSolverType(pc, MATSOLVERSUPERLU) # see: http://www.mcs.anl.gov/petsc/petsc-dev/src/ksp/ksp/examples/tutorials/ex52.c.html # PETSc.PC().setFactorSolverType("superlu") L.assemblyBegin() L.assemblyEnd() ksp.setOperators(L) ksp.setFromOptions() for iteration in range(self.iterations): errorVector = L * x - b tol = errorVector.norm() if iteration == 0: tol0 = tol if (tol / tol0) <= self.tolerance: break xError = x.copy() ksp.solve(errorVector, xError) x -= xError if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT # L.view() # b.view() PRINT('solver:', ksp.type) PRINT('precon:', ksp.getPC().type) PRINT('iterations: %d / %d' % (iteration+1, self.iterations)) PRINT('residual:', errorVector.norm(1))
def _solve_(self, L, x, b): Solver = AztecOO.AztecOO(L, x, b) Solver.SetAztecOption(AztecOO.AZ_solver, self.solver) ## Solver.SetAztecOption(AztecOO.AZ_kspace, 30) Solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_none) if self.preconditioner is not None: self.preconditioner._applyToSolver(solver=Solver, matrix=L) else: Solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_none) output = Solver.Iterate(self.iterations, self.tolerance) if self.preconditioner is not None: if hasattr(self.preconditioner, 'Prec'): del self.preconditioner.Prec if 'FIPY_VERBOSE_SOLVER' in os.environ: status = Solver.GetAztecStatus() from fipy.tools.debug import PRINT PRINT('iterations: %d / %d' % (status[AztecOO.AZ_its], self.iterations)) failure = { AztecOO.AZ_normal: 'AztecOO.AZ_normal', AztecOO.AZ_param: 'AztecOO.AZ_param', AztecOO.AZ_breakdown: 'AztecOO.AZ_breakdown', AztecOO.AZ_loss: 'AztecOO.AZ_loss', AztecOO.AZ_ill_cond: 'AztecOO.AZ_ill_cond', AztecOO.AZ_maxits: 'AztecOO.AZ_maxits' } PRINT('failure', failure[status[AztecOO.AZ_why]]) PRINT('AztecOO.AZ_r:', status[AztecOO.AZ_r]) PRINT('AztecOO.AZ_scaled_r:', status[AztecOO.AZ_scaled_r]) PRINT('AztecOO.AZ_rec_r:', status[AztecOO.AZ_rec_r]) PRINT('AztecOO.AZ_solve_time:', status[AztecOO.AZ_solve_time]) PRINT('AztecOO.AZ_Aztec_version:', status[AztecOO.AZ_Aztec_version]) return output
def _solve_(self, L, x, b): ksp = PETSc.KSP() ksp.create(L.comm) ksp.setType(self.solver) if self.preconditioner is not None: ksp.getPC().setType(self.preconditioner) ksp.setTolerances(rtol=self.tolerance, max_it=self.iterations) L.assemble() ksp.setOperators(L) ksp.setFromOptions() ksp.solve(b, x) if 'FIPY_VERBOSE_SOLVER' in os.environ: from fipy.tools.debug import PRINT # L.view() # b.view() PRINT('solver:', ksp.type) PRINT('precon:', ksp.getPC().type) PRINT('convergence: %s' % _reason[ksp.reason]) PRINT('iterations: %d / %d' % (ksp.its, self.iterations)) PRINT('norm:', ksp.norm) PRINT('norm_type:', ksp.norm_type)