All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cellMapper.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-2022 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 "polyTopoChangeMap.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 " << mpm_.cellMap().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 
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_.nOldCells();
379 }
380 
381 
383 {
384  if (!direct())
385  {
387  << "Requested direct addressing for an interpolative mapper."
388  << abort(FatalError);
389  }
390 
391  if (!insertedObjects())
392  {
393  // No inserted cells. Re-use cellMap
394  return mpm_.cellMap();
395  }
396  else
397  {
398  if (!directAddrPtr_)
399  {
400  calcAddressing();
401  }
402 
403  return *directAddrPtr_;
404  }
405 }
406 
407 
409 {
410  if (direct())
411  {
413  << "Requested interpolative addressing for a direct mapper."
414  << abort(FatalError);
415  }
416 
417  if (!interpolationAddrPtr_)
418  {
419  calcAddressing();
420  }
421 
422  return *interpolationAddrPtr_;
423 }
424 
425 
427 {
428  if (direct())
429  {
431  << "Requested interpolative weights for a direct mapper."
432  << abort(FatalError);
433  }
434 
435  if (!weightsPtr_)
436  {
437  calcAddressing();
438  }
439 
440  return *weightsPtr_;
441 }
442 
443 
445 {
446  if (!insertedCellLabelsPtr_)
447  {
448  if (!insertedObjects())
449  {
450  // There are no inserted cells
451  insertedCellLabelsPtr_ = new labelList(0);
452  }
453  else
454  {
455  calcAddressing();
456  }
457  }
458 
459  return *insertedCellLabelsPtr_;
460 }
461 
462 
463 // ************************************************************************* //
const scalarField & oldCellVolumes() const
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
error FatalError
virtual label sizeBeforeMapping() const
Return size before mapping.
Definition: cellMapper.C:376
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
virtual const scalarListList & weights() const
Return interpolation weights.
Definition: cellMapper.C:426
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
label nCells() const
const List< objectMap > & cellsFromPointsMap() const
Cells inflated from points.
const List< objectMap > & cellsFromFacesMap() const
Cells inflated from faces.
const List< objectMap > & cellsFromEdgesMap() const
Cells inflated from edges.
fvMesh & mesh
const labelList & cellMap() const
Old cell map.
const List< objectMap > & cellsFromCellsMap() const
Cells originating from cells.
List< scalarList > scalarListList
Definition: scalarList.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: cellMapper.C:408
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
cellMapper(const polyTopoChangeMap &mpm)
Construct from polyTopoChangeMap.
Definition: cellMapper.C:289
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:60
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: cellMapper.C:382
Template functions to aid in the implementation of demand driven data.
virtual const labelList & insertedObjectLabels() const
Return list of inserted cells.
Definition: cellMapper.C:444
void deleteDemandDrivenData(DataPtr &dataPtr)
virtual ~cellMapper()
Destructor.
Definition: cellMapper.C:368
label nOldCells() const
Number of old cells.
virtual bool insertedObjects() const
Are there any inserted cells.
Definition: cellMapper.H:140
virtual bool direct() const
Is the mapping direct.
Definition: cellMapper.H:120