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"
16 #include "MueLu_TopSmootherFactory.hpp"
17 #include "MueLu_Level_fwd.hpp"
19 //#include "MueLu_SmootherBase_fwd.hpp"
20 #include "MueLu_SmootherFactory.hpp"
21 #include "MueLu_SmootherPrototype.hpp"
22 //#include "MueLu_Hierarchy_fwd.hpp"
23 //#include "MueLu_HierarchyManager_fwd.hpp"
24 
25 namespace MueLu {
26  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28  TEUCHOS_TEST_FOR_EXCEPTION(varName != "CoarseSolver" && varName != "Smoother", Exceptions::RuntimeError, "varName should be either \"CoarseSolver\" or \"Smoother\"");
29 
30  if (varName == "CoarseSolver") {
31  // For coarsest level, we only need one smoother/solver
32  // If a user wants to do something weird there (like, solve coarsest system by using 2 forward
33  // GS and 1 backward GS), one can use MergedSmoother
34  RCP<const FactoryBase> coarseSolverFactory = parentFactoryManager->GetFactory("CoarseSolver");
35  RCP<const SmootherFactory> coarseSmootherFactory = Teuchos::rcp_dynamic_cast<const SmootherFactory>(coarseSolverFactory);
36  if (coarseSmootherFactory != Teuchos::null) {
37  RCP<SmootherPrototype> preProto;
38  RCP<SmootherPrototype> postProto;
39  coarseSmootherFactory->GetSmootherPrototypes(preProto, postProto);
40 
41  if (preProto == postProto)
42  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
43  else {
44  // check whether pre- and/or post-smoothing is desired on coarsest level
45  if(preProto != Teuchos::null)
46  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
47  if(postProto != Teuchos::null)
48  postSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
49  }
50  }
51  else // default handling: get default direct solver as presmoother on coarsest level
52  preSmootherFact_ = parentFactoryManager->GetFactory("CoarseSolver");
53 
54  } else {
55  preSmootherFact_ = parentFactoryManager->GetFactory("PreSmoother");
56  postSmootherFact_ = parentFactoryManager->GetFactory("PostSmoother");
57  }
58  }
59 
60  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62 
63  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
65  if (preSmootherFact_ != Teuchos::null)
66  level.DeclareInput("PreSmoother", preSmootherFact_.get());
67  if (postSmootherFact_ != Teuchos::null)
68  level.DeclareInput("PostSmoother", postSmootherFact_.get());
69  }
70 
71  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
73  if (preSmootherFact_.is_null() && postSmootherFact_.is_null())
74  return;
75 
76  // 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.
77  // 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
78  // 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.
79 
80  // NOTE 2: some smoother factories are tricky (see comments in MueLu::SmootherFactory
81  // Sometimes, we don't know whether the factory is able to generate "PreSmoother" or "PostSmoother"
82  // For the SmootherFactory, however, we are able to check that.
83 
84  if (!preSmootherFact_.is_null()) {
85  // Checking for null is not sufficient, as SmootherFactory(null, something) does not generate "PreSmoother"
86  bool isAble = true;
87  RCP<const SmootherFactory> s = rcp_dynamic_cast<const SmootherFactory>(preSmootherFact_);
88  if (!s.is_null()) {
89  RCP<SmootherPrototype> pre, post;
90  s->GetSmootherPrototypes(pre, post);
91  if (pre.is_null())
92  isAble = false;
93  } else {
94  // We assume that if presmoother factory is not SmootherFactory, it *is* able to generate "PreSmoother"
95  }
96 
97  if (isAble) {
98  RCP<SmootherBase> Pre = level.Get<RCP<SmootherBase> >("PreSmoother", preSmootherFact_.get());
99 
100  level.Set ("PreSmoother", Pre, NoFactory::get());
101 
102  level.AddKeepFlag ("PreSmoother", NoFactory::get(), MueLu::Final);
103  level.RemoveKeepFlag("PreSmoother", NoFactory::get(), MueLu::UserData);
104  }
105  }
106 
107  if (!postSmootherFact_.is_null()) {
108  // Checking for null is not sufficient, as SmootherFactory(something, null) does not generate "PostSmoother"
109  bool isAble = true;
110  RCP<const SmootherFactory> s = rcp_dynamic_cast<const SmootherFactory>(postSmootherFact_);
111  if (!s.is_null()) {
112  RCP<SmootherPrototype> pre, post;
113  s->GetSmootherPrototypes(pre, post);
114  if (post.is_null())
115  isAble = false;
116  } else {
117  // We assume that if presmoother factory is not SmootherFactory, it *is* able to generate "PreSmoother"
118  }
119 
120  if (isAble) {
121  RCP<SmootherBase> Post = level.Get<RCP<SmootherBase> >("PostSmoother", postSmootherFact_.get());
122 
123  level.Set ("PostSmoother", Post, NoFactory::get());
124 
125  level.AddKeepFlag ("PostSmoother", NoFactory::get(), MueLu::Final);
126  level.RemoveKeepFlag("PostSmoother", NoFactory::get(), MueLu::UserData);
127  }
128  }
129  }
130 }
131 
132 
133 #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