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-2024 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),
87  dict_(dict)
88 {
89  if (!dict.found("value"))
90  {
92  << "\n Cannot find 'value' entry"
93  << " on patch " << this->patch().name()
94  << " of field " << this->internalField().name()
95  << " in file " << this->internalField().objectPath()
96  << nl
97  << " which is required to set the"
98  " values of the generic patch field." << nl
99  << " (Actual type " << actualTypeName() << ")" << nl
100  << "\n Please add the 'value' entry to the write function "
101  "of the user-defined boundary-condition\n"
102  << exit(FatalIOError);
103  }
104 
105  forAllConstIter(dictionary, dict_, iter)
106  {
107  if (iter().keyword() != "type" && iter().keyword() != "value")
108  {
109  if
110  (
111  iter().isStream()
112  && iter().stream().size()
113  )
114  {
115  ITstream& is = iter().stream();
116 
117  // Read first token
118  token firstToken(is);
119 
120  if
121  (
122  firstToken.isWord()
123  && firstToken.wordToken() == "nonuniform"
124  )
125  {
126  token fieldToken(is);
127 
128  if (!fieldToken.isCompound())
129  {
130  if
131  (
132  fieldToken.isLabel()
133  && fieldToken.labelToken() == 0
134  )
135  {
136  scalarFields_.insert
137  (
138  iter().keyword(),
139  new scalarField(0)
140  );
141  }
142  else
143  {
145  << "\n token following 'nonuniform' "
146  "is not a compound"
147  << "\n on patch " << this->patch().name()
148  << " of field "
149  << this->internalField().name()
150  << " in file "
151  << this->internalField().objectPath()
152  << exit(FatalIOError);
153  }
154  }
155 
156  #define ReadTypeField(Type, nullArg) \
157  else if \
158  ( \
159  fieldToken.compoundToken().type() \
160  == token::Compound<List<Type>>::typeName \
161  ) \
162  { \
163  Field<Type>* fPtr = new Field<Type>; \
164  fPtr->transfer \
165  ( \
166  dynamicCast<token::Compound<List<Type>>> \
167  ( \
168  fieldToken.transferCompoundToken(is) \
169  ) \
170  ); \
171  \
172  if (fPtr->size() != this->size()) \
173  { \
174  FatalIOErrorInFunction(dict) \
175  << "\n size of field " \
176  << iter().keyword() \
177  << " (" << fPtr->size() << ')' \
178  << " is not the same size as the patch (" \
179  << this->size() << ')' \
180  << "\n on patch " \
181  << this->patch().name() \
182  << " of field " \
183  << this->internalField().name() \
184  << " in file " \
185  << this->internalField().objectPath() \
186  << exit(FatalIOError); \
187  } \
188  \
189  Type##Fields_.insert(iter().keyword(), fPtr); \
190  }
191  FOR_ALL_FIELD_TYPES(ReadTypeField)
192  #undef ReadTypeField
193 
194  else
195  {
197  << "\n compound " << fieldToken.compoundToken()
198  << " not supported"
199  << "\n on patch " << this->patch().name()
200  << " of field "
201  << this->internalField().name()
202  << " in file "
203  << this->internalField().objectPath()
204  << exit(FatalIOError);
205  }
206  }
207  else if
208  (
209  firstToken.isWord()
210  && firstToken.wordToken() == "uniform"
211  )
212  {
213  token fieldToken(is);
214 
215  if (!fieldToken.isPunctuation())
216  {
217  scalarFields_.insert
218  (
219  iter().keyword(),
220  new scalarField
221  (
222  this->size(),
223  fieldToken.number()
224  )
225  );
226  }
227  else
228  {
229  // Read as a scalarList
230  is.putBack(fieldToken);
231  scalarList l(is);
232 
233  #define InsertUniformTypeField(Type, nullArg) \
234  || insertUniformTypeField \
235  ( \
236  l, \
237  iter().keyword(), \
238  this->size(), \
239  Type##Fields_ \
240  )
241  if (!(0 FOR_ALL_FIELD_TYPES(InsertUniformTypeField)))
242  {
244  << "\n unrecognised native type " << l
245  << "\n on patch " << this->patch().name()
246  << " of field "
247  << this->internalField().name()
248  << " in file "
249  << this->internalField().objectPath()
250  << exit(FatalIOError);
251  }
252  #undef InsertUniformTypeField
253  }
254  }
255  }
256  }
257  }
258 }
259 
260 
261 template<class Type>
263 (
264  const genericFvPatchField<Type>& ptf,
265  const fvPatch& p,
267  const fieldMapper& mapper
268 )
269 :
270  genericFieldBase(ptf),
271  calculatedFvPatchField<Type>(ptf, p, iF, mapper),
272  dict_(ptf.dict_)
273 {
274  #define MapTypeFields(Type, nullArg) \
275  forAllConstIter(HashPtrTable<Field<Type>>, ptf.Type##Fields_, iter) \
276  { \
277  Type##Fields_.insert \
278  ( \
279  iter.key(), \
280  mapper(*iter()).ptr() \
281  ); \
282  }
283  FOR_ALL_FIELD_TYPES(MapTypeFields);
284  #undef MapTypeFields
285 }
286 
287 
288 template<class Type>
290 (
291  const genericFvPatchField<Type>& ptf,
293 )
294 :
295  genericFieldBase(ptf),
296  calculatedFvPatchField<Type>(ptf, iF),
297  dict_(ptf.dict_)
298  #define CopyTypeFields(Type, nullArg) \
299  , Type##Fields_(ptf.Type##Fields_)
301  #undef CopyTypeFields
302 {}
303 
304 
305 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
306 
307 template<class Type>
309 (
310  const fvPatchField<Type>& ptf,
311  const fieldMapper& mapper
312 )
313 {
315 
316  const genericFvPatchField<Type>& dptf =
317  refCast<const genericFvPatchField<Type>>(ptf);
318 
319  #define MapTypeFields(Type, nullArg) \
320  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
321  { \
322  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
323  dptf.Type##Fields_.find(iter.key()); \
324  \
325  if (dptfIter != dptf.Type##Fields_.end()) \
326  { \
327  mapper(*iter(), *dptfIter()); \
328  } \
329  }
331  #undef MapTypeFields
332 }
333 
334 
335 template<class Type>
337 (
338  const fvPatchField<Type>& ptf
339 )
340 {
342 
343  const genericFvPatchField<Type>& dptf =
344  refCast<const genericFvPatchField<Type>>(ptf);
345 
346  #define ResetTypeFields(Type, nullArg) \
347  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
348  { \
349  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
350  dptf.Type##Fields_.find(iter.key()); \
351  \
352  if (dptfIter != dptf.Type##Fields_.end()) \
353  { \
354  iter()->reset(*dptfIter()); \
355  } \
356  }
358  #undef ResetTypeFields
359 }
360 
361 
362 template<class Type>
365 (
366  const tmp<scalarField>&
367 ) const
368 {
370  << "cannot be called for a genericFvPatchField"
371  " (actual type " << actualTypeName() << ")"
372  << "\n on patch " << this->patch().name()
373  << " of field " << this->internalField().name()
374  << " in file " << this->internalField().objectPath()
375  << "\n You are probably trying to solve for a field with a "
376  "generic boundary condition."
377  << abort(FatalError);
378 
379  return *this;
380 }
381 
382 
383 template<class Type>
386 (
387  const tmp<scalarField>&
388 ) const
389 {
391  << "cannot be called for a genericFvPatchField"
392  " (actual type " << actualTypeName() << ")"
393  << "\n on patch " << this->patch().name()
394  << " of field " << this->internalField().name()
395  << " in file " << this->internalField().objectPath()
396  << "\n You are probably trying to solve for a field with a "
397  "generic boundary condition."
398  << abort(FatalError);
399 
400  return *this;
401 }
402 
403 
404 template<class Type>
407 {
409  << "cannot be called for a genericFvPatchField"
410  " (actual type " << actualTypeName() << ")"
411  << "\n on patch " << this->patch().name()
412  << " of field " << this->internalField().name()
413  << " in file " << this->internalField().objectPath()
414  << "\n You are probably trying to solve for a field with a "
415  "generic boundary condition."
416  << abort(FatalError);
417 
418  return *this;
419 }
420 
421 template<class Type>
424 {
426  << "cannot be called for a genericFvPatchField"
427  " (actual type " << actualTypeName() << ")"
428  << "\n on patch " << this->patch().name()
429  << " of field " << this->internalField().name()
430  << " in file " << this->internalField().objectPath()
431  << "\n You are probably trying to solve for a field with a "
432  "generic boundary condition."
433  << abort(FatalError);
434 
435  return *this;
436 }
437 
438 
439 template<class Type>
441 {
442  writeEntry(os, "type", actualTypeName());
443 
444  forAllConstIter(dictionary, dict_, iter)
445  {
446  if (iter().keyword() != "type" && iter().keyword() != "value")
447  {
448  if
449  (
450  iter().isStream()
451  && iter().stream().size()
452  && iter().stream()[0].isWord()
453  && iter().stream()[0].wordToken() == "nonuniform"
454  )
455  {
456  #define WriteTypeFieldEntry(Type, nullArg) \
457  else if (Type##Fields_.found(iter().keyword())) \
458  { \
459  writeEntry \
460  ( \
461  os, \
462  iter().keyword(), \
463  *Type##Fields_.find(iter().keyword())() \
464  ); \
465  }
467  #undef WriteTypeFieldEntry
468  }
469  else
470  {
471  iter().write(os);
472  }
473  }
474  }
475 
476  writeEntry(os, "value", *this);
477 }
478 
479 
480 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Pre-declare SubField and related Field type.
Definition: Field.H:83
A HashTable specialisation for hashing pointers.
Definition: HashPtrTable.H:67
Input token stream.
Definition: ITstream.H:53
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
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:91
const DimensionedField< Type, volMesh > & internalField() const
Return dimensioned internal field reference.
Definition: fvPatchField.H:374
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
Definition: fvPatchField.C:203
const fvPatch & patch() const
Return patch.
Definition: fvPatchField.H:368
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:64
virtual const word & name() const
Return name.
Definition: fvPatch.H:126
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.
genericFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &, const dictionary &)
Construct from patch, internal field and dictionary.
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.
A class for managing temporary objects.
Definition: tmp.H:55
A token holds items read from Istream.
Definition: token.H:73
bool isLabel() const
Definition: tokenI.H:571
bool isPunctuation() const
Definition: tokenI.H:280
label labelToken() const
Definition: tokenI.H:590
bool isCompound() const
Definition: tokenI.H:784
const compound & compoundToken() const
Definition: tokenI.H:789
bool isWord() const
Definition: tokenI.H:298
const word & wordToken() const
Definition: tokenI.H:303
scalar number() const
Definition: tokenI.H:755
A class for handling words, derived from string.
Definition: word.H:62
#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)
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
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
bool insertUniformTypeField(const scalarList &components, const word &keyword, const label size, HashPtrTable< Field< Type >> &typeFields)
IOerror FatalIOError
error FatalError
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
static const char nl
Definition: Ostream.H:267
dictionary dict
volScalarField & p