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

lduAddressing.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 "lduAddressing.H"
00028 #include "demandDrivenData.H"
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 void Foam::lduAddressing::calcLosort() const
00033 {
00034     if (losortPtr_)
00035     {
00036         FatalErrorIn("lduAddressing::calcLosort() const")
00037             << "losort already calculated"
00038             << abort(FatalError);
00039     }
00040 
00041     // Scan the neighbour list to find out how many times the cell
00042     // appears as a neighbour of the face. Done this way to avoid guessing
00043     // and resizing list
00044     labelList nNbrOfFace(size(), 0);
00045 
00046     const unallocLabelList& nbr = upperAddr();
00047 
00048     forAll (nbr, nbrI)
00049     {
00050         nNbrOfFace[nbr[nbrI]]++;
00051     }
00052 
00053     // Create temporary neighbour addressing
00054     labelListList cellNbrFaces(size());
00055 
00056     forAll (cellNbrFaces, cellI)
00057     {
00058         cellNbrFaces[cellI].setSize(nNbrOfFace[cellI]);
00059     }
00060 
00061     // Reset the list of number of neighbours to zero
00062     nNbrOfFace = 0;
00063 
00064     // Scatter the neighbour faces
00065     forAll (nbr, nbrI)
00066     {
00067         cellNbrFaces[nbr[nbrI]][nNbrOfFace[nbr[nbrI]]] = nbrI;
00068 
00069         nNbrOfFace[nbr[nbrI]]++;
00070     }
00071 
00072     // Gather the neighbours into the losort array
00073     losortPtr_ = new labelList(nbr.size(), -1);
00074 
00075     labelList& lst = *losortPtr_;
00076 
00077     // Set counter for losort
00078     label lstI = 0;
00079 
00080     forAll (cellNbrFaces, cellI)
00081     {
00082         const labelList& curNbr = cellNbrFaces[cellI];
00083 
00084         forAll (curNbr, curNbrI)
00085         {
00086             lst[lstI] = curNbr[curNbrI];
00087             lstI++;
00088         }
00089     }
00090 }
00091 
00092 
00093 void Foam::lduAddressing::calcOwnerStart() const
00094 {
00095     if (ownerStartPtr_)
00096     {
00097         FatalErrorIn("lduAddressing::calcOwnerStart() const")
00098             << "owner start already calculated"
00099             << abort(FatalError);
00100     }
00101 
00102     const labelList& own = lowerAddr();
00103 
00104     ownerStartPtr_ = new labelList(size() + 1, own.size());
00105 
00106     labelList& ownStart = *ownerStartPtr_;
00107 
00108     // Set up first lookup by hand
00109     ownStart[0] = 0;
00110     label nOwnStart = 0;
00111     label i = 1;
00112 
00113     forAll (own, faceI)
00114     {
00115         label curOwn = own[faceI];
00116 
00117         if (curOwn > nOwnStart)
00118         {
00119             while (i <= curOwn)
00120             {
00121                 ownStart[i++] = faceI;
00122             }
00123 
00124             nOwnStart = curOwn;
00125         }
00126     }
00127 }
00128 
00129 
00130 void Foam::lduAddressing::calcLosortStart() const
00131 {
00132     if (losortStartPtr_)
00133     {
00134         FatalErrorIn("lduAddressing::calcLosortStart() const")
00135             << "losort start already calculated"
00136             << abort(FatalError);
00137     }
00138 
00139     losortStartPtr_ = new labelList(size() + 1, 0);
00140 
00141     labelList& lsrtStart = *losortStartPtr_;
00142 
00143     const labelList& nbr = upperAddr();
00144 
00145     const labelList& lsrt = losortAddr();
00146 
00147     // Set up first lookup by hand
00148     lsrtStart[0] = 0;
00149     label nLsrtStart = 0;
00150     label i = 0;
00151 
00152     forAll (lsrt, faceI)
00153     {
00154         // Get neighbour
00155         const label curNbr = nbr[lsrt[faceI]];
00156 
00157         if (curNbr > nLsrtStart)
00158         {
00159             while (i <= curNbr)
00160             {
00161                 lsrtStart[i++] = faceI;
00162             }
00163 
00164             nLsrtStart = curNbr;
00165         }
00166     }
00167 
00168     // Set up last lookup by hand
00169     lsrtStart[size()] = nbr.size();
00170 }
00171 
00172 
00173 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00174 
00175 Foam::lduAddressing::~lduAddressing()
00176 {
00177     deleteDemandDrivenData(losortPtr_);
00178     deleteDemandDrivenData(ownerStartPtr_);
00179     deleteDemandDrivenData(losortStartPtr_);
00180 }
00181 
00182 
00183 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00184 
00185 const Foam::unallocLabelList& Foam::lduAddressing::losortAddr() const
00186 {
00187     if (!losortPtr_)
00188     {
00189         calcLosort();
00190     }
00191 
00192     return *losortPtr_;
00193 }
00194 
00195 
00196 const Foam::unallocLabelList& Foam::lduAddressing::ownerStartAddr() const
00197 {
00198     if (!ownerStartPtr_)
00199     {
00200         calcOwnerStart();
00201     }
00202 
00203     return *ownerStartPtr_;
00204 }
00205 
00206 
00207 const Foam::unallocLabelList& Foam::lduAddressing::losortStartAddr() const
00208 {
00209     if (!losortStartPtr_)
00210     {
00211         calcLosortStart();
00212     }
00213 
00214     return *losortStartPtr_;
00215 }
00216 
00217 
00218 // Return edge index given owner and neighbour label
00219 Foam::label Foam::lduAddressing::triIndex(const label a, const label b) const
00220 {
00221     label own = min(a, b);
00222 
00223     label nbr = max(a, b);
00224 
00225     label startLabel = ownerStartAddr()[own];
00226 
00227     label endLabel = ownerStartAddr()[own + 1];
00228 
00229     const unallocLabelList& neighbour = upperAddr();
00230 
00231     for (label i = startLabel; i < endLabel; i++)
00232     {
00233         if (neighbour[i] == nbr)
00234         {
00235             return i;
00236         }
00237     }
00238 
00239     // If neighbour has not been found, something has gone seriously
00240     // wrong with the addressing mechanism
00241     FatalErrorIn
00242     (
00243         "lduAddressing::triIndex(const label owner, const label nbr) const"
00244     )   << "neighbour " << nbr << " not found for owner " << own << ". "
00245         << "Problem with addressing"
00246         << abort(FatalError);
00247 
00248     return -1;
00249 }
00250 
00251 
00252 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd