FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

pointMapper.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "pointMapper.H"
00027 #include <OpenFOAM/demandDrivenData.H>
00028 #include <OpenFOAM/pointMesh.H>
00029 #include <OpenFOAM/mapPolyMesh.H>
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 void Foam::pointMapper::calcAddressing() const
00037 {
00038     if
00039     (
00040         directAddrPtr_
00041      || interpolationAddrPtr_
00042      || weightsPtr_
00043      || insertedPointLabelsPtr_
00044     )
00045     {
00046         FatalErrorIn("void pointMapper::calcAddressing() const")
00047             << "Addressing already calculated."
00048             << abort(FatalError);
00049     }
00050 
00051     if (direct())
00052     {
00053         // Direct addressing, no weights
00054 
00055         directAddrPtr_ = new labelList(mpm_.pointMap());
00056         labelList& directAddr = *directAddrPtr_;
00057 
00058         // Not necessary to resize the list as there are no retired points
00059         // directAddr.setSize(pMesh_.size());
00060 
00061         insertedPointLabelsPtr_ = new labelList(pMesh_.size());
00062         labelList& insertedPoints = *insertedPointLabelsPtr_;
00063 
00064         label nInsertedPoints = 0;
00065 
00066         forAll (directAddr, pointI)
00067         {
00068             if (directAddr[pointI] < 0)
00069             {
00070                 // Found inserted point
00071                 directAddr[pointI] = 0;
00072                 insertedPoints[nInsertedPoints] = pointI;
00073                 nInsertedPoints++;
00074             }
00075         }
00076 
00077         insertedPoints.setSize(nInsertedPoints);
00078     }
00079     else
00080     {
00081         // Interpolative addressing
00082 
00083         interpolationAddrPtr_ = new labelListList(pMesh_.size());
00084         labelListList& addr = *interpolationAddrPtr_;
00085 
00086         weightsPtr_ = new scalarListList(pMesh_.size());
00087         scalarListList& w = *weightsPtr_;
00088 
00089         // Points created from other points (i.e. points merged into it).
00090         const List<objectMap>& cfc = mpm_.pointsFromPointsMap();
00091 
00092         forAll (cfc, cfcI)
00093         {
00094             // Get addressing
00095             const labelList& mo = cfc[cfcI].masterObjects();
00096 
00097             label pointI = cfc[cfcI].index();
00098 
00099             if (addr[pointI].size())
00100             {
00101                 FatalErrorIn("void pointMapper::calcAddressing() const")
00102                     << "Master point " << pointI
00103                     << " mapped from points " << mo
00104                     << " already destination of mapping." << abort(FatalError);
00105             }
00106 
00107             // Map from masters, uniform weights
00108             addr[pointI] = mo;
00109             w[pointI] = scalarList(mo.size(), 1.0/mo.size());
00110         }
00111 
00112 
00113         // Do mapped points. Note that can already be set from pointsFromPoints
00114         // so check if addressing size still zero.
00115 
00116         const labelList& cm = mpm_.pointMap();
00117 
00118         forAll (cm, pointI)
00119         {
00120             if (cm[pointI] > -1 && addr[pointI].empty())
00121             {
00122                 // Mapped from a single point
00123                 addr[pointI] = labelList(1, cm[pointI]);
00124                 w[pointI] = scalarList(1, 1.0);
00125             }
00126         }
00127 
00128         // Grab inserted points (for them the size of addressing is still zero)
00129 
00130         insertedPointLabelsPtr_ = new labelList(pMesh_.size());
00131         labelList& insertedPoints = *insertedPointLabelsPtr_;
00132 
00133         label nInsertedPoints = 0;
00134 
00135         forAll (addr, pointI)
00136         {
00137             if (addr[pointI].empty())
00138             {
00139                 // Mapped from a dummy point. Take point 0 with weight 1.
00140                 addr[pointI] = labelList(1, 0);
00141                 w[pointI] = scalarList(1, 1.0);
00142 
00143                 insertedPoints[nInsertedPoints] = pointI;
00144                 nInsertedPoints++;
00145             }
00146         }
00147 
00148         insertedPoints.setSize(nInsertedPoints);
00149     }
00150 }
00151 
00152 
00153 void Foam::pointMapper::clearOut()
00154 {
00155     deleteDemandDrivenData(directAddrPtr_);
00156     deleteDemandDrivenData(interpolationAddrPtr_);
00157     deleteDemandDrivenData(weightsPtr_);
00158     deleteDemandDrivenData(insertedPointLabelsPtr_);
00159 }
00160 
00161 
00162 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00163 
00164 // Construct from components
00165 Foam::pointMapper::pointMapper(const pointMesh& pMesh, const mapPolyMesh& mpm)
00166 :
00167     pMesh_(pMesh),
00168     mpm_(mpm),
00169     insertedPoints_(true),
00170     direct_(false),
00171     directAddrPtr_(NULL),
00172     interpolationAddrPtr_(NULL),
00173     weightsPtr_(NULL),
00174     insertedPointLabelsPtr_(NULL)
00175 {
00176     // Check for possibility of direct mapping
00177     if (mpm_.pointsFromPointsMap().empty())
00178     {
00179         direct_ = true;
00180     }
00181     else
00182     {
00183         direct_ = false;
00184     }
00185 
00186     // Check for inserted points
00187     if (direct_ && (mpm_.pointMap().empty() || min(mpm_.pointMap()) > -1))
00188     {
00189         insertedPoints_ = false;
00190     }
00191     else
00192     {
00193         //Check if there are inserted points with no owner
00194 
00195         // Make a copy of the point map, add the entries for points from points
00196         // and check for left-overs
00197         labelList cm(pMesh_.size(), -1);
00198 
00199         const List<objectMap>& cfc = mpm_.pointsFromPointsMap();
00200 
00201         forAll (cfc, cfcI)
00202         {
00203             cm[cfc[cfcI].index()] = 0;
00204         }
00205 
00206         if (min(cm) < 0)
00207         {
00208             insertedPoints_ = true;
00209         }
00210     }
00211 }
00212 
00213 
00214 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00215 
00216 Foam::pointMapper::~pointMapper()
00217 {
00218     clearOut();
00219 }
00220 
00221 
00222 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00223 
00224 Foam::label Foam::pointMapper::size() const
00225 {
00226     return mpm_.pointMap().size();
00227 }
00228 
00229 
00230 Foam::label Foam::pointMapper::sizeBeforeMapping() const
00231 {
00232     return mpm_.nOldPoints();
00233 }
00234 
00235 
00236 const Foam::unallocLabelList& Foam::pointMapper::directAddressing() const
00237 {
00238     if (!direct())
00239     {
00240         FatalErrorIn
00241         (
00242             "const unallocLabelList& pointMapper::directAddressing() const"
00243         )   << "Requested direct addressing for an interpolative mapper."
00244             << abort(FatalError);
00245     }
00246 
00247     if (!insertedObjects())
00248     {
00249         // No inserted points.  Re-use pointMap
00250         return mpm_.pointMap();
00251     }
00252     else
00253     {
00254         if (!directAddrPtr_)
00255         {
00256             calcAddressing();
00257         }
00258 
00259         return *directAddrPtr_;
00260     }
00261 }
00262 
00263 
00264 const Foam::labelListList& Foam::pointMapper::addressing() const
00265 {
00266     if (direct())
00267     {
00268         FatalErrorIn
00269         (
00270             "const labelListList& pointMapper::addressing() const"
00271         )   << "Requested interpolative addressing for a direct mapper."
00272             << abort(FatalError);
00273     }
00274 
00275     if (!interpolationAddrPtr_)
00276     {
00277         calcAddressing();
00278     }
00279 
00280     return *interpolationAddrPtr_;
00281 }
00282 
00283 
00284 const Foam::scalarListList& Foam::pointMapper::weights() const
00285 {
00286     if (direct())
00287     {
00288         FatalErrorIn
00289         (
00290             "const scalarListList& pointMapper::weights() const"
00291         )   << "Requested interpolative weights for a direct mapper."
00292             << abort(FatalError);
00293     }
00294 
00295     if (!weightsPtr_)
00296     {
00297         calcAddressing();
00298     }
00299 
00300     return *weightsPtr_;
00301 }
00302 
00303 
00304 const Foam::labelList& Foam::pointMapper::insertedObjectLabels() const
00305 {
00306     if (!insertedPointLabelsPtr_)
00307     {
00308         if (!insertedObjects())
00309         {
00310             // There are no inserted points
00311             insertedPointLabelsPtr_ = new labelList(0);
00312         }
00313         else
00314         {
00315             calcAddressing();
00316         }
00317     }
00318 
00319     return *insertedPointLabelsPtr_;
00320 }
00321         
00322 
00323 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00324 
00325 
00326 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00327 
00328 
00329 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00330 
00331 
00332 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines