Panzer  Version of the Day
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 
63 void PeriodicBC_Parser::setParameterList(const Teuchos::RCP<Teuchos::ParameterList> & pl)
64 {
65  if(not pl->isParameter(countStr_)) {
66  bool validEntry = false;
67  TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
68  !validEntry, Teuchos::Exceptions::InvalidParameterName,
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"
73  << getValidParameters()->currentParametersString() << "\n\n"
74  << "Passed parameter list: \n" << pl->currentParametersString()
75  );
76  }
77 
78  int numBCs = pl->get<int>(countStr_);
79  pl->validateParameters(*getValidParameters(numBCs));
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  matchers_.push_back(buildMatcher(cond));
88  }
89 
90  storedPL_ = pl;
91 }
92 
93 Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::getNonconstParameterList()
94 {
95  return storedPL_;
96 }
97 
98 Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::unsetParameterList()
99 {
100  Teuchos::RCP<Teuchos::ParameterList> pl = storedPL_;
101  storedPL_ = Teuchos::null;
102  return pl;
103 }
104 
105 Teuchos::RCP<const Teuchos::ParameterList> PeriodicBC_Parser::getValidParameters() const
106 {
107  static Teuchos::RCP<Teuchos::ParameterList> pl;
108 
109  // build a sample parameter list with a single preconditioner
110  if(pl==Teuchos::null) {
111  std::stringstream ss;
112  ss << condPrefix_ << 1 << std::endl;
113 
114  pl = Teuchos::rcp(new Teuchos::ParameterList);
115  pl->set<int>(countStr_,1,
116  "Number of set periodic boundary conditions");
117  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2",
118  "Boundary condition fairs formatted: <MatchCondition> <bndry1>;<bndry2>");
119  }
120 
121  return pl.getConst();
122 }
123 
124 Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::getValidParameters(int count) const
125 {
126  TEUCHOS_TEST_FOR_EXCEPTION(count<0,std::logic_error,
127  "PeriodicBC requires a positive number (or none) of periodic boundary conditions.");
128 
129  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::rcp(new Teuchos::ParameterList);
130  pl->set(countStr_,count);
131 
132  for(int k=1;k<=count;k++) {
133  std::stringstream ss;
134  ss << condPrefix_ << k;
135 
136  pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2");
137  }
138 
139  return pl;
140 }
141 
142 // basic string utilities to help wit parsing (only local)
144 
145 static std::string trim_left(const std::string & s)
146 {
147  std::string::size_type beg = s.find_first_not_of(' ');
148 
149  return s.substr(beg,s.length()-beg);
150 }
151 
152 static std::string trim_right(const std::string & s)
153 {
154  std::string::size_type end = s.find_last_not_of(' ');
155 
156  return s.substr(0,end+1);
157 }
158 
159 static std::string trim(const std::string & s)
160 {
161  return trim_right(trim_left(s));
162 }
163 
165 
166 void PeriodicBC_Parser::buildMatcher_Tokenize(const std::string & buildStr,
167  std::string & matcher,
168  std::string & bndry1,
169  std::string & bndry2) const
170 {
171  std::string::size_type endMatch = buildStr.find_first_of(' ');
172  std::string::size_type begBndry = buildStr.find_first_of(';');
173 
174  matcher = trim(buildStr.substr(0,endMatch));
175  bndry1 = trim(buildStr.substr(endMatch,begBndry-endMatch));
176  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
177 }
178 
179 bool PeriodicBC_Parser::buildMatcher_Tokenize_withParams(const std::string & buildStr,
180  std::string & matcher,
181  std::vector<std::string> & params,
182  std::string & bndry1,
183  std::string & bndry2) const
184 {
185  std::string::size_type endMatchAndParams = buildStr.find_first_of(':');
186  std::string::size_type begBndry = buildStr.find_first_of(';');
187 
188  // no parameters: default to old style input
189  if(endMatchAndParams==std::string::npos) {
190  buildMatcher_Tokenize(buildStr,matcher,bndry1,bndry2);
191  return false;
192  }
193 
194  bndry1 = trim(buildStr.substr(endMatchAndParams+1,begBndry-(endMatchAndParams+1)));
195  bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
196 
197  std::string matchAndParams = trim(buildStr.substr(0,endMatchAndParams));
198  std::string::size_type endMatch = matchAndParams.find_first_of(' ');
199 
200  // no parameters included
201  if(endMatch==std::string::npos) {
202  matcher = matchAndParams;
203  return true;
204  }
205 
206  // find parameters
208 
209  // check matching conditions
210  matcher = trim(matchAndParams.substr(0,endMatch));
211  matchAndParams = matchAndParams.substr(endMatch+1);
212 
213  std::string::size_type comma = matchAndParams.find_first_of(',');
214  while(comma!=std::string::npos) {
215  std::string p = trim(matchAndParams.substr(0,comma));
216 
217  TEUCHOS_TEST_FOR_EXCEPTION(p.length()<1,std::logic_error,
218  "Error parsing periodic boundary condition \"" + buildStr + "\"");
219 
220  params.push_back(p);
221  matchAndParams = matchAndParams.substr(comma+1);
222  comma = matchAndParams.find_first_of(',');
223  }
224 
225  std::string finalParam = trim(matchAndParams);
226  if(finalParam.length()>0)
227  params.push_back(finalParam);
228 
229  return true;
230 }
231 
232 Teuchos::RCP<const PeriodicBC_MatcherBase>
233 PeriodicBC_Parser::buildMatcher(const std::string & buildStr) const
234 {
235  std::vector<std::string> params;
236  std::string matcher, bndry1, bndry2;
237 
238  buildMatcher_Tokenize_withParams(buildStr,matcher,params,bndry1,bndry2);
239 
240  if(matcher=="x-coord") {
241  panzer_stk::CoordMatcher matcher(0,params);
242  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
243  }
244 
245  if(matcher=="y-coord") {
246  panzer_stk::CoordMatcher matcher(1,params);
247  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
248  }
249 
250  if(matcher=="z-coord") {
251  panzer_stk::CoordMatcher matcher(2,params);
252  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
253  }
254 
255  if(matcher=="x-edge") {
256  panzer_stk::CoordMatcher matcher(0,params);
257  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
258  }
259 
260  if(matcher=="y-edge") {
261  panzer_stk::CoordMatcher matcher(1,params);
262  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
263  }
264 
265  if(matcher=="z-edge") {
266  panzer_stk::CoordMatcher matcher(2,params);
267  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
268  }
269 
270  if(matcher=="xy-coord" || matcher=="yx-coord") {
271  panzer_stk::PlaneMatcher matcher(0,1,params);
272  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
273  }
274 
275  if(matcher=="xz-coord" || matcher=="zx-coord") {
276  panzer_stk::PlaneMatcher matcher(0,2,params);
277  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
278  }
279 
280  if(matcher=="yz-coord" || matcher=="zy-coord") {
281  panzer_stk::PlaneMatcher matcher(1,2,params);
282  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
283  }
284 
285  if(matcher=="xy-edge" || matcher=="yx-edge") {
286  panzer_stk::PlaneMatcher matcher(0,1,params);
287  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
288  }
289 
290  if(matcher=="xz-edge" || matcher=="zx-edge") {
291  panzer_stk::PlaneMatcher matcher(0,2,params);
292  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
293  }
294 
295  if(matcher=="yz-edge" || matcher=="zy-edge") {
296  panzer_stk::PlaneMatcher matcher(1,2,params);
297  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
298  }
299 
300  if(matcher=="xy-face" || matcher=="yx-face") {
301  panzer_stk::PlaneMatcher matcher(0,1,params);
302  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
303  }
304 
305  if(matcher=="xz-face" || matcher=="zx-face") {
306  panzer_stk::PlaneMatcher matcher(0,2,params);
307  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
308  }
309 
310  if(matcher=="yz-face" || matcher=="zy-face") {
311  panzer_stk::PlaneMatcher matcher(1,2,params);
312  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
313  }
314 
315  if(matcher=="(xy)z-quarter-coord") {
316  panzer_stk::QuarterPlaneMatcher matcher(0,1,2,params);
317  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
318  }
319 
320  if(matcher=="(yx)z-quarter-coord") {
321  panzer_stk::QuarterPlaneMatcher matcher(1,0,2,params);
322  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
323  }
324 
325  if(matcher=="(xz)y-quarter-coord") {
326  panzer_stk::QuarterPlaneMatcher matcher(0,2,1,params);
327  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
328  }
329 
330  if(matcher=="(zx)y-quarter-coord") {
331  panzer_stk::QuarterPlaneMatcher matcher(2,0,1,params);
332  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
333  }
334 
335  if(matcher=="(yz)x-quarter-coord") {
336  panzer_stk::QuarterPlaneMatcher matcher(1,2,0,params);
337  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
338  }
339 
340  if(matcher=="(zy)x-quarter-coord") {
341  panzer_stk::QuarterPlaneMatcher matcher(2,1,0,params);
342  return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
343  }
344 
345  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
346  "Failed parsing parameter list: could not find periodic boundary "
347  "condition matcher \"" << matcher << "\" "
348  "in string \"" << buildStr << "\"");
349 
350  return Teuchos::null;
351 }
352 
353 }
void buildMatcher_Tokenize(const std::string &buildStr, std::string &matcher, std::string &bndry1, std::string &bndry2) const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
Teuchos::RCP< Teuchos::ParameterList > storedPL_
stored parameter list
Teuchos::RCP< const PeriodicBC_MatcherBase > buildMatcher(const std::string &buildStr) const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
static std::string trim_right(const std::string &s)
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > matchers_
matchers constructed by "setParameterList"
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
bool buildMatcher_Tokenize_withParams(const std::string &buildStr, std::string &matcher, std::vector< std::string > &params, std::string &bndry1, std::string &bndry2) const
static std::string trim_left(const std::string &s)
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)
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
const std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > & getMatchers() const