| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/** |
| 11 |
\class BelosKrylov |
| 12 |
\brief Provides interface for using ROL::Vector with Belos solvers |
| 13 |
|
| 14 |
\author Created by Greg von Winckel |
| 15 |
*/ |
| 16 |
|
| 17 |
|
| 18 |
#ifndef ROL_BELOS_KRYLOV_HPP |
| 19 |
#define ROL_BELOS_KRYLOV_HPP |
| 20 |
|
| 21 |
// The GenericSolverFactory will register managers for any types. |
| 22 |
// To be used when the types are not part of the standard set. |
| 23 |
// You must explicitly use GenericSolverFactory instead of SolverFactory. |
| 24 |
#include "BelosSolverFactory_Generic.hpp" |
| 25 |
|
| 26 |
#include "ROL_Krylov.hpp" |
| 27 |
#include "ROL_BelosMultiVector.hpp" |
| 28 |
#include "ROL_BelosOperator.hpp" |
| 29 |
#include "ROL_MultiVectorDefault.hpp" |
| 30 |
|
| 31 |
namespace ROL { |
| 32 |
|
| 33 |
template<class Real> |
| 34 |
class BelosKrylov : public Krylov<Real> { |
| 35 |
typedef Real ST; |
| 36 |
typedef LinearOperator<ST> OP; |
| 37 |
typedef Vector<Real> V; |
| 38 |
typedef MultiVector<ST> MV; |
| 39 |
typedef MultiVectorDefault<ST> MVD; |
| 40 |
|
| 41 |
// For testing |
| 42 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 43 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 44 |
|
| 45 |
private: |
| 46 |
|
| 47 |
Belos::GenericSolverFactory<ST,MV,OP> factory_; |
| 48 |
Teuchos::RCP<Belos::SolverManager<ST,MV,OP> > solver_; |
| 49 |
Teuchos::RCP<Belos::LinearProblem<ST,MV,OP> > problem_; |
| 50 |
|
| 51 |
public: |
| 52 |
|
| 53 |
/// \brief Create a Belos solver |
| 54 |
BelosKrylov(Teuchos::ParameterList &parlist) : |
| 55 |
problem_(ROL::makePtr<Belos::LinearProblem<ST,MV,OP>>()) { |
| 56 |
|
| 57 |
Teuchos::RCP<Teuchos::ParameterList> solverParams = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 58 |
|
| 59 |
// Options likely to be of interest include CG, MINRES, GMRES, and RCG |
| 60 |
int blockSize = 1; // Only support single solution & single RHS for now |
| 61 |
std::string solverName = parlist.get("Krylov Method","MINRES"); |
| 62 |
int maxit = parlist.get("Maximum Number of Krylov Iterations",50); |
| 63 |
Real abstol = parlist.get("Absolute Krylov Tolerance",1.e-4); |
| 64 |
int numVectors = parlist.get("Number of Stored Vectors",3); |
| 65 |
|
| 66 |
solverParams->setName("Belos input parameters"); |
| 67 |
solverParams->set("Block Size",blockSize); |
| 68 |
solverParams->set("Maximum Iterations",maxit); |
| 69 |
solverParams->set("Convergence Tolerance",abstol); |
| 70 |
solverParams->set("Num Blocks",numVectors); |
| 71 |
|
| 72 |
solver_ = factory_.create(solverName,solverParams); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/// \brief Compute solution vector |
| 77 |
Real run( V &x, OP& A, const V &b, OP &M, int &iter, int &flag ) { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
; |
| 82 |
|
| 83 |
|
| 84 |
// Get pointers to ROL::Vectors |
| 85 |
Teuchos::RCP<V> xp = Teuchos::rcpFromRef(x); |
| 86 |
|
| 87 |
// Wasteful, but have not yet implemented const case for MV |
| 88 |
Teuchos::RCP<V> bp = b.clone(); |
| 89 |
bp->set(b); |
| 90 |
|
| 91 |
// Make ROL::MultiVectors from the pointers to ROL::Vectors |
| 92 |
Teuchos::RCP<MV> xmvp = Teuchos::rcp( new MultiVectorDefault<Real>(xp) ); |
| 93 |
Teuchos::RCP<MV> bmvp = Teuchos::rcp( new MultiVectorDefault<Real>(bp) ); |
| 94 |
|
| 95 |
Teuchos::RCP<OP> Ap = Teuchos::rcpFromRef(A); |
| 96 |
Teuchos::RCP<OP> Mp = Teuchos::rcpFromRef(M); |
| 97 |
|
| 98 |
// Wrap x and b in ROL::MultiVector objects |
| 99 |
MVD xmv(xp); |
| 100 |
MVD bmv(bp); |
| 101 |
|
| 102 |
problem_->setOperator(Ap); |
| 103 |
problem_->setLeftPrec(Mp); |
| 104 |
problem_->setProblem(xmvp,bmvp); |
| 105 |
|
| 106 |
solver_->setProblem(problem_); |
| 107 |
|
| 108 |
flag = static_cast<int>(solver_->solve()); |
| 109 |
|
| 110 |
iter = solver_->getNumIters(); |
| 111 |
|
| 112 |
return solver_->achievedTol(); |
| 113 |
} |
| 114 |
}; |
| 115 |
} |
| 116 |
|
| 117 |
#endif |