genericFvPatchField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
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 "genericFvPatchField.H"
27 #include "fieldMapper.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 template<class Type>
36 (
37  const scalarList& components,
38  const word& keyword,
39  const label size,
40  HashPtrTable<Field<Type>>& typeFields
41 )
42 {
43  if (components.size() != Type::nComponents)
44  {
45  return false;
46  }
47 
48  Type t;
49  forAll(t, i)
50  {
51  t[i] = components[i];
52  }
53 
54  typeFields.insert(keyword, new Field<Type>(size, t));
55 
56  return true;
57 }
58 
59 
60 template<>
62 (
63  const scalarList& components,
64  const word& keyword,
65  const label size,
66  HashPtrTable<Field<scalar>>& typeFields
67 )
68 {
69  return false;
70 }
71 
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
76 
77 template<class Type>
79 (
80  const fvPatch& p,
82  const dictionary& dict
83 )
84 :
85  genericFieldBase(dict.lookup("type")),
86  calculatedFvPatchField<Type>(p, iF, dict, false),
87  dict_(dict),
88  valuePresent_(false)
89 {
90  if (dict_.found("value"))
91  {
92  valuePresent_ = true;
93 
95  (
96  Field<Type>("value", iF.dimensions(), dict_, p.size())
97  );
98  }
99  else
100  {
101  Info<< "\n Cannot find 'value' entry"
102  << " for patch " << this->patch().name()
103  << " of field " << this->internalField().name()
104  << " for generic patch field with actual type "
105  << actualTypeName() << nl
106  << " in file " << this->internalField().objectPath() << nl
107  << " Extrapolating the internal field to the patch."
108  << endl;
109 
111  }
112 
113  forAllConstIter(dictionary, dict_, iter)
114  {
115  if (iter().keyword() != "type" && iter().keyword() != "value")
116  {
117  if
118  (
119  iter().isStream()
120  && iter().stream().size()
121  )
122  {
123  ITstream& is = iter().stream();
124 
125  // Read first token
126  token firstToken(is);
127 
128  if
129  (
130  firstToken.isWord()
131  && firstToken.wordToken() == "nonuniform"
132  )
133  {
134  token fieldToken(is);
135 
136  if (!fieldToken.isCompound())
137  {
138  if
139  (
140  fieldToken.isLabel()
141  && fieldToken.labelToken() == 0
142  )
143  {
144  scalarFields_.insert
145  (
146  iter().keyword(),
147  new scalarField(0)
148  );
149  }
150  else
151  {
153  << "\n token following 'nonuniform' "
154  "is not a compound"
155  << "\n on patch " << this->patch().name()
156  << " of field "
157  << this->internalField().name()
158  << " in file "
159  << this->internalField().objectPath()
160  << exit(FatalIOError);
161  }
162  }
163 
164  #define ReadTypeField(Type, nullArg) \
165  else if \
166  ( \
167  fieldToken.compoundToken().type() \
168  == token::Compound<List<Type>>::typeName \
169  ) \
170  { \
171  Field<Type>* fPtr = new Field<Type>; \
172  fPtr->transfer \
173  ( \
174  dynamicCast<token::Compound<List<Type>>> \
175  ( \
176  fieldToken.transferCompoundToken(is) \
177  ) \
178  ); \
179  \
180  if (fPtr->size() != this->size()) \
181  { \
182  FatalIOErrorInFunction(dict) \
183  << "\n size of field " \
184  << iter().keyword() \
185  << " (" << fPtr->size() << ')' \
186  << " is not the same size as the patch (" \
187  << this->size() << ')' \
188  << "\n on patch " \
189  << this->patch().name() \
190  << " of field " \
191  << this->internalField().name() \
192  << " in file " \
193  << this->internalField().objectPath() \
194  << exit(FatalIOError); \
195  } \
196  \
197  Type##Fields_.insert(iter().keyword(), fPtr); \
198  }
199  FOR_ALL_FIELD_TYPES(ReadTypeField)
200  #undef ReadTypeField
201 
202  else
203  {
205  << "\n compound " << fieldToken.compoundToken()
206  << " not supported"
207  << "\n on patch " << this->patch().name()
208  << " of field "
209  << this->internalField().name()
210  << " in file "
211  << this->internalField().objectPath()
212  << exit(FatalIOError);
213  }
214  }
215  else if
216  (
217  firstToken.isWord()
218  && firstToken.wordToken() == "uniform"
219  )
220  {
221  token fieldToken(is);
222 
223  if (!fieldToken.isPunctuation())
224  {
225  scalarFields_.insert
226  (
227  iter().keyword(),
228  new scalarField
229  (
230  this->size(),
231  fieldToken.number()
232  )
233  );
234  }
235  else
236  {
237  // Read as a scalarList
238  is.putBack(fieldToken);
239  scalarList l(is);
240 
241  #define InsertUniformTypeField(Type, nullArg) \
242  || insertUniformTypeField \
243  ( \
244  l, \
245  iter().keyword(), \
246  this->size(), \
247  Type##Fields_ \
248  )
249  if (!(0 FOR_ALL_FIELD_TYPES(InsertUniformTypeField)))
250  {
252  << "\n unrecognised native type " << l
253  << "\n on patch " << this->patch().name()
254  << " of field "
255  << this->internalField().name()
256  << " in file "
257  << this->internalField().objectPath()
258  << exit(FatalIOError);
259  }
260  #undef InsertUniformTypeField
261  }
262  }
263  }
264  }
265  }
266 }
267 
268 
269 template<class Type>
271 (
272  const genericFvPatchField<Type>& ptf,
273  const fvPatch& p,
275  const fieldMapper& mapper
276 )
277 :
278  genericFieldBase(ptf),
279  calculatedFvPatchField<Type>(ptf, p, iF, mapper),
280  dict_(ptf.dict_),
281  valuePresent_(ptf.valuePresent_)
282 {
283  #define MapTypeFields(Type, nullArg) \
284  forAllConstIter(HashPtrTable<Field<Type>>, ptf.Type##Fields_, iter) \
285  { \
286  Type##Fields_.insert \
287  ( \
288  iter.key(), \
289  mapper(*iter()).ptr() \
290  ); \
291  }
292  FOR_ALL_FIELD_TYPES(MapTypeFields);
293  #undef MapTypeFields
294 }
295 
296 
297 template<class Type>
299 (
300  const genericFvPatchField<Type>& ptf,
302 )
303 :
304  genericFieldBase(ptf),
305  calculatedFvPatchField<Type>(ptf, iF),
306  dict_(ptf.dict_),
307  valuePresent_(ptf.valuePresent_)
308  #define CopyTypeFields(Type, nullArg) \
309  , Type##Fields_(ptf.Type##Fields_)
311  #undef CopyTypeFields
312 {}
313 
314 
315 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
316 
317 template<class Type>
319 (
320  const fvPatchField<Type>& ptf,
321  const fieldMapper& mapper
322 )
323 {
325 
326  const genericFvPatchField<Type>& dptf =
327  refCast<const genericFvPatchField<Type>>(ptf);
328 
329  #define MapTypeFields(Type, nullArg) \
330  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
331  { \
332  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
333  dptf.Type##Fields_.find(iter.key()); \
334  \
335  if (dptfIter != dptf.Type##Fields_.end()) \
336  { \
337  mapper(*iter(), *dptfIter()); \
338  } \
339  }
341  #undef MapTypeFields
342 }
343 
344 
345 template<class Type>
347 (
348  const fvPatchField<Type>& ptf
349 )
350 {
352 
353  const genericFvPatchField<Type>& dptf =
354  refCast<const genericFvPatchField<Type>>(ptf);
355 
356  #define ResetTypeFields(Type, nullArg) \
357  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
358  { \
359  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
360  dptf.Type##Fields_.find(iter.key()); \
361  \
362  if (dptfIter != dptf.Type##Fields_.end()) \
363  { \
364  iter()->reset(*dptfIter()); \
365  } \
366  }
368  #undef ResetTypeFields
369 }
370 
371 
372 template<class Type>
375 (
376  const tmp<scalarField>&
377 ) const
378 {
380  << "cannot be called for a genericFvPatchField"
381  " (actual type " << actualTypeName() << ")"
382  << "\n on patch " << this->patch().name()
383  << " of field " << this->internalField().name()
384  << " in file " << this->internalField().objectPath()
385  << "\n You are probably trying to solve for a field with a "
386  "generic boundary condition."
387  << abort(FatalError);
388 
389  return *this;
390 }
391 
392 
393 template<class Type>
396 (
397  const tmp<scalarField>&
398 ) const
399 {
401  << "cannot be called for a genericFvPatchField"
402  " (actual type " << actualTypeName() << ")"
403  << "\n on patch " << this->patch().name()
404  << " of field " << this->internalField().name()
405  << " in file " << this->internalField().objectPath()
406  << "\n You are probably trying to solve for a field with a "
407  "generic boundary condition."
408  << abort(FatalError);
409 
410  return *this;
411 }
412 
413 
414 template<class Type>
417 {
419  << "cannot be called for a genericFvPatchField"
420  " (actual type " << actualTypeName() << ")"
421  << "\n on patch " << this->patch().name()
422  << " of field " << this->internalField().name()
423  << " in file " << this->internalField().objectPath()
424  << "\n You are probably trying to solve for a field with a "
425  "generic boundary condition."
426  << abort(FatalError);
427 
428  return *this;
429 }
430 
431 template<class Type>
434 {
436  << "cannot be called for a genericFvPatchField"
437  " (actual type " << actualTypeName() << ")"
438  << "\n on patch " << this->patch().name()
439  << " of field " << this->internalField().name()
440  << " in file " << this->internalField().objectPath()
441  << "\n You are probably trying to solve for a field with a "
442  "generic boundary condition."
443  << abort(FatalError);
444 
445  return *this;
446 }
447 
448 
449 template<class Type>
451 {
452  writeEntry(os, "type", actualTypeName());
453 
454  forAllConstIter(dictionary, dict_, iter)
455  {
456  if (iter().keyword() != "type")
457  {
458  if
459  (
460  iter().isStream()
461  && iter().stream().size()
462  && iter().stream()[0].isWord()
463  && iter().stream()[0].wordToken() == "nonuniform"
464  )
465  {
466  #define WriteTypeFieldEntry(Type, nullArg) \
467  else if (Type##Fields_.found(iter().keyword())) \
468  { \
469  writeEntry \
470  ( \
471  os, \
472  iter().keyword(), \
473  *Type##Fields_.find(iter().keyword())() \
474  ); \
475  }
477  #undef WriteTypeFieldEntry
478  }
479  else
480  {
481  iter().write(os);
482  }
483  }
484  }
485 
486 
487  forAllConstIter(dictionary, dict_, iter)
488  {
489  if (iter().keyword() != "type" && iter().keyword() != "value")
490  {
491  if
492  (
493  iter().isStream()
494  && iter().stream().size()
495  && iter().stream()[0].isWord()
496  && iter().stream()[0].wordToken() == "nonuniform"
497  )
498  {
499  #define WriteTypeFieldEntry(Type, nullArg) \
500  else if (Type##Fields_.found(iter().keyword())) \
501  { \
502  writeEntry \
503  ( \
504  os, \
505  iter().keyword(), \
506  *Type##Fields_.find(iter().keyword())() \
507  ); \
508  }
510  #undef WriteTypeFieldEntry
511  }
512  else
513  {
514  iter().write(os);
515  }
516  }
517  }
518 
519  if (valuePresent_)
520  {
521  writeEntry(os, "value", *this);
522  }
523 }
524 
525 
526 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
const dimensionSet & dimensions() const
Return dimensions.
Pre-declare SubField and related Field type.
Definition: Field.H:83
A HashTable specialisation for hashing pointers.
Definition: HashPtrTable.H:68
Input token stream.
Definition: ITstream.H:56
void putBack(const token &)
Put back token.
Definition: Istream.C:30
label size() const
Return the number of elements in the UList.
Definition: ListI.H:171
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
This boundary condition is not designed to be evaluated; it is assumed that the value is assigned via...
virtual void map(const fvPatchField< Type > &, const fieldMapper &)
Map the given fvPatchField onto this fvPatchField.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
Abstract base class for field mapping.
Definition: fieldMapper.H:48
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: fvPatchField.H:90
virtual tmp< Field< Type > > patchInternalField() const
Return internal field next to patch as patch field.
Definition: fvPatchField.C:176
virtual void operator=(const UList< Type > &)
Definition: fvPatchField.C:255
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
Definition: fvPatchField.C:201
const DimensionedField< Type, fvMesh > & internalField() const
Return dimensioned internal field reference.
Definition: fvPatchField.H:376
const fvPatch & patch() const
Return patch.
Definition: fvPatchField.H:370
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:58
virtual const word & name() const
Return name.
Definition: fvPatch.H:135
Base class for generic field types. Facilitates down-casting so that the actual type can be queried.
const word & actualTypeName() const
Return the actual type name.
This boundary condition provides a generic version of the calculated condition, useful as a fallback ...
virtual void write(Ostream &) const
Write.
tmp< Field< Type > > gradientInternalCoeffs() const
Return the matrix diagonal coefficients corresponding to the.
virtual tmp< Field< Type > > valueInternalCoeffs(const tmp< scalarField > &) const
Return the matrix diagonal coefficients corresponding to the.
tmp< Field< Type > > gradientBoundaryCoeffs() const
Return the matrix source coefficients corresponding to the.
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
virtual tmp< Field< Type > > valueBoundaryCoeffs(const tmp< scalarField > &) const
Return the matrix source coefficients corresponding to the.
virtual void map(const fvPatchField< Type > &, const fieldMapper &)
Map the given fvPatchField onto this fvPatchField.
genericFvPatchField(const fvPatch &, const DimensionedField< Type, fvMesh > &, const dictionary &)
Construct from patch, internal field and dictionary.
A class for managing temporary objects.
Definition: tmp.H:55
A token holds items read from Istream.
Definition: token.H:74
bool isLabel() const
Definition: tokenI.H:615
bool isPunctuation() const
Definition: tokenI.H:324
label labelToken() const
Definition: tokenI.H:634
bool isCompound() const
Definition: tokenI.H:828
const compound & compoundToken() const
Definition: tokenI.H:833
bool isWord() const
Definition: tokenI.H:342
const word & wordToken() const
Definition: tokenI.H:347
scalar number() const
Definition: tokenI.H:799
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WriteTypeFieldEntry(Type, nullArg)
#define ResetTypeFields(Type, nullArg)
#define CopyTypeFields(Type, nullArg)
#define MapTypeFields(Type, nullArg)
#define ReadTypeField(Type, nullArg)
#define InsertUniformTypeField(Type, nullArg)
const unitSet & lookup(const word &unitName)
Lookup and return the named unit from the table.
Definition: units.C:346
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
errorManip< error > abort(error &err)
Definition: errorManip.H:131
FOR_ALL_FIELD_TYPES(makeDimensionedPointFieldFunctions)
messageStream Info
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
bool insertUniformTypeField(const scalarList &components, const word &keyword, const label size, HashPtrTable< Field< Type >> &typeFields)
IOerror FatalIOError
error FatalError
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
static const char nl
Definition: Ostream.H:297
dictionary dict
volScalarField & p