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-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 "cellMapper.H"
27 #include "demandDrivenData.H"
28 #include "polyMesh.H"
29 #include "mapPolyMesh.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 void Foam::cellMapper::calcAddressing() const
34 {
35  if
36  (
37  directAddrPtr_
38  || interpolationAddrPtr_
39  || weightsPtr_
40  || insertedCellLabelsPtr_
41  )
42  {
43  FatalErrorIn("void cellMapper::calcAddressing() const")
44  << "Addressing already calculated."
45  << abort(FatalError);
46  }
47 
48  if (direct())
49  {
50  // Direct addressing, no weights
51 
52  directAddrPtr_ = new labelList(mpm_.cellMap());
53  labelList& directAddr = *directAddrPtr_;
54 
55  // Not necessary to resize the list as there are no retired cells
56  // directAddr.setSize(mesh_.nCells());
57 
58  insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
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 
80  interpolationAddrPtr_ = new labelListList(mesh_.nCells());
81  labelListList& addr = *interpolationAddrPtr_;
82 
83  weightsPtr_ = new scalarListList(mesh_.nCells());
84  scalarListList& w = *weightsPtr_;
85 
86  const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
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  {
97  FatalErrorIn("void cellMapper::calcAddressing() const")
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  {
119  FatalErrorIn("void cellMapper::calcAddressing() const")
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  {
141  FatalErrorIn("void cellMapper::calcAddressing() const")
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  {
165  FatalErrorIn("void cellMapper::calcAddressing() const")
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  {
184  FatalErrorIn("void cellMapper::calcAddressing() const")
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 
255  insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
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 
278 void Foam::cellMapper::clearOut()
279 {
280  deleteDemandDrivenData(directAddrPtr_);
281  deleteDemandDrivenData(interpolationAddrPtr_);
282  deleteDemandDrivenData(weightsPtr_);
283  deleteDemandDrivenData(insertedCellLabelsPtr_);
284 }
285 
286 
287 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
288 
289 // Construct from components
290 Foam::cellMapper::cellMapper(const mapPolyMesh& mpm)
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  (
395  "const labelUList& cellMapper::directAddressing() const"
396  ) << "Requested direct addressing for an interpolative mapper."
397  << abort(FatalError);
398  }
399 
400  if (!insertedObjects())
401  {
402  // No inserted cells. Re-use cellMap
403  return mpm_.cellMap();
404  }
405  else
406  {
407  if (!directAddrPtr_)
408  {
409  calcAddressing();
410  }
411 
412  return *directAddrPtr_;
413  }
414 }
415 
416 
418 {
419  if (direct())
420  {
422  (
423  "const labelListList& cellMapper::addressing() const"
424  ) << "Requested interpolative addressing for a direct mapper."
425  << abort(FatalError);
426  }
427 
428  if (!interpolationAddrPtr_)
429  {
430  calcAddressing();
431  }
432 
433  return *interpolationAddrPtr_;
434 }
435 
436 
438 {
439  if (direct())
440  {
442  (
443  "const scalarListList& cellMapper::weights() const"
444  ) << "Requested interpolative weights for a direct mapper."
445  << abort(FatalError);
446  }
447 
448  if (!weightsPtr_)
449  {
450  calcAddressing();
451  }
452 
453  return *weightsPtr_;
454 }
455 
456 
458 {
459  if (!insertedCellLabelsPtr_)
460  {
461  if (!insertedObjects())
462  {
463  // There are no inserted cells
464  insertedCellLabelsPtr_ = new labelList(0);
465  }
466  else
467  {
468  calcAddressing();
469  }
470  }
471 
472  return *insertedCellLabelsPtr_;
473 }
474 
475 
476 // ************************************************************************* //
List< scalarList > scalarListList
Definition: scalarList.H:51
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: cellMapper.C:417
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
void deleteDemandDrivenData(DataPtr &dataPtr)
const List< objectMap > & cellsFromPointsMap() const
Cells inflated from points.
Definition: mapPolyMesh.H:437
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 const labelList & insertedObjectLabels() const
Return list of inserted cells.
Definition: cellMapper.C:457
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
label nCells() const
dynamicFvMesh & mesh
virtual label size() const
Return size.
Definition: cellMapper.C:377
const scalarField & oldCellVolumes() const
Definition: mapPolyMesh.H:647
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
bool hasOldCellVolumes() const
Definition: mapPolyMesh.H:642
virtual label sizeBeforeMapping() const
Return size before mapping.
Definition: cellMapper.C:383
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: cellMapper.C:389
#define forAll(list, i)
Definition: UList.H:421
Template functions to aid in the implementation of demand driven data.
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
errorManip< error > abort(error &err)
Definition: errorManip.H:131
virtual const scalarListList & weights() const
Return interpolaion weights.
Definition: cellMapper.C:437
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
const List< objectMap > & cellsFromFacesMap() const
Cells inflated from faces.
Definition: mapPolyMesh.H:449
virtual bool direct() const
Is the mapping direct.
Definition: cellMapper.H:127
const List< objectMap > & cellsFromCellsMap() const
Cells originating from cells.
Definition: mapPolyMesh.H:455
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
virtual ~cellMapper()
Destructor.
Definition: cellMapper.C:369
const List< objectMap > & cellsFromEdgesMap() const
Cells inflated from edges.
Definition: mapPolyMesh.H:443
label nOldCells() const
Number of old cells.
Definition: mapPolyMesh.H:383
virtual bool insertedObjects() const
Are there any inserted cells.
Definition: cellMapper.H:147
const labelList & cellMap() const
Old cell map.
Definition: mapPolyMesh.H:431