MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_TekoSmoother_decl.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_TEKOSMOOTHER_DECL_HPP_
11#define MUELU_TEKOSMOOTHER_DECL_HPP_
12
13#ifdef HAVE_MUELU_TEKO
14
15#include "Teko_Utilities.hpp"
16
17#include "Teko_InverseLibrary.hpp"
18#include "Teko_InverseFactory.hpp"
19
20#include "MueLu_ConfigDefs.hpp"
21
22#include <Teuchos_ParameterList.hpp>
23
24#include <Xpetra_MapExtractor_fwd.hpp>
25
27#include "MueLu_SmootherPrototype.hpp"
29#include "MueLu_Monitor.hpp"
30
31namespace MueLu {
32
42template <class Scalar = SmootherPrototype<>::scalar_type,
46class TekoSmoother : public SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
47 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
48
49#undef MUELU_TEKOSMOOTHER_SHORT
51
52 public:
54
55
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.");
61 };
62
64 virtual ~TekoSmoother() {}
66
68
69 RCP<const ParameterList> GetValidParameterList() const {
70 RCP<ParameterList> validParamList = rcp(new ParameterList());
71 return validParamList;
72 }
73
74 void DeclareInput(Level &currentLevel) const {}
75
76 void SetTekoParameters(RCP<ParameterList> tekoParams){};
78
80
81
84 void Setup(Level &currentLevel) {}
85
92 void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {}
94
95 RCP<SmootherPrototype> Copy() const { return Teuchos::null; }
96
98
99
101 std::string description() const {
102 std::ostringstream out;
104 out << "{type = " << type_ << "}";
105 return out.str();
106 }
107
109 // using MueLu::Describable::describe; // overloading, not hiding
110 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
112
113 if (verbLevel & Parameters0)
114 out0 << "Prec. type: " << type_ << std::endl;
115
116 if (verbLevel & Debug)
117 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
118 }
119
121 size_t getNodeSmootherComplexity() const;
122
124
125 private:
127 std::string type_;
128}; // class TekoSmoother
129
144template <class GlobalOrdinal,
145 class Node>
146class TekoSmoother<double, int, GlobalOrdinal, Node> : public SmootherPrototype<double, int, GlobalOrdinal, Node> {
147 typedef int LocalOrdinal;
148 typedef double Scalar;
149 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
150
151#undef MUELU_TEKOSMOOTHER_SHORT
153
154 public:
156
157
161 : type_("Teko smoother")
162 , A_(Teuchos::null)
163 , bA_(Teuchos::null)
164 , bThyOp_(Teuchos::null)
165 , tekoParams_(Teuchos::null)
166 , inverseOp_(Teuchos::null){};
167
169 virtual ~TekoSmoother() {}
171
173
174 RCP<const ParameterList> GetValidParameterList() const {
175 RCP<ParameterList> validParamList = rcp(new ParameterList());
176
177 validParamList->set<RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
178 validParamList->set<std::string>("Inverse Type", "", "Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
179
180 return validParamList;
181 }
182
183 void DeclareInput(Level &currentLevel) const {
184 this->Input(currentLevel, "A");
185 }
186
187 void SetTekoParameters(RCP<ParameterList> tekoParams) { tekoParams_ = tekoParams; };
189
191
192
195 void Setup(Level &currentLevel) {
196 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
197
198 FactoryMonitor m(*this, "Setup TekoSmoother", currentLevel);
199 if (this->IsSetup() == true)
200 this->GetOStream(Warnings0) << "MueLu::TekoSmoother::Setup(): Setup() has already been called";
201
202 // extract blocked operator A from current level
203 A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
204 bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
205 TEUCHOS_TEST_FOR_EXCEPTION(bA_.is_null(), Exceptions::BadCast,
206 "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
207
208 bThyOp_ = bA_->getThyraOperator();
209 TEUCHOS_TEST_FOR_EXCEPTION(bThyOp_.is_null(), Exceptions::BadCast,
210 "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
211
212 Teuchos::RCP<const Thyra::LinearOpBase<Scalar> > thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar> >(bThyOp_);
213 TEUCHOS_TEST_FOR_EXCEPTION(thyOp.is_null(), Exceptions::BadCast,
214 "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
215
216 // parameter list contains TekoSmoother parameters but does not handle the Teko parameters itself!
217 const ParameterList &pL = Factory::GetParameterList();
218 std::string smootherType = pL.get<std::string>("Inverse Type");
219 TEUCHOS_TEST_FOR_EXCEPTION(smootherType.empty(), Exceptions::RuntimeError,
220 "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
221 type_ = smootherType;
222
223 TEUCHOS_TEST_FOR_EXCEPTION(tekoParams_.is_null(), Exceptions::BadCast,
224 "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
225
226 Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
227 Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
228
229 inverseOp_ = Teko::buildInverse(*inverse, thyOp);
230 TEUCHOS_TEST_FOR_EXCEPTION(inverseOp_.is_null(), Exceptions::BadCast,
231 "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
232
233 this->IsSetup(true);
234 }
235
242 void Apply(MultiVector &X, const MultiVector &B, bool /* InitialGuessIsZero */ = false) const {
243 TEUCHOS_TEST_FOR_EXCEPTION(this->IsSetup() == false, Exceptions::RuntimeError,
244 "MueLu::TekoSmoother::Apply(): Setup() has not been called");
245
246 Teuchos::RCP<const Teuchos::Comm<int> > comm = X.getMap()->getComm();
247
248 Teuchos::RCP<const MapExtractor> rgMapExtractor = bA_->getRangeMapExtractor();
249 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
250
251 // copy initial solution vector X to Ptr<Thyra::MultiVectorBase> YY
252
253 // create a Thyra RHS vector
254 Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyB = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productRange()), Teuchos::as<int>(B.getNumVectors()));
255 Teuchos::RCP<Thyra::ProductMultiVectorBase<Scalar> > thyProdB =
256 Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyB);
257 TEUCHOS_TEST_FOR_EXCEPTION(thyProdB.is_null(), Exceptions::BadCast,
258 "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
259
260 // copy RHS vector B to Thyra::MultiVectorBase thyProdB
261 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(B), rgMapExtractor, thyProdB);
262
263 // create a Thyra SOL vector
264 Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyX = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productDomain()), Teuchos::as<int>(X.getNumVectors()));
265 Teuchos::RCP<Thyra::ProductMultiVectorBase<Scalar> > thyProdX =
266 Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyX);
267 TEUCHOS_TEST_FOR_EXCEPTION(thyProdX.is_null(), Exceptions::BadCast,
268 "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
269
270 // copy RHS vector X to Thyra::MultiVectorBase thyProdX
271 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
272
273 inverseOp_->apply(
274 Thyra::NOTRANS,
275 *thyB, // const MultiVectorBase<Scalar> &X,
276 thyX.ptr(), // const Ptr<MultiVectorBase<Scalar> > &Y,
277 1.0,
278 0.0);
279
280 // copy back content of Ptr<Thyra::MultiVectorBase> thyX into X
281 Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > XX =
282 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
283
284 X.update(Teuchos::ScalarTraits<Scalar>::one(), *XX, Teuchos::ScalarTraits<Scalar>::zero());
285 }
287
288 RCP<SmootherPrototype> Copy() const { return Teuchos::rcp(new MueLu::TekoSmoother<double, int, GlobalOrdinal, Node>(*this)); }
289
291
292
294 std::string description() const {
295 std::ostringstream out;
297 out << "{type = " << type_ << "}";
298 return out.str();
299 }
300
302 // using MueLu::Describable::describe; // overloading, not hiding
303 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
305
306 if (verbLevel & Parameters0)
307 out0 << "Prec. type: " << type_ << std::endl;
308
309 if (verbLevel & Debug)
310 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
311 }
312
315 size_t cplx = 0;
316 return cplx;
317 }
318
320
321 private:
323 std::string type_;
324
326 RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
327 RCP<BlockedCrsMatrix> bA_;
328 RCP<const Thyra::BlockedLinearOpBase<Scalar> > bThyOp_;
329
331 RCP<ParameterList> tekoParams_; // < ! parameter list containing Teko parameters. These parameters are not administrated by the factory and not validated.
332
333 Teko::LinearOp inverseOp_; // < ! Teko inverse operator
334}; // class TekoSmoother (specialization on SC=double)
335} // namespace MueLu
336
337#define MUELU_TEKOSMOOTHER_SHORT
338
339#endif // HAVE_MUELU_TEKO
340
341#endif /* MUELU_TEKOSMOOTHER_DECL_HPP_ */
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultGlobalOrdinal GlobalOrdinal
MueLu::DefaultNode Node
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
Base class for smoother prototypes.
bool IsSetup() const
Get the state of a smoother prototype.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
RCP< const Thyra::BlockedLinearOpBase< Scalar > > bThyOp_
std::string description() const
Return a simple one-line description of this object.
RCP< const ParameterList > GetValidParameterList() const
Input.
void Apply(MultiVector &X, const MultiVector &B, bool=false) const
Apply the Teko smoother.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
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 &currentLevel)
Setup routine.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
void SetTekoParameters(RCP< ParameterList > tekoParams)
RCP< SmootherPrototype > Copy() const
void DeclareInput(Level &currentLevel) const
Input.
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.