MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_TopSmootherFactory_def.hpp
Go to the documentation of this file.
1 /*
2  * MueLu_TopSmootherFactory_def.hpp
3  *
4  * Created on: Jan 25, 2016
5  * Author: tawiesn
6  */
7 
8 #ifndef PACKAGES_MUELU_SRC_MUECENTRAL_MUELU_TOPSMOOTHERFACTORY_DEF_HPP_
9 #define PACKAGES_MUELU_SRC_MUECENTRAL_MUELU_TOPSMOOTHERFACTORY_DEF_HPP_
10 
11 #include "MueLu_ConfigDefs.hpp"
12 
13 //#include "MueLu_FactoryManager_fwd.hpp"
15 //#include "MueLu_HierarchyHelpers_fwd.hpp"
17 //#include "MueLu_SmootherBase_fwd.hpp"
18 #include "MueLu_SmootherFactory.hpp"
19 #include "MueLu_SmootherPrototype.hpp"
20 //#include "MueLu_Hierarchy_fwd.hpp"
21 //#include "MueLu_HierarchyManager_fwd.hpp"
22 
23 namespace MueLu {
24 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
26  TEUCHOS_TEST_FOR_EXCEPTION(varName != "CoarseSolver" && varName != "Smoother", Exceptions::RuntimeError, "varName should be either \"CoarseSolver\" or \"Smoother\"");
27 
28  if (varName == "CoarseSolver") {
29  // For coarsest level, we only need one smoother/solver
30  // If a user wants to do something weird there (like, solve coarsest system by using 2 forward
31  // GS and 1 backward GS), one can use MergedSmoother
32  RCP<const FactoryBase> coarseSolverFactory = parentFactoryManager->GetFactory("CoarseSolver");
33  RCP<const SmootherFactory> coarseSmootherFactory = Teuchos::rcp_dynamic_cast<const SmootherFactory>(coarseSolverFactory);
34  if (coarseSmootherFactory != Teuchos::null) {
35  RCP<SmootherPrototype> preProto;
36  RCP<SmootherPrototype> postProto;
37  coarseSmootherFactory->GetSmootherPrototypes(preProto, postProto);
38 
39  if (preProto == postProto)
40  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
41  else {
42  // check whether pre- and/or post-smoothing is desired on coarsest level
43  if (preProto != Teuchos::null)
44  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
45  if (postProto != Teuchos::null)
46  postSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
47  }
48  } else // default handling: get default direct solver as presmoother on coarsest level
49  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
50 
51  } else {
52  preSmootherFact_ = parentFactoryManager->GetFactory("PreSmoother");
53  postSmootherFact_ = parentFactoryManager->GetFactory("PostSmoother");
54  }
55 }
56 
57 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
59 
60 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62  if (preSmootherFact_ != Teuchos::null)
63  level.DeclareInput("PreSmoother", preSmootherFact_.get());
64  if (postSmootherFact_ != Teuchos::null)
65  level.DeclareInput("PostSmoother", postSmootherFact_.get());
66 }
67 
68 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  if (preSmootherFact_.is_null() && postSmootherFact_.is_null())
71  return;
72 
73  // NOTE 1: We need to set at least some keep flag for the smoothers, otherwise it is going to be removed as soon as all requests are released.
74  // We choose to set the Final flag for the data. In addition, we allow this data to be retrieved by only using the name by the means
75  // of using NoFactory. However, any data set with NoFactory gets UserData flag by default. We don't really want that flag, so we remove it.
76 
77  // NOTE 2: some smoother factories are tricky (see comments in MueLu::SmootherFactory
78  // Sometimes, we don't know whether the factory is able to generate "PreSmoother" or "PostSmoother"
79  // For the SmootherFactory, however, we are able to check that.
80 
81  if (!preSmootherFact_.is_null()) {
82  // Checking for null is not sufficient, as SmootherFactory(null, something) does not generate "PreSmoother"
83  bool isAble = true;
84  RCP<const SmootherFactory> s = rcp_dynamic_cast<const SmootherFactory>(preSmootherFact_);
85  if (!s.is_null()) {
86  RCP<SmootherPrototype> pre, post;
87  s->GetSmootherPrototypes(pre, post);
88  if (pre.is_null())
89  isAble = false;
90  } else {
91  // We assume that if presmoother factory is not SmootherFactory, it *is* able to generate "PreSmoother"
92  }
93 
94  if (isAble) {
95  RCP<SmootherBase> Pre = level.Get<RCP<SmootherBase> >("PreSmoother", preSmootherFact_.get());
96 
97  level.Set("PreSmoother", Pre, NoFactory::get());
98 
99  level.AddKeepFlag("PreSmoother", NoFactory::get(), MueLu::Final);
100  level.RemoveKeepFlag("PreSmoother", NoFactory::get(), MueLu::UserData);
101  }
102  }
103 
104  if (!postSmootherFact_.is_null()) {
105  // Checking for null is not sufficient, as SmootherFactory(something, null) does not generate "PostSmoother"
106  bool isAble = true;
107  RCP<const SmootherFactory> s = rcp_dynamic_cast<const SmootherFactory>(postSmootherFact_);
108  if (!s.is_null()) {
109  RCP<SmootherPrototype> pre, post;
110  s->GetSmootherPrototypes(pre, post);
111  if (post.is_null())
112  isAble = false;
113  } else {
114  // We assume that if presmoother factory is not SmootherFactory, it *is* able to generate "PreSmoother"
115  }
116 
117  if (isAble) {
118  RCP<SmootherBase> Post = level.Get<RCP<SmootherBase> >("PostSmoother", postSmootherFact_.get());
119 
120  level.Set("PostSmoother", Post, NoFactory::get());
121 
122  level.AddKeepFlag("PostSmoother", NoFactory::get(), MueLu::Final);
123  level.RemoveKeepFlag("PostSmoother", NoFactory::get(), MueLu::UserData);
124  }
125  }
126 }
127 } // namespace MueLu
128 
129 #endif /* PACKAGES_MUELU_SRC_MUECENTRAL_MUELU_TOPSMOOTHERFACTORY_DEF_HPP_ */
Generic Smoother Factory for generating the smoothers of the MG hierarchy.
void Build(Level &level) const
Build an object with this factory.
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level-&gt;Get&lt; RCP&lt;Matrix&gt; &gt;(&quot;A&quot;, factory) if factory == NULL =&gt; use default factory.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
User data are always kept. This flag is set automatically when Level::Set(&quot;data&quot;, data) is used...
static const NoFactory * get()
virtual const RCP< const FactoryBase > GetFactory(const std::string &varName) const =0
Get.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
Keep data only for this run. Used to keep data useful for Hierarchy::Iterate(). Data will be deleted ...
void RemoveKeepFlag(const std::string &ename, const FactoryBase *factory, KeepType keep=MueLu::All)
void AddKeepFlag(const std::string &ename, const FactoryBase *factory=NoFactory::get(), KeepType keep=MueLu::Keep)
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
void GetSmootherPrototypes(RCP< SmootherPrototype > &preSmootherPrototype, RCP< SmootherPrototype > &postSmootherPrototype) const
Get smoother prototypes.
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()
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
TopSmootherFactory(RCP< const FactoryManagerBase > parentFactoryManager, const std::string &varName)
bool is_null() const