initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / meshes / pointMesh / pointMeshMapper / pointMapper.C
blob2cfd6e848cfaedfaa0181e388d80ecbc767d4d7d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "pointMapper.H"
28 #include "demandDrivenData.H"
29 #include "pointMesh.H"
30 #include "mapPolyMesh.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 void Foam::pointMapper::calcAddressing() const
39     if
40     (
41         directAddrPtr_
42      || interpolationAddrPtr_
43      || weightsPtr_
44      || insertedPointLabelsPtr_
45     )
46     {
47         FatalErrorIn("void pointMapper::calcAddressing() const")
48             << "Addressing already calculated."
49             << abort(FatalError);
50     }
52     if (direct())
53     {
54         // Direct addressing, no weights
56         directAddrPtr_ = new labelList(mpm_.pointMap());
57         labelList& directAddr = *directAddrPtr_;
59         // Not necessary to resize the list as there are no retired points
60         // directAddr.setSize(pMesh_.size());
62         insertedPointLabelsPtr_ = new labelList(pMesh_.size());
63         labelList& insertedPoints = *insertedPointLabelsPtr_;
65         label nInsertedPoints = 0;
67         forAll (directAddr, pointI)
68         {
69             if (directAddr[pointI] < 0)
70             {
71                 // Found inserted point
72                 directAddr[pointI] = 0;
73                 insertedPoints[nInsertedPoints] = pointI;
74                 nInsertedPoints++;
75             }
76         }
78         insertedPoints.setSize(nInsertedPoints);
79     }
80     else
81     {
82         // Interpolative addressing
84         interpolationAddrPtr_ = new labelListList(pMesh_.size());
85         labelListList& addr = *interpolationAddrPtr_;
87         weightsPtr_ = new scalarListList(pMesh_.size());
88         scalarListList& w = *weightsPtr_;
90         // Points created from other points (i.e. points merged into it).
91         const List<objectMap>& cfc = mpm_.pointsFromPointsMap();
93         forAll (cfc, cfcI)
94         {
95             // Get addressing
96             const labelList& mo = cfc[cfcI].masterObjects();
98             label pointI = cfc[cfcI].index();
100             if (addr[pointI].size())
101             {
102                 FatalErrorIn("void pointMapper::calcAddressing() const")
103                     << "Master point " << pointI
104                     << " mapped from points " << mo
105                     << " already destination of mapping." << abort(FatalError);
106             }
108             // Map from masters, uniform weights
109             addr[pointI] = mo;
110             w[pointI] = scalarList(mo.size(), 1.0/mo.size());
111         }
114         // Do mapped points. Note that can already be set from pointsFromPoints
115         // so check if addressing size still zero.
117         const labelList& cm = mpm_.pointMap();
119         forAll (cm, pointI)
120         {
121             if (cm[pointI] > -1 && addr[pointI].empty())
122             {
123                 // Mapped from a single point
124                 addr[pointI] = labelList(1, cm[pointI]);
125                 w[pointI] = scalarList(1, 1.0);
126             }
127         }
129         // Grab inserted points (for them the size of addressing is still zero)
131         insertedPointLabelsPtr_ = new labelList(pMesh_.size());
132         labelList& insertedPoints = *insertedPointLabelsPtr_;
134         label nInsertedPoints = 0;
136         forAll (addr, pointI)
137         {
138             if (addr[pointI].empty())
139             {
140                 // Mapped from a dummy point. Take point 0 with weight 1.
141                 addr[pointI] = labelList(1, 0);
142                 w[pointI] = scalarList(1, 1.0);
144                 insertedPoints[nInsertedPoints] = pointI;
145                 nInsertedPoints++;
146             }
147         }
149         insertedPoints.setSize(nInsertedPoints);
150     }
154 void Foam::pointMapper::clearOut()
156     deleteDemandDrivenData(directAddrPtr_);
157     deleteDemandDrivenData(interpolationAddrPtr_);
158     deleteDemandDrivenData(weightsPtr_);
159     deleteDemandDrivenData(insertedPointLabelsPtr_);
163 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
165 // Construct from components
166 Foam::pointMapper::pointMapper(const pointMesh& pMesh, const mapPolyMesh& mpm)
168     pMesh_(pMesh),
169     mpm_(mpm),
170     insertedPoints_(true),
171     direct_(false),
172     directAddrPtr_(NULL),
173     interpolationAddrPtr_(NULL),
174     weightsPtr_(NULL),
175     insertedPointLabelsPtr_(NULL)
177     // Check for possibility of direct mapping
178     if (mpm_.pointsFromPointsMap().empty())
179     {
180         direct_ = true;
181     }
182     else
183     {
184         direct_ = false;
185     }
187     // Check for inserted points
188     if (direct_ && (mpm_.pointMap().empty() || min(mpm_.pointMap()) > -1))
189     {
190         insertedPoints_ = false;
191     }
192     else
193     {
194         //Check if there are inserted points with no owner
196         // Make a copy of the point map, add the entries for points from points
197         // and check for left-overs
198         labelList cm(pMesh_.size(), -1);
200         const List<objectMap>& cfc = mpm_.pointsFromPointsMap();
202         forAll (cfc, cfcI)
203         {
204             cm[cfc[cfcI].index()] = 0;
205         }
207         if (min(cm) < 0)
208         {
209             insertedPoints_ = true;
210         }
211     }
215 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
217 Foam::pointMapper::~pointMapper()
219     clearOut();
223 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
225 Foam::label Foam::pointMapper::size() const
227     return mpm_.pointMap().size();
231 Foam::label Foam::pointMapper::sizeBeforeMapping() const
233     return mpm_.nOldPoints();
237 const Foam::unallocLabelList& Foam::pointMapper::directAddressing() const
239     if (!direct())
240     {
241         FatalErrorIn
242         (
243             "const unallocLabelList& pointMapper::directAddressing() const"
244         )   << "Requested direct addressing for an interpolative mapper."
245             << abort(FatalError);
246     }
248     if (!insertedObjects())
249     {
250         // No inserted points.  Re-use pointMap
251         return mpm_.pointMap();
252     }
253     else
254     {
255         if (!directAddrPtr_)
256         {
257             calcAddressing();
258         }
260         return *directAddrPtr_;
261     }
265 const Foam::labelListList& Foam::pointMapper::addressing() const
267     if (direct())
268     {
269         FatalErrorIn
270         (
271             "const labelListList& pointMapper::addressing() const"
272         )   << "Requested interpolative addressing for a direct mapper."
273             << abort(FatalError);
274     }
276     if (!interpolationAddrPtr_)
277     {
278         calcAddressing();
279     }
281     return *interpolationAddrPtr_;
285 const Foam::scalarListList& Foam::pointMapper::weights() const
287     if (direct())
288     {
289         FatalErrorIn
290         (
291             "const scalarListList& pointMapper::weights() const"
292         )   << "Requested interpolative weights for a direct mapper."
293             << abort(FatalError);
294     }
296     if (!weightsPtr_)
297     {
298         calcAddressing();
299     }
301     return *weightsPtr_;
305 const Foam::labelList& Foam::pointMapper::insertedObjectLabels() const
307     if (!insertedPointLabelsPtr_)
308     {
309         if (!insertedObjects())
310         {
311             // There are no inserted points
312             insertedPointLabelsPtr_ = new labelList(0);
313         }
314         else
315         {
316             calcAddressing();
317         }
318     }
320     return *insertedPointLabelsPtr_;
322         
324 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
327 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
330 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
333 // ************************************************************************* //