faceMapper.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-2022 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 "faceMapper.H"
27 #include "fieldTypes.H"
28 #include "demandDrivenData.H"
29 #include "polyMesh.H"
30 #include "polyTopoChangeMap.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::faceMapper::calcAddressing() const
35 {
36  if
37  (
38  directAddrPtr_
39  || interpolationAddrPtr_
40  || weightsPtr_
41  || insertedFaceLabelsPtr_
42  )
43  {
45  << "Addressing already calculated."
46  << abort(FatalError);
47  }
48 
49  if (direct())
50  {
51  // Direct addressing, no weights
52 
53  directAddrPtr_ = new labelList(mpm_.faceMap());
54  labelList& directAddr = *directAddrPtr_;
55 
56  // Reset the size of addressing list to contain only live faces
57  directAddr.setSize(mesh_.nFaces());
58 
59  insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces());
60  labelList& insertedFaces = *insertedFaceLabelsPtr_;
61 
62  label nInsertedFaces = 0;
63 
64  forAll(directAddr, facei)
65  {
66  if (directAddr[facei] < 0)
67  {
68  // Found inserted face
69  directAddr[facei] = 0;
70  insertedFaces[nInsertedFaces] = facei;
71  nInsertedFaces++;
72  }
73  }
74 
75  insertedFaces.setSize(nInsertedFaces);
76  }
77  else
78  {
79  // Interpolative addressing
80 
81  interpolationAddrPtr_ = new labelListList(mesh_.nFaces());
82  labelListList& addr = *interpolationAddrPtr_;
83 
84  weightsPtr_ = new scalarListList(mesh_.nFaces());
85  scalarListList& w = *weightsPtr_;
86 
87  const List<objectMap>& ffp = mpm_.facesFromPointsMap();
88 
89  forAll(ffp, ffpI)
90  {
91  // Get addressing
92  const labelList& mo = ffp[ffpI].masterObjects();
93 
94  label facei = ffp[ffpI].index();
95 
96  if (addr[facei].size())
97  {
99  << "Master face " << facei
100  << " mapped from point faces " << mo
101  << " already destination of mapping." << abort(FatalError);
102  }
103 
104  // Map from masters, uniform weights
105  addr[facei] = mo;
106  w[facei] = scalarList(mo.size(), 1.0/mo.size());
107  }
108 
109  const List<objectMap>& ffe = mpm_.facesFromEdgesMap();
110 
111  forAll(ffe, ffeI)
112  {
113  // Get addressing
114  const labelList& mo = ffe[ffeI].masterObjects();
115 
116  label facei = ffe[ffeI].index();
117 
118  if (addr[facei].size())
119  {
121  << "Master face " << facei
122  << " mapped from edge faces " << mo
123  << " already destination of mapping." << abort(FatalError);
124  }
125 
126  // Map from masters, uniform weights
127  addr[facei] = mo;
128  w[facei] = scalarList(mo.size(), 1.0/mo.size());
129  }
130 
131  const List<objectMap>& fff = mpm_.facesFromFacesMap();
132 
133  forAll(fff, fffI)
134  {
135  // Get addressing
136  const labelList& mo = fff[fffI].masterObjects();
137 
138  label facei = fff[fffI].index();
139 
140  if (addr[facei].size())
141  {
143  << "Master face " << facei
144  << " mapped from face faces " << mo
145  << " already destination of mapping." << abort(FatalError);
146  }
147 
148  // Map from masters, uniform weights
149  addr[facei] = mo;
150  w[facei] = scalarList(mo.size(), 1.0/mo.size());
151  }
152 
153 
154  // Do mapped faces. Note that can already be set from facesFromFaces
155  // so check if addressing size still zero.
156  const labelList& fm = mpm_.faceMap();
157 
158  forAll(fm, facei)
159  {
160  if (fm[facei] > -1 && addr[facei].empty())
161  {
162  // Mapped from a single face
163  addr[facei] = labelList(1, fm[facei]);
164  w[facei] = scalarList(1, 1.0);
165  }
166  }
167 
168 
169  // Grab inserted faces (for them the size of addressing is still zero)
170 
171  insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces());
172  labelList& insertedFaces = *insertedFaceLabelsPtr_;
173 
174  label nInsertedFaces = 0;
175 
176  forAll(addr, facei)
177  {
178  if (addr[facei].empty())
179  {
180  // Mapped from a dummy face
181  addr[facei] = labelList(1, label(0));
182  w[facei] = scalarList(1, 1.0);
183 
184  insertedFaces[nInsertedFaces] = facei;
185  nInsertedFaces++;
186  }
187  }
188 
189  insertedFaces.setSize(nInsertedFaces);
190  }
191 }
192 
193 
194 void Foam::faceMapper::clearOut()
195 {
196  deleteDemandDrivenData(directAddrPtr_);
197  deleteDemandDrivenData(interpolationAddrPtr_);
198  deleteDemandDrivenData(weightsPtr_);
199  deleteDemandDrivenData(insertedFaceLabelsPtr_);
200 }
201 
202 
203 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
204 
206 :
207  mesh_(mpm.mesh()),
208  mpm_(mpm),
209  insertedFaces_(true),
210  direct_(false),
211  directAddrPtr_(nullptr),
212  interpolationAddrPtr_(nullptr),
213  weightsPtr_(nullptr),
214  insertedFaceLabelsPtr_(nullptr)
215 {
216  // Check for possibility of direct mapping
217  if
218  (
219  mpm_.facesFromPointsMap().empty()
220  && mpm_.facesFromEdgesMap().empty()
221  && mpm_.facesFromFacesMap().empty()
222  )
223  {
224  direct_ = true;
225  }
226  else
227  {
228  direct_ = false;
229  }
230 
231  // Check for inserted faces
232  if (direct_ && (mpm_.faceMap().empty() || min(mpm_.faceMap()) > -1))
233  {
234  insertedFaces_ = false;
235  }
236  else
237  {
238  // Need to check all 3 lists to see if there are inserted faces
239  // with no owner
240 
241  // Make a copy of the face map, add the entries for faces from points
242  // and faces from edges and check for left-overs
243  labelList fm(mesh_.nFaces(), -1);
244 
245  const List<objectMap>& ffp = mpm_.facesFromPointsMap();
246 
247  forAll(ffp, ffpI)
248  {
249  fm[ffp[ffpI].index()] = 0;
250  }
251 
252  const List<objectMap>& ffe = mpm_.facesFromEdgesMap();
253 
254  forAll(ffe, ffeI)
255  {
256  fm[ffe[ffeI].index()] = 0;
257  }
258 
259  const List<objectMap>& fff = mpm_.facesFromFacesMap();
260 
261  forAll(fff, fffI)
262  {
263  fm[fff[fffI].index()] = 0;
264  }
265 
266  if (min(fm) < 0)
267  {
268  insertedFaces_ = true;
269  }
270  }
271 }
272 
273 
274 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
275 
277 {
278  clearOut();
279 }
280 
281 
282 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
283 
285 {
286  return mpm_.nOldFaces();
287 }
288 
289 
291 {
292  return mpm_.nOldInternalFaces();
293 }
294 
295 
297 {
298  if (!direct())
299  {
301  << "Requested direct addressing for an interpolative mapper."
302  << abort(FatalError);
303  }
304 
305  if (!insertedObjects())
306  {
307  // No inserted faces. Re-use faceMap
308  return mpm_.faceMap();
309  }
310  else
311  {
312  if (!directAddrPtr_)
313  {
314  calcAddressing();
315  }
316 
317  return *directAddrPtr_;
318  }
319 }
320 
321 
323 {
324  if (direct())
325  {
327  << "Requested interpolative addressing for a direct mapper."
328  << abort(FatalError);
329  }
330 
331  if (!interpolationAddrPtr_)
332  {
333  calcAddressing();
334  }
335 
336  return *interpolationAddrPtr_;
337 }
338 
339 
341 {
342  if (direct())
343  {
345  << "Requested interpolative weights for a direct mapper."
346  << abort(FatalError);
347  }
348 
349  if (!weightsPtr_)
350  {
351  calcAddressing();
352  }
353 
354  return *weightsPtr_;
355 }
356 
357 
359 {
360  if (!insertedFaceLabelsPtr_)
361  {
362  if (!insertedObjects())
363  {
364  // There are no inserted faces
365  insertedFaceLabelsPtr_ = new labelList(0);
366  }
367  else
368  {
369  calcAddressing();
370  }
371  }
372 
373  return *insertedFaceLabelsPtr_;
374 }
375 
376 
378 {
379  return mpm_.flipFaceFlux();
380 }
381 
382 
384 {
385  return mpm_.nOldInternalFaces();
386 }
387 
388 
390 {
391  return mpm_.oldPatchStarts();
392 }
393 
394 
396 {
397  return mpm_.oldPatchSizes();
398 }
399 
400 
401 // ************************************************************************* //
const labelHashSet & flipFaceFlux() const
Map of flipped face flux faces.
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
const labelList & oldPatchStarts() const
Return list of the old patch start labels.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
const List< objectMap > & facesFromEdgesMap() const
Faces inflated from edges.
virtual label internalSizeBeforeMapping() const
Return number of internal faces before mapping.
Definition: faceMapper.C:290
const labelList & faceMap() const
Old face map.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
label nFaces() const
virtual const labelList & insertedObjectLabels() const
Return list of inserted faces.
Definition: faceMapper.C:358
fvMesh & mesh
virtual label sizeBeforeMapping() const
Return size of field before mapping.
Definition: faceMapper.C:284
virtual bool insertedObjects() const
Are there any inserted faces.
Definition: faceMapper.H:156
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: faceMapper.C:322
Include the header files for all the primitive types that Fields are instantiated for...
virtual const labelHashSet & flipFaceFlux() const
Return flux flip map.
Definition: faceMapper.C:377
List< scalarList > scalarListList
Definition: scalarList.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
faceMapper(const polyTopoChangeMap &mpm)
Construct from polyTopoChangeMap.
Definition: faceMapper.C:205
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
virtual const scalarListList & weights() const
Return interpolation weights.
Definition: faceMapper.C:340
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
List< label > labelList
A List of labels.
Definition: labelList.H:56
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
virtual bool direct() const
Is the mapping direct.
Definition: faceMapper.H:124
label nOldInternalFaces() const
Number of old internal faces.
virtual ~faceMapper()
Destructor.
Definition: faceMapper.C:276
void setSize(const label)
Reset size of List.
Definition: List.C:281
Template functions to aid in the implementation of demand driven data.
const List< objectMap > & facesFromFacesMap() const
Faces originating from faces.
virtual const labelList & oldPatchStarts() const
Return old patch starts.
Definition: faceMapper.C:389
virtual label nOldInternalFaces() const
Return number of old internalFaces.
Definition: faceMapper.C:383
const labelList & oldPatchSizes() const
Return list of the old patch sizes.
void deleteDemandDrivenData(DataPtr &dataPtr)
label nOldFaces() const
Number of old faces.
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: faceMapper.C:296
const List< objectMap > & facesFromPointsMap() const
Faces inflated from points.
virtual const labelList & oldPatchSizes() const
Return old patch sizes.
Definition: faceMapper.C:395