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  */
62  long ExtractNonSerializableData(const Teuchos::ParameterList& inList, Teuchos::ParameterList& serialList, Teuchos::ParameterList& nonSerialList) {
63  using Teuchos::ParameterList;
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) {
73  int levelID = strtol(levelName.substr(6).c_str(), 0, 0);
74  if (maxLevel < levelID)
75  maxLevel = levelID;
76 
77  // Split the sublist
78  const ParameterList& levelList = inList.sublist(levelName);
79  for (ParameterList::ConstIterator it2 = levelList.begin(); it2 != levelList.end(); it2++) {
80  const std::string& name = it2->first;
81  if (name == "A" || name == "P" || name == "R" || name == "Nullspace" || name == "Coordinates")
82  nonSerialList.sublist(levelName).setEntry(name, it2->second);
83  #ifdef HAVE_MUELU_MATLAB
84  else if(IsParamMuemexVariable(name))
85  {
86  nonSerialList.sublist(levelName).setEntry(name, it2->second);
87  }
88  #endif
89  else
90  serialList.sublist(levelName).setEntry(name, it2->second);
91  }
92 
93  } else {
94  serialList.setEntry(it->first, it->second);
95  }
96  }
97 
98  return maxLevel;
99  }
100 
101  void TokenizeStringAndStripWhiteSpace(const std::string& stream, std::vector<std::string>& tokenList, const char* delimChars)
102  {
103  //note: default delimiter string is ","
104  // Take a comma-separated list and tokenize it, stripping out leading & trailing whitespace. Then add to tokenList
105  char* buf = (char*) malloc(stream.size() + 1);
106  strcpy(buf, stream.c_str());
107  char* token = strtok(buf, delimChars);
108  if(token == NULL)
109  {
110  free(buf);
111  return;
112  }
113  while(token)
114  {
115  //token points to start of string to add to tokenList
116  //remove front whitespace...
117  char* tokStart = token;
118  char* tokEnd = token + strlen(token) - 1;
119  while(*tokStart == ' ' && tokStart < tokEnd)
120  tokStart++;
121  while(*tokEnd == ' ' && tokStart < tokEnd)
122  tokEnd--;
123  tokEnd++;
124  if(tokStart < tokEnd)
125  {
126  std::string finishedToken(tokStart, tokEnd - tokStart); //use the constructor that takes a certain # of chars
127  tokenList.push_back(finishedToken);
128  }
129  token = strtok(NULL, delimChars);
130  }
131  free(buf);
132  }
133 
134  bool IsParamMuemexVariable(const std::string& name)
135  {
136  //see if paramName is exactly two "words" - like "OrdinalVector myNullspace" or something
137  char* str = (char*) malloc(name.length() + 1);
138  strcpy(str, name.c_str());
139  //Strip leading and trailing whitespace
140  char* firstWord = strtok(str, " ");
141  if(!firstWord)
142  return false;
143  char* secondWord = strtok(NULL, " ");
144  if(!secondWord)
145  return false;
146  char* thirdWord = strtok(NULL, " ");
147  if(thirdWord)
148  return false;
149  //convert first word to all lowercase for case insensitive compare
150  char* tolowerIt = firstWord;
151  while(*tolowerIt)
152  {
153  *tolowerIt = (char) tolower(*tolowerIt);
154  tolowerIt++;
155  }
156  //See if the first word is one of the custom variable names
157  if(strstr(firstWord, "matrix") ||
158  strstr(firstWord, "multivector") ||
159  strstr(firstWord, "map") ||
160  strstr(firstWord, "ordinalvector") ||
161  strstr(firstWord, "int") ||
162  strstr(firstWord, "scalar") ||
163  strstr(firstWord, "double") ||
164  strstr(firstWord, "complex") ||
165  strstr(firstWord, "string"))
166  //Add name to list of keys to remove
167  {
168  free(str);
169  return true;
170  }
171  else
172  {
173  free(str);
174  return false;
175  }
176  }
177 
178 } // namespace MueLu
Namespace for MueLu classes and methods.
bool IsParamMuemexVariable(const std::string &name)
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)