Panzer Version of the Day
Loading...
Searching...
No Matches
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
49namespace panzer_stk {
50
52 : countStr_("Count")
53 , condPrefix_("Periodic Condition ")
54 , searchStr_("Use BBox Search")
55 , useBBoxSearch_(false) // TODO swap this to change default search (see also STK_interface.hpp)
56{
57}
58
59const std::vector<Teuchos::RCP<const PeriodicBC_MatcherBase> > &
61{
62 return matchers_;
63}
64
66{
67 return useBBoxSearch_;
68}
69
70void PeriodicBC_Parser::setParameterList(const Teuchos::RCP<Teuchos::ParameterList> & pl)
71{
72 if(!pl->isParameter(countStr_)) {
73 bool validEntry = false;
74 TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(
75 !validEntry, Teuchos::Exceptions::InvalidParameterName,
76 "Error, the parameter {name=\"" << countStr_ << "\","
77 "type=\"int\""
78 "\nis required in parameter (sub)list \""<< pl->name() <<"\"."
79 "\n\nThe valid parameters and types are:\n"
80 << getValidParameters()->currentParametersString() << "\n\n"
81 << "Passed parameter list: \n" << pl->currentParametersString()
82 );
83 }
84
85 int numBCs = pl->get<int>(countStr_);
87 pl->validateParameters(*getValidParameters(numBCs));
88
89 // loop over boundary conditions
90 for(int i=1;i<=numBCs;i++) {
91 std::stringstream ss;
92
93 ss << condPrefix_ << i;
94 std::string cond = pl->get<std::string>(ss.str());
95
96 std::pair<std::string, unsigned int> matcherPair = getMatcherTypeAndDim(cond);
97 std::string matcherType = matcherPair.first;
98 unsigned int matcherDim = matcherPair.second;
99 if(matcherType == "coord"){
100 matchers_.push_back(buildMatcher(cond));
101 }else if(matcherType == "edge")
102 edgeMatchers_.push_back(buildMatcher(cond));
103 else if(matcherType == "face")
104 faceMatchers_.push_back(buildMatcher(cond));
105 else if(matcherType == "all"){
106 matchers_.push_back(buildMatcher(replaceMatcherType(cond,"coord")));
107 edgeMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"edge")));
108 if(matcherDim > 2)
109 faceMatchers_.push_back(buildMatcher(replaceMatcherType(cond,"face")));
110 }
111 }
112
113 // Order BCs with all coords first, followed by edges, then faces
114 matchers_.insert(matchers_.end(),edgeMatchers_.begin(),edgeMatchers_.end());
115 matchers_.insert(matchers_.end(),faceMatchers_.begin(),faceMatchers_.end());
116
117 storedPL_ = pl;
118}
119
120Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::getNonconstParameterList()
121{
122 return storedPL_;
123}
124
125Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::unsetParameterList()
126{
127 Teuchos::RCP<Teuchos::ParameterList> pl = storedPL_;
128 storedPL_ = Teuchos::null;
129 return pl;
130}
131
132Teuchos::RCP<const Teuchos::ParameterList> PeriodicBC_Parser::getValidParameters() const
133{
134 static Teuchos::RCP<Teuchos::ParameterList> pl;
135
136 // build a sample parameter list with a single preconditioner
137 if(pl==Teuchos::null) {
138 std::stringstream ss;
139 ss << condPrefix_ << 1 << std::endl;
140
141 pl = Teuchos::rcp(new Teuchos::ParameterList);
142 pl->set<int>(countStr_,1,
143 "Number of set periodic boundary conditions");
144 pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2",
145 "Boundary condition pairs formatted: <MatchCondition> <bndry1>;<bndry2>");
146 // TODO swap this to change default search (see also STK_interface.hpp)
147 pl->set<bool>(searchStr_,false,"Use bounding box search for GID match (requires STKSearch) or not");
148 }
149
150 return pl.getConst();
151}
152
153Teuchos::RCP<Teuchos::ParameterList> PeriodicBC_Parser::getValidParameters(int count) const
154{
155 TEUCHOS_TEST_FOR_EXCEPTION(count<0,std::logic_error,
156 "PeriodicBC requires a positive number (or none) of periodic boundary conditions.");
157
158 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::rcp(new Teuchos::ParameterList);
159 pl->set(countStr_,count);
160 // TODO swap this to change default search (see also STK_interface.hpp)
161 pl->set<bool>(searchStr_,false,"Use bounding box search for GID match (requires STKSearch) or not");
162
163 for(int k=1;k<=count;k++) {
164 std::stringstream ss;
165 ss << condPrefix_ << k;
166
167 pl->set<std::string>(ss.str(),"MatchCondition bndry1;bndry2");
168 }
169
170 return pl;
171}
172
173// basic string utilities to help wit parsing (only local)
175
176static std::string trim_left(const std::string & s)
177{
178 std::string::size_type beg = s.find_first_not_of(' ');
179
180 return s.substr(beg,s.length()-beg);
181}
182
183static std::string trim_right(const std::string & s)
184{
185 std::string::size_type end = s.find_last_not_of(' ');
186
187 return s.substr(0,end+1);
188}
189
190static std::string trim(const std::string & s)
191{
192 return trim_right(trim_left(s));
193}
194
196
197std::pair<std::string, unsigned int> PeriodicBC_Parser::getMatcherTypeAndDim(const std::string & buildStr) const
198{
199 std::string::size_type endMatch = buildStr.find_first_of(' ');
200
201 std::string matcher = trim(buildStr.substr(0,endMatch));
202
203 std::string::size_type hyphenMatch = matcher.find_last_of('-');
204
205 TEUCHOS_TEST_FOR_EXCEPTION(hyphenMatch==std::string::npos,std::logic_error,
206 "Failed parsing parameter list: could not find periodic boundary "
207 "condition matcher \"" << matcher << "\" "
208 "in string \"" << buildStr << "\n"
209 "Matcher " << matcher << " requires a hyphen, e.g. x-coord, yz-edge\"");
210
211 std::string matcherType = trim(matcher.substr(hyphenMatch+1,matcher.length()));
212
213 TEUCHOS_TEST_FOR_EXCEPTION((matcherType != "coord") && (matcherType != "edge") && (matcherType != "face") && (matcherType != "all"),std::logic_error,
214 "Failed parsing parameter list: could not find periodic boundary "
215 "condition matcher \"" << matcher << "\" "
216 "in string \"" << buildStr << "\n"
217 "Type " << matcherType << " is not a valid boundary condition type. Must be coord, edge, face, or all\"");
218
219 std::string matcherCoord = trim(matcher.substr(0,hyphenMatch));
220 unsigned int matcherDim = 3;
221 if((matcherCoord == "x") || (matcherCoord == "y") || (matcherCoord == "z"))
222 matcherDim = 2;
223
224 return std::make_pair(matcherType,matcherDim);
225}
226
227std::string PeriodicBC_Parser::replaceMatcherType(const std::string & buildStr, const std::string & matcherType) const
228{
229 std::string::size_type allPosition = buildStr.find("all");
230
231 std::string beforeType = trim(buildStr.substr(0,allPosition));
232 std::string afterType = trim(buildStr.substr(allPosition+3,buildStr.length()));
233
234 return beforeType + matcherType + " " + afterType;
235}
236
237void PeriodicBC_Parser::buildMatcher_Tokenize(const std::string & buildStr,
238 std::string & matcher,
239 std::string & bndry1,
240 std::string & bndry2) const
241{
242 std::string::size_type endMatch = buildStr.find_first_of(' ');
243 std::string::size_type begBndry = buildStr.find_first_of(';');
244
245 matcher = trim(buildStr.substr(0,endMatch));
246 bndry1 = trim(buildStr.substr(endMatch,begBndry-endMatch));
247 bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
248}
249
251 std::string & matcher,
252 std::vector<std::string> & params,
253 std::string & bndry1,
254 std::string & bndry2) const
255{
256 std::string::size_type endMatchAndParams = buildStr.find_first_of(':');
257 std::string::size_type begBndry = buildStr.find_first_of(';');
258
259 // no parameters: default to old style input
260 if(endMatchAndParams==std::string::npos) {
261 buildMatcher_Tokenize(buildStr,matcher,bndry1,bndry2);
262 return false;
263 }
264
265 bndry1 = trim(buildStr.substr(endMatchAndParams+1,begBndry-(endMatchAndParams+1)));
266 bndry2 = trim(buildStr.substr(begBndry+1,buildStr.length()));
267
268 std::string matchAndParams = trim(buildStr.substr(0,endMatchAndParams));
269 std::string::size_type endMatch = matchAndParams.find_first_of(' ');
270
271 // no parameters included
272 if(endMatch==std::string::npos) {
273 matcher = matchAndParams;
274 return true;
275 }
276
277 // find parameters
279
280 // check matching conditions
281 matcher = trim(matchAndParams.substr(0,endMatch));
282 matchAndParams = matchAndParams.substr(endMatch+1);
283
284 std::string::size_type comma = matchAndParams.find_first_of(',');
285 while(comma!=std::string::npos) {
286 std::string p = trim(matchAndParams.substr(0,comma));
287
288 TEUCHOS_TEST_FOR_EXCEPTION(p.length()<1,std::logic_error,
289 "Error parsing periodic boundary condition \"" + buildStr + "\"");
290
291 params.push_back(p);
292 matchAndParams = matchAndParams.substr(comma+1);
293 comma = matchAndParams.find_first_of(',');
294 }
295
296 std::string finalParam = trim(matchAndParams);
297 if(finalParam.length()>0)
298 params.push_back(finalParam);
299
300 return true;
301}
302
303Teuchos::RCP<const PeriodicBC_MatcherBase>
304PeriodicBC_Parser::buildMatcher(const std::string & buildStr) const
305{
306 std::vector<std::string> params;
307 std::string s_matcher, bndry1, bndry2;
308
309 buildMatcher_Tokenize_withParams(buildStr,s_matcher,params,bndry1,bndry2);
310
311 if(s_matcher=="x-coord") {
312 panzer_stk::CoordMatcher matcher(0,params);
313 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
314 }
315
316 if(s_matcher=="y-coord") {
317 panzer_stk::CoordMatcher matcher(1,params);
318 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
319 }
320
321 if(s_matcher=="z-coord") {
322 panzer_stk::CoordMatcher matcher(2,params);
323 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
324 }
325
326 if(s_matcher=="x-edge") {
327 panzer_stk::CoordMatcher matcher(0,params);
328 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
329 }
330
331 if(s_matcher=="y-edge") {
332 panzer_stk::CoordMatcher matcher(1,params);
333 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
334 }
335
336 if(s_matcher=="z-edge") {
337 panzer_stk::CoordMatcher matcher(2,params);
338 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
339 }
340
341 if(s_matcher=="xy-coord" || s_matcher=="yx-coord") {
342 panzer_stk::PlaneMatcher matcher(0,1,params);
343 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
344 }
345
346 if(s_matcher=="xz-coord" || s_matcher=="zx-coord") {
347 panzer_stk::PlaneMatcher matcher(0,2,params);
348 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
349 }
350
351 if(s_matcher=="yz-coord" || s_matcher=="zy-coord") {
352 panzer_stk::PlaneMatcher matcher(1,2,params);
353 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
354 }
355
356 if(s_matcher=="xy-edge" || s_matcher=="yx-edge") {
357 panzer_stk::PlaneMatcher matcher(0,1,params);
358 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
359 }
360
361 if(s_matcher=="xz-edge" || s_matcher=="zx-edge") {
362 panzer_stk::PlaneMatcher matcher(0,2,params);
363 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
364 }
365
366 if(s_matcher=="yz-edge" || s_matcher=="zy-edge") {
367 panzer_stk::PlaneMatcher matcher(1,2,params);
368 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
369 }
370
371 if(s_matcher=="xy-face" || s_matcher=="yx-face") {
372 panzer_stk::PlaneMatcher matcher(0,1,params);
373 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
374 }
375
376 if(s_matcher=="xz-face" || s_matcher=="zx-face") {
377 panzer_stk::PlaneMatcher matcher(0,2,params);
378 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
379 }
380
381 if(s_matcher=="yz-face" || s_matcher=="zy-face") {
382 panzer_stk::PlaneMatcher matcher(1,2,params);
383 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
384 }
385
386 if(s_matcher=="wx-coord") {
388 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
389 }
390
391 if(s_matcher=="wy-coord") {
393 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
394 }
395
396 if(s_matcher=="wx-edge") {
398 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
399 }
400
401 if(s_matcher=="wy-edge") {
403 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"edge");
404 }
405
406 if(s_matcher=="wx-face") {
408 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
409 }
410
411 if(s_matcher=="wy-face") {
413 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher,"face");
414 }
415
416 if(s_matcher=="(xy)z-quarter-coord") {
417 panzer_stk::QuarterPlaneMatcher matcher(0,1,2,params);
418 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
419 }
420
421 if(s_matcher=="(yx)z-quarter-coord") {
422 panzer_stk::QuarterPlaneMatcher matcher(1,0,2,params);
423 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
424 }
425
426 if(s_matcher=="(xz)y-quarter-coord") {
427 panzer_stk::QuarterPlaneMatcher matcher(0,2,1,params);
428 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
429 }
430
431 if(s_matcher=="(zx)y-quarter-coord") {
432 panzer_stk::QuarterPlaneMatcher matcher(2,0,1,params);
433 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
434 }
435
436 if(s_matcher=="(yz)x-quarter-coord") {
437 panzer_stk::QuarterPlaneMatcher matcher(1,2,0,params);
438 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
439 }
440
441 if(s_matcher=="(zy)x-quarter-coord") {
442 panzer_stk::QuarterPlaneMatcher matcher(2,1,0,params);
443 return panzer_stk::buildPeriodicBC_Matcher(bndry1,bndry2,matcher);
444 }
445
446 TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
447 "Failed parsing parameter list: could not find periodic boundary "
448 "condition matcher \"" << s_matcher << "\" "
449 "in string \"" << buildStr << "\"");
450
451 return Teuchos::null;
452}
453
454}
const std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > & getMatchers() const
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > edgeMatchers_
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > matchers_
matchers constructed by "setParameterList"
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
std::pair< std::string, unsigned int > getMatcherTypeAndDim(const std::string &buildStr) const
Teuchos::RCP< const PeriodicBC_MatcherBase > buildMatcher(const std::string &buildStr) const
std::vector< Teuchos::RCP< const PeriodicBC_MatcherBase > > faceMatchers_
std::string replaceMatcherType(const std::string &buildStr, const std::string &matcherType) const
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
bool buildMatcher_Tokenize_withParams(const std::string &buildStr, std::string &matcher, std::vector< std::string > &params, std::string &bndry1, std::string &bndry2) const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Teuchos::RCP< Teuchos::ParameterList > storedPL_
stored parameter list
void buildMatcher_Tokenize(const std::string &buildStr, std::string &matcher, std::string &bndry1, std::string &bndry2) const
static std::string trim_left(const std::string &s)
static std::string trim(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_right(const std::string &s)