mappedPatchFieldBase.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2013 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 "mappedPatchFieldBase.H"
27 #include "mappedPatchBase.H"
28 #include "interpolationCell.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
37 template<class Type>
39 (
40  const mappedPatchBase& mapper,
41  const fvPatchField<Type>& patchField,
42  const word& fieldName,
43  const bool setAverage,
44  const Type average,
45  const word& interpolationScheme
46 )
47 :
48  mapper_(mapper),
49  patchField_(patchField),
50  fieldName_(fieldName),
51  setAverage_(setAverage),
52  average_(average),
53  interpolationScheme_(interpolationScheme)
54 {}
55 
56 
57 template<class Type>
59 (
60  const mappedPatchBase& mapper,
61  const fvPatchField<Type>& patchField,
62  const dictionary& dict
63 )
64 :
65  mapper_(mapper),
66  patchField_(patchField),
67  fieldName_
68  (
69  dict.template lookupOrDefault<word>
70  (
71  "fieldName",
72  patchField_.dimensionedInternalField().name()
73  )
74  ),
75  setAverage_(readBool(dict.lookup("setAverage"))),
76  average_(pTraits<Type>(dict.lookup("average"))),
77  interpolationScheme_(interpolationCell<Type>::typeName)
78 {
79  if (mapper_.mode() == mappedPatchBase::NEARESTCELL)
80  {
81  dict.lookup("interpolationScheme") >> interpolationScheme_;
82  }
83 }
84 
85 
86 template<class Type>
88 (
89  const mappedPatchBase& mapper,
90  const fvPatchField<Type>& patchField
91 )
92 :
93  mapper_(mapper),
94  patchField_(patchField),
95  fieldName_(patchField_.dimensionedInternalField().name()),
96  setAverage_(false),
97  average_(pTraits<Type>::zero),
98  interpolationScheme_(interpolationCell<Type>::typeName)
99 {}
100 
101 
102 template<class Type>
104 (
105  const mappedPatchFieldBase<Type>& mapper
106 )
107 :
108  mapper_(mapper.mapper_),
109  patchField_(mapper.patchField_),
110  fieldName_(mapper.fieldName_),
111  setAverage_(mapper.setAverage_),
112  average_(mapper.average_),
113  interpolationScheme_(mapper.interpolationScheme_)
114 {}
115 
116 
117 template<class Type>
119 (
120  const mappedPatchBase& mapper,
121  const fvPatchField<Type>& patchField,
122  const mappedPatchFieldBase<Type>& base
123 )
124 :
125  mapper_(mapper),
126  patchField_(patchField),
127  fieldName_(base.fieldName_),
128  setAverage_(base.setAverage_),
129  average_(base.average_),
130  interpolationScheme_(base.interpolationScheme_)
131 {}
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
136 template<class Type>
139 {
141 
142  const fvMesh& nbrMesh = refCast<const fvMesh>(mapper_.sampleMesh());
143 
144  if (mapper_.sameRegion())
145  {
146  if (fieldName_ == patchField_.dimensionedInternalField().name())
147  {
148  // Optimisation: bypass field lookup
149  return
150  dynamic_cast<const fieldType&>
151  (
152  patchField_.dimensionedInternalField()
153  );
154  }
155  else
156  {
157  const fvMesh& thisMesh = patchField_.patch().boundaryMesh().mesh();
158  return thisMesh.template lookupObject<fieldType>(fieldName_);
159  }
160  }
161  else
162  {
163  return nbrMesh.template lookupObject<fieldType>(fieldName_);
164  }
165 }
166 
167 
168 template<class Type>
170 {
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& thisMesh = patchField_.patch().boundaryMesh().mesh();
179  const fvMesh& nbrMesh = refCast<const fvMesh>(mapper_.sampleMesh());
180 
181  // Result of obtaining remote values
182  tmp<Field<Type> > tnewValues(new Field<Type>(0));
183  Field<Type>& newValues = tnewValues();
184 
185  switch (mapper_.mode())
186  {
188  {
189  const mapDistribute& distMap = mapper_.map();
190 
191  if (interpolationScheme_ != interpolationCell<Type>::typeName)
192  {
193  // Send back sample points to the processor that holds the cell
194  vectorField samples(mapper_.samplePoints());
195  distMap.reverseDistribute
196  (
197  (
198  mapper_.sameRegion()
199  ? thisMesh.nCells()
200  : nbrMesh.nCells()
201  ),
202  point::max,
203  samples
204  );
205 
206  autoPtr<interpolation<Type> > interpolator
207  (
209  (
210  interpolationScheme_,
211  sampleField()
212  )
213  );
214  const interpolation<Type>& interp = interpolator();
215 
216  newValues.setSize(samples.size(), pTraits<Type>::max);
217  forAll(samples, cellI)
218  {
219  if (samples[cellI] != point::max)
220  {
221  newValues[cellI] = interp.interpolate
222  (
223  samples[cellI],
224  cellI
225  );
226  }
227  }
228  }
229  else
230  {
231  newValues = sampleField();
232  }
233 
234  distMap.distribute(newValues);
235 
236  break;
237  }
240  {
241  const label nbrPatchID =
242  nbrMesh.boundaryMesh().findPatchID(mapper_.samplePatch());
243 
244  if (nbrPatchID < 0)
245  {
247  (
248  "void mappedPatchFieldBase<Type>::updateCoeffs()"
249  )<< "Unable to find sample patch " << mapper_.samplePatch()
250  << " in region " << mapper_.sampleRegion()
251  << " for patch " << patchField_.patch().name() << nl
252  << abort(FatalError);
253  }
254 
255  const fieldType& nbrField = sampleField();
256 
257  newValues = nbrField.boundaryField()[nbrPatchID];
258  mapper_.distribute(newValues);
259 
260  break;
261  }
263  {
264  Field<Type> allValues(nbrMesh.nFaces(), pTraits<Type>::zero);
265 
266  const fieldType& nbrField = sampleField();
267 
268  forAll(nbrField.boundaryField(), patchI)
269  {
270  const fvPatchField<Type>& pf =
271  nbrField.boundaryField()[patchI];
272  label faceStart = pf.patch().start();
273 
274  forAll(pf, faceI)
275  {
276  allValues[faceStart++] = pf[faceI];
277  }
278  }
279 
280  mapper_.distribute(allValues);
281  newValues.transfer(allValues);
282 
283  break;
284  }
285  default:
286  {
288  (
289  "mappedPatchFieldBase<Type>::updateCoeffs()"
290  )<< "Unknown sampling mode: " << mapper_.mode()
291  << nl << abort(FatalError);
292  }
293  }
294 
295  if (setAverage_)
296  {
297  Type averagePsi =
298  gSum(patchField_.patch().magSf()*newValues)
299  /gSum(patchField_.patch().magSf());
300 
301  if (mag(averagePsi)/mag(average_) > 0.5)
302  {
303  newValues *= mag(average_)/mag(averagePsi);
304  }
305  else
306  {
307  newValues += (average_ - averagePsi);
308  }
309  }
310 
311  // Restore tag
312  UPstream::msgType() = oldTag;
313 
314  return tnewValues;
315 }
316 
317 
318 template<class Type>
320 {
321  os.writeKeyword("fieldName") << fieldName_ << token::END_STATEMENT << nl;
322  os.writeKeyword("setAverage") << setAverage_ << token::END_STATEMENT << nl;
323  os.writeKeyword("average") << average_ << token::END_STATEMENT << nl;
324  os.writeKeyword("interpolationScheme") << interpolationScheme_
325  << token::END_STATEMENT << nl;
326 }
327 
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 } // End namespace Foam
332 
333 // ************************************************************************* //
bool readBool(Istream &)
Definition: boolIO.C:60
const Type average_
Average value the mapped field is adjusted to maintain if.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
label nFaces() const
static void distribute(const Pstream::commsTypes commsType, const List< labelPair > &schedule, const label constructSize, const labelListList &subMap, const labelListList &constructMap, List< T > &, const int tag=UPstream::msgType())
Distribute data. Note:schedule only used for Pstream::scheduled.
void transfer(List< Type > &)
Transfer the contents of the argument List into this list.
dimensioned< scalar > mag(const dimensioned< Type > &)
mappedPatchFieldBase(const mappedPatchBase &mapper, const fvPatchField< Type > &patchField, const word &fieldName, const bool setAverage, const Type average, const word &interpolationScheme)
Construct from components.
word interpolationScheme_
Interpolation scheme to use for nearestcell mode.
Class containing processor-to-processor mapping information.
Uses the cell value for any point in the cell.
A class for handling words, derived from string.
Definition: word.H:59
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
Abstract base class for interpolation.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
label nCells() const
static const Vector max
Definition: Vector.H:82
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const mappedPatchBase & mapper_
Mapping engine.
Namespace for OpenFOAM.
Type gSum(const FieldField< Field, Type > &f)
void reverseDistribute(const label constructSize, List< T > &, const bool dummyTransform=true, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType.
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
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.
word fieldName_
Name of field to sample.
#define forAll(list, i)
Definition: UList.H:421
virtual tmp< Field< Type > > mappedField() const
Map sampleField onto *this patch.
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:451
virtual void write(Ostream &) const
Write.
Functionality for sampling fields using mappedPatchBase. Every call to mappedField() returns a sample...
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: fvPatchField.H:65
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Generic GeometricField class.
Traits class for primitives.
Definition: pTraits.H:50
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
const polyMesh & mesh() const
Return the mesh reference.
const bool setAverage_
If true adjust the mapped field to maintain average value average_.
const fvPatchField< Type > & patchField_
Underlying patch field.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
scalarField samples(nIntervals, 0)
Determines a mapping between patch face centres and mesh cell or face centres and processors they&#39;re ...
const GeometricField< Type, fvPatchField, volMesh > & sampleField() const
Field to sample. Either on my or nbr mesh.
label findPatchID(const word &patchName) const
Find patch index given a name.
const fvPatch & patch() const
Return patch.
Definition: fvPatchField.H:300
label start() const
Return start label of this patch in the polyMesh face list.
Definition: fvPatch.H:155
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
A class for managing temporary objects.
Definition: PtrList.H:118