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

cellFeatures.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 "cellFeatures.H"
00030 #include "primitiveMesh.H"
00031 #include "HashSet.H"
00032 #include "Map.H"
00033 #include "demandDrivenData.H"
00034 #include "ListOps.H"
00035 #include "meshTools.H"
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 
00040 // Return true if edge start and end are on increasing face vertices. (edge is
00041 // guaranteed to be on face)
00042 bool Foam::cellFeatures::faceAlignedEdge(const label faceI, const label edgeI)
00043  const
00044 {
00045     const edge& e = mesh_.edges()[edgeI];
00046 
00047     const face& f = mesh_.faces()[faceI];
00048 
00049     forAll(f, fp)
00050     {
00051         if (f[fp] == e.start())
00052         {
00053             label fp1 = f.fcIndex(fp);
00054 
00055             return f[fp1] == e.end();
00056         }
00057     }
00058 
00059     FatalErrorIn
00060     (
00061         "cellFeatures::faceAlignedEdge(const label, const label)"
00062     )   << "Can not find edge " << mesh_.edges()[edgeI]
00063         << " on face " << faceI << abort(FatalError);
00064 
00065     return false;
00066 }
00067 
00068 
00069 // Return edge in featureEdge that uses vertI and is on same superface
00070 // but is not edgeI
00071 Foam::label Foam::cellFeatures::nextEdge
00072 (
00073     const Map<label>& toSuperFace,
00074     const label superFaceI,
00075     const label thisEdgeI,
00076     const label thisVertI
00077 ) const
00078 {
00079     const labelList& pEdges = mesh_.pointEdges()[thisVertI];
00080 
00081     forAll(pEdges, pEdgeI)
00082     {
00083         label edgeI = pEdges[pEdgeI];
00084 
00085         if ((edgeI != thisEdgeI) && featureEdge_.found(edgeI))
00086         {
00087             // Check that edge is used by a face on same superFace
00088 
00089             const labelList& eFaces = mesh_.edgeFaces()[edgeI];
00090 
00091             forAll(eFaces, eFaceI)
00092             {
00093                 label faceI = eFaces[eFaceI];
00094 
00095                 if
00096                 (
00097                     meshTools::faceOnCell(mesh_, cellI_, faceI)
00098                  && (toSuperFace[faceI] == superFaceI)
00099                 )
00100                 {
00101                     return edgeI;
00102                 }
00103             }
00104         }
00105     }
00106 
00107     FatalErrorIn
00108     (
00109         "cellFeatures::nextEdge(const label, const Map<label>"
00110         ", const labelHashSet&, const label, const label, const label)"
00111     )   << "Can not find edge in " << featureEdge_ << " connected to edge "
00112         << thisEdgeI << " at vertex " << thisVertI << endl
00113         << "This might mean that the externalEdges do not form a closed loop"
00114         << abort(FatalError);
00115 
00116     return -1;
00117 }
00118 
00119 
00120 // Return true if angle between faces using it is larger than certain value.
00121 bool Foam::cellFeatures::isCellFeatureEdge
00122 (
00123     const scalar minCos,
00124     const label edgeI
00125 ) const
00126 {
00127     // Get the two faces using this edge
00128 
00129     label face0;
00130     label face1;
00131     meshTools::getEdgeFaces(mesh_, cellI_, edgeI, face0, face1);
00132 
00133     // Check the angle between them by comparing the face normals.
00134 
00135     vector n0 = mesh_.faceAreas()[face0];
00136     n0 /= mag(n0);
00137 
00138     vector n1 = mesh_.faceAreas()[face1];
00139     n1 /= mag(n1);
00140 
00141     scalar cosAngle = n0 & n1;
00142 
00143 
00144     const edge& e = mesh_.edges()[edgeI];
00145 
00146     const face& f0 = mesh_.faces()[face0];
00147 
00148     label face0Start = findIndex(f0, e.start());
00149     label face0End   = f0.fcIndex(face0Start);
00150 
00151     const face& f1 = mesh_.faces()[face1];
00152 
00153     label face1Start = findIndex(f1, e.start());
00154     label face1End   = f1.fcIndex(face1Start);
00155 
00156     if
00157     (
00158         (
00159             (f0[face0End] == e.end())
00160          && (f1[face1End] != e.end())
00161         )
00162      || (
00163             (f0[face0End] != e.end())
00164          && (f1[face1End] == e.end())
00165         )
00166     )
00167     {
00168     }
00169     else
00170     {
00171         cosAngle = -cosAngle;
00172     }
00173 
00174     if (cosAngle < minCos)
00175     {
00176         return true;
00177     }
00178     else
00179     {
00180         return false;
00181     }
00182 }
00183 
00184 
00185 // Recursively mark (on toSuperFace) all face reachable across non-feature
00186 // edges.
00187 void Foam::cellFeatures::walkSuperFace
00188 (
00189     const label faceI,
00190     const label superFaceI,
00191     Map<label>& toSuperFace
00192 ) const
00193 {
00194     if (!toSuperFace.found(faceI))
00195     {
00196         toSuperFace.insert(faceI, superFaceI);
00197 
00198         const labelList& fEdges = mesh_.faceEdges()[faceI];
00199 
00200         forAll(fEdges, fEdgeI)
00201         {
00202             label edgeI = fEdges[fEdgeI];
00203 
00204             if (!featureEdge_.found(edgeI))
00205             {
00206                 label face0;
00207                 label face1;
00208                 meshTools::getEdgeFaces(mesh_, cellI_, edgeI, face0, face1);
00209 
00210                 if (face0 == faceI)
00211                 {
00212                     face0 = face1;
00213                 }
00214 
00215                 walkSuperFace
00216                 (
00217                     face0,
00218                     superFaceI,
00219                     toSuperFace
00220                 );
00221             }
00222         }
00223     }
00224 }
00225 
00226 
00227 void Foam::cellFeatures::calcSuperFaces() const
00228 {
00229     // Determine superfaces by edge walking across non-feature edges
00230 
00231     const labelList& cFaces = mesh_.cells()[cellI_];
00232 
00233     // Mapping from old to super face:
00234     //    <not found> : not visited
00235     //    >=0 : superFace
00236     Map<label> toSuperFace(10*cFaces.size());
00237 
00238     label superFaceI = 0;
00239 
00240     forAll(cFaces, cFaceI)
00241     {
00242         label faceI = cFaces[cFaceI];
00243 
00244         if (!toSuperFace.found(faceI))
00245         {
00246             walkSuperFace
00247             (
00248                 faceI,
00249                 superFaceI,
00250                 toSuperFace
00251             );
00252             superFaceI++;
00253         }
00254     }
00255 
00256     // Construct superFace-to-oldface mapping.
00257 
00258     faceMap_.setSize(superFaceI);
00259 
00260     forAll(cFaces, cFaceI)
00261     {
00262         label faceI = cFaces[cFaceI];
00263 
00264         faceMap_[toSuperFace[faceI]].append(faceI);
00265     }
00266 
00267     forAll(faceMap_, superI)
00268     {
00269         faceMap_[superI].shrink();
00270     }
00271 
00272 
00273     // Construct superFaces
00274 
00275     facesPtr_ = new faceList(superFaceI);
00276 
00277     faceList& faces = *facesPtr_;
00278 
00279     forAll(cFaces, cFaceI)
00280     {
00281         label faceI = cFaces[cFaceI];
00282 
00283         label superFaceI = toSuperFace[faceI];
00284 
00285         if (faces[superFaceI].empty())
00286         {
00287             // Superface not yet constructed.
00288 
00289             // Find starting feature edge on face.
00290             label startEdgeI = -1;
00291 
00292             const labelList& fEdges = mesh_.faceEdges()[faceI];
00293 
00294             forAll(fEdges, fEdgeI)
00295             {
00296                 label edgeI = fEdges[fEdgeI];
00297 
00298                 if (featureEdge_.found(edgeI))
00299                 {
00300                     startEdgeI = edgeI;
00301 
00302                     break;
00303                 }
00304             }
00305 
00306 
00307             if (startEdgeI != -1)
00308             {
00309                 // Walk point-edge-point along feature edges
00310 
00311                 DynamicList<label> superFace(10*mesh_.faces()[faceI].size());
00312 
00313                 const edge& e = mesh_.edges()[startEdgeI];
00314 
00315                 // Walk either start-end or end-start depending on orientation
00316                 // of face. SuperFace will have cellI as owner.
00317                 bool flipOrientation =
00318                     (mesh_.faceOwner()[faceI] == cellI_)
00319                   ^ (faceAlignedEdge(faceI, startEdgeI));
00320 
00321                 label startVertI = -1;
00322 
00323                 if (flipOrientation)
00324                 {
00325                     startVertI = e.end();
00326                 }
00327                 else
00328                 {
00329                     startVertI = e.start();
00330                 }
00331 
00332                 label edgeI = startEdgeI;
00333 
00334                 label vertI = e.otherVertex(startVertI);
00335 
00336                 do
00337                 {
00338                     label newEdgeI = nextEdge
00339                     (
00340                         toSuperFace,
00341                         superFaceI,
00342                         edgeI,
00343                         vertI
00344                     );
00345 
00346                     // Determine angle between edges.
00347                     if (isFeaturePoint(edgeI, newEdgeI))
00348                     {
00349                         superFace.append(vertI);
00350                     }
00351 
00352                     edgeI = newEdgeI;
00353 
00354                     if (vertI == startVertI)
00355                     {
00356                         break;
00357                     }
00358 
00359                     vertI = mesh_.edges()[edgeI].otherVertex(vertI);
00360                 }
00361                 while (true);
00362 
00363                 if (superFace.size() <= 2)
00364                 {
00365                     WarningIn("cellFeatures::calcSuperFaces")
00366                         << " Can not collapse faces " << faceMap_[superFaceI]
00367                         << " into one big face on cell " << cellI_ << endl
00368                         << "Try decreasing minCos:" << minCos_ << endl;
00369                 }
00370                 else
00371                 {
00372                     faces[superFaceI].transfer(superFace);
00373                 }
00374             }
00375         }
00376     }
00377 }
00378 
00379 
00380 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00381 
00382 // Construct from components
00383 Foam::cellFeatures::cellFeatures
00384 (
00385     const primitiveMesh& mesh,
00386     const scalar minCos,
00387     const label cellI
00388 )
00389 :
00390     mesh_(mesh),
00391     minCos_(minCos),
00392     cellI_(cellI),
00393     featureEdge_(10*mesh.cellEdges()[cellI].size()),
00394     facesPtr_(NULL),
00395     faceMap_(0)
00396 {
00397     const labelList& cEdges = mesh_.cellEdges()[cellI_];
00398 
00399     forAll(cEdges, cEdgeI)
00400     {
00401         label edgeI = cEdges[cEdgeI];
00402 
00403         if (isCellFeatureEdge(minCos_, edgeI))
00404         {
00405             featureEdge_.insert(edgeI);
00406         }
00407     }
00408 
00409 }
00410 
00411 
00412 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00413 
00414 Foam::cellFeatures::~cellFeatures()
00415 {
00416     deleteDemandDrivenData(facesPtr_);
00417 }
00418 
00419 
00420 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00421 
00422 bool Foam::cellFeatures::isFeaturePoint(const label edge0, const label edge1)
00423  const
00424 {
00425     if
00426     (
00427         (edge0 < 0)
00428      || (edge0 >= mesh_.nEdges())
00429      || (edge1 < 0)
00430      || (edge1 >= mesh_.nEdges())
00431     )
00432     {
00433         FatalErrorIn
00434         (
00435             "cellFeatures::isFeatureVertex(const label, const label)"
00436         )   << "Illegal edge labels : edge0:" << edge0 << " edge1:" << edge1
00437             << abort(FatalError);
00438     }
00439 
00440     const edge& e0 = mesh_.edges()[edge0];
00441 
00442     vector e0Vec = e0.vec(mesh_.points());
00443     e0Vec /= mag(e0Vec);
00444 
00445     const edge& e1 = mesh_.edges()[edge1];
00446 
00447     vector e1Vec = e1.vec(mesh_.points());
00448     e1Vec /= mag(e1Vec);
00449 
00450     scalar cosAngle;
00451 
00452     if
00453     (
00454         (e0.start() == e1.end())
00455      || (e0.end() == e1.start())
00456     )
00457     {
00458         // Same direction
00459         cosAngle = e0Vec & e1Vec;
00460     }
00461     else if
00462     (
00463         (e0.start() == e1.start())
00464      || (e0.end() == e1.end())
00465     )
00466     {
00467         // back on back
00468         cosAngle = - e0Vec & e1Vec;
00469     }
00470     else
00471     {
00472         cosAngle = GREAT;   // satisfy compiler
00473 
00474         FatalErrorIn
00475         (
00476             "cellFeatures::isFeaturePoint(const label, const label"
00477             ", const label)"
00478         )   << "Edges do not share common vertex. e0:" << e0
00479             << " e1:" << e1 << abort(FatalError);
00480     }
00481 
00482     if (cosAngle < minCos_)
00483     {
00484         // Angle larger than criterium
00485         return true;
00486     }
00487     else
00488     {
00489         return false;
00490     }
00491 }
00492 
00493 
00494 bool Foam::cellFeatures::isFeatureVertex(const label faceI, const label vertI)
00495  const
00496 {
00497     if
00498     (
00499         (faceI < 0)
00500      || (faceI >= mesh_.nFaces())
00501      || (vertI < 0)
00502      || (vertI >= mesh_.nPoints())
00503     )
00504     {
00505         FatalErrorIn
00506         (
00507             "cellFeatures::isFeatureVertex(const label, const label)"
00508         )   << "Illegal face " << faceI << " or vertex " << vertI
00509             << abort(FatalError);
00510     }
00511 
00512     const labelList& pEdges = mesh_.pointEdges()[vertI];
00513 
00514     label edge0 = -1;
00515     label edge1 = -1;
00516 
00517     forAll(pEdges, pEdgeI)
00518     {
00519         label edgeI = pEdges[pEdgeI];
00520 
00521         if (meshTools::edgeOnFace(mesh_, faceI, edgeI))
00522         {
00523             if (edge0 == -1)
00524             {
00525                 edge0 = edgeI;
00526             }
00527             else
00528             {
00529                 edge1 = edgeI;
00530 
00531                 // Found the two edges.
00532                 break;
00533             }
00534         }
00535     }
00536 
00537     if (edge1 == -1)
00538     {
00539         FatalErrorIn
00540         (
00541             "cellFeatures::isFeatureVertex(const label, const label)"
00542         )   << "Did not find two edges sharing vertex " << vertI
00543             << " on face " << faceI << " vertices:" << mesh_.faces()[faceI]
00544             << abort(FatalError);
00545     }
00546 
00547     return isFeaturePoint(edge0, edge1);
00548 }
00549 
00550 
00551 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd