| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/*! \file BelosEpetraOperator.cpp |
| 11 |
\brief This file provides the implementation for an Epetra_Operator |
| 12 |
interface so Belos can be integrated into other codes as an abstract operator. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "BelosEpetraOperator.h" |
| 16 |
#include "BelosSolverFactory.hpp" |
| 17 |
|
| 18 |
namespace Belos { |
| 19 |
|
| 20 |
//-------------------------------------------------------------- |
| 21 |
// |
| 22 |
// implementation of the EpetraOperator class. |
| 23 |
// |
| 24 |
// Constructor. |
| 25 |
// |
| 26 |
EpetraOperator::EpetraOperator( const Teuchos::RCP<LinearProblem<double,Epetra_MultiVector,Epetra_Operator> >& lp, |
| 27 |
const Teuchos::RCP<Teuchos::ParameterList>& plist, |
| 28 |
bool initSolnVec ) |
| 29 |
: lp_(lp), |
| 30 |
plist_(plist), |
| 31 |
initSolnVec_(initSolnVec) |
| 32 |
{ |
| 33 |
// Get the solver's name from the parameter list, use block Gmres by default. |
| 34 |
std::string solver = plist_->get("Solver", "BlockGmres"); |
| 35 |
|
| 36 |
// Create a label for this Epetra_Operator. |
| 37 |
std::string solver_name = "Belos " + solver + " Solver"; |
| 38 |
|
| 39 |
// Copy std::string to character array. |
| 40 |
// Not using conversion routine copy() because it's not supported by RW on Janus. (HKT 11/13/2003) |
| 41 |
Solver.resize(solver_name.length()+1); |
| 42 |
for (int i=0; i<(int)solver_name.length(); i++) { |
| 43 |
Solver[i] = solver_name[i]; |
| 44 |
} |
| 45 |
Solver[solver_name.length()] = 0; |
| 46 |
|
| 47 |
// |
| 48 |
// Create solver and solve problem. This is inefficient, an instance of the solver should |
| 49 |
// exist already and just be reset with a new RHS. |
| 50 |
// |
| 51 |
if (solver == "BlockGmres") { |
| 52 |
solver = "Block Gmres"; |
| 53 |
} |
| 54 |
else if (solver == "PseudoBlockGmres") { |
| 55 |
solver = "Pseudo Block Gmres"; |
| 56 |
} |
| 57 |
else if (solver == "BlockCG") { |
| 58 |
solver = "Block CG"; |
| 59 |
} |
| 60 |
else if (solver == "PseudoBlockCG") { |
| 61 |
solver = "Pseudo Block CG"; |
| 62 |
} |
| 63 |
|
| 64 |
plist_->remove( "Solver" ); |
| 65 |
|
| 66 |
Belos::SolverFactory<double,Epetra_MultiVector,Epetra_Operator> factory; |
| 67 |
solver_ = factory.create( solver, plist ); |
| 68 |
solver_->setProblem( lp_ ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
const Epetra_Comm& EpetraOperator::Comm() const |
| 73 |
{ |
| 74 |
return (lp_->getOperator()->Comm()); |
| 75 |
} |
| 76 |
|
| 77 |
const Epetra_Map& EpetraOperator::OperatorDomainMap() const |
| 78 |
{ |
| 79 |
return (lp_->getOperator()->OperatorDomainMap()); |
| 80 |
} |
| 81 |
|
| 82 |
const Epetra_Map& EpetraOperator::OperatorRangeMap() const |
| 83 |
{ |
| 84 |
return (lp_->getOperator()->OperatorRangeMap()); |
| 85 |
} |
| 86 |
|
| 87 |
int EpetraOperator::Apply( const Epetra_MultiVector &X, Epetra_MultiVector &Y ) const |
| 88 |
{ |
| 89 |
Teuchos::RCP<const Epetra_MultiVector> vec_X = Teuchos::rcp( &X, false ); |
| 90 |
Teuchos::RCP<Epetra_MultiVector> vec_Y = Teuchos::rcp( &Y, false ); |
| 91 |
if (initSolnVec_) |
| 92 |
{ |
| 93 |
vec_Y->PutScalar( 0.0 ); |
| 94 |
lp_->setInitResVec( vec_X ); |
| 95 |
} |
| 96 |
lp_->setProblem( vec_Y, vec_X ); |
| 97 |
Belos::ReturnType ret = solver_->solve(); |
| 98 |
|
| 99 |
if (ret != Converged) |
| 100 |
return(-1); |
| 101 |
|
| 102 |
return(0); |
| 103 |
} |
| 104 |
|
| 105 |
int EpetraOperator::ApplyInverse( const Epetra_MultiVector &X, Epetra_MultiVector &Y ) const |
| 106 |
{ |
| 107 |
Teuchos::RCP<const Epetra_MultiVector> vec_X = Teuchos::rcp( &X, false ); |
| 108 |
Teuchos::RCP<Epetra_MultiVector> vec_Y = Teuchos::rcp( &Y, false ); |
| 109 |
if (initSolnVec_) |
| 110 |
{ |
| 111 |
vec_Y->PutScalar( 0.0 ); |
| 112 |
lp_->setInitResVec( vec_X ); |
| 113 |
} |
| 114 |
lp_->setProblem( vec_Y, vec_X ); |
| 115 |
Belos::ReturnType ret = solver_->solve(); |
| 116 |
|
| 117 |
if (ret != Converged) |
| 118 |
return(-1); |
| 119 |
|
| 120 |
return(0); |
| 121 |
} |
| 122 |
|
| 123 |
} // end namespace Belos |