cellMapper.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) 2011-2015 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 "cellMapper.H"
27 #include "demandDrivenData.H"
28 #include "polyMesh.H"
29 #include "mapPolyMesh.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
34 {
35  if
36  (
39  || weightsPtr_
41  )
42  {
44  << "Addressing already calculated."
45  << abort(FatalError);
46  }
47 
48  if (direct())
49  {
50  // Direct addressing, no weights
51 
53  labelList& directAddr = *directAddrPtr_;
54 
55  // Not necessary to resize the list as there are no retired cells
56  // directAddr.setSize(mesh_.nCells());
57 
59  labelList& insertedCells = *insertedCellLabelsPtr_;
60 
61  label nInsertedCells = 0;
62 
63  forAll(directAddr, cellI)
64  {
65  if (directAddr[cellI] < 0)
66  {
67  // Found inserted cell
68  directAddr[cellI] = 0;
69  insertedCells[nInsertedCells] = cellI;
70  nInsertedCells++;
71  }
72  }
73 
74  insertedCells.setSize(nInsertedCells);
75  }
76  else
77  {
78  // Interpolative addressing
79 
82 
85 
87 
88  forAll(cfp, cfpI)
89  {
90  // Get addressing
91  const labelList& mo = cfp[cfpI].masterObjects();
92 
93  label cellI = cfp[cfpI].index();
94 
95  if (addr[cellI].size())
96  {
98  << "Master cell " << cellI
99  << " mapped from point cells " << mo
100  << " already destination of mapping." << abort(FatalError);
101  }
102 
103  // Map from masters, uniform weights
104  addr[cellI] = mo;
105  w[cellI] = scalarList(mo.size(), 1.0/mo.size());
106  }
107 
108  const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
109 
110  forAll(cfe, cfeI)
111  {
112  // Get addressing
113  const labelList& mo = cfe[cfeI].masterObjects();
114 
115  label cellI = cfe[cfeI].index();
116 
117  if (addr[cellI].size())
118  {
120  << "Master cell " << cellI
121  << " mapped from edge cells " << mo
122  << " already destination of mapping." << abort(FatalError);
123  }
124 
125  // Map from masters, uniform weights
126  addr[cellI] = mo;
127  w[cellI] = scalarList(mo.size(), 1.0/mo.size());
128  }
129 
130  const List<objectMap>& cff = mpm_.cellsFromFacesMap();
131 
132  forAll(cff, cffI)
133  {
134  // Get addressing
135  const labelList& mo = cff[cffI].masterObjects();
136 
137  label cellI = cff[cffI].index();
138 
139  if (addr[cellI].size())
140  {
142  << "Master cell " << cellI
143  << " mapped from face cells " << mo
144  << " already destination of mapping." << abort(FatalError);
145  }
146 
147  // Map from masters, uniform weights
148  addr[cellI] = mo;
149  w[cellI] = scalarList(mo.size(), 1.0/mo.size());
150  }
151 
152  // Volume conservative mapping if possible
153 
154  const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
155 
156  forAll(cfc, cfcI)
157  {
158  // Get addressing
159  const labelList& mo = cfc[cfcI].masterObjects();
160 
161  label cellI = cfc[cfcI].index();
162 
163  if (addr[cellI].size())
164  {
166  << "Master cell " << cellI
167  << " mapped from cell cells " << mo
168  << " already destination of mapping."
169  << abort(FatalError);
170  }
171 
172  // Map from masters
173  addr[cellI] = mo;
174  }
175 
176  if (mpm_.hasOldCellVolumes())
177  {
178  // Volume weighted
179 
180  const scalarField& V = mpm_.oldCellVolumes();
181 
182  if (V.size() != sizeBeforeMapping())
183  {
185  << "cellVolumes size " << V.size()
186  << " is not the old number of cells " << sizeBeforeMapping()
187  << ". Are your cellVolumes already mapped?"
188  << " (new number of cells " << size() << ")"
189  << abort(FatalError);
190  }
191 
192  forAll(cfc, cfcI)
193  {
194  const labelList& mo = cfc[cfcI].masterObjects();
195 
196  label cellI = cfc[cfcI].index();
197 
198  w[cellI].setSize(mo.size());
199 
200  if (mo.size())
201  {
202  scalar sumV = 0;
203  forAll(mo, ci)
204  {
205  w[cellI][ci] = V[mo[ci]];
206  sumV += V[mo[ci]];
207  }
208  if (sumV > VSMALL)
209  {
210  forAll(mo, ci)
211  {
212  w[cellI][ci] /= sumV;
213  }
214  }
215  else
216  {
217  // Exception: zero volume. Use uniform mapping
218  w[cellI] = scalarList(mo.size(), 1.0/mo.size());
219  }
220  }
221  }
222  }
223  else
224  {
225  // Uniform weighted
226 
227  forAll(cfc, cfcI)
228  {
229  const labelList& mo = cfc[cfcI].masterObjects();
230 
231  label cellI = cfc[cfcI].index();
232 
233  w[cellI] = scalarList(mo.size(), 1.0/mo.size());
234  }
235  }
236 
237 
238  // Do mapped faces. Note that can already be set from cellsFromCells
239  // so check if addressing size still zero.
240 
241  const labelList& cm = mpm_.cellMap();
242 
243  forAll(cm, cellI)
244  {
245  if (cm[cellI] > -1 && addr[cellI].empty())
246  {
247  // Mapped from a single cell
248  addr[cellI] = labelList(1, cm[cellI]);
249  w[cellI] = scalarList(1, 1.0);
250  }
251  }
252 
253  // Grab inserted points (for them the size of addressing is still zero)
254 
256  labelList& insertedCells = *insertedCellLabelsPtr_;
257 
258  label nInsertedCells = 0;
259 
260  forAll(addr, cellI)
261  {
262  if (addr[cellI].empty())
263  {
264  // Mapped from a dummy cell
265  addr[cellI] = labelList(1, label(0));
266  w[cellI] = scalarList(1, 1.0);
267 
268  insertedCells[nInsertedCells] = cellI;
269  nInsertedCells++;
270  }
271  }
272 
273  insertedCells.setSize(nInsertedCells);
274  }
275 }
276 
277 
279 {
280  deleteDemandDrivenData(directAddrPtr_);
281  deleteDemandDrivenData(interpolationAddrPtr_);
282  deleteDemandDrivenData(weightsPtr_);
283  deleteDemandDrivenData(insertedCellLabelsPtr_);
284 }
285 
286 
287 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
288 
289 // Construct from components
291 :
292  mesh_(mpm.mesh()),
293  mpm_(mpm),
294  insertedCells_(true),
295  direct_(false),
296  directAddrPtr_(NULL),
297  interpolationAddrPtr_(NULL),
298  weightsPtr_(NULL),
299  insertedCellLabelsPtr_(NULL)
300 {
301  // Check for possibility of direct mapping
302  if
303  (
304  mpm_.cellsFromPointsMap().empty()
305  && mpm_.cellsFromEdgesMap().empty()
306  && mpm_.cellsFromFacesMap().empty()
307  && mpm_.cellsFromCellsMap().empty()
308  )
309  {
310  direct_ = true;
311  }
312  else
313  {
314  direct_ = false;
315  }
316 
317  // Check for inserted cells
318  if (direct_ && (mpm_.cellMap().empty() || min(mpm_.cellMap()) > -1))
319  {
320  insertedCells_ = false;
321  }
322  else
323  {
324  // Need to check all 3 lists to see if there are inserted cells
325  // with no owner
326 
327  // Make a copy of the cell map, add the entried for cells from points,
328  // cells from edges and cells from faces and check for left-overs
329  labelList cm(mesh_.nCells(), -1);
330 
331  const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
332 
333  forAll(cfp, cfpI)
334  {
335  cm[cfp[cfpI].index()] = 0;
336  }
337 
338  const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
339 
340  forAll(cfe, cfeI)
341  {
342  cm[cfe[cfeI].index()] = 0;
343  }
344 
345  const List<objectMap>& cff = mpm_.cellsFromFacesMap();
346 
347  forAll(cff, cffI)
348  {
349  cm[cff[cffI].index()] = 0;
350  }
351 
352  const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
353 
354  forAll(cfc, cfcI)
355  {
356  cm[cfc[cfcI].index()] = 0;
357  }
358 
359  if (min(cm) < 0)
360  {
361  insertedCells_ = true;
362  }
363  }
364 }
365 
366 
367 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
368 
370 {
371  clearOut();
372 }
373 
374 
375 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
376 
378 {
379  return mpm_.cellMap().size();
380 }
381 
382 
384 {
385  return mpm_.nOldCells();
386 }
387 
388 
390 {
391  if (!direct())
392  {
394  << "Requested direct addressing for an interpolative mapper."
395  << abort(FatalError);
396  }
397 
398  if (!insertedObjects())
399  {
400  // No inserted cells. Re-use cellMap
401  return mpm_.cellMap();
402  }
403  else
404  {
405  if (!directAddrPtr_)
406  {
407  calcAddressing();
408  }
409 
410  return *directAddrPtr_;
411  }
412 }
413 
414 
416 {
417  if (direct())
418  {
420  << "Requested interpolative addressing for a direct mapper."
421  << abort(FatalError);
422  }
423 
424  if (!interpolationAddrPtr_)
425  {
426  calcAddressing();
427  }
428 
429  return *interpolationAddrPtr_;
430 }
431 
432 
434 {
435  if (direct())
436  {
438  << "Requested interpolative weights for a direct mapper."
439  << abort(FatalError);
440  }
441 
442  if (!weightsPtr_)
443  {
444  calcAddressing();
445  }
446 
447  return *weightsPtr_;
448 }
449 
450 
452 {
453  if (!insertedCellLabelsPtr_)
454  {
455  if (!insertedObjects())
456  {
457  // There are no inserted cells
458  insertedCellLabelsPtr_ = new labelList(0);
459  }
460  else
461  {
462  calcAddressing();
463  }
464  }
465 
466  return *insertedCellLabelsPtr_;
467 }
468 
469 
470 // ************************************************************************* //
Foam::scalarList
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
w
volScalarField w(IOobject("w", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE), mesh, dimensionedScalar("w", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0))
Foam::cellMapper::calcAddressing
void calcAddressing() const
Calculate addressing for mapping with inserted cells.
Definition: cellMapper.C:33
Foam::cellMapper::interpolationAddrPtr_
labelListList * interpolationAddrPtr_
Interpolated addressing (only one for of addressing is used)
Definition: cellMapper.H:79
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::cellMapper::mpm_
const mapPolyMesh & mpm_
Reference to mapPolyMesh.
Definition: cellMapper.H:64
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
mapPolyMesh.H
Foam::cellMapper::directAddressing
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: cellMapper.C:389
Foam::cellMapper::direct_
bool direct_
Is the mapping direct.
Definition: cellMapper.H:70
Foam::mapPolyMesh::cellsFromPointsMap
const List< objectMap > & cellsFromPointsMap() const
Cells inflated from points.
Definition: mapPolyMesh.H:437
Foam::cellMapper::~cellMapper
virtual ~cellMapper()
Destructor.
Definition: cellMapper.C:369
polyMesh.H
Foam::cellMapper::direct
virtual bool direct() const
Is the mapping direct.
Definition: cellMapper.H:127
Foam::primitiveMesh::nCells
label nCells() const
Definition: primitiveMeshI.H:64
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
cellMapper.H
Foam::label
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
Foam::cellMapper::addressing
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: cellMapper.C:415
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::cellMapper::directAddrPtr_
labelList * directAddrPtr_
Direct addressing (only one for of addressing is used)
Definition: cellMapper.H:76
Foam::cellMapper::cellMapper
cellMapper(const cellMapper &)
Disallow default bitwise copy construct.
Foam::mapPolyMesh::cellsFromEdgesMap
const List< objectMap > & cellsFromEdgesMap() const
Cells inflated from edges.
Definition: mapPolyMesh.H:443
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::cellMapper::sizeBeforeMapping
virtual label sizeBeforeMapping() const
Return size before mapping.
Definition: cellMapper.C:383
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::mapPolyMesh::oldCellVolumes
const scalarField & oldCellVolumes() const
Definition: mapPolyMesh.H:647
Foam::cellMapper::weights
virtual const scalarListList & weights() const
Return interpolaion weights.
Definition: cellMapper.C:433
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::labelListList
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
Foam::cellMapper::size
virtual label size() const
Return size.
Definition: cellMapper.C:377
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::cellMapper::weightsPtr_
scalarListList * weightsPtr_
Interpolation weights.
Definition: cellMapper.H:82
Foam::UList
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
Foam::cellMapper::clearOut
void clearOut()
Clear out local storage.
Definition: cellMapper.C:278
Foam::mapPolyMesh::hasOldCellVolumes
bool hasOldCellVolumes() const
Definition: mapPolyMesh.H:642
Foam::mapPolyMesh::cellsFromFacesMap
const List< objectMap > & cellsFromFacesMap() const
Cells inflated from faces.
Definition: mapPolyMesh.H:449
Foam::cellMapper::insertedObjectLabels
const virtual labelList & insertedObjectLabels() const
Return list of inserted cells.
Definition: cellMapper.C:451
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::scalarListList
List< scalarList > scalarListList
Definition: scalarList.H:51
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::cellMapper::insertedCells_
bool insertedCells_
Are there any inserted (unmapped) cells.
Definition: cellMapper.H:67
Foam::mapPolyMesh::cellMap
const labelList & cellMap() const
Old cell map.
Definition: mapPolyMesh.H:431
Foam::mapPolyMesh::cellsFromCellsMap
const List< objectMap > & cellsFromCellsMap() const
Cells originating from cells.
Definition: mapPolyMesh.H:455
Foam::cellMapper::insertedCellLabelsPtr_
labelList * insertedCellLabelsPtr_
Inserted cells.
Definition: cellMapper.H:85
Foam::cellMapper::mesh_
const polyMesh & mesh_
Reference to polyMesh.
Definition: cellMapper.H:61