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

topoCellLooper.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 "topoCellLooper.H"
00028 #include "cellFeatures.H"
00029 #include "polyMesh.H"
00030 #include "mathematicalConstants.H"
00031 #include "DynamicList.H"
00032 #include "ListOps.H"
00033 #include "meshTools.H"
00034 #include "hexMatcher.H"
00035 
00036 #include "addToRunTimeSelectionTable.H"
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 
00040 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00041 namespace Foam
00042 {
00043    defineTypeNameAndDebug(topoCellLooper, 0);
00044    addToRunTimeSelectionTable(cellLooper, topoCellLooper, word);
00045 }
00046 
00047 // Angle for polys to be considered splitHexes.
00048 const Foam::scalar Foam::topoCellLooper::featureCos =
00049     Foam::cos(10.0 * mathematicalConstant::pi/180.0);
00050 
00051 
00052 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00053 
00054 // In-memory truncate a list
00055 template <class T>
00056 void Foam::topoCellLooper::subsetList
00057 (
00058     const label startI,
00059     const label freeI,
00060     DynamicList<T>& lst
00061 )
00062 {
00063     if (startI == 0)
00064     {
00065         // Truncate (setSize decides itself not to do anything if nothing
00066         // changed)
00067         if (freeI < 0)
00068         {
00069             FatalErrorIn("topoCellLooper::subsetList")
00070                 << "startI:" << startI << "  freeI:" << freeI
00071                 << "  lst:" << lst << abort(FatalError);
00072         }
00073         lst.setCapacity(freeI);
00074     }
00075     else
00076     {
00077         // Shift elements down
00078         label newI = 0;
00079         for (label elemI = startI; elemI < freeI; elemI++)
00080         {
00081             lst[newI++] = lst[elemI];
00082         }
00083 
00084         if ((freeI - startI) < 0)
00085         {
00086             FatalErrorIn("topoCellLooper::subsetList")
00087                 << "startI:" << startI << "  freeI:" << freeI
00088                 << "  lst:" << lst << abort(FatalError);
00089         }
00090 
00091         lst.setCapacity(freeI - startI);
00092     }
00093 }
00094 
00095 
00096 // Returns label of edge nFeaturePts away from startEdge (in the direction of
00097 // startVertI) and not counting non-featurePoints.
00098 //
00099 // When stepping to this face it can happen in 3 different ways:
00100 //
00101 //  --|------
00102 //    |
00103 //  1 |   0
00104 //    |A
00105 //    |
00106 //    |
00107 //  --|------
00108 //
00109 //  A: jumping from face0 to face1 across edge A.
00110 //      startEdge != -1
00111 //      startVert == -1
00112 //
00113 //  --|------
00114 //    |
00115 //  1 |   0
00116 //    +B
00117 //    |
00118 //    |
00119 //  --|------
00120 //
00121 //  B: jumping from face0 to face1 across (non-feature) point B
00122 //      startEdge == -1
00123 //      startVert != -1
00124 //
00125 //  --|------
00126 //  0 |   1
00127 //    |C
00128 //  --+
00129 //    |
00130 //    |
00131 //  --|------
00132 //
00133 //  C: jumping from face0 to face1 across (feature) point C on edge.
00134 //      startEdge != -1
00135 //      startVert != -1
00136 //
00137 void Foam::topoCellLooper::walkFace
00138 (
00139     const cellFeatures& features,
00140     const label faceI,
00141     const label startEdgeI,
00142     const label startVertI,
00143     const label nFeaturePts,
00144 
00145     label& edgeI,
00146     label& vertI
00147 ) const
00148 {
00149     const labelList& fEdges = mesh().faceEdges()[faceI];
00150 
00151     edgeI = startEdgeI;
00152 
00153     vertI = startVertI;
00154 
00155     // Number of feature points crossed so far
00156     label nVisited = 0;
00157 
00158     if (vertI == -1)
00159     {
00160         // Started on edge. Go to one of its endpoints.
00161         vertI = mesh().edges()[edgeI].start();
00162 
00163         if (features.isFeatureVertex(faceI, vertI))
00164         {
00165             nVisited++;
00166         }
00167     }
00168 
00169     if ((edgeI == -1) || !meshTools::edgeOnFace(mesh(), faceI, edgeI))
00170     {
00171         // Either edge is not set or not on current face.  Just take one of
00172         // the edges on this face as starting edge.
00173         edgeI = getFirstVertEdge(faceI, vertI);
00174     }
00175 
00176     // Now we should have starting edge on face and a vertex on that edge.
00177 
00178     do
00179     {
00180 
00181         edgeI = meshTools::otherEdge(mesh(), fEdges, edgeI, vertI);
00182 
00183         if (nVisited == nFeaturePts)
00184         {
00185             break;
00186         }
00187 
00188         vertI = mesh().edges()[edgeI].otherVertex(vertI);
00189 
00190         if (features.isFeatureVertex(faceI, vertI))
00191         {
00192             nVisited++;
00193         }
00194     }
00195     while (true);
00196 }
00197 
00198 
00199 // Returns list of vertices on 'superEdge' i.e. list of edges connected by
00200 // non-feature points. First and last are feature points, ones inbetween are
00201 // not.
00202 Foam::labelList Foam::topoCellLooper::getSuperEdge
00203 (
00204     const cellFeatures& features,
00205     const label faceI,
00206     const label startEdgeI,
00207     const label startVertI
00208 ) const
00209 {
00210     const labelList& fEdges = mesh().faceEdges()[faceI];
00211 
00212     labelList superVerts(fEdges.size());
00213     label superVertI = 0;
00214 
00215 
00216     label edgeI = startEdgeI;
00217 
00218     label vertI = startVertI;
00219 
00220     superVerts[superVertI++] = vertI;
00221 
00222     label prevEdgeI = -1;
00223 
00224     do
00225     {
00226         vertI = mesh().edges()[edgeI].otherVertex(vertI);
00227 
00228         superVerts[superVertI++] = vertI;
00229 
00230         prevEdgeI = edgeI;
00231 
00232         edgeI = meshTools::otherEdge(mesh(), fEdges, edgeI, vertI);
00233     }
00234     while (!features.isFeaturePoint(prevEdgeI, edgeI));
00235 
00236     superVerts.setSize(superVertI);
00237 
00238     return superVerts;
00239 }
00240 
00241 
00242 // Return non-feature edge from cells' edges that is most perpendicular
00243 // to refinement direction.
00244 Foam::label Foam::topoCellLooper::getAlignedNonFeatureEdge
00245 (
00246     const vector& refDir,
00247     const label cellI,
00248     const cellFeatures& features
00249 ) const
00250 {
00251     const labelList& cEdges = mesh().cellEdges()[cellI];
00252 
00253     const point& ctr = mesh().cellCentres()[cellI];
00254 
00255     label cutEdgeI = -1;
00256     scalar maxCos = -GREAT;
00257 
00258     forAll(cEdges, cEdgeI)
00259     {
00260         label edgeI = cEdges[cEdgeI];
00261 
00262         if (!features.isFeatureEdge(edgeI))
00263         {
00264             const edge& e = mesh().edges()[edgeI];
00265 
00266             // Get plane spanned by e.start, e.end and cell centre.
00267             vector e0 = mesh().points()[e.start()] - ctr;
00268             vector e1 = mesh().points()[e.end()] - ctr;
00269 
00270             vector n = e0 ^ e1;
00271             n /= mag(n);
00272 
00273             scalar cosAngle = mag(refDir & n);
00274 
00275             if (cosAngle > maxCos)
00276             {
00277                 maxCos = cosAngle;
00278 
00279                 cutEdgeI = edgeI;
00280             }
00281         }
00282     }
00283 
00284     return cutEdgeI;
00285 }
00286 
00287 
00288 // Starts from edge and vertex on edge on face (or neighbouring face)
00289 // and steps either to existing vertex (vertI != -1) or to edge (vertI == -1)
00290 // by walking point-edge and crossing nFeats featurePoints.
00291 void Foam::topoCellLooper::walkAcrossFace
00292 (
00293     const cellFeatures& features,
00294     const label faceI,
00295     const label startEdgeI,
00296     const label startVertI,
00297     const label nFeats,
00298 
00299     label& edgeI,
00300     label& vertI
00301 ) const
00302 {
00303     label oppositeVertI = -1;
00304     label oppositeEdgeI = -1;
00305 
00306     // Go to oppositeEdge and a vertex on that.
00307     walkFace
00308     (
00309         features,
00310         faceI,
00311         startEdgeI,
00312         startVertI,
00313         nFeats,
00314 
00315         oppositeEdgeI,
00316         oppositeVertI
00317     );
00318 
00319     // Loop over super edge to find internal points if there are any.
00320 
00321     labelList superEdge =
00322         getSuperEdge
00323         (
00324             features,
00325             faceI,
00326             oppositeEdgeI,
00327             oppositeVertI
00328         );
00329 
00330     label sz = superEdge.size();
00331 
00332     if (sz == 2)
00333     {
00334         // No non-feature point inbetween feature points.
00335         // Mark edge.
00336 
00337         vertI = -1;
00338         edgeI = oppositeEdgeI;
00339     }
00340     else if (sz == 3)
00341     {
00342         vertI = superEdge[1];
00343         edgeI = -1;
00344     }
00345     else
00346     {
00347         //Should choose acc. to geometry!
00348         label index = sz/2;
00349 
00350         if (debug)
00351         {
00352             Pout<< "    Don't know what to do. Stepped to non-feature point "
00353                 << "at index " << index << " in superEdge:" << superEdge
00354                 << endl;
00355         }
00356 
00357         vertI = superEdge[index];
00358         edgeI = -1;
00359     }
00360 }
00361 
00362 
00363 // Walks cell circumference. Updates face, edge, vertex.
00364 //
00365 // Position on face is given by:
00366 //
00367 //  vertI == -1, faceI != -1, edgeI != -1
00368 //      on edge of face. Cross edge to neighbouring face.
00369 //
00370 //  vertI != -1, edgeI != -1, faceI == -1
00371 //      coming from edge onto vertex vertI. Need to step to one
00372 //      of the faces not using edgeI.
00373 //
00374 //  vertI != -1, edgeI == -1, faceI != -1
00375 //      coming from vertex on side of face. Step to one of the faces
00376 //      using vertI but not faceI
00377 void Foam::topoCellLooper::walkSplitHex
00378 (
00379     const label cellI,
00380     const cellFeatures& features,
00381     const label fromFaceI,
00382     const label fromEdgeI,
00383     const label fromVertI,
00384 
00385     DynamicList<label>& loop,
00386     DynamicList<scalar>& loopWeights
00387 ) const
00388 {
00389     // Work vars giving position on cell
00390     label faceI = fromFaceI;
00391     label edgeI = fromEdgeI;
00392     label vertI = fromVertI;
00393 
00394     do
00395     {
00396         if (debug)
00397         {
00398             Pout<< "Entering walk with : cell:" << cellI << " face:" << faceI;
00399             if (faceI != -1)
00400             {
00401                 Pout<< " verts:" << mesh().faces()[faceI];
00402             }
00403             Pout<< " edge:" << edgeI;
00404             if (edgeI != -1)
00405             {
00406                 Pout<< " verts:" << mesh().edges()[edgeI];
00407             }
00408             Pout<< " vert:" << vertI << endl;
00409         }
00410 
00411         label startLoop = -1;
00412 
00413         if
00414         (
00415             (vertI != -1)
00416          && (
00417                 (startLoop =
00418                     findIndex
00419                     (
00420                         loop,
00421                         vertToEVert(vertI)
00422                     )
00423                 )
00424             != -1
00425             )
00426         )
00427         {
00428             // Breaking walk since vertI already cut
00429             label firstFree = loop.size();
00430 
00431             subsetList(startLoop, firstFree, loop);
00432             subsetList(startLoop, firstFree, loopWeights);
00433 
00434             break;
00435         }
00436         if
00437         (
00438             (edgeI != -1)
00439          && (
00440                 (startLoop =
00441                     findIndex
00442                     (
00443                         loop,
00444                         edgeToEVert(edgeI)
00445                     )
00446                 )
00447              != -1
00448             )
00449         )
00450         {
00451             // Breaking walk since edgeI already cut
00452             label firstFree = loop.size();
00453 
00454             subsetList(startLoop, firstFree, loop);
00455             subsetList(startLoop, firstFree, loopWeights);
00456 
00457             break;
00458         }
00459 
00460 
00461         if (vertI == -1)
00462         {
00463             // On edge
00464             if (edgeI == -1)
00465             {
00466                 FatalErrorIn("walkSplitHex") << "Illegal edge and vert"
00467                     << abort(FatalError);
00468             }
00469 
00470             loop.append(edgeToEVert(edgeI));
00471             loopWeights.append(0.5);
00472 
00473             // Cross edge to next face
00474             faceI = meshTools::otherFace(mesh(), cellI, faceI, edgeI);
00475 
00476             if (debug)
00477             {
00478                 Pout<< "    stepped across edge " << mesh().edges()[edgeI]
00479                     << " to face " << faceI << " verts:"
00480                     << mesh().faces()[faceI] << endl;
00481             }
00482 
00483             label nextEdgeI = -1;
00484             label nextVertI = -1;
00485 
00486             // Walk face along its edges
00487             walkAcrossFace
00488             (
00489                 features,
00490                 faceI,
00491                 edgeI,
00492                 vertI,
00493                 2,
00494 
00495                 nextEdgeI,
00496                 nextVertI
00497             );
00498 
00499             edgeI = nextEdgeI;
00500             vertI = nextVertI;
00501         }
00502         else
00503         {
00504             // On vertex.
00505 
00506             loop.append(vertToEVert(vertI));
00507             loopWeights.append(-GREAT);
00508 
00509             if (edgeI == -1)
00510             {
00511                 // Normal vertex on edge of face. Get edges connected to it
00512                 // which are not on faceI.
00513                 labelList nextEdges = getVertEdgesNonFace
00514                 (
00515                     cellI,
00516                     faceI,
00517                     vertI
00518                 );
00519 
00520                 if (nextEdges.empty())
00521                 {
00522                     // Cross to other face (there is only one since no edges)
00523                     const labelList& pFaces = mesh().pointFaces()[vertI];
00524 
00525                     forAll(pFaces, pFaceI)
00526                     {
00527                         label thisFaceI = pFaces[pFaceI];
00528 
00529                         if
00530                         (
00531                             (thisFaceI != faceI)
00532                          && meshTools::faceOnCell(mesh(), cellI, thisFaceI)
00533                         )
00534                         {
00535                             faceI = thisFaceI;
00536                             break;
00537                         }
00538                     }
00539 
00540                     if (debug)
00541                     {
00542                         Pout<< "    stepped from non-edge vertex " << vertI
00543                             << " to face " << faceI << " verts:"
00544                             << mesh().faces()[faceI]
00545                             << " since candidate edges:" << nextEdges << endl;
00546                     }
00547 
00548                     label nextEdgeI = -1;
00549                     label nextVertI = -1;
00550 
00551                     walkAcrossFace
00552                     (
00553                         features,
00554                         faceI,
00555                         edgeI,
00556                         vertI,
00557                         2,          // 2 vertices to cross
00558 
00559                         nextEdgeI,
00560                         nextVertI
00561                     );
00562 
00563                     edgeI = nextEdgeI;
00564                     vertI = nextVertI;
00565                 }
00566                 else if (nextEdges.size() == 1)
00567                 {
00568                     // One edge. Go along this one.
00569                     edgeI = nextEdges[0];
00570 
00571                     if (debug)
00572                     {
00573                         Pout<< "    stepped from non-edge vertex " << vertI
00574                             << " along edge " << edgeI << " verts:"
00575                             << mesh().edges()[edgeI]
00576                             << " out of candidate edges:"
00577                             << nextEdges << endl;
00578                     }
00579 
00580                     vertI = mesh().edges()[edgeI].otherVertex(vertI);
00581 
00582                     faceI = -1;
00583                 }
00584                 else
00585                 {
00586                     // Multiple edges to choose from. Pick any one.
00587                     // (ideally should be geometric)
00588 
00589                     label index = nextEdges.size()/2;
00590 
00591                     edgeI = nextEdges[index];
00592 
00593                     if (debug)
00594                     {
00595                         Pout<< "    stepped from non-edge vertex " << vertI
00596                             << " along edge " << edgeI << " verts:"
00597                             << mesh().edges()[edgeI]
00598                             << " out of candidate edges:" << nextEdges << endl;
00599                     }
00600 
00601                     vertI = mesh().edges()[edgeI].otherVertex(vertI);
00602 
00603                     faceI = -1;
00604                 }
00605             }
00606             else
00607             {
00608                 // Get faces connected to startVertI but not startEdgeI
00609                 labelList nextFaces =
00610                     getVertFacesNonEdge
00611                     (
00612                         cellI,
00613                         edgeI,
00614                         vertI
00615                     );
00616 
00617                 if (nextFaces.size() == 1)
00618                 {
00619                     // Only one face to cross.
00620                     faceI = nextFaces[0];
00621 
00622                     label nextEdgeI = -1;
00623                     label nextVertI = -1;
00624 
00625                     walkAcrossFace
00626                     (
00627                         features,
00628                         faceI,
00629                         edgeI,
00630                         vertI,
00631                         2,          // 2 vertices to cross
00632 
00633                         nextEdgeI,
00634                         nextVertI
00635                     );
00636 
00637                     edgeI = nextEdgeI;
00638                     vertI = nextVertI;
00639                 }
00640                 else if (nextFaces.size() == 2)
00641                 {
00642                     // Split face. Get edge inbetween.
00643                     faceI = -1;
00644 
00645                     edgeI =
00646                         meshTools::getSharedEdge
00647                         (
00648                             mesh(),
00649                             nextFaces[0],
00650                             nextFaces[1]
00651                         );
00652 
00653                     vertI = mesh().edges()[edgeI].otherVertex(vertI);
00654                 }
00655                 else
00656                 {
00657                     FatalErrorIn("walkFromVert") << "Not yet implemented"
00658                         << "Choosing from more than "
00659                         << "two candidates:" << nextFaces
00660                         << " when coming from vertex " << vertI << " on cell "
00661                         << cellI << abort(FatalError);
00662                 }
00663             }
00664         }
00665 
00666         if (debug)
00667         {
00668             Pout<< "Walked to : face:" << faceI;
00669             if (faceI != -1)
00670             {
00671                 Pout<< " verts:" << mesh().faces()[faceI];
00672             }
00673             Pout<< " edge:" << edgeI;
00674             if (edgeI != -1)
00675             {
00676                 Pout<< " verts:" << mesh().edges()[edgeI];
00677             }
00678             Pout<< " vert:" << vertI << endl;
00679         }
00680     }
00681     while (true);
00682 }
00683 
00684 
00685 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00686 
00687 // Construct from components
00688 Foam::topoCellLooper::topoCellLooper(const polyMesh& mesh)
00689 :
00690     hexCellLooper(mesh)
00691 {}
00692 
00693 
00694 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00695 
00696 Foam::topoCellLooper::~topoCellLooper()
00697 {}
00698 
00699 
00700 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00701 
00702 bool Foam::topoCellLooper::cut
00703 (
00704     const vector& refDir,
00705     const label cellI,
00706     const boolList& vertIsCut,
00707     const boolList& edgeIsCut,
00708     const scalarField& edgeWeight,
00709 
00710     labelList& loop,
00711     scalarField& loopWeights
00712 ) const
00713 {
00714     if (mesh().cellShapes()[cellI].model() == hex_)
00715     {
00716         // Let parent handle hex case.
00717         return
00718             hexCellLooper::cut
00719             (
00720                 refDir,
00721                 cellI,
00722                 vertIsCut,
00723                 edgeIsCut,
00724                 edgeWeight,
00725                 loop,
00726                 loopWeights
00727             );
00728     }
00729     else
00730     {
00731         cellFeatures superCell(mesh(), featureCos, cellI);
00732 
00733         if (hexMatcher().isA(superCell.faces()))
00734         {
00735             label edgeI =
00736                 getAlignedNonFeatureEdge
00737                 (
00738                     refDir,
00739                     cellI,
00740                     superCell
00741                 );
00742 
00743             label vertI = -1;
00744 
00745             label faceI = -1;
00746 
00747             if (edgeI != -1)
00748             {
00749                 // Found non-feature edge. Start walking from vertex on edge.
00750                 vertI = mesh().edges()[edgeI].start();
00751             }
00752             else
00753             {
00754                 // No 'matching' non-feature edge found on cell. Get starting
00755                 // normal i.e. feature edge.
00756                 edgeI = getMisAlignedEdge(refDir, cellI);
00757 
00758                 // Get any face using edge
00759                 label face0;
00760                 label face1;
00761                 meshTools::getEdgeFaces(mesh(), cellI, edgeI, face0, face1);
00762 
00763                 faceI = face0;
00764             }
00765 
00766             label nEstCuts = 2*mesh().cells()[cellI].size();
00767 
00768             DynamicList<label> localLoop(nEstCuts);
00769             DynamicList<scalar> localLoopWeights(nEstCuts);
00770 
00771             walkSplitHex
00772             (
00773                 cellI,
00774                 superCell,
00775                 faceI,
00776                 edgeI,
00777                 vertI,
00778 
00779                 localLoop,
00780                 localLoopWeights
00781             );
00782 
00783             if (localLoop.size() <=2)
00784             {
00785                 return false;
00786             }
00787             else
00788             {
00789                 loop.transfer(localLoop);
00790                 loopWeights.transfer(localLoopWeights);
00791 
00792                 return true;
00793             }
00794         }
00795         else
00796         {
00797             // Let parent handle poly case.
00798             return hexCellLooper::cut
00799             (
00800                 refDir,
00801                 cellI,
00802                 vertIsCut,
00803                 edgeIsCut,
00804                 edgeWeight,
00805                 loop,
00806                 loopWeights
00807             );
00808         }
00809     }
00810 }
00811 
00812 
00813 bool Foam::topoCellLooper::cut
00814 (
00815     const plane& cutPlane,
00816     const label cellI,
00817     const boolList& vertIsCut,
00818     const boolList& edgeIsCut,
00819     const scalarField& edgeWeight,
00820 
00821     labelList& loop,
00822     scalarField& loopWeights
00823 ) const
00824 {
00825     // Let parent handle cut with plane.
00826     return
00827         hexCellLooper::cut
00828         (
00829             cutPlane,
00830             cellI,
00831             vertIsCut,
00832             edgeIsCut,
00833             edgeWeight,
00834 
00835             loop,
00836             loopWeights
00837         );
00838 }
00839 
00840 
00841 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd