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