MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_UzawaSmoother_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 
47 #ifndef MUELU_UZAWASMOOTHER_DEF_HPP_
48 #define MUELU_UZAWASMOOTHER_DEF_HPP_
49 
50 #include <Teuchos_ArrayViewDecl.hpp>
51 #include <Teuchos_ScalarTraits.hpp>
52 
53 #include "MueLu_ConfigDefs.hpp"
54 
55 #include <Xpetra_Matrix.hpp>
57 #include <Xpetra_MultiVectorFactory.hpp>
58 #include <Xpetra_VectorFactory.hpp>
60 
62 #include "MueLu_Level.hpp"
63 #include "MueLu_Monitor.hpp"
64 #include "MueLu_HierarchyUtils.hpp"
65 #include "MueLu_SmootherBase.hpp"
66 
67 #include "MueLu_FactoryManager.hpp"
68 
69 namespace MueLu {
70 
71 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
73  RCP<ParameterList> validParamList = rcp(new ParameterList());
74 
75  validParamList->set<RCP<const FactoryBase>>("A", Teuchos::null, "Generating factory of the matrix A");
76  validParamList->set<Scalar>("Damping factor", 1.0, "Damping/Scaling factor in SIMPLE");
77  validParamList->set<LocalOrdinal>("Sweeps", 1, "Number of SIMPLE sweeps (default = 1)");
78 
79  return validParamList;
80 }
81 
82 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84  TEUCHOS_TEST_FOR_EXCEPTION(pos < 0, Exceptions::RuntimeError, "MueLu::UzawaSmoother::AddFactoryManager: parameter \'pos\' must not be negative! error.");
85 
86  size_t myPos = Teuchos::as<size_t>(pos);
87 
88  if (myPos < FactManager_.size()) {
89  // replace existing entris in FactManager_ vector
90  FactManager_.at(myPos) = FactManager;
91  } else if (myPos == FactManager_.size()) {
92  // add new Factory manager in the end of the vector
93  FactManager_.push_back(FactManager);
94  } else { // if(myPos > FactManager_.size())
95  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
96  *out << "Warning: cannot add new FactoryManager at proper position " << pos << ". The FactoryManager is just appended to the end. Check this!" << std::endl;
97 
98  // add new Factory manager in the end of the vector
99  FactManager_.push_back(FactManager);
100  }
101 }
102 
103 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
105  AddFactoryManager(FactManager, 0); // overwrite factory manager for predicting the primary variable
106 }
107 
108 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
110  AddFactoryManager(FactManager, 1); // overwrite factory manager for SchurComplement
111 }
112 
113 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
115  currentLevel.DeclareInput("A", this->GetFactory("A").get());
116 
117  TEUCHOS_TEST_FOR_EXCEPTION(FactManager_.size() != 2, Exceptions::RuntimeError, "MueLu::UzawaSmoother::DeclareInput: You have to declare two FactoryManagers with a \"Smoother\" object: One for predicting the primary variable and one for the SchurComplement system. The smoother for the SchurComplement system needs a SchurComplementFactory as input for variable \"A\". make sure that you use the same proper damping factors for omega both in the SchurComplementFactory and in the SIMPLE smoother!");
118 
119  // loop over all factory managers for the subblocks of blocked operator A
120  std::vector<Teuchos::RCP<const FactoryManagerBase>>::const_iterator it;
121  for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
122  SetFactoryManager currentSFM(rcpFromRef(currentLevel), *it);
123 
124  // request "Smoother" for current subblock row.
125  currentLevel.DeclareInput("PreSmoother", (*it)->GetFactory("Smoother").get());
126  }
127 }
128 
129 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
131  FactoryMonitor m(*this, "Setup blocked Uzawa Smoother", currentLevel);
132 
133  if (SmootherPrototype::IsSetup() == true)
134  this->GetOStream(Warnings0) << "MueLu::UzawaSmoother::Setup(): Setup() has already been called";
135 
136  // extract blocked operator A from current level
137  A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A");
138 
139  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
140  TEUCHOS_TEST_FOR_EXCEPTION(bA == Teuchos::null, Exceptions::BadCast, "MueLu::UzawaSmoother::Setup: input matrix A is not of type BlockedCrsMatrix! error.");
141 
142  // store map extractors
143  rangeMapExtractor_ = bA->getRangeMapExtractor();
144  domainMapExtractor_ = bA->getDomainMapExtractor();
145 
146  // Store the blocks in local member variables
147  F_ = bA->getMatrix(0, 0);
148  G_ = bA->getMatrix(0, 1);
149  D_ = bA->getMatrix(1, 0);
150  Z_ = bA->getMatrix(1, 1);
151 
152  // Set the Smoother
153  // carefully switch to the SubFactoryManagers (defined by the users)
154  {
155  RCP<const FactoryManagerBase> velpredictFactManager = FactManager_.at(0);
156  SetFactoryManager currentSFM(rcpFromRef(currentLevel), velpredictFactManager);
157  velPredictSmoo_ = currentLevel.Get<RCP<SmootherBase>>("PreSmoother", velpredictFactManager->GetFactory("Smoother").get());
158  }
159  {
160  RCP<const FactoryManagerBase> schurFactManager = FactManager_.at(1);
161  SetFactoryManager currentSFM(rcpFromRef(currentLevel), schurFactManager);
162  schurCompSmoo_ = currentLevel.Get<RCP<SmootherBase>>("PreSmoother", schurFactManager->GetFactory("Smoother").get());
163  }
164 
166 }
167 
168 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
169 void UzawaSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector &X, const MultiVector &B, bool /* InitialGuessIsZero */) const {
170  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::UzawaSmoother::Apply(): Setup() has not been called");
171 
172  Teuchos::RCP<Teuchos::FancyOStream> fos = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
173 
175 
176  bool bRangeThyraMode = rangeMapExtractor_->getThyraMode();
177  bool bDomainThyraMode = domainMapExtractor_->getThyraMode();
178 
179  // extract parameters from internal parameter list
181  LocalOrdinal nSweeps = pL.get<LocalOrdinal>("Sweeps");
182  Scalar omega = pL.get<Scalar>("Damping factor");
183 
184  // wrap current solution vector in RCP
185  RCP<MultiVector> rcpX = Teuchos::rcpFromRef(X);
186  RCP<const MultiVector> rcpB = Teuchos::rcpFromRef(B);
187 
188  // make sure that both rcpX and rcpB are BlockedMultiVector objects
189  bool bCopyResultX = false;
190  bool bReorderX = false;
191  bool bReorderB = false;
192  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
193  MUELU_TEST_FOR_EXCEPTION(bA.is_null() == true, Exceptions::RuntimeError, "MueLu::BlockedGaussSeidelSmoother::Apply(): A_ must be a BlockedCrsMatrix");
194  RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
195  RCP<const BlockedMultiVector> bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
196 
197  // check the type of operator
200 
201  if (rbA.is_null() == false) {
202  // A is a ReorderedBlockedCrsMatrix and thus uses nested maps, retrieve BlockedCrsMatrix and use plain blocked
203  // map for the construction of the blocked multivectors
204 
205  // check type of X vector
206  if (bX.is_null() == true) {
207  RCP<MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(rbA->getBlockedCrsMatrix()->getBlockedDomainMap(), rcpX));
208  rcpX.swap(vectorWithBlockedMap);
209  bCopyResultX = true;
210  bReorderX = true;
211  }
212 
213  // check type of B vector
214  if (bB.is_null() == true) {
215  RCP<const MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(rbA->getBlockedCrsMatrix()->getBlockedRangeMap(), rcpB));
216  rcpB.swap(vectorWithBlockedMap);
217  bReorderB = true;
218  }
219  } else {
220  // A is a BlockedCrsMatrix and uses a plain blocked map
221  if (bX.is_null() == true) {
222  RCP<MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(bA->getBlockedDomainMap(), rcpX));
223  rcpX.swap(vectorWithBlockedMap);
224  bCopyResultX = true;
225  }
226 
227  if (bB.is_null() == true) {
228  RCP<const MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(bA->getBlockedRangeMap(), rcpB));
229  rcpB.swap(vectorWithBlockedMap);
230  }
231  }
232 
233  // we now can guarantee that X and B are blocked multi vectors
234  bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
235  bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
236 
237  // Finally we can do a reordering of the blocked multivectors if A is a ReorderedBlockedCrsMatrix
238  if (rbA.is_null() == false) {
239  Teuchos::RCP<const Xpetra::BlockReorderManager> brm = rbA->getBlockReorderManager();
240 
241  // check type of X vector
242  if (bX->getBlockedMap()->getNumMaps() != bA->getDomainMapExtractor()->NumMaps() || bReorderX) {
243  // X is a blocked multi vector but incompatible to the reordered blocked operator A
244  Teuchos::RCP<MultiVector> reorderedBlockedVector = buildReorderedBlockedMultiVector(brm, bX);
245  rcpX.swap(reorderedBlockedVector);
246  }
247 
248  if (bB->getBlockedMap()->getNumMaps() != bA->getRangeMapExtractor()->NumMaps() || bReorderB) {
249  // B is a blocked multi vector but incompatible to the reordered blocked operator A
250  Teuchos::RCP<const MultiVector> reorderedBlockedVector = buildReorderedBlockedMultiVector(brm, bB);
251  rcpB.swap(reorderedBlockedVector);
252  }
253  }
254 
255  // Throughout the rest of the algorithm rcpX and rcpB are used for solution vector and RHS
256 
257  // create residual vector
258  // contains current residual of current solution X with rhs B
259  RCP<MultiVector> residual = MultiVectorFactory::Build(rcpB->getMap(), rcpB->getNumVectors());
260  RCP<BlockedMultiVector> bresidual = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(residual);
261  Teuchos::RCP<MultiVector> r1 = bresidual->getMultiVector(0, bRangeThyraMode);
262  Teuchos::RCP<MultiVector> r2 = bresidual->getMultiVector(1, bRangeThyraMode);
263 
264  // helper vector 1
265  RCP<MultiVector> xtilde = MultiVectorFactory::Build(rcpX->getMap(), rcpX->getNumVectors());
266  RCP<BlockedMultiVector> bxtilde = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(xtilde);
267  RCP<MultiVector> xtilde1 = bxtilde->getMultiVector(0, bDomainThyraMode);
268  RCP<MultiVector> xtilde2 = bxtilde->getMultiVector(1, bDomainThyraMode);
269 
270  // incrementally improve solution vector X
271  for (LocalOrdinal run = 0; run < nSweeps; ++run) {
272  // 1) calculate current residual
273  residual->update(one, *rcpB, zero); // residual = B
274  A_->apply(*rcpX, *residual, Teuchos::NO_TRANS, -one, one);
275 
276  // 2) solve F * \Delta \tilde{x}_1 = r_1
277  // start with zero guess \Delta \tilde{x}_1
278  bxtilde->putScalar(zero);
279  velPredictSmoo_->Apply(*xtilde1, *r1);
280 
281  // 3) calculate rhs for SchurComp equation
282  // r_2 - D \Delta \tilde{x}_1
283  RCP<MultiVector> schurCompRHS = MultiVectorFactory::Build(r2->getMap(), rcpB->getNumVectors());
284  D_->apply(*xtilde1, *schurCompRHS);
285  schurCompRHS->update(one, *r2, -one);
286 
287  // 4) solve SchurComp equation
288  // start with zero guess \Delta \tilde{x}_2
289  schurCompSmoo_->Apply(*xtilde2, *schurCompRHS);
290 
291  rcpX->update(omega, *bxtilde, one);
292  }
293 
294  if (bCopyResultX == true) {
295  RCP<MultiVector> Xmerged = bX->Merge();
296  X.update(one, *Xmerged, zero);
297  }
298 }
299 
300 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
303  return rcp(new UzawaSmoother(*this));
304 }
305 
306 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
308  std::ostringstream out;
310  out << "{type = " << type_ << "}";
311  return out.str();
312 }
313 
314 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
317 
318  if (verbLevel & Parameters0) {
319  out0 << "Prec. type: " << type_ << /*" Sweeps: " << nSweeps_ << " damping: " << omega_ <<*/ std::endl;
320  }
321 
322  if (verbLevel & Debug) {
323  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
324  }
325 }
326 
327 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
329  // FIXME: This is a placeholder
331 }
332 
333 } // namespace MueLu
334 
335 #endif /* MUELU_UZAWASMOOTHER_DEF_HPP_ */
Important warning messages (one line)
virtual const Teuchos::ParameterList & GetParameterList() const
Exception indicating invalid cast attempted.
void Setup(Level &currentLevel)
Setup routine.
MueLu::DefaultLocalOrdinal LocalOrdinal
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void swap(RCP< T > &r_ptr)
Print additional debugging information.
void DeclareInput(Level &currentLevel) const
Input.
void Apply(MultiVector &X, MultiVector const &B, bool InitialGuessIsZero=false) const
Apply the Braess Sarazin smoother.
RCP< const ParameterList > GetValidParameterList() const
Input.
void SetVelocityPredictionFactoryManager(RCP< FactoryManager > FactManager)
Set factory manager for internal velocity prediction.
virtual const RCP< const FactoryBase > GetFactory(const std::string &varName) const =0
Get.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
Block triangle Uzawa smoother for 2x2 block matrices.
std::string description() const
Return a simple one-line description of this object.
RCP< SmootherPrototype > Copy() const
bool IsSetup() const
Get the state of a smoother prototype.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Print class parameters.
void SetSchurCompFactoryManager(RCP< FactoryManager > FactManager)
Set factory manager for internal SchurComplement handling.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
Scalar SC
void AddFactoryManager(RCP< const FactoryManagerBase > FactManager, int pos)
Add a factory manager at a specific position.
void residual(const Operator< SC, LO, GO, NO > &Aop, const MultiVector< SC, LO, GO, NO > &X_in, const MultiVector< SC, LO, GO, NO > &B_in, MultiVector< SC, LO, GO, NO > &R_in)
Exception throws to report errors in the internal logical of the program.
#define MUELU_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
An exception safe way to call the method &#39;Level::SetFactoryManager()&#39;.
Teuchos::RCP< const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > buildReorderedBlockedMultiVector(Teuchos::RCP< const Xpetra::BlockReorderManager > brm, Teuchos::RCP< const Xpetra::BlockedMultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > bvec)
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)
bool is_null() const