MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_ParameterListUtils.cpp
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 
48 #include "MueLu_Exceptions.hpp"
49 
50 namespace MueLu {
51 
52  /* See also: ML_Epetra::UpdateList */
58  void MergeParameterList(const Teuchos::ParameterList &source, Teuchos::ParameterList &dest, bool overWrite){
59  for(Teuchos::ParameterList::ConstIterator param=source.begin(); param!=source.end(); ++param)
60  if (dest.isParameter(source.name(param)) == false || overWrite)
61  dest.setEntry(source.name(param),source.entry(param));
62  }
63 
65  {
67  using std::string;
68 
69  newList.setName(List.name());
70 
71  // Copy general (= not level-specific) options and sublists to the new list.
72  // - Coarse and level-specific parameters are not copied yet. They will be moved to sublists later.
73  // - Already existing level-specific lists are copied to the new list but the coarse list is not copied
74  // yet because it has to be modified before copy (s/coarse/smoother/)
75  for (ParameterList::ConstIterator param=List.begin(); param!=List.end(); ++param)
76  {
77  const string & pname=List.name(param);
78 
79  if ((pname.find(" (level",0) == string::npos || pname.find("smoother: list (level",0) == 0 || pname.find("aggregation: list (level",0) == 0) &&
80  (pname.find("coarse: ",0) == string::npos))
81  {
82  newList.setEntry(pname,List.entry(param));
83  }
84  } // for
85 
86  // Copy of the sublist "coarse: list" to the new list. Change "coarse:" to "smoother:" along the way.
87  if (List.isSublist("coarse: list")) {
88  const ParameterList &coarseList = List.sublist("coarse: list");
89  ParameterList &newCoarseList = newList.sublist("coarse: list");
90  for (ParameterList::ConstIterator param=coarseList.begin(); param!=coarseList.end() ; ++param) {
91  const string & pname=coarseList.name(param);
92 
93  if (pname.find("coarse:",0) == 0) {
94  // change "coarse: " to "smoother:"
95  newCoarseList.setEntry("smoother: "+pname.substr(8),coarseList.entry(param));
96  } else {
97  newCoarseList.setEntry(pname,coarseList.entry(param));
98  }
99  }
100  } // if
101 
102  // Copy of level-specific parameters and coarse parameters to sublist
103  for (ParameterList::ConstIterator param=List.begin(); param!=List.end(); ++param)
104  {
105  const string & pname=List.name(param);
106  if (pname.find(" (level",0) != string::npos && pname.find("smoother: list (level",0) != 0 && pname.find("aggregation: list (level",0) != 0)
107  {
108  // Copy level-specific parameters (smoother and aggregation)
109 
110  // Scan pname (ex: pname="smoother: type (level 2)")
111  string type, option;
112  int levelID=-1;
113  {
114  typedef Teuchos::ArrayRCP<char>::size_type size_type; // (!)
115  Teuchos::Array<char> ctype (size_type(pname.size()+1));
116  Teuchos::Array<char> coption(size_type(pname.size()+1));
117 
118  int matched = sscanf(pname.c_str(),"%s %[^(](level %d)", ctype.getRawPtr(), coption.getRawPtr(), &levelID); // use [^(] instead of %s to allow for strings with white-spaces (ex: "ifpack list")
119  type = string(ctype.getRawPtr());
120  option = string(coption.getRawPtr()); option.resize(option.size () - 1); // remove final white-space
121 
122  if (matched != 3 || (type != "smoother:" && type != "aggregation:")) {
123  TEUCHOS_TEST_FOR_EXCEPTION(true, MueLu::Exceptions::RuntimeError, "MueLu::CreateSublist(), Line " << __LINE__ << ". "
124  << "Error in creating level-specific sublists" << std::endl
125  << "Offending parameter: " << pname << std::endl);
126  }
127  }
128 
129  // Create/grab the corresponding sublist of newList
130  ParameterList &newSubList = newList.sublist(type + " list (level " + Teuchos::toString(levelID) + ")");
131  // Shove option w/o level number into sublist
132  newSubList.setEntry(type + " " + option,List.entry(param));
133 
134  } else if (pname.find("coarse:",0) == 0 && pname != "coarse: list") {
135  // Copy coarse parameters
136  ParameterList &newCoarseList = newList.sublist("coarse: list"); // the coarse sublist is created only if there is at least one "coarse:" parameter
137  newCoarseList.setEntry("smoother: "+pname.substr(8),List.entry(param)); // change "coarse: " to "smoother:"
138  } // end if
139 
140  } // for
141 
142  } //MueLu::CreateSublist()
143 
144  // Usage: GetMLSubList(paramList, "smoother", 2);
145  const Teuchos::ParameterList & GetMLSubList(const Teuchos::ParameterList & paramList, const std::string & type, int levelID) {
146  static const Teuchos::ParameterList emptyParamList;
147 
148  char levelChar[11];
149  sprintf(levelChar, "(level %d)", levelID);
150  std::string levelStr(levelChar);
151 
152  if (paramList.isSublist(type + ": list " + levelStr)) {
153  return paramList.sublist(type + ": list " + levelStr);
154  } else {
155  return emptyParamList;
156  }
157  }
158 
159  // Extract all the parameters that begin with "str:" (but skip sublist)
162 
163  for (Teuchos::ParameterList::ConstIterator param = paramList.begin(); param != paramList.end(); ++param) {
164  const Teuchos::ParameterEntry & entry = paramList.entry(param);
165  const std::string & pname = paramList.name(param);
166  if (pname.find(str+":",0) == 0 && !entry.isList()) {
167  subList->setEntry(pname,entry);
168  }
169  }
170 
171  return subList;
172  }
173 
174  // replace all string occurrences "from" with "to" in "str"
175  void replaceAll(std::string& str, const std::string& from, const std::string& to) {
176  if(from.empty())
177  return;
178  size_t start_pos = 0;
179  while((start_pos = str.find(from, start_pos)) != std::string::npos) {
180  str.replace(start_pos, from.length(), to);
181  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
182  }
183  }
184 
185 } // namespace MueLu
const std::string & name() const
void MergeParameterList(const Teuchos::ParameterList &source, Teuchos::ParameterList &dest, bool overWrite)
: merge two parameter lists
ConstIterator end() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const Teuchos::ParameterList & GetMLSubList(const Teuchos::ParameterList &paramList, const std::string &type, int levelID)
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
bool isParameter(const std::string &name) const
void CreateSublists(const ParameterList &List, ParameterList &newList)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool isSublist(const std::string &name) const
Teuchos::RCP< Teuchos::ParameterList > ExtractSetOfParameters(const Teuchos::ParameterList &paramList, const std::string &str)
params_t::ConstIterator ConstIterator
ConstIterator begin() const
const ParameterEntry & entry(ConstIterator i) const
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & setName(const std::string &name)
Exception throws to report errors in the internal logical of the program.
void replaceAll(std::string &str, const std::string &from, const std::string &to)
std::string toString(const T &t)