OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fvPatchMapper.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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "fvPatchMapper.H"
00028 #include "fvPatch.H"
00029 #include "fvBoundaryMesh.H"
00030 #include "fvMesh.H"
00031 #include "mapPolyMesh.H"
00032 #include "faceMapper.H"
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 void Foam::fvPatchMapper::calcAddressing() const
00037 {
00038     if
00039     (
00040         directAddrPtr_
00041      || interpolationAddrPtr_
00042      || weightsPtr_
00043     )
00044     {
00045         FatalErrorIn
00046         (
00047             "void fvPatchMapper::calcAddressing() const)"
00048         )   << "Addressing already calculated"
00049             << abort(FatalError);
00050     }
00051 
00052     // Mapping
00053     const label oldPatchStart =
00054         faceMap_.oldPatchStarts()[patch_.index()];
00055 
00056     const label oldPatchEnd =
00057         oldPatchStart + faceMap_.oldPatchSizes()[patch_.index()];
00058 
00059     // Assemble the maps: slice to patch
00060     if (direct())
00061     {
00062         // Direct mapping - slice to size
00063         directAddrPtr_ = new labelList
00064         (
00065             patch_.patchSlice
00066             (
00067                 static_cast<const labelList&>(faceMap_.directAddressing())
00068             )
00069         );
00070         labelList& addr = *directAddrPtr_;
00071 
00072         // Adjust mapping to manage hits into other patches and into
00073         // internal
00074         forAll (addr, faceI)
00075         {
00076             if
00077             (
00078                 addr[faceI] >= oldPatchStart
00079              && addr[faceI] < oldPatchEnd
00080             )
00081             {
00082                 addr[faceI] -= oldPatchStart;
00083             }
00084             else
00085             {
00086                 addr[faceI] = 0;
00087             }
00088         }
00089 
00090         if (fvMesh::debug)
00091         {
00092             if (min(addr) < 0)
00093             {
00094                 FatalErrorIn
00095                 (
00096                     "void fvPatchMapper::calcAddressing() const"
00097                 )   << "Error in patch mapping for patch "
00098                     << patch_.index() << " named " << patch_.name()
00099                     << abort(FatalError);
00100             }
00101         }
00102     }
00103     else
00104     {
00105         // Interpolative mapping
00106         interpolationAddrPtr_ =
00107             new labelListList
00108             (
00109                 patch_.patchSlice(faceMap_.addressing())
00110             );
00111         labelListList& addr = *interpolationAddrPtr_;
00112 
00113         weightsPtr_ =
00114             new scalarListList
00115             (
00116                 patch_.patchSlice(faceMap_.weights())
00117             );
00118         scalarListList& w = *weightsPtr_;
00119 
00120         // Adjust mapping to manage hits into other patches and into
00121         // internal
00122         forAll (addr, faceI)
00123         {
00124             labelList& curAddr = addr[faceI];
00125             scalarList& curW = w[faceI];
00126 
00127             if
00128             (
00129                 min(curAddr) >= oldPatchStart
00130              && max(curAddr) < oldPatchEnd
00131             )
00132             {
00133                 // No adjustment of weights, just subtract patch start
00134                 forAll (curAddr, i)
00135                 {
00136                     curAddr[i] -= oldPatchStart;
00137                 }
00138             }
00139             else
00140             {
00141                 // Need to recalculate weights to exclude hits into internal
00142                 labelList newAddr(curAddr.size(), false);
00143                 scalarField newWeights(curAddr.size());
00144                 label nActive = 0;
00145 
00146                 forAll (curAddr, lfI)
00147                 {
00148                     if
00149                     (
00150                         curAddr[lfI] >= oldPatchStart
00151                      && curAddr[lfI] < oldPatchEnd
00152                     )
00153                     {
00154                         newAddr[nActive] = curAddr[lfI] - oldPatchStart;
00155                         newWeights[nActive] = curW[lfI];
00156                         nActive++;
00157                     }
00158                 }
00159 
00160                 // Cater for bad mapping
00161                 if (nActive == 0)
00162                 {
00163                     newAddr[nActive] = 0;
00164                     newWeights[nActive] = 1;
00165                     nActive++;
00166                 }
00167 
00168                 newAddr.setSize(nActive);
00169                 newWeights.setSize(nActive);
00170 
00171                 // Re-scale the weights
00172                 newWeights /= sum(newWeights);
00173 
00174                 // Reset addressing and weights
00175                 curAddr = newAddr;
00176                 curW = newWeights;
00177             }
00178         }
00179 
00180         if (fvMesh::debug)
00181         {
00182             forAll (addr, i)
00183             {
00184                 if (min(addr[i]) < 0)
00185                 {
00186                     FatalErrorIn
00187                     (
00188                         "void fvPatchMapper::calcAddressing() const"
00189                     )   << "Error in patch mapping for patch "
00190                         << patch_.index() << " named " << patch_.name()
00191                         << abort(FatalError);
00192                 }
00193             }
00194         }
00195     }
00196 }
00197 
00198 
00199 void Foam::fvPatchMapper::clearOut()
00200 {
00201     deleteDemandDrivenData(directAddrPtr_);
00202     deleteDemandDrivenData(interpolationAddrPtr_);
00203     deleteDemandDrivenData(weightsPtr_);
00204 }
00205 
00206 
00207 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00208 
00209 // Construct from components
00210 Foam::fvPatchMapper::fvPatchMapper
00211 (
00212     const fvPatch& patch,
00213     const faceMapper& faceMap
00214 )
00215 :
00216     patch_(patch),
00217     faceMap_(faceMap),
00218     sizeBeforeMapping_(faceMap.oldPatchSizes()[patch_.index()]),
00219     directAddrPtr_(NULL),
00220     interpolationAddrPtr_(NULL),
00221     weightsPtr_(NULL)
00222 {}
00223 
00224 
00225 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00226 
00227 Foam::fvPatchMapper::~fvPatchMapper()
00228 {
00229     clearOut();
00230 }
00231 
00232 
00233 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00234 
00235 const Foam::unallocLabelList& Foam::fvPatchMapper::directAddressing() const
00236 {
00237     if (!direct())
00238     {
00239         FatalErrorIn
00240         (
00241             "const unallocLabelList& fvPatchMapper::directAddressing() const"
00242         )   << "Requested direct addressing for an interpolative mapper."
00243             << abort(FatalError);
00244     }
00245 
00246     if (!directAddrPtr_)
00247     {
00248         calcAddressing();
00249     }
00250 
00251     return *directAddrPtr_;
00252 }
00253 
00254 
00255 const Foam::labelListList& Foam::fvPatchMapper::addressing() const
00256 {
00257     if (direct())
00258     {
00259         FatalErrorIn
00260         (
00261             "const labelListList& fvPatchMapper::addressing() const"
00262         )   << "Requested interpolative addressing for a direct mapper."
00263             << abort(FatalError);
00264     }
00265 
00266     if (!interpolationAddrPtr_)
00267     {
00268         calcAddressing();
00269     }
00270 
00271     return *interpolationAddrPtr_;
00272 }
00273 
00274 
00275 const Foam::scalarListList& Foam::fvPatchMapper::weights() const
00276 {
00277     if (direct())
00278     {
00279         FatalErrorIn
00280         (
00281             "const scalarListList& fvPatchMapper::weights() const"
00282         )   << "Requested interpolative weights for a direct mapper."
00283             << abort(FatalError);
00284     }
00285 
00286     if (!weightsPtr_)
00287     {
00288         calcAddressing();
00289     }
00290 
00291     return *weightsPtr_;
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00296 
00297 
00298 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00299 
00300 
00301 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00302 
00303 
00304 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd