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-2024 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 "polyMesh.H"
28 #include "polyTopoChangeMap.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 void Foam::cellMapper::calcAddressing() const
33 {
34  if
35  (
36  directAddrPtr_
37  || interpolationAddrPtr_
38  || weightsPtr_
39  || insertedCellLabelsPtr_
40  )
41  {
43  << "Addressing already calculated."
44  << abort(FatalError);
45  }
46 
47  if (direct())
48  {
49  // Direct addressing, no weights
50 
51  directAddrPtr_ = new labelList(mpm_.cellMap());
52  labelList& directAddr = *directAddrPtr_;
53 
54  // Not necessary to resize the list as there are no retired cells
55  // directAddr.setSize(mesh_.nCells());
56 
57  insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
58  labelList& insertedCells = *insertedCellLabelsPtr_;
59 
60  label nInsertedCells = 0;
61 
62  forAll(directAddr, celli)
63  {
64  if (directAddr[celli] < 0)
65  {
66  // Found inserted cell
67  directAddr[celli] = 0;
68  insertedCells[nInsertedCells] = celli;
69  nInsertedCells++;
70  }
71  }
72 
73  insertedCells.setSize(nInsertedCells);
74  }
75  else
76  {
77  // Interpolative addressing
78 
79  interpolationAddrPtr_ = new labelListList(mesh_.nCells());
80  labelListList& addr = *interpolationAddrPtr_;
81 
82  weightsPtr_ = new scalarListList(mesh_.nCells());
83  scalarListList& w = *weightsPtr_;
84 
85  // Volume conservative mapping if possible
86 
87  const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
88 
89  forAll(cfc, cfcI)
90  {
91  // Get addressing
92  const labelList& mo = cfc[cfcI].masterObjects();
93 
94  label celli = cfc[cfcI].index();
95 
96  if (addr[celli].size())
97  {
99  << "Master cell " << celli
100  << " mapped from cell cells " << mo
101  << " already destination of mapping."
102  << abort(FatalError);
103  }
104 
105  // Map from masters
106  addr[celli] = mo;
107  }
108 
109  if (mpm_.hasOldCellVolumes())
110  {
111  // Volume weighted
112 
113  const scalarField& V = mpm_.oldCellVolumes();
114 
115  if (V.size() != sizeBeforeMapping())
116  {
118  << "cellVolumes size " << V.size()
119  << " is not the old number of cells " << sizeBeforeMapping()
120  << ". Are your cellVolumes already mapped?"
121  << " (new number of cells " << mpm_.cellMap().size() << ")"
122  << abort(FatalError);
123  }
124 
125  forAll(cfc, cfcI)
126  {
127  const labelList& mo = cfc[cfcI].masterObjects();
128 
129  label celli = cfc[cfcI].index();
130 
131  w[celli].setSize(mo.size());
132 
133  if (mo.size())
134  {
135  scalar sumV = 0;
136  forAll(mo, ci)
137  {
138  w[celli][ci] = V[mo[ci]];
139  sumV += V[mo[ci]];
140  }
141  if (sumV > vSmall)
142  {
143  forAll(mo, ci)
144  {
145  w[celli][ci] /= sumV;
146  }
147  }
148  else
149  {
150  // Exception: zero volume. Use uniform mapping
151  w[celli] = scalarList(mo.size(), 1.0/mo.size());
152  }
153  }
154  }
155  }
156  else
157  {
158  // Uniform weighted
159 
160  forAll(cfc, cfcI)
161  {
162  const labelList& mo = cfc[cfcI].masterObjects();
163 
164  label celli = cfc[cfcI].index();
165 
166  w[celli] = scalarList(mo.size(), 1.0/mo.size());
167  }
168  }
169 
170 
171  // Do mapped faces. Note that can already be set from cellsFromCells
172  // so check if addressing size still zero.
173 
174  const labelList& cm = mpm_.cellMap();
175 
176  forAll(cm, celli)
177  {
178  if (cm[celli] > -1 && addr[celli].empty())
179  {
180  // Mapped from a single cell
181  addr[celli] = labelList(1, cm[celli]);
182  w[celli] = scalarList(1, 1.0);
183  }
184  }
185 
186  // Grab inserted points (for them the size of addressing is still zero)
187 
188  insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
189  labelList& insertedCells = *insertedCellLabelsPtr_;
190 
191  label nInsertedCells = 0;
192 
193  forAll(addr, celli)
194  {
195  if (addr[celli].empty())
196  {
198  << "No interpolative addressing provided for cell " << celli
199  << abort(FatalError);
200  }
201  }
202 
203  insertedCells.setSize(nInsertedCells);
204  }
205 }
206 
207 
208 void Foam::cellMapper::clearOut()
209 {
210  deleteDemandDrivenData(directAddrPtr_);
211  deleteDemandDrivenData(interpolationAddrPtr_);
212  deleteDemandDrivenData(weightsPtr_);
213  deleteDemandDrivenData(insertedCellLabelsPtr_);
214 }
215 
216 
217 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
218 
220 :
221  mesh_(mpm.mesh()),
222  mpm_(mpm),
223  insertedCells_(true),
224  direct_(false),
225  directAddrPtr_(nullptr),
226  interpolationAddrPtr_(nullptr),
227  weightsPtr_(nullptr),
228  insertedCellLabelsPtr_(nullptr)
229 {
230  // Check for possibility of direct mapping
231  if (mpm_.cellsFromCellsMap().empty())
232  {
233  direct_ = true;
234  }
235  else
236  {
237  direct_ = false;
238  }
239 
240  // Check for inserted cells
241  if (direct_ && (mpm_.cellMap().empty() || min(mpm_.cellMap()) > -1))
242  {
243  insertedCells_ = false;
244  }
245  else
246  {
247  // Need to check all 3 lists to see if there are inserted cells
248  // with no owner
249 
250  // Make a copy of the cell map, add the entried for cells from points,
251  // cells from edges and cells from faces and check for left-overs
252  labelList cm(mesh_.nCells(), -1);
253 
254  const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
255 
256  forAll(cfc, cfcI)
257  {
258  cm[cfc[cfcI].index()] = 0;
259  }
260 
261  if (min(cm) < 0)
262  {
263  insertedCells_ = true;
264  }
265  }
266 }
267 
268 
269 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
270 
272 {
273  clearOut();
274 }
275 
276 
277 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
278 
280 {
281  if (!direct())
282  {
284  << "Requested direct addressing for an interpolative mapper."
285  << abort(FatalError);
286  }
287 
288  if (!insertedObjects())
289  {
290  // No inserted cells. Reuse cellMap
291  return mpm_.cellMap();
292  }
293  else
294  {
295  if (!directAddrPtr_)
296  {
297  calcAddressing();
298  }
299 
300  return *directAddrPtr_;
301  }
302 }
303 
304 
306 {
307  if (direct())
308  {
310  << "Requested interpolative addressing for a direct mapper."
311  << abort(FatalError);
312  }
313 
314  if (!interpolationAddrPtr_)
315  {
316  calcAddressing();
317  }
318 
319  return *interpolationAddrPtr_;
320 }
321 
322 
324 {
325  if (direct())
326  {
328  << "Requested interpolative weights for a direct mapper."
329  << abort(FatalError);
330  }
331 
332  if (!weightsPtr_)
333  {
334  calcAddressing();
335  }
336 
337  return *weightsPtr_;
338 }
339 
340 
342 {
343  return mpm_.nOldCells();
344 }
345 
346 
348 {
349  if (!insertedCellLabelsPtr_)
350  {
351  if (!insertedObjects())
352  {
353  // There are no inserted cells
354  insertedCellLabelsPtr_ = new labelList(0);
355  }
356  else
357  {
358  calcAddressing();
359  }
360  }
361 
362  return *insertedCellLabelsPtr_;
363 }
364 
365 
366 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
cellMapper(const polyTopoChangeMap &mpm)
Construct from polyTopoChangeMap.
Definition: cellMapper.C:219
virtual ~cellMapper()
Destructor.
Definition: cellMapper.C:271
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: cellMapper.C:305
virtual const scalarListList & weights() const
Return interpolation weights.
Definition: cellMapper.C:323
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: cellMapper.C:279
virtual const labelList & insertedObjectLabels() const
Return list of inserted cells.
Definition: cellMapper.C:347
virtual label sizeBeforeMapping() const
Return size before mapping.
Definition: cellMapper.C:341
virtual bool direct() const
Is the mapping direct.
Definition: cellMapper.H:117
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const labelList & cellMap() const
Old cell map.
const scalarField & oldCellVolumes() const
const List< objectMap > & cellsFromCellsMap() const
Cells originating from cells.
label nCells() const
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
List< scalarList > scalarListList
Definition: scalarList.H:51
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
void deleteDemandDrivenData(DataType *&dataPtr)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
error FatalError