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

cellDistFuncs.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "cellDistFuncs.H"
00030 #include "polyMesh.H"
00031 #include "wallPolyPatch.H"
00032 #include "polyBoundaryMesh.H"
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00038 
00039 defineTypeNameAndDebug(cellDistFuncs, 0);
00040 
00041 }
00042 
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 // Find val in first nElems elements of elems.
00047 Foam::label Foam::cellDistFuncs::findIndex
00048 (
00049     const label nElems,
00050     const labelList& elems,
00051     const label val
00052 )
00053 {
00054     for (label i = 0; i < nElems; i++)
00055     {
00056         if (elems[i] == val)
00057         {
00058             return i;
00059         }
00060     }
00061     return -1;
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00066 
00067 // Construct from mesh
00068 Foam::cellDistFuncs::cellDistFuncs(const polyMesh& mesh)
00069 :
00070     mesh_(mesh)
00071 {}
00072 
00073 
00074 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00075 
00076 // Get patch ids of named patches
00077 Foam::labelHashSet Foam::cellDistFuncs::getPatchIDs
00078 (
00079     const wordList& patchNames
00080 ) const
00081 {
00082     const polyBoundaryMesh& bMesh = mesh().boundaryMesh();
00083 
00084     // Construct Set of patchNames for easy checking if included
00085     HashSet<word> patchNamesHash(patchNames.size());
00086 
00087     forAll(patchNames, patchI)
00088     {
00089         patchNamesHash.insert(patchNames[patchI]);
00090     }
00091 
00092     // Loop over all patches and check if patch name in hashset
00093 
00094     labelHashSet patchIDs(bMesh.size());
00095 
00096     forAll(bMesh, patchI)
00097     {
00098         const polyPatch& patch = bMesh[patchI];
00099 
00100         if (patchNamesHash.found(patch.name()))
00101         {
00102             patchIDs.insert(patchI);
00103         }
00104     }
00105     return patchIDs;
00106 }
00107 
00108 
00109 // Get patch ids of patches of certain type (e.g. 'polyProcessorPatch')
00110 Foam::labelHashSet Foam::cellDistFuncs::getPatchIDs(const word& wantedType)
00111  const
00112 {
00113     const polyBoundaryMesh& bMesh = mesh().boundaryMesh();
00114 
00115     labelHashSet patchIDs(bMesh.size());
00116 
00117     forAll(bMesh, patchI)
00118     {
00119         const polyPatch& patch = bMesh[patchI];
00120 
00121         if (patch.type() == wantedType)
00122         {
00123             patchIDs.insert(patchI);
00124         }
00125     }
00126     return patchIDs;
00127 }
00128 
00129 
00130 // Return smallest true distance from p to any of wallFaces.
00131 // Note that even if normal hits face we still check other faces.
00132 // Note that wallFaces is untruncated and we explicitly pass in size.
00133 Foam::scalar Foam::cellDistFuncs::smallestDist
00134 (
00135     const point& p,
00136     const polyPatch& patch,
00137     const label nWallFaces,
00138     const labelList& wallFaces,
00139     label& minFaceI
00140 ) const
00141 {
00142     const pointField& points = patch.points();
00143 
00144     scalar minDist = GREAT;
00145     minFaceI = -1;
00146 
00147     for(label wallFaceI = 0; wallFaceI < nWallFaces; wallFaceI++)
00148     {
00149         label patchFaceI = wallFaces[wallFaceI];
00150 
00151         pointHit curHit = patch[patchFaceI].nearestPoint(p, points);
00152 
00153         if (curHit.distance() < minDist)
00154         {
00155             minDist = curHit.distance();
00156             minFaceI = patch.start() + patchFaceI;
00157         }
00158     }
00159 
00160     return minDist;
00161 }
00162 
00163 
00164 // Get point neighbours of faceI (including faceI). Returns number of faces.
00165 // Note: does not allocate storage but does use linear search to determine
00166 // uniqueness. For polygonal faces this might be quite inefficient.
00167 Foam::label Foam::cellDistFuncs::getPointNeighbours
00168 (
00169     const primitivePatch& patch,
00170     const label patchFaceI,
00171     labelList& neighbours
00172 ) const
00173 {
00174     label nNeighbours = 0;
00175 
00176     // Add myself
00177     neighbours[nNeighbours++] = patchFaceI;
00178 
00179     // Add all face neighbours
00180     const labelList& faceNeighbours = patch.faceFaces()[patchFaceI];
00181 
00182     forAll(faceNeighbours, faceNeighbourI)
00183     {
00184         neighbours[nNeighbours++] = faceNeighbours[faceNeighbourI];
00185     }
00186 
00187     // Remember part of neighbours that contains edge-connected faces.
00188     label nEdgeNbs = nNeighbours;
00189 
00190 
00191     // Add all point-only neighbours by linear searching in edge neighbours.
00192     // Assumes that point-only neighbours are not using multiple points on
00193     // face.
00194 
00195     const face& f = patch.localFaces()[patchFaceI];
00196 
00197     forAll(f, fp)
00198     {
00199         label pointI = f[fp];
00200 
00201         const labelList& pointNbs = patch.pointFaces()[pointI];
00202 
00203         forAll(pointNbs, nbI)
00204         {
00205             label faceI = pointNbs[nbI];
00206 
00207             // Check for faceI in edge-neighbours part of neighbours
00208             if (findIndex(nEdgeNbs, neighbours, faceI) == -1)
00209             {
00210                 neighbours[nNeighbours++] = faceI;
00211             }
00212         }
00213     }
00214 
00215 
00216     if (debug)
00217     {
00218         // Check for duplicates
00219 
00220         // Use hashSet to determine nbs.
00221         labelHashSet nbs(4*f.size());
00222 
00223         forAll(f, fp)
00224         {
00225             const labelList& pointNbs = patch.pointFaces()[f[fp]];
00226 
00227             forAll(pointNbs, i)
00228             {
00229                 nbs.insert(pointNbs[i]);
00230             }
00231         }
00232 
00233         // Subtract ours.
00234         for (label i = 0; i < nNeighbours; i++)
00235         {
00236             label nb = neighbours[i];
00237 
00238             if (!nbs.found(nb))
00239             {
00240                 SeriousErrorIn("Foam::cellDistFuncs::getPointNeighbours")
00241                     << "getPointNeighbours : patchFaceI:" << patchFaceI
00242                     << " verts:" << f << endl;
00243 
00244                 forAll(f, fp)
00245                 {
00246                     SeriousErrorIn("Foam::cellDistFuncs::getPointNeighbours")
00247                         << "point:" << f[fp] << " pointFaces:"
00248                         << patch.pointFaces()[f[fp]] << endl;
00249                 }
00250 
00251                 for (label i = 0; i < nNeighbours; i++)
00252                 {
00253                     SeriousErrorIn("Foam::cellDistFuncs::getPointNeighbours")
00254                         << "fast nbr:" << neighbours[i]
00255                         << endl;
00256                 }
00257 
00258                 FatalErrorIn("getPointNeighbours")
00259                     << "Problem: fast pointNeighbours routine included " << nb
00260                     << " which is not in proper neigbour list " << nbs.toc()
00261                     << abort(FatalError);
00262             }
00263             nbs.erase(nb);
00264         }
00265 
00266         if (nbs.size())
00267         {
00268             FatalErrorIn("getPointNeighbours")
00269                 << "Problem: fast pointNeighbours routine did not find "
00270                 << nbs.toc() << abort(FatalError);
00271         }
00272     }
00273 
00274     return nNeighbours;
00275 }
00276 
00277 
00278 // size of largest patch (out of supplied subset of patches)
00279 Foam::label Foam::cellDistFuncs::maxPatchSize(const labelHashSet& patchIDs)
00280  const
00281 {
00282     label maxSize = 0;
00283 
00284     forAll(mesh().boundaryMesh(), patchI)
00285     {
00286         if (patchIDs.found(patchI))
00287         {
00288             const polyPatch& patch = mesh().boundaryMesh()[patchI];
00289 
00290             maxSize = Foam::max(maxSize, patch.size());
00291         }
00292     }
00293     return maxSize;
00294 }
00295 
00296 
00297 // sum of patch sizes (out of supplied subset of patches)
00298 Foam::label Foam::cellDistFuncs::sumPatchSize(const labelHashSet& patchIDs)
00299  const
00300 {
00301     label sum = 0;
00302 
00303     forAll(mesh().boundaryMesh(), patchI)
00304     {
00305         if (patchIDs.found(patchI))
00306         {
00307             const polyPatch& patch = mesh().boundaryMesh()[patchI];
00308 
00309             sum += patch.size();
00310         }
00311     }
00312     return sum;
00313 }
00314 
00315 
00316 // Gets nearest wall for cells next to wall
00317 void Foam::cellDistFuncs::correctBoundaryFaceCells
00318 (
00319     const labelHashSet& patchIDs,
00320     scalarField& wallDistCorrected,
00321     Map<label>& nearestFace
00322 ) const
00323 {
00324     // Size neighbours array for maximum possible (= size of largest patch)
00325     label maxPointNeighbours = maxPatchSize(patchIDs);
00326 
00327     labelList neighbours(maxPointNeighbours);
00328 
00329 
00330     // Correct all cells with face on wall
00331     const vectorField& cellCentres = mesh().cellCentres();
00332     const labelList& faceOwner = mesh().faceOwner();
00333 
00334     forAll(mesh().boundaryMesh(), patchI)
00335     {
00336         if (patchIDs.found(patchI))
00337         {
00338             const polyPatch& patch = mesh().boundaryMesh()[patchI];
00339 
00340             // Check cells with face on wall
00341             forAll(patch, patchFaceI)
00342             {
00343                 label nNeighbours = getPointNeighbours
00344                 (
00345                     patch,
00346                     patchFaceI,
00347                     neighbours
00348                 );
00349 
00350                 label cellI = faceOwner[patch.start() + patchFaceI];
00351 
00352                 label minFaceI = -1;
00353 
00354                 wallDistCorrected[cellI] = smallestDist
00355                 (
00356                     cellCentres[cellI],
00357                     patch,
00358                     nNeighbours,
00359                     neighbours,
00360                     minFaceI
00361                 );
00362 
00363                 // Store wallCell and its nearest neighbour
00364                 nearestFace.insert(cellI, minFaceI);
00365             }
00366         }
00367     }
00368 
00369 }
00370 
00371 
00372 // Correct all cells connected to wall (via point) and not in nearestFace
00373 void Foam::cellDistFuncs::correctBoundaryPointCells
00374 (
00375     const labelHashSet& patchIDs,
00376     scalarField& wallDistCorrected,
00377     Map<label>& nearestFace
00378 ) const
00379 {
00380     // Correct all (non-visited) cells with point on wall
00381 
00382     const labelListList& pointCells = mesh().pointCells();
00383     const vectorField& cellCentres = mesh().cellCentres();
00384 
00385     forAll(mesh().boundaryMesh(), patchI)
00386     {
00387         if (patchIDs.found(patchI))
00388         {
00389             const polyPatch& patch = mesh().boundaryMesh()[patchI];
00390 
00391             const labelList& meshPoints = patch.meshPoints();
00392             const labelListList& pointFaces = patch.pointFaces();
00393 
00394             forAll(meshPoints, meshPointI)
00395             {
00396                 label vertI = meshPoints[meshPointI];
00397 
00398                 const labelList& neighbours = pointCells[vertI];
00399 
00400                 forAll(neighbours, neighbourI)
00401                 {
00402                     label cellI = neighbours[neighbourI];
00403 
00404                     if (!nearestFace.found(cellI))
00405                     {
00406                         const labelList& wallFaces = pointFaces[meshPointI];
00407 
00408                         label minFaceI = -1;
00409 
00410                         wallDistCorrected[cellI] = smallestDist
00411                         (
00412                             cellCentres[cellI],
00413                             patch,
00414                             wallFaces.size(),
00415                             wallFaces,
00416                             minFaceI
00417                         );
00418 
00419                         // Store wallCell and its nearest neighbour
00420                         nearestFace.insert(cellI, minFaceI);
00421                     }
00422                 }
00423             }
00424         }
00425     }
00426 }
00427 
00428 
00429 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd