//============================================================================== int Ifpack_Chebyshev:: PowerMethod(const Epetra_Operator& Operator, const Epetra_Vector& InvPointDiagonal, const int MaximumIterations, double& lambda_max) { // this is a simple power method lambda_max = 0.0; double RQ_top, RQ_bottom, norm; Epetra_Vector x(Operator.OperatorDomainMap()); Epetra_Vector y(Operator.OperatorRangeMap()); x.Random(); x.Norm2(&norm); if (norm == 0.0) IFPACK_CHK_ERR(-1); x.Scale(1.0 / norm); for (int iter = 0; iter < MaximumIterations; ++iter) { Operator.Apply(x, y); IFPACK_CHK_ERR(y.Multiply(1.0, InvPointDiagonal, y, 0.0)); IFPACK_CHK_ERR(y.Dot(x, &RQ_top)); IFPACK_CHK_ERR(x.Dot(x, &RQ_bottom)); lambda_max = RQ_top / RQ_bottom; IFPACK_CHK_ERR(y.Norm2(&norm)); if (norm == 0.0) IFPACK_CHK_ERR(-1); IFPACK_CHK_ERR(x.Update(1.0 / norm, y, 0.0)); } return(0); }
// ============================================================================ bool ML_Epetra::MatrixFreePreconditioner:: CheckSPD(const Epetra_Operator& A, const bool UseApply, const int NumChecks, const int NumVectors) const { bool res = true; std::vector<double> norm(NumVectors); if (!IsComputed()) return(false); if (MyPID() == 0) std::cout << "Checking SPD property of the operator... " << std::endl; Epetra_MultiVector X(A.OperatorDomainMap(), NumVectors); Epetra_MultiVector AX(A.OperatorRangeMap(), NumVectors); try { for (int i = 0; i < NumChecks; ++i) { int ierr; if (X.Random()) res = false; if (UseApply) ierr = A.Apply(X, AX); else ierr = A.ApplyInverse(X, AX); if (ierr < 0) throw(-1); AX.Dot(X, &norm[0]); for (int v = 0; v < NumVectors; ++v) { std::cout << norm[v] << std::endl; if (norm[v] <= 0.0) throw(-2); } } } catch(...) { res = false; } if (MyPID() == 0) { if (res) std::cout << "Passed: all x * A * x are positive." << std::endl; else std::cout << "Failed: some x * A * x are negative or zero!" << std::endl; } return(res); }
int runOperatorTests(Epetra_Operator & A, bool verbose) { int ierr = 0; double residual; EPETRA_CHK_ERR(EpetraExt::OperatorToMatrixMarketFile("Test_A1.mm", A, "Official EpetraExt test operator", "This is the official EpetraExt test operator generated by the EpetraExt regression tests")); EPETRA_CHK_ERR(EpetraExt::OperatorToMatlabFile("Test_A1.dat", A)); A.OperatorRangeMap().Comm().Barrier(); A.OperatorRangeMap().Comm().Barrier(); Epetra_CrsMatrix * A1; Epetra_CrsMatrix * A2; EPETRA_CHK_ERR(EpetraExt::MatrixMarketFileToCrsMatrix("Test_A1.mm", A.OperatorRangeMap(), A1)); EPETRA_CHK_ERR(EpetraExt::MatlabFileToCrsMatrix("Test_A1.dat", A.OperatorRangeMap().Comm(), A2)); residual = A.NormInf(); double rAInf = residual; if (verbose) std::cout << "Inf Norm of Operator A = " << residual << std::endl; residual = A1->NormInf(); double rA1Inf = residual; if (verbose) std::cout << "Inf Norm of Matrix A1 = " << residual << std::endl; ierr += checkValues(rA1Inf,rAInf,"Inf Norm of A", verbose); Epetra_Vector x(A.OperatorDomainMap()); x.Random(); Epetra_Vector y1(A.OperatorRangeMap()); Epetra_Vector y2(A.OperatorRangeMap()); Epetra_Vector y3(A.OperatorRangeMap()); A.Apply(x,y1); A1->Multiply(false, x, y2); A2->Multiply(false, x, y3); y1.Norm2(&residual); double rAx1 = residual; if (verbose) std::cout << "Norm of A*x = " << residual << std::endl; y2.Norm2(&residual); double rAx2 = residual; if (verbose) std::cout << "Norm of A1*x = " << residual << std::endl; ierr += checkValues(rAx1,rAx2,"Norm of A1*x", verbose); y3.Norm2(&residual); double rAx3 = residual; if (verbose) std::cout << "Norm of A2*x = " << residual << std::endl; ierr += checkValues(rAx1,rAx3,"Norm of A2*x", verbose); delete A1; delete A2; return(ierr); }