MueLu  Version of the Day
MueLu_Utilities.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 #include "MueLu_Utilities_def.hpp"
47 
48 #include <string>
49 
50 #ifdef HAVE_MUELU_EPETRAEXT
52 #endif
53 
54 namespace MueLu {
55 
56  /* Removes the following non-serializable data (A,P,R,Nullspace,Coordinates)
57  from level-specific sublists from inList
58  and moves it to nonSerialList. Everything else is copied to serialList.
59  This function returns the level number of the highest level for which
60  non-serializable data was provided.
61  */
64 
65  ParameterList dummy;
66  long maxLevel = 0;
67 
68  for (ParameterList::ConstIterator it = inList.begin(); it != inList.end(); it++) {
69  const std::string& levelName = it->first;
70 
71  // Check for mach of the form "level X" where X is a positive integer
72  if (inList.isSublist(levelName) && ((levelName.find("level ") == 0 && levelName.size() > 6) || levelName.find("user data") == 0)) {
73  int levelID = strtol(levelName.substr(6).c_str(), 0, 0);
74  bool userFlag = true;
75  if(levelName.find("user data") == std::string::npos) { // if "user data" is not found in levelName, switc userFlag and set levelID
76  userFlag = false;
77  levelID = strtol(levelName.substr(6).c_str(), 0, 0);
78  if (maxLevel < levelID)
79  maxLevel = levelID;
80  }
81 
82  // Split the sublist
83  const ParameterList& levelList = inList.sublist(levelName);
84  for (ParameterList::ConstIterator it2 = levelList.begin(); it2 != levelList.end(); it2++) {
85  const std::string& name = it2->first;
86  if (name == "A" || name == "P" || name == "R" || name== "M" || name == "K" || name == "Nullspace" || name == "Coordinates"
87 #ifdef HAVE_MUELU_INTREPID2 // For the IntrepidPCoarsenFactory
88  || name == "pcoarsen: element to node map"
89 #endif
90  ) {
91  nonSerialList.sublist(levelName).setEntry(name, it2->second);
92  }
93 #ifdef HAVE_MUELU_MATLAB
94  else if(!userFlag && IsParamMuemexVariable(name))
95  {
96  nonSerialList.sublist(levelName).setEntry(name, it2->second);
97  }
98 #endif
99  else if( userFlag && IsParamValidVariable(name)) {
100  nonSerialList.sublist(levelName).setEntry(name, it2->second);
101  } else {
102  serialList.sublist(levelName).setEntry(name, it2->second);
103  }
104  }
105 
106  } else {
107  serialList.setEntry(it->first, it->second);
108  }
109  }
110 
111  return maxLevel;
112  }
113 
114  void TokenizeStringAndStripWhiteSpace(const std::string& stream, std::vector<std::string>& tokenList, const char* delimChars)
115  {
116  //note: default delimiter string is ","
117  // Take a comma-separated list and tokenize it, stripping out leading & trailing whitespace. Then add to tokenList
118  char* buf = (char*) malloc(stream.size() + 1);
119  strcpy(buf, stream.c_str());
120  char* token = strtok(buf, delimChars);
121  if(token == NULL)
122  {
123  free(buf);
124  return;
125  }
126  while(token)
127  {
128  //token points to start of string to add to tokenList
129  //remove front whitespace...
130  char* tokStart = token;
131  char* tokEnd = token + strlen(token) - 1;
132  while(*tokStart == ' ' && tokStart < tokEnd)
133  tokStart++;
134  while(*tokEnd == ' ' && tokStart < tokEnd)
135  tokEnd--;
136  tokEnd++;
137  if(tokStart < tokEnd)
138  {
139  std::string finishedToken(tokStart, tokEnd - tokStart); //use the constructor that takes a certain # of chars
140  tokenList.push_back(finishedToken);
141  }
142  token = strtok(NULL, delimChars);
143  }
144  free(buf);
145  }
146 
147  bool IsParamMuemexVariable(const std::string& name)
148  {
149  //see if paramName is exactly two "words" - like "OrdinalVector myNullspace" or something
150  char* str = (char*) malloc(name.length() + 1);
151  strcpy(str, name.c_str());
152  //Strip leading and trailing whitespace
153  char* firstWord = strtok(str, " ");
154  if(!firstWord) {
155  free(str);
156  return false;
157  }
158  char* secondWord = strtok(NULL, " ");
159  if(!secondWord) {
160  free(str);
161  return false;
162  }
163  char* thirdWord = strtok(NULL, " ");
164  if(thirdWord) {
165  free(str);
166  return false;
167  }
168  //convert first word to all lowercase for case insensitive compare
169  char* tolowerIt = firstWord;
170  while(*tolowerIt)
171  {
172  *tolowerIt = (char) tolower(*tolowerIt);
173  tolowerIt++;
174  }
175  //See if the first word is one of the custom variable names
176  if(strstr(firstWord, "matrix") ||
177  strstr(firstWord, "multivector") ||
178  strstr(firstWord, "map") ||
179  strstr(firstWord, "ordinalvector") ||
180  strstr(firstWord, "int") ||
181  strstr(firstWord, "scalar") ||
182  strstr(firstWord, "double") ||
183  strstr(firstWord, "complex") ||
184  strstr(firstWord, "string"))
185  //Add name to list of keys to remove
186  {
187  free(str);
188  return true;
189  }
190  else
191  {
192  free(str);
193  return false;
194  }
195  }
196 
197 bool IsParamValidVariable(const std::string& name)
198  {
199  //see if paramName is exactly two "words" - like "OrdinalVector myNullspace" or something
200  char* str = (char*) malloc(name.length() + 1);
201  strcpy(str, name.c_str());
202  //Strip leading and trailing whitespace
203  char* firstWord = strtok(str, " ");
204  if(!firstWord) {
205  free(str);
206  return false;
207  }
208  char* secondWord = strtok(NULL, " ");
209  if(!secondWord) {
210  free(str);
211  return false;
212  }
213  char* thirdWord = strtok(NULL, " ");
214  if(thirdWord) {
215  free(str);
216  return false;
217  }
218  //convert first word to all lowercase for case insensitive compare
219  char* tolowerIt = firstWord;
220  while(*tolowerIt)
221  {
222  *tolowerIt = (char) tolower(*tolowerIt);
223  tolowerIt++;
224  }
225  //See if the first word is one of the custom variable names
226  if(strstr(firstWord, "matrix") ||
227  strstr(firstWord, "multivector") ||
228  strstr(firstWord, "map") ||
229  strstr(firstWord, "ordinalvector") ||
230  strstr(firstWord, "int") ||
231  strstr(firstWord, "scalar") ||
232  strstr(firstWord, "double") ||
233  strstr(firstWord, "complex") ||
234  strstr(firstWord, "string") ||
235  strstr(firstWord, "array<go>") ||
236  strstr(firstWord, "array<lo>") ||
237  strstr(firstWord, "arrayrcp<lo>") ||
238  strstr(firstWord, "arrayrcp<go>"))
239  //Add name to list of keys to remove
240  {
241  free(str);
242  return true;
243  }
244  else
245  {
246  free(str);
247  return false;
248  }
249  }
250 
251 } // namespace MueLu
ConstIterator begin() const
bool isSublist(const std::string &name) const
std::string tolower(const std::string &str)
Namespace for MueLu classes and methods.
bool IsParamMuemexVariable(const std::string &name)
ParameterList & setEntry(const std::string &name, const ParameterEntry &entry)
ConstIterator end() const
params_t::ConstIterator ConstIterator
bool IsParamValidVariable(const std::string &name)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
void TokenizeStringAndStripWhiteSpace(const std::string &stream, std::vector< std::string > &tokenList, const char *delimChars)
long ExtractNonSerializableData(const Teuchos::ParameterList &inList, Teuchos::ParameterList &serialList, Teuchos::ParameterList &nonSerialList)