MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_BelosSmoother_def.hpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// MueLu: A package for multigrid based preconditioning
6// Copyright 2012 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact
39// Jonathan Hu (jhu@sandia.gov)
40// Andrey Prokopenko (aprokop@sandia.gov)
41// Ray Tuminaro (rstumin@sandia.gov)
42//
43// ***********************************************************************
44//
45// @HEADER
46#ifndef MUELU_BELOSSMOOTHER_DEF_HPP
47#define MUELU_BELOSSMOOTHER_DEF_HPP
48
49#include "MueLu_ConfigDefs.hpp"
50
51#if defined(HAVE_MUELU_BELOS)
52
53#include <Teuchos_ParameterList.hpp>
54
55#include <Xpetra_CrsMatrix.hpp>
56#include <Xpetra_Matrix.hpp>
57#include <Xpetra_MultiVectorFactory.hpp>
58#ifdef HAVE_XPETRA_TPETRA
59#include <Xpetra_TpetraMultiVector.hpp>
60#endif
61
63#include "MueLu_Level.hpp"
64#include "MueLu_Utilities.hpp"
65#include "MueLu_Monitor.hpp"
66
67#include <BelosLinearProblem.hpp>
68#include <BelosSolverFactory.hpp>
69
70
71
72namespace MueLu {
73
74 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
75 BelosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BelosSmoother(const std::string type, const Teuchos::ParameterList& paramList)
76 : type_(type)
77 {
78 bool solverSupported = false;
79 Belos::SolverFactory<Scalar,tMV,tOP> tFactory;
80 solverSupported = solverSupported || tFactory.isSupported(type);
81 // if (!solverSupported) {
82 // Teuchos::Array<std::string> supportedSolverNames = factory.supportedSolverNames();
83
84 // std::ostringstream outString;
85 // outString << "[";
86 // for (auto iter = supportedSolverNames.begin(); iter != supportedSolverNames.end(); ++iter) {
87 // outString << "\"" << *iter << "\"";
88 // if (iter + 1 != supportedSolverNames.end()) {
89 // outString << ", ";
90 // }
91 // }
92 // outString << "]";
93
94 // TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "Belos does not provide the solver '" << type_ << "'.\nSupported Solvers: " << outString.str());
95 // }
96 this->declareConstructionOutcome(!solverSupported, "Belos does not provide the smoother '" + type_ + "'.");
97 if (solverSupported)
98 SetParameterList(paramList);
99 }
100
101 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
103 Factory::SetParameterList(paramList);
104 }
105
106 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
108
109 this->Input(currentLevel, "A");
110
111 }
112
113 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
115 FactoryMonitor m(*this, "Setup Smoother", currentLevel);
116
117 A_ = Factory::Get< RCP<Matrix> >(currentLevel, "A");
118 SetupBelos(currentLevel);
120 this->GetOStream(Statistics1) << description() << std::endl;
121 }
122
123
124 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
126
127 bool useTpetra = A_->getRowMap()->lib() == Xpetra::UseTpetra;
128
129 if (useTpetra) {
130 tBelosProblem_ = rcp(new Belos::LinearProblem<Scalar, tMV, tOP>());
131 RCP<tOP> tA = Utilities::Op2NonConstTpetraCrs(A_);
132 tBelosProblem_->setOperator(tA);
133
134 Belos::SolverFactory<SC, tMV, tOP> solverFactory;
135 tSolver_ = solverFactory.create(type_, rcpFromRef(const_cast<ParameterList&>(this->GetParameterList())));
136 tSolver_->setProblem(tBelosProblem_);
137 } else {
138 TEUCHOS_ASSERT(false);
139 }
140 }
141
142 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
143 void BelosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
144 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::BelosSmoother::Apply(): Setup() has not been called");
145
146 if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
147 if (InitialGuessIsZero) {
148 X.putScalar(0.0);
149
150 RCP<Tpetra::MultiVector<SC,LO,GO,NO> > tpX = rcpFromRef(Utilities::MV2NonConstTpetraMV(X));
151 RCP<const Tpetra::MultiVector<SC,LO,GO,NO> > tpB = rcpFromRef(Utilities::MV2TpetraMV(B));
152
153 tBelosProblem_->setInitResVec(tpB);
154 tBelosProblem_->setProblem(tpX, tpB);
155 tSolver_->solve();
156
157 } else {
158 typedef Teuchos::ScalarTraits<Scalar> TST;
159 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
160 RCP<MultiVector> Correction = MultiVectorFactory::Build(A_->getDomainMap(), X.getNumVectors());
161
162 RCP<Tpetra::MultiVector<SC,LO,GO,NO> > tpX = rcpFromRef(Utilities::MV2NonConstTpetraMV(*Correction));
163 RCP<const Tpetra::MultiVector<SC,LO,GO,NO> > tpB = rcpFromRef(Utilities::MV2TpetraMV(*Residual));
164
165 tBelosProblem_->setInitResVec(tpB);
166 tBelosProblem_->setProblem(tpX, tpB);
167 tSolver_->solve();
168
169 X.update(TST::one(), *Correction, TST::one());
170 }
171 } else {
172 TEUCHOS_ASSERT(false);
173 }
174
175 }
176
177 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
178 RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> > BelosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
179 RCP<BelosSmoother> smoother = rcp(new BelosSmoother(*this) );
180 smoother->SetParameterList(this->GetParameterList());
181 return smoother;
182 }
183
184 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
186 std::ostringstream out;
188 if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
189 out << tSolver_->description();
190 }
191 } else {
192 out << "BELOS {type = " << type_ << "}";
193 }
194 return out.str();
195 }
196
197 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
198 void BelosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream &out, const VerbLevel verbLevel) const {
200
201 if (verbLevel & Parameters1) {
202 out0 << "Parameter list: " << std::endl;
203 Teuchos::OSTab tab2(out);
204 out << this->GetParameterList();
205 }
206
207 if (verbLevel & External) {
208 if (tSolver_ != Teuchos::null) {
209 Teuchos::OSTab tab2(out);
210 out << *tSolver_ << std::endl;
211 }
212 }
213
214 if (verbLevel & Debug) {
215 if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
216 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
217 << "-" << std::endl
218 << "RCP<solver_>: " << tSolver_ << std::endl;
219 }
220 }
221 }
222
223 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
225 return Teuchos::OrdinalTraits<size_t>::invalid();
226 }
227
228
229} // namespace MueLu
230
231#endif // HAVE_MUELU_BELOS
232#endif // MUELU_BELOSSMOOTHER_DEF_HPP
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Class that encapsulates Belos smoothers.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the preconditioner.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
friend class BelosSmoother
Constructor.
void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
void DeclareInput(Level &currentLevel) const
Input.
void SetupBelos(Level &currentLevel)
std::string description() const
Return a simple one-line description of this object.
size_t getNodeSmootherComplexity() const
For diagnostic purposes.
void Setup(Level &currentLevel)
Set up the smoother.
RCP< SmootherPrototype > Copy() const
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
virtual void SetParameterList(const Teuchos::ParameterList &paramList)=0
Set parameters from a parameter list and return with default values.
void declareConstructionOutcome(bool fail, std::string msg)
bool IsSetup() const
Get the state of a smoother prototype.
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2TpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > const vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > vec)
Namespace for MueLu classes and methods.
@ Debug
Print additional debugging information.
@ Statistics1
Print more statistics.
@ External
Print external lib objects.
@ Parameters1
Print class parameters (more parameters, more verbose)