Report patch name instead of index in debug
[foam-extend-3.2.git] / src / foam / meshes / polyMesh / mapPolyMesh / cellMapper / cellMapper.C
blob3886ffb2581dd11fe5cbf4f3edce8585c7b78ef1
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cellMapper.H"
27 #include "demandDrivenData.H"
28 #include "polyMesh.H"
29 #include "mapPolyMesh.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
36 void Foam::cellMapper::calcAddressing() const
38     if
39     (
40         directAddrPtr_
41      || interpolationAddrPtr_
42      || weightsPtr_
43      || insertedCellLabelsPtr_
44     )
45     {
46         FatalErrorIn("void cellMapper::calcAddressing() const")
47             << "Addressing already calculated."
48             << abort(FatalError);
49     }
51     if (direct())
52     {
53         // Direct addressing, no weights
55         directAddrPtr_ = new labelList(mpm_.cellMap());
56         labelList& directAddr = *directAddrPtr_;
58         // Not necessary to resize the list as there are no retired cells
59         // directAddr.setSize(mesh_.nCells());
61         insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
62         labelList& insertedCells = *insertedCellLabelsPtr_;
64         label nInsertedCells = 0;
66         forAll (directAddr, cellI)
67         {
68             if (directAddr[cellI] < 0)
69             {
70                 // Found inserted cell
71                 directAddr[cellI] = 0;
72                 insertedCells[nInsertedCells] = cellI;
73                 nInsertedCells++;
74             }
75         }
77         insertedCells.setSize(nInsertedCells);
78     }
79     else
80     {
81         // Interpolative addressing
83         interpolationAddrPtr_ = new labelListList(mesh_.nCells());
84         labelListList& addr = *interpolationAddrPtr_;
86         weightsPtr_ = new scalarListList(mesh_.nCells());
87         scalarListList& w = *weightsPtr_;
89         const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
91         forAll (cfp, cfpI)
92         {
93             // Get addressing
94             const labelList& mo = cfp[cfpI].masterObjects();
96             label cellI = cfp[cfpI].index();
98             if (addr[cellI].size())
99             {
100                 FatalErrorIn("void cellMapper::calcAddressing() const")
101                     << "Master cell " << cellI
102                     << " mapped from point cells " << mo
103                     << " already destination of mapping." << abort(FatalError);
104             }
106             // Map from masters, uniform weights
107             addr[cellI] = mo;
108             w[cellI] = scalarList(mo.size(), 1.0/mo.size());
109         }
111         const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
113         forAll (cfe, cfeI)
114         {
115             // Get addressing
116             const labelList& mo = cfe[cfeI].masterObjects();
118             label cellI = cfe[cfeI].index();
120             if (addr[cellI].size())
121             {
122                 FatalErrorIn("void cellMapper::calcAddressing() const")
123                     << "Master cell " << cellI
124                     << " mapped from edge cells " << mo
125                     << " already destination of mapping." << abort(FatalError);
126             }
128             // Map from masters, uniform weights
129             addr[cellI] = mo;
130             w[cellI] = scalarList(mo.size(), 1.0/mo.size());
131         }
133         const List<objectMap>& cff = mpm_.cellsFromFacesMap();
135         forAll (cff, cffI)
136         {
137             // Get addressing
138             const labelList& mo = cff[cffI].masterObjects();
140             label cellI = cff[cffI].index();
142             if (addr[cellI].size())
143             {
144                 FatalErrorIn("void cellMapper::calcAddressing() const")
145                     << "Master cell " << cellI
146                     << " mapped from face cells " << mo
147                     << " already destination of mapping." << abort(FatalError);
148             }
150             // Map from masters, uniform weights
151             addr[cellI] = mo;
152             w[cellI] = scalarList(mo.size(), 1.0/mo.size());
153         }
155         const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
157         forAll (cfc, cfcI)
158         {
159             // Get addressing
160             const labelList& mo = cfc[cfcI].masterObjects();
162             label cellI = cfc[cfcI].index();
164             if (addr[cellI].size())
165             {
166                 FatalErrorIn("void cellMapper::calcAddressing() const")
167                     << "Master cell " << cellI
168                     << " mapped from cell cells " << mo
169                     << " already destination of mapping." << abort(FatalError);
170             }
172             // Map from masters, uniform weights
173             addr[cellI] = mo;
174             w[cellI] = scalarList(mo.size(), 1.0/mo.size());
175         }
178         // Do mapped faces. Note that can already be set from cellsFromCells
179         // so check if addressing size still zero.
181         const labelList& cm = mpm_.cellMap();
183         forAll (cm, cellI)
184         {
185             if (cm[cellI] > -1 && addr[cellI].empty())
186             {
187                 // Mapped from a single cell
188                 addr[cellI] = labelList(1, cm[cellI]);
189                 w[cellI] = scalarList(1, 1.0);
190             }
191         }
193         // Grab inserted points (for them the size of addressing is still zero)
195         insertedCellLabelsPtr_ = new labelList(mesh_.nCells());
196         labelList& insertedCells = *insertedCellLabelsPtr_;
198         label nInsertedCells = 0;
200         forAll (addr, cellI)
201         {
202             if (addr[cellI].empty())
203             {
204                 // Mapped from a dummy cell
205                 addr[cellI] = labelList(1, 0);
206                 w[cellI] = scalarList(1, 1.0);
208                 insertedCells[nInsertedCells] = cellI;
209                 nInsertedCells++;
210             }
211         }
213         insertedCells.setSize(nInsertedCells);
214     }
218 void Foam::cellMapper::clearOut()
220     deleteDemandDrivenData(directAddrPtr_);
221     deleteDemandDrivenData(interpolationAddrPtr_);
222     deleteDemandDrivenData(weightsPtr_);
223     deleteDemandDrivenData(insertedCellLabelsPtr_);
227 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
229 // Construct from components
230 Foam::cellMapper::cellMapper(const mapPolyMesh& mpm)
232     mesh_(mpm.mesh()),
233     mpm_(mpm),
234     insertedCells_(true),
235     direct_(false),
236     directAddrPtr_(NULL),
237     interpolationAddrPtr_(NULL),
238     weightsPtr_(NULL),
239     insertedCellLabelsPtr_(NULL)
241     // Check for possibility of direct mapping
242     if
243     (
244         mpm_.cellsFromPointsMap().empty()
245      && mpm_.cellsFromEdgesMap().empty()
246      && mpm_.cellsFromFacesMap().empty()
247      && mpm_.cellsFromCellsMap().empty()
248     )
249     {
250         direct_ = true;
251     }
252     else
253     {
254         direct_ = false;
255     }
257     // Check for inserted cells
258     // HJ, different criterion in faces: this is due to the fact that
259     // I can retire faces but cannot retire cells
260     // HJ, 5/Sep/2007
261     if (direct_ && (mpm_.cellMap().size() == 0 || min(mpm_.cellMap()) > -1))
262     {
263         insertedCells_ = false;
264     }
265     else
266     {
267         // Need to check all 3 lists to see if there are inserted cells
268         // with no owner
270         // Make a copy of the cell map, add the entried for cells from points,
271         // cells from edges and cells from faces and check for left-overs
272         labelList cm(mesh_.nCells(), -1);
274         const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
276         forAll (cfp, cfpI)
277         {
278             cm[cfp[cfpI].index()] = 0;
279         }
281         const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
283         forAll (cfe, cfeI)
284         {
285             cm[cfe[cfeI].index()] = 0;
286         }
288         const List<objectMap>& cff = mpm_.cellsFromFacesMap();
290         forAll (cff, cffI)
291         {
292             cm[cff[cffI].index()] = 0;
293         }
295         const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
297         forAll (cfc, cfcI)
298         {
299             cm[cfc[cfcI].index()] = 0;
300         }
302         if (min(cm) < 0)
303         {
304             insertedCells_ = true;
305         }
306     }
310 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
312 Foam::cellMapper::~cellMapper()
314     clearOut();
318 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
320 Foam::label Foam::cellMapper::size() const
322     return mpm_.cellMap().size();
326 Foam::label Foam::cellMapper::sizeBeforeMapping() const
328     return mpm_.nOldCells();
332 const Foam::unallocLabelList& Foam::cellMapper::directAddressing() const
334     if (!direct())
335     {
336         FatalErrorIn
337         (
338             "const unallocLabelList& cellMapper::directAddressing() const"
339         )   << "Requested direct addressing for an interpolative mapper."
340             << abort(FatalError);
341     }
343     if (!insertedObjects())
344     {
345         // No inserted cells.  Re-use cellMap
346         return mpm_.cellMap();
347     }
348     else
349     {
350         if (!directAddrPtr_)
351         {
352             calcAddressing();
353         }
355         return *directAddrPtr_;
356     }
360 const Foam::labelListList& Foam::cellMapper::addressing() const
362     if (direct())
363     {
364         FatalErrorIn
365         (
366             "const labelListList& cellMapper::addressing() const"
367         )   << "Requested interpolative addressing for a direct mapper."
368             << abort(FatalError);
369     }
371     if (!interpolationAddrPtr_)
372     {
373         calcAddressing();
374     }
376     return *interpolationAddrPtr_;
380 const Foam::scalarListList& Foam::cellMapper::weights() const
382     if (direct())
383     {
384         FatalErrorIn
385         (
386             "const scalarListList& cellMapper::weights() const"
387         )   << "Requested interpolative weights for a direct mapper."
388             << abort(FatalError);
389     }
391     if (!weightsPtr_)
392     {
393         calcAddressing();
394     }
396     return *weightsPtr_;
400 const Foam::labelList& Foam::cellMapper::insertedObjectLabels() const
402     if (!insertedCellLabelsPtr_)
403     {
404         if (!insertedObjects())
405         {
406             // There are no inserted cells
407             insertedCellLabelsPtr_ = new labelList(0);
408         }
409         else
410         {
411             calcAddressing();
412         }
413     }
415     return *insertedCellLabelsPtr_;
419 // ************************************************************************* //