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

cell.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 "cell.H"
00028 #include "pyramidPointFaceRef.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 const char* const Foam::cell::typeName = "cell";
00033 
00034 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00035 
00036 Foam::labelList Foam::cell::labels(const unallocFaceList& f) const
00037 {
00038     // return the unordered list of vertex labels supporting the cell
00039 
00040     // count the maximum size of all vertices
00041     label maxVert = 0;
00042 
00043     const labelList& faces = *this;
00044 
00045     forAll (faces, faceI)
00046     {
00047         maxVert += f[faces[faceI]].size();
00048     }
00049 
00050     // set the fill-in list
00051     labelList p(maxVert);
00052 
00053     // in the first face there is no duplicates
00054     const labelList& first = f[faces[0]];
00055 
00056     forAll (first, pointI)
00057     {
00058         p[pointI] = first[pointI];
00059     }
00060 
00061     // re-use maxVert to count the real vertices
00062     maxVert = first.size();
00063 
00064     // go through the rest of the faces. For each vertex, check if the point is
00065     // already inserted (up to maxVert, which now carries the number of real
00066     // points. If not, add it at the end of the list.
00067     for (label faceI = 1; faceI < faces.size(); faceI++)
00068     {
00069         const labelList& curFace = f[faces[faceI]];
00070 
00071         forAll (curFace, pointI)
00072         {
00073             const label curPoint = curFace[pointI];
00074 
00075             bool found = false;
00076 
00077             for (register label checkI = 0; checkI < maxVert; checkI++)
00078             {
00079                 if (curPoint == p[checkI])
00080                 {
00081                     found = true;
00082                     break;
00083                 }
00084             }
00085 
00086             if (!found)
00087             {
00088                 p[maxVert] = curPoint;
00089 
00090                 maxVert++;
00091             }
00092         }
00093     }
00094 
00095     // reset the size of the list
00096     p.setSize(maxVert);
00097 
00098     return p;
00099 }
00100 
00101 
00102 Foam::pointField Foam::cell::points
00103 (
00104     const unallocFaceList& f,
00105     const pointField& meshPoints
00106 ) const
00107 {
00108     labelList pointLabels = labels(f);
00109 
00110     pointField p(pointLabels.size());
00111 
00112     forAll(p, i)
00113     {
00114         p[i] = meshPoints[pointLabels[i]];
00115     }
00116 
00117     return p;
00118 }
00119 
00120 
00121 Foam::edgeList Foam::cell::edges(const unallocFaceList& f) const
00122 {
00123     // return the lisf of cell edges
00124 
00125     const labelList& curFaces = *this;
00126 
00127     // create a list of edges
00128     label maxNoEdges = 0;
00129 
00130     forAll (curFaces, faceI)
00131     {
00132         maxNoEdges += f[curFaces[faceI]].nEdges();
00133     }
00134 
00135     edgeList allEdges(maxNoEdges);
00136     label nEdges = 0;
00137 
00138     forAll (curFaces, faceI)
00139     {
00140         const edgeList curFaceEdges = f[curFaces[faceI]].edges();
00141 
00142         forAll (curFaceEdges, faceEdgeI)
00143         {
00144             const edge& curEdge = curFaceEdges[faceEdgeI];
00145 
00146             bool edgeFound = false;
00147 
00148             for (label addedEdgeI = 0; addedEdgeI < nEdges; addedEdgeI++)
00149             {
00150                 if (allEdges[addedEdgeI] == curEdge)
00151                 {
00152                     edgeFound = true;
00153 
00154                     break;
00155                 }
00156             }
00157 
00158             if (!edgeFound)
00159             {
00160                 // Add the new edge onto the list
00161                 allEdges[nEdges] = curEdge;
00162                 nEdges++;
00163             }
00164         }
00165     }
00166 
00167     allEdges.setSize(nEdges);
00168 
00169     return allEdges;
00170 }
00171 
00172 
00173 Foam::point Foam::cell::centre
00174 (
00175     const pointField& p,
00176     const unallocFaceList& f
00177 ) const
00178 {
00179     // When one wants to access the cell centre and magnitude, the
00180     // functionality on the mesh level should be used in preference to the
00181     // functions provided here. They do not rely to the functionality
00182     // implemented here, provide additional checking and are more efficient.
00183     // The cell::centre and cell::mag functions may be removed in the future.
00184 
00185     // WARNING!
00186     // In the old version of the code, it was possible to establish whether any
00187     // of the pyramids had a negative volume, caused either by the poor
00188     // estimate of the cell centre or by the fact that the cell was defined
00189     // inside out. In the new description of the cell, this can only be
00190     // established with the reference to the owner-neighbour face-cell
00191     // relationship, which is not available on this level. Thus, all the
00192     // pyramids are believed to be positive with no checking.
00193 
00194     // first calculate the aproximate cell centre as the average of all
00195     // face centres
00196     vector cEst = vector::zero;
00197     scalar sumArea = 0;
00198 
00199     const labelList& faces = *this;
00200 
00201     forAll (faces, faceI)
00202     {
00203         scalar a = f[faces[faceI]].mag(p);
00204         cEst += f[faces[faceI]].centre(p)*a;
00205         sumArea += a;
00206     }
00207 
00208     cEst /= sumArea + VSMALL;
00209 
00210     // Calculate the centre by breaking the cell into pyramids and
00211     // volume-weighted averaging their centres
00212     vector sumVc = vector::zero;
00213 
00214     scalar sumV = 0;
00215 
00216     forAll (faces, faceI)
00217     {
00218         // calculate pyramid volume. If it is greater than zero, OK.
00219         // If not, the pyramid is inside-out. Create a face with the opposite
00220         // order and recalculate pyramid centre!
00221         scalar pyrVol = pyramidPointFaceRef(f[faces[faceI]], cEst).mag(p);
00222         vector pyrCentre = pyramidPointFaceRef(f[faces[faceI]], cEst).centre(p);
00223 
00224         // if pyramid inside-out because face points inwards invert
00225         // N.B. pyramid remains unchanged
00226         if (pyrVol < 0)
00227         {
00228             pyrVol = -pyrVol;
00229         }
00230 
00231         sumVc += pyrVol*pyrCentre;
00232         sumV += pyrVol;
00233     }
00234 
00235     return sumVc/(sumV + VSMALL);
00236 }
00237 
00238 
00239 Foam::scalar Foam::cell::mag
00240 (
00241     const pointField& p,
00242     const unallocFaceList& f
00243 ) const
00244 {
00245     // When one wants to access the cell centre and magnitude, the
00246     // functionality on the mesh level should be used in preference to the
00247     // functions provided here. They do not rely to the functionality
00248     // implemented here, provide additional checking and are more efficient.
00249     // The cell::centre and cell::mag functions may be removed in the future.
00250 
00251     // WARNING! See cell::centre
00252 
00253     // first calculate the aproximate cell centre as the average of all
00254     // face centres
00255     vector cEst = vector::zero;
00256     scalar nCellFaces = 0;
00257 
00258     const labelList& faces = *this;
00259 
00260     forAll (faces, faceI)
00261     {
00262         cEst += f[faces[faceI]].centre(p);
00263         nCellFaces += 1;
00264     }
00265 
00266     cEst /= nCellFaces;
00267 
00268     // Calculate the magnitude by summing the mags of the pyramids
00269     scalar v = 0;
00270 
00271     forAll (faces, faceI)
00272     {
00273         v += ::Foam::mag(pyramidPointFaceRef(f[faces[faceI]], cEst).mag(p));
00274     }
00275 
00276     return v;
00277 }
00278 
00279 
00280 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00281 
00282 bool Foam::operator==(const cell& a, const cell& b)
00283 {
00284     // Trivial reject: faces are different size
00285     if (a.size() != b.size())
00286     {
00287         return false;
00288     }
00289 
00290     List<bool> fnd(a.size(), false);
00291 
00292     forAll (b, bI)
00293     {
00294         label curLabel = b[bI];
00295 
00296         bool found = false;
00297 
00298         forAll (a, aI)
00299         {
00300             if (a[aI] == curLabel)
00301             {
00302                 found = true;
00303                 fnd[aI] = true;
00304                 break;
00305             }
00306         }
00307 
00308         if (!found)
00309         {
00310             return false;
00311         }
00312     }
00313 
00314     // check if all faces on a were marked
00315     bool result = true;
00316 
00317     forAll (fnd, aI)
00318     {
00319         result = (result && fnd[aI]);
00320     }
00321 
00322     return result;
00323 }
00324 
00325 
00326 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd