mappedInternalValueFvPatchField.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) 2022-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 
27 #include "volFields.H"
28 #include "interpolationCell.H"
29 
30 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
35 {
36  if (mapperPtr_.valid())
37  {
38  return mapperPtr_();
39  }
40 
41  if (isA<mappedInternalPatchBase>(this->patch().patch()))
42  {
43  return refCast<const mappedInternalPatchBase>(this->patch().patch());
44  }
45 
47  << "Field " << this->internalField().name() << " on patch "
48  << this->patch().name() << " in file "
49  << this->internalField().objectPath()
50  << " has neither a mapper specified nor is the patch of "
51  << mappedInternalPatchBase::typeName << " type"
52  << exit(FatalError);
53 
54  return NullObjectRef<mappedInternalPatchBase>();
55 }
56 
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
60 template<class Type>
63 (
64  const fvPatch& p,
66  const dictionary& dict
67 )
68 :
69  fixedValueFvPatchField<Type>(p, iF, dict),
70  fieldName_(dict.lookupOrDefault<word>("field", iF.name())),
71  setAverage_
72  (
73  dict.lookupOrDefault<bool>("setAverage", dict.found("average"))
74  ),
75  average_(setAverage_ ? dict.lookup<Type>("average") : Zero),
76  interpolationScheme_(dict.lookup<word>("interpolationScheme")),
77  mapperPtr_
78  (
79  mappedInternalPatchBase::specified(dict)
80  ? new mappedInternalPatchBase(p.patch(), dict)
81  : nullptr
82  )
83 {}
84 
85 
86 template<class Type>
89 (
91  const fvPatch& p,
93  const fvPatchFieldMapper& mapper
94 )
95 :
96  fixedValueFvPatchField<Type>(ptf, p, iF, mapper),
97  fieldName_(ptf.fieldName_),
98  setAverage_(ptf.setAverage_),
99  average_(ptf.average_),
100  interpolationScheme_(ptf.interpolationScheme_),
101  mapperPtr_
102  (
103  ptf.mapperPtr_.valid()
104  ? new mappedInternalPatchBase(p.patch(), ptf.mapperPtr_())
105  : nullptr
106  )
107 {}
108 
109 
110 template<class Type>
113 (
116 )
117 :
118  fixedValueFvPatchField<Type>(ptf, iF),
119  fieldName_(ptf.fieldName_),
120  setAverage_(ptf.setAverage_),
121  average_(ptf.average_),
122  interpolationScheme_(ptf.interpolationScheme_),
123  mapperPtr_
124  (
125  ptf.mapperPtr_.valid()
126  ? new mappedInternalPatchBase(ptf.patch().patch(), ptf.mapperPtr_())
127  : nullptr
128  )
129 {}
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
134 template<class Type>
136 (
137  const fvPatchField<Type>& ptf,
138  const fvPatchFieldMapper& mapper
139 )
140 {
142 
143  if (mapperPtr_.valid())
144  {
145  mapperPtr_->clearOut();
146  }
147 }
148 
149 
150 template<class Type>
152 (
153  const fvPatchField<Type>& ptf
154 )
155 {
157 
158  if (mapperPtr_.valid())
159  {
160  mapperPtr_->clearOut();
161  }
162 }
163 
164 
165 template<class Type>
167 {
168  if (this->updated())
169  {
170  return;
171  }
172 
173  // Since we're inside initEvaluate/evaluate there might be processor
174  // comms underway. Change the tag we use.
175  int oldTag = UPstream::msgType();
176  UPstream::msgType() = oldTag + 1;
177 
178  const fvMesh& nbrMesh = refCast<const fvMesh>(this->mapper().nbrMesh());
179 
180  const VolField<Type>& nbrField =
181  this->mapper().sameRegion()
182  && this->fieldName_ == this->internalField().name()
183  ? refCast<const VolField<Type>>(this->internalField())
184  : nbrMesh.template lookupObject<VolField<Type>>(this->fieldName_);
185 
186  // Construct mapped values
187  Field<Type> sampleValues;
188 
189  if (interpolationScheme_ != interpolationCell<Type>::typeName)
190  {
191  // Create an interpolation
192  autoPtr<interpolation<Type>> interpolatorPtr
193  (
195  (
196  interpolationScheme_,
197  nbrField
198  )
199  );
200  const interpolation<Type>& interpolator = interpolatorPtr();
201 
202  // Cells on which samples are generated
203  const labelList& sampleCells = mapper().cellIndices();
204 
205  // Send the patch points to the cells
206  pointField samplePoints(mapper().samplePoints());
207  mapper().map().reverseDistribute
208  (
209  sampleCells.size(),
210  samplePoints
211  );
212 
213  // Interpolate values
214  sampleValues.resize(sampleCells.size());
215  forAll(sampleCells, i)
216  {
217  if (sampleCells[i] != -1)
218  {
219  sampleValues[i] =
220  interpolator.interpolate
221  (
222  samplePoints[i],
223  sampleCells[i]
224  );
225  }
226  }
227 
228  // Send the values back to the patch
229  mapper().map().distribute(sampleValues);
230  }
231  else
232  {
233  // No interpolation. Just sample cell values directly.
234  sampleValues = mapper().distribute(nbrField);
235  }
236 
237  // Set the average, if necessary
238  if (setAverage_)
239  {
240  const Type sampleAverageValue =
241  gSum(this->patch().magSf()*sampleValues)
242  /gSum(this->patch().magSf());
243 
244  if (mag(sampleAverageValue)/mag(average_) > 0.5)
245  {
246  sampleValues *= mag(average_)/mag(sampleAverageValue);
247  }
248  else
249  {
250  sampleValues += average_ - sampleAverageValue;
251  }
252  }
253 
254  // Assign sampled patch values
255  this->operator==(sampleValues);
256 
257  // Restore tag
258  UPstream::msgType() = oldTag;
259 
261 }
262 
263 
264 template<class Type>
266 {
268 
270  (
271  os,
272  "field",
273  this->internalField().name(),
274  fieldName_
275  );
276 
277  if (setAverage_)
278  {
279  writeEntry(os, "average", average_);
280  }
281 
282  writeEntry(os, "interpolationScheme", interpolationScheme_);
283 
284  if (mapperPtr_.valid())
285  {
286  mapperPtr_->write(os);
287  }
288 
289  writeEntry(os, "value", *this);
290 }
291 
292 
293 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:82
Generic GeometricField class.
const word & name() const
Return name.
Definition: IOobject.H:310
void resize(const label)
Alias for setSize(const label)
Definition: ListI.H:138
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
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
This boundary condition supplies a fixed value constraint, and is the base class for a number of othe...
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:101
Foam::fvPatchFieldMapper.
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: fvPatchField.H:87
virtual void write(Ostream &) const
Write.
Definition: fvPatchField.C:231
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
Definition: fvPatchField.C:204
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
Definition: fvPatchField.C:197
virtual void map(const fvPatchField< Type > &, const fvPatchFieldMapper &)
Map the given fvPatchField onto this fvPatchField.
Definition: fvPatchField.C:187
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:64
Uses the cell value for any point in the cell.
Abstract base class for interpolation.
Definition: interpolation.H:55
virtual Type interpolate(const vector &position, const label celli, const label facei=-1) const =0
Interpolate field to the given point in the given cell.
Engine which provides mapping from cells to patch faces.
This boundary condition maps the values from a internal cells to this patch.
mappedInternalValueFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &, const dictionary &)
Construct from patch, internal field and dictionary.
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
virtual void map(const fvPatchField< Type > &, const fvPatchFieldMapper &)
Map the given fvPatchField onto this fvPatchField.
const mappedInternalPatchBase & mapper() const
Return the mapping engine.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
bool valid(const PtrList< ModelType > &l)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
Type gSum(const FieldField< Field, Type > &f)
void writeEntryIfDifferent(Ostream &os, const word &entryName, const EntryType &value1, const EntryType &value2)
Helper function to write the keyword and entry only if the.
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
dimensioned< scalar > mag(const dimensioned< Type > &)
error FatalError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dictionary dict
volScalarField & p