mappedFvPatchField.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 "mappedFvPatchField.H"
27 #include "mappedPolyPatch.H"
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 template<class Type>
34 {
35  return
36  mapperPtr_.valid()
37  ? mapperPtr_()
38  : mappedPatchBase::getMap(p_.patch());
39 }
40 
41 
42 template<class Type>
45 {
46  const fvMesh& nbrMesh =
47  refCast<const fvMesh>(this->mapper().nbrMesh());
48 
49  const VolField<Type>& nbrField =
50  this->mapper().sameRegion()
51  && this->fieldName_ == iF_.name()
52  ? refCast<const VolField<Type>>(iF_)
53  : nbrMesh.template lookupObject<VolField<Type>>(this->fieldName_);
54 
55  const label nbrPatchi = this->mapper().nbrPolyPatch().index();
56 
57  return nbrField.boundaryField()[nbrPatchi];
58 }
59 
60 
61 template<class Type>
64 (
65  const Field<Type>& nbrPatchField
66 ) const
67 {
68  // Since we're inside initEvaluate/evaluate there might be processor
69  // comms underway. Change the tag we use.
70  int oldTag = UPstream::msgType();
71  UPstream::msgType() = oldTag + 1;
72 
73  // Map values
74  tmp<Field<Type>> tResult = this->mapper().fromNeighbour(nbrPatchField);
75 
76  // Set the average, if necessary
77  if (setAverage_)
78  {
79  const Type nbrAverageValue =
80  gSum(p_.magSf()*tResult())
81  /gSum(p_.magSf());
82 
83  if (mag(nbrAverageValue)/mag(average_) > 0.5)
84  {
85  tResult.ref() *= mag(average_)/mag(nbrAverageValue);
86  }
87  else
88  {
89  tResult.ref() += average_ - nbrAverageValue;
90  }
91  }
92 
93  // Restore tag
94  UPstream::msgType() = oldTag;
95 
96  return tResult;
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
101 
102 template<class Type>
104 (
105  const fvPatch& p,
107  const dictionary& dict
108 )
109 :
110  p_(p),
111  iF_(iF),
112  fieldName_(dict.lookupOrDefault<word>("field", iF.name())),
113  setAverage_
114  (
115  dict.lookupOrDefault<bool>("setAverage", dict.found("average"))
116  ),
117  average_(setAverage_ ? dict.lookup<Type>("average") : Zero),
118  mapperPtr_
119  (
120  mappedPatchBase::specified(dict)
121  ? new mappedPatchBase(p.patch(), dict, false)
122  : nullptr
123  )
124 {
125  if (!mapperPtr_.valid() && !isA<mappedPatchBase>(p.patch()))
126  {
127  OStringStream str;
128  str << "Field " << iF.name() << " of type "
129  << type() << " on patch " << p.patch().name()
130  << " of type " << p.patch().type() << " does not "
131  << "have mapping specified (i.e., neighbourPatch, and/or "
132  << "neighbourRegion entries) nor is the patch of "
133  << mappedPolyPatch::typeName << " type";
135  << stringOps::breakIntoIndentedLines(str.str()).c_str()
136  << exit(FatalIOError);
137  }
138 
139  this->mapper().validateForField
140  (
141  *this,
142  iF,
143  dict,
144  this->mapper().sameUntransformedPatch()
145  && this->fieldName_ == iF.name()
146  ? mappedPatchBase::from::differentPatch
147  : mappedPatchBase::from::any
148  );
149 }
150 
151 
152 template<class Type>
154 (
155  const mappedFvPatchField<Type>& ptf,
156  const fvPatch& p,
158  const fvPatchFieldMapper& mapper
159 )
160 :
161  p_(p),
162  iF_(iF),
163  fieldName_(ptf.fieldName_),
164  setAverage_(ptf.setAverage_),
165  average_(ptf.average_),
166  mapperPtr_
167  (
168  ptf.mapperPtr_.valid()
169  ? new mappedPatchBase(p.patch(), ptf.mapperPtr_())
170  : nullptr
171  )
172 {}
173 
174 
175 template<class Type>
177 (
178  const mappedFvPatchField<Type>& ptf,
180 )
181 :
182  p_(ptf.p_),
183  iF_(iF),
184  fieldName_(ptf.fieldName_),
185  setAverage_(ptf.setAverage_),
186  average_(ptf.average_),
187  mapperPtr_
188  (
189  ptf.mapperPtr_.valid()
190  ? new mappedPatchBase(ptf.p_.patch(), ptf.mapperPtr_())
191  : nullptr
192  )
193 {}
194 
195 
196 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
197 
198 template<class Type>
200 {
201  if (mapperPtr_.valid())
202  {
203  mapperPtr_->clearOut();
204  }
205 }
206 
207 
208 template<class Type>
210 {
212  (
213  os,
214  "field",
215  iF_.name(),
216  fieldName_
217  );
218 
219  if (setAverage_)
220  {
221  writeEntry(os, "average", average_);
222  }
223 
224  if (mapperPtr_.valid())
225  {
226  mapperPtr_->write(os);
227  }
228 }
229 
230 
231 // ************************************************************************* //
bool found
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 Boundary & boundaryField() const
Return const-reference to the boundary field.
const word & name() const
Return name.
Definition: IOobject.H:310
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
Output to memory buffer stream.
Definition: OStringStream.H:52
string str() const
Return the string.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
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
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:64
Base class for mapped patch fields.
const mappedPatchBase & mapper() const
Return the mapping engine.
mappedFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &, const dictionary &)
Construct from patch, internal field and dictionary.
virtual void write(Ostream &) const
Write.
tmp< Field< Type > > mappedValues(const Field< Type > &nbrPatchField) const
Return the mapped values, given the neighbouring field.
const fvPatchField< Type > & nbrPatchField() const
Return the neighbouring patch field.
virtual void clearOut()
Clear the mapper if present.
Engine which provides mapping between two patches.
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
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:318
bool valid(const PtrList< ModelType > &l)
string breakIntoIndentedLines(const string &str, const string::size_type nLength=80, const string::size_type nIndent=0)
Break a string up into indented lines.
Definition: stringOps.C:950
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.
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
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
dimensioned< scalar > mag(const dimensioned< Type > &)
IOerror FatalIOError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict
volScalarField & p