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-2023 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  scalarFields_(ptf.scalarFields_),
299  vectorFields_(ptf.vectorFields_),
300  sphericalTensorFields_(ptf.sphericalTensorFields_),
301  symmTensorFields_(ptf.symmTensorFields_),
302  tensorFields_(ptf.tensorFields_)
303 {}
304 
305 
306 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
307 
308 template<class Type>
310 (
311  const fvPatchField<Type>& ptf,
312  const fieldMapper& mapper
313 )
314 {
316 
317  const genericFvPatchField<Type>& dptf =
318  refCast<const genericFvPatchField<Type>>(ptf);
319 
320  #define MapTypeFields(Type, nullArg) \
321  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
322  { \
323  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
324  dptf.Type##Fields_.find(iter.key()); \
325  \
326  if (dptfIter != dptf.Type##Fields_.end()) \
327  { \
328  mapper(*iter(), *dptfIter()); \
329  } \
330  }
332  #undef MapTypeFields
333 }
334 
335 
336 template<class Type>
338 (
339  const fvPatchField<Type>& ptf
340 )
341 {
343 
344  const genericFvPatchField<Type>& dptf =
345  refCast<const genericFvPatchField<Type>>(ptf);
346 
347  #define ResetTypeFields(Type, nullArg) \
348  forAllIter(HashPtrTable<Field<Type>>, Type##Fields_, iter) \
349  { \
350  HashPtrTable<Field<Type>>::const_iterator dptfIter = \
351  dptf.Type##Fields_.find(iter.key()); \
352  \
353  if (dptfIter != dptf.Type##Fields_.end()) \
354  { \
355  iter()->reset(*dptfIter()); \
356  } \
357  }
359  #undef ResetTypeFields
360 }
361 
362 
363 template<class Type>
366 (
367  const tmp<scalarField>&
368 ) const
369 {
371  << "cannot be called for a genericFvPatchField"
372  " (actual type " << actualTypeName() << ")"
373  << "\n on patch " << this->patch().name()
374  << " of field " << this->internalField().name()
375  << " in file " << this->internalField().objectPath()
376  << "\n You are probably trying to solve for a field with a "
377  "generic boundary condition."
378  << abort(FatalError);
379 
380  return *this;
381 }
382 
383 
384 template<class Type>
387 (
388  const tmp<scalarField>&
389 ) const
390 {
392  << "cannot be called for a genericFvPatchField"
393  " (actual type " << actualTypeName() << ")"
394  << "\n on patch " << this->patch().name()
395  << " of field " << this->internalField().name()
396  << " in file " << this->internalField().objectPath()
397  << "\n You are probably trying to solve for a field with a "
398  "generic boundary condition."
399  << abort(FatalError);
400 
401  return *this;
402 }
403 
404 
405 template<class Type>
408 {
410  << "cannot be called for a genericFvPatchField"
411  " (actual type " << actualTypeName() << ")"
412  << "\n on patch " << this->patch().name()
413  << " of field " << this->internalField().name()
414  << " in file " << this->internalField().objectPath()
415  << "\n You are probably trying to solve for a field with a "
416  "generic boundary condition."
417  << abort(FatalError);
418 
419  return *this;
420 }
421 
422 template<class Type>
425 {
427  << "cannot be called for a genericFvPatchField"
428  " (actual type " << actualTypeName() << ")"
429  << "\n on patch " << this->patch().name()
430  << " of field " << this->internalField().name()
431  << " in file " << this->internalField().objectPath()
432  << "\n You are probably trying to solve for a field with a "
433  "generic boundary condition."
434  << abort(FatalError);
435 
436  return *this;
437 }
438 
439 
440 template<class Type>
442 {
443  writeEntry(os, "type", actualTypeName());
444 
445  forAllConstIter(dictionary, dict_, iter)
446  {
447  if (iter().keyword() != "type" && iter().keyword() != "value")
448  {
449  if
450  (
451  iter().isStream()
452  && iter().stream().size()
453  && iter().stream()[0].isWord()
454  && iter().stream()[0].wordToken() == "nonuniform"
455  )
456  {
457  #define WriteTypeFieldEntry(Type, nullArg) \
458  else if (Type##Fields_.found(iter().keyword())) \
459  { \
460  writeEntry \
461  ( \
462  os, \
463  iter().keyword(), \
464  *Type##Fields_.find(iter().keyword())() \
465  ); \
466  }
468  #undef WriteTypeFieldEntry
469  }
470  else
471  {
472  iter().write(os);
473  }
474  }
475  }
476 
477  writeEntry(os, "value", *this);
478 }
479 
480 
481 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
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 keyword definitions, which are a keyword followed by any number of values (e....
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:88
const DimensionedField< Type, volMesh > & internalField() const
Return dimensioned internal field reference.
Definition: fvPatchField.H:362
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
Definition: fvPatchField.C:195
const fvPatch & patch() const
Return patch.
Definition: fvPatchField.H:356
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 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:266
dictionary dict
volScalarField & p