MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_BlockedDirectSolver_def.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 /*
11  * MueLu_BlockedDirectSolver_def.hpp
12  *
13  * Created on: 09.02.2014
14  * Author: tobias
15  */
16 
17 #ifndef MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
18 #define MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
19 
20 #include "Teuchos_ArrayViewDecl.hpp"
21 #include "Teuchos_ScalarTraits.hpp"
22 
23 #include "MueLu_ConfigDefs.hpp"
24 
25 #include <Xpetra_Matrix.hpp>
27 #include <Xpetra_MultiVectorFactory.hpp>
28 
30 #include "MueLu_MergedBlockedMatrixFactory.hpp"
31 #include "MueLu_Level.hpp"
32 #include "MueLu_Monitor.hpp"
33 #include "MueLu_DirectSolver.hpp"
34 
35 namespace MueLu {
36 
37 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
39  MergedAFact_ = Teuchos::rcp(new MergedBlockedMatrixFactory());
40  s_ = Teuchos::rcp(new DirectSolver(type, paramList));
41  type_ = "blocked direct solver (" + type + ")";
42 }
43 
44 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
46  RCP<ParameterList> validParamList = rcp(new ParameterList());
47 
48  validParamList->set<RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
49 
50  return validParamList;
51 }
52 
53 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
55  // Note that we have a nested smoother/solver object (of type DirectSolver), so we have to declare the dependencies by hand
56  // call DeclareInput by hand, since this->Input(currentLevel, "A") would not properly free A in the release mode (dependencies)
57  // We need the blocked version of A as input for the MergedAFact_
58  currentLevel.DeclareInput("A", this->GetFactory("A").get());
59 
60  // syncronize input factory for "A" and nested representation for "A"
61  MergedAFact_->SetFactory("A", this->GetFactory("A"));
62 
63  // declare input factories for nested direct solver
64  s_->SetFactory("A", MergedAFact_);
65  s_->DeclareInput(currentLevel);
66 }
67 
68 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
71 
72  FactoryMonitor m(*this, "Setup BlockedDirectSolver", currentLevel);
73  if (this->IsSetup() == true)
74  this->GetOStream(Warnings0) << "MueLu::BlockedDirectSolver::Setup(): Setup() has already been called";
75 
76  // extract blocked operator A from current level
77  A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
78  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
80  "MueLu::BlockedDirectSolver::Build: input matrix A is not of type BlockedCrsMatrix.");
81 
82  s_->Setup(currentLevel);
83 
84  this->IsSetup(true);
85 }
86 
87 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88 void BlockedDirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero) const {
89  TEUCHOS_TEST_FOR_EXCEPTION(this->IsSetup() == false, Exceptions::RuntimeError,
90  "MueLu::BlockedDirectSolver::Apply(): Setup() has not been called");
91 
92  RCP<MultiVector> rcpX = Teuchos::rcpFromRef(X);
93  RCP<const MultiVector> rcpB = Teuchos::rcpFromRef(B);
94  RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
95  RCP<const BlockedMultiVector> bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
96 
97 #ifdef HAVE_MUELU_DEBUG
98  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
99  if (bB.is_null() == false) {
100  // TEUCHOS_TEST_FOR_EXCEPTION(A_->getRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of RHS vector B is not the same as range map of the blocked operator A. Please check the map of B and A.");
101  } else {
102  TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of RHS vector B is not the same as range map of the blocked operator A. Please check the map of B and A.");
103  }
104  if (bX.is_null() == false) {
105  // TEUCHOS_TEST_FOR_EXCEPTION(A_->getDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of the solution vector X is not the same as domain map of the blocked operator A. Please check the map of X and A.");
106  } else {
107  TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of the solution vector X is not the same as domain map of the blocked operator A. Please check the map of X and A.");
108  }
109 #endif
110 
111  if (bB.is_null() == true && bX.is_null() == true) {
112  // standard case (neither B nor X are blocked)
113  s_->Apply(X, B, InitialGuessIsZero);
114  } else if (bB.is_null() == false && bX.is_null() == false) {
115  // both B and X are blocked
116  RCP<MultiVector> mergedX = bX->Merge();
117  RCP<const MultiVector> mergedB = bB->Merge();
118  s_->Apply(*mergedX, *mergedB, InitialGuessIsZero);
119  RCP<MultiVector> xx = Teuchos::rcp(new BlockedMultiVector(bX->getBlockedMap(), mergedX));
121  X.update(one, *xx, zero);
122  } else if (bB.is_null() == true && bX.is_null() == false) {
123  // the solution vector is blocked
124  RCP<MultiVector> mergedX = bX->Merge();
125  s_->Apply(*mergedX, B, InitialGuessIsZero);
126  RCP<MultiVector> xx = Teuchos::rcp(new BlockedMultiVector(bX->getBlockedMap(), mergedX));
128  X.update(one, *xx, zero);
129  } else if (bB.is_null() == false && bX.is_null() == true) {
130  // only the RHS vector is blocked
131  RCP<const MultiVector> mergedB = bB->Merge();
132  s_->Apply(X, *mergedB, InitialGuessIsZero);
133  }
134 }
135 
136 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
139  return rcp(new BlockedDirectSolver(*this));
140 }
141 
142 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
144  std::ostringstream out;
146  out << "{type = " << type_ << "}";
147  return out.str();
148 }
149 
150 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
153 
154  if (verbLevel & Parameters0)
155  out0 << "Prec. type: " << type_ << std::endl;
156 
157  if (verbLevel & Debug)
158  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
159 }
160 
161 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
163  // FIXME: This is a placeholder
165 }
166 
167 } // namespace MueLu
168 
169 #endif /* MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_ */
Important warning messages (one line)
Exception indicating invalid cast attempted.
void Setup(Level &currentLevel)
Setup routine Call the underlaying Setup routine of the nested direct solver once the input block mat...
BlockedDirectSolver(const std::string &type="", const Teuchos::ParameterList &paramList=Teuchos::ParameterList())
Constructor.
void DeclareInput(Level &currentLevel) const
Input.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print additional debugging information.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the direct solver. Solves the linear system AX=B using the constructed solver.
RCP< const ParameterList > GetValidParameterList() const
Input.
std::string description() const
Return a simple one-line description of this object.
Class that encapsulates direct solvers. Autoselection of AmesosSmoother or Amesos2Smoother according ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
RCP< SmootherPrototype > Copy() const
bool IsSetup() const
Get the state of a smoother prototype.
direct solver for nxn blocked matrices
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Print class parameters.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Scalar SC
Exception throws to report errors in the internal logical of the program.
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