Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_STK_PeriodicBC_Parser.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) 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 Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
44 
46 
47 #include "Teuchos_ParameterListExceptions.hpp"
48 
49 namespace panzer_stk {
50 
52  : countStr_("Count")
53  , condPrefix_("Periodic Condition ")
54 {
55 }
56 
57 const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > &
59 {
60  return matchers_;
61 }
62 
64 {
65  if(!pl->isParameter(countStr_)) {
66  bool validEntry = false;
69  "Error, the parameter {name=\"" << countStr_ << "\","
70  "type=\"int\""
71  "\nis required in parameter (sub)list \""<< pl->name() <<"\"."
72  "\n\nThe valid parameters and types are:\n"
74  << "Passed parameter list: \n" << pl->currentParametersString()
75  );
76  }
77 
78  int numBCs = pl->get<int>(countStr_);
80 
81  // loop over boundary conditions
82  for(int i=1;i<=numBCs;i++) {
83  std::stringstream ss;
84 
85  ss << condPrefix_ << i;
86  std::string cond = pl->get<std::string>(ss.str());
87 
88  std::pair<std::string, unsigned int> matcherPair = getMatcherTypeAndDim(cond);
89  std::string matcherType = matcherPair.first;
90  unsigned int matcherDim = matcherPair.second;
91  if(matcherType == "coord"){
92  matchers_.push_back(buildMatcher(cond));
93  }else if(matcherType == "edge")
94  edgeMatchers_.push_back(buildMatcher(cond));
95  else if(matcherType == "face")
96  faceMatchers_.push_back(buildMatcher(cond));
97  else if(matcherType == "all"){
98  matchers_.push_back(buildMatcher(replaceMatcherType(cond,"coord")));
99  edgeMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"edge")));
100  if(matcherDim > 2)
101  faceMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"face")));
102  }
103  }
104 
105  // Order BCs with all coords first, followed by edges, then faces
106  matchers_.insert(matchers_.end(),edgeMatchers_.begin(),edgeMatchers_.end());
107  matchers_.insert(matchers_.end(),faceMatchers_.begin(),faceMatchers_.end());
108 
109  storedPL_ = pl;
110 }
111 
113 {
114  return storedPL_;
115 }
116 
118 {
120  storedPL_ = Teuchos::null;
121  return pl;
122 }
123 
125 {
127 
128  // build a sample parameter list with a single preconditioner
129  if(pl==Teuchos::null) {
130  std::stringstream ss;
131  ss << condPrefix_ << 1 << std::endl;
132 
134  pl->set<int>(countStr_,1,
135  "Number of set periodic boundary conditions");
136  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2",
137  "Boundary condition fairs formatted: <MatchCondition> <bndry1>;<bndry2>");
138  }
139 
140  return pl.getConst();
141 }
142 
144 {
145  TEUCHOS_TEST_FOR_EXCEPTION(count<0,std::logic_error,
146  "PeriodicBC requires a positive number (or none) of periodic boundary conditions.");
147 
149  pl->set(countStr_,count);
150 
151  for(int k=1;k<=count;k++) {
152  std::stringstream ss;
153  ss << condPrefix_ << k;
154 
155  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2");
156  }
157 
158  return pl;
159 }
160 
161 // basic string utilities to help wit parsing (only local)
163 
164 static std::string trim_left(const std::string & s)
165 {
166  std::string::size_type beg = s.find_first_not_of(' ');
167 
168  return s.substr(beg,s.length()-beg);
169 }
170 
171 static std::string trim_right(const std::string & s)
172 {
173  std::string::size_type end = s.find_last_not_of(' ');
174 
175  return s.substr(0,end+1);
176 }
177 
178 static std::string trim(const std::string & s)
179 {
180  return trim_right(trim_left(s));
181 }
182 
184 
185 std::pair<std::string, unsigned int> PeriodicBC_Parser::getMatcherTypeAndDim(const std::string & buildStr) const
186 {
187  std::string::size_type endMatch = buildStr.find_first_of(' ');
188 
189  std::string matcher = trim(buildStr.substr(0,endMatch));
190 
191  std::string::size_type hyphenMatch = matcher.find_last_of('-');
192 
193  TEUCHOS_TEST_FOR_EXCEPTION(hyphenMatch==std::string::npos,std::logic_error,
194  "Failed parsing parameter list: could not find periodic boundary "
195  "condition matcher \"" << matcher << "\" "
196  "in string \"" << buildStr << "\n"
197  "Matcher " << matcher << " requires a hyphen, e.g. x-coord, yz-edge\"");
198 
199  std::string matcherType = trim(matcher.substr(hyphenMatch+1,matcher.length()));
200 
201  TEUCHOS_TEST_FOR_EXCEPTION((matcherType != "coord") && (matcherType != "edge") && (matcherType != "face") && (matcherType != "all"),std::logic_error,
202  "Failed parsing parameter list: could not find periodic boundary "
203  "condition matcher \"" << matcher << "\" "
204  "in string \"" << buildStr << "\n"
205  "Type " << matcherType << " is not a valid boundary condition type. Must be coord, edge, face, or all\"");
206 
207  std::string matcherCoord = trim(matcher.substr(0,hyphenMatch));
208  unsigned int matcherDim = 3;
209  if((matcherCoord == "x") || (matcherCoord == "y") || (matcherCoord == "z"))
210  matcherDim = 2;
211 
212  return std::make_pair(matcherType,matcherDim);
213 }
214 
215 std::string PeriodicBC_Parser::replaceMatcherType(const std::string & buildStr, const std::string & matcherType) const
216 {
217  std::string::size_type allPosition = buildStr.find("all");
218 
219  std::string beforeType = trim(buildStr.substr(0,allPosition));
220  std::string afterType = trim(buildStr.substr(allPosition+3,buildStr.length()));
221 
222  return beforeType + matcherType + " " + afterType;
223 }
224 
225 void PeriodicBC_Parser::buildMatcher_Tokenize(const std::string & buildStr,
226  std::string & matcher,
227  std::string & bndry1,
228  std::string & bndry2) const
229 {
230  std::string::size_type endMatch = buildStr.find_first_of(' ');
231  std::string::size_type begBndry = buildStr.find_first_of(';');
232 
233  matcher = trim(buildStr.substr(0,endMatch));
234  bndry1 = trim(buildStr.substr(endMatch,begBndry-endMatch));
235  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
236 }
237 
238 bool PeriodicBC_Parser::buildMatcher_Tokenize_withParams(const std::string & buildStr,
239  std::string & matcher,
240  std::vector<std::string> & params,
241  std::string & bndry1,
242  std::string & bndry2) const
243 {
244  std::string::size_type endMatchAndParams = buildStr.find_first_of(':');
245  std::string::size_type begBndry = buildStr.find_first_of(';');
246 
247  // no parameters: default to old style input
248  if(endMatchAndParams==std::string::npos) {
249  buildMatcher_Tokenize(buildStr,matcher,bndry1,bndry2);
250  return false;
251  }
252 
253  bndry1 = trim(buildStr.substr(endMatchAndParams+1,begBndry-(endMatchAndParams+1)));
254  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
255 
256  std::string matchAndParams = trim(buildStr.substr(0,endMatchAndParams));
257  std::string::size_type endMatch = matchAndParams.find_first_of(' ');
258 
259  // no parameters included
260  if(endMatch==std::string::npos) {
261  matcher = matchAndParams;
262  return true;
263  }
264 
265  // find parameters
267 
268  // check matching conditions
269  matcher = trim(matchAndParams.substr(0,endMatch));
270  matchAndParams = matchAndParams.substr(endMatch+1);
271 
272  std::string::size_type comma = matchAndParams.find_first_of(',');
273  while(comma!=std::string::npos) {
274  std::string p = trim(matchAndParams.substr(0,comma));
275 
276  TEUCHOS_TEST_FOR_EXCEPTION(p.length()<1,std::logic_error,
277  "Error parsing periodic boundary condition \"" + buildStr + "\"");
278 
279  params.push_back(p);
280  matchAndParams = matchAndParams.substr(comma+1);
281  comma = matchAndParams.find_first_of(',');
282  }
283 
284  std::string finalParam = trim(matchAndParams);
285  if(finalParam.length()>0)
286  params.push_back(finalParam);
287 
288  return true;
289 }
290 
292 PeriodicBC_Parser::buildMatcher(const std::string & buildStr) const
293 {
294  std::vector<std::string> params;
295  std::string s_matcher, bndry1, bndry2;
296 
297  buildMatcher_Tokenize_withParams(buildStr,s_matcher,params,bndry1,bndry2);
298 
299  if(s_matcher=="x-coord") {
300  panzer_stk::CoordMatcher matcher(0,params);
301  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
302  }
303 
304  if(s_matcher=="y-coord") {
305  panzer_stk::CoordMatcher matcher(1,params);
306  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
307  }
308 
309  if(s_matcher=="z-coord") {
310  panzer_stk::CoordMatcher matcher(2,params);
311  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
312  }
313 
314  if(s_matcher=="x-edge") {
315  panzer_stk::CoordMatcher matcher(0,params);
316  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
317  }
318 
319  if(s_matcher=="y-edge") {
320  panzer_stk::CoordMatcher matcher(1,params);
321  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
322  }
323 
324  if(s_matcher=="z-edge") {
325  panzer_stk::CoordMatcher matcher(2,params);
326  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
327  }
328 
329  if(s_matcher=="xy-coord" || s_matcher=="yx-coord") {
330  panzer_stk::PlaneMatcher matcher(0,1,params);
331  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
332  }
333 
334  if(s_matcher=="xz-coord" || s_matcher=="zx-coord") {
335  panzer_stk::PlaneMatcher matcher(0,2,params);
336  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
337  }
338 
339  if(s_matcher=="yz-coord" || s_matcher=="zy-coord") {
340  panzer_stk::PlaneMatcher matcher(1,2,params);
341  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
342  }
343 
344  if(s_matcher=="xy-edge" || s_matcher=="yx-edge") {
345  panzer_stk::PlaneMatcher matcher(0,1,params);
346  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
347  }
348 
349  if(s_matcher=="xz-edge" || s_matcher=="zx-edge") {
350  panzer_stk::PlaneMatcher matcher(0,2,params);
351  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
352  }
353 
354  if(s_matcher=="yz-edge" || s_matcher=="zy-edge") {
355  panzer_stk::PlaneMatcher matcher(1,2,params);
356  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
357  }
358 
359  if(s_matcher=="xy-face" || s_matcher=="yx-face") {
360  panzer_stk::PlaneMatcher matcher(0,1,params);
361  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
362  }
363 
364  if(s_matcher=="xz-face" || s_matcher=="zx-face") {
365  panzer_stk::PlaneMatcher matcher(0,2,params);
366  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
367  }
368 
369  if(s_matcher=="yz-face" || s_matcher=="zy-face") {
370  panzer_stk::PlaneMatcher matcher(1,2,params);
371  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
372  }
373 
374  if(s_matcher=="wx-coord") {
376  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
377  }
378 
379  if(s_matcher=="wy-coord") {
381  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
382  }
383 
384  if(s_matcher=="wx-edge") {
386  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
387  }
388 
389  if(s_matcher=="wy-edge") {
391  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
392  }
393 
394  if(s_matcher=="wx-face") {
396  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
397  }
398 
399  if(s_matcher=="wy-face") {
401  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
402  }
403 
404  if(s_matcher=="(xy)z-quarter-coord") {
405  panzer_stk::QuarterPlaneMatcher matcher(0,1,2,params);
406  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
407  }
408 
409  if(s_matcher=="(yx)z-quarter-coord") {
410  panzer_stk::QuarterPlaneMatcher matcher(1,0,2,params);
411  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
412  }
413 
414  if(s_matcher=="(xz)y-quarter-coord") {
415  panzer_stk::QuarterPlaneMatcher matcher(0,2,1,params);
416  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
417  }
418 
419  if(s_matcher=="(zx)y-quarter-coord") {
420  panzer_stk::QuarterPlaneMatcher matcher(2,0,1,params);
421  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
422  }
423 
424  if(s_matcher=="(yz)x-quarter-coord") {
425  panzer_stk::QuarterPlaneMatcher matcher(1,2,0,params);
426  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
427  }
428 
429  if(s_matcher=="(zy)x-quarter-coord") {
430  panzer_stk::QuarterPlaneMatcher matcher(2,1,0,params);
431  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
432  }
433 
434  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
435  "Failed parsing parameter list: could not find periodic boundary "
436  "condition matcher \"" << s_matcher << "\" "
437  "in string \"" << buildStr << "\"");
438 
439  return Teuchos::null;
440 }
441 
442 }
bool buildMatcher_Tokenize_withParams(const std::string &buildStr, std::string &matcher, std::vector< std::string > &params, std::string &bndry1, std::string &bndry2) const
RCP< const T > getConst() const
const std::string & name() const
std::string currentParametersString() const
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)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< Teuchos::ParameterList > storedPL_
stored parameter list
static std::string trim_right(const std::string &s)
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > faceMatchers_
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > matchers_
matchers constructed by &quot;setParameterList&quot;
bool isParameter(const std::string &name) const
const std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > & getMatchers() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > edgeMatchers_
void buildMatcher_Tokenize(const std::string &buildStr, std::string &matcher, std::string &bndry1, std::string &bndry2) const
std::string replaceMatcherType(const std::string &buildStr, const std::string &matcherType) const
static std::string trim_left(const std::string &s)
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Teuchos::RCP< PeriodicBC_MatcherBase > buildPeriodicBC_Matcher(const std::string &left, const std::string &right, const Matcher &matcher, const std::string type="coord")
static std::string trim(const std::string &s)
std::pair< std::string, unsigned int > getMatcherTypeAndDim(const std::string &buildStr) const
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
TransListIter end
Teuchos::RCP< const PeriodicBC_MatcherBase > buildMatcher(const std::string &buildStr) const
#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg)