| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package |
| 4 |
// |
| 5 |
// Copyright 2009 NTESS and the Ifpack2 contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#include "Solve.hpp" |
| 10 |
|
| 11 |
//#include <Ifpack2_Factory.hpp> |
| 12 |
#include <Ifpack2_AdditiveSchwarz.hpp> |
| 13 |
#include <Ifpack2_RILUK.hpp> |
| 14 |
#include <BelosSolverFactory.hpp> |
| 15 |
#include <BelosTpetraAdapter.hpp> |
| 16 |
|
| 17 |
namespace Ifpack2 { |
| 18 |
namespace Test { |
| 19 |
|
| 20 |
void |
| 21 |
solve (const sparse_mat_type& A, |
| 22 |
const multivector_type& b, |
| 23 |
multivector_type& x, |
| 24 |
const int ilukFillLevel, |
| 25 |
const int overlapLevel, |
| 26 |
const int numBlocks, |
| 27 |
const int maxIters, |
| 28 |
const double tol, |
| 29 |
const bool reorder, |
| 30 |
const std::string& innerPrecondType) |
| 31 |
{ |
| 32 |
using Teuchos::ParameterList; |
| 33 |
using Teuchos::parameterList; |
| 34 |
using Teuchos::RCP; |
| 35 |
using Teuchos::rcp; |
| 36 |
using Teuchos::rcpFromRef; |
| 37 |
using std::cout; |
| 38 |
using std::endl; |
| 39 |
|
| 40 |
typedef Tpetra::RowMatrix<scalar_type, LO, GO, node_type> row_matrix_type; |
| 41 |
typedef Ifpack2::AdditiveSchwarz<row_matrix_type> outer_prec_type; |
| 42 |
typedef Tpetra::Operator<scalar_type, LO, GO, node_type> op_type; |
| 43 |
|
| 44 |
RCP<const Teuchos::Comm<int> > comm = A.getRowMap ()->getComm (); |
| 45 |
const int myRank = comm->getRank (); |
| 46 |
|
| 47 |
RCP<const sparse_mat_type> A_ptr = rcpFromRef (A); |
| 48 |
RCP<const multivector_type> b_ptr = rcpFromRef (b); |
| 49 |
RCP<multivector_type> x_ptr = rcpFromRef (x); |
| 50 |
|
| 51 |
// Set RILUK parameters |
| 52 |
ParameterList innerPlist; |
| 53 |
innerPlist.set ("fact: drop tolerance", 0.0); |
| 54 |
innerPlist.set ("fact: iluk level-of-fill", ilukFillLevel); |
| 55 |
innerPlist.set ("fact: relax value", 0.0); |
| 56 |
|
| 57 |
//Teuchos::RCP<prec_type> innerPrec; |
| 58 |
//Ifpack2::Factory factory; |
| 59 |
//innerPrec = factory.create (innerPrecondType, A_ptr); |
| 60 |
//innerPrec->setParameters (innerPlist); |
| 61 |
|
| 62 |
// Create (outer) Additive Schwarz preconditioner |
| 63 |
RCP<outer_prec_type> additiveSchwarz (new outer_prec_type (A_ptr)); |
| 64 |
|
| 65 |
// Set outer preconditioner parameters |
| 66 |
ParameterList ASlist; |
| 67 |
ASlist.set ("inner preconditioner name", innerPrecondType); |
| 68 |
ASlist.set ("inner preconditioner parameters", innerPlist); |
| 69 |
ASlist.set ("schwarz: combine mode", "ZERO"); |
| 70 |
ASlist.set ("schwarz: overlap level", overlapLevel); |
| 71 |
ASlist.set ("schwarz: use reordering", reorder); |
| 72 |
|
| 73 |
additiveSchwarz->setParameters (ASlist); |
| 74 |
|
| 75 |
// Compute (set up) the (outer) preconditioner |
| 76 |
additiveSchwarz->initialize (); |
| 77 |
additiveSchwarz->compute (); |
| 78 |
|
| 79 |
// Set GMRES (iterative linear solver) parameters |
| 80 |
RCP<ParameterList> solverParams = parameterList (); |
| 81 |
solverParams->set ("Num Blocks", numBlocks); |
| 82 |
solverParams->set ("Block Size", 1); |
| 83 |
solverParams->set ("Maximum Iterations", maxIters); |
| 84 |
solverParams->set ("Maximum Restarts", 10); |
| 85 |
solverParams->set ("Convergence Tolerance", tol); |
| 86 |
solverParams->set ("Implicit Residual Scaling", "None"); |
| 87 |
solverParams->set ("Explicit Residual Scaling", "None"); |
| 88 |
|
| 89 |
// Create the GMRES solver using a "factory" and |
| 90 |
// the list of solver parameters created above. |
| 91 |
typedef Belos::SolverFactory<scalar_type, multivector_type, op_type> belos_factory_type; |
| 92 |
typedef Belos::SolverManager<scalar_type, multivector_type, op_type> solver_type; |
| 93 |
RCP<solver_type> solver = belos_factory_type ().create ("GMRES", solverParams); |
| 94 |
|
| 95 |
// Create a LinearProblem struct with the problem to solve. |
| 96 |
// A, X, B, and M are passed by (smart) pointer, not copied. |
| 97 |
typedef Belos::LinearProblem<scalar_type, multivector_type, op_type> problem_type; |
| 98 |
RCP<problem_type> problem (new problem_type (A_ptr, x_ptr, b_ptr)); |
| 99 |
problem->setRightPrec (additiveSchwarz); |
| 100 |
|
| 101 |
// Tell the solver what problem you want to solve. |
| 102 |
solver->setProblem (problem); |
| 103 |
solver->reset (Belos::Problem); |
| 104 |
Belos::ReturnType result = solver->solve (); |
| 105 |
|
| 106 |
// Ask the solver how many iterations the last solve() took. |
| 107 |
const int numIters = solver->getNumIters (); |
| 108 |
|
| 109 |
if (myRank == 0) { |
| 110 |
if (solver->isLOADetected ()) { |
| 111 |
cout << "Detected a loss of accuracy!" << endl; |
| 112 |
} |
| 113 |
cout << "The Belos solve took " << numIters << " iteration" |
| 114 |
<< (numIters != 1 ? "s" : ""); |
| 115 |
if (result == Belos::Converged) { |
| 116 |
cout << " to converge." << endl; |
| 117 |
} else { |
| 118 |
cout << ", but did not converge." << endl; |
| 119 |
} |
| 120 |
cout << "It achieved a tolerance of: " << solver->achievedTol () << endl; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
} // namespace Test |
| 125 |
} // namespace Ifpack2 |