10#ifndef MUELU_TEKOSMOOTHER_DECL_HPP_
11#define MUELU_TEKOSMOOTHER_DECL_HPP_
15#include "Teko_Utilities.hpp"
17#include "Teko_InverseLibrary.hpp"
18#include "Teko_InverseFactory.hpp"
22#include <Teuchos_ParameterList.hpp>
24#include <Xpetra_MapExtractor_fwd.hpp>
27#include "MueLu_SmootherPrototype.hpp"
42template <class Scalar = SmootherPrototype<>::scalar_type,
49#undef MUELU_TEKOSMOOTHER_SHORT
59 :
type_(
"Teko smoother") {
60 TEUCHOS_TEST_FOR_EXCEPTION(
true,
Exceptions::RuntimeError,
"MueLu::TekoSmoother: Teko can only be used with SC=double. For more information refer to the doxygen documentation of TekoSmoother.");
64 :
type_(
"Teko smoother") {
65 TEUCHOS_TEST_FOR_EXCEPTION(
true,
Exceptions::RuntimeError,
"MueLu::TekoSmoother: Teko can only be used with SC=double. For more information refer to the doxygen documentation of TekoSmoother.");
75 RCP<ParameterList> validParamList = rcp(
new ParameterList());
76 return validParamList;
97 void Apply(MultiVector &X,
const MultiVector &B,
bool InitialGuessIsZero =
false)
const {}
100 RCP<SmootherPrototype>
Copy()
const {
return Teuchos::null; }
107 std::ostringstream out;
109 out <<
"{type = " <<
type_ <<
"}";
119 out0 <<
"Prec. type: " <<
type_ << std::endl;
121 if (verbLevel &
Debug)
156#undef MUELU_TEKOSMOOTHER_SHORT
166 :
type_(
"Teko smoother")
176 :
type_(
"Teko smoother")
182 this->
SetParameter(
"Inverse Type", paramList.getEntry(
"Inverse Type"));
183 const auto &tekoSettings = paramList.sublist(
"Inverse Factory Library");
184 tekoParams_ = Teuchos::make_rcp<Teuchos::ParameterList>(tekoSettings);
194 RCP<ParameterList> validParamList = rcp(
new ParameterList());
196 validParamList->set<RCP<const FactoryBase>>(
"A", null,
"Generating factory of the matrix A");
197 validParamList->set<std::string>(
"Inverse Type",
"",
"Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
199 return validParamList;
203 this->
Input(currentLevel,
"A");
215 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
219 this->
GetOStream(
Warnings0) <<
"MueLu::TekoSmoother::Setup(): Setup() has already been called";
222 A_ = Factory::Get<RCP<Matrix>>(currentLevel,
"A");
223 bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
225 "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
227 bThyOp_ = bA_->getThyraOperator();
229 "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
231 Teuchos::RCP<const Thyra::LinearOpBase<Scalar>> thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar>>(bThyOp_);
233 "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
237 std::string smootherType = pL.get<std::string>(
"Inverse Type");
239 "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
240 type_ = smootherType;
243 "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
245 Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
246 Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
248 inverseOp_ = Teko::buildInverse(*inverse, thyOp);
250 "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
261 void Apply(MultiVector &X,
const MultiVector &B,
bool InitialGuessIsZero =
false)
const {
263 "MueLu::TekoSmoother::Apply(): Setup() has not been called");
265 auto comm = X.getMap()->getComm();
266 auto rgMapExtractor = bA_->getRangeMapExtractor();
267 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
269 using STS = Teuchos::ScalarTraits<Scalar>;
271 auto createProductMultiVector = [](
const Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> &space,
273 const char *castErrorMsg) {
274 auto mv = Thyra::createMembers(space, numVecs);
275 auto prodMv = Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar>>(mv);
277 return std::make_pair(mv, prodMv);
280 auto solveWithThyra = [&](
const Teuchos::RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> &rhs,
281 bool initializeFromX,
282 const typename STS::magnitudeType alphaX) {
283 auto [thyB, thyProdB] = createProductMultiVector(
284 Teuchos::rcp_dynamic_cast<
const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productRange()),
285 Teuchos::as<int>(rhs->getNumVectors()),
286 "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
288 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(rhs, rgMapExtractor, thyProdB);
290 auto [thyX, thyProdX] = createProductMultiVector(
291 Teuchos::rcp_dynamic_cast<
const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productDomain()),
292 Teuchos::as<int>(X.getNumVectors()),
293 "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
295 if (initializeFromX) {
296 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
306 auto XX = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
307 X.update(STS::one(), *XX, alphaX);
310 if (InitialGuessIsZero) {
311 solveWithThyra(Teuchos::rcpFromRef(B),
true, STS::zero());
316 solveWithThyra(residual,
false, STS::one());
327 std::ostringstream out;
329 out <<
"{type = " <<
type_ <<
"}";
339 out0 <<
"Prec. type: " <<
type_ << std::endl;
341 if (verbLevel &
Debug)
359 RCP<BlockedCrsMatrix>
bA_;
360 RCP<const Thyra::BlockedLinearOpBase<Scalar>>
bThyOp_;
369#define MUELU_TEKOSMOOTHER_SHORT
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultGlobalOrdinal GlobalOrdinal
virtual std::string description() const
Return a simple one-line description of this object.
Exception indicating invalid cast attempted.
Exception throws to report errors in the internal logical of the program.
void Input(Level &level, const std::string &varName) const
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
virtual const Teuchos::ParameterList & GetParameterList() const =0
void SetParameter(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
Base class for smoother prototypes.
bool IsSetup() const
Get the state of a smoother prototype.
GlobalOrdinal global_ordinal_type
LocalOrdinal local_ordinal_type
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
RCP< const Thyra::BlockedLinearOpBase< Scalar > > bThyOp_
RCP< SmootherPrototype > Copy() const
std::string description() const
Return a simple one-line description of this object.
std::string type_
smoother type
RCP< Matrix > A_
block operator
RCP< const ParameterList > GetValidParameterList() const
Input.
void Setup(Level ¤tLevel)
Setup routine.
virtual ~TekoSmoother()
Destructor.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
Teko::LinearOp inverseOp_
RCP< BlockedCrsMatrix > bA_
RCP< ParameterList > tekoParams_
Teko parameters.
TekoSmoother()
Constructor.
TekoSmoother(const Teuchos::ParameterList ¶mList)
Constructor.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
void SetTekoParameters(RCP< ParameterList > tekoParams)
void DeclareInput(Level ¤tLevel) const
Input.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Interface to block smoothers in Teko package.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
std::string description() const
Return a simple one-line description of this object.
virtual ~TekoSmoother()
Destructor.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
RCP< const ParameterList > GetValidParameterList() const
Input.
std::string type_
smoother type
void Setup(Level ¤tLevel)
Setup routine.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
TekoSmoother(const Teuchos::ParameterList ¶mList)
void SetTekoParameters(RCP< ParameterList > tekoParams)
RCP< SmootherPrototype > Copy() const
TekoSmoother()
Constructor.
void DeclareInput(Level ¤tLevel) const
Input.
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
@ Warnings0
Important warning messages (one line)
@ Debug
Print additional debugging information.
@ Parameters0
Print class parameters.