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-2016 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  {
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  {
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 
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 Foam::cellMapper::cellMapper(const mapPolyMesh& mpm)
290 :
291  mesh_(mpm.mesh()),
292  mpm_(mpm),
293  insertedCells_(true),
294  direct_(false),
295  directAddrPtr_(nullptr),
296  interpolationAddrPtr_(nullptr),
297  weightsPtr_(nullptr),
298  insertedCellLabelsPtr_(nullptr)
299 {
300  // Check for possibility of direct mapping
301  if
302  (
303  mpm_.cellsFromPointsMap().empty()
304  && mpm_.cellsFromEdgesMap().empty()
305  && mpm_.cellsFromFacesMap().empty()
306  && mpm_.cellsFromCellsMap().empty()
307  )
308  {
309  direct_ = true;
310  }
311  else
312  {
313  direct_ = false;
314  }
315 
316  // Check for inserted cells
317  if (direct_ && (mpm_.cellMap().empty() || min(mpm_.cellMap()) > -1))
318  {
319  insertedCells_ = false;
320  }
321  else
322  {
323  // Need to check all 3 lists to see if there are inserted cells
324  // with no owner
325 
326  // Make a copy of the cell map, add the entried for cells from points,
327  // cells from edges and cells from faces and check for left-overs
328  labelList cm(mesh_.nCells(), -1);
329 
330  const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
331 
332  forAll(cfp, cfpI)
333  {
334  cm[cfp[cfpI].index()] = 0;
335  }
336 
337  const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
338 
339  forAll(cfe, cfeI)
340  {
341  cm[cfe[cfeI].index()] = 0;
342  }
343 
344  const List<objectMap>& cff = mpm_.cellsFromFacesMap();
345 
346  forAll(cff, cffI)
347  {
348  cm[cff[cffI].index()] = 0;
349  }
350 
351  const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
352 
353  forAll(cfc, cfcI)
354  {
355  cm[cfc[cfcI].index()] = 0;
356  }
357 
358  if (min(cm) < 0)
359  {
360  insertedCells_ = true;
361  }
362  }
363 }
364 
365 
366 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
367 
369 {
370  clearOut();
371 }
372 
373 
374 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
375 
377 {
378  return mpm_.cellMap().size();
379 }
380 
381 
383 {
384  return mpm_.nOldCells();
385 }
386 
387 
389 {
390  if (!direct())
391  {
393  << "Requested direct addressing for an interpolative mapper."
394  << abort(FatalError);
395  }
396 
397  if (!insertedObjects())
398  {
399  // No inserted cells. Re-use cellMap
400  return mpm_.cellMap();
401  }
402  else
403  {
404  if (!directAddrPtr_)
405  {
406  calcAddressing();
407  }
408 
409  return *directAddrPtr_;
410  }
411 }
412 
413 
415 {
416  if (direct())
417  {
419  << "Requested interpolative addressing for a direct mapper."
420  << abort(FatalError);
421  }
422 
423  if (!interpolationAddrPtr_)
424  {
425  calcAddressing();
426  }
427 
428  return *interpolationAddrPtr_;
429 }
430 
431 
433 {
434  if (direct())
435  {
437  << "Requested interpolative weights for a direct mapper."
438  << abort(FatalError);
439  }
440 
441  if (!weightsPtr_)
442  {
443  calcAddressing();
444  }
445 
446  return *weightsPtr_;
447 }
448 
449 
451 {
452  if (!insertedCellLabelsPtr_)
453  {
454  if (!insertedObjects())
455  {
456  // There are no inserted cells
457  insertedCellLabelsPtr_ = new labelList(0);
458  }
459  else
460  {
461  calcAddressing();
462  }
463  }
464 
465  return *insertedCellLabelsPtr_;
466 }
467 
468 
469 // ************************************************************************* //
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
label nOldCells() const
Number of old cells.
Definition: mapPolyMesh.H:381
const labelList & cellMap() const
Old cell map.
Definition: mapPolyMesh.H:429
error FatalError
virtual label sizeBeforeMapping() const
Return size before mapping.
Definition: cellMapper.C:382
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual const scalarListList & weights() const
Return interpolaion weights.
Definition: cellMapper.C:432
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
label nCells() const
const List< objectMap > & cellsFromCellsMap() const
Cells originating from cells.
Definition: mapPolyMesh.H:453
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
bool hasOldCellVolumes() const
Definition: mapPolyMesh.H:640
dynamicFvMesh & mesh
List< scalarList > scalarListList
Definition: scalarList.H:51
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: cellMapper.C:414
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
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
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: cellMapper.C:388
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Template functions to aid in the implementation of demand driven data.
const List< objectMap > & cellsFromFacesMap() const
Cells inflated from faces.
Definition: mapPolyMesh.H:447
const scalarField & oldCellVolumes() const
Definition: mapPolyMesh.H:645
virtual label size() const
Return size.
Definition: cellMapper.C:376
const List< objectMap > & cellsFromEdgesMap() const
Cells inflated from edges.
Definition: mapPolyMesh.H:441
virtual const labelList & insertedObjectLabels() const
Return list of inserted cells.
Definition: cellMapper.C:450
const List< objectMap > & cellsFromPointsMap() const
Cells inflated from points.
Definition: mapPolyMesh.H:435
void deleteDemandDrivenData(DataPtr &dataPtr)
virtual ~cellMapper()
Destructor.
Definition: cellMapper.C:368
virtual bool insertedObjects() const
Are there any inserted cells.
Definition: cellMapper.H:147
virtual bool direct() const
Is the mapping direct.
Definition: cellMapper.H:127