Debian Codegraph @

packages/muelu/src/MueCentral/MueLu_MasterList.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef MUELU_MASTERLIST_HPP
11 #define MUELU_MASTERLIST_HPP
12
13 #include <sstream>
14 #include <map>
15
16 #include <Teuchos_ParameterList.hpp>
17 #include <Teuchos_ParameterEntry.hpp>
18 #include <Teuchos_any.hpp>
19
20 namespace MueLu {
21
22 /*!
23 @class MasterList class.
24 @brief Static class that holds the complete list of valid MueLu parameters.
25
26 This class creates a ParameterList that is used to validate user-supplied ParameterLists. This same list
27 is the source of default values if a parameter does not appear in the user ParameterList.
28 This class also provides ParameterLists for certain common problem types, such as 2D and 3D isotropic Poisson.
29 @todo Add method to register user-defined problem type. This will need both string identifier
30 and ParameterList (or string version of parameter list).
31
32 */
33
34 template <typename T, typename U>
35 class DefaultProblemStrings;
36
37 class MasterList {
38 public:
39 //! @brief Return a "master" list of all valid parameters and their default values.
40 static Teuchos::RCP<const Teuchos::ParameterList> List();
41 //! @brief Return default parameter settings for the specified problem type.
42 static Teuchos::RCP<Teuchos::ParameterList> GetProblemSpecificList(std::string const& problemType);
43
44 //! @brief Returns default value on the "master" list for a parameter with the specified name and type.
45 template <typename T>
46 static const T& getDefault(const std::string& name) {
47 return List()->get<T>(name);
48 }
49
50 //! @brief Returns default entry from the "master" list corresponding to the specified name.
51 static const Teuchos::ParameterEntry& getEntry(const std::string& name) {
52 return List()->getEntry(name);
53 }
54
55 //! @brief Create xml string for given MueLu parameter (easy xml input format)
56 //!
57 //! @note: We should check whether template type T is the same as the expected parameter type in the parameter list
58 template <typename T>
59 static std::string generateXMLParameterString(const std::string& name, T data) {
60 Teuchos::ParameterEntry entry = getEntry(name);
61 std::stringstream ss;
62 // TODO: check whether getAny().typeName() matches the typeid(T)
63 // One could use a technique as described here: http://stackoverflow.com/questions/4484982/how-to-convert-typename-t-to-string-in-c
64 ss << "<Parameter name=\"" << name << "\" type=\"" << entry.getAny().typeName() << "\" value=\"" << data << "\"/>";
65 return ss.str();
66 }
67
68 //! @brief Translate ML parameter to corresponding MueLu parameter
69 static std::string ML2MueLu(const std::string& name) {
70 std::map<std::string, std::string>::iterator it;
71 it = ML2MueLuLists_.find(name);
72 if (it != ML2MueLuLists_.end())
73 return it->second;
74 else
75 return "";
76 }
77
78 static std::string interpretParameterName(const std::string& name, const std::string& value);
79
80 private:
81 MasterList();
82 MasterList(const MasterList&);
83 MasterList& operator=(const MasterList&);
84
85 //! @brief A ParameterList that holds all valid parameters and their default values.
86 static Teuchos::RCP<Teuchos::ParameterList> masterList_;
87 //! @brief String equivalent of the masterList_.
88 static const std::string stringList_;
89 //! @brief A ParameterList that holds all valid parameters and their default values for a particular problem type.
90 static Teuchos::RCP<Teuchos::ParameterList> problemSpecificList_;
91 //! @brief The problem type associated with the current problem-specific ParameterList.
92 static std::string problemType_;
93 //! @brief Map of string equivalents of the problemSpecificList_. The first entry is the problem type, the second is the string equivalent.
94 static std::map<std::string, std::string> DefaultProblemTypeLists_;
95 //! @brief Map of ML parameter strings to corresponding MueLu parametes
96 static std::map<std::string, std::string> ML2MueLuLists_;
97 };
98
99 /*!
100 @class DefaultProblemStrings class.
101 @brief Helper class to initialize DefaultProblemTypeLists_ in class MasterList.
102 */
103 template <typename T, typename U>
104 class DefaultProblemStrings {
105 public:
106 DefaultProblemStrings(const T& key, const U& val) {
107 map_[key] = val;
108 }
109
110 DefaultProblemStrings<T, U>& operator()(const T& key, const U& val) {
111 map_[key] = val;
112 return *this;
113 }
114
115 operator std::map<T, U>() const {
116 return map_;
117 }
118
119 private:
120 std::map<T, U> map_;
121 };
122
123 } // namespace MueLu
124
125 #endif