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