PatchFunction1New.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "ConstantField.H"
29 
30 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
31 
32 template<class Type>
35 (
36  const polyPatch& pp,
37  const word& entryName,
38  const entry* eptr,
39  const dictionary& dict,
40  const bool faceValues,
41  const bool mandatory
42 )
43 {
44  word modelType;
45 
46  const dictionary* coeffs = (eptr ? eptr->dictPtr() : nullptr);
47 
48  if (coeffs)
49  {
50  // Dictionary entry
51 
53  << "For " << entryName << " with dictionary entries: "
54  << flatOutput(coeffs->toc()) << nl;
55 
56  coeffs->readEntry
57  (
58  "type",
59  modelType,
60  keyType::LITERAL
61  // "type" entry is mandatory, there is no 'redirect'
62  );
63  }
64  else if (eptr)
65  {
66  // Primitive entry
67  // - word : the modelType, or uniform/nonuniform
68  // - non-word : value for constant (uniform) function
69 
71  << "For " << entryName << " with primitive entry" << nl;
72 
73  ITstream& is = eptr->stream();
74 
75  if (is.peek().isWord())
76  {
77  modelType = is.peek().wordToken();
78  }
79  else
80  {
81  // A value - compatibility for reading uniform (constant) field
82 
83  const Type constValue = pTraits<Type>(is);
84 
85  return autoPtr<PatchFunction1<Type>>
86  (
87  new PatchFunction1Types::ConstantField<Type>
88  (
89  pp,
90  entryName,
91  constValue,
92  dict,
93  faceValues
94  )
95  );
96  }
97 
98  // Looks like a normal field entry?
99  if (modelType == "uniform" || modelType == "nonuniform")
100  {
101  return autoPtr<PatchFunction1<Type>>
102  (
103  new PatchFunction1Types::ConstantField<Type>
104  (
105  pp,
106  eptr,
107  entryName,
108  dict,
109  faceValues
110  )
111  );
112  }
113 
114  // Fallthrough
115  }
116 
117 
118  if (modelType.empty())
119  {
120  // Entry missing
121 
122  if (mandatory)
123  {
125  << "Missing or invalid PatchFunction1 entry: "
126  << entryName << nl
127  << exit(FatalIOError);
128  }
129 
130  return nullptr;
131  }
132  else if (!coeffs)
133  {
134  // Primitive entry. Coeffs dictionary is optional.
135  // Use keyword() - not entryName - for compatibility lookup!
136 
137  const word& kw =
138  (
139  eptr
140  ? eptr->keyword() // Could be a compatibility lookup
141  : entryName
142  );
143 
144  coeffs = &dict.optionalSubDict(kw + "Coeffs", keyType::LITERAL);
145  }
146 
147 
148  auto* ctorPtr = dictionaryConstructorTable(modelType);
149 
150  if (!ctorPtr)
151  {
153  << "Unknown PatchFunction1 type "
154  << modelType << " for " << entryName
155  << "\n\nValid PatchFunction1 types :\n"
156  << dictionaryConstructorTablePtr_->sortedToc() << nl
157  << exit(FatalIOError);
158  }
159 
160  return ctorPtr(pp, modelType, entryName, *coeffs, faceValues);
161 }
162 
163 
164 template<class Type>
167 (
168  const polyPatch& pp,
169  const word& entryName,
170  const dictionary& dict,
171  const bool faceValues,
172  const bool mandatory
173 )
174 {
176  (
177  pp,
178  entryName,
179  dict.findEntry(entryName, keyType::LITERAL),
180  dict,
181  faceValues,
182  mandatory
183  );
184 }
185 
186 
187 template<class Type>
190 (
191  const polyPatch& pp,
192  const word& entryName,
193  std::initializer_list<std::pair<const char*,int>> compat,
194  const dictionary& dict,
195  const bool faceValues,
196  const bool mandatory
197 )
198 {
200  (
201  pp,
202  entryName,
203  dict.findCompat(entryName, compat, keyType::LITERAL),
204  dict,
205  faceValues,
206  mandatory
207  );
208 }
209 
210 
211 template<class Type>
214 (
215  const polyPatch& pp,
216  const word& entryName,
217  const dictionary& dict,
218  const bool faceValues
219 )
220 {
221  // mandatory = false
222  return PatchFunction1<Type>::New(pp, entryName, dict, faceValues, false);
223 }
224 
225 
226 template<class Type>
229 (
231 
232  const polyPatch& pp,
233  const word& entryName,
234  const dictionary& dict,
235  enum keyType::option matchOpt,
236  const bool faceValues,
237  const bool mandatory
238 )
239 {
240  // See corresponding comments in Function1::New (caching version)
241 
242  refPtr<PatchFunction1<Type>> fref; // return value
243 
244  // Try for direct cache hit
245  fref.cref(cache.get(entryName));
246 
247  if (fref)
248  {
249  return fref;
250  }
251 
252 
253  // Lookup from dictionary
254  const entry* eptr = dict.findEntry(entryName, matchOpt);
255 
256  if (eptr)
257  {
258  // Use keyword (potentially a wildcard) instead of entry name
259  const auto& kw = eptr->keyword();
260 
261  // Try for a cache hit
262  fref.cref(cache.get(kw));
263 
264  if (!fref)
265  {
266  // Create new entry
267  auto fauto
268  (
270  (
271  pp,
272  kw,
273  eptr, // Already resolved
274  dict,
275  faceValues,
276  mandatory
277  )
278  );
279 
280  if (fauto)
281  {
282  // Cache the newly created function
283  fref.cref(fauto.get());
284  cache.set(kw, fauto);
285  }
286  }
287  }
288 
289  if (mandatory && !fref)
290  {
292  << "No match for " << entryName << nl
293  << exit(FatalIOError);
294  }
295 
296  return fref;
297 }
298 
299 
300 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
ConstantField.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::FatalIOError
IOerror FatalIOError
Foam::refPtr::cref
const T & cref() const
Definition: refPtrI.H:189
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:68
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:60
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
Foam::PatchFunction1::NewCompat
static autoPtr< PatchFunction1< Type > > NewCompat(const polyPatch &pp, const word &entryName, std::initializer_list< std::pair< const char *, int >> compat, const dictionary &dict, const bool faceValues=true, const bool mandatory=true)
Compatibility selector.
Definition: PatchFunction1New.C:190
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::keyType::option
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:78
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
Foam::PatchFunction1::NewIfPresent
static autoPtr< PatchFunction1< Type > > NewIfPresent(const polyPatch &pp, const word &entryName, const dictionary &dict, const bool faceValues=true)
An optional selector.
Definition: PatchFunction1New.C:214