1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cellMapper.H"
27 #include "demandDrivenData.H"
29 #include "mapPolyMesh.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 void Foam::cellMapper::calcAddressing() const
41 || interpolationAddrPtr_
43 || insertedCellLabelsPtr_
46 FatalErrorIn("void cellMapper::calcAddressing() const")
47 << "Addressing already calculated."
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)
68 if (directAddr[cellI] < 0)
70 // Found inserted cell
71 directAddr[cellI] = 0;
72 insertedCells[nInsertedCells] = cellI;
77 insertedCells.setSize(nInsertedCells);
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();
94 const labelList& mo = cfp[cfpI].masterObjects();
96 label cellI = cfp[cfpI].index();
98 if (addr[cellI].size())
100 FatalErrorIn("void cellMapper::calcAddressing() const")
101 << "Master cell " << cellI
102 << " mapped from point cells " << mo
103 << " already destination of mapping." << abort(FatalError);
106 // Map from masters, uniform weights
108 w[cellI] = scalarList(mo.size(), 1.0/mo.size());
111 const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
116 const labelList& mo = cfe[cfeI].masterObjects();
118 label cellI = cfe[cfeI].index();
120 if (addr[cellI].size())
122 FatalErrorIn("void cellMapper::calcAddressing() const")
123 << "Master cell " << cellI
124 << " mapped from edge cells " << mo
125 << " already destination of mapping." << abort(FatalError);
128 // Map from masters, uniform weights
130 w[cellI] = scalarList(mo.size(), 1.0/mo.size());
133 const List<objectMap>& cff = mpm_.cellsFromFacesMap();
138 const labelList& mo = cff[cffI].masterObjects();
140 label cellI = cff[cffI].index();
142 if (addr[cellI].size())
144 FatalErrorIn("void cellMapper::calcAddressing() const")
145 << "Master cell " << cellI
146 << " mapped from face cells " << mo
147 << " already destination of mapping." << abort(FatalError);
150 // Map from masters, uniform weights
152 w[cellI] = scalarList(mo.size(), 1.0/mo.size());
155 const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
160 const labelList& mo = cfc[cfcI].masterObjects();
162 label cellI = cfc[cfcI].index();
164 if (addr[cellI].size())
166 FatalErrorIn("void cellMapper::calcAddressing() const")
167 << "Master cell " << cellI
168 << " mapped from cell cells " << mo
169 << " already destination of mapping." << abort(FatalError);
172 // Map from masters, uniform weights
174 w[cellI] = scalarList(mo.size(), 1.0/mo.size());
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();
185 if (cm[cellI] > -1 && addr[cellI].empty())
187 // Mapped from a single cell
188 addr[cellI] = labelList(1, cm[cellI]);
189 w[cellI] = scalarList(1, 1.0);
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;
202 if (addr[cellI].empty())
204 // Mapped from a dummy cell
205 addr[cellI] = labelList(1, 0);
206 w[cellI] = scalarList(1, 1.0);
208 insertedCells[nInsertedCells] = cellI;
213 insertedCells.setSize(nInsertedCells);
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)
234 insertedCells_(true),
236 directAddrPtr_(NULL),
237 interpolationAddrPtr_(NULL),
239 insertedCellLabelsPtr_(NULL)
241 // Check for possibility of direct mapping
244 mpm_.cellsFromPointsMap().empty()
245 && mpm_.cellsFromEdgesMap().empty()
246 && mpm_.cellsFromFacesMap().empty()
247 && mpm_.cellsFromCellsMap().empty()
257 // Check for inserted cells
258 if (direct_ && (mpm_.cellMap().empty() || min(mpm_.cellMap()) > -1))
260 insertedCells_ = false;
264 // Need to check all 3 lists to see if there are inserted cells
267 // Make a copy of the cell map, add the entried for cells from points,
268 // cells from edges and cells from faces and check for left-overs
269 labelList cm(mesh_.nCells(), -1);
271 const List<objectMap>& cfp = mpm_.cellsFromPointsMap();
275 cm[cfp[cfpI].index()] = 0;
278 const List<objectMap>& cfe = mpm_.cellsFromEdgesMap();
282 cm[cfe[cfeI].index()] = 0;
285 const List<objectMap>& cff = mpm_.cellsFromFacesMap();
289 cm[cff[cffI].index()] = 0;
292 const List<objectMap>& cfc = mpm_.cellsFromCellsMap();
296 cm[cfc[cfcI].index()] = 0;
301 insertedCells_ = true;
307 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
309 Foam::cellMapper::~cellMapper()
315 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
317 Foam::label Foam::cellMapper::size() const
319 return mpm_.cellMap().size();
323 Foam::label Foam::cellMapper::sizeBeforeMapping() const
325 return mpm_.nOldCells();
329 const Foam::labelUList& Foam::cellMapper::directAddressing() const
335 "const labelUList& cellMapper::directAddressing() const"
336 ) << "Requested direct addressing for an interpolative mapper."
337 << abort(FatalError);
340 if (!insertedObjects())
342 // No inserted cells. Re-use cellMap
343 return mpm_.cellMap();
352 return *directAddrPtr_;
357 const Foam::labelListList& Foam::cellMapper::addressing() const
363 "const labelListList& cellMapper::addressing() const"
364 ) << "Requested interpolative addressing for a direct mapper."
365 << abort(FatalError);
368 if (!interpolationAddrPtr_)
373 return *interpolationAddrPtr_;
377 const Foam::scalarListList& Foam::cellMapper::weights() const
383 "const scalarListList& cellMapper::weights() const"
384 ) << "Requested interpolative weights for a direct mapper."
385 << abort(FatalError);
397 const Foam::labelList& Foam::cellMapper::insertedObjectLabels() const
399 if (!insertedCellLabelsPtr_)
401 if (!insertedObjects())
403 // There are no inserted cells
404 insertedCellLabelsPtr_ = new labelList(0);
412 return *insertedCellLabelsPtr_;
416 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
419 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
422 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
425 // ************************************************************************* //