boundaryTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "boundaryTemplates.H"
27 #include "Time.H"
28 #include "IFstream.H"
29 #include "OStringStream.H"
30 
31 using namespace Foam;
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
36 (
37  const fileName& baseDir,
38  const Time& runTime,
39  const word& solverType
40 )
41 :
42  templates_(dictionary::null),
43  options_(dictionary::null)
44 {
45  Info<< " Reading boundary templates" << endl;
46 
47  fileName BCDir(baseDir/"boundaryConditions");
48 
49  IOdictionary regionBCs
50  (
51  IOobject
52  (
53  fileName(BCDir/"boundaries"),
54  runTime,
56  )
57  );
58 
59  forAllConstIter(dictionary, regionBCs, iter)
60  {
61  const word& regionType = iter().keyword();
62  wordList patchTypes(regionBCs.lookup(regionType));
63 
64  dictionary regionTemplate = dictionary::null;
65  dictionary regionOptions = dictionary::null;
66 
67  // read general boundary types
68  forAll(patchTypes, i)
69  {
71  (
72  IOobject
73  (
74  fileName(BCDir/regionType/patchTypes[i]),
75  runTime,
77  )
78  );
79 
80  regionTemplate.add(patchTypes[i], dictionary(dict));
81  }
82 
83  // add solver type boundary types
84  forAll(patchTypes, i)
85  {
86  IOobject io
87  (
88  fileName(BCDir/regionType/solverType/patchTypes[i]),
89  runTime,
91  );
92 
93  if (io.headerOk())
94  {
95  IOdictionary dict(io);
96  regionTemplate.subDict(patchTypes[i]).merge(dict);
97  }
98  }
99 
100  // read general boundary options
101  forAll(patchTypes, i)
102  {
103  fileName optFile(BCDir/regionType/patchTypes[i] + "Options");
104 
105  IFstream is(optFile);
106 
107  if (is.good())
108  {
110  (
111  IOobject
112  (
113  optFile,
114  runTime,
116  )
117  );
118 
119  regionOptions.add(patchTypes[i], dictionary(dict));
120  }
121  }
122 
123  // add solver type boundary options
124  forAll(patchTypes, i)
125  {
126  // options are optional - however, if file exists, assume that it
127  // is to be read
128  fileName optFile
129  (
130  BCDir/regionType/solverType/patchTypes[i] + "Options"
131  );
132 
133  IFstream is(optFile);
134 
135  if (is.good())
136  {
138  (
139  IOobject
140  (
141  optFile,
142  runTime,
144  )
145  );
146 
147  regionOptions.subDict(patchTypes[i]).merge(dict);
148  }
149  }
150 
151  templates_.add(regionType, regionTemplate);
152  options_.add(regionType, regionOptions);
153  }
154 }
155 
156 
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
158 
160 {
161  return templates_;
162 }
163 
164 
166 (
167  const word& regionPrefix,
168  const word& fieldName,
169  const word& condition,
170  const word& category,
171  const word& patchType,
172  const dictionary& conditionOptions
173 ) const
174 {
175  const dictionary& regionTemplates = templates_.subDict(regionPrefix);
176 
177  // look for inlet, outlet, wall etc
178  if (regionTemplates.found(category))
179  {
180  const dictionary& categoryDict(regionTemplates.subDict(category));
181 
182  // look for subSonic, slip etc
183  if (categoryDict.found(patchType))
184  {
185  dictionary patchDict = categoryDict.subDict(patchType);
186 
187  // add any options
188  if (patchDict.found("OPTIONS"))
189  {
190  const dictionary& regionOptions =
191  options_.subDict(regionPrefix);
192 
193  if (!regionOptions.found(category))
194  {
196  << "No options available for category "
197  << category << exit(FatalError);
198  }
199 
200  const dictionary& dict = regionOptions.subDict(category);
201 
202  const wordList requiredOptions(patchDict.lookup("OPTIONS"));
203 
204  forAll(requiredOptions, i)
205  {
206  const word& option = requiredOptions[i];
207 
208  word selected;
209  if (!conditionOptions.readIfPresent(option, selected))
210  {
212  << "Condition " << condition << ": "
213  << "No option '" << option
214  << "' available for category '" << category
215  << "' and patch type '" << patchType
216  << "'. Valid options are: "
217  << conditionOptions.toc()
218  << exit(FatalError);
219  }
220 
221  if (!dict.found(option))
222  {
224  << "Condition " << condition << ": "
225  << "No option '" << option
226  << "' available for category '" << category
227  << "' and patch type '" << patchType
228  << "'. Valid options are " << dict.toc()
229  << exit(FatalError);
230  }
231 
232  const dictionary& optionDict = dict.subDict(option);
233 
234  if (!optionDict.found(selected))
235  {
237  << "Condition " << condition << ": "
238  << "No option '" << selected
239  << "' available for category '" << category
240  << "' and patch type '" << patchType
241  << "'. Valid options are " << optionDict.toc()
242  << exit(FatalError);
243  }
244 
245  const dictionary& dict = optionDict.subDict(selected);
246 
247  patchDict.merge(dict);
248  }
249  }
250 
251  // look for field name
252  if (patchDict.found(fieldName))
253  {
255  const dictionary& fieldDict(patchDict.subDict(fieldName));
256 
257  forAllConstIter(IDLList<entry>, fieldDict, iter)
258  {
259  OStringStream oss;
260  oss << iter();
261  string s(oss.str());
262  s.replace(iter().keyword(), "");
263  s.replace
264  (
265  "VALUE",
266  "boundaryConditions." + condition + ".values"
267  );
268  dict.add(iter().keyword(), s.c_str());
269  }
270 
271  return dict;
272  }
273  else
274  {
276  << "Condition " << condition << ": "
277  << "No '" << patchType
278  << "' condition found for field '" << fieldName
279  << "' in category type '" << category << "'"
280  << exit(FatalError);
281  }
282  }
283  else
284  {
286  << "Condition " << condition << ": "
287  << "No '" << patchType << "' boundary types defined in "
288  << categoryDict.dictName() << " templates. "
289  << "Available types are: " << categoryDict.toc()
290  << exit(FatalError);
291  }
292  }
293  else
294  {
296  << "Condition " << condition << ": "
297  << "Invalid boundary condition type '" << patchType
298  << "'. Valid types are:" << regionTemplates.toc()
299  << exit(FatalError);
300  }
301 
302  return dictionary::null;
303 }
304 
305 
307 (
308  const word& regionPrefix,
309  const word& condition,
310  const word& category,
311  const word& patchType
312 ) const
313 {
314  const dictionary& regionTemplates = templates_.subDict(regionPrefix);
315 
316  if (!regionTemplates.found(category))
317  {
319  << "Condition " << condition << ": "
320  << "Unknown category '" << category
321  << "'. Valid categories are: " << regionTemplates.toc()
322  << exit(FatalError);
323  }
324 
325  const dictionary& categoryDict = regionTemplates.subDict(category);
326 
327  if (!categoryDict.found(patchType))
328  {
330  << "Condition " << condition << ": "
331  << "Unknown type '" << patchType << "' in category '"
332  << category << "'. Valid types are: " << categoryDict.toc()
333  << exit(FatalError);
334  }
335 }
336 
337 
339 (
340  const word& regionPrefix,
341  const word& category,
342  const word& patchType
343 ) const
344 {
345  const dictionary& regionTemplates = templates_.subDict(regionPrefix);
346 
347  if (regionTemplates.found(category))
348  {
349  const dictionary& categoryDict(regionTemplates.subDict(category));
350 
351  if (categoryDict.found(patchType))
352  {
353  const dictionary& patchDict = categoryDict.subDict(patchType);
354 
355  if (patchDict.found("OPTIONS"))
356  {
357  return true;
358  }
359  }
360  else
361  {
363  << "No type '" << patchType << "' found in category '"
364  << category << "'. Valid types are "
365  << categoryDict.toc()
366  << exit(FatalError);
367  }
368  }
369  else
370  {
372  << "No category '" << category << "' found in templates. "
373  << "Valid categories are " << templates_.toc()
374  << exit(FatalError);
375  }
376 
377  return false;
378 }
379 
380 
381 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::dictionaryName::dictName
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::boundaryTemplates::checkPatch
void checkPatch(const word &regionPrefix, const word &condition, const word &category, const word &patchType) const
Check that user supplied patch info is valid.
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
patchTypes
wordList patchTypes(nPatches)
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::boundaryTemplates::boundaryTemplates
boundaryTemplates(const fileName &baseDir, const Time &runTime, const word &solverType)
Constructor.
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::IDLList< entry >
Foam::Info
messageStream Info
OStringStream.H
Foam::dictionary::merge
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1005
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
boundaryTemplates.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::boundaryTemplates::templates
const dictionary & templates() const
Return the dictionary of boundary templates.
Foam::boundaryTemplates::generatePatchDict
dictionary generatePatchDict(const word &regionPrefix, const word &fieldName, const word &condition, const word &category, const word &patchType, const dictionary &conditionOptions) const
Generate a dictionary representation of patch boundary condition.
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::boundaryTemplates::optionsRequired
bool optionsRequired(const word &regionPrefix, const word &category, const word &patchType) const
Return true if condition requires additional user options.
Foam::dictionary::null
static const dictionary null
Null dictionary.
Definition: dictionary.H:193
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729