MueLu  Version of the Day
MueLu_MergedSmoother_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_MERGEDSMOOTHER_DEF_HPP
47 #define MUELU_MERGEDSMOOTHER_DEF_HPP
48 
50 #include "MueLu_Exceptions.hpp"
51 
52 namespace MueLu {
53 
54  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
55  MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MergedSmoother(ArrayRCP<RCP<SmootherPrototype> > & smootherList, bool verbose)
56  : smootherList_(smootherList), reverseOrder_(false), verbose_(verbose) {
57  // TODO: check that on each method TEUCHOS_TEST_FOR_EXCEPTION(smootherList == Teuchos::null, MueLu::Exceptions::RuntimeError, "");
58 
60  }
61 
62  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
64  : reverseOrder_(src.reverseOrder_), verbose_(src.verbose_) {
65  // Deep copy of src.smootherList_
67  }
68 
69  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
70  void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetFactory(const std::string& varName, const RCP<const FactoryBase>& factory) {
71  // We need to propagate SetFactory to proper place
72  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); it++)
73  (*it)->SetFactory(varName, factory);
74  }
75 
76 
77  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
79  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); ++it)
80  (*it)->DeclareInput(currentLevel);
81  }
82 
83  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
85  if (SmootherPrototype::IsSetup() == true)
86  this->GetOStream(Warnings0) << "MueLu::MergedSmoother::Setup(): Setup() has already been called";
87 
88  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); ++it) {
89  try {
90  (*it)->Setup(level);
91 
93  std::string msg = "MueLu::MergedSmoother<>::Setup(): Runtime Error.\n One of the underlying smoother throwed the following exception: \n"; msg += e.what();
95  }
96  }
97 
99  }
100 
101  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
102  void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
103  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, MueLu::Exceptions::RuntimeError, "MueLu::MergedSmoother<>:Apply(): Setup() has not been called");
104 
105  typedef typename ArrayRCP<RCP<SmootherPrototype> >::size_type sz_t;
106  sz_t n = smootherList_.size(), c = (reverseOrder_ ? n-1 : 0);
107  char d = (reverseOrder_ ? -1 : 1);
108 
109  for (sz_t i = 0; i < n; i++) // loop unifying both forward and reverse order
110  try {
111  // Be careful with nonnegative numbers
112  smootherList_[c + d*Teuchos::as<char>(i)]->Apply(X, B, InitialGuessIsZero);
113 
114  // For second and later iterations, initial guess = previous result
115  InitialGuessIsZero = false;
116 
118  std::string msg = "MueLu::MergedSmoother<>::Apply(): Runtime Error. One of the underlying smoothers throws the following exception: \n"; msg += e.what();
120  }
121  }
122 
123  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
124  void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
125  throw Exceptions::NotImplemented("MueLu::MergedSmoother<>::Print() is not implemented");
126  }
127 
128  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
129  void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CopyParameters(RCP<SmootherPrototype> src) { // TODO: wrong prototype. We do not need an RCP here.
130  RCP<MergedSmoother> srcMergedSmoother = rcp_dynamic_cast<MergedSmoother>(src); // TODO: check if dynamic cast fails
131 
132  reverseOrder_ = srcMergedSmoother->GetReverseOrder();
133 
134  {
135  const ArrayRCP<const RCP<SmootherPrototype> >& srcSmootherList = srcMergedSmoother->GetSmootherList();
136  const ArrayRCP<const RCP<SmootherPrototype> >& thisSmootherList = smootherList_;
137  TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList == Teuchos::null, MueLu::Exceptions::RuntimeError, // might be allowed later if useful
138  "MueLu::MergedSmoother<>:CopyParameters(): thisSmootherList == Teuchos::null");
139 
140  // If the smootherList of 'this' and 'src' contains the same type of smoothers,
141  // we can transfert parameters from src to 'this' in order to tentatively reuse
142  // the current setup information of each smoothers. Note that the reuse of the
143  // setup phase of the MergedSmoother 'src' can be implemented for a larger set
144  // of cases (and more complicated cases), but it does not seems useful for now.
145 
146  bool reuse = true; // true == can we transfert parameters of smoothers one by one or do we have to copy the whole list of src?
147 
148  // test 1: same list size
149  reuse = reuse && (thisSmootherList.size() == srcSmootherList.size());
150 
151  if (reuse) {
152  // test 2: one-by-one comparison of smoother types
153  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
154  // The following test should never throw in our use cases because 'src' is a prototype and
155  // 'this' is a real smoother so they don't share any data. We may allow such case later if useful.
156  TEUCHOS_TEST_FOR_EXCEPTION((thisSmootherList[i] == srcSmootherList[i]) && (thisSmootherList[i] != Teuchos::null), MueLu::Exceptions::RuntimeError,
157  "MueLu::MergedSmoother<>:CopyParameters(): internal logic error");
158 
159  //TODO
160  // reuse = reuse && ((thisSmootherList[i] == Teuchos::null && srcSmootherList[i] == Teuchos::null) ||
161  // thisSmootherList[i]->GetType() == srcSmootherList[i]->GetType());
162  }
163  }
164 
165  reuse = false; //TODO: temporary disactivated.
166 
167  if (reuse) {
168  bool isSetup = true;
169 
170  // Call CopyParameters for each smoothers and update IsSetup status of the MergedSmoother
171  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
172  if (srcSmootherList[i] != Teuchos::null) {
173  TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList[i] == Teuchos::null, MueLu::Exceptions::RuntimeError, "MueLu::MergedSmoother<>:CopyParameters(): internal logic error");
174 
175  //TODO thisSmootherList[i]->CopyParameters(srcSmootherList[i]);
176  isSetup = isSetup && thisSmootherList[i]->IsSetup();
177  }
179  }
180 
181  } else {
182  // No reuse: copy srcSmootherList.
183  smootherList_ = Teuchos::null;
184  smootherList_ = SmootherListDeepCopy(srcSmootherList);
186  }
187 
188  }
189  }
190 
191  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
192  RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
194  Copy () const
195  {
196  return rcp(new MergedSmoother(*this));
197  }
198 
199  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
200  ArrayRCP<RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >
202  SmootherListDeepCopy (const ArrayRCP<const RCP<SmootherPrototype> >& srcSmootherList)
203  {
204  ArrayRCP<RCP<SmootherPrototype> > newSmootherList(srcSmootherList.size());
205 
206  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++)
207  newSmootherList[i] = srcSmootherList[i]->Copy();
208 
209  return newSmootherList;
210  }
211 
212 } //namespace MueLu
213 
214 #endif // MUELU_MERGEDSMOOTHER_DEF_HPP
Important warning messages (one line)
ArrayRCP< RCP< SmootherPrototype > > smootherList_
RCP< SmootherPrototype > Copy() const
Copy method (performs a deep copy of input object)
Copy
bool IsSetup() const
Get the state of a smoother prototype.
Namespace for MueLu classes and methods.
void CopyParameters(RCP< SmootherPrototype > src)
void DeclareInput(Level &currentLevel) const
Input.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
Exception throws when you call an unimplemented method of MueLu.
ArrayRCP< RCP< SmootherPrototype > > SmootherListDeepCopy(const ArrayRCP< const RCP< SmootherPrototype > > &srcSmootherList)
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.
MergedSmoother(ArrayRCP< RCP< SmootherPrototype > > &smootherList, bool verbose=false)
Constructor.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Exception throws to report errors in the internal logical of the program.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply.
const ArrayRCP< const RCP< SmootherPrototype > > GetSmootherList() const
void Setup(Level &level)
Set up.