Skip to content

Commit

Permalink
Merge Pull Request trilinos#13270 from cgcgcg/Trilinos/mueluCondEst
Browse files Browse the repository at this point in the history
Automatically Merged using Trilinos Pull Request AutoTester
PR Title: b'MueLu: Add option to compute eigenvalue and condition number estimates to Driver'
PR Author: cgcgcg
  • Loading branch information
trilinos-autotester authored Jul 23, 2024
2 parents f426a0c + 68840fd commit edffbe5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/muelu/example/basic/Simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int ar
clp.setOption("solver", &solveType, "solve type: (none | belos)");
std::string belosType = "cg";
clp.setOption("belosType", &belosType, "belos solver type: (Pseudoblock CG | Block CG | Pseudoblock GMRES | Block GMRES | ...) see BelosSolverFactory.hpp for exhaustive list of solvers");
bool computeCondEst = false;
clp.setOption("condEst", "noCondEst", &computeCondEst, "compute condition number estimate (currently only available for Pseudoblock CG)");
double tol = 1e-12;
clp.setOption("tol", &tol, "solver convergence tolerance");
bool binaryFormat = false;
Expand Down Expand Up @@ -199,7 +201,7 @@ int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int ar
// =========================================================================
{
comm->barrier();
SystemSolve(A, X, B, H, Prec, out, solveType, belosType, false, false, useML, cacheSize, 0, scaleResidualHist, solvePreconditioned, maxIts, tol);
SystemSolve(A, X, B, H, Prec, out, solveType, belosType, false, false, useML, cacheSize, 0, scaleResidualHist, solvePreconditioned, maxIts, tol, computeCondEst);
comm->barrier();
}

Expand Down
4 changes: 3 additions & 1 deletion packages/muelu/test/scaling/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int ar
clp.setOption("solver", &dsolveType, "solve type: (none | belos | standalone | matvec)");
std::string belosType = "cg";
clp.setOption("belosType", &belosType, "belos solver type: (Pseudoblock CG | Block CG | Pseudoblock GMRES | Block GMRES | ...) see BelosSolverFactory.hpp for exhaustive list of solvers");
bool computeCondEst = false;
clp.setOption("condEst", "noCondEst", &computeCondEst, "compute condition number estimate (currently only available for Pseudoblock CG)");
double dtol = 1e-12, tol;
clp.setOption("tol", &dtol, "solver convergence tolerance");
bool binaryFormat = false;
Expand Down Expand Up @@ -521,7 +523,7 @@ int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int ar
}

// Solve the system numResolves+1 times
SystemSolve(A, X, B, H, Prec, out2, solveType, belosType, profileSolve, useAMGX, useML, cacheSize, numResolves, scaleResidualHist, solvePreconditioned, maxIts, tol);
SystemSolve(A, X, B, H, Prec, out2, solveType, belosType, profileSolve, useAMGX, useML, cacheSize, numResolves, scaleResidualHist, solvePreconditioned, maxIts, tol, computeCondEst);

comm->barrier();
} catch (const std::exception& e) {
Expand Down
29 changes: 28 additions & 1 deletion packages/muelu/test/scaling/DriverCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal
bool scaleResidualHist,
bool solvePreconditioned,
int maxIts,
double tol) {
double tol,
bool computeCondEst) {
#include <MueLu_UseShortNames.hpp>
using Teuchos::RCP;
using Teuchos::rcp;
Expand Down Expand Up @@ -337,6 +338,9 @@ void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal
belosPrec = rcp(new Belos::MueLuOp<SC, LO, GO, NO>(H)); // Turns a MueLu::Hierarchy object into a Belos operator
}

std::string belosTypeUpper(belosType);
std::transform(belosTypeUpper.begin(), belosTypeUpper.end(), belosTypeUpper.begin(), ::toupper);

// Belos parameter list
RCP<Teuchos::ParameterList> belosList = Teuchos::parameterList();
belosList->set("Maximum Iterations", maxIts); // Maximum number of iterations allowed
Expand All @@ -346,8 +350,12 @@ void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal
belosList->set("Output Style", Belos::Brief);
if (!scaleResidualHist)
belosList->set("Implicit Residual Scaling", "None");
if (computeCondEst && (belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG"))
belosList->set("Estimate Condition Number", true);

int numIts;
Scalar conditionNumberEstimate = zero;
Teuchos::ArrayRCP<typename STS::magnitudeType> eigenvalueEstimates;
Belos::ReturnType ret = Belos::Unconverged;

constexpr bool verbose = false;
Expand Down Expand Up @@ -393,6 +401,13 @@ void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal
ret = solver->solve();
numIts = solver->getNumIters();

if ((belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG") &&
belosList->isParameter("Estimate Condition Number") &&
belosList->get<bool>("Estimate Condition Number")) {
conditionNumberEstimate = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, tMV, tOP>>(solver)->getConditionEstimate();
eigenvalueEstimates = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, tMV, tOP>>(solver)->getEigenEstimates();
}

} catch (std::invalid_argument&) {
// Construct a Belos LinearProblem object
RCP<Belos::LinearProblem<SC, MV, OP>> belosProblem = rcp(new Belos::LinearProblem<SC, MV, OP>(belosOp, X, B));
Expand All @@ -411,11 +426,23 @@ void SystemSolve(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal
// Perform solve
ret = solver->solve();
numIts = solver->getNumIters();

if ((belosTypeUpper == "CG" || belosTypeUpper == "PSEUDOBLOCK CG") &&
belosList->isParameter("Estimate Condition Number") &&
belosList->get<bool>("Estimate Condition Number")) {
conditionNumberEstimate = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, MV, OP>>(solver)->getConditionEstimate();
eigenvalueEstimates = Teuchos::rcp_dynamic_cast<Belos::PseudoBlockCGSolMgr<SC, MV, OP>>(solver)->getEigenEstimates();
}
}

// Get the number of iterations for this solve.
out << "Number of iterations performed for this solve: " << numIts << std::endl;

if (conditionNumberEstimate != zero) {
out << "Condition number estimate: " << conditionNumberEstimate << std::endl;
out << "Eigenvalue estimates: " << eigenvalueEstimates().toString() << std::endl;
}

// Check convergence
if (ret != Belos::Converged)
out << std::endl
Expand Down

0 comments on commit edffbe5

Please sign in to comment.