MueLu  Version of the Day
MueLu_BlockedDirectSolver_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  * MueLu_BlockedDirectSolver_def.hpp
48  *
49  * Created on: 09.02.2014
50  * Author: tobias
51  */
52 
53 #ifndef MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
54 #define MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
55 
56 #include "Teuchos_ArrayViewDecl.hpp"
57 #include "Teuchos_ScalarTraits.hpp"
58 
59 #include "MueLu_ConfigDefs.hpp"
60 
61 #include <Xpetra_Matrix.hpp>
64 
65 #include "MueLu_BlockedDirectSolver.hpp"
66 #include "MueLu_MergedBlockedMatrixFactory.hpp"
67 #include "MueLu_Level.hpp"
68 #include "MueLu_Utilities.hpp"
69 #include "MueLu_Monitor.hpp"
70 #include "MueLu_HierarchyUtils.hpp"
71 #include "MueLu_SmootherBase.hpp"
72 #include "MueLu_DirectSolver.hpp"
73 
74 namespace MueLu {
75 
76  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
78  : type_("blocked direct solver")
79  {
80  MergedAFact_ = Teuchos::rcp(new MergedBlockedMatrixFactory());
81 
82  Teuchos::ParameterList params;
83  s_ = Teuchos::rcp(new DirectSolver("", params));
84  }
85 
86  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88  RCP<ParameterList> validParamList = rcp(new ParameterList());
89 
90  validParamList->set< RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
91 
92  return validParamList;
93  }
94 
95  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
97  // Note that we have a nested smoother/solver object (of type DirectSolver), so we have to declare the dependencies by hand
98  // call DeclareInput by hand, since this->Input(currentLevel, "A") would not properly free A in the release mode (dependencies)
99  // We need the blocked version of A as input for the MergedAFact_
100  currentLevel.DeclareInput("A",this->GetFactory("A").get());
101 
102  // syncronize input factory for "A" and nested representation for "A"
103  MergedAFact_->SetFactory("A", this->GetFactory("A"));
104 
105  // declare input factories for nested direct solver
106  s_->SetFactory("A",MergedAFact_);
107  s_->DeclareInput(currentLevel);
108  }
109 
110  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
112  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
113 
114  FactoryMonitor m(*this, "Setup BlockedDirectSolver", currentLevel);
115  if (this->IsSetup() == true)
116  this->GetOStream(Warnings0) << "MueLu::BlockedDirectSolver::Setup(): Setup() has already been called";
117 
118  // extract blocked operator A from current level
119  A_ = Factory::Get< RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
120  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
121  TEUCHOS_TEST_FOR_EXCEPTION(bA.is_null(), Exceptions::BadCast,
122  "MueLu::BlockedDirectSolver::Build: input matrix A is not of type BlockedCrsMatrix.");
123 
124  s_->Setup(currentLevel);
125 
126  this->IsSetup(true);
127  }
128 
129  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
130  void BlockedDirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector &X, const MultiVector& B, bool InitialGuessIsZero) const {
131  TEUCHOS_TEST_FOR_EXCEPTION(this->IsSetup() == false, Exceptions::RuntimeError,
132  "MueLu::BlockedDirectSolver::Apply(): Setup() has not been called");
133  TEUCHOS_TEST_FOR_EXCEPTION(X.getMap()->isSameAs(*(A_->getDomainMap())) == false, Exceptions::RuntimeError,
134  "MueLu::BlockedDirectSolver::Apply(): Map of solution vector X is not same as domain map of A.");
135  TEUCHOS_TEST_FOR_EXCEPTION(B.getMap()->isSameAs(*(A_->getRangeMap())) == false, Exceptions::RuntimeError,
136  "MueLu::BlockedDirectSolver::Apply(): Map of rhs vector X is not same as range map of A.");
137 
138  s_->Apply(X, B, InitialGuessIsZero);
139  }
140 
141  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
142  RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
144  return rcp (new BlockedDirectSolver (*this));
145  }
146 
147  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
149  std::ostringstream out;
151  out << "{type = " << type_ << "}";
152  return out.str();
153  }
154 
155  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
156  void BlockedDirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream &out, const VerbLevel verbLevel) const {
158 
159  if (verbLevel & Parameters0)
160  out0 << "Prec. type: " << type_ << std::endl;
161 
162  if (verbLevel & Debug)
163  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
164  }
165 
166 } // namespace MueLu
167 
168 
169 
170 #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...
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
bool IsSetup() const
Get the state of a smoother prototype.
Timer to be used in factories. Similar to Monitor but with additional timers.
void DeclareInput(Level &currentLevel) const
Input.
Print additional debugging information.
Namespace for MueLu classes and methods.
Class that encapsulates direct solvers. Autoselection of AmesosSmoother or Amesos2Smoother according ...
RCP< const ParameterList > GetValidParameterList() const
Input.
RCP< MergedBlockedMatrixFactory > MergedAFact_
Factory to generate merged block matrix.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
direct solver for nxn blocked matrices
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
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.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Print class parameters.
Exception throws to report errors in the internal logical of the program.
RCP< SmootherPrototype > Copy() const
RCP< DirectSolver > s_
Direct solver.
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 description() const
Return a simple one-line description of this object.