MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_FineLevelInputDataFactory_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 // Tobias Wiesner (tawiesn@sandia.gov)
42 // Ray Tuminaro (rstumin@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef PACKAGES_MUELU_SRC_MISC_MUELU_FINELEVELINPUTDATAFACTORY_DEF_HPP_
48 #define PACKAGES_MUELU_SRC_MISC_MUELU_FINELEVELINPUTDATAFACTORY_DEF_HPP_
49 
50 #include "Xpetra_Matrix.hpp"
51 
53 #include "MueLu_Level.hpp"
54 #include "MueLu_Monitor.hpp"
55 
56 namespace MueLu {
57 
58 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
60  RCP<ParameterList> validParamList = rcp(new ParameterList());
61 
62  // Variable name (e.g. A or P or Coordinates)
63  validParamList->set<std::string>("Variable", std::string("A"), "Variable name on all coarse levels (except the finest level).");
64 
65  // Names of generating factories (on finest level and coarse levels)
66  validParamList->set<RCP<const FactoryBase> >("Fine level factory", Teuchos::null, "Generating factory of the fine level variable");
67  validParamList->set<RCP<const FactoryBase> >("Coarse level factory", Teuchos::null, "Generating factory for data on all coarse levels (except the finest)");
68 
69  // Type of variable (see source code for a complete list of all available types)
70  validParamList->set<std::string>("Variable type", std::string("Matrix"), "Type of variable");
71 
72  return validParamList;
73 }
74 
75 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
77  const ParameterList& pL = GetParameterList();
78 
79  std::string variableName = "";
80  if (pL.isParameter("Variable"))
81  variableName = pL.get<std::string>("Variable");
82 
83  std::string factoryName = "NoFactory";
84  if (currentLevel.GetLevelID() == 0) {
85  factoryName = "Fine level factory";
86  } else {
87  factoryName = "Coarse level factory";
88  }
89 
90  TEUCHOS_TEST_FOR_EXCEPTION(variableName == "", MueLu::Exceptions::RuntimeError, "FineLevelInputDataFactory: no variable name provided. Please set \'Variable\' parameter in your input deck.");
91 
92  // data must be specified in factory! (not in factory manager)
93  RCP<const FactoryBase> fact = GetFactory(factoryName);
94  currentLevel.DeclareInput(variableName, fact.get(), this);
95 }
96 
97 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
99  FactoryMonitor m(*this, "InputUserData", currentLevel);
100 
101  const ParameterList& pL = GetParameterList();
102 
103  std::string variableName = "";
104  if (pL.isParameter("Variable"))
105  variableName = pL.get<std::string>("Variable");
106 
107  std::string variableType = "";
108  if (pL.isParameter("Variable type"))
109  variableType = pL.get<std::string>("Variable type");
110 
111  std::string factoryName = "NoFactory";
112  if (currentLevel.GetLevelID() == 0) {
113  factoryName = "Fine level factory";
114  } else {
115  factoryName = "Coarse level factory";
116  }
117  RCP<const FactoryBase> fact = GetFactory(factoryName);
118 
119  GetOStream(Debug) << "Use " << variableName << " of type " << variableType << " from " << factoryName << "(" << fact.get() << ")" << std::endl;
120 
121  // check data type
122  // std::string strType = currentLevel.GetTypeName(variableName, fact.get());
123  if (variableType == "int") {
124  int data = currentLevel.Get<int>(variableName, fact.get());
125  Set(currentLevel, variableName, data);
126  } else if (variableType == "double") {
127  double data = currentLevel.Get<double>(variableName, fact.get());
128  Set(currentLevel, variableName, data);
129  } else if (variableType == "string") {
130  std::string data = currentLevel.Get<std::string>(variableName, fact.get());
131  Set(currentLevel, variableName, data);
132  } else {
133  size_t npos = std::string::npos;
134 
135  if (variableType.find("Aggregates") != npos) {
136  RCP<Aggregates> data = currentLevel.Get<RCP<Aggregates> >(variableName, fact.get());
137  Set(currentLevel, variableName, data);
138  } else if (variableType.find("SmootherBase") != npos) {
139  RCP<SmootherBase> data = currentLevel.Get<RCP<SmootherBase> >(variableName, fact.get());
140  Set(currentLevel, variableName, data);
141  } else if (variableType.find("SmootherPrototype") != npos) {
142  RCP<SmootherPrototype> data = currentLevel.Get<RCP<SmootherPrototype> >(variableName, fact.get());
143  Set(currentLevel, variableName, data);
144  } else if (variableType.find("Export") != npos) {
145  RCP<Export> data = currentLevel.Get<RCP<Export> >(variableName, fact.get());
146  Set(currentLevel, variableName, data);
147  } else if (variableType.find("Import") != npos) {
148  RCP<Import> data = currentLevel.Get<RCP<Import> >(variableName, fact.get());
149  Set(currentLevel, variableName, data);
150  } else if (variableType.find("Map") != npos) {
151  RCP<Map> data = currentLevel.Get<RCP<Map> >(variableName, fact.get());
152  Set(currentLevel, variableName, data);
153  } else if (variableType.find("Matrix") != npos) {
154  RCP<Matrix> data = currentLevel.Get<RCP<Matrix> >(variableName, fact.get());
155  Set(currentLevel, variableName, data);
156  } else if (variableType.find("MultiVector") != npos) {
157  RCP<MultiVector> data = currentLevel.Get<RCP<MultiVector> >(variableName, fact.get());
158  Set(currentLevel, variableName, data);
159  } else if (variableType.find("Operator") != npos) {
160  RCP<Operator> data = currentLevel.Get<RCP<Operator> >(variableName, fact.get());
161  Set(currentLevel, variableName, data);
162  } else {
163  // TAW: is this working with empty procs?
164  TEUCHOS_TEST_FOR_EXCEPTION(true, MueLu::Exceptions::RuntimeError, "FineLevelInputDataFactory: cannot detect type of variable " << variableName << " generated by " << fact.get() << ". User provided type " << variableType);
165  }
166  }
167 }
168 
169 } // namespace MueLu
170 
171 #endif /* PACKAGES_MUELU_SRC_MISC_MUELU_FINELEVELINPUTDATAFACTORY_DEF_HPP_ */
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.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.
T * get() const
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void Build(Level &currentLevel) const
Build method.
void DeclareInput(Level &currentLevel) const
Input.
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
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()