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

primitiveMeshEdges.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 "primitiveMesh.H"
00028 #include "DynamicList.H"
00029 #include "demandDrivenData.H"
00030 #include "SortableList.H"
00031 #include "ListOps.H"
00032 
00033 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00034 
00035 // Returns edgeI between two points.
00036 Foam::label Foam::primitiveMesh::getEdge
00037 (
00038     List<DynamicList<label> >& pe,
00039     DynamicList<edge>& es,
00040 
00041     const label pointI,
00042     const label nextPointI
00043 )
00044 {
00045     // Find connection between pointI and nextPointI
00046     forAll(pe[pointI], ppI)
00047     {
00048         label eI = pe[pointI][ppI];
00049 
00050         const edge& e = es[eI];
00051 
00052         if (e.start() == nextPointI || e.end() == nextPointI)
00053         {
00054             return eI;
00055         }
00056     }
00057 
00058     // Make new edge.
00059     label edgeI = es.size();
00060     pe[pointI].append(edgeI);
00061     pe[nextPointI].append(edgeI);
00062     if (pointI < nextPointI)
00063     {
00064         es.append(edge(pointI, nextPointI));
00065     }
00066     else
00067     {
00068         es.append(edge(nextPointI, pointI));
00069     }
00070     return edgeI;
00071 }
00072 
00073 
00074 void Foam::primitiveMesh::calcEdges(const bool doFaceEdges) const
00075 {
00076     if (debug)
00077     {
00078         Pout<< "primitiveMesh::calcEdges(const bool) : "
00079             << "calculating edges, pointEdges and optionally faceEdges"
00080             << endl;
00081     }
00082 
00083     // It is an error to attempt to recalculate edges
00084     // if the pointer is already set
00085     if ((edgesPtr_ || pePtr_) || (doFaceEdges && fePtr_))
00086     {
00087         FatalErrorIn("primitiveMesh::calcEdges(const bool) const")
00088             << "edges or pointEdges or faceEdges already calculated"
00089             << abort(FatalError);
00090     }
00091     else
00092     {
00093         // ALGORITHM:
00094         // Go through the faces list. Search pointEdges for existing edge.
00095         // If not found create edge and add to pointEdges.
00096         // In second loop sort edges in order of increasing neighbouring
00097         // point.
00098         // This algorithm replaces the one using pointFaces which used more
00099         // allocations but less memory and was on practical cases
00100         // quite a bit slower.
00101 
00102         const faceList& fcs = faces();
00103 
00104         // Size up lists
00105         // ~~~~~~~~~~~~~
00106 
00107         // Estimate pointEdges storage
00108         List<DynamicList<label> > pe(nPoints());
00109         forAll(pe, pointI)
00110         {
00111             pe[pointI].setCapacity(primitiveMesh::edgesPerPoint_);
00112         }
00113 
00114         // Estimate edges storage
00115         DynamicList<edge> es(pe.size()*primitiveMesh::edgesPerPoint_/2);
00116 
00117         // Estimate faceEdges storage
00118         if (doFaceEdges)
00119         {
00120             fePtr_ = new labelListList(fcs.size());
00121             labelListList& faceEdges = *fePtr_;
00122             forAll(fcs, faceI)
00123             {
00124                 faceEdges[faceI].setSize(fcs[faceI].size());
00125             }
00126         }
00127 
00128 
00129         // Find consecutive face points in edge list
00130         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00131 
00132         // Edges on boundary faces
00133         label nExtEdges = 0;
00134         // Edges using no boundary point
00135         nInternal0Edges_ = 0;
00136         // Edges using 1 boundary point
00137         label nInt1Edges = 0;
00138         // Edges using two boundary points but not on boundary face:
00139         // edges.size()-nExtEdges-nInternal0Edges_-nInt1Edges
00140 
00141         // Ordering is different if points are ordered.
00142         if (nInternalPoints_ == -1)
00143         {
00144             // No ordering. No distinction between types.
00145             forAll(fcs, faceI)
00146             {
00147                 const face& f = fcs[faceI];
00148 
00149                 forAll(f, fp)
00150                 {
00151                     label pointI = f[fp];
00152                     label nextPointI = f[f.fcIndex(fp)];
00153 
00154                     label edgeI = getEdge(pe, es, pointI, nextPointI);
00155 
00156                     if (doFaceEdges)
00157                     {
00158                         (*fePtr_)[faceI][fp] = edgeI;
00159                     }
00160                 }
00161             }
00162             // Assume all edges internal
00163             nExtEdges = 0;
00164             nInternal0Edges_ = es.size();
00165             nInt1Edges = 0;
00166         }
00167         else
00168         {
00169             // 1. Do external faces first. This creates external edges.
00170             for (label faceI = nInternalFaces_; faceI < fcs.size(); faceI++)
00171             {
00172                 const face& f = fcs[faceI];
00173 
00174                 forAll(f, fp)
00175                 {
00176                     label pointI = f[fp];
00177                     label nextPointI = f[f.fcIndex(fp)];
00178 
00179                     label oldNEdges = es.size();
00180                     label edgeI = getEdge(pe, es, pointI, nextPointI);
00181 
00182                     if (es.size() > oldNEdges)
00183                     {
00184                         nExtEdges++;
00185                     }
00186                     if (doFaceEdges)
00187                     {
00188                         (*fePtr_)[faceI][fp] = edgeI;
00189                     }
00190                 }
00191             }
00192 
00193             // 2. Do internal faces. This creates internal edges.
00194             for (label faceI = 0; faceI < nInternalFaces_; faceI++)
00195             {
00196                 const face& f = fcs[faceI];
00197 
00198                 forAll(f, fp)
00199                 {
00200                     label pointI = f[fp];
00201                     label nextPointI = f[f.fcIndex(fp)];
00202 
00203                     label oldNEdges = es.size();
00204                     label edgeI = getEdge(pe, es, pointI, nextPointI);
00205 
00206                     if (es.size() > oldNEdges)
00207                     {
00208                         if (pointI < nInternalPoints_)
00209                         {
00210                             if (nextPointI < nInternalPoints_)
00211                             {
00212                                 nInternal0Edges_++;
00213                             }
00214                             else
00215                             {
00216                                 nInt1Edges++;
00217                             }
00218                         }
00219                         else
00220                         {
00221                             if (nextPointI < nInternalPoints_)
00222                             {
00223                                 nInt1Edges++;
00224                             }
00225                             else
00226                             {
00227                                 // Internal edge with two points on boundary
00228                             }
00229                         }
00230                     }
00231                     if (doFaceEdges)
00232                     {
00233                         (*fePtr_)[faceI][fp] = edgeI;
00234                     }
00235                 }
00236             }
00237         }
00238 
00239 
00240         // For unsorted meshes the edges will be in order of occurrence inside
00241         // the faces. For sorted meshes the first nExtEdges will be the external
00242         // edges.
00243 
00244         if (nInternalPoints_ != -1)
00245         {
00246             nInternalEdges_ = es.size()-nExtEdges;
00247             nInternal1Edges_ = nInternal0Edges_+nInt1Edges;
00248 
00249             //Pout<< "Edge overview:" << nl
00250             //    << "    total number of edges           : " << es.size()
00251             //    << nl
00252             //    << "    boundary edges                  : " << nExtEdges
00253             //    << nl
00254             //    << "    edges using no boundary point   : "
00255             //    << nInternal0Edges_
00256             //    << nl
00257             //    << "    edges using one boundary points : " << nInt1Edges
00258             //   << nl
00259             //    << "    edges using two boundary points : "
00260             //    << es.size()-nExtEdges-nInternal0Edges_-nInt1Edges << endl;
00261         }
00262 
00263 
00264         // Like faces sort edges in order of increasing neigbouring point.
00265         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00266         // Automatically if points are sorted into internal and external points
00267         // the edges will be sorted into
00268         // - internal point to internal point
00269         // - internal point to external point
00270         // - external point to external point
00271         // The problem is that the last one mixes external edges with internal
00272         // edges with two points on the boundary.
00273 
00274 
00275         // Map to sort into new upper-triangular order
00276         labelList oldToNew(es.size());
00277         if (debug)
00278         {
00279             oldToNew = -1;
00280         }
00281 
00282         // start of edges with 0 boundary points
00283         label internal0EdgeI = 0;
00284 
00285         // start of edges with 1 boundary points
00286         label internal1EdgeI = nInternal0Edges_;
00287 
00288         // start of edges with 2 boundary points
00289         label internal2EdgeI = nInternal1Edges_;
00290 
00291         // start of external edges
00292         label externalEdgeI = nInternalEdges_;
00293 
00294 
00295         // To sort neighbouring points in increasing order. Defined outside
00296         // for optimisation reasons: if all connectivity size same will need
00297         // no reallocations
00298         SortableList<label> nbrPoints(primitiveMesh::edgesPerPoint_);
00299 
00300         forAll(pe, pointI)
00301         {
00302             const DynamicList<label>& pEdges = pe[pointI];
00303 
00304             nbrPoints.setSize(pEdges.size());
00305 
00306             forAll(pEdges, i)
00307             {
00308                 const edge& e = es[pEdges[i]];
00309 
00310                 label nbrPointI = e.otherVertex(pointI);
00311 
00312                 if (nbrPointI < pointI)
00313                 {
00314                     nbrPoints[i] = -1;
00315                 }
00316                 else
00317                 {
00318                     nbrPoints[i] = nbrPointI;
00319                 }
00320             }
00321             nbrPoints.sort();
00322 
00323 
00324             if (nInternalPoints_ == -1)
00325             {
00326                 // Sort all upper-triangular
00327                 forAll(nbrPoints, i)
00328                 {
00329                     if (nbrPoints[i] != -1)
00330                     {
00331                         label edgeI = pEdges[nbrPoints.indices()[i]];
00332 
00333                         oldToNew[edgeI] = internal0EdgeI++;
00334                     }
00335                 }
00336             }
00337             else
00338             {
00339                 if (pointI < nInternalPoints_)
00340                 {
00341                     forAll(nbrPoints, i)
00342                     {
00343                         label nbrPointI = nbrPoints[i];
00344 
00345                         label edgeI = pEdges[nbrPoints.indices()[i]];
00346 
00347                         if (nbrPointI != -1)
00348                         {
00349                             if (edgeI < nExtEdges)
00350                             {
00351                                 // External edge
00352                                 oldToNew[edgeI] = externalEdgeI++;
00353                             }
00354                             else if (nbrPointI < nInternalPoints_)
00355                             {
00356                                 // Both points inside
00357                                 oldToNew[edgeI] = internal0EdgeI++;
00358                             }
00359                             else
00360                             {
00361                                 // One points inside, one outside
00362                                 oldToNew[edgeI] = internal1EdgeI++;
00363                             }
00364                         }
00365                     }
00366                 }
00367                 else
00368                 {
00369                     forAll(nbrPoints, i)
00370                     {
00371                         label nbrPointI = nbrPoints[i];
00372 
00373                         label edgeI = pEdges[nbrPoints.indices()[i]];
00374 
00375                         if (nbrPointI != -1)
00376                         {
00377                             if (edgeI < nExtEdges)
00378                             {
00379                                 // External edge
00380                                 oldToNew[edgeI] = externalEdgeI++;
00381                             }
00382                             else if (nbrPointI < nInternalPoints_)
00383                             {
00384                                 // Not possible!
00385                                 FatalErrorIn("primitiveMesh::calcEdges(..)")
00386                                     << abort(FatalError);
00387                             }
00388                             else
00389                             {
00390                                 // Both points outside
00391                                 oldToNew[edgeI] = internal2EdgeI++;
00392                             }
00393                         }
00394                     }
00395                 }
00396             }
00397         }
00398 
00399 
00400         if (debug)
00401         {
00402             label edgeI = findIndex(oldToNew, -1);
00403 
00404             if (edgeI != -1)
00405             {
00406                 const edge& e = es[edgeI];
00407 
00408                 FatalErrorIn("primitiveMesh::calcEdges(..)")
00409                     << "Did not sort edge " << edgeI << " points:" << e
00410                     << " coords:" << points()[e[0]] << points()[e[1]]
00411                     << endl
00412                     << "Current buckets:" << endl
00413                     << "    internal0EdgeI:" << internal0EdgeI << endl
00414                     << "    internal1EdgeI:" << internal1EdgeI << endl
00415                     << "    internal2EdgeI:" << internal2EdgeI << endl
00416                     << "    externalEdgeI:" << externalEdgeI << endl
00417                     << abort(FatalError);
00418             }
00419         }
00420 
00421 
00422 
00423         // Renumber and transfer.
00424 
00425         // Edges
00426         edgesPtr_ = new edgeList(es.size());
00427         edgeList& edges = *edgesPtr_;
00428         forAll(es, edgeI)
00429         {
00430             edges[oldToNew[edgeI]] = es[edgeI];
00431         }
00432 
00433         // pointEdges
00434         pePtr_ = new labelListList(nPoints());
00435         labelListList& pointEdges = *pePtr_;
00436         forAll(pe, pointI)
00437         {
00438             DynamicList<label>& pEdges = pe[pointI];
00439             pEdges.shrink();
00440             inplaceRenumber(oldToNew, pEdges);
00441             pointEdges[pointI].transfer(pEdges);
00442             Foam::sort(pointEdges[pointI]);
00443         }
00444 
00445         // faceEdges
00446         if (doFaceEdges)
00447         {
00448             labelListList& faceEdges = *fePtr_;
00449             forAll(faceEdges, faceI)
00450             {
00451                 inplaceRenumber(oldToNew, faceEdges[faceI]);
00452             }
00453         }
00454     }
00455 }
00456 
00457 
00458 Foam::label Foam::primitiveMesh::findFirstCommonElementFromSortedLists
00459 (
00460     const labelList& list1,
00461     const labelList& list2
00462 )
00463 {
00464     label result = -1;
00465 
00466     labelList::const_iterator iter1 = list1.begin();
00467     labelList::const_iterator iter2 = list2.begin();
00468 
00469     while (iter1 != list1.end() && iter2 != list2.end())
00470     {
00471         if( *iter1 < *iter2)
00472         {
00473             ++iter1;
00474         }
00475         else if (*iter1 > *iter2)
00476         {
00477             ++iter2;
00478         }
00479         else
00480         {
00481             result = *iter1;
00482             break;
00483         }
00484     }
00485     if (result == -1)
00486     {
00487         FatalErrorIn
00488         (
00489             "primitiveMesh::findFirstCommonElementFromSortedLists"
00490             "(const labelList&, const labelList&)"
00491         )   << "No common elements in lists " << list1 << " and " << list2
00492             << abort(FatalError);
00493     }
00494     return result;
00495 }
00496 
00497 
00498 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00499 
00500 const Foam::edgeList& Foam::primitiveMesh::edges() const
00501 {
00502     if (!edgesPtr_)
00503     {
00504         //calcEdges(true);
00505         calcEdges(false);
00506     }
00507 
00508     return *edgesPtr_;
00509 }
00510 
00511 const Foam::labelListList& Foam::primitiveMesh::pointEdges() const
00512 {
00513     if (!pePtr_)
00514     {
00515         //calcEdges(true);
00516         calcEdges(false);
00517     }
00518 
00519     return *pePtr_;
00520 }
00521 
00522 
00523 const Foam::labelListList& Foam::primitiveMesh::faceEdges() const
00524 {
00525     if (!fePtr_)
00526     {
00527         if (debug)
00528         {
00529             Pout<< "primitiveMesh::faceEdges() : "
00530                 << "calculating faceEdges" << endl;
00531         }
00532 
00533         //calcEdges(true);
00534         const faceList& fcs = faces();
00535         const labelListList& pe = pointEdges();
00536         const edgeList& es = edges();
00537 
00538         fePtr_ = new labelListList(fcs.size());
00539         labelListList& faceEdges = *fePtr_;
00540 
00541         forAll(fcs, faceI)
00542         {
00543             const face& f = fcs[faceI];
00544 
00545             labelList& fEdges = faceEdges[faceI];
00546             fEdges.setSize(f.size());
00547 
00548             forAll(f, fp)
00549             {
00550                 label pointI = f[fp];
00551                 label nextPointI = f[f.fcIndex(fp)];
00552 
00553                 // Find edge between pointI, nextPontI
00554                 const labelList& pEdges = pe[pointI];
00555 
00556                 forAll(pEdges, i)
00557                 {
00558                     label edgeI = pEdges[i];
00559 
00560                     if (es[edgeI].otherVertex(pointI) == nextPointI)
00561                     {
00562                         fEdges[fp] = edgeI;
00563                         break;
00564                     }
00565                 }
00566             }
00567         }
00568     }
00569 
00570     return *fePtr_;
00571 }
00572 
00573 
00574 void Foam::primitiveMesh::clearOutEdges()
00575 {
00576     deleteDemandDrivenData(edgesPtr_);
00577     deleteDemandDrivenData(pePtr_);
00578     deleteDemandDrivenData(fePtr_);
00579     labels_.clear();
00580     labelSet_.clear();
00581 }
00582 
00583 
00584 const Foam::labelList& Foam::primitiveMesh::faceEdges
00585 (
00586     const label faceI,
00587     DynamicList<label>& storage
00588 ) const
00589 {
00590     if (hasFaceEdges())
00591     {
00592         return faceEdges()[faceI];
00593     }
00594     else
00595     {
00596         const labelListList& pointEs = pointEdges();
00597         const face& f = faces()[faceI];
00598 
00599         storage.clear();
00600         if (f.size() > storage.capacity())
00601         {
00602             storage.setCapacity(f.size());
00603         }
00604 
00605         forAll(f, fp)
00606         {
00607             storage.append
00608             (
00609                 findFirstCommonElementFromSortedLists
00610                 (
00611                     pointEs[f[fp]],
00612                     pointEs[f.nextLabel(fp)]
00613                 )
00614             );
00615         }
00616 
00617         return storage;
00618     }
00619 }
00620 
00621 
00622 const Foam::labelList& Foam::primitiveMesh::faceEdges(const label faceI) const
00623 {
00624     return faceEdges(faceI, labels_);
00625 }
00626 
00627 
00628 const Foam::labelList& Foam::primitiveMesh::cellEdges
00629 (
00630     const label cellI,
00631     DynamicList<label>& storage
00632 ) const
00633 {
00634     if (hasCellEdges())
00635     {
00636         return cellEdges()[cellI];
00637     }
00638     else
00639     {
00640         const labelList& cFaces = cells()[cellI];
00641 
00642         labelSet_.clear();
00643 
00644         forAll(cFaces, i)
00645         {
00646             const labelList& fe = faceEdges(cFaces[i]);
00647 
00648             forAll(fe, feI)
00649             {
00650                 labelSet_.insert(fe[feI]);
00651             }
00652         }
00653 
00654         storage.clear();
00655 
00656         if (labelSet_.size() > storage.capacity())
00657         {
00658             storage.setCapacity(labelSet_.size());
00659         }
00660 
00661         forAllConstIter(labelHashSet, labelSet_, iter)
00662         {
00663             storage.append(iter.key());
00664         }
00665 
00666         return storage;
00667     }
00668 }
00669 
00670 
00671 const Foam::labelList& Foam::primitiveMesh::cellEdges(const label cellI) const
00672 {
00673     return cellEdges(cellI, labels_);
00674 }
00675 
00676 
00677 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00678 
00679 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd