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 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
12 
14 
15 #include "Teuchos_ParameterListExceptions.hpp"
16 
17 namespace panzer_stk {
18 
20  : countStr_("Count")
21  , condPrefix_("Periodic Condition ")
22  , searchStr_("Use BBox Search")
23  , useBBoxSearch_(false) // TODO swap this to change default search (see also STK_interface.hpp)
24 {
25 }
26 
27 const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > &
29 {
30  return matchers_;
31 }
32 
34 {
35  return useBBoxSearch_;
36 }
37 
39 {
40  if(!pl->isParameter(countStr_)) {
41  bool validEntry = false;
44  "Error, the parameter {name=\"" << countStr_ << "\","
45  "type=\"int\""
46  "\nis required in parameter (sub)list \""<< pl->name() <<"\"."
47  "\n\nThe valid parameters and types are:\n"
49  << "Passed parameter list: \n" << pl->currentParametersString()
50  );
51  }
52 
53  int numBCs = pl->get<int>(countStr_);
56 
57  // loop over boundary conditions
58  for(int i=1;i<=numBCs;i++) {
59  std::stringstream ss;
60 
61  ss << condPrefix_ << i;
62  std::string cond = pl->get<std::string>(ss.str());
63 
64  std::pair<std::string, unsigned int> matcherPair = getMatcherTypeAndDim(cond);
65  std::string matcherType = matcherPair.first;
66  unsigned int matcherDim = matcherPair.second;
67  if(matcherType == "coord"){
68  matchers_.push_back(buildMatcher(cond));
69  }else if(matcherType == "edge")
70  edgeMatchers_.push_back(buildMatcher(cond));
71  else if(matcherType == "face")
72  faceMatchers_.push_back(buildMatcher(cond));
73  else if(matcherType == "all"){
74  matchers_.push_back(buildMatcher(replaceMatcherType(cond,"coord")));
75  edgeMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"edge")));
76  if(matcherDim > 2)
77  faceMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"face")));
78  }
79  }
80 
81  // Order BCs with all coords first, followed by edges, then faces
82  matchers_.insert(matchers_.end(),edgeMatchers_.begin(),edgeMatchers_.end());
83  matchers_.insert(matchers_.end(),faceMatchers_.begin(),faceMatchers_.end());
84 
85  storedPL_ = pl;
86 }
87 
89 {
90  return storedPL_;
91 }
92 
94 {
96  storedPL_ = Teuchos::null;
97  return pl;
98 }
99 
101 {
103 
104  // build a sample parameter list with a single preconditioner
105  if(pl==Teuchos::null) {
106  std::stringstream ss;
107  ss << condPrefix_ << 1 << std::endl;
108 
110  pl->set<int>(countStr_,1,
111  "Number of set periodic boundary conditions");
112  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2",
113  "Boundary condition pairs formatted: <MatchCondition> <bndry1>;<bndry2>");
114  // TODO swap this to change default search (see also STK_interface.hpp)
115  pl->set<bool>(searchStr_,false,"Use bounding box search for GID match (requires STKSearch) or not");
116  }
117 
118  return pl.getConst();
119 }
120 
122 {
123  TEUCHOS_TEST_FOR_EXCEPTION(count<0,std::logic_error,
124  "PeriodicBC requires a positive number (or none) of periodic boundary conditions.");
125 
127  pl->set(countStr_,count);
128  // TODO swap this to change default search (see also STK_interface.hpp)
129  pl->set<bool>(searchStr_,false,"Use bounding box search for GID match (requires STKSearch) or not");
130 
131  for(int k=1;k<=count;k++) {
132  std::stringstream ss;
133  ss << condPrefix_ << k;
134 
135  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2");
136  }
137 
138  return pl;
139 }
140 
141 // basic string utilities to help wit parsing (only local)
143 
144 static std::string trim_left(const std::string & s)
145 {
146  std::string::size_type beg = s.find_first_not_of(' ');
147 
148  return s.substr(beg,s.length()-beg);
149 }
150 
151 static std::string trim_right(const std::string & s)
152 {
153  std::string::size_type end = s.find_last_not_of(' ');
154 
155  return s.substr(0,end+1);
156 }
157 
158 static std::string trim(const std::string & s)
159 {
160  return trim_right(trim_left(s));
161 }
162 
164 
165 std::pair<std::string, unsigned int> PeriodicBC_Parser::getMatcherTypeAndDim(const std::string & buildStr) const
166 {
167  std::string::size_type endMatch = buildStr.find_first_of(' ');
168 
169  std::string matcher = trim(buildStr.substr(0,endMatch));
170 
171  std::string::size_type hyphenMatch = matcher.find_last_of('-');
172 
173  TEUCHOS_TEST_FOR_EXCEPTION(hyphenMatch==std::string::npos,std::logic_error,
174  "Failed parsing parameter list: could not find periodic boundary "
175  "condition matcher \"" << matcher << "\" "
176  "in string \"" << buildStr << "\n"
177  "Matcher " << matcher << " requires a hyphen, e.g. x-coord, yz-edge\"");
178 
179  std::string matcherType = trim(matcher.substr(hyphenMatch+1,matcher.length()));
180 
181  TEUCHOS_TEST_FOR_EXCEPTION((matcherType != "coord") && (matcherType != "edge") && (matcherType != "face") && (matcherType != "all"),std::logic_error,
182  "Failed parsing parameter list: could not find periodic boundary "
183  "condition matcher \"" << matcher << "\" "
184  "in string \"" << buildStr << "\n"
185  "Type " << matcherType << " is not a valid boundary condition type. Must be coord, edge, face, or all\"");
186 
187  std::string matcherCoord = trim(matcher.substr(0,hyphenMatch));
188  unsigned int matcherDim = 3;
189  if((matcherCoord == "x") || (matcherCoord == "y") || (matcherCoord == "z"))
190  matcherDim = 2;
191 
192  return std::make_pair(matcherType,matcherDim);
193 }
194 
195 std::string PeriodicBC_Parser::replaceMatcherType(const std::string & buildStr, const std::string & matcherType) const
196 {
197  std::string::size_type allPosition = buildStr.find("all");
198 
199  std::string beforeType = trim(buildStr.substr(0,allPosition));
200  std::string afterType = trim(buildStr.substr(allPosition+3,buildStr.length()));
201 
202  return beforeType + matcherType + " " + afterType;
203 }
204 
205 void PeriodicBC_Parser::buildMatcher_Tokenize(const std::string & buildStr,
206  std::string & matcher,
207  std::string & bndry1,
208  std::string & bndry2) const
209 {
210  std::string::size_type endMatch = buildStr.find_first_of(' ');
211  std::string::size_type begBndry = buildStr.find_first_of(';');
212 
213  matcher = trim(buildStr.substr(0,endMatch));
214  bndry1 = trim(buildStr.substr(endMatch,begBndry-endMatch));
215  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
216 }
217 
218 bool PeriodicBC_Parser::buildMatcher_Tokenize_withParams(const std::string & buildStr,
219  std::string & matcher,
220  std::vector<std::string> & params,
221  std::string & bndry1,
222  std::string & bndry2) const
223 {
224  std::string::size_type endMatchAndParams = buildStr.find_first_of(':');
225  std::string::size_type begBndry = buildStr.find_first_of(';');
226 
227  // no parameters: default to old style input
228  if(endMatchAndParams==std::string::npos) {
229  buildMatcher_Tokenize(buildStr,matcher,bndry1,bndry2);
230  return false;
231  }
232 
233  bndry1 = trim(buildStr.substr(endMatchAndParams+1,begBndry-(endMatchAndParams+1)));
234  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
235 
236  std::string matchAndParams = trim(buildStr.substr(0,endMatchAndParams));
237  std::string::size_type endMatch = matchAndParams.find_first_of(' ');
238 
239  // no parameters included
240  if(endMatch==std::string::npos) {
241  matcher = matchAndParams;
242  return true;
243  }
244 
245  // find parameters
247 
248  // check matching conditions
249  matcher = trim(matchAndParams.substr(0,endMatch));
250  matchAndParams = matchAndParams.substr(endMatch+1);
251 
252  std::string::size_type comma = matchAndParams.find_first_of(',');
253  while(comma!=std::string::npos) {
254  std::string p = trim(matchAndParams.substr(0,comma));
255 
256  TEUCHOS_TEST_FOR_EXCEPTION(p.length()<1,std::logic_error,
257  "Error parsing periodic boundary condition \"" + buildStr + "\"");
258 
259  params.push_back(p);
260  matchAndParams = matchAndParams.substr(comma+1);
261  comma = matchAndParams.find_first_of(',');
262  }
263 
264  std::string finalParam = trim(matchAndParams);
265  if(finalParam.length()>0)
266  params.push_back(finalParam);
267 
268  return true;
269 }
270 
272 PeriodicBC_Parser::buildMatcher(const std::string & buildStr) const
273 {
274  std::vector<std::string> params;
275  std::string s_matcher, bndry1, bndry2;
276 
277  buildMatcher_Tokenize_withParams(buildStr,s_matcher,params,bndry1,bndry2);
278 
279  if(s_matcher=="x-coord") {
280  panzer_stk::CoordMatcher matcher(0,params);
281  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
282  }
283 
284  if(s_matcher=="y-coord") {
285  panzer_stk::CoordMatcher matcher(1,params);
286  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
287  }
288 
289  if(s_matcher=="z-coord") {
290  panzer_stk::CoordMatcher matcher(2,params);
291  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
292  }
293 
294  if(s_matcher=="x-edge") {
295  panzer_stk::CoordMatcher matcher(0,params);
296  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
297  }
298 
299  if(s_matcher=="y-edge") {
300  panzer_stk::CoordMatcher matcher(1,params);
301  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
302  }
303 
304  if(s_matcher=="z-edge") {
305  panzer_stk::CoordMatcher matcher(2,params);
306  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
307  }
308 
309  if(s_matcher=="xy-coord" || s_matcher=="yx-coord") {
310  panzer_stk::PlaneMatcher matcher(0,1,params);
311  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
312  }
313 
314  if(s_matcher=="xz-coord" || s_matcher=="zx-coord") {
315  panzer_stk::PlaneMatcher matcher(0,2,params);
316  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
317  }
318 
319  if(s_matcher=="yz-coord" || s_matcher=="zy-coord") {
320  panzer_stk::PlaneMatcher matcher(1,2,params);
321  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
322  }
323 
324  if(s_matcher=="xy-edge" || s_matcher=="yx-edge") {
325  panzer_stk::PlaneMatcher matcher(0,1,params);
326  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
327  }
328 
329  if(s_matcher=="xz-edge" || s_matcher=="zx-edge") {
330  panzer_stk::PlaneMatcher matcher(0,2,params);
331  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
332  }
333 
334  if(s_matcher=="yz-edge" || s_matcher=="zy-edge") {
335  panzer_stk::PlaneMatcher matcher(1,2,params);
336  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
337  }
338 
339  if(s_matcher=="xy-face" || s_matcher=="yx-face") {
340  panzer_stk::PlaneMatcher matcher(0,1,params);
341  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
342  }
343 
344  if(s_matcher=="xz-face" || s_matcher=="zx-face") {
345  panzer_stk::PlaneMatcher matcher(0,2,params);
346  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
347  }
348 
349  if(s_matcher=="yz-face" || s_matcher=="zy-face") {
350  panzer_stk::PlaneMatcher matcher(1,2,params);
351  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
352  }
353 
354  if(s_matcher=="wx-coord") {
356  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
357  }
358 
359  if(s_matcher=="wy-coord") {
361  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
362  }
363 
364  if(s_matcher=="wx-edge") {
366  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
367  }
368 
369  if(s_matcher=="wy-edge") {
371  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
372  }
373 
374  if(s_matcher=="wx-face") {
376  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
377  }
378 
379  if(s_matcher=="wy-face") {
381  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
382  }
383 
384  if(s_matcher=="(xy)z-quarter-coord") {
385  panzer_stk::QuarterPlaneMatcher matcher(0,1,2,params);
386  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
387  }
388 
389  if(s_matcher=="(yx)z-quarter-coord") {
390  panzer_stk::QuarterPlaneMatcher matcher(1,0,2,params);
391  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
392  }
393 
394  if(s_matcher=="(xz)y-quarter-coord") {
395  panzer_stk::QuarterPlaneMatcher matcher(0,2,1,params);
396  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
397  }
398 
399  if(s_matcher=="(zx)y-quarter-coord") {
400  panzer_stk::QuarterPlaneMatcher matcher(2,0,1,params);
401  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
402  }
403 
404  if(s_matcher=="(yz)x-quarter-coord") {
405  panzer_stk::QuarterPlaneMatcher matcher(1,2,0,params);
406  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
407  }
408 
409  if(s_matcher=="(zy)x-quarter-coord") {
410  panzer_stk::QuarterPlaneMatcher matcher(2,1,0,params);
411  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
412  }
413 
414  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
415  "Failed parsing parameter list: could not find periodic boundary "
416  "condition matcher \"" << s_matcher << "\" "
417  "in string \"" << buildStr << "\"");
418 
419  return Teuchos::null;
420 }
421 
422 }
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)
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
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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)