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-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 
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_
76  (
77  setAverage_
78  ? dict.lookup<Type>("average", iF.dimensions())
79  : Zero
80  ),
81  interpolationScheme_(dict.lookup<word>("interpolationScheme")),
82  mapperPtr_
83  (
84  mappedInternalPatchBase::specified(dict)
85  ? new mappedInternalPatchBase(p.patch(), dict)
86  : nullptr
87  )
88 {}
89 
90 
91 template<class Type>
94 (
96  const fvPatch& p,
98  const fieldMapper& mapper
99 )
100 :
101  fixedValueFvPatchField<Type>(ptf, p, iF, mapper),
102  fieldName_(ptf.fieldName_),
103  setAverage_(ptf.setAverage_),
104  average_(ptf.average_),
105  interpolationScheme_(ptf.interpolationScheme_),
106  mapperPtr_
107  (
108  ptf.mapperPtr_.valid()
109  ? new mappedInternalPatchBase(p.patch(), ptf.mapperPtr_())
110  : nullptr
111  )
112 {}
113 
114 
115 template<class Type>
118 (
121 )
122 :
123  fixedValueFvPatchField<Type>(ptf, iF),
124  fieldName_(ptf.fieldName_),
125  setAverage_(ptf.setAverage_),
126  average_(ptf.average_),
127  interpolationScheme_(ptf.interpolationScheme_),
128  mapperPtr_
129  (
130  ptf.mapperPtr_.valid()
131  ? new mappedInternalPatchBase(ptf.patch().patch(), ptf.mapperPtr_())
132  : nullptr
133  )
134 {}
135 
136 
137 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 
139 template<class Type>
141 (
142  const fvPatchField<Type>& ptf,
143  const fieldMapper& mapper
144 )
145 {
147 
148  if (mapperPtr_.valid())
149  {
150  mapperPtr_->clearOut();
151  }
152 }
153 
154 
155 template<class Type>
157 (
158  const fvPatchField<Type>& ptf
159 )
160 {
162 
163  if (mapperPtr_.valid())
164  {
165  mapperPtr_->clearOut();
166  }
167 }
168 
169 
170 template<class Type>
172 {
173  if (this->updated())
174  {
175  return;
176  }
177 
178  // Since we're inside initEvaluate/evaluate there might be processor
179  // comms underway. Change the tag we use.
180  int oldTag = UPstream::msgType();
181  UPstream::msgType() = oldTag + 1;
182 
183  const fvMesh& nbrMesh = refCast<const fvMesh>(this->mapper().nbrMesh());
184 
185  const VolField<Type>& nbrField =
186  this->mapper().sameRegion()
187  && this->fieldName_ == this->internalField().name()
188  ? refCast<const VolField<Type>>(this->internalField())
189  : nbrMesh.template lookupObject<VolField<Type>>(this->fieldName_);
190 
191  // Construct mapped values
192  Field<Type> sampleValues;
193 
194  if (interpolationScheme_ != interpolationCell<Type>::typeName)
195  {
196  // Create an interpolation
197  autoPtr<interpolation<Type>> interpolatorPtr
198  (
200  (
201  interpolationScheme_,
202  nbrField
203  )
204  );
205  const interpolation<Type>& interpolator = interpolatorPtr();
206 
207  // Cells on which samples are generated
208  const labelList& sampleCells = mapper().cellIndices();
209 
210  // Send the patch points to the cells
211  pointField samplePoints(mapper().samplePoints());
212  mapper().map().reverseDistribute
213  (
214  sampleCells.size(),
215  samplePoints
216  );
217 
218  // Interpolate values
219  sampleValues.resize(sampleCells.size());
220  forAll(sampleCells, i)
221  {
222  if (sampleCells[i] != -1)
223  {
224  sampleValues[i] =
225  interpolator.interpolate
226  (
227  samplePoints[i],
228  sampleCells[i]
229  );
230  }
231  }
232 
233  // Send the values back to the patch
234  mapper().map().distribute(sampleValues);
235  }
236  else
237  {
238  // No interpolation. Just sample cell values directly.
239  sampleValues = mapper().distribute(nbrField);
240  }
241 
242  // Set the average, if necessary
243  if (setAverage_)
244  {
245  const Type sampleAverageValue =
246  gSum(this->patch().magSf()*sampleValues)
247  /gSum(this->patch().magSf());
248 
249  if (mag(sampleAverageValue)/mag(average_) > 0.5)
250  {
251  sampleValues *= mag(average_)/mag(sampleAverageValue);
252  }
253  else
254  {
255  sampleValues += average_ - sampleAverageValue;
256  }
257  }
258 
259  // Assign sampled patch values
260  this->operator==(sampleValues);
261 
262  // Restore tag
263  UPstream::msgType() = oldTag;
264 
266 }
267 
268 
269 template<class Type>
271 {
273 
275  (
276  os,
277  "field",
278  this->internalField().name(),
279  fieldName_
280  );
281 
282  if (setAverage_)
283  {
284  writeEntry(os, "average", average_);
285  }
286 
287  writeEntry(os, "interpolationScheme", interpolationScheme_);
288 
289  if (mapperPtr_.valid())
290  {
291  mapperPtr_->write(os);
292  }
293 
294  writeEntry(os, "value", *this);
295 }
296 
297 
298 // ************************************************************************* //
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:83
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:162
Abstract base class for field mapping.
Definition: fieldMapper.H:48
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:99
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: fvPatchField.H:88
virtual void write(Ostream &) const
Write.
Definition: fvPatchField.C:229
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
Definition: fvPatchField.C:202
virtual void reset(const fvPatchField< Type > &)
Reset the fvPatchField to the given fvPatchField.
Definition: fvPatchField.C:195
virtual void map(const fvPatchField< Type > &, const fieldMapper &)
Map the given fvPatchField onto this fvPatchField.
Definition: fvPatchField.C:185
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.
const mappedInternalPatchBase & mapper() const
Return the mapping engine.
virtual void map(const fvPatchField< Type > &, const fieldMapper &)
Map the given fvPatchField onto this fvPatchField.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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 > &)
const HashTable< dimensionSet > & dimensions()
Get the table of dimension sets.
Definition: dimensionSets.C:96
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
dimensioned< scalar > mag(const dimensioned< Type > &)
error FatalError
dictionary dict
volScalarField & p